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