drm/i915/gtt/dg1: add PTE_LM plumbing for ppGTT
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gt / intel_gtt.h
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"
29 #include "i915_selftest.h"
30 #include "i915_vma_types.h"
31
32 #define I915_GFP_ALLOW_FAIL (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN)
33
34 #if IS_ENABLED(CONFIG_DRM_I915_TRACE_GTT)
35 #define DBG(...) trace_printk(__VA_ARGS__)
36 #else
37 #define DBG(...)
38 #endif
39
40 #define NALLOC 3 /* 1 normal, 1 for concurrent threads, 1 for preallocation */
41
42 #define I915_GTT_PAGE_SIZE_4K   BIT_ULL(12)
43 #define I915_GTT_PAGE_SIZE_64K  BIT_ULL(16)
44 #define I915_GTT_PAGE_SIZE_2M   BIT_ULL(21)
45
46 #define I915_GTT_PAGE_SIZE I915_GTT_PAGE_SIZE_4K
47 #define I915_GTT_MAX_PAGE_SIZE I915_GTT_PAGE_SIZE_2M
48
49 #define I915_GTT_PAGE_MASK -I915_GTT_PAGE_SIZE
50
51 #define I915_GTT_MIN_ALIGNMENT I915_GTT_PAGE_SIZE
52
53 #define I915_FENCE_REG_NONE -1
54 #define I915_MAX_NUM_FENCES 32
55 /* 32 fences + sign bit for FENCE_REG_NONE */
56 #define I915_MAX_NUM_FENCE_BITS 6
57
58 typedef u32 gen6_pte_t;
59 typedef u64 gen8_pte_t;
60
61 #define ggtt_total_entries(ggtt) ((ggtt)->vm.total >> PAGE_SHIFT)
62
63 #define I915_PTES(pte_len)              ((unsigned int)(PAGE_SIZE / (pte_len)))
64 #define I915_PTE_MASK(pte_len)          (I915_PTES(pte_len) - 1)
65 #define I915_PDES                       512
66 #define I915_PDE_MASK                   (I915_PDES - 1)
67
68 /* gen6-hsw has bit 11-4 for physical addr bit 39-32 */
69 #define GEN6_GTT_ADDR_ENCODE(addr)      ((addr) | (((addr) >> 28) & 0xff0))
70 #define GEN6_PTE_ADDR_ENCODE(addr)      GEN6_GTT_ADDR_ENCODE(addr)
71 #define GEN6_PDE_ADDR_ENCODE(addr)      GEN6_GTT_ADDR_ENCODE(addr)
72 #define GEN6_PTE_CACHE_LLC              (2 << 1)
73 #define GEN6_PTE_UNCACHED               (1 << 1)
74 #define GEN6_PTE_VALID                  REG_BIT(0)
75
76 #define GEN6_PTES                       I915_PTES(sizeof(gen6_pte_t))
77 #define GEN6_PD_SIZE                    (I915_PDES * PAGE_SIZE)
78 #define GEN6_PD_ALIGN                   (PAGE_SIZE * 16)
79 #define GEN6_PDE_SHIFT                  22
80 #define GEN6_PDE_VALID                  REG_BIT(0)
81 #define NUM_PTE(pde_shift)     (1 << (pde_shift - PAGE_SHIFT))
82
83 #define GEN7_PTE_CACHE_L3_LLC           (3 << 1)
84
85 #define BYT_PTE_SNOOPED_BY_CPU_CACHES   REG_BIT(2)
86 #define BYT_PTE_WRITEABLE               REG_BIT(1)
87
88 #define GEN12_PPGTT_PTE_LM BIT_ULL(11)
89
90 /*
91  * Cacheability Control is a 4-bit value. The low three bits are stored in bits
92  * 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE.
93  */
94 #define HSW_CACHEABILITY_CONTROL(bits)  ((((bits) & 0x7) << 1) | \
95                                          (((bits) & 0x8) << (11 - 3)))
96 #define HSW_WB_LLC_AGE3                 HSW_CACHEABILITY_CONTROL(0x2)
97 #define HSW_WB_LLC_AGE0                 HSW_CACHEABILITY_CONTROL(0x3)
98 #define HSW_WB_ELLC_LLC_AGE3            HSW_CACHEABILITY_CONTROL(0x8)
99 #define HSW_WB_ELLC_LLC_AGE0            HSW_CACHEABILITY_CONTROL(0xb)
100 #define HSW_WT_ELLC_LLC_AGE3            HSW_CACHEABILITY_CONTROL(0x7)
101 #define HSW_WT_ELLC_LLC_AGE0            HSW_CACHEABILITY_CONTROL(0x6)
102 #define HSW_PTE_UNCACHED                (0)
103 #define HSW_GTT_ADDR_ENCODE(addr)       ((addr) | (((addr) >> 28) & 0x7f0))
104 #define HSW_PTE_ADDR_ENCODE(addr)       HSW_GTT_ADDR_ENCODE(addr)
105
106 /*
107  * GEN8 32b style address is defined as a 3 level page table:
108  * 31:30 | 29:21 | 20:12 |  11:0
109  * PDPE  |  PDE  |  PTE  | offset
110  * The difference as compared to normal x86 3 level page table is the PDPEs are
111  * programmed via register.
112  *
113  * GEN8 48b style address is defined as a 4 level page table:
114  * 47:39 | 38:30 | 29:21 | 20:12 |  11:0
115  * PML4E | PDPE  |  PDE  |  PTE  | offset
116  */
117 #define GEN8_3LVL_PDPES                 4
118
119 #define PPAT_UNCACHED                   (_PAGE_PWT | _PAGE_PCD)
120 #define PPAT_CACHED_PDE                 0 /* WB LLC */
121 #define PPAT_CACHED                     _PAGE_PAT /* WB LLCeLLC */
122 #define PPAT_DISPLAY_ELLC               _PAGE_PCD /* WT eLLC */
123
124 #define CHV_PPAT_SNOOP                  REG_BIT(6)
125 #define GEN8_PPAT_AGE(x)                ((x)<<4)
126 #define GEN8_PPAT_LLCeLLC               (3<<2)
127 #define GEN8_PPAT_LLCELLC               (2<<2)
128 #define GEN8_PPAT_LLC                   (1<<2)
129 #define GEN8_PPAT_WB                    (3<<0)
130 #define GEN8_PPAT_WT                    (2<<0)
131 #define GEN8_PPAT_WC                    (1<<0)
132 #define GEN8_PPAT_UC                    (0<<0)
133 #define GEN8_PPAT_ELLC_OVERRIDE         (0<<2)
134 #define GEN8_PPAT(i, x)                 ((u64)(x) << ((i) * 8))
135
136 #define GEN8_PDE_IPS_64K BIT(11)
137 #define GEN8_PDE_PS_2M   BIT(7)
138
139 enum i915_cache_level;
140
141 struct drm_i915_file_private;
142 struct drm_i915_gem_object;
143 struct i915_fence_reg;
144 struct i915_vma;
145 struct intel_gt;
146
147 #define for_each_sgt_daddr(__dp, __iter, __sgt) \
148         __for_each_sgt_daddr(__dp, __iter, __sgt, I915_GTT_PAGE_SIZE)
149
150 struct i915_page_table {
151         struct drm_i915_gem_object *base;
152         union {
153                 atomic_t used;
154                 struct i915_page_table *stash;
155         };
156 };
157
158 struct i915_page_directory {
159         struct i915_page_table pt;
160         spinlock_t lock;
161         void **entry;
162 };
163
164 #define __px_choose_expr(x, type, expr, other) \
165         __builtin_choose_expr( \
166         __builtin_types_compatible_p(typeof(x), type) || \
167         __builtin_types_compatible_p(typeof(x), const type), \
168         ({ type __x = (type)(x); expr; }), \
169         other)
170
171 #define px_base(px) \
172         __px_choose_expr(px, struct drm_i915_gem_object *, __x, \
173         __px_choose_expr(px, struct i915_page_table *, __x->base, \
174         __px_choose_expr(px, struct i915_page_directory *, __x->pt.base, \
175         (void)0)))
176
177 struct page *__px_page(struct drm_i915_gem_object *p);
178 dma_addr_t __px_dma(struct drm_i915_gem_object *p);
179 #define px_dma(px) (__px_dma(px_base(px)))
180
181 #define px_pt(px) \
182         __px_choose_expr(px, struct i915_page_table *, __x, \
183         __px_choose_expr(px, struct i915_page_directory *, &__x->pt, \
184         (void)0))
185 #define px_used(px) (&px_pt(px)->used)
186
187 struct i915_vm_pt_stash {
188         /* preallocated chains of page tables/directories */
189         struct i915_page_table *pt[2];
190 };
191
192 struct i915_vma_ops {
193         /* Map an object into an address space with the given cache flags. */
194         void (*bind_vma)(struct i915_address_space *vm,
195                          struct i915_vm_pt_stash *stash,
196                          struct i915_vma *vma,
197                          enum i915_cache_level cache_level,
198                          u32 flags);
199         /*
200          * Unmap an object from an address space. This usually consists of
201          * setting the valid PTE entries to a reserved scratch page.
202          */
203         void (*unbind_vma)(struct i915_address_space *vm,
204                            struct i915_vma *vma);
205
206         int (*set_pages)(struct i915_vma *vma);
207         void (*clear_pages)(struct i915_vma *vma);
208 };
209
210 struct i915_address_space {
211         struct kref ref;
212         struct rcu_work rcu;
213
214         struct drm_mm mm;
215         struct intel_gt *gt;
216         struct drm_i915_private *i915;
217         struct device *dma;
218         /*
219          * Every address space belongs to a struct file - except for the global
220          * GTT that is owned by the driver (and so @file is set to NULL). In
221          * principle, no information should leak from one context to another
222          * (or between files/processes etc) unless explicitly shared by the
223          * owner. Tracking the owner is important in order to free up per-file
224          * objects along with the file, to aide resource tracking, and to
225          * assign blame.
226          */
227         struct drm_i915_file_private *file;
228         u64 total;              /* size addr space maps (ex. 2GB for ggtt) */
229         u64 reserved;           /* size addr space reserved */
230
231         unsigned int bind_async_flags;
232
233         /*
234          * Each active user context has its own address space (in full-ppgtt).
235          * Since the vm may be shared between multiple contexts, we count how
236          * many contexts keep us "open". Once open hits zero, we are closed
237          * and do not allow any new attachments, and proceed to shutdown our
238          * vma and page directories.
239          */
240         atomic_t open;
241
242         struct mutex mutex; /* protects vma and our lists */
243 #define VM_CLASS_GGTT 0
244 #define VM_CLASS_PPGTT 1
245
246         struct drm_i915_gem_object *scratch[4];
247         /**
248          * List of vma currently bound.
249          */
250         struct list_head bound_list;
251
252         /* Global GTT */
253         bool is_ggtt:1;
254
255         /* Some systems support read-only mappings for GGTT and/or PPGTT */
256         bool has_read_only:1;
257
258         u8 top;
259         u8 pd_shift;
260         u8 scratch_order;
261
262         struct drm_i915_gem_object *
263                 (*alloc_pt_dma)(struct i915_address_space *vm, int sz);
264
265         u64 (*pte_encode)(dma_addr_t addr,
266                           enum i915_cache_level level,
267                           u32 flags); /* Create a valid PTE */
268 #define PTE_READ_ONLY   BIT(0)
269 #define PTE_LM          BIT(1)
270
271         void (*allocate_va_range)(struct i915_address_space *vm,
272                                   struct i915_vm_pt_stash *stash,
273                                   u64 start, u64 length);
274         void (*clear_range)(struct i915_address_space *vm,
275                             u64 start, u64 length);
276         void (*insert_page)(struct i915_address_space *vm,
277                             dma_addr_t addr,
278                             u64 offset,
279                             enum i915_cache_level cache_level,
280                             u32 flags);
281         void (*insert_entries)(struct i915_address_space *vm,
282                                struct i915_vma *vma,
283                                enum i915_cache_level cache_level,
284                                u32 flags);
285         void (*cleanup)(struct i915_address_space *vm);
286
287         struct i915_vma_ops vma_ops;
288
289         I915_SELFTEST_DECLARE(struct fault_attr fault_attr);
290         I915_SELFTEST_DECLARE(bool scrub_64K);
291 };
292
293 /*
294  * The Graphics Translation Table is the way in which GEN hardware translates a
295  * Graphics Virtual Address into a Physical Address. In addition to the normal
296  * collateral associated with any va->pa translations GEN hardware also has a
297  * portion of the GTT which can be mapped by the CPU and remain both coherent
298  * and correct (in cases like swizzling). That region is referred to as GMADR in
299  * the spec.
300  */
301 struct i915_ggtt {
302         struct i915_address_space vm;
303
304         struct io_mapping iomap;        /* Mapping to our CPU mappable region */
305         struct resource gmadr;          /* GMADR resource */
306         resource_size_t mappable_end;   /* End offset that we can CPU map */
307
308         /** "Graphics Stolen Memory" holds the global PTEs */
309         void __iomem *gsm;
310         void (*invalidate)(struct i915_ggtt *ggtt);
311
312         /** PPGTT used for aliasing the PPGTT with the GTT */
313         struct i915_ppgtt *alias;
314
315         bool do_idle_maps;
316
317         int mtrr;
318
319         /** Bit 6 swizzling required for X tiling */
320         u32 bit_6_swizzle_x;
321         /** Bit 6 swizzling required for Y tiling */
322         u32 bit_6_swizzle_y;
323
324         u32 pin_bias;
325
326         unsigned int num_fences;
327         struct i915_fence_reg *fence_regs;
328         struct list_head fence_list;
329
330         /**
331          * List of all objects in gtt_space, currently mmaped by userspace.
332          * All objects within this list must also be on bound_list.
333          */
334         struct list_head userfault_list;
335
336         /* Manual runtime pm autosuspend delay for user GGTT mmaps */
337         struct intel_wakeref_auto userfault_wakeref;
338
339         struct mutex error_mutex;
340         struct drm_mm_node error_capture;
341         struct drm_mm_node uc_fw;
342 };
343
344 struct i915_ppgtt {
345         struct i915_address_space vm;
346
347         struct i915_page_directory *pd;
348 };
349
350 #define i915_is_ggtt(vm) ((vm)->is_ggtt)
351
352 static inline bool
353 i915_vm_is_4lvl(const struct i915_address_space *vm)
354 {
355         return (vm->total - 1) >> 32;
356 }
357
358 static inline bool
359 i915_vm_has_scratch_64K(struct i915_address_space *vm)
360 {
361         return vm->scratch_order == get_order(I915_GTT_PAGE_SIZE_64K);
362 }
363
364 static inline bool
365 i915_vm_has_cache_coloring(struct i915_address_space *vm)
366 {
367         return i915_is_ggtt(vm) && vm->mm.color_adjust;
368 }
369
370 static inline struct i915_ggtt *
371 i915_vm_to_ggtt(struct i915_address_space *vm)
372 {
373         BUILD_BUG_ON(offsetof(struct i915_ggtt, vm));
374         GEM_BUG_ON(!i915_is_ggtt(vm));
375         return container_of(vm, struct i915_ggtt, vm);
376 }
377
378 static inline struct i915_ppgtt *
379 i915_vm_to_ppgtt(struct i915_address_space *vm)
380 {
381         BUILD_BUG_ON(offsetof(struct i915_ppgtt, vm));
382         GEM_BUG_ON(i915_is_ggtt(vm));
383         return container_of(vm, struct i915_ppgtt, vm);
384 }
385
386 static inline struct i915_address_space *
387 i915_vm_get(struct i915_address_space *vm)
388 {
389         kref_get(&vm->ref);
390         return vm;
391 }
392
393 void i915_vm_release(struct kref *kref);
394
395 static inline void i915_vm_put(struct i915_address_space *vm)
396 {
397         kref_put(&vm->ref, i915_vm_release);
398 }
399
400 static inline struct i915_address_space *
401 i915_vm_open(struct i915_address_space *vm)
402 {
403         GEM_BUG_ON(!atomic_read(&vm->open));
404         atomic_inc(&vm->open);
405         return i915_vm_get(vm);
406 }
407
408 static inline bool
409 i915_vm_tryopen(struct i915_address_space *vm)
410 {
411         if (atomic_add_unless(&vm->open, 1, 0))
412                 return i915_vm_get(vm);
413
414         return false;
415 }
416
417 void __i915_vm_close(struct i915_address_space *vm);
418
419 static inline void
420 i915_vm_close(struct i915_address_space *vm)
421 {
422         GEM_BUG_ON(!atomic_read(&vm->open));
423         __i915_vm_close(vm);
424
425         i915_vm_put(vm);
426 }
427
428 void i915_address_space_init(struct i915_address_space *vm, int subclass);
429 void i915_address_space_fini(struct i915_address_space *vm);
430
431 static inline u32 i915_pte_index(u64 address, unsigned int pde_shift)
432 {
433         const u32 mask = NUM_PTE(pde_shift) - 1;
434
435         return (address >> PAGE_SHIFT) & mask;
436 }
437
438 /*
439  * Helper to counts the number of PTEs within the given length. This count
440  * does not cross a page table boundary, so the max value would be
441  * GEN6_PTES for GEN6, and GEN8_PTES for GEN8.
442  */
443 static inline u32 i915_pte_count(u64 addr, u64 length, unsigned int pde_shift)
444 {
445         const u64 mask = ~((1ULL << pde_shift) - 1);
446         u64 end;
447
448         GEM_BUG_ON(length == 0);
449         GEM_BUG_ON(offset_in_page(addr | length));
450
451         end = addr + length;
452
453         if ((addr & mask) != (end & mask))
454                 return NUM_PTE(pde_shift) - i915_pte_index(addr, pde_shift);
455
456         return i915_pte_index(end, pde_shift) - i915_pte_index(addr, pde_shift);
457 }
458
459 static inline u32 i915_pde_index(u64 addr, u32 shift)
460 {
461         return (addr >> shift) & I915_PDE_MASK;
462 }
463
464 static inline struct i915_page_table *
465 i915_pt_entry(const struct i915_page_directory * const pd,
466               const unsigned short n)
467 {
468         return pd->entry[n];
469 }
470
471 static inline struct i915_page_directory *
472 i915_pd_entry(const struct i915_page_directory * const pdp,
473               const unsigned short n)
474 {
475         return pdp->entry[n];
476 }
477
478 static inline dma_addr_t
479 i915_page_dir_dma_addr(const struct i915_ppgtt *ppgtt, const unsigned int n)
480 {
481         struct i915_page_table *pt = ppgtt->pd->entry[n];
482
483         return __px_dma(pt ? px_base(pt) : ppgtt->vm.scratch[ppgtt->vm.top]);
484 }
485
486 void ppgtt_init(struct i915_ppgtt *ppgtt, struct intel_gt *gt);
487
488 int i915_ggtt_probe_hw(struct drm_i915_private *i915);
489 int i915_ggtt_init_hw(struct drm_i915_private *i915);
490 int i915_ggtt_enable_hw(struct drm_i915_private *i915);
491 void i915_ggtt_enable_guc(struct i915_ggtt *ggtt);
492 void i915_ggtt_disable_guc(struct i915_ggtt *ggtt);
493 int i915_init_ggtt(struct drm_i915_private *i915);
494 void i915_ggtt_driver_release(struct drm_i915_private *i915);
495
496 static inline bool i915_ggtt_has_aperture(const struct i915_ggtt *ggtt)
497 {
498         return ggtt->mappable_end > 0;
499 }
500
501 int i915_ppgtt_init_hw(struct intel_gt *gt);
502
503 struct i915_ppgtt *i915_ppgtt_create(struct intel_gt *gt);
504
505 void i915_ggtt_suspend(struct i915_ggtt *gtt);
506 void i915_ggtt_resume(struct i915_ggtt *ggtt);
507
508 #define kmap_atomic_px(px) kmap_atomic(__px_page(px_base(px)))
509
510 void
511 fill_page_dma(struct drm_i915_gem_object *p, const u64 val, unsigned int count);
512
513 #define fill_px(px, v) fill_page_dma(px_base(px), (v), PAGE_SIZE / sizeof(u64))
514 #define fill32_px(px, v) do {                                           \
515         u64 v__ = lower_32_bits(v);                                     \
516         fill_px((px), v__ << 32 | v__);                                 \
517 } while (0)
518
519 int setup_scratch_page(struct i915_address_space *vm);
520 void free_scratch(struct i915_address_space *vm);
521
522 struct drm_i915_gem_object *alloc_pt_dma(struct i915_address_space *vm, int sz);
523 struct i915_page_table *alloc_pt(struct i915_address_space *vm);
524 struct i915_page_directory *alloc_pd(struct i915_address_space *vm);
525 struct i915_page_directory *__alloc_pd(int npde);
526
527 int pin_pt_dma(struct i915_address_space *vm, struct drm_i915_gem_object *obj);
528
529 void free_px(struct i915_address_space *vm,
530              struct i915_page_table *pt, int lvl);
531 #define free_pt(vm, px) free_px(vm, px, 0)
532 #define free_pd(vm, px) free_px(vm, px_pt(px), 1)
533
534 void
535 __set_pd_entry(struct i915_page_directory * const pd,
536                const unsigned short idx,
537                struct i915_page_table *pt,
538                u64 (*encode)(const dma_addr_t, const enum i915_cache_level));
539
540 #define set_pd_entry(pd, idx, to) \
541         __set_pd_entry((pd), (idx), px_pt(to), gen8_pde_encode)
542
543 void
544 clear_pd_entry(struct i915_page_directory * const pd,
545                const unsigned short idx,
546                const struct drm_i915_gem_object * const scratch);
547
548 bool
549 release_pd_entry(struct i915_page_directory * const pd,
550                  const unsigned short idx,
551                  struct i915_page_table * const pt,
552                  const struct drm_i915_gem_object * const scratch);
553 void gen6_ggtt_invalidate(struct i915_ggtt *ggtt);
554
555 int ggtt_set_pages(struct i915_vma *vma);
556 int ppgtt_set_pages(struct i915_vma *vma);
557 void clear_pages(struct i915_vma *vma);
558
559 void ppgtt_bind_vma(struct i915_address_space *vm,
560                     struct i915_vm_pt_stash *stash,
561                     struct i915_vma *vma,
562                     enum i915_cache_level cache_level,
563                     u32 flags);
564 void ppgtt_unbind_vma(struct i915_address_space *vm,
565                       struct i915_vma *vma);
566
567 void gtt_write_workarounds(struct intel_gt *gt);
568
569 void setup_private_pat(struct intel_uncore *uncore);
570
571 int i915_vm_alloc_pt_stash(struct i915_address_space *vm,
572                            struct i915_vm_pt_stash *stash,
573                            u64 size);
574 int i915_vm_pin_pt_stash(struct i915_address_space *vm,
575                          struct i915_vm_pt_stash *stash);
576 void i915_vm_free_pt_stash(struct i915_address_space *vm,
577                            struct i915_vm_pt_stash *stash);
578
579 struct i915_vma *
580 __vm_create_scratch_for_read(struct i915_address_space *vm, unsigned long size);
581
582 static inline struct sgt_dma {
583         struct scatterlist *sg;
584         dma_addr_t dma, max;
585 } sgt_dma(struct i915_vma *vma) {
586         struct scatterlist *sg = vma->pages->sgl;
587         dma_addr_t addr = sg_dma_address(sg);
588
589         return (struct sgt_dma){ sg, addr, addr + sg_dma_len(sg) };
590 }
591
592 #endif