GNU Linux-libre 4.19.286-gnu1
[releases.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_request.h"
37 #include "i915_selftest.h"
38
39 struct drm_i915_gem_object;
40
41 /*
42  * struct i915_lut_handle tracks the fast lookups from handle to vma used
43  * for execbuf. Although we use a radixtree for that mapping, in order to
44  * remove them as the object or context is closed, we need a secondary list
45  * and a translation entry (i915_lut_handle).
46  */
47 struct i915_lut_handle {
48         struct list_head obj_link;
49         struct list_head ctx_link;
50         struct i915_gem_context *ctx;
51         u32 handle;
52 };
53
54 struct drm_i915_gem_object_ops {
55         unsigned int flags;
56 #define I915_GEM_OBJECT_HAS_STRUCT_PAGE BIT(0)
57 #define I915_GEM_OBJECT_IS_SHRINKABLE   BIT(1)
58 #define I915_GEM_OBJECT_IS_PROXY        BIT(2)
59
60         /* Interface between the GEM object and its backing storage.
61          * get_pages() is called once prior to the use of the associated set
62          * of pages before to binding them into the GTT, and put_pages() is
63          * called after we no longer need them. As we expect there to be
64          * associated cost with migrating pages between the backing storage
65          * and making them available for the GPU (e.g. clflush), we may hold
66          * onto the pages after they are no longer referenced by the GPU
67          * in case they may be used again shortly (for example migrating the
68          * pages to a different memory domain within the GTT). put_pages()
69          * will therefore most likely be called when the object itself is
70          * being released or under memory pressure (where we attempt to
71          * reap pages for the shrinker).
72          */
73         int (*get_pages)(struct drm_i915_gem_object *);
74         void (*put_pages)(struct drm_i915_gem_object *, struct sg_table *);
75
76         int (*pwrite)(struct drm_i915_gem_object *,
77                       const struct drm_i915_gem_pwrite *);
78
79         int (*dmabuf_export)(struct drm_i915_gem_object *);
80         void (*release)(struct drm_i915_gem_object *);
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         /**
89          * @vma_list: List of VMAs backed by this object
90          *
91          * The VMA on this list are ordered by type, all GGTT vma are placed
92          * at the head and all ppGTT vma are placed at the tail. The different
93          * types of GGTT vma are unordered between themselves, use the
94          * @vma_tree (which has a defined order between all VMA) to find an
95          * exact match.
96          */
97         struct list_head vma_list;
98         /**
99          * @vma_tree: Ordered tree of VMAs backed by this object
100          *
101          * All VMA created for this object are placed in the @vma_tree for
102          * fast retrieval via a binary search in i915_vma_instance().
103          * They are also added to @vma_list for easy iteration.
104          */
105         struct rb_root vma_tree;
106
107         /**
108          * @lut_list: List of vma lookup entries in use for this object.
109          *
110          * If this object is closed, we need to remove all of its VMA from
111          * the fast lookup index in associated contexts; @lut_list provides
112          * this translation from object to context->handles_vma.
113          */
114         struct list_head lut_list;
115
116         /** Stolen memory for this object, instead of being backed by shmem. */
117         struct drm_mm_node *stolen;
118         union {
119                 struct rcu_head rcu;
120                 struct llist_node freed;
121         };
122
123         /**
124          * Whether the object is currently in the GGTT mmap.
125          */
126         unsigned int userfault_count;
127         struct list_head userfault_link;
128
129         struct list_head batch_pool_link;
130         I915_SELFTEST_DECLARE(struct list_head st_link);
131
132         unsigned long flags;
133
134         /**
135          * Have we taken a reference for the object for incomplete GPU
136          * activity?
137          */
138 #define I915_BO_ACTIVE_REF 0
139 #define I915_BO_WAS_BOUND_BIT    1
140
141         /*
142          * Is the object to be mapped as read-only to the GPU
143          * Only honoured if hardware has relevant pte bit
144          */
145         unsigned int cache_level:3;
146         unsigned int cache_coherent:2;
147 #define I915_BO_CACHE_COHERENT_FOR_READ BIT(0)
148 #define I915_BO_CACHE_COHERENT_FOR_WRITE BIT(1)
149         unsigned int cache_dirty:1;
150
151         /**
152          * @read_domains: Read memory domains.
153          *
154          * These monitor which caches contain read/write data related to the
155          * object. When transitioning from one set of domains to another,
156          * the driver is called to ensure that caches are suitably flushed and
157          * invalidated.
158          */
159         u16 read_domains;
160
161         /**
162          * @write_domain: Corresponding unique write memory domain.
163          */
164         u16 write_domain;
165
166         atomic_t frontbuffer_bits;
167         unsigned int frontbuffer_ggtt_origin; /* write once */
168         struct i915_gem_active frontbuffer_write;
169
170         /** Current tiling stride for the object, if it's tiled. */
171         unsigned int tiling_and_stride;
172 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
173 #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
174 #define STRIDE_MASK (~TILING_MASK)
175
176         /** Count of VMA actually bound by this object */
177         unsigned int bind_count;
178         unsigned int active_count;
179         /** Count of how many global VMA are currently pinned for use by HW */
180         unsigned int pin_global;
181
182         struct {
183                 struct mutex lock; /* protects the pages and their use */
184                 atomic_t pages_pin_count;
185
186                 struct sg_table *pages;
187                 void *mapping;
188
189                 /* TODO: whack some of this into the error state */
190                 struct i915_page_sizes {
191                         /**
192                          * The sg mask of the pages sg_table. i.e the mask of
193                          * of the lengths for each sg entry.
194                          */
195                         unsigned int phys;
196
197                         /**
198                          * The gtt page sizes we are allowed to use given the
199                          * sg mask and the supported page sizes. This will
200                          * express the smallest unit we can use for the whole
201                          * object, as well as the larger sizes we may be able
202                          * to use opportunistically.
203                          */
204                         unsigned int sg;
205
206                         /**
207                          * The actual gtt page size usage. Since we can have
208                          * multiple vma associated with this object we need to
209                          * prevent any trampling of state, hence a copy of this
210                          * struct also lives in each vma, therefore the gtt
211                          * value here should only be read/write through the vma.
212                          */
213                         unsigned int gtt;
214                 } page_sizes;
215
216                 I915_SELFTEST_DECLARE(unsigned int page_mask);
217
218                 struct i915_gem_object_page_iter {
219                         struct scatterlist *sg_pos;
220                         unsigned int sg_idx; /* in pages, but 32bit eek! */
221
222                         struct radix_tree_root radix;
223                         struct mutex lock; /* protects this cache */
224                 } get_page;
225
226                 /**
227                  * Element within i915->mm.unbound_list or i915->mm.bound_list,
228                  * locked by i915->mm.obj_lock.
229                  */
230                 struct list_head link;
231
232                 /**
233                  * Advice: are the backing pages purgeable?
234                  */
235                 unsigned int madv:2;
236
237                 /**
238                  * This is set if the object has been written to since the
239                  * pages were last acquired.
240                  */
241                 bool dirty:1;
242
243                 /**
244                  * This is set if the object has been pinned due to unknown
245                  * swizzling.
246                  */
247                 bool quirked:1;
248         } mm;
249
250         /** Breadcrumb of last rendering to the buffer.
251          * There can only be one writer, but we allow for multiple readers.
252          * If there is a writer that necessarily implies that all other
253          * read requests are complete - but we may only be lazily clearing
254          * the read requests. A read request is naturally the most recent
255          * request on a ring, so we may have two different write and read
256          * requests on one ring where the write request is older than the
257          * read request. This allows for the CPU to read from an active
258          * buffer by only waiting for the write to complete.
259          */
260         struct reservation_object *resv;
261
262         /** References from framebuffers, locks out tiling changes. */
263         unsigned int framebuffer_references;
264
265         /** Record of address bit 17 of each page at last unbind. */
266         unsigned long *bit_17;
267
268         union {
269                 struct i915_gem_userptr {
270                         uintptr_t ptr;
271
272                         struct i915_mm_struct *mm;
273                         struct i915_mmu_object *mmu_object;
274                         struct work_struct *work;
275                 } userptr;
276
277                 unsigned long scratch;
278
279                 void *gvt_info;
280         };
281
282         /** for phys allocated objects */
283         struct drm_dma_handle *phys_handle;
284
285         struct reservation_object __builtin_resv;
286 };
287
288 static inline struct drm_i915_gem_object *
289 to_intel_bo(struct drm_gem_object *gem)
290 {
291         /* Assert that to_intel_bo(NULL) == NULL */
292         BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
293
294         return container_of(gem, struct drm_i915_gem_object, base);
295 }
296
297 /**
298  * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
299  * @filp: DRM file private date
300  * @handle: userspace handle
301  *
302  * Returns:
303  *
304  * A pointer to the object named by the handle if such exists on @filp, NULL
305  * otherwise. This object is only valid whilst under the RCU read lock, and
306  * note carefully the object may be in the process of being destroyed.
307  */
308 static inline struct drm_i915_gem_object *
309 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
310 {
311 #ifdef CONFIG_LOCKDEP
312         WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
313 #endif
314         return idr_find(&file->object_idr, handle);
315 }
316
317 static inline struct drm_i915_gem_object *
318 i915_gem_object_lookup(struct drm_file *file, u32 handle)
319 {
320         struct drm_i915_gem_object *obj;
321
322         rcu_read_lock();
323         obj = i915_gem_object_lookup_rcu(file, handle);
324         if (obj && !kref_get_unless_zero(&obj->base.refcount))
325                 obj = NULL;
326         rcu_read_unlock();
327
328         return obj;
329 }
330
331 __deprecated
332 extern struct drm_gem_object *
333 drm_gem_object_lookup(struct drm_file *file, u32 handle);
334
335 __attribute__((nonnull))
336 static inline struct drm_i915_gem_object *
337 i915_gem_object_get(struct drm_i915_gem_object *obj)
338 {
339         drm_gem_object_get(&obj->base);
340         return obj;
341 }
342
343 __attribute__((nonnull))
344 static inline void
345 i915_gem_object_put(struct drm_i915_gem_object *obj)
346 {
347         __drm_gem_object_put(&obj->base);
348 }
349
350 static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj)
351 {
352         reservation_object_lock(obj->resv, NULL);
353 }
354
355 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
356 {
357         reservation_object_unlock(obj->resv);
358 }
359
360 static inline void
361 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
362 {
363         obj->base.vma_node.readonly = true;
364 }
365
366 static inline bool
367 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
368 {
369         return obj->base.vma_node.readonly;
370 }
371
372 static inline bool
373 i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
374 {
375         return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
376 }
377
378 static inline bool
379 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
380 {
381         return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
382 }
383
384 static inline bool
385 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
386 {
387         return obj->ops->flags & I915_GEM_OBJECT_IS_PROXY;
388 }
389
390 static inline bool
391 i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
392 {
393         return obj->active_count;
394 }
395
396 static inline bool
397 i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
398 {
399         return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
400 }
401
402 static inline void
403 i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
404 {
405         lockdep_assert_held(&obj->base.dev->struct_mutex);
406         __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
407 }
408
409 static inline void
410 i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
411 {
412         lockdep_assert_held(&obj->base.dev->struct_mutex);
413         __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
414 }
415
416 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
417
418 static inline bool
419 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
420 {
421         return READ_ONCE(obj->framebuffer_references);
422 }
423
424 static inline unsigned int
425 i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
426 {
427         return obj->tiling_and_stride & TILING_MASK;
428 }
429
430 static inline bool
431 i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
432 {
433         return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
434 }
435
436 static inline unsigned int
437 i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
438 {
439         return obj->tiling_and_stride & STRIDE_MASK;
440 }
441
442 static inline unsigned int
443 i915_gem_tile_height(unsigned int tiling)
444 {
445         GEM_BUG_ON(!tiling);
446         return tiling == I915_TILING_Y ? 32 : 8;
447 }
448
449 static inline unsigned int
450 i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
451 {
452         return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
453 }
454
455 static inline unsigned int
456 i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
457 {
458         return (i915_gem_object_get_stride(obj) *
459                 i915_gem_object_get_tile_height(obj));
460 }
461
462 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
463                                unsigned int tiling, unsigned int stride);
464
465 static inline struct intel_engine_cs *
466 i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
467 {
468         struct intel_engine_cs *engine = NULL;
469         struct dma_fence *fence;
470
471         rcu_read_lock();
472         fence = reservation_object_get_excl_rcu(obj->resv);
473         rcu_read_unlock();
474
475         if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
476                 engine = to_request(fence)->engine;
477         dma_fence_put(fence);
478
479         return engine;
480 }
481
482 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
483                                          unsigned int cache_level);
484 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
485
486 #endif
487