Merge tag 'exynos-drm-next-for-v5.14' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gem / i915_gem_object_types.h
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6
7 #ifndef __I915_GEM_OBJECT_TYPES_H__
8 #define __I915_GEM_OBJECT_TYPES_H__
9
10 #include <linux/mmu_notifier.h>
11
12 #include <drm/drm_gem.h>
13 #include <drm/ttm/ttm_bo_api.h>
14 #include <uapi/drm/i915_drm.h>
15
16 #include "i915_active.h"
17 #include "i915_selftest.h"
18
19 struct drm_i915_gem_object;
20 struct intel_fronbuffer;
21
22 /*
23  * struct i915_lut_handle tracks the fast lookups from handle to vma used
24  * for execbuf. Although we use a radixtree for that mapping, in order to
25  * remove them as the object or context is closed, we need a secondary list
26  * and a translation entry (i915_lut_handle).
27  */
28 struct i915_lut_handle {
29         struct list_head obj_link;
30         struct i915_gem_context *ctx;
31         u32 handle;
32 };
33
34 struct drm_i915_gem_object_ops {
35         unsigned int flags;
36 #define I915_GEM_OBJECT_HAS_IOMEM       BIT(1)
37 #define I915_GEM_OBJECT_IS_SHRINKABLE   BIT(2)
38 #define I915_GEM_OBJECT_IS_PROXY        BIT(3)
39 #define I915_GEM_OBJECT_NO_MMAP         BIT(4)
40
41         /* Interface between the GEM object and its backing storage.
42          * get_pages() is called once prior to the use of the associated set
43          * of pages before to binding them into the GTT, and put_pages() is
44          * called after we no longer need them. As we expect there to be
45          * associated cost with migrating pages between the backing storage
46          * and making them available for the GPU (e.g. clflush), we may hold
47          * onto the pages after they are no longer referenced by the GPU
48          * in case they may be used again shortly (for example migrating the
49          * pages to a different memory domain within the GTT). put_pages()
50          * will therefore most likely be called when the object itself is
51          * being released or under memory pressure (where we attempt to
52          * reap pages for the shrinker).
53          */
54         int (*get_pages)(struct drm_i915_gem_object *obj);
55         void (*put_pages)(struct drm_i915_gem_object *obj,
56                           struct sg_table *pages);
57         void (*truncate)(struct drm_i915_gem_object *obj);
58         void (*writeback)(struct drm_i915_gem_object *obj);
59
60         int (*pread)(struct drm_i915_gem_object *obj,
61                      const struct drm_i915_gem_pread *arg);
62         int (*pwrite)(struct drm_i915_gem_object *obj,
63                       const struct drm_i915_gem_pwrite *arg);
64
65         int (*dmabuf_export)(struct drm_i915_gem_object *obj);
66         void (*release)(struct drm_i915_gem_object *obj);
67
68         const char *name; /* friendly name for debug, e.g. lockdep classes */
69 };
70
71 enum i915_map_type {
72         I915_MAP_WB = 0,
73         I915_MAP_WC,
74 #define I915_MAP_OVERRIDE BIT(31)
75         I915_MAP_FORCE_WB = I915_MAP_WB | I915_MAP_OVERRIDE,
76         I915_MAP_FORCE_WC = I915_MAP_WC | I915_MAP_OVERRIDE,
77 };
78
79 enum i915_mmap_type {
80         I915_MMAP_TYPE_GTT = 0,
81         I915_MMAP_TYPE_WC,
82         I915_MMAP_TYPE_WB,
83         I915_MMAP_TYPE_UC,
84 };
85
86 struct i915_mmap_offset {
87         struct drm_vma_offset_node vma_node;
88         struct drm_i915_gem_object *obj;
89         enum i915_mmap_type mmap_type;
90
91         struct rb_node offset;
92 };
93
94 struct i915_gem_object_page_iter {
95         struct scatterlist *sg_pos;
96         unsigned int sg_idx; /* in pages, but 32bit eek! */
97
98         struct radix_tree_root radix;
99         struct mutex lock; /* protects this cache */
100 };
101
102 struct drm_i915_gem_object {
103         /*
104          * We might have reason to revisit the below since it wastes
105          * a lot of space for non-ttm gem objects.
106          * In any case, always use the accessors for the ttm_buffer_object
107          * when accessing it.
108          */
109         union {
110                 struct drm_gem_object base;
111                 struct ttm_buffer_object __do_not_access;
112         };
113
114         const struct drm_i915_gem_object_ops *ops;
115
116         struct {
117                 /**
118                  * @vma.lock: protect the list/tree of vmas
119                  */
120                 spinlock_t lock;
121
122                 /**
123                  * @vma.list: List of VMAs backed by this object
124                  *
125                  * The VMA on this list are ordered by type, all GGTT vma are
126                  * placed at the head and all ppGTT vma are placed at the tail.
127                  * The different types of GGTT vma are unordered between
128                  * themselves, use the @vma.tree (which has a defined order
129                  * between all VMA) to quickly find an exact match.
130                  */
131                 struct list_head list;
132
133                 /**
134                  * @vma.tree: Ordered tree of VMAs backed by this object
135                  *
136                  * All VMA created for this object are placed in the @vma.tree
137                  * for fast retrieval via a binary search in
138                  * i915_vma_instance(). They are also added to @vma.list for
139                  * easy iteration.
140                  */
141                 struct rb_root tree;
142         } vma;
143
144         /**
145          * @lut_list: List of vma lookup entries in use for this object.
146          *
147          * If this object is closed, we need to remove all of its VMA from
148          * the fast lookup index in associated contexts; @lut_list provides
149          * this translation from object to context->handles_vma.
150          */
151         struct list_head lut_list;
152         spinlock_t lut_lock; /* guards lut_list */
153
154         /**
155          * @obj_link: Link into @i915_gem_ww_ctx.obj_list
156          *
157          * When we lock this object through i915_gem_object_lock() with a
158          * context, we add it to the list to ensure we can unlock everything
159          * when i915_gem_ww_ctx_backoff() or i915_gem_ww_ctx_fini() are called.
160          */
161         struct list_head obj_link;
162         /**
163          * @shared_resv_from: The object shares the resv from this vm.
164          */
165         struct i915_address_space *shares_resv_from;
166
167         union {
168                 struct rcu_head rcu;
169                 struct llist_node freed;
170         };
171
172         /**
173          * Whether the object is currently in the GGTT mmap.
174          */
175         unsigned int userfault_count;
176         struct list_head userfault_link;
177
178         struct {
179                 spinlock_t lock; /* Protects access to mmo offsets */
180                 struct rb_root offsets;
181         } mmo;
182
183         I915_SELFTEST_DECLARE(struct list_head st_link);
184
185         unsigned long flags;
186 #define I915_BO_ALLOC_CONTIGUOUS BIT(0)
187 #define I915_BO_ALLOC_VOLATILE   BIT(1)
188 #define I915_BO_ALLOC_STRUCT_PAGE BIT(2)
189 #define I915_BO_ALLOC_CPU_CLEAR  BIT(3)
190 #define I915_BO_ALLOC_FLAGS (I915_BO_ALLOC_CONTIGUOUS | \
191                              I915_BO_ALLOC_VOLATILE | \
192                              I915_BO_ALLOC_STRUCT_PAGE | \
193                              I915_BO_ALLOC_CPU_CLEAR)
194 #define I915_BO_READONLY         BIT(4)
195 #define I915_TILING_QUIRK_BIT    5 /* unknown swizzling; do not release! */
196
197         /*
198          * Is the object to be mapped as read-only to the GPU
199          * Only honoured if hardware has relevant pte bit
200          */
201         unsigned int cache_level:3;
202         unsigned int cache_coherent:2;
203 #define I915_BO_CACHE_COHERENT_FOR_READ BIT(0)
204 #define I915_BO_CACHE_COHERENT_FOR_WRITE BIT(1)
205         unsigned int cache_dirty:1;
206
207         /**
208          * @read_domains: Read memory domains.
209          *
210          * These monitor which caches contain read/write data related to the
211          * object. When transitioning from one set of domains to another,
212          * the driver is called to ensure that caches are suitably flushed and
213          * invalidated.
214          */
215         u16 read_domains;
216
217         /**
218          * @write_domain: Corresponding unique write memory domain.
219          */
220         u16 write_domain;
221
222         struct intel_frontbuffer __rcu *frontbuffer;
223
224         /** Current tiling stride for the object, if it's tiled. */
225         unsigned int tiling_and_stride;
226 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
227 #define TILING_MASK (FENCE_MINIMUM_STRIDE - 1)
228 #define STRIDE_MASK (~TILING_MASK)
229
230         struct {
231                 /*
232                  * Protects the pages and their use. Do not use directly, but
233                  * instead go through the pin/unpin interfaces.
234                  */
235                 atomic_t pages_pin_count;
236                 atomic_t shrink_pin;
237
238                 /**
239                  * Priority list of potential placements for this object.
240                  */
241                 struct intel_memory_region **placements;
242                 int n_placements;
243
244                 /**
245                  * Memory region for this object.
246                  */
247                 struct intel_memory_region *region;
248
249                 /**
250                  * Memory manager node allocated for this object.
251                  */
252                 void *st_mm_node;
253
254                 /**
255                  * Element within memory_region->objects or region->purgeable
256                  * if the object is marked as DONTNEED. Access is protected by
257                  * region->obj_lock.
258                  */
259                 struct list_head region_link;
260
261                 struct sg_table *pages;
262                 void *mapping;
263
264                 struct i915_page_sizes {
265                         /**
266                          * The sg mask of the pages sg_table. i.e the mask of
267                          * of the lengths for each sg entry.
268                          */
269                         unsigned int phys;
270
271                         /**
272                          * The gtt page sizes we are allowed to use given the
273                          * sg mask and the supported page sizes. This will
274                          * express the smallest unit we can use for the whole
275                          * object, as well as the larger sizes we may be able
276                          * to use opportunistically.
277                          */
278                         unsigned int sg;
279
280                         /**
281                          * The actual gtt page size usage. Since we can have
282                          * multiple vma associated with this object we need to
283                          * prevent any trampling of state, hence a copy of this
284                          * struct also lives in each vma, therefore the gtt
285                          * value here should only be read/write through the vma.
286                          */
287                         unsigned int gtt;
288                 } page_sizes;
289
290                 I915_SELFTEST_DECLARE(unsigned int page_mask);
291
292                 struct i915_gem_object_page_iter get_page;
293                 struct i915_gem_object_page_iter get_dma_page;
294
295                 /**
296                  * Element within i915->mm.unbound_list or i915->mm.bound_list,
297                  * locked by i915->mm.obj_lock.
298                  */
299                 struct list_head link;
300
301                 /**
302                  * Advice: are the backing pages purgeable?
303                  */
304                 unsigned int madv:2;
305
306                 /**
307                  * This is set if the object has been written to since the
308                  * pages were last acquired.
309                  */
310                 bool dirty:1;
311         } mm;
312
313         /** Record of address bit 17 of each page at last unbind. */
314         unsigned long *bit_17;
315
316         union {
317 #ifdef CONFIG_MMU_NOTIFIER
318                 struct i915_gem_userptr {
319                         uintptr_t ptr;
320                         unsigned long notifier_seq;
321
322                         struct mmu_interval_notifier notifier;
323                         struct page **pvec;
324                         int page_ref;
325                 } userptr;
326 #endif
327
328                 struct drm_mm_node *stolen;
329
330                 unsigned long scratch;
331                 u64 encode;
332
333                 void *gvt_info;
334         };
335 };
336
337 static inline struct drm_i915_gem_object *
338 to_intel_bo(struct drm_gem_object *gem)
339 {
340         /* Assert that to_intel_bo(NULL) == NULL */
341         BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
342
343         return container_of(gem, struct drm_i915_gem_object, base);
344 }
345
346 #endif