Commit | Line | Data |
---|---|---|
76aaf220 DV |
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 | ||
760285e7 DH |
25 | #include <drm/drmP.h> |
26 | #include <drm/i915_drm.h> | |
76aaf220 DV |
27 | #include "i915_drv.h" |
28 | #include "i915_trace.h" | |
29 | #include "intel_drv.h" | |
30 | ||
6670a5a5 BW |
31 | #define GEN6_PPGTT_PD_ENTRIES 512 |
32 | #define I915_PPGTT_PT_ENTRIES (PAGE_SIZE / sizeof(gen6_gtt_pte_t)) | |
d31eb10e | 33 | typedef uint64_t gen8_gtt_pte_t; |
6670a5a5 | 34 | |
26b1ff35 BW |
35 | /* PPGTT stuff */ |
36 | #define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0)) | |
0d8ff15e | 37 | #define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0)) |
26b1ff35 BW |
38 | |
39 | #define GEN6_PDE_VALID (1 << 0) | |
40 | /* gen6+ has bit 11-4 for physical addr bit 39-32 */ | |
41 | #define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) | |
42 | ||
43 | #define GEN6_PTE_VALID (1 << 0) | |
44 | #define GEN6_PTE_UNCACHED (1 << 1) | |
45 | #define HSW_PTE_UNCACHED (0) | |
46 | #define GEN6_PTE_CACHE_LLC (2 << 1) | |
350ec881 | 47 | #define GEN7_PTE_CACHE_L3_LLC (3 << 1) |
26b1ff35 | 48 | #define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
0d8ff15e BW |
49 | #define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr) |
50 | ||
51 | /* Cacheability Control is a 4-bit value. The low three bits are stored in * | |
52 | * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE. | |
53 | */ | |
54 | #define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \ | |
55 | (((bits) & 0x8) << (11 - 3))) | |
87a6b688 | 56 | #define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2) |
0d8ff15e | 57 | #define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3) |
4d15c145 | 58 | #define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb) |
651d794f | 59 | #define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6) |
26b1ff35 | 60 | |
fbe5d36e BW |
61 | #define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD) |
62 | #define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */ | |
63 | #define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */ | |
64 | #define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */ | |
65 | ||
94ec8f61 BW |
66 | static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr, |
67 | enum i915_cache_level level, | |
68 | bool valid) | |
69 | { | |
70 | gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0; | |
71 | pte |= addr; | |
fbe5d36e BW |
72 | if (level != I915_CACHE_NONE) |
73 | pte |= PPAT_CACHED_INDEX; | |
74 | else | |
75 | pte |= PPAT_UNCACHED_INDEX; | |
94ec8f61 BW |
76 | return pte; |
77 | } | |
78 | ||
350ec881 | 79 | static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr, |
b35b380e BW |
80 | enum i915_cache_level level, |
81 | bool valid) | |
54d12527 | 82 | { |
b35b380e | 83 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
54d12527 | 84 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
e7210c3c BW |
85 | |
86 | switch (level) { | |
350ec881 CW |
87 | case I915_CACHE_L3_LLC: |
88 | case I915_CACHE_LLC: | |
89 | pte |= GEN6_PTE_CACHE_LLC; | |
90 | break; | |
91 | case I915_CACHE_NONE: | |
92 | pte |= GEN6_PTE_UNCACHED; | |
93 | break; | |
94 | default: | |
95 | WARN_ON(1); | |
96 | } | |
97 | ||
98 | return pte; | |
99 | } | |
100 | ||
101 | static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr, | |
b35b380e BW |
102 | enum i915_cache_level level, |
103 | bool valid) | |
350ec881 | 104 | { |
b35b380e | 105 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
350ec881 CW |
106 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
107 | ||
108 | switch (level) { | |
109 | case I915_CACHE_L3_LLC: | |
110 | pte |= GEN7_PTE_CACHE_L3_LLC; | |
e7210c3c BW |
111 | break; |
112 | case I915_CACHE_LLC: | |
113 | pte |= GEN6_PTE_CACHE_LLC; | |
114 | break; | |
115 | case I915_CACHE_NONE: | |
9119708c | 116 | pte |= GEN6_PTE_UNCACHED; |
e7210c3c BW |
117 | break; |
118 | default: | |
350ec881 | 119 | WARN_ON(1); |
e7210c3c BW |
120 | } |
121 | ||
54d12527 BW |
122 | return pte; |
123 | } | |
124 | ||
93c34e70 KG |
125 | #define BYT_PTE_WRITEABLE (1 << 1) |
126 | #define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2) | |
127 | ||
80a74f7f | 128 | static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr, |
b35b380e BW |
129 | enum i915_cache_level level, |
130 | bool valid) | |
93c34e70 | 131 | { |
b35b380e | 132 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
93c34e70 KG |
133 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
134 | ||
135 | /* Mark the page as writeable. Other platforms don't have a | |
136 | * setting for read-only/writable, so this matches that behavior. | |
137 | */ | |
138 | pte |= BYT_PTE_WRITEABLE; | |
139 | ||
140 | if (level != I915_CACHE_NONE) | |
141 | pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; | |
142 | ||
143 | return pte; | |
144 | } | |
145 | ||
80a74f7f | 146 | static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr, |
b35b380e BW |
147 | enum i915_cache_level level, |
148 | bool valid) | |
9119708c | 149 | { |
b35b380e | 150 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
0d8ff15e | 151 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
9119708c KG |
152 | |
153 | if (level != I915_CACHE_NONE) | |
87a6b688 | 154 | pte |= HSW_WB_LLC_AGE3; |
9119708c KG |
155 | |
156 | return pte; | |
157 | } | |
158 | ||
4d15c145 | 159 | static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr, |
b35b380e BW |
160 | enum i915_cache_level level, |
161 | bool valid) | |
4d15c145 | 162 | { |
b35b380e | 163 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
4d15c145 BW |
164 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
165 | ||
651d794f CW |
166 | switch (level) { |
167 | case I915_CACHE_NONE: | |
168 | break; | |
169 | case I915_CACHE_WT: | |
170 | pte |= HSW_WT_ELLC_LLC_AGE0; | |
171 | break; | |
172 | default: | |
4d15c145 | 173 | pte |= HSW_WB_ELLC_LLC_AGE0; |
651d794f CW |
174 | break; |
175 | } | |
4d15c145 BW |
176 | |
177 | return pte; | |
178 | } | |
179 | ||
3e302542 | 180 | static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt) |
6197349b | 181 | { |
853ba5d2 | 182 | struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; |
6197349b BW |
183 | gen6_gtt_pte_t __iomem *pd_addr; |
184 | uint32_t pd_entry; | |
185 | int i; | |
186 | ||
0a732870 | 187 | WARN_ON(ppgtt->pd_offset & 0x3f); |
6197349b BW |
188 | pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm + |
189 | ppgtt->pd_offset / sizeof(gen6_gtt_pte_t); | |
190 | for (i = 0; i < ppgtt->num_pd_entries; i++) { | |
191 | dma_addr_t pt_addr; | |
192 | ||
193 | pt_addr = ppgtt->pt_dma_addr[i]; | |
194 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr); | |
195 | pd_entry |= GEN6_PDE_VALID; | |
196 | ||
197 | writel(pd_entry, pd_addr + i); | |
198 | } | |
199 | readl(pd_addr); | |
3e302542 BW |
200 | } |
201 | ||
202 | static int gen6_ppgtt_enable(struct drm_device *dev) | |
203 | { | |
204 | drm_i915_private_t *dev_priv = dev->dev_private; | |
205 | uint32_t pd_offset; | |
206 | struct intel_ring_buffer *ring; | |
207 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; | |
208 | int i; | |
209 | ||
210 | BUG_ON(ppgtt->pd_offset & 0x3f); | |
211 | ||
212 | gen6_write_pdes(ppgtt); | |
6197349b BW |
213 | |
214 | pd_offset = ppgtt->pd_offset; | |
215 | pd_offset /= 64; /* in cachelines, */ | |
216 | pd_offset <<= 16; | |
217 | ||
218 | if (INTEL_INFO(dev)->gen == 6) { | |
219 | uint32_t ecochk, gab_ctl, ecobits; | |
220 | ||
221 | ecobits = I915_READ(GAC_ECO_BITS); | |
3b9d7888 VS |
222 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | |
223 | ECOBITS_PPGTT_CACHE64B); | |
6197349b BW |
224 | |
225 | gab_ctl = I915_READ(GAB_CTL); | |
226 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); | |
227 | ||
228 | ecochk = I915_READ(GAM_ECOCHK); | |
229 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | | |
230 | ECOCHK_PPGTT_CACHE64B); | |
231 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); | |
232 | } else if (INTEL_INFO(dev)->gen >= 7) { | |
a6f429a5 | 233 | uint32_t ecochk, ecobits; |
a65c2fcd VS |
234 | |
235 | ecobits = I915_READ(GAC_ECO_BITS); | |
236 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); | |
237 | ||
a6f429a5 VS |
238 | ecochk = I915_READ(GAM_ECOCHK); |
239 | if (IS_HASWELL(dev)) { | |
240 | ecochk |= ECOCHK_PPGTT_WB_HSW; | |
241 | } else { | |
242 | ecochk |= ECOCHK_PPGTT_LLC_IVB; | |
243 | ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; | |
244 | } | |
245 | I915_WRITE(GAM_ECOCHK, ecochk); | |
6197349b BW |
246 | /* GFX_MODE is per-ring on gen7+ */ |
247 | } | |
248 | ||
249 | for_each_ring(ring, dev_priv, i) { | |
250 | if (INTEL_INFO(dev)->gen >= 7) | |
251 | I915_WRITE(RING_MODE_GEN7(ring), | |
252 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); | |
253 | ||
254 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); | |
255 | I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset); | |
256 | } | |
b7c36d25 | 257 | return 0; |
6197349b BW |
258 | } |
259 | ||
1d2a314c | 260 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
853ba5d2 | 261 | static void gen6_ppgtt_clear_range(struct i915_address_space *vm, |
1d2a314c | 262 | unsigned first_entry, |
828c7908 BW |
263 | unsigned num_entries, |
264 | bool use_scratch) | |
1d2a314c | 265 | { |
853ba5d2 BW |
266 | struct i915_hw_ppgtt *ppgtt = |
267 | container_of(vm, struct i915_hw_ppgtt, base); | |
e7c2b58b | 268 | gen6_gtt_pte_t *pt_vaddr, scratch_pte; |
a15326a5 | 269 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
7bddb01f DV |
270 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
271 | unsigned last_pte, i; | |
1d2a314c | 272 | |
b35b380e | 273 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true); |
1d2a314c | 274 | |
7bddb01f DV |
275 | while (num_entries) { |
276 | last_pte = first_pte + num_entries; | |
277 | if (last_pte > I915_PPGTT_PT_ENTRIES) | |
278 | last_pte = I915_PPGTT_PT_ENTRIES; | |
279 | ||
a15326a5 | 280 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
1d2a314c | 281 | |
7bddb01f DV |
282 | for (i = first_pte; i < last_pte; i++) |
283 | pt_vaddr[i] = scratch_pte; | |
1d2a314c DV |
284 | |
285 | kunmap_atomic(pt_vaddr); | |
1d2a314c | 286 | |
7bddb01f DV |
287 | num_entries -= last_pte - first_pte; |
288 | first_pte = 0; | |
a15326a5 | 289 | act_pt++; |
7bddb01f | 290 | } |
1d2a314c DV |
291 | } |
292 | ||
853ba5d2 | 293 | static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, |
def886c3 DV |
294 | struct sg_table *pages, |
295 | unsigned first_entry, | |
296 | enum i915_cache_level cache_level) | |
297 | { | |
853ba5d2 BW |
298 | struct i915_hw_ppgtt *ppgtt = |
299 | container_of(vm, struct i915_hw_ppgtt, base); | |
e7c2b58b | 300 | gen6_gtt_pte_t *pt_vaddr; |
a15326a5 | 301 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
6e995e23 ID |
302 | unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
303 | struct sg_page_iter sg_iter; | |
304 | ||
a15326a5 | 305 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
6e995e23 ID |
306 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
307 | dma_addr_t page_addr; | |
308 | ||
2db76d7c | 309 | page_addr = sg_page_iter_dma_address(&sg_iter); |
b35b380e | 310 | pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true); |
6e995e23 ID |
311 | if (++act_pte == I915_PPGTT_PT_ENTRIES) { |
312 | kunmap_atomic(pt_vaddr); | |
a15326a5 DV |
313 | act_pt++; |
314 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); | |
6e995e23 | 315 | act_pte = 0; |
def886c3 | 316 | |
def886c3 | 317 | } |
def886c3 | 318 | } |
6e995e23 | 319 | kunmap_atomic(pt_vaddr); |
def886c3 DV |
320 | } |
321 | ||
853ba5d2 | 322 | static void gen6_ppgtt_cleanup(struct i915_address_space *vm) |
1d2a314c | 323 | { |
853ba5d2 BW |
324 | struct i915_hw_ppgtt *ppgtt = |
325 | container_of(vm, struct i915_hw_ppgtt, base); | |
3440d265 DV |
326 | int i; |
327 | ||
93bd8649 BW |
328 | drm_mm_takedown(&ppgtt->base.mm); |
329 | ||
3440d265 DV |
330 | if (ppgtt->pt_dma_addr) { |
331 | for (i = 0; i < ppgtt->num_pd_entries; i++) | |
853ba5d2 | 332 | pci_unmap_page(ppgtt->base.dev->pdev, |
3440d265 DV |
333 | ppgtt->pt_dma_addr[i], |
334 | 4096, PCI_DMA_BIDIRECTIONAL); | |
335 | } | |
336 | ||
337 | kfree(ppgtt->pt_dma_addr); | |
338 | for (i = 0; i < ppgtt->num_pd_entries; i++) | |
339 | __free_page(ppgtt->pt_pages[i]); | |
340 | kfree(ppgtt->pt_pages); | |
341 | kfree(ppgtt); | |
342 | } | |
343 | ||
344 | static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) | |
345 | { | |
853ba5d2 | 346 | struct drm_device *dev = ppgtt->base.dev; |
1d2a314c | 347 | struct drm_i915_private *dev_priv = dev->dev_private; |
1d2a314c | 348 | unsigned first_pd_entry_in_global_pt; |
1d2a314c DV |
349 | int i; |
350 | int ret = -ENOMEM; | |
351 | ||
352 | /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024 | |
353 | * entries. For aliasing ppgtt support we just steal them at the end for | |
354 | * now. */ | |
e1b73cba | 355 | first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt); |
1d2a314c | 356 | |
08c45263 | 357 | ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; |
6670a5a5 | 358 | ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES; |
6197349b | 359 | ppgtt->enable = gen6_ppgtt_enable; |
853ba5d2 BW |
360 | ppgtt->base.clear_range = gen6_ppgtt_clear_range; |
361 | ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; | |
362 | ppgtt->base.cleanup = gen6_ppgtt_cleanup; | |
363 | ppgtt->base.scratch = dev_priv->gtt.base.scratch; | |
a1e22653 | 364 | ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *), |
1d2a314c DV |
365 | GFP_KERNEL); |
366 | if (!ppgtt->pt_pages) | |
3440d265 | 367 | return -ENOMEM; |
1d2a314c DV |
368 | |
369 | for (i = 0; i < ppgtt->num_pd_entries; i++) { | |
370 | ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL); | |
371 | if (!ppgtt->pt_pages[i]) | |
372 | goto err_pt_alloc; | |
373 | } | |
374 | ||
a1e22653 | 375 | ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t), |
8d2e6308 BW |
376 | GFP_KERNEL); |
377 | if (!ppgtt->pt_dma_addr) | |
378 | goto err_pt_alloc; | |
1d2a314c | 379 | |
8d2e6308 BW |
380 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
381 | dma_addr_t pt_addr; | |
211c568b | 382 | |
8d2e6308 BW |
383 | pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096, |
384 | PCI_DMA_BIDIRECTIONAL); | |
1d2a314c | 385 | |
8d2e6308 BW |
386 | if (pci_dma_mapping_error(dev->pdev, pt_addr)) { |
387 | ret = -EIO; | |
388 | goto err_pd_pin; | |
1d2a314c | 389 | |
211c568b | 390 | } |
8d2e6308 | 391 | ppgtt->pt_dma_addr[i] = pt_addr; |
1d2a314c | 392 | } |
1d2a314c | 393 | |
853ba5d2 | 394 | ppgtt->base.clear_range(&ppgtt->base, 0, |
828c7908 | 395 | ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true); |
1d2a314c | 396 | |
e7c2b58b | 397 | ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t); |
1d2a314c | 398 | |
1d2a314c DV |
399 | return 0; |
400 | ||
401 | err_pd_pin: | |
402 | if (ppgtt->pt_dma_addr) { | |
403 | for (i--; i >= 0; i--) | |
404 | pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i], | |
405 | 4096, PCI_DMA_BIDIRECTIONAL); | |
406 | } | |
407 | err_pt_alloc: | |
408 | kfree(ppgtt->pt_dma_addr); | |
409 | for (i = 0; i < ppgtt->num_pd_entries; i++) { | |
410 | if (ppgtt->pt_pages[i]) | |
411 | __free_page(ppgtt->pt_pages[i]); | |
412 | } | |
413 | kfree(ppgtt->pt_pages); | |
3440d265 DV |
414 | |
415 | return ret; | |
416 | } | |
417 | ||
418 | static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev) | |
419 | { | |
420 | struct drm_i915_private *dev_priv = dev->dev_private; | |
421 | struct i915_hw_ppgtt *ppgtt; | |
422 | int ret; | |
423 | ||
424 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); | |
425 | if (!ppgtt) | |
426 | return -ENOMEM; | |
427 | ||
853ba5d2 | 428 | ppgtt->base.dev = dev; |
3440d265 | 429 | |
3ed124b2 BW |
430 | if (INTEL_INFO(dev)->gen < 8) |
431 | ret = gen6_ppgtt_init(ppgtt); | |
8fe6bd23 DV |
432 | else if (IS_GEN8(dev)) |
433 | ret = -ENOSYS; | |
3ed124b2 BW |
434 | else |
435 | BUG(); | |
436 | ||
3440d265 DV |
437 | if (ret) |
438 | kfree(ppgtt); | |
93bd8649 | 439 | else { |
3440d265 | 440 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
93bd8649 BW |
441 | drm_mm_init(&ppgtt->base.mm, ppgtt->base.start, |
442 | ppgtt->base.total); | |
443 | } | |
1d2a314c DV |
444 | |
445 | return ret; | |
446 | } | |
447 | ||
448 | void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev) | |
449 | { | |
450 | struct drm_i915_private *dev_priv = dev->dev_private; | |
451 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; | |
1d2a314c DV |
452 | |
453 | if (!ppgtt) | |
454 | return; | |
455 | ||
853ba5d2 | 456 | ppgtt->base.cleanup(&ppgtt->base); |
5963cf04 | 457 | dev_priv->mm.aliasing_ppgtt = NULL; |
1d2a314c DV |
458 | } |
459 | ||
7bddb01f DV |
460 | void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt, |
461 | struct drm_i915_gem_object *obj, | |
462 | enum i915_cache_level cache_level) | |
463 | { | |
853ba5d2 BW |
464 | ppgtt->base.insert_entries(&ppgtt->base, obj->pages, |
465 | i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT, | |
466 | cache_level); | |
7bddb01f DV |
467 | } |
468 | ||
469 | void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt, | |
470 | struct drm_i915_gem_object *obj) | |
471 | { | |
853ba5d2 BW |
472 | ppgtt->base.clear_range(&ppgtt->base, |
473 | i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT, | |
828c7908 BW |
474 | obj->base.size >> PAGE_SHIFT, |
475 | true); | |
7bddb01f DV |
476 | } |
477 | ||
a81cc00c BW |
478 | extern int intel_iommu_gfx_mapped; |
479 | /* Certain Gen5 chipsets require require idling the GPU before | |
480 | * unmapping anything from the GTT when VT-d is enabled. | |
481 | */ | |
482 | static inline bool needs_idle_maps(struct drm_device *dev) | |
483 | { | |
484 | #ifdef CONFIG_INTEL_IOMMU | |
485 | /* Query intel_iommu to see if we need the workaround. Presumably that | |
486 | * was loaded first. | |
487 | */ | |
488 | if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) | |
489 | return true; | |
490 | #endif | |
491 | return false; | |
492 | } | |
493 | ||
5c042287 BW |
494 | static bool do_idling(struct drm_i915_private *dev_priv) |
495 | { | |
496 | bool ret = dev_priv->mm.interruptible; | |
497 | ||
a81cc00c | 498 | if (unlikely(dev_priv->gtt.do_idle_maps)) { |
5c042287 | 499 | dev_priv->mm.interruptible = false; |
b2da9fe5 | 500 | if (i915_gpu_idle(dev_priv->dev)) { |
5c042287 BW |
501 | DRM_ERROR("Couldn't idle GPU\n"); |
502 | /* Wait a bit, in hopes it avoids the hang */ | |
503 | udelay(10); | |
504 | } | |
505 | } | |
506 | ||
507 | return ret; | |
508 | } | |
509 | ||
510 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) | |
511 | { | |
a81cc00c | 512 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
5c042287 BW |
513 | dev_priv->mm.interruptible = interruptible; |
514 | } | |
515 | ||
828c7908 BW |
516 | void i915_check_and_clear_faults(struct drm_device *dev) |
517 | { | |
518 | struct drm_i915_private *dev_priv = dev->dev_private; | |
519 | struct intel_ring_buffer *ring; | |
520 | int i; | |
521 | ||
522 | if (INTEL_INFO(dev)->gen < 6) | |
523 | return; | |
524 | ||
525 | for_each_ring(ring, dev_priv, i) { | |
526 | u32 fault_reg; | |
527 | fault_reg = I915_READ(RING_FAULT_REG(ring)); | |
528 | if (fault_reg & RING_FAULT_VALID) { | |
529 | DRM_DEBUG_DRIVER("Unexpected fault\n" | |
530 | "\tAddr: 0x%08lx\\n" | |
531 | "\tAddress space: %s\n" | |
532 | "\tSource ID: %d\n" | |
533 | "\tType: %d\n", | |
534 | fault_reg & PAGE_MASK, | |
535 | fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", | |
536 | RING_FAULT_SRCID(fault_reg), | |
537 | RING_FAULT_FAULT_TYPE(fault_reg)); | |
538 | I915_WRITE(RING_FAULT_REG(ring), | |
539 | fault_reg & ~RING_FAULT_VALID); | |
540 | } | |
541 | } | |
542 | POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); | |
543 | } | |
544 | ||
545 | void i915_gem_suspend_gtt_mappings(struct drm_device *dev) | |
546 | { | |
547 | struct drm_i915_private *dev_priv = dev->dev_private; | |
548 | ||
549 | /* Don't bother messing with faults pre GEN6 as we have little | |
550 | * documentation supporting that it's a good idea. | |
551 | */ | |
552 | if (INTEL_INFO(dev)->gen < 6) | |
553 | return; | |
554 | ||
555 | i915_check_and_clear_faults(dev); | |
556 | ||
557 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, | |
558 | dev_priv->gtt.base.start / PAGE_SIZE, | |
559 | dev_priv->gtt.base.total / PAGE_SIZE, | |
560 | false); | |
561 | } | |
562 | ||
76aaf220 DV |
563 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
564 | { | |
565 | struct drm_i915_private *dev_priv = dev->dev_private; | |
05394f39 | 566 | struct drm_i915_gem_object *obj; |
76aaf220 | 567 | |
828c7908 BW |
568 | i915_check_and_clear_faults(dev); |
569 | ||
bee4a186 | 570 | /* First fill our portion of the GTT with scratch pages */ |
853ba5d2 BW |
571 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
572 | dev_priv->gtt.base.start / PAGE_SIZE, | |
828c7908 BW |
573 | dev_priv->gtt.base.total / PAGE_SIZE, |
574 | true); | |
bee4a186 | 575 | |
35c20a60 | 576 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
2c22569b | 577 | i915_gem_clflush_object(obj, obj->pin_display); |
74163907 | 578 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
76aaf220 DV |
579 | } |
580 | ||
e76e9aeb | 581 | i915_gem_chipset_flush(dev); |
76aaf220 | 582 | } |
7c2e6fdf | 583 | |
74163907 | 584 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
7c2e6fdf | 585 | { |
9da3da66 | 586 | if (obj->has_dma_mapping) |
74163907 | 587 | return 0; |
9da3da66 CW |
588 | |
589 | if (!dma_map_sg(&obj->base.dev->pdev->dev, | |
590 | obj->pages->sgl, obj->pages->nents, | |
591 | PCI_DMA_BIDIRECTIONAL)) | |
592 | return -ENOSPC; | |
593 | ||
594 | return 0; | |
7c2e6fdf DV |
595 | } |
596 | ||
94ec8f61 BW |
597 | static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte) |
598 | { | |
599 | #ifdef writeq | |
600 | writeq(pte, addr); | |
601 | #else | |
602 | iowrite32((u32)pte, addr); | |
603 | iowrite32(pte >> 32, addr + 4); | |
604 | #endif | |
605 | } | |
606 | ||
607 | static void gen8_ggtt_insert_entries(struct i915_address_space *vm, | |
608 | struct sg_table *st, | |
609 | unsigned int first_entry, | |
610 | enum i915_cache_level level) | |
611 | { | |
612 | struct drm_i915_private *dev_priv = vm->dev->dev_private; | |
613 | gen8_gtt_pte_t __iomem *gtt_entries = | |
614 | (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; | |
615 | int i = 0; | |
616 | struct sg_page_iter sg_iter; | |
617 | dma_addr_t addr; | |
618 | ||
619 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { | |
620 | addr = sg_dma_address(sg_iter.sg) + | |
621 | (sg_iter.sg_pgoffset << PAGE_SHIFT); | |
622 | gen8_set_pte(>t_entries[i], | |
623 | gen8_pte_encode(addr, level, true)); | |
624 | i++; | |
625 | } | |
626 | ||
627 | /* | |
628 | * XXX: This serves as a posting read to make sure that the PTE has | |
629 | * actually been updated. There is some concern that even though | |
630 | * registers and PTEs are within the same BAR that they are potentially | |
631 | * of NUMA access patterns. Therefore, even with the way we assume | |
632 | * hardware should work, we must keep this posting read for paranoia. | |
633 | */ | |
634 | if (i != 0) | |
635 | WARN_ON(readq(>t_entries[i-1]) | |
636 | != gen8_pte_encode(addr, level, true)); | |
637 | ||
638 | #if 0 /* TODO: Still needed on GEN8? */ | |
639 | /* This next bit makes the above posting read even more important. We | |
640 | * want to flush the TLBs only after we're certain all the PTE updates | |
641 | * have finished. | |
642 | */ | |
643 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); | |
644 | POSTING_READ(GFX_FLSH_CNTL_GEN6); | |
645 | #endif | |
646 | } | |
647 | ||
e76e9aeb BW |
648 | /* |
649 | * Binds an object into the global gtt with the specified cache level. The object | |
650 | * will be accessible to the GPU via commands whose operands reference offsets | |
651 | * within the global GTT as well as accessible by the GPU through the GMADR | |
652 | * mapped BAR (dev_priv->mm.gtt->gtt). | |
653 | */ | |
853ba5d2 | 654 | static void gen6_ggtt_insert_entries(struct i915_address_space *vm, |
7faf1ab2 DV |
655 | struct sg_table *st, |
656 | unsigned int first_entry, | |
657 | enum i915_cache_level level) | |
e76e9aeb | 658 | { |
853ba5d2 | 659 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
e7c2b58b BW |
660 | gen6_gtt_pte_t __iomem *gtt_entries = |
661 | (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; | |
6e995e23 ID |
662 | int i = 0; |
663 | struct sg_page_iter sg_iter; | |
e76e9aeb BW |
664 | dma_addr_t addr; |
665 | ||
6e995e23 | 666 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
2db76d7c | 667 | addr = sg_page_iter_dma_address(&sg_iter); |
b35b380e | 668 | iowrite32(vm->pte_encode(addr, level, true), >t_entries[i]); |
6e995e23 | 669 | i++; |
e76e9aeb BW |
670 | } |
671 | ||
e76e9aeb BW |
672 | /* XXX: This serves as a posting read to make sure that the PTE has |
673 | * actually been updated. There is some concern that even though | |
674 | * registers and PTEs are within the same BAR that they are potentially | |
675 | * of NUMA access patterns. Therefore, even with the way we assume | |
676 | * hardware should work, we must keep this posting read for paranoia. | |
677 | */ | |
678 | if (i != 0) | |
853ba5d2 | 679 | WARN_ON(readl(>t_entries[i-1]) != |
b35b380e | 680 | vm->pte_encode(addr, level, true)); |
0f9b91c7 BW |
681 | |
682 | /* This next bit makes the above posting read even more important. We | |
683 | * want to flush the TLBs only after we're certain all the PTE updates | |
684 | * have finished. | |
685 | */ | |
686 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); | |
687 | POSTING_READ(GFX_FLSH_CNTL_GEN6); | |
e76e9aeb BW |
688 | } |
689 | ||
94ec8f61 BW |
690 | static void gen8_ggtt_clear_range(struct i915_address_space *vm, |
691 | unsigned int first_entry, | |
692 | unsigned int num_entries, | |
693 | bool use_scratch) | |
694 | { | |
695 | struct drm_i915_private *dev_priv = vm->dev->dev_private; | |
696 | gen8_gtt_pte_t scratch_pte, __iomem *gtt_base = | |
697 | (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; | |
698 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; | |
699 | int i; | |
700 | ||
701 | if (WARN(num_entries > max_entries, | |
702 | "First entry = %d; Num entries = %d (max=%d)\n", | |
703 | first_entry, num_entries, max_entries)) | |
704 | num_entries = max_entries; | |
705 | ||
706 | scratch_pte = gen8_pte_encode(vm->scratch.addr, | |
707 | I915_CACHE_LLC, | |
708 | use_scratch); | |
709 | for (i = 0; i < num_entries; i++) | |
710 | gen8_set_pte(>t_base[i], scratch_pte); | |
711 | readl(gtt_base); | |
712 | } | |
713 | ||
853ba5d2 | 714 | static void gen6_ggtt_clear_range(struct i915_address_space *vm, |
7faf1ab2 | 715 | unsigned int first_entry, |
828c7908 BW |
716 | unsigned int num_entries, |
717 | bool use_scratch) | |
7faf1ab2 | 718 | { |
853ba5d2 | 719 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
e7c2b58b BW |
720 | gen6_gtt_pte_t scratch_pte, __iomem *gtt_base = |
721 | (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; | |
a54c0c27 | 722 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
7faf1ab2 DV |
723 | int i; |
724 | ||
725 | if (WARN(num_entries > max_entries, | |
726 | "First entry = %d; Num entries = %d (max=%d)\n", | |
727 | first_entry, num_entries, max_entries)) | |
728 | num_entries = max_entries; | |
729 | ||
828c7908 BW |
730 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch); |
731 | ||
7faf1ab2 DV |
732 | for (i = 0; i < num_entries; i++) |
733 | iowrite32(scratch_pte, >t_base[i]); | |
734 | readl(gtt_base); | |
735 | } | |
736 | ||
853ba5d2 | 737 | static void i915_ggtt_insert_entries(struct i915_address_space *vm, |
7faf1ab2 DV |
738 | struct sg_table *st, |
739 | unsigned int pg_start, | |
740 | enum i915_cache_level cache_level) | |
741 | { | |
742 | unsigned int flags = (cache_level == I915_CACHE_NONE) ? | |
743 | AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; | |
744 | ||
745 | intel_gtt_insert_sg_entries(st, pg_start, flags); | |
746 | ||
747 | } | |
748 | ||
853ba5d2 | 749 | static void i915_ggtt_clear_range(struct i915_address_space *vm, |
7faf1ab2 | 750 | unsigned int first_entry, |
828c7908 BW |
751 | unsigned int num_entries, |
752 | bool unused) | |
7faf1ab2 DV |
753 | { |
754 | intel_gtt_clear_range(first_entry, num_entries); | |
755 | } | |
756 | ||
757 | ||
74163907 DV |
758 | void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj, |
759 | enum i915_cache_level cache_level) | |
d5bd1449 CW |
760 | { |
761 | struct drm_device *dev = obj->base.dev; | |
7faf1ab2 | 762 | struct drm_i915_private *dev_priv = dev->dev_private; |
853ba5d2 | 763 | const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; |
7faf1ab2 | 764 | |
853ba5d2 BW |
765 | dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages, |
766 | entry, | |
767 | cache_level); | |
d5bd1449 | 768 | |
74898d7e | 769 | obj->has_global_gtt_mapping = 1; |
d5bd1449 CW |
770 | } |
771 | ||
05394f39 | 772 | void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj) |
74163907 | 773 | { |
7faf1ab2 DV |
774 | struct drm_device *dev = obj->base.dev; |
775 | struct drm_i915_private *dev_priv = dev->dev_private; | |
853ba5d2 | 776 | const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; |
7faf1ab2 | 777 | |
853ba5d2 BW |
778 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
779 | entry, | |
828c7908 BW |
780 | obj->base.size >> PAGE_SHIFT, |
781 | true); | |
74898d7e DV |
782 | |
783 | obj->has_global_gtt_mapping = 0; | |
74163907 DV |
784 | } |
785 | ||
786 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) | |
7c2e6fdf | 787 | { |
5c042287 BW |
788 | struct drm_device *dev = obj->base.dev; |
789 | struct drm_i915_private *dev_priv = dev->dev_private; | |
790 | bool interruptible; | |
791 | ||
792 | interruptible = do_idling(dev_priv); | |
793 | ||
9da3da66 CW |
794 | if (!obj->has_dma_mapping) |
795 | dma_unmap_sg(&dev->pdev->dev, | |
796 | obj->pages->sgl, obj->pages->nents, | |
797 | PCI_DMA_BIDIRECTIONAL); | |
5c042287 BW |
798 | |
799 | undo_idling(dev_priv, interruptible); | |
7c2e6fdf | 800 | } |
644ec02b | 801 | |
42d6ab48 CW |
802 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
803 | unsigned long color, | |
804 | unsigned long *start, | |
805 | unsigned long *end) | |
806 | { | |
807 | if (node->color != color) | |
808 | *start += 4096; | |
809 | ||
810 | if (!list_empty(&node->node_list)) { | |
811 | node = list_entry(node->node_list.next, | |
812 | struct drm_mm_node, | |
813 | node_list); | |
814 | if (node->allocated && node->color != color) | |
815 | *end -= 4096; | |
816 | } | |
817 | } | |
fbe5d36e | 818 | |
d7e5008f BW |
819 | void i915_gem_setup_global_gtt(struct drm_device *dev, |
820 | unsigned long start, | |
821 | unsigned long mappable_end, | |
822 | unsigned long end) | |
644ec02b | 823 | { |
e78891ca BW |
824 | /* Let GEM Manage all of the aperture. |
825 | * | |
826 | * However, leave one page at the end still bound to the scratch page. | |
827 | * There are a number of places where the hardware apparently prefetches | |
828 | * past the end of the object, and we've seen multiple hangs with the | |
829 | * GPU head pointer stuck in a batchbuffer bound at the last page of the | |
830 | * aperture. One page should be enough to keep any prefetching inside | |
831 | * of the aperture. | |
832 | */ | |
40d74980 BW |
833 | struct drm_i915_private *dev_priv = dev->dev_private; |
834 | struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; | |
ed2f3452 CW |
835 | struct drm_mm_node *entry; |
836 | struct drm_i915_gem_object *obj; | |
837 | unsigned long hole_start, hole_end; | |
644ec02b | 838 | |
35451cb6 BW |
839 | BUG_ON(mappable_end > end); |
840 | ||
ed2f3452 | 841 | /* Subtract the guard page ... */ |
40d74980 | 842 | drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE); |
42d6ab48 | 843 | if (!HAS_LLC(dev)) |
93bd8649 | 844 | dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust; |
644ec02b | 845 | |
ed2f3452 | 846 | /* Mark any preallocated objects as occupied */ |
35c20a60 | 847 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
40d74980 | 848 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); |
b3a070cc | 849 | int ret; |
edd41a87 | 850 | DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n", |
c6cfb325 BW |
851 | i915_gem_obj_ggtt_offset(obj), obj->base.size); |
852 | ||
853 | WARN_ON(i915_gem_obj_ggtt_bound(obj)); | |
40d74980 | 854 | ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); |
c6cfb325 | 855 | if (ret) |
b3a070cc | 856 | DRM_DEBUG_KMS("Reservation failed\n"); |
ed2f3452 | 857 | obj->has_global_gtt_mapping = 1; |
2f633156 | 858 | list_add(&vma->vma_link, &obj->vma_list); |
ed2f3452 CW |
859 | } |
860 | ||
853ba5d2 BW |
861 | dev_priv->gtt.base.start = start; |
862 | dev_priv->gtt.base.total = end - start; | |
644ec02b | 863 | |
ed2f3452 | 864 | /* Clear any non-preallocated blocks */ |
40d74980 | 865 | drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) { |
853ba5d2 | 866 | const unsigned long count = (hole_end - hole_start) / PAGE_SIZE; |
ed2f3452 CW |
867 | DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", |
868 | hole_start, hole_end); | |
828c7908 | 869 | ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true); |
ed2f3452 CW |
870 | } |
871 | ||
872 | /* And finally clear the reserved guard page */ | |
828c7908 | 873 | ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true); |
e76e9aeb BW |
874 | } |
875 | ||
d7e5008f BW |
876 | static bool |
877 | intel_enable_ppgtt(struct drm_device *dev) | |
878 | { | |
879 | if (i915_enable_ppgtt >= 0) | |
880 | return i915_enable_ppgtt; | |
881 | ||
882 | #ifdef CONFIG_INTEL_IOMMU | |
883 | /* Disable ppgtt on SNB if VT-d is on. */ | |
884 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) | |
885 | return false; | |
886 | #endif | |
887 | ||
888 | return true; | |
889 | } | |
890 | ||
891 | void i915_gem_init_global_gtt(struct drm_device *dev) | |
892 | { | |
893 | struct drm_i915_private *dev_priv = dev->dev_private; | |
894 | unsigned long gtt_size, mappable_size; | |
d7e5008f | 895 | |
853ba5d2 | 896 | gtt_size = dev_priv->gtt.base.total; |
93d18799 | 897 | mappable_size = dev_priv->gtt.mappable_end; |
d7e5008f BW |
898 | |
899 | if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) { | |
e78891ca | 900 | int ret; |
3eb1c005 BW |
901 | |
902 | if (INTEL_INFO(dev)->gen <= 7) { | |
903 | /* PPGTT pdes are stolen from global gtt ptes, so shrink the | |
904 | * aperture accordingly when using aliasing ppgtt. */ | |
6670a5a5 | 905 | gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE; |
3eb1c005 | 906 | } |
d7e5008f BW |
907 | |
908 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); | |
909 | ||
910 | ret = i915_gem_init_aliasing_ppgtt(dev); | |
e78891ca | 911 | if (!ret) |
d7e5008f | 912 | return; |
e78891ca BW |
913 | |
914 | DRM_ERROR("Aliased PPGTT setup failed %d\n", ret); | |
93bd8649 | 915 | drm_mm_takedown(&dev_priv->gtt.base.mm); |
6670a5a5 | 916 | gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE; |
d7e5008f | 917 | } |
e78891ca | 918 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
e76e9aeb BW |
919 | } |
920 | ||
921 | static int setup_scratch_page(struct drm_device *dev) | |
922 | { | |
923 | struct drm_i915_private *dev_priv = dev->dev_private; | |
924 | struct page *page; | |
925 | dma_addr_t dma_addr; | |
926 | ||
927 | page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); | |
928 | if (page == NULL) | |
929 | return -ENOMEM; | |
930 | get_page(page); | |
931 | set_pages_uc(page, 1); | |
932 | ||
933 | #ifdef CONFIG_INTEL_IOMMU | |
934 | dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, | |
935 | PCI_DMA_BIDIRECTIONAL); | |
936 | if (pci_dma_mapping_error(dev->pdev, dma_addr)) | |
937 | return -EINVAL; | |
938 | #else | |
939 | dma_addr = page_to_phys(page); | |
940 | #endif | |
853ba5d2 BW |
941 | dev_priv->gtt.base.scratch.page = page; |
942 | dev_priv->gtt.base.scratch.addr = dma_addr; | |
e76e9aeb BW |
943 | |
944 | return 0; | |
945 | } | |
946 | ||
947 | static void teardown_scratch_page(struct drm_device *dev) | |
948 | { | |
949 | struct drm_i915_private *dev_priv = dev->dev_private; | |
853ba5d2 BW |
950 | struct page *page = dev_priv->gtt.base.scratch.page; |
951 | ||
952 | set_pages_wb(page, 1); | |
953 | pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr, | |
e76e9aeb | 954 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
853ba5d2 BW |
955 | put_page(page); |
956 | __free_page(page); | |
e76e9aeb BW |
957 | } |
958 | ||
959 | static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) | |
960 | { | |
961 | snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; | |
962 | snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; | |
963 | return snb_gmch_ctl << 20; | |
964 | } | |
965 | ||
9459d252 BW |
966 | static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) |
967 | { | |
968 | bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; | |
969 | bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; | |
970 | if (bdw_gmch_ctl) | |
971 | bdw_gmch_ctl = 1 << bdw_gmch_ctl; | |
972 | return bdw_gmch_ctl << 20; | |
973 | } | |
974 | ||
baa09f5f | 975 | static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl) |
e76e9aeb BW |
976 | { |
977 | snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; | |
978 | snb_gmch_ctl &= SNB_GMCH_GMS_MASK; | |
979 | return snb_gmch_ctl << 25; /* 32 MB units */ | |
980 | } | |
981 | ||
9459d252 BW |
982 | static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) |
983 | { | |
984 | bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; | |
985 | bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; | |
986 | return bdw_gmch_ctl << 25; /* 32 MB units */ | |
987 | } | |
988 | ||
63340133 BW |
989 | static int ggtt_probe_common(struct drm_device *dev, |
990 | size_t gtt_size) | |
991 | { | |
992 | struct drm_i915_private *dev_priv = dev->dev_private; | |
993 | phys_addr_t gtt_bus_addr; | |
994 | int ret; | |
995 | ||
996 | /* For Modern GENs the PTEs and register space are split in the BAR */ | |
997 | gtt_bus_addr = pci_resource_start(dev->pdev, 0) + | |
998 | (pci_resource_len(dev->pdev, 0) / 2); | |
999 | ||
1000 | dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size); | |
1001 | if (!dev_priv->gtt.gsm) { | |
1002 | DRM_ERROR("Failed to map the gtt page table\n"); | |
1003 | return -ENOMEM; | |
1004 | } | |
1005 | ||
1006 | ret = setup_scratch_page(dev); | |
1007 | if (ret) { | |
1008 | DRM_ERROR("Scratch setup failed\n"); | |
1009 | /* iounmap will also get called at remove, but meh */ | |
1010 | iounmap(dev_priv->gtt.gsm); | |
1011 | } | |
1012 | ||
1013 | return ret; | |
1014 | } | |
1015 | ||
fbe5d36e BW |
1016 | /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability |
1017 | * bits. When using advanced contexts each context stores its own PAT, but | |
1018 | * writing this data shouldn't be harmful even in those cases. */ | |
1019 | static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv) | |
1020 | { | |
1021 | #define GEN8_PPAT_UC (0<<0) | |
1022 | #define GEN8_PPAT_WC (1<<0) | |
1023 | #define GEN8_PPAT_WT (2<<0) | |
1024 | #define GEN8_PPAT_WB (3<<0) | |
1025 | #define GEN8_PPAT_ELLC_OVERRIDE (0<<2) | |
1026 | /* FIXME(BDW): Bspec is completely confused about cache control bits. */ | |
1027 | #define GEN8_PPAT_LLC (1<<2) | |
1028 | #define GEN8_PPAT_LLCELLC (2<<2) | |
1029 | #define GEN8_PPAT_LLCeLLC (3<<2) | |
1030 | #define GEN8_PPAT_AGE(x) (x<<4) | |
1031 | #define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8)) | |
1032 | uint64_t pat; | |
1033 | ||
1034 | pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ | |
1035 | GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ | |
1036 | GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */ | |
1037 | GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ | |
1038 | GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | | |
1039 | GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | | |
1040 | GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | | |
1041 | GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); | |
1042 | ||
1043 | /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b | |
1044 | * write would work. */ | |
1045 | I915_WRITE(GEN8_PRIVATE_PAT, pat); | |
1046 | I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); | |
1047 | } | |
1048 | ||
63340133 BW |
1049 | static int gen8_gmch_probe(struct drm_device *dev, |
1050 | size_t *gtt_total, | |
1051 | size_t *stolen, | |
1052 | phys_addr_t *mappable_base, | |
1053 | unsigned long *mappable_end) | |
1054 | { | |
1055 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1056 | unsigned int gtt_size; | |
1057 | u16 snb_gmch_ctl; | |
1058 | int ret; | |
1059 | ||
1060 | /* TODO: We're not aware of mappable constraints on gen8 yet */ | |
1061 | *mappable_base = pci_resource_start(dev->pdev, 2); | |
1062 | *mappable_end = pci_resource_len(dev->pdev, 2); | |
1063 | ||
1064 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39))) | |
1065 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39)); | |
1066 | ||
1067 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); | |
1068 | ||
1069 | *stolen = gen8_get_stolen_size(snb_gmch_ctl); | |
1070 | ||
1071 | gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); | |
d31eb10e | 1072 | *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT; |
63340133 | 1073 | |
fbe5d36e BW |
1074 | gen8_setup_private_ppat(dev_priv); |
1075 | ||
63340133 BW |
1076 | ret = ggtt_probe_common(dev, gtt_size); |
1077 | ||
94ec8f61 BW |
1078 | dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range; |
1079 | dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries; | |
63340133 BW |
1080 | |
1081 | return ret; | |
1082 | } | |
1083 | ||
baa09f5f BW |
1084 | static int gen6_gmch_probe(struct drm_device *dev, |
1085 | size_t *gtt_total, | |
41907ddc BW |
1086 | size_t *stolen, |
1087 | phys_addr_t *mappable_base, | |
1088 | unsigned long *mappable_end) | |
e76e9aeb BW |
1089 | { |
1090 | struct drm_i915_private *dev_priv = dev->dev_private; | |
baa09f5f | 1091 | unsigned int gtt_size; |
e76e9aeb | 1092 | u16 snb_gmch_ctl; |
e76e9aeb BW |
1093 | int ret; |
1094 | ||
41907ddc BW |
1095 | *mappable_base = pci_resource_start(dev->pdev, 2); |
1096 | *mappable_end = pci_resource_len(dev->pdev, 2); | |
1097 | ||
baa09f5f BW |
1098 | /* 64/512MB is the current min/max we actually know of, but this is just |
1099 | * a coarse sanity check. | |
e76e9aeb | 1100 | */ |
41907ddc | 1101 | if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { |
baa09f5f BW |
1102 | DRM_ERROR("Unknown GMADR size (%lx)\n", |
1103 | dev_priv->gtt.mappable_end); | |
1104 | return -ENXIO; | |
e76e9aeb BW |
1105 | } |
1106 | ||
e76e9aeb BW |
1107 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) |
1108 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); | |
e76e9aeb | 1109 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
e76e9aeb | 1110 | |
63340133 | 1111 | *stolen = gen6_get_stolen_size(snb_gmch_ctl); |
e76e9aeb | 1112 | |
63340133 BW |
1113 | gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); |
1114 | *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT; | |
a93e4161 | 1115 | |
63340133 | 1116 | ret = ggtt_probe_common(dev, gtt_size); |
e76e9aeb | 1117 | |
853ba5d2 BW |
1118 | dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; |
1119 | dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; | |
7faf1ab2 | 1120 | |
e76e9aeb BW |
1121 | return ret; |
1122 | } | |
1123 | ||
853ba5d2 | 1124 | static void gen6_gmch_remove(struct i915_address_space *vm) |
e76e9aeb | 1125 | { |
853ba5d2 BW |
1126 | |
1127 | struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); | |
1128 | iounmap(gtt->gsm); | |
1129 | teardown_scratch_page(vm->dev); | |
644ec02b | 1130 | } |
baa09f5f BW |
1131 | |
1132 | static int i915_gmch_probe(struct drm_device *dev, | |
1133 | size_t *gtt_total, | |
41907ddc BW |
1134 | size_t *stolen, |
1135 | phys_addr_t *mappable_base, | |
1136 | unsigned long *mappable_end) | |
baa09f5f BW |
1137 | { |
1138 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1139 | int ret; | |
1140 | ||
baa09f5f BW |
1141 | ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); |
1142 | if (!ret) { | |
1143 | DRM_ERROR("failed to set up gmch\n"); | |
1144 | return -EIO; | |
1145 | } | |
1146 | ||
41907ddc | 1147 | intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); |
baa09f5f BW |
1148 | |
1149 | dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); | |
853ba5d2 BW |
1150 | dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; |
1151 | dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries; | |
baa09f5f BW |
1152 | |
1153 | return 0; | |
1154 | } | |
1155 | ||
853ba5d2 | 1156 | static void i915_gmch_remove(struct i915_address_space *vm) |
baa09f5f BW |
1157 | { |
1158 | intel_gmch_remove(); | |
1159 | } | |
1160 | ||
1161 | int i915_gem_gtt_init(struct drm_device *dev) | |
1162 | { | |
1163 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1164 | struct i915_gtt *gtt = &dev_priv->gtt; | |
baa09f5f BW |
1165 | int ret; |
1166 | ||
baa09f5f | 1167 | if (INTEL_INFO(dev)->gen <= 5) { |
b2f21b4d | 1168 | gtt->gtt_probe = i915_gmch_probe; |
853ba5d2 | 1169 | gtt->base.cleanup = i915_gmch_remove; |
63340133 | 1170 | } else if (INTEL_INFO(dev)->gen < 8) { |
b2f21b4d | 1171 | gtt->gtt_probe = gen6_gmch_probe; |
853ba5d2 | 1172 | gtt->base.cleanup = gen6_gmch_remove; |
4d15c145 | 1173 | if (IS_HASWELL(dev) && dev_priv->ellc_size) |
853ba5d2 | 1174 | gtt->base.pte_encode = iris_pte_encode; |
4d15c145 | 1175 | else if (IS_HASWELL(dev)) |
853ba5d2 | 1176 | gtt->base.pte_encode = hsw_pte_encode; |
b2f21b4d | 1177 | else if (IS_VALLEYVIEW(dev)) |
853ba5d2 | 1178 | gtt->base.pte_encode = byt_pte_encode; |
350ec881 CW |
1179 | else if (INTEL_INFO(dev)->gen >= 7) |
1180 | gtt->base.pte_encode = ivb_pte_encode; | |
b2f21b4d | 1181 | else |
350ec881 | 1182 | gtt->base.pte_encode = snb_pte_encode; |
63340133 BW |
1183 | } else { |
1184 | dev_priv->gtt.gtt_probe = gen8_gmch_probe; | |
1185 | dev_priv->gtt.base.cleanup = gen6_gmch_remove; | |
baa09f5f BW |
1186 | } |
1187 | ||
853ba5d2 | 1188 | ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size, |
b2f21b4d | 1189 | >t->mappable_base, >t->mappable_end); |
a54c0c27 | 1190 | if (ret) |
baa09f5f | 1191 | return ret; |
baa09f5f | 1192 | |
853ba5d2 BW |
1193 | gtt->base.dev = dev; |
1194 | ||
baa09f5f | 1195 | /* GMADR is the PCI mmio aperture into the global GTT. */ |
853ba5d2 BW |
1196 | DRM_INFO("Memory usable by graphics device = %zdM\n", |
1197 | gtt->base.total >> 20); | |
b2f21b4d BW |
1198 | DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20); |
1199 | DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); | |
baa09f5f BW |
1200 | |
1201 | return 0; | |
1202 | } |