Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 Daniel Vetter |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | */ |
| 24 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 25 | #include <drm/drmP.h> |
| 26 | #include <drm/i915_drm.h> |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 27 | #include "i915_drv.h" |
| 28 | #include "i915_trace.h" |
| 29 | #include "intel_drv.h" |
| 30 | |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 31 | #define GEN6_PPGTT_PD_ENTRIES 512 |
| 32 | #define I915_PPGTT_PT_ENTRIES (PAGE_SIZE / sizeof(gen6_gtt_pte_t)) |
| 33 | |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 34 | /* PPGTT stuff */ |
| 35 | #define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0)) |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 36 | #define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0)) |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 37 | |
| 38 | #define GEN6_PDE_VALID (1 << 0) |
| 39 | /* gen6+ has bit 11-4 for physical addr bit 39-32 */ |
| 40 | #define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
| 41 | |
| 42 | #define GEN6_PTE_VALID (1 << 0) |
| 43 | #define GEN6_PTE_UNCACHED (1 << 1) |
| 44 | #define HSW_PTE_UNCACHED (0) |
| 45 | #define GEN6_PTE_CACHE_LLC (2 << 1) |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 46 | #define GEN7_PTE_CACHE_L3_LLC (3 << 1) |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 47 | #define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 48 | #define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr) |
| 49 | |
| 50 | /* Cacheability Control is a 4-bit value. The low three bits are stored in * |
| 51 | * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE. |
| 52 | */ |
| 53 | #define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \ |
| 54 | (((bits) & 0x8) << (11 - 3))) |
Ben Widawsky | 87a6b68 | 2013-08-04 23:47:29 -0700 | [diff] [blame] | 55 | #define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2) |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 56 | #define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3) |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 57 | #define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb) |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 58 | #define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6) |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 59 | |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 60 | static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 61 | enum i915_cache_level level, |
| 62 | bool valid) |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 63 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 64 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 65 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 66 | |
| 67 | switch (level) { |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 68 | case I915_CACHE_L3_LLC: |
| 69 | case I915_CACHE_LLC: |
| 70 | pte |= GEN6_PTE_CACHE_LLC; |
| 71 | break; |
| 72 | case I915_CACHE_NONE: |
| 73 | pte |= GEN6_PTE_UNCACHED; |
| 74 | break; |
| 75 | default: |
| 76 | WARN_ON(1); |
| 77 | } |
| 78 | |
| 79 | return pte; |
| 80 | } |
| 81 | |
| 82 | static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 83 | enum i915_cache_level level, |
| 84 | bool valid) |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 85 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 86 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 87 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 88 | |
| 89 | switch (level) { |
| 90 | case I915_CACHE_L3_LLC: |
| 91 | pte |= GEN7_PTE_CACHE_L3_LLC; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 92 | break; |
| 93 | case I915_CACHE_LLC: |
| 94 | pte |= GEN6_PTE_CACHE_LLC; |
| 95 | break; |
| 96 | case I915_CACHE_NONE: |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 97 | pte |= GEN6_PTE_UNCACHED; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 98 | break; |
| 99 | default: |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 100 | WARN_ON(1); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 103 | return pte; |
| 104 | } |
| 105 | |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 106 | #define BYT_PTE_WRITEABLE (1 << 1) |
| 107 | #define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2) |
| 108 | |
Ben Widawsky | 80a74f7 | 2013-06-27 16:30:19 -0700 | [diff] [blame] | 109 | static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 110 | enum i915_cache_level level, |
| 111 | bool valid) |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 112 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 113 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 114 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 115 | |
| 116 | /* Mark the page as writeable. Other platforms don't have a |
| 117 | * setting for read-only/writable, so this matches that behavior. |
| 118 | */ |
| 119 | pte |= BYT_PTE_WRITEABLE; |
| 120 | |
| 121 | if (level != I915_CACHE_NONE) |
| 122 | pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; |
| 123 | |
| 124 | return pte; |
| 125 | } |
| 126 | |
Ben Widawsky | 80a74f7 | 2013-06-27 16:30:19 -0700 | [diff] [blame] | 127 | static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 128 | enum i915_cache_level level, |
| 129 | bool valid) |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 130 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 131 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 132 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 133 | |
| 134 | if (level != I915_CACHE_NONE) |
Ben Widawsky | 87a6b68 | 2013-08-04 23:47:29 -0700 | [diff] [blame] | 135 | pte |= HSW_WB_LLC_AGE3; |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 136 | |
| 137 | return pte; |
| 138 | } |
| 139 | |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 140 | static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 141 | enum i915_cache_level level, |
| 142 | bool valid) |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 143 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 144 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 145 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
| 146 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 147 | switch (level) { |
| 148 | case I915_CACHE_NONE: |
| 149 | break; |
| 150 | case I915_CACHE_WT: |
| 151 | pte |= HSW_WT_ELLC_LLC_AGE0; |
| 152 | break; |
| 153 | default: |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 154 | pte |= HSW_WB_ELLC_LLC_AGE0; |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 155 | break; |
| 156 | } |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 157 | |
| 158 | return pte; |
| 159 | } |
| 160 | |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 161 | static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt) |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 162 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 163 | struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 164 | gen6_gtt_pte_t __iomem *pd_addr; |
| 165 | uint32_t pd_entry; |
| 166 | int i; |
| 167 | |
Ben Widawsky | 0a73287 | 2013-04-23 23:15:30 -0700 | [diff] [blame] | 168 | WARN_ON(ppgtt->pd_offset & 0x3f); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 169 | pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm + |
| 170 | ppgtt->pd_offset / sizeof(gen6_gtt_pte_t); |
| 171 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 172 | dma_addr_t pt_addr; |
| 173 | |
| 174 | pt_addr = ppgtt->pt_dma_addr[i]; |
| 175 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr); |
| 176 | pd_entry |= GEN6_PDE_VALID; |
| 177 | |
| 178 | writel(pd_entry, pd_addr + i); |
| 179 | } |
| 180 | readl(pd_addr); |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static int gen6_ppgtt_enable(struct drm_device *dev) |
| 184 | { |
| 185 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 186 | uint32_t pd_offset; |
| 187 | struct intel_ring_buffer *ring; |
| 188 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 189 | int i; |
| 190 | |
| 191 | BUG_ON(ppgtt->pd_offset & 0x3f); |
| 192 | |
| 193 | gen6_write_pdes(ppgtt); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 194 | |
| 195 | pd_offset = ppgtt->pd_offset; |
| 196 | pd_offset /= 64; /* in cachelines, */ |
| 197 | pd_offset <<= 16; |
| 198 | |
| 199 | if (INTEL_INFO(dev)->gen == 6) { |
| 200 | uint32_t ecochk, gab_ctl, ecobits; |
| 201 | |
| 202 | ecobits = I915_READ(GAC_ECO_BITS); |
Ville Syrjälä | 3b9d788 | 2013-04-04 15:13:40 +0300 | [diff] [blame] | 203 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | |
| 204 | ECOBITS_PPGTT_CACHE64B); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 205 | |
| 206 | gab_ctl = I915_READ(GAB_CTL); |
| 207 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); |
| 208 | |
| 209 | ecochk = I915_READ(GAM_ECOCHK); |
| 210 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | |
| 211 | ECOCHK_PPGTT_CACHE64B); |
| 212 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
| 213 | } else if (INTEL_INFO(dev)->gen >= 7) { |
Ville Syrjälä | a6f429a | 2013-04-04 15:13:42 +0300 | [diff] [blame] | 214 | uint32_t ecochk, ecobits; |
Ville Syrjälä | a65c2fc | 2013-04-04 15:13:41 +0300 | [diff] [blame] | 215 | |
| 216 | ecobits = I915_READ(GAC_ECO_BITS); |
| 217 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); |
| 218 | |
Ville Syrjälä | a6f429a | 2013-04-04 15:13:42 +0300 | [diff] [blame] | 219 | ecochk = I915_READ(GAM_ECOCHK); |
| 220 | if (IS_HASWELL(dev)) { |
| 221 | ecochk |= ECOCHK_PPGTT_WB_HSW; |
| 222 | } else { |
| 223 | ecochk |= ECOCHK_PPGTT_LLC_IVB; |
| 224 | ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; |
| 225 | } |
| 226 | I915_WRITE(GAM_ECOCHK, ecochk); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 227 | /* GFX_MODE is per-ring on gen7+ */ |
| 228 | } |
| 229 | |
| 230 | for_each_ring(ring, dev_priv, i) { |
| 231 | if (INTEL_INFO(dev)->gen >= 7) |
| 232 | I915_WRITE(RING_MODE_GEN7(ring), |
| 233 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
| 234 | |
| 235 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); |
| 236 | I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset); |
| 237 | } |
Ben Widawsky | b7c36d2 | 2013-04-08 18:43:56 -0700 | [diff] [blame] | 238 | return 0; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 241 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 242 | static void gen6_ppgtt_clear_range(struct i915_address_space *vm, |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 243 | unsigned first_entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 244 | unsigned num_entries, |
| 245 | bool use_scratch) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 246 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 247 | struct i915_hw_ppgtt *ppgtt = |
| 248 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 249 | gen6_gtt_pte_t *pt_vaddr, scratch_pte; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 250 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 251 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 252 | unsigned last_pte, i; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 253 | |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 254 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 255 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 256 | while (num_entries) { |
| 257 | last_pte = first_pte + num_entries; |
| 258 | if (last_pte > I915_PPGTT_PT_ENTRIES) |
| 259 | last_pte = I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 260 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 261 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 262 | |
| 263 | for (i = first_pte; i < last_pte; i++) |
| 264 | pt_vaddr[i] = scratch_pte; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 265 | |
| 266 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 267 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 268 | num_entries -= last_pte - first_pte; |
| 269 | first_pte = 0; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 270 | act_pt++; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 271 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 274 | static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 275 | struct sg_table *pages, |
| 276 | unsigned first_entry, |
| 277 | enum i915_cache_level cache_level) |
| 278 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 279 | struct i915_hw_ppgtt *ppgtt = |
| 280 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 281 | gen6_gtt_pte_t *pt_vaddr; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 282 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 283 | unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 284 | struct sg_page_iter sg_iter; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 285 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 286 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 287 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
| 288 | dma_addr_t page_addr; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 289 | |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 290 | page_addr = sg_page_iter_dma_address(&sg_iter); |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 291 | pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 292 | if (++act_pte == I915_PPGTT_PT_ENTRIES) { |
| 293 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 294 | act_pt++; |
| 295 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 296 | act_pte = 0; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 297 | |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 298 | } |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 299 | } |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 300 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 303 | static void gen6_ppgtt_cleanup(struct i915_address_space *vm) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 304 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 305 | struct i915_hw_ppgtt *ppgtt = |
| 306 | container_of(vm, struct i915_hw_ppgtt, base); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 307 | int i; |
| 308 | |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 309 | drm_mm_takedown(&ppgtt->base.mm); |
| 310 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 311 | if (ppgtt->pt_dma_addr) { |
| 312 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 313 | pci_unmap_page(ppgtt->base.dev->pdev, |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 314 | ppgtt->pt_dma_addr[i], |
| 315 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 316 | } |
| 317 | |
| 318 | kfree(ppgtt->pt_dma_addr); |
| 319 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 320 | __free_page(ppgtt->pt_pages[i]); |
| 321 | kfree(ppgtt->pt_pages); |
| 322 | kfree(ppgtt); |
| 323 | } |
| 324 | |
| 325 | static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) |
| 326 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 327 | struct drm_device *dev = ppgtt->base.dev; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 328 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 329 | unsigned first_pd_entry_in_global_pt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 330 | int i; |
| 331 | int ret = -ENOMEM; |
| 332 | |
| 333 | /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024 |
| 334 | * entries. For aliasing ppgtt support we just steal them at the end for |
| 335 | * now. */ |
Daniel Vetter | e1b73cb | 2013-05-21 09:52:16 +0200 | [diff] [blame] | 336 | first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 337 | |
Chris Wilson | 08c4526 | 2013-07-30 19:04:37 +0100 | [diff] [blame] | 338 | ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 339 | ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 340 | ppgtt->enable = gen6_ppgtt_enable; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 341 | ppgtt->base.clear_range = gen6_ppgtt_clear_range; |
| 342 | ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; |
| 343 | ppgtt->base.cleanup = gen6_ppgtt_cleanup; |
| 344 | ppgtt->base.scratch = dev_priv->gtt.base.scratch; |
Daniel Vetter | a1e2265 | 2013-09-21 00:35:38 +0200 | [diff] [blame] | 345 | ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *), |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 346 | GFP_KERNEL); |
| 347 | if (!ppgtt->pt_pages) |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 348 | return -ENOMEM; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 349 | |
| 350 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 351 | ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL); |
| 352 | if (!ppgtt->pt_pages[i]) |
| 353 | goto err_pt_alloc; |
| 354 | } |
| 355 | |
Daniel Vetter | a1e2265 | 2013-09-21 00:35:38 +0200 | [diff] [blame] | 356 | ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t), |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 357 | GFP_KERNEL); |
| 358 | if (!ppgtt->pt_dma_addr) |
| 359 | goto err_pt_alloc; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 360 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 361 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 362 | dma_addr_t pt_addr; |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 363 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 364 | pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096, |
| 365 | PCI_DMA_BIDIRECTIONAL); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 366 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 367 | if (pci_dma_mapping_error(dev->pdev, pt_addr)) { |
| 368 | ret = -EIO; |
| 369 | goto err_pd_pin; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 370 | |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 371 | } |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 372 | ppgtt->pt_dma_addr[i] = pt_addr; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 373 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 374 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 375 | ppgtt->base.clear_range(&ppgtt->base, 0, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 376 | ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 377 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 378 | ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 379 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 380 | return 0; |
| 381 | |
| 382 | err_pd_pin: |
| 383 | if (ppgtt->pt_dma_addr) { |
| 384 | for (i--; i >= 0; i--) |
| 385 | pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i], |
| 386 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 387 | } |
| 388 | err_pt_alloc: |
| 389 | kfree(ppgtt->pt_dma_addr); |
| 390 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 391 | if (ppgtt->pt_pages[i]) |
| 392 | __free_page(ppgtt->pt_pages[i]); |
| 393 | } |
| 394 | kfree(ppgtt->pt_pages); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 395 | |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev) |
| 400 | { |
| 401 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 402 | struct i915_hw_ppgtt *ppgtt; |
| 403 | int ret; |
| 404 | |
| 405 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 406 | if (!ppgtt) |
| 407 | return -ENOMEM; |
| 408 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 409 | ppgtt->base.dev = dev; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 410 | |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 411 | if (INTEL_INFO(dev)->gen < 8) |
| 412 | ret = gen6_ppgtt_init(ppgtt); |
Daniel Vetter | 8fe6bd2 | 2013-11-02 21:07:01 -0700 | [diff] [blame] | 413 | else if (IS_GEN8(dev)) |
| 414 | ret = -ENOSYS; |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 415 | else |
| 416 | BUG(); |
| 417 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 418 | if (ret) |
| 419 | kfree(ppgtt); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 420 | else { |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 421 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 422 | drm_mm_init(&ppgtt->base.mm, ppgtt->base.start, |
| 423 | ppgtt->base.total); |
| 424 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 425 | |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev) |
| 430 | { |
| 431 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 432 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 433 | |
| 434 | if (!ppgtt) |
| 435 | return; |
| 436 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 437 | ppgtt->base.cleanup(&ppgtt->base); |
Ben Widawsky | 5963cf0 | 2013-04-08 18:43:55 -0700 | [diff] [blame] | 438 | dev_priv->mm.aliasing_ppgtt = NULL; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 441 | void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt, |
| 442 | struct drm_i915_gem_object *obj, |
| 443 | enum i915_cache_level cache_level) |
| 444 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 445 | ppgtt->base.insert_entries(&ppgtt->base, obj->pages, |
| 446 | i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT, |
| 447 | cache_level); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt, |
| 451 | struct drm_i915_gem_object *obj) |
| 452 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 453 | ppgtt->base.clear_range(&ppgtt->base, |
| 454 | i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 455 | obj->base.size >> PAGE_SHIFT, |
| 456 | true); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 459 | extern int intel_iommu_gfx_mapped; |
| 460 | /* Certain Gen5 chipsets require require idling the GPU before |
| 461 | * unmapping anything from the GTT when VT-d is enabled. |
| 462 | */ |
| 463 | static inline bool needs_idle_maps(struct drm_device *dev) |
| 464 | { |
| 465 | #ifdef CONFIG_INTEL_IOMMU |
| 466 | /* Query intel_iommu to see if we need the workaround. Presumably that |
| 467 | * was loaded first. |
| 468 | */ |
| 469 | if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) |
| 470 | return true; |
| 471 | #endif |
| 472 | return false; |
| 473 | } |
| 474 | |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 475 | static bool do_idling(struct drm_i915_private *dev_priv) |
| 476 | { |
| 477 | bool ret = dev_priv->mm.interruptible; |
| 478 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 479 | if (unlikely(dev_priv->gtt.do_idle_maps)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 480 | dev_priv->mm.interruptible = false; |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 481 | if (i915_gpu_idle(dev_priv->dev)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 482 | DRM_ERROR("Couldn't idle GPU\n"); |
| 483 | /* Wait a bit, in hopes it avoids the hang */ |
| 484 | udelay(10); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
| 491 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) |
| 492 | { |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 493 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 494 | dev_priv->mm.interruptible = interruptible; |
| 495 | } |
| 496 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 497 | void i915_check_and_clear_faults(struct drm_device *dev) |
| 498 | { |
| 499 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 500 | struct intel_ring_buffer *ring; |
| 501 | int i; |
| 502 | |
| 503 | if (INTEL_INFO(dev)->gen < 6) |
| 504 | return; |
| 505 | |
| 506 | for_each_ring(ring, dev_priv, i) { |
| 507 | u32 fault_reg; |
| 508 | fault_reg = I915_READ(RING_FAULT_REG(ring)); |
| 509 | if (fault_reg & RING_FAULT_VALID) { |
| 510 | DRM_DEBUG_DRIVER("Unexpected fault\n" |
| 511 | "\tAddr: 0x%08lx\\n" |
| 512 | "\tAddress space: %s\n" |
| 513 | "\tSource ID: %d\n" |
| 514 | "\tType: %d\n", |
| 515 | fault_reg & PAGE_MASK, |
| 516 | fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", |
| 517 | RING_FAULT_SRCID(fault_reg), |
| 518 | RING_FAULT_FAULT_TYPE(fault_reg)); |
| 519 | I915_WRITE(RING_FAULT_REG(ring), |
| 520 | fault_reg & ~RING_FAULT_VALID); |
| 521 | } |
| 522 | } |
| 523 | POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); |
| 524 | } |
| 525 | |
| 526 | void i915_gem_suspend_gtt_mappings(struct drm_device *dev) |
| 527 | { |
| 528 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 529 | |
| 530 | /* Don't bother messing with faults pre GEN6 as we have little |
| 531 | * documentation supporting that it's a good idea. |
| 532 | */ |
| 533 | if (INTEL_INFO(dev)->gen < 6) |
| 534 | return; |
| 535 | |
| 536 | i915_check_and_clear_faults(dev); |
| 537 | |
| 538 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
| 539 | dev_priv->gtt.base.start / PAGE_SIZE, |
| 540 | dev_priv->gtt.base.total / PAGE_SIZE, |
| 541 | false); |
| 542 | } |
| 543 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 544 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
| 545 | { |
| 546 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 547 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 548 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 549 | i915_check_and_clear_faults(dev); |
| 550 | |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 551 | /* First fill our portion of the GTT with scratch pages */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 552 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
| 553 | dev_priv->gtt.base.start / PAGE_SIZE, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 554 | dev_priv->gtt.base.total / PAGE_SIZE, |
| 555 | true); |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 556 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 557 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 558 | i915_gem_clflush_object(obj, obj->pin_display); |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 559 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 562 | i915_gem_chipset_flush(dev); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 563 | } |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 564 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 565 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 566 | { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 567 | if (obj->has_dma_mapping) |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 568 | return 0; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 569 | |
| 570 | if (!dma_map_sg(&obj->base.dev->pdev->dev, |
| 571 | obj->pages->sgl, obj->pages->nents, |
| 572 | PCI_DMA_BIDIRECTIONAL)) |
| 573 | return -ENOSPC; |
| 574 | |
| 575 | return 0; |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 578 | /* |
| 579 | * Binds an object into the global gtt with the specified cache level. The object |
| 580 | * will be accessible to the GPU via commands whose operands reference offsets |
| 581 | * within the global GTT as well as accessible by the GPU through the GMADR |
| 582 | * mapped BAR (dev_priv->mm.gtt->gtt). |
| 583 | */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 584 | static void gen6_ggtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 585 | struct sg_table *st, |
| 586 | unsigned int first_entry, |
| 587 | enum i915_cache_level level) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 588 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 589 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 590 | gen6_gtt_pte_t __iomem *gtt_entries = |
| 591 | (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 592 | int i = 0; |
| 593 | struct sg_page_iter sg_iter; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 594 | dma_addr_t addr; |
| 595 | |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 596 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 597 | addr = sg_page_iter_dma_address(&sg_iter); |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 598 | iowrite32(vm->pte_encode(addr, level, true), >t_entries[i]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 599 | i++; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 602 | /* XXX: This serves as a posting read to make sure that the PTE has |
| 603 | * actually been updated. There is some concern that even though |
| 604 | * registers and PTEs are within the same BAR that they are potentially |
| 605 | * of NUMA access patterns. Therefore, even with the way we assume |
| 606 | * hardware should work, we must keep this posting read for paranoia. |
| 607 | */ |
| 608 | if (i != 0) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 609 | WARN_ON(readl(>t_entries[i-1]) != |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 610 | vm->pte_encode(addr, level, true)); |
Ben Widawsky | 0f9b91c | 2012-11-04 09:21:30 -0800 | [diff] [blame] | 611 | |
| 612 | /* This next bit makes the above posting read even more important. We |
| 613 | * want to flush the TLBs only after we're certain all the PTE updates |
| 614 | * have finished. |
| 615 | */ |
| 616 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 617 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 620 | static void gen6_ggtt_clear_range(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 621 | unsigned int first_entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 622 | unsigned int num_entries, |
| 623 | bool use_scratch) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 624 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 625 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 626 | gen6_gtt_pte_t scratch_pte, __iomem *gtt_base = |
| 627 | (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 628 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 629 | int i; |
| 630 | |
| 631 | if (WARN(num_entries > max_entries, |
| 632 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 633 | first_entry, num_entries, max_entries)) |
| 634 | num_entries = max_entries; |
| 635 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 636 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch); |
| 637 | |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 638 | for (i = 0; i < num_entries; i++) |
| 639 | iowrite32(scratch_pte, >t_base[i]); |
| 640 | readl(gtt_base); |
| 641 | } |
| 642 | |
| 643 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 644 | static void i915_ggtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 645 | struct sg_table *st, |
| 646 | unsigned int pg_start, |
| 647 | enum i915_cache_level cache_level) |
| 648 | { |
| 649 | unsigned int flags = (cache_level == I915_CACHE_NONE) ? |
| 650 | AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; |
| 651 | |
| 652 | intel_gtt_insert_sg_entries(st, pg_start, flags); |
| 653 | |
| 654 | } |
| 655 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 656 | static void i915_ggtt_clear_range(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 657 | unsigned int first_entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 658 | unsigned int num_entries, |
| 659 | bool unused) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 660 | { |
| 661 | intel_gtt_clear_range(first_entry, num_entries); |
| 662 | } |
| 663 | |
| 664 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 665 | void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj, |
| 666 | enum i915_cache_level cache_level) |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 667 | { |
| 668 | struct drm_device *dev = obj->base.dev; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 669 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 670 | const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 671 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 672 | dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages, |
| 673 | entry, |
| 674 | cache_level); |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 675 | |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 676 | obj->has_global_gtt_mapping = 1; |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 677 | } |
| 678 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 679 | void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 680 | { |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 681 | struct drm_device *dev = obj->base.dev; |
| 682 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 683 | const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 684 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 685 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
| 686 | entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 687 | obj->base.size >> PAGE_SHIFT, |
| 688 | true); |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 689 | |
| 690 | obj->has_global_gtt_mapping = 0; |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) |
| 694 | { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 695 | struct drm_device *dev = obj->base.dev; |
| 696 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 697 | bool interruptible; |
| 698 | |
| 699 | interruptible = do_idling(dev_priv); |
| 700 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 701 | if (!obj->has_dma_mapping) |
| 702 | dma_unmap_sg(&dev->pdev->dev, |
| 703 | obj->pages->sgl, obj->pages->nents, |
| 704 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 705 | |
| 706 | undo_idling(dev_priv, interruptible); |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 707 | } |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 708 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 709 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
| 710 | unsigned long color, |
| 711 | unsigned long *start, |
| 712 | unsigned long *end) |
| 713 | { |
| 714 | if (node->color != color) |
| 715 | *start += 4096; |
| 716 | |
| 717 | if (!list_empty(&node->node_list)) { |
| 718 | node = list_entry(node->node_list.next, |
| 719 | struct drm_mm_node, |
| 720 | node_list); |
| 721 | if (node->allocated && node->color != color) |
| 722 | *end -= 4096; |
| 723 | } |
| 724 | } |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 725 | void i915_gem_setup_global_gtt(struct drm_device *dev, |
| 726 | unsigned long start, |
| 727 | unsigned long mappable_end, |
| 728 | unsigned long end) |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 729 | { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 730 | /* Let GEM Manage all of the aperture. |
| 731 | * |
| 732 | * However, leave one page at the end still bound to the scratch page. |
| 733 | * There are a number of places where the hardware apparently prefetches |
| 734 | * past the end of the object, and we've seen multiple hangs with the |
| 735 | * GPU head pointer stuck in a batchbuffer bound at the last page of the |
| 736 | * aperture. One page should be enough to keep any prefetching inside |
| 737 | * of the aperture. |
| 738 | */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 739 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 740 | struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 741 | struct drm_mm_node *entry; |
| 742 | struct drm_i915_gem_object *obj; |
| 743 | unsigned long hole_start, hole_end; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 744 | |
Ben Widawsky | 35451cb | 2013-01-17 12:45:13 -0800 | [diff] [blame] | 745 | BUG_ON(mappable_end > end); |
| 746 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 747 | /* Subtract the guard page ... */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 748 | drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE); |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 749 | if (!HAS_LLC(dev)) |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 750 | dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 751 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 752 | /* Mark any preallocated objects as occupied */ |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 753 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 754 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 755 | int ret; |
Ben Widawsky | edd41a8 | 2013-07-05 14:41:05 -0700 | [diff] [blame] | 756 | DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n", |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 757 | i915_gem_obj_ggtt_offset(obj), obj->base.size); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 758 | |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 759 | WARN_ON(i915_gem_obj_ggtt_bound(obj)); |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 760 | ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 761 | if (ret) |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 762 | DRM_DEBUG_KMS("Reservation failed\n"); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 763 | obj->has_global_gtt_mapping = 1; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 764 | list_add(&vma->vma_link, &obj->vma_list); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 767 | dev_priv->gtt.base.start = start; |
| 768 | dev_priv->gtt.base.total = end - start; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 769 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 770 | /* Clear any non-preallocated blocks */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 771 | drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 772 | const unsigned long count = (hole_end - hole_start) / PAGE_SIZE; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 773 | DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", |
| 774 | hole_start, hole_end); |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 775 | ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* And finally clear the reserved guard page */ |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 779 | ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 782 | static bool |
| 783 | intel_enable_ppgtt(struct drm_device *dev) |
| 784 | { |
| 785 | if (i915_enable_ppgtt >= 0) |
| 786 | return i915_enable_ppgtt; |
| 787 | |
| 788 | #ifdef CONFIG_INTEL_IOMMU |
| 789 | /* Disable ppgtt on SNB if VT-d is on. */ |
| 790 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) |
| 791 | return false; |
| 792 | #endif |
| 793 | |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | void i915_gem_init_global_gtt(struct drm_device *dev) |
| 798 | { |
| 799 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 800 | unsigned long gtt_size, mappable_size; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 801 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 802 | gtt_size = dev_priv->gtt.base.total; |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 803 | mappable_size = dev_priv->gtt.mappable_end; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 804 | |
| 805 | if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 806 | int ret; |
Ben Widawsky | 3eb1c00 | 2013-04-08 18:43:52 -0700 | [diff] [blame] | 807 | |
| 808 | if (INTEL_INFO(dev)->gen <= 7) { |
| 809 | /* PPGTT pdes are stolen from global gtt ptes, so shrink the |
| 810 | * aperture accordingly when using aliasing ppgtt. */ |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 811 | gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE; |
Ben Widawsky | 3eb1c00 | 2013-04-08 18:43:52 -0700 | [diff] [blame] | 812 | } |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 813 | |
| 814 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
| 815 | |
| 816 | ret = i915_gem_init_aliasing_ppgtt(dev); |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 817 | if (!ret) |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 818 | return; |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 819 | |
| 820 | DRM_ERROR("Aliased PPGTT setup failed %d\n", ret); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 821 | drm_mm_takedown(&dev_priv->gtt.base.mm); |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 822 | gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 823 | } |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 824 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static int setup_scratch_page(struct drm_device *dev) |
| 828 | { |
| 829 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 830 | struct page *page; |
| 831 | dma_addr_t dma_addr; |
| 832 | |
| 833 | page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
| 834 | if (page == NULL) |
| 835 | return -ENOMEM; |
| 836 | get_page(page); |
| 837 | set_pages_uc(page, 1); |
| 838 | |
| 839 | #ifdef CONFIG_INTEL_IOMMU |
| 840 | dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, |
| 841 | PCI_DMA_BIDIRECTIONAL); |
| 842 | if (pci_dma_mapping_error(dev->pdev, dma_addr)) |
| 843 | return -EINVAL; |
| 844 | #else |
| 845 | dma_addr = page_to_phys(page); |
| 846 | #endif |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 847 | dev_priv->gtt.base.scratch.page = page; |
| 848 | dev_priv->gtt.base.scratch.addr = dma_addr; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | static void teardown_scratch_page(struct drm_device *dev) |
| 854 | { |
| 855 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 856 | struct page *page = dev_priv->gtt.base.scratch.page; |
| 857 | |
| 858 | set_pages_wb(page, 1); |
| 859 | pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr, |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 860 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 861 | put_page(page); |
| 862 | __free_page(page); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) |
| 866 | { |
| 867 | snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; |
| 868 | snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; |
| 869 | return snb_gmch_ctl << 20; |
| 870 | } |
| 871 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 872 | static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) |
| 873 | { |
| 874 | bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; |
| 875 | bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; |
| 876 | if (bdw_gmch_ctl) |
| 877 | bdw_gmch_ctl = 1 << bdw_gmch_ctl; |
| 878 | return bdw_gmch_ctl << 20; |
| 879 | } |
| 880 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 881 | static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 882 | { |
| 883 | snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; |
| 884 | snb_gmch_ctl &= SNB_GMCH_GMS_MASK; |
| 885 | return snb_gmch_ctl << 25; /* 32 MB units */ |
| 886 | } |
| 887 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 888 | static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) |
| 889 | { |
| 890 | bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; |
| 891 | bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; |
| 892 | return bdw_gmch_ctl << 25; /* 32 MB units */ |
| 893 | } |
| 894 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 895 | static int gen6_gmch_probe(struct drm_device *dev, |
| 896 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 897 | size_t *stolen, |
| 898 | phys_addr_t *mappable_base, |
| 899 | unsigned long *mappable_end) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 900 | { |
| 901 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 902 | phys_addr_t gtt_bus_addr; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 903 | unsigned int gtt_size; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 904 | u16 snb_gmch_ctl; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 905 | int ret; |
| 906 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 907 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 908 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 909 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 910 | /* 64/512MB is the current min/max we actually know of, but this is just |
| 911 | * a coarse sanity check. |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 912 | */ |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 913 | if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 914 | DRM_ERROR("Unknown GMADR size (%lx)\n", |
| 915 | dev_priv->gtt.mappable_end); |
| 916 | return -ENXIO; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 917 | } |
| 918 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 919 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) |
| 920 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 921 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 922 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 923 | if (IS_GEN8(dev)) { |
| 924 | gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); |
| 925 | *gtt_total = (gtt_size / 8) << PAGE_SHIFT; |
| 926 | *stolen = gen8_get_stolen_size(snb_gmch_ctl); |
| 927 | } else { |
| 928 | gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); |
| 929 | *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT; |
| 930 | *stolen = gen6_get_stolen_size(snb_gmch_ctl); |
| 931 | } |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 932 | |
Ben Widawsky | a93e416 | 2013-04-08 18:43:47 -0700 | [diff] [blame] | 933 | /* For Modern GENs the PTEs and register space are split in the BAR */ |
| 934 | gtt_bus_addr = pci_resource_start(dev->pdev, 0) + |
| 935 | (pci_resource_len(dev->pdev, 0) / 2); |
| 936 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 937 | dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size); |
| 938 | if (!dev_priv->gtt.gsm) { |
| 939 | DRM_ERROR("Failed to map the gtt page table\n"); |
| 940 | return -ENOMEM; |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 941 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | ret = setup_scratch_page(dev); |
| 945 | if (ret) |
| 946 | DRM_ERROR("Scratch setup failed\n"); |
| 947 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 948 | dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; |
| 949 | dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 950 | |
| 951 | return ret; |
| 952 | } |
| 953 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 954 | static void gen6_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 955 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 956 | |
| 957 | struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); |
| 958 | iounmap(gtt->gsm); |
| 959 | teardown_scratch_page(vm->dev); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static int i915_gmch_probe(struct drm_device *dev, |
| 963 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 964 | size_t *stolen, |
| 965 | phys_addr_t *mappable_base, |
| 966 | unsigned long *mappable_end) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 967 | { |
| 968 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 969 | int ret; |
| 970 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 971 | ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); |
| 972 | if (!ret) { |
| 973 | DRM_ERROR("failed to set up gmch\n"); |
| 974 | return -EIO; |
| 975 | } |
| 976 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 977 | intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 978 | |
| 979 | dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 980 | dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; |
| 981 | dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 986 | static void i915_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 987 | { |
| 988 | intel_gmch_remove(); |
| 989 | } |
| 990 | |
| 991 | int i915_gem_gtt_init(struct drm_device *dev) |
| 992 | { |
| 993 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 994 | struct i915_gtt *gtt = &dev_priv->gtt; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 995 | int ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 996 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 997 | if (INTEL_INFO(dev)->gen <= 5) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 998 | gtt->gtt_probe = i915_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 999 | gtt->base.cleanup = i915_gmch_remove; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1000 | } else { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1001 | gtt->gtt_probe = gen6_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1002 | gtt->base.cleanup = gen6_gmch_remove; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 1003 | if (IS_HASWELL(dev) && dev_priv->ellc_size) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1004 | gtt->base.pte_encode = iris_pte_encode; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 1005 | else if (IS_HASWELL(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1006 | gtt->base.pte_encode = hsw_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1007 | else if (IS_VALLEYVIEW(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1008 | gtt->base.pte_encode = byt_pte_encode; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 1009 | else if (INTEL_INFO(dev)->gen >= 7) |
| 1010 | gtt->base.pte_encode = ivb_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1011 | else |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 1012 | gtt->base.pte_encode = snb_pte_encode; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1015 | ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size, |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1016 | >t->mappable_base, >t->mappable_end); |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 1017 | if (ret) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1018 | return ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1019 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1020 | gtt->base.dev = dev; |
| 1021 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1022 | /* GMADR is the PCI mmio aperture into the global GTT. */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1023 | DRM_INFO("Memory usable by graphics device = %zdM\n", |
| 1024 | gtt->base.total >> 20); |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1025 | DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20); |
| 1026 | DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1027 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1028 | return 0; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1029 | } |