2 * Copyright © 2010 Daniel Vetter
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <linux/seq_file.h>
27 #include <drm/i915_drm.h>
29 #include "i915_trace.h"
30 #include "intel_drv.h"
32 #define GEN6_PPGTT_PD_ENTRIES 512
33 #define I915_PPGTT_PT_ENTRIES (PAGE_SIZE / sizeof(gen6_gtt_pte_t))
34 typedef uint64_t gen8_gtt_pte_t;
35 typedef gen8_gtt_pte_t gen8_ppgtt_pde_t;
38 #define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
39 #define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
41 #define GEN6_PDE_VALID (1 << 0)
42 /* gen6+ has bit 11-4 for physical addr bit 39-32 */
43 #define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
45 #define GEN6_PTE_VALID (1 << 0)
46 #define GEN6_PTE_UNCACHED (1 << 1)
47 #define HSW_PTE_UNCACHED (0)
48 #define GEN6_PTE_CACHE_LLC (2 << 1)
49 #define GEN7_PTE_CACHE_L3_LLC (3 << 1)
50 #define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
51 #define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
53 /* Cacheability Control is a 4-bit value. The low three bits are stored in *
54 * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
56 #define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
57 (((bits) & 0x8) << (11 - 3)))
58 #define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
59 #define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
60 #define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
61 #define HSW_WB_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x8)
62 #define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
63 #define HSW_WT_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x7)
65 #define GEN8_PTES_PER_PAGE (PAGE_SIZE / sizeof(gen8_gtt_pte_t))
66 #define GEN8_PDES_PER_PAGE (PAGE_SIZE / sizeof(gen8_ppgtt_pde_t))
67 #define GEN8_LEGACY_PDPS 4
69 #define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD)
70 #define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */
71 #define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */
72 #define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */
74 static void ppgtt_bind_vma(struct i915_vma *vma,
75 enum i915_cache_level cache_level,
77 static void ppgtt_unbind_vma(struct i915_vma *vma);
78 static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
80 static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
81 enum i915_cache_level level,
84 gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
86 if (level != I915_CACHE_NONE)
87 pte |= PPAT_CACHED_INDEX;
89 pte |= PPAT_UNCACHED_INDEX;
93 static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
95 enum i915_cache_level level)
97 gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
99 if (level != I915_CACHE_NONE)
100 pde |= PPAT_CACHED_PDE_INDEX;
102 pde |= PPAT_UNCACHED_INDEX;
106 static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
107 enum i915_cache_level level,
110 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
111 pte |= GEN6_PTE_ADDR_ENCODE(addr);
114 case I915_CACHE_L3_LLC:
116 pte |= GEN6_PTE_CACHE_LLC;
118 case I915_CACHE_NONE:
119 pte |= GEN6_PTE_UNCACHED;
128 static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
129 enum i915_cache_level level,
132 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
133 pte |= GEN6_PTE_ADDR_ENCODE(addr);
136 case I915_CACHE_L3_LLC:
137 pte |= GEN7_PTE_CACHE_L3_LLC;
140 pte |= GEN6_PTE_CACHE_LLC;
142 case I915_CACHE_NONE:
143 pte |= GEN6_PTE_UNCACHED;
152 #define BYT_PTE_WRITEABLE (1 << 1)
153 #define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2)
155 static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
156 enum i915_cache_level level,
159 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
160 pte |= GEN6_PTE_ADDR_ENCODE(addr);
162 /* Mark the page as writeable. Other platforms don't have a
163 * setting for read-only/writable, so this matches that behavior.
165 pte |= BYT_PTE_WRITEABLE;
167 if (level != I915_CACHE_NONE)
168 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
173 static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
174 enum i915_cache_level level,
177 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
178 pte |= HSW_PTE_ADDR_ENCODE(addr);
180 if (level != I915_CACHE_NONE)
181 pte |= HSW_WB_LLC_AGE3;
186 static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
187 enum i915_cache_level level,
190 gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
191 pte |= HSW_PTE_ADDR_ENCODE(addr);
194 case I915_CACHE_NONE:
197 pte |= HSW_WT_ELLC_LLC_AGE3;
200 pte |= HSW_WB_ELLC_LLC_AGE3;
207 /* Broadwell Page Directory Pointer Descriptors */
208 static int gen8_write_pdp(struct intel_ring_buffer *ring, unsigned entry,
209 uint64_t val, bool synchronous)
211 struct drm_i915_private *dev_priv = ring->dev->dev_private;
217 I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
218 I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
222 ret = intel_ring_begin(ring, 6);
226 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
227 intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
228 intel_ring_emit(ring, (u32)(val >> 32));
229 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
230 intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
231 intel_ring_emit(ring, (u32)(val));
232 intel_ring_advance(ring);
237 static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
238 struct intel_ring_buffer *ring,
243 /* bit of a hack to find the actual last used pd */
244 int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
246 for (i = used_pd - 1; i >= 0; i--) {
247 dma_addr_t addr = ppgtt->pd_dma_addr[i];
248 ret = gen8_write_pdp(ring, i, addr, synchronous);
256 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
261 struct i915_hw_ppgtt *ppgtt =
262 container_of(vm, struct i915_hw_ppgtt, base);
263 gen8_gtt_pte_t *pt_vaddr, scratch_pte;
264 unsigned first_entry = start >> PAGE_SHIFT;
265 unsigned num_entries = length >> PAGE_SHIFT;
266 unsigned act_pt = first_entry / GEN8_PTES_PER_PAGE;
267 unsigned first_pte = first_entry % GEN8_PTES_PER_PAGE;
268 unsigned last_pte, i;
270 scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
271 I915_CACHE_LLC, use_scratch);
273 while (num_entries) {
274 struct page *page_table = &ppgtt->gen8_pt_pages[act_pt];
276 last_pte = first_pte + num_entries;
277 if (last_pte > GEN8_PTES_PER_PAGE)
278 last_pte = GEN8_PTES_PER_PAGE;
280 pt_vaddr = kmap_atomic(page_table);
282 for (i = first_pte; i < last_pte; i++)
283 pt_vaddr[i] = scratch_pte;
285 kunmap_atomic(pt_vaddr);
287 num_entries -= last_pte - first_pte;
293 static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
294 struct sg_table *pages,
296 enum i915_cache_level cache_level)
298 struct i915_hw_ppgtt *ppgtt =
299 container_of(vm, struct i915_hw_ppgtt, base);
300 gen8_gtt_pte_t *pt_vaddr;
301 unsigned first_entry = start >> PAGE_SHIFT;
302 unsigned act_pt = first_entry / GEN8_PTES_PER_PAGE;
303 unsigned act_pte = first_entry % GEN8_PTES_PER_PAGE;
304 struct sg_page_iter sg_iter;
307 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
308 if (pt_vaddr == NULL)
309 pt_vaddr = kmap_atomic(&ppgtt->gen8_pt_pages[act_pt]);
312 gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
314 if (++act_pte == GEN8_PTES_PER_PAGE) {
315 kunmap_atomic(pt_vaddr);
322 kunmap_atomic(pt_vaddr);
325 static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
329 for (i = 0; i < ppgtt->num_pd_pages ; i++)
330 kfree(ppgtt->gen8_pt_dma_addr[i]);
332 __free_pages(ppgtt->gen8_pt_pages, get_order(ppgtt->num_pt_pages << PAGE_SHIFT));
333 __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
336 static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
338 struct pci_dev *hwdev = ppgtt->base.dev->pdev;
341 for (i = 0; i < ppgtt->num_pd_pages; i++) {
342 /* TODO: In the future we'll support sparse mappings, so this
343 * will have to change. */
344 if (!ppgtt->pd_dma_addr[i])
347 pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
348 PCI_DMA_BIDIRECTIONAL);
350 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
351 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
353 pci_unmap_page(hwdev, addr, PAGE_SIZE,
354 PCI_DMA_BIDIRECTIONAL);
359 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
361 struct i915_hw_ppgtt *ppgtt =
362 container_of(vm, struct i915_hw_ppgtt, base);
364 list_del(&vm->global_link);
365 drm_mm_takedown(&vm->mm);
367 gen8_ppgtt_unmap_pages(ppgtt);
368 gen8_ppgtt_free(ppgtt);
371 static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
374 struct page *pt_pages;
375 const int num_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
377 pt_pages = alloc_pages(GFP_KERNEL, get_order(num_pt_pages << PAGE_SHIFT));
381 ppgtt->gen8_pt_pages = pt_pages;
382 ppgtt->num_pt_pages = 1 << get_order(num_pt_pages << PAGE_SHIFT);
387 static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
391 for (i = 0; i < ppgtt->num_pd_pages; i++) {
392 ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
395 if (!ppgtt->gen8_pt_dma_addr[i])
402 static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
405 ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
406 if (!ppgtt->pd_pages)
409 ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
410 BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
415 static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
420 ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
424 ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
426 __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
430 ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
432 ret = gen8_ppgtt_allocate_dma(ppgtt);
434 gen8_ppgtt_free(ppgtt);
439 static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
445 pd_addr = pci_map_page(ppgtt->base.dev->pdev,
446 &ppgtt->pd_pages[pd], 0,
447 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
449 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
453 ppgtt->pd_dma_addr[pd] = pd_addr;
458 static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
466 p = &ppgtt->gen8_pt_pages[pd * GEN8_PDES_PER_PAGE + pt];
467 pt_addr = pci_map_page(ppgtt->base.dev->pdev,
468 p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
469 ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
473 ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
479 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
480 * with a net effect resembling a 2-level page table in normal x86 terms. Each
481 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
484 * FIXME: split allocation into smaller pieces. For now we only ever do this
485 * once, but with full PPGTT, the multiple contiguous allocations will be bad.
486 * TODO: Do something with the size parameter
488 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
490 const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
491 const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
495 DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
497 /* 1. Do all our allocations for page directories and page tables. */
498 ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
503 * 2. Create DMA mappings for the page directories and page tables.
505 for (i = 0; i < max_pdp; i++) {
506 ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
510 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
511 ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
518 * 3. Map all the page directory entires to point to the page tables
521 * For now, the PPGTT helper functions all require that the PDEs are
522 * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
523 * will never need to touch the PDEs again.
525 for (i = 0; i < max_pdp; i++) {
526 gen8_ppgtt_pde_t *pd_vaddr;
527 pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
528 for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
529 dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
530 pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
533 kunmap_atomic(pd_vaddr);
536 ppgtt->enable = gen8_ppgtt_enable;
537 ppgtt->switch_mm = gen8_mm_switch;
538 ppgtt->base.clear_range = gen8_ppgtt_clear_range;
539 ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
540 ppgtt->base.cleanup = gen8_ppgtt_cleanup;
541 ppgtt->base.start = 0;
542 ppgtt->base.total = ppgtt->num_pt_pages * GEN8_PTES_PER_PAGE * PAGE_SIZE;
544 ppgtt->base.clear_range(&ppgtt->base, 0,
545 ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE,
548 DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
549 ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
550 DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
552 (ppgtt->num_pt_pages - min_pt_pages) +
557 gen8_ppgtt_unmap_pages(ppgtt);
558 gen8_ppgtt_free(ppgtt);
562 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
564 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
565 struct i915_address_space *vm = &ppgtt->base;
566 gen6_gtt_pte_t __iomem *pd_addr;
567 gen6_gtt_pte_t scratch_pte;
571 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
573 pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
574 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
576 seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
577 ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
578 for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
580 gen6_gtt_pte_t *pt_vaddr;
581 dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
582 pd_entry = readl(pd_addr + pde);
583 expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
585 if (pd_entry != expected)
586 seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
590 seq_printf(m, "\tPDE: %x\n", pd_entry);
592 pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
593 for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
595 (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
599 for (i = 0; i < 4; i++)
600 if (pt_vaddr[pte + i] != scratch_pte)
605 seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
606 for (i = 0; i < 4; i++) {
607 if (pt_vaddr[pte + i] != scratch_pte)
608 seq_printf(m, " %08x", pt_vaddr[pte + i]);
610 seq_puts(m, " SCRATCH ");
614 kunmap_atomic(pt_vaddr);
618 static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
620 struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
621 gen6_gtt_pte_t __iomem *pd_addr;
625 WARN_ON(ppgtt->pd_offset & 0x3f);
626 pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
627 ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
628 for (i = 0; i < ppgtt->num_pd_entries; i++) {
631 pt_addr = ppgtt->pt_dma_addr[i];
632 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
633 pd_entry |= GEN6_PDE_VALID;
635 writel(pd_entry, pd_addr + i);
640 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
642 BUG_ON(ppgtt->pd_offset & 0x3f);
644 return (ppgtt->pd_offset / 64) << 16;
647 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
648 struct intel_ring_buffer *ring,
651 struct drm_device *dev = ppgtt->base.dev;
652 struct drm_i915_private *dev_priv = dev->dev_private;
655 /* If we're in reset, we can assume the GPU is sufficiently idle to
656 * manually frob these bits. Ideally we could use the ring functions,
657 * except our error handling makes it quite difficult (can't use
658 * intel_ring_begin, ring->flush, or intel_ring_advance)
660 * FIXME: We should try not to special case reset
663 i915_reset_in_progress(&dev_priv->gpu_error)) {
664 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
665 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
666 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
667 POSTING_READ(RING_PP_DIR_BASE(ring));
671 /* NB: TLBs must be flushed and invalidated before a switch */
672 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
676 ret = intel_ring_begin(ring, 6);
680 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
681 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
682 intel_ring_emit(ring, PP_DIR_DCLV_2G);
683 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
684 intel_ring_emit(ring, get_pd_offset(ppgtt));
685 intel_ring_emit(ring, MI_NOOP);
686 intel_ring_advance(ring);
691 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
692 struct intel_ring_buffer *ring,
695 struct drm_device *dev = ppgtt->base.dev;
696 struct drm_i915_private *dev_priv = dev->dev_private;
699 /* If we're in reset, we can assume the GPU is sufficiently idle to
700 * manually frob these bits. Ideally we could use the ring functions,
701 * except our error handling makes it quite difficult (can't use
702 * intel_ring_begin, ring->flush, or intel_ring_advance)
704 * FIXME: We should try not to special case reset
707 i915_reset_in_progress(&dev_priv->gpu_error)) {
708 WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
709 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
710 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
711 POSTING_READ(RING_PP_DIR_BASE(ring));
715 /* NB: TLBs must be flushed and invalidated before a switch */
716 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
720 ret = intel_ring_begin(ring, 6);
724 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
725 intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
726 intel_ring_emit(ring, PP_DIR_DCLV_2G);
727 intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
728 intel_ring_emit(ring, get_pd_offset(ppgtt));
729 intel_ring_emit(ring, MI_NOOP);
730 intel_ring_advance(ring);
732 /* XXX: RCS is the only one to auto invalidate the TLBs? */
733 if (ring->id != RCS) {
734 ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
742 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
743 struct intel_ring_buffer *ring,
746 struct drm_device *dev = ppgtt->base.dev;
747 struct drm_i915_private *dev_priv = dev->dev_private;
752 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
753 I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
755 POSTING_READ(RING_PP_DIR_DCLV(ring));
760 static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
762 struct drm_device *dev = ppgtt->base.dev;
763 struct drm_i915_private *dev_priv = dev->dev_private;
764 struct intel_ring_buffer *ring;
767 for_each_ring(ring, dev_priv, j) {
768 I915_WRITE(RING_MODE_GEN7(ring),
769 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
771 /* We promise to do a switch later with FULL PPGTT. If this is
772 * aliasing, this is the one and only switch we'll do */
773 if (USES_FULL_PPGTT(dev))
776 ret = ppgtt->switch_mm(ppgtt, ring, true);
784 for_each_ring(ring, dev_priv, j)
785 I915_WRITE(RING_MODE_GEN7(ring),
786 _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
790 static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
792 struct drm_device *dev = ppgtt->base.dev;
793 drm_i915_private_t *dev_priv = dev->dev_private;
794 struct intel_ring_buffer *ring;
795 uint32_t ecochk, ecobits;
798 ecobits = I915_READ(GAC_ECO_BITS);
799 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
801 ecochk = I915_READ(GAM_ECOCHK);
802 if (IS_HASWELL(dev)) {
803 ecochk |= ECOCHK_PPGTT_WB_HSW;
805 ecochk |= ECOCHK_PPGTT_LLC_IVB;
806 ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
808 I915_WRITE(GAM_ECOCHK, ecochk);
810 for_each_ring(ring, dev_priv, i) {
812 /* GFX_MODE is per-ring on gen7+ */
813 I915_WRITE(RING_MODE_GEN7(ring),
814 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
816 /* We promise to do a switch later with FULL PPGTT. If this is
817 * aliasing, this is the one and only switch we'll do */
818 if (USES_FULL_PPGTT(dev))
821 ret = ppgtt->switch_mm(ppgtt, ring, true);
829 static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
831 struct drm_device *dev = ppgtt->base.dev;
832 drm_i915_private_t *dev_priv = dev->dev_private;
833 struct intel_ring_buffer *ring;
834 uint32_t ecochk, gab_ctl, ecobits;
837 ecobits = I915_READ(GAC_ECO_BITS);
838 I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
839 ECOBITS_PPGTT_CACHE64B);
841 gab_ctl = I915_READ(GAB_CTL);
842 I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
844 ecochk = I915_READ(GAM_ECOCHK);
845 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
847 I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
849 for_each_ring(ring, dev_priv, i) {
850 int ret = ppgtt->switch_mm(ppgtt, ring, true);
858 /* PPGTT support for Sandybdrige/Gen6 and later */
859 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
864 struct i915_hw_ppgtt *ppgtt =
865 container_of(vm, struct i915_hw_ppgtt, base);
866 gen6_gtt_pte_t *pt_vaddr, scratch_pte;
867 unsigned first_entry = start >> PAGE_SHIFT;
868 unsigned num_entries = length >> PAGE_SHIFT;
869 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
870 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
871 unsigned last_pte, i;
873 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true);
875 while (num_entries) {
876 last_pte = first_pte + num_entries;
877 if (last_pte > I915_PPGTT_PT_ENTRIES)
878 last_pte = I915_PPGTT_PT_ENTRIES;
880 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
882 for (i = first_pte; i < last_pte; i++)
883 pt_vaddr[i] = scratch_pte;
885 kunmap_atomic(pt_vaddr);
887 num_entries -= last_pte - first_pte;
893 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
894 struct sg_table *pages,
896 enum i915_cache_level cache_level)
898 struct i915_hw_ppgtt *ppgtt =
899 container_of(vm, struct i915_hw_ppgtt, base);
900 gen6_gtt_pte_t *pt_vaddr;
901 unsigned first_entry = start >> PAGE_SHIFT;
902 unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
903 unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
904 struct sg_page_iter sg_iter;
907 for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
908 if (pt_vaddr == NULL)
909 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
912 vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
914 if (++act_pte == I915_PPGTT_PT_ENTRIES) {
915 kunmap_atomic(pt_vaddr);
922 kunmap_atomic(pt_vaddr);
925 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
927 struct i915_hw_ppgtt *ppgtt =
928 container_of(vm, struct i915_hw_ppgtt, base);
931 list_del(&vm->global_link);
932 drm_mm_takedown(&ppgtt->base.mm);
933 drm_mm_remove_node(&ppgtt->node);
935 if (ppgtt->pt_dma_addr) {
936 for (i = 0; i < ppgtt->num_pd_entries; i++)
937 pci_unmap_page(ppgtt->base.dev->pdev,
938 ppgtt->pt_dma_addr[i],
939 4096, PCI_DMA_BIDIRECTIONAL);
942 kfree(ppgtt->pt_dma_addr);
943 for (i = 0; i < ppgtt->num_pd_entries; i++)
944 __free_page(ppgtt->pt_pages[i]);
945 kfree(ppgtt->pt_pages);
948 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
950 #define GEN6_PD_ALIGN (PAGE_SIZE * 16)
951 #define GEN6_PD_SIZE (GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE)
952 struct drm_device *dev = ppgtt->base.dev;
953 struct drm_i915_private *dev_priv = dev->dev_private;
954 bool retried = false;
957 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
958 * allocator works in address space sizes, so it's multiplied by page
959 * size. We allocate at the top of the GTT to avoid fragmentation.
961 BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
963 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
964 &ppgtt->node, GEN6_PD_SIZE,
966 0, dev_priv->gtt.base.total,
967 DRM_MM_SEARCH_DEFAULT);
968 if (ret == -ENOSPC && !retried) {
969 ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
970 GEN6_PD_SIZE, GEN6_PD_ALIGN,
979 if (ppgtt->node.start < dev_priv->gtt.mappable_end)
980 DRM_DEBUG("Forced to use aperture for PDEs\n");
982 ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
983 ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
985 ppgtt->enable = gen6_ppgtt_enable;
986 ppgtt->switch_mm = gen6_mm_switch;
987 } else if (IS_HASWELL(dev)) {
988 ppgtt->enable = gen7_ppgtt_enable;
989 ppgtt->switch_mm = hsw_mm_switch;
990 } else if (IS_GEN7(dev)) {
991 ppgtt->enable = gen7_ppgtt_enable;
992 ppgtt->switch_mm = gen7_mm_switch;
995 ppgtt->base.clear_range = gen6_ppgtt_clear_range;
996 ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
997 ppgtt->base.cleanup = gen6_ppgtt_cleanup;
998 ppgtt->base.scratch = dev_priv->gtt.base.scratch;
999 ppgtt->base.start = 0;
1000 ppgtt->base.total = GEN6_PPGTT_PD_ENTRIES * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
1001 ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
1003 if (!ppgtt->pt_pages) {
1004 drm_mm_remove_node(&ppgtt->node);
1008 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1009 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
1010 if (!ppgtt->pt_pages[i])
1014 ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
1016 if (!ppgtt->pt_dma_addr)
1019 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1022 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
1023 PCI_DMA_BIDIRECTIONAL);
1025 if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
1030 ppgtt->pt_dma_addr[i] = pt_addr;
1033 ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
1034 ppgtt->debug_dump = gen6_dump_ppgtt;
1036 DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
1037 ppgtt->node.size >> 20,
1038 ppgtt->node.start / PAGE_SIZE);
1040 ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
1045 if (ppgtt->pt_dma_addr) {
1046 for (i--; i >= 0; i--)
1047 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
1048 4096, PCI_DMA_BIDIRECTIONAL);
1051 kfree(ppgtt->pt_dma_addr);
1052 for (i = 0; i < ppgtt->num_pd_entries; i++) {
1053 if (ppgtt->pt_pages[i])
1054 __free_page(ppgtt->pt_pages[i]);
1056 kfree(ppgtt->pt_pages);
1057 drm_mm_remove_node(&ppgtt->node);
1062 int i915_gem_init_ppgtt(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1067 ppgtt->base.dev = dev;
1069 if (INTEL_INFO(dev)->gen < 8)
1070 ret = gen6_ppgtt_init(ppgtt);
1071 else if (IS_GEN8(dev))
1072 ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
1077 struct drm_i915_private *dev_priv = dev->dev_private;
1078 kref_init(&ppgtt->ref);
1079 drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
1081 i915_init_vm(dev_priv, &ppgtt->base);
1082 if (INTEL_INFO(dev)->gen < 8) {
1083 gen6_write_pdes(ppgtt);
1084 DRM_DEBUG("Adding PPGTT at offset %x\n",
1085 ppgtt->pd_offset << 10);
1093 ppgtt_bind_vma(struct i915_vma *vma,
1094 enum i915_cache_level cache_level,
1099 vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
1103 static void ppgtt_unbind_vma(struct i915_vma *vma)
1105 vma->vm->clear_range(vma->vm,
1107 vma->obj->base.size,
1111 extern int intel_iommu_gfx_mapped;
1112 /* Certain Gen5 chipsets require require idling the GPU before
1113 * unmapping anything from the GTT when VT-d is enabled.
1115 static inline bool needs_idle_maps(struct drm_device *dev)
1117 #ifdef CONFIG_INTEL_IOMMU
1118 /* Query intel_iommu to see if we need the workaround. Presumably that
1121 if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
1127 static bool do_idling(struct drm_i915_private *dev_priv)
1129 bool ret = dev_priv->mm.interruptible;
1131 if (unlikely(dev_priv->gtt.do_idle_maps)) {
1132 dev_priv->mm.interruptible = false;
1133 if (i915_gpu_idle(dev_priv->dev)) {
1134 DRM_ERROR("Couldn't idle GPU\n");
1135 /* Wait a bit, in hopes it avoids the hang */
1143 static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
1145 if (unlikely(dev_priv->gtt.do_idle_maps))
1146 dev_priv->mm.interruptible = interruptible;
1149 void i915_check_and_clear_faults(struct drm_device *dev)
1151 struct drm_i915_private *dev_priv = dev->dev_private;
1152 struct intel_ring_buffer *ring;
1155 if (INTEL_INFO(dev)->gen < 6)
1158 for_each_ring(ring, dev_priv, i) {
1160 fault_reg = I915_READ(RING_FAULT_REG(ring));
1161 if (fault_reg & RING_FAULT_VALID) {
1162 DRM_DEBUG_DRIVER("Unexpected fault\n"
1163 "\tAddr: 0x%08lx\\n"
1164 "\tAddress space: %s\n"
1167 fault_reg & PAGE_MASK,
1168 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
1169 RING_FAULT_SRCID(fault_reg),
1170 RING_FAULT_FAULT_TYPE(fault_reg));
1171 I915_WRITE(RING_FAULT_REG(ring),
1172 fault_reg & ~RING_FAULT_VALID);
1175 POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
1178 void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
1180 struct drm_i915_private *dev_priv = dev->dev_private;
1182 /* Don't bother messing with faults pre GEN6 as we have little
1183 * documentation supporting that it's a good idea.
1185 if (INTEL_INFO(dev)->gen < 6)
1188 i915_check_and_clear_faults(dev);
1190 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1191 dev_priv->gtt.base.start,
1192 dev_priv->gtt.base.total,
1196 void i915_gem_restore_gtt_mappings(struct drm_device *dev)
1198 struct drm_i915_private *dev_priv = dev->dev_private;
1199 struct drm_i915_gem_object *obj;
1200 struct i915_address_space *vm;
1202 i915_check_and_clear_faults(dev);
1204 /* First fill our portion of the GTT with scratch pages */
1205 dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
1206 dev_priv->gtt.base.start,
1207 dev_priv->gtt.base.total,
1210 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1211 struct i915_vma *vma = i915_gem_obj_to_vma(obj,
1212 &dev_priv->gtt.base);
1216 i915_gem_clflush_object(obj, obj->pin_display);
1217 /* The bind_vma code tries to be smart about tracking mappings.
1218 * Unfortunately above, we've just wiped out the mappings
1219 * without telling our object about it. So we need to fake it.
1221 obj->has_global_gtt_mapping = 0;
1222 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
1226 if (INTEL_INFO(dev)->gen >= 8)
1229 list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
1230 /* TODO: Perhaps it shouldn't be gen6 specific */
1231 if (i915_is_ggtt(vm)) {
1232 if (dev_priv->mm.aliasing_ppgtt)
1233 gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
1237 gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
1240 i915_gem_chipset_flush(dev);
1243 int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
1245 if (obj->has_dma_mapping)
1248 if (!dma_map_sg(&obj->base.dev->pdev->dev,
1249 obj->pages->sgl, obj->pages->nents,
1250 PCI_DMA_BIDIRECTIONAL))
1256 static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
1261 iowrite32((u32)pte, addr);
1262 iowrite32(pte >> 32, addr + 4);
1266 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
1267 struct sg_table *st,
1269 enum i915_cache_level level)
1271 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1272 unsigned first_entry = start >> PAGE_SHIFT;
1273 gen8_gtt_pte_t __iomem *gtt_entries =
1274 (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1276 struct sg_page_iter sg_iter;
1279 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1280 addr = sg_dma_address(sg_iter.sg) +
1281 (sg_iter.sg_pgoffset << PAGE_SHIFT);
1282 gen8_set_pte(>t_entries[i],
1283 gen8_pte_encode(addr, level, true));
1288 * XXX: This serves as a posting read to make sure that the PTE has
1289 * actually been updated. There is some concern that even though
1290 * registers and PTEs are within the same BAR that they are potentially
1291 * of NUMA access patterns. Therefore, even with the way we assume
1292 * hardware should work, we must keep this posting read for paranoia.
1295 WARN_ON(readq(>t_entries[i-1])
1296 != gen8_pte_encode(addr, level, true));
1298 /* This next bit makes the above posting read even more important. We
1299 * want to flush the TLBs only after we're certain all the PTE updates
1302 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1303 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1307 * Binds an object into the global gtt with the specified cache level. The object
1308 * will be accessible to the GPU via commands whose operands reference offsets
1309 * within the global GTT as well as accessible by the GPU through the GMADR
1310 * mapped BAR (dev_priv->mm.gtt->gtt).
1312 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
1313 struct sg_table *st,
1315 enum i915_cache_level level)
1317 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1318 unsigned first_entry = start >> PAGE_SHIFT;
1319 gen6_gtt_pte_t __iomem *gtt_entries =
1320 (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
1322 struct sg_page_iter sg_iter;
1325 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
1326 addr = sg_page_iter_dma_address(&sg_iter);
1327 iowrite32(vm->pte_encode(addr, level, true), >t_entries[i]);
1331 /* XXX: This serves as a posting read to make sure that the PTE has
1332 * actually been updated. There is some concern that even though
1333 * registers and PTEs are within the same BAR that they are potentially
1334 * of NUMA access patterns. Therefore, even with the way we assume
1335 * hardware should work, we must keep this posting read for paranoia.
1338 WARN_ON(readl(>t_entries[i-1]) !=
1339 vm->pte_encode(addr, level, true));
1341 /* This next bit makes the above posting read even more important. We
1342 * want to flush the TLBs only after we're certain all the PTE updates
1345 I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
1346 POSTING_READ(GFX_FLSH_CNTL_GEN6);
1349 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
1354 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1355 unsigned first_entry = start >> PAGE_SHIFT;
1356 unsigned num_entries = length >> PAGE_SHIFT;
1357 gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
1358 (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1359 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1362 if (WARN(num_entries > max_entries,
1363 "First entry = %d; Num entries = %d (max=%d)\n",
1364 first_entry, num_entries, max_entries))
1365 num_entries = max_entries;
1367 scratch_pte = gen8_pte_encode(vm->scratch.addr,
1370 for (i = 0; i < num_entries; i++)
1371 gen8_set_pte(>t_base[i], scratch_pte);
1375 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
1380 struct drm_i915_private *dev_priv = vm->dev->dev_private;
1381 unsigned first_entry = start >> PAGE_SHIFT;
1382 unsigned num_entries = length >> PAGE_SHIFT;
1383 gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
1384 (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
1385 const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
1388 if (WARN(num_entries > max_entries,
1389 "First entry = %d; Num entries = %d (max=%d)\n",
1390 first_entry, num_entries, max_entries))
1391 num_entries = max_entries;
1393 scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch);
1395 for (i = 0; i < num_entries; i++)
1396 iowrite32(scratch_pte, >t_base[i]);
1401 static void i915_ggtt_bind_vma(struct i915_vma *vma,
1402 enum i915_cache_level cache_level,
1405 const unsigned long entry = vma->node.start >> PAGE_SHIFT;
1406 unsigned int flags = (cache_level == I915_CACHE_NONE) ?
1407 AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
1409 BUG_ON(!i915_is_ggtt(vma->vm));
1410 intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
1411 vma->obj->has_global_gtt_mapping = 1;
1414 static void i915_ggtt_clear_range(struct i915_address_space *vm,
1419 unsigned first_entry = start >> PAGE_SHIFT;
1420 unsigned num_entries = length >> PAGE_SHIFT;
1421 intel_gtt_clear_range(first_entry, num_entries);
1424 static void i915_ggtt_unbind_vma(struct i915_vma *vma)
1426 const unsigned int first = vma->node.start >> PAGE_SHIFT;
1427 const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
1429 BUG_ON(!i915_is_ggtt(vma->vm));
1430 vma->obj->has_global_gtt_mapping = 0;
1431 intel_gtt_clear_range(first, size);
1434 static void ggtt_bind_vma(struct i915_vma *vma,
1435 enum i915_cache_level cache_level,
1438 struct drm_device *dev = vma->vm->dev;
1439 struct drm_i915_private *dev_priv = dev->dev_private;
1440 struct drm_i915_gem_object *obj = vma->obj;
1442 /* If there is no aliasing PPGTT, or the caller needs a global mapping,
1443 * or we have a global mapping already but the cacheability flags have
1444 * changed, set the global PTEs.
1446 * If there is an aliasing PPGTT it is anecdotally faster, so use that
1447 * instead if none of the above hold true.
1449 * NB: A global mapping should only be needed for special regions like
1450 * "gtt mappable", SNB errata, or if specified via special execbuf
1451 * flags. At all other times, the GPU will use the aliasing PPGTT.
1453 if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
1454 if (!obj->has_global_gtt_mapping ||
1455 (cache_level != obj->cache_level)) {
1456 vma->vm->insert_entries(vma->vm, obj->pages,
1459 obj->has_global_gtt_mapping = 1;
1463 if (dev_priv->mm.aliasing_ppgtt &&
1464 (!obj->has_aliasing_ppgtt_mapping ||
1465 (cache_level != obj->cache_level))) {
1466 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1467 appgtt->base.insert_entries(&appgtt->base,
1471 vma->obj->has_aliasing_ppgtt_mapping = 1;
1475 static void ggtt_unbind_vma(struct i915_vma *vma)
1477 struct drm_device *dev = vma->vm->dev;
1478 struct drm_i915_private *dev_priv = dev->dev_private;
1479 struct drm_i915_gem_object *obj = vma->obj;
1481 if (obj->has_global_gtt_mapping) {
1482 vma->vm->clear_range(vma->vm,
1486 obj->has_global_gtt_mapping = 0;
1489 if (obj->has_aliasing_ppgtt_mapping) {
1490 struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
1491 appgtt->base.clear_range(&appgtt->base,
1495 obj->has_aliasing_ppgtt_mapping = 0;
1499 void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
1501 struct drm_device *dev = obj->base.dev;
1502 struct drm_i915_private *dev_priv = dev->dev_private;
1505 interruptible = do_idling(dev_priv);
1507 if (!obj->has_dma_mapping)
1508 dma_unmap_sg(&dev->pdev->dev,
1509 obj->pages->sgl, obj->pages->nents,
1510 PCI_DMA_BIDIRECTIONAL);
1512 undo_idling(dev_priv, interruptible);
1515 static void i915_gtt_color_adjust(struct drm_mm_node *node,
1516 unsigned long color,
1517 unsigned long *start,
1520 if (node->color != color)
1523 if (!list_empty(&node->node_list)) {
1524 node = list_entry(node->node_list.next,
1527 if (node->allocated && node->color != color)
1532 void i915_gem_setup_global_gtt(struct drm_device *dev,
1533 unsigned long start,
1534 unsigned long mappable_end,
1537 /* Let GEM Manage all of the aperture.
1539 * However, leave one page at the end still bound to the scratch page.
1540 * There are a number of places where the hardware apparently prefetches
1541 * past the end of the object, and we've seen multiple hangs with the
1542 * GPU head pointer stuck in a batchbuffer bound at the last page of the
1543 * aperture. One page should be enough to keep any prefetching inside
1546 struct drm_i915_private *dev_priv = dev->dev_private;
1547 struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
1548 struct drm_mm_node *entry;
1549 struct drm_i915_gem_object *obj;
1550 unsigned long hole_start, hole_end;
1552 BUG_ON(mappable_end > end);
1554 /* Subtract the guard page ... */
1555 drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
1557 dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
1559 /* Mark any preallocated objects as occupied */
1560 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
1561 struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
1563 DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
1564 i915_gem_obj_ggtt_offset(obj), obj->base.size);
1566 WARN_ON(i915_gem_obj_ggtt_bound(obj));
1567 ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
1569 DRM_DEBUG_KMS("Reservation failed\n");
1570 obj->has_global_gtt_mapping = 1;
1573 dev_priv->gtt.base.start = start;
1574 dev_priv->gtt.base.total = end - start;
1576 /* Clear any non-preallocated blocks */
1577 drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
1578 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
1579 hole_start, hole_end);
1580 ggtt_vm->clear_range(ggtt_vm, hole_start,
1581 hole_end - hole_start, true);
1584 /* And finally clear the reserved guard page */
1585 ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
1588 void i915_gem_init_global_gtt(struct drm_device *dev)
1590 struct drm_i915_private *dev_priv = dev->dev_private;
1591 unsigned long gtt_size, mappable_size;
1593 gtt_size = dev_priv->gtt.base.total;
1594 mappable_size = dev_priv->gtt.mappable_end;
1596 i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
1599 static int setup_scratch_page(struct drm_device *dev)
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1603 dma_addr_t dma_addr;
1605 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
1609 set_pages_uc(page, 1);
1611 #ifdef CONFIG_INTEL_IOMMU
1612 dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
1613 PCI_DMA_BIDIRECTIONAL);
1614 if (pci_dma_mapping_error(dev->pdev, dma_addr))
1617 dma_addr = page_to_phys(page);
1619 dev_priv->gtt.base.scratch.page = page;
1620 dev_priv->gtt.base.scratch.addr = dma_addr;
1625 static void teardown_scratch_page(struct drm_device *dev)
1627 struct drm_i915_private *dev_priv = dev->dev_private;
1628 struct page *page = dev_priv->gtt.base.scratch.page;
1630 set_pages_wb(page, 1);
1631 pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
1632 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1637 static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1639 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1640 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1641 return snb_gmch_ctl << 20;
1644 static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1646 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1647 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1649 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1650 if (bdw_gmch_ctl > 4) {
1651 WARN_ON(!i915.preliminary_hw_support);
1655 return bdw_gmch_ctl << 20;
1658 static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
1660 snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
1661 snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
1662 return snb_gmch_ctl << 25; /* 32 MB units */
1665 static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
1667 bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
1668 bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
1669 return bdw_gmch_ctl << 25; /* 32 MB units */
1672 static int ggtt_probe_common(struct drm_device *dev,
1675 struct drm_i915_private *dev_priv = dev->dev_private;
1676 phys_addr_t gtt_bus_addr;
1679 /* For Modern GENs the PTEs and register space are split in the BAR */
1680 gtt_bus_addr = pci_resource_start(dev->pdev, 0) +
1681 (pci_resource_len(dev->pdev, 0) / 2);
1683 dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size);
1684 if (!dev_priv->gtt.gsm) {
1685 DRM_ERROR("Failed to map the gtt page table\n");
1689 ret = setup_scratch_page(dev);
1691 DRM_ERROR("Scratch setup failed\n");
1692 /* iounmap will also get called at remove, but meh */
1693 iounmap(dev_priv->gtt.gsm);
1699 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
1700 * bits. When using advanced contexts each context stores its own PAT, but
1701 * writing this data shouldn't be harmful even in those cases. */
1702 static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv)
1704 #define GEN8_PPAT_UC (0<<0)
1705 #define GEN8_PPAT_WC (1<<0)
1706 #define GEN8_PPAT_WT (2<<0)
1707 #define GEN8_PPAT_WB (3<<0)
1708 #define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
1709 /* FIXME(BDW): Bspec is completely confused about cache control bits. */
1710 #define GEN8_PPAT_LLC (1<<2)
1711 #define GEN8_PPAT_LLCELLC (2<<2)
1712 #define GEN8_PPAT_LLCeLLC (3<<2)
1713 #define GEN8_PPAT_AGE(x) (x<<4)
1714 #define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8))
1717 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
1718 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
1719 GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
1720 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
1721 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
1722 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
1723 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
1724 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
1726 /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
1727 * write would work. */
1728 I915_WRITE(GEN8_PRIVATE_PAT, pat);
1729 I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
1732 static int gen8_gmch_probe(struct drm_device *dev,
1735 phys_addr_t *mappable_base,
1736 unsigned long *mappable_end)
1738 struct drm_i915_private *dev_priv = dev->dev_private;
1739 unsigned int gtt_size;
1743 /* TODO: We're not aware of mappable constraints on gen8 yet */
1744 *mappable_base = pci_resource_start(dev->pdev, 2);
1745 *mappable_end = pci_resource_len(dev->pdev, 2);
1747 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
1748 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
1750 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1752 *stolen = gen8_get_stolen_size(snb_gmch_ctl);
1754 gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
1755 *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
1757 gen8_setup_private_ppat(dev_priv);
1759 ret = ggtt_probe_common(dev, gtt_size);
1761 dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
1762 dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
1767 static int gen6_gmch_probe(struct drm_device *dev,
1770 phys_addr_t *mappable_base,
1771 unsigned long *mappable_end)
1773 struct drm_i915_private *dev_priv = dev->dev_private;
1774 unsigned int gtt_size;
1778 *mappable_base = pci_resource_start(dev->pdev, 2);
1779 *mappable_end = pci_resource_len(dev->pdev, 2);
1781 /* 64/512MB is the current min/max we actually know of, but this is just
1782 * a coarse sanity check.
1784 if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
1785 DRM_ERROR("Unknown GMADR size (%lx)\n",
1786 dev_priv->gtt.mappable_end);
1790 if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
1791 pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
1792 pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1794 *stolen = gen6_get_stolen_size(snb_gmch_ctl);
1796 gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
1797 *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
1799 ret = ggtt_probe_common(dev, gtt_size);
1801 dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
1802 dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
1807 static void gen6_gmch_remove(struct i915_address_space *vm)
1810 struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
1812 drm_mm_takedown(&vm->mm);
1814 teardown_scratch_page(vm->dev);
1817 static int i915_gmch_probe(struct drm_device *dev,
1820 phys_addr_t *mappable_base,
1821 unsigned long *mappable_end)
1823 struct drm_i915_private *dev_priv = dev->dev_private;
1826 ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
1828 DRM_ERROR("failed to set up gmch\n");
1832 intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
1834 dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
1835 dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
1837 if (unlikely(dev_priv->gtt.do_idle_maps))
1838 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
1843 static void i915_gmch_remove(struct i915_address_space *vm)
1845 intel_gmch_remove();
1848 int i915_gem_gtt_init(struct drm_device *dev)
1850 struct drm_i915_private *dev_priv = dev->dev_private;
1851 struct i915_gtt *gtt = &dev_priv->gtt;
1854 if (INTEL_INFO(dev)->gen <= 5) {
1855 gtt->gtt_probe = i915_gmch_probe;
1856 gtt->base.cleanup = i915_gmch_remove;
1857 } else if (INTEL_INFO(dev)->gen < 8) {
1858 gtt->gtt_probe = gen6_gmch_probe;
1859 gtt->base.cleanup = gen6_gmch_remove;
1860 if (IS_HASWELL(dev) && dev_priv->ellc_size)
1861 gtt->base.pte_encode = iris_pte_encode;
1862 else if (IS_HASWELL(dev))
1863 gtt->base.pte_encode = hsw_pte_encode;
1864 else if (IS_VALLEYVIEW(dev))
1865 gtt->base.pte_encode = byt_pte_encode;
1866 else if (INTEL_INFO(dev)->gen >= 7)
1867 gtt->base.pte_encode = ivb_pte_encode;
1869 gtt->base.pte_encode = snb_pte_encode;
1871 dev_priv->gtt.gtt_probe = gen8_gmch_probe;
1872 dev_priv->gtt.base.cleanup = gen6_gmch_remove;
1875 ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size,
1876 >t->mappable_base, >t->mappable_end);
1880 gtt->base.dev = dev;
1882 /* GMADR is the PCI mmio aperture into the global GTT. */
1883 DRM_INFO("Memory usable by graphics device = %zdM\n",
1884 gtt->base.total >> 20);
1885 DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
1886 DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
1891 static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
1892 struct i915_address_space *vm)
1894 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
1896 return ERR_PTR(-ENOMEM);
1898 INIT_LIST_HEAD(&vma->vma_link);
1899 INIT_LIST_HEAD(&vma->mm_list);
1900 INIT_LIST_HEAD(&vma->exec_list);
1904 switch (INTEL_INFO(vm->dev)->gen) {
1908 if (i915_is_ggtt(vm)) {
1909 vma->unbind_vma = ggtt_unbind_vma;
1910 vma->bind_vma = ggtt_bind_vma;
1912 vma->unbind_vma = ppgtt_unbind_vma;
1913 vma->bind_vma = ppgtt_bind_vma;
1920 BUG_ON(!i915_is_ggtt(vm));
1921 vma->unbind_vma = i915_ggtt_unbind_vma;
1922 vma->bind_vma = i915_ggtt_bind_vma;
1928 /* Keep GGTT vmas first to make debug easier */
1929 if (i915_is_ggtt(vm))
1930 list_add(&vma->vma_link, &obj->vma_list);
1932 list_add_tail(&vma->vma_link, &obj->vma_list);
1938 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
1939 struct i915_address_space *vm)
1941 struct i915_vma *vma;
1943 vma = i915_gem_obj_to_vma(obj, vm);
1945 vma = __i915_gem_vma_create(obj, vm);