2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #ifndef __I915_GEM_OBJECT_H__
26 #define __I915_GEM_OBJECT_H__
28 #include <linux/reservation.h>
30 #include <drm/drm_vma_manager.h>
31 #include <drm/drm_gem.h>
34 #include <drm/i915_drm.h>
36 #include "i915_gem_request.h"
37 #include "i915_selftest.h"
39 struct drm_i915_gem_object
;
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).
47 struct i915_lut_handle
{
48 struct list_head obj_link
;
49 struct list_head ctx_link
;
50 struct i915_gem_context
*ctx
;
54 struct drm_i915_gem_object_ops
{
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)
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).
73 int (*get_pages
)(struct drm_i915_gem_object
*);
74 void (*put_pages
)(struct drm_i915_gem_object
*, struct sg_table
*);
76 int (*pwrite
)(struct drm_i915_gem_object
*,
77 const struct drm_i915_gem_pwrite
*);
79 int (*dmabuf_export
)(struct drm_i915_gem_object
*);
80 void (*release
)(struct drm_i915_gem_object
*);
83 struct drm_i915_gem_object
{
84 struct drm_gem_object base
;
86 const struct drm_i915_gem_object_ops
*ops
;
89 * @vma_list: List of VMAs backed by this object
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
97 struct list_head vma_list
;
99 * @vma_tree: Ordered tree of VMAs backed by this object
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.
105 struct rb_root vma_tree
;
108 * @lut_list: List of vma lookup entries in use for this object.
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.
114 struct list_head lut_list
;
116 /** Stolen memory for this object, instead of being backed by shmem. */
117 struct drm_mm_node
*stolen
;
120 struct llist_node freed
;
124 * Whether the object is currently in the GGTT mmap.
126 unsigned int userfault_count
;
127 struct list_head userfault_link
;
129 struct list_head batch_pool_link
;
130 I915_SELFTEST_DECLARE(struct list_head st_link
);
135 * Have we taken a reference for the object for incomplete GPU
138 #define I915_BO_ACTIVE_REF 0
141 * Is the object to be mapped as read-only to the GPU
142 * Only honoured if hardware has relevant pte bit
144 unsigned long gt_ro
:1;
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;
151 atomic_t frontbuffer_bits
;
152 unsigned int frontbuffer_ggtt_origin
; /* write once */
153 struct i915_gem_active frontbuffer_write
;
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)
161 /** Count of VMA actually bound by this object */
162 unsigned int bind_count
;
163 unsigned int active_count
;
164 /** Count of how many global VMA are currently pinned for use by HW */
165 unsigned int pin_global
;
168 struct mutex lock
; /* protects the pages and their use */
169 atomic_t pages_pin_count
;
171 struct sg_table
*pages
;
174 /* TODO: whack some of this into the error state */
175 struct i915_page_sizes
{
177 * The sg mask of the pages sg_table. i.e the mask of
178 * of the lengths for each sg entry.
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.
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.
201 I915_SELFTEST_DECLARE(unsigned int page_mask
);
203 struct i915_gem_object_page_iter
{
204 struct scatterlist
*sg_pos
;
205 unsigned int sg_idx
; /* in pages, but 32bit eek! */
207 struct radix_tree_root radix
;
208 struct mutex lock
; /* protects this cache */
212 * Element within i915->mm.unbound_list or i915->mm.bound_list,
213 * locked by i915->mm.obj_lock.
215 struct list_head link
;
218 * Advice: are the backing pages purgeable?
223 * This is set if the object has been written to since the
224 * pages were last acquired.
229 * This is set if the object has been pinned due to unknown
235 /** Breadcrumb of last rendering to the buffer.
236 * There can only be one writer, but we allow for multiple readers.
237 * If there is a writer that necessarily implies that all other
238 * read requests are complete - but we may only be lazily clearing
239 * the read requests. A read request is naturally the most recent
240 * request on a ring, so we may have two different write and read
241 * requests on one ring where the write request is older than the
242 * read request. This allows for the CPU to read from an active
243 * buffer by only waiting for the write to complete.
245 struct reservation_object
*resv
;
247 /** References from framebuffers, locks out tiling changes. */
248 unsigned int framebuffer_references
;
250 /** Record of address bit 17 of each page at last unbind. */
251 unsigned long *bit_17
;
254 struct i915_gem_userptr
{
256 unsigned read_only
:1;
258 struct i915_mm_struct
*mm
;
259 struct i915_mmu_object
*mmu_object
;
260 struct work_struct
*work
;
263 unsigned long scratch
;
268 /** for phys allocated objects */
269 struct drm_dma_handle
*phys_handle
;
271 struct reservation_object __builtin_resv
;
274 static inline struct drm_i915_gem_object
*
275 to_intel_bo(struct drm_gem_object
*gem
)
277 /* Assert that to_intel_bo(NULL) == NULL */
278 BUILD_BUG_ON(offsetof(struct drm_i915_gem_object
, base
));
280 return container_of(gem
, struct drm_i915_gem_object
, base
);
284 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
285 * @filp: DRM file private date
286 * @handle: userspace handle
290 * A pointer to the object named by the handle if such exists on @filp, NULL
291 * otherwise. This object is only valid whilst under the RCU read lock, and
292 * note carefully the object may be in the process of being destroyed.
294 static inline struct drm_i915_gem_object
*
295 i915_gem_object_lookup_rcu(struct drm_file
*file
, u32 handle
)
297 #ifdef CONFIG_LOCKDEP
298 WARN_ON(debug_locks
&& !lock_is_held(&rcu_lock_map
));
300 return idr_find(&file
->object_idr
, handle
);
303 static inline struct drm_i915_gem_object
*
304 i915_gem_object_lookup(struct drm_file
*file
, u32 handle
)
306 struct drm_i915_gem_object
*obj
;
309 obj
= i915_gem_object_lookup_rcu(file
, handle
);
310 if (obj
&& !kref_get_unless_zero(&obj
->base
.refcount
))
318 extern struct drm_gem_object
*
319 drm_gem_object_lookup(struct drm_file
*file
, u32 handle
);
321 __attribute__((nonnull
))
322 static inline struct drm_i915_gem_object
*
323 i915_gem_object_get(struct drm_i915_gem_object
*obj
)
325 drm_gem_object_reference(&obj
->base
);
330 extern void drm_gem_object_reference(struct drm_gem_object
*);
332 __attribute__((nonnull
))
334 i915_gem_object_put(struct drm_i915_gem_object
*obj
)
336 __drm_gem_object_unreference(&obj
->base
);
340 extern void drm_gem_object_unreference(struct drm_gem_object
*);
343 extern void drm_gem_object_unreference_unlocked(struct drm_gem_object
*);
345 static inline void i915_gem_object_lock(struct drm_i915_gem_object
*obj
)
347 reservation_object_lock(obj
->resv
, NULL
);
350 static inline void i915_gem_object_unlock(struct drm_i915_gem_object
*obj
)
352 reservation_object_unlock(obj
->resv
);
356 i915_gem_object_has_struct_page(const struct drm_i915_gem_object
*obj
)
358 return obj
->ops
->flags
& I915_GEM_OBJECT_HAS_STRUCT_PAGE
;
362 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object
*obj
)
364 return obj
->ops
->flags
& I915_GEM_OBJECT_IS_SHRINKABLE
;
368 i915_gem_object_is_proxy(const struct drm_i915_gem_object
*obj
)
370 return obj
->ops
->flags
& I915_GEM_OBJECT_IS_PROXY
;
374 i915_gem_object_is_active(const struct drm_i915_gem_object
*obj
)
376 return obj
->active_count
;
380 i915_gem_object_has_active_reference(const struct drm_i915_gem_object
*obj
)
382 return test_bit(I915_BO_ACTIVE_REF
, &obj
->flags
);
386 i915_gem_object_set_active_reference(struct drm_i915_gem_object
*obj
)
388 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
389 __set_bit(I915_BO_ACTIVE_REF
, &obj
->flags
);
393 i915_gem_object_clear_active_reference(struct drm_i915_gem_object
*obj
)
395 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
396 __clear_bit(I915_BO_ACTIVE_REF
, &obj
->flags
);
399 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object
*obj
);
402 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object
*obj
)
404 return READ_ONCE(obj
->framebuffer_references
);
407 static inline unsigned int
408 i915_gem_object_get_tiling(struct drm_i915_gem_object
*obj
)
410 return obj
->tiling_and_stride
& TILING_MASK
;
414 i915_gem_object_is_tiled(struct drm_i915_gem_object
*obj
)
416 return i915_gem_object_get_tiling(obj
) != I915_TILING_NONE
;
419 static inline unsigned int
420 i915_gem_object_get_stride(struct drm_i915_gem_object
*obj
)
422 return obj
->tiling_and_stride
& STRIDE_MASK
;
425 static inline unsigned int
426 i915_gem_tile_height(unsigned int tiling
)
429 return tiling
== I915_TILING_Y
? 32 : 8;
432 static inline unsigned int
433 i915_gem_object_get_tile_height(struct drm_i915_gem_object
*obj
)
435 return i915_gem_tile_height(i915_gem_object_get_tiling(obj
));
438 static inline unsigned int
439 i915_gem_object_get_tile_row_size(struct drm_i915_gem_object
*obj
)
441 return (i915_gem_object_get_stride(obj
) *
442 i915_gem_object_get_tile_height(obj
));
445 int i915_gem_object_set_tiling(struct drm_i915_gem_object
*obj
,
446 unsigned int tiling
, unsigned int stride
);
448 static inline struct intel_engine_cs
*
449 i915_gem_object_last_write_engine(struct drm_i915_gem_object
*obj
)
451 struct intel_engine_cs
*engine
= NULL
;
452 struct dma_fence
*fence
;
455 fence
= reservation_object_get_excl_rcu(obj
->resv
);
458 if (fence
&& dma_fence_is_i915(fence
) && !dma_fence_is_signaled(fence
))
459 engine
= to_request(fence
)->engine
;
460 dma_fence_put(fence
);
465 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object
*obj
,
466 unsigned int cache_level
);
467 void i915_gem_object_flush_if_display(struct drm_i915_gem_object
*obj
);