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