Merge tag 'acpi-5.9-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[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 (*pwrite)(struct drm_i915_gem_object *obj,
60                       const struct drm_i915_gem_pwrite *arg);
61
62         int (*dmabuf_export)(struct drm_i915_gem_object *obj);
63         void (*release)(struct drm_i915_gem_object *obj);
64
65         const char *name; /* friendly name for debug, e.g. lockdep classes */
66 };
67
68 enum i915_mmap_type {
69         I915_MMAP_TYPE_GTT = 0,
70         I915_MMAP_TYPE_WC,
71         I915_MMAP_TYPE_WB,
72         I915_MMAP_TYPE_UC,
73 };
74
75 struct i915_mmap_offset {
76         struct drm_vma_offset_node vma_node;
77         struct drm_i915_gem_object *obj;
78         enum i915_mmap_type mmap_type;
79
80         struct rb_node offset;
81 };
82
83 struct drm_i915_gem_object {
84         struct drm_gem_object base;
85
86         const struct drm_i915_gem_object_ops *ops;
87
88         struct {
89                 /**
90                  * @vma.lock: protect the list/tree of vmas
91                  */
92                 spinlock_t lock;
93
94                 /**
95                  * @vma.list: List of VMAs backed by this object
96                  *
97                  * The VMA on this list are ordered by type, all GGTT vma are
98                  * placed at the head and all ppGTT vma are placed at the tail.
99                  * The different types of GGTT vma are unordered between
100                  * themselves, use the @vma.tree (which has a defined order
101                  * between all VMA) to quickly find an exact match.
102                  */
103                 struct list_head list;
104
105                 /**
106                  * @vma.tree: Ordered tree of VMAs backed by this object
107                  *
108                  * All VMA created for this object are placed in the @vma.tree
109                  * for fast retrieval via a binary search in
110                  * i915_vma_instance(). They are also added to @vma.list for
111                  * easy iteration.
112                  */
113                 struct rb_root tree;
114         } vma;
115
116         /**
117          * @lut_list: List of vma lookup entries in use for this object.
118          *
119          * If this object is closed, we need to remove all of its VMA from
120          * the fast lookup index in associated contexts; @lut_list provides
121          * this translation from object to context->handles_vma.
122          */
123         struct list_head lut_list;
124         spinlock_t lut_lock; /* guards lut_list */
125
126         /** Stolen memory for this object, instead of being backed by shmem. */
127         struct drm_mm_node *stolen;
128         union {
129                 struct rcu_head rcu;
130                 struct llist_node freed;
131         };
132
133         /**
134          * Whether the object is currently in the GGTT mmap.
135          */
136         unsigned int userfault_count;
137         struct list_head userfault_link;
138
139         struct {
140                 spinlock_t lock; /* Protects access to mmo offsets */
141                 struct rb_root offsets;
142         } mmo;
143
144         I915_SELFTEST_DECLARE(struct list_head st_link);
145
146         unsigned long flags;
147 #define I915_BO_ALLOC_CONTIGUOUS BIT(0)
148 #define I915_BO_ALLOC_VOLATILE   BIT(1)
149 #define I915_BO_ALLOC_FLAGS (I915_BO_ALLOC_CONTIGUOUS | I915_BO_ALLOC_VOLATILE)
150 #define I915_BO_READONLY         BIT(2)
151
152         /*
153          * Is the object to be mapped as read-only to the GPU
154          * Only honoured if hardware has relevant pte bit
155          */
156         unsigned int cache_level:3;
157         unsigned int cache_coherent:2;
158 #define I915_BO_CACHE_COHERENT_FOR_READ BIT(0)
159 #define I915_BO_CACHE_COHERENT_FOR_WRITE BIT(1)
160         unsigned int cache_dirty:1;
161
162         /**
163          * @read_domains: Read memory domains.
164          *
165          * These monitor which caches contain read/write data related to the
166          * object. When transitioning from one set of domains to another,
167          * the driver is called to ensure that caches are suitably flushed and
168          * invalidated.
169          */
170         u16 read_domains;
171
172         /**
173          * @write_domain: Corresponding unique write memory domain.
174          */
175         u16 write_domain;
176
177         struct intel_frontbuffer __rcu *frontbuffer;
178
179         /** Current tiling stride for the object, if it's tiled. */
180         unsigned int tiling_and_stride;
181 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
182 #define TILING_MASK (FENCE_MINIMUM_STRIDE - 1)
183 #define STRIDE_MASK (~TILING_MASK)
184
185         struct {
186                 /*
187                  * Protects the pages and their use. Do not use directly, but
188                  * instead go through the pin/unpin interfaces.
189                  */
190                 struct mutex lock;
191                 atomic_t pages_pin_count;
192                 atomic_t shrink_pin;
193
194                 /**
195                  * Memory region for this object.
196                  */
197                 struct intel_memory_region *region;
198                 /**
199                  * List of memory region blocks allocated for this object.
200                  */
201                 struct list_head blocks;
202                 /**
203                  * Element within memory_region->objects or region->purgeable
204                  * if the object is marked as DONTNEED. Access is protected by
205                  * region->obj_lock.
206                  */
207                 struct list_head region_link;
208
209                 struct sg_table *pages;
210                 void *mapping;
211
212                 struct i915_page_sizes {
213                         /**
214                          * The sg mask of the pages sg_table. i.e the mask of
215                          * of the lengths for each sg entry.
216                          */
217                         unsigned int phys;
218
219                         /**
220                          * The gtt page sizes we are allowed to use given the
221                          * sg mask and the supported page sizes. This will
222                          * express the smallest unit we can use for the whole
223                          * object, as well as the larger sizes we may be able
224                          * to use opportunistically.
225                          */
226                         unsigned int sg;
227
228                         /**
229                          * The actual gtt page size usage. Since we can have
230                          * multiple vma associated with this object we need to
231                          * prevent any trampling of state, hence a copy of this
232                          * struct also lives in each vma, therefore the gtt
233                          * value here should only be read/write through the vma.
234                          */
235                         unsigned int gtt;
236                 } page_sizes;
237
238                 I915_SELFTEST_DECLARE(unsigned int page_mask);
239
240                 struct i915_gem_object_page_iter {
241                         struct scatterlist *sg_pos;
242                         unsigned int sg_idx; /* in pages, but 32bit eek! */
243
244                         struct radix_tree_root radix;
245                         struct mutex lock; /* protects this cache */
246                 } get_page;
247
248                 /**
249                  * Element within i915->mm.unbound_list or i915->mm.bound_list,
250                  * locked by i915->mm.obj_lock.
251                  */
252                 struct list_head link;
253
254                 /**
255                  * Advice: are the backing pages purgeable?
256                  */
257                 unsigned int madv:2;
258
259                 /**
260                  * This is set if the object has been written to since the
261                  * pages were last acquired.
262                  */
263                 bool dirty:1;
264
265                 /**
266                  * This is set if the object has been pinned due to unknown
267                  * swizzling.
268                  */
269                 bool quirked:1;
270         } mm;
271
272         /** Record of address bit 17 of each page at last unbind. */
273         unsigned long *bit_17;
274
275         union {
276                 struct i915_gem_userptr {
277                         uintptr_t ptr;
278
279                         struct i915_mm_struct *mm;
280                         struct i915_mmu_object *mmu_object;
281                         struct work_struct *work;
282                 } userptr;
283
284                 unsigned long scratch;
285
286                 void *gvt_info;
287         };
288 };
289
290 static inline struct drm_i915_gem_object *
291 to_intel_bo(struct drm_gem_object *gem)
292 {
293         /* Assert that to_intel_bo(NULL) == NULL */
294         BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
295
296         return container_of(gem, struct drm_i915_gem_object, base);
297 }
298
299 #endif