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