2 * Copyright © 2010 Daniel Vetter
3 * Copyright © 2011-2014 Intel Corporation
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:
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
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
26 #include <linux/slab.h> /* fault-inject.h is not standalone! */
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>
35 #include <drm/i915_drm.h>
38 #include "i915_vgpu.h"
39 #include "i915_trace.h"
40 #include "intel_drv.h"
41 #include "intel_frontbuffer.h"
43 #define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
46 * DOC: Global GTT views
48 * Background and previous state
50 * Historically objects could exists (be bound) in global GTT space only as
51 * singular instances with a view representing all of the object's backing pages
52 * in a linear fashion. This view will be called a normal view.
54 * To support multiple views of the same object, where the number of mapped
55 * pages is not equal to the backing store, or where the layout of the pages
56 * is not linear, concept of a GGTT view was added.
58 * One example of an alternative view is a stereo display driven by a single
59 * image. In this case we would have a framebuffer looking like this
65 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
66 * rendering. In contrast, fed to the display engine would be an alternative
67 * view which could look something like this:
72 * In this example both the size and layout of pages in the alternative view is
73 * different from the normal view.
75 * Implementation and usage
77 * GGTT views are implemented using VMAs and are distinguished via enum
78 * i915_ggtt_view_type and struct i915_ggtt_view.
80 * A new flavour of core GEM functions which work with GGTT bound objects were
81 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
82 * renaming in large amounts of code. They take the struct i915_ggtt_view
83 * parameter encapsulating all metadata required to implement a view.
85 * As a helper for callers which are only interested in the normal view,
86 * globally const i915_ggtt_view_normal singleton instance exists. All old core
87 * GEM API functions, the ones not taking the view parameter, are operating on,
88 * or with the normal GGTT view.
90 * Code wanting to add or use a new GGTT view needs to:
92 * 1. Add a new enum with a suitable name.
93 * 2. Extend the metadata in the i915_ggtt_view structure if required.
94 * 3. Add support to i915_get_vma_pages().
96 * New views are required to build a scatter-gather table from within the
97 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
98 * exists for the lifetime of an VMA.
100 * Core API is designed to have copy semantics which means that passed in
101 * struct i915_ggtt_view does not need to be persistent (left around after
102 * calling the core API functions).
107 i915_get_ggtt_vma_pages(struct i915_vma *vma);
109 static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
111 /* Note that as an uncached mmio write, this should flush the
112 * WCB of the writes into the GGTT before it triggers the invalidate.
114 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
117 static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
119 gen6_ggtt_invalidate(dev_priv);
120 I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
123 static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
125 intel_gtt_chipset_flush();
128 static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
130 i915->ggtt.invalidate(i915);
133 int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
136 bool has_aliasing_ppgtt;
138 bool has_full_48bit_ppgtt;
140 has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
141 has_full_ppgtt = dev_priv->info.has_full_ppgtt;
142 has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
144 if (intel_vgpu_active(dev_priv)) {
145 /* emulation is too hard */
146 has_full_ppgtt = false;
147 has_full_48bit_ppgtt = false;
150 if (!has_aliasing_ppgtt)
154 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
155 * execlists, the sole mechanism available to submit work.
157 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
160 if (enable_ppgtt == 1)
163 if (enable_ppgtt == 2 && has_full_ppgtt)
166 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
169 #ifdef CONFIG_INTEL_IOMMU
170 /* Disable ppgtt on SNB if VT-d is on. */
171 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
172 DRM_INFO("Disabling PPGTT because VT-d is on\n");
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");
183 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
184 return has_full_48bit_ppgtt ? 3 : 2;
186 return has_aliasing_ppgtt ? 1 : 0;
189 static int ppgtt_bind_vma(struct i915_vma *vma,
190 enum i915_cache_level cache_level,
195 vma->pages = vma->obj->mm.pages;
197 /* Currently applicable only to VLV */
199 pte_flags |= PTE_READ_ONLY;
201 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
202 cache_level, pte_flags);
207 static void ppgtt_unbind_vma(struct i915_vma *vma)
209 vma->vm->clear_range(vma->vm,
214 static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
215 enum i915_cache_level level)
217 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
221 case I915_CACHE_NONE:
222 pte |= PPAT_UNCACHED_INDEX;
225 pte |= PPAT_DISPLAY_ELLC_INDEX;
228 pte |= PPAT_CACHED_INDEX;
235 static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
236 const enum i915_cache_level level)
238 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
240 if (level != I915_CACHE_NONE)
241 pde |= PPAT_CACHED_PDE_INDEX;
243 pde |= PPAT_UNCACHED_INDEX;
247 #define gen8_pdpe_encode gen8_pde_encode
248 #define gen8_pml4e_encode gen8_pde_encode
250 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
251 enum i915_cache_level level,
254 gen6_pte_t pte = GEN6_PTE_VALID;
255 pte |= GEN6_PTE_ADDR_ENCODE(addr);
258 case I915_CACHE_L3_LLC:
260 pte |= GEN6_PTE_CACHE_LLC;
262 case I915_CACHE_NONE:
263 pte |= GEN6_PTE_UNCACHED;
272 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
273 enum i915_cache_level level,
276 gen6_pte_t pte = GEN6_PTE_VALID;
277 pte |= GEN6_PTE_ADDR_ENCODE(addr);
280 case I915_CACHE_L3_LLC:
281 pte |= GEN7_PTE_CACHE_L3_LLC;
284 pte |= GEN6_PTE_CACHE_LLC;
286 case I915_CACHE_NONE:
287 pte |= GEN6_PTE_UNCACHED;
296 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
297 enum i915_cache_level level,
300 gen6_pte_t pte = GEN6_PTE_VALID;
301 pte |= GEN6_PTE_ADDR_ENCODE(addr);
303 if (!(flags & PTE_READ_ONLY))
304 pte |= BYT_PTE_WRITEABLE;
306 if (level != I915_CACHE_NONE)
307 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
312 static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
313 enum i915_cache_level level,
316 gen6_pte_t pte = GEN6_PTE_VALID;
317 pte |= HSW_PTE_ADDR_ENCODE(addr);
319 if (level != I915_CACHE_NONE)
320 pte |= HSW_WB_LLC_AGE3;
325 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
326 enum i915_cache_level level,
329 gen6_pte_t pte = GEN6_PTE_VALID;
330 pte |= HSW_PTE_ADDR_ENCODE(addr);
333 case I915_CACHE_NONE:
336 pte |= HSW_WT_ELLC_LLC_AGE3;
339 pte |= HSW_WB_ELLC_LLC_AGE3;
346 static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
350 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
351 i915_gem_shrink_all(vm->i915);
353 if (vm->free_pages.nr)
354 return vm->free_pages.pages[--vm->free_pages.nr];
356 page = alloc_page(gfp);
361 set_pages_array_wc(&page, 1);
366 static void vm_free_pages_release(struct i915_address_space *vm)
368 GEM_BUG_ON(!pagevec_count(&vm->free_pages));
371 set_pages_array_wb(vm->free_pages.pages,
372 pagevec_count(&vm->free_pages));
374 __pagevec_release(&vm->free_pages);
377 static void vm_free_page(struct i915_address_space *vm, struct page *page)
379 if (!pagevec_add(&vm->free_pages, page))
380 vm_free_pages_release(vm);
383 static int __setup_page_dma(struct i915_address_space *vm,
384 struct i915_page_dma *p,
387 p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
388 if (unlikely(!p->page))
391 p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
392 PCI_DMA_BIDIRECTIONAL);
393 if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
394 vm_free_page(vm, p->page);
401 static int setup_page_dma(struct i915_address_space *vm,
402 struct i915_page_dma *p)
404 return __setup_page_dma(vm, p, I915_GFP_DMA);
407 static void cleanup_page_dma(struct i915_address_space *vm,
408 struct i915_page_dma *p)
410 dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
411 vm_free_page(vm, p->page);
414 static void *kmap_page_dma(struct i915_page_dma *p)
416 return kmap_atomic(p->page);
419 /* We use the flushing unmap only with ppgtt structures:
420 * page directories, page tables and scratch pages.
422 static void kunmap_page_dma(void *vaddr)
424 kunmap_atomic(vaddr);
427 #define kmap_px(px) kmap_page_dma(px_base(px))
428 #define kunmap_px(vaddr) kunmap_page_dma((vaddr))
430 #define setup_px(vm, px) setup_page_dma((vm), px_base(px))
431 #define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
432 #define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
433 #define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
435 static void fill_page_dma(struct i915_address_space *vm,
436 struct i915_page_dma *p,
439 u64 * const vaddr = kmap_page_dma(p);
442 for (i = 0; i < 512; i++)
445 kunmap_page_dma(vaddr);
448 static void fill_page_dma_32(struct i915_address_space *vm,
449 struct i915_page_dma *p,
452 fill_page_dma(vm, p, (u64)v << 32 | v);
456 setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
458 return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
461 static void cleanup_scratch_page(struct i915_address_space *vm)
463 cleanup_page_dma(vm, &vm->scratch_page);
466 static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
468 struct i915_page_table *pt;
469 const size_t count = INTEL_GEN(vm->i915) >= 8 ? GEN8_PTES : GEN6_PTES;
472 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
474 return ERR_PTR(-ENOMEM);
476 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
482 ret = setup_px(vm, pt);
489 kfree(pt->used_ptes);
496 static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
499 kfree(pt->used_ptes);
503 static void gen8_initialize_pt(struct i915_address_space *vm,
504 struct i915_page_table *pt)
506 gen8_pte_t scratch_pte;
508 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
511 fill_px(vm, pt, scratch_pte);
514 static void gen6_initialize_pt(struct i915_address_space *vm,
515 struct i915_page_table *pt)
517 gen6_pte_t scratch_pte;
519 WARN_ON(vm->scratch_page.daddr == 0);
521 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
524 fill32_px(vm, pt, scratch_pte);
527 static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
529 struct i915_page_directory *pd;
532 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
534 return ERR_PTR(-ENOMEM);
536 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
537 sizeof(*pd->used_pdes), GFP_KERNEL);
541 ret = setup_px(vm, pd);
548 kfree(pd->used_pdes);
555 static void free_pd(struct i915_address_space *vm,
556 struct i915_page_directory *pd)
560 kfree(pd->used_pdes);
565 static void gen8_initialize_pd(struct i915_address_space *vm,
566 struct i915_page_directory *pd)
568 gen8_pde_t scratch_pde;
570 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
572 fill_px(vm, pd, scratch_pde);
575 static int __pdp_init(struct drm_i915_private *dev_priv,
576 struct i915_page_directory_pointer *pdp)
578 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
580 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
581 sizeof(unsigned long),
583 if (!pdp->used_pdpes)
586 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
588 if (!pdp->page_directory) {
589 kfree(pdp->used_pdpes);
590 /* the PDP might be the statically allocated top level. Keep it
591 * as clean as possible */
592 pdp->used_pdpes = NULL;
599 static void __pdp_fini(struct i915_page_directory_pointer *pdp)
601 kfree(pdp->used_pdpes);
602 kfree(pdp->page_directory);
603 pdp->page_directory = NULL;
606 static struct i915_page_directory_pointer *
607 alloc_pdp(struct i915_address_space *vm)
609 struct i915_page_directory_pointer *pdp;
612 WARN_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
614 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
616 return ERR_PTR(-ENOMEM);
618 ret = __pdp_init(vm->i915, pdp);
622 ret = setup_px(vm, pdp);
636 static void free_pdp(struct i915_address_space *vm,
637 struct i915_page_directory_pointer *pdp)
640 if (USES_FULL_48BIT_PPGTT(vm->i915)) {
646 static void gen8_initialize_pdp(struct i915_address_space *vm,
647 struct i915_page_directory_pointer *pdp)
649 gen8_ppgtt_pdpe_t scratch_pdpe;
651 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
653 fill_px(vm, pdp, scratch_pdpe);
656 static void gen8_initialize_pml4(struct i915_address_space *vm,
657 struct i915_pml4 *pml4)
659 gen8_ppgtt_pml4e_t scratch_pml4e;
661 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
664 fill_px(vm, pml4, scratch_pml4e);
668 gen8_setup_pdpe(struct i915_hw_ppgtt *ppgtt,
669 struct i915_page_directory_pointer *pdp,
670 struct i915_page_directory *pd,
673 gen8_ppgtt_pdpe_t *page_directorypo;
675 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
678 page_directorypo = kmap_px(pdp);
679 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
680 kunmap_px(page_directorypo);
684 gen8_setup_pml4e(struct i915_pml4 *pml4,
685 struct i915_page_directory_pointer *pdp,
688 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
690 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
694 /* Broadwell Page Directory Pointer Descriptors */
695 static int gen8_write_pdp(struct drm_i915_gem_request *req,
699 struct intel_engine_cs *engine = req->engine;
704 cs = intel_ring_begin(req, 6);
708 *cs++ = MI_LOAD_REGISTER_IMM(1);
709 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
710 *cs++ = upper_32_bits(addr);
711 *cs++ = MI_LOAD_REGISTER_IMM(1);
712 *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
713 *cs++ = lower_32_bits(addr);
714 intel_ring_advance(req, cs);
719 static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
720 struct drm_i915_gem_request *req)
724 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
725 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
727 ret = gen8_write_pdp(req, i, pd_daddr);
735 static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
736 struct drm_i915_gem_request *req)
738 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
741 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
742 * the page table structures, we mark them dirty so that
743 * context switching/execlist queuing code takes extra steps
744 * to ensure that tlbs are flushed.
746 static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
748 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
751 /* Removes entries from a single page table, releasing it if it's empty.
752 * Caller can use the return value to update higher-level entries.
754 static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
755 struct i915_page_table *pt,
759 unsigned int num_entries = gen8_pte_count(start, length);
760 unsigned int pte = gen8_pte_index(start);
761 unsigned int pte_end = pte + num_entries;
762 const gen8_pte_t scratch_pte =
763 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
766 if (WARN_ON(!px_page(pt)))
769 GEM_BUG_ON(pte_end > GEN8_PTES);
771 bitmap_clear(pt->used_ptes, pte, num_entries);
772 if (USES_FULL_PPGTT(vm->i915)) {
773 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
778 while (pte < pte_end)
779 vaddr[pte++] = scratch_pte;
785 /* Removes entries from a single page dir, releasing it if it's empty.
786 * Caller can use the return value to update higher-level entries
788 static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
789 struct i915_page_directory *pd,
793 struct i915_page_table *pt;
795 gen8_pde_t *pde_vaddr;
796 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
799 gen8_for_each_pde(pt, pd, start, length, pde) {
800 if (WARN_ON(!pd->page_table[pde]))
803 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
804 __clear_bit(pde, pd->used_pdes);
805 pde_vaddr = kmap_px(pd);
806 pde_vaddr[pde] = scratch_pde;
807 kunmap_px(pde_vaddr);
812 if (bitmap_empty(pd->used_pdes, I915_PDES))
818 /* Removes entries from a single page dir pointer, releasing it if it's empty.
819 * Caller can use the return value to update higher-level entries
821 static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
822 struct i915_page_directory_pointer *pdp,
826 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
827 struct i915_page_directory *pd;
830 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
831 if (WARN_ON(!pdp->page_directory[pdpe]))
834 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
835 __clear_bit(pdpe, pdp->used_pdpes);
836 gen8_setup_pdpe(ppgtt, pdp, vm->scratch_pd, pdpe);
841 mark_tlbs_dirty(ppgtt);
843 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
849 /* Removes entries from a single pml4.
850 * This is the top-level structure in 4-level page tables used on gen8+.
851 * Empty entries are always scratch pml4e.
853 static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
854 struct i915_pml4 *pml4,
858 struct i915_page_directory_pointer *pdp;
861 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(vm->i915));
863 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
864 if (WARN_ON(!pml4->pdps[pml4e]))
867 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
868 __clear_bit(pml4e, pml4->used_pml4es);
869 gen8_setup_pml4e(pml4, vm->scratch_pdp, pml4e);
875 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
876 uint64_t start, uint64_t length)
878 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
880 if (USES_FULL_48BIT_PPGTT(vm->i915))
881 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
883 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
887 struct scatterlist *sg;
891 static __always_inline bool
892 gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
893 struct i915_page_directory_pointer *pdp,
894 struct sgt_dma *iter,
896 enum i915_cache_level cache_level)
898 unsigned int pdpe = gen8_pdpe_index(start);
899 unsigned int pde = gen8_pde_index(start);
900 unsigned int pte = gen8_pte_index(start);
901 struct i915_page_directory *pd;
902 const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
906 pd = pdp->page_directory[pdpe];
907 vaddr = kmap_px(pd->page_table[pde]);
909 vaddr[pte] = pte_encode | iter->dma;
910 iter->dma += PAGE_SIZE;
911 if (iter->dma >= iter->max) {
912 iter->sg = __sg_next(iter->sg);
918 iter->dma = sg_dma_address(iter->sg);
919 iter->max = iter->dma + iter->sg->length;
922 if (++pte == GEN8_PTES) {
923 if (++pde == I915_PDES) {
924 /* Limited by sg length for 3lvl */
925 if (++pdpe == GEN8_PML4ES_PER_PML4) {
930 GEM_BUG_ON(pdpe > GEN8_LEGACY_PDPES);
931 pd = pdp->page_directory[pdpe];
936 vaddr = kmap_px(pd->page_table[pde]);
945 static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
946 struct sg_table *pages,
948 enum i915_cache_level cache_level,
951 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
952 struct sgt_dma iter = {
954 .dma = sg_dma_address(iter.sg),
955 .max = iter.dma + iter.sg->length,
958 gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter,
962 static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
963 struct sg_table *pages,
965 enum i915_cache_level cache_level,
968 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
969 struct sgt_dma iter = {
971 .dma = sg_dma_address(iter.sg),
972 .max = iter.dma + iter.sg->length,
974 struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
975 unsigned int pml4e = gen8_pml4e_index(start);
977 while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[pml4e++], &iter,
982 static void gen8_free_page_tables(struct i915_address_space *vm,
983 struct i915_page_directory *pd)
990 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
991 if (WARN_ON(!pd->page_table[i]))
994 free_pt(vm, pd->page_table[i]);
995 pd->page_table[i] = NULL;
999 static int gen8_init_scratch(struct i915_address_space *vm)
1003 ret = setup_scratch_page(vm, I915_GFP_DMA);
1007 vm->scratch_pt = alloc_pt(vm);
1008 if (IS_ERR(vm->scratch_pt)) {
1009 ret = PTR_ERR(vm->scratch_pt);
1010 goto free_scratch_page;
1013 vm->scratch_pd = alloc_pd(vm);
1014 if (IS_ERR(vm->scratch_pd)) {
1015 ret = PTR_ERR(vm->scratch_pd);
1019 if (USES_FULL_48BIT_PPGTT(dev)) {
1020 vm->scratch_pdp = alloc_pdp(vm);
1021 if (IS_ERR(vm->scratch_pdp)) {
1022 ret = PTR_ERR(vm->scratch_pdp);
1027 gen8_initialize_pt(vm, vm->scratch_pt);
1028 gen8_initialize_pd(vm, vm->scratch_pd);
1029 if (USES_FULL_48BIT_PPGTT(dev_priv))
1030 gen8_initialize_pdp(vm, vm->scratch_pdp);
1035 free_pd(vm, vm->scratch_pd);
1037 free_pt(vm, vm->scratch_pt);
1039 cleanup_scratch_page(vm);
1044 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1046 enum vgt_g2v_type msg;
1047 struct drm_i915_private *dev_priv = ppgtt->base.i915;
1050 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1051 u64 daddr = px_dma(&ppgtt->pml4);
1053 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1054 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
1056 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1057 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1059 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1060 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1062 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1063 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
1066 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1067 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1070 I915_WRITE(vgtif_reg(g2v_notify), msg);
1075 static void gen8_free_scratch(struct i915_address_space *vm)
1077 if (USES_FULL_48BIT_PPGTT(vm->i915))
1078 free_pdp(vm, vm->scratch_pdp);
1079 free_pd(vm, vm->scratch_pd);
1080 free_pt(vm, vm->scratch_pt);
1081 cleanup_scratch_page(vm);
1084 static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
1085 struct i915_page_directory_pointer *pdp)
1089 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(vm->i915)) {
1090 if (WARN_ON(!pdp->page_directory[i]))
1093 gen8_free_page_tables(vm, pdp->page_directory[i]);
1094 free_pd(vm, pdp->page_directory[i]);
1100 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1104 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1105 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1108 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
1111 cleanup_px(&ppgtt->base, &ppgtt->pml4);
1114 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1116 struct drm_i915_private *dev_priv = vm->i915;
1117 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1119 if (intel_vgpu_active(dev_priv))
1120 gen8_ppgtt_notify_vgt(ppgtt, false);
1122 if (!USES_FULL_48BIT_PPGTT(vm->i915))
1123 gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
1125 gen8_ppgtt_cleanup_4lvl(ppgtt);
1127 gen8_free_scratch(vm);
1131 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
1132 * @vm: Master vm structure.
1133 * @pd: Page directory for this address range.
1134 * @start: Starting virtual address to begin allocations.
1135 * @length: Size of the allocations.
1136 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1137 * caller to free on error.
1139 * Allocate the required number of page tables. Extremely similar to
1140 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1141 * the page directory boundary (instead of the page directory pointer). That
1142 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1143 * possible, and likely that the caller will need to use multiple calls of this
1144 * function to achieve the appropriate allocation.
1146 * Return: 0 if success; negative error code otherwise.
1148 static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
1149 struct i915_page_directory *pd,
1152 unsigned long *new_pts)
1154 struct i915_page_table *pt;
1157 gen8_for_each_pde(pt, pd, start, length, pde) {
1158 /* Don't reallocate page tables */
1159 if (test_bit(pde, pd->used_pdes)) {
1160 /* Scratch is never allocated this way */
1161 WARN_ON(pt == vm->scratch_pt);
1169 gen8_initialize_pt(vm, pt);
1170 pd->page_table[pde] = pt;
1171 __set_bit(pde, new_pts);
1172 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
1178 for_each_set_bit(pde, new_pts, I915_PDES)
1179 free_pt(vm, pd->page_table[pde]);
1185 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
1186 * @vm: Master vm structure.
1187 * @pdp: Page directory pointer for this address range.
1188 * @start: Starting virtual address to begin allocations.
1189 * @length: Size of the allocations.
1190 * @new_pds: Bitmap set by function with new allocations. Likely used by the
1191 * caller to free on error.
1193 * Allocate the required number of page directories starting at the pde index of
1194 * @start, and ending at the pde index @start + @length. This function will skip
1195 * over already allocated page directories within the range, and only allocate
1196 * new ones, setting the appropriate pointer within the pdp as well as the
1197 * correct position in the bitmap @new_pds.
1199 * The function will only allocate the pages within the range for a give page
1200 * directory pointer. In other words, if @start + @length straddles a virtually
1201 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1202 * required by the caller, This is not currently possible, and the BUG in the
1203 * code will prevent it.
1205 * Return: 0 if success; negative error code otherwise.
1208 gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1209 struct i915_page_directory_pointer *pdp,
1212 unsigned long *new_pds)
1214 struct i915_page_directory *pd;
1216 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
1218 WARN_ON(!bitmap_empty(new_pds, pdpes));
1220 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1221 if (test_bit(pdpe, pdp->used_pdpes))
1228 gen8_initialize_pd(vm, pd);
1229 pdp->page_directory[pdpe] = pd;
1230 __set_bit(pdpe, new_pds);
1231 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
1237 for_each_set_bit(pdpe, new_pds, pdpes)
1238 free_pd(vm, pdp->page_directory[pdpe]);
1244 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1245 * @vm: Master vm structure.
1246 * @pml4: Page map level 4 for this address range.
1247 * @start: Starting virtual address to begin allocations.
1248 * @length: Size of the allocations.
1249 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1250 * caller to free on error.
1252 * Allocate the required number of page directory pointers. Extremely similar to
1253 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1254 * The main difference is here we are limited by the pml4 boundary (instead of
1255 * the page directory pointer).
1257 * Return: 0 if success; negative error code otherwise.
1260 gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1261 struct i915_pml4 *pml4,
1264 unsigned long *new_pdps)
1266 struct i915_page_directory_pointer *pdp;
1269 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1271 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1272 if (!test_bit(pml4e, pml4->used_pml4es)) {
1273 pdp = alloc_pdp(vm);
1277 gen8_initialize_pdp(vm, pdp);
1278 pml4->pdps[pml4e] = pdp;
1279 __set_bit(pml4e, new_pdps);
1280 trace_i915_page_directory_pointer_entry_alloc(vm,
1290 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1291 free_pdp(vm, pml4->pdps[pml4e]);
1297 free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
1303 /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1304 * of these are based on the number of PDPEs in the system.
1307 int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
1308 unsigned long **new_pts,
1314 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
1318 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1329 free_gen8_temp_bitmaps(pds, pts);
1333 static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1334 struct i915_page_directory_pointer *pdp,
1338 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1339 unsigned long *new_page_dirs, *new_page_tables;
1340 struct i915_page_directory *pd;
1341 const uint64_t orig_start = start;
1342 const uint64_t orig_length = length;
1344 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
1347 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1351 /* Do the allocations first so we can easily bail out */
1352 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1355 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1359 /* For every page directory referenced, allocate page tables */
1360 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1361 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
1362 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
1368 length = orig_length;
1370 /* Allocations have completed successfully, so set the bitmaps, and do
1372 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1373 gen8_pde_t *const page_directory = kmap_px(pd);
1374 struct i915_page_table *pt;
1375 uint64_t pd_len = length;
1376 uint64_t pd_start = start;
1379 /* Every pd should be allocated, we just did that above. */
1382 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1383 /* Same reasoning as pd */
1386 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1388 /* Set our used ptes within the page table */
1389 bitmap_set(pt->used_ptes,
1390 gen8_pte_index(pd_start),
1391 gen8_pte_count(pd_start, pd_len));
1393 /* Our pde is now pointing to the pagetable, pt */
1394 __set_bit(pde, pd->used_pdes);
1396 /* Map the PDE to the page table */
1397 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1399 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1400 gen8_pte_index(start),
1401 gen8_pte_count(start, length),
1404 /* NB: We haven't yet mapped ptes to pages. At this
1405 * point we're still relying on insert_entries() */
1408 kunmap_px(page_directory);
1409 __set_bit(pdpe, pdp->used_pdpes);
1410 gen8_setup_pdpe(ppgtt, pdp, pd, pdpe);
1413 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1414 mark_tlbs_dirty(ppgtt);
1421 for_each_set_bit(temp, new_page_tables + pdpe *
1422 BITS_TO_LONGS(I915_PDES), I915_PDES)
1423 free_pt(vm, pdp->page_directory[pdpe]->page_table[temp]);
1426 for_each_set_bit(pdpe, new_page_dirs, pdpes)
1427 free_pd(vm, pdp->page_directory[pdpe]);
1429 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1430 mark_tlbs_dirty(ppgtt);
1434 static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1435 struct i915_pml4 *pml4,
1439 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
1440 struct i915_page_directory_pointer *pdp;
1444 /* Do the pml4 allocations first, so we don't need to track the newly
1445 * allocated tables below the pdp */
1446 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1448 /* The pagedirectory and pagetable allocations are done in the shared 3
1449 * and 4 level code. Just allocate the pdps.
1451 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1456 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1459 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1463 gen8_setup_pml4e(pml4, pdp, pml4e);
1466 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1467 GEN8_PML4ES_PER_PML4);
1472 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1473 gen8_ppgtt_cleanup_3lvl(vm, pml4->pdps[pml4e]);
1478 static int gen8_alloc_va_range(struct i915_address_space *vm,
1479 uint64_t start, uint64_t length)
1481 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1483 if (USES_FULL_48BIT_PPGTT(vm->i915))
1484 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1486 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1489 static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
1490 struct i915_page_directory_pointer *pdp,
1491 uint64_t start, uint64_t length,
1492 gen8_pte_t scratch_pte,
1495 struct i915_page_directory *pd;
1498 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1499 struct i915_page_table *pt;
1500 uint64_t pd_len = length;
1501 uint64_t pd_start = start;
1504 if (!test_bit(pdpe, pdp->used_pdpes))
1507 seq_printf(m, "\tPDPE #%d\n", pdpe);
1508 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1510 gen8_pte_t *pt_vaddr;
1512 if (!test_bit(pde, pd->used_pdes))
1515 pt_vaddr = kmap_px(pt);
1516 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1518 (pdpe << GEN8_PDPE_SHIFT) |
1519 (pde << GEN8_PDE_SHIFT) |
1520 (pte << GEN8_PTE_SHIFT);
1524 for (i = 0; i < 4; i++)
1525 if (pt_vaddr[pte + i] != scratch_pte)
1530 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1531 for (i = 0; i < 4; i++) {
1532 if (pt_vaddr[pte + i] != scratch_pte)
1533 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1535 seq_puts(m, " SCRATCH ");
1539 /* don't use kunmap_px, it could trigger
1540 * an unnecessary flush.
1542 kunmap_atomic(pt_vaddr);
1547 static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1549 struct i915_address_space *vm = &ppgtt->base;
1550 uint64_t start = ppgtt->base.start;
1551 uint64_t length = ppgtt->base.total;
1552 const gen8_pte_t scratch_pte =
1553 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
1555 if (!USES_FULL_48BIT_PPGTT(vm->i915)) {
1556 gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
1559 struct i915_pml4 *pml4 = &ppgtt->pml4;
1560 struct i915_page_directory_pointer *pdp;
1562 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1563 if (!test_bit(pml4e, pml4->used_pml4es))
1566 seq_printf(m, " PML4E #%llu\n", pml4e);
1567 gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
1572 static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1574 unsigned long *new_page_dirs, *new_page_tables;
1575 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
1578 /* We allocate temp bitmap for page tables for no gain
1579 * but as this is for init only, lets keep the things simple
1581 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1585 /* Allocate for all pdps regardless of how the ppgtt
1588 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1592 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1594 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1600 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1601 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1602 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1606 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1608 struct drm_i915_private *dev_priv = ppgtt->base.i915;
1611 ret = gen8_init_scratch(&ppgtt->base);
1615 ppgtt->base.start = 0;
1616 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1617 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1618 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1619 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1620 ppgtt->base.bind_vma = ppgtt_bind_vma;
1621 ppgtt->debug_dump = gen8_dump_ppgtt;
1623 /* There are only few exceptions for gen >=6. chv and bxt.
1624 * And we are not sure about the latter so play safe for now.
1626 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
1627 ppgtt->base.pt_kmap_wc = true;
1629 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1630 ret = setup_px(&ppgtt->base, &ppgtt->pml4);
1634 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1636 ppgtt->base.total = 1ULL << 48;
1637 ppgtt->switch_mm = gen8_48b_mm_switch;
1639 ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
1641 ret = __pdp_init(dev_priv, &ppgtt->pdp);
1645 ppgtt->base.total = 1ULL << 32;
1646 ppgtt->switch_mm = gen8_legacy_mm_switch;
1647 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1651 if (intel_vgpu_active(dev_priv)) {
1652 ret = gen8_preallocate_top_level_pdps(ppgtt);
1657 ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
1660 if (intel_vgpu_active(dev_priv))
1661 gen8_ppgtt_notify_vgt(ppgtt, true);
1666 gen8_free_scratch(&ppgtt->base);
1670 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1672 struct i915_address_space *vm = &ppgtt->base;
1673 struct i915_page_table *unused;
1674 gen6_pte_t scratch_pte;
1677 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
1679 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
1682 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
1684 gen6_pte_t *pt_vaddr;
1685 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
1686 pd_entry = readl(ppgtt->pd_addr + pde);
1687 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1689 if (pd_entry != expected)
1690 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1694 seq_printf(m, "\tPDE: %x\n", pd_entry);
1696 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1698 for (pte = 0; pte < GEN6_PTES; pte+=4) {
1700 (pde * PAGE_SIZE * GEN6_PTES) +
1704 for (i = 0; i < 4; i++)
1705 if (pt_vaddr[pte + i] != scratch_pte)
1710 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1711 for (i = 0; i < 4; i++) {
1712 if (pt_vaddr[pte + i] != scratch_pte)
1713 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1715 seq_puts(m, " SCRATCH ");
1719 kunmap_px(pt_vaddr);
1723 /* Write pde (index) from the page directory @pd to the page table @pt */
1724 static void gen6_write_pde(struct i915_page_directory *pd,
1725 const int pde, struct i915_page_table *pt)
1727 /* Caller needs to make sure the write completes if necessary */
1728 struct i915_hw_ppgtt *ppgtt =
1729 container_of(pd, struct i915_hw_ppgtt, pd);
1732 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
1733 pd_entry |= GEN6_PDE_VALID;
1735 writel(pd_entry, ppgtt->pd_addr + pde);
1738 /* Write all the page tables found in the ppgtt structure to incrementing page
1740 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1741 struct i915_page_directory *pd,
1742 uint32_t start, uint32_t length)
1744 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1745 struct i915_page_table *pt;
1748 gen6_for_each_pde(pt, pd, start, length, pde)
1749 gen6_write_pde(pd, pde, pt);
1751 /* Make sure write is complete before other code can use this page
1752 * table. Also require for WC mapped PTEs */
1756 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1758 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1760 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
1763 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1764 struct drm_i915_gem_request *req)
1766 struct intel_engine_cs *engine = req->engine;
1770 /* NB: TLBs must be flushed and invalidated before a switch */
1771 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
1775 cs = intel_ring_begin(req, 6);
1779 *cs++ = MI_LOAD_REGISTER_IMM(2);
1780 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1781 *cs++ = PP_DIR_DCLV_2G;
1782 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1783 *cs++ = get_pd_offset(ppgtt);
1785 intel_ring_advance(req, cs);
1790 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1791 struct drm_i915_gem_request *req)
1793 struct intel_engine_cs *engine = req->engine;
1797 /* NB: TLBs must be flushed and invalidated before a switch */
1798 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
1802 cs = intel_ring_begin(req, 6);
1806 *cs++ = MI_LOAD_REGISTER_IMM(2);
1807 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
1808 *cs++ = PP_DIR_DCLV_2G;
1809 *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
1810 *cs++ = get_pd_offset(ppgtt);
1812 intel_ring_advance(req, cs);
1814 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1815 if (engine->id != RCS) {
1816 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
1824 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1825 struct drm_i915_gem_request *req)
1827 struct intel_engine_cs *engine = req->engine;
1828 struct drm_i915_private *dev_priv = req->i915;
1830 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1831 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
1835 static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
1837 struct intel_engine_cs *engine;
1838 enum intel_engine_id id;
1840 for_each_engine(engine, dev_priv, id) {
1841 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1842 GEN8_GFX_PPGTT_48B : 0;
1843 I915_WRITE(RING_MODE_GEN7(engine),
1844 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1848 static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
1850 struct intel_engine_cs *engine;
1851 uint32_t ecochk, ecobits;
1852 enum intel_engine_id id;
1854 ecobits = I915_READ(GAC_ECO_BITS);
1855 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1857 ecochk = I915_READ(GAM_ECOCHK);
1858 if (IS_HASWELL(dev_priv)) {
1859 ecochk |= ECOCHK_PPGTT_WB_HSW;
1861 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1862 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1864 I915_WRITE(GAM_ECOCHK, ecochk);
1866 for_each_engine(engine, dev_priv, id) {
1867 /* GFX_MODE is per-ring on gen7+ */
1868 I915_WRITE(RING_MODE_GEN7(engine),
1869 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1873 static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
1875 uint32_t ecochk, gab_ctl, ecobits;
1877 ecobits = I915_READ(GAC_ECO_BITS);
1878 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1879 ECOBITS_PPGTT_CACHE64B);
1881 gab_ctl = I915_READ(GAB_CTL);
1882 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1884 ecochk = I915_READ(GAM_ECOCHK);
1885 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1887 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1890 /* PPGTT support for Sandybdrige/Gen6 and later */
1891 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1895 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1896 gen6_pte_t *pt_vaddr, scratch_pte;
1897 unsigned first_entry = start >> PAGE_SHIFT;
1898 unsigned num_entries = length >> PAGE_SHIFT;
1899 unsigned act_pt = first_entry / GEN6_PTES;
1900 unsigned first_pte = first_entry % GEN6_PTES;
1901 unsigned last_pte, i;
1903 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
1906 while (num_entries) {
1907 last_pte = first_pte + num_entries;
1908 if (last_pte > GEN6_PTES)
1909 last_pte = GEN6_PTES;
1911 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1913 for (i = first_pte; i < last_pte; i++)
1914 pt_vaddr[i] = scratch_pte;
1916 kunmap_px(pt_vaddr);
1918 num_entries -= last_pte - first_pte;
1924 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1925 struct sg_table *pages,
1927 enum i915_cache_level cache_level, u32 flags)
1929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1930 unsigned first_entry = start >> PAGE_SHIFT;
1931 unsigned act_pt = first_entry / GEN6_PTES;
1932 unsigned act_pte = first_entry % GEN6_PTES;
1933 const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
1934 struct sgt_dma iter;
1937 vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1938 iter.sg = pages->sgl;
1939 iter.dma = sg_dma_address(iter.sg);
1940 iter.max = iter.dma + iter.sg->length;
1942 vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
1944 iter.dma += PAGE_SIZE;
1945 if (iter.dma == iter.max) {
1946 iter.sg = __sg_next(iter.sg);
1950 iter.dma = sg_dma_address(iter.sg);
1951 iter.max = iter.dma + iter.sg->length;
1954 if (++act_pte == GEN6_PTES) {
1956 vaddr = kmap_px(ppgtt->pd.page_table[++act_pt]);
1963 static int gen6_alloc_va_range(struct i915_address_space *vm,
1964 uint64_t start_in, uint64_t length_in)
1966 DECLARE_BITMAP(new_page_tables, I915_PDES);
1967 struct drm_i915_private *dev_priv = vm->i915;
1968 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1969 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1970 struct i915_page_table *pt;
1971 uint32_t start, length, start_save, length_save;
1975 start = start_save = start_in;
1976 length = length_save = length_in;
1978 bitmap_zero(new_page_tables, I915_PDES);
1980 /* The allocation is done in two stages so that we can bail out with
1981 * minimal amount of pain. The first stage finds new page tables that
1982 * need allocation. The second stage marks use ptes within the page
1985 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
1986 if (pt != vm->scratch_pt) {
1987 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1991 /* We've already allocated a page table */
1992 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
2000 gen6_initialize_pt(vm, pt);
2002 ppgtt->pd.page_table[pde] = pt;
2003 __set_bit(pde, new_page_tables);
2004 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
2008 length = length_save;
2010 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
2011 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
2013 bitmap_zero(tmp_bitmap, GEN6_PTES);
2014 bitmap_set(tmp_bitmap, gen6_pte_index(start),
2015 gen6_pte_count(start, length));
2017 if (__test_and_clear_bit(pde, new_page_tables))
2018 gen6_write_pde(&ppgtt->pd, pde, pt);
2020 trace_i915_page_table_entry_map(vm, pde, pt,
2021 gen6_pte_index(start),
2022 gen6_pte_count(start, length),
2024 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
2028 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
2030 /* Make sure write is complete before other code can use this page
2031 * table. Also require for WC mapped PTEs */
2034 mark_tlbs_dirty(ppgtt);
2038 for_each_set_bit(pde, new_page_tables, I915_PDES) {
2039 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
2041 ppgtt->pd.page_table[pde] = vm->scratch_pt;
2045 mark_tlbs_dirty(ppgtt);
2049 static int gen6_init_scratch(struct i915_address_space *vm)
2053 ret = setup_scratch_page(vm, I915_GFP_DMA);
2057 vm->scratch_pt = alloc_pt(vm);
2058 if (IS_ERR(vm->scratch_pt)) {
2059 cleanup_scratch_page(vm);
2060 return PTR_ERR(vm->scratch_pt);
2063 gen6_initialize_pt(vm, vm->scratch_pt);
2068 static void gen6_free_scratch(struct i915_address_space *vm)
2070 free_pt(vm, vm->scratch_pt);
2071 cleanup_scratch_page(vm);
2074 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
2076 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
2077 struct i915_page_directory *pd = &ppgtt->pd;
2078 struct i915_page_table *pt;
2081 drm_mm_remove_node(&ppgtt->node);
2083 gen6_for_all_pdes(pt, pd, pde)
2084 if (pt != vm->scratch_pt)
2087 gen6_free_scratch(vm);
2090 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
2092 struct i915_address_space *vm = &ppgtt->base;
2093 struct drm_i915_private *dev_priv = ppgtt->base.i915;
2094 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2097 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2098 * allocator works in address space sizes, so it's multiplied by page
2099 * size. We allocate at the top of the GTT to avoid fragmentation.
2101 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
2103 ret = gen6_init_scratch(vm);
2107 ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
2108 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2109 I915_COLOR_UNEVICTABLE,
2110 0, ggtt->base.total,
2115 if (ppgtt->node.start < ggtt->mappable_end)
2116 DRM_DEBUG("Forced to use aperture for PDEs\n");
2121 gen6_free_scratch(vm);
2125 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2127 return gen6_ppgtt_allocate_page_directories(ppgtt);
2130 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2131 uint64_t start, uint64_t length)
2133 struct i915_page_table *unused;
2136 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
2137 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
2140 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2142 struct drm_i915_private *dev_priv = ppgtt->base.i915;
2143 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2146 ppgtt->base.pte_encode = ggtt->base.pte_encode;
2147 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
2148 ppgtt->switch_mm = gen6_mm_switch;
2149 else if (IS_HASWELL(dev_priv))
2150 ppgtt->switch_mm = hsw_mm_switch;
2151 else if (IS_GEN7(dev_priv))
2152 ppgtt->switch_mm = gen7_mm_switch;
2156 ret = gen6_ppgtt_alloc(ppgtt);
2160 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
2161 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2162 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
2163 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2164 ppgtt->base.bind_vma = ppgtt_bind_vma;
2165 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
2166 ppgtt->base.start = 0;
2167 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
2168 ppgtt->debug_dump = gen6_dump_ppgtt;
2170 ppgtt->pd.base.ggtt_offset =
2171 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2173 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
2174 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2176 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
2178 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2180 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
2181 ppgtt->node.size >> 20,
2182 ppgtt->node.start / PAGE_SIZE);
2184 DRM_DEBUG("Adding PPGTT at offset %x\n",
2185 ppgtt->pd.base.ggtt_offset << 10);
2190 static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2191 struct drm_i915_private *dev_priv)
2193 ppgtt->base.i915 = dev_priv;
2194 ppgtt->base.dma = &dev_priv->drm.pdev->dev;
2196 if (INTEL_INFO(dev_priv)->gen < 8)
2197 return gen6_ppgtt_init(ppgtt);
2199 return gen8_ppgtt_init(ppgtt);
2202 static void i915_address_space_init(struct i915_address_space *vm,
2203 struct drm_i915_private *dev_priv,
2206 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
2208 drm_mm_init(&vm->mm, vm->start, vm->total);
2209 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
2211 INIT_LIST_HEAD(&vm->active_list);
2212 INIT_LIST_HEAD(&vm->inactive_list);
2213 INIT_LIST_HEAD(&vm->unbound_list);
2215 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2216 pagevec_init(&vm->free_pages, false);
2219 static void i915_address_space_fini(struct i915_address_space *vm)
2221 if (pagevec_count(&vm->free_pages))
2222 vm_free_pages_release(vm);
2224 i915_gem_timeline_fini(&vm->timeline);
2225 drm_mm_takedown(&vm->mm);
2226 list_del(&vm->global_link);
2229 static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
2231 /* This function is for gtt related workarounds. This function is
2232 * called on driver load and after a GPU reset, so you can place
2233 * workarounds here even if they get overwritten by GPU reset.
2235 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
2236 if (IS_BROADWELL(dev_priv))
2237 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2238 else if (IS_CHERRYVIEW(dev_priv))
2239 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2240 else if (IS_GEN9_BC(dev_priv))
2241 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2242 else if (IS_GEN9_LP(dev_priv))
2243 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2246 int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
2248 gtt_write_workarounds(dev_priv);
2250 /* In the case of execlists, PPGTT is enabled by the context descriptor
2251 * and the PDPs are contained within the context itself. We don't
2252 * need to do anything here. */
2253 if (i915.enable_execlists)
2256 if (!USES_PPGTT(dev_priv))
2259 if (IS_GEN6(dev_priv))
2260 gen6_ppgtt_enable(dev_priv);
2261 else if (IS_GEN7(dev_priv))
2262 gen7_ppgtt_enable(dev_priv);
2263 else if (INTEL_GEN(dev_priv) >= 8)
2264 gen8_ppgtt_enable(dev_priv);
2266 MISSING_CASE(INTEL_GEN(dev_priv));
2271 struct i915_hw_ppgtt *
2272 i915_ppgtt_create(struct drm_i915_private *dev_priv,
2273 struct drm_i915_file_private *fpriv,
2276 struct i915_hw_ppgtt *ppgtt;
2279 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2281 return ERR_PTR(-ENOMEM);
2283 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2286 return ERR_PTR(ret);
2289 kref_init(&ppgtt->ref);
2290 i915_address_space_init(&ppgtt->base, dev_priv, name);
2291 ppgtt->base.file = fpriv;
2293 trace_i915_ppgtt_create(&ppgtt->base);
2298 void i915_ppgtt_close(struct i915_address_space *vm)
2300 struct list_head *phases[] = {
2307 GEM_BUG_ON(vm->closed);
2310 for (phase = phases; *phase; phase++) {
2311 struct i915_vma *vma, *vn;
2313 list_for_each_entry_safe(vma, vn, *phase, vm_link)
2314 if (!i915_vma_is_closed(vma))
2315 i915_vma_close(vma);
2319 void i915_ppgtt_release(struct kref *kref)
2321 struct i915_hw_ppgtt *ppgtt =
2322 container_of(kref, struct i915_hw_ppgtt, ref);
2324 trace_i915_ppgtt_release(&ppgtt->base);
2326 /* vmas should already be unbound and destroyed */
2327 WARN_ON(!list_empty(&ppgtt->base.active_list));
2328 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2329 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
2331 ppgtt->base.cleanup(&ppgtt->base);
2332 i915_address_space_fini(&ppgtt->base);
2336 /* Certain Gen5 chipsets require require idling the GPU before
2337 * unmapping anything from the GTT when VT-d is enabled.
2339 static bool needs_idle_maps(struct drm_i915_private *dev_priv)
2341 #ifdef CONFIG_INTEL_IOMMU
2342 /* Query intel_iommu to see if we need the workaround. Presumably that
2345 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
2351 void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
2353 struct intel_engine_cs *engine;
2354 enum intel_engine_id id;
2356 if (INTEL_INFO(dev_priv)->gen < 6)
2359 for_each_engine(engine, dev_priv, id) {
2361 fault_reg = I915_READ(RING_FAULT_REG(engine));
2362 if (fault_reg & RING_FAULT_VALID) {
2363 DRM_DEBUG_DRIVER("Unexpected fault\n"
2365 "\tAddress space: %s\n"
2368 fault_reg & PAGE_MASK,
2369 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2370 RING_FAULT_SRCID(fault_reg),
2371 RING_FAULT_FAULT_TYPE(fault_reg));
2372 I915_WRITE(RING_FAULT_REG(engine),
2373 fault_reg & ~RING_FAULT_VALID);
2377 /* Engine specific init may not have been done till this point. */
2378 if (dev_priv->engine[RCS])
2379 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
2382 void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
2384 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2386 /* Don't bother messing with faults pre GEN6 as we have little
2387 * documentation supporting that it's a good idea.
2389 if (INTEL_GEN(dev_priv) < 6)
2392 i915_check_and_clear_faults(dev_priv);
2394 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
2396 i915_ggtt_invalidate(dev_priv);
2399 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2400 struct sg_table *pages)
2403 if (dma_map_sg(&obj->base.dev->pdev->dev,
2404 pages->sgl, pages->nents,
2405 PCI_DMA_BIDIRECTIONAL))
2408 /* If the DMA remap fails, one cause can be that we have
2409 * too many objects pinned in a small remapping table,
2410 * such as swiotlb. Incrementally purge all other objects and
2411 * try again - if there are no more pages to remove from
2412 * the DMA remapper, i915_gem_shrink will return 0.
2414 GEM_BUG_ON(obj->mm.pages == pages);
2415 } while (i915_gem_shrink(to_i915(obj->base.dev),
2416 obj->base.size >> PAGE_SHIFT,
2418 I915_SHRINK_UNBOUND |
2419 I915_SHRINK_ACTIVE));
2424 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2429 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2432 enum i915_cache_level level,
2435 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2436 gen8_pte_t __iomem *pte =
2437 (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
2439 gen8_set_pte(pte, gen8_pte_encode(addr, level));
2441 ggtt->invalidate(vm->i915);
2444 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2445 struct sg_table *st,
2447 enum i915_cache_level level, u32 unused)
2449 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2450 struct sgt_iter sgt_iter;
2451 gen8_pte_t __iomem *gtt_entries;
2452 const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
2455 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
2456 gtt_entries += start >> PAGE_SHIFT;
2457 for_each_sgt_dma(addr, sgt_iter, st)
2458 gen8_set_pte(gtt_entries++, pte_encode | addr);
2462 /* This next bit makes the above posting read even more important. We
2463 * want to flush the TLBs only after we're certain all the PTE updates
2466 ggtt->invalidate(vm->i915);
2469 struct insert_entries {
2470 struct i915_address_space *vm;
2471 struct sg_table *st;
2473 enum i915_cache_level level;
2477 static int gen8_ggtt_insert_entries__cb(void *_arg)
2479 struct insert_entries *arg = _arg;
2480 gen8_ggtt_insert_entries(arg->vm, arg->st,
2481 arg->start, arg->level, arg->flags);
2485 static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2486 struct sg_table *st,
2488 enum i915_cache_level level,
2491 struct insert_entries arg = { vm, st, start, level, flags };
2492 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2495 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2498 enum i915_cache_level level,
2501 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2502 gen6_pte_t __iomem *pte =
2503 (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
2505 iowrite32(vm->pte_encode(addr, level, flags), pte);
2507 ggtt->invalidate(vm->i915);
2511 * Binds an object into the global gtt with the specified cache level. The object
2512 * will be accessible to the GPU via commands whose operands reference offsets
2513 * within the global GTT as well as accessible by the GPU through the GMADR
2514 * mapped BAR (dev_priv->mm.gtt->gtt).
2516 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2517 struct sg_table *st,
2519 enum i915_cache_level level, u32 flags)
2521 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2522 gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
2523 unsigned int i = start >> PAGE_SHIFT;
2524 struct sgt_iter iter;
2526 for_each_sgt_dma(addr, iter, st)
2527 iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
2530 /* This next bit makes the above posting read even more important. We
2531 * want to flush the TLBs only after we're certain all the PTE updates
2534 ggtt->invalidate(vm->i915);
2537 static void nop_clear_range(struct i915_address_space *vm,
2538 uint64_t start, uint64_t length)
2542 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2543 uint64_t start, uint64_t length)
2545 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2546 unsigned first_entry = start >> PAGE_SHIFT;
2547 unsigned num_entries = length >> PAGE_SHIFT;
2548 const gen8_pte_t scratch_pte =
2549 gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
2550 gen8_pte_t __iomem *gtt_base =
2551 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2552 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2555 if (WARN(num_entries > max_entries,
2556 "First entry = %d; Num entries = %d (max=%d)\n",
2557 first_entry, num_entries, max_entries))
2558 num_entries = max_entries;
2560 for (i = 0; i < num_entries; i++)
2561 gen8_set_pte(>t_base[i], scratch_pte);
2565 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2569 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2570 unsigned first_entry = start >> PAGE_SHIFT;
2571 unsigned num_entries = length >> PAGE_SHIFT;
2572 gen6_pte_t scratch_pte, __iomem *gtt_base =
2573 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2574 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2577 if (WARN(num_entries > max_entries,
2578 "First entry = %d; Num entries = %d (max=%d)\n",
2579 first_entry, num_entries, max_entries))
2580 num_entries = max_entries;
2582 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
2585 for (i = 0; i < num_entries; i++)
2586 iowrite32(scratch_pte, >t_base[i]);
2590 static void i915_ggtt_insert_page(struct i915_address_space *vm,
2593 enum i915_cache_level cache_level,
2596 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2597 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2599 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2602 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2603 struct sg_table *pages,
2605 enum i915_cache_level cache_level, u32 unused)
2607 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2608 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2610 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
2614 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2618 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
2621 static int ggtt_bind_vma(struct i915_vma *vma,
2622 enum i915_cache_level cache_level,
2625 struct drm_i915_private *i915 = vma->vm->i915;
2626 struct drm_i915_gem_object *obj = vma->obj;
2629 if (unlikely(!vma->pages)) {
2630 int ret = i915_get_ggtt_vma_pages(vma);
2635 /* Currently applicable only to VLV */
2638 pte_flags |= PTE_READ_ONLY;
2640 intel_runtime_pm_get(i915);
2641 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
2642 cache_level, pte_flags);
2643 intel_runtime_pm_put(i915);
2646 * Without aliasing PPGTT there's no difference between
2647 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2648 * upgrade to both bound if we bind either to avoid double-binding.
2650 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
2655 static void ggtt_unbind_vma(struct i915_vma *vma)
2657 struct drm_i915_private *i915 = vma->vm->i915;
2659 intel_runtime_pm_get(i915);
2660 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2661 intel_runtime_pm_put(i915);
2664 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2665 enum i915_cache_level cache_level,
2668 struct drm_i915_private *i915 = vma->vm->i915;
2671 if (unlikely(!vma->pages)) {
2672 int ret = i915_get_ggtt_vma_pages(vma);
2677 /* Currently applicable only to VLV */
2679 if (vma->obj->gt_ro)
2680 pte_flags |= PTE_READ_ONLY;
2682 if (flags & I915_VMA_GLOBAL_BIND) {
2683 intel_runtime_pm_get(i915);
2684 vma->vm->insert_entries(vma->vm,
2685 vma->pages, vma->node.start,
2686 cache_level, pte_flags);
2687 intel_runtime_pm_put(i915);
2690 if (flags & I915_VMA_LOCAL_BIND) {
2691 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2692 appgtt->base.insert_entries(&appgtt->base,
2693 vma->pages, vma->node.start,
2694 cache_level, pte_flags);
2700 static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
2702 struct drm_i915_private *i915 = vma->vm->i915;
2704 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2705 intel_runtime_pm_get(i915);
2706 vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
2707 intel_runtime_pm_put(i915);
2710 if (vma->flags & I915_VMA_LOCAL_BIND) {
2711 struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
2713 vm->clear_range(vm, vma->node.start, vma->size);
2717 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2718 struct sg_table *pages)
2720 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2721 struct device *kdev = &dev_priv->drm.pdev->dev;
2722 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2724 if (unlikely(ggtt->do_idle_maps)) {
2725 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
2726 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2727 /* Wait a bit, in hopes it avoids the hang */
2732 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
2735 static void i915_gtt_color_adjust(const struct drm_mm_node *node,
2736 unsigned long color,
2740 if (node->allocated && node->color != color)
2741 *start += I915_GTT_PAGE_SIZE;
2743 /* Also leave a space between the unallocated reserved node after the
2744 * GTT and any objects within the GTT, i.e. we use the color adjustment
2745 * to insert a guard page to prevent prefetches crossing over the
2748 node = list_next_entry(node, node_list);
2749 if (node->color != color)
2750 *end -= I915_GTT_PAGE_SIZE;
2753 int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
2755 struct i915_ggtt *ggtt = &i915->ggtt;
2756 struct i915_hw_ppgtt *ppgtt;
2759 ppgtt = i915_ppgtt_create(i915, NULL, "[alias]");
2761 return PTR_ERR(ppgtt);
2763 if (ppgtt->base.allocate_va_range) {
2764 err = ppgtt->base.allocate_va_range(&ppgtt->base,
2765 0, ppgtt->base.total);
2770 ppgtt->base.clear_range(&ppgtt->base,
2774 i915->mm.aliasing_ppgtt = ppgtt;
2776 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2777 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2779 WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
2780 ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
2785 i915_ppgtt_put(ppgtt);
2789 void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
2791 struct i915_ggtt *ggtt = &i915->ggtt;
2792 struct i915_hw_ppgtt *ppgtt;
2794 ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
2798 i915_ppgtt_put(ppgtt);
2800 ggtt->base.bind_vma = ggtt_bind_vma;
2801 ggtt->base.unbind_vma = ggtt_unbind_vma;
2804 int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
2806 /* Let GEM Manage all of the aperture.
2808 * However, leave one page at the end still bound to the scratch page.
2809 * There are a number of places where the hardware apparently prefetches
2810 * past the end of the object, and we've seen multiple hangs with the
2811 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2812 * aperture. One page should be enough to keep any prefetching inside
2815 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2816 unsigned long hole_start, hole_end;
2817 struct drm_mm_node *entry;
2820 ret = intel_vgt_balloon(dev_priv);
2824 /* Reserve a mappable slot for our lockless error capture */
2825 ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
2826 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
2827 0, ggtt->mappable_end,
2832 /* Clear any non-preallocated blocks */
2833 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
2834 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2835 hole_start, hole_end);
2836 ggtt->base.clear_range(&ggtt->base, hole_start,
2837 hole_end - hole_start);
2840 /* And finally clear the reserved guard page */
2841 ggtt->base.clear_range(&ggtt->base,
2842 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
2844 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
2845 ret = i915_gem_init_aliasing_ppgtt(dev_priv);
2853 drm_mm_remove_node(&ggtt->error_capture);
2858 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
2859 * @dev_priv: i915 device
2861 void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
2863 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2864 struct i915_vma *vma, *vn;
2866 ggtt->base.closed = true;
2868 mutex_lock(&dev_priv->drm.struct_mutex);
2869 WARN_ON(!list_empty(&ggtt->base.active_list));
2870 list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
2871 WARN_ON(i915_vma_unbind(vma));
2872 mutex_unlock(&dev_priv->drm.struct_mutex);
2874 i915_gem_cleanup_stolen(&dev_priv->drm);
2876 mutex_lock(&dev_priv->drm.struct_mutex);
2877 i915_gem_fini_aliasing_ppgtt(dev_priv);
2879 if (drm_mm_node_allocated(&ggtt->error_capture))
2880 drm_mm_remove_node(&ggtt->error_capture);
2882 if (drm_mm_initialized(&ggtt->base.mm)) {
2883 intel_vgt_deballoon(dev_priv);
2884 i915_address_space_fini(&ggtt->base);
2887 ggtt->base.cleanup(&ggtt->base);
2888 mutex_unlock(&dev_priv->drm.struct_mutex);
2890 arch_phys_wc_del(ggtt->mtrr);
2891 io_mapping_fini(&ggtt->mappable);
2894 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2896 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2897 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2898 return snb_gmch_ctl << 20;
2901 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2903 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2904 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2906 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2908 #ifdef CONFIG_X86_32
2909 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2910 if (bdw_gmch_ctl > 4)
2914 return bdw_gmch_ctl << 20;
2917 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2919 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2920 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2923 return 1 << (20 + gmch_ctrl);
2928 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
2930 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2931 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2932 return snb_gmch_ctl << 25; /* 32 MB units */
2935 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2937 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2938 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2939 return bdw_gmch_ctl << 25; /* 32 MB units */
2942 static size_t chv_get_stolen_size(u16 gmch_ctrl)
2944 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2945 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2948 * 0x0 to 0x10: 32MB increments starting at 0MB
2949 * 0x11 to 0x16: 4MB increments starting at 8MB
2950 * 0x17 to 0x1d: 4MB increments start at 36MB
2952 if (gmch_ctrl < 0x11)
2953 return gmch_ctrl << 25;
2954 else if (gmch_ctrl < 0x17)
2955 return (gmch_ctrl - 0x11 + 2) << 22;
2957 return (gmch_ctrl - 0x17 + 9) << 22;
2960 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2962 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2963 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2965 if (gen9_gmch_ctl < 0xf0)
2966 return gen9_gmch_ctl << 25; /* 32 MB units */
2968 /* 4MB increments starting at 0xf0 for 4MB */
2969 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2972 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
2974 struct drm_i915_private *dev_priv = ggtt->base.i915;
2975 struct pci_dev *pdev = dev_priv->drm.pdev;
2976 phys_addr_t phys_addr;
2979 /* For Modern GENs the PTEs and register space are split in the BAR */
2980 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
2983 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2984 * dropped. For WC mappings in general we have 64 byte burst writes
2985 * when the WC buffer is flushed, so we can't use it, but have to
2986 * resort to an uncached mapping. The WC issue is easily caught by the
2987 * readback check when writing GTT PTE entries.
2989 if (IS_GEN9_LP(dev_priv))
2990 ggtt->gsm = ioremap_nocache(phys_addr, size);
2992 ggtt->gsm = ioremap_wc(phys_addr, size);
2994 DRM_ERROR("Failed to map the ggtt page table\n");
2998 ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
3000 DRM_ERROR("Scratch setup failed\n");
3001 /* iounmap will also get called at remove, but meh */
3009 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3010 * bits. When using advanced contexts each context stores its own PAT, but
3011 * writing this data shouldn't be harmful even in those cases. */
3012 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
3016 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3017 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3018 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3019 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3020 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3021 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3022 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3023 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3025 if (!USES_PPGTT(dev_priv))
3026 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3027 * so RTL will always use the value corresponding to
3029 * So let's disable cache for GGTT to avoid screen corruptions.
3030 * MOCS still can be used though.
3031 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3032 * before this patch, i.e. the same uncached + snooping access
3033 * like on gen6/7 seems to be in effect.
3034 * - So this just fixes blitter/render access. Again it looks
3035 * like it's not just uncached access, but uncached + snooping.
3036 * So we can still hold onto all our assumptions wrt cpu
3037 * clflushing on LLC machines.
3039 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3041 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3042 * write would work. */
3043 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3044 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3047 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3052 * Map WB on BDW to snooped on CHV.
3054 * Only the snoop bit has meaning for CHV, the rest is
3057 * The hardware will never snoop for certain types of accesses:
3058 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3059 * - PPGTT page tables
3060 * - some other special cycles
3062 * As with BDW, we also need to consider the following for GT accesses:
3063 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3064 * so RTL will always use the value corresponding to
3066 * Which means we must set the snoop bit in PAT entry 0
3067 * in order to keep the global status page working.
3069 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3073 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3074 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3075 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3076 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3078 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3079 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3082 static void gen6_gmch_remove(struct i915_address_space *vm)
3084 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3087 cleanup_scratch_page(vm);
3090 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
3092 struct drm_i915_private *dev_priv = ggtt->base.i915;
3093 struct pci_dev *pdev = dev_priv->drm.pdev;
3097 /* TODO: We're not aware of mappable constraints on gen8 yet */
3098 ggtt->mappable_base = pci_resource_start(pdev, 2);
3099 ggtt->mappable_end = pci_resource_len(pdev, 2);
3101 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3102 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
3104 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3106 if (INTEL_GEN(dev_priv) >= 9) {
3107 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
3108 size = gen8_get_total_gtt_size(snb_gmch_ctl);
3109 } else if (IS_CHERRYVIEW(dev_priv)) {
3110 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
3111 size = chv_get_total_gtt_size(snb_gmch_ctl);
3113 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
3114 size = gen8_get_total_gtt_size(snb_gmch_ctl);
3117 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3119 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
3120 chv_setup_private_ppat(dev_priv);
3122 bdw_setup_private_ppat(dev_priv);
3124 ggtt->base.cleanup = gen6_gmch_remove;
3125 ggtt->base.bind_vma = ggtt_bind_vma;
3126 ggtt->base.unbind_vma = ggtt_unbind_vma;
3127 ggtt->base.insert_page = gen8_ggtt_insert_page;
3128 ggtt->base.clear_range = nop_clear_range;
3129 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
3130 ggtt->base.clear_range = gen8_ggtt_clear_range;
3132 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3133 if (IS_CHERRYVIEW(dev_priv))
3134 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3136 ggtt->invalidate = gen6_ggtt_invalidate;
3138 return ggtt_probe_common(ggtt, size);
3141 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
3143 struct drm_i915_private *dev_priv = ggtt->base.i915;
3144 struct pci_dev *pdev = dev_priv->drm.pdev;
3148 ggtt->mappable_base = pci_resource_start(pdev, 2);
3149 ggtt->mappable_end = pci_resource_len(pdev, 2);
3151 /* 64/512MB is the current min/max we actually know of, but this is just
3152 * a coarse sanity check.
3154 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
3155 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
3159 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3160 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3161 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3163 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
3165 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3166 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3168 ggtt->base.clear_range = gen6_ggtt_clear_range;
3169 ggtt->base.insert_page = gen6_ggtt_insert_page;
3170 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3171 ggtt->base.bind_vma = ggtt_bind_vma;
3172 ggtt->base.unbind_vma = ggtt_unbind_vma;
3173 ggtt->base.cleanup = gen6_gmch_remove;
3175 ggtt->invalidate = gen6_ggtt_invalidate;
3177 if (HAS_EDRAM(dev_priv))
3178 ggtt->base.pte_encode = iris_pte_encode;
3179 else if (IS_HASWELL(dev_priv))
3180 ggtt->base.pte_encode = hsw_pte_encode;
3181 else if (IS_VALLEYVIEW(dev_priv))
3182 ggtt->base.pte_encode = byt_pte_encode;
3183 else if (INTEL_GEN(dev_priv) >= 7)
3184 ggtt->base.pte_encode = ivb_pte_encode;
3186 ggtt->base.pte_encode = snb_pte_encode;
3188 return ggtt_probe_common(ggtt, size);
3191 static void i915_gmch_remove(struct i915_address_space *vm)
3193 intel_gmch_remove();
3196 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3198 struct drm_i915_private *dev_priv = ggtt->base.i915;
3201 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
3203 DRM_ERROR("failed to set up gmch\n");
3207 intel_gtt_get(&ggtt->base.total,
3209 &ggtt->mappable_base,
3210 &ggtt->mappable_end);
3212 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
3213 ggtt->base.insert_page = i915_ggtt_insert_page;
3214 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3215 ggtt->base.clear_range = i915_ggtt_clear_range;
3216 ggtt->base.bind_vma = ggtt_bind_vma;
3217 ggtt->base.unbind_vma = ggtt_unbind_vma;
3218 ggtt->base.cleanup = i915_gmch_remove;
3220 ggtt->invalidate = gmch_ggtt_invalidate;
3222 if (unlikely(ggtt->do_idle_maps))
3223 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3229 * i915_ggtt_probe_hw - Probe GGTT hardware location
3230 * @dev_priv: i915 device
3232 int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
3234 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3237 ggtt->base.i915 = dev_priv;
3238 ggtt->base.dma = &dev_priv->drm.pdev->dev;
3240 if (INTEL_GEN(dev_priv) <= 5)
3241 ret = i915_gmch_probe(ggtt);
3242 else if (INTEL_GEN(dev_priv) < 8)
3243 ret = gen6_gmch_probe(ggtt);
3245 ret = gen8_gmch_probe(ggtt);
3249 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3250 * This is easier than doing range restriction on the fly, as we
3251 * currently don't have any bits spare to pass in this upper
3254 if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
3255 ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
3256 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3259 if ((ggtt->base.total - 1) >> 32) {
3260 DRM_ERROR("We never expected a Global GTT with more than 32bits"
3261 " of address space! Found %lldM!\n",
3262 ggtt->base.total >> 20);
3263 ggtt->base.total = 1ULL << 32;
3264 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3267 if (ggtt->mappable_end > ggtt->base.total) {
3268 DRM_ERROR("mappable aperture extends past end of GGTT,"
3269 " aperture=%llx, total=%llx\n",
3270 ggtt->mappable_end, ggtt->base.total);
3271 ggtt->mappable_end = ggtt->base.total;
3274 /* GMADR is the PCI mmio aperture into the global GTT. */
3275 DRM_INFO("Memory usable by graphics device = %lluM\n",
3276 ggtt->base.total >> 20);
3277 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3278 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
3279 #ifdef CONFIG_INTEL_IOMMU
3280 if (intel_iommu_gfx_mapped)
3281 DRM_INFO("VT-d active for gfx access\n");
3288 * i915_ggtt_init_hw - Initialize GGTT hardware
3289 * @dev_priv: i915 device
3291 int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
3293 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3296 INIT_LIST_HEAD(&dev_priv->vm_list);
3298 /* Note that we use page colouring to enforce a guard page at the
3299 * end of the address space. This is required as the CS may prefetch
3300 * beyond the end of the batch buffer, across the page boundary,
3301 * and beyond the end of the GTT if we do not provide a guard.
3303 mutex_lock(&dev_priv->drm.struct_mutex);
3304 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
3305 if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
3306 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
3307 mutex_unlock(&dev_priv->drm.struct_mutex);
3309 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3310 dev_priv->ggtt.mappable_base,
3311 dev_priv->ggtt.mappable_end)) {
3313 goto out_gtt_cleanup;
3316 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3319 * Initialise stolen early so that we may reserve preallocated
3320 * objects for the BIOS to KMS transition.
3322 ret = i915_gem_init_stolen(dev_priv);
3324 goto out_gtt_cleanup;
3329 ggtt->base.cleanup(&ggtt->base);
3333 int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
3335 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
3341 void i915_ggtt_enable_guc(struct drm_i915_private *i915)
3343 i915->ggtt.invalidate = guc_ggtt_invalidate;
3346 void i915_ggtt_disable_guc(struct drm_i915_private *i915)
3348 i915->ggtt.invalidate = gen6_ggtt_invalidate;
3351 void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
3353 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3354 struct drm_i915_gem_object *obj, *on;
3356 i915_check_and_clear_faults(dev_priv);
3358 /* First fill our portion of the GTT with scratch pages */
3359 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
3361 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3363 /* clflush objects bound into the GGTT and rebind them. */
3364 list_for_each_entry_safe(obj, on,
3365 &dev_priv->mm.bound_list, global_link) {
3366 bool ggtt_bound = false;
3367 struct i915_vma *vma;
3369 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3370 if (vma->vm != &ggtt->base)
3373 if (!i915_vma_unbind(vma))
3376 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3382 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
3385 ggtt->base.closed = false;
3387 if (INTEL_GEN(dev_priv) >= 8) {
3388 if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
3389 chv_setup_private_ppat(dev_priv);
3391 bdw_setup_private_ppat(dev_priv);
3396 if (USES_PPGTT(dev_priv)) {
3397 struct i915_address_space *vm;
3399 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3400 /* TODO: Perhaps it shouldn't be gen6 specific */
3402 struct i915_hw_ppgtt *ppgtt;
3404 if (i915_is_ggtt(vm))
3405 ppgtt = dev_priv->mm.aliasing_ppgtt;
3407 ppgtt = i915_vm_to_ppgtt(vm);
3409 gen6_write_page_range(dev_priv, &ppgtt->pd,
3410 0, ppgtt->base.total);
3414 i915_ggtt_invalidate(dev_priv);
3417 static struct scatterlist *
3418 rotate_pages(const dma_addr_t *in, unsigned int offset,
3419 unsigned int width, unsigned int height,
3420 unsigned int stride,
3421 struct sg_table *st, struct scatterlist *sg)
3423 unsigned int column, row;
3424 unsigned int src_idx;
3426 for (column = 0; column < width; column++) {
3427 src_idx = stride * (height - 1) + column;
3428 for (row = 0; row < height; row++) {
3430 /* We don't need the pages, but need to initialize
3431 * the entries so the sg list can be happily traversed.
3432 * The only thing we need are DMA addresses.
3434 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3435 sg_dma_address(sg) = in[offset + src_idx];
3436 sg_dma_len(sg) = PAGE_SIZE;
3445 static noinline struct sg_table *
3446 intel_rotate_pages(struct intel_rotation_info *rot_info,
3447 struct drm_i915_gem_object *obj)
3449 const size_t n_pages = obj->base.size / PAGE_SIZE;
3450 unsigned int size = intel_rotation_info_size(rot_info);
3451 struct sgt_iter sgt_iter;
3452 dma_addr_t dma_addr;
3454 dma_addr_t *page_addr_list;
3455 struct sg_table *st;
3456 struct scatterlist *sg;
3459 /* Allocate a temporary list of source pages for random access. */
3460 page_addr_list = drm_malloc_gfp(n_pages,
3463 if (!page_addr_list)
3464 return ERR_PTR(ret);
3466 /* Allocate target SG list. */
3467 st = kmalloc(sizeof(*st), GFP_KERNEL);
3471 ret = sg_alloc_table(st, size, GFP_KERNEL);
3475 /* Populate source page list from the object. */
3477 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
3478 page_addr_list[i++] = dma_addr;
3480 GEM_BUG_ON(i != n_pages);
3484 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3485 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3486 rot_info->plane[i].width, rot_info->plane[i].height,
3487 rot_info->plane[i].stride, st, sg);
3490 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3491 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3493 drm_free_large(page_addr_list);
3500 drm_free_large(page_addr_list);
3502 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3503 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3505 return ERR_PTR(ret);
3508 static noinline struct sg_table *
3509 intel_partial_pages(const struct i915_ggtt_view *view,
3510 struct drm_i915_gem_object *obj)
3512 struct sg_table *st;
3513 struct scatterlist *sg, *iter;
3514 unsigned int count = view->partial.size;
3515 unsigned int offset;
3518 st = kmalloc(sizeof(*st), GFP_KERNEL);
3522 ret = sg_alloc_table(st, count, GFP_KERNEL);
3526 iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
3534 len = min(iter->length - (offset << PAGE_SHIFT),
3535 count << PAGE_SHIFT);
3536 sg_set_page(sg, NULL, len, 0);
3537 sg_dma_address(sg) =
3538 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3539 sg_dma_len(sg) = len;
3542 count -= len >> PAGE_SHIFT;
3549 iter = __sg_next(iter);
3556 return ERR_PTR(ret);
3560 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3564 /* The vma->pages are only valid within the lifespan of the borrowed
3565 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3566 * must be the vma->pages. A simple rule is that vma->pages must only
3567 * be accessed when the obj->mm.pages are pinned.
3569 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3571 switch (vma->ggtt_view.type) {
3572 case I915_GGTT_VIEW_NORMAL:
3573 vma->pages = vma->obj->mm.pages;
3576 case I915_GGTT_VIEW_ROTATED:
3578 intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
3581 case I915_GGTT_VIEW_PARTIAL:
3582 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
3586 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3587 vma->ggtt_view.type);
3592 if (unlikely(IS_ERR(vma->pages))) {
3593 ret = PTR_ERR(vma->pages);
3595 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3596 vma->ggtt_view.type, ret);
3602 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
3603 * @vm: the &struct i915_address_space
3604 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3605 * @size: how much space to allocate inside the GTT,
3606 * must be #I915_GTT_PAGE_SIZE aligned
3607 * @offset: where to insert inside the GTT,
3608 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3609 * (@offset + @size) must fit within the address space
3610 * @color: color to apply to node, if this node is not from a VMA,
3611 * color must be #I915_COLOR_UNEVICTABLE
3612 * @flags: control search and eviction behaviour
3614 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3615 * the address space (using @size and @color). If the @node does not fit, it
3616 * tries to evict any overlapping nodes from the GTT, including any
3617 * neighbouring nodes if the colors do not match (to ensure guard pages between
3618 * differing domains). See i915_gem_evict_for_node() for the gory details
3619 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3620 * evicting active overlapping objects, and any overlapping node that is pinned
3621 * or marked as unevictable will also result in failure.
3623 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3624 * asked to wait for eviction and interrupted.
3626 int i915_gem_gtt_reserve(struct i915_address_space *vm,
3627 struct drm_mm_node *node,
3628 u64 size, u64 offset, unsigned long color,
3634 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3635 GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
3636 GEM_BUG_ON(range_overflows(offset, size, vm->total));
3637 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
3638 GEM_BUG_ON(drm_mm_node_allocated(node));
3641 node->start = offset;
3642 node->color = color;
3644 err = drm_mm_reserve_node(&vm->mm, node);
3648 err = i915_gem_evict_for_node(vm, node, flags);
3650 err = drm_mm_reserve_node(&vm->mm, node);
3655 static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
3659 GEM_BUG_ON(range_overflows(start, len, end));
3660 GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
3662 range = round_down(end - len, align) - round_up(start, align);
3664 if (sizeof(unsigned long) == sizeof(u64)) {
3665 addr = get_random_long();
3667 addr = get_random_int();
3668 if (range > U32_MAX) {
3670 addr |= get_random_int();
3673 div64_u64_rem(addr, range, &addr);
3677 return round_up(start, align);
3681 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
3682 * @vm: the &struct i915_address_space
3683 * @node: the &struct drm_mm_node (typically i915_vma.node)
3684 * @size: how much space to allocate inside the GTT,
3685 * must be #I915_GTT_PAGE_SIZE aligned
3686 * @alignment: required alignment of starting offset, may be 0 but
3687 * if specified, this must be a power-of-two and at least
3688 * #I915_GTT_MIN_ALIGNMENT
3689 * @color: color to apply to node
3690 * @start: start of any range restriction inside GTT (0 for all),
3691 * must be #I915_GTT_PAGE_SIZE aligned
3692 * @end: end of any range restriction inside GTT (U64_MAX for all),
3693 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3694 * @flags: control search and eviction behaviour
3696 * i915_gem_gtt_insert() first searches for an available hole into which
3697 * is can insert the node. The hole address is aligned to @alignment and
3698 * its @size must then fit entirely within the [@start, @end] bounds. The
3699 * nodes on either side of the hole must match @color, or else a guard page
3700 * will be inserted between the two nodes (or the node evicted). If no
3701 * suitable hole is found, first a victim is randomly selected and tested
3702 * for eviction, otherwise then the LRU list of objects within the GTT
3703 * is scanned to find the first set of replacement nodes to create the hole.
3704 * Those old overlapping nodes are evicted from the GTT (and so must be
3705 * rebound before any future use). Any node that is currently pinned cannot
3706 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3707 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3708 * searching for an eviction candidate. See i915_gem_evict_something() for
3709 * the gory details on the eviction algorithm.
3711 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3712 * asked to wait for eviction and interrupted.
3714 int i915_gem_gtt_insert(struct i915_address_space *vm,
3715 struct drm_mm_node *node,
3716 u64 size, u64 alignment, unsigned long color,
3717 u64 start, u64 end, unsigned int flags)
3719 enum drm_mm_insert_mode mode;
3723 lockdep_assert_held(&vm->i915->drm.struct_mutex);
3725 GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
3726 GEM_BUG_ON(alignment && !is_power_of_2(alignment));
3727 GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
3728 GEM_BUG_ON(start >= end);
3729 GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
3730 GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
3731 GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
3732 GEM_BUG_ON(drm_mm_node_allocated(node));
3734 if (unlikely(range_overflows(start, size, end)))
3737 if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
3740 mode = DRM_MM_INSERT_BEST;
3741 if (flags & PIN_HIGH)
3742 mode = DRM_MM_INSERT_HIGH;
3743 if (flags & PIN_MAPPABLE)
3744 mode = DRM_MM_INSERT_LOW;
3746 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3747 * so we know that we always have a minimum alignment of 4096.
3748 * The drm_mm range manager is optimised to return results
3749 * with zero alignment, so where possible use the optimal
3752 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
3753 if (alignment <= I915_GTT_MIN_ALIGNMENT)
3756 err = drm_mm_insert_node_in_range(&vm->mm, node,
3757 size, alignment, color,
3762 /* No free space, pick a slot at random.
3764 * There is a pathological case here using a GTT shared between
3765 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3767 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3768 * (64k objects) (448k objects)
3770 * Now imagine that the eviction LRU is ordered top-down (just because
3771 * pathology meets real life), and that we need to evict an object to
3772 * make room inside the aperture. The eviction scan then has to walk
3773 * the 448k list before it finds one within range. And now imagine that
3774 * it has to search for a new hole between every byte inside the memcpy,
3775 * for several simultaneous clients.
3777 * On a full-ppgtt system, if we have run out of available space, there
3778 * will be lots and lots of objects in the eviction list! Again,
3779 * searching that LRU list may be slow if we are also applying any
3780 * range restrictions (e.g. restriction to low 4GiB) and so, for
3781 * simplicity and similarilty between different GTT, try the single
3782 * random replacement first.
3784 offset = random_offset(start, end,
3785 size, alignment ?: I915_GTT_MIN_ALIGNMENT);
3786 err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
3790 /* Randomly selected placement is pinned, do a search */
3791 err = i915_gem_evict_something(vm, size, alignment, color,
3796 return drm_mm_insert_node_in_range(&vm->mm, node,
3797 size, alignment, color,
3798 start, end, DRM_MM_INSERT_EVICT);
3801 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3802 #include "selftests/mock_gtt.c"
3803 #include "selftests/i915_gem_gtt.c"