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" |
| 30 | #include "i915_trace.h" |
| 31 | #include "intel_drv.h" |
| 32 | |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 33 | static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv); |
| 34 | static void chv_setup_private_ppat(struct drm_i915_private *dev_priv); |
Ben Widawsky | a2319c0 | 2014-03-18 16:09:37 -0700 | [diff] [blame] | 35 | |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 36 | static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt) |
| 37 | { |
| 38 | if (enable_ppgtt == 0 || !HAS_ALIASING_PPGTT(dev)) |
| 39 | return 0; |
| 40 | |
| 41 | if (enable_ppgtt == 1) |
| 42 | return 1; |
| 43 | |
| 44 | if (enable_ppgtt == 2 && HAS_PPGTT(dev)) |
| 45 | return 2; |
| 46 | |
Daniel Vetter | 93a25a9 | 2014-03-06 09:40:43 +0100 | [diff] [blame] | 47 | #ifdef CONFIG_INTEL_IOMMU |
| 48 | /* Disable ppgtt on SNB if VT-d is on. */ |
| 49 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) { |
| 50 | DRM_INFO("Disabling PPGTT because VT-d is on\n"); |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 51 | return 0; |
Daniel Vetter | 93a25a9 | 2014-03-06 09:40:43 +0100 | [diff] [blame] | 52 | } |
| 53 | #endif |
| 54 | |
Jesse Barnes | 62942ed | 2014-06-13 09:28:33 -0700 | [diff] [blame] | 55 | /* Early VLV doesn't have this */ |
Ville Syrjälä | ca2aed6c | 2014-06-28 02:03:56 +0300 | [diff] [blame] | 56 | if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && |
| 57 | dev->pdev->revision < 0xb) { |
Jesse Barnes | 62942ed | 2014-06-13 09:28:33 -0700 | [diff] [blame] | 58 | DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 62 | return HAS_ALIASING_PPGTT(dev) ? 1 : 0; |
Daniel Vetter | 93a25a9 | 2014-03-06 09:40:43 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 65 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 66 | static void ppgtt_bind_vma(struct i915_vma *vma, |
| 67 | enum i915_cache_level cache_level, |
| 68 | u32 flags); |
| 69 | static void ppgtt_unbind_vma(struct i915_vma *vma); |
| 70 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 71 | static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr, |
| 72 | enum i915_cache_level level, |
| 73 | bool valid) |
| 74 | { |
| 75 | gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0; |
| 76 | pte |= addr; |
Ben Widawsky | 63c42e5 | 2014-04-18 18:04:27 -0300 | [diff] [blame] | 77 | |
| 78 | switch (level) { |
| 79 | case I915_CACHE_NONE: |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 80 | pte |= PPAT_UNCACHED_INDEX; |
Ben Widawsky | 63c42e5 | 2014-04-18 18:04:27 -0300 | [diff] [blame] | 81 | break; |
| 82 | case I915_CACHE_WT: |
| 83 | pte |= PPAT_DISPLAY_ELLC_INDEX; |
| 84 | break; |
| 85 | default: |
| 86 | pte |= PPAT_CACHED_INDEX; |
| 87 | break; |
| 88 | } |
| 89 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 90 | return pte; |
| 91 | } |
| 92 | |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 93 | static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev, |
| 94 | dma_addr_t addr, |
| 95 | enum i915_cache_level level) |
| 96 | { |
| 97 | gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW; |
| 98 | pde |= addr; |
| 99 | if (level != I915_CACHE_NONE) |
| 100 | pde |= PPAT_CACHED_PDE_INDEX; |
| 101 | else |
| 102 | pde |= PPAT_UNCACHED_INDEX; |
| 103 | return pde; |
| 104 | } |
| 105 | |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 106 | static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 107 | enum i915_cache_level level, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 108 | bool valid, u32 unused) |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 109 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 110 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 111 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 112 | |
| 113 | switch (level) { |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 114 | case I915_CACHE_L3_LLC: |
| 115 | case I915_CACHE_LLC: |
| 116 | pte |= GEN6_PTE_CACHE_LLC; |
| 117 | break; |
| 118 | case I915_CACHE_NONE: |
| 119 | pte |= GEN6_PTE_UNCACHED; |
| 120 | break; |
| 121 | default: |
| 122 | WARN_ON(1); |
| 123 | } |
| 124 | |
| 125 | return pte; |
| 126 | } |
| 127 | |
| 128 | static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 129 | enum i915_cache_level level, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 130 | bool valid, u32 unused) |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 131 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 132 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 133 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 134 | |
| 135 | switch (level) { |
| 136 | case I915_CACHE_L3_LLC: |
| 137 | pte |= GEN7_PTE_CACHE_L3_LLC; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 138 | break; |
| 139 | case I915_CACHE_LLC: |
| 140 | pte |= GEN6_PTE_CACHE_LLC; |
| 141 | break; |
| 142 | case I915_CACHE_NONE: |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 143 | pte |= GEN6_PTE_UNCACHED; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 144 | break; |
| 145 | default: |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 146 | WARN_ON(1); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 149 | return pte; |
| 150 | } |
| 151 | |
Ben Widawsky | 80a74f7 | 2013-06-27 16:30:19 -0700 | [diff] [blame] | 152 | static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 153 | enum i915_cache_level level, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 154 | bool valid, u32 flags) |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 155 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 156 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 157 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 158 | |
| 159 | /* Mark the page as writeable. Other platforms don't have a |
| 160 | * setting for read-only/writable, so this matches that behavior. |
| 161 | */ |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 162 | if (!(flags & PTE_READ_ONLY)) |
| 163 | pte |= BYT_PTE_WRITEABLE; |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 164 | |
| 165 | if (level != I915_CACHE_NONE) |
| 166 | pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; |
| 167 | |
| 168 | return pte; |
| 169 | } |
| 170 | |
Ben Widawsky | 80a74f7 | 2013-06-27 16:30:19 -0700 | [diff] [blame] | 171 | static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 172 | enum i915_cache_level level, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 173 | bool valid, u32 unused) |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 174 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 175 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 176 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 177 | |
| 178 | if (level != I915_CACHE_NONE) |
Ben Widawsky | 87a6b68 | 2013-08-04 23:47:29 -0700 | [diff] [blame] | 179 | pte |= HSW_WB_LLC_AGE3; |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 180 | |
| 181 | return pte; |
| 182 | } |
| 183 | |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 184 | static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 185 | enum i915_cache_level level, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 186 | bool valid, u32 unused) |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 187 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 188 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 189 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
| 190 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 191 | switch (level) { |
| 192 | case I915_CACHE_NONE: |
| 193 | break; |
| 194 | case I915_CACHE_WT: |
Chris Wilson | c51e970 | 2013-11-22 10:37:53 +0000 | [diff] [blame] | 195 | pte |= HSW_WT_ELLC_LLC_AGE3; |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 196 | break; |
| 197 | default: |
Chris Wilson | c51e970 | 2013-11-22 10:37:53 +0000 | [diff] [blame] | 198 | pte |= HSW_WB_ELLC_LLC_AGE3; |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 199 | break; |
| 200 | } |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 201 | |
| 202 | return pte; |
| 203 | } |
| 204 | |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 205 | /* Broadwell Page Directory Pointer Descriptors */ |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 206 | static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 207 | uint64_t val) |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 208 | { |
| 209 | int ret; |
| 210 | |
| 211 | BUG_ON(entry >= 4); |
| 212 | |
| 213 | ret = intel_ring_begin(ring, 6); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 218 | intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry)); |
| 219 | intel_ring_emit(ring, (u32)(val >> 32)); |
| 220 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 221 | intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry)); |
| 222 | intel_ring_emit(ring, (u32)(val)); |
| 223 | intel_ring_advance(ring); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 228 | static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 229 | struct intel_engine_cs *ring) |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 230 | { |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 231 | int i, ret; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 232 | |
| 233 | /* bit of a hack to find the actual last used pd */ |
| 234 | int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE; |
| 235 | |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 236 | for (i = used_pd - 1; i >= 0; i--) { |
| 237 | dma_addr_t addr = ppgtt->pd_dma_addr[i]; |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 238 | ret = gen8_write_pdp(ring, i, addr); |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 239 | if (ret) |
| 240 | return ret; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 241 | } |
Ben Widawsky | d595bd4 | 2013-11-25 09:54:32 -0800 | [diff] [blame] | 242 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 243 | return 0; |
Ben Widawsky | 94e409c | 2013-11-04 22:29:36 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 246 | static void gen8_ppgtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 247 | uint64_t start, |
| 248 | uint64_t length, |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 249 | bool use_scratch) |
| 250 | { |
| 251 | struct i915_hw_ppgtt *ppgtt = |
| 252 | container_of(vm, struct i915_hw_ppgtt, base); |
| 253 | gen8_gtt_pte_t *pt_vaddr, scratch_pte; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 254 | unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK; |
| 255 | unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK; |
| 256 | unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 257 | unsigned num_entries = length >> PAGE_SHIFT; |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 258 | unsigned last_pte, i; |
| 259 | |
| 260 | scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr, |
| 261 | I915_CACHE_LLC, use_scratch); |
| 262 | |
| 263 | while (num_entries) { |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 264 | struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde]; |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 265 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 266 | last_pte = pte + num_entries; |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 267 | if (last_pte > GEN8_PTES_PER_PAGE) |
| 268 | last_pte = GEN8_PTES_PER_PAGE; |
| 269 | |
| 270 | pt_vaddr = kmap_atomic(page_table); |
| 271 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 272 | for (i = pte; i < last_pte; i++) { |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 273 | pt_vaddr[i] = scratch_pte; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 274 | num_entries--; |
| 275 | } |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 276 | |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 277 | if (!HAS_LLC(ppgtt->base.dev)) |
| 278 | drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 279 | kunmap_atomic(pt_vaddr); |
| 280 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 281 | pte = 0; |
| 282 | if (++pde == GEN8_PDES_PER_PAGE) { |
| 283 | pdpe++; |
| 284 | pde = 0; |
| 285 | } |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 289 | static void gen8_ppgtt_insert_entries(struct i915_address_space *vm, |
| 290 | struct sg_table *pages, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 291 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 292 | enum i915_cache_level cache_level, u32 unused) |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 293 | { |
| 294 | struct i915_hw_ppgtt *ppgtt = |
| 295 | container_of(vm, struct i915_hw_ppgtt, base); |
| 296 | gen8_gtt_pte_t *pt_vaddr; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 297 | unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK; |
| 298 | unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK; |
| 299 | unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK; |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 300 | struct sg_page_iter sg_iter; |
| 301 | |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 302 | pt_vaddr = NULL; |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 303 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 304 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
| 305 | if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS)) |
| 306 | break; |
| 307 | |
| 308 | if (pt_vaddr == NULL) |
| 309 | pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]); |
| 310 | |
| 311 | pt_vaddr[pte] = |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 312 | gen8_pte_encode(sg_page_iter_dma_address(&sg_iter), |
| 313 | cache_level, true); |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 314 | if (++pte == GEN8_PTES_PER_PAGE) { |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 315 | if (!HAS_LLC(ppgtt->base.dev)) |
| 316 | drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 317 | kunmap_atomic(pt_vaddr); |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 318 | pt_vaddr = NULL; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 319 | if (++pde == GEN8_PDES_PER_PAGE) { |
| 320 | pdpe++; |
| 321 | pde = 0; |
| 322 | } |
| 323 | pte = 0; |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 324 | } |
| 325 | } |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 326 | if (pt_vaddr) { |
| 327 | if (!HAS_LLC(ppgtt->base.dev)) |
| 328 | drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); |
Chris Wilson | 6f1cc99 | 2013-12-31 15:50:31 +0000 | [diff] [blame] | 329 | kunmap_atomic(pt_vaddr); |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 330 | } |
Ben Widawsky | 9df15b4 | 2013-11-02 21:07:24 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 333 | static void gen8_free_page_tables(struct page **pt_pages) |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 334 | { |
| 335 | int i; |
| 336 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 337 | if (pt_pages == NULL) |
| 338 | return; |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 339 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 340 | for (i = 0; i < GEN8_PDES_PER_PAGE; i++) |
| 341 | if (pt_pages[i]) |
| 342 | __free_pages(pt_pages[i], 0); |
| 343 | } |
| 344 | |
| 345 | static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt) |
| 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 0; i < ppgtt->num_pd_pages; i++) { |
| 350 | gen8_free_page_tables(ppgtt->gen8_pt_pages[i]); |
| 351 | kfree(ppgtt->gen8_pt_pages[i]); |
| 352 | kfree(ppgtt->gen8_pt_dma_addr[i]); |
| 353 | } |
| 354 | |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 355 | __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT)); |
| 356 | } |
| 357 | |
| 358 | static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt) |
| 359 | { |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 360 | struct pci_dev *hwdev = ppgtt->base.dev->pdev; |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 361 | int i, j; |
| 362 | |
| 363 | for (i = 0; i < ppgtt->num_pd_pages; i++) { |
| 364 | /* TODO: In the future we'll support sparse mappings, so this |
| 365 | * will have to change. */ |
| 366 | if (!ppgtt->pd_dma_addr[i]) |
| 367 | continue; |
| 368 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 369 | pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE, |
| 370 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 371 | |
| 372 | for (j = 0; j < GEN8_PDES_PER_PAGE; j++) { |
| 373 | dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j]; |
| 374 | if (addr) |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 375 | pci_unmap_page(hwdev, addr, PAGE_SIZE, |
| 376 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 381 | static void gen8_ppgtt_cleanup(struct i915_address_space *vm) |
| 382 | { |
| 383 | struct i915_hw_ppgtt *ppgtt = |
| 384 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 385 | |
Ben Widawsky | b45a671 | 2014-02-12 14:28:44 -0800 | [diff] [blame] | 386 | gen8_ppgtt_unmap_pages(ppgtt); |
| 387 | gen8_ppgtt_free(ppgtt); |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 390 | static struct page **__gen8_alloc_page_tables(void) |
| 391 | { |
| 392 | struct page **pt_pages; |
| 393 | int i; |
| 394 | |
| 395 | pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL); |
| 396 | if (!pt_pages) |
| 397 | return ERR_PTR(-ENOMEM); |
| 398 | |
| 399 | for (i = 0; i < GEN8_PDES_PER_PAGE; i++) { |
| 400 | pt_pages[i] = alloc_page(GFP_KERNEL); |
| 401 | if (!pt_pages[i]) |
| 402 | goto bail; |
| 403 | } |
| 404 | |
| 405 | return pt_pages; |
| 406 | |
| 407 | bail: |
| 408 | gen8_free_page_tables(pt_pages); |
| 409 | kfree(pt_pages); |
| 410 | return ERR_PTR(-ENOMEM); |
| 411 | } |
| 412 | |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 413 | static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt, |
| 414 | const int max_pdp) |
| 415 | { |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 416 | struct page **pt_pages[GEN8_LEGACY_PDPS]; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 417 | int i, ret; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 418 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 419 | for (i = 0; i < max_pdp; i++) { |
| 420 | pt_pages[i] = __gen8_alloc_page_tables(); |
| 421 | if (IS_ERR(pt_pages[i])) { |
| 422 | ret = PTR_ERR(pt_pages[i]); |
| 423 | goto unwind_out; |
| 424 | } |
| 425 | } |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 426 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 427 | /* NB: Avoid touching gen8_pt_pages until last to keep the allocation, |
| 428 | * "atomic" - for cleanup purposes. |
| 429 | */ |
| 430 | for (i = 0; i < max_pdp; i++) |
| 431 | ppgtt->gen8_pt_pages[i] = pt_pages[i]; |
| 432 | |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 433 | return 0; |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 434 | |
| 435 | unwind_out: |
| 436 | while (i--) { |
| 437 | gen8_free_page_tables(pt_pages[i]); |
| 438 | kfree(pt_pages[i]); |
| 439 | } |
| 440 | |
| 441 | return ret; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt) |
| 445 | { |
| 446 | int i; |
| 447 | |
| 448 | for (i = 0; i < ppgtt->num_pd_pages; i++) { |
| 449 | ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE, |
| 450 | sizeof(dma_addr_t), |
| 451 | GFP_KERNEL); |
| 452 | if (!ppgtt->gen8_pt_dma_addr[i]) |
| 453 | return -ENOMEM; |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt, |
| 460 | const int max_pdp) |
| 461 | { |
| 462 | ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT)); |
| 463 | if (!ppgtt->pd_pages) |
| 464 | return -ENOMEM; |
| 465 | |
| 466 | ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT); |
| 467 | BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS); |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt, |
| 473 | const int max_pdp) |
| 474 | { |
| 475 | int ret; |
| 476 | |
| 477 | ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | |
| 481 | ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp); |
| 482 | if (ret) { |
| 483 | __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT)); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE; |
| 488 | |
| 489 | ret = gen8_ppgtt_allocate_dma(ppgtt); |
| 490 | if (ret) |
| 491 | gen8_ppgtt_free(ppgtt); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
| 496 | static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt, |
| 497 | const int pd) |
| 498 | { |
| 499 | dma_addr_t pd_addr; |
| 500 | int ret; |
| 501 | |
| 502 | pd_addr = pci_map_page(ppgtt->base.dev->pdev, |
| 503 | &ppgtt->pd_pages[pd], 0, |
| 504 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 505 | |
| 506 | ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr); |
| 507 | if (ret) |
| 508 | return ret; |
| 509 | |
| 510 | ppgtt->pd_dma_addr[pd] = pd_addr; |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt, |
| 516 | const int pd, |
| 517 | const int pt) |
| 518 | { |
| 519 | dma_addr_t pt_addr; |
| 520 | struct page *p; |
| 521 | int ret; |
| 522 | |
Ben Widawsky | 7ad47cf | 2014-02-20 11:51:21 -0800 | [diff] [blame] | 523 | p = ppgtt->gen8_pt_pages[pd][pt]; |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 524 | pt_addr = pci_map_page(ppgtt->base.dev->pdev, |
| 525 | p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 526 | ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr); |
| 527 | if (ret) |
| 528 | return ret; |
| 529 | |
| 530 | ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 535 | /** |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 536 | * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers |
| 537 | * with a net effect resembling a 2-level page table in normal x86 terms. Each |
| 538 | * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address |
| 539 | * space. |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 540 | * |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 541 | * FIXME: split allocation into smaller pieces. For now we only ever do this |
| 542 | * once, but with full PPGTT, the multiple contiguous allocations will be bad. |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 543 | * TODO: Do something with the size parameter |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 544 | */ |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 545 | static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size) |
| 546 | { |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 547 | const int max_pdp = DIV_ROUND_UP(size, 1 << 30); |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 548 | const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp; |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 549 | int i, j, ret; |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 550 | |
| 551 | if (size % (1<<30)) |
| 552 | DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size); |
| 553 | |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 554 | /* 1. Do all our allocations for page directories and page tables. */ |
| 555 | ret = gen8_ppgtt_alloc(ppgtt, max_pdp); |
| 556 | if (ret) |
| 557 | return ret; |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 558 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 559 | /* |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 560 | * 2. Create DMA mappings for the page directories and page tables. |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 561 | */ |
| 562 | for (i = 0; i < max_pdp; i++) { |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 563 | ret = gen8_ppgtt_setup_page_directories(ppgtt, i); |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 564 | if (ret) |
| 565 | goto bail; |
| 566 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 567 | for (j = 0; j < GEN8_PDES_PER_PAGE; j++) { |
Ben Widawsky | bf2b4ed | 2014-02-19 22:05:43 -0800 | [diff] [blame] | 568 | ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j); |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 569 | if (ret) |
| 570 | goto bail; |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * 3. Map all the page directory entires to point to the page tables |
| 576 | * we've allocated. |
| 577 | * |
| 578 | * For now, the PPGTT helper functions all require that the PDEs are |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 579 | * plugged in correctly. So we do that now/here. For aliasing PPGTT, we |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 580 | * will never need to touch the PDEs again. |
| 581 | */ |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 582 | for (i = 0; i < max_pdp; i++) { |
| 583 | gen8_ppgtt_pde_t *pd_vaddr; |
| 584 | pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]); |
| 585 | for (j = 0; j < GEN8_PDES_PER_PAGE; j++) { |
| 586 | dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j]; |
| 587 | pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr, |
| 588 | I915_CACHE_LLC); |
| 589 | } |
Rafael Barbalho | fd1ab8f | 2014-04-09 13:28:02 +0300 | [diff] [blame] | 590 | if (!HAS_LLC(ppgtt->base.dev)) |
| 591 | drm_clflush_virt_range(pd_vaddr, PAGE_SIZE); |
Ben Widawsky | b1fe667 | 2013-11-04 21:20:14 -0800 | [diff] [blame] | 592 | kunmap_atomic(pd_vaddr); |
| 593 | } |
| 594 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 595 | ppgtt->switch_mm = gen8_mm_switch; |
| 596 | ppgtt->base.clear_range = gen8_ppgtt_clear_range; |
| 597 | ppgtt->base.insert_entries = gen8_ppgtt_insert_entries; |
| 598 | ppgtt->base.cleanup = gen8_ppgtt_cleanup; |
| 599 | ppgtt->base.start = 0; |
Ben Widawsky | 5abbcca | 2014-02-21 13:06:34 -0800 | [diff] [blame] | 600 | ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE; |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 601 | |
Ben Widawsky | 5abbcca | 2014-02-21 13:06:34 -0800 | [diff] [blame] | 602 | ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true); |
Ben Widawsky | 459108b | 2013-11-02 21:07:23 -0700 | [diff] [blame] | 603 | |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 604 | DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n", |
| 605 | ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp); |
| 606 | DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n", |
Ben Widawsky | 5abbcca | 2014-02-21 13:06:34 -0800 | [diff] [blame] | 607 | ppgtt->num_pd_entries, |
| 608 | (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30)); |
Ben Widawsky | 28cf541 | 2013-11-02 21:07:26 -0700 | [diff] [blame] | 609 | return 0; |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 610 | |
Ben Widawsky | f3a964b | 2014-02-19 22:05:42 -0800 | [diff] [blame] | 611 | bail: |
| 612 | gen8_ppgtt_unmap_pages(ppgtt); |
| 613 | gen8_ppgtt_free(ppgtt); |
Ben Widawsky | 37aca44 | 2013-11-04 20:47:32 -0800 | [diff] [blame] | 614 | return ret; |
| 615 | } |
| 616 | |
Ben Widawsky | 87d60b6 | 2013-12-06 14:11:29 -0800 | [diff] [blame] | 617 | static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m) |
| 618 | { |
| 619 | struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; |
| 620 | struct i915_address_space *vm = &ppgtt->base; |
| 621 | gen6_gtt_pte_t __iomem *pd_addr; |
| 622 | gen6_gtt_pte_t scratch_pte; |
| 623 | uint32_t pd_entry; |
| 624 | int pte, pde; |
| 625 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 626 | 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] | 627 | |
| 628 | pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + |
| 629 | ppgtt->pd_offset / sizeof(gen6_gtt_pte_t); |
| 630 | |
| 631 | seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm, |
| 632 | ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries); |
| 633 | for (pde = 0; pde < ppgtt->num_pd_entries; pde++) { |
| 634 | u32 expected; |
| 635 | gen6_gtt_pte_t *pt_vaddr; |
| 636 | dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde]; |
| 637 | pd_entry = readl(pd_addr + pde); |
| 638 | expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID); |
| 639 | |
| 640 | if (pd_entry != expected) |
| 641 | seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n", |
| 642 | pde, |
| 643 | pd_entry, |
| 644 | expected); |
| 645 | seq_printf(m, "\tPDE: %x\n", pd_entry); |
| 646 | |
| 647 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]); |
| 648 | for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) { |
| 649 | unsigned long va = |
| 650 | (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) + |
| 651 | (pte * PAGE_SIZE); |
| 652 | int i; |
| 653 | bool found = false; |
| 654 | for (i = 0; i < 4; i++) |
| 655 | if (pt_vaddr[pte + i] != scratch_pte) |
| 656 | found = true; |
| 657 | if (!found) |
| 658 | continue; |
| 659 | |
| 660 | seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte); |
| 661 | for (i = 0; i < 4; i++) { |
| 662 | if (pt_vaddr[pte + i] != scratch_pte) |
| 663 | seq_printf(m, " %08x", pt_vaddr[pte + i]); |
| 664 | else |
| 665 | seq_puts(m, " SCRATCH "); |
| 666 | } |
| 667 | seq_puts(m, "\n"); |
| 668 | } |
| 669 | kunmap_atomic(pt_vaddr); |
| 670 | } |
| 671 | } |
| 672 | |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 673 | static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt) |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 674 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 675 | struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 676 | gen6_gtt_pte_t __iomem *pd_addr; |
| 677 | uint32_t pd_entry; |
| 678 | int i; |
| 679 | |
Ben Widawsky | 0a73287 | 2013-04-23 23:15:30 -0700 | [diff] [blame] | 680 | WARN_ON(ppgtt->pd_offset & 0x3f); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 681 | pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm + |
| 682 | ppgtt->pd_offset / sizeof(gen6_gtt_pte_t); |
| 683 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 684 | dma_addr_t pt_addr; |
| 685 | |
| 686 | pt_addr = ppgtt->pt_dma_addr[i]; |
| 687 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr); |
| 688 | pd_entry |= GEN6_PDE_VALID; |
| 689 | |
| 690 | writel(pd_entry, pd_addr + i); |
| 691 | } |
| 692 | readl(pd_addr); |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 695 | static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt) |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 696 | { |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 697 | BUG_ON(ppgtt->pd_offset & 0x3f); |
| 698 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 699 | return (ppgtt->pd_offset / 64) << 16; |
| 700 | } |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 701 | |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 702 | static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 703 | struct intel_engine_cs *ring) |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 704 | { |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 705 | int ret; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 706 | |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 707 | /* NB: TLBs must be flushed and invalidated before a switch */ |
| 708 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 709 | if (ret) |
| 710 | return ret; |
| 711 | |
| 712 | ret = intel_ring_begin(ring, 6); |
| 713 | if (ret) |
| 714 | return ret; |
| 715 | |
| 716 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); |
| 717 | intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); |
| 718 | intel_ring_emit(ring, PP_DIR_DCLV_2G); |
| 719 | intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); |
| 720 | intel_ring_emit(ring, get_pd_offset(ppgtt)); |
| 721 | intel_ring_emit(ring, MI_NOOP); |
| 722 | intel_ring_advance(ring); |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 727 | static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 728 | struct intel_engine_cs *ring) |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 729 | { |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 730 | int ret; |
| 731 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 732 | /* NB: TLBs must be flushed and invalidated before a switch */ |
| 733 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 734 | if (ret) |
| 735 | return ret; |
| 736 | |
| 737 | ret = intel_ring_begin(ring, 6); |
| 738 | if (ret) |
| 739 | return ret; |
| 740 | |
| 741 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); |
| 742 | intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); |
| 743 | intel_ring_emit(ring, PP_DIR_DCLV_2G); |
| 744 | intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); |
| 745 | intel_ring_emit(ring, get_pd_offset(ppgtt)); |
| 746 | intel_ring_emit(ring, MI_NOOP); |
| 747 | intel_ring_advance(ring); |
| 748 | |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 749 | /* XXX: RCS is the only one to auto invalidate the TLBs? */ |
| 750 | if (ring->id != RCS) { |
| 751 | ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | } |
| 755 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 756 | return 0; |
| 757 | } |
| 758 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 759 | static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt, |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 760 | struct intel_engine_cs *ring) |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 761 | { |
| 762 | struct drm_device *dev = ppgtt->base.dev; |
| 763 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 764 | |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 765 | |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 766 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); |
| 767 | I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); |
| 768 | |
| 769 | POSTING_READ(RING_PP_DIR_DCLV(ring)); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 774 | static void gen8_ppgtt_enable(struct drm_device *dev) |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 775 | { |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 776 | struct drm_i915_private *dev_priv = dev->dev_private; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 777 | struct intel_engine_cs *ring; |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 778 | int j; |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 779 | |
| 780 | for_each_ring(ring, dev_priv, j) { |
| 781 | I915_WRITE(RING_MODE_GEN7(ring), |
| 782 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 783 | } |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 784 | } |
| 785 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 786 | static void gen7_ppgtt_enable(struct drm_device *dev) |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 787 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 788 | struct drm_i915_private *dev_priv = dev->dev_private; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 789 | struct intel_engine_cs *ring; |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 790 | uint32_t ecochk, ecobits; |
| 791 | int i; |
| 792 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 793 | ecobits = I915_READ(GAC_ECO_BITS); |
| 794 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); |
| 795 | |
| 796 | ecochk = I915_READ(GAM_ECOCHK); |
| 797 | if (IS_HASWELL(dev)) { |
| 798 | ecochk |= ECOCHK_PPGTT_WB_HSW; |
| 799 | } else { |
| 800 | ecochk |= ECOCHK_PPGTT_LLC_IVB; |
| 801 | ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; |
| 802 | } |
| 803 | I915_WRITE(GAM_ECOCHK, ecochk); |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 804 | |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 805 | for_each_ring(ring, dev_priv, i) { |
Ben Widawsky | eeb9488 | 2013-12-06 14:11:10 -0800 | [diff] [blame] | 806 | /* GFX_MODE is per-ring on gen7+ */ |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 807 | I915_WRITE(RING_MODE_GEN7(ring), |
| 808 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 809 | } |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 812 | static void gen6_ppgtt_enable(struct drm_device *dev) |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 813 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 814 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 815 | uint32_t ecochk, gab_ctl, ecobits; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 816 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 817 | ecobits = I915_READ(GAC_ECO_BITS); |
| 818 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | |
| 819 | ECOBITS_PPGTT_CACHE64B); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 820 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 821 | gab_ctl = I915_READ(GAB_CTL); |
| 822 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 823 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 824 | ecochk = I915_READ(GAM_ECOCHK); |
| 825 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 826 | |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 827 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 830 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 831 | static void gen6_ppgtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 832 | uint64_t start, |
| 833 | uint64_t length, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 834 | bool use_scratch) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 835 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 836 | struct i915_hw_ppgtt *ppgtt = |
| 837 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 838 | gen6_gtt_pte_t *pt_vaddr, scratch_pte; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 839 | unsigned first_entry = start >> PAGE_SHIFT; |
| 840 | unsigned num_entries = length >> PAGE_SHIFT; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 841 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 842 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 843 | unsigned last_pte, i; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 844 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 845 | 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] | 846 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 847 | while (num_entries) { |
| 848 | last_pte = first_pte + num_entries; |
| 849 | if (last_pte > I915_PPGTT_PT_ENTRIES) |
| 850 | last_pte = I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 851 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 852 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 853 | |
| 854 | for (i = first_pte; i < last_pte; i++) |
| 855 | pt_vaddr[i] = scratch_pte; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 856 | |
| 857 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 858 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 859 | num_entries -= last_pte - first_pte; |
| 860 | first_pte = 0; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 861 | act_pt++; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 862 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 865 | static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 866 | struct sg_table *pages, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 867 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 868 | enum i915_cache_level cache_level, u32 flags) |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 869 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 870 | struct i915_hw_ppgtt *ppgtt = |
| 871 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 872 | gen6_gtt_pte_t *pt_vaddr; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 873 | unsigned first_entry = start >> PAGE_SHIFT; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 874 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 875 | unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 876 | struct sg_page_iter sg_iter; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 877 | |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 878 | pt_vaddr = NULL; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 879 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 880 | if (pt_vaddr == NULL) |
| 881 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 882 | |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 883 | pt_vaddr[act_pte] = |
| 884 | vm->pte_encode(sg_page_iter_dma_address(&sg_iter), |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 885 | cache_level, true, flags); |
| 886 | |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 887 | if (++act_pte == I915_PPGTT_PT_ENTRIES) { |
| 888 | kunmap_atomic(pt_vaddr); |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 889 | pt_vaddr = NULL; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 890 | act_pt++; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 891 | act_pte = 0; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 892 | } |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 893 | } |
Chris Wilson | cc79714 | 2013-12-31 15:50:30 +0000 | [diff] [blame] | 894 | if (pt_vaddr) |
| 895 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 898 | static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 899 | { |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 900 | int i; |
| 901 | |
| 902 | if (ppgtt->pt_dma_addr) { |
| 903 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 904 | pci_unmap_page(ppgtt->base.dev->pdev, |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 905 | ppgtt->pt_dma_addr[i], |
| 906 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 907 | } |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt) |
| 911 | { |
| 912 | int i; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 913 | |
| 914 | kfree(ppgtt->pt_dma_addr); |
| 915 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 916 | __free_page(ppgtt->pt_pages[i]); |
| 917 | kfree(ppgtt->pt_pages); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 920 | static void gen6_ppgtt_cleanup(struct i915_address_space *vm) |
| 921 | { |
| 922 | struct i915_hw_ppgtt *ppgtt = |
| 923 | container_of(vm, struct i915_hw_ppgtt, base); |
| 924 | |
Ben Widawsky | a00d825 | 2014-02-19 22:05:48 -0800 | [diff] [blame] | 925 | drm_mm_remove_node(&ppgtt->node); |
| 926 | |
| 927 | gen6_ppgtt_unmap_pages(ppgtt); |
| 928 | gen6_ppgtt_free(ppgtt); |
| 929 | } |
| 930 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 931 | static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt) |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 932 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 933 | struct drm_device *dev = ppgtt->base.dev; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 934 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 935 | bool retried = false; |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 936 | int ret; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 937 | |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 938 | /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The |
| 939 | * allocator works in address space sizes, so it's multiplied by page |
| 940 | * size. We allocate at the top of the GTT to avoid fragmentation. |
| 941 | */ |
| 942 | BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm)); |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 943 | alloc: |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 944 | ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm, |
| 945 | &ppgtt->node, GEN6_PD_SIZE, |
| 946 | GEN6_PD_ALIGN, 0, |
| 947 | 0, dev_priv->gtt.base.total, |
Ben Widawsky | 3e8b5ae | 2014-05-06 22:21:30 -0700 | [diff] [blame] | 948 | DRM_MM_TOPDOWN); |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 949 | if (ret == -ENOSPC && !retried) { |
| 950 | ret = i915_gem_evict_something(dev, &dev_priv->gtt.base, |
| 951 | GEN6_PD_SIZE, GEN6_PD_ALIGN, |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 952 | I915_CACHE_NONE, |
| 953 | 0, dev_priv->gtt.base.total, |
| 954 | 0); |
Ben Widawsky | e3cc199 | 2013-12-06 14:11:08 -0800 | [diff] [blame] | 955 | if (ret) |
| 956 | return ret; |
| 957 | |
| 958 | retried = true; |
| 959 | goto alloc; |
| 960 | } |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 961 | |
| 962 | if (ppgtt->node.start < dev_priv->gtt.mappable_end) |
| 963 | DRM_DEBUG("Forced to use aperture for PDEs\n"); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 964 | |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 965 | ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES; |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt) |
| 970 | { |
| 971 | int i; |
| 972 | |
| 973 | ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *), |
| 974 | GFP_KERNEL); |
| 975 | |
| 976 | if (!ppgtt->pt_pages) |
| 977 | return -ENOMEM; |
| 978 | |
| 979 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 980 | ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL); |
| 981 | if (!ppgtt->pt_pages[i]) { |
| 982 | gen6_ppgtt_free(ppgtt); |
| 983 | return -ENOMEM; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt) |
| 991 | { |
| 992 | int ret; |
| 993 | |
| 994 | ret = gen6_ppgtt_allocate_page_directories(ppgtt); |
| 995 | if (ret) |
| 996 | return ret; |
| 997 | |
| 998 | ret = gen6_ppgtt_allocate_page_tables(ppgtt); |
| 999 | if (ret) { |
| 1000 | drm_mm_remove_node(&ppgtt->node); |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
| 1004 | ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t), |
| 1005 | GFP_KERNEL); |
| 1006 | if (!ppgtt->pt_dma_addr) { |
| 1007 | drm_mm_remove_node(&ppgtt->node); |
| 1008 | gen6_ppgtt_free(ppgtt); |
| 1009 | return -ENOMEM; |
| 1010 | } |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt) |
| 1016 | { |
| 1017 | struct drm_device *dev = ppgtt->base.dev; |
| 1018 | int i; |
| 1019 | |
| 1020 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 1021 | dma_addr_t pt_addr; |
| 1022 | |
| 1023 | pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096, |
| 1024 | PCI_DMA_BIDIRECTIONAL); |
| 1025 | |
| 1026 | if (pci_dma_mapping_error(dev->pdev, pt_addr)) { |
| 1027 | gen6_ppgtt_unmap_pages(ppgtt); |
| 1028 | return -EIO; |
| 1029 | } |
| 1030 | |
| 1031 | ppgtt->pt_dma_addr[i] = pt_addr; |
| 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) |
| 1038 | { |
| 1039 | struct drm_device *dev = ppgtt->base.dev; |
| 1040 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1041 | int ret; |
| 1042 | |
| 1043 | ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1044 | if (IS_GEN6(dev)) { |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1045 | ppgtt->switch_mm = gen6_mm_switch; |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 1046 | } else if (IS_HASWELL(dev)) { |
Ben Widawsky | 90252e5 | 2013-12-06 14:11:12 -0800 | [diff] [blame] | 1047 | ppgtt->switch_mm = hsw_mm_switch; |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1048 | } else if (IS_GEN7(dev)) { |
Ben Widawsky | 48a1038 | 2013-12-06 14:11:11 -0800 | [diff] [blame] | 1049 | ppgtt->switch_mm = gen7_mm_switch; |
| 1050 | } else |
Ben Widawsky | b4a74e3 | 2013-12-06 14:11:09 -0800 | [diff] [blame] | 1051 | BUG(); |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1052 | |
| 1053 | ret = gen6_ppgtt_alloc(ppgtt); |
| 1054 | if (ret) |
| 1055 | return ret; |
| 1056 | |
| 1057 | ret = gen6_ppgtt_setup_page_tables(ppgtt); |
| 1058 | if (ret) { |
| 1059 | gen6_ppgtt_free(ppgtt); |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1063 | ppgtt->base.clear_range = gen6_ppgtt_clear_range; |
| 1064 | ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; |
| 1065 | ppgtt->base.cleanup = gen6_ppgtt_cleanup; |
Ben Widawsky | 686e1f6 | 2013-11-25 09:54:34 -0800 | [diff] [blame] | 1066 | ppgtt->base.start = 0; |
Ben Widawsky | 5a6c93f | 2014-03-08 11:58:17 -0800 | [diff] [blame] | 1067 | ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE; |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1068 | ppgtt->debug_dump = gen6_dump_ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1069 | |
Ben Widawsky | b146520 | 2014-02-19 22:05:49 -0800 | [diff] [blame] | 1070 | ppgtt->pd_offset = |
| 1071 | ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1072 | |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1073 | ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1074 | |
Ben Widawsky | c8d4c0d | 2013-12-06 14:11:07 -0800 | [diff] [blame] | 1075 | DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n", |
| 1076 | ppgtt->node.size >> 20, |
| 1077 | ppgtt->node.start / PAGE_SIZE); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1078 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1079 | gen6_write_pdes(ppgtt); |
| 1080 | DRM_DEBUG("Adding PPGTT at offset %x\n", |
| 1081 | ppgtt->pd_offset << 10); |
| 1082 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1083 | return 0; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1086 | 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] | 1087 | { |
| 1088 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1089 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1090 | ppgtt->base.dev = dev; |
Ben Widawsky | 8407bb9 | 2014-03-08 11:58:16 -0800 | [diff] [blame] | 1091 | ppgtt->base.scratch = dev_priv->gtt.base.scratch; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 1092 | |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 1093 | if (INTEL_INFO(dev)->gen < 8) |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1094 | return gen6_ppgtt_init(ppgtt); |
Daniel Vetter | 8fe6bd2 | 2013-11-02 21:07:01 -0700 | [diff] [blame] | 1095 | else if (IS_GEN8(dev)) |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1096 | return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total); |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 1097 | else |
| 1098 | BUG(); |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1099 | } |
| 1100 | int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt) |
| 1101 | { |
| 1102 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1103 | int ret = 0; |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 1104 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1105 | ret = __hw_ppgtt_init(dev, ppgtt); |
| 1106 | if (ret == 0) { |
Ben Widawsky | c7c48df | 2013-12-06 14:11:15 -0800 | [diff] [blame] | 1107 | kref_init(&ppgtt->ref); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 1108 | drm_mm_init(&ppgtt->base.mm, ppgtt->base.start, |
| 1109 | ppgtt->base.total); |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1110 | i915_init_vm(dev_priv, &ppgtt->base); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 1111 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1112 | |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1116 | int i915_ppgtt_init_hw(struct drm_device *dev) |
| 1117 | { |
| 1118 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1119 | struct intel_engine_cs *ring; |
| 1120 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 1121 | int i, ret = 0; |
| 1122 | |
Thomas Daniel | 671b5013 | 2014-08-20 16:24:50 +0100 | [diff] [blame^] | 1123 | /* In the case of execlists, PPGTT is enabled by the context descriptor |
| 1124 | * and the PDPs are contained within the context itself. We don't |
| 1125 | * need to do anything here. */ |
| 1126 | if (i915.enable_execlists) |
| 1127 | return 0; |
| 1128 | |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1129 | if (!USES_PPGTT(dev)) |
| 1130 | return 0; |
| 1131 | |
| 1132 | if (IS_GEN6(dev)) |
| 1133 | gen6_ppgtt_enable(dev); |
| 1134 | else if (IS_GEN7(dev)) |
| 1135 | gen7_ppgtt_enable(dev); |
| 1136 | else if (INTEL_INFO(dev)->gen >= 8) |
| 1137 | gen8_ppgtt_enable(dev); |
| 1138 | else |
| 1139 | WARN_ON(1); |
| 1140 | |
| 1141 | if (ppgtt) { |
| 1142 | for_each_ring(ring, dev_priv, i) { |
McAulay, Alistair | 6689c16 | 2014-08-15 18:51:35 +0100 | [diff] [blame] | 1143 | ret = ppgtt->switch_mm(ppgtt, ring); |
Daniel Vetter | 82460d9 | 2014-08-06 20:19:53 +0200 | [diff] [blame] | 1144 | if (ret != 0) |
| 1145 | return ret; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | return ret; |
| 1150 | } |
Daniel Vetter | 4d88470 | 2014-08-06 15:04:47 +0200 | [diff] [blame] | 1151 | struct i915_hw_ppgtt * |
| 1152 | i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv) |
| 1153 | { |
| 1154 | struct i915_hw_ppgtt *ppgtt; |
| 1155 | int ret; |
| 1156 | |
| 1157 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 1158 | if (!ppgtt) |
| 1159 | return ERR_PTR(-ENOMEM); |
| 1160 | |
| 1161 | ret = i915_ppgtt_init(dev, ppgtt); |
| 1162 | if (ret) { |
| 1163 | kfree(ppgtt); |
| 1164 | return ERR_PTR(ret); |
| 1165 | } |
| 1166 | |
| 1167 | ppgtt->file_priv = fpriv; |
| 1168 | |
| 1169 | return ppgtt; |
| 1170 | } |
| 1171 | |
Daniel Vetter | ee960be | 2014-08-06 15:04:45 +0200 | [diff] [blame] | 1172 | void i915_ppgtt_release(struct kref *kref) |
| 1173 | { |
| 1174 | struct i915_hw_ppgtt *ppgtt = |
| 1175 | container_of(kref, struct i915_hw_ppgtt, ref); |
| 1176 | |
| 1177 | /* vmas should already be unbound */ |
| 1178 | WARN_ON(!list_empty(&ppgtt->base.active_list)); |
| 1179 | WARN_ON(!list_empty(&ppgtt->base.inactive_list)); |
| 1180 | |
Daniel Vetter | 19dd120 | 2014-08-06 15:04:55 +0200 | [diff] [blame] | 1181 | list_del(&ppgtt->base.global_link); |
| 1182 | drm_mm_takedown(&ppgtt->base.mm); |
| 1183 | |
Daniel Vetter | ee960be | 2014-08-06 15:04:45 +0200 | [diff] [blame] | 1184 | ppgtt->base.cleanup(&ppgtt->base); |
| 1185 | kfree(ppgtt); |
| 1186 | } |
| 1187 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1188 | static void |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1189 | ppgtt_bind_vma(struct i915_vma *vma, |
| 1190 | enum i915_cache_level cache_level, |
| 1191 | u32 flags) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1192 | { |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1193 | /* Currently applicable only to VLV */ |
| 1194 | if (vma->obj->gt_ro) |
| 1195 | flags |= PTE_READ_ONLY; |
| 1196 | |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1197 | vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1198 | cache_level, flags); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 1199 | } |
| 1200 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1201 | static void ppgtt_unbind_vma(struct i915_vma *vma) |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1202 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1203 | vma->vm->clear_range(vma->vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1204 | vma->node.start, |
| 1205 | vma->obj->base.size, |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1206 | true); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 1209 | extern int intel_iommu_gfx_mapped; |
| 1210 | /* Certain Gen5 chipsets require require idling the GPU before |
| 1211 | * unmapping anything from the GTT when VT-d is enabled. |
| 1212 | */ |
| 1213 | static inline bool needs_idle_maps(struct drm_device *dev) |
| 1214 | { |
| 1215 | #ifdef CONFIG_INTEL_IOMMU |
| 1216 | /* Query intel_iommu to see if we need the workaround. Presumably that |
| 1217 | * was loaded first. |
| 1218 | */ |
| 1219 | if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) |
| 1220 | return true; |
| 1221 | #endif |
| 1222 | return false; |
| 1223 | } |
| 1224 | |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1225 | static bool do_idling(struct drm_i915_private *dev_priv) |
| 1226 | { |
| 1227 | bool ret = dev_priv->mm.interruptible; |
| 1228 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 1229 | if (unlikely(dev_priv->gtt.do_idle_maps)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1230 | dev_priv->mm.interruptible = false; |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 1231 | if (i915_gpu_idle(dev_priv->dev)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1232 | DRM_ERROR("Couldn't idle GPU\n"); |
| 1233 | /* Wait a bit, in hopes it avoids the hang */ |
| 1234 | udelay(10); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) |
| 1242 | { |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 1243 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1244 | dev_priv->mm.interruptible = interruptible; |
| 1245 | } |
| 1246 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1247 | void i915_check_and_clear_faults(struct drm_device *dev) |
| 1248 | { |
| 1249 | struct drm_i915_private *dev_priv = dev->dev_private; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1250 | struct intel_engine_cs *ring; |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1251 | int i; |
| 1252 | |
| 1253 | if (INTEL_INFO(dev)->gen < 6) |
| 1254 | return; |
| 1255 | |
| 1256 | for_each_ring(ring, dev_priv, i) { |
| 1257 | u32 fault_reg; |
| 1258 | fault_reg = I915_READ(RING_FAULT_REG(ring)); |
| 1259 | if (fault_reg & RING_FAULT_VALID) { |
| 1260 | DRM_DEBUG_DRIVER("Unexpected fault\n" |
| 1261 | "\tAddr: 0x%08lx\\n" |
| 1262 | "\tAddress space: %s\n" |
| 1263 | "\tSource ID: %d\n" |
| 1264 | "\tType: %d\n", |
| 1265 | fault_reg & PAGE_MASK, |
| 1266 | fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", |
| 1267 | RING_FAULT_SRCID(fault_reg), |
| 1268 | RING_FAULT_FAULT_TYPE(fault_reg)); |
| 1269 | I915_WRITE(RING_FAULT_REG(ring), |
| 1270 | fault_reg & ~RING_FAULT_VALID); |
| 1271 | } |
| 1272 | } |
| 1273 | POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); |
| 1274 | } |
| 1275 | |
| 1276 | void i915_gem_suspend_gtt_mappings(struct drm_device *dev) |
| 1277 | { |
| 1278 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1279 | |
| 1280 | /* Don't bother messing with faults pre GEN6 as we have little |
| 1281 | * documentation supporting that it's a good idea. |
| 1282 | */ |
| 1283 | if (INTEL_INFO(dev)->gen < 6) |
| 1284 | return; |
| 1285 | |
| 1286 | i915_check_and_clear_faults(dev); |
| 1287 | |
| 1288 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1289 | dev_priv->gtt.base.start, |
| 1290 | dev_priv->gtt.base.total, |
Daniel Vetter | e568af1 | 2014-03-26 20:08:20 +0100 | [diff] [blame] | 1291 | true); |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1294 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
| 1295 | { |
| 1296 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1297 | struct drm_i915_gem_object *obj; |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1298 | struct i915_address_space *vm; |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1299 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1300 | i915_check_and_clear_faults(dev); |
| 1301 | |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 1302 | /* First fill our portion of the GTT with scratch pages */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1303 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1304 | dev_priv->gtt.base.start, |
| 1305 | dev_priv->gtt.base.total, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1306 | true); |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 1307 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1308 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1309 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, |
| 1310 | &dev_priv->gtt.base); |
| 1311 | if (!vma) |
| 1312 | continue; |
| 1313 | |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 1314 | i915_gem_clflush_object(obj, obj->pin_display); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1315 | /* The bind_vma code tries to be smart about tracking mappings. |
| 1316 | * Unfortunately above, we've just wiped out the mappings |
| 1317 | * without telling our object about it. So we need to fake it. |
| 1318 | */ |
| 1319 | obj->has_global_gtt_mapping = 0; |
| 1320 | vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1323 | |
Ben Widawsky | a2319c0 | 2014-03-18 16:09:37 -0700 | [diff] [blame] | 1324 | if (INTEL_INFO(dev)->gen >= 8) { |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 1325 | if (IS_CHERRYVIEW(dev)) |
| 1326 | chv_setup_private_ppat(dev_priv); |
| 1327 | else |
| 1328 | bdw_setup_private_ppat(dev_priv); |
| 1329 | |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1330 | return; |
Ben Widawsky | a2319c0 | 2014-03-18 16:09:37 -0700 | [diff] [blame] | 1331 | } |
Ben Widawsky | 80da216 | 2013-12-06 14:11:17 -0800 | [diff] [blame] | 1332 | |
| 1333 | list_for_each_entry(vm, &dev_priv->vm_list, global_link) { |
| 1334 | /* TODO: Perhaps it shouldn't be gen6 specific */ |
| 1335 | if (i915_is_ggtt(vm)) { |
| 1336 | if (dev_priv->mm.aliasing_ppgtt) |
| 1337 | gen6_write_pdes(dev_priv->mm.aliasing_ppgtt); |
| 1338 | continue; |
| 1339 | } |
| 1340 | |
| 1341 | gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base)); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1342 | } |
| 1343 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1344 | i915_gem_chipset_flush(dev); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1345 | } |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1346 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 1347 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1348 | { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1349 | if (obj->has_dma_mapping) |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 1350 | return 0; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1351 | |
| 1352 | if (!dma_map_sg(&obj->base.dev->pdev->dev, |
| 1353 | obj->pages->sgl, obj->pages->nents, |
| 1354 | PCI_DMA_BIDIRECTIONAL)) |
| 1355 | return -ENOSPC; |
| 1356 | |
| 1357 | return 0; |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1358 | } |
| 1359 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1360 | static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte) |
| 1361 | { |
| 1362 | #ifdef writeq |
| 1363 | writeq(pte, addr); |
| 1364 | #else |
| 1365 | iowrite32((u32)pte, addr); |
| 1366 | iowrite32(pte >> 32, addr + 4); |
| 1367 | #endif |
| 1368 | } |
| 1369 | |
| 1370 | static void gen8_ggtt_insert_entries(struct i915_address_space *vm, |
| 1371 | struct sg_table *st, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1372 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1373 | enum i915_cache_level level, u32 unused) |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1374 | { |
| 1375 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1376 | unsigned first_entry = start >> PAGE_SHIFT; |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1377 | gen8_gtt_pte_t __iomem *gtt_entries = |
| 1378 | (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
| 1379 | int i = 0; |
| 1380 | struct sg_page_iter sg_iter; |
Pavel Machek | 57007df | 2014-07-28 13:20:58 +0200 | [diff] [blame] | 1381 | dma_addr_t addr = 0; /* shut up gcc */ |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1382 | |
| 1383 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
| 1384 | addr = sg_dma_address(sg_iter.sg) + |
| 1385 | (sg_iter.sg_pgoffset << PAGE_SHIFT); |
| 1386 | gen8_set_pte(>t_entries[i], |
| 1387 | gen8_pte_encode(addr, level, true)); |
| 1388 | i++; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
| 1392 | * XXX: This serves as a posting read to make sure that the PTE has |
| 1393 | * actually been updated. There is some concern that even though |
| 1394 | * registers and PTEs are within the same BAR that they are potentially |
| 1395 | * of NUMA access patterns. Therefore, even with the way we assume |
| 1396 | * hardware should work, we must keep this posting read for paranoia. |
| 1397 | */ |
| 1398 | if (i != 0) |
| 1399 | WARN_ON(readq(>t_entries[i-1]) |
| 1400 | != gen8_pte_encode(addr, level, true)); |
| 1401 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1402 | /* This next bit makes the above posting read even more important. We |
| 1403 | * want to flush the TLBs only after we're certain all the PTE updates |
| 1404 | * have finished. |
| 1405 | */ |
| 1406 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 1407 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1410 | /* |
| 1411 | * Binds an object into the global gtt with the specified cache level. The object |
| 1412 | * will be accessible to the GPU via commands whose operands reference offsets |
| 1413 | * within the global GTT as well as accessible by the GPU through the GMADR |
| 1414 | * mapped BAR (dev_priv->mm.gtt->gtt). |
| 1415 | */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1416 | static void gen6_ggtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1417 | struct sg_table *st, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1418 | uint64_t start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1419 | enum i915_cache_level level, u32 flags) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1420 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1421 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1422 | unsigned first_entry = start >> PAGE_SHIFT; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 1423 | gen6_gtt_pte_t __iomem *gtt_entries = |
| 1424 | (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1425 | int i = 0; |
| 1426 | struct sg_page_iter sg_iter; |
Pavel Machek | 57007df | 2014-07-28 13:20:58 +0200 | [diff] [blame] | 1427 | dma_addr_t addr = 0; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1428 | |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1429 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 1430 | addr = sg_page_iter_dma_address(&sg_iter); |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1431 | iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 1432 | i++; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1435 | /* XXX: This serves as a posting read to make sure that the PTE has |
| 1436 | * actually been updated. There is some concern that even though |
| 1437 | * registers and PTEs are within the same BAR that they are potentially |
| 1438 | * of NUMA access patterns. Therefore, even with the way we assume |
| 1439 | * hardware should work, we must keep this posting read for paranoia. |
| 1440 | */ |
Pavel Machek | 57007df | 2014-07-28 13:20:58 +0200 | [diff] [blame] | 1441 | if (i != 0) { |
| 1442 | unsigned long gtt = readl(>t_entries[i-1]); |
| 1443 | WARN_ON(gtt != vm->pte_encode(addr, level, true, flags)); |
| 1444 | } |
Ben Widawsky | 0f9b91c | 2012-11-04 09:21:30 -0800 | [diff] [blame] | 1445 | |
| 1446 | /* This next bit makes the above posting read even more important. We |
| 1447 | * want to flush the TLBs only after we're certain all the PTE updates |
| 1448 | * have finished. |
| 1449 | */ |
| 1450 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 1451 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1452 | } |
| 1453 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1454 | static void gen8_ggtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1455 | uint64_t start, |
| 1456 | uint64_t length, |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1457 | bool use_scratch) |
| 1458 | { |
| 1459 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1460 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1461 | unsigned num_entries = length >> PAGE_SHIFT; |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1462 | gen8_gtt_pte_t scratch_pte, __iomem *gtt_base = |
| 1463 | (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
| 1464 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
| 1465 | int i; |
| 1466 | |
| 1467 | if (WARN(num_entries > max_entries, |
| 1468 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 1469 | first_entry, num_entries, max_entries)) |
| 1470 | num_entries = max_entries; |
| 1471 | |
| 1472 | scratch_pte = gen8_pte_encode(vm->scratch.addr, |
| 1473 | I915_CACHE_LLC, |
| 1474 | use_scratch); |
| 1475 | for (i = 0; i < num_entries; i++) |
| 1476 | gen8_set_pte(>t_base[i], scratch_pte); |
| 1477 | readl(gtt_base); |
| 1478 | } |
| 1479 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1480 | static void gen6_ggtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1481 | uint64_t start, |
| 1482 | uint64_t length, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1483 | bool use_scratch) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1484 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1485 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1486 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1487 | unsigned num_entries = length >> PAGE_SHIFT; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 1488 | gen6_gtt_pte_t scratch_pte, __iomem *gtt_base = |
| 1489 | (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 1490 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1491 | int i; |
| 1492 | |
| 1493 | if (WARN(num_entries > max_entries, |
| 1494 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 1495 | first_entry, num_entries, max_entries)) |
| 1496 | num_entries = max_entries; |
| 1497 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1498 | 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] | 1499 | |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1500 | for (i = 0; i < num_entries; i++) |
| 1501 | iowrite32(scratch_pte, >t_base[i]); |
| 1502 | readl(gtt_base); |
| 1503 | } |
| 1504 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1505 | |
| 1506 | static void i915_ggtt_bind_vma(struct i915_vma *vma, |
| 1507 | enum i915_cache_level cache_level, |
| 1508 | u32 unused) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1509 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1510 | const unsigned long entry = vma->node.start >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1511 | unsigned int flags = (cache_level == I915_CACHE_NONE) ? |
| 1512 | AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; |
| 1513 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1514 | BUG_ON(!i915_is_ggtt(vma->vm)); |
| 1515 | intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags); |
| 1516 | vma->obj->has_global_gtt_mapping = 1; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1517 | } |
| 1518 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1519 | static void i915_ggtt_clear_range(struct i915_address_space *vm, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1520 | uint64_t start, |
| 1521 | uint64_t length, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 1522 | bool unused) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1523 | { |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1524 | unsigned first_entry = start >> PAGE_SHIFT; |
| 1525 | unsigned num_entries = length >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1526 | intel_gtt_clear_range(first_entry, num_entries); |
| 1527 | } |
| 1528 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1529 | static void i915_ggtt_unbind_vma(struct i915_vma *vma) |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 1530 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1531 | const unsigned int first = vma->node.start >> PAGE_SHIFT; |
| 1532 | const unsigned int size = vma->obj->base.size >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1533 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1534 | BUG_ON(!i915_is_ggtt(vma->vm)); |
| 1535 | vma->obj->has_global_gtt_mapping = 0; |
| 1536 | intel_gtt_clear_range(first, size); |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1539 | static void ggtt_bind_vma(struct i915_vma *vma, |
| 1540 | enum i915_cache_level cache_level, |
| 1541 | u32 flags) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1542 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1543 | struct drm_device *dev = vma->vm->dev; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1544 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1545 | struct drm_i915_gem_object *obj = vma->obj; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1546 | |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1547 | /* Currently applicable only to VLV */ |
| 1548 | if (obj->gt_ro) |
| 1549 | flags |= PTE_READ_ONLY; |
| 1550 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1551 | /* If there is no aliasing PPGTT, or the caller needs a global mapping, |
| 1552 | * or we have a global mapping already but the cacheability flags have |
| 1553 | * changed, set the global PTEs. |
| 1554 | * |
| 1555 | * If there is an aliasing PPGTT it is anecdotally faster, so use that |
| 1556 | * instead if none of the above hold true. |
| 1557 | * |
| 1558 | * NB: A global mapping should only be needed for special regions like |
| 1559 | * "gtt mappable", SNB errata, or if specified via special execbuf |
| 1560 | * flags. At all other times, the GPU will use the aliasing PPGTT. |
| 1561 | */ |
| 1562 | if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) { |
| 1563 | if (!obj->has_global_gtt_mapping || |
| 1564 | (cache_level != obj->cache_level)) { |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1565 | vma->vm->insert_entries(vma->vm, obj->pages, |
| 1566 | vma->node.start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1567 | cache_level, flags); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1568 | obj->has_global_gtt_mapping = 1; |
| 1569 | } |
| 1570 | } |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 1571 | |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1572 | if (dev_priv->mm.aliasing_ppgtt && |
| 1573 | (!obj->has_aliasing_ppgtt_mapping || |
| 1574 | (cache_level != obj->cache_level))) { |
| 1575 | struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; |
| 1576 | appgtt->base.insert_entries(&appgtt->base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1577 | vma->obj->pages, |
| 1578 | vma->node.start, |
Akash Goel | 24f3a8c | 2014-06-17 10:59:42 +0530 | [diff] [blame] | 1579 | cache_level, flags); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1580 | vma->obj->has_aliasing_ppgtt_mapping = 1; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | static void ggtt_unbind_vma(struct i915_vma *vma) |
| 1585 | { |
| 1586 | struct drm_device *dev = vma->vm->dev; |
| 1587 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1588 | struct drm_i915_gem_object *obj = vma->obj; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1589 | |
| 1590 | if (obj->has_global_gtt_mapping) { |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1591 | vma->vm->clear_range(vma->vm, |
| 1592 | vma->node.start, |
| 1593 | obj->base.size, |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1594 | true); |
| 1595 | obj->has_global_gtt_mapping = 0; |
| 1596 | } |
| 1597 | |
| 1598 | if (obj->has_aliasing_ppgtt_mapping) { |
| 1599 | struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; |
| 1600 | appgtt->base.clear_range(&appgtt->base, |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1601 | vma->node.start, |
| 1602 | obj->base.size, |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 1603 | true); |
| 1604 | obj->has_aliasing_ppgtt_mapping = 0; |
| 1605 | } |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) |
| 1609 | { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1610 | struct drm_device *dev = obj->base.dev; |
| 1611 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1612 | bool interruptible; |
| 1613 | |
| 1614 | interruptible = do_idling(dev_priv); |
| 1615 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1616 | if (!obj->has_dma_mapping) |
| 1617 | dma_unmap_sg(&dev->pdev->dev, |
| 1618 | obj->pages->sgl, obj->pages->nents, |
| 1619 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 1620 | |
| 1621 | undo_idling(dev_priv, interruptible); |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 1622 | } |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1623 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 1624 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
| 1625 | unsigned long color, |
| 1626 | unsigned long *start, |
| 1627 | unsigned long *end) |
| 1628 | { |
| 1629 | if (node->color != color) |
| 1630 | *start += 4096; |
| 1631 | |
| 1632 | if (!list_empty(&node->node_list)) { |
| 1633 | node = list_entry(node->node_list.next, |
| 1634 | struct drm_mm_node, |
| 1635 | node_list); |
| 1636 | if (node->allocated && node->color != color) |
| 1637 | *end -= 4096; |
| 1638 | } |
| 1639 | } |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 1640 | |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1641 | int i915_gem_setup_global_gtt(struct drm_device *dev, |
| 1642 | unsigned long start, |
| 1643 | unsigned long mappable_end, |
| 1644 | unsigned long end) |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1645 | { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 1646 | /* Let GEM Manage all of the aperture. |
| 1647 | * |
| 1648 | * However, leave one page at the end still bound to the scratch page. |
| 1649 | * There are a number of places where the hardware apparently prefetches |
| 1650 | * past the end of the object, and we've seen multiple hangs with the |
| 1651 | * GPU head pointer stuck in a batchbuffer bound at the last page of the |
| 1652 | * aperture. One page should be enough to keep any prefetching inside |
| 1653 | * of the aperture. |
| 1654 | */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1655 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1656 | struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1657 | struct drm_mm_node *entry; |
| 1658 | struct drm_i915_gem_object *obj; |
| 1659 | unsigned long hole_start, hole_end; |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1660 | int ret; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1661 | |
Ben Widawsky | 35451cb | 2013-01-17 12:45:13 -0800 | [diff] [blame] | 1662 | BUG_ON(mappable_end > end); |
| 1663 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1664 | /* Subtract the guard page ... */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1665 | drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE); |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 1666 | if (!HAS_LLC(dev)) |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 1667 | dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1668 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1669 | /* Mark any preallocated objects as occupied */ |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1670 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1671 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1672 | |
Ben Widawsky | edd41a8 | 2013-07-05 14:41:05 -0700 | [diff] [blame] | 1673 | DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n", |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 1674 | i915_gem_obj_ggtt_offset(obj), obj->base.size); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1675 | |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 1676 | WARN_ON(i915_gem_obj_ggtt_bound(obj)); |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1677 | ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1678 | if (ret) { |
| 1679 | DRM_DEBUG_KMS("Reservation failed: %i\n", ret); |
| 1680 | return ret; |
| 1681 | } |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1682 | obj->has_global_gtt_mapping = 1; |
| 1683 | } |
| 1684 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1685 | dev_priv->gtt.base.start = start; |
| 1686 | dev_priv->gtt.base.total = end - start; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1687 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1688 | /* Clear any non-preallocated blocks */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 1689 | 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] | 1690 | DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", |
| 1691 | hole_start, hole_end); |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1692 | ggtt_vm->clear_range(ggtt_vm, hole_start, |
| 1693 | hole_end - hole_start, true); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | /* And finally clear the reserved guard page */ |
Ben Widawsky | 782f149 | 2014-02-20 11:50:33 -0800 | [diff] [blame] | 1697 | ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true); |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1698 | |
Daniel Vetter | fa76da3 | 2014-08-06 20:19:54 +0200 | [diff] [blame] | 1699 | if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) { |
| 1700 | struct i915_hw_ppgtt *ppgtt; |
| 1701 | |
| 1702 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 1703 | if (!ppgtt) |
| 1704 | return -ENOMEM; |
| 1705 | |
| 1706 | ret = __hw_ppgtt_init(dev, ppgtt); |
| 1707 | if (ret != 0) |
| 1708 | return ret; |
| 1709 | |
| 1710 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
| 1711 | } |
| 1712 | |
Daniel Vetter | 6c5566a | 2014-08-06 15:04:50 +0200 | [diff] [blame] | 1713 | return 0; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 1716 | void i915_gem_init_global_gtt(struct drm_device *dev) |
| 1717 | { |
| 1718 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1719 | unsigned long gtt_size, mappable_size; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 1720 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1721 | gtt_size = dev_priv->gtt.base.total; |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 1722 | mappable_size = dev_priv->gtt.mappable_end; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 1723 | |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 1724 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
Daniel Vetter | 90d0a0e | 2014-08-06 15:04:56 +0200 | [diff] [blame] | 1727 | void i915_global_gtt_cleanup(struct drm_device *dev) |
| 1728 | { |
| 1729 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1730 | struct i915_address_space *vm = &dev_priv->gtt.base; |
| 1731 | |
Daniel Vetter | 70e3254 | 2014-08-06 15:04:57 +0200 | [diff] [blame] | 1732 | if (dev_priv->mm.aliasing_ppgtt) { |
| 1733 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 1734 | |
| 1735 | ppgtt->base.cleanup(&ppgtt->base); |
| 1736 | } |
| 1737 | |
Daniel Vetter | 90d0a0e | 2014-08-06 15:04:56 +0200 | [diff] [blame] | 1738 | if (drm_mm_initialized(&vm->mm)) { |
| 1739 | drm_mm_takedown(&vm->mm); |
| 1740 | list_del(&vm->global_link); |
| 1741 | } |
| 1742 | |
| 1743 | vm->cleanup(vm); |
| 1744 | } |
Daniel Vetter | 70e3254 | 2014-08-06 15:04:57 +0200 | [diff] [blame] | 1745 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1746 | static int setup_scratch_page(struct drm_device *dev) |
| 1747 | { |
| 1748 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1749 | struct page *page; |
| 1750 | dma_addr_t dma_addr; |
| 1751 | |
| 1752 | page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
| 1753 | if (page == NULL) |
| 1754 | return -ENOMEM; |
| 1755 | get_page(page); |
| 1756 | set_pages_uc(page, 1); |
| 1757 | |
| 1758 | #ifdef CONFIG_INTEL_IOMMU |
| 1759 | dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, |
| 1760 | PCI_DMA_BIDIRECTIONAL); |
| 1761 | if (pci_dma_mapping_error(dev->pdev, dma_addr)) |
| 1762 | return -EINVAL; |
| 1763 | #else |
| 1764 | dma_addr = page_to_phys(page); |
| 1765 | #endif |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1766 | dev_priv->gtt.base.scratch.page = page; |
| 1767 | dev_priv->gtt.base.scratch.addr = dma_addr; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1768 | |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | static void teardown_scratch_page(struct drm_device *dev) |
| 1773 | { |
| 1774 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1775 | struct page *page = dev_priv->gtt.base.scratch.page; |
| 1776 | |
| 1777 | set_pages_wb(page, 1); |
| 1778 | pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr, |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1779 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1780 | put_page(page); |
| 1781 | __free_page(page); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) |
| 1785 | { |
| 1786 | snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; |
| 1787 | snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; |
| 1788 | return snb_gmch_ctl << 20; |
| 1789 | } |
| 1790 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 1791 | static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) |
| 1792 | { |
| 1793 | bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; |
| 1794 | bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; |
| 1795 | if (bdw_gmch_ctl) |
| 1796 | bdw_gmch_ctl = 1 << bdw_gmch_ctl; |
Ben Widawsky | 562d55d | 2014-05-27 16:53:08 -0700 | [diff] [blame] | 1797 | |
| 1798 | #ifdef CONFIG_X86_32 |
| 1799 | /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */ |
| 1800 | if (bdw_gmch_ctl > 4) |
| 1801 | bdw_gmch_ctl = 4; |
| 1802 | #endif |
| 1803 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 1804 | return bdw_gmch_ctl << 20; |
| 1805 | } |
| 1806 | |
Damien Lespiau | d7f25f2 | 2014-05-08 22:19:40 +0300 | [diff] [blame] | 1807 | static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl) |
| 1808 | { |
| 1809 | gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT; |
| 1810 | gmch_ctrl &= SNB_GMCH_GGMS_MASK; |
| 1811 | |
| 1812 | if (gmch_ctrl) |
| 1813 | return 1 << (20 + gmch_ctrl); |
| 1814 | |
| 1815 | return 0; |
| 1816 | } |
| 1817 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1818 | static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1819 | { |
| 1820 | snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; |
| 1821 | snb_gmch_ctl &= SNB_GMCH_GMS_MASK; |
| 1822 | return snb_gmch_ctl << 25; /* 32 MB units */ |
| 1823 | } |
| 1824 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 1825 | static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) |
| 1826 | { |
| 1827 | bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; |
| 1828 | bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; |
| 1829 | return bdw_gmch_ctl << 25; /* 32 MB units */ |
| 1830 | } |
| 1831 | |
Damien Lespiau | d7f25f2 | 2014-05-08 22:19:40 +0300 | [diff] [blame] | 1832 | static size_t chv_get_stolen_size(u16 gmch_ctrl) |
| 1833 | { |
| 1834 | gmch_ctrl >>= SNB_GMCH_GMS_SHIFT; |
| 1835 | gmch_ctrl &= SNB_GMCH_GMS_MASK; |
| 1836 | |
| 1837 | /* |
| 1838 | * 0x0 to 0x10: 32MB increments starting at 0MB |
| 1839 | * 0x11 to 0x16: 4MB increments starting at 8MB |
| 1840 | * 0x17 to 0x1d: 4MB increments start at 36MB |
| 1841 | */ |
| 1842 | if (gmch_ctrl < 0x11) |
| 1843 | return gmch_ctrl << 25; |
| 1844 | else if (gmch_ctrl < 0x17) |
| 1845 | return (gmch_ctrl - 0x11 + 2) << 22; |
| 1846 | else |
| 1847 | return (gmch_ctrl - 0x17 + 9) << 22; |
| 1848 | } |
| 1849 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1850 | static int ggtt_probe_common(struct drm_device *dev, |
| 1851 | size_t gtt_size) |
| 1852 | { |
| 1853 | struct drm_i915_private *dev_priv = dev->dev_private; |
Bjorn Helgaas | 21c3460 | 2013-12-21 10:52:52 -0700 | [diff] [blame] | 1854 | phys_addr_t gtt_phys_addr; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1855 | int ret; |
| 1856 | |
| 1857 | /* 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] | 1858 | gtt_phys_addr = pci_resource_start(dev->pdev, 0) + |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1859 | (pci_resource_len(dev->pdev, 0) / 2); |
| 1860 | |
Bjorn Helgaas | 21c3460 | 2013-12-21 10:52:52 -0700 | [diff] [blame] | 1861 | dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size); |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1862 | if (!dev_priv->gtt.gsm) { |
| 1863 | DRM_ERROR("Failed to map the gtt page table\n"); |
| 1864 | return -ENOMEM; |
| 1865 | } |
| 1866 | |
| 1867 | ret = setup_scratch_page(dev); |
| 1868 | if (ret) { |
| 1869 | DRM_ERROR("Scratch setup failed\n"); |
| 1870 | /* iounmap will also get called at remove, but meh */ |
| 1871 | iounmap(dev_priv->gtt.gsm); |
| 1872 | } |
| 1873 | |
| 1874 | return ret; |
| 1875 | } |
| 1876 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 1877 | /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability |
| 1878 | * bits. When using advanced contexts each context stores its own PAT, but |
| 1879 | * writing this data shouldn't be harmful even in those cases. */ |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 1880 | static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv) |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 1881 | { |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 1882 | uint64_t pat; |
| 1883 | |
| 1884 | pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ |
| 1885 | GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ |
| 1886 | GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */ |
| 1887 | GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ |
| 1888 | GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | |
| 1889 | GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | |
| 1890 | GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | |
| 1891 | GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); |
| 1892 | |
| 1893 | /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b |
| 1894 | * write would work. */ |
| 1895 | I915_WRITE(GEN8_PRIVATE_PAT, pat); |
| 1896 | I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); |
| 1897 | } |
| 1898 | |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 1899 | static void chv_setup_private_ppat(struct drm_i915_private *dev_priv) |
| 1900 | { |
| 1901 | uint64_t pat; |
| 1902 | |
| 1903 | /* |
| 1904 | * Map WB on BDW to snooped on CHV. |
| 1905 | * |
| 1906 | * Only the snoop bit has meaning for CHV, the rest is |
| 1907 | * ignored. |
| 1908 | * |
| 1909 | * Note that the harware enforces snooping for all page |
| 1910 | * table accesses. The snoop bit is actually ignored for |
| 1911 | * PDEs. |
| 1912 | */ |
| 1913 | pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) | |
| 1914 | GEN8_PPAT(1, 0) | |
| 1915 | GEN8_PPAT(2, 0) | |
| 1916 | GEN8_PPAT(3, 0) | |
| 1917 | GEN8_PPAT(4, CHV_PPAT_SNOOP) | |
| 1918 | GEN8_PPAT(5, CHV_PPAT_SNOOP) | |
| 1919 | GEN8_PPAT(6, CHV_PPAT_SNOOP) | |
| 1920 | GEN8_PPAT(7, CHV_PPAT_SNOOP); |
| 1921 | |
| 1922 | I915_WRITE(GEN8_PRIVATE_PAT, pat); |
| 1923 | I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); |
| 1924 | } |
| 1925 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1926 | static int gen8_gmch_probe(struct drm_device *dev, |
| 1927 | size_t *gtt_total, |
| 1928 | size_t *stolen, |
| 1929 | phys_addr_t *mappable_base, |
| 1930 | unsigned long *mappable_end) |
| 1931 | { |
| 1932 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1933 | unsigned int gtt_size; |
| 1934 | u16 snb_gmch_ctl; |
| 1935 | int ret; |
| 1936 | |
| 1937 | /* TODO: We're not aware of mappable constraints on gen8 yet */ |
| 1938 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 1939 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 1940 | |
| 1941 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39))) |
| 1942 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39)); |
| 1943 | |
| 1944 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
| 1945 | |
Damien Lespiau | d7f25f2 | 2014-05-08 22:19:40 +0300 | [diff] [blame] | 1946 | if (IS_CHERRYVIEW(dev)) { |
| 1947 | *stolen = chv_get_stolen_size(snb_gmch_ctl); |
| 1948 | gtt_size = chv_get_total_gtt_size(snb_gmch_ctl); |
| 1949 | } else { |
| 1950 | *stolen = gen8_get_stolen_size(snb_gmch_ctl); |
| 1951 | gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); |
| 1952 | } |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1953 | |
Ben Widawsky | d31eb10 | 2013-11-02 21:07:17 -0700 | [diff] [blame] | 1954 | *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1955 | |
Ville Syrjälä | ee0ce47 | 2014-04-09 13:28:01 +0300 | [diff] [blame] | 1956 | if (IS_CHERRYVIEW(dev)) |
| 1957 | chv_setup_private_ppat(dev_priv); |
| 1958 | else |
| 1959 | bdw_setup_private_ppat(dev_priv); |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame] | 1960 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1961 | ret = ggtt_probe_common(dev, gtt_size); |
| 1962 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1963 | dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range; |
| 1964 | dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1965 | |
| 1966 | return ret; |
| 1967 | } |
| 1968 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1969 | static int gen6_gmch_probe(struct drm_device *dev, |
| 1970 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1971 | size_t *stolen, |
| 1972 | phys_addr_t *mappable_base, |
| 1973 | unsigned long *mappable_end) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1974 | { |
| 1975 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1976 | unsigned int gtt_size; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1977 | u16 snb_gmch_ctl; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1978 | int ret; |
| 1979 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1980 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 1981 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 1982 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1983 | /* 64/512MB is the current min/max we actually know of, but this is just |
| 1984 | * a coarse sanity check. |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1985 | */ |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1986 | if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1987 | DRM_ERROR("Unknown GMADR size (%lx)\n", |
| 1988 | dev_priv->gtt.mappable_end); |
| 1989 | return -ENXIO; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1990 | } |
| 1991 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1992 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) |
| 1993 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1994 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1995 | |
Ben Widawsky | c4ae25e | 2013-05-01 11:00:34 -0700 | [diff] [blame] | 1996 | *stolen = gen6_get_stolen_size(snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1997 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1998 | gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1999 | *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT; |
| 2000 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2001 | ret = ggtt_probe_common(dev, gtt_size); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2002 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2003 | dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; |
| 2004 | dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2005 | |
| 2006 | return ret; |
| 2007 | } |
| 2008 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2009 | static void gen6_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2010 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2011 | |
| 2012 | struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); |
Ben Widawsky | 5ed1678 | 2013-11-25 09:54:43 -0800 | [diff] [blame] | 2013 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2014 | iounmap(gtt->gsm); |
| 2015 | teardown_scratch_page(vm->dev); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | static int i915_gmch_probe(struct drm_device *dev, |
| 2019 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2020 | size_t *stolen, |
| 2021 | phys_addr_t *mappable_base, |
| 2022 | unsigned long *mappable_end) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2023 | { |
| 2024 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2025 | int ret; |
| 2026 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2027 | ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); |
| 2028 | if (!ret) { |
| 2029 | DRM_ERROR("failed to set up gmch\n"); |
| 2030 | return -EIO; |
| 2031 | } |
| 2032 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 2033 | intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2034 | |
| 2035 | dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2036 | dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2037 | |
Chris Wilson | c0a7f81 | 2013-12-30 12:16:15 +0000 | [diff] [blame] | 2038 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
| 2039 | DRM_INFO("applying Ironlake quirks for intel_iommu\n"); |
| 2040 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2041 | return 0; |
| 2042 | } |
| 2043 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2044 | static void i915_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2045 | { |
| 2046 | intel_gmch_remove(); |
| 2047 | } |
| 2048 | |
| 2049 | int i915_gem_gtt_init(struct drm_device *dev) |
| 2050 | { |
| 2051 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2052 | struct i915_gtt *gtt = &dev_priv->gtt; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2053 | int ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2054 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2055 | if (INTEL_INFO(dev)->gen <= 5) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2056 | gtt->gtt_probe = i915_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2057 | gtt->base.cleanup = i915_gmch_remove; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2058 | } else if (INTEL_INFO(dev)->gen < 8) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2059 | gtt->gtt_probe = gen6_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2060 | gtt->base.cleanup = gen6_gmch_remove; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 2061 | if (IS_HASWELL(dev) && dev_priv->ellc_size) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2062 | gtt->base.pte_encode = iris_pte_encode; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 2063 | else if (IS_HASWELL(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2064 | gtt->base.pte_encode = hsw_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2065 | else if (IS_VALLEYVIEW(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2066 | gtt->base.pte_encode = byt_pte_encode; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 2067 | else if (INTEL_INFO(dev)->gen >= 7) |
| 2068 | gtt->base.pte_encode = ivb_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2069 | else |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 2070 | gtt->base.pte_encode = snb_pte_encode; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 2071 | } else { |
| 2072 | dev_priv->gtt.gtt_probe = gen8_gmch_probe; |
| 2073 | dev_priv->gtt.base.cleanup = gen6_gmch_remove; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2076 | ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size, |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2077 | >t->mappable_base, >t->mappable_end); |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 2078 | if (ret) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2079 | return ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2080 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2081 | gtt->base.dev = dev; |
| 2082 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 2083 | /* GMADR is the PCI mmio aperture into the global GTT. */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 2084 | DRM_INFO("Memory usable by graphics device = %zdM\n", |
| 2085 | gtt->base.total >> 20); |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 2086 | DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20); |
| 2087 | DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); |
Daniel Vetter | 5db6c73 | 2014-03-31 16:23:04 +0200 | [diff] [blame] | 2088 | #ifdef CONFIG_INTEL_IOMMU |
| 2089 | if (intel_iommu_gfx_mapped) |
| 2090 | DRM_INFO("VT-d active for gfx access\n"); |
| 2091 | #endif |
Daniel Vetter | cfa7c86 | 2014-04-29 11:53:58 +0200 | [diff] [blame] | 2092 | /* |
| 2093 | * i915.enable_ppgtt is read-only, so do an early pass to validate the |
| 2094 | * user's requested state against the hardware/driver capabilities. We |
| 2095 | * do this now so that we can print out any log messages once rather |
| 2096 | * than every time we check intel_enable_ppgtt(). |
| 2097 | */ |
| 2098 | i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt); |
| 2099 | DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 2100 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 2101 | return 0; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 2102 | } |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2103 | |
| 2104 | static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj, |
| 2105 | struct i915_address_space *vm) |
| 2106 | { |
| 2107 | struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 2108 | if (vma == NULL) |
| 2109 | return ERR_PTR(-ENOMEM); |
| 2110 | |
| 2111 | INIT_LIST_HEAD(&vma->vma_link); |
| 2112 | INIT_LIST_HEAD(&vma->mm_list); |
| 2113 | INIT_LIST_HEAD(&vma->exec_list); |
| 2114 | vma->vm = vm; |
| 2115 | vma->obj = obj; |
| 2116 | |
| 2117 | switch (INTEL_INFO(vm->dev)->gen) { |
| 2118 | case 8: |
| 2119 | case 7: |
| 2120 | case 6: |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 2121 | if (i915_is_ggtt(vm)) { |
| 2122 | vma->unbind_vma = ggtt_unbind_vma; |
| 2123 | vma->bind_vma = ggtt_bind_vma; |
| 2124 | } else { |
| 2125 | vma->unbind_vma = ppgtt_unbind_vma; |
| 2126 | vma->bind_vma = ppgtt_bind_vma; |
| 2127 | } |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2128 | break; |
| 2129 | case 5: |
| 2130 | case 4: |
| 2131 | case 3: |
| 2132 | case 2: |
| 2133 | BUG_ON(!i915_is_ggtt(vm)); |
| 2134 | vma->unbind_vma = i915_ggtt_unbind_vma; |
| 2135 | vma->bind_vma = i915_ggtt_bind_vma; |
| 2136 | break; |
| 2137 | default: |
| 2138 | BUG(); |
| 2139 | } |
| 2140 | |
| 2141 | /* Keep GGTT vmas first to make debug easier */ |
| 2142 | if (i915_is_ggtt(vm)) |
| 2143 | list_add(&vma->vma_link, &obj->vma_list); |
Michel Thierry | e07f055 | 2014-08-19 15:49:41 +0100 | [diff] [blame] | 2144 | else { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2145 | list_add_tail(&vma->vma_link, &obj->vma_list); |
Michel Thierry | e07f055 | 2014-08-19 15:49:41 +0100 | [diff] [blame] | 2146 | i915_ppgtt_get(i915_vm_to_ppgtt(vm)); |
| 2147 | } |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 2148 | |
| 2149 | return vma; |
| 2150 | } |
| 2151 | |
| 2152 | struct i915_vma * |
| 2153 | i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj, |
| 2154 | struct i915_address_space *vm) |
| 2155 | { |
| 2156 | struct i915_vma *vma; |
| 2157 | |
| 2158 | vma = i915_gem_obj_to_vma(obj, vm); |
| 2159 | if (!vma) |
| 2160 | vma = __i915_gem_vma_create(obj, vm); |
| 2161 | |
| 2162 | return vma; |
| 2163 | } |