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"
35 #include <linux/bitmap.h>
37 #include <sys/mplock2.h>
40 * DOC: Global GTT views
42 * Background and previous state
44 * Historically objects could exists (be bound) in global GTT space only as
45 * singular instances with a view representing all of the object's backing pages
46 * in a linear fashion. This view will be called a normal view.
48 * To support multiple views of the same object, where the number of mapped
49 * pages is not equal to the backing store, or where the layout of the pages
50 * is not linear, concept of a GGTT view was added.
52 * One example of an alternative view is a stereo display driven by a single
53 * image. In this case we would have a framebuffer looking like this
59 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
60 * rendering. In contrast, fed to the display engine would be an alternative
61 * view which could look something like this:
66 * In this example both the size and layout of pages in the alternative view is
67 * different from the normal view.
69 * Implementation and usage
71 * GGTT views are implemented using VMAs and are distinguished via enum
72 * i915_ggtt_view_type and struct i915_ggtt_view.
74 * A new flavour of core GEM functions which work with GGTT bound objects were
75 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
76 * renaming in large amounts of code. They take the struct i915_ggtt_view
77 * parameter encapsulating all metadata required to implement a view.
79 * As a helper for callers which are only interested in the normal view,
80 * globally const i915_ggtt_view_normal singleton instance exists. All old core
81 * GEM API functions, the ones not taking the view parameter, are operating on,
82 * or with the normal GGTT view.
84 * Code wanting to add or use a new GGTT view needs to:
86 * 1. Add a new enum with a suitable name.
87 * 2. Extend the metadata in the i915_ggtt_view structure if required.
88 * 3. Add support to i915_get_vma_pages().
90 * New views are required to build a scatter-gather table from within the
91 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
92 * exists for the lifetime of an VMA.
94 * Core API is designed to have copy semantics which means that passed in
95 * struct i915_ggtt_view does not need to be persistent (left around after
96 * calling the core API functions).
100 static inline struct i915_ggtt *
101 i915_vm_to_ggtt(struct i915_address_space *vm)
103 GEM_BUG_ON(!i915_is_ggtt(vm));
104 return container_of(vm, struct i915_ggtt, base);
108 i915_get_ggtt_vma_pages(struct i915_vma *vma);
110 const struct i915_ggtt_view i915_ggtt_view_normal = {
111 .type = I915_GGTT_VIEW_NORMAL,
113 const struct i915_ggtt_view i915_ggtt_view_rotated = {
114 .type = I915_GGTT_VIEW_ROTATED,
117 int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
120 bool has_aliasing_ppgtt;
122 bool has_full_48bit_ppgtt;
124 has_aliasing_ppgtt = INTEL_GEN(dev_priv) >= 6;
125 has_full_ppgtt = INTEL_GEN(dev_priv) >= 7;
126 has_full_48bit_ppgtt =
127 IS_BROADWELL(dev_priv) || INTEL_GEN(dev_priv) >= 9;
129 if (intel_vgpu_active(dev_priv))
130 has_full_ppgtt = false; /* emulation is too hard */
132 if (!has_aliasing_ppgtt)
136 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
137 * execlists, the sole mechanism available to submit work.
139 if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
142 if (enable_ppgtt == 1)
145 if (enable_ppgtt == 2 && has_full_ppgtt)
148 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
151 #ifdef CONFIG_INTEL_IOMMU
152 /* Disable ppgtt on SNB if VT-d is on. */
153 if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
154 DRM_INFO("Disabling PPGTT because VT-d is on\n");
159 /* Early VLV doesn't have this */
160 if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
161 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
165 if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists)
166 return has_full_48bit_ppgtt ? 3 : 2;
168 return has_aliasing_ppgtt ? 1 : 0;
171 static int ppgtt_bind_vma(struct i915_vma *vma,
172 enum i915_cache_level cache_level,
177 /* Currently applicable only to VLV */
179 pte_flags |= PTE_READ_ONLY;
181 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
182 cache_level, pte_flags);
187 static void ppgtt_unbind_vma(struct i915_vma *vma)
189 vma->vm->clear_range(vma->vm,
195 static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
196 enum i915_cache_level level,
199 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
203 case I915_CACHE_NONE:
204 pte |= PPAT_UNCACHED_INDEX;
207 pte |= PPAT_DISPLAY_ELLC_INDEX;
210 pte |= PPAT_CACHED_INDEX;
217 static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
218 const enum i915_cache_level level)
220 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
222 if (level != I915_CACHE_NONE)
223 pde |= PPAT_CACHED_PDE_INDEX;
225 pde |= PPAT_UNCACHED_INDEX;
229 #define gen8_pdpe_encode gen8_pde_encode
230 #define gen8_pml4e_encode gen8_pde_encode
232 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
233 enum i915_cache_level level,
234 bool valid, u32 unused)
236 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
237 pte |= GEN6_PTE_ADDR_ENCODE(addr);
240 case I915_CACHE_L3_LLC:
242 pte |= GEN6_PTE_CACHE_LLC;
244 case I915_CACHE_NONE:
245 pte |= GEN6_PTE_UNCACHED;
254 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
255 enum i915_cache_level level,
256 bool valid, u32 unused)
258 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
259 pte |= GEN6_PTE_ADDR_ENCODE(addr);
262 case I915_CACHE_L3_LLC:
263 pte |= GEN7_PTE_CACHE_L3_LLC;
266 pte |= GEN6_PTE_CACHE_LLC;
268 case I915_CACHE_NONE:
269 pte |= GEN6_PTE_UNCACHED;
278 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
279 enum i915_cache_level level,
280 bool valid, u32 flags)
282 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
283 pte |= GEN6_PTE_ADDR_ENCODE(addr);
285 if (!(flags & PTE_READ_ONLY))
286 pte |= BYT_PTE_WRITEABLE;
288 if (level != I915_CACHE_NONE)
289 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
294 static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
295 enum i915_cache_level level,
296 bool valid, u32 unused)
298 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
299 pte |= HSW_PTE_ADDR_ENCODE(addr);
301 if (level != I915_CACHE_NONE)
302 pte |= HSW_WB_LLC_AGE3;
307 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
308 enum i915_cache_level level,
309 bool valid, u32 unused)
311 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
312 pte |= HSW_PTE_ADDR_ENCODE(addr);
315 case I915_CACHE_NONE:
318 pte |= HSW_WT_ELLC_LLC_AGE3;
321 pte |= HSW_WB_ELLC_LLC_AGE3;
328 static int __setup_page_dma(struct drm_device *dev,
329 struct i915_page_dma *p, gfp_t flags)
331 struct device *device = &dev->pdev->dev;
333 p->page = alloc_page(flags);
337 p->daddr = dma_map_page(device,
338 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
340 if (dma_mapping_error(device, p->daddr)) {
341 __free_page(p->page);
348 static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
350 return __setup_page_dma(dev, p, GFP_KERNEL);
353 static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
355 if (WARN_ON(!p->page))
358 dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
359 __free_page(p->page);
360 memset(p, 0, sizeof(*p));
363 static void *kmap_page_dma(struct i915_page_dma *p)
365 return kmap_atomic(p->page);
368 /* We use the flushing unmap only with ppgtt structures:
369 * page directories, page tables and scratch pages.
371 static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
373 /* There are only few exceptions for gen >=6. chv and bxt.
374 * And we are not sure about the latter so play safe for now.
376 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
377 drm_clflush_virt_range(vaddr, PAGE_SIZE);
379 kunmap_atomic(vaddr);
382 #define kmap_px(px) kmap_page_dma(px_base(px))
383 #define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
385 #define setup_px(dev, px) setup_page_dma((dev), px_base(px))
386 #define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
387 #define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
388 #define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
390 static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
394 uint64_t * const vaddr = kmap_page_dma(p);
396 for (i = 0; i < 512; i++)
399 kunmap_page_dma(dev, vaddr);
402 static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
403 const uint32_t val32)
409 fill_page_dma(dev, p, v);
412 static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
414 struct i915_page_scratch *sp;
417 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
419 return ERR_PTR(-ENOMEM);
421 ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
427 set_pages_uc(px_page(sp), 1);
432 static void free_scratch_page(struct drm_device *dev,
433 struct i915_page_scratch *sp)
435 set_pages_wb(px_page(sp), 1);
441 static struct i915_page_table *alloc_pt(struct drm_device *dev)
443 struct i915_page_table *pt;
444 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
445 GEN8_PTES : GEN6_PTES;
448 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
450 return ERR_PTR(-ENOMEM);
452 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
458 ret = setup_px(dev, pt);
465 kfree(pt->used_ptes);
472 static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
475 kfree(pt->used_ptes);
479 static void gen8_initialize_pt(struct i915_address_space *vm,
480 struct i915_page_table *pt)
482 gen8_pte_t scratch_pte;
484 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
485 I915_CACHE_LLC, true);
487 fill_px(vm->dev, pt, scratch_pte);
490 static void gen6_initialize_pt(struct i915_address_space *vm,
491 struct i915_page_table *pt)
493 gen6_pte_t scratch_pte;
495 WARN_ON(px_dma(vm->scratch_page) == 0);
497 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
498 I915_CACHE_LLC, true, 0);
500 fill32_px(vm->dev, pt, scratch_pte);
503 static struct i915_page_directory *alloc_pd(struct drm_device *dev)
505 struct i915_page_directory *pd;
508 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
510 return ERR_PTR(-ENOMEM);
512 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
513 sizeof(*pd->used_pdes), GFP_KERNEL);
517 ret = setup_px(dev, pd);
524 kfree(pd->used_pdes);
531 static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
535 kfree(pd->used_pdes);
540 static void gen8_initialize_pd(struct i915_address_space *vm,
541 struct i915_page_directory *pd)
543 gen8_pde_t scratch_pde;
545 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
547 fill_px(vm->dev, pd, scratch_pde);
550 static int __pdp_init(struct drm_device *dev,
551 struct i915_page_directory_pointer *pdp)
553 size_t pdpes = I915_PDPES_PER_PDP(dev);
555 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
556 sizeof(unsigned long),
558 if (!pdp->used_pdpes)
561 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
563 if (!pdp->page_directory) {
564 kfree(pdp->used_pdpes);
565 /* the PDP might be the statically allocated top level. Keep it
566 * as clean as possible */
567 pdp->used_pdpes = NULL;
574 static void __pdp_fini(struct i915_page_directory_pointer *pdp)
576 kfree(pdp->used_pdpes);
577 kfree(pdp->page_directory);
578 pdp->page_directory = NULL;
582 i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
584 struct i915_page_directory_pointer *pdp;
587 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
589 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
591 return ERR_PTR(-ENOMEM);
593 ret = __pdp_init(dev, pdp);
597 ret = setup_px(dev, pdp);
611 static void free_pdp(struct drm_device *dev,
612 struct i915_page_directory_pointer *pdp)
615 if (USES_FULL_48BIT_PPGTT(dev)) {
616 cleanup_px(dev, pdp);
621 static void gen8_initialize_pdp(struct i915_address_space *vm,
622 struct i915_page_directory_pointer *pdp)
624 gen8_ppgtt_pdpe_t scratch_pdpe;
626 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
628 fill_px(vm->dev, pdp, scratch_pdpe);
631 static void gen8_initialize_pml4(struct i915_address_space *vm,
632 struct i915_pml4 *pml4)
634 gen8_ppgtt_pml4e_t scratch_pml4e;
636 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
639 fill_px(vm->dev, pml4, scratch_pml4e);
643 gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
644 struct i915_page_directory_pointer *pdp,
645 struct i915_page_directory *pd,
648 gen8_ppgtt_pdpe_t *page_directorypo;
650 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
653 page_directorypo = kmap_px(pdp);
654 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
655 kunmap_px(ppgtt, page_directorypo);
659 gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
660 struct i915_pml4 *pml4,
661 struct i915_page_directory_pointer *pdp,
664 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
666 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
667 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
668 kunmap_px(ppgtt, pagemap);
671 /* Broadwell Page Directory Pointer Descriptors */
672 static int gen8_write_pdp(struct drm_i915_gem_request *req,
676 struct intel_engine_cs *engine = req->engine;
681 ret = intel_ring_begin(req, 6);
685 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
686 intel_ring_emit_reg(engine, GEN8_RING_PDP_UDW(engine, entry));
687 intel_ring_emit(engine, upper_32_bits(addr));
688 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
689 intel_ring_emit_reg(engine, GEN8_RING_PDP_LDW(engine, entry));
690 intel_ring_emit(engine, lower_32_bits(addr));
691 intel_ring_advance(engine);
696 static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
697 struct drm_i915_gem_request *req)
701 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
702 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
704 ret = gen8_write_pdp(req, i, pd_daddr);
712 static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
713 struct drm_i915_gem_request *req)
715 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
718 static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
719 struct i915_page_directory_pointer *pdp,
722 gen8_pte_t scratch_pte)
724 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
725 gen8_pte_t *pt_vaddr;
726 unsigned pdpe = gen8_pdpe_index(start);
727 unsigned pde = gen8_pde_index(start);
728 unsigned pte = gen8_pte_index(start);
729 unsigned num_entries = length >> PAGE_SHIFT;
730 unsigned last_pte, i;
735 while (num_entries) {
736 struct i915_page_directory *pd;
737 struct i915_page_table *pt;
739 if (WARN_ON(!pdp->page_directory[pdpe]))
742 pd = pdp->page_directory[pdpe];
744 if (WARN_ON(!pd->page_table[pde]))
747 pt = pd->page_table[pde];
749 if (WARN_ON(!px_page(pt)))
752 last_pte = pte + num_entries;
753 if (last_pte > GEN8_PTES)
754 last_pte = GEN8_PTES;
756 pt_vaddr = kmap_px(pt);
758 for (i = pte; i < last_pte; i++) {
759 pt_vaddr[i] = scratch_pte;
763 kunmap_px(ppgtt, pt_vaddr);
766 if (++pde == I915_PDES) {
767 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
774 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
779 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
780 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
781 I915_CACHE_LLC, use_scratch);
783 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
784 gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length,
788 struct i915_page_directory_pointer *pdp;
790 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
791 gen8_ppgtt_clear_pte_range(vm, pdp, start, length,
798 gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
799 struct i915_page_directory_pointer *pdp,
800 struct sg_page_iter *sg_iter,
802 enum i915_cache_level cache_level)
804 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
805 gen8_pte_t *pt_vaddr;
806 unsigned pdpe = gen8_pdpe_index(start);
807 unsigned pde = gen8_pde_index(start);
808 unsigned pte = gen8_pte_index(start);
812 while (__sg_page_iter_next(sg_iter)) {
813 if (pt_vaddr == NULL) {
814 struct i915_page_directory *pd = pdp->page_directory[pdpe];
815 struct i915_page_table *pt = pd->page_table[pde];
816 pt_vaddr = kmap_px(pt);
820 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
822 if (++pte == GEN8_PTES) {
823 kunmap_px(ppgtt, pt_vaddr);
825 if (++pde == I915_PDES) {
826 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
835 kunmap_px(ppgtt, pt_vaddr);
838 static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
839 struct sg_table *pages,
841 enum i915_cache_level cache_level,
844 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
845 struct sg_page_iter sg_iter;
847 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
849 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
850 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
853 struct i915_page_directory_pointer *pdp;
855 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
857 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
858 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
864 static void gen8_free_page_tables(struct drm_device *dev,
865 struct i915_page_directory *pd)
872 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
873 if (WARN_ON(!pd->page_table[i]))
876 free_pt(dev, pd->page_table[i]);
877 pd->page_table[i] = NULL;
881 static int gen8_init_scratch(struct i915_address_space *vm)
883 struct drm_device *dev = vm->dev;
886 vm->scratch_page = alloc_scratch_page(dev);
887 if (IS_ERR(vm->scratch_page))
888 return PTR_ERR(vm->scratch_page);
890 vm->scratch_pt = alloc_pt(dev);
891 if (IS_ERR(vm->scratch_pt)) {
892 ret = PTR_ERR(vm->scratch_pt);
893 goto free_scratch_page;
896 vm->scratch_pd = alloc_pd(dev);
897 if (IS_ERR(vm->scratch_pd)) {
898 ret = PTR_ERR(vm->scratch_pd);
902 if (USES_FULL_48BIT_PPGTT(dev)) {
903 vm->scratch_pdp = alloc_pdp(dev);
904 if (IS_ERR(vm->scratch_pdp)) {
905 ret = PTR_ERR(vm->scratch_pdp);
910 gen8_initialize_pt(vm, vm->scratch_pt);
911 gen8_initialize_pd(vm, vm->scratch_pd);
912 if (USES_FULL_48BIT_PPGTT(dev))
913 gen8_initialize_pdp(vm, vm->scratch_pdp);
918 free_pd(dev, vm->scratch_pd);
920 free_pt(dev, vm->scratch_pt);
922 free_scratch_page(dev, vm->scratch_page);
927 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
929 enum vgt_g2v_type msg;
930 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
933 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
934 u64 daddr = px_dma(&ppgtt->pml4);
936 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
937 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
939 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
940 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
942 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
943 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
945 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
946 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
949 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
950 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
953 I915_WRITE(vgtif_reg(g2v_notify), msg);
958 static void gen8_free_scratch(struct i915_address_space *vm)
960 struct drm_device *dev = vm->dev;
962 if (USES_FULL_48BIT_PPGTT(dev))
963 free_pdp(dev, vm->scratch_pdp);
964 free_pd(dev, vm->scratch_pd);
965 free_pt(dev, vm->scratch_pt);
966 free_scratch_page(dev, vm->scratch_page);
969 static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
970 struct i915_page_directory_pointer *pdp)
974 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
975 if (WARN_ON(!pdp->page_directory[i]))
978 gen8_free_page_tables(dev, pdp->page_directory[i]);
979 free_pd(dev, pdp->page_directory[i]);
985 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
989 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
990 if (WARN_ON(!ppgtt->pml4.pdps[i]))
993 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
996 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
999 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
1001 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1003 if (intel_vgpu_active(to_i915(vm->dev)))
1004 gen8_ppgtt_notify_vgt(ppgtt, false);
1006 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
1007 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
1009 gen8_ppgtt_cleanup_4lvl(ppgtt);
1011 gen8_free_scratch(vm);
1015 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
1016 * @vm: Master vm structure.
1017 * @pd: Page directory for this address range.
1018 * @start: Starting virtual address to begin allocations.
1019 * @length: Size of the allocations.
1020 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1021 * caller to free on error.
1023 * Allocate the required number of page tables. Extremely similar to
1024 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1025 * the page directory boundary (instead of the page directory pointer). That
1026 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1027 * possible, and likely that the caller will need to use multiple calls of this
1028 * function to achieve the appropriate allocation.
1030 * Return: 0 if success; negative error code otherwise.
1032 static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
1033 struct i915_page_directory *pd,
1036 unsigned long *new_pts)
1038 struct drm_device *dev = vm->dev;
1039 struct i915_page_table *pt;
1042 gen8_for_each_pde(pt, pd, start, length, pde) {
1043 /* Don't reallocate page tables */
1044 if (test_bit(pde, pd->used_pdes)) {
1045 /* Scratch is never allocated this way */
1046 WARN_ON(pt == vm->scratch_pt);
1054 gen8_initialize_pt(vm, pt);
1055 pd->page_table[pde] = pt;
1056 __set_bit(pde, new_pts);
1057 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
1063 for_each_set_bit(pde, new_pts, I915_PDES)
1064 free_pt(dev, pd->page_table[pde]);
1070 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
1071 * @vm: Master vm structure.
1072 * @pdp: Page directory pointer for this address range.
1073 * @start: Starting virtual address to begin allocations.
1074 * @length: Size of the allocations.
1075 * @new_pds: Bitmap set by function with new allocations. Likely used by the
1076 * caller to free on error.
1078 * Allocate the required number of page directories starting at the pde index of
1079 * @start, and ending at the pde index @start + @length. This function will skip
1080 * over already allocated page directories within the range, and only allocate
1081 * new ones, setting the appropriate pointer within the pdp as well as the
1082 * correct position in the bitmap @new_pds.
1084 * The function will only allocate the pages within the range for a give page
1085 * directory pointer. In other words, if @start + @length straddles a virtually
1086 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1087 * required by the caller, This is not currently possible, and the BUG in the
1088 * code will prevent it.
1090 * Return: 0 if success; negative error code otherwise.
1093 gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1094 struct i915_page_directory_pointer *pdp,
1097 unsigned long *new_pds)
1099 struct drm_device *dev = vm->dev;
1100 struct i915_page_directory *pd;
1102 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1104 WARN_ON(!bitmap_empty(new_pds, pdpes));
1106 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1107 if (test_bit(pdpe, pdp->used_pdpes))
1114 gen8_initialize_pd(vm, pd);
1115 pdp->page_directory[pdpe] = pd;
1116 __set_bit(pdpe, new_pds);
1117 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
1123 for_each_set_bit(pdpe, new_pds, pdpes)
1124 free_pd(dev, pdp->page_directory[pdpe]);
1130 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1131 * @vm: Master vm structure.
1132 * @pml4: Page map level 4 for this address range.
1133 * @start: Starting virtual address to begin allocations.
1134 * @length: Size of the allocations.
1135 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1136 * caller to free on error.
1138 * Allocate the required number of page directory pointers. Extremely similar to
1139 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1140 * The main difference is here we are limited by the pml4 boundary (instead of
1141 * the page directory pointer).
1143 * Return: 0 if success; negative error code otherwise.
1146 gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1147 struct i915_pml4 *pml4,
1150 unsigned long *new_pdps)
1152 struct drm_device *dev = vm->dev;
1153 struct i915_page_directory_pointer *pdp;
1156 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1158 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1159 if (!test_bit(pml4e, pml4->used_pml4es)) {
1160 pdp = alloc_pdp(dev);
1164 gen8_initialize_pdp(vm, pdp);
1165 pml4->pdps[pml4e] = pdp;
1166 __set_bit(pml4e, new_pdps);
1167 trace_i915_page_directory_pointer_entry_alloc(vm,
1177 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1178 free_pdp(dev, pml4->pdps[pml4e]);
1184 free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
1190 /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1191 * of these are based on the number of PDPEs in the system.
1194 int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
1195 unsigned long **new_pts,
1201 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
1205 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1216 free_gen8_temp_bitmaps(pds, pts);
1220 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1221 * the page table structures, we mark them dirty so that
1222 * context switching/execlist queuing code takes extra steps
1223 * to ensure that tlbs are flushed.
1225 static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1227 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1230 static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1231 struct i915_page_directory_pointer *pdp,
1235 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1236 unsigned long *new_page_dirs, *new_page_tables;
1237 struct drm_device *dev = vm->dev;
1238 struct i915_page_directory *pd;
1239 const uint64_t orig_start = start;
1240 const uint64_t orig_length = length;
1242 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1245 /* Wrap is never okay since we can only represent 48b, and we don't
1246 * actually use the other side of the canonical address space.
1248 if (WARN_ON(start + length < start))
1251 if (WARN_ON(start + length > vm->total))
1254 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1258 /* Do the allocations first so we can easily bail out */
1259 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1262 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1266 /* For every page directory referenced, allocate page tables */
1267 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1268 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
1269 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
1275 length = orig_length;
1277 /* Allocations have completed successfully, so set the bitmaps, and do
1279 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1280 gen8_pde_t *const page_directory = kmap_px(pd);
1281 struct i915_page_table *pt;
1282 uint64_t pd_len = length;
1283 uint64_t pd_start = start;
1286 /* Every pd should be allocated, we just did that above. */
1289 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1290 /* Same reasoning as pd */
1293 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1295 /* Set our used ptes within the page table */
1296 bitmap_set(pt->used_ptes,
1297 gen8_pte_index(pd_start),
1298 gen8_pte_count(pd_start, pd_len));
1300 /* Our pde is now pointing to the pagetable, pt */
1301 __set_bit(pde, pd->used_pdes);
1303 /* Map the PDE to the page table */
1304 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1306 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1307 gen8_pte_index(start),
1308 gen8_pte_count(start, length),
1311 /* NB: We haven't yet mapped ptes to pages. At this
1312 * point we're still relying on insert_entries() */
1315 kunmap_px(ppgtt, page_directory);
1316 __set_bit(pdpe, pdp->used_pdpes);
1317 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
1320 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1321 mark_tlbs_dirty(ppgtt);
1328 for_each_set_bit(temp, new_page_tables + pdpe *
1329 BITS_TO_LONGS(I915_PDES), I915_PDES)
1330 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
1333 for_each_set_bit(pdpe, new_page_dirs, pdpes)
1334 free_pd(dev, pdp->page_directory[pdpe]);
1336 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1337 mark_tlbs_dirty(ppgtt);
1341 static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1342 struct i915_pml4 *pml4,
1346 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
1347 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1348 struct i915_page_directory_pointer *pdp;
1352 /* Do the pml4 allocations first, so we don't need to track the newly
1353 * allocated tables below the pdp */
1354 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1356 /* The pagedirectory and pagetable allocations are done in the shared 3
1357 * and 4 level code. Just allocate the pdps.
1359 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1364 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1365 "The allocation has spanned more than 512GB. "
1366 "It is highly likely this is incorrect.");
1368 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1371 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1375 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1378 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1379 GEN8_PML4ES_PER_PML4);
1384 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1385 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1390 static int gen8_alloc_va_range(struct i915_address_space *vm,
1391 uint64_t start, uint64_t length)
1393 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1395 if (USES_FULL_48BIT_PPGTT(vm->dev))
1396 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1398 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1401 static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1402 uint64_t start, uint64_t length,
1403 gen8_pte_t scratch_pte,
1406 struct i915_page_directory *pd;
1409 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1410 struct i915_page_table *pt;
1411 uint64_t pd_len = length;
1412 uint64_t pd_start = start;
1415 if (!test_bit(pdpe, pdp->used_pdpes))
1418 seq_printf(m, "\tPDPE #%d\n", pdpe);
1419 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1421 gen8_pte_t *pt_vaddr;
1423 if (!test_bit(pde, pd->used_pdes))
1426 pt_vaddr = kmap_px(pt);
1427 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1429 (pdpe << GEN8_PDPE_SHIFT) |
1430 (pde << GEN8_PDE_SHIFT) |
1431 (pte << GEN8_PTE_SHIFT);
1435 for (i = 0; i < 4; i++)
1436 if (pt_vaddr[pte + i] != scratch_pte)
1441 seq_printf(m, "\t\t0x%lx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1442 for (i = 0; i < 4; i++) {
1443 if (pt_vaddr[pte + i] != scratch_pte)
1444 seq_printf(m, " %lx", pt_vaddr[pte + i]);
1446 seq_puts(m, " SCRATCH ");
1450 /* don't use kunmap_px, it could trigger
1451 * an unnecessary flush.
1453 kunmap_atomic(pt_vaddr);
1458 static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1460 struct i915_address_space *vm = &ppgtt->base;
1461 uint64_t start = ppgtt->base.start;
1462 uint64_t length = ppgtt->base.total;
1463 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
1464 I915_CACHE_LLC, true);
1466 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1467 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1470 struct i915_pml4 *pml4 = &ppgtt->pml4;
1471 struct i915_page_directory_pointer *pdp;
1473 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1474 if (!test_bit(pml4e, pml4->used_pml4es))
1477 seq_printf(m, " PML4E #%lu\n", pml4e);
1478 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1483 static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1485 unsigned long *new_page_dirs, *new_page_tables;
1486 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1489 /* We allocate temp bitmap for page tables for no gain
1490 * but as this is for init only, lets keep the things simple
1492 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1496 /* Allocate for all pdps regardless of how the ppgtt
1499 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1503 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1505 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1511 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1512 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1513 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1517 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1521 ret = gen8_init_scratch(&ppgtt->base);
1525 ppgtt->base.start = 0;
1526 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1527 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1528 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
1529 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1530 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1531 ppgtt->base.bind_vma = ppgtt_bind_vma;
1532 ppgtt->debug_dump = gen8_dump_ppgtt;
1534 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1535 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1539 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1541 ppgtt->base.total = 1ULL << 48;
1542 ppgtt->switch_mm = gen8_48b_mm_switch;
1544 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
1548 ppgtt->base.total = 1ULL << 32;
1549 ppgtt->switch_mm = gen8_legacy_mm_switch;
1550 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1554 if (intel_vgpu_active(to_i915(ppgtt->base.dev))) {
1555 ret = gen8_preallocate_top_level_pdps(ppgtt);
1561 if (intel_vgpu_active(to_i915(ppgtt->base.dev)))
1562 gen8_ppgtt_notify_vgt(ppgtt, true);
1567 gen8_free_scratch(&ppgtt->base);
1571 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1573 struct i915_address_space *vm = &ppgtt->base;
1574 struct i915_page_table *unused;
1575 gen6_pte_t scratch_pte;
1578 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
1580 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1581 I915_CACHE_LLC, true, 0);
1583 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
1585 gen6_pte_t *pt_vaddr;
1586 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
1587 pd_entry = readl(ppgtt->pd_addr + pde);
1588 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1590 if (pd_entry != expected)
1591 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1595 seq_printf(m, "\tPDE: %x\n", pd_entry);
1597 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1599 for (pte = 0; pte < GEN6_PTES; pte+=4) {
1601 (pde * PAGE_SIZE * GEN6_PTES) +
1605 for (i = 0; i < 4; i++)
1606 if (pt_vaddr[pte + i] != scratch_pte)
1611 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1612 for (i = 0; i < 4; i++) {
1613 if (pt_vaddr[pte + i] != scratch_pte)
1614 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1616 seq_puts(m, " SCRATCH ");
1620 kunmap_px(ppgtt, pt_vaddr);
1624 /* Write pde (index) from the page directory @pd to the page table @pt */
1625 static void gen6_write_pde(struct i915_page_directory *pd,
1626 const int pde, struct i915_page_table *pt)
1628 /* Caller needs to make sure the write completes if necessary */
1629 struct i915_hw_ppgtt *ppgtt =
1630 container_of(pd, struct i915_hw_ppgtt, pd);
1633 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
1634 pd_entry |= GEN6_PDE_VALID;
1636 writel(pd_entry, ppgtt->pd_addr + pde);
1639 /* Write all the page tables found in the ppgtt structure to incrementing page
1641 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1642 struct i915_page_directory *pd,
1643 uint32_t start, uint32_t length)
1645 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1646 struct i915_page_table *pt;
1649 gen6_for_each_pde(pt, pd, start, length, pde)
1650 gen6_write_pde(pd, pde, pt);
1652 /* Make sure write is complete before other code can use this page
1653 * table. Also require for WC mapped PTEs */
1657 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1659 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1661 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
1664 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1665 struct drm_i915_gem_request *req)
1667 struct intel_engine_cs *engine = req->engine;
1670 /* NB: TLBs must be flushed and invalidated before a switch */
1671 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1675 ret = intel_ring_begin(req, 6);
1679 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(2));
1680 intel_ring_emit_reg(engine, RING_PP_DIR_DCLV(engine));
1681 intel_ring_emit(engine, PP_DIR_DCLV_2G);
1682 intel_ring_emit_reg(engine, RING_PP_DIR_BASE(engine));
1683 intel_ring_emit(engine, get_pd_offset(ppgtt));
1684 intel_ring_emit(engine, MI_NOOP);
1685 intel_ring_advance(engine);
1690 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1691 struct drm_i915_gem_request *req)
1693 struct intel_engine_cs *engine = req->engine;
1696 /* NB: TLBs must be flushed and invalidated before a switch */
1697 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1701 ret = intel_ring_begin(req, 6);
1705 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(2));
1706 intel_ring_emit_reg(engine, RING_PP_DIR_DCLV(engine));
1707 intel_ring_emit(engine, PP_DIR_DCLV_2G);
1708 intel_ring_emit_reg(engine, RING_PP_DIR_BASE(engine));
1709 intel_ring_emit(engine, get_pd_offset(ppgtt));
1710 intel_ring_emit(engine, MI_NOOP);
1711 intel_ring_advance(engine);
1713 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1714 if (engine->id != RCS) {
1715 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1723 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1724 struct drm_i915_gem_request *req)
1726 struct intel_engine_cs *engine = req->engine;
1727 struct drm_i915_private *dev_priv = req->i915;
1729 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1730 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
1734 static void gen8_ppgtt_enable(struct drm_device *dev)
1736 struct drm_i915_private *dev_priv = to_i915(dev);
1737 struct intel_engine_cs *engine;
1739 for_each_engine(engine, dev_priv) {
1740 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
1741 I915_WRITE(RING_MODE_GEN7(engine),
1742 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1746 static void gen7_ppgtt_enable(struct drm_device *dev)
1748 struct drm_i915_private *dev_priv = to_i915(dev);
1749 struct intel_engine_cs *engine;
1750 uint32_t ecochk, ecobits;
1752 ecobits = I915_READ(GAC_ECO_BITS);
1753 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1755 ecochk = I915_READ(GAM_ECOCHK);
1756 if (IS_HASWELL(dev)) {
1757 ecochk |= ECOCHK_PPGTT_WB_HSW;
1759 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1760 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1762 I915_WRITE(GAM_ECOCHK, ecochk);
1764 for_each_engine(engine, dev_priv) {
1765 /* GFX_MODE is per-ring on gen7+ */
1766 I915_WRITE(RING_MODE_GEN7(engine),
1767 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1771 static void gen6_ppgtt_enable(struct drm_device *dev)
1773 struct drm_i915_private *dev_priv = to_i915(dev);
1774 uint32_t ecochk, gab_ctl, ecobits;
1776 ecobits = I915_READ(GAC_ECO_BITS);
1777 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1778 ECOBITS_PPGTT_CACHE64B);
1780 gab_ctl = I915_READ(GAB_CTL);
1781 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1783 ecochk = I915_READ(GAM_ECOCHK);
1784 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1786 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1789 /* PPGTT support for Sandybdrige/Gen6 and later */
1790 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1795 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1796 gen6_pte_t *pt_vaddr, scratch_pte;
1797 unsigned first_entry = start >> PAGE_SHIFT;
1798 unsigned num_entries = length >> PAGE_SHIFT;
1799 unsigned act_pt = first_entry / GEN6_PTES;
1800 unsigned first_pte = first_entry % GEN6_PTES;
1801 unsigned last_pte, i;
1803 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1804 I915_CACHE_LLC, true, 0);
1806 while (num_entries) {
1807 last_pte = first_pte + num_entries;
1808 if (last_pte > GEN6_PTES)
1809 last_pte = GEN6_PTES;
1811 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1813 for (i = first_pte; i < last_pte; i++)
1814 pt_vaddr[i] = scratch_pte;
1816 kunmap_px(ppgtt, pt_vaddr);
1818 num_entries -= last_pte - first_pte;
1824 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1825 struct sg_table *pages,
1827 enum i915_cache_level cache_level, u32 flags)
1829 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1830 unsigned first_entry = start >> PAGE_SHIFT;
1831 unsigned act_pt = first_entry / GEN6_PTES;
1832 unsigned act_pte = first_entry % GEN6_PTES;
1833 gen6_pte_t *pt_vaddr = NULL;
1834 struct sgt_iter sgt_iter;
1837 for_each_sgt_dma(addr, sgt_iter, pages) {
1838 if (pt_vaddr == NULL)
1839 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1842 vm->pte_encode(addr, cache_level, true, flags);
1844 if (++act_pte == GEN6_PTES) {
1845 kunmap_px(ppgtt, pt_vaddr);
1853 kunmap_px(ppgtt, pt_vaddr);
1856 static int gen6_alloc_va_range(struct i915_address_space *vm,
1857 uint64_t start_in, uint64_t length_in)
1859 DECLARE_BITMAP(new_page_tables, I915_PDES);
1860 struct drm_device *dev = vm->dev;
1861 struct drm_i915_private *dev_priv = to_i915(dev);
1862 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1863 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1864 struct i915_page_table *pt;
1865 uint32_t start, length, start_save, length_save;
1869 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1872 start = start_save = start_in;
1873 length = length_save = length_in;
1875 bitmap_zero(new_page_tables, I915_PDES);
1877 /* The allocation is done in two stages so that we can bail out with
1878 * minimal amount of pain. The first stage finds new page tables that
1879 * need allocation. The second stage marks use ptes within the page
1882 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
1883 if (pt != vm->scratch_pt) {
1884 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1888 /* We've already allocated a page table */
1889 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1897 gen6_initialize_pt(vm, pt);
1899 ppgtt->pd.page_table[pde] = pt;
1900 __set_bit(pde, new_page_tables);
1901 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
1905 length = length_save;
1907 gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
1908 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1910 bitmap_zero(tmp_bitmap, GEN6_PTES);
1911 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1912 gen6_pte_count(start, length));
1914 if (__test_and_clear_bit(pde, new_page_tables))
1915 gen6_write_pde(&ppgtt->pd, pde, pt);
1917 trace_i915_page_table_entry_map(vm, pde, pt,
1918 gen6_pte_index(start),
1919 gen6_pte_count(start, length),
1921 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
1925 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1927 /* Make sure write is complete before other code can use this page
1928 * table. Also require for WC mapped PTEs */
1931 mark_tlbs_dirty(ppgtt);
1935 for_each_set_bit(pde, new_page_tables, I915_PDES) {
1936 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
1938 ppgtt->pd.page_table[pde] = vm->scratch_pt;
1939 free_pt(vm->dev, pt);
1942 mark_tlbs_dirty(ppgtt);
1946 static int gen6_init_scratch(struct i915_address_space *vm)
1948 struct drm_device *dev = vm->dev;
1950 vm->scratch_page = alloc_scratch_page(dev);
1951 if (IS_ERR(vm->scratch_page))
1952 return PTR_ERR(vm->scratch_page);
1954 vm->scratch_pt = alloc_pt(dev);
1955 if (IS_ERR(vm->scratch_pt)) {
1956 free_scratch_page(dev, vm->scratch_page);
1957 return PTR_ERR(vm->scratch_pt);
1960 gen6_initialize_pt(vm, vm->scratch_pt);
1965 static void gen6_free_scratch(struct i915_address_space *vm)
1967 struct drm_device *dev = vm->dev;
1969 free_pt(dev, vm->scratch_pt);
1970 free_scratch_page(dev, vm->scratch_page);
1973 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1975 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1976 struct i915_page_directory *pd = &ppgtt->pd;
1977 struct drm_device *dev = vm->dev;
1978 struct i915_page_table *pt;
1981 drm_mm_remove_node(&ppgtt->node);
1983 gen6_for_all_pdes(pt, pd, pde)
1984 if (pt != vm->scratch_pt)
1987 gen6_free_scratch(vm);
1990 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
1992 struct i915_address_space *vm = &ppgtt->base;
1993 struct drm_device *dev = ppgtt->base.dev;
1994 struct drm_i915_private *dev_priv = to_i915(dev);
1995 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1996 bool retried = false;
1999 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
2000 * allocator works in address space sizes, so it's multiplied by page
2001 * size. We allocate at the top of the GTT to avoid fragmentation.
2003 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
2005 ret = gen6_init_scratch(vm);
2010 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2011 &ppgtt->node, GEN6_PD_SIZE,
2013 0, ggtt->base.total,
2015 if (ret == -ENOSPC && !retried) {
2016 ret = i915_gem_evict_something(dev, &ggtt->base,
2017 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2019 0, ggtt->base.total,
2032 if (ppgtt->node.start < ggtt->mappable_end)
2033 DRM_DEBUG("Forced to use aperture for PDEs\n");
2038 gen6_free_scratch(vm);
2042 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2044 return gen6_ppgtt_allocate_page_directories(ppgtt);
2047 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2048 uint64_t start, uint64_t length)
2050 struct i915_page_table *unused;
2053 gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
2054 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
2057 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2059 struct drm_device *dev = ppgtt->base.dev;
2060 struct drm_i915_private *dev_priv = to_i915(dev);
2061 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2064 ppgtt->base.pte_encode = ggtt->base.pte_encode;
2065 if (intel_vgpu_active(dev_priv) || IS_GEN6(dev))
2066 ppgtt->switch_mm = gen6_mm_switch;
2067 else if (IS_HASWELL(dev))
2068 ppgtt->switch_mm = hsw_mm_switch;
2069 else if (IS_GEN7(dev))
2070 ppgtt->switch_mm = gen7_mm_switch;
2074 ret = gen6_ppgtt_alloc(ppgtt);
2078 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
2079 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
2080 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
2081 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
2082 ppgtt->base.bind_vma = ppgtt_bind_vma;
2083 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
2084 ppgtt->base.start = 0;
2085 ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
2086 ppgtt->debug_dump = gen6_dump_ppgtt;
2088 ppgtt->pd.base.ggtt_offset =
2089 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
2091 ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
2092 ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
2094 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
2096 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
2098 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
2099 ppgtt->node.size >> 20,
2100 ppgtt->node.start / PAGE_SIZE);
2102 DRM_DEBUG("Adding PPGTT at offset %x\n",
2103 ppgtt->pd.base.ggtt_offset << 10);
2108 static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2110 ppgtt->base.dev = dev;
2112 if (INTEL_INFO(dev)->gen < 8)
2113 return gen6_ppgtt_init(ppgtt);
2115 return gen8_ppgtt_init(ppgtt);
2118 static void i915_address_space_init(struct i915_address_space *vm,
2119 struct drm_i915_private *dev_priv)
2121 drm_mm_init(&vm->mm, vm->start, vm->total);
2122 vm->dev = &dev_priv->drm;
2123 INIT_LIST_HEAD(&vm->active_list);
2124 INIT_LIST_HEAD(&vm->inactive_list);
2125 list_add_tail(&vm->global_link, &dev_priv->vm_list);
2128 static void gtt_write_workarounds(struct drm_device *dev)
2130 struct drm_i915_private *dev_priv = to_i915(dev);
2132 /* This function is for gtt related workarounds. This function is
2133 * called on driver load and after a GPU reset, so you can place
2134 * workarounds here even if they get overwritten by GPU reset.
2136 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt */
2137 if (IS_BROADWELL(dev))
2138 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
2139 else if (IS_CHERRYVIEW(dev))
2140 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
2141 else if (IS_SKYLAKE(dev))
2142 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
2143 else if (IS_BROXTON(dev))
2144 I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
2147 static int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2149 struct drm_i915_private *dev_priv = to_i915(dev);
2152 ret = __hw_ppgtt_init(dev, ppgtt);
2154 kref_init(&ppgtt->ref);
2155 i915_address_space_init(&ppgtt->base, dev_priv);
2161 int i915_ppgtt_init_hw(struct drm_device *dev)
2163 gtt_write_workarounds(dev);
2165 /* In the case of execlists, PPGTT is enabled by the context descriptor
2166 * and the PDPs are contained within the context itself. We don't
2167 * need to do anything here. */
2168 if (i915.enable_execlists)
2171 if (!USES_PPGTT(dev))
2175 gen6_ppgtt_enable(dev);
2176 else if (IS_GEN7(dev))
2177 gen7_ppgtt_enable(dev);
2178 else if (INTEL_INFO(dev)->gen >= 8)
2179 gen8_ppgtt_enable(dev);
2181 MISSING_CASE(INTEL_INFO(dev)->gen);
2186 struct i915_hw_ppgtt *
2187 i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
2189 struct i915_hw_ppgtt *ppgtt;
2192 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2194 return ERR_PTR(-ENOMEM);
2196 ret = i915_ppgtt_init(dev, ppgtt);
2199 return ERR_PTR(ret);
2202 ppgtt->file_priv = fpriv;
2204 trace_i915_ppgtt_create(&ppgtt->base);
2209 void i915_ppgtt_release(struct kref *kref)
2211 struct i915_hw_ppgtt *ppgtt =
2212 container_of(kref, struct i915_hw_ppgtt, ref);
2214 trace_i915_ppgtt_release(&ppgtt->base);
2216 /* vmas should already be unbound */
2217 WARN_ON(!list_empty(&ppgtt->base.active_list));
2218 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2220 list_del(&ppgtt->base.global_link);
2221 drm_mm_takedown(&ppgtt->base.mm);
2223 ppgtt->base.cleanup(&ppgtt->base);
2227 extern int intel_iommu_gfx_mapped;
2228 /* Certain Gen5 chipsets require require idling the GPU before
2229 * unmapping anything from the GTT when VT-d is enabled.
2231 static bool needs_idle_maps(struct drm_device *dev)
2233 #ifdef CONFIG_INTEL_IOMMU
2234 /* Query intel_iommu to see if we need the workaround. Presumably that
2237 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
2243 static bool do_idling(struct drm_i915_private *dev_priv)
2245 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2246 bool ret = dev_priv->mm.interruptible;
2248 if (unlikely(ggtt->do_idle_maps)) {
2249 dev_priv->mm.interruptible = false;
2250 if (i915_gem_wait_for_idle(dev_priv)) {
2251 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2252 /* Wait a bit, in hopes it avoids the hang */
2260 static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
2262 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2264 if (unlikely(ggtt->do_idle_maps))
2265 dev_priv->mm.interruptible = interruptible;
2268 void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
2270 struct intel_engine_cs *engine;
2272 if (INTEL_INFO(dev_priv)->gen < 6)
2275 for_each_engine(engine, dev_priv) {
2277 fault_reg = I915_READ(RING_FAULT_REG(engine));
2278 if (fault_reg & RING_FAULT_VALID) {
2279 DRM_DEBUG_DRIVER("Unexpected fault\n"
2281 "\tAddress space: %s\n"
2284 fault_reg & LINUX_PAGE_MASK,
2285 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2286 RING_FAULT_SRCID(fault_reg),
2287 RING_FAULT_FAULT_TYPE(fault_reg));
2288 I915_WRITE(RING_FAULT_REG(engine),
2289 fault_reg & ~RING_FAULT_VALID);
2292 POSTING_READ(RING_FAULT_REG(&dev_priv->engine[RCS]));
2295 static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2297 if (INTEL_INFO(dev_priv)->gen < 6) {
2298 intel_gtt_chipset_flush();
2300 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2301 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2305 void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2307 struct drm_i915_private *dev_priv = to_i915(dev);
2308 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2310 /* Don't bother messing with faults pre GEN6 as we have little
2311 * documentation supporting that it's a good idea.
2313 if (INTEL_INFO(dev)->gen < 6)
2316 i915_check_and_clear_faults(dev_priv);
2318 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
2321 i915_ggtt_flush(dev_priv);
2324 int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
2326 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2327 obj->pages->sgl, obj->pages->nents,
2328 PCI_DMA_BIDIRECTIONAL))
2334 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2339 iowrite32((u32)pte, addr);
2340 iowrite32(pte >> 32, addr + 4);
2344 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
2347 enum i915_cache_level level,
2350 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2351 gen8_pte_t __iomem *pte =
2352 (gen8_pte_t __iomem *)dev_priv->ggtt.gsm +
2353 (offset >> PAGE_SHIFT);
2356 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2358 gen8_set_pte(pte, gen8_pte_encode(addr, level, true));
2360 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2361 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2363 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2366 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2367 struct sg_table *st,
2369 enum i915_cache_level level, u32 unused)
2371 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2372 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2373 struct sgt_iter sgt_iter;
2374 gen8_pte_t __iomem *gtt_entries;
2375 gen8_pte_t gtt_entry;
2380 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2382 gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2384 for_each_sgt_dma(addr, sgt_iter, st) {
2385 gtt_entry = gen8_pte_encode(addr, level, true);
2386 gen8_set_pte(>t_entries[i++], gtt_entry);
2390 * XXX: This serves as a posting read to make sure that the PTE has
2391 * actually been updated. There is some concern that even though
2392 * registers and PTEs are within the same BAR that they are potentially
2393 * of NUMA access patterns. Therefore, even with the way we assume
2394 * hardware should work, we must keep this posting read for paranoia.
2397 WARN_ON(readq(>t_entries[i-1]) != gtt_entry);
2399 /* This next bit makes the above posting read even more important. We
2400 * want to flush the TLBs only after we're certain all the PTE updates
2403 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2404 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2406 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2409 struct insert_entries {
2410 struct i915_address_space *vm;
2411 struct sg_table *st;
2413 enum i915_cache_level level;
2417 static int gen8_ggtt_insert_entries__cb(void *_arg)
2419 struct insert_entries *arg = _arg;
2420 gen8_ggtt_insert_entries(arg->vm, arg->st,
2421 arg->start, arg->level, arg->flags);
2425 static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2426 struct sg_table *st,
2428 enum i915_cache_level level,
2431 struct insert_entries arg = { vm, st, start, level, flags };
2432 #ifndef __DragonFly__
2433 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2435 /* XXX: is this enough ?
2436 * See Linux commit 5bab6f60cb4d1417ad7c599166bcfec87529c1a2 */
2438 gen8_ggtt_insert_entries__cb(&arg);
2443 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
2446 enum i915_cache_level level,
2449 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2450 gen6_pte_t __iomem *pte =
2451 (gen6_pte_t __iomem *)dev_priv->ggtt.gsm +
2452 (offset >> PAGE_SHIFT);
2455 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2457 iowrite32(vm->pte_encode(addr, level, true, flags), pte);
2459 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2460 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2462 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2466 * Binds an object into the global gtt with the specified cache level. The object
2467 * will be accessible to the GPU via commands whose operands reference offsets
2468 * within the global GTT as well as accessible by the GPU through the GMADR
2469 * mapped BAR (dev_priv->mm.gtt->gtt).
2471 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2472 struct sg_table *st,
2474 enum i915_cache_level level, u32 flags)
2476 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2477 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2478 struct sgt_iter sgt_iter;
2479 gen6_pte_t __iomem *gtt_entries;
2480 gen6_pte_t gtt_entry;
2485 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2487 gtt_entries = (gen6_pte_t __iomem *)ggtt->gsm + (start >> PAGE_SHIFT);
2489 for_each_sgt_dma(addr, sgt_iter, st) {
2490 gtt_entry = vm->pte_encode(addr, level, true, flags);
2491 iowrite32(gtt_entry, >t_entries[i++]);
2494 /* XXX: This serves as a posting read to make sure that the PTE has
2495 * actually been updated. There is some concern that even though
2496 * registers and PTEs are within the same BAR that they are potentially
2497 * of NUMA access patterns. Therefore, even with the way we assume
2498 * hardware should work, we must keep this posting read for paranoia.
2501 WARN_ON(readl(>t_entries[i-1]) != gtt_entry);
2503 /* This next bit makes the above posting read even more important. We
2504 * want to flush the TLBs only after we're certain all the PTE updates
2507 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2508 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2510 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2513 static void nop_clear_range(struct i915_address_space *vm,
2520 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2525 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2526 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2527 unsigned first_entry = start >> PAGE_SHIFT;
2528 unsigned num_entries = length >> PAGE_SHIFT;
2529 gen8_pte_t scratch_pte, __iomem *gtt_base =
2530 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2531 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2535 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2537 if (WARN(num_entries > max_entries,
2538 "First entry = %d; Num entries = %d (max=%d)\n",
2539 first_entry, num_entries, max_entries))
2540 num_entries = max_entries;
2542 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
2545 for (i = 0; i < num_entries; i++)
2546 gen8_set_pte(>t_base[i], scratch_pte);
2549 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2552 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2557 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2558 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
2559 unsigned first_entry = start >> PAGE_SHIFT;
2560 unsigned num_entries = length >> PAGE_SHIFT;
2561 gen6_pte_t scratch_pte, __iomem *gtt_base =
2562 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2563 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2567 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2569 if (WARN(num_entries > max_entries,
2570 "First entry = %d; Num entries = %d (max=%d)\n",
2571 first_entry, num_entries, max_entries))
2572 num_entries = max_entries;
2574 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2575 I915_CACHE_LLC, use_scratch, 0);
2577 for (i = 0; i < num_entries; i++)
2578 iowrite32(scratch_pte, >t_base[i]);
2581 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2584 static void i915_ggtt_insert_page(struct i915_address_space *vm,
2587 enum i915_cache_level cache_level,
2590 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2591 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2592 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2595 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2597 intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
2599 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
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 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2608 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2609 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2612 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2614 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
2616 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2620 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2625 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2626 unsigned first_entry = start >> PAGE_SHIFT;
2627 unsigned num_entries = length >> PAGE_SHIFT;
2630 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2632 intel_gtt_clear_range(first_entry, num_entries);
2634 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2637 static int ggtt_bind_vma(struct i915_vma *vma,
2638 enum i915_cache_level cache_level,
2641 struct drm_i915_gem_object *obj = vma->obj;
2645 ret = i915_get_ggtt_vma_pages(vma);
2649 /* Currently applicable only to VLV */
2651 pte_flags |= PTE_READ_ONLY;
2653 vma->vm->insert_entries(vma->vm, vma->ggtt_view.pages,
2655 cache_level, pte_flags);
2658 * Without aliasing PPGTT there's no difference between
2659 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2660 * upgrade to both bound if we bind either to avoid double-binding.
2662 vma->bound |= GLOBAL_BIND | LOCAL_BIND;
2667 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2668 enum i915_cache_level cache_level,
2674 ret = i915_get_ggtt_vma_pages(vma);
2678 /* Currently applicable only to VLV */
2680 if (vma->obj->gt_ro)
2681 pte_flags |= PTE_READ_ONLY;
2684 if (flags & GLOBAL_BIND) {
2685 vma->vm->insert_entries(vma->vm,
2686 vma->ggtt_view.pages,
2688 cache_level, pte_flags);
2691 if (flags & LOCAL_BIND) {
2692 struct i915_hw_ppgtt *appgtt =
2693 to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2694 appgtt->base.insert_entries(&appgtt->base,
2695 vma->ggtt_view.pages,
2697 cache_level, pte_flags);
2703 static void ggtt_unbind_vma(struct i915_vma *vma)
2705 struct drm_device *dev = vma->vm->dev;
2706 struct drm_i915_private *dev_priv = to_i915(dev);
2707 struct drm_i915_gem_object *obj = vma->obj;
2708 const uint64_t size = min_t(uint64_t,
2712 if (vma->bound & GLOBAL_BIND) {
2713 vma->vm->clear_range(vma->vm,
2719 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
2720 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2722 appgtt->base.clear_range(&appgtt->base,
2729 void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2731 struct drm_device *dev = obj->base.dev;
2732 struct drm_i915_private *dev_priv = to_i915(dev);
2735 interruptible = do_idling(dev_priv);
2737 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2738 PCI_DMA_BIDIRECTIONAL);
2740 undo_idling(dev_priv, interruptible);
2743 static void i915_gtt_color_adjust(struct drm_mm_node *node,
2744 unsigned long color,
2748 if (node->color != color)
2751 if (!list_empty(&node->node_list)) {
2752 node = list_entry(node->node_list.next,
2755 if (node->allocated && node->color != color)
2760 static int i915_gem_setup_global_gtt(struct drm_device *dev,
2765 /* Let GEM Manage all of the aperture.
2767 * However, leave one page at the end still bound to the scratch page.
2768 * There are a number of places where the hardware apparently prefetches
2769 * past the end of the object, and we've seen multiple hangs with the
2770 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2771 * aperture. One page should be enough to keep any prefetching inside
2774 struct drm_i915_private *dev_priv = to_i915(dev);
2775 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2776 struct drm_mm_node *entry;
2777 struct drm_i915_gem_object *obj;
2778 unsigned long hole_start, hole_end;
2780 unsigned long mappable;
2783 mappable = min(end, mappable_end) - start;
2784 BUG_ON(mappable_end > end);
2786 ggtt->base.start = start;
2788 /* Subtract the guard page before address space initialization to
2789 * shrink the range used by drm_mm */
2790 ggtt->base.total = end - start - PAGE_SIZE;
2791 i915_address_space_init(&ggtt->base, dev_priv);
2792 ggtt->base.total += PAGE_SIZE;
2794 ret = intel_vgt_balloon(dev_priv);
2799 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
2801 /* Mark any preallocated objects as occupied */
2802 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2803 struct i915_vma *vma = i915_gem_obj_to_vma(obj, &ggtt->base);
2805 DRM_DEBUG_KMS("reserving preallocated space: %llx + %zx\n",
2806 i915_gem_obj_ggtt_offset(obj), obj->base.size);
2808 WARN_ON(i915_gem_obj_ggtt_bound(obj));
2809 ret = drm_mm_reserve_node(&ggtt->base.mm, &vma->node);
2811 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2814 vma->bound |= GLOBAL_BIND;
2815 __i915_vma_set_map_and_fenceable(vma);
2816 list_add_tail(&vma->vm_link, &ggtt->base.inactive_list);
2819 /* Clear any non-preallocated blocks */
2820 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
2821 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2822 hole_start, hole_end);
2823 ggtt->base.clear_range(&ggtt->base, hole_start,
2824 hole_end - hole_start, true);
2827 #ifdef __DragonFly__
2828 device_printf(dev->dev->bsddev,
2829 "taking over the fictitious range 0x%llx-0x%llx\n",
2830 dev_priv->ggtt.mappable_base + start, dev_priv->ggtt.mappable_base + start + mappable);
2831 error = -vm_phys_fictitious_reg_range(dev_priv->ggtt.mappable_base + start,
2832 dev_priv->ggtt.mappable_base + start + mappable, VM_MEMATTR_WRITE_COMBINING);
2835 /* And finally clear the reserved guard page */
2836 ggtt->base.clear_range(&ggtt->base, end - PAGE_SIZE, PAGE_SIZE, true);
2838 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2839 struct i915_hw_ppgtt *ppgtt;
2841 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2845 ret = __hw_ppgtt_init(dev, ppgtt);
2847 ppgtt->base.cleanup(&ppgtt->base);
2852 if (ppgtt->base.allocate_va_range)
2853 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2856 ppgtt->base.cleanup(&ppgtt->base);
2861 ppgtt->base.clear_range(&ppgtt->base,
2866 dev_priv->mm.aliasing_ppgtt = ppgtt;
2867 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2868 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2875 * i915_gem_init_ggtt - Initialize GEM for Global GTT
2878 void i915_gem_init_ggtt(struct drm_device *dev)
2880 struct drm_i915_private *dev_priv = to_i915(dev);
2881 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2883 i915_gem_setup_global_gtt(dev, 0, ggtt->mappable_end, ggtt->base.total);
2887 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
2890 void i915_ggtt_cleanup_hw(struct drm_device *dev)
2892 struct drm_i915_private *dev_priv = to_i915(dev);
2893 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2895 if (dev_priv->mm.aliasing_ppgtt) {
2896 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2898 ppgtt->base.cleanup(&ppgtt->base);
2902 i915_gem_cleanup_stolen(dev);
2904 if (drm_mm_initialized(&ggtt->base.mm)) {
2905 intel_vgt_deballoon(dev_priv);
2907 drm_mm_takedown(&ggtt->base.mm);
2908 list_del(&ggtt->base.global_link);
2911 ggtt->base.cleanup(&ggtt->base);
2914 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2916 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2917 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2918 return snb_gmch_ctl << 20;
2921 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2923 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2924 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2926 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2928 #ifdef CONFIG_X86_32
2929 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2930 if (bdw_gmch_ctl > 4)
2934 return bdw_gmch_ctl << 20;
2937 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2939 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2940 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2943 return 1 << (20 + gmch_ctrl);
2948 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
2950 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2951 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2952 return snb_gmch_ctl << 25; /* 32 MB units */
2955 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2957 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2958 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2959 return bdw_gmch_ctl << 25; /* 32 MB units */
2962 static size_t chv_get_stolen_size(u16 gmch_ctrl)
2964 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2965 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2968 * 0x0 to 0x10: 32MB increments starting at 0MB
2969 * 0x11 to 0x16: 4MB increments starting at 8MB
2970 * 0x17 to 0x1d: 4MB increments start at 36MB
2972 if (gmch_ctrl < 0x11)
2973 return gmch_ctrl << 25;
2974 else if (gmch_ctrl < 0x17)
2975 return (gmch_ctrl - 0x11 + 2) << 22;
2977 return (gmch_ctrl - 0x17 + 9) << 22;
2980 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2982 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2983 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2985 if (gen9_gmch_ctl < 0xf0)
2986 return gen9_gmch_ctl << 25; /* 32 MB units */
2988 /* 4MB increments starting at 0xf0 for 4MB */
2989 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2992 static int ggtt_probe_common(struct drm_device *dev,
2995 struct drm_i915_private *dev_priv = to_i915(dev);
2996 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2997 struct i915_page_scratch *scratch_page;
2998 phys_addr_t ggtt_phys_addr;
3000 /* For Modern GENs the PTEs and register space are split in the BAR */
3001 ggtt_phys_addr = pci_resource_start(dev->pdev, 0) +
3002 (pci_resource_len(dev->pdev, 0) / 2);
3005 * On BXT writes larger than 64 bit to the GTT pagetable range will be
3006 * dropped. For WC mappings in general we have 64 byte burst writes
3007 * when the WC buffer is flushed, so we can't use it, but have to
3008 * resort to an uncached mapping. The WC issue is easily caught by the
3009 * readback check when writing GTT PTE entries.
3011 if (IS_BROXTON(dev))
3012 ggtt->gsm = ioremap_nocache(ggtt_phys_addr, gtt_size);
3014 ggtt->gsm = ioremap_wc(ggtt_phys_addr, gtt_size);
3016 DRM_ERROR("Failed to map the gtt page table\n");
3020 scratch_page = alloc_scratch_page(dev);
3021 if (IS_ERR(scratch_page)) {
3022 DRM_ERROR("Scratch setup failed\n");
3023 /* iounmap will also get called at remove, but meh */
3025 return PTR_ERR(scratch_page);
3028 ggtt->base.scratch_page = scratch_page;
3033 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3034 * bits. When using advanced contexts each context stores its own PAT, but
3035 * writing this data shouldn't be harmful even in those cases. */
3036 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
3040 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
3041 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
3042 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
3043 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3044 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3045 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3046 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3047 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3049 if (!USES_PPGTT(dev_priv))
3050 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3051 * so RTL will always use the value corresponding to
3053 * So let's disable cache for GGTT to avoid screen corruptions.
3054 * MOCS still can be used though.
3055 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3056 * before this patch, i.e. the same uncached + snooping access
3057 * like on gen6/7 seems to be in effect.
3058 * - So this just fixes blitter/render access. Again it looks
3059 * like it's not just uncached access, but uncached + snooping.
3060 * So we can still hold onto all our assumptions wrt cpu
3061 * clflushing on LLC machines.
3063 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3065 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3066 * write would work. */
3067 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3068 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3071 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3076 * Map WB on BDW to snooped on CHV.
3078 * Only the snoop bit has meaning for CHV, the rest is
3081 * The hardware will never snoop for certain types of accesses:
3082 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3083 * - PPGTT page tables
3084 * - some other special cycles
3086 * As with BDW, we also need to consider the following for GT accesses:
3087 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3088 * so RTL will always use the value corresponding to
3090 * Which means we must set the snoop bit in PAT entry 0
3091 * in order to keep the global status page working.
3093 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3097 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3098 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3099 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3100 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3102 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3103 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3106 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
3108 struct drm_device *dev = ggtt->base.dev;
3109 struct drm_i915_private *dev_priv = to_i915(dev);
3113 /* TODO: We're not aware of mappable constraints on gen8 yet */
3114 ggtt->mappable_base = pci_resource_start(dev->pdev, 2);
3115 ggtt->mappable_end = pci_resource_len(dev->pdev, 2);
3118 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
3119 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
3122 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3124 if (INTEL_INFO(dev)->gen >= 9) {
3125 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
3126 ggtt->size = gen8_get_total_gtt_size(snb_gmch_ctl);
3127 } else if (IS_CHERRYVIEW(dev)) {
3128 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
3129 ggtt->size = chv_get_total_gtt_size(snb_gmch_ctl);
3131 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
3132 ggtt->size = gen8_get_total_gtt_size(snb_gmch_ctl);
3135 ggtt->base.total = (ggtt->size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3137 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3138 chv_setup_private_ppat(dev_priv);
3140 bdw_setup_private_ppat(dev_priv);
3142 ret = ggtt_probe_common(dev, ggtt->size);
3144 ggtt->base.bind_vma = ggtt_bind_vma;
3145 ggtt->base.unbind_vma = ggtt_unbind_vma;
3146 ggtt->base.insert_page = gen8_ggtt_insert_page;
3147 ggtt->base.clear_range = nop_clear_range;
3148 if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
3149 ggtt->base.clear_range = gen8_ggtt_clear_range;
3151 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3152 if (IS_CHERRYVIEW(dev_priv))
3153 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3158 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
3160 struct drm_device *dev = ggtt->base.dev;
3164 ggtt->mappable_base = pci_resource_start(dev->pdev, 2);
3165 ggtt->mappable_end = pci_resource_len(dev->pdev, 2);
3167 /* 64/512MB is the current min/max we actually know of, but this is just
3168 * a coarse sanity check.
3170 if ((ggtt->mappable_end < (64<<20) || (ggtt->mappable_end > (512<<20)))) {
3171 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
3176 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
3177 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
3179 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3181 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
3182 ggtt->size = gen6_get_total_gtt_size(snb_gmch_ctl);
3183 ggtt->base.total = (ggtt->size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3185 ret = ggtt_probe_common(dev, ggtt->size);
3187 ggtt->base.clear_range = gen6_ggtt_clear_range;
3188 ggtt->base.insert_page = gen6_ggtt_insert_page;
3189 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3190 ggtt->base.bind_vma = ggtt_bind_vma;
3191 ggtt->base.unbind_vma = ggtt_unbind_vma;
3196 static void gen6_gmch_remove(struct i915_address_space *vm)
3198 struct i915_ggtt *ggtt = container_of(vm, struct i915_ggtt, base);
3201 free_scratch_page(vm->dev, vm->scratch_page);
3204 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3206 struct drm_device *dev = ggtt->base.dev;
3207 struct drm_i915_private *dev_priv = to_i915(dev);
3211 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
3213 DRM_ERROR("failed to set up gmch\n");
3218 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3219 &ggtt->mappable_base, &ggtt->mappable_end);
3221 ggtt->do_idle_maps = needs_idle_maps(&dev_priv->drm);
3222 ggtt->base.insert_page = i915_ggtt_insert_page;
3223 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3224 ggtt->base.clear_range = i915_ggtt_clear_range;
3225 ggtt->base.bind_vma = ggtt_bind_vma;
3226 ggtt->base.unbind_vma = ggtt_unbind_vma;
3228 if (unlikely(ggtt->do_idle_maps))
3229 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3234 static void i915_gmch_remove(struct i915_address_space *vm)
3236 intel_gmch_remove();
3240 * i915_ggtt_init_hw - Initialize GGTT hardware
3243 int i915_ggtt_init_hw(struct drm_device *dev)
3245 struct drm_i915_private *dev_priv = to_i915(dev);
3246 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3249 if (INTEL_INFO(dev)->gen <= 5) {
3250 ggtt->probe = i915_gmch_probe;
3251 ggtt->base.cleanup = i915_gmch_remove;
3252 } else if (INTEL_INFO(dev)->gen < 8) {
3253 ggtt->probe = gen6_gmch_probe;
3254 ggtt->base.cleanup = gen6_gmch_remove;
3257 ggtt->base.pte_encode = iris_pte_encode;
3258 else if (IS_HASWELL(dev))
3259 ggtt->base.pte_encode = hsw_pte_encode;
3260 else if (IS_VALLEYVIEW(dev))
3261 ggtt->base.pte_encode = byt_pte_encode;
3262 else if (INTEL_INFO(dev)->gen >= 7)
3263 ggtt->base.pte_encode = ivb_pte_encode;
3265 ggtt->base.pte_encode = snb_pte_encode;
3267 ggtt->probe = gen8_gmch_probe;
3268 ggtt->base.cleanup = gen6_gmch_remove;
3271 ggtt->base.dev = dev;
3272 ggtt->base.is_ggtt = true;
3274 ret = ggtt->probe(ggtt);
3278 if ((ggtt->base.total - 1) >> 32) {
3279 DRM_ERROR("We never expected a Global GTT with more than 32bits"
3280 "of address space! Found %lldM!\n",
3281 ggtt->base.total >> 20);
3282 ggtt->base.total = 1ULL << 32;
3283 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3287 * Initialise stolen early so that we may reserve preallocated
3288 * objects for the BIOS to KMS transition.
3290 ret = i915_gem_init_stolen(dev);
3292 goto out_gtt_cleanup;
3294 /* GMADR is the PCI mmio aperture into the global GTT. */
3295 DRM_INFO("Memory usable by graphics device = %lluM\n",
3296 ggtt->base.total >> 20);
3297 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3298 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
3299 #ifdef CONFIG_INTEL_IOMMU
3300 if (intel_iommu_gfx_mapped)
3301 DRM_INFO("VT-d active for gfx access\n");
3307 ggtt->base.cleanup(&ggtt->base);
3312 int i915_ggtt_enable_hw(struct drm_device *dev)
3314 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
3320 void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3322 struct drm_i915_private *dev_priv = to_i915(dev);
3323 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3324 struct drm_i915_gem_object *obj;
3325 struct i915_vma *vma;
3327 i915_check_and_clear_faults(dev_priv);
3329 /* First fill our portion of the GTT with scratch pages */
3330 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
3333 /* Cache flush objects bound into GGTT and rebind them. */
3334 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
3335 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3336 if (vma->vm != &ggtt->base)
3339 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3343 if (obj->pin_display)
3344 WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
3347 if (INTEL_INFO(dev)->gen >= 8) {
3348 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3349 chv_setup_private_ppat(dev_priv);
3351 bdw_setup_private_ppat(dev_priv);
3356 if (USES_PPGTT(dev)) {
3357 struct i915_address_space *vm;
3359 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3360 /* TODO: Perhaps it shouldn't be gen6 specific */
3362 struct i915_hw_ppgtt *ppgtt;
3365 ppgtt = dev_priv->mm.aliasing_ppgtt;
3367 ppgtt = i915_vm_to_ppgtt(vm);
3369 gen6_write_page_range(dev_priv, &ppgtt->pd,
3370 0, ppgtt->base.total);
3374 i915_ggtt_flush(dev_priv);
3377 static struct i915_vma *
3378 __i915_gem_vma_create(struct drm_i915_gem_object *obj,
3379 struct i915_address_space *vm,
3380 const struct i915_ggtt_view *ggtt_view)
3382 struct i915_vma *vma;
3384 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3385 return ERR_PTR(-EINVAL);
3387 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
3389 return ERR_PTR(-ENOMEM);
3391 INIT_LIST_HEAD(&vma->vm_link);
3392 INIT_LIST_HEAD(&vma->obj_link);
3393 INIT_LIST_HEAD(&vma->exec_list);
3396 vma->is_ggtt = i915_is_ggtt(vm);
3398 if (i915_is_ggtt(vm))
3399 vma->ggtt_view = *ggtt_view;
3401 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
3403 list_add_tail(&vma->obj_link, &obj->vma_list);
3409 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3410 struct i915_address_space *vm)
3412 struct i915_vma *vma;
3414 vma = i915_gem_obj_to_vma(obj, vm);
3416 vma = __i915_gem_vma_create(obj, vm,
3417 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
3423 i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
3424 const struct i915_ggtt_view *view)
3426 struct drm_device *dev = obj->base.dev;
3427 struct drm_i915_private *dev_priv = to_i915(dev);
3428 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3429 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
3432 vma = __i915_gem_vma_create(obj, &ggtt->base, view);
3438 static struct scatterlist *
3439 rotate_pages(const dma_addr_t *in, unsigned int offset,
3440 unsigned int width, unsigned int height,
3441 unsigned int stride,
3442 struct sg_table *st, struct scatterlist *sg)
3444 unsigned int column, row;
3445 unsigned int src_idx;
3447 for (column = 0; column < width; column++) {
3448 src_idx = stride * (height - 1) + column;
3449 for (row = 0; row < height; row++) {
3451 /* We don't need the pages, but need to initialize
3452 * the entries so the sg list can be happily traversed.
3453 * The only thing we need are DMA addresses.
3455 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3456 sg_dma_address(sg) = in[offset + src_idx];
3457 sg_dma_len(sg) = PAGE_SIZE;
3466 static struct sg_table *
3467 intel_rotate_fb_obj_pages(struct intel_rotation_info *rot_info,
3468 struct drm_i915_gem_object *obj)
3470 unsigned int size_pages = rot_info->plane[0].width * rot_info->plane[0].height;
3471 unsigned int size_pages_uv;
3472 struct sgt_iter sgt_iter;
3473 dma_addr_t dma_addr;
3475 dma_addr_t *page_addr_list;
3476 struct sg_table *st;
3477 unsigned int uv_start_page;
3478 struct scatterlist *sg;
3481 /* Allocate a temporary list of source pages for random access. */
3482 page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
3483 sizeof(dma_addr_t));
3484 if (!page_addr_list)
3485 return ERR_PTR(ret);
3487 /* Account for UV plane with NV12. */
3488 if (rot_info->pixel_format == DRM_FORMAT_NV12)
3489 size_pages_uv = rot_info->plane[1].width * rot_info->plane[1].height;
3493 /* Allocate target SG list. */
3494 st = kmalloc(sizeof(*st), M_DRM, M_WAITOK);
3498 ret = sg_alloc_table(st, size_pages + size_pages_uv, GFP_KERNEL);
3502 /* Populate source page list from the object. */
3504 for_each_sgt_dma(dma_addr, sgt_iter, obj->pages)
3505 page_addr_list[i++] = dma_addr;
3507 GEM_BUG_ON(i != n_pages);
3511 /* Rotate the pages. */
3512 sg = rotate_pages(page_addr_list, 0,
3513 rot_info->plane[0].width, rot_info->plane[0].height,
3514 rot_info->plane[0].width,
3517 /* Append the UV plane if NV12. */
3518 if (rot_info->pixel_format == DRM_FORMAT_NV12) {
3519 uv_start_page = size_pages;
3521 /* Check for tile-row un-alignment. */
3522 if (offset_in_page(rot_info->uv_offset))
3525 rot_info->uv_start_page = uv_start_page;
3527 sg = rotate_pages(page_addr_list, rot_info->uv_start_page,
3528 rot_info->plane[1].width, rot_info->plane[1].height,
3529 rot_info->plane[1].width,
3533 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages (%u plane 0)).\n",
3534 obj->base.size, rot_info->plane[0].width,
3535 rot_info->plane[0].height, size_pages + size_pages_uv,
3538 drm_free_large(page_addr_list);
3545 drm_free_large(page_addr_list);
3547 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%d) (%ux%u tiles, %u pages (%u plane 0))\n",
3548 obj->base.size, ret, rot_info->plane[0].width,
3549 rot_info->plane[0].height, size_pages + size_pages_uv,
3551 return ERR_PTR(ret);
3554 static struct sg_table *
3555 intel_partial_pages(const struct i915_ggtt_view *view,
3556 struct drm_i915_gem_object *obj)
3558 struct sg_table *st;
3559 struct scatterlist *sg;
3560 struct sg_page_iter obj_sg_iter;
3563 st = kmalloc(sizeof(*st), M_DRM, M_WAITOK);
3567 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3573 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3574 view->params.partial.offset)
3576 if (st->nents >= view->params.partial.size)
3579 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3580 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3581 sg_dma_len(sg) = PAGE_SIZE;
3592 return ERR_PTR(ret);
3596 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3600 if (vma->ggtt_view.pages)
3603 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
3604 vma->ggtt_view.pages = vma->obj->pages;
3605 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
3606 vma->ggtt_view.pages =
3607 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
3608 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
3609 vma->ggtt_view.pages =
3610 intel_partial_pages(&vma->ggtt_view, vma->obj);
3612 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3613 vma->ggtt_view.type);
3615 if (!vma->ggtt_view.pages) {
3616 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
3617 vma->ggtt_view.type);
3619 } else if (IS_ERR(vma->ggtt_view.pages)) {
3620 ret = PTR_ERR(vma->ggtt_view.pages);
3621 vma->ggtt_view.pages = NULL;
3622 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3623 vma->ggtt_view.type, ret);
3630 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3632 * @cache_level: mapping cache level
3633 * @flags: flags like global or local mapping
3635 * DMA addresses are taken from the scatter-gather table of this object (or of
3636 * this VMA in case of non-default GGTT views) and PTE entries set up.
3637 * Note that DMA addresses are also the only part of the SG table we care about.
3639 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3645 if (WARN_ON(flags == 0))
3649 if (flags & PIN_GLOBAL)
3650 bind_flags |= GLOBAL_BIND;
3651 if (flags & PIN_USER)
3652 bind_flags |= LOCAL_BIND;
3654 if (flags & PIN_UPDATE)
3655 bind_flags |= vma->bound;
3657 bind_flags &= ~vma->bound;
3659 if (bind_flags == 0)
3662 if (vma->bound == 0 && vma->vm->allocate_va_range) {
3663 /* XXX: i915_vma_pin() will fix this +- hack */
3665 trace_i915_va_alloc(vma);
3666 ret = vma->vm->allocate_va_range(vma->vm,
3674 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
3678 vma->bound |= bind_flags;
3684 * i915_ggtt_view_size - Get the size of a GGTT view.
3685 * @obj: Object the view is of.
3686 * @view: The view in question.
3688 * @return The size of the GGTT view in bytes.
3691 i915_ggtt_view_size(struct drm_i915_gem_object *obj,
3692 const struct i915_ggtt_view *view)
3694 if (view->type == I915_GGTT_VIEW_NORMAL) {
3695 return obj->base.size;
3696 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3697 return intel_rotation_info_size(&view->params.rotated) << PAGE_SHIFT;
3698 } else if (view->type == I915_GGTT_VIEW_PARTIAL) {
3699 return view->params.partial.size << PAGE_SHIFT;
3701 WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
3702 return obj->base.size;
3706 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma)
3710 lockdep_assert_held(&vma->vm->dev->struct_mutex);
3711 if (WARN_ON(!vma->obj->map_and_fenceable))
3712 return ERR_PTR(-ENODEV);
3714 GEM_BUG_ON(!vma->is_ggtt);
3715 GEM_BUG_ON((vma->bound & GLOBAL_BIND) == 0);
3719 ptr = io_mapping_map_wc(i915_vm_to_ggtt(vma->vm)->mappable,
3723 return ERR_PTR(-ENOMEM);