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