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