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