2 * SPDX-License-Identifier: MIT
4 * Copyright © 2016 Intel Corporation
7 #ifndef __I915_GEM_OBJECT_H__
8 #define __I915_GEM_OBJECT_H__
10 #include <drm/drm_gem.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_device.h>
14 #include <drm/i915_drm.h>
16 #include "i915_gem_object_types.h"
18 #include "i915_gem_gtt.h"
20 void i915_gem_init__objects(struct drm_i915_private *i915);
22 struct drm_i915_gem_object *i915_gem_object_alloc(void);
23 void i915_gem_object_free(struct drm_i915_gem_object *obj);
25 void i915_gem_object_init(struct drm_i915_gem_object *obj,
26 const struct drm_i915_gem_object_ops *ops);
27 struct drm_i915_gem_object *
28 i915_gem_object_create_shmem(struct drm_i915_private *i915, u64 size);
29 struct drm_i915_gem_object *
30 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
31 const void *data, size_t size);
33 extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops;
34 void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
35 struct sg_table *pages,
38 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align);
40 void i915_gem_close_object(struct drm_gem_object *gem, struct drm_file *file);
41 void i915_gem_free_object(struct drm_gem_object *obj);
43 void i915_gem_flush_free_objects(struct drm_i915_private *i915);
46 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj);
47 void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
50 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
51 * @filp: DRM file private date
52 * @handle: userspace handle
56 * A pointer to the object named by the handle if such exists on @filp, NULL
57 * otherwise. This object is only valid whilst under the RCU read lock, and
58 * note carefully the object may be in the process of being destroyed.
60 static inline struct drm_i915_gem_object *
61 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
64 WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
66 return idr_find(&file->object_idr, handle);
69 static inline struct drm_i915_gem_object *
70 i915_gem_object_lookup(struct drm_file *file, u32 handle)
72 struct drm_i915_gem_object *obj;
75 obj = i915_gem_object_lookup_rcu(file, handle);
76 if (obj && !kref_get_unless_zero(&obj->base.refcount))
84 struct drm_gem_object *
85 drm_gem_object_lookup(struct drm_file *file, u32 handle);
87 __attribute__((nonnull))
88 static inline struct drm_i915_gem_object *
89 i915_gem_object_get(struct drm_i915_gem_object *obj)
91 drm_gem_object_get(&obj->base);
95 __attribute__((nonnull))
97 i915_gem_object_put(struct drm_i915_gem_object *obj)
99 __drm_gem_object_put(&obj->base);
102 #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv)
104 static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj)
106 dma_resv_lock(obj->base.resv, NULL);
110 i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj)
112 return dma_resv_lock_interruptible(obj->base.resv, NULL);
115 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
117 dma_resv_unlock(obj->base.resv);
121 i915_gem_object_lock_fence(struct drm_i915_gem_object *obj);
122 void i915_gem_object_unlock_fence(struct drm_i915_gem_object *obj,
123 struct dma_fence *fence);
126 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
128 obj->base.vma_node.readonly = true;
132 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
134 return obj->base.vma_node.readonly;
138 i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
140 return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE;
144 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
146 return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE;
150 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
152 return obj->ops->flags & I915_GEM_OBJECT_IS_PROXY;
156 i915_gem_object_needs_async_cancel(const struct drm_i915_gem_object *obj)
158 return obj->ops->flags & I915_GEM_OBJECT_ASYNC_CANCEL;
162 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
164 return READ_ONCE(obj->frontbuffer);
167 static inline unsigned int
168 i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj)
170 return obj->tiling_and_stride & TILING_MASK;
174 i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj)
176 return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
179 static inline unsigned int
180 i915_gem_object_get_stride(const struct drm_i915_gem_object *obj)
182 return obj->tiling_and_stride & STRIDE_MASK;
185 static inline unsigned int
186 i915_gem_tile_height(unsigned int tiling)
189 return tiling == I915_TILING_Y ? 32 : 8;
192 static inline unsigned int
193 i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj)
195 return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
198 static inline unsigned int
199 i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj)
201 return (i915_gem_object_get_stride(obj) *
202 i915_gem_object_get_tile_height(obj));
205 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
206 unsigned int tiling, unsigned int stride);
209 i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
210 unsigned int n, unsigned int *offset);
213 i915_gem_object_get_page(struct drm_i915_gem_object *obj,
217 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
221 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
226 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
229 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
230 struct sg_table *pages,
231 unsigned int sg_page_sizes);
233 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
234 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
236 static inline int __must_check
237 i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
239 might_lock(&obj->mm.lock);
241 if (atomic_inc_not_zero(&obj->mm.pages_pin_count))
244 return __i915_gem_object_get_pages(obj);
248 i915_gem_object_has_pages(struct drm_i915_gem_object *obj)
250 return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages));
254 __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
256 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
258 atomic_inc(&obj->mm.pages_pin_count);
262 i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj)
264 return atomic_read(&obj->mm.pages_pin_count);
268 __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
270 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
271 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
273 atomic_dec(&obj->mm.pages_pin_count);
277 i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
279 __i915_gem_object_unpin_pages(obj);
282 enum i915_mm_subclass { /* lockdep subclass for obj->mm.lock/struct_mutex */
284 I915_MM_SHRINKER /* called "recursively" from direct-reclaim-esque */
287 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj,
288 enum i915_mm_subclass subclass);
289 void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
290 void i915_gem_object_writeback(struct drm_i915_gem_object *obj);
295 #define I915_MAP_OVERRIDE BIT(31)
296 I915_MAP_FORCE_WB = I915_MAP_WB | I915_MAP_OVERRIDE,
297 I915_MAP_FORCE_WC = I915_MAP_WC | I915_MAP_OVERRIDE,
301 * i915_gem_object_pin_map - return a contiguous mapping of the entire object
302 * @obj: the object to map into kernel address space
303 * @type: the type of mapping, used to select pgprot_t
305 * Calls i915_gem_object_pin_pages() to prevent reaping of the object's
306 * pages and then returns a contiguous mapping of the backing storage into
307 * the kernel address space. Based on the @type of mapping, the PTE will be
308 * set to either WriteBack or WriteCombine (via pgprot_t).
310 * The caller is responsible for calling i915_gem_object_unpin_map() when the
311 * mapping is no longer required.
313 * Returns the pointer through which to access the mapped object, or an
314 * ERR_PTR() on error.
316 void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
317 enum i915_map_type type);
319 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
320 unsigned long offset,
322 static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj)
324 __i915_gem_object_flush_map(obj, 0, obj->base.size);
328 * i915_gem_object_unpin_map - releases an earlier mapping
329 * @obj: the object to unmap
331 * After pinning the object and mapping its pages, once you are finished
332 * with your access, call i915_gem_object_unpin_map() to release the pin
333 * upon the mapping. Once the pin count reaches zero, that mapping may be
336 static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
338 i915_gem_object_unpin_pages(obj);
341 void __i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);
342 void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);
345 i915_gem_object_flush_write_domain(struct drm_i915_gem_object *obj,
346 unsigned int flush_domains);
348 int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,
349 unsigned int *needs_clflush);
350 int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,
351 unsigned int *needs_clflush);
352 #define CLFLUSH_BEFORE BIT(0)
353 #define CLFLUSH_AFTER BIT(1)
354 #define CLFLUSH_FLAGS (CLFLUSH_BEFORE | CLFLUSH_AFTER)
357 i915_gem_object_finish_access(struct drm_i915_gem_object *obj)
359 i915_gem_object_unpin_pages(obj);
360 i915_gem_object_unlock(obj);
363 static inline struct intel_engine_cs *
364 i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
366 struct intel_engine_cs *engine = NULL;
367 struct dma_fence *fence;
370 fence = dma_resv_get_excl_rcu(obj->base.resv);
373 if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
374 engine = to_request(fence)->engine;
375 dma_fence_put(fence);
380 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
381 unsigned int cache_level);
382 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
385 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write);
387 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write);
389 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write);
390 struct i915_vma * __must_check
391 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
393 const struct i915_ggtt_view *view,
395 void i915_gem_object_unpin_from_display_plane(struct i915_vma *vma);
397 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj);
398 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
399 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
401 static inline bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
403 if (obj->cache_dirty)
406 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
409 return obj->pin_global; /* currently in use by HW, keep flushed */
412 static inline void __start_cpu_write(struct drm_i915_gem_object *obj)
414 obj->read_domains = I915_GEM_DOMAIN_CPU;
415 obj->write_domain = I915_GEM_DOMAIN_CPU;
416 if (cpu_write_needs_clflush(obj))
417 obj->cache_dirty = true;
420 int i915_gem_object_wait(struct drm_i915_gem_object *obj,
423 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
425 const struct i915_sched_attr *attr);
426 #define I915_PRIORITY_DISPLAY I915_USER_PRIORITY(I915_PRIORITY_MAX)