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