drm/i915: Improve on suspend / resume time with VT-d enabled
[linux-2.6-block.git] / drivers / gpu / drm / i915 / gt / intel_gtt.h
CommitLineData
2c86e55d
MA
1/* SPDX-License-Identifier: MIT */
2/*
3 * Copyright © 2020 Intel Corporation
4 *
5 * Please try to maintain the following order within this file unless it makes
6 * sense to do otherwise. From top to bottom:
7 * 1. typedefs
8 * 2. #defines, and macros
9 * 3. structure definitions
10 * 4. function prototypes
11 *
12 * Within each section, please try to order by generation in ascending order,
13 * from top to bottom (ie. gen6 on the top, gen8 on the bottom).
14 */
15
16#ifndef __INTEL_GTT_H__
17#define __INTEL_GTT_H__
18
19#include <linux/io-mapping.h>
20#include <linux/kref.h>
21#include <linux/mm.h>
22#include <linux/pagevec.h>
23#include <linux/scatterlist.h>
24#include <linux/workqueue.h>
25
26#include <drm/drm_mm.h>
27
28#include "gt/intel_reset.h"
2c86e55d 29#include "i915_selftest.h"
39a2bd34 30#include "i915_vma_resource.h"
2c86e55d 31#include "i915_vma_types.h"
87bd701e
MA
32#include "i915_params.h"
33#include "intel_memory_region.h"
2c86e55d
MA
34
35#define I915_GFP_ALLOW_FAIL (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
36
37#if IS_ENABLED(CONFIG_DRM_I915_TRACE_GTT)
38#define DBG(...) trace_printk(__VA_ARGS__)
39#else
40#define DBG(...)
41#endif
42
43#define NALLOC 3 /* 1 normal, 1 for concurrent threads, 1 for preallocation */
44
45#define I915_GTT_PAGE_SIZE_4K BIT_ULL(12)
46#define I915_GTT_PAGE_SIZE_64K BIT_ULL(16)
47#define I915_GTT_PAGE_SIZE_2M BIT_ULL(21)
48
49#define I915_GTT_PAGE_SIZE I915_GTT_PAGE_SIZE_4K
50#define I915_GTT_MAX_PAGE_SIZE I915_GTT_PAGE_SIZE_2M
51
52#define I915_GTT_PAGE_MASK -I915_GTT_PAGE_SIZE
53
54#define I915_GTT_MIN_ALIGNMENT I915_GTT_PAGE_SIZE
55
56#define I915_FENCE_REG_NONE -1
57#define I915_MAX_NUM_FENCES 32
58/* 32 fences + sign bit for FENCE_REG_NONE */
59#define I915_MAX_NUM_FENCE_BITS 6
60
61typedef u32 gen6_pte_t;
62typedef u64 gen8_pte_t;
63
64#define ggtt_total_entries(ggtt) ((ggtt)->vm.total >> PAGE_SHIFT)
65
66#define I915_PTES(pte_len) ((unsigned int)(PAGE_SIZE / (pte_len)))
67#define I915_PTE_MASK(pte_len) (I915_PTES(pte_len) - 1)
68#define I915_PDES 512
69#define I915_PDE_MASK (I915_PDES - 1)
70
71/* gen6-hsw has bit 11-4 for physical addr bit 39-32 */
72#define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0))
73#define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
74#define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr)
75#define GEN6_PTE_CACHE_LLC (2 << 1)
76#define GEN6_PTE_UNCACHED (1 << 1)
77#define GEN6_PTE_VALID REG_BIT(0)
78
79#define GEN6_PTES I915_PTES(sizeof(gen6_pte_t))
80#define GEN6_PD_SIZE (I915_PDES * PAGE_SIZE)
81#define GEN6_PD_ALIGN (PAGE_SIZE * 16)
82#define GEN6_PDE_SHIFT 22
83#define GEN6_PDE_VALID REG_BIT(0)
84#define NUM_PTE(pde_shift) (1 << (pde_shift - PAGE_SHIFT))
85
86#define GEN7_PTE_CACHE_L3_LLC (3 << 1)
87
88#define BYT_PTE_SNOOPED_BY_CPU_CACHES REG_BIT(2)
89#define BYT_PTE_WRITEABLE REG_BIT(1)
90
e762bdf5
MA
91#define GEN12_PPGTT_PTE_LM BIT_ULL(11)
92
93#define GEN12_GGTT_PTE_LM BIT_ULL(1)
11724eea 94
5189e312
MA
95#define GEN12_PDE_64K BIT(6)
96
2c86e55d
MA
97/*
98 * Cacheability Control is a 4-bit value. The low three bits are stored in bits
99 * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
100 */
101#define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \
102 (((bits) & 0x8) << (11 - 3)))
103#define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2)
104#define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3)
105#define HSW_WB_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x8)
106#define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb)
107#define HSW_WT_ELLC_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x7)
108#define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6)
109#define HSW_PTE_UNCACHED (0)
110#define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0))
111#define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr)
112
113/*
114 * GEN8 32b style address is defined as a 3 level page table:
115 * 31:30 | 29:21 | 20:12 | 11:0
116 * PDPE | PDE | PTE | offset
117 * The difference as compared to normal x86 3 level page table is the PDPEs are
118 * programmed via register.
119 *
120 * GEN8 48b style address is defined as a 4 level page table:
121 * 47:39 | 38:30 | 29:21 | 20:12 | 11:0
122 * PML4E | PDPE | PDE | PTE | offset
123 */
124#define GEN8_3LVL_PDPES 4
125
126#define PPAT_UNCACHED (_PAGE_PWT | _PAGE_PCD)
127#define PPAT_CACHED_PDE 0 /* WB LLC */
128#define PPAT_CACHED _PAGE_PAT /* WB LLCeLLC */
129#define PPAT_DISPLAY_ELLC _PAGE_PCD /* WT eLLC */
130
131#define CHV_PPAT_SNOOP REG_BIT(6)
132#define GEN8_PPAT_AGE(x) ((x)<<4)
133#define GEN8_PPAT_LLCeLLC (3<<2)
134#define GEN8_PPAT_LLCELLC (2<<2)
135#define GEN8_PPAT_LLC (1<<2)
136#define GEN8_PPAT_WB (3<<0)
137#define GEN8_PPAT_WT (2<<0)
138#define GEN8_PPAT_WC (1<<0)
139#define GEN8_PPAT_UC (0<<0)
140#define GEN8_PPAT_ELLC_OVERRIDE (0<<2)
141#define GEN8_PPAT(i, x) ((u64)(x) << ((i) * 8))
142
5f978167
MC
143#define GEN8_PAGE_PRESENT BIT_ULL(0)
144#define GEN8_PAGE_RW BIT_ULL(1)
145
2c86e55d
MA
146#define GEN8_PDE_IPS_64K BIT(11)
147#define GEN8_PDE_PS_2M BIT(7)
148
89351925
CW
149enum i915_cache_level;
150
89351925 151struct drm_i915_gem_object;
0b6bc81d 152struct i915_fence_reg;
89351925
CW
153struct i915_vma;
154struct intel_gt;
0b6bc81d 155
2c86e55d
MA
156#define for_each_sgt_daddr(__dp, __iter, __sgt) \
157 __for_each_sgt_daddr(__dp, __iter, __sgt, I915_GTT_PAGE_SIZE)
158
2c86e55d 159struct i915_page_table {
89351925 160 struct drm_i915_gem_object *base;
cd0452aa
CW
161 union {
162 atomic_t used;
163 struct i915_page_table *stash;
164 };
5189e312 165 bool is_compact;
2c86e55d
MA
166};
167
168struct i915_page_directory {
169 struct i915_page_table pt;
170 spinlock_t lock;
82adf901 171 void **entry;
2c86e55d
MA
172};
173
174#define __px_choose_expr(x, type, expr, other) \
175 __builtin_choose_expr( \
176 __builtin_types_compatible_p(typeof(x), type) || \
177 __builtin_types_compatible_p(typeof(x), const type), \
178 ({ type __x = (type)(x); expr; }), \
179 other)
180
181#define px_base(px) \
89351925
CW
182 __px_choose_expr(px, struct drm_i915_gem_object *, __x, \
183 __px_choose_expr(px, struct i915_page_table *, __x->base, \
184 __px_choose_expr(px, struct i915_page_directory *, __x->pt.base, \
185 (void)0)))
186
187struct page *__px_page(struct drm_i915_gem_object *p);
188dma_addr_t __px_dma(struct drm_i915_gem_object *p);
189#define px_dma(px) (__px_dma(px_base(px)))
2c86e55d 190
529b9ec8
MA
191void *__px_vaddr(struct drm_i915_gem_object *p);
192#define px_vaddr(px) (__px_vaddr(px_base(px)))
193
2c86e55d
MA
194#define px_pt(px) \
195 __px_choose_expr(px, struct i915_page_table *, __x, \
196 __px_choose_expr(px, struct i915_page_directory *, &__x->pt, \
197 (void)0))
198#define px_used(px) (&px_pt(px)->used)
199
cd0452aa
CW
200struct i915_vm_pt_stash {
201 /* preallocated chains of page tables/directories */
202 struct i915_page_table *pt[2];
2cff4b9e
MA
203 /*
204 * Optionally override the alignment/size of the physical page that
205 * contains each PT. If not set defaults back to the usual
206 * I915_GTT_PAGE_SIZE_4K. This does not influence the other paging
207 * structures. MUST be a power-of-two. ONLY applicable on discrete
208 * platforms.
209 */
210 int pt_sz;
cd0452aa
CW
211};
212
2c86e55d
MA
213struct i915_vma_ops {
214 /* Map an object into an address space with the given cache flags. */
cd0452aa
CW
215 void (*bind_vma)(struct i915_address_space *vm,
216 struct i915_vm_pt_stash *stash,
39a2bd34 217 struct i915_vma_resource *vma_res,
cd0452aa
CW
218 enum i915_cache_level cache_level,
219 u32 flags);
2c86e55d
MA
220 /*
221 * Unmap an object from an address space. This usually consists of
222 * setting the valid PTE entries to a reserved scratch page.
223 */
12b07256 224 void (*unbind_vma)(struct i915_address_space *vm,
39a2bd34
TH
225 struct i915_vma_resource *vma_res);
226
2c86e55d
MA
227};
228
2c86e55d
MA
229struct i915_address_space {
230 struct kref ref;
dcc5d820 231 struct work_struct release_work;
2c86e55d
MA
232
233 struct drm_mm mm;
234 struct intel_gt *gt;
235 struct drm_i915_private *i915;
236 struct device *dma;
2c86e55d
MA
237 u64 total; /* size addr space maps (ex. 2GB for ggtt) */
238 u64 reserved; /* size addr space reserved */
87bd701e 239 u64 min_alignment[INTEL_MEMORY_STOLEN_LOCAL + 1];
2c86e55d
MA
240
241 unsigned int bind_async_flags;
242
2c86e55d 243 struct mutex mutex; /* protects vma and our lists */
4d8151ae
TH
244
245 struct kref resv_ref; /* kref to keep the reservation lock alive. */
246 struct dma_resv _resv; /* reservation lock for all pd objects, and buffer pool */
2c86e55d
MA
247#define VM_CLASS_GGTT 0
248#define VM_CLASS_PPGTT 1
33e7a975 249#define VM_CLASS_DPT 2
2c86e55d 250
89351925 251 struct drm_i915_gem_object *scratch[4];
2c86e55d
MA
252 /**
253 * List of vma currently bound.
254 */
255 struct list_head bound_list;
256
e1a7ab4f
TH
257 /**
258 * List of vmas not yet bound or evicted.
259 */
260 struct list_head unbound_list;
261
2c86e55d
MA
262 /* Global GTT */
263 bool is_ggtt:1;
264
33e7a975
VS
265 /* Display page table */
266 bool is_dpt:1;
267
2c86e55d
MA
268 /* Some systems support read-only mappings for GGTT and/or PPGTT */
269 bool has_read_only:1;
270
e1a7ab4f
TH
271 /* Skip pte rewrite on unbind for suspend. Protected by @mutex */
272 bool skip_pte_rewrite:1;
273
cd0452aa
CW
274 u8 top;
275 u8 pd_shift;
276 u8 scratch_order;
277
a259cc14
TH
278 /* Flags used when creating page-table objects for this vm */
279 unsigned long lmem_pt_obj_flags;
280
2f6b90da
TH
281 /* Interval tree for pending unbind vma resources */
282 struct rb_root_cached pending_unbind;
283
89351925
CW
284 struct drm_i915_gem_object *
285 (*alloc_pt_dma)(struct i915_address_space *vm, int sz);
fef53be0
MA
286 struct drm_i915_gem_object *
287 (*alloc_scratch_dma)(struct i915_address_space *vm, int sz);
89351925 288
2c86e55d
MA
289 u64 (*pte_encode)(dma_addr_t addr,
290 enum i915_cache_level level,
291 u32 flags); /* Create a valid PTE */
292#define PTE_READ_ONLY BIT(0)
11724eea 293#define PTE_LM BIT(1)
2c86e55d 294
cd0452aa
CW
295 void (*allocate_va_range)(struct i915_address_space *vm,
296 struct i915_vm_pt_stash *stash,
297 u64 start, u64 length);
2c86e55d
MA
298 void (*clear_range)(struct i915_address_space *vm,
299 u64 start, u64 length);
300 void (*insert_page)(struct i915_address_space *vm,
301 dma_addr_t addr,
302 u64 offset,
303 enum i915_cache_level cache_level,
304 u32 flags);
305 void (*insert_entries)(struct i915_address_space *vm,
39a2bd34 306 struct i915_vma_resource *vma_res,
2c86e55d
MA
307 enum i915_cache_level cache_level,
308 u32 flags);
309 void (*cleanup)(struct i915_address_space *vm);
310
3607e1e9
CW
311 void (*foreach)(struct i915_address_space *vm,
312 u64 start, u64 length,
313 void (*fn)(struct i915_address_space *vm,
314 struct i915_page_table *pt,
315 void *data),
316 void *data);
317
2c86e55d
MA
318 struct i915_vma_ops vma_ops;
319
320 I915_SELFTEST_DECLARE(struct fault_attr fault_attr);
321 I915_SELFTEST_DECLARE(bool scrub_64K);
322};
323
324/*
325 * The Graphics Translation Table is the way in which GEN hardware translates a
326 * Graphics Virtual Address into a Physical Address. In addition to the normal
327 * collateral associated with any va->pa translations GEN hardware also has a
328 * portion of the GTT which can be mapped by the CPU and remain both coherent
329 * and correct (in cases like swizzling). That region is referred to as GMADR in
330 * the spec.
331 */
332struct i915_ggtt {
333 struct i915_address_space vm;
334
335 struct io_mapping iomap; /* Mapping to our CPU mappable region */
336 struct resource gmadr; /* GMADR resource */
337 resource_size_t mappable_end; /* End offset that we can CPU map */
338
339 /** "Graphics Stolen Memory" holds the global PTEs */
340 void __iomem *gsm;
341 void (*invalidate)(struct i915_ggtt *ggtt);
342
343 /** PPGTT used for aliasing the PPGTT with the GTT */
344 struct i915_ppgtt *alias;
345
346 bool do_idle_maps;
347
2ef6efa7
TH
348 /**
349 * @pte_lost: Are ptes lost on resume?
350 *
351 * Whether the system was recently restored from hibernate and
352 * thus may have lost pte content.
353 */
354 bool pte_lost;
355
356 /**
357 * @probed_pte: Probed pte value on suspend. Re-checked on resume.
358 */
359 u64 probed_pte;
360
2c86e55d
MA
361 int mtrr;
362
363 /** Bit 6 swizzling required for X tiling */
364 u32 bit_6_swizzle_x;
365 /** Bit 6 swizzling required for Y tiling */
366 u32 bit_6_swizzle_y;
367
368 u32 pin_bias;
369
370 unsigned int num_fences;
0b6bc81d 371 struct i915_fence_reg *fence_regs;
2c86e55d
MA
372 struct list_head fence_list;
373
374 /**
375 * List of all objects in gtt_space, currently mmaped by userspace.
376 * All objects within this list must also be on bound_list.
377 */
378 struct list_head userfault_list;
379
380 /* Manual runtime pm autosuspend delay for user GGTT mmaps */
381 struct intel_wakeref_auto userfault_wakeref;
382
742379c0 383 struct mutex error_mutex;
2c86e55d
MA
384 struct drm_mm_node error_capture;
385 struct drm_mm_node uc_fw;
386};
387
388struct i915_ppgtt {
389 struct i915_address_space vm;
390
391 struct i915_page_directory *pd;
392};
393
394#define i915_is_ggtt(vm) ((vm)->is_ggtt)
33e7a975 395#define i915_is_dpt(vm) ((vm)->is_dpt)
74862d4c 396#define i915_is_ggtt_or_dpt(vm) (i915_is_ggtt(vm) || i915_is_dpt(vm))
2c86e55d 397
a7f46d5b
TU
398bool intel_vm_no_concurrent_access_wa(struct drm_i915_private *i915);
399
26ad4f8b
ML
400int __must_check
401i915_vm_lock_objects(struct i915_address_space *vm, struct i915_gem_ww_ctx *ww);
402
2c86e55d
MA
403static inline bool
404i915_vm_is_4lvl(const struct i915_address_space *vm)
405{
406 return (vm->total - 1) >> 32;
407}
408
409static inline bool
410i915_vm_has_scratch_64K(struct i915_address_space *vm)
411{
412 return vm->scratch_order == get_order(I915_GTT_PAGE_SIZE_64K);
413}
414
87bd701e
MA
415static inline u64 i915_vm_min_alignment(struct i915_address_space *vm,
416 enum intel_memory_type type)
417{
418 /* avoid INTEL_MEMORY_MOCK overflow */
419 if ((int)type >= ARRAY_SIZE(vm->min_alignment))
420 type = INTEL_MEMORY_SYSTEM;
421
422 return vm->min_alignment[type];
423}
424
425static inline u64 i915_vm_obj_min_alignment(struct i915_address_space *vm,
426 struct drm_i915_gem_object *obj)
427{
428 struct intel_memory_region *mr = READ_ONCE(obj->mm.region);
429 enum intel_memory_type type = mr ? mr->type : INTEL_MEMORY_SYSTEM;
430
431 return i915_vm_min_alignment(vm, type);
432}
433
2c86e55d
MA
434static inline bool
435i915_vm_has_cache_coloring(struct i915_address_space *vm)
436{
437 return i915_is_ggtt(vm) && vm->mm.color_adjust;
438}
439
440static inline struct i915_ggtt *
441i915_vm_to_ggtt(struct i915_address_space *vm)
442{
443 BUILD_BUG_ON(offsetof(struct i915_ggtt, vm));
444 GEM_BUG_ON(!i915_is_ggtt(vm));
445 return container_of(vm, struct i915_ggtt, vm);
446}
447
448static inline struct i915_ppgtt *
449i915_vm_to_ppgtt(struct i915_address_space *vm)
450{
451 BUILD_BUG_ON(offsetof(struct i915_ppgtt, vm));
74862d4c 452 GEM_BUG_ON(i915_is_ggtt_or_dpt(vm));
2c86e55d
MA
453 return container_of(vm, struct i915_ppgtt, vm);
454}
455
456static inline struct i915_address_space *
457i915_vm_get(struct i915_address_space *vm)
458{
459 kref_get(&vm->ref);
460 return vm;
461}
462
e1a7ab4f
TH
463static inline struct i915_address_space *
464i915_vm_tryget(struct i915_address_space *vm)
465{
466 return kref_get_unless_zero(&vm->ref) ? vm : NULL;
467}
468
469static inline void assert_vm_alive(struct i915_address_space *vm)
470{
471 GEM_BUG_ON(!kref_read(&vm->ref));
472}
473
4d8151ae
TH
474/**
475 * i915_vm_resv_get - Obtain a reference on the vm's reservation lock
476 * @vm: The vm whose reservation lock we want to share.
477 *
478 * Return: A pointer to the vm's reservation lock.
479 */
480static inline struct dma_resv *i915_vm_resv_get(struct i915_address_space *vm)
481{
482 kref_get(&vm->resv_ref);
483 return &vm->_resv;
484}
485
2c86e55d
MA
486void i915_vm_release(struct kref *kref);
487
4d8151ae
TH
488void i915_vm_resv_release(struct kref *kref);
489
2c86e55d
MA
490static inline void i915_vm_put(struct i915_address_space *vm)
491{
492 kref_put(&vm->ref, i915_vm_release);
493}
494
4d8151ae
TH
495/**
496 * i915_vm_resv_put - Release a reference on the vm's reservation lock
497 * @resv: Pointer to a reservation lock obtained from i915_vm_resv_get()
498 */
499static inline void i915_vm_resv_put(struct i915_address_space *vm)
500{
501 kref_put(&vm->resv_ref, i915_vm_resv_release);
502}
503
2c86e55d
MA
504void i915_address_space_init(struct i915_address_space *vm, int subclass);
505void i915_address_space_fini(struct i915_address_space *vm);
506
507static inline u32 i915_pte_index(u64 address, unsigned int pde_shift)
508{
509 const u32 mask = NUM_PTE(pde_shift) - 1;
510
511 return (address >> PAGE_SHIFT) & mask;
512}
513
514/*
515 * Helper to counts the number of PTEs within the given length. This count
516 * does not cross a page table boundary, so the max value would be
517 * GEN6_PTES for GEN6, and GEN8_PTES for GEN8.
518 */
519static inline u32 i915_pte_count(u64 addr, u64 length, unsigned int pde_shift)
520{
521 const u64 mask = ~((1ULL << pde_shift) - 1);
522 u64 end;
523
524 GEM_BUG_ON(length == 0);
525 GEM_BUG_ON(offset_in_page(addr | length));
526
527 end = addr + length;
528
529 if ((addr & mask) != (end & mask))
530 return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift);
531
532 return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift);
533}
534
535static inline u32 i915_pde_index(u64 addr, u32 shift)
536{
537 return (addr >> shift) & I915_PDE_MASK;
538}
539
540static inline struct i915_page_table *
541i915_pt_entry(const struct i915_page_directory * const pd,
542 const unsigned short n)
543{
544 return pd->entry[n];
545}
546
547static inline struct i915_page_directory *
548i915_pd_entry(const struct i915_page_directory * const pdp,
549 const unsigned short n)
550{
551 return pdp->entry[n];
552}
553
554static inline dma_addr_t
555i915_page_dir_dma_addr(const struct i915_ppgtt *ppgtt, const unsigned int n)
556{
89351925 557 struct i915_page_table *pt = ppgtt->pd->entry[n];
2c86e55d 558
89351925 559 return __px_dma(pt ? px_base(pt) : ppgtt->vm.scratch[ppgtt->vm.top]);
2c86e55d
MA
560}
561
a259cc14
TH
562void ppgtt_init(struct i915_ppgtt *ppgtt, struct intel_gt *gt,
563 unsigned long lmem_pt_obj_flags);
2c86e55d 564
7a5c9223
CB
565void intel_ggtt_bind_vma(struct i915_address_space *vm,
566 struct i915_vm_pt_stash *stash,
567 struct i915_vma_resource *vma_res,
568 enum i915_cache_level cache_level,
569 u32 flags);
570void intel_ggtt_unbind_vma(struct i915_address_space *vm,
571 struct i915_vma_resource *vma_res);
572
2c86e55d
MA
573int i915_ggtt_probe_hw(struct drm_i915_private *i915);
574int i915_ggtt_init_hw(struct drm_i915_private *i915);
575int i915_ggtt_enable_hw(struct drm_i915_private *i915);
576void i915_ggtt_enable_guc(struct i915_ggtt *ggtt);
577void i915_ggtt_disable_guc(struct i915_ggtt *ggtt);
578int i915_init_ggtt(struct drm_i915_private *i915);
579void i915_ggtt_driver_release(struct drm_i915_private *i915);
4d8151ae 580void i915_ggtt_driver_late_release(struct drm_i915_private *i915);
2c86e55d
MA
581
582static inline bool i915_ggtt_has_aperture(const struct i915_ggtt *ggtt)
583{
584 return ggtt->mappable_end > 0;
585}
586
587int i915_ppgtt_init_hw(struct intel_gt *gt);
588
a259cc14
TH
589struct i915_ppgtt *i915_ppgtt_create(struct intel_gt *gt,
590 unsigned long lmem_pt_obj_flags);
2c86e55d 591
8d2f683f
ID
592void i915_ggtt_suspend_vm(struct i915_address_space *vm);
593bool i915_ggtt_resume_vm(struct i915_address_space *vm);
e986209c
CW
594void i915_ggtt_suspend(struct i915_ggtt *gtt);
595void i915_ggtt_resume(struct i915_ggtt *ggtt);
2c86e55d 596
2ef6efa7
TH
597/**
598 * i915_ggtt_mark_pte_lost - Mark ggtt ptes as lost or clear such a marking
599 * @i915 The device private.
600 * @val whether the ptes should be marked as lost.
601 *
602 * In some cases pte content is retained across suspend, but typically lost
603 * across hibernate. Typically they should be marked as lost on
604 * hibernation restore and such marking cleared on suspend.
605 */
606void i915_ggtt_mark_pte_lost(struct drm_i915_private *i915, bool val);
607
2c86e55d 608void
89351925 609fill_page_dma(struct drm_i915_gem_object *p, const u64 val, unsigned int count);
2c86e55d
MA
610
611#define fill_px(px, v) fill_page_dma(px_base(px), (v), PAGE_SIZE / sizeof(u64))
612#define fill32_px(px, v) do { \
613 u64 v__ = lower_32_bits(v); \
614 fill_px((px), v__ << 32 | v__); \
615} while (0)
616
89351925 617int setup_scratch_page(struct i915_address_space *vm);
2c86e55d
MA
618void free_scratch(struct i915_address_space *vm);
619
89351925 620struct drm_i915_gem_object *alloc_pt_dma(struct i915_address_space *vm, int sz);
6aed5673 621struct drm_i915_gem_object *alloc_pt_lmem(struct i915_address_space *vm, int sz);
2cff4b9e 622struct i915_page_table *alloc_pt(struct i915_address_space *vm, int sz);
2c86e55d 623struct i915_page_directory *alloc_pd(struct i915_address_space *vm);
82adf901 624struct i915_page_directory *__alloc_pd(int npde);
2c86e55d 625
529b9ec8
MA
626int map_pt_dma(struct i915_address_space *vm, struct drm_i915_gem_object *obj);
627int map_pt_dma_locked(struct i915_address_space *vm, struct drm_i915_gem_object *obj);
2c86e55d 628
82adf901
CW
629void free_px(struct i915_address_space *vm,
630 struct i915_page_table *pt, int lvl);
631#define free_pt(vm, px) free_px(vm, px, 0)
632#define free_pd(vm, px) free_px(vm, px_pt(px), 1)
2c86e55d
MA
633
634void
635__set_pd_entry(struct i915_page_directory * const pd,
636 const unsigned short idx,
89351925 637 struct i915_page_table *pt,
2c86e55d
MA
638 u64 (*encode)(const dma_addr_t, const enum i915_cache_level));
639
640#define set_pd_entry(pd, idx, to) \
89351925 641 __set_pd_entry((pd), (idx), px_pt(to), gen8_pde_encode)
2c86e55d
MA
642
643void
644clear_pd_entry(struct i915_page_directory * const pd,
645 const unsigned short idx,
89351925 646 const struct drm_i915_gem_object * const scratch);
2c86e55d
MA
647
648bool
649release_pd_entry(struct i915_page_directory * const pd,
650 const unsigned short idx,
651 struct i915_page_table * const pt,
89351925 652 const struct drm_i915_gem_object * const scratch);
2c86e55d 653void gen6_ggtt_invalidate(struct i915_ggtt *ggtt);
7a5c9223 654void gen8_ggtt_invalidate(struct i915_ggtt *ggtt);
2c86e55d 655
cd0452aa
CW
656void ppgtt_bind_vma(struct i915_address_space *vm,
657 struct i915_vm_pt_stash *stash,
39a2bd34 658 struct i915_vma_resource *vma_res,
cd0452aa
CW
659 enum i915_cache_level cache_level,
660 u32 flags);
12b07256 661void ppgtt_unbind_vma(struct i915_address_space *vm,
39a2bd34 662 struct i915_vma_resource *vma_res);
12b07256 663
2c86e55d
MA
664void gtt_write_workarounds(struct intel_gt *gt);
665
666void setup_private_pat(struct intel_uncore *uncore);
667
cd0452aa
CW
668int i915_vm_alloc_pt_stash(struct i915_address_space *vm,
669 struct i915_vm_pt_stash *stash,
670 u64 size);
529b9ec8 671int i915_vm_map_pt_stash(struct i915_address_space *vm,
89351925 672 struct i915_vm_pt_stash *stash);
cd0452aa
CW
673void i915_vm_free_pt_stash(struct i915_address_space *vm,
674 struct i915_vm_pt_stash *stash);
675
a4d86249
CW
676struct i915_vma *
677__vm_create_scratch_for_read(struct i915_address_space *vm, unsigned long size);
678
2a665968
ML
679struct i915_vma *
680__vm_create_scratch_for_read_pinned(struct i915_address_space *vm, unsigned long size);
681
2c86e55d
MA
682static inline struct sgt_dma {
683 struct scatterlist *sg;
684 dma_addr_t dma, max;
39a2bd34
TH
685} sgt_dma(struct i915_vma_resource *vma_res) {
686 struct scatterlist *sg = vma_res->bi.pages->sgl;
2c86e55d
MA
687 dma_addr_t addr = sg_dma_address(sg);
688
8a473dba 689 return (struct sgt_dma){ sg, addr, addr + sg_dma_len(sg) };
2c86e55d
MA
690}
691
692#endif