Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Daniel Vetter |
Ben Widawsky | c4ac524 | 2014-02-19 22:05:47 -0800 | [diff] [blame] | 3 | * Copyright © 2011-2014 Intel Corporation |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the "Software"), |
| 7 | * to deal in the Software without restriction, including without limitation |
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 | * and/or sell copies of the Software, and to permit persons to whom the |
| 10 | * Software is furnished to do so, subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the next |
| 13 | * paragraph) shall be included in all copies or substantial portions of the |
| 14 | * Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 | * IN THE SOFTWARE. |
| 23 | * |
| 24 | */ |
| 25 | |
Daniel Vetter | 0e46ce2 | 2014-01-08 16:10:27 +0100 | [diff] [blame] | 26 | #include <linux/seq_file.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 27 | #include <drm/drmP.h> |
| 28 | #include <drm/i915_drm.h> |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 29 | #include "i915_drv.h" |
Yu Zhang | 5dda8fa | 2015-02-10 19:05:48 +0800 | [diff] [blame] | 30 | #include "i915_vgpu.h" |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 31 | #include "i915_trace.h" |
| 32 | #include "intel_drv.h" |
| 33 | |
Tvrtko Ursulin | 45f8f69 | 2014-12-10 17:27:59 +0000 | [diff] [blame] | 34 | /** |
| 35 | * DOC: Global GTT views |
| 36 | * |
| 37 | * Background and previous state |
| 38 | * |
| 39 | * Historically objects could exists (be bound) in global GTT space only as |
| 40 | * singular instances with a view representing all of the object's backing pages |
| 41 | * in a linear fashion. This view will be called a normal view. |
| 42 | * |
| 43 | * To support multiple views of the same object, where the number of mapped |
| 44 | * pages is not equal to the backing store, or where the layout of the pages |
| 45 | * is not linear, concept of a GGTT view was added. |
| 46 | * |
| 47 | * One example of an alternative view is a stereo display driven by a single |
| 48 | * image. In this case we would have a framebuffer looking like this |
| 49 | * (2x2 pages): |
| 50 | * |
| 51 | * 12 |
| 52 | * 34 |
| 53 | * |
| 54 | * Above would represent a normal GGTT view as normally mapped for GPU or CPU |
| 55 | * rendering. In contrast, fed to the display engine would be an alternative |
| 56 | * view which could look something like this: |
| 57 | * |
| 58 | * 1212 |
| 59 | * 3434 |
| 60 | * |
| 61 | * In this example both the size and layout of pages in the alternative view is |
| 62 | * different from the normal view. |
| 63 | * |
| 64 | * Implementation and usage |
| 65 | * |
| 66 | * GGTT views are implemented using VMAs and are distinguished via enum |
| 67 | * i915_ggtt_view_type and struct i915_ggtt_view. |
| 68 | * |
| 69 | * A new flavour of core GEM functions which work with GGTT bound objects were |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 70 | * added with the _ggtt_ infix, and sometimes with _view postfix to avoid |
| 71 | * renaming in large amounts of code. They take the struct i915_ggtt_view |
| 72 | * parameter encapsulating all metadata required to implement a view. |
Tvrtko Ursulin | 45f8f69 | 2014-12-10 17:27:59 +0000 | [diff] [blame] | 73 | * |
| 74 | * As a helper for callers which are only interested in the normal view, |
| 75 | * globally const i915_ggtt_view_normal singleton instance exists. All old core |
| 76 | * GEM API functions, the ones not taking the view parameter, are operating on, |
| 77 | * or with the normal GGTT view. |
| 78 | * |
| 79 | * Code wanting to add or use a new GGTT view needs to: |
| 80 | * |
| 81 | * 1. Add a new enum with a suitable name. |
| 82 | * 2. Extend the metadata in the i915_ggtt_view structure if required. |
| 83 | * 3. Add support to i915_get_vma_pages(). |
| 84 | * |
| 85 | * New views are required to build a scatter-gather table from within the |
| 86 | * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and |
| 87 | * exists for the lifetime of an VMA. |
| 88 | * |
| 89 | * Core API is designed to have copy semantics which means that passed in |
| 90 | * struct i915_ggtt_view does not need to be persistent (left around after |
| 91 | * calling the core API functions). |
| 92 | * |
| 93 | */ |
| 94 | |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 95 | const struct i915_ggtt_view i915_ggtt_view_normal; |
| 96 | |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 97 | static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv); |
| 98 | static void chv_setup_private_ppat(struct drm_i915_private *dev_priv); |
Ben Widawsky | a2319c0 | 2014-03-18 16:09:37 -0700 | [diff] [blame] | 99 | |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 100 | static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt) |
| 101 | { |
Chris Wilson | 1893a71 | 2014-09-19 11:56:27 +0100 | [diff] [blame] | 102 | bool has_aliasing_ppgtt; |
| 103 | bool has_full_ppgtt; |
| 104 | |
| 105 | has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6; |
| 106 | has_full_ppgtt = INTEL_INFO(dev)->gen >= 7; |
Chris Wilson | 1893a71 | 2014-09-19 11:56:27 +0100 | [diff] [blame] | 107 | |
Yu Zhang | 71ba2d6 | 2015-02-10 19:05:54 +0800 | [diff] [blame] | 108 | if (intel_vgpu_active(dev)) |
| 109 | has_full_ppgtt = false; /* emulation is too hard */ |
| 110 | |
Damien Lespiau | 70ee45e | 2014-11-14 15:05:59 +0000 | [diff] [blame] | 111 | /* |
| 112 | * We don't allow disabling PPGTT for gen9+ as it's a requirement for |
| 113 | * execlists, the sole mechanism available to submit work. |
| 114 | */ |
| 115 | if (INTEL_INFO(dev)->gen < 9 && |
| 116 | (enable_ppgtt == 0 || !has_aliasing_ppgtt)) |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | |
| 119 | if (enable_ppgtt == 1) |
| 120 | return 1; |
| 121 | |
Chris Wilson | 1893a71 | 2014-09-19 11:56:27 +0100 | [diff] [blame] | 122 | if (enable_ppgtt == 2 && has_full_ppgtt) |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 123 | return 2; |
| 124 | |
Daniel Vetter | 93a25a9 | 2014-03-06 09:40:43 +0100 | [diff] [blame] | 125 | #ifdef CONFIG_INTEL_IOMMU |
| 126 | /* Disable ppgtt on SNB if VT-d is on. */ |
| 127 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) { |
| 128 | DRM_INFO("Disabling PPGTT because VT-d is on\n"); |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 129 | return 0; |
Daniel Vetter | 93a25a9 | 2014-03-06 09:40:43 +0100 | [diff] [blame] | 130 | } |
| 131 | #endif |
| 132 | |
Jesse Barnes | 62942ed | 2014-06-13 09:28:33 -0700 | [diff] [blame] | 133 | /* Early VLV doesn't have this */ |
Ville Syrjälä | ca2aed6c | 2014-06-28 02:03:56 +0300 | [diff] [blame] | 134 | if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && |
| 135 | dev->pdev->revision < 0xb) { |
Jesse Barnes | 62942ed | 2014-06-13 09:28:33 -0700 | [diff] [blame] | 136 | DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n"); |
| 137 | return 0; |
| 138 | } |
| 139 | |
Michel Thierry | 2f82bbd | 2014-12-15 14:58:00 +0000 | [diff] [blame] | 140 | if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists) |
| 141 | return 2; |
| 142 | else |
| 143 | return has_aliasing_ppgtt ? 1 : 0; |
Daniel Vetter | 93a25a9 | 2014-03-06 09:40:43 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 146 | static void ppgtt_bind_vma(struct i915_vma *vma, |
| 147 | enum i915_cache_level cache_level, |
| 148 | u32 flags); |
| 149 | static void ppgtt_unbind_vma(struct i915_vma *vma); |
| 150 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 151 | static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr, |
| 152 | enum i915_cache_level level, |
| 153 | bool valid) |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 154 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 155 | gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0; |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 156 | pte |= addr; |
Ben Widawsky | 63c42e5 | 2014-04-18 18:04:27 -0300 | [diff] [blame] | 157 | |
| 158 | switch (level) { |
| 159 | case I915_CACHE_NONE: |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 160 | pte |= PPAT_UNCACHED_INDEX; |
Ben Widawsky | 63c42e5 | 2014-04-18 18:04:27 -0300 | [diff] [blame] | 161 | break; |
| 162 | case I915_CACHE_WT: |
| 163 | pte |= PPAT_DISPLAY_ELLC_INDEX; |
| 164 | break; |
| 165 | default: |
| 166 | pte |= PPAT_CACHED_INDEX; |
| 167 | break; |
| 168 | } |
| 169 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 170 | return pte; |
| 171 | } |
| 172 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 173 | static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev, |
| 174 | dma_addr_t addr, |
| 175 | enum i915_cache_level level) |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 176 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 177 | gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW; |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 178 | pde |= addr; |
| 179 | if (level != I915_CACHE_NONE) |
| 180 | pde |= PPAT_CACHED_PDE_INDEX; |
| 181 | else |
| 182 | pde |= PPAT_UNCACHED_INDEX; |
| 183 | return pde; |
| 184 | } |
| 185 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 186 | static gen6_pte_t snb_pte_encode(dma_addr_t addr, |
| 187 | enum i915_cache_level level, |
| 188 | bool valid, u32 unused) |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 189 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 190 | gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 191 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 192 | |
| 193 | switch (level) { |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 194 | case I915_CACHE_L3_LLC: |
| 195 | case I915_CACHE_LLC: |
| 196 | pte |= GEN6_PTE_CACHE_LLC; |
| 197 | break; |
| 198 | case I915_CACHE_NONE: |
| 199 | pte |= GEN6_PTE_UNCACHED; |
| 200 | break; |
| 201 | default: |
Daniel Vetter | 5f77eeb | 2014-12-08 16:40:10 +0100 | [diff] [blame] | 202 | MISSING_CASE(level); |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return pte; |
| 206 | } |
| 207 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 208 | static gen6_pte_t ivb_pte_encode(dma_addr_t addr, |
| 209 | enum i915_cache_level level, |
| 210 | bool valid, u32 unused) |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 211 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 212 | gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 213 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 214 | |
| 215 | switch (level) { |
| 216 | case I915_CACHE_L3_LLC: |
| 217 | pte |= GEN7_PTE_CACHE_L3_LLC; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 218 | break; |
| 219 | case I915_CACHE_LLC: |
| 220 | pte |= GEN6_PTE_CACHE_LLC; |
| 221 | break; |
| 222 | case I915_CACHE_NONE: |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 223 | pte |= GEN6_PTE_UNCACHED; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 224 | break; |
| 225 | default: |
Daniel Vetter | 5f77eeb | 2014-12-08 16:40:10 +0100 | [diff] [blame] | 226 | MISSING_CASE(level); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 229 | return pte; |
| 230 | } |
| 231 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 232 | static gen6_pte_t byt_pte_encode(dma_addr_t addr, |
| 233 | enum i915_cache_level level, |
| 234 | bool valid, u32 flags) |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 235 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 236 | gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 237 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 238 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 239 | if (!(flags & PTE_READ_ONLY)) |
| 240 | pte |= BYT_PTE_WRITEABLE; |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 241 | |
| 242 | if (level != I915_CACHE_NONE) |
| 243 | pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; |
| 244 | |
| 245 | return pte; |
| 246 | } |
| 247 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 248 | static gen6_pte_t hsw_pte_encode(dma_addr_t addr, |
| 249 | enum i915_cache_level level, |
| 250 | bool valid, u32 unused) |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 251 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 252 | gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 253 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 254 | |
| 255 | if (level != I915_CACHE_NONE) |
Ben Widawsky | 87a6b68 | 2013-08-04 23:47:29 -0700 | [diff] [blame] | 256 | pte |= HSW_WB_LLC_AGE3; |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 257 | |
| 258 | return pte; |
| 259 | } |
| 260 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 261 | static gen6_pte_t iris_pte_encode(dma_addr_t addr, |
| 262 | enum i915_cache_level level, |
| 263 | bool valid, u32 unused) |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 264 | { |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 265 | gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 266 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
| 267 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 268 | switch (level) { |
| 269 | case I915_CACHE_NONE: |
| 270 | break; |
| 271 | case I915_CACHE_WT: |
Chris Wilson | c51e970 | 2013-11-22 10:37:53 +0000 | [diff] [blame] | 272 | pte |= HSW_WT_ELLC_LLC_AGE3; |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 273 | break; |
| 274 | default: |
Chris Wilson | c51e970 | 2013-11-22 10:37:53 +0000 | [diff] [blame] | 275 | pte |= HSW_WB_ELLC_LLC_AGE3; |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 276 | break; |
| 277 | } |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 278 | |
| 279 | return pte; |
| 280 | } |
| 281 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 282 | #define i915_dma_unmap_single(px, dev) \ |
| 283 | __i915_dma_unmap_single((px)->daddr, dev) |
| 284 | |
| 285 | static inline void __i915_dma_unmap_single(dma_addr_t daddr, |
| 286 | struct drm_device *dev) |
| 287 | { |
| 288 | struct device *device = &dev->pdev->dev; |
| 289 | |
| 290 | dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc. |
| 295 | * @px: Page table/dir/etc to get a DMA map for |
| 296 | * @dev: drm device |
| 297 | * |
| 298 | * Page table allocations are unified across all gens. They always require a |
| 299 | * single 4k allocation, as well as a DMA mapping. If we keep the structs |
| 300 | * symmetric here, the simple macro covers us for every page table type. |
| 301 | * |
| 302 | * Return: 0 if success. |
| 303 | */ |
| 304 | #define i915_dma_map_single(px, dev) \ |
| 305 | i915_dma_map_page_single((px)->page, (dev), &(px)->daddr) |
| 306 | |
| 307 | static inline int i915_dma_map_page_single(struct page *page, |
| 308 | struct drm_device *dev, |
| 309 | dma_addr_t *daddr) |
| 310 | { |
| 311 | struct device *device = &dev->pdev->dev; |
| 312 | |
| 313 | *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL); |
| 314 | return dma_mapping_error(device, *daddr); |
| 315 | } |
| 316 | |
| 317 | static void unmap_and_free_pt(struct i915_page_table_entry *pt, |
| 318 | struct drm_device *dev) |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 319 | { |
| 320 | if (WARN_ON(!pt->page)) |
| 321 | return; |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 322 | |
| 323 | i915_dma_unmap_single(pt, dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 324 | __free_page(pt->page); |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 325 | kfree(pt->used_ptes); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 326 | kfree(pt); |
| 327 | } |
| 328 | |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 329 | static struct i915_page_table_entry *alloc_pt_single(struct drm_device *dev) |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 330 | { |
| 331 | struct i915_page_table_entry *pt; |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 332 | const size_t count = INTEL_INFO(dev)->gen >= 8 ? |
| 333 | GEN8_PTES : GEN6_PTES; |
| 334 | int ret = -ENOMEM; |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 335 | |
| 336 | pt = kzalloc(sizeof(*pt), GFP_KERNEL); |
| 337 | if (!pt) |
| 338 | return ERR_PTR(-ENOMEM); |
| 339 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 340 | pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes), |
| 341 | GFP_KERNEL); |
| 342 | |
| 343 | if (!pt->used_ptes) |
| 344 | goto fail_bitmap; |
| 345 | |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 346 | pt->page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 347 | if (!pt->page) |
| 348 | goto fail_page; |
| 349 | |
| 350 | ret = i915_dma_map_single(pt, dev); |
| 351 | if (ret) |
| 352 | goto fail_dma; |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 353 | |
| 354 | return pt; |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 355 | |
| 356 | fail_dma: |
| 357 | __free_page(pt->page); |
| 358 | fail_page: |
| 359 | kfree(pt->used_ptes); |
| 360 | fail_bitmap: |
| 361 | kfree(pt); |
| 362 | |
| 363 | return ERR_PTR(ret); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /** |
| 367 | * alloc_pt_range() - Allocate a multiple page tables |
| 368 | * @pd: The page directory which will have at least @count entries |
| 369 | * available to point to the allocated page tables. |
| 370 | * @pde: First page directory entry for which we are allocating. |
| 371 | * @count: Number of pages to allocate. |
Michel Thierry | 719cd21 | 2015-02-26 11:28:13 +0000 | [diff] [blame] | 372 | * @dev: DRM device. |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 373 | * |
| 374 | * Allocates multiple page table pages and sets the appropriate entries in the |
| 375 | * page table structure within the page directory. Function cleans up after |
| 376 | * itself on any failures. |
| 377 | * |
| 378 | * Return: 0 if allocation succeeded. |
| 379 | */ |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 380 | static int alloc_pt_range(struct i915_page_directory_entry *pd, uint16_t pde, size_t count, |
| 381 | struct drm_device *dev) |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 382 | { |
| 383 | int i, ret; |
| 384 | |
| 385 | /* 512 is the max page tables per page_directory on any platform. */ |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 386 | if (WARN_ON(pde + count > I915_PDES)) |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 387 | return -EINVAL; |
| 388 | |
| 389 | for (i = pde; i < pde + count; i++) { |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 390 | struct i915_page_table_entry *pt = alloc_pt_single(dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 391 | |
| 392 | if (IS_ERR(pt)) { |
| 393 | ret = PTR_ERR(pt); |
| 394 | goto err_out; |
| 395 | } |
| 396 | WARN(pd->page_table[i], |
Dan Carpenter | 686135d | 2015-02-26 19:53:54 +0300 | [diff] [blame] | 397 | "Leaking page directory entry %d (%p)\n", |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 398 | i, pd->page_table[i]); |
| 399 | pd->page_table[i] = pt; |
| 400 | } |
| 401 | |
| 402 | return 0; |
| 403 | |
| 404 | err_out: |
| 405 | while (i-- > pde) |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 406 | unmap_and_free_pt(pd->page_table[i], dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | static void unmap_and_free_pd(struct i915_page_directory_entry *pd) |
| 411 | { |
| 412 | if (pd->page) { |
| 413 | __free_page(pd->page); |
| 414 | kfree(pd); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | static struct i915_page_directory_entry *alloc_pd_single(void) |
| 419 | { |
| 420 | struct i915_page_directory_entry *pd; |
| 421 | |
| 422 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 423 | if (!pd) |
| 424 | return ERR_PTR(-ENOMEM); |
| 425 | |
| 426 | pd->page = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 427 | if (!pd->page) { |
| 428 | kfree(pd); |
| 429 | return ERR_PTR(-ENOMEM); |
| 430 | } |
| 431 | |
| 432 | return pd; |
| 433 | } |
| 434 | |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 435 | /* Broadwell Page Directory Pointer Descriptors */ |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 436 | static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 437 | uint64_t val) |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 438 | { |
| 439 | int ret; |
| 440 | |
| 441 | BUG_ON(entry >= 4); |
| 442 | |
| 443 | ret = intel_ring_begin(ring, 6); |
| 444 | if (ret) |
| 445 | return ret; |
| 446 | |
| 447 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 448 | intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry)); |
| 449 | intel_ring_emit(ring, (u32)(val >> 32)); |
| 450 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 451 | intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry)); |
| 452 | intel_ring_emit(ring, (u32)(val)); |
| 453 | intel_ring_advance(ring); |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 458 | static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 459 | struct intel_engine_cs *ring) |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 460 | { |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 461 | int i, ret; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 462 | |
| 463 | /* bit of a hack to find the actual last used pd */ |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 464 | int used_pd = ppgtt->num_pd_entries / I915_PDES; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 465 | |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 466 | for (i = used_pd - 1; i >= 0; i--) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 467 | dma_addr_t addr = ppgtt->pdp.page_directory[i]->daddr; |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 468 | ret = gen8_write_pdp(ring, i, addr); |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 469 | if (ret) |
| 470 | return ret; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 471 | } |
Ben Widawsky | d595bd4 | 2013-11-25 09:54:32 -0800 | [diff] [blame] | 472 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 473 | return 0; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 474 | } |
| 475 | |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 476 | static void gen8_ppgtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 477 | uint64_t start, |
| 478 | uint64_t length, |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 479 | bool use_scratch) |
| 480 | { |
| 481 | struct i915_hw_ppgtt *ppgtt = |
| 482 | container_of(vm, struct i915_hw_ppgtt, base); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 483 | gen8_pte_t *pt_vaddr, scratch_pte; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 484 | unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK; |
| 485 | unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK; |
| 486 | unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 487 | unsigned num_entries = length >> PAGE_SHIFT; |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 488 | unsigned last_pte, i; |
| 489 | |
| 490 | scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr, |
| 491 | I915_CACHE_LLC, use_scratch); |
| 492 | |
| 493 | while (num_entries) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 494 | struct i915_page_directory_entry *pd; |
| 495 | struct i915_page_table_entry *pt; |
| 496 | struct page *page_table; |
| 497 | |
| 498 | if (WARN_ON(!ppgtt->pdp.page_directory[pdpe])) |
| 499 | continue; |
| 500 | |
| 501 | pd = ppgtt->pdp.page_directory[pdpe]; |
| 502 | |
| 503 | if (WARN_ON(!pd->page_table[pde])) |
| 504 | continue; |
| 505 | |
| 506 | pt = pd->page_table[pde]; |
| 507 | |
| 508 | if (WARN_ON(!pt->page)) |
| 509 | continue; |
| 510 | |
| 511 | page_table = pt->page; |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 512 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 513 | last_pte = pte + num_entries; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 514 | if (last_pte > GEN8_PTES) |
| 515 | last_pte = GEN8_PTES; |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 516 | |
| 517 | pt_vaddr = kmap_atomic(page_table); |
| 518 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 519 | for (i = pte; i < last_pte; i++) { |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 520 | pt_vaddr[i] = scratch_pte; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 521 | num_entries--; |
| 522 | } |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 523 | |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 524 | if (!HAS_LLC(ppgtt->base.dev)) |
| 525 | drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 526 | kunmap_atomic(pt_vaddr); |
| 527 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 528 | pte = 0; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 529 | if (++pde == I915_PDES) { |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 530 | pdpe++; |
| 531 | pde = 0; |
| 532 | } |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 536 | static void gen8_ppgtt_insert_entries(struct i915_address_space *vm, |
| 537 | struct sg_table *pages, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 538 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 539 | enum i915_cache_level cache_level, u32 unused) |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 540 | { |
| 541 | struct i915_hw_ppgtt *ppgtt = |
| 542 | container_of(vm, struct i915_hw_ppgtt, base); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 543 | gen8_pte_t *pt_vaddr; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 544 | unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK; |
| 545 | unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK; |
| 546 | unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK; |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 547 | struct sg_page_iter sg_iter; |
| 548 | |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 549 | pt_vaddr = NULL; |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 550 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 551 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
Ben Widawsky | 7664360 | 2015-01-22 17:01:24 +0000 | [diff] [blame] | 552 | if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES)) |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 553 | break; |
| 554 | |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 555 | if (pt_vaddr == NULL) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 556 | struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[pdpe]; |
| 557 | struct i915_page_table_entry *pt = pd->page_table[pde]; |
| 558 | struct page *page_table = pt->page; |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 559 | |
| 560 | pt_vaddr = kmap_atomic(page_table); |
| 561 | } |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 562 | |
| 563 | pt_vaddr[pte] = |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 564 | gen8_pte_encode(sg_page_iter_dma_address(&sg_iter), |
| 565 | cache_level, true); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 566 | if (++pte == GEN8_PTES) { |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 567 | if (!HAS_LLC(ppgtt->base.dev)) |
| 568 | drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 569 | kunmap_atomic(pt_vaddr); |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 570 | pt_vaddr = NULL; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 571 | if (++pde == I915_PDES) { |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 572 | pdpe++; |
| 573 | pde = 0; |
| 574 | } |
| 575 | pte = 0; |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 578 | if (pt_vaddr) { |
| 579 | if (!HAS_LLC(ppgtt->base.dev)) |
| 580 | drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 581 | kunmap_atomic(pt_vaddr); |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 582 | } |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 585 | static void gen8_free_page_tables(struct i915_page_directory_entry *pd, struct drm_device *dev) |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 586 | { |
| 587 | int i; |
| 588 | |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 589 | if (!pd->page) |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 590 | return; |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 591 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 592 | for (i = 0; i < I915_PDES; i++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 593 | if (WARN_ON(!pd->page_table[i])) |
| 594 | continue; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 595 | |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 596 | unmap_and_free_pt(pd->page_table[i], dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 597 | pd->page_table[i] = NULL; |
| 598 | } |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt) |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 602 | { |
| 603 | int i; |
| 604 | |
| 605 | for (i = 0; i < ppgtt->num_pd_pages; i++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 606 | if (WARN_ON(!ppgtt->pdp.page_directory[i])) |
| 607 | continue; |
| 608 | |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 609 | gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 610 | unmap_and_free_pd(ppgtt->pdp.page_directory[i]); |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 611 | } |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt) |
| 615 | { |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 616 | struct pci_dev *hwdev = ppgtt->base.dev->pdev; |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 617 | int i, j; |
| 618 | |
| 619 | for (i = 0; i < ppgtt->num_pd_pages; i++) { |
| 620 | /* TODO: In the future we'll support sparse mappings, so this |
| 621 | * will have to change. */ |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 622 | if (!ppgtt->pdp.page_directory[i]->daddr) |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 623 | continue; |
| 624 | |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 625 | pci_unmap_page(hwdev, ppgtt->pdp.page_directory[i]->daddr, PAGE_SIZE, |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 626 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 627 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 628 | for (j = 0; j < I915_PDES; j++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 629 | struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i]; |
| 630 | struct i915_page_table_entry *pt; |
| 631 | dma_addr_t addr; |
| 632 | |
| 633 | if (WARN_ON(!pd->page_table[j])) |
| 634 | continue; |
| 635 | |
| 636 | pt = pd->page_table[j]; |
| 637 | addr = pt->daddr; |
| 638 | |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 639 | if (addr) |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 640 | pci_unmap_page(hwdev, addr, PAGE_SIZE, |
| 641 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 646 | static void gen8_ppgtt_cleanup(struct i915_address_space *vm) |
| 647 | { |
| 648 | struct i915_hw_ppgtt *ppgtt = |
| 649 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 650 | |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 651 | gen8_ppgtt_unmap_pages(ppgtt); |
| 652 | gen8_ppgtt_free(ppgtt); |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 655 | static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt) |
| 656 | { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 657 | int i, ret; |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 658 | |
| 659 | for (i = 0; i < ppgtt->num_pd_pages; i++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 660 | ret = alloc_pt_range(ppgtt->pdp.page_directory[i], |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 661 | 0, I915_PDES, ppgtt->base.dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 662 | if (ret) |
| 663 | goto unwind_out; |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | |
| 668 | unwind_out: |
| 669 | while (i--) |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 670 | gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev); |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 671 | |
| 672 | return -ENOMEM; |
| 673 | } |
| 674 | |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 675 | static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt, |
| 676 | const int max_pdp) |
| 677 | { |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 678 | int i; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 679 | |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 680 | for (i = 0; i < max_pdp; i++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 681 | ppgtt->pdp.page_directory[i] = alloc_pd_single(); |
| 682 | if (IS_ERR(ppgtt->pdp.page_directory[i])) |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 683 | goto unwind_out; |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | ppgtt->num_pd_pages = max_pdp; |
Ben Widawsky | 7664360 | 2015-01-22 17:01:24 +0000 | [diff] [blame] | 687 | BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPES); |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 688 | |
| 689 | return 0; |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 690 | |
| 691 | unwind_out: |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 692 | while (i--) |
| 693 | unmap_and_free_pd(ppgtt->pdp.page_directory[i]); |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 694 | |
| 695 | return -ENOMEM; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt, |
| 699 | const int max_pdp) |
| 700 | { |
| 701 | int ret; |
| 702 | |
| 703 | ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp); |
| 704 | if (ret) |
| 705 | return ret; |
| 706 | |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 707 | ret = gen8_ppgtt_allocate_page_tables(ppgtt); |
| 708 | if (ret) |
| 709 | goto err_out; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 710 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 711 | ppgtt->num_pd_entries = max_pdp * I915_PDES; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 712 | |
Ben Widawsky | d7b3de9 | 2015-02-24 16:22:34 +0000 | [diff] [blame] | 713 | return 0; |
| 714 | |
| 715 | err_out: |
| 716 | gen8_ppgtt_free(ppgtt); |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 717 | return ret; |
| 718 | } |
| 719 | |
| 720 | static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt, |
| 721 | const int pd) |
| 722 | { |
| 723 | dma_addr_t pd_addr; |
| 724 | int ret; |
| 725 | |
| 726 | pd_addr = pci_map_page(ppgtt->base.dev->pdev, |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 727 | ppgtt->pdp.page_directory[pd]->page, 0, |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 728 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 729 | |
| 730 | ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr); |
| 731 | if (ret) |
| 732 | return ret; |
| 733 | |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 734 | ppgtt->pdp.page_directory[pd]->daddr = pd_addr; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt, |
| 740 | const int pd, |
| 741 | const int pt) |
| 742 | { |
| 743 | dma_addr_t pt_addr; |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 744 | struct i915_page_directory_entry *pdir = ppgtt->pdp.page_directory[pd]; |
| 745 | struct i915_page_table_entry *ptab = pdir->page_table[pt]; |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 746 | struct page *p = ptab->page; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 747 | int ret; |
| 748 | |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 749 | pt_addr = pci_map_page(ppgtt->base.dev->pdev, |
| 750 | p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 751 | ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 755 | ptab->daddr = pt_addr; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
Daniel Vetter | eb0b44a | 2015-03-18 14:47:59 +0100 | [diff] [blame] | 760 | /* |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 761 | * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers |
| 762 | * with a net effect resembling a 2-level page table in normal x86 terms. Each |
| 763 | * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address |
| 764 | * space. |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 765 | * |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 766 | * FIXME: split allocation into smaller pieces. For now we only ever do this |
| 767 | * once, but with full PPGTT, the multiple contiguous allocations will be bad. |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 768 | * TODO: Do something with the size parameter |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 769 | */ |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 770 | static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size) |
| 771 | { |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 772 | const int max_pdp = DIV_ROUND_UP(size, 1 << 30); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 773 | const int min_pt_pages = I915_PDES * max_pdp; |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 774 | int i, j, ret; |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 775 | |
| 776 | if (size % (1<<30)) |
| 777 | DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size); |
| 778 | |
Mika Kuoppala | 2934368 | 2015-03-04 14:55:17 +0200 | [diff] [blame] | 779 | /* 1. Do all our allocations for page directories and page tables. |
| 780 | * We allocate more than was asked so that we can point the unused parts |
| 781 | * to valid entries that point to scratch page. Dynamic page tables |
| 782 | * will fix this eventually. |
| 783 | */ |
| 784 | ret = gen8_ppgtt_alloc(ppgtt, GEN8_LEGACY_PDPES); |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 785 | if (ret) |
| 786 | return ret; |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 787 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 788 | /* |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 789 | * 2. Create DMA mappings for the page directories and page tables. |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 790 | */ |
Mika Kuoppala | 2934368 | 2015-03-04 14:55:17 +0200 | [diff] [blame] | 791 | for (i = 0; i < GEN8_LEGACY_PDPES; i++) { |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 792 | ret = gen8_ppgtt_setup_page_directories(ppgtt, i); |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 793 | if (ret) |
| 794 | goto bail; |
| 795 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 796 | for (j = 0; j < I915_PDES; j++) { |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 797 | ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j); |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 798 | if (ret) |
| 799 | goto bail; |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * 3. Map all the page directory entires to point to the page tables |
| 805 | * we've allocated. |
| 806 | * |
| 807 | * For now, the PPGTT helper functions all require that the PDEs are |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 808 | * plugged in correctly. So we do that now/here. For aliasing PPGTT, we |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 809 | * will never need to touch the PDEs again. |
| 810 | */ |
Mika Kuoppala | 2934368 | 2015-03-04 14:55:17 +0200 | [diff] [blame] | 811 | for (i = 0; i < GEN8_LEGACY_PDPES; i++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 812 | struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i]; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 813 | gen8_pde_t *pd_vaddr; |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 814 | pd_vaddr = kmap_atomic(ppgtt->pdp.page_directory[i]->page); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 815 | for (j = 0; j < I915_PDES; j++) { |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 816 | struct i915_page_table_entry *pt = pd->page_table[j]; |
| 817 | dma_addr_t addr = pt->daddr; |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 818 | pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr, |
| 819 | I915_CACHE_LLC); |
| 820 | } |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 821 | if (!HAS_LLC(ppgtt->base.dev)) |
| 822 | drm_clflush_virt_range(pd_vaddr, PAGE_SIZE); |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 823 | kunmap_atomic(pd_vaddr); |
| 824 | } |
| 825 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 826 | ppgtt->switch_mm = gen8_mm_switch; |
| 827 | ppgtt->base.clear_range = gen8_ppgtt_clear_range; |
| 828 | ppgtt->base.insert_entries = gen8_ppgtt_insert_entries; |
| 829 | ppgtt->base.cleanup = gen8_ppgtt_cleanup; |
| 830 | ppgtt->base.start = 0; |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 831 | |
Mika Kuoppala | 2934368 | 2015-03-04 14:55:17 +0200 | [diff] [blame] | 832 | /* This is the area that we advertise as usable for the caller */ |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 833 | ppgtt->base.total = max_pdp * I915_PDES * GEN8_PTES * PAGE_SIZE; |
Mika Kuoppala | 2934368 | 2015-03-04 14:55:17 +0200 | [diff] [blame] | 834 | |
| 835 | /* Set all ptes to a valid scratch page. Also above requested space */ |
| 836 | ppgtt->base.clear_range(&ppgtt->base, 0, |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 837 | ppgtt->num_pd_pages * GEN8_PTES * PAGE_SIZE, |
Mika Kuoppala | 2934368 | 2015-03-04 14:55:17 +0200 | [diff] [blame] | 838 | true); |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 839 | |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 840 | DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n", |
| 841 | ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp); |
| 842 | DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n", |
Ben Widawsky | 5abbcca | 2014-02-21 13:06:34 -0800 | [diff] [blame] | 843 | ppgtt->num_pd_entries, |
| 844 | (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30)); |
Ben Widawsky | 28cf541 | 2013-11-02 21:07:26 -0700 | [diff] [blame] | 845 | return 0; |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 846 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 847 | bail: |
| 848 | gen8_ppgtt_unmap_pages(ppgtt); |
| 849 | gen8_ppgtt_free(ppgtt); |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 850 | return ret; |
| 851 | } |
| 852 | |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 853 | static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m) |
| 854 | { |
| 855 | struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; |
| 856 | struct i915_address_space *vm = &ppgtt->base; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 857 | gen6_pte_t __iomem *pd_addr; |
| 858 | gen6_pte_t scratch_pte; |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 859 | uint32_t pd_entry; |
| 860 | int pte, pde; |
| 861 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 862 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0); |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 863 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 864 | pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm + |
| 865 | ppgtt->pd.pd_offset / sizeof(gen6_pte_t); |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 866 | |
| 867 | seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm, |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 868 | ppgtt->pd.pd_offset, |
| 869 | ppgtt->pd.pd_offset + ppgtt->num_pd_entries); |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 870 | for (pde = 0; pde < ppgtt->num_pd_entries; pde++) { |
| 871 | u32 expected; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 872 | gen6_pte_t *pt_vaddr; |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 873 | dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr; |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 874 | pd_entry = readl(pd_addr + pde); |
| 875 | expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID); |
| 876 | |
| 877 | if (pd_entry != expected) |
| 878 | seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n", |
| 879 | pde, |
| 880 | pd_entry, |
| 881 | expected); |
| 882 | seq_printf(m, "\tPDE: %x\n", pd_entry); |
| 883 | |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 884 | pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 885 | for (pte = 0; pte < GEN6_PTES; pte+=4) { |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 886 | unsigned long va = |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 887 | (pde * PAGE_SIZE * GEN6_PTES) + |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 888 | (pte * PAGE_SIZE); |
| 889 | int i; |
| 890 | bool found = false; |
| 891 | for (i = 0; i < 4; i++) |
| 892 | if (pt_vaddr[pte + i] != scratch_pte) |
| 893 | found = true; |
| 894 | if (!found) |
| 895 | continue; |
| 896 | |
| 897 | seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte); |
| 898 | for (i = 0; i < 4; i++) { |
| 899 | if (pt_vaddr[pte + i] != scratch_pte) |
| 900 | seq_printf(m, " %08x", pt_vaddr[pte + i]); |
| 901 | else |
| 902 | seq_puts(m, " SCRATCH "); |
| 903 | } |
| 904 | seq_puts(m, "\n"); |
| 905 | } |
| 906 | kunmap_atomic(pt_vaddr); |
| 907 | } |
| 908 | } |
| 909 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 910 | /* Write pde (index) from the page directory @pd to the page table @pt */ |
| 911 | static void gen6_write_pde(struct i915_page_directory_entry *pd, |
| 912 | const int pde, struct i915_page_table_entry *pt) |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 913 | { |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 914 | /* Caller needs to make sure the write completes if necessary */ |
| 915 | struct i915_hw_ppgtt *ppgtt = |
| 916 | container_of(pd, struct i915_hw_ppgtt, pd); |
| 917 | u32 pd_entry; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 918 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 919 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr); |
| 920 | pd_entry |= GEN6_PDE_VALID; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 921 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 922 | writel(pd_entry, ppgtt->pd_addr + pde); |
| 923 | } |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 924 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 925 | /* Write all the page tables found in the ppgtt structure to incrementing page |
| 926 | * directories. */ |
| 927 | static void gen6_write_page_range(struct drm_i915_private *dev_priv, |
| 928 | struct i915_page_directory_entry *pd, |
| 929 | uint32_t start, uint32_t length) |
| 930 | { |
| 931 | struct i915_page_table_entry *pt; |
| 932 | uint32_t pde, temp; |
| 933 | |
| 934 | gen6_for_each_pde(pt, pd, start, length, temp, pde) |
| 935 | gen6_write_pde(pd, pde, pt); |
| 936 | |
| 937 | /* Make sure write is complete before other code can use this page |
| 938 | * table. Also require for WC mapped PTEs */ |
| 939 | readl(dev_priv->gtt.gsm); |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 942 | static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt) |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 943 | { |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 944 | BUG_ON(ppgtt->pd.pd_offset & 0x3f); |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 945 | |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 946 | return (ppgtt->pd.pd_offset / 64) << 16; |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 947 | } |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 948 | |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 949 | static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 950 | struct intel_engine_cs *ring) |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 951 | { |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 952 | int ret; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 953 | |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 954 | /* NB: TLBs must be flushed and invalidated before a switch */ |
| 955 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 956 | if (ret) |
| 957 | return ret; |
| 958 | |
| 959 | ret = intel_ring_begin(ring, 6); |
| 960 | if (ret) |
| 961 | return ret; |
| 962 | |
| 963 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); |
| 964 | intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); |
| 965 | intel_ring_emit(ring, PP_DIR_DCLV_2G); |
| 966 | intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); |
| 967 | intel_ring_emit(ring, get_pd_offset(ppgtt)); |
| 968 | intel_ring_emit(ring, MI_NOOP); |
| 969 | intel_ring_advance(ring); |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
Yu Zhang | 71ba2d6 | 2015-02-10 19:05:54 +0800 | [diff] [blame] | 974 | static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt, |
| 975 | struct intel_engine_cs *ring) |
| 976 | { |
| 977 | struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev); |
| 978 | |
| 979 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); |
| 980 | I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); |
| 981 | return 0; |
| 982 | } |
| 983 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 984 | static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 985 | struct intel_engine_cs *ring) |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 986 | { |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 987 | int ret; |
| 988 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 989 | /* NB: TLBs must be flushed and invalidated before a switch */ |
| 990 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 991 | if (ret) |
| 992 | return ret; |
| 993 | |
| 994 | ret = intel_ring_begin(ring, 6); |
| 995 | if (ret) |
| 996 | return ret; |
| 997 | |
| 998 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); |
| 999 | intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); |
| 1000 | intel_ring_emit(ring, PP_DIR_DCLV_2G); |
| 1001 | intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); |
| 1002 | intel_ring_emit(ring, get_pd_offset(ppgtt)); |
| 1003 | intel_ring_emit(ring, MI_NOOP); |
| 1004 | intel_ring_advance(ring); |
| 1005 | |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 1006 | /* XXX: RCS is the only one to auto invalidate the TLBs? */ |
| 1007 | if (ring->id != RCS) { |
| 1008 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 1009 | if (ret) |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
| 1015 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1016 | static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 1017 | struct intel_engine_cs *ring) |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1018 | { |
| 1019 | struct drm_device *dev = ppgtt->base.dev; |
| 1020 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1021 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1022 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1023 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); |
| 1024 | I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); |
| 1025 | |
| 1026 | POSTING_READ(RING_PP_DIR_DCLV(ring)); |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1031 | static void gen8_ppgtt_enable(struct drm_device *dev) |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1032 | { |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1033 | struct drm_i915_private *dev_priv = dev->dev_private; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1034 | struct intel_engine_cs *ring; |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1035 | int j; |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1036 | |
| 1037 | for_each_ring(ring, dev_priv, j) { |
| 1038 | I915_WRITE(RING_MODE_GEN7(ring), |
| 1039 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1040 | } |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1043 | static void gen7_ppgtt_enable(struct drm_device *dev) |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1044 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 1045 | struct drm_i915_private *dev_priv = dev->dev_private; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1046 | struct intel_engine_cs *ring; |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1047 | uint32_t ecochk, ecobits; |
| 1048 | int i; |
| 1049 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1050 | ecobits = I915_READ(GAC_ECO_BITS); |
| 1051 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); |
| 1052 | |
| 1053 | ecochk = I915_READ(GAM_ECOCHK); |
| 1054 | if (IS_HASWELL(dev)) { |
| 1055 | ecochk |= ECOCHK_PPGTT_WB_HSW; |
| 1056 | } else { |
| 1057 | ecochk |= ECOCHK_PPGTT_LLC_IVB; |
| 1058 | ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; |
| 1059 | } |
| 1060 | I915_WRITE(GAM_ECOCHK, ecochk); |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1061 | |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1062 | for_each_ring(ring, dev_priv, i) { |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 1063 | /* GFX_MODE is per-ring on gen7+ */ |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1064 | I915_WRITE(RING_MODE_GEN7(ring), |
| 1065 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1066 | } |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1069 | static void gen6_ppgtt_enable(struct drm_device *dev) |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1070 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 1071 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1072 | uint32_t ecochk, gab_ctl, ecobits; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1073 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1074 | ecobits = I915_READ(GAC_ECO_BITS); |
| 1075 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | |
| 1076 | ECOBITS_PPGTT_CACHE64B); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1077 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1078 | gab_ctl = I915_READ(GAB_CTL); |
| 1079 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1080 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1081 | ecochk = I915_READ(GAM_ECOCHK); |
| 1082 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1083 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1084 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1087 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1088 | static void gen6_ppgtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1089 | uint64_t start, |
| 1090 | uint64_t length, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1091 | bool use_scratch) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1092 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1093 | struct i915_hw_ppgtt *ppgtt = |
| 1094 | container_of(vm, struct i915_hw_ppgtt, base); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1095 | gen6_pte_t *pt_vaddr, scratch_pte; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1096 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1097 | unsigned num_entries = length >> PAGE_SHIFT; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1098 | unsigned act_pt = first_entry / GEN6_PTES; |
| 1099 | unsigned first_pte = first_entry % GEN6_PTES; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1100 | unsigned last_pte, i; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1101 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1102 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1103 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1104 | while (num_entries) { |
| 1105 | last_pte = first_pte + num_entries; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1106 | if (last_pte > GEN6_PTES) |
| 1107 | last_pte = GEN6_PTES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1108 | |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 1109 | pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1110 | |
| 1111 | for (i = first_pte; i < last_pte; i++) |
| 1112 | pt_vaddr[i] = scratch_pte; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1113 | |
| 1114 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1115 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1116 | num_entries -= last_pte - first_pte; |
| 1117 | first_pte = 0; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 1118 | act_pt++; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1119 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1122 | static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1123 | struct sg_table *pages, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1124 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1125 | enum i915_cache_level cache_level, u32 flags) |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1126 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1127 | struct i915_hw_ppgtt *ppgtt = |
| 1128 | container_of(vm, struct i915_hw_ppgtt, base); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1129 | gen6_pte_t *pt_vaddr; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1130 | unsigned first_entry = start >> PAGE_SHIFT; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1131 | unsigned act_pt = first_entry / GEN6_PTES; |
| 1132 | unsigned act_pte = first_entry % GEN6_PTES; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1133 | struct sg_page_iter sg_iter; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1134 | |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 1135 | pt_vaddr = NULL; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1136 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 1137 | if (pt_vaddr == NULL) |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 1138 | pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1139 | |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 1140 | pt_vaddr[act_pte] = |
| 1141 | vm->pte_encode(sg_page_iter_dma_address(&sg_iter), |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1142 | cache_level, true, flags); |
| 1143 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1144 | if (++act_pte == GEN6_PTES) { |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1145 | kunmap_atomic(pt_vaddr); |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 1146 | pt_vaddr = NULL; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 1147 | act_pt++; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1148 | act_pte = 0; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1149 | } |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1150 | } |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 1151 | if (pt_vaddr) |
| 1152 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 1153 | } |
| 1154 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 1155 | static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1156 | { |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1157 | int i; |
| 1158 | |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 1159 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 1160 | pci_unmap_page(ppgtt->base.dev->pdev, |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 1161 | ppgtt->pd.page_table[i]->daddr, |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 1162 | 4096, PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 1163 | } |
| 1164 | |
Ben Widawsky | 563222a | 2015-03-19 12:53:28 +0000 | [diff] [blame^] | 1165 | /* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we |
| 1166 | * are switching between contexts with the same LRCA, we also must do a force |
| 1167 | * restore. |
| 1168 | */ |
| 1169 | static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt) |
| 1170 | { |
| 1171 | /* If current vm != vm, */ |
| 1172 | ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask; |
| 1173 | } |
| 1174 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1175 | static int gen6_alloc_va_range(struct i915_address_space *vm, |
| 1176 | uint64_t start, uint64_t length) |
| 1177 | { |
| 1178 | struct i915_hw_ppgtt *ppgtt = |
| 1179 | container_of(vm, struct i915_hw_ppgtt, base); |
| 1180 | struct i915_page_table_entry *pt; |
| 1181 | uint32_t pde, temp; |
| 1182 | |
| 1183 | gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) { |
| 1184 | DECLARE_BITMAP(tmp_bitmap, GEN6_PTES); |
| 1185 | |
| 1186 | bitmap_zero(tmp_bitmap, GEN6_PTES); |
| 1187 | bitmap_set(tmp_bitmap, gen6_pte_index(start), |
| 1188 | gen6_pte_count(start, length)); |
| 1189 | |
| 1190 | bitmap_or(pt->used_ptes, pt->used_ptes, tmp_bitmap, |
| 1191 | GEN6_PTES); |
| 1192 | } |
| 1193 | |
Ben Widawsky | 563222a | 2015-03-19 12:53:28 +0000 | [diff] [blame^] | 1194 | mark_tlbs_dirty(ppgtt); |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1195 | return 0; |
| 1196 | } |
| 1197 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 1198 | static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt) |
| 1199 | { |
| 1200 | int i; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1201 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1202 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 1203 | unmap_and_free_pt(ppgtt->pd.page_table[i], ppgtt->base.dev); |
Ben Widawsky | 06fda60 | 2015-02-24 16:22:36 +0000 | [diff] [blame] | 1204 | |
| 1205 | unmap_and_free_pd(&ppgtt->pd); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 1208 | static void gen6_ppgtt_cleanup(struct i915_address_space *vm) |
| 1209 | { |
| 1210 | struct i915_hw_ppgtt *ppgtt = |
| 1211 | container_of(vm, struct i915_hw_ppgtt, base); |
| 1212 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 1213 | drm_mm_remove_node(&ppgtt->node); |
| 1214 | |
| 1215 | gen6_ppgtt_unmap_pages(ppgtt); |
| 1216 | gen6_ppgtt_free(ppgtt); |
| 1217 | } |
| 1218 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1219 | static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt) |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1220 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1221 | struct drm_device *dev = ppgtt->base.dev; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1222 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 1223 | bool retried = false; |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1224 | int ret; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1225 | |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 1226 | /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The |
| 1227 | * allocator works in address space sizes, so it's multiplied by page |
| 1228 | * size. We allocate at the top of the GTT to avoid fragmentation. |
| 1229 | */ |
| 1230 | BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm)); |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 1231 | alloc: |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 1232 | ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm, |
| 1233 | &ppgtt->node, GEN6_PD_SIZE, |
| 1234 | GEN6_PD_ALIGN, 0, |
| 1235 | 0, dev_priv->gtt.base.total, |
Ben Widawsky | 3e8b5ae | 2014-05-06 22:21:30 -0700 | [diff] [blame] | 1236 | DRM_MM_TOPDOWN); |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 1237 | if (ret == -ENOSPC && !retried) { |
| 1238 | ret = i915_gem_evict_something(dev, &dev_priv->gtt.base, |
| 1239 | GEN6_PD_SIZE, GEN6_PD_ALIGN, |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 1240 | I915_CACHE_NONE, |
| 1241 | 0, dev_priv->gtt.base.total, |
| 1242 | 0); |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 1243 | if (ret) |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1244 | goto err_out; |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 1245 | |
| 1246 | retried = true; |
| 1247 | goto alloc; |
| 1248 | } |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 1249 | |
Ben Widawsky | c8c2662 | 2015-01-22 17:01:25 +0000 | [diff] [blame] | 1250 | if (ret) |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1251 | goto err_out; |
| 1252 | |
Ben Widawsky | c8c2662 | 2015-01-22 17:01:25 +0000 | [diff] [blame] | 1253 | |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 1254 | if (ppgtt->node.start < dev_priv->gtt.mappable_end) |
| 1255 | DRM_DEBUG("Forced to use aperture for PDEs\n"); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1256 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1257 | ppgtt->num_pd_entries = I915_PDES; |
Ben Widawsky | c8c2662 | 2015-01-22 17:01:25 +0000 | [diff] [blame] | 1258 | return 0; |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1259 | |
| 1260 | err_out: |
| 1261 | return ret; |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1264 | static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt) |
| 1265 | { |
| 1266 | int ret; |
| 1267 | |
| 1268 | ret = gen6_ppgtt_allocate_page_directories(ppgtt); |
| 1269 | if (ret) |
| 1270 | return ret; |
| 1271 | |
Michel Thierry | 06dc68d | 2015-02-24 16:22:37 +0000 | [diff] [blame] | 1272 | ret = alloc_pt_range(&ppgtt->pd, 0, ppgtt->num_pd_entries, |
| 1273 | ppgtt->base.dev); |
| 1274 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1275 | if (ret) { |
| 1276 | drm_mm_remove_node(&ppgtt->node); |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1280 | return 0; |
| 1281 | } |
| 1282 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1283 | static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) |
| 1284 | { |
| 1285 | struct drm_device *dev = ppgtt->base.dev; |
| 1286 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1287 | int ret; |
| 1288 | |
| 1289 | ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1290 | if (IS_GEN6(dev)) { |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1291 | ppgtt->switch_mm = gen6_mm_switch; |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 1292 | } else if (IS_HASWELL(dev)) { |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 1293 | ppgtt->switch_mm = hsw_mm_switch; |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1294 | } else if (IS_GEN7(dev)) { |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1295 | ppgtt->switch_mm = gen7_mm_switch; |
| 1296 | } else |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1297 | BUG(); |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1298 | |
Yu Zhang | 71ba2d6 | 2015-02-10 19:05:54 +0800 | [diff] [blame] | 1299 | if (intel_vgpu_active(dev)) |
| 1300 | ppgtt->switch_mm = vgpu_mm_switch; |
| 1301 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1302 | ret = gen6_ppgtt_alloc(ppgtt); |
| 1303 | if (ret) |
| 1304 | return ret; |
| 1305 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1306 | ppgtt->base.allocate_va_range = gen6_alloc_va_range; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1307 | ppgtt->base.clear_range = gen6_ppgtt_clear_range; |
| 1308 | ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; |
| 1309 | ppgtt->base.cleanup = gen6_ppgtt_cleanup; |
Ben Widawsky | 686e1f6 | 2013-11-25 09:54:34 -0800 | [diff] [blame] | 1310 | ppgtt->base.start = 0; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1311 | ppgtt->base.total = ppgtt->num_pd_entries * GEN6_PTES * PAGE_SIZE; |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1312 | ppgtt->debug_dump = gen6_dump_ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1313 | |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 1314 | ppgtt->pd.pd_offset = |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1315 | ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1316 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1317 | ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm + |
| 1318 | ppgtt->pd.pd_offset / sizeof(gen6_pte_t); |
| 1319 | |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1320 | ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1321 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1322 | gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total); |
| 1323 | |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 1324 | DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n", |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 1325 | ppgtt->node.size >> 20, |
| 1326 | ppgtt->node.start / PAGE_SIZE); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1327 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1328 | DRM_DEBUG("Adding PPGTT at offset %x\n", |
Ben Widawsky | 7324cc0 | 2015-02-24 16:22:35 +0000 | [diff] [blame] | 1329 | ppgtt->pd.pd_offset << 10); |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1330 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1331 | return 0; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1334 | static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt) |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1335 | { |
| 1336 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1337 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1338 | ppgtt->base.dev = dev; |
Ben Widawsky | 8407bb9 | 2014-03-08 11:58:16 -0800 | [diff] [blame] | 1339 | ppgtt->base.scratch = dev_priv->gtt.base.scratch; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1340 | |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 1341 | if (INTEL_INFO(dev)->gen < 8) |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1342 | return gen6_ppgtt_init(ppgtt); |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 1343 | else |
Rodrigo Vivi | 1eb0f00 | 2014-12-03 04:55:26 -0800 | [diff] [blame] | 1344 | return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total); |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1345 | } |
| 1346 | int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt) |
| 1347 | { |
| 1348 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1349 | int ret = 0; |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 1350 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1351 | ret = __hw_ppgtt_init(dev, ppgtt); |
| 1352 | if (ret == 0) { |
Ben Widawsky | c7c48df | 2013-12-06 14:11:15 -0800 | [diff] [blame] | 1353 | kref_init(&ppgtt->ref); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 1354 | drm_mm_init(&ppgtt->base.mm, ppgtt->base.start, |
| 1355 | ppgtt->base.total); |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1356 | i915_init_vm(dev_priv, &ppgtt->base); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 1357 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1358 | |
| 1359 | return ret; |
| 1360 | } |
| 1361 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1362 | int i915_ppgtt_init_hw(struct drm_device *dev) |
| 1363 | { |
| 1364 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1365 | struct intel_engine_cs *ring; |
| 1366 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 1367 | int i, ret = 0; |
| 1368 | |
Thomas Daniel | 671b5013 | 2014-08-20 16:24:50 +0100 | [diff] [blame] | 1369 | /* In the case of execlists, PPGTT is enabled by the context descriptor |
| 1370 | * and the PDPs are contained within the context itself. We don't |
| 1371 | * need to do anything here. */ |
| 1372 | if (i915.enable_execlists) |
| 1373 | return 0; |
| 1374 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1375 | if (!USES_PPGTT(dev)) |
| 1376 | return 0; |
| 1377 | |
| 1378 | if (IS_GEN6(dev)) |
| 1379 | gen6_ppgtt_enable(dev); |
| 1380 | else if (IS_GEN7(dev)) |
| 1381 | gen7_ppgtt_enable(dev); |
| 1382 | else if (INTEL_INFO(dev)->gen >= 8) |
| 1383 | gen8_ppgtt_enable(dev); |
| 1384 | else |
Daniel Vetter | 5f77eeb | 2014-12-08 16:40:10 +0100 | [diff] [blame] | 1385 | MISSING_CASE(INTEL_INFO(dev)->gen); |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1386 | |
| 1387 | if (ppgtt) { |
| 1388 | for_each_ring(ring, dev_priv, i) { |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 1389 | ret = ppgtt->switch_mm(ppgtt, ring); |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1390 | if (ret != 0) |
| 1391 | return ret; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | return ret; |
| 1396 | } |
Daniel Vetter | 4d88470 | 2014-08-06 15:04:47 +0200 | [diff] [blame] | 1397 | struct i915_hw_ppgtt * |
| 1398 | i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv) |
| 1399 | { |
| 1400 | struct i915_hw_ppgtt *ppgtt; |
| 1401 | int ret; |
| 1402 | |
| 1403 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 1404 | if (!ppgtt) |
| 1405 | return ERR_PTR(-ENOMEM); |
| 1406 | |
| 1407 | ret = i915_ppgtt_init(dev, ppgtt); |
| 1408 | if (ret) { |
| 1409 | kfree(ppgtt); |
| 1410 | return ERR_PTR(ret); |
| 1411 | } |
| 1412 | |
| 1413 | ppgtt->file_priv = fpriv; |
| 1414 | |
Daniele Ceraolo Spurio | 198c974 | 2014-11-10 13:44:31 +0000 | [diff] [blame] | 1415 | trace_i915_ppgtt_create(&ppgtt->base); |
| 1416 | |
Daniel Vetter | 4d88470 | 2014-08-06 15:04:47 +0200 | [diff] [blame] | 1417 | return ppgtt; |
| 1418 | } |
| 1419 | |
Daniel Vetter | ee960be | 2014-08-06 15:04:45 +0200 | [diff] [blame] | 1420 | void i915_ppgtt_release(struct kref *kref) |
| 1421 | { |
| 1422 | struct i915_hw_ppgtt *ppgtt = |
| 1423 | container_of(kref, struct i915_hw_ppgtt, ref); |
| 1424 | |
Daniele Ceraolo Spurio | 198c974 | 2014-11-10 13:44:31 +0000 | [diff] [blame] | 1425 | trace_i915_ppgtt_release(&ppgtt->base); |
| 1426 | |
Daniel Vetter | ee960be | 2014-08-06 15:04:45 +0200 | [diff] [blame] | 1427 | /* vmas should already be unbound */ |
| 1428 | WARN_ON(!list_empty(&ppgtt->base.active_list)); |
| 1429 | WARN_ON(!list_empty(&ppgtt->base.inactive_list)); |
| 1430 | |
Daniel Vetter | 19dd120 | 2014-08-06 15:04:55 +0200 | [diff] [blame] | 1431 | list_del(&ppgtt->base.global_link); |
| 1432 | drm_mm_takedown(&ppgtt->base.mm); |
| 1433 | |
Daniel Vetter | ee960be | 2014-08-06 15:04:45 +0200 | [diff] [blame] | 1434 | ppgtt->base.cleanup(&ppgtt->base); |
| 1435 | kfree(ppgtt); |
| 1436 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1437 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1438 | static void |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1439 | ppgtt_bind_vma(struct i915_vma *vma, |
| 1440 | enum i915_cache_level cache_level, |
| 1441 | u32 flags) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1442 | { |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1443 | /* Currently applicable only to VLV */ |
| 1444 | if (vma->obj->gt_ro) |
| 1445 | flags |= PTE_READ_ONLY; |
| 1446 | |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1447 | vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1448 | cache_level, flags); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1451 | static void ppgtt_unbind_vma(struct i915_vma *vma) |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1452 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1453 | vma->vm->clear_range(vma->vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1454 | vma->node.start, |
| 1455 | vma->obj->base.size, |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1456 | true); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 1459 | extern int intel_iommu_gfx_mapped; |
| 1460 | /* Certain Gen5 chipsets require require idling the GPU before |
| 1461 | * unmapping anything from the GTT when VT-d is enabled. |
| 1462 | */ |
| 1463 | static inline bool needs_idle_maps(struct drm_device *dev) |
| 1464 | { |
| 1465 | #ifdef CONFIG_INTEL_IOMMU |
| 1466 | /* Query intel_iommu to see if we need the workaround. Presumably that |
| 1467 | * was loaded first. |
| 1468 | */ |
| 1469 | if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) |
| 1470 | return true; |
| 1471 | #endif |
| 1472 | return false; |
| 1473 | } |
| 1474 | |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1475 | static bool do_idling(struct drm_i915_private *dev_priv) |
| 1476 | { |
| 1477 | bool ret = dev_priv->mm.interruptible; |
| 1478 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 1479 | if (unlikely(dev_priv->gtt.do_idle_maps)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1480 | dev_priv->mm.interruptible = false; |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 1481 | if (i915_gpu_idle(dev_priv->dev)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1482 | DRM_ERROR("Couldn't idle GPU\n"); |
| 1483 | /* Wait a bit, in hopes it avoids the hang */ |
| 1484 | udelay(10); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | return ret; |
| 1489 | } |
| 1490 | |
| 1491 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) |
| 1492 | { |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 1493 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1494 | dev_priv->mm.interruptible = interruptible; |
| 1495 | } |
| 1496 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1497 | void i915_check_and_clear_faults(struct drm_device *dev) |
| 1498 | { |
| 1499 | struct drm_i915_private *dev_priv = dev->dev_private; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1500 | struct intel_engine_cs *ring; |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1501 | int i; |
| 1502 | |
| 1503 | if (INTEL_INFO(dev)->gen < 6) |
| 1504 | return; |
| 1505 | |
| 1506 | for_each_ring(ring, dev_priv, i) { |
| 1507 | u32 fault_reg; |
| 1508 | fault_reg = I915_READ(RING_FAULT_REG(ring)); |
| 1509 | if (fault_reg & RING_FAULT_VALID) { |
| 1510 | DRM_DEBUG_DRIVER("Unexpected fault\n" |
Paulo Zanoni | 59a5d29 | 2014-10-30 15:52:45 -0200 | [diff] [blame] | 1511 | "\tAddr: 0x%08lx\n" |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1512 | "\tAddress space: %s\n" |
| 1513 | "\tSource ID: %d\n" |
| 1514 | "\tType: %d\n", |
| 1515 | fault_reg & PAGE_MASK, |
| 1516 | fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", |
| 1517 | RING_FAULT_SRCID(fault_reg), |
| 1518 | RING_FAULT_FAULT_TYPE(fault_reg)); |
| 1519 | I915_WRITE(RING_FAULT_REG(ring), |
| 1520 | fault_reg & ~RING_FAULT_VALID); |
| 1521 | } |
| 1522 | } |
| 1523 | POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); |
| 1524 | } |
| 1525 | |
Chris Wilson | 91e5649 | 2014-09-25 10:13:12 +0100 | [diff] [blame] | 1526 | static void i915_ggtt_flush(struct drm_i915_private *dev_priv) |
| 1527 | { |
| 1528 | if (INTEL_INFO(dev_priv->dev)->gen < 6) { |
| 1529 | intel_gtt_chipset_flush(); |
| 1530 | } else { |
| 1531 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 1532 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
| 1533 | } |
| 1534 | } |
| 1535 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1536 | void i915_gem_suspend_gtt_mappings(struct drm_device *dev) |
| 1537 | { |
| 1538 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1539 | |
| 1540 | /* Don't bother messing with faults pre GEN6 as we have little |
| 1541 | * documentation supporting that it's a good idea. |
| 1542 | */ |
| 1543 | if (INTEL_INFO(dev)->gen < 6) |
| 1544 | return; |
| 1545 | |
| 1546 | i915_check_and_clear_faults(dev); |
| 1547 | |
| 1548 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1549 | dev_priv->gtt.base.start, |
| 1550 | dev_priv->gtt.base.total, |
Daniel Vetter | e568af1 | 2014-03-26 20:08:20 +0100 | [diff] [blame] | 1551 | true); |
Chris Wilson | 91e5649 | 2014-09-25 10:13:12 +0100 | [diff] [blame] | 1552 | |
| 1553 | i915_ggtt_flush(dev_priv); |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1556 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
| 1557 | { |
| 1558 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1559 | struct drm_i915_gem_object *obj; |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1560 | struct i915_address_space *vm; |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1561 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1562 | i915_check_and_clear_faults(dev); |
| 1563 | |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 1564 | /* First fill our portion of the GTT with scratch pages */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1565 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1566 | dev_priv->gtt.base.start, |
| 1567 | dev_priv->gtt.base.total, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1568 | true); |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 1569 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1570 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1571 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, |
| 1572 | &dev_priv->gtt.base); |
| 1573 | if (!vma) |
| 1574 | continue; |
| 1575 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 1576 | i915_gem_clflush_object(obj, obj->pin_display); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1577 | /* The bind_vma code tries to be smart about tracking mappings. |
| 1578 | * Unfortunately above, we've just wiped out the mappings |
| 1579 | * without telling our object about it. So we need to fake it. |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 1580 | * |
| 1581 | * Bind is not expected to fail since this is only called on |
| 1582 | * resume and assumption is all requirements exist already. |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1583 | */ |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1584 | vma->bound &= ~GLOBAL_BIND; |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 1585 | WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND)); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1588 | |
Ben Widawsky | a2319c0 | 2014-03-18 16:09:37 -0700 | [diff] [blame] | 1589 | if (INTEL_INFO(dev)->gen >= 8) { |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 1590 | if (IS_CHERRYVIEW(dev)) |
| 1591 | chv_setup_private_ppat(dev_priv); |
| 1592 | else |
| 1593 | bdw_setup_private_ppat(dev_priv); |
| 1594 | |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1595 | return; |
Ben Widawsky | a2319c0 | 2014-03-18 16:09:37 -0700 | [diff] [blame] | 1596 | } |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1597 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1598 | if (USES_PPGTT(dev)) { |
| 1599 | list_for_each_entry(vm, &dev_priv->vm_list, global_link) { |
| 1600 | /* TODO: Perhaps it shouldn't be gen6 specific */ |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1601 | |
Ben Widawsky | 678d96f | 2015-03-16 16:00:56 +0000 | [diff] [blame] | 1602 | struct i915_hw_ppgtt *ppgtt = |
| 1603 | container_of(vm, struct i915_hw_ppgtt, |
| 1604 | base); |
| 1605 | |
| 1606 | if (i915_is_ggtt(vm)) |
| 1607 | ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 1608 | |
| 1609 | gen6_write_page_range(dev_priv, &ppgtt->pd, |
| 1610 | 0, ppgtt->base.total); |
| 1611 | } |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1612 | } |
| 1613 | |
Chris Wilson | 91e5649 | 2014-09-25 10:13:12 +0100 | [diff] [blame] | 1614 | i915_ggtt_flush(dev_priv); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1615 | } |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1616 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 1617 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1618 | { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1619 | if (obj->has_dma_mapping) |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 1620 | return 0; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1621 | |
| 1622 | if (!dma_map_sg(&obj->base.dev->pdev->dev, |
| 1623 | obj->pages->sgl, obj->pages->nents, |
| 1624 | PCI_DMA_BIDIRECTIONAL)) |
| 1625 | return -ENOSPC; |
| 1626 | |
| 1627 | return 0; |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1630 | static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte) |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1631 | { |
| 1632 | #ifdef writeq |
| 1633 | writeq(pte, addr); |
| 1634 | #else |
| 1635 | iowrite32((u32)pte, addr); |
| 1636 | iowrite32(pte >> 32, addr + 4); |
| 1637 | #endif |
| 1638 | } |
| 1639 | |
| 1640 | static void gen8_ggtt_insert_entries(struct i915_address_space *vm, |
| 1641 | struct sg_table *st, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1642 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1643 | enum i915_cache_level level, u32 unused) |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1644 | { |
| 1645 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1646 | unsigned first_entry = start >> PAGE_SHIFT; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1647 | gen8_pte_t __iomem *gtt_entries = |
| 1648 | (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1649 | int i = 0; |
| 1650 | struct sg_page_iter sg_iter; |
Pavel Machek | 57007df | 2014-07-28 13:20:58 +0200 | [diff] [blame] | 1651 | dma_addr_t addr = 0; /* shut up gcc */ |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1652 | |
| 1653 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
| 1654 | addr = sg_dma_address(sg_iter.sg) + |
| 1655 | (sg_iter.sg_pgoffset << PAGE_SHIFT); |
| 1656 | gen8_set_pte(>t_entries[i], |
| 1657 | gen8_pte_encode(addr, level, true)); |
| 1658 | i++; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * XXX: This serves as a posting read to make sure that the PTE has |
| 1663 | * actually been updated. There is some concern that even though |
| 1664 | * registers and PTEs are within the same BAR that they are potentially |
| 1665 | * of NUMA access patterns. Therefore, even with the way we assume |
| 1666 | * hardware should work, we must keep this posting read for paranoia. |
| 1667 | */ |
| 1668 | if (i != 0) |
| 1669 | WARN_ON(readq(>t_entries[i-1]) |
| 1670 | != gen8_pte_encode(addr, level, true)); |
| 1671 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1672 | /* This next bit makes the above posting read even more important. We |
| 1673 | * want to flush the TLBs only after we're certain all the PTE updates |
| 1674 | * have finished. |
| 1675 | */ |
| 1676 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 1677 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1680 | /* |
| 1681 | * Binds an object into the global gtt with the specified cache level. The object |
| 1682 | * will be accessible to the GPU via commands whose operands reference offsets |
| 1683 | * within the global GTT as well as accessible by the GPU through the GMADR |
| 1684 | * mapped BAR (dev_priv->mm.gtt->gtt). |
| 1685 | */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1686 | static void gen6_ggtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1687 | struct sg_table *st, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1688 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1689 | enum i915_cache_level level, u32 flags) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1690 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1691 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1692 | unsigned first_entry = start >> PAGE_SHIFT; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1693 | gen6_pte_t __iomem *gtt_entries = |
| 1694 | (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1695 | int i = 0; |
| 1696 | struct sg_page_iter sg_iter; |
Pavel Machek | 57007df | 2014-07-28 13:20:58 +0200 | [diff] [blame] | 1697 | dma_addr_t addr = 0; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1698 | |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1699 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 1700 | addr = sg_page_iter_dma_address(&sg_iter); |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1701 | iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1702 | i++; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1705 | /* XXX: This serves as a posting read to make sure that the PTE has |
| 1706 | * actually been updated. There is some concern that even though |
| 1707 | * registers and PTEs are within the same BAR that they are potentially |
| 1708 | * of NUMA access patterns. Therefore, even with the way we assume |
| 1709 | * hardware should work, we must keep this posting read for paranoia. |
| 1710 | */ |
Pavel Machek | 57007df | 2014-07-28 13:20:58 +0200 | [diff] [blame] | 1711 | if (i != 0) { |
| 1712 | unsigned long gtt = readl(>t_entries[i-1]); |
| 1713 | WARN_ON(gtt != vm->pte_encode(addr, level, true, flags)); |
| 1714 | } |
Ben Widawsky | 0f9b91c | 2012-11-04 09:21:30 -0800 | [diff] [blame] | 1715 | |
| 1716 | /* This next bit makes the above posting read even more important. We |
| 1717 | * want to flush the TLBs only after we're certain all the PTE updates |
| 1718 | * have finished. |
| 1719 | */ |
| 1720 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 1721 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1724 | static void gen8_ggtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1725 | uint64_t start, |
| 1726 | uint64_t length, |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1727 | bool use_scratch) |
| 1728 | { |
| 1729 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1730 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1731 | unsigned num_entries = length >> PAGE_SHIFT; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1732 | gen8_pte_t scratch_pte, __iomem *gtt_base = |
| 1733 | (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1734 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
| 1735 | int i; |
| 1736 | |
| 1737 | if (WARN(num_entries > max_entries, |
| 1738 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 1739 | first_entry, num_entries, max_entries)) |
| 1740 | num_entries = max_entries; |
| 1741 | |
| 1742 | scratch_pte = gen8_pte_encode(vm->scratch.addr, |
| 1743 | I915_CACHE_LLC, |
| 1744 | use_scratch); |
| 1745 | for (i = 0; i < num_entries; i++) |
| 1746 | gen8_set_pte(>t_base[i], scratch_pte); |
| 1747 | readl(gtt_base); |
| 1748 | } |
| 1749 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1750 | static void gen6_ggtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1751 | uint64_t start, |
| 1752 | uint64_t length, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1753 | bool use_scratch) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1754 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1755 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1756 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1757 | unsigned num_entries = length >> PAGE_SHIFT; |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 1758 | gen6_pte_t scratch_pte, __iomem *gtt_base = |
| 1759 | (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 1760 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1761 | int i; |
| 1762 | |
| 1763 | if (WARN(num_entries > max_entries, |
| 1764 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 1765 | first_entry, num_entries, max_entries)) |
| 1766 | num_entries = max_entries; |
| 1767 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1768 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0); |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1769 | |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1770 | for (i = 0; i < num_entries; i++) |
| 1771 | iowrite32(scratch_pte, >t_base[i]); |
| 1772 | readl(gtt_base); |
| 1773 | } |
| 1774 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1775 | |
| 1776 | static void i915_ggtt_bind_vma(struct i915_vma *vma, |
| 1777 | enum i915_cache_level cache_level, |
| 1778 | u32 unused) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1779 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1780 | const unsigned long entry = vma->node.start >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1781 | unsigned int flags = (cache_level == I915_CACHE_NONE) ? |
| 1782 | AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; |
| 1783 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1784 | BUG_ON(!i915_is_ggtt(vma->vm)); |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 1785 | intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags); |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1786 | vma->bound = GLOBAL_BIND; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1787 | } |
| 1788 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1789 | static void i915_ggtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1790 | uint64_t start, |
| 1791 | uint64_t length, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1792 | bool unused) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1793 | { |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1794 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1795 | unsigned num_entries = length >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1796 | intel_gtt_clear_range(first_entry, num_entries); |
| 1797 | } |
| 1798 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1799 | static void i915_ggtt_unbind_vma(struct i915_vma *vma) |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 1800 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1801 | const unsigned int first = vma->node.start >> PAGE_SHIFT; |
| 1802 | const unsigned int size = vma->obj->base.size >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1803 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1804 | BUG_ON(!i915_is_ggtt(vma->vm)); |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1805 | vma->bound = 0; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1806 | intel_gtt_clear_range(first, size); |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 1807 | } |
| 1808 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1809 | static void ggtt_bind_vma(struct i915_vma *vma, |
| 1810 | enum i915_cache_level cache_level, |
| 1811 | u32 flags) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1812 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1813 | struct drm_device *dev = vma->vm->dev; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1814 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1815 | struct drm_i915_gem_object *obj = vma->obj; |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1816 | struct sg_table *pages = obj->pages; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1817 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1818 | /* Currently applicable only to VLV */ |
| 1819 | if (obj->gt_ro) |
| 1820 | flags |= PTE_READ_ONLY; |
| 1821 | |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1822 | if (i915_is_ggtt(vma->vm)) |
| 1823 | pages = vma->ggtt_view.pages; |
| 1824 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1825 | /* If there is no aliasing PPGTT, or the caller needs a global mapping, |
| 1826 | * or we have a global mapping already but the cacheability flags have |
| 1827 | * changed, set the global PTEs. |
| 1828 | * |
| 1829 | * If there is an aliasing PPGTT it is anecdotally faster, so use that |
| 1830 | * instead if none of the above hold true. |
| 1831 | * |
| 1832 | * NB: A global mapping should only be needed for special regions like |
| 1833 | * "gtt mappable", SNB errata, or if specified via special execbuf |
| 1834 | * flags. At all other times, the GPU will use the aliasing PPGTT. |
| 1835 | */ |
| 1836 | if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) { |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1837 | if (!(vma->bound & GLOBAL_BIND) || |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1838 | (cache_level != obj->cache_level)) { |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1839 | vma->vm->insert_entries(vma->vm, pages, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1840 | vma->node.start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1841 | cache_level, flags); |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1842 | vma->bound |= GLOBAL_BIND; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1843 | } |
| 1844 | } |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 1845 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1846 | if (dev_priv->mm.aliasing_ppgtt && |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1847 | (!(vma->bound & LOCAL_BIND) || |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1848 | (cache_level != obj->cache_level))) { |
| 1849 | struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 1850 | appgtt->base.insert_entries(&appgtt->base, pages, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1851 | vma->node.start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1852 | cache_level, flags); |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1853 | vma->bound |= LOCAL_BIND; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | static void ggtt_unbind_vma(struct i915_vma *vma) |
| 1858 | { |
| 1859 | struct drm_device *dev = vma->vm->dev; |
| 1860 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1861 | struct drm_i915_gem_object *obj = vma->obj; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1862 | |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1863 | if (vma->bound & GLOBAL_BIND) { |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1864 | vma->vm->clear_range(vma->vm, |
| 1865 | vma->node.start, |
| 1866 | obj->base.size, |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1867 | true); |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1868 | vma->bound &= ~GLOBAL_BIND; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1871 | if (vma->bound & LOCAL_BIND) { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1872 | struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; |
| 1873 | appgtt->base.clear_range(&appgtt->base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1874 | vma->node.start, |
| 1875 | obj->base.size, |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1876 | true); |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1877 | vma->bound &= ~LOCAL_BIND; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1878 | } |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) |
| 1882 | { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1883 | struct drm_device *dev = obj->base.dev; |
| 1884 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1885 | bool interruptible; |
| 1886 | |
| 1887 | interruptible = do_idling(dev_priv); |
| 1888 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1889 | if (!obj->has_dma_mapping) |
| 1890 | dma_unmap_sg(&dev->pdev->dev, |
| 1891 | obj->pages->sgl, obj->pages->nents, |
| 1892 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1893 | |
| 1894 | undo_idling(dev_priv, interruptible); |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1895 | } |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1896 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 1897 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
| 1898 | unsigned long color, |
Thierry Reding | 440fd52 | 2015-01-23 09:05:06 +0100 | [diff] [blame] | 1899 | u64 *start, |
| 1900 | u64 *end) |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 1901 | { |
| 1902 | if (node->color != color) |
| 1903 | *start += 4096; |
| 1904 | |
| 1905 | if (!list_empty(&node->node_list)) { |
| 1906 | node = list_entry(node->node_list.next, |
| 1907 | struct drm_mm_node, |
| 1908 | node_list); |
| 1909 | if (node->allocated && node->color != color) |
| 1910 | *end -= 4096; |
| 1911 | } |
| 1912 | } |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 1913 | |
Daniel Vetter | f548c0e | 2014-11-19 21:40:13 +0100 | [diff] [blame] | 1914 | static int i915_gem_setup_global_gtt(struct drm_device *dev, |
| 1915 | unsigned long start, |
| 1916 | unsigned long mappable_end, |
| 1917 | unsigned long end) |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1918 | { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 1919 | /* Let GEM Manage all of the aperture. |
| 1920 | * |
| 1921 | * However, leave one page at the end still bound to the scratch page. |
| 1922 | * There are a number of places where the hardware apparently prefetches |
| 1923 | * past the end of the object, and we've seen multiple hangs with the |
| 1924 | * GPU head pointer stuck in a batchbuffer bound at the last page of the |
| 1925 | * aperture. One page should be enough to keep any prefetching inside |
| 1926 | * of the aperture. |
| 1927 | */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1928 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1929 | struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1930 | struct drm_mm_node *entry; |
| 1931 | struct drm_i915_gem_object *obj; |
| 1932 | unsigned long hole_start, hole_end; |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1933 | int ret; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1934 | |
Ben Widawsky | 35451cb | 2013-01-17 12:45:13 -0800 | [diff] [blame] | 1935 | BUG_ON(mappable_end > end); |
| 1936 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1937 | /* Subtract the guard page ... */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1938 | drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE); |
Yu Zhang | 5dda8fa | 2015-02-10 19:05:48 +0800 | [diff] [blame] | 1939 | |
| 1940 | dev_priv->gtt.base.start = start; |
| 1941 | dev_priv->gtt.base.total = end - start; |
| 1942 | |
| 1943 | if (intel_vgpu_active(dev)) { |
| 1944 | ret = intel_vgt_balloon(dev); |
| 1945 | if (ret) |
| 1946 | return ret; |
| 1947 | } |
| 1948 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 1949 | if (!HAS_LLC(dev)) |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 1950 | dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1951 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1952 | /* Mark any preallocated objects as occupied */ |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1953 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1954 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1955 | |
Ben Widawsky | edd41a8 | 2013-07-05 14:41:05 -0700 | [diff] [blame] | 1956 | DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n", |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 1957 | i915_gem_obj_ggtt_offset(obj), obj->base.size); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1958 | |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 1959 | WARN_ON(i915_gem_obj_ggtt_bound(obj)); |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1960 | ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1961 | if (ret) { |
| 1962 | DRM_DEBUG_KMS("Reservation failed: %i\n", ret); |
| 1963 | return ret; |
| 1964 | } |
Tvrtko Ursulin | aff4376 | 2014-10-24 12:42:33 +0100 | [diff] [blame] | 1965 | vma->bound |= GLOBAL_BIND; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1968 | /* Clear any non-preallocated blocks */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1969 | drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) { |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1970 | DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", |
| 1971 | hole_start, hole_end); |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1972 | ggtt_vm->clear_range(ggtt_vm, hole_start, |
| 1973 | hole_end - hole_start, true); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | /* And finally clear the reserved guard page */ |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1977 | ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true); |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1978 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1979 | if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) { |
| 1980 | struct i915_hw_ppgtt *ppgtt; |
| 1981 | |
| 1982 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 1983 | if (!ppgtt) |
| 1984 | return -ENOMEM; |
| 1985 | |
| 1986 | ret = __hw_ppgtt_init(dev, ppgtt); |
| 1987 | if (ret != 0) |
| 1988 | return ret; |
| 1989 | |
| 1990 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
| 1991 | } |
| 1992 | |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1993 | return 0; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 1996 | void i915_gem_init_global_gtt(struct drm_device *dev) |
| 1997 | { |
| 1998 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1999 | unsigned long gtt_size, mappable_size; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 2000 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2001 | gtt_size = dev_priv->gtt.base.total; |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 2002 | mappable_size = dev_priv->gtt.mappable_end; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 2003 | |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 2004 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2005 | } |
| 2006 | |
Daniel Vetter | 90d0a0e | 2014-08-06 15:04:56 +0200 | [diff] [blame] | 2007 | void i915_global_gtt_cleanup(struct drm_device *dev) |
| 2008 | { |
| 2009 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2010 | struct i915_address_space *vm = &dev_priv->gtt.base; |
| 2011 | |
Daniel Vetter | 70e3254 | 2014-08-06 15:04:57 +0200 | [diff] [blame] | 2012 | if (dev_priv->mm.aliasing_ppgtt) { |
| 2013 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 2014 | |
| 2015 | ppgtt->base.cleanup(&ppgtt->base); |
| 2016 | } |
| 2017 | |
Daniel Vetter | 90d0a0e | 2014-08-06 15:04:56 +0200 | [diff] [blame] | 2018 | if (drm_mm_initialized(&vm->mm)) { |
Yu Zhang | 5dda8fa | 2015-02-10 19:05:48 +0800 | [diff] [blame] | 2019 | if (intel_vgpu_active(dev)) |
| 2020 | intel_vgt_deballoon(); |
| 2021 | |
Daniel Vetter | 90d0a0e | 2014-08-06 15:04:56 +0200 | [diff] [blame] | 2022 | drm_mm_takedown(&vm->mm); |
| 2023 | list_del(&vm->global_link); |
| 2024 | } |
| 2025 | |
| 2026 | vm->cleanup(vm); |
| 2027 | } |
Daniel Vetter | 70e3254 | 2014-08-06 15:04:57 +0200 | [diff] [blame] | 2028 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2029 | static int setup_scratch_page(struct drm_device *dev) |
| 2030 | { |
| 2031 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2032 | struct page *page; |
| 2033 | dma_addr_t dma_addr; |
| 2034 | |
| 2035 | page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
| 2036 | if (page == NULL) |
| 2037 | return -ENOMEM; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2038 | set_pages_uc(page, 1); |
| 2039 | |
| 2040 | #ifdef CONFIG_INTEL_IOMMU |
| 2041 | dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, |
| 2042 | PCI_DMA_BIDIRECTIONAL); |
| 2043 | if (pci_dma_mapping_error(dev->pdev, dma_addr)) |
| 2044 | return -EINVAL; |
| 2045 | #else |
| 2046 | dma_addr = page_to_phys(page); |
| 2047 | #endif |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2048 | dev_priv->gtt.base.scratch.page = page; |
| 2049 | dev_priv->gtt.base.scratch.addr = dma_addr; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2050 | |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | static void teardown_scratch_page(struct drm_device *dev) |
| 2055 | { |
| 2056 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2057 | struct page *page = dev_priv->gtt.base.scratch.page; |
| 2058 | |
| 2059 | set_pages_wb(page, 1); |
| 2060 | pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr, |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2061 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2062 | __free_page(page); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) |
| 2066 | { |
| 2067 | snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; |
| 2068 | snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; |
| 2069 | return snb_gmch_ctl << 20; |
| 2070 | } |
| 2071 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 2072 | static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) |
| 2073 | { |
| 2074 | bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; |
| 2075 | bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; |
| 2076 | if (bdw_gmch_ctl) |
| 2077 | bdw_gmch_ctl = 1 << bdw_gmch_ctl; |
Ben Widawsky | 562d55d | 2014-05-27 16:53:08 -0700 | [diff] [blame] | 2078 | |
| 2079 | #ifdef CONFIG_X86_32 |
| 2080 | /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */ |
| 2081 | if (bdw_gmch_ctl > 4) |
| 2082 | bdw_gmch_ctl = 4; |
| 2083 | #endif |
| 2084 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 2085 | return bdw_gmch_ctl << 20; |
| 2086 | } |
| 2087 | |
Damien Lespiau | d7f25f2 | 2014-05-08 22:19:40 +0300 | [diff] [blame] | 2088 | static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl) |
| 2089 | { |
| 2090 | gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT; |
| 2091 | gmch_ctrl &= SNB_GMCH_GGMS_MASK; |
| 2092 | |
| 2093 | if (gmch_ctrl) |
| 2094 | return 1 << (20 + gmch_ctrl); |
| 2095 | |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2099 | static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2100 | { |
| 2101 | snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; |
| 2102 | snb_gmch_ctl &= SNB_GMCH_GMS_MASK; |
| 2103 | return snb_gmch_ctl << 25; /* 32 MB units */ |
| 2104 | } |
| 2105 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 2106 | static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) |
| 2107 | { |
| 2108 | bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; |
| 2109 | bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; |
| 2110 | return bdw_gmch_ctl << 25; /* 32 MB units */ |
| 2111 | } |
| 2112 | |
Damien Lespiau | d7f25f2 | 2014-05-08 22:19:40 +0300 | [diff] [blame] | 2113 | static size_t chv_get_stolen_size(u16 gmch_ctrl) |
| 2114 | { |
| 2115 | gmch_ctrl >>= SNB_GMCH_GMS_SHIFT; |
| 2116 | gmch_ctrl &= SNB_GMCH_GMS_MASK; |
| 2117 | |
| 2118 | /* |
| 2119 | * 0x0 to 0x10: 32MB increments starting at 0MB |
| 2120 | * 0x11 to 0x16: 4MB increments starting at 8MB |
| 2121 | * 0x17 to 0x1d: 4MB increments start at 36MB |
| 2122 | */ |
| 2123 | if (gmch_ctrl < 0x11) |
| 2124 | return gmch_ctrl << 25; |
| 2125 | else if (gmch_ctrl < 0x17) |
| 2126 | return (gmch_ctrl - 0x11 + 2) << 22; |
| 2127 | else |
| 2128 | return (gmch_ctrl - 0x17 + 9) << 22; |
| 2129 | } |
| 2130 | |
Damien Lespiau | 6637501 | 2014-01-09 18:02:46 +0000 | [diff] [blame] | 2131 | static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl) |
| 2132 | { |
| 2133 | gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; |
| 2134 | gen9_gmch_ctl &= BDW_GMCH_GMS_MASK; |
| 2135 | |
| 2136 | if (gen9_gmch_ctl < 0xf0) |
| 2137 | return gen9_gmch_ctl << 25; /* 32 MB units */ |
| 2138 | else |
| 2139 | /* 4MB increments starting at 0xf0 for 4MB */ |
| 2140 | return (gen9_gmch_ctl - 0xf0 + 1) << 22; |
| 2141 | } |
| 2142 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2143 | static int ggtt_probe_common(struct drm_device *dev, |
| 2144 | size_t gtt_size) |
| 2145 | { |
| 2146 | struct drm_i915_private *dev_priv = dev->dev_private; |
Bjorn Helgaas | 21c3460 | 2013-12-21 10:52:52 -0700 | [diff] [blame] | 2147 | phys_addr_t gtt_phys_addr; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2148 | int ret; |
| 2149 | |
| 2150 | /* For Modern GENs the PTEs and register space are split in the BAR */ |
Bjorn Helgaas | 21c3460 | 2013-12-21 10:52:52 -0700 | [diff] [blame] | 2151 | gtt_phys_addr = pci_resource_start(dev->pdev, 0) + |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2152 | (pci_resource_len(dev->pdev, 0) / 2); |
| 2153 | |
Bjorn Helgaas | 21c3460 | 2013-12-21 10:52:52 -0700 | [diff] [blame] | 2154 | dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size); |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2155 | if (!dev_priv->gtt.gsm) { |
| 2156 | DRM_ERROR("Failed to map the gtt page table\n"); |
| 2157 | return -ENOMEM; |
| 2158 | } |
| 2159 | |
| 2160 | ret = setup_scratch_page(dev); |
| 2161 | if (ret) { |
| 2162 | DRM_ERROR("Scratch setup failed\n"); |
| 2163 | /* iounmap will also get called at remove, but meh */ |
| 2164 | iounmap(dev_priv->gtt.gsm); |
| 2165 | } |
| 2166 | |
| 2167 | return ret; |
| 2168 | } |
| 2169 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 2170 | /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability |
| 2171 | * bits. When using advanced contexts each context stores its own PAT, but |
| 2172 | * writing this data shouldn't be harmful even in those cases. */ |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 2173 | static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv) |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 2174 | { |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 2175 | uint64_t pat; |
| 2176 | |
| 2177 | pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ |
| 2178 | GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ |
| 2179 | GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */ |
| 2180 | GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ |
| 2181 | GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | |
| 2182 | GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | |
| 2183 | GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | |
| 2184 | GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); |
| 2185 | |
Rodrigo Vivi | d6a8b72 | 2014-11-05 16:56:36 -0800 | [diff] [blame] | 2186 | if (!USES_PPGTT(dev_priv->dev)) |
| 2187 | /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry, |
| 2188 | * so RTL will always use the value corresponding to |
| 2189 | * pat_sel = 000". |
| 2190 | * So let's disable cache for GGTT to avoid screen corruptions. |
| 2191 | * MOCS still can be used though. |
| 2192 | * - System agent ggtt writes (i.e. cpu gtt mmaps) already work |
| 2193 | * before this patch, i.e. the same uncached + snooping access |
| 2194 | * like on gen6/7 seems to be in effect. |
| 2195 | * - So this just fixes blitter/render access. Again it looks |
| 2196 | * like it's not just uncached access, but uncached + snooping. |
| 2197 | * So we can still hold onto all our assumptions wrt cpu |
| 2198 | * clflushing on LLC machines. |
| 2199 | */ |
| 2200 | pat = GEN8_PPAT(0, GEN8_PPAT_UC); |
| 2201 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 2202 | /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b |
| 2203 | * write would work. */ |
| 2204 | I915_WRITE(GEN8_PRIVATE_PAT, pat); |
| 2205 | I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); |
| 2206 | } |
| 2207 | |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 2208 | static void chv_setup_private_ppat(struct drm_i915_private *dev_priv) |
| 2209 | { |
| 2210 | uint64_t pat; |
| 2211 | |
| 2212 | /* |
| 2213 | * Map WB on BDW to snooped on CHV. |
| 2214 | * |
| 2215 | * Only the snoop bit has meaning for CHV, the rest is |
| 2216 | * ignored. |
| 2217 | * |
Ville Syrjälä | cf3d262 | 2014-11-14 21:02:44 +0200 | [diff] [blame] | 2218 | * The hardware will never snoop for certain types of accesses: |
| 2219 | * - CPU GTT (GMADR->GGTT->no snoop->memory) |
| 2220 | * - PPGTT page tables |
| 2221 | * - some other special cycles |
| 2222 | * |
| 2223 | * As with BDW, we also need to consider the following for GT accesses: |
| 2224 | * "For GGTT, there is NO pat_sel[2:0] from the entry, |
| 2225 | * so RTL will always use the value corresponding to |
| 2226 | * pat_sel = 000". |
| 2227 | * Which means we must set the snoop bit in PAT entry 0 |
| 2228 | * in order to keep the global status page working. |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 2229 | */ |
| 2230 | pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) | |
| 2231 | GEN8_PPAT(1, 0) | |
| 2232 | GEN8_PPAT(2, 0) | |
| 2233 | GEN8_PPAT(3, 0) | |
| 2234 | GEN8_PPAT(4, CHV_PPAT_SNOOP) | |
| 2235 | GEN8_PPAT(5, CHV_PPAT_SNOOP) | |
| 2236 | GEN8_PPAT(6, CHV_PPAT_SNOOP) | |
| 2237 | GEN8_PPAT(7, CHV_PPAT_SNOOP); |
| 2238 | |
| 2239 | I915_WRITE(GEN8_PRIVATE_PAT, pat); |
| 2240 | I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); |
| 2241 | } |
| 2242 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2243 | static int gen8_gmch_probe(struct drm_device *dev, |
| 2244 | size_t *gtt_total, |
| 2245 | size_t *stolen, |
| 2246 | phys_addr_t *mappable_base, |
| 2247 | unsigned long *mappable_end) |
| 2248 | { |
| 2249 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2250 | unsigned int gtt_size; |
| 2251 | u16 snb_gmch_ctl; |
| 2252 | int ret; |
| 2253 | |
| 2254 | /* TODO: We're not aware of mappable constraints on gen8 yet */ |
| 2255 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 2256 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 2257 | |
| 2258 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39))) |
| 2259 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39)); |
| 2260 | |
| 2261 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
| 2262 | |
Damien Lespiau | 6637501 | 2014-01-09 18:02:46 +0000 | [diff] [blame] | 2263 | if (INTEL_INFO(dev)->gen >= 9) { |
| 2264 | *stolen = gen9_get_stolen_size(snb_gmch_ctl); |
| 2265 | gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); |
| 2266 | } else if (IS_CHERRYVIEW(dev)) { |
Damien Lespiau | d7f25f2 | 2014-05-08 22:19:40 +0300 | [diff] [blame] | 2267 | *stolen = chv_get_stolen_size(snb_gmch_ctl); |
| 2268 | gtt_size = chv_get_total_gtt_size(snb_gmch_ctl); |
| 2269 | } else { |
| 2270 | *stolen = gen8_get_stolen_size(snb_gmch_ctl); |
| 2271 | gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); |
| 2272 | } |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2273 | |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 2274 | *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2275 | |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 2276 | if (IS_CHERRYVIEW(dev)) |
| 2277 | chv_setup_private_ppat(dev_priv); |
| 2278 | else |
| 2279 | bdw_setup_private_ppat(dev_priv); |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 2280 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2281 | ret = ggtt_probe_common(dev, gtt_size); |
| 2282 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 2283 | dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range; |
| 2284 | dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2285 | |
| 2286 | return ret; |
| 2287 | } |
| 2288 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2289 | static int gen6_gmch_probe(struct drm_device *dev, |
| 2290 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2291 | size_t *stolen, |
| 2292 | phys_addr_t *mappable_base, |
| 2293 | unsigned long *mappable_end) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2294 | { |
| 2295 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2296 | unsigned int gtt_size; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2297 | u16 snb_gmch_ctl; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2298 | int ret; |
| 2299 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2300 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 2301 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 2302 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2303 | /* 64/512MB is the current min/max we actually know of, but this is just |
| 2304 | * a coarse sanity check. |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2305 | */ |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2306 | if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2307 | DRM_ERROR("Unknown GMADR size (%lx)\n", |
| 2308 | dev_priv->gtt.mappable_end); |
| 2309 | return -ENXIO; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2310 | } |
| 2311 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2312 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) |
| 2313 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2314 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2315 | |
Ben Widawsky | c4ae25e | 2013-05-01 11:00:34 -0700 | [diff] [blame] | 2316 | *stolen = gen6_get_stolen_size(snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2317 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2318 | gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); |
Michel Thierry | 07749ef | 2015-03-16 16:00:54 +0000 | [diff] [blame] | 2319 | *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2320 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2321 | ret = ggtt_probe_common(dev, gtt_size); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2322 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2323 | dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; |
| 2324 | dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2325 | |
| 2326 | return ret; |
| 2327 | } |
| 2328 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2329 | static void gen6_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2330 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2331 | |
| 2332 | struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); |
Ben Widawsky | 5ed1678 | 2013-11-25 09:54:43 -0800 | [diff] [blame] | 2333 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2334 | iounmap(gtt->gsm); |
| 2335 | teardown_scratch_page(vm->dev); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | static int i915_gmch_probe(struct drm_device *dev, |
| 2339 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2340 | size_t *stolen, |
| 2341 | phys_addr_t *mappable_base, |
| 2342 | unsigned long *mappable_end) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2343 | { |
| 2344 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2345 | int ret; |
| 2346 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2347 | ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); |
| 2348 | if (!ret) { |
| 2349 | DRM_ERROR("failed to set up gmch\n"); |
| 2350 | return -EIO; |
| 2351 | } |
| 2352 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2353 | intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2354 | |
| 2355 | dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2356 | dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2357 | |
Chris Wilson | c0a7f81 | 2013-12-30 12:16:15 +0000 | [diff] [blame] | 2358 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
| 2359 | DRM_INFO("applying Ironlake quirks for intel_iommu\n"); |
| 2360 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2361 | return 0; |
| 2362 | } |
| 2363 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2364 | static void i915_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2365 | { |
| 2366 | intel_gmch_remove(); |
| 2367 | } |
| 2368 | |
| 2369 | int i915_gem_gtt_init(struct drm_device *dev) |
| 2370 | { |
| 2371 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2372 | struct i915_gtt *gtt = &dev_priv->gtt; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2373 | int ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2374 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2375 | if (INTEL_INFO(dev)->gen <= 5) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2376 | gtt->gtt_probe = i915_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2377 | gtt->base.cleanup = i915_gmch_remove; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2378 | } else if (INTEL_INFO(dev)->gen < 8) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2379 | gtt->gtt_probe = gen6_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2380 | gtt->base.cleanup = gen6_gmch_remove; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 2381 | if (IS_HASWELL(dev) && dev_priv->ellc_size) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2382 | gtt->base.pte_encode = iris_pte_encode; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 2383 | else if (IS_HASWELL(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2384 | gtt->base.pte_encode = hsw_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2385 | else if (IS_VALLEYVIEW(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2386 | gtt->base.pte_encode = byt_pte_encode; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 2387 | else if (INTEL_INFO(dev)->gen >= 7) |
| 2388 | gtt->base.pte_encode = ivb_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2389 | else |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 2390 | gtt->base.pte_encode = snb_pte_encode; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2391 | } else { |
| 2392 | dev_priv->gtt.gtt_probe = gen8_gmch_probe; |
| 2393 | dev_priv->gtt.base.cleanup = gen6_gmch_remove; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2396 | ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size, |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2397 | >t->mappable_base, >t->mappable_end); |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 2398 | if (ret) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2399 | return ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2400 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2401 | gtt->base.dev = dev; |
| 2402 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2403 | /* GMADR is the PCI mmio aperture into the global GTT. */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2404 | DRM_INFO("Memory usable by graphics device = %zdM\n", |
| 2405 | gtt->base.total >> 20); |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2406 | DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20); |
| 2407 | DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); |
Daniel Vetter | 5db6c73 | 2014-03-31 16:23:04 +0200 | [diff] [blame] | 2408 | #ifdef CONFIG_INTEL_IOMMU |
| 2409 | if (intel_iommu_gfx_mapped) |
| 2410 | DRM_INFO("VT-d active for gfx access\n"); |
| 2411 | #endif |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 2412 | /* |
| 2413 | * i915.enable_ppgtt is read-only, so do an early pass to validate the |
| 2414 | * user's requested state against the hardware/driver capabilities. We |
| 2415 | * do this now so that we can print out any log messages once rather |
| 2416 | * than every time we check intel_enable_ppgtt(). |
| 2417 | */ |
| 2418 | i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt); |
| 2419 | DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 2420 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2421 | return 0; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 2422 | } |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2423 | |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2424 | static struct i915_vma * |
| 2425 | __i915_gem_vma_create(struct drm_i915_gem_object *obj, |
| 2426 | struct i915_address_space *vm, |
| 2427 | const struct i915_ggtt_view *ggtt_view) |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2428 | { |
Dan Carpenter | dabde5c | 2015-03-18 11:21:58 +0300 | [diff] [blame] | 2429 | struct i915_vma *vma; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2430 | |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2431 | if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view)) |
| 2432 | return ERR_PTR(-EINVAL); |
Dan Carpenter | dabde5c | 2015-03-18 11:21:58 +0300 | [diff] [blame] | 2433 | vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 2434 | if (vma == NULL) |
| 2435 | return ERR_PTR(-ENOMEM); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2436 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2437 | INIT_LIST_HEAD(&vma->vma_link); |
| 2438 | INIT_LIST_HEAD(&vma->mm_list); |
| 2439 | INIT_LIST_HEAD(&vma->exec_list); |
| 2440 | vma->vm = vm; |
| 2441 | vma->obj = obj; |
| 2442 | |
Rodrigo Vivi | b1252bc | 2014-12-03 04:55:29 -0800 | [diff] [blame] | 2443 | if (INTEL_INFO(vm->dev)->gen >= 6) { |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 2444 | if (i915_is_ggtt(vm)) { |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2445 | vma->ggtt_view = *ggtt_view; |
| 2446 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 2447 | vma->unbind_vma = ggtt_unbind_vma; |
| 2448 | vma->bind_vma = ggtt_bind_vma; |
| 2449 | } else { |
| 2450 | vma->unbind_vma = ppgtt_unbind_vma; |
| 2451 | vma->bind_vma = ppgtt_bind_vma; |
| 2452 | } |
Rodrigo Vivi | b1252bc | 2014-12-03 04:55:29 -0800 | [diff] [blame] | 2453 | } else { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2454 | BUG_ON(!i915_is_ggtt(vm)); |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2455 | vma->ggtt_view = *ggtt_view; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2456 | vma->unbind_vma = i915_ggtt_unbind_vma; |
| 2457 | vma->bind_vma = i915_ggtt_bind_vma; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2458 | } |
| 2459 | |
Tvrtko Ursulin | f763566 | 2014-12-03 14:59:24 +0000 | [diff] [blame] | 2460 | list_add_tail(&vma->vma_link, &obj->vma_list); |
| 2461 | if (!i915_is_ggtt(vm)) |
Michel Thierry | e07f055 | 2014-08-19 15:49:41 +0100 | [diff] [blame] | 2462 | i915_ppgtt_get(i915_vm_to_ppgtt(vm)); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2463 | |
| 2464 | return vma; |
| 2465 | } |
| 2466 | |
| 2467 | struct i915_vma * |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2468 | i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj, |
| 2469 | struct i915_address_space *vm) |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2470 | { |
| 2471 | struct i915_vma *vma; |
| 2472 | |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2473 | vma = i915_gem_obj_to_vma(obj, vm); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2474 | if (!vma) |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2475 | vma = __i915_gem_vma_create(obj, vm, |
| 2476 | i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2477 | |
| 2478 | return vma; |
| 2479 | } |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 2480 | |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2481 | struct i915_vma * |
| 2482 | i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj, |
| 2483 | const struct i915_ggtt_view *view) |
| 2484 | { |
| 2485 | struct i915_address_space *ggtt = i915_obj_to_ggtt(obj); |
| 2486 | struct i915_vma *vma; |
| 2487 | |
| 2488 | if (WARN_ON(!view)) |
| 2489 | return ERR_PTR(-EINVAL); |
| 2490 | |
| 2491 | vma = i915_gem_obj_to_ggtt_view(obj, view); |
| 2492 | |
| 2493 | if (IS_ERR(vma)) |
| 2494 | return vma; |
| 2495 | |
| 2496 | if (!vma) |
| 2497 | vma = __i915_gem_vma_create(obj, ggtt, view); |
| 2498 | |
| 2499 | return vma; |
| 2500 | |
| 2501 | } |
| 2502 | |
| 2503 | |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 2504 | static inline |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2505 | int i915_get_ggtt_vma_pages(struct i915_vma *vma) |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 2506 | { |
| 2507 | if (vma->ggtt_view.pages) |
| 2508 | return 0; |
| 2509 | |
| 2510 | if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) |
| 2511 | vma->ggtt_view.pages = vma->obj->pages; |
| 2512 | else |
| 2513 | WARN_ONCE(1, "GGTT view %u not implemented!\n", |
| 2514 | vma->ggtt_view.type); |
| 2515 | |
| 2516 | if (!vma->ggtt_view.pages) { |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2517 | DRM_ERROR("Failed to get pages for GGTT view type %u!\n", |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 2518 | vma->ggtt_view.type); |
| 2519 | return -EINVAL; |
| 2520 | } |
| 2521 | |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | /** |
| 2526 | * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. |
| 2527 | * @vma: VMA to map |
| 2528 | * @cache_level: mapping cache level |
| 2529 | * @flags: flags like global or local mapping |
| 2530 | * |
| 2531 | * DMA addresses are taken from the scatter-gather table of this object (or of |
| 2532 | * this VMA in case of non-default GGTT views) and PTE entries set up. |
| 2533 | * Note that DMA addresses are also the only part of the SG table we care about. |
| 2534 | */ |
| 2535 | int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level, |
| 2536 | u32 flags) |
| 2537 | { |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2538 | if (i915_is_ggtt(vma->vm)) { |
| 2539 | int ret = i915_get_ggtt_vma_pages(vma); |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 2540 | |
Joonas Lahtinen | ec7adb6 | 2015-03-16 14:11:13 +0200 | [diff] [blame] | 2541 | if (ret) |
| 2542 | return ret; |
| 2543 | } |
Tvrtko Ursulin | fe14d5f | 2014-12-10 17:27:58 +0000 | [diff] [blame] | 2544 | |
| 2545 | vma->bind_vma(vma, cache_level, flags); |
| 2546 | |
| 2547 | return 0; |
| 2548 | } |