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/slab.h> /* fault-inject.h is not standalone! */
28 #include <linux/fault-inject.h>
29 #include <linux/log2.h>
30 #include <linux/random.h>
31 #include <linux/seq_file.h>
32 #include <linux/stop_machine.h>
34 #include <asm/set_memory.h>
37 #include <drm/i915_drm.h>
40 #include "i915_vgpu.h"
41 #include "i915_trace.h"
42 #include "intel_drv.h"
43 #include "intel_frontbuffer.h"
45 #define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
48 * DOC: Global GTT views
50 * Background and previous state
52 * Historically objects could exists (be bound) in global GTT space only as
53 * singular instances with a view representing all of the object's backing pages
54 * in a linear fashion. This view will be called a normal view.
56 * To support multiple views of the same object, where the number of mapped
57 * pages is not equal to the backing store, or where the layout of the pages
58 * is not linear, concept of a GGTT view was added.
60 * One example of an alternative view is a stereo display driven by a single
61 * image. In this case we would have a framebuffer looking like this
67 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
68 * rendering. In contrast, fed to the display engine would be an alternative
69 * view which could look something like this:
74 * In this example both the size and layout of pages in the alternative view is
75 * different from the normal view.
77 * Implementation and usage
79 * GGTT views are implemented using VMAs and are distinguished via enum
80 * i915_ggtt_view_type and struct i915_ggtt_view.
82 * A new flavour of core GEM functions which work with GGTT bound objects were
83 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
84 * renaming in large amounts of code. They take the struct i915_ggtt_view
85 * parameter encapsulating all metadata required to implement a view.
87 * As a helper for callers which are only interested in the normal view,
88 * globally const i915_ggtt_view_normal singleton instance exists. All old core
89 * GEM API functions, the ones not taking the view parameter, are operating on,
90 * or with the normal GGTT view.
92 * Code wanting to add or use a new GGTT view needs to:
94 * 1. Add a new enum with a suitable name.
95 * 2. Extend the metadata in the i915_ggtt_view structure if required.
96 * 3. Add support to i915_get_vma_pages().
98 * New views are required to build a scatter-gather table from within the
99 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
100 * exists for the lifetime of an VMA.
102 * Core API is designed to have copy semantics which means that passed in
103 * struct i915_ggtt_view does not need to be persistent (left around after
104 * calling the core API functions).
109 i915_get_ggtt_vma_pages(struct i915_vma
*vma
);
111 static void gen6_ggtt_invalidate(struct drm_i915_private
*dev_priv
)
113 /* Note that as an uncached mmio write, this should flush the
114 * WCB of the writes into the GGTT before it triggers the invalidate.
116 I915_WRITE(GFX_FLSH_CNTL_GEN6
, GFX_FLSH_CNTL_EN
);
119 static void guc_ggtt_invalidate(struct drm_i915_private
*dev_priv
)
121 gen6_ggtt_invalidate(dev_priv
);
122 I915_WRITE(GEN8_GTCR
, GEN8_GTCR_INVALIDATE
);
125 static void gmch_ggtt_invalidate(struct drm_i915_private
*dev_priv
)
127 intel_gtt_chipset_flush();
130 static inline void i915_ggtt_invalidate(struct drm_i915_private
*i915
)
132 i915
->ggtt
.invalidate(i915
);
135 int intel_sanitize_enable_ppgtt(struct drm_i915_private
*dev_priv
,
138 bool has_aliasing_ppgtt
;
140 bool has_full_48bit_ppgtt
;
142 has_aliasing_ppgtt
= dev_priv
->info
.has_aliasing_ppgtt
;
143 has_full_ppgtt
= dev_priv
->info
.has_full_ppgtt
;
144 has_full_48bit_ppgtt
= dev_priv
->info
.has_full_48bit_ppgtt
;
146 if (intel_vgpu_active(dev_priv
)) {
147 /* GVT-g has no support for 32bit ppgtt */
148 has_full_ppgtt
= false;
149 has_full_48bit_ppgtt
= intel_vgpu_has_full_48bit_ppgtt(dev_priv
);
152 if (!has_aliasing_ppgtt
)
156 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
157 * execlists, the sole mechanism available to submit work.
159 if (enable_ppgtt
== 0 && INTEL_GEN(dev_priv
) < 9)
162 if (enable_ppgtt
== 1)
165 if (enable_ppgtt
== 2 && has_full_ppgtt
)
168 if (enable_ppgtt
== 3 && has_full_48bit_ppgtt
)
171 /* Disable ppgtt on SNB if VT-d is on. */
172 if (IS_GEN6(dev_priv
) && intel_vtd_active()) {
173 DRM_INFO("Disabling PPGTT because VT-d is on\n");
177 /* Early VLV doesn't have this */
178 if (IS_VALLEYVIEW(dev_priv
) && dev_priv
->drm
.pdev
->revision
< 0xb) {
179 DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
183 if (INTEL_GEN(dev_priv
) >= 8 && i915_modparams
.enable_execlists
) {
184 if (has_full_48bit_ppgtt
)
191 return has_aliasing_ppgtt
? 1 : 0;
194 static int ppgtt_bind_vma(struct i915_vma
*vma
,
195 enum i915_cache_level cache_level
,
201 if (!(vma
->flags
& I915_VMA_LOCAL_BIND
)) {
202 ret
= vma
->vm
->allocate_va_range(vma
->vm
, vma
->node
.start
,
208 vma
->pages
= vma
->obj
->mm
.pages
;
210 /* Currently applicable only to VLV */
213 pte_flags
|= PTE_READ_ONLY
;
215 vma
->vm
->insert_entries(vma
->vm
, vma
, cache_level
, pte_flags
);
220 static void ppgtt_unbind_vma(struct i915_vma
*vma
)
222 vma
->vm
->clear_range(vma
->vm
, vma
->node
.start
, vma
->size
);
225 static gen8_pte_t
gen8_pte_encode(dma_addr_t addr
,
226 enum i915_cache_level level
)
228 gen8_pte_t pte
= _PAGE_PRESENT
| _PAGE_RW
;
232 case I915_CACHE_NONE
:
233 pte
|= PPAT_UNCACHED
;
236 pte
|= PPAT_DISPLAY_ELLC
;
246 static gen8_pde_t
gen8_pde_encode(const dma_addr_t addr
,
247 const enum i915_cache_level level
)
249 gen8_pde_t pde
= _PAGE_PRESENT
| _PAGE_RW
;
251 if (level
!= I915_CACHE_NONE
)
252 pde
|= PPAT_CACHED_PDE
;
254 pde
|= PPAT_UNCACHED
;
258 #define gen8_pdpe_encode gen8_pde_encode
259 #define gen8_pml4e_encode gen8_pde_encode
261 static gen6_pte_t
snb_pte_encode(dma_addr_t addr
,
262 enum i915_cache_level level
,
265 gen6_pte_t pte
= GEN6_PTE_VALID
;
266 pte
|= GEN6_PTE_ADDR_ENCODE(addr
);
269 case I915_CACHE_L3_LLC
:
271 pte
|= GEN6_PTE_CACHE_LLC
;
273 case I915_CACHE_NONE
:
274 pte
|= GEN6_PTE_UNCACHED
;
283 static gen6_pte_t
ivb_pte_encode(dma_addr_t addr
,
284 enum i915_cache_level level
,
287 gen6_pte_t pte
= GEN6_PTE_VALID
;
288 pte
|= GEN6_PTE_ADDR_ENCODE(addr
);
291 case I915_CACHE_L3_LLC
:
292 pte
|= GEN7_PTE_CACHE_L3_LLC
;
295 pte
|= GEN6_PTE_CACHE_LLC
;
297 case I915_CACHE_NONE
:
298 pte
|= GEN6_PTE_UNCACHED
;
307 static gen6_pte_t
byt_pte_encode(dma_addr_t addr
,
308 enum i915_cache_level level
,
311 gen6_pte_t pte
= GEN6_PTE_VALID
;
312 pte
|= GEN6_PTE_ADDR_ENCODE(addr
);
314 if (!(flags
& PTE_READ_ONLY
))
315 pte
|= BYT_PTE_WRITEABLE
;
317 if (level
!= I915_CACHE_NONE
)
318 pte
|= BYT_PTE_SNOOPED_BY_CPU_CACHES
;
323 static gen6_pte_t
hsw_pte_encode(dma_addr_t addr
,
324 enum i915_cache_level level
,
327 gen6_pte_t pte
= GEN6_PTE_VALID
;
328 pte
|= HSW_PTE_ADDR_ENCODE(addr
);
330 if (level
!= I915_CACHE_NONE
)
331 pte
|= HSW_WB_LLC_AGE3
;
336 static gen6_pte_t
iris_pte_encode(dma_addr_t addr
,
337 enum i915_cache_level level
,
340 gen6_pte_t pte
= GEN6_PTE_VALID
;
341 pte
|= HSW_PTE_ADDR_ENCODE(addr
);
344 case I915_CACHE_NONE
:
347 pte
|= HSW_WT_ELLC_LLC_AGE3
;
350 pte
|= HSW_WB_ELLC_LLC_AGE3
;
357 static struct page
*vm_alloc_page(struct i915_address_space
*vm
, gfp_t gfp
)
359 struct pagevec
*pvec
= &vm
->free_pages
;
361 if (I915_SELFTEST_ONLY(should_fail(&vm
->fault_attr
, 1)))
362 i915_gem_shrink_all(vm
->i915
);
364 if (likely(pvec
->nr
))
365 return pvec
->pages
[--pvec
->nr
];
368 return alloc_page(gfp
);
370 /* A placeholder for a specific mutex to guard the WC stash */
371 lockdep_assert_held(&vm
->i915
->drm
.struct_mutex
);
373 /* Look in our global stash of WC pages... */
374 pvec
= &vm
->i915
->mm
.wc_stash
;
375 if (likely(pvec
->nr
))
376 return pvec
->pages
[--pvec
->nr
];
378 /* Otherwise batch allocate pages to amoritize cost of set_pages_wc. */
382 page
= alloc_page(gfp
);
386 pvec
->pages
[pvec
->nr
++] = page
;
387 } while (pagevec_space(pvec
));
389 if (unlikely(!pvec
->nr
))
392 set_pages_array_wc(pvec
->pages
, pvec
->nr
);
394 return pvec
->pages
[--pvec
->nr
];
397 static void vm_free_pages_release(struct i915_address_space
*vm
,
400 struct pagevec
*pvec
= &vm
->free_pages
;
402 GEM_BUG_ON(!pagevec_count(pvec
));
404 if (vm
->pt_kmap_wc
) {
405 struct pagevec
*stash
= &vm
->i915
->mm
.wc_stash
;
407 /* When we use WC, first fill up the global stash and then
408 * only if full immediately free the overflow.
411 lockdep_assert_held(&vm
->i915
->drm
.struct_mutex
);
412 if (pagevec_space(stash
)) {
414 stash
->pages
[stash
->nr
++] =
415 pvec
->pages
[--pvec
->nr
];
418 } while (pagevec_space(stash
));
420 /* As we have made some room in the VM's free_pages,
421 * we can wait for it to fill again. Unless we are
422 * inside i915_address_space_fini() and must
423 * immediately release the pages!
429 set_pages_array_wb(pvec
->pages
, pvec
->nr
);
432 __pagevec_release(pvec
);
435 static void vm_free_page(struct i915_address_space
*vm
, struct page
*page
)
437 if (!pagevec_add(&vm
->free_pages
, page
))
438 vm_free_pages_release(vm
, false);
441 static int __setup_page_dma(struct i915_address_space
*vm
,
442 struct i915_page_dma
*p
,
445 p
->page
= vm_alloc_page(vm
, gfp
| __GFP_NOWARN
| __GFP_NORETRY
);
446 if (unlikely(!p
->page
))
449 p
->daddr
= dma_map_page(vm
->dma
, p
->page
, 0, PAGE_SIZE
,
450 PCI_DMA_BIDIRECTIONAL
);
451 if (unlikely(dma_mapping_error(vm
->dma
, p
->daddr
))) {
452 vm_free_page(vm
, p
->page
);
459 static int setup_page_dma(struct i915_address_space
*vm
,
460 struct i915_page_dma
*p
)
462 return __setup_page_dma(vm
, p
, I915_GFP_DMA
);
465 static void cleanup_page_dma(struct i915_address_space
*vm
,
466 struct i915_page_dma
*p
)
468 dma_unmap_page(vm
->dma
, p
->daddr
, PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
469 vm_free_page(vm
, p
->page
);
472 #define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
474 #define setup_px(vm, px) setup_page_dma((vm), px_base(px))
475 #define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
476 #define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
477 #define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
479 static void fill_page_dma(struct i915_address_space
*vm
,
480 struct i915_page_dma
*p
,
483 u64
* const vaddr
= kmap_atomic(p
->page
);
485 memset64(vaddr
, val
, PAGE_SIZE
/ sizeof(val
));
487 kunmap_atomic(vaddr
);
490 static void fill_page_dma_32(struct i915_address_space
*vm
,
491 struct i915_page_dma
*p
,
494 fill_page_dma(vm
, p
, (u64
)v
<< 32 | v
);
498 setup_scratch_page(struct i915_address_space
*vm
, gfp_t gfp
)
503 page
= alloc_page(gfp
| __GFP_ZERO
);
507 addr
= dma_map_page(vm
->dma
, page
, 0, PAGE_SIZE
,
508 PCI_DMA_BIDIRECTIONAL
);
509 if (unlikely(dma_mapping_error(vm
->dma
, addr
))) {
514 vm
->scratch_page
.page
= page
;
515 vm
->scratch_page
.daddr
= addr
;
519 static void cleanup_scratch_page(struct i915_address_space
*vm
)
521 struct i915_page_dma
*p
= &vm
->scratch_page
;
523 dma_unmap_page(vm
->dma
, p
->daddr
, PAGE_SIZE
, PCI_DMA_BIDIRECTIONAL
);
524 __free_page(p
->page
);
527 static struct i915_page_table
*alloc_pt(struct i915_address_space
*vm
)
529 struct i915_page_table
*pt
;
531 pt
= kmalloc(sizeof(*pt
), GFP_KERNEL
| __GFP_NOWARN
);
533 return ERR_PTR(-ENOMEM
);
535 if (unlikely(setup_px(vm
, pt
))) {
537 return ERR_PTR(-ENOMEM
);
544 static void free_pt(struct i915_address_space
*vm
, struct i915_page_table
*pt
)
550 static void gen8_initialize_pt(struct i915_address_space
*vm
,
551 struct i915_page_table
*pt
)
554 gen8_pte_encode(vm
->scratch_page
.daddr
, I915_CACHE_LLC
));
557 static void gen6_initialize_pt(struct i915_address_space
*vm
,
558 struct i915_page_table
*pt
)
561 vm
->pte_encode(vm
->scratch_page
.daddr
, I915_CACHE_LLC
, 0));
564 static struct i915_page_directory
*alloc_pd(struct i915_address_space
*vm
)
566 struct i915_page_directory
*pd
;
568 pd
= kzalloc(sizeof(*pd
), GFP_KERNEL
| __GFP_NOWARN
);
570 return ERR_PTR(-ENOMEM
);
572 if (unlikely(setup_px(vm
, pd
))) {
574 return ERR_PTR(-ENOMEM
);
581 static void free_pd(struct i915_address_space
*vm
,
582 struct i915_page_directory
*pd
)
588 static void gen8_initialize_pd(struct i915_address_space
*vm
,
589 struct i915_page_directory
*pd
)
594 gen8_pde_encode(px_dma(vm
->scratch_pt
), I915_CACHE_LLC
));
595 for (i
= 0; i
< I915_PDES
; i
++)
596 pd
->page_table
[i
] = vm
->scratch_pt
;
599 static int __pdp_init(struct i915_address_space
*vm
,
600 struct i915_page_directory_pointer
*pdp
)
602 const unsigned int pdpes
= i915_pdpes_per_pdp(vm
);
605 pdp
->page_directory
= kmalloc_array(pdpes
, sizeof(*pdp
->page_directory
),
606 GFP_KERNEL
| __GFP_NOWARN
);
607 if (unlikely(!pdp
->page_directory
))
610 for (i
= 0; i
< pdpes
; i
++)
611 pdp
->page_directory
[i
] = vm
->scratch_pd
;
616 static void __pdp_fini(struct i915_page_directory_pointer
*pdp
)
618 kfree(pdp
->page_directory
);
619 pdp
->page_directory
= NULL
;
622 static inline bool use_4lvl(const struct i915_address_space
*vm
)
624 return i915_vm_is_48bit(vm
);
627 static struct i915_page_directory_pointer
*
628 alloc_pdp(struct i915_address_space
*vm
)
630 struct i915_page_directory_pointer
*pdp
;
633 WARN_ON(!use_4lvl(vm
));
635 pdp
= kzalloc(sizeof(*pdp
), GFP_KERNEL
);
637 return ERR_PTR(-ENOMEM
);
639 ret
= __pdp_init(vm
, pdp
);
643 ret
= setup_px(vm
, pdp
);
657 static void free_pdp(struct i915_address_space
*vm
,
658 struct i915_page_directory_pointer
*pdp
)
669 static void gen8_initialize_pdp(struct i915_address_space
*vm
,
670 struct i915_page_directory_pointer
*pdp
)
672 gen8_ppgtt_pdpe_t scratch_pdpe
;
674 scratch_pdpe
= gen8_pdpe_encode(px_dma(vm
->scratch_pd
), I915_CACHE_LLC
);
676 fill_px(vm
, pdp
, scratch_pdpe
);
679 static void gen8_initialize_pml4(struct i915_address_space
*vm
,
680 struct i915_pml4
*pml4
)
685 gen8_pml4e_encode(px_dma(vm
->scratch_pdp
), I915_CACHE_LLC
));
686 for (i
= 0; i
< GEN8_PML4ES_PER_PML4
; i
++)
687 pml4
->pdps
[i
] = vm
->scratch_pdp
;
690 /* Broadwell Page Directory Pointer Descriptors */
691 static int gen8_write_pdp(struct drm_i915_gem_request
*req
,
695 struct intel_engine_cs
*engine
= req
->engine
;
700 cs
= intel_ring_begin(req
, 6);
704 *cs
++ = MI_LOAD_REGISTER_IMM(1);
705 *cs
++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine
, entry
));
706 *cs
++ = upper_32_bits(addr
);
707 *cs
++ = MI_LOAD_REGISTER_IMM(1);
708 *cs
++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine
, entry
));
709 *cs
++ = lower_32_bits(addr
);
710 intel_ring_advance(req
, cs
);
715 static int gen8_mm_switch_3lvl(struct i915_hw_ppgtt
*ppgtt
,
716 struct drm_i915_gem_request
*req
)
720 for (i
= GEN8_3LVL_PDPES
- 1; i
>= 0; i
--) {
721 const dma_addr_t pd_daddr
= i915_page_dir_dma_addr(ppgtt
, i
);
723 ret
= gen8_write_pdp(req
, i
, pd_daddr
);
731 static int gen8_mm_switch_4lvl(struct i915_hw_ppgtt
*ppgtt
,
732 struct drm_i915_gem_request
*req
)
734 return gen8_write_pdp(req
, 0, px_dma(&ppgtt
->pml4
));
737 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
738 * the page table structures, we mark them dirty so that
739 * context switching/execlist queuing code takes extra steps
740 * to ensure that tlbs are flushed.
742 static void mark_tlbs_dirty(struct i915_hw_ppgtt
*ppgtt
)
744 ppgtt
->pd_dirty_rings
= INTEL_INFO(ppgtt
->base
.i915
)->ring_mask
;
747 /* Removes entries from a single page table, releasing it if it's empty.
748 * Caller can use the return value to update higher-level entries.
750 static bool gen8_ppgtt_clear_pt(struct i915_address_space
*vm
,
751 struct i915_page_table
*pt
,
752 u64 start
, u64 length
)
754 unsigned int num_entries
= gen8_pte_count(start
, length
);
755 unsigned int pte
= gen8_pte_index(start
);
756 unsigned int pte_end
= pte
+ num_entries
;
757 const gen8_pte_t scratch_pte
=
758 gen8_pte_encode(vm
->scratch_page
.daddr
, I915_CACHE_LLC
);
761 GEM_BUG_ON(num_entries
> pt
->used_ptes
);
763 pt
->used_ptes
-= num_entries
;
767 vaddr
= kmap_atomic_px(pt
);
768 while (pte
< pte_end
)
769 vaddr
[pte
++] = scratch_pte
;
770 kunmap_atomic(vaddr
);
775 static void gen8_ppgtt_set_pde(struct i915_address_space
*vm
,
776 struct i915_page_directory
*pd
,
777 struct i915_page_table
*pt
,
782 pd
->page_table
[pde
] = pt
;
784 vaddr
= kmap_atomic_px(pd
);
785 vaddr
[pde
] = gen8_pde_encode(px_dma(pt
), I915_CACHE_LLC
);
786 kunmap_atomic(vaddr
);
789 static bool gen8_ppgtt_clear_pd(struct i915_address_space
*vm
,
790 struct i915_page_directory
*pd
,
791 u64 start
, u64 length
)
793 struct i915_page_table
*pt
;
796 gen8_for_each_pde(pt
, pd
, start
, length
, pde
) {
797 GEM_BUG_ON(pt
== vm
->scratch_pt
);
799 if (!gen8_ppgtt_clear_pt(vm
, pt
, start
, length
))
802 gen8_ppgtt_set_pde(vm
, pd
, vm
->scratch_pt
, pde
);
803 GEM_BUG_ON(!pd
->used_pdes
);
809 return !pd
->used_pdes
;
812 static void gen8_ppgtt_set_pdpe(struct i915_address_space
*vm
,
813 struct i915_page_directory_pointer
*pdp
,
814 struct i915_page_directory
*pd
,
817 gen8_ppgtt_pdpe_t
*vaddr
;
819 pdp
->page_directory
[pdpe
] = pd
;
823 vaddr
= kmap_atomic_px(pdp
);
824 vaddr
[pdpe
] = gen8_pdpe_encode(px_dma(pd
), I915_CACHE_LLC
);
825 kunmap_atomic(vaddr
);
828 /* Removes entries from a single page dir pointer, releasing it if it's empty.
829 * Caller can use the return value to update higher-level entries
831 static bool gen8_ppgtt_clear_pdp(struct i915_address_space
*vm
,
832 struct i915_page_directory_pointer
*pdp
,
833 u64 start
, u64 length
)
835 struct i915_page_directory
*pd
;
838 gen8_for_each_pdpe(pd
, pdp
, start
, length
, pdpe
) {
839 GEM_BUG_ON(pd
== vm
->scratch_pd
);
841 if (!gen8_ppgtt_clear_pd(vm
, pd
, start
, length
))
844 gen8_ppgtt_set_pdpe(vm
, pdp
, vm
->scratch_pd
, pdpe
);
845 GEM_BUG_ON(!pdp
->used_pdpes
);
851 return !pdp
->used_pdpes
;
854 static void gen8_ppgtt_clear_3lvl(struct i915_address_space
*vm
,
855 u64 start
, u64 length
)
857 gen8_ppgtt_clear_pdp(vm
, &i915_vm_to_ppgtt(vm
)->pdp
, start
, length
);
860 static void gen8_ppgtt_set_pml4e(struct i915_pml4
*pml4
,
861 struct i915_page_directory_pointer
*pdp
,
864 gen8_ppgtt_pml4e_t
*vaddr
;
866 pml4
->pdps
[pml4e
] = pdp
;
868 vaddr
= kmap_atomic_px(pml4
);
869 vaddr
[pml4e
] = gen8_pml4e_encode(px_dma(pdp
), I915_CACHE_LLC
);
870 kunmap_atomic(vaddr
);
873 /* Removes entries from a single pml4.
874 * This is the top-level structure in 4-level page tables used on gen8+.
875 * Empty entries are always scratch pml4e.
877 static void gen8_ppgtt_clear_4lvl(struct i915_address_space
*vm
,
878 u64 start
, u64 length
)
880 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
881 struct i915_pml4
*pml4
= &ppgtt
->pml4
;
882 struct i915_page_directory_pointer
*pdp
;
885 GEM_BUG_ON(!use_4lvl(vm
));
887 gen8_for_each_pml4e(pdp
, pml4
, start
, length
, pml4e
) {
888 GEM_BUG_ON(pdp
== vm
->scratch_pdp
);
890 if (!gen8_ppgtt_clear_pdp(vm
, pdp
, start
, length
))
893 gen8_ppgtt_set_pml4e(pml4
, vm
->scratch_pdp
, pml4e
);
900 struct scatterlist
*sg
;
904 struct gen8_insert_pte
{
911 static __always_inline
struct gen8_insert_pte
gen8_insert_pte(u64 start
)
913 return (struct gen8_insert_pte
) {
914 gen8_pml4e_index(start
),
915 gen8_pdpe_index(start
),
916 gen8_pde_index(start
),
917 gen8_pte_index(start
),
921 static __always_inline
bool
922 gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt
*ppgtt
,
923 struct i915_page_directory_pointer
*pdp
,
924 struct sgt_dma
*iter
,
925 struct gen8_insert_pte
*idx
,
926 enum i915_cache_level cache_level
)
928 struct i915_page_directory
*pd
;
929 const gen8_pte_t pte_encode
= gen8_pte_encode(0, cache_level
);
933 GEM_BUG_ON(idx
->pdpe
>= i915_pdpes_per_pdp(&ppgtt
->base
));
934 pd
= pdp
->page_directory
[idx
->pdpe
];
935 vaddr
= kmap_atomic_px(pd
->page_table
[idx
->pde
]);
937 vaddr
[idx
->pte
] = pte_encode
| iter
->dma
;
939 iter
->dma
+= PAGE_SIZE
;
940 if (iter
->dma
>= iter
->max
) {
941 iter
->sg
= __sg_next(iter
->sg
);
947 iter
->dma
= sg_dma_address(iter
->sg
);
948 iter
->max
= iter
->dma
+ iter
->sg
->length
;
951 if (++idx
->pte
== GEN8_PTES
) {
954 if (++idx
->pde
== I915_PDES
) {
957 /* Limited by sg length for 3lvl */
958 if (++idx
->pdpe
== GEN8_PML4ES_PER_PML4
) {
964 GEM_BUG_ON(idx
->pdpe
>= i915_pdpes_per_pdp(&ppgtt
->base
));
965 pd
= pdp
->page_directory
[idx
->pdpe
];
968 kunmap_atomic(vaddr
);
969 vaddr
= kmap_atomic_px(pd
->page_table
[idx
->pde
]);
972 kunmap_atomic(vaddr
);
977 static void gen8_ppgtt_insert_3lvl(struct i915_address_space
*vm
,
978 struct i915_vma
*vma
,
979 enum i915_cache_level cache_level
,
982 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
983 struct sgt_dma iter
= {
984 .sg
= vma
->pages
->sgl
,
985 .dma
= sg_dma_address(iter
.sg
),
986 .max
= iter
.dma
+ iter
.sg
->length
,
988 struct gen8_insert_pte idx
= gen8_insert_pte(vma
->node
.start
);
990 gen8_ppgtt_insert_pte_entries(ppgtt
, &ppgtt
->pdp
, &iter
, &idx
,
994 static void gen8_ppgtt_insert_4lvl(struct i915_address_space
*vm
,
995 struct i915_vma
*vma
,
996 enum i915_cache_level cache_level
,
999 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1000 struct sgt_dma iter
= {
1001 .sg
= vma
->pages
->sgl
,
1002 .dma
= sg_dma_address(iter
.sg
),
1003 .max
= iter
.dma
+ iter
.sg
->length
,
1005 struct i915_page_directory_pointer
**pdps
= ppgtt
->pml4
.pdps
;
1006 struct gen8_insert_pte idx
= gen8_insert_pte(vma
->node
.start
);
1008 while (gen8_ppgtt_insert_pte_entries(ppgtt
, pdps
[idx
.pml4e
++], &iter
,
1010 GEM_BUG_ON(idx
.pml4e
>= GEN8_PML4ES_PER_PML4
);
1013 static void gen8_free_page_tables(struct i915_address_space
*vm
,
1014 struct i915_page_directory
*pd
)
1021 for (i
= 0; i
< I915_PDES
; i
++) {
1022 if (pd
->page_table
[i
] != vm
->scratch_pt
)
1023 free_pt(vm
, pd
->page_table
[i
]);
1027 static int gen8_init_scratch(struct i915_address_space
*vm
)
1031 ret
= setup_scratch_page(vm
, I915_GFP_DMA
);
1035 vm
->scratch_pt
= alloc_pt(vm
);
1036 if (IS_ERR(vm
->scratch_pt
)) {
1037 ret
= PTR_ERR(vm
->scratch_pt
);
1038 goto free_scratch_page
;
1041 vm
->scratch_pd
= alloc_pd(vm
);
1042 if (IS_ERR(vm
->scratch_pd
)) {
1043 ret
= PTR_ERR(vm
->scratch_pd
);
1048 vm
->scratch_pdp
= alloc_pdp(vm
);
1049 if (IS_ERR(vm
->scratch_pdp
)) {
1050 ret
= PTR_ERR(vm
->scratch_pdp
);
1055 gen8_initialize_pt(vm
, vm
->scratch_pt
);
1056 gen8_initialize_pd(vm
, vm
->scratch_pd
);
1058 gen8_initialize_pdp(vm
, vm
->scratch_pdp
);
1063 free_pd(vm
, vm
->scratch_pd
);
1065 free_pt(vm
, vm
->scratch_pt
);
1067 cleanup_scratch_page(vm
);
1072 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt
*ppgtt
, bool create
)
1074 struct i915_address_space
*vm
= &ppgtt
->base
;
1075 struct drm_i915_private
*dev_priv
= vm
->i915
;
1076 enum vgt_g2v_type msg
;
1080 const u64 daddr
= px_dma(&ppgtt
->pml4
);
1082 I915_WRITE(vgtif_reg(pdp
[0].lo
), lower_32_bits(daddr
));
1083 I915_WRITE(vgtif_reg(pdp
[0].hi
), upper_32_bits(daddr
));
1085 msg
= (create
? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE
:
1086 VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY
);
1088 for (i
= 0; i
< GEN8_3LVL_PDPES
; i
++) {
1089 const u64 daddr
= i915_page_dir_dma_addr(ppgtt
, i
);
1091 I915_WRITE(vgtif_reg(pdp
[i
].lo
), lower_32_bits(daddr
));
1092 I915_WRITE(vgtif_reg(pdp
[i
].hi
), upper_32_bits(daddr
));
1095 msg
= (create
? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE
:
1096 VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY
);
1099 I915_WRITE(vgtif_reg(g2v_notify
), msg
);
1104 static void gen8_free_scratch(struct i915_address_space
*vm
)
1107 free_pdp(vm
, vm
->scratch_pdp
);
1108 free_pd(vm
, vm
->scratch_pd
);
1109 free_pt(vm
, vm
->scratch_pt
);
1110 cleanup_scratch_page(vm
);
1113 static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space
*vm
,
1114 struct i915_page_directory_pointer
*pdp
)
1116 const unsigned int pdpes
= i915_pdpes_per_pdp(vm
);
1119 for (i
= 0; i
< pdpes
; i
++) {
1120 if (pdp
->page_directory
[i
] == vm
->scratch_pd
)
1123 gen8_free_page_tables(vm
, pdp
->page_directory
[i
]);
1124 free_pd(vm
, pdp
->page_directory
[i
]);
1130 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt
*ppgtt
)
1134 for (i
= 0; i
< GEN8_PML4ES_PER_PML4
; i
++) {
1135 if (ppgtt
->pml4
.pdps
[i
] == ppgtt
->base
.scratch_pdp
)
1138 gen8_ppgtt_cleanup_3lvl(&ppgtt
->base
, ppgtt
->pml4
.pdps
[i
]);
1141 cleanup_px(&ppgtt
->base
, &ppgtt
->pml4
);
1144 static void gen8_ppgtt_cleanup(struct i915_address_space
*vm
)
1146 struct drm_i915_private
*dev_priv
= vm
->i915
;
1147 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1149 if (intel_vgpu_active(dev_priv
))
1150 gen8_ppgtt_notify_vgt(ppgtt
, false);
1153 gen8_ppgtt_cleanup_4lvl(ppgtt
);
1155 gen8_ppgtt_cleanup_3lvl(&ppgtt
->base
, &ppgtt
->pdp
);
1157 gen8_free_scratch(vm
);
1160 static int gen8_ppgtt_alloc_pd(struct i915_address_space
*vm
,
1161 struct i915_page_directory
*pd
,
1162 u64 start
, u64 length
)
1164 struct i915_page_table
*pt
;
1168 gen8_for_each_pde(pt
, pd
, start
, length
, pde
) {
1169 int count
= gen8_pte_count(start
, length
);
1171 if (pt
== vm
->scratch_pt
) {
1176 if (count
< GEN8_PTES
)
1177 gen8_initialize_pt(vm
, pt
);
1179 gen8_ppgtt_set_pde(vm
, pd
, pt
, pde
);
1181 GEM_BUG_ON(pd
->used_pdes
> I915_PDES
);
1184 pt
->used_ptes
+= count
;
1189 gen8_ppgtt_clear_pd(vm
, pd
, from
, start
- from
);
1193 static int gen8_ppgtt_alloc_pdp(struct i915_address_space
*vm
,
1194 struct i915_page_directory_pointer
*pdp
,
1195 u64 start
, u64 length
)
1197 struct i915_page_directory
*pd
;
1202 gen8_for_each_pdpe(pd
, pdp
, start
, length
, pdpe
) {
1203 if (pd
== vm
->scratch_pd
) {
1208 gen8_initialize_pd(vm
, pd
);
1209 gen8_ppgtt_set_pdpe(vm
, pdp
, pd
, pdpe
);
1211 GEM_BUG_ON(pdp
->used_pdpes
> i915_pdpes_per_pdp(vm
));
1213 mark_tlbs_dirty(i915_vm_to_ppgtt(vm
));
1216 ret
= gen8_ppgtt_alloc_pd(vm
, pd
, start
, length
);
1224 if (!pd
->used_pdes
) {
1225 gen8_ppgtt_set_pdpe(vm
, pdp
, vm
->scratch_pd
, pdpe
);
1226 GEM_BUG_ON(!pdp
->used_pdpes
);
1231 gen8_ppgtt_clear_pdp(vm
, pdp
, from
, start
- from
);
1235 static int gen8_ppgtt_alloc_3lvl(struct i915_address_space
*vm
,
1236 u64 start
, u64 length
)
1238 return gen8_ppgtt_alloc_pdp(vm
,
1239 &i915_vm_to_ppgtt(vm
)->pdp
, start
, length
);
1242 static int gen8_ppgtt_alloc_4lvl(struct i915_address_space
*vm
,
1243 u64 start
, u64 length
)
1245 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1246 struct i915_pml4
*pml4
= &ppgtt
->pml4
;
1247 struct i915_page_directory_pointer
*pdp
;
1252 gen8_for_each_pml4e(pdp
, pml4
, start
, length
, pml4e
) {
1253 if (pml4
->pdps
[pml4e
] == vm
->scratch_pdp
) {
1254 pdp
= alloc_pdp(vm
);
1258 gen8_initialize_pdp(vm
, pdp
);
1259 gen8_ppgtt_set_pml4e(pml4
, pdp
, pml4e
);
1262 ret
= gen8_ppgtt_alloc_pdp(vm
, pdp
, start
, length
);
1270 if (!pdp
->used_pdpes
) {
1271 gen8_ppgtt_set_pml4e(pml4
, vm
->scratch_pdp
, pml4e
);
1275 gen8_ppgtt_clear_4lvl(vm
, from
, start
- from
);
1279 static void gen8_dump_pdp(struct i915_hw_ppgtt
*ppgtt
,
1280 struct i915_page_directory_pointer
*pdp
,
1281 u64 start
, u64 length
,
1282 gen8_pte_t scratch_pte
,
1285 struct i915_address_space
*vm
= &ppgtt
->base
;
1286 struct i915_page_directory
*pd
;
1289 gen8_for_each_pdpe(pd
, pdp
, start
, length
, pdpe
) {
1290 struct i915_page_table
*pt
;
1291 u64 pd_len
= length
;
1292 u64 pd_start
= start
;
1295 if (pdp
->page_directory
[pdpe
] == ppgtt
->base
.scratch_pd
)
1298 seq_printf(m
, "\tPDPE #%d\n", pdpe
);
1299 gen8_for_each_pde(pt
, pd
, pd_start
, pd_len
, pde
) {
1301 gen8_pte_t
*pt_vaddr
;
1303 if (pd
->page_table
[pde
] == ppgtt
->base
.scratch_pt
)
1306 pt_vaddr
= kmap_atomic_px(pt
);
1307 for (pte
= 0; pte
< GEN8_PTES
; pte
+= 4) {
1308 u64 va
= (pdpe
<< GEN8_PDPE_SHIFT
|
1309 pde
<< GEN8_PDE_SHIFT
|
1310 pte
<< GEN8_PTE_SHIFT
);
1314 for (i
= 0; i
< 4; i
++)
1315 if (pt_vaddr
[pte
+ i
] != scratch_pte
)
1320 seq_printf(m
, "\t\t0x%llx [%03d,%03d,%04d]: =", va
, pdpe
, pde
, pte
);
1321 for (i
= 0; i
< 4; i
++) {
1322 if (pt_vaddr
[pte
+ i
] != scratch_pte
)
1323 seq_printf(m
, " %llx", pt_vaddr
[pte
+ i
]);
1325 seq_puts(m
, " SCRATCH ");
1329 kunmap_atomic(pt_vaddr
);
1334 static void gen8_dump_ppgtt(struct i915_hw_ppgtt
*ppgtt
, struct seq_file
*m
)
1336 struct i915_address_space
*vm
= &ppgtt
->base
;
1337 const gen8_pte_t scratch_pte
=
1338 gen8_pte_encode(vm
->scratch_page
.daddr
, I915_CACHE_LLC
);
1339 u64 start
= 0, length
= ppgtt
->base
.total
;
1343 struct i915_pml4
*pml4
= &ppgtt
->pml4
;
1344 struct i915_page_directory_pointer
*pdp
;
1346 gen8_for_each_pml4e(pdp
, pml4
, start
, length
, pml4e
) {
1347 if (pml4
->pdps
[pml4e
] == ppgtt
->base
.scratch_pdp
)
1350 seq_printf(m
, " PML4E #%llu\n", pml4e
);
1351 gen8_dump_pdp(ppgtt
, pdp
, start
, length
, scratch_pte
, m
);
1354 gen8_dump_pdp(ppgtt
, &ppgtt
->pdp
, start
, length
, scratch_pte
, m
);
1358 static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt
*ppgtt
)
1360 struct i915_address_space
*vm
= &ppgtt
->base
;
1361 struct i915_page_directory_pointer
*pdp
= &ppgtt
->pdp
;
1362 struct i915_page_directory
*pd
;
1363 u64 start
= 0, length
= ppgtt
->base
.total
;
1367 gen8_for_each_pdpe(pd
, pdp
, start
, length
, pdpe
) {
1372 gen8_initialize_pd(vm
, pd
);
1373 gen8_ppgtt_set_pdpe(vm
, pdp
, pd
, pdpe
);
1377 pdp
->used_pdpes
++; /* never remove */
1382 gen8_for_each_pdpe(pd
, pdp
, from
, start
, pdpe
) {
1383 gen8_ppgtt_set_pdpe(vm
, pdp
, vm
->scratch_pd
, pdpe
);
1386 pdp
->used_pdpes
= 0;
1391 * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
1392 * with a net effect resembling a 2-level page table in normal x86 terms. Each
1393 * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
1397 static int gen8_ppgtt_init(struct i915_hw_ppgtt
*ppgtt
)
1399 struct i915_address_space
*vm
= &ppgtt
->base
;
1400 struct drm_i915_private
*dev_priv
= vm
->i915
;
1403 ppgtt
->base
.total
= USES_FULL_48BIT_PPGTT(dev_priv
) ?
1407 /* There are only few exceptions for gen >=6. chv and bxt.
1408 * And we are not sure about the latter so play safe for now.
1410 if (IS_CHERRYVIEW(dev_priv
) || IS_BROXTON(dev_priv
))
1411 ppgtt
->base
.pt_kmap_wc
= true;
1413 ret
= gen8_init_scratch(&ppgtt
->base
);
1415 ppgtt
->base
.total
= 0;
1420 ret
= setup_px(&ppgtt
->base
, &ppgtt
->pml4
);
1424 gen8_initialize_pml4(&ppgtt
->base
, &ppgtt
->pml4
);
1426 ppgtt
->switch_mm
= gen8_mm_switch_4lvl
;
1427 ppgtt
->base
.allocate_va_range
= gen8_ppgtt_alloc_4lvl
;
1428 ppgtt
->base
.insert_entries
= gen8_ppgtt_insert_4lvl
;
1429 ppgtt
->base
.clear_range
= gen8_ppgtt_clear_4lvl
;
1431 ret
= __pdp_init(&ppgtt
->base
, &ppgtt
->pdp
);
1435 if (intel_vgpu_active(dev_priv
)) {
1436 ret
= gen8_preallocate_top_level_pdp(ppgtt
);
1438 __pdp_fini(&ppgtt
->pdp
);
1443 ppgtt
->switch_mm
= gen8_mm_switch_3lvl
;
1444 ppgtt
->base
.allocate_va_range
= gen8_ppgtt_alloc_3lvl
;
1445 ppgtt
->base
.insert_entries
= gen8_ppgtt_insert_3lvl
;
1446 ppgtt
->base
.clear_range
= gen8_ppgtt_clear_3lvl
;
1449 if (intel_vgpu_active(dev_priv
))
1450 gen8_ppgtt_notify_vgt(ppgtt
, true);
1452 ppgtt
->base
.cleanup
= gen8_ppgtt_cleanup
;
1453 ppgtt
->base
.unbind_vma
= ppgtt_unbind_vma
;
1454 ppgtt
->base
.bind_vma
= ppgtt_bind_vma
;
1455 ppgtt
->debug_dump
= gen8_dump_ppgtt
;
1460 gen8_free_scratch(&ppgtt
->base
);
1464 static void gen6_dump_ppgtt(struct i915_hw_ppgtt
*ppgtt
, struct seq_file
*m
)
1466 struct i915_address_space
*vm
= &ppgtt
->base
;
1467 struct i915_page_table
*unused
;
1468 gen6_pte_t scratch_pte
;
1469 u32 pd_entry
, pte
, pde
;
1470 u32 start
= 0, length
= ppgtt
->base
.total
;
1472 scratch_pte
= vm
->pte_encode(vm
->scratch_page
.daddr
,
1475 gen6_for_each_pde(unused
, &ppgtt
->pd
, start
, length
, pde
) {
1477 gen6_pte_t
*pt_vaddr
;
1478 const dma_addr_t pt_addr
= px_dma(ppgtt
->pd
.page_table
[pde
]);
1479 pd_entry
= readl(ppgtt
->pd_addr
+ pde
);
1480 expected
= (GEN6_PDE_ADDR_ENCODE(pt_addr
) | GEN6_PDE_VALID
);
1482 if (pd_entry
!= expected
)
1483 seq_printf(m
, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
1487 seq_printf(m
, "\tPDE: %x\n", pd_entry
);
1489 pt_vaddr
= kmap_atomic_px(ppgtt
->pd
.page_table
[pde
]);
1491 for (pte
= 0; pte
< GEN6_PTES
; pte
+=4) {
1493 (pde
* PAGE_SIZE
* GEN6_PTES
) +
1497 for (i
= 0; i
< 4; i
++)
1498 if (pt_vaddr
[pte
+ i
] != scratch_pte
)
1503 seq_printf(m
, "\t\t0x%lx [%03d,%04d]: =", va
, pde
, pte
);
1504 for (i
= 0; i
< 4; i
++) {
1505 if (pt_vaddr
[pte
+ i
] != scratch_pte
)
1506 seq_printf(m
, " %08x", pt_vaddr
[pte
+ i
]);
1508 seq_puts(m
, " SCRATCH ");
1512 kunmap_atomic(pt_vaddr
);
1516 /* Write pde (index) from the page directory @pd to the page table @pt */
1517 static inline void gen6_write_pde(const struct i915_hw_ppgtt
*ppgtt
,
1518 const unsigned int pde
,
1519 const struct i915_page_table
*pt
)
1521 /* Caller needs to make sure the write completes if necessary */
1522 writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt
)) | GEN6_PDE_VALID
,
1523 ppgtt
->pd_addr
+ pde
);
1526 /* Write all the page tables found in the ppgtt structure to incrementing page
1528 static void gen6_write_page_range(struct i915_hw_ppgtt
*ppgtt
,
1529 u32 start
, u32 length
)
1531 struct i915_page_table
*pt
;
1534 gen6_for_each_pde(pt
, &ppgtt
->pd
, start
, length
, pde
)
1535 gen6_write_pde(ppgtt
, pde
, pt
);
1537 mark_tlbs_dirty(ppgtt
);
1541 static inline u32
get_pd_offset(struct i915_hw_ppgtt
*ppgtt
)
1543 GEM_BUG_ON(ppgtt
->pd
.base
.ggtt_offset
& 0x3f);
1544 return ppgtt
->pd
.base
.ggtt_offset
<< 10;
1547 static int hsw_mm_switch(struct i915_hw_ppgtt
*ppgtt
,
1548 struct drm_i915_gem_request
*req
)
1550 struct intel_engine_cs
*engine
= req
->engine
;
1553 /* NB: TLBs must be flushed and invalidated before a switch */
1554 cs
= intel_ring_begin(req
, 6);
1558 *cs
++ = MI_LOAD_REGISTER_IMM(2);
1559 *cs
++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine
));
1560 *cs
++ = PP_DIR_DCLV_2G
;
1561 *cs
++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine
));
1562 *cs
++ = get_pd_offset(ppgtt
);
1564 intel_ring_advance(req
, cs
);
1569 static int gen7_mm_switch(struct i915_hw_ppgtt
*ppgtt
,
1570 struct drm_i915_gem_request
*req
)
1572 struct intel_engine_cs
*engine
= req
->engine
;
1575 /* NB: TLBs must be flushed and invalidated before a switch */
1576 cs
= intel_ring_begin(req
, 6);
1580 *cs
++ = MI_LOAD_REGISTER_IMM(2);
1581 *cs
++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine
));
1582 *cs
++ = PP_DIR_DCLV_2G
;
1583 *cs
++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine
));
1584 *cs
++ = get_pd_offset(ppgtt
);
1586 intel_ring_advance(req
, cs
);
1591 static int gen6_mm_switch(struct i915_hw_ppgtt
*ppgtt
,
1592 struct drm_i915_gem_request
*req
)
1594 struct intel_engine_cs
*engine
= req
->engine
;
1595 struct drm_i915_private
*dev_priv
= req
->i915
;
1597 I915_WRITE(RING_PP_DIR_DCLV(engine
), PP_DIR_DCLV_2G
);
1598 I915_WRITE(RING_PP_DIR_BASE(engine
), get_pd_offset(ppgtt
));
1602 static void gen8_ppgtt_enable(struct drm_i915_private
*dev_priv
)
1604 struct intel_engine_cs
*engine
;
1605 enum intel_engine_id id
;
1607 for_each_engine(engine
, dev_priv
, id
) {
1608 u32 four_level
= USES_FULL_48BIT_PPGTT(dev_priv
) ?
1609 GEN8_GFX_PPGTT_48B
: 0;
1610 I915_WRITE(RING_MODE_GEN7(engine
),
1611 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE
| four_level
));
1615 static void gen7_ppgtt_enable(struct drm_i915_private
*dev_priv
)
1617 struct intel_engine_cs
*engine
;
1618 u32 ecochk
, ecobits
;
1619 enum intel_engine_id id
;
1621 ecobits
= I915_READ(GAC_ECO_BITS
);
1622 I915_WRITE(GAC_ECO_BITS
, ecobits
| ECOBITS_PPGTT_CACHE64B
);
1624 ecochk
= I915_READ(GAM_ECOCHK
);
1625 if (IS_HASWELL(dev_priv
)) {
1626 ecochk
|= ECOCHK_PPGTT_WB_HSW
;
1628 ecochk
|= ECOCHK_PPGTT_LLC_IVB
;
1629 ecochk
&= ~ECOCHK_PPGTT_GFDT_IVB
;
1631 I915_WRITE(GAM_ECOCHK
, ecochk
);
1633 for_each_engine(engine
, dev_priv
, id
) {
1634 /* GFX_MODE is per-ring on gen7+ */
1635 I915_WRITE(RING_MODE_GEN7(engine
),
1636 _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE
));
1640 static void gen6_ppgtt_enable(struct drm_i915_private
*dev_priv
)
1642 u32 ecochk
, gab_ctl
, ecobits
;
1644 ecobits
= I915_READ(GAC_ECO_BITS
);
1645 I915_WRITE(GAC_ECO_BITS
, ecobits
| ECOBITS_SNB_BIT
|
1646 ECOBITS_PPGTT_CACHE64B
);
1648 gab_ctl
= I915_READ(GAB_CTL
);
1649 I915_WRITE(GAB_CTL
, gab_ctl
| GAB_CTL_CONT_AFTER_PAGEFAULT
);
1651 ecochk
= I915_READ(GAM_ECOCHK
);
1652 I915_WRITE(GAM_ECOCHK
, ecochk
| ECOCHK_SNB_BIT
| ECOCHK_PPGTT_CACHE64B
);
1654 I915_WRITE(GFX_MODE
, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE
));
1657 /* PPGTT support for Sandybdrige/Gen6 and later */
1658 static void gen6_ppgtt_clear_range(struct i915_address_space
*vm
,
1659 u64 start
, u64 length
)
1661 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1662 unsigned int first_entry
= start
>> PAGE_SHIFT
;
1663 unsigned int pde
= first_entry
/ GEN6_PTES
;
1664 unsigned int pte
= first_entry
% GEN6_PTES
;
1665 unsigned int num_entries
= length
>> PAGE_SHIFT
;
1666 gen6_pte_t scratch_pte
=
1667 vm
->pte_encode(vm
->scratch_page
.daddr
, I915_CACHE_LLC
, 0);
1669 while (num_entries
) {
1670 struct i915_page_table
*pt
= ppgtt
->pd
.page_table
[pde
++];
1671 unsigned int end
= min(pte
+ num_entries
, GEN6_PTES
);
1674 num_entries
-= end
- pte
;
1676 /* Note that the hw doesn't support removing PDE on the fly
1677 * (they are cached inside the context with no means to
1678 * invalidate the cache), so we can only reset the PTE
1679 * entries back to scratch.
1682 vaddr
= kmap_atomic_px(pt
);
1684 vaddr
[pte
++] = scratch_pte
;
1685 } while (pte
< end
);
1686 kunmap_atomic(vaddr
);
1692 static void gen6_ppgtt_insert_entries(struct i915_address_space
*vm
,
1693 struct i915_vma
*vma
,
1694 enum i915_cache_level cache_level
,
1697 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1698 unsigned first_entry
= vma
->node
.start
>> PAGE_SHIFT
;
1699 unsigned act_pt
= first_entry
/ GEN6_PTES
;
1700 unsigned act_pte
= first_entry
% GEN6_PTES
;
1701 const u32 pte_encode
= vm
->pte_encode(0, cache_level
, flags
);
1702 struct sgt_dma iter
;
1705 vaddr
= kmap_atomic_px(ppgtt
->pd
.page_table
[act_pt
]);
1706 iter
.sg
= vma
->pages
->sgl
;
1707 iter
.dma
= sg_dma_address(iter
.sg
);
1708 iter
.max
= iter
.dma
+ iter
.sg
->length
;
1710 vaddr
[act_pte
] = pte_encode
| GEN6_PTE_ADDR_ENCODE(iter
.dma
);
1712 iter
.dma
+= PAGE_SIZE
;
1713 if (iter
.dma
== iter
.max
) {
1714 iter
.sg
= __sg_next(iter
.sg
);
1718 iter
.dma
= sg_dma_address(iter
.sg
);
1719 iter
.max
= iter
.dma
+ iter
.sg
->length
;
1722 if (++act_pte
== GEN6_PTES
) {
1723 kunmap_atomic(vaddr
);
1724 vaddr
= kmap_atomic_px(ppgtt
->pd
.page_table
[++act_pt
]);
1728 kunmap_atomic(vaddr
);
1731 static int gen6_alloc_va_range(struct i915_address_space
*vm
,
1732 u64 start
, u64 length
)
1734 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1735 struct i915_page_table
*pt
;
1740 gen6_for_each_pde(pt
, &ppgtt
->pd
, start
, length
, pde
) {
1741 if (pt
== vm
->scratch_pt
) {
1746 gen6_initialize_pt(vm
, pt
);
1747 ppgtt
->pd
.page_table
[pde
] = pt
;
1748 gen6_write_pde(ppgtt
, pde
, pt
);
1754 mark_tlbs_dirty(ppgtt
);
1761 gen6_ppgtt_clear_range(vm
, from
, start
);
1765 static int gen6_init_scratch(struct i915_address_space
*vm
)
1769 ret
= setup_scratch_page(vm
, I915_GFP_DMA
);
1773 vm
->scratch_pt
= alloc_pt(vm
);
1774 if (IS_ERR(vm
->scratch_pt
)) {
1775 cleanup_scratch_page(vm
);
1776 return PTR_ERR(vm
->scratch_pt
);
1779 gen6_initialize_pt(vm
, vm
->scratch_pt
);
1784 static void gen6_free_scratch(struct i915_address_space
*vm
)
1786 free_pt(vm
, vm
->scratch_pt
);
1787 cleanup_scratch_page(vm
);
1790 static void gen6_ppgtt_cleanup(struct i915_address_space
*vm
)
1792 struct i915_hw_ppgtt
*ppgtt
= i915_vm_to_ppgtt(vm
);
1793 struct i915_page_directory
*pd
= &ppgtt
->pd
;
1794 struct i915_page_table
*pt
;
1797 drm_mm_remove_node(&ppgtt
->node
);
1799 gen6_for_all_pdes(pt
, pd
, pde
)
1800 if (pt
!= vm
->scratch_pt
)
1803 gen6_free_scratch(vm
);
1806 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt
*ppgtt
)
1808 struct i915_address_space
*vm
= &ppgtt
->base
;
1809 struct drm_i915_private
*dev_priv
= ppgtt
->base
.i915
;
1810 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
1813 /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
1814 * allocator works in address space sizes, so it's multiplied by page
1815 * size. We allocate at the top of the GTT to avoid fragmentation.
1817 BUG_ON(!drm_mm_initialized(&ggtt
->base
.mm
));
1819 ret
= gen6_init_scratch(vm
);
1823 ret
= i915_gem_gtt_insert(&ggtt
->base
, &ppgtt
->node
,
1824 GEN6_PD_SIZE
, GEN6_PD_ALIGN
,
1825 I915_COLOR_UNEVICTABLE
,
1826 0, ggtt
->base
.total
,
1831 if (ppgtt
->node
.start
< ggtt
->mappable_end
)
1832 DRM_DEBUG("Forced to use aperture for PDEs\n");
1834 ppgtt
->pd
.base
.ggtt_offset
=
1835 ppgtt
->node
.start
/ PAGE_SIZE
* sizeof(gen6_pte_t
);
1837 ppgtt
->pd_addr
= (gen6_pte_t __iomem
*)ggtt
->gsm
+
1838 ppgtt
->pd
.base
.ggtt_offset
/ sizeof(gen6_pte_t
);
1843 gen6_free_scratch(vm
);
1847 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt
*ppgtt
)
1849 return gen6_ppgtt_allocate_page_directories(ppgtt
);
1852 static void gen6_scratch_va_range(struct i915_hw_ppgtt
*ppgtt
,
1853 u64 start
, u64 length
)
1855 struct i915_page_table
*unused
;
1858 gen6_for_each_pde(unused
, &ppgtt
->pd
, start
, length
, pde
)
1859 ppgtt
->pd
.page_table
[pde
] = ppgtt
->base
.scratch_pt
;
1862 static int gen6_ppgtt_init(struct i915_hw_ppgtt
*ppgtt
)
1864 struct drm_i915_private
*dev_priv
= ppgtt
->base
.i915
;
1865 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
1868 ppgtt
->base
.pte_encode
= ggtt
->base
.pte_encode
;
1869 if (intel_vgpu_active(dev_priv
) || IS_GEN6(dev_priv
))
1870 ppgtt
->switch_mm
= gen6_mm_switch
;
1871 else if (IS_HASWELL(dev_priv
))
1872 ppgtt
->switch_mm
= hsw_mm_switch
;
1873 else if (IS_GEN7(dev_priv
))
1874 ppgtt
->switch_mm
= gen7_mm_switch
;
1878 ret
= gen6_ppgtt_alloc(ppgtt
);
1882 ppgtt
->base
.total
= I915_PDES
* GEN6_PTES
* PAGE_SIZE
;
1884 gen6_scratch_va_range(ppgtt
, 0, ppgtt
->base
.total
);
1885 gen6_write_page_range(ppgtt
, 0, ppgtt
->base
.total
);
1887 ret
= gen6_alloc_va_range(&ppgtt
->base
, 0, ppgtt
->base
.total
);
1889 gen6_ppgtt_cleanup(&ppgtt
->base
);
1893 ppgtt
->base
.clear_range
= gen6_ppgtt_clear_range
;
1894 ppgtt
->base
.insert_entries
= gen6_ppgtt_insert_entries
;
1895 ppgtt
->base
.unbind_vma
= ppgtt_unbind_vma
;
1896 ppgtt
->base
.bind_vma
= ppgtt_bind_vma
;
1897 ppgtt
->base
.cleanup
= gen6_ppgtt_cleanup
;
1898 ppgtt
->debug_dump
= gen6_dump_ppgtt
;
1900 DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
1901 ppgtt
->node
.size
>> 20,
1902 ppgtt
->node
.start
/ PAGE_SIZE
);
1904 DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
1905 ppgtt
->pd
.base
.ggtt_offset
<< 10);
1910 static int __hw_ppgtt_init(struct i915_hw_ppgtt
*ppgtt
,
1911 struct drm_i915_private
*dev_priv
)
1913 ppgtt
->base
.i915
= dev_priv
;
1914 ppgtt
->base
.dma
= &dev_priv
->drm
.pdev
->dev
;
1916 if (INTEL_INFO(dev_priv
)->gen
< 8)
1917 return gen6_ppgtt_init(ppgtt
);
1919 return gen8_ppgtt_init(ppgtt
);
1922 static void i915_address_space_init(struct i915_address_space
*vm
,
1923 struct drm_i915_private
*dev_priv
,
1926 i915_gem_timeline_init(dev_priv
, &vm
->timeline
, name
);
1928 drm_mm_init(&vm
->mm
, 0, vm
->total
);
1929 vm
->mm
.head_node
.color
= I915_COLOR_UNEVICTABLE
;
1931 INIT_LIST_HEAD(&vm
->active_list
);
1932 INIT_LIST_HEAD(&vm
->inactive_list
);
1933 INIT_LIST_HEAD(&vm
->unbound_list
);
1935 list_add_tail(&vm
->global_link
, &dev_priv
->vm_list
);
1936 pagevec_init(&vm
->free_pages
, false);
1939 static void i915_address_space_fini(struct i915_address_space
*vm
)
1941 if (pagevec_count(&vm
->free_pages
))
1942 vm_free_pages_release(vm
, true);
1944 i915_gem_timeline_fini(&vm
->timeline
);
1945 drm_mm_takedown(&vm
->mm
);
1946 list_del(&vm
->global_link
);
1949 static void gtt_write_workarounds(struct drm_i915_private
*dev_priv
)
1951 /* This function is for gtt related workarounds. This function is
1952 * called on driver load and after a GPU reset, so you can place
1953 * workarounds here even if they get overwritten by GPU reset.
1955 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl */
1956 if (IS_BROADWELL(dev_priv
))
1957 I915_WRITE(GEN8_L3_LRA_1_GPGPU
, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW
);
1958 else if (IS_CHERRYVIEW(dev_priv
))
1959 I915_WRITE(GEN8_L3_LRA_1_GPGPU
, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV
);
1960 else if (IS_GEN9_BC(dev_priv
) || IS_GEN10(dev_priv
))
1961 I915_WRITE(GEN8_L3_LRA_1_GPGPU
, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL
);
1962 else if (IS_GEN9_LP(dev_priv
))
1963 I915_WRITE(GEN8_L3_LRA_1_GPGPU
, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT
);
1966 int i915_ppgtt_init_hw(struct drm_i915_private
*dev_priv
)
1968 gtt_write_workarounds(dev_priv
);
1970 /* In the case of execlists, PPGTT is enabled by the context descriptor
1971 * and the PDPs are contained within the context itself. We don't
1972 * need to do anything here. */
1973 if (i915_modparams
.enable_execlists
)
1976 if (!USES_PPGTT(dev_priv
))
1979 if (IS_GEN6(dev_priv
))
1980 gen6_ppgtt_enable(dev_priv
);
1981 else if (IS_GEN7(dev_priv
))
1982 gen7_ppgtt_enable(dev_priv
);
1983 else if (INTEL_GEN(dev_priv
) >= 8)
1984 gen8_ppgtt_enable(dev_priv
);
1986 MISSING_CASE(INTEL_GEN(dev_priv
));
1991 struct i915_hw_ppgtt
*
1992 i915_ppgtt_create(struct drm_i915_private
*dev_priv
,
1993 struct drm_i915_file_private
*fpriv
,
1996 struct i915_hw_ppgtt
*ppgtt
;
1999 ppgtt
= kzalloc(sizeof(*ppgtt
), GFP_KERNEL
);
2001 return ERR_PTR(-ENOMEM
);
2003 ret
= __hw_ppgtt_init(ppgtt
, dev_priv
);
2006 return ERR_PTR(ret
);
2009 kref_init(&ppgtt
->ref
);
2010 i915_address_space_init(&ppgtt
->base
, dev_priv
, name
);
2011 ppgtt
->base
.file
= fpriv
;
2013 trace_i915_ppgtt_create(&ppgtt
->base
);
2018 void i915_ppgtt_close(struct i915_address_space
*vm
)
2020 struct list_head
*phases
[] = {
2027 GEM_BUG_ON(vm
->closed
);
2030 for (phase
= phases
; *phase
; phase
++) {
2031 struct i915_vma
*vma
, *vn
;
2033 list_for_each_entry_safe(vma
, vn
, *phase
, vm_link
)
2034 if (!i915_vma_is_closed(vma
))
2035 i915_vma_close(vma
);
2039 void i915_ppgtt_release(struct kref
*kref
)
2041 struct i915_hw_ppgtt
*ppgtt
=
2042 container_of(kref
, struct i915_hw_ppgtt
, ref
);
2044 trace_i915_ppgtt_release(&ppgtt
->base
);
2046 /* vmas should already be unbound and destroyed */
2047 WARN_ON(!list_empty(&ppgtt
->base
.active_list
));
2048 WARN_ON(!list_empty(&ppgtt
->base
.inactive_list
));
2049 WARN_ON(!list_empty(&ppgtt
->base
.unbound_list
));
2051 ppgtt
->base
.cleanup(&ppgtt
->base
);
2052 i915_address_space_fini(&ppgtt
->base
);
2056 /* Certain Gen5 chipsets require require idling the GPU before
2057 * unmapping anything from the GTT when VT-d is enabled.
2059 static bool needs_idle_maps(struct drm_i915_private
*dev_priv
)
2061 /* Query intel_iommu to see if we need the workaround. Presumably that
2064 return IS_GEN5(dev_priv
) && IS_MOBILE(dev_priv
) && intel_vtd_active();
2067 void i915_check_and_clear_faults(struct drm_i915_private
*dev_priv
)
2069 struct intel_engine_cs
*engine
;
2070 enum intel_engine_id id
;
2072 if (INTEL_INFO(dev_priv
)->gen
< 6)
2075 for_each_engine(engine
, dev_priv
, id
) {
2077 fault_reg
= I915_READ(RING_FAULT_REG(engine
));
2078 if (fault_reg
& RING_FAULT_VALID
) {
2079 DRM_DEBUG_DRIVER("Unexpected fault\n"
2081 "\tAddress space: %s\n"
2084 fault_reg
& PAGE_MASK
,
2085 fault_reg
& RING_FAULT_GTTSEL_MASK
? "GGTT" : "PPGTT",
2086 RING_FAULT_SRCID(fault_reg
),
2087 RING_FAULT_FAULT_TYPE(fault_reg
));
2088 I915_WRITE(RING_FAULT_REG(engine
),
2089 fault_reg
& ~RING_FAULT_VALID
);
2093 /* Engine specific init may not have been done till this point. */
2094 if (dev_priv
->engine
[RCS
])
2095 POSTING_READ(RING_FAULT_REG(dev_priv
->engine
[RCS
]));
2098 void i915_gem_suspend_gtt_mappings(struct drm_i915_private
*dev_priv
)
2100 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
2102 /* Don't bother messing with faults pre GEN6 as we have little
2103 * documentation supporting that it's a good idea.
2105 if (INTEL_GEN(dev_priv
) < 6)
2108 i915_check_and_clear_faults(dev_priv
);
2110 ggtt
->base
.clear_range(&ggtt
->base
, 0, ggtt
->base
.total
);
2112 i915_ggtt_invalidate(dev_priv
);
2115 int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object
*obj
,
2116 struct sg_table
*pages
)
2119 if (dma_map_sg(&obj
->base
.dev
->pdev
->dev
,
2120 pages
->sgl
, pages
->nents
,
2121 PCI_DMA_BIDIRECTIONAL
))
2124 /* If the DMA remap fails, one cause can be that we have
2125 * too many objects pinned in a small remapping table,
2126 * such as swiotlb. Incrementally purge all other objects and
2127 * try again - if there are no more pages to remove from
2128 * the DMA remapper, i915_gem_shrink will return 0.
2130 GEM_BUG_ON(obj
->mm
.pages
== pages
);
2131 } while (i915_gem_shrink(to_i915(obj
->base
.dev
),
2132 obj
->base
.size
>> PAGE_SHIFT
, NULL
,
2134 I915_SHRINK_UNBOUND
|
2135 I915_SHRINK_ACTIVE
));
2140 static void gen8_set_pte(void __iomem
*addr
, gen8_pte_t pte
)
2145 static void gen8_ggtt_insert_page(struct i915_address_space
*vm
,
2148 enum i915_cache_level level
,
2151 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
2152 gen8_pte_t __iomem
*pte
=
2153 (gen8_pte_t __iomem
*)ggtt
->gsm
+ (offset
>> PAGE_SHIFT
);
2155 gen8_set_pte(pte
, gen8_pte_encode(addr
, level
));
2157 ggtt
->invalidate(vm
->i915
);
2160 static void gen8_ggtt_insert_entries(struct i915_address_space
*vm
,
2161 struct i915_vma
*vma
,
2162 enum i915_cache_level level
,
2165 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
2166 struct sgt_iter sgt_iter
;
2167 gen8_pte_t __iomem
*gtt_entries
;
2168 const gen8_pte_t pte_encode
= gen8_pte_encode(0, level
);
2171 gtt_entries
= (gen8_pte_t __iomem
*)ggtt
->gsm
;
2172 gtt_entries
+= vma
->node
.start
>> PAGE_SHIFT
;
2173 for_each_sgt_dma(addr
, sgt_iter
, vma
->pages
)
2174 gen8_set_pte(gtt_entries
++, pte_encode
| addr
);
2178 /* This next bit makes the above posting read even more important. We
2179 * want to flush the TLBs only after we're certain all the PTE updates
2182 ggtt
->invalidate(vm
->i915
);
2185 static void gen6_ggtt_insert_page(struct i915_address_space
*vm
,
2188 enum i915_cache_level level
,
2191 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
2192 gen6_pte_t __iomem
*pte
=
2193 (gen6_pte_t __iomem
*)ggtt
->gsm
+ (offset
>> PAGE_SHIFT
);
2195 iowrite32(vm
->pte_encode(addr
, level
, flags
), pte
);
2197 ggtt
->invalidate(vm
->i915
);
2201 * Binds an object into the global gtt with the specified cache level. The object
2202 * will be accessible to the GPU via commands whose operands reference offsets
2203 * within the global GTT as well as accessible by the GPU through the GMADR
2204 * mapped BAR (dev_priv->mm.gtt->gtt).
2206 static void gen6_ggtt_insert_entries(struct i915_address_space
*vm
,
2207 struct i915_vma
*vma
,
2208 enum i915_cache_level level
,
2211 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
2212 gen6_pte_t __iomem
*entries
= (gen6_pte_t __iomem
*)ggtt
->gsm
;
2213 unsigned int i
= vma
->node
.start
>> PAGE_SHIFT
;
2214 struct sgt_iter iter
;
2216 for_each_sgt_dma(addr
, iter
, vma
->pages
)
2217 iowrite32(vm
->pte_encode(addr
, level
, flags
), &entries
[i
++]);
2220 /* This next bit makes the above posting read even more important. We
2221 * want to flush the TLBs only after we're certain all the PTE updates
2224 ggtt
->invalidate(vm
->i915
);
2227 static void nop_clear_range(struct i915_address_space
*vm
,
2228 u64 start
, u64 length
)
2232 static void gen8_ggtt_clear_range(struct i915_address_space
*vm
,
2233 u64 start
, u64 length
)
2235 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
2236 unsigned first_entry
= start
>> PAGE_SHIFT
;
2237 unsigned num_entries
= length
>> PAGE_SHIFT
;
2238 const gen8_pte_t scratch_pte
=
2239 gen8_pte_encode(vm
->scratch_page
.daddr
, I915_CACHE_LLC
);
2240 gen8_pte_t __iomem
*gtt_base
=
2241 (gen8_pte_t __iomem
*)ggtt
->gsm
+ first_entry
;
2242 const int max_entries
= ggtt_total_entries(ggtt
) - first_entry
;
2245 if (WARN(num_entries
> max_entries
,
2246 "First entry = %d; Num entries = %d (max=%d)\n",
2247 first_entry
, num_entries
, max_entries
))
2248 num_entries
= max_entries
;
2250 for (i
= 0; i
< num_entries
; i
++)
2251 gen8_set_pte(>t_base
[i
], scratch_pte
);
2254 static void bxt_vtd_ggtt_wa(struct i915_address_space
*vm
)
2256 struct drm_i915_private
*dev_priv
= vm
->i915
;
2259 * Make sure the internal GAM fifo has been cleared of all GTT
2260 * writes before exiting stop_machine(). This guarantees that
2261 * any aperture accesses waiting to start in another process
2262 * cannot back up behind the GTT writes causing a hang.
2263 * The register can be any arbitrary GAM register.
2265 POSTING_READ(GFX_FLSH_CNTL_GEN6
);
2268 struct insert_page
{
2269 struct i915_address_space
*vm
;
2272 enum i915_cache_level level
;
2275 static int bxt_vtd_ggtt_insert_page__cb(void *_arg
)
2277 struct insert_page
*arg
= _arg
;
2279 gen8_ggtt_insert_page(arg
->vm
, arg
->addr
, arg
->offset
, arg
->level
, 0);
2280 bxt_vtd_ggtt_wa(arg
->vm
);
2285 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space
*vm
,
2288 enum i915_cache_level level
,
2291 struct insert_page arg
= { vm
, addr
, offset
, level
};
2293 stop_machine(bxt_vtd_ggtt_insert_page__cb
, &arg
, NULL
);
2296 struct insert_entries
{
2297 struct i915_address_space
*vm
;
2298 struct i915_vma
*vma
;
2299 enum i915_cache_level level
;
2302 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg
)
2304 struct insert_entries
*arg
= _arg
;
2306 gen8_ggtt_insert_entries(arg
->vm
, arg
->vma
, arg
->level
, 0);
2307 bxt_vtd_ggtt_wa(arg
->vm
);
2312 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space
*vm
,
2313 struct i915_vma
*vma
,
2314 enum i915_cache_level level
,
2317 struct insert_entries arg
= { vm
, vma
, level
};
2319 stop_machine(bxt_vtd_ggtt_insert_entries__cb
, &arg
, NULL
);
2322 struct clear_range
{
2323 struct i915_address_space
*vm
;
2328 static int bxt_vtd_ggtt_clear_range__cb(void *_arg
)
2330 struct clear_range
*arg
= _arg
;
2332 gen8_ggtt_clear_range(arg
->vm
, arg
->start
, arg
->length
);
2333 bxt_vtd_ggtt_wa(arg
->vm
);
2338 static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space
*vm
,
2342 struct clear_range arg
= { vm
, start
, length
};
2344 stop_machine(bxt_vtd_ggtt_clear_range__cb
, &arg
, NULL
);
2347 static void gen6_ggtt_clear_range(struct i915_address_space
*vm
,
2348 u64 start
, u64 length
)
2350 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
2351 unsigned first_entry
= start
>> PAGE_SHIFT
;
2352 unsigned num_entries
= length
>> PAGE_SHIFT
;
2353 gen6_pte_t scratch_pte
, __iomem
*gtt_base
=
2354 (gen6_pte_t __iomem
*)ggtt
->gsm
+ first_entry
;
2355 const int max_entries
= ggtt_total_entries(ggtt
) - first_entry
;
2358 if (WARN(num_entries
> max_entries
,
2359 "First entry = %d; Num entries = %d (max=%d)\n",
2360 first_entry
, num_entries
, max_entries
))
2361 num_entries
= max_entries
;
2363 scratch_pte
= vm
->pte_encode(vm
->scratch_page
.daddr
,
2366 for (i
= 0; i
< num_entries
; i
++)
2367 iowrite32(scratch_pte
, >t_base
[i
]);
2370 static void i915_ggtt_insert_page(struct i915_address_space
*vm
,
2373 enum i915_cache_level cache_level
,
2376 unsigned int flags
= (cache_level
== I915_CACHE_NONE
) ?
2377 AGP_USER_MEMORY
: AGP_USER_CACHED_MEMORY
;
2379 intel_gtt_insert_page(addr
, offset
>> PAGE_SHIFT
, flags
);
2382 static void i915_ggtt_insert_entries(struct i915_address_space
*vm
,
2383 struct i915_vma
*vma
,
2384 enum i915_cache_level cache_level
,
2387 unsigned int flags
= (cache_level
== I915_CACHE_NONE
) ?
2388 AGP_USER_MEMORY
: AGP_USER_CACHED_MEMORY
;
2390 intel_gtt_insert_sg_entries(vma
->pages
, vma
->node
.start
>> PAGE_SHIFT
,
2394 static void i915_ggtt_clear_range(struct i915_address_space
*vm
,
2395 u64 start
, u64 length
)
2397 intel_gtt_clear_range(start
>> PAGE_SHIFT
, length
>> PAGE_SHIFT
);
2400 static int ggtt_bind_vma(struct i915_vma
*vma
,
2401 enum i915_cache_level cache_level
,
2404 struct drm_i915_private
*i915
= vma
->vm
->i915
;
2405 struct drm_i915_gem_object
*obj
= vma
->obj
;
2408 if (unlikely(!vma
->pages
)) {
2409 int ret
= i915_get_ggtt_vma_pages(vma
);
2414 /* Currently applicable only to VLV */
2417 pte_flags
|= PTE_READ_ONLY
;
2419 intel_runtime_pm_get(i915
);
2420 vma
->vm
->insert_entries(vma
->vm
, vma
, cache_level
, pte_flags
);
2421 intel_runtime_pm_put(i915
);
2424 * Without aliasing PPGTT there's no difference between
2425 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
2426 * upgrade to both bound if we bind either to avoid double-binding.
2428 vma
->flags
|= I915_VMA_GLOBAL_BIND
| I915_VMA_LOCAL_BIND
;
2433 static void ggtt_unbind_vma(struct i915_vma
*vma
)
2435 struct drm_i915_private
*i915
= vma
->vm
->i915
;
2437 intel_runtime_pm_get(i915
);
2438 vma
->vm
->clear_range(vma
->vm
, vma
->node
.start
, vma
->size
);
2439 intel_runtime_pm_put(i915
);
2442 static int aliasing_gtt_bind_vma(struct i915_vma
*vma
,
2443 enum i915_cache_level cache_level
,
2446 struct drm_i915_private
*i915
= vma
->vm
->i915
;
2450 if (unlikely(!vma
->pages
)) {
2451 ret
= i915_get_ggtt_vma_pages(vma
);
2456 /* Currently applicable only to VLV */
2458 if (vma
->obj
->gt_ro
)
2459 pte_flags
|= PTE_READ_ONLY
;
2461 if (flags
& I915_VMA_LOCAL_BIND
) {
2462 struct i915_hw_ppgtt
*appgtt
= i915
->mm
.aliasing_ppgtt
;
2464 if (!(vma
->flags
& I915_VMA_LOCAL_BIND
) &&
2465 appgtt
->base
.allocate_va_range
) {
2466 ret
= appgtt
->base
.allocate_va_range(&appgtt
->base
,
2473 appgtt
->base
.insert_entries(&appgtt
->base
, vma
, cache_level
,
2477 if (flags
& I915_VMA_GLOBAL_BIND
) {
2478 intel_runtime_pm_get(i915
);
2479 vma
->vm
->insert_entries(vma
->vm
, vma
, cache_level
, pte_flags
);
2480 intel_runtime_pm_put(i915
);
2486 if (!(vma
->flags
& (I915_VMA_GLOBAL_BIND
| I915_VMA_LOCAL_BIND
))) {
2487 if (vma
->pages
!= vma
->obj
->mm
.pages
) {
2488 GEM_BUG_ON(!vma
->pages
);
2489 sg_free_table(vma
->pages
);
2497 static void aliasing_gtt_unbind_vma(struct i915_vma
*vma
)
2499 struct drm_i915_private
*i915
= vma
->vm
->i915
;
2501 if (vma
->flags
& I915_VMA_GLOBAL_BIND
) {
2502 intel_runtime_pm_get(i915
);
2503 vma
->vm
->clear_range(vma
->vm
, vma
->node
.start
, vma
->size
);
2504 intel_runtime_pm_put(i915
);
2507 if (vma
->flags
& I915_VMA_LOCAL_BIND
) {
2508 struct i915_address_space
*vm
= &i915
->mm
.aliasing_ppgtt
->base
;
2510 vm
->clear_range(vm
, vma
->node
.start
, vma
->size
);
2514 void i915_gem_gtt_finish_pages(struct drm_i915_gem_object
*obj
,
2515 struct sg_table
*pages
)
2517 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
2518 struct device
*kdev
= &dev_priv
->drm
.pdev
->dev
;
2519 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
2521 if (unlikely(ggtt
->do_idle_maps
)) {
2522 if (i915_gem_wait_for_idle(dev_priv
, 0)) {
2523 DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
2524 /* Wait a bit, in hopes it avoids the hang */
2529 dma_unmap_sg(kdev
, pages
->sgl
, pages
->nents
, PCI_DMA_BIDIRECTIONAL
);
2532 static void i915_gtt_color_adjust(const struct drm_mm_node
*node
,
2533 unsigned long color
,
2537 if (node
->allocated
&& node
->color
!= color
)
2538 *start
+= I915_GTT_PAGE_SIZE
;
2540 /* Also leave a space between the unallocated reserved node after the
2541 * GTT and any objects within the GTT, i.e. we use the color adjustment
2542 * to insert a guard page to prevent prefetches crossing over the
2545 node
= list_next_entry(node
, node_list
);
2546 if (node
->color
!= color
)
2547 *end
-= I915_GTT_PAGE_SIZE
;
2550 int i915_gem_init_aliasing_ppgtt(struct drm_i915_private
*i915
)
2552 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
2553 struct i915_hw_ppgtt
*ppgtt
;
2556 ppgtt
= i915_ppgtt_create(i915
, ERR_PTR(-EPERM
), "[alias]");
2558 return PTR_ERR(ppgtt
);
2560 if (WARN_ON(ppgtt
->base
.total
< ggtt
->base
.total
)) {
2565 if (ppgtt
->base
.allocate_va_range
) {
2566 /* Note we only pre-allocate as far as the end of the global
2567 * GTT. On 48b / 4-level page-tables, the difference is very,
2568 * very significant! We have to preallocate as GVT/vgpu does
2569 * not like the page directory disappearing.
2571 err
= ppgtt
->base
.allocate_va_range(&ppgtt
->base
,
2572 0, ggtt
->base
.total
);
2577 i915
->mm
.aliasing_ppgtt
= ppgtt
;
2579 WARN_ON(ggtt
->base
.bind_vma
!= ggtt_bind_vma
);
2580 ggtt
->base
.bind_vma
= aliasing_gtt_bind_vma
;
2582 WARN_ON(ggtt
->base
.unbind_vma
!= ggtt_unbind_vma
);
2583 ggtt
->base
.unbind_vma
= aliasing_gtt_unbind_vma
;
2588 i915_ppgtt_put(ppgtt
);
2592 void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private
*i915
)
2594 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
2595 struct i915_hw_ppgtt
*ppgtt
;
2597 ppgtt
= fetch_and_zero(&i915
->mm
.aliasing_ppgtt
);
2601 i915_ppgtt_put(ppgtt
);
2603 ggtt
->base
.bind_vma
= ggtt_bind_vma
;
2604 ggtt
->base
.unbind_vma
= ggtt_unbind_vma
;
2607 int i915_gem_init_ggtt(struct drm_i915_private
*dev_priv
)
2609 /* Let GEM Manage all of the aperture.
2611 * However, leave one page at the end still bound to the scratch page.
2612 * There are a number of places where the hardware apparently prefetches
2613 * past the end of the object, and we've seen multiple hangs with the
2614 * GPU head pointer stuck in a batchbuffer bound at the last page of the
2615 * aperture. One page should be enough to keep any prefetching inside
2618 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
2619 unsigned long hole_start
, hole_end
;
2620 struct drm_mm_node
*entry
;
2623 ret
= intel_vgt_balloon(dev_priv
);
2627 /* Reserve a mappable slot for our lockless error capture */
2628 ret
= drm_mm_insert_node_in_range(&ggtt
->base
.mm
, &ggtt
->error_capture
,
2629 PAGE_SIZE
, 0, I915_COLOR_UNEVICTABLE
,
2630 0, ggtt
->mappable_end
,
2635 /* Clear any non-preallocated blocks */
2636 drm_mm_for_each_hole(entry
, &ggtt
->base
.mm
, hole_start
, hole_end
) {
2637 DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
2638 hole_start
, hole_end
);
2639 ggtt
->base
.clear_range(&ggtt
->base
, hole_start
,
2640 hole_end
- hole_start
);
2643 /* And finally clear the reserved guard page */
2644 ggtt
->base
.clear_range(&ggtt
->base
,
2645 ggtt
->base
.total
- PAGE_SIZE
, PAGE_SIZE
);
2647 if (USES_PPGTT(dev_priv
) && !USES_FULL_PPGTT(dev_priv
)) {
2648 ret
= i915_gem_init_aliasing_ppgtt(dev_priv
);
2656 drm_mm_remove_node(&ggtt
->error_capture
);
2661 * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
2662 * @dev_priv: i915 device
2664 void i915_ggtt_cleanup_hw(struct drm_i915_private
*dev_priv
)
2666 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
2667 struct i915_vma
*vma
, *vn
;
2668 struct pagevec
*pvec
;
2670 ggtt
->base
.closed
= true;
2672 mutex_lock(&dev_priv
->drm
.struct_mutex
);
2673 WARN_ON(!list_empty(&ggtt
->base
.active_list
));
2674 list_for_each_entry_safe(vma
, vn
, &ggtt
->base
.inactive_list
, vm_link
)
2675 WARN_ON(i915_vma_unbind(vma
));
2676 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
2678 i915_gem_cleanup_stolen(&dev_priv
->drm
);
2680 mutex_lock(&dev_priv
->drm
.struct_mutex
);
2681 i915_gem_fini_aliasing_ppgtt(dev_priv
);
2683 if (drm_mm_node_allocated(&ggtt
->error_capture
))
2684 drm_mm_remove_node(&ggtt
->error_capture
);
2686 if (drm_mm_initialized(&ggtt
->base
.mm
)) {
2687 intel_vgt_deballoon(dev_priv
);
2688 i915_address_space_fini(&ggtt
->base
);
2691 ggtt
->base
.cleanup(&ggtt
->base
);
2693 pvec
= &dev_priv
->mm
.wc_stash
;
2695 set_pages_array_wb(pvec
->pages
, pvec
->nr
);
2696 __pagevec_release(pvec
);
2699 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
2701 arch_phys_wc_del(ggtt
->mtrr
);
2702 io_mapping_fini(&ggtt
->mappable
);
2705 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl
)
2707 snb_gmch_ctl
>>= SNB_GMCH_GGMS_SHIFT
;
2708 snb_gmch_ctl
&= SNB_GMCH_GGMS_MASK
;
2709 return snb_gmch_ctl
<< 20;
2712 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl
)
2714 bdw_gmch_ctl
>>= BDW_GMCH_GGMS_SHIFT
;
2715 bdw_gmch_ctl
&= BDW_GMCH_GGMS_MASK
;
2717 bdw_gmch_ctl
= 1 << bdw_gmch_ctl
;
2719 #ifdef CONFIG_X86_32
2720 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
2721 if (bdw_gmch_ctl
> 4)
2725 return bdw_gmch_ctl
<< 20;
2728 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl
)
2730 gmch_ctrl
>>= SNB_GMCH_GGMS_SHIFT
;
2731 gmch_ctrl
&= SNB_GMCH_GGMS_MASK
;
2734 return 1 << (20 + gmch_ctrl
);
2739 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl
)
2741 snb_gmch_ctl
>>= SNB_GMCH_GMS_SHIFT
;
2742 snb_gmch_ctl
&= SNB_GMCH_GMS_MASK
;
2743 return (size_t)snb_gmch_ctl
<< 25; /* 32 MB units */
2746 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl
)
2748 bdw_gmch_ctl
>>= BDW_GMCH_GMS_SHIFT
;
2749 bdw_gmch_ctl
&= BDW_GMCH_GMS_MASK
;
2750 return (size_t)bdw_gmch_ctl
<< 25; /* 32 MB units */
2753 static size_t chv_get_stolen_size(u16 gmch_ctrl
)
2755 gmch_ctrl
>>= SNB_GMCH_GMS_SHIFT
;
2756 gmch_ctrl
&= SNB_GMCH_GMS_MASK
;
2759 * 0x0 to 0x10: 32MB increments starting at 0MB
2760 * 0x11 to 0x16: 4MB increments starting at 8MB
2761 * 0x17 to 0x1d: 4MB increments start at 36MB
2763 if (gmch_ctrl
< 0x11)
2764 return (size_t)gmch_ctrl
<< 25;
2765 else if (gmch_ctrl
< 0x17)
2766 return (size_t)(gmch_ctrl
- 0x11 + 2) << 22;
2768 return (size_t)(gmch_ctrl
- 0x17 + 9) << 22;
2771 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl
)
2773 gen9_gmch_ctl
>>= BDW_GMCH_GMS_SHIFT
;
2774 gen9_gmch_ctl
&= BDW_GMCH_GMS_MASK
;
2776 if (gen9_gmch_ctl
< 0xf0)
2777 return (size_t)gen9_gmch_ctl
<< 25; /* 32 MB units */
2779 /* 4MB increments starting at 0xf0 for 4MB */
2780 return (size_t)(gen9_gmch_ctl
- 0xf0 + 1) << 22;
2783 static int ggtt_probe_common(struct i915_ggtt
*ggtt
, u64 size
)
2785 struct drm_i915_private
*dev_priv
= ggtt
->base
.i915
;
2786 struct pci_dev
*pdev
= dev_priv
->drm
.pdev
;
2787 phys_addr_t phys_addr
;
2790 /* For Modern GENs the PTEs and register space are split in the BAR */
2791 phys_addr
= pci_resource_start(pdev
, 0) + pci_resource_len(pdev
, 0) / 2;
2794 * On BXT+/CNL+ writes larger than 64 bit to the GTT pagetable range
2795 * will be dropped. For WC mappings in general we have 64 byte burst
2796 * writes when the WC buffer is flushed, so we can't use it, but have to
2797 * resort to an uncached mapping. The WC issue is easily caught by the
2798 * readback check when writing GTT PTE entries.
2800 if (IS_GEN9_LP(dev_priv
) || INTEL_GEN(dev_priv
) >= 10)
2801 ggtt
->gsm
= ioremap_nocache(phys_addr
, size
);
2803 ggtt
->gsm
= ioremap_wc(phys_addr
, size
);
2805 DRM_ERROR("Failed to map the ggtt page table\n");
2809 ret
= setup_scratch_page(&ggtt
->base
, GFP_DMA32
);
2811 DRM_ERROR("Scratch setup failed\n");
2812 /* iounmap will also get called at remove, but meh */
2820 static struct intel_ppat_entry
*
2821 __alloc_ppat_entry(struct intel_ppat
*ppat
, unsigned int index
, u8 value
)
2823 struct intel_ppat_entry
*entry
= &ppat
->entries
[index
];
2825 GEM_BUG_ON(index
>= ppat
->max_entries
);
2826 GEM_BUG_ON(test_bit(index
, ppat
->used
));
2829 entry
->value
= value
;
2830 kref_init(&entry
->ref
);
2831 set_bit(index
, ppat
->used
);
2832 set_bit(index
, ppat
->dirty
);
2837 static void __free_ppat_entry(struct intel_ppat_entry
*entry
)
2839 struct intel_ppat
*ppat
= entry
->ppat
;
2840 unsigned int index
= entry
- ppat
->entries
;
2842 GEM_BUG_ON(index
>= ppat
->max_entries
);
2843 GEM_BUG_ON(!test_bit(index
, ppat
->used
));
2845 entry
->value
= ppat
->clear_value
;
2846 clear_bit(index
, ppat
->used
);
2847 set_bit(index
, ppat
->dirty
);
2851 * intel_ppat_get - get a usable PPAT entry
2852 * @i915: i915 device instance
2853 * @value: the PPAT value required by the caller
2855 * The function tries to search if there is an existing PPAT entry which
2856 * matches with the required value. If perfectly matched, the existing PPAT
2857 * entry will be used. If only partially matched, it will try to check if
2858 * there is any available PPAT index. If yes, it will allocate a new PPAT
2859 * index for the required entry and update the HW. If not, the partially
2860 * matched entry will be used.
2862 const struct intel_ppat_entry
*
2863 intel_ppat_get(struct drm_i915_private
*i915
, u8 value
)
2865 struct intel_ppat
*ppat
= &i915
->ppat
;
2866 struct intel_ppat_entry
*entry
;
2867 unsigned int scanned
, best_score
;
2870 GEM_BUG_ON(!ppat
->max_entries
);
2872 scanned
= best_score
= 0;
2873 for_each_set_bit(i
, ppat
->used
, ppat
->max_entries
) {
2876 score
= ppat
->match(ppat
->entries
[i
].value
, value
);
2877 if (score
> best_score
) {
2878 entry
= &ppat
->entries
[i
];
2879 if (score
== INTEL_PPAT_PERFECT_MATCH
) {
2880 kref_get(&entry
->ref
);
2888 if (scanned
== ppat
->max_entries
) {
2890 return ERR_PTR(-ENOSPC
);
2892 kref_get(&entry
->ref
);
2896 i
= find_first_zero_bit(ppat
->used
, ppat
->max_entries
);
2897 entry
= __alloc_ppat_entry(ppat
, i
, value
);
2898 ppat
->update_hw(i915
);
2902 static void release_ppat(struct kref
*kref
)
2904 struct intel_ppat_entry
*entry
=
2905 container_of(kref
, struct intel_ppat_entry
, ref
);
2906 struct drm_i915_private
*i915
= entry
->ppat
->i915
;
2908 __free_ppat_entry(entry
);
2909 entry
->ppat
->update_hw(i915
);
2913 * intel_ppat_put - put back the PPAT entry got from intel_ppat_get()
2914 * @entry: an intel PPAT entry
2916 * Put back the PPAT entry got from intel_ppat_get(). If the PPAT index of the
2917 * entry is dynamically allocated, its reference count will be decreased. Once
2918 * the reference count becomes into zero, the PPAT index becomes free again.
2920 void intel_ppat_put(const struct intel_ppat_entry
*entry
)
2922 struct intel_ppat
*ppat
= entry
->ppat
;
2923 unsigned int index
= entry
- ppat
->entries
;
2925 GEM_BUG_ON(!ppat
->max_entries
);
2927 kref_put(&ppat
->entries
[index
].ref
, release_ppat
);
2930 static void cnl_private_pat_update_hw(struct drm_i915_private
*dev_priv
)
2932 struct intel_ppat
*ppat
= &dev_priv
->ppat
;
2935 for_each_set_bit(i
, ppat
->dirty
, ppat
->max_entries
) {
2936 I915_WRITE(GEN10_PAT_INDEX(i
), ppat
->entries
[i
].value
);
2937 clear_bit(i
, ppat
->dirty
);
2941 static void bdw_private_pat_update_hw(struct drm_i915_private
*dev_priv
)
2943 struct intel_ppat
*ppat
= &dev_priv
->ppat
;
2947 for (i
= 0; i
< ppat
->max_entries
; i
++)
2948 pat
|= GEN8_PPAT(i
, ppat
->entries
[i
].value
);
2950 bitmap_clear(ppat
->dirty
, 0, ppat
->max_entries
);
2952 I915_WRITE(GEN8_PRIVATE_PAT_LO
, lower_32_bits(pat
));
2953 I915_WRITE(GEN8_PRIVATE_PAT_HI
, upper_32_bits(pat
));
2956 static unsigned int bdw_private_pat_match(u8 src
, u8 dst
)
2958 unsigned int score
= 0;
2965 /* Cache attribute has to be matched. */
2966 if (GEN8_PPAT_GET_CA(src
) != GEN8_PPAT_GET_CA(dst
))
2971 if (GEN8_PPAT_GET_TC(src
) == GEN8_PPAT_GET_TC(dst
))
2974 if (GEN8_PPAT_GET_AGE(src
) == GEN8_PPAT_GET_AGE(dst
))
2977 if (score
== (AGE_MATCH
| TC_MATCH
| CA_MATCH
))
2978 return INTEL_PPAT_PERFECT_MATCH
;
2983 static unsigned int chv_private_pat_match(u8 src
, u8 dst
)
2985 return (CHV_PPAT_GET_SNOOP(src
) == CHV_PPAT_GET_SNOOP(dst
)) ?
2986 INTEL_PPAT_PERFECT_MATCH
: 0;
2989 static void cnl_setup_private_ppat(struct intel_ppat
*ppat
)
2991 ppat
->max_entries
= 8;
2992 ppat
->update_hw
= cnl_private_pat_update_hw
;
2993 ppat
->match
= bdw_private_pat_match
;
2994 ppat
->clear_value
= GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(3);
2996 /* XXX: spec is unclear if this is still needed for CNL+ */
2997 if (!USES_PPGTT(ppat
->i915
)) {
2998 __alloc_ppat_entry(ppat
, 0, GEN8_PPAT_UC
);
3002 __alloc_ppat_entry(ppat
, 0, GEN8_PPAT_WB
| GEN8_PPAT_LLC
);
3003 __alloc_ppat_entry(ppat
, 1, GEN8_PPAT_WC
| GEN8_PPAT_LLCELLC
);
3004 __alloc_ppat_entry(ppat
, 2, GEN8_PPAT_WT
| GEN8_PPAT_LLCELLC
);
3005 __alloc_ppat_entry(ppat
, 3, GEN8_PPAT_UC
);
3006 __alloc_ppat_entry(ppat
, 4, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(0));
3007 __alloc_ppat_entry(ppat
, 5, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(1));
3008 __alloc_ppat_entry(ppat
, 6, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(2));
3009 __alloc_ppat_entry(ppat
, 7, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(3));
3012 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
3013 * bits. When using advanced contexts each context stores its own PAT, but
3014 * writing this data shouldn't be harmful even in those cases. */
3015 static void bdw_setup_private_ppat(struct intel_ppat
*ppat
)
3017 ppat
->max_entries
= 8;
3018 ppat
->update_hw
= bdw_private_pat_update_hw
;
3019 ppat
->match
= bdw_private_pat_match
;
3020 ppat
->clear_value
= GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(3);
3022 if (!USES_PPGTT(ppat
->i915
)) {
3023 /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
3024 * so RTL will always use the value corresponding to
3026 * So let's disable cache for GGTT to avoid screen corruptions.
3027 * MOCS still can be used though.
3028 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
3029 * before this patch, i.e. the same uncached + snooping access
3030 * like on gen6/7 seems to be in effect.
3031 * - So this just fixes blitter/render access. Again it looks
3032 * like it's not just uncached access, but uncached + snooping.
3033 * So we can still hold onto all our assumptions wrt cpu
3034 * clflushing on LLC machines.
3036 __alloc_ppat_entry(ppat
, 0, GEN8_PPAT_UC
);
3040 __alloc_ppat_entry(ppat
, 0, GEN8_PPAT_WB
| GEN8_PPAT_LLC
); /* for normal objects, no eLLC */
3041 __alloc_ppat_entry(ppat
, 1, GEN8_PPAT_WC
| GEN8_PPAT_LLCELLC
); /* for something pointing to ptes? */
3042 __alloc_ppat_entry(ppat
, 2, GEN8_PPAT_WT
| GEN8_PPAT_LLCELLC
); /* for scanout with eLLC */
3043 __alloc_ppat_entry(ppat
, 3, GEN8_PPAT_UC
); /* Uncached objects, mostly for scanout */
3044 __alloc_ppat_entry(ppat
, 4, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(0));
3045 __alloc_ppat_entry(ppat
, 5, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(1));
3046 __alloc_ppat_entry(ppat
, 6, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(2));
3047 __alloc_ppat_entry(ppat
, 7, GEN8_PPAT_WB
| GEN8_PPAT_LLCELLC
| GEN8_PPAT_AGE(3));
3050 static void chv_setup_private_ppat(struct intel_ppat
*ppat
)
3052 ppat
->max_entries
= 8;
3053 ppat
->update_hw
= bdw_private_pat_update_hw
;
3054 ppat
->match
= chv_private_pat_match
;
3055 ppat
->clear_value
= CHV_PPAT_SNOOP
;
3058 * Map WB on BDW to snooped on CHV.
3060 * Only the snoop bit has meaning for CHV, the rest is
3063 * The hardware will never snoop for certain types of accesses:
3064 * - CPU GTT (GMADR->GGTT->no snoop->memory)
3065 * - PPGTT page tables
3066 * - some other special cycles
3068 * As with BDW, we also need to consider the following for GT accesses:
3069 * "For GGTT, there is NO pat_sel[2:0] from the entry,
3070 * so RTL will always use the value corresponding to
3072 * Which means we must set the snoop bit in PAT entry 0
3073 * in order to keep the global status page working.
3076 __alloc_ppat_entry(ppat
, 0, CHV_PPAT_SNOOP
);
3077 __alloc_ppat_entry(ppat
, 1, 0);
3078 __alloc_ppat_entry(ppat
, 2, 0);
3079 __alloc_ppat_entry(ppat
, 3, 0);
3080 __alloc_ppat_entry(ppat
, 4, CHV_PPAT_SNOOP
);
3081 __alloc_ppat_entry(ppat
, 5, CHV_PPAT_SNOOP
);
3082 __alloc_ppat_entry(ppat
, 6, CHV_PPAT_SNOOP
);
3083 __alloc_ppat_entry(ppat
, 7, CHV_PPAT_SNOOP
);
3086 static void gen6_gmch_remove(struct i915_address_space
*vm
)
3088 struct i915_ggtt
*ggtt
= i915_vm_to_ggtt(vm
);
3091 cleanup_scratch_page(vm
);
3094 static void setup_private_pat(struct drm_i915_private
*dev_priv
)
3096 struct intel_ppat
*ppat
= &dev_priv
->ppat
;
3099 ppat
->i915
= dev_priv
;
3101 if (INTEL_GEN(dev_priv
) >= 10)
3102 cnl_setup_private_ppat(ppat
);
3103 else if (IS_CHERRYVIEW(dev_priv
) || IS_GEN9_LP(dev_priv
))
3104 chv_setup_private_ppat(ppat
);
3106 bdw_setup_private_ppat(ppat
);
3108 GEM_BUG_ON(ppat
->max_entries
> INTEL_MAX_PPAT_ENTRIES
);
3110 for_each_clear_bit(i
, ppat
->used
, ppat
->max_entries
) {
3111 ppat
->entries
[i
].value
= ppat
->clear_value
;
3112 ppat
->entries
[i
].ppat
= ppat
;
3113 set_bit(i
, ppat
->dirty
);
3116 ppat
->update_hw(dev_priv
);
3119 static int gen8_gmch_probe(struct i915_ggtt
*ggtt
)
3121 struct drm_i915_private
*dev_priv
= ggtt
->base
.i915
;
3122 struct pci_dev
*pdev
= dev_priv
->drm
.pdev
;
3127 /* TODO: We're not aware of mappable constraints on gen8 yet */
3128 ggtt
->mappable_base
= pci_resource_start(pdev
, 2);
3129 ggtt
->mappable_end
= pci_resource_len(pdev
, 2);
3131 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(39));
3133 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(39));
3135 DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err
);
3137 pci_read_config_word(pdev
, SNB_GMCH_CTRL
, &snb_gmch_ctl
);
3139 if (INTEL_GEN(dev_priv
) >= 9) {
3140 ggtt
->stolen_size
= gen9_get_stolen_size(snb_gmch_ctl
);
3141 size
= gen8_get_total_gtt_size(snb_gmch_ctl
);
3142 } else if (IS_CHERRYVIEW(dev_priv
)) {
3143 ggtt
->stolen_size
= chv_get_stolen_size(snb_gmch_ctl
);
3144 size
= chv_get_total_gtt_size(snb_gmch_ctl
);
3146 ggtt
->stolen_size
= gen8_get_stolen_size(snb_gmch_ctl
);
3147 size
= gen8_get_total_gtt_size(snb_gmch_ctl
);
3150 ggtt
->base
.total
= (size
/ sizeof(gen8_pte_t
)) << PAGE_SHIFT
;
3151 ggtt
->base
.cleanup
= gen6_gmch_remove
;
3152 ggtt
->base
.bind_vma
= ggtt_bind_vma
;
3153 ggtt
->base
.unbind_vma
= ggtt_unbind_vma
;
3154 ggtt
->base
.insert_page
= gen8_ggtt_insert_page
;
3155 ggtt
->base
.clear_range
= nop_clear_range
;
3156 if (!USES_FULL_PPGTT(dev_priv
) || intel_scanout_needs_vtd_wa(dev_priv
))
3157 ggtt
->base
.clear_range
= gen8_ggtt_clear_range
;
3159 ggtt
->base
.insert_entries
= gen8_ggtt_insert_entries
;
3161 /* Serialize GTT updates with aperture access on BXT if VT-d is on. */
3162 if (intel_ggtt_update_needs_vtd_wa(dev_priv
)) {
3163 ggtt
->base
.insert_entries
= bxt_vtd_ggtt_insert_entries__BKL
;
3164 ggtt
->base
.insert_page
= bxt_vtd_ggtt_insert_page__BKL
;
3165 if (ggtt
->base
.clear_range
!= nop_clear_range
)
3166 ggtt
->base
.clear_range
= bxt_vtd_ggtt_clear_range__BKL
;
3169 ggtt
->invalidate
= gen6_ggtt_invalidate
;
3171 setup_private_pat(dev_priv
);
3173 return ggtt_probe_common(ggtt
, size
);
3176 static int gen6_gmch_probe(struct i915_ggtt
*ggtt
)
3178 struct drm_i915_private
*dev_priv
= ggtt
->base
.i915
;
3179 struct pci_dev
*pdev
= dev_priv
->drm
.pdev
;
3184 ggtt
->mappable_base
= pci_resource_start(pdev
, 2);
3185 ggtt
->mappable_end
= pci_resource_len(pdev
, 2);
3187 /* 64/512MB is the current min/max we actually know of, but this is just
3188 * a coarse sanity check.
3190 if (ggtt
->mappable_end
< (64<<20) || ggtt
->mappable_end
> (512<<20)) {
3191 DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt
->mappable_end
);
3195 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(40));
3197 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(40));
3199 DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err
);
3200 pci_read_config_word(pdev
, SNB_GMCH_CTRL
, &snb_gmch_ctl
);
3202 ggtt
->stolen_size
= gen6_get_stolen_size(snb_gmch_ctl
);
3204 size
= gen6_get_total_gtt_size(snb_gmch_ctl
);
3205 ggtt
->base
.total
= (size
/ sizeof(gen6_pte_t
)) << PAGE_SHIFT
;
3207 ggtt
->base
.clear_range
= gen6_ggtt_clear_range
;
3208 ggtt
->base
.insert_page
= gen6_ggtt_insert_page
;
3209 ggtt
->base
.insert_entries
= gen6_ggtt_insert_entries
;
3210 ggtt
->base
.bind_vma
= ggtt_bind_vma
;
3211 ggtt
->base
.unbind_vma
= ggtt_unbind_vma
;
3212 ggtt
->base
.cleanup
= gen6_gmch_remove
;
3214 ggtt
->invalidate
= gen6_ggtt_invalidate
;
3216 if (HAS_EDRAM(dev_priv
))
3217 ggtt
->base
.pte_encode
= iris_pte_encode
;
3218 else if (IS_HASWELL(dev_priv
))
3219 ggtt
->base
.pte_encode
= hsw_pte_encode
;
3220 else if (IS_VALLEYVIEW(dev_priv
))
3221 ggtt
->base
.pte_encode
= byt_pte_encode
;
3222 else if (INTEL_GEN(dev_priv
) >= 7)
3223 ggtt
->base
.pte_encode
= ivb_pte_encode
;
3225 ggtt
->base
.pte_encode
= snb_pte_encode
;
3227 return ggtt_probe_common(ggtt
, size
);
3230 static void i915_gmch_remove(struct i915_address_space
*vm
)
3232 intel_gmch_remove();
3235 static int i915_gmch_probe(struct i915_ggtt
*ggtt
)
3237 struct drm_i915_private
*dev_priv
= ggtt
->base
.i915
;
3240 ret
= intel_gmch_probe(dev_priv
->bridge_dev
, dev_priv
->drm
.pdev
, NULL
);
3242 DRM_ERROR("failed to set up gmch\n");
3246 intel_gtt_get(&ggtt
->base
.total
,
3248 &ggtt
->mappable_base
,
3249 &ggtt
->mappable_end
);
3251 ggtt
->do_idle_maps
= needs_idle_maps(dev_priv
);
3252 ggtt
->base
.insert_page
= i915_ggtt_insert_page
;
3253 ggtt
->base
.insert_entries
= i915_ggtt_insert_entries
;
3254 ggtt
->base
.clear_range
= i915_ggtt_clear_range
;
3255 ggtt
->base
.bind_vma
= ggtt_bind_vma
;
3256 ggtt
->base
.unbind_vma
= ggtt_unbind_vma
;
3257 ggtt
->base
.cleanup
= i915_gmch_remove
;
3259 ggtt
->invalidate
= gmch_ggtt_invalidate
;
3261 if (unlikely(ggtt
->do_idle_maps
))
3262 DRM_INFO("applying Ironlake quirks for intel_iommu\n");
3268 * i915_ggtt_probe_hw - Probe GGTT hardware location
3269 * @dev_priv: i915 device
3271 int i915_ggtt_probe_hw(struct drm_i915_private
*dev_priv
)
3273 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
3276 ggtt
->base
.i915
= dev_priv
;
3277 ggtt
->base
.dma
= &dev_priv
->drm
.pdev
->dev
;
3279 if (INTEL_GEN(dev_priv
) <= 5)
3280 ret
= i915_gmch_probe(ggtt
);
3281 else if (INTEL_GEN(dev_priv
) < 8)
3282 ret
= gen6_gmch_probe(ggtt
);
3284 ret
= gen8_gmch_probe(ggtt
);
3288 /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
3289 * This is easier than doing range restriction on the fly, as we
3290 * currently don't have any bits spare to pass in this upper
3293 if (HAS_GUC(dev_priv
) && i915_modparams
.enable_guc_loading
) {
3294 ggtt
->base
.total
= min_t(u64
, ggtt
->base
.total
, GUC_GGTT_TOP
);
3295 ggtt
->mappable_end
= min(ggtt
->mappable_end
, ggtt
->base
.total
);
3298 if ((ggtt
->base
.total
- 1) >> 32) {
3299 DRM_ERROR("We never expected a Global GTT with more than 32bits"
3300 " of address space! Found %lldM!\n",
3301 ggtt
->base
.total
>> 20);
3302 ggtt
->base
.total
= 1ULL << 32;
3303 ggtt
->mappable_end
= min(ggtt
->mappable_end
, ggtt
->base
.total
);
3306 if (ggtt
->mappable_end
> ggtt
->base
.total
) {
3307 DRM_ERROR("mappable aperture extends past end of GGTT,"
3308 " aperture=%llx, total=%llx\n",
3309 ggtt
->mappable_end
, ggtt
->base
.total
);
3310 ggtt
->mappable_end
= ggtt
->base
.total
;
3313 /* GMADR is the PCI mmio aperture into the global GTT. */
3314 DRM_INFO("Memory usable by graphics device = %lluM\n",
3315 ggtt
->base
.total
>> 20);
3316 DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt
->mappable_end
>> 20);
3317 DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt
->stolen_size
>> 20);
3318 if (intel_vtd_active())
3319 DRM_INFO("VT-d active for gfx access\n");
3325 * i915_ggtt_init_hw - Initialize GGTT hardware
3326 * @dev_priv: i915 device
3328 int i915_ggtt_init_hw(struct drm_i915_private
*dev_priv
)
3330 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
3333 INIT_LIST_HEAD(&dev_priv
->vm_list
);
3335 /* Note that we use page colouring to enforce a guard page at the
3336 * end of the address space. This is required as the CS may prefetch
3337 * beyond the end of the batch buffer, across the page boundary,
3338 * and beyond the end of the GTT if we do not provide a guard.
3340 mutex_lock(&dev_priv
->drm
.struct_mutex
);
3341 i915_address_space_init(&ggtt
->base
, dev_priv
, "[global]");
3342 if (!HAS_LLC(dev_priv
) && !USES_PPGTT(dev_priv
))
3343 ggtt
->base
.mm
.color_adjust
= i915_gtt_color_adjust
;
3344 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
3346 if (!io_mapping_init_wc(&dev_priv
->ggtt
.mappable
,
3347 dev_priv
->ggtt
.mappable_base
,
3348 dev_priv
->ggtt
.mappable_end
)) {
3350 goto out_gtt_cleanup
;
3353 ggtt
->mtrr
= arch_phys_wc_add(ggtt
->mappable_base
, ggtt
->mappable_end
);
3356 * Initialise stolen early so that we may reserve preallocated
3357 * objects for the BIOS to KMS transition.
3359 ret
= i915_gem_init_stolen(dev_priv
);
3361 goto out_gtt_cleanup
;
3366 ggtt
->base
.cleanup(&ggtt
->base
);
3370 int i915_ggtt_enable_hw(struct drm_i915_private
*dev_priv
)
3372 if (INTEL_GEN(dev_priv
) < 6 && !intel_enable_gtt())
3378 void i915_ggtt_enable_guc(struct drm_i915_private
*i915
)
3380 GEM_BUG_ON(i915
->ggtt
.invalidate
!= gen6_ggtt_invalidate
);
3382 i915
->ggtt
.invalidate
= guc_ggtt_invalidate
;
3385 void i915_ggtt_disable_guc(struct drm_i915_private
*i915
)
3387 /* We should only be called after i915_ggtt_enable_guc() */
3388 GEM_BUG_ON(i915
->ggtt
.invalidate
!= guc_ggtt_invalidate
);
3390 i915
->ggtt
.invalidate
= gen6_ggtt_invalidate
;
3393 void i915_gem_restore_gtt_mappings(struct drm_i915_private
*dev_priv
)
3395 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
3396 struct drm_i915_gem_object
*obj
, *on
;
3398 i915_check_and_clear_faults(dev_priv
);
3400 /* First fill our portion of the GTT with scratch pages */
3401 ggtt
->base
.clear_range(&ggtt
->base
, 0, ggtt
->base
.total
);
3403 ggtt
->base
.closed
= true; /* skip rewriting PTE on VMA unbind */
3405 /* clflush objects bound into the GGTT and rebind them. */
3406 list_for_each_entry_safe(obj
, on
,
3407 &dev_priv
->mm
.bound_list
, global_link
) {
3408 bool ggtt_bound
= false;
3409 struct i915_vma
*vma
;
3411 list_for_each_entry(vma
, &obj
->vma_list
, obj_link
) {
3412 if (vma
->vm
!= &ggtt
->base
)
3415 if (!i915_vma_unbind(vma
))
3418 WARN_ON(i915_vma_bind(vma
, obj
->cache_level
,
3424 WARN_ON(i915_gem_object_set_to_gtt_domain(obj
, false));
3427 ggtt
->base
.closed
= false;
3429 if (INTEL_GEN(dev_priv
) >= 8) {
3430 struct intel_ppat
*ppat
= &dev_priv
->ppat
;
3432 bitmap_set(ppat
->dirty
, 0, ppat
->max_entries
);
3433 dev_priv
->ppat
.update_hw(dev_priv
);
3437 if (USES_PPGTT(dev_priv
)) {
3438 struct i915_address_space
*vm
;
3440 list_for_each_entry(vm
, &dev_priv
->vm_list
, global_link
) {
3441 struct i915_hw_ppgtt
*ppgtt
;
3443 if (i915_is_ggtt(vm
))
3444 ppgtt
= dev_priv
->mm
.aliasing_ppgtt
;
3446 ppgtt
= i915_vm_to_ppgtt(vm
);
3448 gen6_write_page_range(ppgtt
, 0, ppgtt
->base
.total
);
3452 i915_ggtt_invalidate(dev_priv
);
3455 static struct scatterlist
*
3456 rotate_pages(const dma_addr_t
*in
, unsigned int offset
,
3457 unsigned int width
, unsigned int height
,
3458 unsigned int stride
,
3459 struct sg_table
*st
, struct scatterlist
*sg
)
3461 unsigned int column
, row
;
3462 unsigned int src_idx
;
3464 for (column
= 0; column
< width
; column
++) {
3465 src_idx
= stride
* (height
- 1) + column
;
3466 for (row
= 0; row
< height
; row
++) {
3468 /* We don't need the pages, but need to initialize
3469 * the entries so the sg list can be happily traversed.
3470 * The only thing we need are DMA addresses.
3472 sg_set_page(sg
, NULL
, PAGE_SIZE
, 0);
3473 sg_dma_address(sg
) = in
[offset
+ src_idx
];
3474 sg_dma_len(sg
) = PAGE_SIZE
;
3483 static noinline
struct sg_table
*
3484 intel_rotate_pages(struct intel_rotation_info
*rot_info
,
3485 struct drm_i915_gem_object
*obj
)
3487 const unsigned long n_pages
= obj
->base
.size
/ PAGE_SIZE
;
3488 unsigned int size
= intel_rotation_info_size(rot_info
);
3489 struct sgt_iter sgt_iter
;
3490 dma_addr_t dma_addr
;
3492 dma_addr_t
*page_addr_list
;
3493 struct sg_table
*st
;
3494 struct scatterlist
*sg
;
3497 /* Allocate a temporary list of source pages for random access. */
3498 page_addr_list
= kvmalloc_array(n_pages
,
3501 if (!page_addr_list
)
3502 return ERR_PTR(ret
);
3504 /* Allocate target SG list. */
3505 st
= kmalloc(sizeof(*st
), GFP_KERNEL
);
3509 ret
= sg_alloc_table(st
, size
, GFP_KERNEL
);
3513 /* Populate source page list from the object. */
3515 for_each_sgt_dma(dma_addr
, sgt_iter
, obj
->mm
.pages
)
3516 page_addr_list
[i
++] = dma_addr
;
3518 GEM_BUG_ON(i
!= n_pages
);
3522 for (i
= 0 ; i
< ARRAY_SIZE(rot_info
->plane
); i
++) {
3523 sg
= rotate_pages(page_addr_list
, rot_info
->plane
[i
].offset
,
3524 rot_info
->plane
[i
].width
, rot_info
->plane
[i
].height
,
3525 rot_info
->plane
[i
].stride
, st
, sg
);
3528 DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
3529 obj
->base
.size
, rot_info
->plane
[0].width
, rot_info
->plane
[0].height
, size
);
3531 kvfree(page_addr_list
);
3538 kvfree(page_addr_list
);
3540 DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
3541 obj
->base
.size
, rot_info
->plane
[0].width
, rot_info
->plane
[0].height
, size
);
3543 return ERR_PTR(ret
);
3546 static noinline
struct sg_table
*
3547 intel_partial_pages(const struct i915_ggtt_view
*view
,
3548 struct drm_i915_gem_object
*obj
)
3550 struct sg_table
*st
;
3551 struct scatterlist
*sg
, *iter
;
3552 unsigned int count
= view
->partial
.size
;
3553 unsigned int offset
;
3556 st
= kmalloc(sizeof(*st
), GFP_KERNEL
);
3560 ret
= sg_alloc_table(st
, count
, GFP_KERNEL
);
3564 iter
= i915_gem_object_get_sg(obj
, view
->partial
.offset
, &offset
);
3572 len
= min(iter
->length
- (offset
<< PAGE_SHIFT
),
3573 count
<< PAGE_SHIFT
);
3574 sg_set_page(sg
, NULL
, len
, 0);
3575 sg_dma_address(sg
) =
3576 sg_dma_address(iter
) + (offset
<< PAGE_SHIFT
);
3577 sg_dma_len(sg
) = len
;
3580 count
-= len
>> PAGE_SHIFT
;
3587 iter
= __sg_next(iter
);
3594 return ERR_PTR(ret
);
3598 i915_get_ggtt_vma_pages(struct i915_vma
*vma
)
3602 /* The vma->pages are only valid within the lifespan of the borrowed
3603 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
3604 * must be the vma->pages. A simple rule is that vma->pages must only
3605 * be accessed when the obj->mm.pages are pinned.
3607 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma
->obj
));
3609 switch (vma
->ggtt_view
.type
) {
3610 case I915_GGTT_VIEW_NORMAL
:
3611 vma
->pages
= vma
->obj
->mm
.pages
;
3614 case I915_GGTT_VIEW_ROTATED
:
3616 intel_rotate_pages(&vma
->ggtt_view
.rotated
, vma
->obj
);
3619 case I915_GGTT_VIEW_PARTIAL
:
3620 vma
->pages
= intel_partial_pages(&vma
->ggtt_view
, vma
->obj
);
3624 WARN_ONCE(1, "GGTT view %u not implemented!\n",
3625 vma
->ggtt_view
.type
);
3630 if (unlikely(IS_ERR(vma
->pages
))) {
3631 ret
= PTR_ERR(vma
->pages
);
3633 DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
3634 vma
->ggtt_view
.type
, ret
);
3640 * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
3641 * @vm: the &struct i915_address_space
3642 * @node: the &struct drm_mm_node (typically i915_vma.mode)
3643 * @size: how much space to allocate inside the GTT,
3644 * must be #I915_GTT_PAGE_SIZE aligned
3645 * @offset: where to insert inside the GTT,
3646 * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
3647 * (@offset + @size) must fit within the address space
3648 * @color: color to apply to node, if this node is not from a VMA,
3649 * color must be #I915_COLOR_UNEVICTABLE
3650 * @flags: control search and eviction behaviour
3652 * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
3653 * the address space (using @size and @color). If the @node does not fit, it
3654 * tries to evict any overlapping nodes from the GTT, including any
3655 * neighbouring nodes if the colors do not match (to ensure guard pages between
3656 * differing domains). See i915_gem_evict_for_node() for the gory details
3657 * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
3658 * evicting active overlapping objects, and any overlapping node that is pinned
3659 * or marked as unevictable will also result in failure.
3661 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3662 * asked to wait for eviction and interrupted.
3664 int i915_gem_gtt_reserve(struct i915_address_space
*vm
,
3665 struct drm_mm_node
*node
,
3666 u64 size
, u64 offset
, unsigned long color
,
3672 GEM_BUG_ON(!IS_ALIGNED(size
, I915_GTT_PAGE_SIZE
));
3673 GEM_BUG_ON(!IS_ALIGNED(offset
, I915_GTT_MIN_ALIGNMENT
));
3674 GEM_BUG_ON(range_overflows(offset
, size
, vm
->total
));
3675 GEM_BUG_ON(vm
== &vm
->i915
->mm
.aliasing_ppgtt
->base
);
3676 GEM_BUG_ON(drm_mm_node_allocated(node
));
3679 node
->start
= offset
;
3680 node
->color
= color
;
3682 err
= drm_mm_reserve_node(&vm
->mm
, node
);
3686 if (flags
& PIN_NOEVICT
)
3689 err
= i915_gem_evict_for_node(vm
, node
, flags
);
3691 err
= drm_mm_reserve_node(&vm
->mm
, node
);
3696 static u64
random_offset(u64 start
, u64 end
, u64 len
, u64 align
)
3700 GEM_BUG_ON(range_overflows(start
, len
, end
));
3701 GEM_BUG_ON(round_up(start
, align
) > round_down(end
- len
, align
));
3703 range
= round_down(end
- len
, align
) - round_up(start
, align
);
3705 if (sizeof(unsigned long) == sizeof(u64
)) {
3706 addr
= get_random_long();
3708 addr
= get_random_int();
3709 if (range
> U32_MAX
) {
3711 addr
|= get_random_int();
3714 div64_u64_rem(addr
, range
, &addr
);
3718 return round_up(start
, align
);
3722 * i915_gem_gtt_insert - insert a node into an address_space (GTT)
3723 * @vm: the &struct i915_address_space
3724 * @node: the &struct drm_mm_node (typically i915_vma.node)
3725 * @size: how much space to allocate inside the GTT,
3726 * must be #I915_GTT_PAGE_SIZE aligned
3727 * @alignment: required alignment of starting offset, may be 0 but
3728 * if specified, this must be a power-of-two and at least
3729 * #I915_GTT_MIN_ALIGNMENT
3730 * @color: color to apply to node
3731 * @start: start of any range restriction inside GTT (0 for all),
3732 * must be #I915_GTT_PAGE_SIZE aligned
3733 * @end: end of any range restriction inside GTT (U64_MAX for all),
3734 * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
3735 * @flags: control search and eviction behaviour
3737 * i915_gem_gtt_insert() first searches for an available hole into which
3738 * is can insert the node. The hole address is aligned to @alignment and
3739 * its @size must then fit entirely within the [@start, @end] bounds. The
3740 * nodes on either side of the hole must match @color, or else a guard page
3741 * will be inserted between the two nodes (or the node evicted). If no
3742 * suitable hole is found, first a victim is randomly selected and tested
3743 * for eviction, otherwise then the LRU list of objects within the GTT
3744 * is scanned to find the first set of replacement nodes to create the hole.
3745 * Those old overlapping nodes are evicted from the GTT (and so must be
3746 * rebound before any future use). Any node that is currently pinned cannot
3747 * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
3748 * active and #PIN_NONBLOCK is specified, that node is also skipped when
3749 * searching for an eviction candidate. See i915_gem_evict_something() for
3750 * the gory details on the eviction algorithm.
3752 * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
3753 * asked to wait for eviction and interrupted.
3755 int i915_gem_gtt_insert(struct i915_address_space
*vm
,
3756 struct drm_mm_node
*node
,
3757 u64 size
, u64 alignment
, unsigned long color
,
3758 u64 start
, u64 end
, unsigned int flags
)
3760 enum drm_mm_insert_mode mode
;
3764 lockdep_assert_held(&vm
->i915
->drm
.struct_mutex
);
3766 GEM_BUG_ON(!IS_ALIGNED(size
, I915_GTT_PAGE_SIZE
));
3767 GEM_BUG_ON(alignment
&& !is_power_of_2(alignment
));
3768 GEM_BUG_ON(alignment
&& !IS_ALIGNED(alignment
, I915_GTT_MIN_ALIGNMENT
));
3769 GEM_BUG_ON(start
>= end
);
3770 GEM_BUG_ON(start
> 0 && !IS_ALIGNED(start
, I915_GTT_PAGE_SIZE
));
3771 GEM_BUG_ON(end
< U64_MAX
&& !IS_ALIGNED(end
, I915_GTT_PAGE_SIZE
));
3772 GEM_BUG_ON(vm
== &vm
->i915
->mm
.aliasing_ppgtt
->base
);
3773 GEM_BUG_ON(drm_mm_node_allocated(node
));
3775 if (unlikely(range_overflows(start
, size
, end
)))
3778 if (unlikely(round_up(start
, alignment
) > round_down(end
- size
, alignment
)))
3781 mode
= DRM_MM_INSERT_BEST
;
3782 if (flags
& PIN_HIGH
)
3783 mode
= DRM_MM_INSERT_HIGH
;
3784 if (flags
& PIN_MAPPABLE
)
3785 mode
= DRM_MM_INSERT_LOW
;
3787 /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
3788 * so we know that we always have a minimum alignment of 4096.
3789 * The drm_mm range manager is optimised to return results
3790 * with zero alignment, so where possible use the optimal
3793 BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT
> I915_GTT_PAGE_SIZE
);
3794 if (alignment
<= I915_GTT_MIN_ALIGNMENT
)
3797 err
= drm_mm_insert_node_in_range(&vm
->mm
, node
,
3798 size
, alignment
, color
,
3803 if (flags
& PIN_NOEVICT
)
3806 /* No free space, pick a slot at random.
3808 * There is a pathological case here using a GTT shared between
3809 * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
3811 * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
3812 * (64k objects) (448k objects)
3814 * Now imagine that the eviction LRU is ordered top-down (just because
3815 * pathology meets real life), and that we need to evict an object to
3816 * make room inside the aperture. The eviction scan then has to walk
3817 * the 448k list before it finds one within range. And now imagine that
3818 * it has to search for a new hole between every byte inside the memcpy,
3819 * for several simultaneous clients.
3821 * On a full-ppgtt system, if we have run out of available space, there
3822 * will be lots and lots of objects in the eviction list! Again,
3823 * searching that LRU list may be slow if we are also applying any
3824 * range restrictions (e.g. restriction to low 4GiB) and so, for
3825 * simplicity and similarilty between different GTT, try the single
3826 * random replacement first.
3828 offset
= random_offset(start
, end
,
3829 size
, alignment
?: I915_GTT_MIN_ALIGNMENT
);
3830 err
= i915_gem_gtt_reserve(vm
, node
, size
, offset
, color
, flags
);
3834 /* Randomly selected placement is pinned, do a search */
3835 err
= i915_gem_evict_something(vm
, size
, alignment
, color
,
3840 return drm_mm_insert_node_in_range(&vm
->mm
, node
,
3841 size
, alignment
, color
,
3842 start
, end
, DRM_MM_INSERT_EVICT
);
3845 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
3846 #include "selftests/mock_gtt.c"
3847 #include "selftests/i915_gem_gtt.c"