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>
28 #include <drm/i915_drm.h>
30 #include "i915_vgpu.h"
31 #include "i915_trace.h"
32 #include "intel_drv.h"
34 #include <linux/bitmap.h>
35 #include <linux/highmem.h>
38 * DOC: Global GTT views
40 * Background and previous state
42 * Historically objects could exists (be bound) in global GTT space only as
43 * singular instances with a view representing all of the object's backing pages
44 * in a linear fashion. This view will be called a normal view.
46 * To support multiple views of the same object, where the number of mapped
47 * pages is not equal to the backing store, or where the layout of the pages
48 * is not linear, concept of a GGTT view was added.
50 * One example of an alternative view is a stereo display driven by a single
51 * image. In this case we would have a framebuffer looking like this
57 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
58 * rendering. In contrast, fed to the display engine would be an alternative
59 * view which could look something like this:
64 * In this example both the size and layout of pages in the alternative view is
65 * different from the normal view.
67 * Implementation and usage
69 * GGTT views are implemented using VMAs and are distinguished via enum
70 * i915_ggtt_view_type and struct i915_ggtt_view.
72 * A new flavour of core GEM functions which work with GGTT bound objects were
73 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
74 * renaming in large amounts of code. They take the struct i915_ggtt_view
75 * parameter encapsulating all metadata required to implement a view.
77 * As a helper for callers which are only interested in the normal view,
78 * globally const i915_ggtt_view_normal singleton instance exists. All old core
79 * GEM API functions, the ones not taking the view parameter, are operating on,
80 * or with the normal GGTT view.
82 * Code wanting to add or use a new GGTT view needs to:
84 * 1. Add a new enum with a suitable name.
85 * 2. Extend the metadata in the i915_ggtt_view structure if required.
86 * 3. Add support to i915_get_vma_pages().
88 * New views are required to build a scatter-gather table from within the
89 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
90 * exists for the lifetime of an VMA.
92 * Core API is designed to have copy semantics which means that passed in
93 * struct i915_ggtt_view does not need to be persistent (left around after
94 * calling the core API functions).
98 const struct i915_ggtt_view i915_ggtt_view_normal;
99 const struct i915_ggtt_view i915_ggtt_view_rotated = {
100 .type = I915_GGTT_VIEW_ROTATED
103 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
104 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
106 static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
108 bool has_aliasing_ppgtt;
111 has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
112 has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
114 if (intel_vgpu_active(dev))
115 has_full_ppgtt = false; /* emulation is too hard */
118 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
119 * execlists, the sole mechanism available to submit work.
121 if (INTEL_INFO(dev)->gen < 9 &&
122 (enable_ppgtt == 0 || !has_aliasing_ppgtt))
125 if (enable_ppgtt == 1)
128 if (enable_ppgtt == 2 && has_full_ppgtt)
131 #ifdef CONFIG_INTEL_IOMMU
132 /* Disable ppgtt on SNB if VT-d is on. */
133 if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
134 DRM_INFO("Disabling PPGTT because VT-d is on\n");
139 /* Early VLV doesn't have this */
140 if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
141 dev->pdev->revision < 0xb) {
142 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
146 if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
149 return has_aliasing_ppgtt ? 1 : 0;
152 static void ppgtt_bind_vma(struct i915_vma *vma,
153 enum i915_cache_level cache_level,
155 static void ppgtt_unbind_vma(struct i915_vma *vma);
157 static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr,
158 enum i915_cache_level level,
161 gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
165 case I915_CACHE_NONE:
166 pte |= PPAT_UNCACHED_INDEX;
169 pte |= PPAT_DISPLAY_ELLC_INDEX;
172 pte |= PPAT_CACHED_INDEX;
179 static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev,
181 enum i915_cache_level level)
183 gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
185 if (level != I915_CACHE_NONE)
186 pde |= PPAT_CACHED_PDE_INDEX;
188 pde |= PPAT_UNCACHED_INDEX;
192 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
193 enum i915_cache_level level,
194 bool valid, u32 unused)
196 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
197 pte |= GEN6_PTE_ADDR_ENCODE(addr);
200 case I915_CACHE_L3_LLC:
202 pte |= GEN6_PTE_CACHE_LLC;
204 case I915_CACHE_NONE:
205 pte |= GEN6_PTE_UNCACHED;
214 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
215 enum i915_cache_level level,
216 bool valid, u32 unused)
218 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
219 pte |= GEN6_PTE_ADDR_ENCODE(addr);
222 case I915_CACHE_L3_LLC:
223 pte |= GEN7_PTE_CACHE_L3_LLC;
226 pte |= GEN6_PTE_CACHE_LLC;
228 case I915_CACHE_NONE:
229 pte |= GEN6_PTE_UNCACHED;
238 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
239 enum i915_cache_level level,
240 bool valid, u32 flags)
242 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
243 pte |= GEN6_PTE_ADDR_ENCODE(addr);
245 if (!(flags & PTE_READ_ONLY))
246 pte |= BYT_PTE_WRITEABLE;
248 if (level != I915_CACHE_NONE)
249 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
254 static gen6_pte_t hsw_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 |= HSW_PTE_ADDR_ENCODE(addr);
261 if (level != I915_CACHE_NONE)
262 pte |= HSW_WB_LLC_AGE3;
267 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
268 enum i915_cache_level level,
269 bool valid, u32 unused)
271 gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
272 pte |= HSW_PTE_ADDR_ENCODE(addr);
275 case I915_CACHE_NONE:
278 pte |= HSW_WT_ELLC_LLC_AGE3;
281 pte |= HSW_WB_ELLC_LLC_AGE3;
288 #define i915_dma_unmap_single(px, dev) \
289 __i915_dma_unmap_single((px)->daddr, dev)
291 static inline void __i915_dma_unmap_single(dma_addr_t daddr,
292 struct drm_device *dev)
295 struct device *device = &dev->pdev->dev;
297 dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
302 * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
303 * @px: Page table/dir/etc to get a DMA map for
306 * Page table allocations are unified across all gens. They always require a
307 * single 4k allocation, as well as a DMA mapping. If we keep the structs
308 * symmetric here, the simple macro covers us for every page table type.
310 * Return: 0 if success.
312 #define i915_dma_map_single(px, dev) \
313 i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
315 static inline int i915_dma_map_page_single(struct vm_page *page,
316 struct drm_device *dev,
319 struct device *device = dev->pdev->dev;
321 *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
322 if (dma_mapping_error(device, *daddr))
328 static void unmap_and_free_pt(struct i915_page_table_entry *pt,
329 struct drm_device *dev)
331 if (WARN_ON(!pt->page))
334 i915_dma_unmap_single(pt, dev);
335 __free_page(pt->page);
336 kfree(pt->used_ptes);
340 static struct i915_page_table_entry *alloc_pt_single(struct drm_device *dev)
342 struct i915_page_table_entry *pt;
343 const size_t count = INTEL_INFO(dev)->gen >= 8 ?
344 GEN8_PTES : GEN6_PTES;
347 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
349 return ERR_PTR(-ENOMEM);
351 pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
357 pt->page = alloc_page(GFP_KERNEL);
361 ret = i915_dma_map_single(pt, dev);
368 __free_page(pt->page);
370 kfree(pt->used_ptes);
378 * alloc_pt_range() - Allocate a multiple page tables
379 * @pd: The page directory which will have at least @count entries
380 * available to point to the allocated page tables.
381 * @pde: First page directory entry for which we are allocating.
382 * @count: Number of pages to allocate.
385 * Allocates multiple page table pages and sets the appropriate entries in the
386 * page table structure within the page directory. Function cleans up after
387 * itself on any failures.
389 * Return: 0 if allocation succeeded.
391 static int alloc_pt_range(struct i915_page_directory_entry *pd, uint16_t pde, size_t count,
392 struct drm_device *dev)
396 /* 512 is the max page tables per page_directory on any platform. */
397 if (WARN_ON(pde + count > I915_PDES))
400 for (i = pde; i < pde + count; i++) {
401 struct i915_page_table_entry *pt = alloc_pt_single(dev);
407 WARN(pd->page_table[i],
408 "Leaking page directory entry %d (%p)\n",
409 i, pd->page_table[i]);
410 pd->page_table[i] = pt;
417 unmap_and_free_pt(pd->page_table[i], dev);
421 static void unmap_and_free_pd(struct i915_page_directory_entry *pd)
424 __free_page(pd->page);
429 static struct i915_page_directory_entry *alloc_pd_single(void)
431 struct i915_page_directory_entry *pd;
433 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
435 return ERR_PTR(-ENOMEM);
437 pd->page = alloc_page(GFP_KERNEL | __GFP_ZERO);
440 return ERR_PTR(-ENOMEM);
446 /* Broadwell Page Directory Pointer Descriptors */
447 static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
454 ret = intel_ring_begin(ring, 6);
458 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
459 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
460 intel_ring_emit(ring, (u32)(val >> 32));
461 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
462 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
463 intel_ring_emit(ring, (u32)(val));
464 intel_ring_advance(ring);
469 static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
470 struct intel_engine_cs *ring)
474 /* bit of a hack to find the actual last used pd */
475 int used_pd = ppgtt->num_pd_entries / I915_PDES;
477 for (i = used_pd - 1; i >= 0; i--) {
478 dma_addr_t addr = ppgtt->pdp.page_directory[i]->daddr;
479 ret = gen8_write_pdp(ring, i, addr);
487 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
492 struct i915_hw_ppgtt *ppgtt =
493 container_of(vm, struct i915_hw_ppgtt, base);
494 gen8_pte_t *pt_vaddr, scratch_pte;
495 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
496 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
497 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
498 unsigned num_entries = length >> PAGE_SHIFT;
499 unsigned last_pte, i;
501 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
502 I915_CACHE_LLC, use_scratch);
504 while (num_entries) {
505 struct i915_page_directory_entry *pd;
506 struct i915_page_table_entry *pt;
507 struct vm_page *page_table;
509 if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
512 pd = ppgtt->pdp.page_directory[pdpe];
514 if (WARN_ON(!pd->page_table[pde]))
517 pt = pd->page_table[pde];
519 if (WARN_ON(!pt->page))
522 page_table = pt->page;
524 last_pte = pte + num_entries;
525 if (last_pte > GEN8_PTES)
526 last_pte = GEN8_PTES;
528 pt_vaddr = kmap_atomic(page_table);
530 for (i = pte; i < last_pte; i++) {
531 pt_vaddr[i] = scratch_pte;
535 if (!HAS_LLC(ppgtt->base.dev))
536 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
537 kunmap_atomic(pt_vaddr);
540 if (++pde == I915_PDES) {
547 static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
550 unsigned int num_entries,
551 enum i915_cache_level cache_level, u32 unused)
553 struct i915_hw_ppgtt *ppgtt =
554 container_of(vm, struct i915_hw_ppgtt, base);
555 gen8_pte_t *pt_vaddr;
556 unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
557 unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
558 unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
563 for (i=0;i<num_entries;i++) {
564 if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
567 if (pt_vaddr == NULL) {
568 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[pdpe];
569 struct i915_page_table_entry *pt = pd->page_table[pde];
570 struct vm_page *page_table = pt->page;
572 pt_vaddr = kmap_atomic(page_table);
576 gen8_pte_encode(VM_PAGE_TO_PHYS(pages[i]),
578 if (++pte == GEN8_PTES) {
579 if (!HAS_LLC(ppgtt->base.dev))
580 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
581 kunmap_atomic(pt_vaddr);
583 if (++pde == I915_PDES) {
591 if (!HAS_LLC(ppgtt->base.dev))
592 drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
593 kunmap_atomic(pt_vaddr);
597 static void gen8_free_page_tables(struct i915_page_directory_entry *pd, struct drm_device *dev)
604 for (i = 0; i < I915_PDES; i++) {
605 if (WARN_ON(!pd->page_table[i]))
608 unmap_and_free_pt(pd->page_table[i], dev);
609 pd->page_table[i] = NULL;
613 static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
617 for (i = 0; i < ppgtt->num_pd_pages; i++) {
618 if (WARN_ON(!ppgtt->pdp.page_directory[i]))
621 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
622 unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
626 static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
628 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
631 for (i = 0; i < ppgtt->num_pd_pages; i++) {
632 /* TODO: In the future we'll support sparse mappings, so this
633 * will have to change. */
634 if (!ppgtt->pdp.page_directory[i]->daddr)
637 pci_unmap_page(hwdev, ppgtt->pdp.page_directory[i]->daddr, PAGE_SIZE,
638 PCI_DMA_BIDIRECTIONAL);
640 for (j = 0; j < I915_PDES; j++) {
641 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
642 struct i915_page_table_entry *pt;
645 if (WARN_ON(!pd->page_table[j]))
648 pt = pd->page_table[j];
652 pci_unmap_page(hwdev, addr, PAGE_SIZE,
653 PCI_DMA_BIDIRECTIONAL);
658 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
660 struct i915_hw_ppgtt *ppgtt =
661 container_of(vm, struct i915_hw_ppgtt, base);
663 gen8_ppgtt_unmap_pages(ppgtt);
664 gen8_ppgtt_free(ppgtt);
667 static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
671 for (i = 0; i < ppgtt->num_pd_pages; i++) {
672 ret = alloc_pt_range(ppgtt->pdp.page_directory[i],
673 0, I915_PDES, ppgtt->base.dev);
682 gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
687 static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
692 for (i = 0; i < max_pdp; i++) {
693 ppgtt->pdp.page_directory[i] = alloc_pd_single();
694 if (IS_ERR(ppgtt->pdp.page_directory[i]))
698 ppgtt->num_pd_pages = max_pdp;
699 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPES);
705 unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
710 static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
715 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
719 ret = gen8_ppgtt_allocate_page_tables(ppgtt);
723 ppgtt->num_pd_entries = max_pdp * I915_PDES;
728 gen8_ppgtt_free(ppgtt);
732 static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
738 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
739 ppgtt->pdp.page_directory[pd]->page, 0,
740 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
742 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
746 ppgtt->pdp.page_directory[pd]->daddr = pd_addr;
751 static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
756 struct i915_page_directory_entry *pdir = ppgtt->pdp.page_directory[pd];
757 struct i915_page_table_entry *ptab = pdir->page_table[pt];
758 struct vm_page *p = ptab->page;
761 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
762 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
763 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
767 ptab->daddr = pt_addr;
773 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
774 * with a net effect resembling a 2-level page table in normal x86 terms. Each
775 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
778 * FIXME: split allocation into smaller pieces. For now we only ever do this
779 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
780 * TODO: Do something with the size parameter
782 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
784 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
785 const int min_pt_pages = I915_PDES * max_pdp;
789 DRM_INFO("Pages will be wasted unless GTT size (%lu) is divisible by 1GB\n", size);
791 /* 1. Do all our allocations for page directories and page tables.
792 * We allocate more than was asked so that we can point the unused parts
793 * to valid entries that point to scratch page. Dynamic page tables
794 * will fix this eventually.
796 ret = gen8_ppgtt_alloc(ppgtt, GEN8_LEGACY_PDPES);
801 * 2. Create DMA mappings for the page directories and page tables.
803 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
804 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
808 for (j = 0; j < I915_PDES; j++) {
809 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
816 * 3. Map all the page directory entires to point to the page tables
819 * For now, the PPGTT helper functions all require that the PDEs are
820 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
821 * will never need to touch the PDEs again.
823 for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
824 struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
825 gen8_pde_t *pd_vaddr;
826 pd_vaddr = kmap_atomic(ppgtt->pdp.page_directory[i]->page);
827 for (j = 0; j < I915_PDES; j++) {
828 struct i915_page_table_entry *pt = pd->page_table[j];
829 dma_addr_t addr = pt->daddr;
830 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
833 if (!HAS_LLC(ppgtt->base.dev))
834 drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
835 kunmap_atomic(pd_vaddr);
838 ppgtt->switch_mm = gen8_mm_switch;
839 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
840 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
841 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
842 ppgtt->base.start = 0;
844 /* This is the area that we advertise as usable for the caller */
845 ppgtt->base.total = max_pdp * I915_PDES * GEN8_PTES * PAGE_SIZE;
847 /* Set all ptes to a valid scratch page. Also above requested space */
848 ppgtt->base.clear_range(&ppgtt->base, 0,
849 ppgtt->num_pd_pages * GEN8_PTES * PAGE_SIZE,
852 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
853 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
854 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%ld wasted)\n",
855 ppgtt->num_pd_entries,
856 (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
860 gen8_ppgtt_unmap_pages(ppgtt);
861 gen8_ppgtt_free(ppgtt);
865 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
867 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
868 struct i915_address_space *vm = &ppgtt->base;
869 gen6_pte_t __iomem *pd_addr;
870 gen6_pte_t scratch_pte;
874 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
876 pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
877 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
879 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
881 ppgtt->pd.pd_offset + ppgtt->num_pd_entries);
882 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
884 gen6_pte_t *pt_vaddr;
885 dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
886 pd_entry = readl(pd_addr + pde);
887 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
889 if (pd_entry != expected)
890 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
894 seq_printf(m, "\tPDE: %x\n", pd_entry);
896 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
897 for (pte = 0; pte < GEN6_PTES; pte+=4) {
899 (pde * PAGE_SIZE * GEN6_PTES) +
903 for (i = 0; i < 4; i++)
904 if (pt_vaddr[pte + i] != scratch_pte)
909 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
910 for (i = 0; i < 4; i++) {
911 if (pt_vaddr[pte + i] != scratch_pte)
912 seq_printf(m, " %08x", pt_vaddr[pte + i]);
914 seq_puts(m, " SCRATCH ");
918 kunmap_atomic(pt_vaddr);
922 /* Write pde (index) from the page directory @pd to the page table @pt */
923 static void gen6_write_pde(struct i915_page_directory_entry *pd,
924 const int pde, struct i915_page_table_entry *pt)
926 /* Caller needs to make sure the write completes if necessary */
927 struct i915_hw_ppgtt *ppgtt =
928 container_of(pd, struct i915_hw_ppgtt, pd);
931 pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
932 pd_entry |= GEN6_PDE_VALID;
934 writel(pd_entry, ppgtt->pd_addr + pde);
937 /* Write all the page tables found in the ppgtt structure to incrementing page
939 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
940 struct i915_page_directory_entry *pd,
941 uint32_t start, uint32_t length)
943 struct i915_page_table_entry *pt;
946 gen6_for_each_pde(pt, pd, start, length, temp, pde)
947 gen6_write_pde(pd, pde, pt);
949 /* Make sure write is complete before other code can use this page
950 * table. Also require for WC mapped PTEs */
951 readl(dev_priv->gtt.gsm);
954 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
956 BUG_ON(ppgtt->pd.pd_offset & 0x3f);
958 return (ppgtt->pd.pd_offset / 64) << 16;
961 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
962 struct intel_engine_cs *ring)
966 /* NB: TLBs must be flushed and invalidated before a switch */
967 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
971 ret = intel_ring_begin(ring, 6);
975 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
976 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
977 intel_ring_emit(ring, PP_DIR_DCLV_2G);
978 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
979 intel_ring_emit(ring, get_pd_offset(ppgtt));
980 intel_ring_emit(ring, MI_NOOP);
981 intel_ring_advance(ring);
986 static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
987 struct intel_engine_cs *ring)
989 struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
991 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
992 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
996 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
997 struct intel_engine_cs *ring)
1001 /* NB: TLBs must be flushed and invalidated before a switch */
1002 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1006 ret = intel_ring_begin(ring, 6);
1010 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
1011 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
1012 intel_ring_emit(ring, PP_DIR_DCLV_2G);
1013 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
1014 intel_ring_emit(ring, get_pd_offset(ppgtt));
1015 intel_ring_emit(ring, MI_NOOP);
1016 intel_ring_advance(ring);
1018 /* XXX: RCS is the only one to auto invalidate the TLBs? */
1019 if (ring->id != RCS) {
1020 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
1028 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
1029 struct intel_engine_cs *ring)
1031 struct drm_device *dev = ppgtt->base.dev;
1032 struct drm_i915_private *dev_priv = dev->dev_private;
1035 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
1036 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
1038 POSTING_READ(RING_PP_DIR_DCLV(ring));
1043 static void gen8_ppgtt_enable(struct drm_device *dev)
1045 struct drm_i915_private *dev_priv = dev->dev_private;
1046 struct intel_engine_cs *ring;
1049 for_each_ring(ring, dev_priv, j) {
1050 I915_WRITE(RING_MODE_GEN7(ring),
1051 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1055 static void gen7_ppgtt_enable(struct drm_device *dev)
1057 struct drm_i915_private *dev_priv = dev->dev_private;
1058 struct intel_engine_cs *ring;
1059 uint32_t ecochk, ecobits;
1062 ecobits = I915_READ(GAC_ECO_BITS);
1063 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
1065 ecochk = I915_READ(GAM_ECOCHK);
1066 if (IS_HASWELL(dev)) {
1067 ecochk |= ECOCHK_PPGTT_WB_HSW;
1069 ecochk |= ECOCHK_PPGTT_LLC_IVB;
1070 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
1072 I915_WRITE(GAM_ECOCHK, ecochk);
1074 for_each_ring(ring, dev_priv, i) {
1075 /* GFX_MODE is per-ring on gen7+ */
1076 I915_WRITE(RING_MODE_GEN7(ring),
1077 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1081 static void gen6_ppgtt_enable(struct drm_device *dev)
1083 struct drm_i915_private *dev_priv = dev->dev_private;
1084 uint32_t ecochk, gab_ctl, ecobits;
1086 ecobits = I915_READ(GAC_ECO_BITS);
1087 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
1088 ECOBITS_PPGTT_CACHE64B);
1090 gab_ctl = I915_READ(GAB_CTL);
1091 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
1093 ecochk = I915_READ(GAM_ECOCHK);
1094 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
1096 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
1099 /* PPGTT support for Sandybdrige/Gen6 and later */
1100 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
1105 struct i915_hw_ppgtt *ppgtt =
1106 container_of(vm, struct i915_hw_ppgtt, base);
1107 gen6_pte_t *pt_vaddr, scratch_pte;
1108 unsigned first_entry = start >> PAGE_SHIFT;
1109 unsigned num_entries = length >> PAGE_SHIFT;
1110 unsigned act_pt = first_entry / GEN6_PTES;
1111 unsigned first_pte = first_entry % GEN6_PTES;
1112 unsigned last_pte, i;
1114 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
1116 while (num_entries) {
1117 last_pte = first_pte + num_entries;
1118 if (last_pte > GEN6_PTES)
1119 last_pte = GEN6_PTES;
1121 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
1123 for (i = first_pte; i < last_pte; i++)
1124 pt_vaddr[i] = scratch_pte;
1126 kunmap_atomic(pt_vaddr);
1128 num_entries -= last_pte - first_pte;
1134 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
1137 unsigned num_entries,
1138 enum i915_cache_level cache_level, u32 flags)
1140 struct i915_hw_ppgtt *ppgtt =
1141 container_of(vm, struct i915_hw_ppgtt, base);
1142 gen6_pte_t *pt_vaddr;
1143 unsigned first_entry = start >> PAGE_SHIFT;
1144 unsigned act_pt = first_entry / GEN6_PTES;
1145 unsigned act_pte = first_entry % GEN6_PTES;
1148 for (int i=0;i<num_entries;i++) {
1149 if (pt_vaddr == NULL)
1150 pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
1153 vm->pte_encode(VM_PAGE_TO_PHYS(pages[i]),
1154 cache_level, true, flags);
1156 if (++act_pte == GEN6_PTES) {
1157 kunmap_atomic(pt_vaddr);
1164 kunmap_atomic(pt_vaddr);
1167 /* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
1168 * are switching between contexts with the same LRCA, we also must do a force
1171 static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
1173 /* If current vm != vm, */
1174 ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
1177 static void gen6_initialize_pt(struct i915_address_space *vm,
1178 struct i915_page_table_entry *pt)
1180 gen6_pte_t *pt_vaddr, scratch_pte;
1183 WARN_ON(vm->scratch.addr == 0);
1185 scratch_pte = vm->pte_encode(vm->scratch.addr,
1186 I915_CACHE_LLC, true, 0);
1188 pt_vaddr = kmap_atomic(pt->page);
1190 for (i = 0; i < GEN6_PTES; i++)
1191 pt_vaddr[i] = scratch_pte;
1193 kunmap_atomic(pt_vaddr);
1196 static int gen6_alloc_va_range(struct i915_address_space *vm,
1197 uint64_t start, uint64_t length)
1199 DECLARE_BITMAP(new_page_tables, I915_PDES);
1200 struct drm_device *dev = vm->dev;
1201 struct drm_i915_private *dev_priv = dev->dev_private;
1202 struct i915_hw_ppgtt *ppgtt =
1203 container_of(vm, struct i915_hw_ppgtt, base);
1204 struct i915_page_table_entry *pt;
1205 const uint32_t start_save = start, length_save = length;
1209 WARN_ON(upper_32_bits(start));
1211 bitmap_zero(new_page_tables, I915_PDES);
1213 /* The allocation is done in two stages so that we can bail out with
1214 * minimal amount of pain. The first stage finds new page tables that
1215 * need allocation. The second stage marks use ptes within the page
1218 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1219 if (pt != ppgtt->scratch_pt) {
1220 WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
1224 /* We've already allocated a page table */
1225 WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
1227 pt = alloc_pt_single(dev);
1233 gen6_initialize_pt(vm, pt);
1235 ppgtt->pd.page_table[pde] = pt;
1236 set_bit(pde, new_page_tables);
1237 trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
1241 length = length_save;
1243 gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
1244 DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
1246 bitmap_zero(tmp_bitmap, GEN6_PTES);
1247 bitmap_set(tmp_bitmap, gen6_pte_index(start),
1248 gen6_pte_count(start, length));
1250 if (test_and_clear_bit(pde, new_page_tables))
1251 gen6_write_pde(&ppgtt->pd, pde, pt);
1253 trace_i915_page_table_entry_map(vm, pde, pt,
1254 gen6_pte_index(start),
1255 gen6_pte_count(start, length),
1257 bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
1261 WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
1263 /* Make sure write is complete before other code can use this page
1264 * table. Also require for WC mapped PTEs */
1265 readl(dev_priv->gtt.gsm);
1267 mark_tlbs_dirty(ppgtt);
1271 for_each_set_bit(pde, new_page_tables, I915_PDES) {
1272 struct i915_page_table_entry *pt = ppgtt->pd.page_table[pde];
1274 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1275 unmap_and_free_pt(pt, vm->dev);
1278 mark_tlbs_dirty(ppgtt);
1282 static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
1286 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1287 struct i915_page_table_entry *pt = ppgtt->pd.page_table[i];
1289 if (pt != ppgtt->scratch_pt)
1290 unmap_and_free_pt(ppgtt->pd.page_table[i], ppgtt->base.dev);
1293 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
1294 unmap_and_free_pd(&ppgtt->pd);
1297 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
1299 struct i915_hw_ppgtt *ppgtt =
1300 container_of(vm, struct i915_hw_ppgtt, base);
1302 drm_mm_remove_node(&ppgtt->node);
1304 gen6_ppgtt_free(ppgtt);
1307 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
1309 struct drm_device *dev = ppgtt->base.dev;
1310 struct drm_i915_private *dev_priv = dev->dev_private;
1311 bool retried = false;
1314 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1315 * allocator works in address space sizes, so it's multiplied by page
1316 * size. We allocate at the top of the GTT to avoid fragmentation.
1318 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
1319 ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
1320 if (IS_ERR(ppgtt->scratch_pt))
1321 return PTR_ERR(ppgtt->scratch_pt);
1323 gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
1326 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
1327 &ppgtt->node, GEN6_PD_SIZE,
1329 0, dev_priv->gtt.base.total,
1331 if (ret == -ENOSPC && !retried) {
1332 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
1333 GEN6_PD_SIZE, GEN6_PD_ALIGN,
1335 0, dev_priv->gtt.base.total,
1348 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
1349 DRM_DEBUG("Forced to use aperture for PDEs\n");
1351 ppgtt->num_pd_entries = I915_PDES;
1355 unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
1359 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
1361 return gen6_ppgtt_allocate_page_directories(ppgtt);
1364 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
1365 uint64_t start, uint64_t length)
1367 struct i915_page_table_entry *unused;
1370 gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
1371 ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
1374 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt, bool aliasing)
1376 struct drm_device *dev = ppgtt->base.dev;
1377 struct drm_i915_private *dev_priv = dev->dev_private;
1380 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
1382 ppgtt->switch_mm = gen6_mm_switch;
1383 } else if (IS_HASWELL(dev)) {
1384 ppgtt->switch_mm = hsw_mm_switch;
1385 } else if (IS_GEN7(dev)) {
1386 ppgtt->switch_mm = gen7_mm_switch;
1390 if (intel_vgpu_active(dev))
1391 ppgtt->switch_mm = vgpu_mm_switch;
1393 ret = gen6_ppgtt_alloc(ppgtt);
1398 /* preallocate all pts */
1399 ret = alloc_pt_range(&ppgtt->pd, 0, ppgtt->num_pd_entries,
1403 gen6_ppgtt_cleanup(&ppgtt->base);
1408 ppgtt->base.allocate_va_range = gen6_alloc_va_range;
1409 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
1410 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
1411 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
1412 ppgtt->base.start = 0;
1413 ppgtt->base.total = ppgtt->num_pd_entries * GEN6_PTES * PAGE_SIZE;
1414 ppgtt->debug_dump = gen6_dump_ppgtt;
1416 ppgtt->pd.pd_offset =
1417 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
1419 ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
1420 ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
1423 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1425 gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
1427 gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
1429 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1430 ppgtt->node.size >> 20,
1431 ppgtt->node.start / PAGE_SIZE);
1433 DRM_DEBUG("Adding PPGTT at offset %x\n",
1434 ppgtt->pd.pd_offset << 10);
1439 static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt,
1442 struct drm_i915_private *dev_priv = dev->dev_private;
1444 ppgtt->base.dev = dev;
1445 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
1447 if (INTEL_INFO(dev)->gen < 8)
1448 return gen6_ppgtt_init(ppgtt, aliasing);
1450 return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
1452 int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1454 struct drm_i915_private *dev_priv = dev->dev_private;
1457 ret = __hw_ppgtt_init(dev, ppgtt, false);
1459 kref_init(&ppgtt->ref);
1460 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1462 i915_init_vm(dev_priv, &ppgtt->base);
1468 int i915_ppgtt_init_hw(struct drm_device *dev)
1470 struct drm_i915_private *dev_priv = dev->dev_private;
1471 struct intel_engine_cs *ring;
1472 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
1475 /* In the case of execlists, PPGTT is enabled by the context descriptor
1476 * and the PDPs are contained within the context itself. We don't
1477 * need to do anything here. */
1478 if (i915.enable_execlists)
1481 if (!USES_PPGTT(dev))
1485 gen6_ppgtt_enable(dev);
1486 else if (IS_GEN7(dev))
1487 gen7_ppgtt_enable(dev);
1488 else if (INTEL_INFO(dev)->gen >= 8)
1489 gen8_ppgtt_enable(dev);
1491 MISSING_CASE(INTEL_INFO(dev)->gen);
1494 for_each_ring(ring, dev_priv, i) {
1495 ret = ppgtt->switch_mm(ppgtt, ring);
1503 struct i915_hw_ppgtt *
1504 i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
1506 struct i915_hw_ppgtt *ppgtt;
1509 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
1511 return ERR_PTR(-ENOMEM);
1513 ret = i915_ppgtt_init(dev, ppgtt);
1516 return ERR_PTR(ret);
1519 ppgtt->file_priv = fpriv;
1521 trace_i915_ppgtt_create(&ppgtt->base);
1526 void i915_ppgtt_release(struct kref *kref)
1528 struct i915_hw_ppgtt *ppgtt =
1529 container_of(kref, struct i915_hw_ppgtt, ref);
1531 trace_i915_ppgtt_release(&ppgtt->base);
1533 /* vmas should already be unbound */
1534 WARN_ON(!list_empty(&ppgtt->base.active_list));
1535 WARN_ON(!list_empty(&ppgtt->base.inactive_list));
1537 list_del(&ppgtt->base.global_link);
1538 drm_mm_takedown(&ppgtt->base.mm);
1540 ppgtt->base.cleanup(&ppgtt->base);
1545 ppgtt_bind_vma(struct i915_vma *vma,
1546 enum i915_cache_level cache_level,
1549 const unsigned int num_entries = vma->obj->base.size >> PAGE_SHIFT;
1551 /* Currently applicable only to VLV */
1552 if (vma->obj->gt_ro)
1553 flags |= PTE_READ_ONLY;
1555 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
1557 cache_level, flags);
1560 static void ppgtt_unbind_vma(struct i915_vma *vma)
1562 vma->vm->clear_range(vma->vm,
1564 vma->obj->base.size,
1568 extern int intel_iommu_gfx_mapped;
1569 /* Certain Gen5 chipsets require require idling the GPU before
1570 * unmapping anything from the GTT when VT-d is enabled.
1572 static inline bool needs_idle_maps(struct drm_device *dev)
1574 #ifdef CONFIG_INTEL_IOMMU
1575 /* Query intel_iommu to see if we need the workaround. Presumably that
1578 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1584 static bool do_idling(struct drm_i915_private *dev_priv)
1586 bool ret = dev_priv->mm.interruptible;
1588 if (unlikely(dev_priv->gtt.do_idle_maps)) {
1589 dev_priv->mm.interruptible = false;
1590 if (i915_gpu_idle(dev_priv->dev)) {
1591 DRM_ERROR("Couldn't idle GPU\n");
1592 /* Wait a bit, in hopes it avoids the hang */
1600 static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1602 if (unlikely(dev_priv->gtt.do_idle_maps))
1603 dev_priv->mm.interruptible = interruptible;
1606 void i915_check_and_clear_faults(struct drm_device *dev)
1608 struct drm_i915_private *dev_priv = dev->dev_private;
1609 struct intel_engine_cs *ring;
1612 if (INTEL_INFO(dev)->gen < 6)
1615 for_each_ring(ring, dev_priv, i) {
1617 fault_reg = I915_READ(RING_FAULT_REG(ring));
1618 if (fault_reg & RING_FAULT_VALID) {
1620 DRM_DEBUG_DRIVER("Unexpected fault\n"
1622 "\tAddress space: %s\n"
1625 fault_reg & PAGE_MASK,
1626 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1627 RING_FAULT_SRCID(fault_reg),
1628 RING_FAULT_FAULT_TYPE(fault_reg));
1630 I915_WRITE(RING_FAULT_REG(ring),
1631 fault_reg & ~RING_FAULT_VALID);
1634 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1637 static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
1639 if (INTEL_INFO(dev_priv->dev)->gen < 6) {
1640 intel_gtt_chipset_flush();
1642 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1643 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1647 void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1649 struct drm_i915_private *dev_priv = dev->dev_private;
1651 /* Don't bother messing with faults pre GEN6 as we have little
1652 * documentation supporting that it's a good idea.
1654 if (INTEL_INFO(dev)->gen < 6)
1657 i915_check_and_clear_faults(dev);
1659 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1660 dev_priv->gtt.base.start,
1661 dev_priv->gtt.base.total,
1664 i915_ggtt_flush(dev_priv);
1667 void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1669 struct drm_i915_private *dev_priv = dev->dev_private;
1670 struct drm_i915_gem_object *obj;
1671 struct i915_address_space *vm;
1673 i915_check_and_clear_faults(dev);
1675 /* First fill our portion of the GTT with scratch pages */
1676 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1677 dev_priv->gtt.base.start,
1678 dev_priv->gtt.base.total,
1681 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1682 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1683 &dev_priv->gtt.base);
1687 i915_gem_clflush_object(obj, obj->pin_display);
1688 /* The bind_vma code tries to be smart about tracking mappings.
1689 * Unfortunately above, we've just wiped out the mappings
1690 * without telling our object about it. So we need to fake it.
1692 * Bind is not expected to fail since this is only called on
1693 * resume and assumption is all requirements exist already.
1695 vma->bound &= ~GLOBAL_BIND;
1696 WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND));
1700 if (INTEL_INFO(dev)->gen >= 8) {
1701 if (IS_CHERRYVIEW(dev))
1702 chv_setup_private_ppat(dev_priv);
1704 bdw_setup_private_ppat(dev_priv);
1709 if (USES_PPGTT(dev)) {
1710 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1711 /* TODO: Perhaps it shouldn't be gen6 specific */
1713 struct i915_hw_ppgtt *ppgtt =
1714 container_of(vm, struct i915_hw_ppgtt,
1717 if (i915_is_ggtt(vm))
1718 ppgtt = dev_priv->mm.aliasing_ppgtt;
1720 gen6_write_page_range(dev_priv, &ppgtt->pd,
1721 0, ppgtt->base.total);
1725 i915_ggtt_flush(dev_priv);
1728 int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
1730 if (obj->has_dma_mapping)
1734 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1735 obj->pages->sgl, obj->pages->nents,
1736 PCI_DMA_BIDIRECTIONAL))
1743 static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
1748 iowrite32((u32)pte, addr);
1749 iowrite32(pte >> 32, addr + 4);
1753 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1756 unsigned int num_entries,
1757 enum i915_cache_level level, u32 unused)
1759 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1760 unsigned first_entry = start >> PAGE_SHIFT;
1761 gen8_pte_t __iomem *gtt_entries =
1762 (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1764 dma_addr_t addr = 0;
1766 for (i=0;i<num_entries;i++) {
1767 addr = VM_PAGE_TO_PHYS(pages[i]);
1768 gen8_set_pte(>t_entries[i],
1769 gen8_pte_encode(addr, level, true));
1773 * XXX: This serves as a posting read to make sure that the PTE has
1774 * actually been updated. There is some concern that even though
1775 * registers and PTEs are within the same BAR that they are potentially
1776 * of NUMA access patterns. Therefore, even with the way we assume
1777 * hardware should work, we must keep this posting read for paranoia.
1780 WARN_ON(readq(>t_entries[i-1])
1781 != gen8_pte_encode(addr, level, true));
1783 /* This next bit makes the above posting read even more important. We
1784 * want to flush the TLBs only after we're certain all the PTE updates
1787 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1788 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1792 * Binds an object into the global gtt with the specified cache level. The object
1793 * will be accessible to the GPU via commands whose operands reference offsets
1794 * within the global GTT as well as accessible by the GPU through the GMADR
1795 * mapped BAR (dev_priv->mm.gtt->gtt).
1797 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
1800 unsigned int num_entries,
1801 enum i915_cache_level level, u32 flags)
1803 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1804 unsigned first_entry = start >> PAGE_SHIFT;
1805 gen6_pte_t __iomem *gtt_entries =
1806 (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1808 dma_addr_t addr = 0; /* shut up gcc */
1810 for (i = 0; i < num_entries; i++) {
1811 addr = VM_PAGE_TO_PHYS(pages[i]);
1812 iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]);
1815 /* XXX: This serves as a posting read to make sure that the PTE has
1816 * actually been updated. There is some concern that even though
1817 * registers and PTEs are within the same BAR that they are potentially
1818 * of NUMA access patterns. Therefore, even with the way we assume
1819 * hardware should work, we must keep this posting read for paranoia.
1822 unsigned long gtt = readl(>t_entries[i-1]);
1823 WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
1826 /* This next bit makes the above posting read even more important. We
1827 * want to flush the TLBs only after we're certain all the PTE updates
1830 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1831 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1834 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1839 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1840 unsigned first_entry = start >> PAGE_SHIFT;
1841 unsigned num_entries = length >> PAGE_SHIFT;
1842 gen8_pte_t scratch_pte, __iomem *gtt_base =
1843 (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1844 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1847 if (WARN(num_entries > max_entries,
1848 "First entry = %d; Num entries = %d (max=%d)\n",
1849 first_entry, num_entries, max_entries))
1850 num_entries = max_entries;
1852 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1855 for (i = 0; i < num_entries; i++)
1856 gen8_set_pte(>t_base[i], scratch_pte);
1860 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
1865 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1866 unsigned first_entry = start >> PAGE_SHIFT;
1867 unsigned num_entries = length >> PAGE_SHIFT;
1868 gen6_pte_t scratch_pte, __iomem *gtt_base =
1869 (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1870 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1873 if (WARN(num_entries > max_entries,
1874 "First entry = %d; Num entries = %d (max=%d)\n",
1875 first_entry, num_entries, max_entries))
1876 num_entries = max_entries;
1878 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
1880 for (i = 0; i < num_entries; i++)
1881 iowrite32(scratch_pte, >t_base[i]);
1885 static void i915_ggtt_bind_vma(struct i915_vma *vma,
1886 enum i915_cache_level cache_level,
1889 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1890 const unsigned int num_entries = vma->obj->base.size >> PAGE_SHIFT;
1891 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1892 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1894 BUG_ON(!i915_is_ggtt(vma->vm));
1895 intel_gtt_insert_pages(entry, num_entries, vma->ggtt_view.pages, flags);
1896 vma->bound = GLOBAL_BIND;
1899 static void i915_ggtt_clear_range(struct i915_address_space *vm,
1904 unsigned first_entry = start >> PAGE_SHIFT;
1905 unsigned num_entries = length >> PAGE_SHIFT;
1906 intel_gtt_clear_range(first_entry, num_entries);
1909 static void i915_ggtt_unbind_vma(struct i915_vma *vma)
1911 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1912 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
1914 BUG_ON(!i915_is_ggtt(vma->vm));
1916 intel_gtt_clear_range(first, size);
1919 static void ggtt_bind_vma(struct i915_vma *vma,
1920 enum i915_cache_level cache_level,
1923 struct drm_device *dev = vma->vm->dev;
1924 struct drm_i915_private *dev_priv = dev->dev_private;
1925 struct drm_i915_gem_object *obj = vma->obj;
1926 struct vm_page **pages = obj->pages;
1928 /* Currently applicable only to VLV */
1930 flags |= PTE_READ_ONLY;
1932 if (i915_is_ggtt(vma->vm))
1933 pages = vma->ggtt_view.pages;
1935 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1936 * or we have a global mapping already but the cacheability flags have
1937 * changed, set the global PTEs.
1939 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1940 * instead if none of the above hold true.
1942 * NB: A global mapping should only be needed for special regions like
1943 * "gtt mappable", SNB errata, or if specified via special execbuf
1944 * flags. At all other times, the GPU will use the aliasing PPGTT.
1946 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1947 if (!(vma->bound & GLOBAL_BIND) ||
1948 (cache_level != obj->cache_level)) {
1949 vma->vm->insert_entries(vma->vm, pages,
1951 obj->base.size >> PAGE_SHIFT,
1952 cache_level, flags);
1953 vma->bound |= GLOBAL_BIND;
1957 if (dev_priv->mm.aliasing_ppgtt &&
1958 (!(vma->bound & LOCAL_BIND) ||
1959 (cache_level != obj->cache_level))) {
1960 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1961 appgtt->base.insert_entries(&appgtt->base, pages,
1963 obj->base.size >> PAGE_SHIFT,
1964 cache_level, flags);
1965 vma->bound |= LOCAL_BIND;
1969 static void ggtt_unbind_vma(struct i915_vma *vma)
1971 struct drm_device *dev = vma->vm->dev;
1972 struct drm_i915_private *dev_priv = dev->dev_private;
1973 struct drm_i915_gem_object *obj = vma->obj;
1975 if (vma->bound & GLOBAL_BIND) {
1976 vma->vm->clear_range(vma->vm,
1980 vma->bound &= ~GLOBAL_BIND;
1983 if (vma->bound & LOCAL_BIND) {
1984 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1985 appgtt->base.clear_range(&appgtt->base,
1989 vma->bound &= ~LOCAL_BIND;
1993 void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1995 struct drm_device *dev = obj->base.dev;
1996 struct drm_i915_private *dev_priv = dev->dev_private;
1999 interruptible = do_idling(dev_priv);
2002 if (!obj->has_dma_mapping)
2003 dma_unmap_sg(&dev->pdev->dev,
2004 obj->pages->sgl, obj->pages->nents,
2005 PCI_DMA_BIDIRECTIONAL);
2008 undo_idling(dev_priv, interruptible);
2011 static void i915_gtt_color_adjust(struct drm_mm_node *node,
2012 unsigned long color,
2016 if (node->color != color)
2019 if (!list_empty(&node->node_list)) {
2020 node = list_entry(node->node_list.next,
2023 if (node->allocated && node->color != color)
2028 static int i915_gem_setup_global_gtt(struct drm_device *dev,
2029 unsigned long start,
2030 unsigned long mappable_end,
2033 /* Let GEM Manage all of the aperture.
2035 * However, leave one page at the end still bound to the scratch page.
2036 * There are a number of places where the hardware apparently prefetches
2037 * past the end of the object, and we've seen multiple hangs with the
2038 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2039 * aperture. One page should be enough to keep any prefetching inside
2042 struct drm_i915_private *dev_priv = dev->dev_private;
2043 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
2044 unsigned long mappable;
2046 struct drm_mm_node *entry;
2047 struct drm_i915_gem_object *obj;
2048 unsigned long hole_start, hole_end;
2051 kprintf("MAPPABLE_END VS END %016jx %016jx\n", mappable_end, end);
2052 tsleep(&mappable_end, 0, "DELAY", hz); /* for kprintf */
2053 /*BUG_ON(mappable_end > end);*/
2055 mappable = min(end, mappable_end) - start;
2057 /* Subtract the guard page ... */
2058 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
2060 dev_priv->gtt.base.start = start;
2061 dev_priv->gtt.base.total = end - start;
2063 if (intel_vgpu_active(dev)) {
2064 ret = intel_vgt_balloon(dev);
2070 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
2072 /* Mark any preallocated objects as occupied */
2073 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
2074 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
2076 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
2077 i915_gem_obj_ggtt_offset(obj), obj->base.size);
2079 WARN_ON(i915_gem_obj_ggtt_bound(obj));
2080 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
2082 DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
2085 vma->bound |= GLOBAL_BIND;
2088 /* Clear any non-preallocated blocks */
2089 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
2090 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2091 hole_start, hole_end);
2092 ggtt_vm->clear_range(ggtt_vm, hole_start,
2093 hole_end - hole_start, true);
2096 #ifdef __DragonFly__
2097 device_printf(dev->dev,
2098 "taking over the fictitious range 0x%lx-0x%lx\n",
2099 dev_priv->gtt.mappable_base + start, dev_priv->gtt.mappable_base + start + mappable);
2100 error = -vm_phys_fictitious_reg_range(dev_priv->gtt.mappable_base + start,
2101 dev_priv->gtt.mappable_base + start + mappable, VM_MEMATTR_WRITE_COMBINING);
2104 /* And finally clear the reserved guard page */
2105 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
2107 if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
2108 struct i915_hw_ppgtt *ppgtt;
2110 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
2114 ret = __hw_ppgtt_init(dev, ppgtt, true);
2120 dev_priv->mm.aliasing_ppgtt = ppgtt;
2126 void i915_gem_init_global_gtt(struct drm_device *dev)
2128 struct drm_i915_private *dev_priv = dev->dev_private;
2129 unsigned long gtt_size, mappable_size;
2131 gtt_size = dev_priv->gtt.base.total;
2132 mappable_size = dev_priv->gtt.mappable_end;
2134 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
2137 void i915_global_gtt_cleanup(struct drm_device *dev)
2139 struct drm_i915_private *dev_priv = dev->dev_private;
2140 struct i915_address_space *vm = &dev_priv->gtt.base;
2142 if (dev_priv->mm.aliasing_ppgtt) {
2143 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
2145 ppgtt->base.cleanup(&ppgtt->base);
2148 if (drm_mm_initialized(&vm->mm)) {
2149 if (intel_vgpu_active(dev))
2150 intel_vgt_deballoon();
2152 drm_mm_takedown(&vm->mm);
2153 list_del(&vm->global_link);
2159 static int setup_scratch_page(struct drm_device *dev)
2161 struct drm_i915_private *dev_priv = dev->dev_private;
2162 struct vm_page *page;
2163 dma_addr_t dma_addr;
2165 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
2168 set_pages_uc(page, 1);
2170 #ifdef CONFIG_INTEL_IOMMU
2171 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
2172 PCI_DMA_BIDIRECTIONAL);
2173 if (pci_dma_mapping_error(dev->pdev, dma_addr))
2176 dma_addr = page_to_phys(page);
2178 dev_priv->gtt.base.scratch.page = page;
2179 dev_priv->gtt.base.scratch.addr = dma_addr;
2185 static void teardown_scratch_page(struct drm_device *dev)
2187 struct drm_i915_private *dev_priv = dev->dev_private;
2188 struct vm_page *page = dev_priv->gtt.base.scratch.page;
2190 set_pages_wb(page, 1);
2191 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
2192 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
2197 static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
2199 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
2200 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
2201 return snb_gmch_ctl << 20;
2204 static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
2206 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
2207 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
2209 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
2211 #ifdef CONFIG_X86_32
2212 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2213 if (bdw_gmch_ctl > 4)
2217 return bdw_gmch_ctl << 20;
2220 static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
2222 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
2223 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
2226 return 1 << (20 + gmch_ctrl);
2231 static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
2233 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
2234 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
2235 return snb_gmch_ctl << 25; /* 32 MB units */
2238 static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
2240 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2241 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
2242 return bdw_gmch_ctl << 25; /* 32 MB units */
2245 static size_t chv_get_stolen_size(u16 gmch_ctrl)
2247 gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
2248 gmch_ctrl &= SNB_GMCH_GMS_MASK;
2251 * 0x0 to 0x10: 32MB increments starting at 0MB
2252 * 0x11 to 0x16: 4MB increments starting at 8MB
2253 * 0x17 to 0x1d: 4MB increments start at 36MB
2255 if (gmch_ctrl < 0x11)
2256 return gmch_ctrl << 25;
2257 else if (gmch_ctrl < 0x17)
2258 return (gmch_ctrl - 0x11 + 2) << 22;
2260 return (gmch_ctrl - 0x17 + 9) << 22;
2263 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
2265 gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
2266 gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
2268 if (gen9_gmch_ctl < 0xf0)
2269 return gen9_gmch_ctl << 25; /* 32 MB units */
2271 /* 4MB increments starting at 0xf0 for 4MB */
2272 return (gen9_gmch_ctl - 0xf0 + 1) << 22;
2275 static int ggtt_probe_common(struct drm_device *dev,
2278 struct drm_i915_private *dev_priv = dev->dev_private;
2279 phys_addr_t gtt_phys_addr;
2282 /* For Modern GENs the PTEs and register space are split in the BAR */
2283 gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
2284 (pci_resource_len(dev->pdev, 0) / 2);
2286 dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
2287 if (!dev_priv->gtt.gsm) {
2288 DRM_ERROR("Failed to map the gtt page table\n");
2292 ret = setup_scratch_page(dev);
2294 DRM_ERROR("Scratch setup failed\n");
2295 /* iounmap will also get called at remove, but meh */
2297 iounmap(dev_priv->gtt.gsm);
2304 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
2305 * bits. When using advanced contexts each context stores its own PAT, but
2306 * writing this data shouldn't be harmful even in those cases. */
2307 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
2311 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
2312 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
2313 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
2314 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
2315 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
2316 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
2317 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
2318 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
2320 if (!USES_PPGTT(dev_priv->dev))
2321 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
2322 * so RTL will always use the value corresponding to
2324 * So let's disable cache for GGTT to avoid screen corruptions.
2325 * MOCS still can be used though.
2326 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
2327 * before this patch, i.e. the same uncached + snooping access
2328 * like on gen6/7 seems to be in effect.
2329 * - So this just fixes blitter/render access. Again it looks
2330 * like it's not just uncached access, but uncached + snooping.
2331 * So we can still hold onto all our assumptions wrt cpu
2332 * clflushing on LLC machines.
2334 pat = GEN8_PPAT(0, GEN8_PPAT_UC);
2336 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
2337 * write would work. */
2338 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2339 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2342 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
2347 * Map WB on BDW to snooped on CHV.
2349 * Only the snoop bit has meaning for CHV, the rest is
2352 * The hardware will never snoop for certain types of accesses:
2353 * - CPU GTT (GMADR->GGTT->no snoop->memory)
2354 * - PPGTT page tables
2355 * - some other special cycles
2357 * As with BDW, we also need to consider the following for GT accesses:
2358 * "For GGTT, there is NO pat_sel[2:0] from the entry,
2359 * so RTL will always use the value corresponding to
2361 * Which means we must set the snoop bit in PAT entry 0
2362 * in order to keep the global status page working.
2364 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
2368 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
2369 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
2370 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
2371 GEN8_PPAT(7, CHV_PPAT_SNOOP);
2373 I915_WRITE(GEN8_PRIVATE_PAT, pat);
2374 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
2377 static int gen8_gmch_probe(struct drm_device *dev,
2380 phys_addr_t *mappable_base,
2381 unsigned long *mappable_end)
2383 struct drm_i915_private *dev_priv = dev->dev_private;
2384 unsigned int gtt_size;
2388 /* TODO: We're not aware of mappable constraints on gen8 yet */
2389 *mappable_base = pci_resource_start(dev->pdev, 2);
2390 *mappable_end = pci_resource_len(dev->pdev, 2);
2393 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
2394 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
2397 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2399 if (INTEL_INFO(dev)->gen >= 9) {
2400 *stolen = gen9_get_stolen_size(snb_gmch_ctl);
2401 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2402 } else if (IS_CHERRYVIEW(dev)) {
2403 *stolen = chv_get_stolen_size(snb_gmch_ctl);
2404 gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
2406 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
2407 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
2410 *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
2412 if (IS_CHERRYVIEW(dev))
2413 chv_setup_private_ppat(dev_priv);
2415 bdw_setup_private_ppat(dev_priv);
2417 ret = ggtt_probe_common(dev, gtt_size);
2419 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
2420 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
2425 static int gen6_gmch_probe(struct drm_device *dev,
2428 phys_addr_t *mappable_base,
2429 unsigned long *mappable_end)
2431 struct drm_i915_private *dev_priv = dev->dev_private;
2432 unsigned int gtt_size;
2436 *mappable_base = pci_resource_start(dev->pdev, 2);
2437 *mappable_end = pci_resource_len(dev->pdev, 2);
2439 /* 64/512MB is the current min/max we actually know of, but this is just
2440 * a coarse sanity check.
2442 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
2443 DRM_ERROR("Unknown GMADR size (%lx)\n",
2444 dev_priv->gtt.mappable_end);
2449 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
2450 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
2452 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
2454 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
2456 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
2457 *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
2459 ret = ggtt_probe_common(dev, gtt_size);
2461 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
2462 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
2467 static void gen6_gmch_remove(struct i915_address_space *vm)
2470 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
2473 teardown_scratch_page(vm->dev);
2477 static int i915_gmch_probe(struct drm_device *dev,
2480 phys_addr_t *mappable_base,
2481 unsigned long *mappable_end)
2483 struct drm_i915_private *dev_priv = dev->dev_private;
2487 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
2489 DRM_ERROR("failed to set up gmch\n");
2494 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
2496 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
2497 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
2499 if (unlikely(dev_priv->gtt.do_idle_maps))
2500 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
2505 static void i915_gmch_remove(struct i915_address_space *vm)
2507 intel_gmch_remove();
2510 int i915_gem_gtt_init(struct drm_device *dev)
2512 struct drm_i915_private *dev_priv = dev->dev_private;
2513 struct i915_gtt *gtt = &dev_priv->gtt;
2516 if (INTEL_INFO(dev)->gen <= 5) {
2517 gtt->gtt_probe = i915_gmch_probe;
2518 gtt->base.cleanup = i915_gmch_remove;
2519 } else if (INTEL_INFO(dev)->gen < 8) {
2520 gtt->gtt_probe = gen6_gmch_probe;
2521 gtt->base.cleanup = gen6_gmch_remove;
2522 if (IS_HASWELL(dev) && dev_priv->ellc_size)
2523 gtt->base.pte_encode = iris_pte_encode;
2524 else if (IS_HASWELL(dev))
2525 gtt->base.pte_encode = hsw_pte_encode;
2526 else if (IS_VALLEYVIEW(dev))
2527 gtt->base.pte_encode = byt_pte_encode;
2528 else if (INTEL_INFO(dev)->gen >= 7)
2529 gtt->base.pte_encode = ivb_pte_encode;
2531 gtt->base.pte_encode = snb_pte_encode;
2533 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
2534 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
2537 ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size,
2538 >t->mappable_base, >t->mappable_end);
2542 gtt->base.dev = dev;
2544 /* GMADR is the PCI mmio aperture into the global GTT. */
2545 DRM_INFO("Memory usable by graphics device = %zdM\n",
2546 gtt->base.total >> 20);
2547 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
2548 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
2549 #ifdef CONFIG_INTEL_IOMMU
2550 if (intel_iommu_gfx_mapped)
2551 DRM_INFO("VT-d active for gfx access\n");
2554 * i915.enable_ppgtt is read-only, so do an early pass to validate the
2555 * user's requested state against the hardware/driver capabilities. We
2556 * do this now so that we can print out any log messages once rather
2557 * than every time we check intel_enable_ppgtt().
2559 i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
2560 DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
2565 static struct i915_vma *
2566 __i915_gem_vma_create(struct drm_i915_gem_object *obj,
2567 struct i915_address_space *vm,
2568 const struct i915_ggtt_view *ggtt_view)
2570 struct i915_vma *vma;
2572 if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
2573 return ERR_PTR(-EINVAL);
2574 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
2576 return ERR_PTR(-ENOMEM);
2578 INIT_LIST_HEAD(&vma->vma_link);
2579 INIT_LIST_HEAD(&vma->mm_list);
2580 INIT_LIST_HEAD(&vma->exec_list);
2584 if (INTEL_INFO(vm->dev)->gen >= 6) {
2585 if (i915_is_ggtt(vm)) {
2586 vma->ggtt_view = *ggtt_view;
2588 vma->unbind_vma = ggtt_unbind_vma;
2589 vma->bind_vma = ggtt_bind_vma;
2591 vma->unbind_vma = ppgtt_unbind_vma;
2592 vma->bind_vma = ppgtt_bind_vma;
2595 BUG_ON(!i915_is_ggtt(vm));
2596 vma->ggtt_view = *ggtt_view;
2597 vma->unbind_vma = i915_ggtt_unbind_vma;
2598 vma->bind_vma = i915_ggtt_bind_vma;
2601 list_add_tail(&vma->vma_link, &obj->vma_list);
2602 if (!i915_is_ggtt(vm))
2603 i915_ppgtt_get(i915_vm_to_ppgtt(vm));
2609 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
2610 struct i915_address_space *vm)
2612 struct i915_vma *vma;
2614 vma = i915_gem_obj_to_vma(obj, vm);
2616 vma = __i915_gem_vma_create(obj, vm,
2617 i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
2623 i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
2624 const struct i915_ggtt_view *view)
2626 struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
2627 struct i915_vma *vma;
2630 return ERR_PTR(-EINVAL);
2632 vma = i915_gem_obj_to_ggtt_view(obj, view);
2638 vma = __i915_gem_vma_create(obj, ggtt, view);
2646 rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
2647 struct sg_table *st)
2649 unsigned int column, row;
2650 unsigned int src_idx;
2651 struct scatterlist *sg = st->sgl;
2655 for (column = 0; column < width; column++) {
2656 src_idx = width * (height - 1) + column;
2657 for (row = 0; row < height; row++) {
2659 /* We don't need the pages, but need to initialize
2660 * the entries so the sg list can be happily traversed.
2661 * The only thing we need are DMA addresses.
2663 sg_set_page(sg, NULL, PAGE_SIZE, 0);
2664 sg_dma_address(sg) = in[src_idx];
2665 sg_dma_len(sg) = PAGE_SIZE;
2672 static struct sg_table *
2673 intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
2674 struct drm_i915_gem_object *obj)
2676 struct drm_device *dev = obj->base.dev;
2677 struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
2678 unsigned long size, pages, rot_pages;
2679 struct sg_page_iter sg_iter;
2681 dma_addr_t *page_addr_list;
2682 struct sg_table *st;
2683 unsigned int tile_pitch, tile_height;
2684 unsigned int width_pages, height_pages;
2687 pages = obj->base.size / PAGE_SIZE;
2689 /* Calculate tiling geometry. */
2690 tile_height = intel_tile_height(dev, rot_info->pixel_format,
2691 rot_info->fb_modifier);
2692 tile_pitch = PAGE_SIZE / tile_height;
2693 width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
2694 height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
2695 rot_pages = width_pages * height_pages;
2696 size = rot_pages * PAGE_SIZE;
2698 /* Allocate a temporary list of source pages for random access. */
2699 page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
2700 if (!page_addr_list)
2701 return ERR_PTR(ret);
2703 /* Allocate target SG list. */
2704 st = kmalloc(sizeof(*st), GFP_KERNEL);
2708 ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
2712 /* Populate source page list from the object. */
2714 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
2715 page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
2719 /* Rotate the pages. */
2720 rotate_pages(page_addr_list, width_pages, height_pages, st);
2723 "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
2724 size, rot_info->pitch, rot_info->height,
2725 rot_info->pixel_format, width_pages, height_pages,
2728 drm_free_large(page_addr_list);
2735 drm_free_large(page_addr_list);
2738 "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
2739 size, ret, rot_info->pitch, rot_info->height,
2740 rot_info->pixel_format, width_pages, height_pages,
2742 return ERR_PTR(ret);
2747 i915_get_ggtt_vma_pages(struct i915_vma *vma)
2751 if (vma->ggtt_view.pages)
2754 if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
2755 vma->ggtt_view.pages = vma->obj->pages;
2757 else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
2758 vma->ggtt_view.pages =
2759 intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
2762 WARN_ONCE(1, "GGTT view %u not implemented!\n",
2763 vma->ggtt_view.type);
2765 if (!vma->ggtt_view.pages) {
2766 DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
2767 vma->ggtt_view.type);
2769 } else if (IS_ERR(vma->ggtt_view.pages)) {
2770 ret = PTR_ERR(vma->ggtt_view.pages);
2771 vma->ggtt_view.pages = NULL;
2772 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
2773 vma->ggtt_view.type, ret);
2780 * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
2782 * @cache_level: mapping cache level
2783 * @flags: flags like global or local mapping
2785 * DMA addresses are taken from the scatter-gather table of this object (or of
2786 * this VMA in case of non-default GGTT views) and PTE entries set up.
2787 * Note that DMA addresses are also the only part of the SG table we care about.
2789 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2792 if (i915_is_ggtt(vma->vm)) {
2793 int ret = i915_get_ggtt_vma_pages(vma);
2799 vma->bind_vma(vma, cache_level, flags);