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 struct drm_i915_gem_object_ops
{
38 #define I915_GEM_OBJECT_HAS_STRUCT_PAGE 0x1
39 #define I915_GEM_OBJECT_IS_SHRINKABLE 0x2
41 /* Interface between the GEM object and its backing storage.
42 * get_pages() is called once prior to the use of the associated set
43 * of pages before to binding them into the GTT, and put_pages() is
44 * called after we no longer need them. As we expect there to be
45 * associated cost with migrating pages between the backing storage
46 * and making them available for the GPU (e.g. clflush), we may hold
47 * onto the pages after they are no longer referenced by the GPU
48 * in case they may be used again shortly (for example migrating the
49 * pages to a different memory domain within the GTT). put_pages()
50 * will therefore most likely be called when the object itself is
51 * being released or under memory pressure (where we attempt to
52 * reap pages for the shrinker).
54 struct sg_table
*(*get_pages
)(struct drm_i915_gem_object
*);
55 void (*put_pages
)(struct drm_i915_gem_object
*, struct sg_table
*);
57 int (*pwrite
)(struct drm_i915_gem_object
*,
58 const struct drm_i915_gem_pwrite
*);
60 int (*dmabuf_export
)(struct drm_i915_gem_object
*);
61 void (*release
)(struct drm_i915_gem_object
*);
64 struct drm_i915_gem_object
{
65 struct drm_gem_object base
;
67 const struct drm_i915_gem_object_ops
*ops
;
69 /** List of VMAs backed by this object */
70 struct list_head vma_list
;
71 struct rb_root vma_tree
;
73 /** Stolen memory for this object, instead of being backed by shmem. */
74 struct drm_mm_node
*stolen
;
75 struct list_head global_link
;
78 struct llist_node freed
;
82 * Whether the object is currently in the GGTT mmap.
84 struct list_head userfault_link
;
86 /** Used in execbuf to temporarily hold a ref */
87 struct list_head obj_exec_link
;
89 struct list_head batch_pool_link
;
94 * Have we taken a reference for the object for incomplete GPU
97 #define I915_BO_ACTIVE_REF 0
100 * Is the object to be mapped as read-only to the GPU
101 * Only honoured if hardware has relevant pte bit
103 unsigned long gt_ro
:1;
104 unsigned int cache_level
:3;
105 unsigned int cache_dirty
:1;
107 atomic_t frontbuffer_bits
;
108 unsigned int frontbuffer_ggtt_origin
; /* write once */
109 struct i915_gem_active frontbuffer_write
;
111 /** Current tiling stride for the object, if it's tiled. */
112 unsigned int tiling_and_stride
;
113 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
114 #define TILING_MASK (FENCE_MINIMUM_STRIDE-1)
115 #define STRIDE_MASK (~TILING_MASK)
117 /** Count of VMA actually bound by this object */
118 unsigned int bind_count
;
119 unsigned int active_count
;
120 unsigned int pin_display
;
123 struct mutex lock
; /* protects the pages and their use */
124 atomic_t pages_pin_count
;
126 struct sg_table
*pages
;
129 struct i915_gem_object_page_iter
{
130 struct scatterlist
*sg_pos
;
131 unsigned int sg_idx
; /* in pages, but 32bit eek! */
133 struct radix_tree_root radix
;
134 struct mutex lock
; /* protects this cache */
138 * Advice: are the backing pages purgeable?
143 * This is set if the object has been written to since the
144 * pages were last acquired.
149 * This is set if the object has been pinned due to unknown
155 /** Breadcrumb of last rendering to the buffer.
156 * There can only be one writer, but we allow for multiple readers.
157 * If there is a writer that necessarily implies that all other
158 * read requests are complete - but we may only be lazily clearing
159 * the read requests. A read request is naturally the most recent
160 * request on a ring, so we may have two different write and read
161 * requests on one ring where the write request is older than the
162 * read request. This allows for the CPU to read from an active
163 * buffer by only waiting for the write to complete.
165 struct reservation_object
*resv
;
167 /** References from framebuffers, locks out tiling changes. */
168 unsigned long framebuffer_references
;
170 /** Record of address bit 17 of each page at last unbind. */
171 unsigned long *bit_17
;
173 struct i915_gem_userptr
{
175 unsigned read_only
:1;
177 struct i915_mm_struct
*mm
;
178 struct i915_mmu_object
*mmu_object
;
179 struct work_struct
*work
;
182 /** for phys allocated objects */
183 struct drm_dma_handle
*phys_handle
;
185 struct reservation_object __builtin_resv
;
188 static inline struct drm_i915_gem_object
*
189 to_intel_bo(struct drm_gem_object
*gem
)
191 /* Assert that to_intel_bo(NULL) == NULL */
192 BUILD_BUG_ON(offsetof(struct drm_i915_gem_object
, base
));
194 return container_of(gem
, struct drm_i915_gem_object
, base
);
198 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
199 * @filp: DRM file private date
200 * @handle: userspace handle
204 * A pointer to the object named by the handle if such exists on @filp, NULL
205 * otherwise. This object is only valid whilst under the RCU read lock, and
206 * note carefully the object may be in the process of being destroyed.
208 static inline struct drm_i915_gem_object
*
209 i915_gem_object_lookup_rcu(struct drm_file
*file
, u32 handle
)
211 #ifdef CONFIG_LOCKDEP
212 WARN_ON(debug_locks
&& !lock_is_held(&rcu_lock_map
));
214 return idr_find(&file
->object_idr
, handle
);
217 static inline struct drm_i915_gem_object
*
218 i915_gem_object_lookup(struct drm_file
*file
, u32 handle
)
220 struct drm_i915_gem_object
*obj
;
223 obj
= i915_gem_object_lookup_rcu(file
, handle
);
224 if (obj
&& !kref_get_unless_zero(&obj
->base
.refcount
))
232 extern struct drm_gem_object
*
233 drm_gem_object_lookup(struct drm_file
*file
, u32 handle
);
235 __attribute__((nonnull
))
236 static inline struct drm_i915_gem_object
*
237 i915_gem_object_get(struct drm_i915_gem_object
*obj
)
239 drm_gem_object_reference(&obj
->base
);
244 extern void drm_gem_object_reference(struct drm_gem_object
*);
246 __attribute__((nonnull
))
248 i915_gem_object_put(struct drm_i915_gem_object
*obj
)
250 __drm_gem_object_unreference(&obj
->base
);
254 extern void drm_gem_object_unreference(struct drm_gem_object
*);
257 extern void drm_gem_object_unreference_unlocked(struct drm_gem_object
*);
260 i915_gem_object_is_dead(const struct drm_i915_gem_object
*obj
)
262 return kref_read(&obj
->base
.refcount
) == 0;
266 i915_gem_object_has_struct_page(const struct drm_i915_gem_object
*obj
)
268 return obj
->ops
->flags
& I915_GEM_OBJECT_HAS_STRUCT_PAGE
;
272 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object
*obj
)
274 return obj
->ops
->flags
& I915_GEM_OBJECT_IS_SHRINKABLE
;
278 i915_gem_object_is_active(const struct drm_i915_gem_object
*obj
)
280 return obj
->active_count
;
284 i915_gem_object_has_active_reference(const struct drm_i915_gem_object
*obj
)
286 return test_bit(I915_BO_ACTIVE_REF
, &obj
->flags
);
290 i915_gem_object_set_active_reference(struct drm_i915_gem_object
*obj
)
292 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
293 __set_bit(I915_BO_ACTIVE_REF
, &obj
->flags
);
297 i915_gem_object_clear_active_reference(struct drm_i915_gem_object
*obj
)
299 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
300 __clear_bit(I915_BO_ACTIVE_REF
, &obj
->flags
);
303 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object
*obj
);
305 static inline unsigned int
306 i915_gem_object_get_tiling(struct drm_i915_gem_object
*obj
)
308 return obj
->tiling_and_stride
& TILING_MASK
;
312 i915_gem_object_is_tiled(struct drm_i915_gem_object
*obj
)
314 return i915_gem_object_get_tiling(obj
) != I915_TILING_NONE
;
317 static inline unsigned int
318 i915_gem_object_get_stride(struct drm_i915_gem_object
*obj
)
320 return obj
->tiling_and_stride
& STRIDE_MASK
;
323 static inline unsigned int
324 i915_gem_tile_height(unsigned int tiling
)
327 return tiling
== I915_TILING_Y
? 32 : 8;
330 static inline unsigned int
331 i915_gem_object_get_tile_height(struct drm_i915_gem_object
*obj
)
333 return i915_gem_tile_height(i915_gem_object_get_tiling(obj
));
336 static inline unsigned int
337 i915_gem_object_get_tile_row_size(struct drm_i915_gem_object
*obj
)
339 return (i915_gem_object_get_stride(obj
) *
340 i915_gem_object_get_tile_height(obj
));
343 int i915_gem_object_set_tiling(struct drm_i915_gem_object
*obj
,
344 unsigned int tiling
, unsigned int stride
);
346 static inline struct intel_engine_cs
*
347 i915_gem_object_last_write_engine(struct drm_i915_gem_object
*obj
)
349 struct intel_engine_cs
*engine
= NULL
;
350 struct dma_fence
*fence
;
353 fence
= reservation_object_get_excl_rcu(obj
->resv
);
356 if (fence
&& dma_fence_is_i915(fence
) && !dma_fence_is_signaled(fence
))
357 engine
= to_request(fence
)->engine
;
358 dma_fence_put(fence
);