]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blob - drivers/gpu/drm/i915/i915_gem_gtt.c
Merge tag 'drm-misc-next-2018-06-21' of git://anongit.freedesktop.org/drm/drm-misc...
[mirror_ubuntu-disco-kernel.git] / drivers / gpu / drm / i915 / i915_gem_gtt.c
1 /*
2 * Copyright © 2010 Daniel Vetter
3 * Copyright © 2011-2014 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 */
25
26 #include <linux/slab.h> /* fault-inject.h is not standalone! */
27
28 #include <linux/fault-inject.h>
29 #include <linux/log2.h>
30 #include <linux/random.h>
31 #include <linux/seq_file.h>
32 #include <linux/stop_machine.h>
33
34 #include <asm/set_memory.h>
35
36 #include <drm/drmP.h>
37 #include <drm/i915_drm.h>
38
39 #include "i915_drv.h"
40 #include "i915_vgpu.h"
41 #include "i915_trace.h"
42 #include "intel_drv.h"
43 #include "intel_frontbuffer.h"
44
45 #define I915_GFP_ALLOW_FAIL (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
46
47 /**
48 * DOC: Global GTT views
49 *
50 * Background and previous state
51 *
52 * Historically objects could exists (be bound) in global GTT space only as
53 * singular instances with a view representing all of the object's backing pages
54 * in a linear fashion. This view will be called a normal view.
55 *
56 * To support multiple views of the same object, where the number of mapped
57 * pages is not equal to the backing store, or where the layout of the pages
58 * is not linear, concept of a GGTT view was added.
59 *
60 * One example of an alternative view is a stereo display driven by a single
61 * image. In this case we would have a framebuffer looking like this
62 * (2x2 pages):
63 *
64 * 12
65 * 34
66 *
67 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
68 * rendering. In contrast, fed to the display engine would be an alternative
69 * view which could look something like this:
70 *
71 * 1212
72 * 3434
73 *
74 * In this example both the size and layout of pages in the alternative view is
75 * different from the normal view.
76 *
77 * Implementation and usage
78 *
79 * GGTT views are implemented using VMAs and are distinguished via enum
80 * i915_ggtt_view_type and struct i915_ggtt_view.
81 *
82 * A new flavour of core GEM functions which work with GGTT bound objects were
83 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
84 * renaming in large amounts of code. They take the struct i915_ggtt_view
85 * parameter encapsulating all metadata required to implement a view.
86 *
87 * As a helper for callers which are only interested in the normal view,
88 * globally const i915_ggtt_view_normal singleton instance exists. All old core
89 * GEM API functions, the ones not taking the view parameter, are operating on,
90 * or with the normal GGTT view.
91 *
92 * Code wanting to add or use a new GGTT view needs to:
93 *
94 * 1. Add a new enum with a suitable name.
95 * 2. Extend the metadata in the i915_ggtt_view structure if required.
96 * 3. Add support to i915_get_vma_pages().
97 *
98 * New views are required to build a scatter-gather table from within the
99 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
100 * exists for the lifetime of an VMA.
101 *
102 * Core API is designed to have copy semantics which means that passed in
103 * struct i915_ggtt_view does not need to be persistent (left around after
104 * calling the core API functions).
105 *
106 */
107
108 static int
109 i915_get_ggtt_vma_pages(struct i915_vma *vma);
110
111 static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
112 {
113 /*
114 * Note that as an uncached mmio write, this will flush the
115 * WCB of the writes into the GGTT before it triggers the invalidate.
116 */
117 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
118 }
119
120 static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
121 {
122 gen6_ggtt_invalidate(dev_priv);
123 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
124 }
125
126 static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
127 {
128 intel_gtt_chipset_flush();
129 }
130
131 static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
132 {
133 i915->ggtt.invalidate(i915);
134 }
135
136 int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
137 int enable_ppgtt)
138 {
139 bool has_full_ppgtt;
140 bool has_full_48bit_ppgtt;
141
142 if (!dev_priv->info.has_aliasing_ppgtt)
143 return 0;
144
145 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
146 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
147
148 if (intel_vgpu_active(dev_priv)) {
149 /* GVT-g has no support for 32bit ppgtt */
150 has_full_ppgtt = false;
151 has_full_48bit_ppgtt = intel_vgpu_has_full_48bit_ppgtt(dev_priv);
152 }
153
154 /*
155 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
156 * execlists, the sole mechanism available to submit work.
157 */
158 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
159 return 0;
160
161 if (enable_ppgtt == 1)
162 return 1;
163
164 if (enable_ppgtt == 2 && has_full_ppgtt)
165 return 2;
166
167 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
168 return 3;
169
170 /* Disable ppgtt on SNB if VT-d is on. */
171 if (IS_GEN6(dev_priv) && intel_vtd_active()) {
172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
173 return 0;
174 }
175
176 /* Early VLV doesn't have this */
177 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
178 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
179 return 0;
180 }
181
182 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv)) {
183 if (has_full_48bit_ppgtt)
184 return 3;
185
186 if (has_full_ppgtt)
187 return 2;
188 }
189
190 return 1;
191 }
192
193 static int gen6_ppgtt_bind_vma(struct i915_vma *vma,
194 enum i915_cache_level cache_level,
195 u32 unused)
196 {
197 u32 pte_flags;
198
199 /* Currently applicable only to VLV */
200 pte_flags = 0;
201 if (vma->obj->gt_ro)
202 pte_flags |= PTE_READ_ONLY;
203
204 vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
205
206 return 0;
207 }
208
209 static int gen8_ppgtt_bind_vma(struct i915_vma *vma,
210 enum i915_cache_level cache_level,
211 u32 unused)
212 {
213 int ret;
214
215 if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
216 ret = vma->vm->allocate_va_range(vma->vm,
217 vma->node.start, vma->size);
218 if (ret)
219 return ret;
220 }
221
222 return gen6_ppgtt_bind_vma(vma, cache_level, unused);
223 }
224
225 static void ppgtt_unbind_vma(struct i915_vma *vma)
226 {
227 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
228 }
229
230 static int ppgtt_set_pages(struct i915_vma *vma)
231 {
232 GEM_BUG_ON(vma->pages);
233
234 vma->pages = vma->obj->mm.pages;
235
236 vma->page_sizes = vma->obj->mm.page_sizes;
237
238 return 0;
239 }
240
241 static void clear_pages(struct i915_vma *vma)
242 {
243 GEM_BUG_ON(!vma->pages);
244
245 if (vma->pages != vma->obj->mm.pages) {
246 sg_free_table(vma->pages);
247 kfree(vma->pages);
248 }
249 vma->pages = NULL;
250
251 memset(&vma->page_sizes, 0, sizeof(vma->page_sizes));
252 }
253
254 static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
255 enum i915_cache_level level)
256 {
257 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
258 pte |= addr;
259
260 switch (level) {
261 case I915_CACHE_NONE:
262 pte |= PPAT_UNCACHED;
263 break;
264 case I915_CACHE_WT:
265 pte |= PPAT_DISPLAY_ELLC;
266 break;
267 default:
268 pte |= PPAT_CACHED;
269 break;
270 }
271
272 return pte;
273 }
274
275 static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
276 const enum i915_cache_level level)
277 {
278 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
279 pde |= addr;
280 if (level != I915_CACHE_NONE)
281 pde |= PPAT_CACHED_PDE;
282 else
283 pde |= PPAT_UNCACHED;
284 return pde;
285 }
286
287 #define gen8_pdpe_encode gen8_pde_encode
288 #define gen8_pml4e_encode gen8_pde_encode
289
290 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
291 enum i915_cache_level level,
292 u32 unused)
293 {
294 gen6_pte_t pte = GEN6_PTE_VALID;
295 pte |= GEN6_PTE_ADDR_ENCODE(addr);
296
297 switch (level) {
298 case I915_CACHE_L3_LLC:
299 case I915_CACHE_LLC:
300 pte |= GEN6_PTE_CACHE_LLC;
301 break;
302 case I915_CACHE_NONE:
303 pte |= GEN6_PTE_UNCACHED;
304 break;
305 default:
306 MISSING_CASE(level);
307 }
308
309 return pte;
310 }
311
312 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
313 enum i915_cache_level level,
314 u32 unused)
315 {
316 gen6_pte_t pte = GEN6_PTE_VALID;
317 pte |= GEN6_PTE_ADDR_ENCODE(addr);
318
319 switch (level) {
320 case I915_CACHE_L3_LLC:
321 pte |= GEN7_PTE_CACHE_L3_LLC;
322 break;
323 case I915_CACHE_LLC:
324 pte |= GEN6_PTE_CACHE_LLC;
325 break;
326 case I915_CACHE_NONE:
327 pte |= GEN6_PTE_UNCACHED;
328 break;
329 default:
330 MISSING_CASE(level);
331 }
332
333 return pte;
334 }
335
336 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
337 enum i915_cache_level level,
338 u32 flags)
339 {
340 gen6_pte_t pte = GEN6_PTE_VALID;
341 pte |= GEN6_PTE_ADDR_ENCODE(addr);
342
343 if (!(flags & PTE_READ_ONLY))
344 pte |= BYT_PTE_WRITEABLE;
345
346 if (level != I915_CACHE_NONE)
347 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
348
349 return pte;
350 }
351
352 static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
353 enum i915_cache_level level,
354 u32 unused)
355 {
356 gen6_pte_t pte = GEN6_PTE_VALID;
357 pte |= HSW_PTE_ADDR_ENCODE(addr);
358
359 if (level != I915_CACHE_NONE)
360 pte |= HSW_WB_LLC_AGE3;
361
362 return pte;
363 }
364
365 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
366 enum i915_cache_level level,
367 u32 unused)
368 {
369 gen6_pte_t pte = GEN6_PTE_VALID;
370 pte |= HSW_PTE_ADDR_ENCODE(addr);
371
372 switch (level) {
373 case I915_CACHE_NONE:
374 break;
375 case I915_CACHE_WT:
376 pte |= HSW_WT_ELLC_LLC_AGE3;
377 break;
378 default:
379 pte |= HSW_WB_ELLC_LLC_AGE3;
380 break;
381 }
382
383 return pte;
384 }
385
386 static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
387 {
388 struct pagevec *pvec = &vm->free_pages;
389 struct pagevec stash;
390
391 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
392 i915_gem_shrink_all(vm->i915);
393
394 if (likely(pvec->nr))
395 return pvec->pages[--pvec->nr];
396
397 if (!vm->pt_kmap_wc)
398 return alloc_page(gfp);
399
400 /* A placeholder for a specific mutex to guard the WC stash */
401 lockdep_assert_held(&vm->i915->drm.struct_mutex);
402
403 /* Look in our global stash of WC pages... */
404 pvec = &vm->i915->mm.wc_stash;
405 if (likely(pvec->nr))
406 return pvec->pages[--pvec->nr];
407
408 /*
409 * Otherwise batch allocate pages to amoritize cost of set_pages_wc.
410 *
411 * We have to be careful as page allocation may trigger the shrinker
412 * (via direct reclaim) which will fill up the WC stash underneath us.
413 * So we add our WB pages into a temporary pvec on the stack and merge
414 * them into the WC stash after all the allocations are complete.
415 */
416 pagevec_init(&stash);
417 do {
418 struct page *page;
419
420 page = alloc_page(gfp);
421 if (unlikely(!page))
422 break;
423
424 stash.pages[stash.nr++] = page;
425 } while (stash.nr < pagevec_space(pvec));
426
427 if (stash.nr) {
428 int nr = min_t(int, stash.nr, pagevec_space(pvec));
429 struct page **pages = stash.pages + stash.nr - nr;
430
431 if (nr && !set_pages_array_wc(pages, nr)) {
432 memcpy(pvec->pages + pvec->nr,
433 pages, sizeof(pages[0]) * nr);
434 pvec->nr += nr;
435 stash.nr -= nr;
436 }
437
438 pagevec_release(&stash);
439 }
440
441 return likely(pvec->nr) ? pvec->pages[--pvec->nr] : NULL;
442 }
443
444 static void vm_free_pages_release(struct i915_address_space *vm,
445 bool immediate)
446 {
447 struct pagevec *pvec = &vm->free_pages;
448
449 GEM_BUG_ON(!pagevec_count(pvec));
450
451 if (vm->pt_kmap_wc) {
452 struct pagevec *stash = &vm->i915->mm.wc_stash;
453
454 /* When we use WC, first fill up the global stash and then
455 * only if full immediately free the overflow.
456 */
457
458 lockdep_assert_held(&vm->i915->drm.struct_mutex);
459 if (pagevec_space(stash)) {
460 do {
461 stash->pages[stash->nr++] =
462 pvec->pages[--pvec->nr];
463 if (!pvec->nr)
464 return;
465 } while (pagevec_space(stash));
466
467 /* As we have made some room in the VM's free_pages,
468 * we can wait for it to fill again. Unless we are
469 * inside i915_address_space_fini() and must
470 * immediately release the pages!
471 */
472 if (!immediate)
473 return;
474 }
475
476 set_pages_array_wb(pvec->pages, pvec->nr);
477 }
478
479 __pagevec_release(pvec);
480 }
481
482 static void vm_free_page(struct i915_address_space *vm, struct page *page)
483 {
484 /*
485 * On !llc, we need to change the pages back to WB. We only do so
486 * in bulk, so we rarely need to change the page attributes here,
487 * but doing so requires a stop_machine() from deep inside arch/x86/mm.
488 * To make detection of the possible sleep more likely, use an
489 * unconditional might_sleep() for everybody.
490 */
491 might_sleep();
492 if (!pagevec_add(&vm->free_pages, page))
493 vm_free_pages_release(vm, false);
494 }
495
496 static int __setup_page_dma(struct i915_address_space *vm,
497 struct i915_page_dma *p,
498 gfp_t gfp)
499 {
500 p->page = vm_alloc_page(vm, gfp | I915_GFP_ALLOW_FAIL);
501 if (unlikely(!p->page))
502 return -ENOMEM;
503
504 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
505 PCI_DMA_BIDIRECTIONAL);
506 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
507 vm_free_page(vm, p->page);
508 return -ENOMEM;
509 }
510
511 return 0;
512 }
513
514 static int setup_page_dma(struct i915_address_space *vm,
515 struct i915_page_dma *p)
516 {
517 return __setup_page_dma(vm, p, __GFP_HIGHMEM);
518 }
519
520 static void cleanup_page_dma(struct i915_address_space *vm,
521 struct i915_page_dma *p)
522 {
523 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
524 vm_free_page(vm, p->page);
525 }
526
527 #define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
528
529 #define setup_px(vm, px) setup_page_dma((vm), px_base(px))
530 #define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
531 #define fill_px(vm, px, v) fill_page_dma((vm), px_base(px), (v))
532 #define fill32_px(vm, px, v) fill_page_dma_32((vm), px_base(px), (v))
533
534 static void fill_page_dma(struct i915_address_space *vm,
535 struct i915_page_dma *p,
536 const u64 val)
537 {
538 u64 * const vaddr = kmap_atomic(p->page);
539
540 memset64(vaddr, val, PAGE_SIZE / sizeof(val));
541
542 kunmap_atomic(vaddr);
543 }
544
545 static void fill_page_dma_32(struct i915_address_space *vm,
546 struct i915_page_dma *p,
547 const u32 v)
548 {
549 fill_page_dma(vm, p, (u64)v << 32 | v);
550 }
551
552 static int
553 setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
554 {
555 unsigned long size;
556
557 /*
558 * In order to utilize 64K pages for an object with a size < 2M, we will
559 * need to support a 64K scratch page, given that every 16th entry for a
560 * page-table operating in 64K mode must point to a properly aligned 64K
561 * region, including any PTEs which happen to point to scratch.
562 *
563 * This is only relevant for the 48b PPGTT where we support
564 * huge-gtt-pages, see also i915_vma_insert().
565 *
566 * TODO: we should really consider write-protecting the scratch-page and
567 * sharing between ppgtt
568 */
569 size = I915_GTT_PAGE_SIZE_4K;
570 if (i915_vm_is_48bit(vm) &&
571 HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K)) {
572 size = I915_GTT_PAGE_SIZE_64K;
573 gfp |= __GFP_NOWARN;
574 }
575 gfp |= __GFP_ZERO | __GFP_RETRY_MAYFAIL;
576
577 do {
578 int order = get_order(size);
579 struct page *page;
580 dma_addr_t addr;
581
582 page = alloc_pages(gfp, order);
583 if (unlikely(!page))
584 goto skip;
585
586 addr = dma_map_page(vm->dma, page, 0, size,
587 PCI_DMA_BIDIRECTIONAL);
588 if (unlikely(dma_mapping_error(vm->dma, addr)))
589 goto free_page;
590
591 if (unlikely(!IS_ALIGNED(addr, size)))
592 goto unmap_page;
593
594 vm->scratch_page.page = page;
595 vm->scratch_page.daddr = addr;
596 vm->scratch_page.order = order;
597 return 0;
598
599 unmap_page:
600 dma_unmap_page(vm->dma, addr, size, PCI_DMA_BIDIRECTIONAL);
601 free_page:
602 __free_pages(page, order);
603 skip:
604 if (size == I915_GTT_PAGE_SIZE_4K)
605 return -ENOMEM;
606
607 size = I915_GTT_PAGE_SIZE_4K;
608 gfp &= ~__GFP_NOWARN;
609 } while (1);
610 }
611
612 static void cleanup_scratch_page(struct i915_address_space *vm)
613 {
614 struct i915_page_dma *p = &vm->scratch_page;
615
616 dma_unmap_page(vm->dma, p->daddr, BIT(p->order) << PAGE_SHIFT,
617 PCI_DMA_BIDIRECTIONAL);
618 __free_pages(p->page, p->order);
619 }
620
621 static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
622 {
623 struct i915_page_table *pt;
624
625 pt = kmalloc(sizeof(*pt), I915_GFP_ALLOW_FAIL);
626 if (unlikely(!pt))
627 return ERR_PTR(-ENOMEM);
628
629 if (unlikely(setup_px(vm, pt))) {
630 kfree(pt);
631 return ERR_PTR(-ENOMEM);
632 }
633
634 pt->used_ptes = 0;
635 return pt;
636 }
637
638 static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
639 {
640 cleanup_px(vm, pt);
641 kfree(pt);
642 }
643
644 static void gen8_initialize_pt(struct i915_address_space *vm,
645 struct i915_page_table *pt)
646 {
647 fill_px(vm, pt,
648 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC));
649 }
650
651 static void gen6_initialize_pt(struct i915_address_space *vm,
652 struct i915_page_table *pt)
653 {
654 fill32_px(vm, pt,
655 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0));
656 }
657
658 static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
659 {
660 struct i915_page_directory *pd;
661
662 pd = kzalloc(sizeof(*pd), I915_GFP_ALLOW_FAIL);
663 if (unlikely(!pd))
664 return ERR_PTR(-ENOMEM);
665
666 if (unlikely(setup_px(vm, pd))) {
667 kfree(pd);
668 return ERR_PTR(-ENOMEM);
669 }
670
671 pd->used_pdes = 0;
672 return pd;
673 }
674
675 static void free_pd(struct i915_address_space *vm,
676 struct i915_page_directory *pd)
677 {
678 cleanup_px(vm, pd);
679 kfree(pd);
680 }
681
682 static void gen8_initialize_pd(struct i915_address_space *vm,
683 struct i915_page_directory *pd)
684 {
685 fill_px(vm, pd,
686 gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC));
687 memset_p((void **)pd->page_table, vm->scratch_pt, I915_PDES);
688 }
689
690 static int __pdp_init(struct i915_address_space *vm,
691 struct i915_page_directory_pointer *pdp)
692 {
693 const unsigned int pdpes = i915_pdpes_per_pdp(vm);
694
695 pdp->page_directory = kmalloc_array(pdpes, sizeof(*pdp->page_directory),
696 I915_GFP_ALLOW_FAIL);
697 if (unlikely(!pdp->page_directory))
698 return -ENOMEM;
699
700 memset_p((void **)pdp->page_directory, vm->scratch_pd, pdpes);
701
702 return 0;
703 }
704
705 static void __pdp_fini(struct i915_page_directory_pointer *pdp)
706 {
707 kfree(pdp->page_directory);
708 pdp->page_directory = NULL;
709 }
710
711 static inline bool use_4lvl(const struct i915_address_space *vm)
712 {
713 return i915_vm_is_48bit(vm);
714 }
715
716 static struct i915_page_directory_pointer *
717 alloc_pdp(struct i915_address_space *vm)
718 {
719 struct i915_page_directory_pointer *pdp;
720 int ret = -ENOMEM;
721
722 GEM_BUG_ON(!use_4lvl(vm));
723
724 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
725 if (!pdp)
726 return ERR_PTR(-ENOMEM);
727
728 ret = __pdp_init(vm, pdp);
729 if (ret)
730 goto fail_bitmap;
731
732 ret = setup_px(vm, pdp);
733 if (ret)
734 goto fail_page_m;
735
736 return pdp;
737
738 fail_page_m:
739 __pdp_fini(pdp);
740 fail_bitmap:
741 kfree(pdp);
742
743 return ERR_PTR(ret);
744 }
745
746 static void free_pdp(struct i915_address_space *vm,
747 struct i915_page_directory_pointer *pdp)
748 {
749 __pdp_fini(pdp);
750
751 if (!use_4lvl(vm))
752 return;
753
754 cleanup_px(vm, pdp);
755 kfree(pdp);
756 }
757
758 static void gen8_initialize_pdp(struct i915_address_space *vm,
759 struct i915_page_directory_pointer *pdp)
760 {
761 gen8_ppgtt_pdpe_t scratch_pdpe;
762
763 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
764
765 fill_px(vm, pdp, scratch_pdpe);
766 }
767
768 static void gen8_initialize_pml4(struct i915_address_space *vm,
769 struct i915_pml4 *pml4)
770 {
771 fill_px(vm, pml4,
772 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC));
773 memset_p((void **)pml4->pdps, vm->scratch_pdp, GEN8_PML4ES_PER_PML4);
774 }
775
776 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
777 * the page table structures, we mark them dirty so that
778 * context switching/execlist queuing code takes extra steps
779 * to ensure that tlbs are flushed.
780 */
781 static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
782 {
783 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->vm.i915)->ring_mask;
784 }
785
786 /* Removes entries from a single page table, releasing it if it's empty.
787 * Caller can use the return value to update higher-level entries.
788 */
789 static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
790 struct i915_page_table *pt,
791 u64 start, u64 length)
792 {
793 unsigned int num_entries = gen8_pte_count(start, length);
794 unsigned int pte = gen8_pte_index(start);
795 unsigned int pte_end = pte + num_entries;
796 const gen8_pte_t scratch_pte =
797 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
798 gen8_pte_t *vaddr;
799
800 GEM_BUG_ON(num_entries > pt->used_ptes);
801
802 pt->used_ptes -= num_entries;
803 if (!pt->used_ptes)
804 return true;
805
806 vaddr = kmap_atomic_px(pt);
807 while (pte < pte_end)
808 vaddr[pte++] = scratch_pte;
809 kunmap_atomic(vaddr);
810
811 return false;
812 }
813
814 static void gen8_ppgtt_set_pde(struct i915_address_space *vm,
815 struct i915_page_directory *pd,
816 struct i915_page_table *pt,
817 unsigned int pde)
818 {
819 gen8_pde_t *vaddr;
820
821 pd->page_table[pde] = pt;
822
823 vaddr = kmap_atomic_px(pd);
824 vaddr[pde] = gen8_pde_encode(px_dma(pt), I915_CACHE_LLC);
825 kunmap_atomic(vaddr);
826 }
827
828 static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
829 struct i915_page_directory *pd,
830 u64 start, u64 length)
831 {
832 struct i915_page_table *pt;
833 u32 pde;
834
835 gen8_for_each_pde(pt, pd, start, length, pde) {
836 GEM_BUG_ON(pt == vm->scratch_pt);
837
838 if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
839 continue;
840
841 gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
842 GEM_BUG_ON(!pd->used_pdes);
843 pd->used_pdes--;
844
845 free_pt(vm, pt);
846 }
847
848 return !pd->used_pdes;
849 }
850
851 static void gen8_ppgtt_set_pdpe(struct i915_address_space *vm,
852 struct i915_page_directory_pointer *pdp,
853 struct i915_page_directory *pd,
854 unsigned int pdpe)
855 {
856 gen8_ppgtt_pdpe_t *vaddr;
857
858 pdp->page_directory[pdpe] = pd;
859 if (!use_4lvl(vm))
860 return;
861
862 vaddr = kmap_atomic_px(pdp);
863 vaddr[pdpe] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
864 kunmap_atomic(vaddr);
865 }
866
867 /* Removes entries from a single page dir pointer, releasing it if it's empty.
868 * Caller can use the return value to update higher-level entries
869 */
870 static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
871 struct i915_page_directory_pointer *pdp,
872 u64 start, u64 length)
873 {
874 struct i915_page_directory *pd;
875 unsigned int pdpe;
876
877 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
878 GEM_BUG_ON(pd == vm->scratch_pd);
879
880 if (!gen8_ppgtt_clear_pd(vm, pd, start, length))
881 continue;
882
883 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
884 GEM_BUG_ON(!pdp->used_pdpes);
885 pdp->used_pdpes--;
886
887 free_pd(vm, pd);
888 }
889
890 return !pdp->used_pdpes;
891 }
892
893 static void gen8_ppgtt_clear_3lvl(struct i915_address_space *vm,
894 u64 start, u64 length)
895 {
896 gen8_ppgtt_clear_pdp(vm, &i915_vm_to_ppgtt(vm)->pdp, start, length);
897 }
898
899 static void gen8_ppgtt_set_pml4e(struct i915_pml4 *pml4,
900 struct i915_page_directory_pointer *pdp,
901 unsigned int pml4e)
902 {
903 gen8_ppgtt_pml4e_t *vaddr;
904
905 pml4->pdps[pml4e] = pdp;
906
907 vaddr = kmap_atomic_px(pml4);
908 vaddr[pml4e] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
909 kunmap_atomic(vaddr);
910 }
911
912 /* Removes entries from a single pml4.
913 * This is the top-level structure in 4-level page tables used on gen8+.
914 * Empty entries are always scratch pml4e.
915 */
916 static void gen8_ppgtt_clear_4lvl(struct i915_address_space *vm,
917 u64 start, u64 length)
918 {
919 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
920 struct i915_pml4 *pml4 = &ppgtt->pml4;
921 struct i915_page_directory_pointer *pdp;
922 unsigned int pml4e;
923
924 GEM_BUG_ON(!use_4lvl(vm));
925
926 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
927 GEM_BUG_ON(pdp == vm->scratch_pdp);
928
929 if (!gen8_ppgtt_clear_pdp(vm, pdp, start, length))
930 continue;
931
932 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
933
934 free_pdp(vm, pdp);
935 }
936 }
937
938 static inline struct sgt_dma {
939 struct scatterlist *sg;
940 dma_addr_t dma, max;
941 } sgt_dma(struct i915_vma *vma) {
942 struct scatterlist *sg = vma->pages->sgl;
943 dma_addr_t addr = sg_dma_address(sg);
944 return (struct sgt_dma) { sg, addr, addr + sg->length };
945 }
946
947 struct gen8_insert_pte {
948 u16 pml4e;
949 u16 pdpe;
950 u16 pde;
951 u16 pte;
952 };
953
954 static __always_inline struct gen8_insert_pte gen8_insert_pte(u64 start)
955 {
956 return (struct gen8_insert_pte) {
957 gen8_pml4e_index(start),
958 gen8_pdpe_index(start),
959 gen8_pde_index(start),
960 gen8_pte_index(start),
961 };
962 }
963
964 static __always_inline bool
965 gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
966 struct i915_page_directory_pointer *pdp,
967 struct sgt_dma *iter,
968 struct gen8_insert_pte *idx,
969 enum i915_cache_level cache_level)
970 {
971 struct i915_page_directory *pd;
972 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
973 gen8_pte_t *vaddr;
974 bool ret;
975
976 GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->vm));
977 pd = pdp->page_directory[idx->pdpe];
978 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
979 do {
980 vaddr[idx->pte] = pte_encode | iter->dma;
981
982 iter->dma += PAGE_SIZE;
983 if (iter->dma >= iter->max) {
984 iter->sg = __sg_next(iter->sg);
985 if (!iter->sg) {
986 ret = false;
987 break;
988 }
989
990 iter->dma = sg_dma_address(iter->sg);
991 iter->max = iter->dma + iter->sg->length;
992 }
993
994 if (++idx->pte == GEN8_PTES) {
995 idx->pte = 0;
996
997 if (++idx->pde == I915_PDES) {
998 idx->pde = 0;
999
1000 /* Limited by sg length for 3lvl */
1001 if (++idx->pdpe == GEN8_PML4ES_PER_PML4) {
1002 idx->pdpe = 0;
1003 ret = true;
1004 break;
1005 }
1006
1007 GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->vm));
1008 pd = pdp->page_directory[idx->pdpe];
1009 }
1010
1011 kunmap_atomic(vaddr);
1012 vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
1013 }
1014 } while (1);
1015 kunmap_atomic(vaddr);
1016
1017 return ret;
1018 }
1019
1020 static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
1021 struct i915_vma *vma,
1022 enum i915_cache_level cache_level,
1023 u32 unused)
1024 {
1025 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1026 struct sgt_dma iter = sgt_dma(vma);
1027 struct gen8_insert_pte idx = gen8_insert_pte(vma->node.start);
1028
1029 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter, &idx,
1030 cache_level);
1031
1032 vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1033 }
1034
1035 static void gen8_ppgtt_insert_huge_entries(struct i915_vma *vma,
1036 struct i915_page_directory_pointer **pdps,
1037 struct sgt_dma *iter,
1038 enum i915_cache_level cache_level)
1039 {
1040 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
1041 u64 start = vma->node.start;
1042 dma_addr_t rem = iter->sg->length;
1043
1044 do {
1045 struct gen8_insert_pte idx = gen8_insert_pte(start);
1046 struct i915_page_directory_pointer *pdp = pdps[idx.pml4e];
1047 struct i915_page_directory *pd = pdp->page_directory[idx.pdpe];
1048 unsigned int page_size;
1049 bool maybe_64K = false;
1050 gen8_pte_t encode = pte_encode;
1051 gen8_pte_t *vaddr;
1052 u16 index, max;
1053
1054 if (vma->page_sizes.sg & I915_GTT_PAGE_SIZE_2M &&
1055 IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_2M) &&
1056 rem >= I915_GTT_PAGE_SIZE_2M && !idx.pte) {
1057 index = idx.pde;
1058 max = I915_PDES;
1059 page_size = I915_GTT_PAGE_SIZE_2M;
1060
1061 encode |= GEN8_PDE_PS_2M;
1062
1063 vaddr = kmap_atomic_px(pd);
1064 } else {
1065 struct i915_page_table *pt = pd->page_table[idx.pde];
1066
1067 index = idx.pte;
1068 max = GEN8_PTES;
1069 page_size = I915_GTT_PAGE_SIZE;
1070
1071 if (!index &&
1072 vma->page_sizes.sg & I915_GTT_PAGE_SIZE_64K &&
1073 IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_64K) &&
1074 (IS_ALIGNED(rem, I915_GTT_PAGE_SIZE_64K) ||
1075 rem >= (max - index) << PAGE_SHIFT))
1076 maybe_64K = true;
1077
1078 vaddr = kmap_atomic_px(pt);
1079 }
1080
1081 do {
1082 GEM_BUG_ON(iter->sg->length < page_size);
1083 vaddr[index++] = encode | iter->dma;
1084
1085 start += page_size;
1086 iter->dma += page_size;
1087 rem -= page_size;
1088 if (iter->dma >= iter->max) {
1089 iter->sg = __sg_next(iter->sg);
1090 if (!iter->sg)
1091 break;
1092
1093 rem = iter->sg->length;
1094 iter->dma = sg_dma_address(iter->sg);
1095 iter->max = iter->dma + rem;
1096
1097 if (maybe_64K && index < max &&
1098 !(IS_ALIGNED(iter->dma, I915_GTT_PAGE_SIZE_64K) &&
1099 (IS_ALIGNED(rem, I915_GTT_PAGE_SIZE_64K) ||
1100 rem >= (max - index) << PAGE_SHIFT)))
1101 maybe_64K = false;
1102
1103 if (unlikely(!IS_ALIGNED(iter->dma, page_size)))
1104 break;
1105 }
1106 } while (rem >= page_size && index < max);
1107
1108 kunmap_atomic(vaddr);
1109
1110 /*
1111 * Is it safe to mark the 2M block as 64K? -- Either we have
1112 * filled whole page-table with 64K entries, or filled part of
1113 * it and have reached the end of the sg table and we have
1114 * enough padding.
1115 */
1116 if (maybe_64K &&
1117 (index == max ||
1118 (i915_vm_has_scratch_64K(vma->vm) &&
1119 !iter->sg && IS_ALIGNED(vma->node.start +
1120 vma->node.size,
1121 I915_GTT_PAGE_SIZE_2M)))) {
1122 vaddr = kmap_atomic_px(pd);
1123 vaddr[idx.pde] |= GEN8_PDE_IPS_64K;
1124 kunmap_atomic(vaddr);
1125 page_size = I915_GTT_PAGE_SIZE_64K;
1126
1127 /*
1128 * We write all 4K page entries, even when using 64K
1129 * pages. In order to verify that the HW isn't cheating
1130 * by using the 4K PTE instead of the 64K PTE, we want
1131 * to remove all the surplus entries. If the HW skipped
1132 * the 64K PTE, it will read/write into the scratch page
1133 * instead - which we detect as missing results during
1134 * selftests.
1135 */
1136 if (I915_SELFTEST_ONLY(vma->vm->scrub_64K)) {
1137 u16 i;
1138
1139 encode = pte_encode | vma->vm->scratch_page.daddr;
1140 vaddr = kmap_atomic_px(pd->page_table[idx.pde]);
1141
1142 for (i = 1; i < index; i += 16)
1143 memset64(vaddr + i, encode, 15);
1144
1145 kunmap_atomic(vaddr);
1146 }
1147 }
1148
1149 vma->page_sizes.gtt |= page_size;
1150 } while (iter->sg);
1151 }
1152
1153 static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
1154 struct i915_vma *vma,
1155 enum i915_cache_level cache_level,
1156 u32 unused)
1157 {
1158 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1159 struct sgt_dma iter = sgt_dma(vma);
1160 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
1161
1162 if (vma->page_sizes.sg > I915_GTT_PAGE_SIZE) {
1163 gen8_ppgtt_insert_huge_entries(vma, pdps, &iter, cache_level);
1164 } else {
1165 struct gen8_insert_pte idx = gen8_insert_pte(vma->node.start);
1166
1167 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[idx.pml4e++],
1168 &iter, &idx, cache_level))
1169 GEM_BUG_ON(idx.pml4e >= GEN8_PML4ES_PER_PML4);
1170
1171 vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1172 }
1173 }
1174
1175 static void gen8_free_page_tables(struct i915_address_space *vm,
1176 struct i915_page_directory *pd)
1177 {
1178 int i;
1179
1180 if (!px_page(pd))
1181 return;
1182
1183 for (i = 0; i < I915_PDES; i++) {
1184 if (pd->page_table[i] != vm->scratch_pt)
1185 free_pt(vm, pd->page_table[i]);
1186 }
1187 }
1188
1189 static int gen8_init_scratch(struct i915_address_space *vm)
1190 {
1191 int ret;
1192
1193 ret = setup_scratch_page(vm, __GFP_HIGHMEM);
1194 if (ret)
1195 return ret;
1196
1197 vm->scratch_pt = alloc_pt(vm);
1198 if (IS_ERR(vm->scratch_pt)) {
1199 ret = PTR_ERR(vm->scratch_pt);
1200 goto free_scratch_page;
1201 }
1202
1203 vm->scratch_pd = alloc_pd(vm);
1204 if (IS_ERR(vm->scratch_pd)) {
1205 ret = PTR_ERR(vm->scratch_pd);
1206 goto free_pt;
1207 }
1208
1209 if (use_4lvl(vm)) {
1210 vm->scratch_pdp = alloc_pdp(vm);
1211 if (IS_ERR(vm->scratch_pdp)) {
1212 ret = PTR_ERR(vm->scratch_pdp);
1213 goto free_pd;
1214 }
1215 }
1216
1217 gen8_initialize_pt(vm, vm->scratch_pt);
1218 gen8_initialize_pd(vm, vm->scratch_pd);
1219 if (use_4lvl(vm))
1220 gen8_initialize_pdp(vm, vm->scratch_pdp);
1221
1222 return 0;
1223
1224 free_pd:
1225 free_pd(vm, vm->scratch_pd);
1226 free_pt:
1227 free_pt(vm, vm->scratch_pt);
1228 free_scratch_page:
1229 cleanup_scratch_page(vm);
1230
1231 return ret;
1232 }
1233
1234 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1235 {
1236 struct i915_address_space *vm = &ppgtt->vm;
1237 struct drm_i915_private *dev_priv = vm->i915;
1238 enum vgt_g2v_type msg;
1239 int i;
1240
1241 if (use_4lvl(vm)) {
1242 const u64 daddr = px_dma(&ppgtt->pml4);
1243
1244 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1245 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
1246
1247 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1248 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1249 } else {
1250 for (i = 0; i < GEN8_3LVL_PDPES; i++) {
1251 const u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1252
1253 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1254 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
1255 }
1256
1257 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1258 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1259 }
1260
1261 I915_WRITE(vgtif_reg(g2v_notify), msg);
1262
1263 return 0;
1264 }
1265
1266 static void gen8_free_scratch(struct i915_address_space *vm)
1267 {
1268 if (use_4lvl(vm))
1269 free_pdp(vm, vm->scratch_pdp);
1270 free_pd(vm, vm->scratch_pd);
1271 free_pt(vm, vm->scratch_pt);
1272 cleanup_scratch_page(vm);
1273 }
1274
1275 static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
1276 struct i915_page_directory_pointer *pdp)
1277 {
1278 const unsigned int pdpes = i915_pdpes_per_pdp(vm);
1279 int i;
1280
1281 for (i = 0; i < pdpes; i++) {
1282 if (pdp->page_directory[i] == vm->scratch_pd)
1283 continue;
1284
1285 gen8_free_page_tables(vm, pdp->page_directory[i]);
1286 free_pd(vm, pdp->page_directory[i]);
1287 }
1288
1289 free_pdp(vm, pdp);
1290 }
1291
1292 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1293 {
1294 int i;
1295
1296 for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
1297 if (ppgtt->pml4.pdps[i] == ppgtt->vm.scratch_pdp)
1298 continue;
1299
1300 gen8_ppgtt_cleanup_3lvl(&ppgtt->vm, ppgtt->pml4.pdps[i]);
1301 }
1302
1303 cleanup_px(&ppgtt->vm, &ppgtt->pml4);
1304 }
1305
1306 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1307 {
1308 struct drm_i915_private *dev_priv = vm->i915;
1309 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1310
1311 if (intel_vgpu_active(dev_priv))
1312 gen8_ppgtt_notify_vgt(ppgtt, false);
1313
1314 if (use_4lvl(vm))
1315 gen8_ppgtt_cleanup_4lvl(ppgtt);
1316 else
1317 gen8_ppgtt_cleanup_3lvl(&ppgtt->vm, &ppgtt->pdp);
1318
1319 gen8_free_scratch(vm);
1320 }
1321
1322 static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
1323 struct i915_page_directory *pd,
1324 u64 start, u64 length)
1325 {
1326 struct i915_page_table *pt;
1327 u64 from = start;
1328 unsigned int pde;
1329
1330 gen8_for_each_pde(pt, pd, start, length, pde) {
1331 int count = gen8_pte_count(start, length);
1332
1333 if (pt == vm->scratch_pt) {
1334 pd->used_pdes++;
1335
1336 pt = alloc_pt(vm);
1337 if (IS_ERR(pt)) {
1338 pd->used_pdes--;
1339 goto unwind;
1340 }
1341
1342 if (count < GEN8_PTES || intel_vgpu_active(vm->i915))
1343 gen8_initialize_pt(vm, pt);
1344
1345 gen8_ppgtt_set_pde(vm, pd, pt, pde);
1346 GEM_BUG_ON(pd->used_pdes > I915_PDES);
1347 }
1348
1349 pt->used_ptes += count;
1350 }
1351 return 0;
1352
1353 unwind:
1354 gen8_ppgtt_clear_pd(vm, pd, from, start - from);
1355 return -ENOMEM;
1356 }
1357
1358 static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
1359 struct i915_page_directory_pointer *pdp,
1360 u64 start, u64 length)
1361 {
1362 struct i915_page_directory *pd;
1363 u64 from = start;
1364 unsigned int pdpe;
1365 int ret;
1366
1367 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1368 if (pd == vm->scratch_pd) {
1369 pdp->used_pdpes++;
1370
1371 pd = alloc_pd(vm);
1372 if (IS_ERR(pd)) {
1373 pdp->used_pdpes--;
1374 goto unwind;
1375 }
1376
1377 gen8_initialize_pd(vm, pd);
1378 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1379 GEM_BUG_ON(pdp->used_pdpes > i915_pdpes_per_pdp(vm));
1380
1381 mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
1382 }
1383
1384 ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
1385 if (unlikely(ret))
1386 goto unwind_pd;
1387 }
1388
1389 return 0;
1390
1391 unwind_pd:
1392 if (!pd->used_pdes) {
1393 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1394 GEM_BUG_ON(!pdp->used_pdpes);
1395 pdp->used_pdpes--;
1396 free_pd(vm, pd);
1397 }
1398 unwind:
1399 gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
1400 return -ENOMEM;
1401 }
1402
1403 static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
1404 u64 start, u64 length)
1405 {
1406 return gen8_ppgtt_alloc_pdp(vm,
1407 &i915_vm_to_ppgtt(vm)->pdp, start, length);
1408 }
1409
1410 static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
1411 u64 start, u64 length)
1412 {
1413 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1414 struct i915_pml4 *pml4 = &ppgtt->pml4;
1415 struct i915_page_directory_pointer *pdp;
1416 u64 from = start;
1417 u32 pml4e;
1418 int ret;
1419
1420 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1421 if (pml4->pdps[pml4e] == vm->scratch_pdp) {
1422 pdp = alloc_pdp(vm);
1423 if (IS_ERR(pdp))
1424 goto unwind;
1425
1426 gen8_initialize_pdp(vm, pdp);
1427 gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
1428 }
1429
1430 ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
1431 if (unlikely(ret))
1432 goto unwind_pdp;
1433 }
1434
1435 return 0;
1436
1437 unwind_pdp:
1438 if (!pdp->used_pdpes) {
1439 gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
1440 free_pdp(vm, pdp);
1441 }
1442 unwind:
1443 gen8_ppgtt_clear_4lvl(vm, from, start - from);
1444 return -ENOMEM;
1445 }
1446
1447 static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1448 struct i915_page_directory_pointer *pdp,
1449 u64 start, u64 length,
1450 gen8_pte_t scratch_pte,
1451 struct seq_file *m)
1452 {
1453 struct i915_address_space *vm = &ppgtt->vm;
1454 struct i915_page_directory *pd;
1455 u32 pdpe;
1456
1457 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1458 struct i915_page_table *pt;
1459 u64 pd_len = length;
1460 u64 pd_start = start;
1461 u32 pde;
1462
1463 if (pdp->page_directory[pdpe] == ppgtt->vm.scratch_pd)
1464 continue;
1465
1466 seq_printf(m, "\tPDPE #%d\n", pdpe);
1467 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1468 u32 pte;
1469 gen8_pte_t *pt_vaddr;
1470
1471 if (pd->page_table[pde] == ppgtt->vm.scratch_pt)
1472 continue;
1473
1474 pt_vaddr = kmap_atomic_px(pt);
1475 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1476 u64 va = (pdpe << GEN8_PDPE_SHIFT |
1477 pde << GEN8_PDE_SHIFT |
1478 pte << GEN8_PTE_SHIFT);
1479 int i;
1480 bool found = false;
1481
1482 for (i = 0; i < 4; i++)
1483 if (pt_vaddr[pte + i] != scratch_pte)
1484 found = true;
1485 if (!found)
1486 continue;
1487
1488 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1489 for (i = 0; i < 4; i++) {
1490 if (pt_vaddr[pte + i] != scratch_pte)
1491 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1492 else
1493 seq_puts(m, " SCRATCH ");
1494 }
1495 seq_puts(m, "\n");
1496 }
1497 kunmap_atomic(pt_vaddr);
1498 }
1499 }
1500 }
1501
1502 static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1503 {
1504 struct i915_address_space *vm = &ppgtt->vm;
1505 const gen8_pte_t scratch_pte =
1506 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
1507 u64 start = 0, length = ppgtt->vm.total;
1508
1509 if (use_4lvl(vm)) {
1510 u64 pml4e;
1511 struct i915_pml4 *pml4 = &ppgtt->pml4;
1512 struct i915_page_directory_pointer *pdp;
1513
1514 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1515 if (pml4->pdps[pml4e] == ppgtt->vm.scratch_pdp)
1516 continue;
1517
1518 seq_printf(m, " PML4E #%llu\n", pml4e);
1519 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
1520 }
1521 } else {
1522 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
1523 }
1524 }
1525
1526 static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
1527 {
1528 struct i915_address_space *vm = &ppgtt->vm;
1529 struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
1530 struct i915_page_directory *pd;
1531 u64 start = 0, length = ppgtt->vm.total;
1532 u64 from = start;
1533 unsigned int pdpe;
1534
1535 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1536 pd = alloc_pd(vm);
1537 if (IS_ERR(pd))
1538 goto unwind;
1539
1540 gen8_initialize_pd(vm, pd);
1541 gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
1542 pdp->used_pdpes++;
1543 }
1544
1545 pdp->used_pdpes++; /* never remove */
1546 return 0;
1547
1548 unwind:
1549 start -= from;
1550 gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
1551 gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
1552 free_pd(vm, pd);
1553 }
1554 pdp->used_pdpes = 0;
1555 return -ENOMEM;
1556 }
1557
1558 /*
1559 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1560 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1561 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1562 * space.
1563 *
1564 */
1565 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1566 {
1567 struct i915_address_space *vm = &ppgtt->vm;
1568 struct drm_i915_private *dev_priv = vm->i915;
1569 int ret;
1570
1571 ppgtt->vm.total = USES_FULL_48BIT_PPGTT(dev_priv) ?
1572 1ULL << 48 :
1573 1ULL << 32;
1574
1575 /* There are only few exceptions for gen >=6. chv and bxt.
1576 * And we are not sure about the latter so play safe for now.
1577 */
1578 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1579 ppgtt->vm.pt_kmap_wc = true;
1580
1581 ret = gen8_init_scratch(&ppgtt->vm);
1582 if (ret) {
1583 ppgtt->vm.total = 0;
1584 return ret;
1585 }
1586
1587 if (use_4lvl(vm)) {
1588 ret = setup_px(&ppgtt->vm, &ppgtt->pml4);
1589 if (ret)
1590 goto free_scratch;
1591
1592 gen8_initialize_pml4(&ppgtt->vm, &ppgtt->pml4);
1593
1594 ppgtt->vm.allocate_va_range = gen8_ppgtt_alloc_4lvl;
1595 ppgtt->vm.insert_entries = gen8_ppgtt_insert_4lvl;
1596 ppgtt->vm.clear_range = gen8_ppgtt_clear_4lvl;
1597 } else {
1598 ret = __pdp_init(&ppgtt->vm, &ppgtt->pdp);
1599 if (ret)
1600 goto free_scratch;
1601
1602 if (intel_vgpu_active(dev_priv)) {
1603 ret = gen8_preallocate_top_level_pdp(ppgtt);
1604 if (ret) {
1605 __pdp_fini(&ppgtt->pdp);
1606 goto free_scratch;
1607 }
1608 }
1609
1610 ppgtt->vm.allocate_va_range = gen8_ppgtt_alloc_3lvl;
1611 ppgtt->vm.insert_entries = gen8_ppgtt_insert_3lvl;
1612 ppgtt->vm.clear_range = gen8_ppgtt_clear_3lvl;
1613 }
1614
1615 if (intel_vgpu_active(dev_priv))
1616 gen8_ppgtt_notify_vgt(ppgtt, true);
1617
1618 ppgtt->vm.cleanup = gen8_ppgtt_cleanup;
1619 ppgtt->vm.bind_vma = gen8_ppgtt_bind_vma;
1620 ppgtt->vm.unbind_vma = ppgtt_unbind_vma;
1621 ppgtt->vm.set_pages = ppgtt_set_pages;
1622 ppgtt->vm.clear_pages = clear_pages;
1623 ppgtt->debug_dump = gen8_dump_ppgtt;
1624
1625 return 0;
1626
1627 free_scratch:
1628 gen8_free_scratch(&ppgtt->vm);
1629 return ret;
1630 }
1631
1632 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1633 {
1634 struct i915_address_space *vm = &ppgtt->vm;
1635 struct i915_page_table *unused;
1636 gen6_pte_t scratch_pte;
1637 u32 pd_entry, pte, pde;
1638 u32 start = 0, length = ppgtt->vm.total;
1639
1640 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
1641 I915_CACHE_LLC, 0);
1642
1643 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
1644 u32 expected;
1645 gen6_pte_t *pt_vaddr;
1646 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
1647 pd_entry = readl(ppgtt->pd_addr + pde);
1648 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1649
1650 if (pd_entry != expected)
1651 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1652 pde,
1653 pd_entry,
1654 expected);
1655 seq_printf(m, "\tPDE: %x\n", pd_entry);
1656
1657 pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
1658
1659 for (pte = 0; pte < GEN6_PTES; pte+=4) {
1660 unsigned long va =
1661 (pde * PAGE_SIZE * GEN6_PTES) +
1662 (pte * PAGE_SIZE);
1663 int i;
1664 bool found = false;
1665 for (i = 0; i < 4; i++)
1666 if (pt_vaddr[pte + i] != scratch_pte)
1667 found = true;
1668 if (!found)
1669 continue;
1670
1671 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1672 for (i = 0; i < 4; i++) {
1673 if (pt_vaddr[pte + i] != scratch_pte)
1674 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1675 else
1676 seq_puts(m, " SCRATCH ");
1677 }
1678 seq_puts(m, "\n");
1679 }
1680 kunmap_atomic(pt_vaddr);
1681 }
1682 }
1683
1684 /* Write pde (index) from the page directory @pd to the page table @pt */
1685 static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
1686 const unsigned int pde,
1687 const struct i915_page_table *pt)
1688 {
1689 /* Caller needs to make sure the write completes if necessary */
1690 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
1691 ppgtt->pd_addr + pde);
1692 }
1693
1694 /* Write all the page tables found in the ppgtt structure to incrementing page
1695 * directories. */
1696 static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
1697 u32 start, u32 length)
1698 {
1699 struct i915_page_table *pt;
1700 unsigned int pde;
1701
1702 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
1703 gen6_write_pde(ppgtt, pde, pt);
1704
1705 mark_tlbs_dirty(ppgtt);
1706 wmb();
1707 }
1708
1709 static inline u32 get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1710 {
1711 GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1712 return ppgtt->pd.base.ggtt_offset << 10;
1713 }
1714
1715 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1716 struct i915_request *rq)
1717 {
1718 struct intel_engine_cs *engine = rq->engine;
1719 u32 *cs;
1720
1721 /* NB: TLBs must be flushed and invalidated before a switch */
1722 cs = intel_ring_begin(rq, 6);
1723 if (IS_ERR(cs))
1724 return PTR_ERR(cs);
1725
1726 *cs++ = MI_LOAD_REGISTER_IMM(2);
1727 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1728 *cs++ = PP_DIR_DCLV_2G;
1729 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1730 *cs++ = get_pd_offset(ppgtt);
1731 *cs++ = MI_NOOP;
1732 intel_ring_advance(rq, cs);
1733
1734 return 0;
1735 }
1736
1737 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1738 struct i915_request *rq)
1739 {
1740 struct intel_engine_cs *engine = rq->engine;
1741 u32 *cs;
1742
1743 /* NB: TLBs must be flushed and invalidated before a switch */
1744 cs = intel_ring_begin(rq, 6);
1745 if (IS_ERR(cs))
1746 return PTR_ERR(cs);
1747
1748 *cs++ = MI_LOAD_REGISTER_IMM(2);
1749 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1750 *cs++ = PP_DIR_DCLV_2G;
1751 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1752 *cs++ = get_pd_offset(ppgtt);
1753 *cs++ = MI_NOOP;
1754 intel_ring_advance(rq, cs);
1755
1756 return 0;
1757 }
1758
1759 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1760 struct i915_request *rq)
1761 {
1762 struct intel_engine_cs *engine = rq->engine;
1763 struct drm_i915_private *dev_priv = rq->i915;
1764
1765 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1766 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
1767 return 0;
1768 }
1769
1770 static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
1771 {
1772 struct intel_engine_cs *engine;
1773 enum intel_engine_id id;
1774
1775 for_each_engine(engine, dev_priv, id) {
1776 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1777 GEN8_GFX_PPGTT_48B : 0;
1778 I915_WRITE(RING_MODE_GEN7(engine),
1779 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1780 }
1781 }
1782
1783 static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
1784 {
1785 struct intel_engine_cs *engine;
1786 u32 ecochk, ecobits;
1787 enum intel_engine_id id;
1788
1789 ecobits = I915_READ(GAC_ECO_BITS);
1790 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1791
1792 ecochk = I915_READ(GAM_ECOCHK);
1793 if (IS_HASWELL(dev_priv)) {
1794 ecochk |= ECOCHK_PPGTT_WB_HSW;
1795 } else {
1796 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1797 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1798 }
1799 I915_WRITE(GAM_ECOCHK, ecochk);
1800
1801 for_each_engine(engine, dev_priv, id) {
1802 /* GFX_MODE is per-ring on gen7+ */
1803 I915_WRITE(RING_MODE_GEN7(engine),
1804 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1805 }
1806 }
1807
1808 static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
1809 {
1810 u32 ecochk, gab_ctl, ecobits;
1811
1812 ecobits = I915_READ(GAC_ECO_BITS);
1813 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1814 ECOBITS_PPGTT_CACHE64B);
1815
1816 gab_ctl = I915_READ(GAB_CTL);
1817 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1818
1819 ecochk = I915_READ(GAM_ECOCHK);
1820 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1821
1822 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1823 }
1824
1825 /* PPGTT support for Sandybdrige/Gen6 and later */
1826 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1827 u64 start, u64 length)
1828 {
1829 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1830 unsigned int first_entry = start >> PAGE_SHIFT;
1831 unsigned int pde = first_entry / GEN6_PTES;
1832 unsigned int pte = first_entry % GEN6_PTES;
1833 unsigned int num_entries = length >> PAGE_SHIFT;
1834 gen6_pte_t scratch_pte =
1835 vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
1836
1837 while (num_entries) {
1838 struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
1839 unsigned int end = min(pte + num_entries, GEN6_PTES);
1840 gen6_pte_t *vaddr;
1841
1842 num_entries -= end - pte;
1843
1844 /* Note that the hw doesn't support removing PDE on the fly
1845 * (they are cached inside the context with no means to
1846 * invalidate the cache), so we can only reset the PTE
1847 * entries back to scratch.
1848 */
1849
1850 vaddr = kmap_atomic_px(pt);
1851 do {
1852 vaddr[pte++] = scratch_pte;
1853 } while (pte < end);
1854 kunmap_atomic(vaddr);
1855
1856 pte = 0;
1857 }
1858 }
1859
1860 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1861 struct i915_vma *vma,
1862 enum i915_cache_level cache_level,
1863 u32 flags)
1864 {
1865 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1866 unsigned first_entry = vma->node.start >> PAGE_SHIFT;
1867 unsigned act_pt = first_entry / GEN6_PTES;
1868 unsigned act_pte = first_entry % GEN6_PTES;
1869 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1870 struct sgt_dma iter = sgt_dma(vma);
1871 gen6_pte_t *vaddr;
1872
1873 vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
1874 do {
1875 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
1876
1877 iter.dma += PAGE_SIZE;
1878 if (iter.dma == iter.max) {
1879 iter.sg = __sg_next(iter.sg);
1880 if (!iter.sg)
1881 break;
1882
1883 iter.dma = sg_dma_address(iter.sg);
1884 iter.max = iter.dma + iter.sg->length;
1885 }
1886
1887 if (++act_pte == GEN6_PTES) {
1888 kunmap_atomic(vaddr);
1889 vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
1890 act_pte = 0;
1891 }
1892 } while (1);
1893 kunmap_atomic(vaddr);
1894
1895 vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
1896 }
1897
1898 static int gen6_alloc_va_range(struct i915_address_space *vm,
1899 u64 start, u64 length)
1900 {
1901 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1902 struct i915_page_table *pt;
1903 u64 from = start;
1904 unsigned int pde;
1905 bool flush = false;
1906
1907 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
1908 if (pt == vm->scratch_pt) {
1909 pt = alloc_pt(vm);
1910 if (IS_ERR(pt))
1911 goto unwind_out;
1912
1913 gen6_initialize_pt(vm, pt);
1914 ppgtt->pd.page_table[pde] = pt;
1915 gen6_write_pde(ppgtt, pde, pt);
1916 flush = true;
1917 }
1918 }
1919
1920 if (flush) {
1921 mark_tlbs_dirty(ppgtt);
1922 wmb();
1923 }
1924
1925 return 0;
1926
1927 unwind_out:
1928 gen6_ppgtt_clear_range(vm, from, start);
1929 return -ENOMEM;
1930 }
1931
1932 static int gen6_init_scratch(struct i915_address_space *vm)
1933 {
1934 int ret;
1935
1936 ret = setup_scratch_page(vm, __GFP_HIGHMEM);
1937 if (ret)
1938 return ret;
1939
1940 vm->scratch_pt = alloc_pt(vm);
1941 if (IS_ERR(vm->scratch_pt)) {
1942 cleanup_scratch_page(vm);
1943 return PTR_ERR(vm->scratch_pt);
1944 }
1945
1946 gen6_initialize_pt(vm, vm->scratch_pt);
1947
1948 return 0;
1949 }
1950
1951 static void gen6_free_scratch(struct i915_address_space *vm)
1952 {
1953 free_pt(vm, vm->scratch_pt);
1954 cleanup_scratch_page(vm);
1955 }
1956
1957 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1958 {
1959 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1960 struct i915_page_directory *pd = &ppgtt->pd;
1961 struct i915_page_table *pt;
1962 u32 pde;
1963
1964 drm_mm_remove_node(&ppgtt->node);
1965
1966 gen6_for_all_pdes(pt, pd, pde)
1967 if (pt != vm->scratch_pt)
1968 free_pt(vm, pt);
1969
1970 gen6_free_scratch(vm);
1971 }
1972
1973 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
1974 {
1975 struct i915_address_space *vm = &ppgtt->vm;
1976 struct drm_i915_private *dev_priv = ppgtt->vm.i915;
1977 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1978 int ret;
1979
1980 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1981 * allocator works in address space sizes, so it's multiplied by page
1982 * size. We allocate at the top of the GTT to avoid fragmentation.
1983 */
1984 BUG_ON(!drm_mm_initialized(&ggtt->vm.mm));
1985
1986 ret = gen6_init_scratch(vm);
1987 if (ret)
1988 return ret;
1989
1990 ret = i915_gem_gtt_insert(&ggtt->vm, &ppgtt->node,
1991 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1992 I915_COLOR_UNEVICTABLE,
1993 0, ggtt->vm.total,
1994 PIN_HIGH);
1995 if (ret)
1996 goto err_out;
1997
1998 if (ppgtt->node.start < ggtt->mappable_end)
1999 DRM_DEBUG("Forced to use aperture for PDEs\n");
2000
2001 ppgtt->pd.base.ggtt_offset =
2002 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2003
2004 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
2005 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2006
2007 return 0;
2008
2009 err_out:
2010 gen6_free_scratch(vm);
2011 return ret;
2012 }
2013
2014 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2015 {
2016 return gen6_ppgtt_allocate_page_directories(ppgtt);
2017 }
2018
2019 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2020 u64 start, u64 length)
2021 {
2022 struct i915_page_table *unused;
2023 u32 pde;
2024
2025 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
2026 ppgtt->pd.page_table[pde] = ppgtt->vm.scratch_pt;
2027 }
2028
2029 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2030 {
2031 struct drm_i915_private *dev_priv = ppgtt->vm.i915;
2032 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2033 int ret;
2034
2035 ppgtt->vm.pte_encode = ggtt->vm.pte_encode;
2036 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
2037 ppgtt->switch_mm = gen6_mm_switch;
2038 else if (IS_HASWELL(dev_priv))
2039 ppgtt->switch_mm = hsw_mm_switch;
2040 else if (IS_GEN7(dev_priv))
2041 ppgtt->switch_mm = gen7_mm_switch;
2042 else
2043 BUG();
2044
2045 ret = gen6_ppgtt_alloc(ppgtt);
2046 if (ret)
2047 return ret;
2048
2049 ppgtt->vm.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
2050
2051 gen6_scratch_va_range(ppgtt, 0, ppgtt->vm.total);
2052 gen6_write_page_range(ppgtt, 0, ppgtt->vm.total);
2053
2054 ret = gen6_alloc_va_range(&ppgtt->vm, 0, ppgtt->vm.total);
2055 if (ret) {
2056 gen6_ppgtt_cleanup(&ppgtt->vm);
2057 return ret;
2058 }
2059
2060 ppgtt->vm.clear_range = gen6_ppgtt_clear_range;
2061 ppgtt->vm.insert_entries = gen6_ppgtt_insert_entries;
2062 ppgtt->vm.bind_vma = gen6_ppgtt_bind_vma;
2063 ppgtt->vm.unbind_vma = ppgtt_unbind_vma;
2064 ppgtt->vm.set_pages = ppgtt_set_pages;
2065 ppgtt->vm.clear_pages = clear_pages;
2066 ppgtt->vm.cleanup = gen6_ppgtt_cleanup;
2067 ppgtt->debug_dump = gen6_dump_ppgtt;
2068
2069 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
2070 ppgtt->node.size >> 20,
2071 ppgtt->node.start / PAGE_SIZE);
2072
2073 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
2074 ppgtt->pd.base.ggtt_offset << 10);
2075
2076 return 0;
2077 }
2078
2079 static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2080 struct drm_i915_private *dev_priv)
2081 {
2082 ppgtt->vm.i915 = dev_priv;
2083 ppgtt->vm.dma = &dev_priv->drm.pdev->dev;
2084
2085 if (INTEL_GEN(dev_priv) < 8)
2086 return gen6_ppgtt_init(ppgtt);
2087 else
2088 return gen8_ppgtt_init(ppgtt);
2089 }
2090
2091 static void i915_address_space_init(struct i915_address_space *vm,
2092 struct drm_i915_private *dev_priv,
2093 const char *name)
2094 {
2095 drm_mm_init(&vm->mm, 0, vm->total);
2096 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2097
2098 INIT_LIST_HEAD(&vm->active_list);
2099 INIT_LIST_HEAD(&vm->inactive_list);
2100 INIT_LIST_HEAD(&vm->unbound_list);
2101
2102 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2103 pagevec_init(&vm->free_pages);
2104 }
2105
2106 static void i915_address_space_fini(struct i915_address_space *vm)
2107 {
2108 if (pagevec_count(&vm->free_pages))
2109 vm_free_pages_release(vm, true);
2110
2111 drm_mm_takedown(&vm->mm);
2112 list_del(&vm->global_link);
2113 }
2114
2115 static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
2116 {
2117 /* This function is for gtt related workarounds. This function is
2118 * called on driver load and after a GPU reset, so you can place
2119 * workarounds here even if they get overwritten by GPU reset.
2120 */
2121 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
2122 if (IS_BROADWELL(dev_priv))
2123 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2124 else if (IS_CHERRYVIEW(dev_priv))
2125 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2126 else if (IS_GEN9_LP(dev_priv))
2127 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2128 else if (INTEL_GEN(dev_priv) >= 9)
2129 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2130
2131 /*
2132 * To support 64K PTEs we need to first enable the use of the
2133 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
2134 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
2135 * shouldn't be needed after GEN10.
2136 *
2137 * 64K pages were first introduced from BDW+, although technically they
2138 * only *work* from gen9+. For pre-BDW we instead have the option for
2139 * 32K pages, but we don't currently have any support for it in our
2140 * driver.
2141 */
2142 if (HAS_PAGE_SIZES(dev_priv, I915_GTT_PAGE_SIZE_64K) &&
2143 INTEL_GEN(dev_priv) <= 10)
2144 I915_WRITE(GEN8_GAMW_ECO_DEV_RW_IA,
2145 I915_READ(GEN8_GAMW_ECO_DEV_RW_IA) |
2146 GAMW_ECO_ENABLE_64K_IPS_FIELD);
2147 }
2148
2149 int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
2150 {
2151 gtt_write_workarounds(dev_priv);
2152
2153 /* In the case of execlists, PPGTT is enabled by the context descriptor
2154 * and the PDPs are contained within the context itself. We don't
2155 * need to do anything here. */
2156 if (HAS_LOGICAL_RING_CONTEXTS(dev_priv))
2157 return 0;
2158
2159 if (!USES_PPGTT(dev_priv))
2160 return 0;
2161
2162 if (IS_GEN6(dev_priv))
2163 gen6_ppgtt_enable(dev_priv);
2164 else if (IS_GEN7(dev_priv))
2165 gen7_ppgtt_enable(dev_priv);
2166 else if (INTEL_GEN(dev_priv) >= 8)
2167 gen8_ppgtt_enable(dev_priv);
2168 else
2169 MISSING_CASE(INTEL_GEN(dev_priv));
2170
2171 return 0;
2172 }
2173
2174 struct i915_hw_ppgtt *
2175 i915_ppgtt_create(struct drm_i915_private *dev_priv,
2176 struct drm_i915_file_private *fpriv,
2177 const char *name)
2178 {
2179 struct i915_hw_ppgtt *ppgtt;
2180 int ret;
2181
2182 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2183 if (!ppgtt)
2184 return ERR_PTR(-ENOMEM);
2185
2186 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2187 if (ret) {
2188 kfree(ppgtt);
2189 return ERR_PTR(ret);
2190 }
2191
2192 kref_init(&ppgtt->ref);
2193 i915_address_space_init(&ppgtt->vm, dev_priv, name);
2194 ppgtt->vm.file = fpriv;
2195
2196 trace_i915_ppgtt_create(&ppgtt->vm);
2197
2198 return ppgtt;
2199 }
2200
2201 void i915_ppgtt_close(struct i915_address_space *vm)
2202 {
2203 GEM_BUG_ON(vm->closed);
2204 vm->closed = true;
2205 }
2206
2207 static void ppgtt_destroy_vma(struct i915_address_space *vm)
2208 {
2209 struct list_head *phases[] = {
2210 &vm->active_list,
2211 &vm->inactive_list,
2212 &vm->unbound_list,
2213 NULL,
2214 }, **phase;
2215
2216 vm->closed = true;
2217 for (phase = phases; *phase; phase++) {
2218 struct i915_vma *vma, *vn;
2219
2220 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2221 i915_vma_destroy(vma);
2222 }
2223 }
2224
2225 void i915_ppgtt_release(struct kref *kref)
2226 {
2227 struct i915_hw_ppgtt *ppgtt =
2228 container_of(kref, struct i915_hw_ppgtt, ref);
2229
2230 trace_i915_ppgtt_release(&ppgtt->vm);
2231
2232 ppgtt_destroy_vma(&ppgtt->vm);
2233
2234 GEM_BUG_ON(!list_empty(&ppgtt->vm.active_list));
2235 GEM_BUG_ON(!list_empty(&ppgtt->vm.inactive_list));
2236 GEM_BUG_ON(!list_empty(&ppgtt->vm.unbound_list));
2237
2238 ppgtt->vm.cleanup(&ppgtt->vm);
2239 i915_address_space_fini(&ppgtt->vm);
2240 kfree(ppgtt);
2241 }
2242
2243 /* Certain Gen5 chipsets require require idling the GPU before
2244 * unmapping anything from the GTT when VT-d is enabled.
2245 */
2246 static bool needs_idle_maps(struct drm_i915_private *dev_priv)
2247 {
2248 /* Query intel_iommu to see if we need the workaround. Presumably that
2249 * was loaded first.
2250 */
2251 return IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_vtd_active();
2252 }
2253
2254 static void gen6_check_and_clear_faults(struct drm_i915_private *dev_priv)
2255 {
2256 struct intel_engine_cs *engine;
2257 enum intel_engine_id id;
2258 u32 fault;
2259
2260 for_each_engine(engine, dev_priv, id) {
2261 fault = I915_READ(RING_FAULT_REG(engine));
2262 if (fault & RING_FAULT_VALID) {
2263 DRM_DEBUG_DRIVER("Unexpected fault\n"
2264 "\tAddr: 0x%08lx\n"
2265 "\tAddress space: %s\n"
2266 "\tSource ID: %d\n"
2267 "\tType: %d\n",
2268 fault & PAGE_MASK,
2269 fault & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2270 RING_FAULT_SRCID(fault),
2271 RING_FAULT_FAULT_TYPE(fault));
2272 I915_WRITE(RING_FAULT_REG(engine),
2273 fault & ~RING_FAULT_VALID);
2274 }
2275 }
2276
2277 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
2278 }
2279
2280 static void gen8_check_and_clear_faults(struct drm_i915_private *dev_priv)
2281 {
2282 u32 fault = I915_READ(GEN8_RING_FAULT_REG);
2283
2284 if (fault & RING_FAULT_VALID) {
2285 u32 fault_data0, fault_data1;
2286 u64 fault_addr;
2287
2288 fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
2289 fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
2290 fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
2291 ((u64)fault_data0 << 12);
2292
2293 DRM_DEBUG_DRIVER("Unexpected fault\n"
2294 "\tAddr: 0x%08x_%08x\n"
2295 "\tAddress space: %s\n"
2296 "\tEngine ID: %d\n"
2297 "\tSource ID: %d\n"
2298 "\tType: %d\n",
2299 upper_32_bits(fault_addr),
2300 lower_32_bits(fault_addr),
2301 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
2302 GEN8_RING_FAULT_ENGINE_ID(fault),
2303 RING_FAULT_SRCID(fault),
2304 RING_FAULT_FAULT_TYPE(fault));
2305 I915_WRITE(GEN8_RING_FAULT_REG,
2306 fault & ~RING_FAULT_VALID);
2307 }
2308
2309 POSTING_READ(GEN8_RING_FAULT_REG);
2310 }
2311
2312 void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
2313 {
2314 /* From GEN8 onwards we only have one 'All Engine Fault Register' */
2315 if (INTEL_GEN(dev_priv) >= 8)
2316 gen8_check_and_clear_faults(dev_priv);
2317 else if (INTEL_GEN(dev_priv) >= 6)
2318 gen6_check_and_clear_faults(dev_priv);
2319 else
2320 return;
2321 }
2322
2323 void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
2324 {
2325 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2326
2327 /* Don't bother messing with faults pre GEN6 as we have little
2328 * documentation supporting that it's a good idea.
2329 */
2330 if (INTEL_GEN(dev_priv) < 6)
2331 return;
2332
2333 i915_check_and_clear_faults(dev_priv);
2334
2335 ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
2336
2337 i915_ggtt_invalidate(dev_priv);
2338 }
2339
2340 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2341 struct sg_table *pages)
2342 {
2343 do {
2344 if (dma_map_sg_attrs(&obj->base.dev->pdev->dev,
2345 pages->sgl, pages->nents,
2346 PCI_DMA_BIDIRECTIONAL,
2347 DMA_ATTR_NO_WARN))
2348 return 0;
2349
2350 /* If the DMA remap fails, one cause can be that we have
2351 * too many objects pinned in a small remapping table,
2352 * such as swiotlb. Incrementally purge all other objects and
2353 * try again - if there are no more pages to remove from
2354 * the DMA remapper, i915_gem_shrink will return 0.
2355 */
2356 GEM_BUG_ON(obj->mm.pages == pages);
2357 } while (i915_gem_shrink(to_i915(obj->base.dev),
2358 obj->base.size >> PAGE_SHIFT, NULL,
2359 I915_SHRINK_BOUND |
2360 I915_SHRINK_UNBOUND |
2361 I915_SHRINK_ACTIVE));
2362
2363 return -ENOSPC;
2364 }
2365
2366 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2367 {
2368 writeq(pte, addr);
2369 }
2370
2371 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2372 dma_addr_t addr,
2373 u64 offset,
2374 enum i915_cache_level level,
2375 u32 unused)
2376 {
2377 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2378 gen8_pte_t __iomem *pte =
2379 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
2380
2381 gen8_set_pte(pte, gen8_pte_encode(addr, level));
2382
2383 ggtt->invalidate(vm->i915);
2384 }
2385
2386 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2387 struct i915_vma *vma,
2388 enum i915_cache_level level,
2389 u32 unused)
2390 {
2391 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2392 struct sgt_iter sgt_iter;
2393 gen8_pte_t __iomem *gtt_entries;
2394 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
2395 dma_addr_t addr;
2396
2397 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2398 gtt_entries += vma->node.start >> PAGE_SHIFT;
2399 for_each_sgt_dma(addr, sgt_iter, vma->pages)
2400 gen8_set_pte(gtt_entries++, pte_encode | addr);
2401
2402 /*
2403 * We want to flush the TLBs only after we're certain all the PTE
2404 * updates have finished.
2405 */
2406 ggtt->invalidate(vm->i915);
2407 }
2408
2409 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2410 dma_addr_t addr,
2411 u64 offset,
2412 enum i915_cache_level level,
2413 u32 flags)
2414 {
2415 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2416 gen6_pte_t __iomem *pte =
2417 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
2418
2419 iowrite32(vm->pte_encode(addr, level, flags), pte);
2420
2421 ggtt->invalidate(vm->i915);
2422 }
2423
2424 /*
2425 * Binds an object into the global gtt with the specified cache level. The object
2426 * will be accessible to the GPU via commands whose operands reference offsets
2427 * within the global GTT as well as accessible by the GPU through the GMADR
2428 * mapped BAR (dev_priv->mm.gtt->gtt).
2429 */
2430 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2431 struct i915_vma *vma,
2432 enum i915_cache_level level,
2433 u32 flags)
2434 {
2435 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2436 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2437 unsigned int i = vma->node.start >> PAGE_SHIFT;
2438 struct sgt_iter iter;
2439 dma_addr_t addr;
2440 for_each_sgt_dma(addr, iter, vma->pages)
2441 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2442
2443 /*
2444 * We want to flush the TLBs only after we're certain all the PTE
2445 * updates have finished.
2446 */
2447 ggtt->invalidate(vm->i915);
2448 }
2449
2450 static void nop_clear_range(struct i915_address_space *vm,
2451 u64 start, u64 length)
2452 {
2453 }
2454
2455 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2456 u64 start, u64 length)
2457 {
2458 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2459 unsigned first_entry = start >> PAGE_SHIFT;
2460 unsigned num_entries = length >> PAGE_SHIFT;
2461 const gen8_pte_t scratch_pte =
2462 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2463 gen8_pte_t __iomem *gtt_base =
2464 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2465 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2466 int i;
2467
2468 if (WARN(num_entries > max_entries,
2469 "First entry = %d; Num entries = %d (max=%d)\n",
2470 first_entry, num_entries, max_entries))
2471 num_entries = max_entries;
2472
2473 for (i = 0; i < num_entries; i++)
2474 gen8_set_pte(&gtt_base[i], scratch_pte);
2475 }
2476
2477 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
2478 {
2479 struct drm_i915_private *dev_priv = vm->i915;
2480
2481 /*
2482 * Make sure the internal GAM fifo has been cleared of all GTT
2483 * writes before exiting stop_machine(). This guarantees that
2484 * any aperture accesses waiting to start in another process
2485 * cannot back up behind the GTT writes causing a hang.
2486 * The register can be any arbitrary GAM register.
2487 */
2488 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2489 }
2490
2491 struct insert_page {
2492 struct i915_address_space *vm;
2493 dma_addr_t addr;
2494 u64 offset;
2495 enum i915_cache_level level;
2496 };
2497
2498 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
2499 {
2500 struct insert_page *arg = _arg;
2501
2502 gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
2503 bxt_vtd_ggtt_wa(arg->vm);
2504
2505 return 0;
2506 }
2507
2508 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
2509 dma_addr_t addr,
2510 u64 offset,
2511 enum i915_cache_level level,
2512 u32 unused)
2513 {
2514 struct insert_page arg = { vm, addr, offset, level };
2515
2516 stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
2517 }
2518
2519 struct insert_entries {
2520 struct i915_address_space *vm;
2521 struct i915_vma *vma;
2522 enum i915_cache_level level;
2523 };
2524
2525 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
2526 {
2527 struct insert_entries *arg = _arg;
2528
2529 gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, 0);
2530 bxt_vtd_ggtt_wa(arg->vm);
2531
2532 return 0;
2533 }
2534
2535 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2536 struct i915_vma *vma,
2537 enum i915_cache_level level,
2538 u32 unused)
2539 {
2540 struct insert_entries arg = { vm, vma, level };
2541
2542 stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
2543 }
2544
2545 struct clear_range {
2546 struct i915_address_space *vm;
2547 u64 start;
2548 u64 length;
2549 };
2550
2551 static int bxt_vtd_ggtt_clear_range__cb(void *_arg)
2552 {
2553 struct clear_range *arg = _arg;
2554
2555 gen8_ggtt_clear_range(arg->vm, arg->start, arg->length);
2556 bxt_vtd_ggtt_wa(arg->vm);
2557
2558 return 0;
2559 }
2560
2561 static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space *vm,
2562 u64 start,
2563 u64 length)
2564 {
2565 struct clear_range arg = { vm, start, length };
2566
2567 stop_machine(bxt_vtd_ggtt_clear_range__cb, &arg, NULL);
2568 }
2569
2570 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2571 u64 start, u64 length)
2572 {
2573 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2574 unsigned first_entry = start >> PAGE_SHIFT;
2575 unsigned num_entries = length >> PAGE_SHIFT;
2576 gen6_pte_t scratch_pte, __iomem *gtt_base =
2577 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2578 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2579 int i;
2580
2581 if (WARN(num_entries > max_entries,
2582 "First entry = %d; Num entries = %d (max=%d)\n",
2583 first_entry, num_entries, max_entries))
2584 num_entries = max_entries;
2585
2586 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
2587 I915_CACHE_LLC, 0);
2588
2589 for (i = 0; i < num_entries; i++)
2590 iowrite32(scratch_pte, &gtt_base[i]);
2591 }
2592
2593 static void i915_ggtt_insert_page(struct i915_address_space *vm,
2594 dma_addr_t addr,
2595 u64 offset,
2596 enum i915_cache_level cache_level,
2597 u32 unused)
2598 {
2599 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2600 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2601
2602 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2603 }
2604
2605 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2606 struct i915_vma *vma,
2607 enum i915_cache_level cache_level,
2608 u32 unused)
2609 {
2610 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2611 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2612
2613 intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
2614 flags);
2615 }
2616
2617 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2618 u64 start, u64 length)
2619 {
2620 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
2621 }
2622
2623 static int ggtt_bind_vma(struct i915_vma *vma,
2624 enum i915_cache_level cache_level,
2625 u32 flags)
2626 {
2627 struct drm_i915_private *i915 = vma->vm->i915;
2628 struct drm_i915_gem_object *obj = vma->obj;
2629 u32 pte_flags;
2630
2631 /* Currently applicable only to VLV */
2632 pte_flags = 0;
2633 if (obj->gt_ro)
2634 pte_flags |= PTE_READ_ONLY;
2635
2636 intel_runtime_pm_get(i915);
2637 vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
2638 intel_runtime_pm_put(i915);
2639
2640 vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
2641
2642 /*
2643 * Without aliasing PPGTT there's no difference between
2644 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2645 * upgrade to both bound if we bind either to avoid double-binding.
2646 */
2647 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
2648
2649 return 0;
2650 }
2651
2652 static void ggtt_unbind_vma(struct i915_vma *vma)
2653 {
2654 struct drm_i915_private *i915 = vma->vm->i915;
2655
2656 intel_runtime_pm_get(i915);
2657 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2658 intel_runtime_pm_put(i915);
2659 }
2660
2661 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2662 enum i915_cache_level cache_level,
2663 u32 flags)
2664 {
2665 struct drm_i915_private *i915 = vma->vm->i915;
2666 u32 pte_flags;
2667 int ret;
2668
2669 /* Currently applicable only to VLV */
2670 pte_flags = 0;
2671 if (vma->obj->gt_ro)
2672 pte_flags |= PTE_READ_ONLY;
2673
2674 if (flags & I915_VMA_LOCAL_BIND) {
2675 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2676
2677 if (!(vma->flags & I915_VMA_LOCAL_BIND) &&
2678 appgtt->vm.allocate_va_range) {
2679 ret = appgtt->vm.allocate_va_range(&appgtt->vm,
2680 vma->node.start,
2681 vma->size);
2682 if (ret)
2683 return ret;
2684 }
2685
2686 appgtt->vm.insert_entries(&appgtt->vm, vma, cache_level,
2687 pte_flags);
2688 }
2689
2690 if (flags & I915_VMA_GLOBAL_BIND) {
2691 intel_runtime_pm_get(i915);
2692 vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
2693 intel_runtime_pm_put(i915);
2694 }
2695
2696 return 0;
2697 }
2698
2699 static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
2700 {
2701 struct drm_i915_private *i915 = vma->vm->i915;
2702
2703 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2704 intel_runtime_pm_get(i915);
2705 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2706 intel_runtime_pm_put(i915);
2707 }
2708
2709 if (vma->flags & I915_VMA_LOCAL_BIND) {
2710 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->vm;
2711
2712 vm->clear_range(vm, vma->node.start, vma->size);
2713 }
2714 }
2715
2716 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2717 struct sg_table *pages)
2718 {
2719 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2720 struct device *kdev = &dev_priv->drm.pdev->dev;
2721 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2722
2723 if (unlikely(ggtt->do_idle_maps)) {
2724 if (i915_gem_wait_for_idle(dev_priv, 0)) {
2725 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2726 /* Wait a bit, in hopes it avoids the hang */
2727 udelay(10);
2728 }
2729 }
2730
2731 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
2732 }
2733
2734 static int ggtt_set_pages(struct i915_vma *vma)
2735 {
2736 int ret;
2737
2738 GEM_BUG_ON(vma->pages);
2739
2740 ret = i915_get_ggtt_vma_pages(vma);
2741 if (ret)
2742 return ret;
2743
2744 vma->page_sizes = vma->obj->mm.page_sizes;
2745
2746 return 0;
2747 }
2748
2749 static void i915_gtt_color_adjust(const struct drm_mm_node *node,
2750 unsigned long color,
2751 u64 *start,
2752 u64 *end)
2753 {
2754 if (node->allocated && node->color != color)
2755 *start += I915_GTT_PAGE_SIZE;
2756
2757 /* Also leave a space between the unallocated reserved node after the
2758 * GTT and any objects within the GTT, i.e. we use the color adjustment
2759 * to insert a guard page to prevent prefetches crossing over the
2760 * GTT boundary.
2761 */
2762 node = list_next_entry(node, node_list);
2763 if (node->color != color)
2764 *end -= I915_GTT_PAGE_SIZE;
2765 }
2766
2767 int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2768 {
2769 struct i915_ggtt *ggtt = &i915->ggtt;
2770 struct i915_hw_ppgtt *ppgtt;
2771 int err;
2772
2773 ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
2774 if (IS_ERR(ppgtt))
2775 return PTR_ERR(ppgtt);
2776
2777 if (WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
2778 err = -ENODEV;
2779 goto err_ppgtt;
2780 }
2781
2782 if (ppgtt->vm.allocate_va_range) {
2783 /* Note we only pre-allocate as far as the end of the global
2784 * GTT. On 48b / 4-level page-tables, the difference is very,
2785 * very significant! We have to preallocate as GVT/vgpu does
2786 * not like the page directory disappearing.
2787 */
2788 err = ppgtt->vm.allocate_va_range(&ppgtt->vm,
2789 0, ggtt->vm.total);
2790 if (err)
2791 goto err_ppgtt;
2792 }
2793
2794 i915->mm.aliasing_ppgtt = ppgtt;
2795
2796 GEM_BUG_ON(ggtt->vm.bind_vma != ggtt_bind_vma);
2797 ggtt->vm.bind_vma = aliasing_gtt_bind_vma;
2798
2799 GEM_BUG_ON(ggtt->vm.unbind_vma != ggtt_unbind_vma);
2800 ggtt->vm.unbind_vma = aliasing_gtt_unbind_vma;
2801
2802 return 0;
2803
2804 err_ppgtt:
2805 i915_ppgtt_put(ppgtt);
2806 return err;
2807 }
2808
2809 void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2810 {
2811 struct i915_ggtt *ggtt = &i915->ggtt;
2812 struct i915_hw_ppgtt *ppgtt;
2813
2814 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2815 if (!ppgtt)
2816 return;
2817
2818 i915_ppgtt_put(ppgtt);
2819
2820 ggtt->vm.bind_vma = ggtt_bind_vma;
2821 ggtt->vm.unbind_vma = ggtt_unbind_vma;
2822 }
2823
2824 int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
2825 {
2826 /* Let GEM Manage all of the aperture.
2827 *
2828 * However, leave one page at the end still bound to the scratch page.
2829 * There are a number of places where the hardware apparently prefetches
2830 * past the end of the object, and we've seen multiple hangs with the
2831 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2832 * aperture. One page should be enough to keep any prefetching inside
2833 * of the aperture.
2834 */
2835 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2836 unsigned long hole_start, hole_end;
2837 struct drm_mm_node *entry;
2838 int ret;
2839
2840 ret = intel_vgt_balloon(dev_priv);
2841 if (ret)
2842 return ret;
2843
2844 /* Reserve a mappable slot for our lockless error capture */
2845 ret = drm_mm_insert_node_in_range(&ggtt->vm.mm, &ggtt->error_capture,
2846 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2847 0, ggtt->mappable_end,
2848 DRM_MM_INSERT_LOW);
2849 if (ret)
2850 return ret;
2851
2852 /* Clear any non-preallocated blocks */
2853 drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
2854 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2855 hole_start, hole_end);
2856 ggtt->vm.clear_range(&ggtt->vm, hole_start,
2857 hole_end - hole_start);
2858 }
2859
2860 /* And finally clear the reserved guard page */
2861 ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
2862
2863 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
2864 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
2865 if (ret)
2866 goto err;
2867 }
2868
2869 return 0;
2870
2871 err:
2872 drm_mm_remove_node(&ggtt->error_capture);
2873 return ret;
2874 }
2875
2876 /**
2877 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
2878 * @dev_priv: i915 device
2879 */
2880 void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
2881 {
2882 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2883 struct i915_vma *vma, *vn;
2884 struct pagevec *pvec;
2885
2886 ggtt->vm.closed = true;
2887
2888 mutex_lock(&dev_priv->drm.struct_mutex);
2889 GEM_BUG_ON(!list_empty(&ggtt->vm.active_list));
2890 list_for_each_entry_safe(vma, vn, &ggtt->vm.inactive_list, vm_link)
2891 WARN_ON(i915_vma_unbind(vma));
2892 mutex_unlock(&dev_priv->drm.struct_mutex);
2893
2894 i915_gem_cleanup_stolen(&dev_priv->drm);
2895
2896 mutex_lock(&dev_priv->drm.struct_mutex);
2897 i915_gem_fini_aliasing_ppgtt(dev_priv);
2898
2899 if (drm_mm_node_allocated(&ggtt->error_capture))
2900 drm_mm_remove_node(&ggtt->error_capture);
2901
2902 if (drm_mm_initialized(&ggtt->vm.mm)) {
2903 intel_vgt_deballoon(dev_priv);
2904 i915_address_space_fini(&ggtt->vm);
2905 }
2906
2907 ggtt->vm.cleanup(&ggtt->vm);
2908
2909 pvec = &dev_priv->mm.wc_stash;
2910 if (pvec->nr) {
2911 set_pages_array_wb(pvec->pages, pvec->nr);
2912 __pagevec_release(pvec);
2913 }
2914
2915 mutex_unlock(&dev_priv->drm.struct_mutex);
2916
2917 arch_phys_wc_del(ggtt->mtrr);
2918 io_mapping_fini(&ggtt->iomap);
2919 }
2920
2921 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2922 {
2923 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2924 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2925 return snb_gmch_ctl << 20;
2926 }
2927
2928 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2929 {
2930 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2931 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2932 if (bdw_gmch_ctl)
2933 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2934
2935 #ifdef CONFIG_X86_32
2936 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2937 if (bdw_gmch_ctl > 4)
2938 bdw_gmch_ctl = 4;
2939 #endif
2940
2941 return bdw_gmch_ctl << 20;
2942 }
2943
2944 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2945 {
2946 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2947 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2948
2949 if (gmch_ctrl)
2950 return 1 << (20 + gmch_ctrl);
2951
2952 return 0;
2953 }
2954
2955 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
2956 {
2957 struct drm_i915_private *dev_priv = ggtt->vm.i915;
2958 struct pci_dev *pdev = dev_priv->drm.pdev;
2959 phys_addr_t phys_addr;
2960 int ret;
2961
2962 /* For Modern GENs the PTEs and register space are split in the BAR */
2963 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
2964
2965 /*
2966 * On BXT+/CNL+ writes larger than 64 bit to the GTT pagetable range
2967 * will be dropped. For WC mappings in general we have 64 byte burst
2968 * writes when the WC buffer is flushed, so we can't use it, but have to
2969 * resort to an uncached mapping. The WC issue is easily caught by the
2970 * readback check when writing GTT PTE entries.
2971 */
2972 if (IS_GEN9_LP(dev_priv) || INTEL_GEN(dev_priv) >= 10)
2973 ggtt->gsm = ioremap_nocache(phys_addr, size);
2974 else
2975 ggtt->gsm = ioremap_wc(phys_addr, size);
2976 if (!ggtt->gsm) {
2977 DRM_ERROR("Failed to map the ggtt page table\n");
2978 return -ENOMEM;
2979 }
2980
2981 ret = setup_scratch_page(&ggtt->vm, GFP_DMA32);
2982 if (ret) {
2983 DRM_ERROR("Scratch setup failed\n");
2984 /* iounmap will also get called at remove, but meh */
2985 iounmap(ggtt->gsm);
2986 return ret;
2987 }
2988
2989 return 0;
2990 }
2991
2992 static struct intel_ppat_entry *
2993 __alloc_ppat_entry(struct intel_ppat *ppat, unsigned int index, u8 value)
2994 {
2995 struct intel_ppat_entry *entry = &ppat->entries[index];
2996
2997 GEM_BUG_ON(index >= ppat->max_entries);
2998 GEM_BUG_ON(test_bit(index, ppat->used));
2999
3000 entry->ppat = ppat;
3001 entry->value = value;
3002 kref_init(&entry->ref);
3003 set_bit(index, ppat->used);
3004 set_bit(index, ppat->dirty);
3005
3006 return entry;
3007 }
3008
3009 static void __free_ppat_entry(struct intel_ppat_entry *entry)
3010 {
3011 struct intel_ppat *ppat = entry->ppat;
3012 unsigned int index = entry - ppat->entries;
3013
3014 GEM_BUG_ON(index >= ppat->max_entries);
3015 GEM_BUG_ON(!test_bit(index, ppat->used));
3016
3017 entry->value = ppat->clear_value;
3018 clear_bit(index, ppat->used);
3019 set_bit(index, ppat->dirty);
3020 }
3021
3022 /**
3023 * intel_ppat_get - get a usable PPAT entry
3024 * @i915: i915 device instance
3025 * @value: the PPAT value required by the caller
3026 *
3027 * The function tries to search if there is an existing PPAT entry which
3028 * matches with the required value. If perfectly matched, the existing PPAT
3029 * entry will be used. If only partially matched, it will try to check if
3030 * there is any available PPAT index. If yes, it will allocate a new PPAT
3031 * index for the required entry and update the HW. If not, the partially
3032 * matched entry will be used.
3033 */
3034 const struct intel_ppat_entry *
3035 intel_ppat_get(struct drm_i915_private *i915, u8 value)
3036 {
3037 struct intel_ppat *ppat = &i915->ppat;
3038 struct intel_ppat_entry *entry = NULL;
3039 unsigned int scanned, best_score;
3040 int i;
3041
3042 GEM_BUG_ON(!ppat->max_entries);
3043
3044 scanned = best_score = 0;
3045 for_each_set_bit(i, ppat->used, ppat->max_entries) {
3046 unsigned int score;
3047
3048 score = ppat->match(ppat->entries[i].value, value);
3049 if (score > best_score) {
3050 entry = &ppat->entries[i];
3051 if (score == INTEL_PPAT_PERFECT_MATCH) {
3052 kref_get(&entry->ref);
3053 return entry;
3054 }
3055 best_score = score;
3056 }
3057 scanned++;
3058 }
3059
3060 if (scanned == ppat->max_entries) {
3061 if (!entry)
3062 return ERR_PTR(-ENOSPC);
3063
3064 kref_get(&entry->ref);
3065 return entry;
3066 }
3067
3068 i = find_first_zero_bit(ppat->used, ppat->max_entries);
3069 entry = __alloc_ppat_entry(ppat, i, value);
3070 ppat->update_hw(i915);
3071 return entry;
3072 }
3073
3074 static void release_ppat(struct kref *kref)
3075 {
3076 struct intel_ppat_entry *entry =
3077 container_of(kref, struct intel_ppat_entry, ref);
3078 struct drm_i915_private *i915 = entry->ppat->i915;
3079
3080 __free_ppat_entry(entry);
3081 entry->ppat->update_hw(i915);
3082 }
3083
3084 /**
3085 * intel_ppat_put - put back the PPAT entry got from intel_ppat_get()
3086 * @entry: an intel PPAT entry
3087 *
3088 * Put back the PPAT entry got from intel_ppat_get(). If the PPAT index of the
3089 * entry is dynamically allocated, its reference count will be decreased. Once
3090 * the reference count becomes into zero, the PPAT index becomes free again.
3091 */
3092 void intel_ppat_put(const struct intel_ppat_entry *entry)
3093 {
3094 struct intel_ppat *ppat = entry->ppat;
3095 unsigned int index = entry - ppat->entries;
3096
3097 GEM_BUG_ON(!ppat->max_entries);
3098
3099 kref_put(&ppat->entries[index].ref, release_ppat);
3100 }
3101
3102 static void cnl_private_pat_update_hw(struct drm_i915_private *dev_priv)
3103 {
3104 struct intel_ppat *ppat = &dev_priv->ppat;
3105 int i;
3106
3107 for_each_set_bit(i, ppat->dirty, ppat->max_entries) {
3108 I915_WRITE(GEN10_PAT_INDEX(i), ppat->entries[i].value);
3109 clear_bit(i, ppat->dirty);
3110 }
3111 }
3112
3113 static void bdw_private_pat_update_hw(struct drm_i915_private *dev_priv)
3114 {
3115 struct intel_ppat *ppat = &dev_priv->ppat;
3116 u64 pat = 0;
3117 int i;
3118
3119 for (i = 0; i < ppat->max_entries; i++)
3120 pat |= GEN8_PPAT(i, ppat->entries[i].value);
3121
3122 bitmap_clear(ppat->dirty, 0, ppat->max_entries);
3123
3124 I915_WRITE(GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
3125 I915_WRITE(GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
3126 }
3127
3128 static unsigned int bdw_private_pat_match(u8 src, u8 dst)
3129 {
3130 unsigned int score = 0;
3131 enum {
3132 AGE_MATCH = BIT(0),
3133 TC_MATCH = BIT(1),
3134 CA_MATCH = BIT(2),
3135 };
3136
3137 /* Cache attribute has to be matched. */
3138 if (GEN8_PPAT_GET_CA(src) != GEN8_PPAT_GET_CA(dst))
3139 return 0;
3140
3141 score |= CA_MATCH;
3142
3143 if (GEN8_PPAT_GET_TC(src) == GEN8_PPAT_GET_TC(dst))
3144 score |= TC_MATCH;
3145
3146 if (GEN8_PPAT_GET_AGE(src) == GEN8_PPAT_GET_AGE(dst))
3147 score |= AGE_MATCH;
3148
3149 if (score == (AGE_MATCH | TC_MATCH | CA_MATCH))
3150 return INTEL_PPAT_PERFECT_MATCH;
3151
3152 return score;
3153 }
3154
3155 static unsigned int chv_private_pat_match(u8 src, u8 dst)
3156 {
3157 return (CHV_PPAT_GET_SNOOP(src) == CHV_PPAT_GET_SNOOP(dst)) ?
3158 INTEL_PPAT_PERFECT_MATCH : 0;
3159 }
3160
3161 static void cnl_setup_private_ppat(struct intel_ppat *ppat)
3162 {
3163 ppat->max_entries = 8;
3164 ppat->update_hw = cnl_private_pat_update_hw;
3165 ppat->match = bdw_private_pat_match;
3166 ppat->clear_value = GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3);
3167
3168 __alloc_ppat_entry(ppat, 0, GEN8_PPAT_WB | GEN8_PPAT_LLC);
3169 __alloc_ppat_entry(ppat, 1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
3170 __alloc_ppat_entry(ppat, 2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
3171 __alloc_ppat_entry(ppat, 3, GEN8_PPAT_UC);
3172 __alloc_ppat_entry(ppat, 4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
3173 __alloc_ppat_entry(ppat, 5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
3174 __alloc_ppat_entry(ppat, 6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
3175 __alloc_ppat_entry(ppat, 7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3176 }
3177
3178 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3179 * bits. When using advanced contexts each context stores its own PAT, but
3180 * writing this data shouldn't be harmful even in those cases. */
3181 static void bdw_setup_private_ppat(struct intel_ppat *ppat)
3182 {
3183 ppat->max_entries = 8;
3184 ppat->update_hw = bdw_private_pat_update_hw;
3185 ppat->match = bdw_private_pat_match;
3186 ppat->clear_value = GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3);
3187
3188 if (!USES_PPGTT(ppat->i915)) {
3189 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3190 * so RTL will always use the value corresponding to
3191 * pat_sel = 000".
3192 * So let's disable cache for GGTT to avoid screen corruptions.
3193 * MOCS still can be used though.
3194 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3195 * before this patch, i.e. the same uncached + snooping access
3196 * like on gen6/7 seems to be in effect.
3197 * - So this just fixes blitter/render access. Again it looks
3198 * like it's not just uncached access, but uncached + snooping.
3199 * So we can still hold onto all our assumptions wrt cpu
3200 * clflushing on LLC machines.
3201 */
3202 __alloc_ppat_entry(ppat, 0, GEN8_PPAT_UC);
3203 return;
3204 }
3205
3206 __alloc_ppat_entry(ppat, 0, GEN8_PPAT_WB | GEN8_PPAT_LLC); /* for normal objects, no eLLC */
3207 __alloc_ppat_entry(ppat, 1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC); /* for something pointing to ptes? */
3208 __alloc_ppat_entry(ppat, 2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC); /* for scanout with eLLC */
3209 __alloc_ppat_entry(ppat, 3, GEN8_PPAT_UC); /* Uncached objects, mostly for scanout */
3210 __alloc_ppat_entry(ppat, 4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
3211 __alloc_ppat_entry(ppat, 5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
3212 __alloc_ppat_entry(ppat, 6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
3213 __alloc_ppat_entry(ppat, 7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3214 }
3215
3216 static void chv_setup_private_ppat(struct intel_ppat *ppat)
3217 {
3218 ppat->max_entries = 8;
3219 ppat->update_hw = bdw_private_pat_update_hw;
3220 ppat->match = chv_private_pat_match;
3221 ppat->clear_value = CHV_PPAT_SNOOP;
3222
3223 /*
3224 * Map WB on BDW to snooped on CHV.
3225 *
3226 * Only the snoop bit has meaning for CHV, the rest is
3227 * ignored.
3228 *
3229 * The hardware will never snoop for certain types of accesses:
3230 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3231 * - PPGTT page tables
3232 * - some other special cycles
3233 *
3234 * As with BDW, we also need to consider the following for GT accesses:
3235 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3236 * so RTL will always use the value corresponding to
3237 * pat_sel = 000".
3238 * Which means we must set the snoop bit in PAT entry 0
3239 * in order to keep the global status page working.
3240 */
3241
3242 __alloc_ppat_entry(ppat, 0, CHV_PPAT_SNOOP);
3243 __alloc_ppat_entry(ppat, 1, 0);
3244 __alloc_ppat_entry(ppat, 2, 0);
3245 __alloc_ppat_entry(ppat, 3, 0);
3246 __alloc_ppat_entry(ppat, 4, CHV_PPAT_SNOOP);
3247 __alloc_ppat_entry(ppat, 5, CHV_PPAT_SNOOP);
3248 __alloc_ppat_entry(ppat, 6, CHV_PPAT_SNOOP);
3249 __alloc_ppat_entry(ppat, 7, CHV_PPAT_SNOOP);
3250 }
3251
3252 static void gen6_gmch_remove(struct i915_address_space *vm)
3253 {
3254 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3255
3256 iounmap(ggtt->gsm);
3257 cleanup_scratch_page(vm);
3258 }
3259
3260 static void setup_private_pat(struct drm_i915_private *dev_priv)
3261 {
3262 struct intel_ppat *ppat = &dev_priv->ppat;
3263 int i;
3264
3265 ppat->i915 = dev_priv;
3266
3267 if (INTEL_GEN(dev_priv) >= 10)
3268 cnl_setup_private_ppat(ppat);
3269 else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
3270 chv_setup_private_ppat(ppat);
3271 else
3272 bdw_setup_private_ppat(ppat);
3273
3274 GEM_BUG_ON(ppat->max_entries > INTEL_MAX_PPAT_ENTRIES);
3275
3276 for_each_clear_bit(i, ppat->used, ppat->max_entries) {
3277 ppat->entries[i].value = ppat->clear_value;
3278 ppat->entries[i].ppat = ppat;
3279 set_bit(i, ppat->dirty);
3280 }
3281
3282 ppat->update_hw(dev_priv);
3283 }
3284
3285 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
3286 {
3287 struct drm_i915_private *dev_priv = ggtt->vm.i915;
3288 struct pci_dev *pdev = dev_priv->drm.pdev;
3289 unsigned int size;
3290 u16 snb_gmch_ctl;
3291 int err;
3292
3293 /* TODO: We're not aware of mappable constraints on gen8 yet */
3294 ggtt->gmadr =
3295 (struct resource) DEFINE_RES_MEM(pci_resource_start(pdev, 2),
3296 pci_resource_len(pdev, 2));
3297 ggtt->mappable_end = resource_size(&ggtt->gmadr);
3298
3299 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
3300 if (!err)
3301 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
3302 if (err)
3303 DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
3304
3305 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3306 if (IS_CHERRYVIEW(dev_priv))
3307 size = chv_get_total_gtt_size(snb_gmch_ctl);
3308 else
3309 size = gen8_get_total_gtt_size(snb_gmch_ctl);
3310
3311 ggtt->vm.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3312 ggtt->vm.cleanup = gen6_gmch_remove;
3313 ggtt->vm.bind_vma = ggtt_bind_vma;
3314 ggtt->vm.unbind_vma = ggtt_unbind_vma;
3315 ggtt->vm.set_pages = ggtt_set_pages;
3316 ggtt->vm.clear_pages = clear_pages;
3317 ggtt->vm.insert_page = gen8_ggtt_insert_page;
3318 ggtt->vm.clear_range = nop_clear_range;
3319 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
3320 ggtt->vm.clear_range = gen8_ggtt_clear_range;
3321
3322 ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
3323
3324 /* Serialize GTT updates with aperture access on BXT if VT-d is on. */
3325 if (intel_ggtt_update_needs_vtd_wa(dev_priv)) {
3326 ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
3327 ggtt->vm.insert_page = bxt_vtd_ggtt_insert_page__BKL;
3328 if (ggtt->vm.clear_range != nop_clear_range)
3329 ggtt->vm.clear_range = bxt_vtd_ggtt_clear_range__BKL;
3330 }
3331
3332 ggtt->invalidate = gen6_ggtt_invalidate;
3333
3334 setup_private_pat(dev_priv);
3335
3336 return ggtt_probe_common(ggtt, size);
3337 }
3338
3339 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
3340 {
3341 struct drm_i915_private *dev_priv = ggtt->vm.i915;
3342 struct pci_dev *pdev = dev_priv->drm.pdev;
3343 unsigned int size;
3344 u16 snb_gmch_ctl;
3345 int err;
3346
3347 ggtt->gmadr =
3348 (struct resource) DEFINE_RES_MEM(pci_resource_start(pdev, 2),
3349 pci_resource_len(pdev, 2));
3350 ggtt->mappable_end = resource_size(&ggtt->gmadr);
3351
3352 /* 64/512MB is the current min/max we actually know of, but this is just
3353 * a coarse sanity check.
3354 */
3355 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
3356 DRM_ERROR("Unknown GMADR size (%pa)\n", &ggtt->mappable_end);
3357 return -ENXIO;
3358 }
3359
3360 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
3361 if (!err)
3362 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3363 if (err)
3364 DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
3365 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3366
3367 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3368 ggtt->vm.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3369
3370 ggtt->vm.clear_range = gen6_ggtt_clear_range;
3371 ggtt->vm.insert_page = gen6_ggtt_insert_page;
3372 ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
3373 ggtt->vm.bind_vma = ggtt_bind_vma;
3374 ggtt->vm.unbind_vma = ggtt_unbind_vma;
3375 ggtt->vm.set_pages = ggtt_set_pages;
3376 ggtt->vm.clear_pages = clear_pages;
3377 ggtt->vm.cleanup = gen6_gmch_remove;
3378
3379 ggtt->invalidate = gen6_ggtt_invalidate;
3380
3381 if (HAS_EDRAM(dev_priv))
3382 ggtt->vm.pte_encode = iris_pte_encode;
3383 else if (IS_HASWELL(dev_priv))
3384 ggtt->vm.pte_encode = hsw_pte_encode;
3385 else if (IS_VALLEYVIEW(dev_priv))
3386 ggtt->vm.pte_encode = byt_pte_encode;
3387 else if (INTEL_GEN(dev_priv) >= 7)
3388 ggtt->vm.pte_encode = ivb_pte_encode;
3389 else
3390 ggtt->vm.pte_encode = snb_pte_encode;
3391
3392 return ggtt_probe_common(ggtt, size);
3393 }
3394
3395 static void i915_gmch_remove(struct i915_address_space *vm)
3396 {
3397 intel_gmch_remove();
3398 }
3399
3400 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3401 {
3402 struct drm_i915_private *dev_priv = ggtt->vm.i915;
3403 phys_addr_t gmadr_base;
3404 int ret;
3405
3406 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
3407 if (!ret) {
3408 DRM_ERROR("failed to set up gmch\n");
3409 return -EIO;
3410 }
3411
3412 intel_gtt_get(&ggtt->vm.total, &gmadr_base, &ggtt->mappable_end);
3413
3414 ggtt->gmadr =
3415 (struct resource) DEFINE_RES_MEM(gmadr_base,
3416 ggtt->mappable_end);
3417
3418 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
3419 ggtt->vm.insert_page = i915_ggtt_insert_page;
3420 ggtt->vm.insert_entries = i915_ggtt_insert_entries;
3421 ggtt->vm.clear_range = i915_ggtt_clear_range;
3422 ggtt->vm.bind_vma = ggtt_bind_vma;
3423 ggtt->vm.unbind_vma = ggtt_unbind_vma;
3424 ggtt->vm.set_pages = ggtt_set_pages;
3425 ggtt->vm.clear_pages = clear_pages;
3426 ggtt->vm.cleanup = i915_gmch_remove;
3427
3428 ggtt->invalidate = gmch_ggtt_invalidate;
3429
3430 if (unlikely(ggtt->do_idle_maps))
3431 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3432
3433 return 0;
3434 }
3435
3436 /**
3437 * i915_ggtt_probe_hw - Probe GGTT hardware location
3438 * @dev_priv: i915 device
3439 */
3440 int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
3441 {
3442 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3443 int ret;
3444
3445 ggtt->vm.i915 = dev_priv;
3446 ggtt->vm.dma = &dev_priv->drm.pdev->dev;
3447
3448 if (INTEL_GEN(dev_priv) <= 5)
3449 ret = i915_gmch_probe(ggtt);
3450 else if (INTEL_GEN(dev_priv) < 8)
3451 ret = gen6_gmch_probe(ggtt);
3452 else
3453 ret = gen8_gmch_probe(ggtt);
3454 if (ret)
3455 return ret;
3456
3457 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3458 * This is easier than doing range restriction on the fly, as we
3459 * currently don't have any bits spare to pass in this upper
3460 * restriction!
3461 */
3462 if (USES_GUC(dev_priv)) {
3463 ggtt->vm.total = min_t(u64, ggtt->vm.total, GUC_GGTT_TOP);
3464 ggtt->mappable_end =
3465 min_t(u64, ggtt->mappable_end, ggtt->vm.total);
3466 }
3467
3468 if ((ggtt->vm.total - 1) >> 32) {
3469 DRM_ERROR("We never expected a Global GTT with more than 32bits"
3470 " of address space! Found %lldM!\n",
3471 ggtt->vm.total >> 20);
3472 ggtt->vm.total = 1ULL << 32;
3473 ggtt->mappable_end =
3474 min_t(u64, ggtt->mappable_end, ggtt->vm.total);
3475 }
3476
3477 if (ggtt->mappable_end > ggtt->vm.total) {
3478 DRM_ERROR("mappable aperture extends past end of GGTT,"
3479 " aperture=%pa, total=%llx\n",
3480 &ggtt->mappable_end, ggtt->vm.total);
3481 ggtt->mappable_end = ggtt->vm.total;
3482 }
3483
3484 /* GMADR is the PCI mmio aperture into the global GTT. */
3485 DRM_DEBUG_DRIVER("GGTT size = %lluM\n", ggtt->vm.total >> 20);
3486 DRM_DEBUG_DRIVER("GMADR size = %lluM\n", (u64)ggtt->mappable_end >> 20);
3487 DRM_DEBUG_DRIVER("DSM size = %lluM\n",
3488 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
3489 if (intel_vtd_active())
3490 DRM_INFO("VT-d active for gfx access\n");
3491
3492 return 0;
3493 }
3494
3495 /**
3496 * i915_ggtt_init_hw - Initialize GGTT hardware
3497 * @dev_priv: i915 device
3498 */
3499 int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
3500 {
3501 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3502 int ret;
3503
3504 INIT_LIST_HEAD(&dev_priv->vm_list);
3505
3506 /* Note that we use page colouring to enforce a guard page at the
3507 * end of the address space. This is required as the CS may prefetch
3508 * beyond the end of the batch buffer, across the page boundary,
3509 * and beyond the end of the GTT if we do not provide a guard.
3510 */
3511 mutex_lock(&dev_priv->drm.struct_mutex);
3512 i915_address_space_init(&ggtt->vm, dev_priv, "[global]");
3513 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
3514 ggtt->vm.mm.color_adjust = i915_gtt_color_adjust;
3515 mutex_unlock(&dev_priv->drm.struct_mutex);
3516
3517 if (!io_mapping_init_wc(&dev_priv->ggtt.iomap,
3518 dev_priv->ggtt.gmadr.start,
3519 dev_priv->ggtt.mappable_end)) {
3520 ret = -EIO;
3521 goto out_gtt_cleanup;
3522 }
3523
3524 ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start, ggtt->mappable_end);
3525
3526 /*
3527 * Initialise stolen early so that we may reserve preallocated
3528 * objects for the BIOS to KMS transition.
3529 */
3530 ret = i915_gem_init_stolen(dev_priv);
3531 if (ret)
3532 goto out_gtt_cleanup;
3533
3534 return 0;
3535
3536 out_gtt_cleanup:
3537 ggtt->vm.cleanup(&ggtt->vm);
3538 return ret;
3539 }
3540
3541 int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
3542 {
3543 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
3544 return -EIO;
3545
3546 return 0;
3547 }
3548
3549 void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3550 {
3551 GEM_BUG_ON(i915->ggtt.invalidate != gen6_ggtt_invalidate);
3552
3553 i915->ggtt.invalidate = guc_ggtt_invalidate;
3554
3555 i915_ggtt_invalidate(i915);
3556 }
3557
3558 void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3559 {
3560 /* We should only be called after i915_ggtt_enable_guc() */
3561 GEM_BUG_ON(i915->ggtt.invalidate != guc_ggtt_invalidate);
3562
3563 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3564
3565 i915_ggtt_invalidate(i915);
3566 }
3567
3568 void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
3569 {
3570 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3571 struct i915_vma *vma, *vn;
3572
3573 i915_check_and_clear_faults(dev_priv);
3574
3575 /* First fill our portion of the GTT with scratch pages */
3576 ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
3577
3578 ggtt->vm.closed = true; /* skip rewriting PTE on VMA unbind */
3579
3580 /* clflush objects bound into the GGTT and rebind them. */
3581 GEM_BUG_ON(!list_empty(&ggtt->vm.active_list));
3582 list_for_each_entry_safe(vma, vn, &ggtt->vm.inactive_list, vm_link) {
3583 struct drm_i915_gem_object *obj = vma->obj;
3584
3585 if (!(vma->flags & I915_VMA_GLOBAL_BIND))
3586 continue;
3587
3588 if (!i915_vma_unbind(vma))
3589 continue;
3590
3591 WARN_ON(i915_vma_bind(vma, obj->cache_level, PIN_UPDATE));
3592 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
3593 }
3594
3595 ggtt->vm.closed = false;
3596
3597 if (INTEL_GEN(dev_priv) >= 8) {
3598 struct intel_ppat *ppat = &dev_priv->ppat;
3599
3600 bitmap_set(ppat->dirty, 0, ppat->max_entries);
3601 dev_priv->ppat.update_hw(dev_priv);
3602 return;
3603 }
3604
3605 if (USES_PPGTT(dev_priv)) {
3606 struct i915_address_space *vm;
3607
3608 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3609 struct i915_hw_ppgtt *ppgtt;
3610
3611 if (i915_is_ggtt(vm))
3612 ppgtt = dev_priv->mm.aliasing_ppgtt;
3613 else
3614 ppgtt = i915_vm_to_ppgtt(vm);
3615 if (!ppgtt)
3616 continue;
3617
3618 gen6_write_page_range(ppgtt, 0, ppgtt->vm.total);
3619 }
3620 }
3621
3622 i915_ggtt_invalidate(dev_priv);
3623 }
3624
3625 static struct scatterlist *
3626 rotate_pages(const dma_addr_t *in, unsigned int offset,
3627 unsigned int width, unsigned int height,
3628 unsigned int stride,
3629 struct sg_table *st, struct scatterlist *sg)
3630 {
3631 unsigned int column, row;
3632 unsigned int src_idx;
3633
3634 for (column = 0; column < width; column++) {
3635 src_idx = stride * (height - 1) + column;
3636 for (row = 0; row < height; row++) {
3637 st->nents++;
3638 /* We don't need the pages, but need to initialize
3639 * the entries so the sg list can be happily traversed.
3640 * The only thing we need are DMA addresses.
3641 */
3642 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3643 sg_dma_address(sg) = in[offset + src_idx];
3644 sg_dma_len(sg) = PAGE_SIZE;
3645 sg = sg_next(sg);
3646 src_idx -= stride;
3647 }
3648 }
3649
3650 return sg;
3651 }
3652
3653 static noinline struct sg_table *
3654 intel_rotate_pages(struct intel_rotation_info *rot_info,
3655 struct drm_i915_gem_object *obj)
3656 {
3657 const unsigned long n_pages = obj->base.size / PAGE_SIZE;
3658 unsigned int size = intel_rotation_info_size(rot_info);
3659 struct sgt_iter sgt_iter;
3660 dma_addr_t dma_addr;
3661 unsigned long i;
3662 dma_addr_t *page_addr_list;
3663 struct sg_table *st;
3664 struct scatterlist *sg;
3665 int ret = -ENOMEM;
3666
3667 /* Allocate a temporary list of source pages for random access. */
3668 page_addr_list = kvmalloc_array(n_pages,
3669 sizeof(dma_addr_t),
3670 GFP_KERNEL);
3671 if (!page_addr_list)
3672 return ERR_PTR(ret);
3673
3674 /* Allocate target SG list. */
3675 st = kmalloc(sizeof(*st), GFP_KERNEL);
3676 if (!st)
3677 goto err_st_alloc;
3678
3679 ret = sg_alloc_table(st, size, GFP_KERNEL);
3680 if (ret)
3681 goto err_sg_alloc;
3682
3683 /* Populate source page list from the object. */
3684 i = 0;
3685 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
3686 page_addr_list[i++] = dma_addr;
3687
3688 GEM_BUG_ON(i != n_pages);
3689 st->nents = 0;
3690 sg = st->sgl;
3691
3692 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3693 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3694 rot_info->plane[i].width, rot_info->plane[i].height,
3695 rot_info->plane[i].stride, st, sg);
3696 }
3697
3698 kvfree(page_addr_list);
3699
3700 return st;
3701
3702 err_sg_alloc:
3703 kfree(st);
3704 err_st_alloc:
3705 kvfree(page_addr_list);
3706
3707 DRM_DEBUG_DRIVER("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3708 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3709
3710 return ERR_PTR(ret);
3711 }
3712
3713 static noinline struct sg_table *
3714 intel_partial_pages(const struct i915_ggtt_view *view,
3715 struct drm_i915_gem_object *obj)
3716 {
3717 struct sg_table *st;
3718 struct scatterlist *sg, *iter;
3719 unsigned int count = view->partial.size;
3720 unsigned int offset;
3721 int ret = -ENOMEM;
3722
3723 st = kmalloc(sizeof(*st), GFP_KERNEL);
3724 if (!st)
3725 goto err_st_alloc;
3726
3727 ret = sg_alloc_table(st, count, GFP_KERNEL);
3728 if (ret)
3729 goto err_sg_alloc;
3730
3731 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
3732 GEM_BUG_ON(!iter);
3733
3734 sg = st->sgl;
3735 st->nents = 0;
3736 do {
3737 unsigned int len;
3738
3739 len = min(iter->length - (offset << PAGE_SHIFT),
3740 count << PAGE_SHIFT);
3741 sg_set_page(sg, NULL, len, 0);
3742 sg_dma_address(sg) =
3743 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3744 sg_dma_len(sg) = len;
3745
3746 st->nents++;
3747 count -= len >> PAGE_SHIFT;
3748 if (count == 0) {
3749 sg_mark_end(sg);
3750 return st;
3751 }
3752
3753 sg = __sg_next(sg);
3754 iter = __sg_next(iter);
3755 offset = 0;
3756 } while (1);
3757
3758 err_sg_alloc:
3759 kfree(st);
3760 err_st_alloc:
3761 return ERR_PTR(ret);
3762 }
3763
3764 static int
3765 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3766 {
3767 int ret;
3768
3769 /* The vma->pages are only valid within the lifespan of the borrowed
3770 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3771 * must be the vma->pages. A simple rule is that vma->pages must only
3772 * be accessed when the obj->mm.pages are pinned.
3773 */
3774 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3775
3776 switch (vma->ggtt_view.type) {
3777 default:
3778 GEM_BUG_ON(vma->ggtt_view.type);
3779 /* fall through */
3780 case I915_GGTT_VIEW_NORMAL:
3781 vma->pages = vma->obj->mm.pages;
3782 return 0;
3783
3784 case I915_GGTT_VIEW_ROTATED:
3785 vma->pages =
3786 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3787 break;
3788
3789 case I915_GGTT_VIEW_PARTIAL:
3790 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
3791 break;
3792 }
3793
3794 ret = 0;
3795 if (unlikely(IS_ERR(vma->pages))) {
3796 ret = PTR_ERR(vma->pages);
3797 vma->pages = NULL;
3798 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3799 vma->ggtt_view.type, ret);
3800 }
3801 return ret;
3802 }
3803
3804 /**
3805 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
3806 * @vm: the &struct i915_address_space
3807 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3808 * @size: how much space to allocate inside the GTT,
3809 * must be #I915_GTT_PAGE_SIZE aligned
3810 * @offset: where to insert inside the GTT,
3811 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3812 * (@offset + @size) must fit within the address space
3813 * @color: color to apply to node, if this node is not from a VMA,
3814 * color must be #I915_COLOR_UNEVICTABLE
3815 * @flags: control search and eviction behaviour
3816 *
3817 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3818 * the address space (using @size and @color). If the @node does not fit, it
3819 * tries to evict any overlapping nodes from the GTT, including any
3820 * neighbouring nodes if the colors do not match (to ensure guard pages between
3821 * differing domains). See i915_gem_evict_for_node() for the gory details
3822 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3823 * evicting active overlapping objects, and any overlapping node that is pinned
3824 * or marked as unevictable will also result in failure.
3825 *
3826 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3827 * asked to wait for eviction and interrupted.
3828 */
3829 int i915_gem_gtt_reserve(struct i915_address_space *vm,
3830 struct drm_mm_node *node,
3831 u64 size, u64 offset, unsigned long color,
3832 unsigned int flags)
3833 {
3834 int err;
3835
3836 GEM_BUG_ON(!size);
3837 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3838 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3839 GEM_BUG_ON(range_overflows(offset, size, vm->total));
3840 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
3841 GEM_BUG_ON(drm_mm_node_allocated(node));
3842
3843 node->size = size;
3844 node->start = offset;
3845 node->color = color;
3846
3847 err = drm_mm_reserve_node(&vm->mm, node);
3848 if (err != -ENOSPC)
3849 return err;
3850
3851 if (flags & PIN_NOEVICT)
3852 return -ENOSPC;
3853
3854 err = i915_gem_evict_for_node(vm, node, flags);
3855 if (err == 0)
3856 err = drm_mm_reserve_node(&vm->mm, node);
3857
3858 return err;
3859 }
3860
3861 static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3862 {
3863 u64 range, addr;
3864
3865 GEM_BUG_ON(range_overflows(start, len, end));
3866 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3867
3868 range = round_down(end - len, align) - round_up(start, align);
3869 if (range) {
3870 if (sizeof(unsigned long) == sizeof(u64)) {
3871 addr = get_random_long();
3872 } else {
3873 addr = get_random_int();
3874 if (range > U32_MAX) {
3875 addr <<= 32;
3876 addr |= get_random_int();
3877 }
3878 }
3879 div64_u64_rem(addr, range, &addr);
3880 start += addr;
3881 }
3882
3883 return round_up(start, align);
3884 }
3885
3886 /**
3887 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
3888 * @vm: the &struct i915_address_space
3889 * @node: the &struct drm_mm_node (typically i915_vma.node)
3890 * @size: how much space to allocate inside the GTT,
3891 * must be #I915_GTT_PAGE_SIZE aligned
3892 * @alignment: required alignment of starting offset, may be 0 but
3893 * if specified, this must be a power-of-two and at least
3894 * #I915_GTT_MIN_ALIGNMENT
3895 * @color: color to apply to node
3896 * @start: start of any range restriction inside GTT (0 for all),
3897 * must be #I915_GTT_PAGE_SIZE aligned
3898 * @end: end of any range restriction inside GTT (U64_MAX for all),
3899 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3900 * @flags: control search and eviction behaviour
3901 *
3902 * i915_gem_gtt_insert() first searches for an available hole into which
3903 * is can insert the node. The hole address is aligned to @alignment and
3904 * its @size must then fit entirely within the [@start, @end] bounds. The
3905 * nodes on either side of the hole must match @color, or else a guard page
3906 * will be inserted between the two nodes (or the node evicted). If no
3907 * suitable hole is found, first a victim is randomly selected and tested
3908 * for eviction, otherwise then the LRU list of objects within the GTT
3909 * is scanned to find the first set of replacement nodes to create the hole.
3910 * Those old overlapping nodes are evicted from the GTT (and so must be
3911 * rebound before any future use). Any node that is currently pinned cannot
3912 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3913 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3914 * searching for an eviction candidate. See i915_gem_evict_something() for
3915 * the gory details on the eviction algorithm.
3916 *
3917 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3918 * asked to wait for eviction and interrupted.
3919 */
3920 int i915_gem_gtt_insert(struct i915_address_space *vm,
3921 struct drm_mm_node *node,
3922 u64 size, u64 alignment, unsigned long color,
3923 u64 start, u64 end, unsigned int flags)
3924 {
3925 enum drm_mm_insert_mode mode;
3926 u64 offset;
3927 int err;
3928
3929 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3930 GEM_BUG_ON(!size);
3931 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3932 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3933 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3934 GEM_BUG_ON(start >= end);
3935 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3936 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3937 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->vm);
3938 GEM_BUG_ON(drm_mm_node_allocated(node));
3939
3940 if (unlikely(range_overflows(start, size, end)))
3941 return -ENOSPC;
3942
3943 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3944 return -ENOSPC;
3945
3946 mode = DRM_MM_INSERT_BEST;
3947 if (flags & PIN_HIGH)
3948 mode = DRM_MM_INSERT_HIGHEST;
3949 if (flags & PIN_MAPPABLE)
3950 mode = DRM_MM_INSERT_LOW;
3951
3952 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3953 * so we know that we always have a minimum alignment of 4096.
3954 * The drm_mm range manager is optimised to return results
3955 * with zero alignment, so where possible use the optimal
3956 * path.
3957 */
3958 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3959 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3960 alignment = 0;
3961
3962 err = drm_mm_insert_node_in_range(&vm->mm, node,
3963 size, alignment, color,
3964 start, end, mode);
3965 if (err != -ENOSPC)
3966 return err;
3967
3968 if (mode & DRM_MM_INSERT_ONCE) {
3969 err = drm_mm_insert_node_in_range(&vm->mm, node,
3970 size, alignment, color,
3971 start, end,
3972 DRM_MM_INSERT_BEST);
3973 if (err != -ENOSPC)
3974 return err;
3975 }
3976
3977 if (flags & PIN_NOEVICT)
3978 return -ENOSPC;
3979
3980 /* No free space, pick a slot at random.
3981 *
3982 * There is a pathological case here using a GTT shared between
3983 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3984 *
3985 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3986 * (64k objects) (448k objects)
3987 *
3988 * Now imagine that the eviction LRU is ordered top-down (just because
3989 * pathology meets real life), and that we need to evict an object to
3990 * make room inside the aperture. The eviction scan then has to walk
3991 * the 448k list before it finds one within range. And now imagine that
3992 * it has to search for a new hole between every byte inside the memcpy,
3993 * for several simultaneous clients.
3994 *
3995 * On a full-ppgtt system, if we have run out of available space, there
3996 * will be lots and lots of objects in the eviction list! Again,
3997 * searching that LRU list may be slow if we are also applying any
3998 * range restrictions (e.g. restriction to low 4GiB) and so, for
3999 * simplicity and similarilty between different GTT, try the single
4000 * random replacement first.
4001 */
4002 offset = random_offset(start, end,
4003 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
4004 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
4005 if (err != -ENOSPC)
4006 return err;
4007
4008 /* Randomly selected placement is pinned, do a search */
4009 err = i915_gem_evict_something(vm, size, alignment, color,
4010 start, end, flags);
4011 if (err)
4012 return err;
4013
4014 return drm_mm_insert_node_in_range(&vm->mm, node,
4015 size, alignment, color,
4016 start, end, DRM_MM_INSERT_EVICT);
4017 }
4018
4019 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
4020 #include "selftests/mock_gtt.c"
4021 #include "selftests/i915_gem_gtt.c"
4022 #endif