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).
101 i915_get_ggtt_vma_pages(struct i915_vma *vma);
103 const struct i915_ggtt_view i915_ggtt_view_normal = {
104 .type = I915_GGTT_VIEW_NORMAL,
106 const struct i915_ggtt_view i915_ggtt_view_rotated = {
107 .type = I915_GGTT_VIEW_ROTATED,
110 static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
112 bool has_aliasing_ppgtt;
114 bool has_full_48bit_ppgtt;
116 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
117 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
118 has_full_48bit_ppgtt = IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9;
120 if (intel_vgpu_active(dev))
121 has_full_ppgtt = false; /* emulation is too hard */
124 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
125 * execlists, the sole mechanism available to submit work.
127 if (INTEL_INFO(dev)->gen < 9 &&
128 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
131 if (enable_ppgtt == 1)
134 if (enable_ppgtt == 2 && has_full_ppgtt)
137 if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
140 #ifdef CONFIG_INTEL_IOMMU
141 /* Disable ppgtt on SNB if VT-d is on. */
142 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
143 DRM_INFO("Disabling PPGTT because VT-d is on\n");
148 /* Early VLV doesn't have this */
149 if (IS_VALLEYVIEW(dev) && dev->pdev->revision < 0xb) {
150 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
154 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
155 return has_full_48bit_ppgtt ? 3 : 2;
157 return has_aliasing_ppgtt ? 1 : 0;
160 static int ppgtt_bind_vma(struct i915_vma *vma,
161 enum i915_cache_level cache_level,
166 /* Currently applicable only to VLV */
168 pte_flags |= PTE_READ_ONLY;
170 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
171 cache_level, pte_flags);
176 static void ppgtt_unbind_vma(struct i915_vma *vma)
178 vma->vm->clear_range(vma->vm,
184 static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
185 enum i915_cache_level level,
188 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
192 case I915_CACHE_NONE:
193 pte |= PPAT_UNCACHED_INDEX;
196 pte |= PPAT_DISPLAY_ELLC_INDEX;
199 pte |= PPAT_CACHED_INDEX;
206 static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
207 const enum i915_cache_level level)
209 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
211 if (level != I915_CACHE_NONE)
212 pde |= PPAT_CACHED_PDE_INDEX;
214 pde |= PPAT_UNCACHED_INDEX;
218 #define gen8_pdpe_encode gen8_pde_encode
219 #define gen8_pml4e_encode gen8_pde_encode
221 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
222 enum i915_cache_level level,
223 bool valid, u32 unused)
225 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
226 pte |= GEN6_PTE_ADDR_ENCODE(addr);
229 case I915_CACHE_L3_LLC:
231 pte |= GEN6_PTE_CACHE_LLC;
233 case I915_CACHE_NONE:
234 pte |= GEN6_PTE_UNCACHED;
243 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
244 enum i915_cache_level level,
245 bool valid, u32 unused)
247 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
248 pte |= GEN6_PTE_ADDR_ENCODE(addr);
251 case I915_CACHE_L3_LLC:
252 pte |= GEN7_PTE_CACHE_L3_LLC;
255 pte |= GEN6_PTE_CACHE_LLC;
257 case I915_CACHE_NONE:
258 pte |= GEN6_PTE_UNCACHED;
267 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
268 enum i915_cache_level level,
269 bool valid, u32 flags)
271 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
272 pte |= GEN6_PTE_ADDR_ENCODE(addr);
274 if (!(flags & PTE_READ_ONLY))
275 pte |= BYT_PTE_WRITEABLE;
277 if (level != I915_CACHE_NONE)
278 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
283 static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
284 enum i915_cache_level level,
285 bool valid, u32 unused)
287 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
288 pte |= HSW_PTE_ADDR_ENCODE(addr);
290 if (level != I915_CACHE_NONE)
291 pte |= HSW_WB_LLC_AGE3;
296 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
297 enum i915_cache_level level,
298 bool valid, u32 unused)
300 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
301 pte |= HSW_PTE_ADDR_ENCODE(addr);
304 case I915_CACHE_NONE:
307 pte |= HSW_WT_ELLC_LLC_AGE3;
310 pte |= HSW_WB_ELLC_LLC_AGE3;
317 static int __setup_page_dma(struct drm_device *dev,
318 struct i915_page_dma *p, gfp_t flags)
320 struct device *device = &dev->pdev->dev;
322 p->page = alloc_page(flags);
326 p->daddr = dma_map_page(device,
327 p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
329 if (dma_mapping_error(device, p->daddr)) {
330 __free_page(p->page);
337 static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
339 return __setup_page_dma(dev, p, GFP_KERNEL);
342 static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
344 if (WARN_ON(!p->page))
347 dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
348 __free_page(p->page);
349 memset(p, 0, sizeof(*p));
352 static void *kmap_page_dma(struct i915_page_dma *p)
354 return kmap_atomic(p->page);
357 /* We use the flushing unmap only with ppgtt structures:
358 * page directories, page tables and scratch pages.
360 static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
362 /* There are only few exceptions for gen >=6. chv and bxt.
363 * And we are not sure about the latter so play safe for now.
365 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
366 drm_clflush_virt_range(vaddr, PAGE_SIZE);
368 kunmap_atomic(vaddr);
371 #define kmap_px(px) kmap_page_dma(px_base(px))
372 #define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
374 #define setup_px(dev, px) setup_page_dma((dev), px_base(px))
375 #define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
376 #define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
377 #define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
379 static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
383 uint64_t * const vaddr = kmap_page_dma(p);
385 for (i = 0; i < 512; i++)
388 kunmap_page_dma(dev, vaddr);
391 static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
392 const uint32_t val32)
398 fill_page_dma(dev, p, v);
401 static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
403 struct i915_page_scratch *sp;
406 sp = kzalloc(sizeof(*sp), GFP_KERNEL);
408 return ERR_PTR(-ENOMEM);
410 ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
416 set_pages_uc(px_page(sp), 1);
421 static void free_scratch_page(struct drm_device *dev,
422 struct i915_page_scratch *sp)
424 set_pages_wb(px_page(sp), 1);
430 static struct i915_page_table *alloc_pt(struct drm_device *dev)
432 struct i915_page_table *pt;
433 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
434 GEN8_PTES : GEN6_PTES;
437 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
439 return ERR_PTR(-ENOMEM);
441 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
447 ret = setup_px(dev, pt);
454 kfree(pt->used_ptes);
461 static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
464 kfree(pt->used_ptes);
468 static void gen8_initialize_pt(struct i915_address_space *vm,
469 struct i915_page_table *pt)
471 gen8_pte_t scratch_pte;
473 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
474 I915_CACHE_LLC, true);
476 fill_px(vm->dev, pt, scratch_pte);
479 static void gen6_initialize_pt(struct i915_address_space *vm,
480 struct i915_page_table *pt)
482 gen6_pte_t scratch_pte;
484 WARN_ON(px_dma(vm->scratch_page) == 0);
486 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
487 I915_CACHE_LLC, true, 0);
489 fill32_px(vm->dev, pt, scratch_pte);
492 static struct i915_page_directory *alloc_pd(struct drm_device *dev)
494 struct i915_page_directory *pd;
497 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
499 return ERR_PTR(-ENOMEM);
501 pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
502 sizeof(*pd->used_pdes), GFP_KERNEL);
506 ret = setup_px(dev, pd);
513 kfree(pd->used_pdes);
520 static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
524 kfree(pd->used_pdes);
529 static void gen8_initialize_pd(struct i915_address_space *vm,
530 struct i915_page_directory *pd)
532 gen8_pde_t scratch_pde;
534 scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
536 fill_px(vm->dev, pd, scratch_pde);
539 static int __pdp_init(struct drm_device *dev,
540 struct i915_page_directory_pointer *pdp)
542 size_t pdpes = I915_PDPES_PER_PDP(dev);
544 pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
545 sizeof(unsigned long),
547 if (!pdp->used_pdpes)
550 pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
552 if (!pdp->page_directory) {
553 kfree(pdp->used_pdpes);
554 /* the PDP might be the statically allocated top level. Keep it
555 * as clean as possible */
556 pdp->used_pdpes = NULL;
563 static void __pdp_fini(struct i915_page_directory_pointer *pdp)
565 kfree(pdp->used_pdpes);
566 kfree(pdp->page_directory);
567 pdp->page_directory = NULL;
571 i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
573 struct i915_page_directory_pointer *pdp;
576 WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
578 pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
580 return ERR_PTR(-ENOMEM);
582 ret = __pdp_init(dev, pdp);
586 ret = setup_px(dev, pdp);
600 static void free_pdp(struct drm_device *dev,
601 struct i915_page_directory_pointer *pdp)
604 if (USES_FULL_48BIT_PPGTT(dev)) {
605 cleanup_px(dev, pdp);
610 static void gen8_initialize_pdp(struct i915_address_space *vm,
611 struct i915_page_directory_pointer *pdp)
613 gen8_ppgtt_pdpe_t scratch_pdpe;
615 scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
617 fill_px(vm->dev, pdp, scratch_pdpe);
620 static void gen8_initialize_pml4(struct i915_address_space *vm,
621 struct i915_pml4 *pml4)
623 gen8_ppgtt_pml4e_t scratch_pml4e;
625 scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
628 fill_px(vm->dev, pml4, scratch_pml4e);
632 gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
633 struct i915_page_directory_pointer *pdp,
634 struct i915_page_directory *pd,
637 gen8_ppgtt_pdpe_t *page_directorypo;
639 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
642 page_directorypo = kmap_px(pdp);
643 page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
644 kunmap_px(ppgtt, page_directorypo);
648 gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
649 struct i915_pml4 *pml4,
650 struct i915_page_directory_pointer *pdp,
653 gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
655 WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
656 pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
657 kunmap_px(ppgtt, pagemap);
660 /* Broadwell Page Directory Pointer Descriptors */
661 static int gen8_write_pdp(struct drm_i915_gem_request *req,
665 struct intel_engine_cs *engine = req->engine;
670 ret = intel_ring_begin(req, 6);
674 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
675 intel_ring_emit_reg(engine, GEN8_RING_PDP_UDW(engine, entry));
676 intel_ring_emit(engine, upper_32_bits(addr));
677 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
678 intel_ring_emit_reg(engine, GEN8_RING_PDP_LDW(engine, entry));
679 intel_ring_emit(engine, lower_32_bits(addr));
680 intel_ring_advance(engine);
685 static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
686 struct drm_i915_gem_request *req)
690 for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
691 const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
693 ret = gen8_write_pdp(req, i, pd_daddr);
701 static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
702 struct drm_i915_gem_request *req)
704 return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
707 static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
708 struct i915_page_directory_pointer *pdp,
711 gen8_pte_t scratch_pte)
713 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
714 gen8_pte_t *pt_vaddr;
715 unsigned pdpe = gen8_pdpe_index(start);
716 unsigned pde = gen8_pde_index(start);
717 unsigned pte = gen8_pte_index(start);
718 unsigned num_entries = length >> PAGE_SHIFT;
719 unsigned last_pte, i;
724 while (num_entries) {
725 struct i915_page_directory *pd;
726 struct i915_page_table *pt;
728 if (WARN_ON(!pdp->page_directory[pdpe]))
731 pd = pdp->page_directory[pdpe];
733 if (WARN_ON(!pd->page_table[pde]))
736 pt = pd->page_table[pde];
738 if (WARN_ON(!px_page(pt)))
741 last_pte = pte + num_entries;
742 if (last_pte > GEN8_PTES)
743 last_pte = GEN8_PTES;
745 pt_vaddr = kmap_px(pt);
747 for (i = pte; i < last_pte; i++) {
748 pt_vaddr[i] = scratch_pte;
752 kunmap_px(ppgtt, pt_vaddr);
755 if (++pde == I915_PDES) {
756 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
763 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
768 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
769 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
770 I915_CACHE_LLC, use_scratch);
772 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
773 gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length,
777 struct i915_page_directory_pointer *pdp;
779 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
780 gen8_ppgtt_clear_pte_range(vm, pdp, start, length,
787 gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
788 struct i915_page_directory_pointer *pdp,
789 struct sg_page_iter *sg_iter,
791 enum i915_cache_level cache_level)
793 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
794 gen8_pte_t *pt_vaddr;
795 unsigned pdpe = gen8_pdpe_index(start);
796 unsigned pde = gen8_pde_index(start);
797 unsigned pte = gen8_pte_index(start);
801 while (__sg_page_iter_next(sg_iter)) {
802 if (pt_vaddr == NULL) {
803 struct i915_page_directory *pd = pdp->page_directory[pdpe];
804 struct i915_page_table *pt = pd->page_table[pde];
805 pt_vaddr = kmap_px(pt);
809 gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
811 if (++pte == GEN8_PTES) {
812 kunmap_px(ppgtt, pt_vaddr);
814 if (++pde == I915_PDES) {
815 if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
824 kunmap_px(ppgtt, pt_vaddr);
827 static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
828 struct sg_table *pages,
830 enum i915_cache_level cache_level,
833 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
834 struct sg_page_iter sg_iter;
836 __sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
838 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
839 gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
842 struct i915_page_directory_pointer *pdp;
844 uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
846 gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, pml4e) {
847 gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
853 static void gen8_free_page_tables(struct drm_device *dev,
854 struct i915_page_directory *pd)
861 for_each_set_bit(i, pd->used_pdes, I915_PDES) {
862 if (WARN_ON(!pd->page_table[i]))
865 free_pt(dev, pd->page_table[i]);
866 pd->page_table[i] = NULL;
870 static int gen8_init_scratch(struct i915_address_space *vm)
872 struct drm_device *dev = vm->dev;
874 vm->scratch_page = alloc_scratch_page(dev);
875 if (IS_ERR(vm->scratch_page))
876 return PTR_ERR(vm->scratch_page);
878 vm->scratch_pt = alloc_pt(dev);
879 if (IS_ERR(vm->scratch_pt)) {
880 free_scratch_page(dev, vm->scratch_page);
881 return PTR_ERR(vm->scratch_pt);
884 vm->scratch_pd = alloc_pd(dev);
885 if (IS_ERR(vm->scratch_pd)) {
886 free_pt(dev, vm->scratch_pt);
887 free_scratch_page(dev, vm->scratch_page);
888 return PTR_ERR(vm->scratch_pd);
891 if (USES_FULL_48BIT_PPGTT(dev)) {
892 vm->scratch_pdp = alloc_pdp(dev);
893 if (IS_ERR(vm->scratch_pdp)) {
894 free_pd(dev, vm->scratch_pd);
895 free_pt(dev, vm->scratch_pt);
896 free_scratch_page(dev, vm->scratch_page);
897 return PTR_ERR(vm->scratch_pdp);
901 gen8_initialize_pt(vm, vm->scratch_pt);
902 gen8_initialize_pd(vm, vm->scratch_pd);
903 if (USES_FULL_48BIT_PPGTT(dev))
904 gen8_initialize_pdp(vm, vm->scratch_pdp);
909 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
911 enum vgt_g2v_type msg;
912 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
915 if (USES_FULL_48BIT_PPGTT(dev_priv)) {
916 u64 daddr = px_dma(&ppgtt->pml4);
918 I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
919 I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
921 msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
922 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
924 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
925 u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
927 I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
928 I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
931 msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
932 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
935 I915_WRITE(vgtif_reg(g2v_notify), msg);
940 static void gen8_free_scratch(struct i915_address_space *vm)
942 struct drm_device *dev = vm->dev;
944 if (USES_FULL_48BIT_PPGTT(dev))
945 free_pdp(dev, vm->scratch_pdp);
946 free_pd(dev, vm->scratch_pd);
947 free_pt(dev, vm->scratch_pt);
948 free_scratch_page(dev, vm->scratch_page);
951 static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
952 struct i915_page_directory_pointer *pdp)
956 for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
957 if (WARN_ON(!pdp->page_directory[i]))
960 gen8_free_page_tables(dev, pdp->page_directory[i]);
961 free_pd(dev, pdp->page_directory[i]);
967 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
971 for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
972 if (WARN_ON(!ppgtt->pml4.pdps[i]))
975 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
978 cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
981 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
983 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
985 if (intel_vgpu_active(vm->dev))
986 gen8_ppgtt_notify_vgt(ppgtt, false);
988 if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
989 gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
991 gen8_ppgtt_cleanup_4lvl(ppgtt);
993 gen8_free_scratch(vm);
997 * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
998 * @vm: Master vm structure.
999 * @pd: Page directory for this address range.
1000 * @start: Starting virtual address to begin allocations.
1001 * @length: Size of the allocations.
1002 * @new_pts: Bitmap set by function with new allocations. Likely used by the
1003 * caller to free on error.
1005 * Allocate the required number of page tables. Extremely similar to
1006 * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
1007 * the page directory boundary (instead of the page directory pointer). That
1008 * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
1009 * possible, and likely that the caller will need to use multiple calls of this
1010 * function to achieve the appropriate allocation.
1012 * Return: 0 if success; negative error code otherwise.
1014 static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
1015 struct i915_page_directory *pd,
1018 unsigned long *new_pts)
1020 struct drm_device *dev = vm->dev;
1021 struct i915_page_table *pt;
1024 gen8_for_each_pde(pt, pd, start, length, pde) {
1025 /* Don't reallocate page tables */
1026 if (test_bit(pde, pd->used_pdes)) {
1027 /* Scratch is never allocated this way */
1028 WARN_ON(pt == vm->scratch_pt);
1036 gen8_initialize_pt(vm, pt);
1037 pd->page_table[pde] = pt;
1038 __set_bit(pde, new_pts);
1039 trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
1045 for_each_set_bit(pde, new_pts, I915_PDES)
1046 free_pt(dev, pd->page_table[pde]);
1052 * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
1053 * @vm: Master vm structure.
1054 * @pdp: Page directory pointer for this address range.
1055 * @start: Starting virtual address to begin allocations.
1056 * @length: Size of the allocations.
1057 * @new_pds: Bitmap set by function with new allocations. Likely used by the
1058 * caller to free on error.
1060 * Allocate the required number of page directories starting at the pde index of
1061 * @start, and ending at the pde index @start + @length. This function will skip
1062 * over already allocated page directories within the range, and only allocate
1063 * new ones, setting the appropriate pointer within the pdp as well as the
1064 * correct position in the bitmap @new_pds.
1066 * The function will only allocate the pages within the range for a give page
1067 * directory pointer. In other words, if @start + @length straddles a virtually
1068 * addressed PDP boundary (512GB for 4k pages), there will be more allocations
1069 * required by the caller, This is not currently possible, and the BUG in the
1070 * code will prevent it.
1072 * Return: 0 if success; negative error code otherwise.
1075 gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
1076 struct i915_page_directory_pointer *pdp,
1079 unsigned long *new_pds)
1081 struct drm_device *dev = vm->dev;
1082 struct i915_page_directory *pd;
1084 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1086 WARN_ON(!bitmap_empty(new_pds, pdpes));
1088 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1089 if (test_bit(pdpe, pdp->used_pdpes))
1096 gen8_initialize_pd(vm, pd);
1097 pdp->page_directory[pdpe] = pd;
1098 __set_bit(pdpe, new_pds);
1099 trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
1105 for_each_set_bit(pdpe, new_pds, pdpes)
1106 free_pd(dev, pdp->page_directory[pdpe]);
1112 * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
1113 * @vm: Master vm structure.
1114 * @pml4: Page map level 4 for this address range.
1115 * @start: Starting virtual address to begin allocations.
1116 * @length: Size of the allocations.
1117 * @new_pdps: Bitmap set by function with new allocations. Likely used by the
1118 * caller to free on error.
1120 * Allocate the required number of page directory pointers. Extremely similar to
1121 * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
1122 * The main difference is here we are limited by the pml4 boundary (instead of
1123 * the page directory pointer).
1125 * Return: 0 if success; negative error code otherwise.
1128 gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
1129 struct i915_pml4 *pml4,
1132 unsigned long *new_pdps)
1134 struct drm_device *dev = vm->dev;
1135 struct i915_page_directory_pointer *pdp;
1138 WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
1140 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1141 if (!test_bit(pml4e, pml4->used_pml4es)) {
1142 pdp = alloc_pdp(dev);
1146 gen8_initialize_pdp(vm, pdp);
1147 pml4->pdps[pml4e] = pdp;
1148 __set_bit(pml4e, new_pdps);
1149 trace_i915_page_directory_pointer_entry_alloc(vm,
1159 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1160 free_pdp(dev, pml4->pdps[pml4e]);
1166 free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
1172 /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
1173 * of these are based on the number of PDPEs in the system.
1176 int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
1177 unsigned long **new_pts,
1183 pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
1187 pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
1198 free_gen8_temp_bitmaps(pds, pts);
1202 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
1203 * the page table structures, we mark them dirty so that
1204 * context switching/execlist queuing code takes extra steps
1205 * to ensure that tlbs are flushed.
1207 static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1209 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1212 static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
1213 struct i915_page_directory_pointer *pdp,
1217 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1218 unsigned long *new_page_dirs, *new_page_tables;
1219 struct drm_device *dev = vm->dev;
1220 struct i915_page_directory *pd;
1221 const uint64_t orig_start = start;
1222 const uint64_t orig_length = length;
1224 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1227 /* Wrap is never okay since we can only represent 48b, and we don't
1228 * actually use the other side of the canonical address space.
1230 if (WARN_ON(start + length < start))
1233 if (WARN_ON(start + length > vm->total))
1236 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1240 /* Do the allocations first so we can easily bail out */
1241 ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
1244 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1248 /* For every page directory referenced, allocate page tables */
1249 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1250 ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
1251 new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
1257 length = orig_length;
1259 /* Allocations have completed successfully, so set the bitmaps, and do
1261 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1262 gen8_pde_t *const page_directory = kmap_px(pd);
1263 struct i915_page_table *pt;
1264 uint64_t pd_len = length;
1265 uint64_t pd_start = start;
1268 /* Every pd should be allocated, we just did that above. */
1271 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1272 /* Same reasoning as pd */
1275 WARN_ON(!gen8_pte_count(pd_start, pd_len));
1277 /* Set our used ptes within the page table */
1278 bitmap_set(pt->used_ptes,
1279 gen8_pte_index(pd_start),
1280 gen8_pte_count(pd_start, pd_len));
1282 /* Our pde is now pointing to the pagetable, pt */
1283 __set_bit(pde, pd->used_pdes);
1285 /* Map the PDE to the page table */
1286 page_directory[pde] = gen8_pde_encode(px_dma(pt),
1288 trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
1289 gen8_pte_index(start),
1290 gen8_pte_count(start, length),
1293 /* NB: We haven't yet mapped ptes to pages. At this
1294 * point we're still relying on insert_entries() */
1297 kunmap_px(ppgtt, page_directory);
1298 __set_bit(pdpe, pdp->used_pdpes);
1299 gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
1302 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1303 mark_tlbs_dirty(ppgtt);
1310 for_each_set_bit(temp, new_page_tables + pdpe *
1311 BITS_TO_LONGS(I915_PDES), I915_PDES)
1312 free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
1315 for_each_set_bit(pdpe, new_page_dirs, pdpes)
1316 free_pd(dev, pdp->page_directory[pdpe]);
1318 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1319 mark_tlbs_dirty(ppgtt);
1323 static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
1324 struct i915_pml4 *pml4,
1328 DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
1329 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1330 struct i915_page_directory_pointer *pdp;
1334 /* Do the pml4 allocations first, so we don't need to track the newly
1335 * allocated tables below the pdp */
1336 bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
1338 /* The pagedirectory and pagetable allocations are done in the shared 3
1339 * and 4 level code. Just allocate the pdps.
1341 ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
1346 WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
1347 "The allocation has spanned more than 512GB. "
1348 "It is highly likely this is incorrect.");
1350 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1353 ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
1357 gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
1360 bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
1361 GEN8_PML4ES_PER_PML4);
1366 for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
1367 gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
1372 static int gen8_alloc_va_range(struct i915_address_space *vm,
1373 uint64_t start, uint64_t length)
1375 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1377 if (USES_FULL_48BIT_PPGTT(vm->dev))
1378 return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
1380 return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
1383 static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
1384 uint64_t start, uint64_t length,
1385 gen8_pte_t scratch_pte,
1388 struct i915_page_directory *pd;
1391 gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
1392 struct i915_page_table *pt;
1393 uint64_t pd_len = length;
1394 uint64_t pd_start = start;
1397 if (!test_bit(pdpe, pdp->used_pdpes))
1400 seq_printf(m, "\tPDPE #%d\n", pdpe);
1401 gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
1403 gen8_pte_t *pt_vaddr;
1405 if (!test_bit(pde, pd->used_pdes))
1408 pt_vaddr = kmap_px(pt);
1409 for (pte = 0; pte < GEN8_PTES; pte += 4) {
1411 (pdpe << GEN8_PDPE_SHIFT) |
1412 (pde << GEN8_PDE_SHIFT) |
1413 (pte << GEN8_PTE_SHIFT);
1417 for (i = 0; i < 4; i++)
1418 if (pt_vaddr[pte + i] != scratch_pte)
1423 seq_printf(m, "\t\t0x%lx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
1424 for (i = 0; i < 4; i++) {
1425 if (pt_vaddr[pte + i] != scratch_pte)
1426 seq_printf(m, " %lx", pt_vaddr[pte + i]);
1428 seq_puts(m, " SCRATCH ");
1432 /* don't use kunmap_px, it could trigger
1433 * an unnecessary flush.
1435 kunmap_atomic(pt_vaddr);
1440 static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1442 struct i915_address_space *vm = &ppgtt->base;
1443 uint64_t start = ppgtt->base.start;
1444 uint64_t length = ppgtt->base.total;
1445 gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
1446 I915_CACHE_LLC, true);
1448 if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
1449 gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
1452 struct i915_pml4 *pml4 = &ppgtt->pml4;
1453 struct i915_page_directory_pointer *pdp;
1455 gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
1456 if (!test_bit(pml4e, pml4->used_pml4es))
1459 seq_printf(m, " PML4E #%lu\n", pml4e);
1460 gen8_dump_pdp(pdp, start, length, scratch_pte, m);
1465 static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
1467 unsigned long *new_page_dirs, *new_page_tables;
1468 uint32_t pdpes = I915_PDPES_PER_PDP(dev);
1471 /* We allocate temp bitmap for page tables for no gain
1472 * but as this is for init only, lets keep the things simple
1474 ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
1478 /* Allocate for all pdps regardless of how the ppgtt
1481 ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
1485 *ppgtt->pdp.used_pdpes = *new_page_dirs;
1487 free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
1493 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1494 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1495 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1499 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
1503 ret = gen8_init_scratch(&ppgtt->base);
1507 ppgtt->base.start = 0;
1508 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
1509 ppgtt->base.allocate_va_range = gen8_alloc_va_range;
1510 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
1511 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
1512 ppgtt->base.unbind_vma = ppgtt_unbind_vma;
1513 ppgtt->base.bind_vma = ppgtt_bind_vma;
1514 ppgtt->debug_dump = gen8_dump_ppgtt;
1516 if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
1517 ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
1521 gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
1523 ppgtt->base.total = 1ULL << 48;
1524 ppgtt->switch_mm = gen8_48b_mm_switch;
1526 ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
1530 ppgtt->base.total = 1ULL << 32;
1531 ppgtt->switch_mm = gen8_legacy_mm_switch;
1532 trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
1536 if (intel_vgpu_active(ppgtt->base.dev)) {
1537 ret = gen8_preallocate_top_level_pdps(ppgtt);
1543 if (intel_vgpu_active(ppgtt->base.dev))
1544 gen8_ppgtt_notify_vgt(ppgtt, true);
1549 gen8_free_scratch(&ppgtt->base);
1553 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
1555 struct i915_address_space *vm = &ppgtt->base;
1556 struct i915_page_table *unused;
1557 gen6_pte_t scratch_pte;
1559 uint32_t pte, pde, temp;
1560 uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
1562 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1563 I915_CACHE_LLC, true, 0);
1565 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
1567 gen6_pte_t *pt_vaddr;
1568 const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
1569 pd_entry = readl(ppgtt->pd_addr + pde);
1570 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
1572 if (pd_entry != expected)
1573 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1577 seq_printf(m, "\tPDE: %x\n", pd_entry);
1579 pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
1581 for (pte = 0; pte < GEN6_PTES; pte+=4) {
1583 (pde * PAGE_SIZE * GEN6_PTES) +
1587 for (i = 0; i < 4; i++)
1588 if (pt_vaddr[pte + i] != scratch_pte)
1593 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
1594 for (i = 0; i < 4; i++) {
1595 if (pt_vaddr[pte + i] != scratch_pte)
1596 seq_printf(m, " %08x", pt_vaddr[pte + i]);
1598 seq_puts(m, " SCRATCH ");
1602 kunmap_px(ppgtt, pt_vaddr);
1606 /* Write pde (index) from the page directory @pd to the page table @pt */
1607 static void gen6_write_pde(struct i915_page_directory *pd,
1608 const int pde, struct i915_page_table *pt)
1610 /* Caller needs to make sure the write completes if necessary */
1611 struct i915_hw_ppgtt *ppgtt =
1612 container_of(pd, struct i915_hw_ppgtt, pd);
1615 pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
1616 pd_entry |= GEN6_PDE_VALID;
1618 writel(pd_entry, ppgtt->pd_addr + pde);
1621 /* Write all the page tables found in the ppgtt structure to incrementing page
1623 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
1624 struct i915_page_directory *pd,
1625 uint32_t start, uint32_t length)
1627 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1628 struct i915_page_table *pt;
1631 gen6_for_each_pde(pt, pd, start, length, temp, pde)
1632 gen6_write_pde(pd, pde, pt);
1634 /* Make sure write is complete before other code can use this page
1635 * table. Also require for WC mapped PTEs */
1639 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
1641 BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
1643 return (ppgtt->pd.base.ggtt_offset / 64) << 16;
1646 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
1647 struct drm_i915_gem_request *req)
1649 struct intel_engine_cs *engine = req->engine;
1652 /* NB: TLBs must be flushed and invalidated before a switch */
1653 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1657 ret = intel_ring_begin(req, 6);
1661 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(2));
1662 intel_ring_emit_reg(engine, RING_PP_DIR_DCLV(engine));
1663 intel_ring_emit(engine, PP_DIR_DCLV_2G);
1664 intel_ring_emit_reg(engine, RING_PP_DIR_BASE(engine));
1665 intel_ring_emit(engine, get_pd_offset(ppgtt));
1666 intel_ring_emit(engine, MI_NOOP);
1667 intel_ring_advance(engine);
1672 static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
1673 struct drm_i915_gem_request *req)
1675 struct intel_engine_cs *engine = req->engine;
1676 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
1678 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1679 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
1683 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
1684 struct drm_i915_gem_request *req)
1686 struct intel_engine_cs *engine = req->engine;
1689 /* NB: TLBs must be flushed and invalidated before a switch */
1690 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1694 ret = intel_ring_begin(req, 6);
1698 intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(2));
1699 intel_ring_emit_reg(engine, RING_PP_DIR_DCLV(engine));
1700 intel_ring_emit(engine, PP_DIR_DCLV_2G);
1701 intel_ring_emit_reg(engine, RING_PP_DIR_BASE(engine));
1702 intel_ring_emit(engine, get_pd_offset(ppgtt));
1703 intel_ring_emit(engine, MI_NOOP);
1704 intel_ring_advance(engine);
1706 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1707 if (engine->id != RCS) {
1708 ret = engine->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1716 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1717 struct drm_i915_gem_request *req)
1719 struct intel_engine_cs *engine = req->engine;
1720 struct drm_device *dev = ppgtt->base.dev;
1721 struct drm_i915_private *dev_priv = dev->dev_private;
1724 I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
1725 I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
1727 POSTING_READ(RING_PP_DIR_DCLV(engine));
1732 static void gen8_ppgtt_enable(struct drm_device *dev)
1734 struct drm_i915_private *dev_priv = dev->dev_private;
1735 struct intel_engine_cs *engine;
1737 for_each_engine(engine, dev_priv) {
1738 u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
1739 I915_WRITE(RING_MODE_GEN7(engine),
1740 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
1744 static void gen7_ppgtt_enable(struct drm_device *dev)
1746 struct drm_i915_private *dev_priv = dev->dev_private;
1747 struct intel_engine_cs *engine;
1748 uint32_t ecochk, ecobits;
1750 ecobits = I915_READ(GAC_ECO_BITS);
1751 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1753 ecochk = I915_READ(GAM_ECOCHK);
1754 if (IS_HASWELL(dev)) {
1755 ecochk |= ECOCHK_PPGTT_WB_HSW;
1757 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1758 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1760 I915_WRITE(GAM_ECOCHK, ecochk);
1762 for_each_engine(engine, dev_priv) {
1763 /* GFX_MODE is per-ring on gen7+ */
1764 I915_WRITE(RING_MODE_GEN7(engine),
1765 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1769 static void gen6_ppgtt_enable(struct drm_device *dev)
1771 struct drm_i915_private *dev_priv = dev->dev_private;
1772 uint32_t ecochk, gab_ctl, ecobits;
1774 ecobits = I915_READ(GAC_ECO_BITS);
1775 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1776 ECOBITS_PPGTT_CACHE64B);
1778 gab_ctl = I915_READ(GAB_CTL);
1779 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1781 ecochk = I915_READ(GAM_ECOCHK);
1782 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1784 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1787 /* PPGTT support for Sandybdrige/Gen6 and later */
1788 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1793 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1794 gen6_pte_t *pt_vaddr, scratch_pte;
1795 unsigned first_entry = start >> PAGE_SHIFT;
1796 unsigned num_entries = length >> PAGE_SHIFT;
1797 unsigned act_pt = first_entry / GEN6_PTES;
1798 unsigned first_pte = first_entry % GEN6_PTES;
1799 unsigned last_pte, i;
1801 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
1802 I915_CACHE_LLC, true, 0);
1804 while (num_entries) {
1805 last_pte = first_pte + num_entries;
1806 if (last_pte > GEN6_PTES)
1807 last_pte = GEN6_PTES;
1809 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1811 for (i = first_pte; i < last_pte; i++)
1812 pt_vaddr[i] = scratch_pte;
1814 kunmap_px(ppgtt, pt_vaddr);
1816 num_entries -= last_pte - first_pte;
1822 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1823 struct sg_table *pages,
1825 enum i915_cache_level cache_level, u32 flags)
1827 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1828 gen6_pte_t *pt_vaddr;
1829 unsigned first_entry = start >> PAGE_SHIFT;
1830 unsigned act_pt = first_entry / GEN6_PTES;
1831 unsigned act_pte = first_entry % GEN6_PTES;
1832 struct sg_page_iter sg_iter;
1835 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
1836 if (pt_vaddr == NULL)
1837 pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
1840 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
1841 cache_level, true, flags);
1843 if (++act_pte == GEN6_PTES) {
1844 kunmap_px(ppgtt, pt_vaddr);
1851 kunmap_px(ppgtt, pt_vaddr);
1854 static int gen6_alloc_va_range(struct i915_address_space *vm,
1855 uint64_t start_in, uint64_t length_in)
1857 DECLARE_BITMAP(new_page_tables, I915_PDES);
1858 struct drm_device *dev = vm->dev;
1859 struct drm_i915_private *dev_priv = to_i915(dev);
1860 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1861 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1862 struct i915_page_table *pt;
1863 uint32_t start, length, start_save, length_save;
1867 if (WARN_ON(start_in + length_in > ppgtt->base.total))
1870 start = start_save = start_in;
1871 length = length_save = length_in;
1873 bitmap_zero(new_page_tables, I915_PDES);
1875 /* The allocation is done in two stages so that we can bail out with
1876 * minimal amount of pain. The first stage finds new page tables that
1877 * need allocation. The second stage marks use ptes within the page
1880 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1881 if (pt != vm->scratch_pt) {
1882 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1886 /* We've already allocated a page table */
1887 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1895 gen6_initialize_pt(vm, pt);
1897 ppgtt->pd.page_table[pde] = pt;
1898 __set_bit(pde, new_page_tables);
1899 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
1903 length = length_save;
1905 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1906 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1908 bitmap_zero(tmp_bitmap, GEN6_PTES);
1909 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1910 gen6_pte_count(start, length));
1912 if (__test_and_clear_bit(pde, new_page_tables))
1913 gen6_write_pde(&ppgtt->pd, pde, pt);
1915 trace_i915_page_table_entry_map(vm, pde, pt,
1916 gen6_pte_index(start),
1917 gen6_pte_count(start, length),
1919 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
1923 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1925 /* Make sure write is complete before other code can use this page
1926 * table. Also require for WC mapped PTEs */
1929 mark_tlbs_dirty(ppgtt);
1933 for_each_set_bit(pde, new_page_tables, I915_PDES) {
1934 struct i915_page_table *pt = ppgtt->pd.page_table[pde];
1936 ppgtt->pd.page_table[pde] = vm->scratch_pt;
1937 free_pt(vm->dev, pt);
1940 mark_tlbs_dirty(ppgtt);
1944 static int gen6_init_scratch(struct i915_address_space *vm)
1946 struct drm_device *dev = vm->dev;
1948 vm->scratch_page = alloc_scratch_page(dev);
1949 if (IS_ERR(vm->scratch_page))
1950 return PTR_ERR(vm->scratch_page);
1952 vm->scratch_pt = alloc_pt(dev);
1953 if (IS_ERR(vm->scratch_pt)) {
1954 free_scratch_page(dev, vm->scratch_page);
1955 return PTR_ERR(vm->scratch_pt);
1958 gen6_initialize_pt(vm, vm->scratch_pt);
1963 static void gen6_free_scratch(struct i915_address_space *vm)
1965 struct drm_device *dev = vm->dev;
1967 free_pt(dev, vm->scratch_pt);
1968 free_scratch_page(dev, vm->scratch_page);
1971 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1973 struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
1974 struct i915_page_table *pt;
1977 drm_mm_remove_node(&ppgtt->node);
1979 gen6_for_all_pdes(pt, ppgtt, pde) {
1980 if (pt != vm->scratch_pt)
1981 free_pt(ppgtt->base.dev, pt);
1984 gen6_free_scratch(vm);
1987 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
1989 struct i915_address_space *vm = &ppgtt->base;
1990 struct drm_device *dev = ppgtt->base.dev;
1991 struct drm_i915_private *dev_priv = to_i915(dev);
1992 struct i915_ggtt *ggtt = &dev_priv->ggtt;
1993 bool retried = false;
1996 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1997 * allocator works in address space sizes, so it's multiplied by page
1998 * size. We allocate at the top of the GTT to avoid fragmentation.
2000 BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
2002 ret = gen6_init_scratch(vm);
2007 ret = drm_mm_insert_node_in_range_generic(&ggtt->base.mm,
2008 &ppgtt->node, GEN6_PD_SIZE,
2010 0, ggtt->base.total,
2012 if (ret == -ENOSPC && !retried) {
2013 ret = i915_gem_evict_something(dev, &ggtt->base,
2014 GEN6_PD_SIZE, GEN6_PD_ALIGN,
2016 0, ggtt->base.total,
2029 if (ppgtt->node.start < ggtt->mappable_end)
2030 DRM_DEBUG("Forced to use aperture for PDEs\n");
2035 gen6_free_scratch(vm);
2039 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
2041 return gen6_ppgtt_allocate_page_directories(ppgtt);
2044 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
2045 uint64_t start, uint64_t length)
2047 struct i915_page_table *unused;
2050 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
2051 ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
2054 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
2056 struct drm_device *dev = ppgtt->base.dev;
2057 struct drm_i915_private *dev_priv = to_i915(dev);
2058 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2061 ppgtt->base.pte_encode = ggtt->base.pte_encode;
2063 ppgtt->switch_mm = gen6_mm_switch;
2064 } else if (IS_HASWELL(dev)) {
2065 ppgtt->switch_mm = hsw_mm_switch;
2066 } else if (IS_GEN7(dev)) {
2067 ppgtt->switch_mm = gen7_mm_switch;
2071 if (intel_vgpu_active(dev))
2072 ppgtt->switch_mm = vgpu_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->dev;
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 = dev->dev_private;
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 int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
2149 struct drm_i915_private *dev_priv = dev->dev_private;
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 int i915_ppgtt_init_ring(struct drm_i915_gem_request *req)
2188 struct drm_i915_private *dev_priv = req->i915;
2189 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2191 if (i915.enable_execlists)
2197 return ppgtt->switch_mm(ppgtt, req);
2200 struct i915_hw_ppgtt *
2201 i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
2203 struct i915_hw_ppgtt *ppgtt;
2206 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2208 return ERR_PTR(-ENOMEM);
2210 ret = i915_ppgtt_init(dev, ppgtt);
2213 return ERR_PTR(ret);
2216 ppgtt->file_priv = fpriv;
2218 trace_i915_ppgtt_create(&ppgtt->base);
2223 void i915_ppgtt_release(struct kref *kref)
2225 struct i915_hw_ppgtt *ppgtt =
2226 container_of(kref, struct i915_hw_ppgtt, ref);
2228 trace_i915_ppgtt_release(&ppgtt->base);
2230 /* vmas should already be unbound */
2231 WARN_ON(!list_empty(&ppgtt->base.active_list));
2232 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
2234 list_del(&ppgtt->base.global_link);
2235 drm_mm_takedown(&ppgtt->base.mm);
2237 ppgtt->base.cleanup(&ppgtt->base);
2241 extern int intel_iommu_gfx_mapped;
2242 /* Certain Gen5 chipsets require require idling the GPU before
2243 * unmapping anything from the GTT when VT-d is enabled.
2245 static bool needs_idle_maps(struct drm_device *dev)
2247 #ifdef CONFIG_INTEL_IOMMU
2248 /* Query intel_iommu to see if we need the workaround. Presumably that
2251 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
2257 static bool do_idling(struct drm_i915_private *dev_priv)
2259 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2260 bool ret = dev_priv->mm.interruptible;
2262 if (unlikely(ggtt->do_idle_maps)) {
2263 dev_priv->mm.interruptible = false;
2264 if (i915_gpu_idle(dev_priv->dev)) {
2265 DRM_ERROR("Couldn't idle GPU\n");
2266 /* Wait a bit, in hopes it avoids the hang */
2274 static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
2276 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2278 if (unlikely(ggtt->do_idle_maps))
2279 dev_priv->mm.interruptible = interruptible;
2282 void i915_check_and_clear_faults(struct drm_device *dev)
2284 struct drm_i915_private *dev_priv = dev->dev_private;
2285 struct intel_engine_cs *engine;
2287 if (INTEL_INFO(dev)->gen < 6)
2290 for_each_engine(engine, dev_priv) {
2292 fault_reg = I915_READ(RING_FAULT_REG(engine));
2293 if (fault_reg & RING_FAULT_VALID) {
2295 DRM_DEBUG_DRIVER("Unexpected fault\n"
2297 "\tAddress space: %s\n"
2300 fault_reg & PAGE_MASK,
2301 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
2302 RING_FAULT_SRCID(fault_reg),
2303 RING_FAULT_FAULT_TYPE(fault_reg));
2305 I915_WRITE(RING_FAULT_REG(engine),
2306 fault_reg & ~RING_FAULT_VALID);
2309 POSTING_READ(RING_FAULT_REG(&dev_priv->engine[RCS]));
2312 static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
2314 if (INTEL_INFO(dev_priv)->gen < 6) {
2315 intel_gtt_chipset_flush();
2317 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2318 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2322 void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
2324 struct drm_i915_private *dev_priv = to_i915(dev);
2325 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2327 /* Don't bother messing with faults pre GEN6 as we have little
2328 * documentation supporting that it's a good idea.
2330 if (INTEL_INFO(dev)->gen < 6)
2333 i915_check_and_clear_faults(dev);
2335 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
2338 i915_ggtt_flush(dev_priv);
2341 int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
2343 if (!dma_map_sg(&obj->base.dev->pdev->dev,
2344 obj->pages->sgl, obj->pages->nents,
2345 PCI_DMA_BIDIRECTIONAL))
2351 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
2356 iowrite32((u32)pte, addr);
2357 iowrite32(pte >> 32, addr + 4);
2361 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
2362 struct sg_table *st,
2364 enum i915_cache_level level, u32 unused)
2366 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2367 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2368 unsigned first_entry = start >> PAGE_SHIFT;
2369 gen8_pte_t __iomem *gtt_entries =
2370 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2372 struct sg_page_iter sg_iter;
2373 dma_addr_t addr = 0; /* shut up gcc */
2376 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2378 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2379 addr = sg_dma_address(sg_iter.sg) +
2380 (sg_iter.sg_pgoffset << PAGE_SHIFT);
2381 gen8_set_pte(>t_entries[i],
2382 gen8_pte_encode(addr, level, true));
2387 * XXX: This serves as a posting read to make sure that the PTE has
2388 * actually been updated. There is some concern that even though
2389 * registers and PTEs are within the same BAR that they are potentially
2390 * of NUMA access patterns. Therefore, even with the way we assume
2391 * hardware should work, we must keep this posting read for paranoia.
2394 WARN_ON(readq(>t_entries[i-1])
2395 != gen8_pte_encode(addr, level, true));
2397 /* This next bit makes the above posting read even more important. We
2398 * want to flush the TLBs only after we're certain all the PTE updates
2401 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2402 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2404 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2407 struct insert_entries {
2408 struct i915_address_space *vm;
2409 struct sg_table *st;
2411 enum i915_cache_level level;
2415 static int gen8_ggtt_insert_entries__cb(void *_arg)
2417 struct insert_entries *arg = _arg;
2418 gen8_ggtt_insert_entries(arg->vm, arg->st,
2419 arg->start, arg->level, arg->flags);
2423 static void gen8_ggtt_insert_entries__BKL(struct i915_address_space *vm,
2424 struct sg_table *st,
2426 enum i915_cache_level level,
2429 struct insert_entries arg = { vm, st, start, level, flags };
2430 #ifndef __DragonFly__
2431 stop_machine(gen8_ggtt_insert_entries__cb, &arg, NULL);
2433 /* XXX: is this enough ?
2434 * See Linux commit 5bab6f60cb4d1417ad7c599166bcfec87529c1a2 */
2436 gen8_ggtt_insert_entries__cb(&arg);
2442 * Binds an object into the global gtt with the specified cache level. The object
2443 * will be accessible to the GPU via commands whose operands reference offsets
2444 * within the global GTT as well as accessible by the GPU through the GMADR
2445 * mapped BAR (dev_priv->mm.gtt->gtt).
2447 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
2448 struct sg_table *st,
2450 enum i915_cache_level level, u32 flags)
2452 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2453 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2454 unsigned first_entry = start >> PAGE_SHIFT;
2455 gen6_pte_t __iomem *gtt_entries =
2456 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2458 struct sg_page_iter sg_iter;
2459 dma_addr_t addr = 0;
2462 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2464 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
2465 addr = sg_page_iter_dma_address(&sg_iter);
2466 iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]);
2470 /* XXX: This serves as a posting read to make sure that the PTE has
2471 * actually been updated. There is some concern that even though
2472 * registers and PTEs are within the same BAR that they are potentially
2473 * of NUMA access patterns. Therefore, even with the way we assume
2474 * hardware should work, we must keep this posting read for paranoia.
2477 unsigned long gtt = readl(>t_entries[i-1]);
2478 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
2481 /* This next bit makes the above posting read even more important. We
2482 * want to flush the TLBs only after we're certain all the PTE updates
2485 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
2486 POSTING_READ(GFX_FLSH_CNTL_GEN6);
2488 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2491 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
2496 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2497 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2498 unsigned first_entry = start >> PAGE_SHIFT;
2499 unsigned num_entries = length >> PAGE_SHIFT;
2500 gen8_pte_t scratch_pte, __iomem *gtt_base =
2501 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
2502 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2506 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2508 if (WARN(num_entries > max_entries,
2509 "First entry = %d; Num entries = %d (max=%d)\n",
2510 first_entry, num_entries, max_entries))
2511 num_entries = max_entries;
2513 scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
2516 for (i = 0; i < num_entries; i++)
2517 gen8_set_pte(>t_base[i], scratch_pte);
2520 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2523 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
2528 struct drm_i915_private *dev_priv = to_i915(vm->dev);
2529 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2530 unsigned first_entry = start >> PAGE_SHIFT;
2531 unsigned num_entries = length >> PAGE_SHIFT;
2532 gen6_pte_t scratch_pte, __iomem *gtt_base =
2533 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
2534 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
2538 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2540 if (WARN(num_entries > max_entries,
2541 "First entry = %d; Num entries = %d (max=%d)\n",
2542 first_entry, num_entries, max_entries))
2543 num_entries = max_entries;
2545 scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
2546 I915_CACHE_LLC, use_scratch, 0);
2548 for (i = 0; i < num_entries; i++)
2549 iowrite32(scratch_pte, >t_base[i]);
2552 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2555 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
2556 struct sg_table *pages,
2558 enum i915_cache_level cache_level, u32 unused)
2560 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2561 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
2562 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
2565 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2567 intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
2569 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2573 static void i915_ggtt_clear_range(struct i915_address_space *vm,
2578 struct drm_i915_private *dev_priv = vm->dev->dev_private;
2579 unsigned first_entry = start >> PAGE_SHIFT;
2580 unsigned num_entries = length >> PAGE_SHIFT;
2583 rpm_atomic_seq = assert_rpm_atomic_begin(dev_priv);
2585 intel_gtt_clear_range(first_entry, num_entries);
2587 assert_rpm_atomic_end(dev_priv, rpm_atomic_seq);
2590 static int ggtt_bind_vma(struct i915_vma *vma,
2591 enum i915_cache_level cache_level,
2594 struct drm_i915_gem_object *obj = vma->obj;
2598 ret = i915_get_ggtt_vma_pages(vma);
2602 /* Currently applicable only to VLV */
2604 pte_flags |= PTE_READ_ONLY;
2606 vma->vm->insert_entries(vma->vm, vma->ggtt_view.pages,
2608 cache_level, pte_flags);
2611 * Without aliasing PPGTT there's no difference between
2612 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2613 * upgrade to both bound if we bind either to avoid double-binding.
2615 vma->bound |= GLOBAL_BIND | LOCAL_BIND;
2620 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
2621 enum i915_cache_level cache_level,
2627 ret = i915_get_ggtt_vma_pages(vma);
2631 /* Currently applicable only to VLV */
2633 if (vma->obj->gt_ro)
2634 pte_flags |= PTE_READ_ONLY;
2637 if (flags & GLOBAL_BIND) {
2638 vma->vm->insert_entries(vma->vm,
2639 vma->ggtt_view.pages,
2641 cache_level, pte_flags);
2644 if (flags & LOCAL_BIND) {
2645 struct i915_hw_ppgtt *appgtt =
2646 to_i915(vma->vm->dev)->mm.aliasing_ppgtt;
2647 appgtt->base.insert_entries(&appgtt->base,
2648 vma->ggtt_view.pages,
2650 cache_level, pte_flags);
2656 static void ggtt_unbind_vma(struct i915_vma *vma)
2658 struct drm_device *dev = vma->vm->dev;
2659 struct drm_i915_private *dev_priv = dev->dev_private;
2660 struct drm_i915_gem_object *obj = vma->obj;
2661 const uint64_t size = min_t(uint64_t,
2665 if (vma->bound & GLOBAL_BIND) {
2666 vma->vm->clear_range(vma->vm,
2672 if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
2673 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
2675 appgtt->base.clear_range(&appgtt->base,
2682 void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
2684 struct drm_device *dev = obj->base.dev;
2685 struct drm_i915_private *dev_priv = dev->dev_private;
2688 interruptible = do_idling(dev_priv);
2690 dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
2691 PCI_DMA_BIDIRECTIONAL);
2693 undo_idling(dev_priv, interruptible);
2696 static void i915_gtt_color_adjust(struct drm_mm_node *node,
2697 unsigned long color,
2701 if (node->color != color)
2704 if (!list_empty(&node->node_list)) {
2705 node = list_entry(node->node_list.next,
2708 if (node->allocated && node->color != color)
2713 static int i915_gem_setup_global_gtt(struct drm_device *dev,
2718 /* Let GEM Manage all of the aperture.
2720 * However, leave one page at the end still bound to the scratch page.
2721 * There are a number of places where the hardware apparently prefetches
2722 * past the end of the object, and we've seen multiple hangs with the
2723 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2724 * aperture. One page should be enough to keep any prefetching inside
2727 struct drm_i915_private *dev_priv = to_i915(dev);
2728 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2729 struct drm_mm_node *entry;
2730 struct drm_i915_gem_object *obj;
2731 unsigned long hole_start, hole_end;
2733 unsigned long mappable;
2736 mappable = min(end, mappable_end) - start;
2737 BUG_ON(mappable_end > end);
2739 ggtt->base.start = start;
2741 /* Subtract the guard page before address space initialization to
2742 * shrink the range used by drm_mm */
2743 ggtt->base.total = end - start - PAGE_SIZE;
2744 i915_address_space_init(&ggtt->base, dev_priv);
2745 ggtt->base.total += PAGE_SIZE;
2747 if (intel_vgpu_active(dev)) {
2748 ret = intel_vgt_balloon(dev);
2754 ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
2756 /* Mark any preallocated objects as occupied */
2757 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2758 struct i915_vma *vma = i915_gem_obj_to_vma(obj, &ggtt->base);
2760 DRM_DEBUG_KMS("reserving preallocated space: %llx + %zx\n",
2761 i915_gem_obj_ggtt_offset(obj), obj->base.size);
2763 WARN_ON(i915_gem_obj_ggtt_bound(obj));
2764 ret = drm_mm_reserve_node(&ggtt->base.mm, &vma->node);
2766 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2769 vma->bound |= GLOBAL_BIND;
2770 __i915_vma_set_map_and_fenceable(vma);
2771 list_add_tail(&vma->vm_link, &ggtt->base.inactive_list);
2774 /* Clear any non-preallocated blocks */
2775 drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
2776 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2777 hole_start, hole_end);
2778 ggtt->base.clear_range(&ggtt->base, hole_start,
2779 hole_end - hole_start, true);
2782 #ifdef __DragonFly__
2783 device_printf(dev->dev->bsddev,
2784 "taking over the fictitious range 0x%llx-0x%llx\n",
2785 dev_priv->ggtt.mappable_base + start, dev_priv->ggtt.mappable_base + start + mappable);
2786 error = -vm_phys_fictitious_reg_range(dev_priv->ggtt.mappable_base + start,
2787 dev_priv->ggtt.mappable_base + start + mappable, VM_MEMATTR_WRITE_COMBINING);
2790 /* And finally clear the reserved guard page */
2791 ggtt->base.clear_range(&ggtt->base, end - PAGE_SIZE, PAGE_SIZE, true);
2793 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2794 struct i915_hw_ppgtt *ppgtt;
2796 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2800 ret = __hw_ppgtt_init(dev, ppgtt);
2802 ppgtt->base.cleanup(&ppgtt->base);
2807 if (ppgtt->base.allocate_va_range)
2808 ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
2811 ppgtt->base.cleanup(&ppgtt->base);
2816 ppgtt->base.clear_range(&ppgtt->base,
2821 dev_priv->mm.aliasing_ppgtt = ppgtt;
2822 WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
2823 ggtt->base.bind_vma = aliasing_gtt_bind_vma;
2830 * i915_gem_init_ggtt - Initialize GEM for Global GTT
2833 void i915_gem_init_ggtt(struct drm_device *dev)
2835 struct drm_i915_private *dev_priv = to_i915(dev);
2836 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2838 i915_gem_setup_global_gtt(dev, 0, ggtt->mappable_end, ggtt->base.total);
2842 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
2845 void i915_ggtt_cleanup_hw(struct drm_device *dev)
2847 struct drm_i915_private *dev_priv = to_i915(dev);
2848 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2850 if (dev_priv->mm.aliasing_ppgtt) {
2851 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2853 ppgtt->base.cleanup(&ppgtt->base);
2857 i915_gem_cleanup_stolen(dev);
2859 if (drm_mm_initialized(&ggtt->base.mm)) {
2860 if (intel_vgpu_active(dev))
2861 intel_vgt_deballoon();
2863 drm_mm_takedown(&ggtt->base.mm);
2864 list_del(&ggtt->base.global_link);
2867 ggtt->base.cleanup(&ggtt->base);
2870 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2872 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2873 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2874 return snb_gmch_ctl << 20;
2877 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2879 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2880 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2882 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2884 #ifdef CONFIG_X86_32
2885 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2886 if (bdw_gmch_ctl > 4)
2890 return bdw_gmch_ctl << 20;
2893 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2895 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2896 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2899 return 1 << (20 + gmch_ctrl);
2904 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
2906 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2907 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2908 return snb_gmch_ctl << 25; /* 32 MB units */
2911 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2913 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2914 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2915 return bdw_gmch_ctl << 25; /* 32 MB units */
2918 static size_t chv_get_stolen_size(u16 gmch_ctrl)
2920 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2921 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2924 * 0x0 to 0x10: 32MB increments starting at 0MB
2925 * 0x11 to 0x16: 4MB increments starting at 8MB
2926 * 0x17 to 0x1d: 4MB increments start at 36MB
2928 if (gmch_ctrl < 0x11)
2929 return gmch_ctrl << 25;
2930 else if (gmch_ctrl < 0x17)
2931 return (gmch_ctrl - 0x11 + 2) << 22;
2933 return (gmch_ctrl - 0x17 + 9) << 22;
2936 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2938 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2939 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2941 if (gen9_gmch_ctl < 0xf0)
2942 return gen9_gmch_ctl << 25; /* 32 MB units */
2944 /* 4MB increments starting at 0xf0 for 4MB */
2945 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2948 static int ggtt_probe_common(struct drm_device *dev,
2951 struct drm_i915_private *dev_priv = to_i915(dev);
2952 struct i915_ggtt *ggtt = &dev_priv->ggtt;
2953 struct i915_page_scratch *scratch_page;
2954 phys_addr_t ggtt_phys_addr;
2956 /* For Modern GENs the PTEs and register space are split in the BAR */
2957 ggtt_phys_addr = pci_resource_start(dev->pdev, 0) +
2958 (pci_resource_len(dev->pdev, 0) / 2);
2961 * On BXT writes larger than 64 bit to the GTT pagetable range will be
2962 * dropped. For WC mappings in general we have 64 byte burst writes
2963 * when the WC buffer is flushed, so we can't use it, but have to
2964 * resort to an uncached mapping. The WC issue is easily caught by the
2965 * readback check when writing GTT PTE entries.
2967 if (IS_BROXTON(dev))
2968 ggtt->gsm = ioremap_nocache(ggtt_phys_addr, gtt_size);
2970 ggtt->gsm = ioremap_wc(ggtt_phys_addr, gtt_size);
2972 DRM_ERROR("Failed to map the gtt page table\n");
2976 scratch_page = alloc_scratch_page(dev);
2977 if (IS_ERR(scratch_page)) {
2978 DRM_ERROR("Scratch setup failed\n");
2979 /* iounmap will also get called at remove, but meh */
2981 return PTR_ERR(scratch_page);
2984 ggtt->base.scratch_page = scratch_page;
2989 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2990 * bits. When using advanced contexts each context stores its own PAT, but
2991 * writing this data shouldn't be harmful even in those cases. */
2992 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
2996 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2997 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2998 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2999 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
3000 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
3001 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
3002 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
3003 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
3005 if (!USES_PPGTT(dev_priv))
3006 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3007 * so RTL will always use the value corresponding to
3009 * So let's disable cache for GGTT to avoid screen corruptions.
3010 * MOCS still can be used though.
3011 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3012 * before this patch, i.e. the same uncached + snooping access
3013 * like on gen6/7 seems to be in effect.
3014 * - So this just fixes blitter/render access. Again it looks
3015 * like it's not just uncached access, but uncached + snooping.
3016 * So we can still hold onto all our assumptions wrt cpu
3017 * clflushing on LLC machines.
3019 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
3021 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
3022 * write would work. */
3023 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3024 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3027 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
3032 * Map WB on BDW to snooped on CHV.
3034 * Only the snoop bit has meaning for CHV, the rest is
3037 * The hardware will never snoop for certain types of accesses:
3038 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3039 * - PPGTT page tables
3040 * - some other special cycles
3042 * As with BDW, we also need to consider the following for GT accesses:
3043 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3044 * so RTL will always use the value corresponding to
3046 * Which means we must set the snoop bit in PAT entry 0
3047 * in order to keep the global status page working.
3049 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
3053 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
3054 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
3055 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
3056 GEN8_PPAT(7, CHV_PPAT_SNOOP);
3058 I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
3059 I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
3062 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
3064 struct drm_device *dev = ggtt->base.dev;
3065 struct drm_i915_private *dev_priv = to_i915(dev);
3069 /* TODO: We're not aware of mappable constraints on gen8 yet */
3070 ggtt->mappable_base = pci_resource_start(dev->pdev, 2);
3071 ggtt->mappable_end = pci_resource_len(dev->pdev, 2);
3074 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
3075 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
3078 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3080 if (INTEL_INFO(dev)->gen >= 9) {
3081 ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
3082 ggtt->size = gen8_get_total_gtt_size(snb_gmch_ctl);
3083 } else if (IS_CHERRYVIEW(dev)) {
3084 ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
3085 ggtt->size = chv_get_total_gtt_size(snb_gmch_ctl);
3087 ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
3088 ggtt->size = gen8_get_total_gtt_size(snb_gmch_ctl);
3091 ggtt->base.total = (ggtt->size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
3093 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3094 chv_setup_private_ppat(dev_priv);
3096 bdw_setup_private_ppat(dev_priv);
3098 ret = ggtt_probe_common(dev, ggtt->size);
3100 ggtt->base.clear_range = gen8_ggtt_clear_range;
3101 if (IS_CHERRYVIEW(dev_priv))
3102 ggtt->base.insert_entries = gen8_ggtt_insert_entries__BKL;
3104 ggtt->base.insert_entries = gen8_ggtt_insert_entries;
3105 ggtt->base.bind_vma = ggtt_bind_vma;
3106 ggtt->base.unbind_vma = ggtt_unbind_vma;
3111 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
3113 struct drm_device *dev = ggtt->base.dev;
3117 ggtt->mappable_base = pci_resource_start(dev->pdev, 2);
3118 ggtt->mappable_end = pci_resource_len(dev->pdev, 2);
3120 /* 64/512MB is the current min/max we actually know of, but this is just
3121 * a coarse sanity check.
3123 if ((ggtt->mappable_end < (64<<20) || (ggtt->mappable_end > (512<<20)))) {
3124 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
3129 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
3130 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
3132 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
3134 ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
3135 ggtt->size = gen6_get_total_gtt_size(snb_gmch_ctl);
3136 ggtt->base.total = (ggtt->size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
3138 ret = ggtt_probe_common(dev, ggtt->size);
3140 ggtt->base.clear_range = gen6_ggtt_clear_range;
3141 ggtt->base.insert_entries = gen6_ggtt_insert_entries;
3142 ggtt->base.bind_vma = ggtt_bind_vma;
3143 ggtt->base.unbind_vma = ggtt_unbind_vma;
3148 static void gen6_gmch_remove(struct i915_address_space *vm)
3150 struct i915_ggtt *ggtt = container_of(vm, struct i915_ggtt, base);
3153 free_scratch_page(vm->dev, vm->scratch_page);
3156 static int i915_gmch_probe(struct i915_ggtt *ggtt)
3158 struct drm_device *dev = ggtt->base.dev;
3159 struct drm_i915_private *dev_priv = to_i915(dev);
3163 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
3165 DRM_ERROR("failed to set up gmch\n");
3170 intel_gtt_get(&ggtt->base.total, &ggtt->stolen_size,
3171 &ggtt->mappable_base, &ggtt->mappable_end);
3173 ggtt->do_idle_maps = needs_idle_maps(dev_priv->dev);
3174 ggtt->base.insert_entries = i915_ggtt_insert_entries;
3175 ggtt->base.clear_range = i915_ggtt_clear_range;
3176 ggtt->base.bind_vma = ggtt_bind_vma;
3177 ggtt->base.unbind_vma = ggtt_unbind_vma;
3179 if (unlikely(ggtt->do_idle_maps))
3180 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3185 static void i915_gmch_remove(struct i915_address_space *vm)
3187 intel_gmch_remove();
3191 * i915_ggtt_init_hw - Initialize GGTT hardware
3194 int i915_ggtt_init_hw(struct drm_device *dev)
3196 struct drm_i915_private *dev_priv = to_i915(dev);
3197 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3200 if (INTEL_INFO(dev)->gen <= 5) {
3201 ggtt->probe = i915_gmch_probe;
3202 ggtt->base.cleanup = i915_gmch_remove;
3203 } else if (INTEL_INFO(dev)->gen < 8) {
3204 ggtt->probe = gen6_gmch_probe;
3205 ggtt->base.cleanup = gen6_gmch_remove;
3208 ggtt->base.pte_encode = iris_pte_encode;
3209 else if (IS_HASWELL(dev))
3210 ggtt->base.pte_encode = hsw_pte_encode;
3211 else if (IS_VALLEYVIEW(dev))
3212 ggtt->base.pte_encode = byt_pte_encode;
3213 else if (INTEL_INFO(dev)->gen >= 7)
3214 ggtt->base.pte_encode = ivb_pte_encode;
3216 ggtt->base.pte_encode = snb_pte_encode;
3218 ggtt->probe = gen8_gmch_probe;
3219 ggtt->base.cleanup = gen6_gmch_remove;
3222 ggtt->base.dev = dev;
3223 ggtt->base.is_ggtt = true;
3225 ret = ggtt->probe(ggtt);
3229 if ((ggtt->base.total - 1) >> 32) {
3230 DRM_ERROR("We never expected a Global GTT with more than 32bits"
3231 "of address space! Found %lldM!\n",
3232 ggtt->base.total >> 20);
3233 ggtt->base.total = 1ULL << 32;
3234 ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
3238 * Initialise stolen early so that we may reserve preallocated
3239 * objects for the BIOS to KMS transition.
3241 ret = i915_gem_init_stolen(dev);
3243 goto out_gtt_cleanup;
3245 /* GMADR is the PCI mmio aperture into the global GTT. */
3246 DRM_INFO("Memory usable by graphics device = %lluM\n",
3247 ggtt->base.total >> 20);
3248 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
3249 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", ggtt->stolen_size >> 20);
3250 #ifdef CONFIG_INTEL_IOMMU
3251 if (intel_iommu_gfx_mapped)
3252 DRM_INFO("VT-d active for gfx access\n");
3255 * i915.enable_ppgtt is read-only, so do an early pass to validate the
3256 * user's requested state against the hardware/driver capabilities. We
3257 * do this now so that we can print out any log messages once rather
3258 * than every time we check intel_enable_ppgtt().
3260 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
3261 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
3266 ggtt->base.cleanup(&ggtt->base);
3271 int i915_ggtt_enable_hw(struct drm_device *dev)
3273 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
3279 void i915_gem_restore_gtt_mappings(struct drm_device *dev)
3281 struct drm_i915_private *dev_priv = to_i915(dev);
3282 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3283 struct drm_i915_gem_object *obj;
3284 struct i915_vma *vma;
3287 i915_check_and_clear_faults(dev);
3289 /* First fill our portion of the GTT with scratch pages */
3290 ggtt->base.clear_range(&ggtt->base, ggtt->base.start, ggtt->base.total,
3293 /* Cache flush objects bound into GGTT and rebind them. */
3294 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
3296 list_for_each_entry(vma, &obj->vma_list, obj_link) {
3297 if (vma->vm != &ggtt->base)
3300 WARN_ON(i915_vma_bind(vma, obj->cache_level,
3307 i915_gem_clflush_object(obj, obj->pin_display);
3310 if (INTEL_INFO(dev)->gen >= 8) {
3311 if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
3312 chv_setup_private_ppat(dev_priv);
3314 bdw_setup_private_ppat(dev_priv);
3319 if (USES_PPGTT(dev)) {
3320 struct i915_address_space *vm;
3322 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
3323 /* TODO: Perhaps it shouldn't be gen6 specific */
3325 struct i915_hw_ppgtt *ppgtt;
3328 ppgtt = dev_priv->mm.aliasing_ppgtt;
3330 ppgtt = i915_vm_to_ppgtt(vm);
3332 gen6_write_page_range(dev_priv, &ppgtt->pd,
3333 0, ppgtt->base.total);
3337 i915_ggtt_flush(dev_priv);
3340 static struct i915_vma *
3341 __i915_gem_vma_create(struct drm_i915_gem_object *obj,
3342 struct i915_address_space *vm,
3343 const struct i915_ggtt_view *ggtt_view)
3345 struct i915_vma *vma;
3347 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
3348 return ERR_PTR(-EINVAL);
3350 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
3352 return ERR_PTR(-ENOMEM);
3354 INIT_LIST_HEAD(&vma->vm_link);
3355 INIT_LIST_HEAD(&vma->obj_link);
3356 INIT_LIST_HEAD(&vma->exec_list);
3359 vma->is_ggtt = i915_is_ggtt(vm);
3361 if (i915_is_ggtt(vm))
3362 vma->ggtt_view = *ggtt_view;
3364 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
3366 list_add_tail(&vma->obj_link, &obj->vma_list);
3372 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
3373 struct i915_address_space *vm)
3375 struct i915_vma *vma;
3377 vma = i915_gem_obj_to_vma(obj, vm);
3379 vma = __i915_gem_vma_create(obj, vm,
3380 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
3386 i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
3387 const struct i915_ggtt_view *view)
3389 struct drm_device *dev = obj->base.dev;
3390 struct drm_i915_private *dev_priv = to_i915(dev);
3391 struct i915_ggtt *ggtt = &dev_priv->ggtt;
3392 struct i915_vma *vma = i915_gem_obj_to_ggtt_view(obj, view);
3395 vma = __i915_gem_vma_create(obj, &ggtt->base, view);
3401 static struct scatterlist *
3402 rotate_pages(const dma_addr_t *in, unsigned int offset,
3403 unsigned int width, unsigned int height,
3404 unsigned int stride,
3405 struct sg_table *st, struct scatterlist *sg)
3407 unsigned int column, row;
3408 unsigned int src_idx;
3410 for (column = 0; column < width; column++) {
3411 src_idx = stride * (height - 1) + column;
3412 for (row = 0; row < height; row++) {
3414 /* We don't need the pages, but need to initialize
3415 * the entries so the sg list can be happily traversed.
3416 * The only thing we need are DMA addresses.
3418 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3419 sg_dma_address(sg) = in[offset + src_idx];
3420 sg_dma_len(sg) = PAGE_SIZE;
3429 static struct sg_table *
3430 intel_rotate_fb_obj_pages(struct intel_rotation_info *rot_info,
3431 struct drm_i915_gem_object *obj)
3433 unsigned int size_pages = rot_info->plane[0].width * rot_info->plane[0].height;
3434 unsigned int size_pages_uv;
3435 struct sg_page_iter sg_iter;
3437 dma_addr_t *page_addr_list;
3438 struct sg_table *st;
3439 unsigned int uv_start_page;
3440 struct scatterlist *sg;
3443 /* Allocate a temporary list of source pages for random access. */
3444 page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
3445 sizeof(dma_addr_t));
3446 if (!page_addr_list)
3447 return ERR_PTR(ret);
3449 /* Account for UV plane with NV12. */
3450 if (rot_info->pixel_format == DRM_FORMAT_NV12)
3451 size_pages_uv = rot_info->plane[1].width * rot_info->plane[1].height;
3455 /* Allocate target SG list. */
3456 st = kmalloc(sizeof(*st), M_DRM, M_WAITOK);
3460 ret = sg_alloc_table(st, size_pages + size_pages_uv, GFP_KERNEL);
3464 /* Populate source page list from the object. */
3466 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
3467 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
3474 /* Rotate the pages. */
3475 sg = rotate_pages(page_addr_list, 0,
3476 rot_info->plane[0].width, rot_info->plane[0].height,
3477 rot_info->plane[0].width,
3480 /* Append the UV plane if NV12. */
3481 if (rot_info->pixel_format == DRM_FORMAT_NV12) {
3482 uv_start_page = size_pages;
3484 /* Check for tile-row un-alignment. */
3485 if (offset_in_page(rot_info->uv_offset))
3488 rot_info->uv_start_page = uv_start_page;
3490 sg = rotate_pages(page_addr_list, rot_info->uv_start_page,
3491 rot_info->plane[1].width, rot_info->plane[1].height,
3492 rot_info->plane[1].width,
3496 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages (%u plane 0)).\n",
3497 obj->base.size, rot_info->plane[0].width,
3498 rot_info->plane[0].height, size_pages + size_pages_uv,
3501 drm_free_large(page_addr_list);
3508 drm_free_large(page_addr_list);
3510 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%d) (%ux%u tiles, %u pages (%u plane 0))\n",
3511 obj->base.size, ret, rot_info->plane[0].width,
3512 rot_info->plane[0].height, size_pages + size_pages_uv,
3514 return ERR_PTR(ret);
3517 static struct sg_table *
3518 intel_partial_pages(const struct i915_ggtt_view *view,
3519 struct drm_i915_gem_object *obj)
3521 struct sg_table *st;
3522 struct scatterlist *sg;
3523 struct sg_page_iter obj_sg_iter;
3526 st = kmalloc(sizeof(*st), M_DRM, M_WAITOK);
3530 ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
3536 for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
3537 view->params.partial.offset)
3539 if (st->nents >= view->params.partial.size)
3542 sg_set_page(sg, NULL, PAGE_SIZE, 0);
3543 sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
3544 sg_dma_len(sg) = PAGE_SIZE;
3555 return ERR_PTR(ret);
3559 i915_get_ggtt_vma_pages(struct i915_vma *vma)
3563 if (vma->ggtt_view.pages)
3566 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
3567 vma->ggtt_view.pages = vma->obj->pages;
3568 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
3569 vma->ggtt_view.pages =
3570 intel_rotate_fb_obj_pages(&vma->ggtt_view.params.rotated, vma->obj);
3571 else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
3572 vma->ggtt_view.pages =
3573 intel_partial_pages(&vma->ggtt_view, vma->obj);
3575 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3576 vma->ggtt_view.type);
3578 if (!vma->ggtt_view.pages) {
3579 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
3580 vma->ggtt_view.type);
3582 } else if (IS_ERR(vma->ggtt_view.pages)) {
3583 ret = PTR_ERR(vma->ggtt_view.pages);
3584 vma->ggtt_view.pages = NULL;
3585 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3586 vma->ggtt_view.type, ret);
3593 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
3595 * @cache_level: mapping cache level
3596 * @flags: flags like global or local mapping
3598 * DMA addresses are taken from the scatter-gather table of this object (or of
3599 * this VMA in case of non-default GGTT views) and PTE entries set up.
3600 * Note that DMA addresses are also the only part of the SG table we care about.
3602 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
3608 if (WARN_ON(flags == 0))
3612 if (flags & PIN_GLOBAL)
3613 bind_flags |= GLOBAL_BIND;
3614 if (flags & PIN_USER)
3615 bind_flags |= LOCAL_BIND;
3617 if (flags & PIN_UPDATE)
3618 bind_flags |= vma->bound;
3620 bind_flags &= ~vma->bound;
3622 if (bind_flags == 0)
3625 if (vma->bound == 0 && vma->vm->allocate_va_range) {
3626 /* XXX: i915_vma_pin() will fix this +- hack */
3628 trace_i915_va_alloc(vma);
3629 ret = vma->vm->allocate_va_range(vma->vm,
3637 ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
3641 vma->bound |= bind_flags;
3647 * i915_ggtt_view_size - Get the size of a GGTT view.
3648 * @obj: Object the view is of.
3649 * @view: The view in question.
3651 * @return The size of the GGTT view in bytes.
3654 i915_ggtt_view_size(struct drm_i915_gem_object *obj,
3655 const struct i915_ggtt_view *view)
3657 if (view->type == I915_GGTT_VIEW_NORMAL) {
3658 return obj->base.size;
3659 } else if (view->type == I915_GGTT_VIEW_ROTATED) {
3660 return intel_rotation_info_size(&view->params.rotated) << PAGE_SHIFT;
3661 } else if (view->type == I915_GGTT_VIEW_PARTIAL) {
3662 return view->params.partial.size << PAGE_SHIFT;
3664 WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
3665 return obj->base.size;