blob: e89bfdb42102709476e1d43f82cc4db4feb8cb04 [file] [log] [blame]
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001/*
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
Chris Wilson8d28ba42017-02-13 17:15:39 +000036#include "i915_selftest.h"
37
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020038struct drm_i915_gem_object_ops {
39 unsigned int flags;
40#define I915_GEM_OBJECT_HAS_STRUCT_PAGE 0x1
41#define I915_GEM_OBJECT_IS_SHRINKABLE 0x2
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
Chris Wilson7c55e2c2017-03-07 12:03:38 +000059 int (*pwrite)(struct drm_i915_gem_object *,
60 const struct drm_i915_gem_pwrite *);
61
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020062 int (*dmabuf_export)(struct drm_i915_gem_object *);
63 void (*release)(struct drm_i915_gem_object *);
64};
65
66struct drm_i915_gem_object {
67 struct drm_gem_object base;
68
69 const struct drm_i915_gem_object_ops *ops;
70
71 /** List of VMAs backed by this object */
72 struct list_head vma_list;
73 struct rb_root vma_tree;
74
75 /** Stolen memory for this object, instead of being backed by shmem. */
76 struct drm_mm_node *stolen;
77 struct list_head global_link;
78 union {
79 struct rcu_head rcu;
80 struct llist_node freed;
81 };
82
83 /**
84 * Whether the object is currently in the GGTT mmap.
85 */
86 struct list_head userfault_link;
87
88 /** Used in execbuf to temporarily hold a ref */
89 struct list_head obj_exec_link;
90
91 struct list_head batch_pool_link;
Chris Wilson8d28ba42017-02-13 17:15:39 +000092 I915_SELFTEST_DECLARE(struct list_head st_link);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020093
94 unsigned long flags;
95
96 /**
97 * Have we taken a reference for the object for incomplete GPU
98 * activity?
99 */
100#define I915_BO_ACTIVE_REF 0
101
102 /*
103 * Is the object to be mapped as read-only to the GPU
104 * Only honoured if hardware has relevant pte bit
105 */
106 unsigned long gt_ro:1;
107 unsigned int cache_level:3;
108 unsigned int cache_dirty:1;
109
110 atomic_t frontbuffer_bits;
111 unsigned int frontbuffer_ggtt_origin; /* write once */
Chris Wilson5b8c8ae2016-11-16 19:07:04 +0000112 struct i915_gem_active frontbuffer_write;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200113
114 /** Current tiling stride for the object, if it's tiled. */
115 unsigned int tiling_and_stride;
116#define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
117#define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
118#define STRIDE_MASK (~TILING_MASK)
119
120 /** Count of VMA actually bound by this object */
121 unsigned int bind_count;
122 unsigned int active_count;
123 unsigned int pin_display;
124
125 struct {
126 struct mutex lock; /* protects the pages and their use */
127 atomic_t pages_pin_count;
128
129 struct sg_table *pages;
130 void *mapping;
131
132 struct i915_gem_object_page_iter {
133 struct scatterlist *sg_pos;
134 unsigned int sg_idx; /* in pages, but 32bit eek! */
135
136 struct radix_tree_root radix;
137 struct mutex lock; /* protects this cache */
138 } get_page;
139
140 /**
141 * Advice: are the backing pages purgeable?
142 */
143 unsigned int madv:2;
144
145 /**
146 * This is set if the object has been written to since the
147 * pages were last acquired.
148 */
149 bool dirty:1;
150
151 /**
152 * This is set if the object has been pinned due to unknown
153 * swizzling.
154 */
155 bool quirked:1;
156 } mm;
157
158 /** Breadcrumb of last rendering to the buffer.
159 * There can only be one writer, but we allow for multiple readers.
160 * If there is a writer that necessarily implies that all other
161 * read requests are complete - but we may only be lazily clearing
162 * the read requests. A read request is naturally the most recent
163 * request on a ring, so we may have two different write and read
164 * requests on one ring where the write request is older than the
165 * read request. This allows for the CPU to read from an active
166 * buffer by only waiting for the write to complete.
167 */
168 struct reservation_object *resv;
169
170 /** References from framebuffers, locks out tiling changes. */
Chris Wilsondd689282017-03-01 15:41:28 +0000171 unsigned int framebuffer_references;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200172
173 /** Record of address bit 17 of each page at last unbind. */
174 unsigned long *bit_17;
175
Chris Wilson44653982017-02-13 17:15:20 +0000176 union {
177 struct i915_gem_userptr {
178 uintptr_t ptr;
179 unsigned read_only :1;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200180
Chris Wilson44653982017-02-13 17:15:20 +0000181 struct i915_mm_struct *mm;
182 struct i915_mmu_object *mmu_object;
183 struct work_struct *work;
184 } userptr;
185
186 unsigned long scratch;
187 };
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200188
189 /** for phys allocated objects */
190 struct drm_dma_handle *phys_handle;
191
192 struct reservation_object __builtin_resv;
193};
194
195static inline struct drm_i915_gem_object *
196to_intel_bo(struct drm_gem_object *gem)
197{
198 /* Assert that to_intel_bo(NULL) == NULL */
199 BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
200
201 return container_of(gem, struct drm_i915_gem_object, base);
202}
203
204/**
205 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
206 * @filp: DRM file private date
207 * @handle: userspace handle
208 *
209 * Returns:
210 *
211 * A pointer to the object named by the handle if such exists on @filp, NULL
212 * otherwise. This object is only valid whilst under the RCU read lock, and
213 * note carefully the object may be in the process of being destroyed.
214 */
215static inline struct drm_i915_gem_object *
216i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
217{
218#ifdef CONFIG_LOCKDEP
219 WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
220#endif
221 return idr_find(&file->object_idr, handle);
222}
223
224static inline struct drm_i915_gem_object *
225i915_gem_object_lookup(struct drm_file *file, u32 handle)
226{
227 struct drm_i915_gem_object *obj;
228
229 rcu_read_lock();
230 obj = i915_gem_object_lookup_rcu(file, handle);
231 if (obj && !kref_get_unless_zero(&obj->base.refcount))
232 obj = NULL;
233 rcu_read_unlock();
234
235 return obj;
236}
237
238__deprecated
239extern struct drm_gem_object *
240drm_gem_object_lookup(struct drm_file *file, u32 handle);
241
242__attribute__((nonnull))
243static inline struct drm_i915_gem_object *
244i915_gem_object_get(struct drm_i915_gem_object *obj)
245{
246 drm_gem_object_reference(&obj->base);
247 return obj;
248}
249
250__deprecated
251extern void drm_gem_object_reference(struct drm_gem_object *);
252
253__attribute__((nonnull))
254static inline void
255i915_gem_object_put(struct drm_i915_gem_object *obj)
256{
257 __drm_gem_object_unreference(&obj->base);
258}
259
260__deprecated
261extern void drm_gem_object_unreference(struct drm_gem_object *);
262
263__deprecated
264extern void drm_gem_object_unreference_unlocked(struct drm_gem_object *);
265
Chris Wilsondd689282017-03-01 15:41:28 +0000266static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj)
267{
268 reservation_object_lock(obj->resv, NULL);
269}
270
271static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
272{
273 reservation_object_unlock(obj->resv);
274}
275
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200276static inline bool
277i915_gem_object_is_dead(const struct drm_i915_gem_object *obj)
278{
279 return atomic_read(&obj->base.refcount.refcount) == 0;
280}
281
282static inline bool
283i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
284{
285 return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
286}
287
288static inline bool
289i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
290{
291 return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
292}
293
294static inline bool
295i915_gem_object_is_active(const struct drm_i915_gem_object *obj)
296{
297 return obj->active_count;
298}
299
300static inline bool
301i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj)
302{
303 return test_bit(I915_BO_ACTIVE_REF, &obj->flags);
304}
305
306static inline void
307i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj)
308{
309 lockdep_assert_held(&obj->base.dev->struct_mutex);
310 __set_bit(I915_BO_ACTIVE_REF, &obj->flags);
311}
312
313static inline void
314i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj)
315{
316 lockdep_assert_held(&obj->base.dev->struct_mutex);
317 __clear_bit(I915_BO_ACTIVE_REF, &obj->flags);
318}
319
320void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj);
321
Chris Wilsondd689282017-03-01 15:41:28 +0000322static inline bool
323i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
324{
325 return READ_ONCE(obj->framebuffer_references);
326}
327
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200328static inline unsigned int
329i915_gem_object_get_tiling(struct drm_i915_gem_object *obj)
330{
331 return obj->tiling_and_stride & TILING_MASK;
332}
333
334static inline bool
335i915_gem_object_is_tiled(struct drm_i915_gem_object *obj)
336{
337 return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
338}
339
340static inline unsigned int
341i915_gem_object_get_stride(struct drm_i915_gem_object *obj)
342{
343 return obj->tiling_and_stride & STRIDE_MASK;
344}
345
Chris Wilson6649a0b2017-01-09 16:16:08 +0000346static inline unsigned int
347i915_gem_tile_height(unsigned int tiling)
348{
349 GEM_BUG_ON(!tiling);
350 return tiling == I915_TILING_Y ? 32 : 8;
351}
352
353static inline unsigned int
354i915_gem_object_get_tile_height(struct drm_i915_gem_object *obj)
355{
356 return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
357}
358
359static inline unsigned int
360i915_gem_object_get_tile_row_size(struct drm_i915_gem_object *obj)
361{
362 return (i915_gem_object_get_stride(obj) *
363 i915_gem_object_get_tile_height(obj));
364}
365
Chris Wilson957870f2017-01-10 12:10:45 +0000366int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
367 unsigned int tiling, unsigned int stride);
368
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200369static inline struct intel_engine_cs *
370i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
371{
372 struct intel_engine_cs *engine = NULL;
373 struct dma_fence *fence;
374
375 rcu_read_lock();
376 fence = reservation_object_get_excl_rcu(obj->resv);
377 rcu_read_unlock();
378
379 if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
380 engine = to_request(fence)->engine;
381 dma_fence_put(fence);
382
383 return engine;
384}
385
Chris Wilson5a97bcc2017-02-22 11:40:46 +0000386void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
387
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200388#endif
389