intel_gtt.c revision 1.2 1 /* $NetBSD: intel_gtt.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $ */
2
3 // SPDX-License-Identifier: MIT
4 /*
5 * Copyright 2020 Intel Corporation
6 */
7
8 #include <sys/cdefs.h>
9 __KERNEL_RCSID(0, "$NetBSD: intel_gtt.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $");
10
11 #include <linux/slab.h> /* fault-inject.h is not standalone! */
12
13 #include <linux/fault-inject.h>
14
15 #include "i915_trace.h"
16 #include "intel_gt.h"
17 #include "intel_gtt.h"
18
19 void stash_init(struct pagestash *stash)
20 {
21 pagevec_init(&stash->pvec);
22 spin_lock_init(&stash->lock);
23 }
24
25 static struct page *stash_pop_page(struct pagestash *stash)
26 {
27 struct page *page = NULL;
28
29 spin_lock(&stash->lock);
30 if (likely(stash->pvec.nr))
31 page = stash->pvec.pages[--stash->pvec.nr];
32 spin_unlock(&stash->lock);
33
34 return page;
35 }
36
37 static void stash_push_pagevec(struct pagestash *stash, struct pagevec *pvec)
38 {
39 unsigned int nr;
40
41 spin_lock_nested(&stash->lock, SINGLE_DEPTH_NESTING);
42
43 nr = min_t(typeof(nr), pvec->nr, pagevec_space(&stash->pvec));
44 memcpy(stash->pvec.pages + stash->pvec.nr,
45 pvec->pages + pvec->nr - nr,
46 sizeof(pvec->pages[0]) * nr);
47 stash->pvec.nr += nr;
48
49 spin_unlock(&stash->lock);
50
51 pvec->nr -= nr;
52 }
53
54 static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
55 {
56 struct pagevec stack;
57 struct page *page;
58
59 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
60 i915_gem_shrink_all(vm->i915);
61
62 page = stash_pop_page(&vm->free_pages);
63 if (page)
64 return page;
65
66 if (!vm->pt_kmap_wc)
67 return alloc_page(gfp);
68
69 /* Look in our global stash of WC pages... */
70 page = stash_pop_page(&vm->i915->mm.wc_stash);
71 if (page)
72 return page;
73
74 /*
75 * Otherwise batch allocate pages to amortize cost of set_pages_wc.
76 *
77 * We have to be careful as page allocation may trigger the shrinker
78 * (via direct reclaim) which will fill up the WC stash underneath us.
79 * So we add our WB pages into a temporary pvec on the stack and merge
80 * them into the WC stash after all the allocations are complete.
81 */
82 pagevec_init(&stack);
83 do {
84 struct page *page;
85
86 page = alloc_page(gfp);
87 if (unlikely(!page))
88 break;
89
90 stack.pages[stack.nr++] = page;
91 } while (pagevec_space(&stack));
92
93 if (stack.nr && !set_pages_array_wc(stack.pages, stack.nr)) {
94 page = stack.pages[--stack.nr];
95
96 /* Merge spare WC pages to the global stash */
97 if (stack.nr)
98 stash_push_pagevec(&vm->i915->mm.wc_stash, &stack);
99
100 /* Push any surplus WC pages onto the local VM stash */
101 if (stack.nr)
102 stash_push_pagevec(&vm->free_pages, &stack);
103 }
104
105 /* Return unwanted leftovers */
106 if (unlikely(stack.nr)) {
107 WARN_ON_ONCE(set_pages_array_wb(stack.pages, stack.nr));
108 __pagevec_release(&stack);
109 }
110
111 return page;
112 }
113
114 static void vm_free_pages_release(struct i915_address_space *vm,
115 bool immediate)
116 {
117 struct pagevec *pvec = &vm->free_pages.pvec;
118 struct pagevec stack;
119
120 lockdep_assert_held(&vm->free_pages.lock);
121 GEM_BUG_ON(!pagevec_count(pvec));
122
123 if (vm->pt_kmap_wc) {
124 /*
125 * When we use WC, first fill up the global stash and then
126 * only if full immediately free the overflow.
127 */
128 stash_push_pagevec(&vm->i915->mm.wc_stash, pvec);
129
130 /*
131 * As we have made some room in the VM's free_pages,
132 * we can wait for it to fill again. Unless we are
133 * inside i915_address_space_fini() and must
134 * immediately release the pages!
135 */
136 if (pvec->nr <= (immediate ? 0 : PAGEVEC_SIZE - 1))
137 return;
138
139 /*
140 * We have to drop the lock to allow ourselves to sleep,
141 * so take a copy of the pvec and clear the stash for
142 * others to use it as we sleep.
143 */
144 stack = *pvec;
145 pagevec_reinit(pvec);
146 spin_unlock(&vm->free_pages.lock);
147
148 pvec = &stack;
149 set_pages_array_wb(pvec->pages, pvec->nr);
150
151 spin_lock(&vm->free_pages.lock);
152 }
153
154 __pagevec_release(pvec);
155 }
156
157 static void vm_free_page(struct i915_address_space *vm, struct page *page)
158 {
159 /*
160 * On !llc, we need to change the pages back to WB. We only do so
161 * in bulk, so we rarely need to change the page attributes here,
162 * but doing so requires a stop_machine() from deep inside arch/x86/mm.
163 * To make detection of the possible sleep more likely, use an
164 * unconditional might_sleep() for everybody.
165 */
166 might_sleep();
167 spin_lock(&vm->free_pages.lock);
168 while (!pagevec_space(&vm->free_pages.pvec))
169 vm_free_pages_release(vm, false);
170 GEM_BUG_ON(pagevec_count(&vm->free_pages.pvec) >= PAGEVEC_SIZE);
171 pagevec_add(&vm->free_pages.pvec, page);
172 spin_unlock(&vm->free_pages.lock);
173 }
174
175 void __i915_vm_close(struct i915_address_space *vm)
176 {
177 struct i915_vma *vma, *vn;
178
179 mutex_lock(&vm->mutex);
180 list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
181 struct drm_i915_gem_object *obj = vma->obj;
182
183 /* Keep the obj (and hence the vma) alive as _we_ destroy it */
184 if (!kref_get_unless_zero(&obj->base.refcount))
185 continue;
186
187 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
188 WARN_ON(__i915_vma_unbind(vma));
189 __i915_vma_put(vma);
190
191 i915_gem_object_put(obj);
192 }
193 GEM_BUG_ON(!list_empty(&vm->bound_list));
194 mutex_unlock(&vm->mutex);
195 }
196
197 void i915_address_space_fini(struct i915_address_space *vm)
198 {
199 spin_lock(&vm->free_pages.lock);
200 if (pagevec_count(&vm->free_pages.pvec))
201 vm_free_pages_release(vm, true);
202 GEM_BUG_ON(pagevec_count(&vm->free_pages.pvec));
203 spin_unlock(&vm->free_pages.lock);
204
205 drm_mm_takedown(&vm->mm);
206
207 mutex_destroy(&vm->mutex);
208 }
209
210 static void __i915_vm_release(struct work_struct *work)
211 {
212 struct i915_address_space *vm =
213 container_of(work, struct i915_address_space, rcu.work);
214
215 vm->cleanup(vm);
216 i915_address_space_fini(vm);
217
218 kfree(vm);
219 }
220
221 void i915_vm_release(struct kref *kref)
222 {
223 struct i915_address_space *vm =
224 container_of(kref, struct i915_address_space, ref);
225
226 GEM_BUG_ON(i915_is_ggtt(vm));
227 trace_i915_ppgtt_release(vm);
228
229 queue_rcu_work(vm->i915->wq, &vm->rcu);
230 }
231
232 void i915_address_space_init(struct i915_address_space *vm, int subclass)
233 {
234 kref_init(&vm->ref);
235 INIT_RCU_WORK(&vm->rcu, __i915_vm_release);
236 atomic_set(&vm->open, 1);
237
238 /*
239 * The vm->mutex must be reclaim safe (for use in the shrinker).
240 * Do a dummy acquire now under fs_reclaim so that any allocation
241 * attempt holding the lock is immediately reported by lockdep.
242 */
243 mutex_init(&vm->mutex);
244 lockdep_set_subclass(&vm->mutex, subclass);
245 i915_gem_shrinker_taints_mutex(vm->i915, &vm->mutex);
246
247 GEM_BUG_ON(!vm->total);
248 drm_mm_init(&vm->mm, 0, vm->total);
249 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
250
251 stash_init(&vm->free_pages);
252
253 INIT_LIST_HEAD(&vm->bound_list);
254 }
255
256 void clear_pages(struct i915_vma *vma)
257 {
258 GEM_BUG_ON(!vma->pages);
259
260 if (vma->pages != vma->obj->mm.pages) {
261 sg_free_table(vma->pages);
262 kfree(vma->pages);
263 }
264 vma->pages = NULL;
265
266 memset(&vma->page_sizes, 0, sizeof(vma->page_sizes));
267 }
268
269 static int __setup_page_dma(struct i915_address_space *vm,
270 struct i915_page_dma *p,
271 gfp_t gfp)
272 {
273 p->page = vm_alloc_page(vm, gfp | I915_GFP_ALLOW_FAIL);
274 if (unlikely(!p->page))
275 return -ENOMEM;
276
277 p->daddr = dma_map_page_attrs(vm->dma,
278 p->page, 0, PAGE_SIZE,
279 PCI_DMA_BIDIRECTIONAL,
280 DMA_ATTR_SKIP_CPU_SYNC |
281 DMA_ATTR_NO_WARN);
282 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
283 vm_free_page(vm, p->page);
284 return -ENOMEM;
285 }
286
287 return 0;
288 }
289
290 int setup_page_dma(struct i915_address_space *vm, struct i915_page_dma *p)
291 {
292 return __setup_page_dma(vm, p, __GFP_HIGHMEM);
293 }
294
295 void cleanup_page_dma(struct i915_address_space *vm, struct i915_page_dma *p)
296 {
297 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
298 vm_free_page(vm, p->page);
299 }
300
301 void
302 fill_page_dma(const struct i915_page_dma *p, const u64 val, unsigned int count)
303 {
304 kunmap_atomic(memset64(kmap_atomic(p->page), val, count));
305 }
306
307 int setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
308 {
309 unsigned long size;
310
311 /*
312 * In order to utilize 64K pages for an object with a size < 2M, we will
313 * need to support a 64K scratch page, given that every 16th entry for a
314 * page-table operating in 64K mode must point to a properly aligned 64K
315 * region, including any PTEs which happen to point to scratch.
316 *
317 * This is only relevant for the 48b PPGTT where we support
318 * huge-gtt-pages, see also i915_vma_insert(). However, as we share the
319 * scratch (read-only) between all vm, we create one 64k scratch page
320 * for all.
321 */
322 size = I915_GTT_PAGE_SIZE_4K;
323 if (i915_vm_is_4lvl(vm) &&
324 HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K)) {
325 size = I915_GTT_PAGE_SIZE_64K;
326 gfp |= __GFP_NOWARN;
327 }
328 gfp |= __GFP_ZERO | __GFP_RETRY_MAYFAIL;
329
330 do {
331 unsigned int order = get_order(size);
332 struct page *page;
333 dma_addr_t addr;
334
335 page = alloc_pages(gfp, order);
336 if (unlikely(!page))
337 goto skip;
338
339 addr = dma_map_page_attrs(vm->dma,
340 page, 0, size,
341 PCI_DMA_BIDIRECTIONAL,
342 DMA_ATTR_SKIP_CPU_SYNC |
343 DMA_ATTR_NO_WARN);
344 if (unlikely(dma_mapping_error(vm->dma, addr)))
345 goto free_page;
346
347 if (unlikely(!IS_ALIGNED(addr, size)))
348 goto unmap_page;
349
350 vm->scratch[0].base.page = page;
351 vm->scratch[0].base.daddr = addr;
352 vm->scratch_order = order;
353 return 0;
354
355 unmap_page:
356 dma_unmap_page(vm->dma, addr, size, PCI_DMA_BIDIRECTIONAL);
357 free_page:
358 __free_pages(page, order);
359 skip:
360 if (size == I915_GTT_PAGE_SIZE_4K)
361 return -ENOMEM;
362
363 size = I915_GTT_PAGE_SIZE_4K;
364 gfp &= ~__GFP_NOWARN;
365 } while (1);
366 }
367
368 void cleanup_scratch_page(struct i915_address_space *vm)
369 {
370 struct i915_page_dma *p = px_base(&vm->scratch[0]);
371 unsigned int order = vm->scratch_order;
372
373 dma_unmap_page(vm->dma, p->daddr, BIT(order) << PAGE_SHIFT,
374 PCI_DMA_BIDIRECTIONAL);
375 __free_pages(p->page, order);
376 }
377
378 void free_scratch(struct i915_address_space *vm)
379 {
380 int i;
381
382 if (!px_dma(&vm->scratch[0])) /* set to 0 on clones */
383 return;
384
385 for (i = 1; i <= vm->top; i++) {
386 if (!px_dma(&vm->scratch[i]))
387 break;
388 cleanup_page_dma(vm, px_base(&vm->scratch[i]));
389 }
390
391 cleanup_scratch_page(vm);
392 }
393
394 void gtt_write_workarounds(struct intel_gt *gt)
395 {
396 struct drm_i915_private *i915 = gt->i915;
397 struct intel_uncore *uncore = gt->uncore;
398
399 /*
400 * This function is for gtt related workarounds. This function is
401 * called on driver load and after a GPU reset, so you can place
402 * workarounds here even if they get overwritten by GPU reset.
403 */
404 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
405 if (IS_BROADWELL(i915))
406 intel_uncore_write(uncore,
407 GEN8_L3_LRA_1_GPGPU,
408 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
409 else if (IS_CHERRYVIEW(i915))
410 intel_uncore_write(uncore,
411 GEN8_L3_LRA_1_GPGPU,
412 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
413 else if (IS_GEN9_LP(i915))
414 intel_uncore_write(uncore,
415 GEN8_L3_LRA_1_GPGPU,
416 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
417 else if (INTEL_GEN(i915) >= 9 && INTEL_GEN(i915) <= 11)
418 intel_uncore_write(uncore,
419 GEN8_L3_LRA_1_GPGPU,
420 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
421
422 /*
423 * To support 64K PTEs we need to first enable the use of the
424 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
425 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
426 * shouldn't be needed after GEN10.
427 *
428 * 64K pages were first introduced from BDW+, although technically they
429 * only *work* from gen9+. For pre-BDW we instead have the option for
430 * 32K pages, but we don't currently have any support for it in our
431 * driver.
432 */
433 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) &&
434 INTEL_GEN(i915) <= 10)
435 intel_uncore_rmw(uncore,
436 GEN8_GAMW_ECO_DEV_RW_IA,
437 0,
438 GAMW_ECO_ENABLE_64K_IPS_FIELD);
439
440 if (IS_GEN_RANGE(i915, 8, 11)) {
441 bool can_use_gtt_cache = true;
442
443 /*
444 * According to the BSpec if we use 2M/1G pages then we also
445 * need to disable the GTT cache. At least on BDW we can see
446 * visual corruption when using 2M pages, and not disabling the
447 * GTT cache.
448 */
449 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_2M))
450 can_use_gtt_cache = false;
451
452 /* WaGttCachingOffByDefault */
453 intel_uncore_write(uncore,
454 HSW_GTT_CACHE_EN,
455 can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
456 WARN_ON_ONCE(can_use_gtt_cache &&
457 intel_uncore_read(uncore,
458 HSW_GTT_CACHE_EN) == 0);
459 }
460 }
461
462 u64 gen8_pte_encode(dma_addr_t addr,
463 enum i915_cache_level level,
464 u32 flags)
465 {
466 gen8_pte_t pte = addr | _PAGE_PRESENT | _PAGE_RW;
467
468 if (unlikely(flags & PTE_READ_ONLY))
469 pte &= ~_PAGE_RW;
470
471 switch (level) {
472 case I915_CACHE_NONE:
473 pte |= PPAT_UNCACHED;
474 break;
475 case I915_CACHE_WT:
476 pte |= PPAT_DISPLAY_ELLC;
477 break;
478 default:
479 pte |= PPAT_CACHED;
480 break;
481 }
482
483 return pte;
484 }
485
486 static void tgl_setup_private_ppat(struct intel_uncore *uncore)
487 {
488 /* TGL doesn't support LLC or AGE settings */
489 intel_uncore_write(uncore, GEN12_PAT_INDEX(0), GEN8_PPAT_WB);
490 intel_uncore_write(uncore, GEN12_PAT_INDEX(1), GEN8_PPAT_WC);
491 intel_uncore_write(uncore, GEN12_PAT_INDEX(2), GEN8_PPAT_WT);
492 intel_uncore_write(uncore, GEN12_PAT_INDEX(3), GEN8_PPAT_UC);
493 intel_uncore_write(uncore, GEN12_PAT_INDEX(4), GEN8_PPAT_WB);
494 intel_uncore_write(uncore, GEN12_PAT_INDEX(5), GEN8_PPAT_WB);
495 intel_uncore_write(uncore, GEN12_PAT_INDEX(6), GEN8_PPAT_WB);
496 intel_uncore_write(uncore, GEN12_PAT_INDEX(7), GEN8_PPAT_WB);
497 }
498
499 static void cnl_setup_private_ppat(struct intel_uncore *uncore)
500 {
501 intel_uncore_write(uncore,
502 GEN10_PAT_INDEX(0),
503 GEN8_PPAT_WB | GEN8_PPAT_LLC);
504 intel_uncore_write(uncore,
505 GEN10_PAT_INDEX(1),
506 GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
507 intel_uncore_write(uncore,
508 GEN10_PAT_INDEX(2),
509 GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
510 intel_uncore_write(uncore,
511 GEN10_PAT_INDEX(3),
512 GEN8_PPAT_UC);
513 intel_uncore_write(uncore,
514 GEN10_PAT_INDEX(4),
515 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
516 intel_uncore_write(uncore,
517 GEN10_PAT_INDEX(5),
518 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
519 intel_uncore_write(uncore,
520 GEN10_PAT_INDEX(6),
521 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
522 intel_uncore_write(uncore,
523 GEN10_PAT_INDEX(7),
524 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
525 }
526
527 /*
528 * The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
529 * bits. When using advanced contexts each context stores its own PAT, but
530 * writing this data shouldn't be harmful even in those cases.
531 */
532 static void bdw_setup_private_ppat(struct intel_uncore *uncore)
533 {
534 u64 pat;
535
536 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
537 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
538 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
539 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
540 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
541 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
542 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
543 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
544
545 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
546 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
547 }
548
549 static void chv_setup_private_ppat(struct intel_uncore *uncore)
550 {
551 u64 pat;
552
553 /*
554 * Map WB on BDW to snooped on CHV.
555 *
556 * Only the snoop bit has meaning for CHV, the rest is
557 * ignored.
558 *
559 * The hardware will never snoop for certain types of accesses:
560 * - CPU GTT (GMADR->GGTT->no snoop->memory)
561 * - PPGTT page tables
562 * - some other special cycles
563 *
564 * As with BDW, we also need to consider the following for GT accesses:
565 * "For GGTT, there is NO pat_sel[2:0] from the entry,
566 * so RTL will always use the value corresponding to
567 * pat_sel = 000".
568 * Which means we must set the snoop bit in PAT entry 0
569 * in order to keep the global status page working.
570 */
571
572 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
573 GEN8_PPAT(1, 0) |
574 GEN8_PPAT(2, 0) |
575 GEN8_PPAT(3, 0) |
576 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
577 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
578 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
579 GEN8_PPAT(7, CHV_PPAT_SNOOP);
580
581 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
582 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
583 }
584
585 void setup_private_pat(struct intel_uncore *uncore)
586 {
587 struct drm_i915_private *i915 = uncore->i915;
588
589 GEM_BUG_ON(INTEL_GEN(i915) < 8);
590
591 if (INTEL_GEN(i915) >= 12)
592 tgl_setup_private_ppat(uncore);
593 else if (INTEL_GEN(i915) >= 10)
594 cnl_setup_private_ppat(uncore);
595 else if (IS_CHERRYVIEW(i915) || IS_GEN9_LP(i915))
596 chv_setup_private_ppat(uncore);
597 else
598 bdw_setup_private_ppat(uncore);
599 }
600
601 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
602 #include "selftests/mock_gtt.c"
603 #endif
604