]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i915/i915_gem_object.h
drm/i915: Add kerneldoc to describe i915_gem_object.vma_list
[karo-tx-linux.git] / drivers / gpu / drm / i915 / i915_gem_object.h
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #ifndef __I915_GEM_OBJECT_H__
26 #define __I915_GEM_OBJECT_H__
27
28 #include <linux/reservation.h>
29
30 #include <drm/drm_vma_manager.h>
31 #include <drm/drm_gem.h>
32 #include <drm/drmP.h>
33
34 #include <drm/i915_drm.h>
35
36 #include "i915_selftest.h"
37
38 struct drm_i915_gem_object_ops {
39         unsigned int flags;
40 #define I915_GEM_OBJECT_HAS_STRUCT_PAGE BIT(0)
41 #define I915_GEM_OBJECT_IS_SHRINKABLE   BIT(1)
42
43         /* Interface between the GEM object and its backing storage.
44          * get_pages() is called once prior to the use of the associated set
45          * of pages before to binding them into the GTT, and put_pages() is
46          * called after we no longer need them. As we expect there to be
47          * associated cost with migrating pages between the backing storage
48          * and making them available for the GPU (e.g. clflush), we may hold
49          * onto the pages after they are no longer referenced by the GPU
50          * in case they may be used again shortly (for example migrating the
51          * pages to a different memory domain within the GTT). put_pages()
52          * will therefore most likely be called when the object itself is
53          * being released or under memory pressure (where we attempt to
54          * reap pages for the shrinker).
55          */
56         struct sg_table *(*get_pages)(struct drm_i915_gem_object *);
57         void (*put_pages)(struct drm_i915_gem_object *, struct sg_table *);
58
59         int (*pwrite)(struct drm_i915_gem_object *,
60                       const struct drm_i915_gem_pwrite *);
61
62         int (*dmabuf_export)(struct drm_i915_gem_object *);
63         void (*release)(struct drm_i915_gem_object *);
64 };
65
66 struct drm_i915_gem_object {
67         struct drm_gem_object base;
68
69         const struct drm_i915_gem_object_ops *ops;
70
71         /**
72          * @vma_list: List of VMAs backed by this object
73          *
74          * The VMA on this list are ordered by type, all GGTT vma are placed
75          * at the head and all ppGTT vma are placed at the tail. The different
76          * types of GGTT vma are unordered between themselves, use the
77          * @vma_tree (which has a defined order between all VMA) to find an
78          * exact match.
79          */
80         struct list_head vma_list;
81         /**
82          * @vma_tree: Ordered tree of VMAs backed by this object
83          *
84          * All VMA created for this object are placed in the @vma_tree for
85          * fast retrieval via a binary search in i915_vma_instance().
86          * They are also added to @vma_list for easy iteration.
87          */
88         struct rb_root vma_tree;
89
90         /** Stolen memory for this object, instead of being backed by shmem. */
91         struct drm_mm_node *stolen;
92         struct list_head global_link;
93         union {
94                 struct rcu_head rcu;
95                 struct llist_node freed;
96         };
97
98         /**
99          * Whether the object is currently in the GGTT mmap.
100          */
101         struct list_head userfault_link;
102
103         /** Used in execbuf to temporarily hold a ref */
104         struct list_head obj_exec_link;
105
106         struct list_head batch_pool_link;
107         I915_SELFTEST_DECLARE(struct list_head st_link);
108
109         unsigned long flags;
110
111         /**
112          * Have we taken a reference for the object for incomplete GPU
113          * activity?
114          */
115 #define I915_BO_ACTIVE_REF 0
116
117         /*
118          * Is the object to be mapped as read-only to the GPU
119          * Only honoured if hardware has relevant pte bit
120          */
121         unsigned long gt_ro:1;
122         unsigned int cache_level:3;
123         unsigned int cache_dirty:1;
124
125         atomic_t frontbuffer_bits;
126         unsigned int frontbuffer_ggtt_origin; /* write once */
127         struct i915_gem_active frontbuffer_write;
128
129         /** Current tiling stride for the object, if it's tiled. */
130         unsigned int tiling_and_stride;
131 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
132 #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
133 #define STRIDE_MASK (~TILING_MASK)
134
135         /** Count of VMA actually bound by this object */
136         unsigned int bind_count;
137         unsigned int active_count;
138         unsigned int pin_display;
139
140         struct {
141                 struct mutex lock; /* protects the pages and their use */
142                 atomic_t pages_pin_count;
143
144                 struct sg_table *pages;
145                 void *mapping;
146
147                 struct i915_gem_object_page_iter {
148                         struct scatterlist *sg_pos;
149                         unsigned int sg_idx; /* in pages, but 32bit eek! */
150
151                         struct radix_tree_root radix;
152                         struct mutex lock; /* protects this cache */
153                 } get_page;
154
155                 /**
156                  * Advice: are the backing pages purgeable?
157                  */
158                 unsigned int madv:2;
159
160                 /**
161                  * This is set if the object has been written to since the
162                  * pages were last acquired.
163                  */
164                 bool dirty:1;
165
166                 /**
167                  * This is set if the object has been pinned due to unknown
168                  * swizzling.
169                  */
170                 bool quirked:1;
171         } mm;
172
173         /** Breadcrumb of last rendering to the buffer.
174          * There can only be one writer, but we allow for multiple readers.
175          * If there is a writer that necessarily implies that all other
176          * read requests are complete - but we may only be lazily clearing
177          * the read requests. A read request is naturally the most recent
178          * request on a ring, so we may have two different write and read
179          * requests on one ring where the write request is older than the
180          * read request. This allows for the CPU to read from an active
181          * buffer by only waiting for the write to complete.
182          */
183         struct reservation_object *resv;
184
185         /** References from framebuffers, locks out tiling changes. */
186         unsigned int framebuffer_references;
187
188         /** Record of address bit 17 of each page at last unbind. */
189         unsigned long *bit_17;
190
191         union {
192                 struct i915_gem_userptr {
193                         uintptr_t ptr;
194                         unsigned read_only :1;
195
196                         struct i915_mm_struct *mm;
197                         struct i915_mmu_object *mmu_object;
198                         struct work_struct *work;
199                 } userptr;
200
201                 unsigned long scratch;
202         };
203
204         /** for phys allocated objects */
205         struct drm_dma_handle *phys_handle;
206
207         struct reservation_object __builtin_resv;
208 };
209
210 static inline struct drm_i915_gem_object *
211 to_intel_bo(struct drm_gem_object *gem)
212 {
213         /* Assert that to_intel_bo(NULL) == NULL */
214         BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
215
216         return container_of(gem, struct drm_i915_gem_object, base);
217 }
218
219 /**
220  * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
221  * @filp: DRM file private date
222  * @handle: userspace handle
223  *
224  * Returns:
225  *
226  * A pointer to the object named by the handle if such exists on @filp, NULL
227  * otherwise. This object is only valid whilst under the RCU read lock, and
228  * note carefully the object may be in the process of being destroyed.
229  */
230 static inline struct drm_i915_gem_object *
231 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
232 {
233 #ifdef CONFIG_LOCKDEP
234         WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
235 #endif
236         return idr_find(&file->object_idr, handle);
237 }
238
239 static inline struct drm_i915_gem_object *
240 i915_gem_object_lookup(struct drm_file *file, u32 handle)
241 {
242         struct drm_i915_gem_object *obj;
243
244         rcu_read_lock();
245         obj = i915_gem_object_lookup_rcu(file, handle);
246         if (obj && !kref_get_unless_zero(&obj->base.refcount))
247                 obj = NULL;
248         rcu_read_unlock();
249
250         return obj;
251 }
252
253 __deprecated
254 extern struct drm_gem_object *
255 drm_gem_object_lookup(struct drm_file *file, u32 handle);
256
257 __attribute__((nonnull))
258 static inline struct drm_i915_gem_object *
259 i915_gem_object_get(struct drm_i915_gem_object *obj)
260 {
261         drm_gem_object_reference(&obj->base);
262         return obj;
263 }
264
265 __deprecated
266 extern void drm_gem_object_reference(struct drm_gem_object *);
267
268 __attribute__((nonnull))
269 static inline void
270 i915_gem_object_put(struct drm_i915_gem_object *obj)
271 {
272         __drm_gem_object_unreference(&obj->base);
273 }
274
275 __deprecated
276 extern void drm_gem_object_unreference(struct drm_gem_object *);
277
278 __deprecated
279 extern void drm_gem_object_unreference_unlocked(struct drm_gem_object *);
280
281 static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj)
282 {
283         reservation_object_lock(obj->resv, NULL);
284 }
285
286 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
287 {
288         reservation_object_unlock(obj->resv);
289 }
290
291 static inline bool
292 i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
293 {
294         return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
295 }
296
297 static inline bool
298 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
299 {
300         return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
301 }
302
303 static inline bool
304 i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
305 {
306         return obj->active_count;
307 }
308
309 static inline bool
310 i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
311 {
312         return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
313 }
314
315 static inline void
316 i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
317 {
318         lockdep_assert_held(&obj->base.dev->struct_mutex);
319         __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
320 }
321
322 static inline void
323 i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
324 {
325         lockdep_assert_held(&obj->base.dev->struct_mutex);
326         __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
327 }
328
329 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
330
331 static inline bool
332 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
333 {
334         return READ_ONCE(obj->framebuffer_references);
335 }
336
337 static inline unsigned int
338 i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
339 {
340         return obj->tiling_and_stride & TILING_MASK;
341 }
342
343 static inline bool
344 i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
345 {
346         return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
347 }
348
349 static inline unsigned int
350 i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
351 {
352         return obj->tiling_and_stride & STRIDE_MASK;
353 }
354
355 static inline unsigned int
356 i915_gem_tile_height(unsigned int tiling)
357 {
358         GEM_BUG_ON(!tiling);
359         return tiling == I915_TILING_Y ? 32 : 8;
360 }
361
362 static inline unsigned int
363 i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
364 {
365         return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
366 }
367
368 static inline unsigned int
369 i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
370 {
371         return (i915_gem_object_get_stride(obj) *
372                 i915_gem_object_get_tile_height(obj));
373 }
374
375 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
376                                unsigned int tiling, unsigned int stride);
377
378 static inline struct intel_engine_cs *
379 i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
380 {
381         struct intel_engine_cs *engine = NULL;
382         struct dma_fence *fence;
383
384         rcu_read_lock();
385         fence = reservation_object_get_excl_rcu(obj->resv);
386         rcu_read_unlock();
387
388         if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
389                 engine = to_request(fence)->engine;
390         dma_fence_put(fence);
391
392         return engine;
393 }
394
395 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
396
397 #endif
398