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/seq_file.h>
27 #include <linux/stop_machine.h>
29 #include <drm/i915_drm.h>
31 #include "i915_vgpu.h"
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include "intel_frontbuffer.h"
36 #define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
39 * DOC: Global GTT views
41 * Background and previous state
43 * Historically objects could exists (be bound) in global GTT space only as
44 * singular instances with a view representing all of the object's backing pages
45 * in a linear fashion. This view will be called a normal view.
47 * To support multiple views of the same object, where the number of mapped
48 * pages is not equal to the backing store, or where the layout of the pages
49 * is not linear, concept of a GGTT view was added.
51 * One example of an alternative view is a stereo display driven by a single
52 * image. In this case we would have a framebuffer looking like this
58 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
59 * rendering. In contrast, fed to the display engine would be an alternative
60 * view which could look something like this:
65 * In this example both the size and layout of pages in the alternative view is
66 * different from the normal view.
68 * Implementation and usage
70 * GGTT views are implemented using VMAs and are distinguished via enum
71 * i915_ggtt_view_type and struct i915_ggtt_view.
73 * A new flavour of core GEM functions which work with GGTT bound objects were
74 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
75 * renaming in large amounts of code. They take the struct i915_ggtt_view
76 * parameter encapsulating all metadata required to implement a view.
78 * As a helper for callers which are only interested in the normal view,
79 * globally const i915_ggtt_view_normal singleton instance exists. All old core
80 * GEM API functions, the ones not taking the view parameter, are operating on,
81 * or with the normal GGTT view.
83 * Code wanting to add or use a new GGTT view needs to:
85 * 1. Add a new enum with a suitable name.
86 * 2. Extend the metadata in the i915_ggtt_view structure if required.
87 * 3. Add support to i915_get_vma_pages().
89 * New views are required to build a scatter-gather table from within the
90 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
91 * exists for the lifetime of an VMA.
93 * Core API is designed to have copy semantics which means that passed in
94 * struct i915_ggtt_view does not need to be persistent (left around after
95 * calling the core API functions).
100 i915_get_ggtt_vma_pages(struct i915_vma *vma);
102 const struct i915_ggtt_view i915_ggtt_view_normal = {
103 .type = I915_GGTT_VIEW_NORMAL,
105 const struct i915_ggtt_view i915_ggtt_view_rotated = {
106 .type = I915_GGTT_VIEW_ROTATED,
109 int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
112 bool has_aliasing_ppgtt;
114 bool has_full_48bit_ppgtt;
116 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
117 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
118 has_full_48bit_ppgtt =
119 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
121 if (intel_vgpu_active(dev_priv)) {
122 /* emulation is too hard */
123 has_full_ppgtt = false;
124 has_full_48bit_ppgtt = false;
127 if (!has_aliasing_ppgtt)
131 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
132 * execlists, the sole mechanism available to submit work.
134 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
137 if (enable_ppgtt == 1)
140 if (enable_ppgtt == 2 && has_full_ppgtt)
143 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
146 #ifdef CONFIG_INTEL_IOMMU
147 /* Disable ppgtt on SNB if VT-d is on. */
148 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
149 DRM_INFO("Disabling PPGTT because VT-d is on\n");
154 /* Early VLV doesn't have this */
155 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
156 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
160 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
161 return has_full_48bit_ppgtt ? 3 : 2;
163 return has_aliasing_ppgtt ? 1 : 0;
166 static int ppgtt_bind_vma(struct i915_vma *vma,
167 enum i915_cache_level cache_level,
172 vma->pages = vma->obj->mm.pages;
174 /* Currently applicable only to VLV */
176 pte_flags |= PTE_READ_ONLY;
178 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
179 cache_level, pte_flags);
184 static void ppgtt_unbind_vma(struct i915_vma *vma)
186 vma->vm->clear_range(vma->vm,
191 static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
192 enum i915_cache_level level)
194 gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
198 case I915_CACHE_NONE:
199 pte |= PPAT_UNCACHED_INDEX;
202 pte |= PPAT_DISPLAY_ELLC_INDEX;
205 pte |= PPAT_CACHED_INDEX;
212 static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
213 const enum i915_cache_level level)
215 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
217 if (level != I915_CACHE_NONE)
218 pde |= PPAT_CACHED_PDE_INDEX;
220 pde |= PPAT_UNCACHED_INDEX;
224 #define gen8_pdpe_encode gen8_pde_encode
225 #define gen8_pml4e_encode gen8_pde_encode
227 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
228 enum i915_cache_level level,
231 gen6_pte_t pte = GEN6_PTE_VALID;
232 pte |= GEN6_PTE_ADDR_ENCODE(addr);
235 case I915_CACHE_L3_LLC:
237 pte |= GEN6_PTE_CACHE_LLC;
239 case I915_CACHE_NONE:
240 pte |= GEN6_PTE_UNCACHED;
249 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
250 enum i915_cache_level level,
253 gen6_pte_t pte = GEN6_PTE_VALID;
254 pte |= GEN6_PTE_ADDR_ENCODE(addr);
257 case I915_CACHE_L3_LLC:
258 pte |= GEN7_PTE_CACHE_L3_LLC;
261 pte |= GEN6_PTE_CACHE_LLC;
263 case I915_CACHE_NONE:
264 pte |= GEN6_PTE_UNCACHED;
273 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
274 enum i915_cache_level level,
277 gen6_pte_t pte = GEN6_PTE_VALID;
278 pte |= GEN6_PTE_ADDR_ENCODE(addr);
280 if (!(flags & PTE_READ_ONLY))
281 pte |= BYT_PTE_WRITEABLE;
283 if (level != I915_CACHE_NONE)
284 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
289 static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
290 enum i915_cache_level level,
293 gen6_pte_t pte = GEN6_PTE_VALID;
294 pte |= HSW_PTE_ADDR_ENCODE(addr);
296 if (level != I915_CACHE_NONE)
297 pte |= HSW_WB_LLC_AGE3;
302 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
303 enum i915_cache_level level,
306 gen6_pte_t pte = GEN6_PTE_VALID;
307 pte |= HSW_PTE_ADDR_ENCODE(addr);
310 case I915_CACHE_NONE:
313 pte |= HSW_WT_ELLC_LLC_AGE3;
316 pte |= HSW_WB_ELLC_LLC_AGE3;
323 static int __setup_page_dma(struct drm_i915_private *dev_priv,
324 struct i915_page_dma *p, gfp_t flags)
326 struct device *kdev = &dev_priv->drm.pdev->dev;
328 p->page = alloc_page(flags);
332 p->daddr = dma_map_page(kdev,
333 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
335 if (dma_mapping_error(kdev, p->daddr)) {
336 __free_page(p->page);
343 static int setup_page_dma(struct drm_i915_private *dev_priv,
344 struct i915_page_dma *p)
346 return __setup_page_dma(dev_priv, p, I915_GFP_DMA);
349 static void cleanup_page_dma(struct drm_i915_private *dev_priv,
350 struct i915_page_dma *p)
352 struct pci_dev *pdev = dev_priv->drm.pdev;
354 if (WARN_ON(!p->page))
357 dma_unmap_page(&pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
358 __free_page(p->page);
359 memset(p, 0, sizeof(*p));
362 static void *kmap_page_dma(struct i915_page_dma *p)
364 return kmap_atomic(p->page);
367 /* We use the flushing unmap only with ppgtt structures:
368 * page directories, page tables and scratch pages.
370 static void kunmap_page_dma(struct drm_i915_private *dev_priv, void *vaddr)
372 /* There are only few exceptions for gen >=6. chv and bxt.
373 * And we are not sure about the latter so play safe for now.
375 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
376 drm_clflush_virt_range(vaddr, PAGE_SIZE);
378 kunmap_atomic(vaddr);
381 #define kmap_px(px) kmap_page_dma(px_base(px))
382 #define kunmap_px(ppgtt, vaddr) \
383 kunmap_page_dma(to_i915((ppgtt)->base.dev), (vaddr))
385 #define setup_px(dev_priv, px) setup_page_dma((dev_priv), px_base(px))
386 #define cleanup_px(dev_priv, px) cleanup_page_dma((dev_priv), px_base(px))
387 #define fill_px(dev_priv, px, v) fill_page_dma((dev_priv), px_base(px), (v))
388 #define fill32_px(dev_priv, px, v) \
389 fill_page_dma_32((dev_priv), px_base(px), (v))
391 static void fill_page_dma(struct drm_i915_private *dev_priv,
392 struct i915_page_dma *p, const uint64_t val)
395 uint64_t * const vaddr = kmap_page_dma(p);
397 for (i = 0; i < 512; i++)
400 kunmap_page_dma(dev_priv, vaddr);
403 static void fill_page_dma_32(struct drm_i915_private *dev_priv,
404 struct i915_page_dma *p, const uint32_t val32)
410 fill_page_dma(dev_priv, p, v);
414 setup_scratch_page(struct drm_i915_private *dev_priv,
415 struct i915_page_dma *scratch,
418 return __setup_page_dma(dev_priv, scratch, gfp | __GFP_ZERO);
421 static void cleanup_scratch_page(struct drm_i915_private *dev_priv,
422 struct i915_page_dma *scratch)
424 cleanup_page_dma(dev_priv, scratch);
427 static struct i915_page_table *alloc_pt(struct drm_i915_private *dev_priv)
429 struct i915_page_table *pt;
430 const size_t count = INTEL_GEN(dev_priv) >= 8 ? GEN8_PTES : GEN6_PTES;
433 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
435 return ERR_PTR(-ENOMEM);
437 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
443 ret = setup_px(dev_priv, pt);
450 kfree(pt->used_ptes);
457 static void free_pt(struct drm_i915_private *dev_priv,
458 struct i915_page_table *pt)
460 cleanup_px(dev_priv, pt);
461 kfree(pt->used_ptes);
465 static void gen8_initialize_pt(struct i915_address_space *vm,
466 struct i915_page_table *pt)
468 gen8_pte_t scratch_pte;
470 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
473 fill_px(to_i915(vm->dev), pt, scratch_pte);
476 static void gen6_initialize_pt(struct i915_address_space *vm,
477 struct i915_page_table *pt)
479 gen6_pte_t scratch_pte;
481 WARN_ON(vm->scratch_page.daddr == 0);
483 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
486 fill32_px(to_i915(vm->dev), pt, scratch_pte);
489 static struct i915_page_directory *alloc_pd(struct drm_i915_private *dev_priv)
491 struct i915_page_directory *pd;
494 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
496 return ERR_PTR(-ENOMEM);
498 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
499 sizeof(*pd->used_pdes), GFP_KERNEL);
503 ret = setup_px(dev_priv, pd);
510 kfree(pd->used_pdes);
517 static void free_pd(struct drm_i915_private *dev_priv,
518 struct i915_page_directory *pd)
521 cleanup_px(dev_priv, pd);
522 kfree(pd->used_pdes);
527 static void gen8_initialize_pd(struct i915_address_space *vm,
528 struct i915_page_directory *pd)
530 gen8_pde_t scratch_pde;
532 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
534 fill_px(to_i915(vm->dev), pd, scratch_pde);
537 static int __pdp_init(struct drm_i915_private *dev_priv,
538 struct i915_page_directory_pointer *pdp)
540 size_t pdpes = I915_PDPES_PER_PDP(dev_priv);
542 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
543 sizeof(unsigned long),
545 if (!pdp->used_pdpes)
548 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
550 if (!pdp->page_directory) {
551 kfree(pdp->used_pdpes);
552 /* the PDP might be the statically allocated top level. Keep it
553 * as clean as possible */
554 pdp->used_pdpes = NULL;
561 static void __pdp_fini(struct i915_page_directory_pointer *pdp)
563 kfree(pdp->used_pdpes);
564 kfree(pdp->page_directory);
565 pdp->page_directory = NULL;
569 i915_page_directory_pointer *alloc_pdp(struct drm_i915_private *dev_priv)
571 struct i915_page_directory_pointer *pdp;
574 WARN_ON(!USES_FULL_48BIT_PPGTT(dev_priv));
576 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
578 return ERR_PTR(-ENOMEM);
580 ret = __pdp_init(dev_priv, pdp);
584 ret = setup_px(dev_priv, pdp);
598 static void free_pdp(struct drm_i915_private *dev_priv,
599 struct i915_page_directory_pointer *pdp)
602 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
603 cleanup_px(dev_priv, pdp);
608 static void gen8_initialize_pdp(struct i915_address_space *vm,
609 struct i915_page_directory_pointer *pdp)
611 gen8_ppgtt_pdpe_t scratch_pdpe;
613 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
615 fill_px(to_i915(vm->dev), pdp, scratch_pdpe);
618 static void gen8_initialize_pml4(struct i915_address_space *vm,
619 struct i915_pml4 *pml4)
621 gen8_ppgtt_pml4e_t scratch_pml4e;
623 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
626 fill_px(to_i915(vm->dev), pml4, scratch_pml4e);
630 gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
631 struct i915_page_directory_pointer *pdp,
632 struct i915_page_directory *pd,
635 gen8_ppgtt_pdpe_t *page_directorypo;
637 if (!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)))
640 page_directorypo = kmap_px(pdp);
641 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
642 kunmap_px(ppgtt, page_directorypo);
646 gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
647 struct i915_pml4 *pml4,
648 struct i915_page_directory_pointer *pdp,
651 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
653 WARN_ON(!USES_FULL_48BIT_PPGTT(to_i915(ppgtt->base.dev)));
654 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
655 kunmap_px(ppgtt, pagemap);
658 /* Broadwell Page Directory Pointer Descriptors */
659 static int gen8_write_pdp(struct drm_i915_gem_request *req,
663 struct intel_ring *ring = req->ring;
664 struct intel_engine_cs *engine = req->engine;
669 ret = intel_ring_begin(req, 6);
673 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
674 intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, entry));
675 intel_ring_emit(ring, upper_32_bits(addr));
676 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
677 intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, entry));
678 intel_ring_emit(ring, lower_32_bits(addr));
679 intel_ring_advance(ring);
684 static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
685 struct drm_i915_gem_request *req)
689 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
690 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
692 ret = gen8_write_pdp(req, i, pd_daddr);
700 static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
701 struct drm_i915_gem_request *req)
703 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
706 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
707 * the page table structures, we mark them dirty so that
708 * context switching/execlist queuing code takes extra steps
709 * to ensure that tlbs are flushed.
711 static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
713 ppgtt->pd_dirty_rings = INTEL_INFO(to_i915(ppgtt->base.dev))->ring_mask;
716 /* Removes entries from a single page table, releasing it if it's empty.
717 * Caller can use the return value to update higher-level entries.
719 static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
720 struct i915_page_table *pt,
724 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
725 unsigned int num_entries = gen8_pte_count(start, length);
726 unsigned int pte = gen8_pte_index(start);
727 unsigned int pte_end = pte + num_entries;
728 gen8_pte_t *pt_vaddr;
729 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
732 if (WARN_ON(!px_page(pt)))
735 GEM_BUG_ON(pte_end > GEN8_PTES);
737 bitmap_clear(pt->used_ptes, pte, num_entries);
738 if (USES_FULL_PPGTT(vm->i915)) {
739 if (bitmap_empty(pt->used_ptes, GEN8_PTES))
743 pt_vaddr = kmap_px(pt);
745 while (pte < pte_end)
746 pt_vaddr[pte++] = scratch_pte;
748 kunmap_px(ppgtt, pt_vaddr);
753 /* Removes entries from a single page dir, releasing it if it's empty.
754 * Caller can use the return value to update higher-level entries
756 static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
757 struct i915_page_directory *pd,
761 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
762 struct i915_page_table *pt;
764 gen8_pde_t *pde_vaddr;
765 gen8_pde_t scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt),
768 gen8_for_each_pde(pt, pd, start, length, pde) {
769 if (WARN_ON(!pd->page_table[pde]))
772 if (gen8_ppgtt_clear_pt(vm, pt, start, length)) {
773 __clear_bit(pde, pd->used_pdes);
774 pde_vaddr = kmap_px(pd);
775 pde_vaddr[pde] = scratch_pde;
776 kunmap_px(ppgtt, pde_vaddr);
777 free_pt(to_i915(vm->dev), pt);
781 if (bitmap_empty(pd->used_pdes, I915_PDES))
787 /* Removes entries from a single page dir pointer, releasing it if it's empty.
788 * Caller can use the return value to update higher-level entries
790 static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
791 struct i915_page_directory_pointer *pdp,
795 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
796 struct i915_page_directory *pd;
798 gen8_ppgtt_pdpe_t *pdpe_vaddr;
799 gen8_ppgtt_pdpe_t scratch_pdpe =
800 gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
802 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
803 if (WARN_ON(!pdp->page_directory[pdpe]))
806 if (gen8_ppgtt_clear_pd(vm, pd, start, length)) {
807 __clear_bit(pdpe, pdp->used_pdpes);
808 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
809 pdpe_vaddr = kmap_px(pdp);
810 pdpe_vaddr[pdpe] = scratch_pdpe;
811 kunmap_px(ppgtt, pdpe_vaddr);
813 free_pd(to_i915(vm->dev), pd);
817 mark_tlbs_dirty(ppgtt);
819 if (bitmap_empty(pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)))
825 /* Removes entries from a single pml4.
826 * This is the top-level structure in 4-level page tables used on gen8+.
827 * Empty entries are always scratch pml4e.
829 static void gen8_ppgtt_clear_pml4(struct i915_address_space *vm,
830 struct i915_pml4 *pml4,
834 struct drm_i915_private *dev_priv = to_i915(vm->dev);
835 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
836 struct i915_page_directory_pointer *pdp;
838 gen8_ppgtt_pml4e_t *pml4e_vaddr;
839 gen8_ppgtt_pml4e_t scratch_pml4e =
840 gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC);
842 GEM_BUG_ON(!USES_FULL_48BIT_PPGTT(to_i915(vm->dev)));
844 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
845 if (WARN_ON(!pml4->pdps[pml4e]))
848 if (gen8_ppgtt_clear_pdp(vm, pdp, start, length)) {
849 __clear_bit(pml4e, pml4->used_pml4es);
850 pml4e_vaddr = kmap_px(pml4);
851 pml4e_vaddr[pml4e] = scratch_pml4e;
852 kunmap_px(ppgtt, pml4e_vaddr);
853 free_pdp(dev_priv, pdp);
858 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
859 uint64_t start, uint64_t length)
861 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
863 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
864 gen8_ppgtt_clear_pml4(vm, &ppgtt->pml4, start, length);
866 gen8_ppgtt_clear_pdp(vm, &ppgtt->pdp, start, length);
870 gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
871 struct i915_page_directory_pointer *pdp,
872 struct sg_page_iter *sg_iter,
874 enum i915_cache_level cache_level)
876 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
877 gen8_pte_t *pt_vaddr;
878 unsigned pdpe = gen8_pdpe_index(start);
879 unsigned pde = gen8_pde_index(start);
880 unsigned pte = gen8_pte_index(start);
884 while (__sg_page_iter_next(sg_iter)) {
885 if (pt_vaddr == NULL) {
886 struct i915_page_directory *pd = pdp->page_directory[pdpe];
887 struct i915_page_table *pt = pd->page_table[pde];
888 pt_vaddr = kmap_px(pt);
892 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
894 if (++pte == GEN8_PTES) {
895 kunmap_px(ppgtt, pt_vaddr);
897 if (++pde == I915_PDES) {
898 if (++pdpe == I915_PDPES_PER_PDP(to_i915(vm->dev)))
907 kunmap_px(ppgtt, pt_vaddr);
910 static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
911 struct sg_table *pages,
913 enum i915_cache_level cache_level,
916 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
917 struct sg_page_iter sg_iter;
919 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
921 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
922 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
925 struct i915_page_directory_pointer *pdp;
927 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
929 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
930 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
936 static void gen8_free_page_tables(struct drm_i915_private *dev_priv,
937 struct i915_page_directory *pd)
944 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
945 if (WARN_ON(!pd->page_table[i]))
948 free_pt(dev_priv, pd->page_table[i]);
949 pd->page_table[i] = NULL;
953 static int gen8_init_scratch(struct i915_address_space *vm)
955 struct drm_i915_private *dev_priv = to_i915(vm->dev);
958 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
962 vm->scratch_pt = alloc_pt(dev_priv);
963 if (IS_ERR(vm->scratch_pt)) {
964 ret = PTR_ERR(vm->scratch_pt);
965 goto free_scratch_page;
968 vm->scratch_pd = alloc_pd(dev_priv);
969 if (IS_ERR(vm->scratch_pd)) {
970 ret = PTR_ERR(vm->scratch_pd);
974 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
975 vm->scratch_pdp = alloc_pdp(dev_priv);
976 if (IS_ERR(vm->scratch_pdp)) {
977 ret = PTR_ERR(vm->scratch_pdp);
982 gen8_initialize_pt(vm, vm->scratch_pt);
983 gen8_initialize_pd(vm, vm->scratch_pd);
984 if (USES_FULL_48BIT_PPGTT(dev_priv))
985 gen8_initialize_pdp(vm, vm->scratch_pdp);
990 free_pd(dev_priv, vm->scratch_pd);
992 free_pt(dev_priv, vm->scratch_pt);
994 cleanup_scratch_page(dev_priv, &vm->scratch_page);
999 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
1001 enum vgt_g2v_type msg;
1002 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1005 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1006 u64 daddr = px_dma(&ppgtt->pml4);
1008 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
1009 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
1011 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
1012 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
1014 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
1015 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
1017 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
1018 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
1021 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
1022 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
1025 I915_WRITE(vgtif_reg(g2v_notify), msg);
1030 static void gen8_free_scratch(struct i915_address_space *vm)
1032 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1034 if (USES_FULL_48BIT_PPGTT(dev_priv))
1035 free_pdp(dev_priv, vm->scratch_pdp);
1036 free_pd(dev_priv, vm->scratch_pd);
1037 free_pt(dev_priv, vm->scratch_pt);
1038 cleanup_scratch_page(dev_priv, &vm->scratch_page);
1041 static void gen8_ppgtt_cleanup_3lvl(struct drm_i915_private *dev_priv,
1042 struct i915_page_directory_pointer *pdp)
1046 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev_priv)) {
1047 if (WARN_ON(!pdp->page_directory[i]))
1050 gen8_free_page_tables(dev_priv, pdp->page_directory[i]);
1051 free_pd(dev_priv, pdp->page_directory[i]);
1054 free_pdp(dev_priv, pdp);
1057 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
1059 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1062 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
1063 if (WARN_ON(!ppgtt->pml4.pdps[i]))
1066 gen8_ppgtt_cleanup_3lvl(dev_priv, ppgtt->pml4.pdps[i]);
1069 cleanup_px(dev_priv, &ppgtt->pml4);
1072 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1074 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1075 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1077 if (intel_vgpu_active(dev_priv))
1078 gen8_ppgtt_notify_vgt(ppgtt, false);
1080 if (!USES_FULL_48BIT_PPGTT(dev_priv))
1081 gen8_ppgtt_cleanup_3lvl(dev_priv, &ppgtt->pdp);
1083 gen8_ppgtt_cleanup_4lvl(ppgtt);
1085 gen8_free_scratch(vm);
1089 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
1090 * @vm: Master vm structure.
1091 * @pd: Page directory for this address range.
1092 * @start: Starting virtual address to begin allocations.
1093 * @length: Size of the allocations.
1094 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1095 * caller to free on error.
1097 * Allocate the required number of page tables. Extremely similar to
1098 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1099 * the page directory boundary (instead of the page directory pointer). That
1100 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1101 * possible, and likely that the caller will need to use multiple calls of this
1102 * function to achieve the appropriate allocation.
1104 * Return: 0 if success; negative error code otherwise.
1106 static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
1107 struct i915_page_directory *pd,
1110 unsigned long *new_pts)
1112 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1113 struct i915_page_table *pt;
1116 gen8_for_each_pde(pt, pd, start, length, pde) {
1117 /* Don't reallocate page tables */
1118 if (test_bit(pde, pd->used_pdes)) {
1119 /* Scratch is never allocated this way */
1120 WARN_ON(pt == vm->scratch_pt);
1124 pt = alloc_pt(dev_priv);
1128 gen8_initialize_pt(vm, pt);
1129 pd->page_table[pde] = pt;
1130 __set_bit(pde, new_pts);
1131 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
1137 for_each_set_bit(pde, new_pts, I915_PDES)
1138 free_pt(dev_priv, pd->page_table[pde]);
1144 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
1145 * @vm: Master vm structure.
1146 * @pdp: Page directory pointer for this address range.
1147 * @start: Starting virtual address to begin allocations.
1148 * @length: Size of the allocations.
1149 * @new_pds: Bitmap set by function with new allocations. Likely used by the
1150 * caller to free on error.
1152 * Allocate the required number of page directories starting at the pde index of
1153 * @start, and ending at the pde index @start + @length. This function will skip
1154 * over already allocated page directories within the range, and only allocate
1155 * new ones, setting the appropriate pointer within the pdp as well as the
1156 * correct position in the bitmap @new_pds.
1158 * The function will only allocate the pages within the range for a give page
1159 * directory pointer. In other words, if @start + @length straddles a virtually
1160 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1161 * required by the caller, This is not currently possible, and the BUG in the
1162 * code will prevent it.
1164 * Return: 0 if success; negative error code otherwise.
1167 gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1168 struct i915_page_directory_pointer *pdp,
1171 unsigned long *new_pds)
1173 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1174 struct i915_page_directory *pd;
1176 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
1178 WARN_ON(!bitmap_empty(new_pds, pdpes));
1180 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1181 if (test_bit(pdpe, pdp->used_pdpes))
1184 pd = alloc_pd(dev_priv);
1188 gen8_initialize_pd(vm, pd);
1189 pdp->page_directory[pdpe] = pd;
1190 __set_bit(pdpe, new_pds);
1191 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
1197 for_each_set_bit(pdpe, new_pds, pdpes)
1198 free_pd(dev_priv, pdp->page_directory[pdpe]);
1204 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1205 * @vm: Master vm structure.
1206 * @pml4: Page map level 4 for this address range.
1207 * @start: Starting virtual address to begin allocations.
1208 * @length: Size of the allocations.
1209 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1210 * caller to free on error.
1212 * Allocate the required number of page directory pointers. Extremely similar to
1213 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1214 * The main difference is here we are limited by the pml4 boundary (instead of
1215 * the page directory pointer).
1217 * Return: 0 if success; negative error code otherwise.
1220 gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1221 struct i915_pml4 *pml4,
1224 unsigned long *new_pdps)
1226 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1227 struct i915_page_directory_pointer *pdp;
1230 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1232 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1233 if (!test_bit(pml4e, pml4->used_pml4es)) {
1234 pdp = alloc_pdp(dev_priv);
1238 gen8_initialize_pdp(vm, pdp);
1239 pml4->pdps[pml4e] = pdp;
1240 __set_bit(pml4e, new_pdps);
1241 trace_i915_page_directory_pointer_entry_alloc(vm,
1251 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1252 free_pdp(dev_priv, pml4->pdps[pml4e]);
1258 free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
1264 /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1265 * of these are based on the number of PDPEs in the system.
1268 int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
1269 unsigned long **new_pts,
1275 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
1279 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1290 free_gen8_temp_bitmaps(pds, pts);
1294 static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1295 struct i915_page_directory_pointer *pdp,
1299 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1300 unsigned long *new_page_dirs, *new_page_tables;
1301 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1302 struct i915_page_directory *pd;
1303 const uint64_t orig_start = start;
1304 const uint64_t orig_length = length;
1306 uint32_t pdpes = I915_PDPES_PER_PDP(dev_priv);
1309 /* Wrap is never okay since we can only represent 48b, and we don't
1310 * actually use the other side of the canonical address space.
1312 if (WARN_ON(start + length < start))
1315 if (WARN_ON(start + length > vm->total))
1318 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1322 /* Do the allocations first so we can easily bail out */
1323 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1326 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1330 /* For every page directory referenced, allocate page tables */
1331 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1332 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
1333 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
1339 length = orig_length;
1341 /* Allocations have completed successfully, so set the bitmaps, and do
1343 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1344 gen8_pde_t *const page_directory = kmap_px(pd);
1345 struct i915_page_table *pt;
1346 uint64_t pd_len = length;
1347 uint64_t pd_start = start;
1350 /* Every pd should be allocated, we just did that above. */
1353 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1354 /* Same reasoning as pd */
1357 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1359 /* Set our used ptes within the page table */
1360 bitmap_set(pt->used_ptes,
1361 gen8_pte_index(pd_start),
1362 gen8_pte_count(pd_start, pd_len));
1364 /* Our pde is now pointing to the pagetable, pt */
1365 __set_bit(pde, pd->used_pdes);
1367 /* Map the PDE to the page table */
1368 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1370 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1371 gen8_pte_index(start),
1372 gen8_pte_count(start, length),
1375 /* NB: We haven't yet mapped ptes to pages. At this
1376 * point we're still relying on insert_entries() */
1379 kunmap_px(ppgtt, page_directory);
1380 __set_bit(pdpe, pdp->used_pdpes);
1381 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
1384 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1385 mark_tlbs_dirty(ppgtt);
1392 for_each_set_bit(temp, new_page_tables + pdpe *
1393 BITS_TO_LONGS(I915_PDES), I915_PDES)
1395 pdp->page_directory[pdpe]->page_table[temp]);
1398 for_each_set_bit(pdpe, new_page_dirs, pdpes)
1399 free_pd(dev_priv, pdp->page_directory[pdpe]);
1401 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1402 mark_tlbs_dirty(ppgtt);
1406 static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1407 struct i915_pml4 *pml4,
1411 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
1412 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1413 struct i915_page_directory_pointer *pdp;
1417 /* Do the pml4 allocations first, so we don't need to track the newly
1418 * allocated tables below the pdp */
1419 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1421 /* The pagedirectory and pagetable allocations are done in the shared 3
1422 * and 4 level code. Just allocate the pdps.
1424 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1429 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1430 "The allocation has spanned more than 512GB. "
1431 "It is highly likely this is incorrect.");
1433 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1436 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1440 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1443 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1444 GEN8_PML4ES_PER_PML4);
1449 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1450 gen8_ppgtt_cleanup_3lvl(to_i915(vm->dev), pml4->pdps[pml4e]);
1455 static int gen8_alloc_va_range(struct i915_address_space *vm,
1456 uint64_t start, uint64_t length)
1458 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1460 if (USES_FULL_48BIT_PPGTT(to_i915(vm->dev)))
1461 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1463 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1466 static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1467 uint64_t start, uint64_t length,
1468 gen8_pte_t scratch_pte,
1471 struct i915_page_directory *pd;
1474 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1475 struct i915_page_table *pt;
1476 uint64_t pd_len = length;
1477 uint64_t pd_start = start;
1480 if (!test_bit(pdpe, pdp->used_pdpes))
1483 seq_printf(m, "\tPDPE #%d\n", pdpe);
1484 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1486 gen8_pte_t *pt_vaddr;
1488 if (!test_bit(pde, pd->used_pdes))
1491 pt_vaddr = kmap_px(pt);
1492 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1494 (pdpe << GEN8_PDPE_SHIFT) |
1495 (pde << GEN8_PDE_SHIFT) |
1496 (pte << GEN8_PTE_SHIFT);
1500 for (i = 0; i < 4; i++)
1501 if (pt_vaddr[pte + i] != scratch_pte)
1506 seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1507 for (i = 0; i < 4; i++) {
1508 if (pt_vaddr[pte + i] != scratch_pte)
1509 seq_printf(m, " %llx", pt_vaddr[pte + i]);
1511 seq_puts(m, " SCRATCH ");
1515 /* don't use kunmap_px, it could trigger
1516 * an unnecessary flush.
1518 kunmap_atomic(pt_vaddr);
1523 static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1525 struct i915_address_space *vm = &ppgtt->base;
1526 uint64_t start = ppgtt->base.start;
1527 uint64_t length = ppgtt->base.total;
1528 gen8_pte_t scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
1531 if (!USES_FULL_48BIT_PPGTT(to_i915(vm->dev))) {
1532 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1535 struct i915_pml4 *pml4 = &ppgtt->pml4;
1536 struct i915_page_directory_pointer *pdp;
1538 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1539 if (!test_bit(pml4e, pml4->used_pml4es))
1542 seq_printf(m, " PML4E #%llu\n", pml4e);
1543 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1548 static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1550 unsigned long *new_page_dirs, *new_page_tables;
1551 uint32_t pdpes = I915_PDPES_PER_PDP(to_i915(ppgtt->base.dev));
1554 /* We allocate temp bitmap for page tables for no gain
1555 * but as this is for init only, lets keep the things simple
1557 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1561 /* Allocate for all pdps regardless of how the ppgtt
1564 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1568 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1570 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1576 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1577 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1578 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1582 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1584 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1587 ret = gen8_init_scratch(&ppgtt->base);
1591 ppgtt->base.start = 0;
1592 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1593 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1594 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
1595 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1596 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1597 ppgtt->base.bind_vma = ppgtt_bind_vma;
1598 ppgtt->debug_dump = gen8_dump_ppgtt;
1600 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
1601 ret = setup_px(dev_priv, &ppgtt->pml4);
1605 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1607 ppgtt->base.total = 1ULL << 48;
1608 ppgtt->switch_mm = gen8_48b_mm_switch;
1610 ret = __pdp_init(dev_priv, &ppgtt->pdp);
1614 ppgtt->base.total = 1ULL << 32;
1615 ppgtt->switch_mm = gen8_legacy_mm_switch;
1616 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1620 if (intel_vgpu_active(dev_priv)) {
1621 ret = gen8_preallocate_top_level_pdps(ppgtt);
1627 if (intel_vgpu_active(dev_priv))
1628 gen8_ppgtt_notify_vgt(ppgtt, true);
1633 gen8_free_scratch(&ppgtt->base);
1637 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1639 struct i915_address_space *vm = &ppgtt->base;
1640 struct i915_page_table *unused;
1641 gen6_pte_t scratch_pte;
1644 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
1646 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
1649 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
1651 gen6_pte_t *pt_vaddr;
1652 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
1653 pd_entry = readl(ppgtt->pd_addr + pde);
1654 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1656 if (pd_entry != expected)
1657 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1661 seq_printf(m, "\tPDE: %x\n", pd_entry);
1663 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1665 for (pte = 0; pte < GEN6_PTES; pte+=4) {
1667 (pde * PAGE_SIZE * GEN6_PTES) +
1671 for (i = 0; i < 4; i++)
1672 if (pt_vaddr[pte + i] != scratch_pte)
1677 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1678 for (i = 0; i < 4; i++) {
1679 if (pt_vaddr[pte + i] != scratch_pte)
1680 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1682 seq_puts(m, " SCRATCH ");
1686 kunmap_px(ppgtt, pt_vaddr);
1690 /* Write pde (index) from the page directory @pd to the page table @pt */
1691 static void gen6_write_pde(struct i915_page_directory *pd,
1692 const int pde, struct i915_page_table *pt)
1694 /* Caller needs to make sure the write completes if necessary */
1695 struct i915_hw_ppgtt *ppgtt =
1696 container_of(pd, struct i915_hw_ppgtt, pd);
1699 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
1700 pd_entry |= GEN6_PDE_VALID;
1702 writel(pd_entry, ppgtt->pd_addr + pde);
1705 /* Write all the page tables found in the ppgtt structure to incrementing page
1707 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1708 struct i915_page_directory *pd,
1709 uint32_t start, uint32_t length)
1711 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1712 struct i915_page_table *pt;
1715 gen6_for_each_pde(pt, pd, start, length, pde)
1716 gen6_write_pde(pd, pde, pt);
1718 /* Make sure write is complete before other code can use this page
1719 * table. Also require for WC mapped PTEs */
1723 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1725 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1727 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
1730 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1731 struct drm_i915_gem_request *req)
1733 struct intel_ring *ring = req->ring;
1734 struct intel_engine_cs *engine = req->engine;
1737 /* NB: TLBs must be flushed and invalidated before a switch */
1738 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
1742 ret = intel_ring_begin(req, 6);
1746 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1747 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1748 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1749 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1750 intel_ring_emit(ring, get_pd_offset(ppgtt));
1751 intel_ring_emit(ring, MI_NOOP);
1752 intel_ring_advance(ring);
1757 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1758 struct drm_i915_gem_request *req)
1760 struct intel_ring *ring = req->ring;
1761 struct intel_engine_cs *engine = req->engine;
1764 /* NB: TLBs must be flushed and invalidated before a switch */
1765 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
1769 ret = intel_ring_begin(req, 6);
1773 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1774 intel_ring_emit_reg(ring, RING_PP_DIR_DCLV(engine));
1775 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1776 intel_ring_emit_reg(ring, RING_PP_DIR_BASE(engine));
1777 intel_ring_emit(ring, get_pd_offset(ppgtt));
1778 intel_ring_emit(ring, MI_NOOP);
1779 intel_ring_advance(ring);
1781 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1782 if (engine->id != RCS) {
1783 ret = engine->emit_flush(req, EMIT_INVALIDATE | EMIT_FLUSH);
1791 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1792 struct drm_i915_gem_request *req)
1794 struct intel_engine_cs *engine = req->engine;
1795 struct drm_i915_private *dev_priv = req->i915;
1797 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1798 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
1802 static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
1804 struct intel_engine_cs *engine;
1805 enum intel_engine_id id;
1807 for_each_engine(engine, dev_priv, id) {
1808 u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
1809 GEN8_GFX_PPGTT_48B : 0;
1810 I915_WRITE(RING_MODE_GEN7(engine),
1811 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1815 static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
1817 struct intel_engine_cs *engine;
1818 uint32_t ecochk, ecobits;
1819 enum intel_engine_id id;
1821 ecobits = I915_READ(GAC_ECO_BITS);
1822 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1824 ecochk = I915_READ(GAM_ECOCHK);
1825 if (IS_HASWELL(dev_priv)) {
1826 ecochk |= ECOCHK_PPGTT_WB_HSW;
1828 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1829 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1831 I915_WRITE(GAM_ECOCHK, ecochk);
1833 for_each_engine(engine, dev_priv, id) {
1834 /* GFX_MODE is per-ring on gen7+ */
1835 I915_WRITE(RING_MODE_GEN7(engine),
1836 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1840 static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
1842 uint32_t ecochk, gab_ctl, ecobits;
1844 ecobits = I915_READ(GAC_ECO_BITS);
1845 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1846 ECOBITS_PPGTT_CACHE64B);
1848 gab_ctl = I915_READ(GAB_CTL);
1849 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1851 ecochk = I915_READ(GAM_ECOCHK);
1852 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1854 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1857 /* PPGTT support for Sandybdrige/Gen6 and later */
1858 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1862 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1863 gen6_pte_t *pt_vaddr, scratch_pte;
1864 unsigned first_entry = start >> PAGE_SHIFT;
1865 unsigned num_entries = length >> PAGE_SHIFT;
1866 unsigned act_pt = first_entry / GEN6_PTES;
1867 unsigned first_pte = first_entry % GEN6_PTES;
1868 unsigned last_pte, i;
1870 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
1873 while (num_entries) {
1874 last_pte = first_pte + num_entries;
1875 if (last_pte > GEN6_PTES)
1876 last_pte = GEN6_PTES;
1878 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1880 for (i = first_pte; i < last_pte; i++)
1881 pt_vaddr[i] = scratch_pte;
1883 kunmap_px(ppgtt, pt_vaddr);
1885 num_entries -= last_pte - first_pte;
1891 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1892 struct sg_table *pages,
1894 enum i915_cache_level cache_level, u32 flags)
1896 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1897 unsigned first_entry = start >> PAGE_SHIFT;
1898 unsigned act_pt = first_entry / GEN6_PTES;
1899 unsigned act_pte = first_entry % GEN6_PTES;
1900 gen6_pte_t *pt_vaddr = NULL;
1901 struct sgt_iter sgt_iter;
1904 for_each_sgt_dma(addr, sgt_iter, pages) {
1905 if (pt_vaddr == NULL)
1906 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1909 vm->pte_encode(addr, cache_level, flags);
1911 if (++act_pte == GEN6_PTES) {
1912 kunmap_px(ppgtt, pt_vaddr);
1920 kunmap_px(ppgtt, pt_vaddr);
1923 static int gen6_alloc_va_range(struct i915_address_space *vm,
1924 uint64_t start_in, uint64_t length_in)
1926 DECLARE_BITMAP(new_page_tables, I915_PDES);
1927 struct drm_i915_private *dev_priv = to_i915(vm->dev);
1928 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1929 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1930 struct i915_page_table *pt;
1931 uint32_t start, length, start_save, length_save;
1935 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1938 start = start_save = start_in;
1939 length = length_save = length_in;
1941 bitmap_zero(new_page_tables, I915_PDES);
1943 /* The allocation is done in two stages so that we can bail out with
1944 * minimal amount of pain. The first stage finds new page tables that
1945 * need allocation. The second stage marks use ptes within the page
1948 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
1949 if (pt != vm->scratch_pt) {
1950 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1954 /* We've already allocated a page table */
1955 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1957 pt = alloc_pt(dev_priv);
1963 gen6_initialize_pt(vm, pt);
1965 ppgtt->pd.page_table[pde] = pt;
1966 __set_bit(pde, new_page_tables);
1967 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
1971 length = length_save;
1973 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
1974 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1976 bitmap_zero(tmp_bitmap, GEN6_PTES);
1977 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1978 gen6_pte_count(start, length));
1980 if (__test_and_clear_bit(pde, new_page_tables))
1981 gen6_write_pde(&ppgtt->pd, pde, pt);
1983 trace_i915_page_table_entry_map(vm, pde, pt,
1984 gen6_pte_index(start),
1985 gen6_pte_count(start, length),
1987 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
1991 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1993 /* Make sure write is complete before other code can use this page
1994 * table. Also require for WC mapped PTEs */
1997 mark_tlbs_dirty(ppgtt);
2001 for_each_set_bit(pde, new_page_tables, I915_PDES) {
2002 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
2004 ppgtt->pd.page_table[pde] = vm->scratch_pt;
2005 free_pt(dev_priv, pt);
2008 mark_tlbs_dirty(ppgtt);
2012 static int gen6_init_scratch(struct i915_address_space *vm)
2014 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2017 ret = setup_scratch_page(dev_priv, &vm->scratch_page, I915_GFP_DMA);
2021 vm->scratch_pt = alloc_pt(dev_priv);
2022 if (IS_ERR(vm->scratch_pt)) {
2023 cleanup_scratch_page(dev_priv, &vm->scratch_page);
2024 return PTR_ERR(vm->scratch_pt);
2027 gen6_initialize_pt(vm, vm->scratch_pt);
2032 static void gen6_free_scratch(struct i915_address_space *vm)
2034 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2036 free_pt(dev_priv, vm->scratch_pt);
2037 cleanup_scratch_page(dev_priv, &vm->scratch_page);
2040 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
2042 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
2043 struct i915_page_directory *pd = &ppgtt->pd;
2044 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2045 struct i915_page_table *pt;
2048 drm_mm_remove_node(&ppgtt->node);
2050 gen6_for_all_pdes(pt, pd, pde)
2051 if (pt != vm->scratch_pt)
2052 free_pt(dev_priv, pt);
2054 gen6_free_scratch(vm);
2057 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
2059 struct i915_address_space *vm = &ppgtt->base;
2060 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
2061 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2062 bool retried = false;
2065 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2066 * allocator works in address space sizes, so it's multiplied by page
2067 * size. We allocate at the top of the GTT to avoid fragmentation.
2069 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
2071 ret = gen6_init_scratch(vm);
2076 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2077 &ppgtt->node, GEN6_PD_SIZE,
2079 0, ggtt->base.total,
2081 if (ret == -ENOSPC && !retried) {
2082 ret = i915_gem_evict_something(&ggtt->base,
2083 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2085 0, ggtt->base.total,
2098 if (ppgtt->node.start < ggtt->mappable_end)
2099 DRM_DEBUG("Forced to use aperture for PDEs\n");
2104 gen6_free_scratch(vm);
2108 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2110 return gen6_ppgtt_allocate_page_directories(ppgtt);
2113 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2114 uint64_t start, uint64_t length)
2116 struct i915_page_table *unused;
2119 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
2120 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
2123 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2125 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
2126 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2129 ppgtt->base.pte_encode = ggtt->base.pte_encode;
2130 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
2131 ppgtt->switch_mm = gen6_mm_switch;
2132 else if (IS_HASWELL(dev_priv))
2133 ppgtt->switch_mm = hsw_mm_switch;
2134 else if (IS_GEN7(dev_priv))
2135 ppgtt->switch_mm = gen7_mm_switch;
2139 ret = gen6_ppgtt_alloc(ppgtt);
2143 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
2144 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2145 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
2146 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2147 ppgtt->base.bind_vma = ppgtt_bind_vma;
2148 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
2149 ppgtt->base.start = 0;
2150 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
2151 ppgtt->debug_dump = gen6_dump_ppgtt;
2153 ppgtt->pd.base.ggtt_offset =
2154 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2156 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
2157 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2159 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
2161 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2163 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
2164 ppgtt->node.size >> 20,
2165 ppgtt->node.start / PAGE_SIZE);
2167 DRM_DEBUG("Adding PPGTT at offset %x\n",
2168 ppgtt->pd.base.ggtt_offset << 10);
2173 static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2174 struct drm_i915_private *dev_priv)
2176 ppgtt->base.dev = &dev_priv->drm;
2178 if (INTEL_INFO(dev_priv)->gen < 8)
2179 return gen6_ppgtt_init(ppgtt);
2181 return gen8_ppgtt_init(ppgtt);
2184 static void i915_address_space_init(struct i915_address_space *vm,
2185 struct drm_i915_private *dev_priv,
2188 i915_gem_timeline_init(dev_priv, &vm->timeline, name);
2189 drm_mm_init(&vm->mm, vm->start, vm->total);
2190 INIT_LIST_HEAD(&vm->active_list);
2191 INIT_LIST_HEAD(&vm->inactive_list);
2192 INIT_LIST_HEAD(&vm->unbound_list);
2193 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2196 static void i915_address_space_fini(struct i915_address_space *vm)
2198 i915_gem_timeline_fini(&vm->timeline);
2199 drm_mm_takedown(&vm->mm);
2200 list_del(&vm->global_link);
2203 static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
2205 /* This function is for gtt related workarounds. This function is
2206 * called on driver load and after a GPU reset, so you can place
2207 * workarounds here even if they get overwritten by GPU reset.
2209 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
2210 if (IS_BROADWELL(dev_priv))
2211 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2212 else if (IS_CHERRYVIEW(dev_priv))
2213 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2214 else if (IS_SKYLAKE(dev_priv))
2215 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2216 else if (IS_BROXTON(dev_priv))
2217 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2220 static int i915_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
2221 struct drm_i915_private *dev_priv,
2222 struct drm_i915_file_private *file_priv,
2227 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2229 kref_init(&ppgtt->ref);
2230 i915_address_space_init(&ppgtt->base, dev_priv, name);
2231 ppgtt->base.file = file_priv;
2237 int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
2239 gtt_write_workarounds(dev_priv);
2241 /* In the case of execlists, PPGTT is enabled by the context descriptor
2242 * and the PDPs are contained within the context itself. We don't
2243 * need to do anything here. */
2244 if (i915.enable_execlists)
2247 if (!USES_PPGTT(dev_priv))
2250 if (IS_GEN6(dev_priv))
2251 gen6_ppgtt_enable(dev_priv);
2252 else if (IS_GEN7(dev_priv))
2253 gen7_ppgtt_enable(dev_priv);
2254 else if (INTEL_GEN(dev_priv) >= 8)
2255 gen8_ppgtt_enable(dev_priv);
2257 MISSING_CASE(INTEL_GEN(dev_priv));
2262 struct i915_hw_ppgtt *
2263 i915_ppgtt_create(struct drm_i915_private *dev_priv,
2264 struct drm_i915_file_private *fpriv,
2267 struct i915_hw_ppgtt *ppgtt;
2270 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2272 return ERR_PTR(-ENOMEM);
2274 ret = i915_ppgtt_init(ppgtt, dev_priv, fpriv, name);
2277 return ERR_PTR(ret);
2280 trace_i915_ppgtt_create(&ppgtt->base);
2285 void i915_ppgtt_release(struct kref *kref)
2287 struct i915_hw_ppgtt *ppgtt =
2288 container_of(kref, struct i915_hw_ppgtt, ref);
2290 trace_i915_ppgtt_release(&ppgtt->base);
2292 /* vmas should already be unbound and destroyed */
2293 WARN_ON(!list_empty(&ppgtt->base.active_list));
2294 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2295 WARN_ON(!list_empty(&ppgtt->base.unbound_list));
2297 i915_address_space_fini(&ppgtt->base);
2299 ppgtt->base.cleanup(&ppgtt->base);
2303 /* Certain Gen5 chipsets require require idling the GPU before
2304 * unmapping anything from the GTT when VT-d is enabled.
2306 static bool needs_idle_maps(struct drm_i915_private *dev_priv)
2308 #ifdef CONFIG_INTEL_IOMMU
2309 /* Query intel_iommu to see if we need the workaround. Presumably that
2312 if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
2318 void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
2320 struct intel_engine_cs *engine;
2321 enum intel_engine_id id;
2323 if (INTEL_INFO(dev_priv)->gen < 6)
2326 for_each_engine(engine, dev_priv, id) {
2328 fault_reg = I915_READ(RING_FAULT_REG(engine));
2329 if (fault_reg & RING_FAULT_VALID) {
2330 DRM_DEBUG_DRIVER("Unexpected fault\n"
2332 "\tAddress space: %s\n"
2335 fault_reg & LINUX_PAGE_MASK,
2336 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2337 RING_FAULT_SRCID(fault_reg),
2338 RING_FAULT_FAULT_TYPE(fault_reg));
2339 I915_WRITE(RING_FAULT_REG(engine),
2340 fault_reg & ~RING_FAULT_VALID);
2344 /* Engine specific init may not have been done till this point. */
2345 if (dev_priv->engine[RCS])
2346 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
2349 static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2351 if (INTEL_INFO(dev_priv)->gen < 6) {
2352 intel_gtt_chipset_flush();
2354 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2355 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2359 void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
2361 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2363 /* Don't bother messing with faults pre GEN6 as we have little
2364 * documentation supporting that it's a good idea.
2366 if (INTEL_GEN(dev_priv) < 6)
2369 i915_check_and_clear_faults(dev_priv);
2371 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
2373 i915_ggtt_flush(dev_priv);
2376 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
2377 struct sg_table *pages)
2379 if (dma_map_sg(&obj->base.dev->pdev->dev,
2380 pages->sgl, pages->nents,
2381 PCI_DMA_BIDIRECTIONAL))
2387 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2392 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2395 enum i915_cache_level level,
2398 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2399 gen8_pte_t __iomem *pte =
2400 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2401 (offset >> PAGE_SHIFT);
2403 gen8_set_pte(pte, gen8_pte_encode(addr, level));
2405 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2406 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2409 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2410 struct sg_table *st,
2412 enum i915_cache_level level, u32 unused)
2414 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2415 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2416 struct sgt_iter sgt_iter;
2417 gen8_pte_t __iomem *gtt_entries;
2418 gen8_pte_t gtt_entry;
2422 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2424 for_each_sgt_dma(addr, sgt_iter, st) {
2425 gtt_entry = gen8_pte_encode(addr, level);
2426 gen8_set_pte(>t_entries[i++], gtt_entry);
2430 * XXX: This serves as a posting read to make sure that the PTE has
2431 * actually been updated. There is some concern that even though
2432 * registers and PTEs are within the same BAR that they are potentially
2433 * of NUMA access patterns. Therefore, even with the way we assume
2434 * hardware should work, we must keep this posting read for paranoia.
2437 WARN_ON(readq(>t_entries[i-1]) != gtt_entry);
2439 /* This next bit makes the above posting read even more important. We
2440 * want to flush the TLBs only after we're certain all the PTE updates
2443 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2444 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2447 struct insert_entries {
2448 struct i915_address_space *vm;
2449 struct sg_table *st;
2451 enum i915_cache_level level;
2455 static int gen8_ggtt_insert_entries__cb(void *_arg)
2457 struct insert_entries *arg = _arg;
2458 gen8_ggtt_insert_entries(arg->vm, arg->st,
2459 arg->start, arg->level, arg->flags);
2463 static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2464 struct sg_table *st,
2466 enum i915_cache_level level,
2469 struct insert_entries arg = { vm, st, start, level, flags };
2470 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2473 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2476 enum i915_cache_level level,
2479 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2480 gen6_pte_t __iomem *pte =
2481 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2482 (offset >> PAGE_SHIFT);
2484 iowrite32(vm->pte_encode(addr, level, flags), pte);
2486 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2487 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2491 * Binds an object into the global gtt with the specified cache level. The object
2492 * will be accessible to the GPU via commands whose operands reference offsets
2493 * within the global GTT as well as accessible by the GPU through the GMADR
2494 * mapped BAR (dev_priv->mm.gtt->gtt).
2496 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2497 struct sg_table *st,
2499 enum i915_cache_level level, u32 flags)
2501 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2502 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2503 struct sgt_iter sgt_iter;
2504 gen6_pte_t __iomem *gtt_entries;
2505 gen6_pte_t gtt_entry;
2509 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2511 for_each_sgt_dma(addr, sgt_iter, st) {
2512 gtt_entry = vm->pte_encode(addr, level, flags);
2513 iowrite32(gtt_entry, >t_entries[i++]);
2516 /* XXX: This serves as a posting read to make sure that the PTE has
2517 * actually been updated. There is some concern that even though
2518 * registers and PTEs are within the same BAR that they are potentially
2519 * of NUMA access patterns. Therefore, even with the way we assume
2520 * hardware should work, we must keep this posting read for paranoia.
2523 WARN_ON(readl(>t_entries[i-1]) != gtt_entry);
2525 /* This next bit makes the above posting read even more important. We
2526 * want to flush the TLBs only after we're certain all the PTE updates
2529 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2530 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2533 static void nop_clear_range(struct i915_address_space *vm,
2534 uint64_t start, uint64_t length)
2538 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2539 uint64_t start, uint64_t length)
2541 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2542 unsigned first_entry = start >> PAGE_SHIFT;
2543 unsigned num_entries = length >> PAGE_SHIFT;
2544 gen8_pte_t scratch_pte, __iomem *gtt_base =
2545 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2546 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2549 if (WARN(num_entries > max_entries,
2550 "First entry = %d; Num entries = %d (max=%d)\n",
2551 first_entry, num_entries, max_entries))
2552 num_entries = max_entries;
2554 scratch_pte = gen8_pte_encode(vm->scratch_page.daddr,
2556 for (i = 0; i < num_entries; i++)
2557 gen8_set_pte(>t_base[i], scratch_pte);
2561 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2565 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2566 unsigned first_entry = start >> PAGE_SHIFT;
2567 unsigned num_entries = length >> PAGE_SHIFT;
2568 gen6_pte_t scratch_pte, __iomem *gtt_base =
2569 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2570 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2573 if (WARN(num_entries > max_entries,
2574 "First entry = %d; Num entries = %d (max=%d)\n",
2575 first_entry, num_entries, max_entries))
2576 num_entries = max_entries;
2578 scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
2581 for (i = 0; i < num_entries; i++)
2582 iowrite32(scratch_pte, >t_base[i]);
2586 static void i915_ggtt_insert_page(struct i915_address_space *vm,
2589 enum i915_cache_level cache_level,
2592 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2593 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2595 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2598 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2599 struct sg_table *pages,
2601 enum i915_cache_level cache_level, u32 unused)
2603 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2604 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2606 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
2610 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2614 intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
2617 static int ggtt_bind_vma(struct i915_vma *vma,
2618 enum i915_cache_level cache_level,
2621 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2622 struct drm_i915_gem_object *obj = vma->obj;
2626 ret = i915_get_ggtt_vma_pages(vma);
2630 /* Currently applicable only to VLV */
2632 pte_flags |= PTE_READ_ONLY;
2634 intel_runtime_pm_get(i915);
2635 vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
2636 cache_level, pte_flags);
2637 intel_runtime_pm_put(i915);
2640 * Without aliasing PPGTT there's no difference between
2641 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2642 * upgrade to both bound if we bind either to avoid double-binding.
2644 vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
2649 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2650 enum i915_cache_level cache_level,
2653 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2657 ret = i915_get_ggtt_vma_pages(vma);
2661 /* Currently applicable only to VLV */
2663 if (vma->obj->gt_ro)
2664 pte_flags |= PTE_READ_ONLY;
2667 if (flags & I915_VMA_GLOBAL_BIND) {
2668 intel_runtime_pm_get(i915);
2669 vma->vm->insert_entries(vma->vm,
2670 vma->pages, vma->node.start,
2671 cache_level, pte_flags);
2672 intel_runtime_pm_put(i915);
2675 if (flags & I915_VMA_LOCAL_BIND) {
2676 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2677 appgtt->base.insert_entries(&appgtt->base,
2678 vma->pages, vma->node.start,
2679 cache_level, pte_flags);
2685 static void ggtt_unbind_vma(struct i915_vma *vma)
2687 struct drm_i915_private *i915 = to_i915(vma->vm->dev);
2688 struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
2689 const u64 size = min(vma->size, vma->node.size);
2691 if (vma->flags & I915_VMA_GLOBAL_BIND) {
2692 intel_runtime_pm_get(i915);
2693 vma->vm->clear_range(vma->vm,
2694 vma->node.start, size);
2695 intel_runtime_pm_put(i915);
2698 if (vma->flags & I915_VMA_LOCAL_BIND && appgtt)
2699 appgtt->base.clear_range(&appgtt->base,
2700 vma->node.start, size);
2703 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
2704 struct sg_table *pages)
2706 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
2707 struct device *kdev = &dev_priv->drm.pdev->dev;
2708 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2710 if (unlikely(ggtt->do_idle_maps)) {
2711 if (i915_gem_wait_for_idle(dev_priv, I915_WAIT_LOCKED)) {
2712 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2713 /* Wait a bit, in hopes it avoids the hang */
2718 dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
2721 static void i915_gtt_color_adjust(struct drm_mm_node *node,
2722 unsigned long color,
2726 if (node->color != color)
2729 node = list_first_entry_or_null(&node->node_list,
2732 if (node && node->allocated && node->color != color)
2736 int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
2738 /* Let GEM Manage all of the aperture.
2740 * However, leave one page at the end still bound to the scratch page.
2741 * There are a number of places where the hardware apparently prefetches
2742 * past the end of the object, and we've seen multiple hangs with the
2743 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2744 * aperture. One page should be enough to keep any prefetching inside
2747 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2748 unsigned long hole_start, hole_end;
2749 struct i915_hw_ppgtt *ppgtt;
2750 struct drm_mm_node *entry;
2752 unsigned long mappable = min(ggtt->base.total, ggtt->mappable_end);
2754 ret = intel_vgt_balloon(dev_priv);
2758 /* Reserve a mappable slot for our lockless error capture */
2759 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2760 &ggtt->error_capture,
2762 0, ggtt->mappable_end,
2767 /* Clear any non-preallocated blocks */
2768 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
2769 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2770 hole_start, hole_end);
2771 ggtt->base.clear_range(&ggtt->base, hole_start,
2772 hole_end - hole_start);
2775 #ifdef __DragonFly__
2776 DRM_INFO("taking over the fictitious range 0x%llx-0x%llx\n",
2777 dev_priv->ggtt.mappable_base, dev_priv->ggtt.mappable_end);
2778 vm_phys_fictitious_reg_range(dev_priv->ggtt.mappable_base,
2779 dev_priv->ggtt.mappable_base + mappable, VM_MEMATTR_WRITE_COMBINING);
2782 /* And finally clear the reserved guard page */
2783 ggtt->base.clear_range(&ggtt->base,
2784 ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
2786 if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
2787 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2793 ret = __hw_ppgtt_init(ppgtt, dev_priv);
2797 if (ppgtt->base.allocate_va_range) {
2798 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2801 goto err_ppgtt_cleanup;
2804 ppgtt->base.clear_range(&ppgtt->base,
2808 dev_priv->mm.aliasing_ppgtt = ppgtt;
2809 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2810 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2816 ppgtt->base.cleanup(&ppgtt->base);
2820 drm_mm_remove_node(&ggtt->error_capture);
2825 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
2826 * @dev_priv: i915 device
2828 void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
2830 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2832 if (dev_priv->mm.aliasing_ppgtt) {
2833 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2834 ppgtt->base.cleanup(&ppgtt->base);
2838 i915_gem_cleanup_stolen(&dev_priv->drm);
2840 if (drm_mm_node_allocated(&ggtt->error_capture))
2841 drm_mm_remove_node(&ggtt->error_capture);
2843 if (drm_mm_initialized(&ggtt->base.mm)) {
2844 intel_vgt_deballoon(dev_priv);
2846 mutex_lock(&dev_priv->drm.struct_mutex);
2847 i915_address_space_fini(&ggtt->base);
2848 mutex_unlock(&dev_priv->drm.struct_mutex);
2851 ggtt->base.cleanup(&ggtt->base);
2853 arch_phys_wc_del(ggtt->mtrr);
2854 io_mapping_fini(&ggtt->mappable);
2857 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2859 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2860 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2861 return snb_gmch_ctl << 20;
2864 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2866 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2867 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2869 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2871 #ifdef CONFIG_X86_32
2872 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2873 if (bdw_gmch_ctl > 4)
2877 return bdw_gmch_ctl << 20;
2880 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2882 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2883 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2886 return 1 << (20 + gmch_ctrl);
2891 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
2893 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2894 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2895 return snb_gmch_ctl << 25; /* 32 MB units */
2898 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2900 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2901 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2902 return bdw_gmch_ctl << 25; /* 32 MB units */
2905 static size_t chv_get_stolen_size(u16 gmch_ctrl)
2907 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2908 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2911 * 0x0 to 0x10: 32MB increments starting at 0MB
2912 * 0x11 to 0x16: 4MB increments starting at 8MB
2913 * 0x17 to 0x1d: 4MB increments start at 36MB
2915 if (gmch_ctrl < 0x11)
2916 return gmch_ctrl << 25;
2917 else if (gmch_ctrl < 0x17)
2918 return (gmch_ctrl - 0x11 + 2) << 22;
2920 return (gmch_ctrl - 0x17 + 9) << 22;
2923 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2925 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2926 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2928 if (gen9_gmch_ctl < 0xf0)
2929 return gen9_gmch_ctl << 25; /* 32 MB units */
2931 /* 4MB increments starting at 0xf0 for 4MB */
2932 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2935 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
2937 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
2938 struct pci_dev *pdev = ggtt->base.dev->pdev;
2939 phys_addr_t phys_addr;
2942 /* For Modern GENs the PTEs and register space are split in the BAR */
2943 phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
2946 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2947 * dropped. For WC mappings in general we have 64 byte burst writes
2948 * when the WC buffer is flushed, so we can't use it, but have to
2949 * resort to an uncached mapping. The WC issue is easily caught by the
2950 * readback check when writing GTT PTE entries.
2952 if (IS_BROXTON(dev_priv))
2953 ggtt->gsm = ioremap_nocache(phys_addr, size);
2955 ggtt->gsm = ioremap_wc(phys_addr, size);
2957 DRM_ERROR("Failed to map the ggtt page table\n");
2961 ret = setup_scratch_page(dev_priv, &ggtt->base.scratch_page, GFP_DMA32);
2963 DRM_ERROR("Scratch setup failed\n");
2964 /* iounmap will also get called at remove, but meh */
2972 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2973 * bits. When using advanced contexts each context stores its own PAT, but
2974 * writing this data shouldn't be harmful even in those cases. */
2975 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
2979 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2980 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2981 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2982 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2983 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2984 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2985 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2986 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2988 if (!USES_PPGTT(dev_priv))
2989 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2990 * so RTL will always use the value corresponding to
2992 * So let's disable cache for GGTT to avoid screen corruptions.
2993 * MOCS still can be used though.
2994 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2995 * before this patch, i.e. the same uncached + snooping access
2996 * like on gen6/7 seems to be in effect.
2997 * - So this just fixes blitter/render access. Again it looks
2998 * like it's not just uncached access, but uncached + snooping.
2999 * So we can still hold onto all our assumptions wrt cpu
3000 * clflushing on LLC machines.
3002 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3004 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3005 * write would work. */
3006 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3007 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3010 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3015 * Map WB on BDW to snooped on CHV.
3017 * Only the snoop bit has meaning for CHV, the rest is
3020 * The hardware will never snoop for certain types of accesses:
3021 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3022 * - PPGTT page tables
3023 * - some other special cycles
3025 * As with BDW, we also need to consider the following for GT accesses:
3026 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3027 * so RTL will always use the value corresponding to
3029 * Which means we must set the snoop bit in PAT entry 0
3030 * in order to keep the global status page working.
3032 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3036 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3037 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3038 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3039 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3041 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3042 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3045 static void gen6_gmch_remove(struct i915_address_space *vm)
3047 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
3050 cleanup_scratch_page(to_i915(vm->dev), &vm->scratch_page);
3053 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
3055 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3056 struct pci_dev *pdev = dev_priv->drm.pdev;
3060 /* TODO: We're not aware of mappable constraints on gen8 yet */
3061 ggtt->mappable_base = pci_resource_start(pdev, 2);
3062 ggtt->mappable_end = pci_resource_len(pdev, 2);
3064 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
3065 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
3067 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3069 if (INTEL_GEN(dev_priv) >= 9) {
3070 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
3071 size = gen8_get_total_gtt_size(snb_gmch_ctl);
3072 } else if (IS_CHERRYVIEW(dev_priv)) {
3073 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
3074 size = chv_get_total_gtt_size(snb_gmch_ctl);
3076 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
3077 size = gen8_get_total_gtt_size(snb_gmch_ctl);
3080 ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3082 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
3083 chv_setup_private_ppat(dev_priv);
3085 bdw_setup_private_ppat(dev_priv);
3087 ggtt->base.cleanup = gen6_gmch_remove;
3088 ggtt->base.bind_vma = ggtt_bind_vma;
3089 ggtt->base.unbind_vma = ggtt_unbind_vma;
3090 ggtt->base.insert_page = gen8_ggtt_insert_page;
3091 ggtt->base.clear_range = nop_clear_range;
3092 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
3093 ggtt->base.clear_range = gen8_ggtt_clear_range;
3095 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3096 if (IS_CHERRYVIEW(dev_priv))
3097 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3099 return ggtt_probe_common(ggtt, size);
3102 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
3104 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3105 struct pci_dev *pdev = dev_priv->drm.pdev;
3109 ggtt->mappable_base = pci_resource_start(pdev, 2);
3110 ggtt->mappable_end = pci_resource_len(pdev, 2);
3112 /* 64/512MB is the current min/max we actually know of, but this is just
3113 * a coarse sanity check.
3115 if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
3116 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
3120 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
3121 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
3122 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3124 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
3126 size = gen6_get_total_gtt_size(snb_gmch_ctl);
3127 ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3129 ggtt->base.clear_range = gen6_ggtt_clear_range;
3130 ggtt->base.insert_page = gen6_ggtt_insert_page;
3131 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3132 ggtt->base.bind_vma = ggtt_bind_vma;
3133 ggtt->base.unbind_vma = ggtt_unbind_vma;
3134 ggtt->base.cleanup = gen6_gmch_remove;
3136 if (HAS_EDRAM(dev_priv))
3137 ggtt->base.pte_encode = iris_pte_encode;
3138 else if (IS_HASWELL(dev_priv))
3139 ggtt->base.pte_encode = hsw_pte_encode;
3140 else if (IS_VALLEYVIEW(dev_priv))
3141 ggtt->base.pte_encode = byt_pte_encode;
3142 else if (INTEL_GEN(dev_priv) >= 7)
3143 ggtt->base.pte_encode = ivb_pte_encode;
3145 ggtt->base.pte_encode = snb_pte_encode;
3147 return ggtt_probe_common(ggtt, size);
3150 static void i915_gmch_remove(struct i915_address_space *vm)
3152 intel_gmch_remove();
3155 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3157 struct drm_i915_private *dev_priv = to_i915(ggtt->base.dev);
3161 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
3163 DRM_ERROR("failed to set up gmch\n");
3168 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3169 &ggtt->mappable_base, &ggtt->mappable_end);
3171 ggtt->do_idle_maps = needs_idle_maps(dev_priv);
3172 ggtt->base.insert_page = i915_ggtt_insert_page;
3173 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3174 ggtt->base.clear_range = i915_ggtt_clear_range;
3175 ggtt->base.bind_vma = ggtt_bind_vma;
3176 ggtt->base.unbind_vma = ggtt_unbind_vma;
3177 ggtt->base.cleanup = i915_gmch_remove;
3179 if (unlikely(ggtt->do_idle_maps))
3180 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3186 * i915_ggtt_probe_hw - Probe GGTT hardware location
3187 * @dev_priv: i915 device
3189 int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
3191 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3194 ggtt->base.dev = &dev_priv->drm;
3196 if (INTEL_GEN(dev_priv) <= 5)
3197 ret = i915_gmch_probe(ggtt);
3198 else if (INTEL_GEN(dev_priv) < 8)
3199 ret = gen6_gmch_probe(ggtt);
3201 ret = gen8_gmch_probe(ggtt);
3205 if ((ggtt->base.total - 1) >> 32) {
3206 DRM_ERROR("We never expected a Global GTT with more than 32bits"
3207 " of address space! Found %lldM!\n",
3208 ggtt->base.total >> 20);
3209 ggtt->base.total = 1ULL << 32;
3210 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3213 if (ggtt->mappable_end > ggtt->base.total) {
3214 DRM_ERROR("mappable aperture extends past end of GGTT,"
3215 " aperture=%llx, total=%llx\n",
3216 ggtt->mappable_end, ggtt->base.total);
3217 ggtt->mappable_end = ggtt->base.total;
3220 /* GMADR is the PCI mmio aperture into the global GTT. */
3221 DRM_INFO("Memory usable by graphics device = %lluM\n",
3222 ggtt->base.total >> 20);
3223 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3224 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
3225 #ifdef CONFIG_INTEL_IOMMU
3226 if (intel_iommu_gfx_mapped)
3227 DRM_INFO("VT-d active for gfx access\n");
3234 * i915_ggtt_init_hw - Initialize GGTT hardware
3235 * @dev_priv: i915 device
3237 int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
3239 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3242 INIT_LIST_HEAD(&dev_priv->vm_list);
3244 /* Subtract the guard page before address space initialization to
3245 * shrink the range used by drm_mm.
3247 mutex_lock(&dev_priv->drm.struct_mutex);
3248 ggtt->base.total -= PAGE_SIZE;
3249 i915_address_space_init(&ggtt->base, dev_priv, "[global]");
3250 ggtt->base.total += PAGE_SIZE;
3251 if (!HAS_LLC(dev_priv))
3252 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
3253 mutex_unlock(&dev_priv->drm.struct_mutex);
3255 if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
3256 dev_priv->ggtt.mappable_base,
3257 dev_priv->ggtt.mappable_end)) {
3259 goto out_gtt_cleanup;
3262 ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
3265 * Initialise stolen early so that we may reserve preallocated
3266 * objects for the BIOS to KMS transition.
3268 ret = i915_gem_init_stolen(dev_priv);
3270 goto out_gtt_cleanup;
3275 ggtt->base.cleanup(&ggtt->base);
3279 int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
3281 if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
3287 void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
3289 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3290 struct drm_i915_gem_object *obj, *on;
3292 i915_check_and_clear_faults(dev_priv);
3294 /* First fill our portion of the GTT with scratch pages */
3295 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total);
3297 ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
3299 /* clflush objects bound into the GGTT and rebind them. */
3300 list_for_each_entry_safe(obj, on,
3301 &dev_priv->mm.bound_list, global_link) {
3302 bool ggtt_bound = false;
3303 struct i915_vma *vma;
3305 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3306 if (vma->vm != &ggtt->base)
3309 if (!i915_vma_unbind(vma))
3312 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3318 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
3321 ggtt->base.closed = false;
3323 if (INTEL_GEN(dev_priv) >= 8) {
3324 if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
3325 chv_setup_private_ppat(dev_priv);
3327 bdw_setup_private_ppat(dev_priv);
3332 if (USES_PPGTT(dev_priv)) {
3333 struct i915_address_space *vm;
3335 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3336 /* TODO: Perhaps it shouldn't be gen6 specific */
3338 struct i915_hw_ppgtt *ppgtt;
3340 if (i915_is_ggtt(vm))
3341 ppgtt = dev_priv->mm.aliasing_ppgtt;
3343 ppgtt = i915_vm_to_ppgtt(vm);
3345 gen6_write_page_range(dev_priv, &ppgtt->pd,
3346 0, ppgtt->base.total);
3350 i915_ggtt_flush(dev_priv);
3354 i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
3355 struct i915_address_space *vm,
3356 const struct i915_ggtt_view *view)
3360 rb = obj->vma_tree.rb_node;
3362 struct i915_vma *vma = rb_entry(rb, struct i915_vma, obj_node);
3365 cmp = i915_vma_compare(vma, vm, view);
3379 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3380 struct i915_address_space *vm,
3381 const struct i915_ggtt_view *view)
3383 struct i915_vma *vma;
3385 lockdep_assert_held(&obj->base.dev->struct_mutex);
3386 GEM_BUG_ON(view && !i915_is_ggtt(vm));
3388 vma = i915_gem_obj_to_vma(obj, vm, view);
3390 vma = i915_vma_create(obj, vm, view);
3391 GEM_BUG_ON(vma != i915_gem_obj_to_vma(obj, vm, view));
3394 GEM_BUG_ON(i915_vma_is_closed(vma));
3398 static struct scatterlist *
3399 rotate_pages(const dma_addr_t *in, unsigned int offset,
3400 unsigned int width, unsigned int height,
3401 unsigned int stride,
3402 struct sg_table *st, struct scatterlist *sg)
3404 unsigned int column, row;
3405 unsigned int src_idx;
3407 for (column = 0; column < width; column++) {
3408 src_idx = stride * (height - 1) + column;
3409 for (row = 0; row < height; row++) {
3411 /* We don't need the pages, but need to initialize
3412 * the entries so the sg list can be happily traversed.
3413 * The only thing we need are DMA addresses.
3415 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3416 sg_dma_address(sg) = in[offset + src_idx];
3417 sg_dma_len(sg) = PAGE_SIZE;
3426 static struct sg_table *
3427 intel_rotate_fb_obj_pages(const struct intel_rotation_info *rot_info,
3428 struct drm_i915_gem_object *obj)
3430 const size_t n_pages = obj->base.size / PAGE_SIZE;
3431 unsigned int size = intel_rotation_info_size(rot_info);
3432 struct sgt_iter sgt_iter;
3433 dma_addr_t dma_addr;
3435 dma_addr_t *page_addr_list;
3436 struct sg_table *st;
3437 struct scatterlist *sg;
3440 /* Allocate a temporary list of source pages for random access. */
3441 page_addr_list = drm_malloc_gfp(n_pages,
3444 if (!page_addr_list)
3445 return ERR_PTR(ret);
3447 /* Allocate target SG list. */
3448 st = kmalloc(sizeof(*st), M_DRM, GFP_KERNEL);
3452 ret = sg_alloc_table(st, size, GFP_KERNEL);
3456 /* Populate source page list from the object. */
3458 for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
3459 page_addr_list[i++] = dma_addr;
3461 GEM_BUG_ON(i != n_pages);
3465 for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
3466 sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
3467 rot_info->plane[i].width, rot_info->plane[i].height,
3468 rot_info->plane[i].stride, st, sg);
3471 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3472 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3474 drm_free_large(page_addr_list);
3481 drm_free_large(page_addr_list);
3483 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3484 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
3486 return ERR_PTR(ret);
3489 static struct sg_table *
3490 intel_partial_pages(const struct i915_ggtt_view *view,
3491 struct drm_i915_gem_object *obj)
3493 struct sg_table *st;
3494 struct scatterlist *sg, *iter;
3495 unsigned int count = view->params.partial.size;
3496 unsigned int offset;
3499 st = kmalloc(sizeof(*st), M_DRM, GFP_KERNEL);
3503 ret = sg_alloc_table(st, count, GFP_KERNEL);
3507 iter = i915_gem_object_get_sg(obj,
3508 view->params.partial.offset,
3517 len = min(iter->length - (offset << PAGE_SHIFT),
3518 count << PAGE_SHIFT);
3519 sg_set_page(sg, NULL, len, 0);
3520 sg_dma_address(sg) =
3521 sg_dma_address(iter) + (offset << PAGE_SHIFT);
3522 sg_dma_len(sg) = len;
3525 count -= len >> PAGE_SHIFT;
3532 iter = __sg_next(iter);
3539 return ERR_PTR(ret);
3543 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3547 /* The vma->pages are only valid within the lifespan of the borrowed
3548 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3549 * must be the vma->pages. A simple rule is that vma->pages must only
3550 * be accessed when the obj->mm.pages are pinned.
3552 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
3557 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
3558 vma->pages = vma->obj->mm.pages;
3559 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
3561 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
3562 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
3563 vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
3565 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3566 vma->ggtt_view.type);
3569 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
3570 vma->ggtt_view.type);
3572 } else if (IS_ERR(vma->pages)) {
3573 ret = PTR_ERR(vma->pages);
3575 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3576 vma->ggtt_view.type, ret);