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 "display/intel_frontbuffer.h"
15 #include "i915_gem_object_types.h"
16 #include "i915_gem_gtt.h"
17 #include "i915_vma_types.h"
20 * XXX: There is a prevalence of the assumption that we fit the
21 * object's page count inside a 32bit _signed_ variable. Let's document
22 * this and catch if we ever need to fix it. In the meantime, if you do
23 * spot such a local variable, please consider fixing!
25 * Aside from our own locals (for which we have no excuse!):
26 * - sg_table embeds unsigned int for num_pages
27 * - get_user_pages*() mixed ints with longs
29 #define GEM_CHECK_SIZE_OVERFLOW(sz) \
30 GEM_WARN_ON((sz) >> PAGE_SHIFT > INT_MAX)
32 static inline bool i915_gem_object_size_2big(u64 size)
34 struct drm_i915_gem_object *obj;
36 if (GEM_CHECK_SIZE_OVERFLOW(size))
39 if (overflows_type(size, obj->base.size))
45 void i915_gem_init__objects(struct drm_i915_private *i915);
47 struct drm_i915_gem_object *i915_gem_object_alloc(void);
48 void i915_gem_object_free(struct drm_i915_gem_object *obj);
50 void i915_gem_object_init(struct drm_i915_gem_object *obj,
51 const struct drm_i915_gem_object_ops *ops,
52 struct lock_class_key *key,
53 unsigned alloc_flags);
54 struct drm_i915_gem_object *
55 i915_gem_object_create_shmem(struct drm_i915_private *i915,
56 resource_size_t size);
57 struct drm_i915_gem_object *
58 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
59 const void *data, resource_size_t size);
61 extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops;
63 void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
64 struct sg_table *pages,
67 int i915_gem_object_pwrite_phys(struct drm_i915_gem_object *obj,
68 const struct drm_i915_gem_pwrite *args);
69 int i915_gem_object_pread_phys(struct drm_i915_gem_object *obj,
70 const struct drm_i915_gem_pread *args);
72 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align);
73 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj,
74 struct sg_table *pages);
75 void i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
76 struct sg_table *pages);
78 void i915_gem_flush_free_objects(struct drm_i915_private *i915);
81 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj);
82 void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
85 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
86 * @filp: DRM file private date
87 * @handle: userspace handle
91 * A pointer to the object named by the handle if such exists on @filp, NULL
92 * otherwise. This object is only valid whilst under the RCU read lock, and
93 * note carefully the object may be in the process of being destroyed.
95 static inline struct drm_i915_gem_object *
96 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
99 WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
101 return idr_find(&file->object_idr, handle);
104 static inline struct drm_i915_gem_object *
105 i915_gem_object_get_rcu(struct drm_i915_gem_object *obj)
107 if (obj && !kref_get_unless_zero(&obj->base.refcount))
113 static inline struct drm_i915_gem_object *
114 i915_gem_object_lookup(struct drm_file *file, u32 handle)
116 struct drm_i915_gem_object *obj;
119 obj = i915_gem_object_lookup_rcu(file, handle);
120 obj = i915_gem_object_get_rcu(obj);
127 struct drm_gem_object *
128 drm_gem_object_lookup(struct drm_file *file, u32 handle);
130 __attribute__((nonnull))
131 static inline struct drm_i915_gem_object *
132 i915_gem_object_get(struct drm_i915_gem_object *obj)
134 drm_gem_object_get(&obj->base);
138 __attribute__((nonnull))
140 i915_gem_object_put(struct drm_i915_gem_object *obj)
142 __drm_gem_object_put(&obj->base);
145 #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv)
148 * If more than one potential simultaneous locker, assert held.
150 static inline void assert_object_held_shared(struct drm_i915_gem_object *obj)
153 * Note mm list lookup is protected by
154 * kref_get_unless_zero().
156 if (IS_ENABLED(CONFIG_LOCKDEP) &&
157 kref_read(&obj->base.refcount) > 0)
158 assert_object_held(obj);
161 static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj,
162 struct i915_gem_ww_ctx *ww,
168 ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL);
170 ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL);
173 list_add_tail(&obj->obj_link, &ww->obj_list);
174 if (ret == -EALREADY)
183 static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj,
184 struct i915_gem_ww_ctx *ww)
186 return __i915_gem_object_lock(obj, ww, ww && ww->intr);
189 static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj,
190 struct i915_gem_ww_ctx *ww)
192 WARN_ON(ww && !ww->intr);
193 return __i915_gem_object_lock(obj, ww, true);
196 static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj)
198 return dma_resv_trylock(obj->base.resv);
201 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
203 dma_resv_unlock(obj->base.resv);
207 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
209 obj->flags |= I915_BO_READONLY;
213 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
215 return obj->flags & I915_BO_READONLY;
219 i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj)
221 return obj->flags & I915_BO_ALLOC_CONTIGUOUS;
225 i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj)
227 return obj->flags & I915_BO_ALLOC_VOLATILE;
231 i915_gem_object_set_volatile(struct drm_i915_gem_object *obj)
233 obj->flags |= I915_BO_ALLOC_VOLATILE;
237 i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj)
239 return test_bit(I915_TILING_QUIRK_BIT, &obj->flags);
243 i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj)
245 set_bit(I915_TILING_QUIRK_BIT, &obj->flags);
249 i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj)
251 clear_bit(I915_TILING_QUIRK_BIT, &obj->flags);
255 i915_gem_object_type_has(const struct drm_i915_gem_object *obj,
258 return obj->ops->flags & flags;
262 i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj)
264 return obj->flags & I915_BO_ALLOC_STRUCT_PAGE;
268 i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj)
270 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_HAS_IOMEM);
274 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
276 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE);
280 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
282 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY);
286 i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj)
288 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP);
292 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
294 return READ_ONCE(obj->frontbuffer);
297 static inline unsigned int
298 i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj)
300 return obj->tiling_and_stride & TILING_MASK;
304 i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj)
306 return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
309 static inline unsigned int
310 i915_gem_object_get_stride(const struct drm_i915_gem_object *obj)
312 return obj->tiling_and_stride & STRIDE_MASK;
315 static inline unsigned int
316 i915_gem_tile_height(unsigned int tiling)
319 return tiling == I915_TILING_Y ? 32 : 8;
322 static inline unsigned int
323 i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj)
325 return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
328 static inline unsigned int
329 i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj)
331 return (i915_gem_object_get_stride(obj) *
332 i915_gem_object_get_tile_height(obj));
335 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
336 unsigned int tiling, unsigned int stride);
339 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
340 struct i915_gem_object_page_iter *iter,
342 unsigned int *offset, bool allow_alloc);
344 static inline struct scatterlist *
345 i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
347 unsigned int *offset, bool allow_alloc)
349 return __i915_gem_object_get_sg(obj, &obj->mm.get_page, n, offset, allow_alloc);
352 static inline struct scatterlist *
353 i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj,
355 unsigned int *offset, bool allow_alloc)
357 return __i915_gem_object_get_sg(obj, &obj->mm.get_dma_page, n, offset, allow_alloc);
361 i915_gem_object_get_page(struct drm_i915_gem_object *obj,
365 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
369 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
374 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
377 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
378 struct sg_table *pages,
379 unsigned int sg_page_sizes);
381 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
382 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
384 static inline int __must_check
385 i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
387 assert_object_held(obj);
389 if (atomic_inc_not_zero(&obj->mm.pages_pin_count))
392 return __i915_gem_object_get_pages(obj);
395 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj);
398 i915_gem_object_has_pages(struct drm_i915_gem_object *obj)
400 return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages));
404 __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
406 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
408 atomic_inc(&obj->mm.pages_pin_count);
412 i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj)
414 return atomic_read(&obj->mm.pages_pin_count);
418 __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
420 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
421 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
423 atomic_dec(&obj->mm.pages_pin_count);
427 i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
429 __i915_gem_object_unpin_pages(obj);
432 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj);
433 void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
434 void i915_gem_object_writeback(struct drm_i915_gem_object *obj);
437 * i915_gem_object_pin_map - return a contiguous mapping of the entire object
438 * @obj: the object to map into kernel address space
439 * @type: the type of mapping, used to select pgprot_t
441 * Calls i915_gem_object_pin_pages() to prevent reaping of the object's
442 * pages and then returns a contiguous mapping of the backing storage into
443 * the kernel address space. Based on the @type of mapping, the PTE will be
444 * set to either WriteBack or WriteCombine (via pgprot_t).
446 * The caller is responsible for calling i915_gem_object_unpin_map() when the
447 * mapping is no longer required.
449 * Returns the pointer through which to access the mapped object, or an
450 * ERR_PTR() on error.
452 void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
453 enum i915_map_type type);
455 void *__must_check i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
456 enum i915_map_type type);
458 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
459 unsigned long offset,
461 static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj)
463 __i915_gem_object_flush_map(obj, 0, obj->base.size);
467 * i915_gem_object_unpin_map - releases an earlier mapping
468 * @obj: the object to unmap
470 * After pinning the object and mapping its pages, once you are finished
471 * with your access, call i915_gem_object_unpin_map() to release the pin
472 * upon the mapping. Once the pin count reaches zero, that mapping may be
475 static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
477 i915_gem_object_unpin_pages(obj);
480 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj);
482 int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,
483 unsigned int *needs_clflush);
484 int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,
485 unsigned int *needs_clflush);
486 #define CLFLUSH_BEFORE BIT(0)
487 #define CLFLUSH_AFTER BIT(1)
488 #define CLFLUSH_FLAGS (CLFLUSH_BEFORE | CLFLUSH_AFTER)
491 i915_gem_object_finish_access(struct drm_i915_gem_object *obj)
493 i915_gem_object_unpin_pages(obj);
496 static inline struct intel_engine_cs *
497 i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
499 struct intel_engine_cs *engine = NULL;
500 struct dma_fence *fence;
503 fence = dma_resv_get_excl_unlocked(obj->base.resv);
506 if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
507 engine = to_request(fence)->engine;
508 dma_fence_put(fence);
513 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
514 unsigned int cache_level);
515 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
516 void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj);
519 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write);
521 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write);
523 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write);
524 struct i915_vma * __must_check
525 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
526 struct i915_gem_ww_ctx *ww,
528 const struct i915_ggtt_view *view,
531 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj);
532 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
533 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
535 static inline bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
537 if (obj->cache_dirty)
540 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
543 /* Currently in use by HW (display engine)? Keep flushed. */
544 return i915_gem_object_is_framebuffer(obj);
547 static inline void __start_cpu_write(struct drm_i915_gem_object *obj)
549 obj->read_domains = I915_GEM_DOMAIN_CPU;
550 obj->write_domain = I915_GEM_DOMAIN_CPU;
551 if (cpu_write_needs_clflush(obj))
552 obj->cache_dirty = true;
555 void i915_gem_fence_wait_priority(struct dma_fence *fence,
556 const struct i915_sched_attr *attr);
558 int i915_gem_object_wait(struct drm_i915_gem_object *obj,
561 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
563 const struct i915_sched_attr *attr);
565 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
566 enum fb_op_origin origin);
567 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
568 enum fb_op_origin origin);
571 i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
572 enum fb_op_origin origin)
574 if (unlikely(rcu_access_pointer(obj->frontbuffer)))
575 __i915_gem_object_flush_frontbuffer(obj, origin);
579 i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
580 enum fb_op_origin origin)
582 if (unlikely(rcu_access_pointer(obj->frontbuffer)))
583 __i915_gem_object_invalidate_frontbuffer(obj, origin);
586 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size);
588 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj);
590 #ifdef CONFIG_MMU_NOTIFIER
592 i915_gem_object_is_userptr(struct drm_i915_gem_object *obj)
594 return obj->userptr.notifier.mm;
597 int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj);
598 int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj);
599 void i915_gem_object_userptr_submit_fini(struct drm_i915_gem_object *obj);
600 int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj);
602 static inline bool i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) { return false; }
604 static inline int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
605 static inline int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
606 static inline void i915_gem_object_userptr_submit_fini(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); }
607 static inline int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }