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 "intel_memory_region.h"
16 #include "i915_gem_object_types.h"
17 #include "i915_gem_gtt.h"
18 #include "i915_gem_ww.h"
19 #include "i915_vma_types.h"
24 * XXX: There is a prevalence of the assumption that we fit the
25 * object's page count inside a 32bit _signed_ variable. Let's document
26 * this and catch if we ever need to fix it. In the meantime, if you do
27 * spot such a local variable, please consider fixing!
29 * Aside from our own locals (for which we have no excuse!):
30 * - sg_table embeds unsigned int for num_pages
31 * - get_user_pages*() mixed ints with longs
33 #define GEM_CHECK_SIZE_OVERFLOW(sz) \
34 GEM_WARN_ON((sz) >> PAGE_SHIFT > INT_MAX)
36 static inline bool i915_gem_object_size_2big(u64 size)
38 struct drm_i915_gem_object *obj;
40 if (GEM_CHECK_SIZE_OVERFLOW(size))
43 if (overflows_type(size, obj->base.size))
49 void i915_gem_init__objects(struct drm_i915_private *i915);
51 void i915_objects_module_exit(void);
52 int i915_objects_module_init(void);
54 struct drm_i915_gem_object *i915_gem_object_alloc(void);
55 void i915_gem_object_free(struct drm_i915_gem_object *obj);
57 void i915_gem_object_init(struct drm_i915_gem_object *obj,
58 const struct drm_i915_gem_object_ops *ops,
59 struct lock_class_key *key,
60 unsigned alloc_flags);
62 void __i915_gem_object_fini(struct drm_i915_gem_object *obj);
64 struct drm_i915_gem_object *
65 i915_gem_object_create_shmem(struct drm_i915_private *i915,
66 resource_size_t size);
67 struct drm_i915_gem_object *
68 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
69 const void *data, resource_size_t size);
70 struct drm_i915_gem_object *
71 __i915_gem_object_create_user(struct drm_i915_private *i915, u64 size,
72 struct intel_memory_region **placements,
73 unsigned int n_placements);
75 extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops;
77 void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
78 struct sg_table *pages,
81 int i915_gem_object_pwrite_phys(struct drm_i915_gem_object *obj,
82 const struct drm_i915_gem_pwrite *args);
83 int i915_gem_object_pread_phys(struct drm_i915_gem_object *obj,
84 const struct drm_i915_gem_pread *args);
86 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align);
87 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj,
88 struct sg_table *pages);
89 void i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
90 struct sg_table *pages);
92 void i915_gem_flush_free_objects(struct drm_i915_private *i915);
95 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj);
98 * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
99 * @filp: DRM file private date
100 * @handle: userspace handle
104 * A pointer to the object named by the handle if such exists on @filp, NULL
105 * otherwise. This object is only valid whilst under the RCU read lock, and
106 * note carefully the object may be in the process of being destroyed.
108 static inline struct drm_i915_gem_object *
109 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
111 #ifdef CONFIG_LOCKDEP
112 WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
114 return idr_find(&file->object_idr, handle);
117 static inline struct drm_i915_gem_object *
118 i915_gem_object_get_rcu(struct drm_i915_gem_object *obj)
120 if (obj && !kref_get_unless_zero(&obj->base.refcount))
126 static inline struct drm_i915_gem_object *
127 i915_gem_object_lookup(struct drm_file *file, u32 handle)
129 struct drm_i915_gem_object *obj;
132 obj = i915_gem_object_lookup_rcu(file, handle);
133 obj = i915_gem_object_get_rcu(obj);
140 struct drm_gem_object *
141 drm_gem_object_lookup(struct drm_file *file, u32 handle);
143 __attribute__((nonnull))
144 static inline struct drm_i915_gem_object *
145 i915_gem_object_get(struct drm_i915_gem_object *obj)
147 drm_gem_object_get(&obj->base);
151 __attribute__((nonnull))
153 i915_gem_object_put(struct drm_i915_gem_object *obj)
155 __drm_gem_object_put(&obj->base);
158 #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv)
161 * If more than one potential simultaneous locker, assert held.
163 static inline void assert_object_held_shared(const struct drm_i915_gem_object *obj)
166 * Note mm list lookup is protected by
167 * kref_get_unless_zero().
169 if (IS_ENABLED(CONFIG_LOCKDEP) &&
170 kref_read(&obj->base.refcount) > 0)
171 assert_object_held(obj);
174 static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj,
175 struct i915_gem_ww_ctx *ww,
181 ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL);
183 ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL);
186 i915_gem_object_get(obj);
187 list_add_tail(&obj->obj_link, &ww->obj_list);
189 if (ret == -EALREADY)
192 if (ret == -EDEADLK) {
193 i915_gem_object_get(obj);
200 static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj,
201 struct i915_gem_ww_ctx *ww)
203 return __i915_gem_object_lock(obj, ww, ww && ww->intr);
206 static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj,
207 struct i915_gem_ww_ctx *ww)
209 WARN_ON(ww && !ww->intr);
210 return __i915_gem_object_lock(obj, ww, true);
213 static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj,
214 struct i915_gem_ww_ctx *ww)
217 return dma_resv_trylock(obj->base.resv);
219 return ww_mutex_trylock(&obj->base.resv->lock, &ww->ctx);
222 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
224 if (obj->ops->adjust_lru)
225 obj->ops->adjust_lru(obj);
227 dma_resv_unlock(obj->base.resv);
231 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
233 obj->flags |= I915_BO_READONLY;
237 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
239 return obj->flags & I915_BO_READONLY;
243 i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj)
245 return obj->flags & I915_BO_ALLOC_CONTIGUOUS;
249 i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj)
251 return obj->flags & I915_BO_ALLOC_VOLATILE;
255 i915_gem_object_set_volatile(struct drm_i915_gem_object *obj)
257 obj->flags |= I915_BO_ALLOC_VOLATILE;
261 i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj)
263 return test_bit(I915_TILING_QUIRK_BIT, &obj->flags);
267 i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj)
269 set_bit(I915_TILING_QUIRK_BIT, &obj->flags);
273 i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj)
275 clear_bit(I915_TILING_QUIRK_BIT, &obj->flags);
279 i915_gem_object_is_protected(const struct drm_i915_gem_object *obj)
281 return obj->flags & I915_BO_PROTECTED;
285 i915_gem_object_type_has(const struct drm_i915_gem_object *obj,
288 return obj->ops->flags & flags;
291 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj);
293 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj);
296 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
298 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE);
302 i915_gem_object_has_self_managed_shrink_list(const struct drm_i915_gem_object *obj)
304 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_SELF_MANAGED_SHRINK_LIST);
308 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
310 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY);
314 i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj)
316 return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP);
320 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
322 return READ_ONCE(obj->frontbuffer);
325 static inline unsigned int
326 i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj)
328 return obj->tiling_and_stride & TILING_MASK;
332 i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj)
334 return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
337 static inline unsigned int
338 i915_gem_object_get_stride(const struct drm_i915_gem_object *obj)
340 return obj->tiling_and_stride & STRIDE_MASK;
343 static inline unsigned int
344 i915_gem_tile_height(unsigned int tiling)
347 return tiling == I915_TILING_Y ? 32 : 8;
350 static inline unsigned int
351 i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj)
353 return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
356 static inline unsigned int
357 i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj)
359 return (i915_gem_object_get_stride(obj) *
360 i915_gem_object_get_tile_height(obj));
363 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
364 unsigned int tiling, unsigned int stride);
367 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
368 struct i915_gem_object_page_iter *iter,
370 unsigned int *offset, bool dma);
372 static inline struct scatterlist *
373 i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
375 unsigned int *offset)
377 return __i915_gem_object_get_sg(obj, &obj->mm.get_page, n, offset, false);
380 static inline struct scatterlist *
381 i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj,
383 unsigned int *offset)
385 return __i915_gem_object_get_sg(obj, &obj->mm.get_dma_page, n, offset, true);
389 i915_gem_object_get_page(struct drm_i915_gem_object *obj,
393 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
397 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
402 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
405 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
406 struct sg_table *pages,
407 unsigned int sg_page_sizes);
409 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
410 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
412 static inline int __must_check
413 i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
415 assert_object_held(obj);
417 if (atomic_inc_not_zero(&obj->mm.pages_pin_count))
420 return __i915_gem_object_get_pages(obj);
423 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj);
426 i915_gem_object_has_pages(struct drm_i915_gem_object *obj)
428 return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages));
432 __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
434 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
436 atomic_inc(&obj->mm.pages_pin_count);
440 i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj)
442 return atomic_read(&obj->mm.pages_pin_count);
446 __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
448 GEM_BUG_ON(!i915_gem_object_has_pages(obj));
449 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
451 atomic_dec(&obj->mm.pages_pin_count);
455 i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
457 __i915_gem_object_unpin_pages(obj);
460 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj);
461 int i915_gem_object_truncate(struct drm_i915_gem_object *obj);
464 * i915_gem_object_pin_map - return a contiguous mapping of the entire object
465 * @obj: the object to map into kernel address space
466 * @type: the type of mapping, used to select pgprot_t
468 * Calls i915_gem_object_pin_pages() to prevent reaping of the object's
469 * pages and then returns a contiguous mapping of the backing storage into
470 * the kernel address space. Based on the @type of mapping, the PTE will be
471 * set to either WriteBack or WriteCombine (via pgprot_t).
473 * The caller is responsible for calling i915_gem_object_unpin_map() when the
474 * mapping is no longer required.
476 * Returns the pointer through which to access the mapped object, or an
477 * ERR_PTR() on error.
479 void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
480 enum i915_map_type type);
482 void *__must_check i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
483 enum i915_map_type type);
485 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
486 unsigned long offset,
488 static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj)
490 __i915_gem_object_flush_map(obj, 0, obj->base.size);
494 * i915_gem_object_unpin_map - releases an earlier mapping
495 * @obj: the object to unmap
497 * After pinning the object and mapping its pages, once you are finished
498 * with your access, call i915_gem_object_unpin_map() to release the pin
499 * upon the mapping. Once the pin count reaches zero, that mapping may be
502 static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
504 i915_gem_object_unpin_pages(obj);
507 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj);
509 int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,
510 unsigned int *needs_clflush);
511 int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,
512 unsigned int *needs_clflush);
513 #define CLFLUSH_BEFORE BIT(0)
514 #define CLFLUSH_AFTER BIT(1)
515 #define CLFLUSH_FLAGS (CLFLUSH_BEFORE | CLFLUSH_AFTER)
518 i915_gem_object_finish_access(struct drm_i915_gem_object *obj)
520 i915_gem_object_unpin_pages(obj);
523 int i915_gem_object_get_moving_fence(struct drm_i915_gem_object *obj,
524 struct dma_fence **fence);
525 int i915_gem_object_wait_moving_fence(struct drm_i915_gem_object *obj,
528 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
529 unsigned int cache_level);
530 bool i915_gem_object_can_bypass_llc(struct drm_i915_gem_object *obj);
531 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
532 void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj);
533 bool i915_gem_cpu_write_needs_clflush(struct drm_i915_gem_object *obj);
536 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write);
538 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write);
540 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write);
541 struct i915_vma * __must_check
542 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
543 struct i915_gem_ww_ctx *ww,
545 const struct i915_ggtt_view *view,
548 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj);
549 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
550 void __i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
551 void __i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
552 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
554 static inline void __start_cpu_write(struct drm_i915_gem_object *obj)
556 obj->read_domains = I915_GEM_DOMAIN_CPU;
557 obj->write_domain = I915_GEM_DOMAIN_CPU;
558 if (i915_gem_cpu_write_needs_clflush(obj))
559 obj->cache_dirty = true;
562 void i915_gem_fence_wait_priority(struct dma_fence *fence,
563 const struct i915_sched_attr *attr);
565 int i915_gem_object_wait(struct drm_i915_gem_object *obj,
568 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
570 const struct i915_sched_attr *attr);
572 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
573 enum fb_op_origin origin);
574 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
575 enum fb_op_origin origin);
578 i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
579 enum fb_op_origin origin)
581 if (unlikely(rcu_access_pointer(obj->frontbuffer)))
582 __i915_gem_object_flush_frontbuffer(obj, origin);
586 i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
587 enum fb_op_origin origin)
589 if (unlikely(rcu_access_pointer(obj->frontbuffer)))
590 __i915_gem_object_invalidate_frontbuffer(obj, origin);
593 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size);
595 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj);
597 void __i915_gem_free_object_rcu(struct rcu_head *head);
599 void __i915_gem_object_pages_fini(struct drm_i915_gem_object *obj);
601 void __i915_gem_free_object(struct drm_i915_gem_object *obj);
603 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj);
605 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj);
607 int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
608 struct i915_gem_ww_ctx *ww,
609 enum intel_region_id id);
611 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
612 enum intel_region_id id);
614 int i915_gem_object_wait_migration(struct drm_i915_gem_object *obj,
617 bool i915_gem_object_placement_possible(struct drm_i915_gem_object *obj,
618 enum intel_memory_type type);
620 int shmem_sg_alloc_table(struct drm_i915_private *i915, struct sg_table *st,
621 size_t size, struct intel_memory_region *mr,
622 struct address_space *mapping,
623 unsigned int max_segment);
624 void shmem_sg_free_table(struct sg_table *st, struct address_space *mapping,
625 bool dirty, bool backup);
626 void __shmem_writeback(size_t size, struct address_space *mapping);
628 #ifdef CONFIG_MMU_NOTIFIER
630 i915_gem_object_is_userptr(struct drm_i915_gem_object *obj)
632 return obj->userptr.notifier.mm;
635 int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj);
636 int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj);
637 int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj);
639 static inline bool i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) { return false; }
641 static inline int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
642 static inline int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
643 static inline int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }