| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright © 2016 Intel Corporation |
| 5 | */ |
| 6 | |
| 7 | #ifndef __I915_GEM_OBJECT_H__ |
| 8 | #define __I915_GEM_OBJECT_H__ |
| 9 | |
| 10 | #include <drm/drm_gem.h> |
| 11 | #include <drm/drm_file.h> |
| 12 | #include <drm/drm_device.h> |
| 13 | |
| 14 | #include <drm/i915_drm.h> |
| 15 | |
| 16 | #include "gem/i915_gem_object_types.h" |
| 17 | |
| 18 | struct drm_i915_gem_object *i915_gem_object_alloc(void); |
| 19 | void i915_gem_object_free(struct drm_i915_gem_object *obj); |
| 20 | |
| 21 | /** |
| 22 | * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle |
| 23 | * @filp: DRM file private date |
| 24 | * @handle: userspace handle |
| 25 | * |
| 26 | * Returns: |
| 27 | * |
| 28 | * A pointer to the object named by the handle if such exists on @filp, NULL |
| 29 | * otherwise. This object is only valid whilst under the RCU read lock, and |
| 30 | * note carefully the object may be in the process of being destroyed. |
| 31 | */ |
| 32 | static inline struct drm_i915_gem_object * |
| 33 | i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle) |
| 34 | { |
| 35 | #ifdef CONFIG_LOCKDEP |
| 36 | WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map)); |
| 37 | #endif |
| 38 | return idr_find(&file->object_idr, handle); |
| 39 | } |
| 40 | |
| 41 | static inline struct drm_i915_gem_object * |
| 42 | i915_gem_object_lookup(struct drm_file *file, u32 handle) |
| 43 | { |
| 44 | struct drm_i915_gem_object *obj; |
| 45 | |
| 46 | rcu_read_lock(); |
| 47 | obj = i915_gem_object_lookup_rcu(file, handle); |
| 48 | if (obj && !kref_get_unless_zero(&obj->base.refcount)) |
| 49 | obj = NULL; |
| 50 | rcu_read_unlock(); |
| 51 | |
| 52 | return obj; |
| 53 | } |
| 54 | |
| 55 | __deprecated |
| 56 | extern struct drm_gem_object * |
| 57 | drm_gem_object_lookup(struct drm_file *file, u32 handle); |
| 58 | |
| 59 | __attribute__((nonnull)) |
| 60 | static inline struct drm_i915_gem_object * |
| 61 | i915_gem_object_get(struct drm_i915_gem_object *obj) |
| 62 | { |
| 63 | drm_gem_object_get(&obj->base); |
| 64 | return obj; |
| 65 | } |
| 66 | |
| 67 | __attribute__((nonnull)) |
| 68 | static inline void |
| 69 | i915_gem_object_put(struct drm_i915_gem_object *obj) |
| 70 | { |
| 71 | __drm_gem_object_put(&obj->base); |
| 72 | } |
| 73 | |
| 74 | static inline void i915_gem_object_lock(struct drm_i915_gem_object *obj) |
| 75 | { |
| 76 | reservation_object_lock(obj->resv, NULL); |
| 77 | } |
| 78 | |
| 79 | static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj) |
| 80 | { |
| 81 | reservation_object_unlock(obj->resv); |
| 82 | } |
| 83 | |
| 84 | static inline void |
| 85 | i915_gem_object_set_readonly(struct drm_i915_gem_object *obj) |
| 86 | { |
| 87 | obj->base.vma_node.readonly = true; |
| 88 | } |
| 89 | |
| 90 | static inline bool |
| 91 | i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj) |
| 92 | { |
| 93 | return obj->base.vma_node.readonly; |
| 94 | } |
| 95 | |
| 96 | static inline bool |
| 97 | i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj) |
| 98 | { |
| 99 | return obj->ops->flags & I915_GEM_OBJECT_HAS_STRUCT_PAGE; |
| 100 | } |
| 101 | |
| 102 | static inline bool |
| 103 | i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj) |
| 104 | { |
| 105 | return obj->ops->flags & I915_GEM_OBJECT_IS_SHRINKABLE; |
| 106 | } |
| 107 | |
| 108 | static inline bool |
| 109 | i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj) |
| 110 | { |
| 111 | return obj->ops->flags & I915_GEM_OBJECT_IS_PROXY; |
| 112 | } |
| 113 | |
| 114 | static inline bool |
| 115 | i915_gem_object_needs_async_cancel(const struct drm_i915_gem_object *obj) |
| 116 | { |
| 117 | return obj->ops->flags & I915_GEM_OBJECT_ASYNC_CANCEL; |
| 118 | } |
| 119 | |
| 120 | static inline bool |
| 121 | i915_gem_object_is_active(const struct drm_i915_gem_object *obj) |
| 122 | { |
| 123 | return obj->active_count; |
| 124 | } |
| 125 | |
| 126 | static inline bool |
| 127 | i915_gem_object_has_active_reference(const struct drm_i915_gem_object *obj) |
| 128 | { |
| 129 | return test_bit(I915_BO_ACTIVE_REF, &obj->flags); |
| 130 | } |
| 131 | |
| 132 | static inline void |
| 133 | i915_gem_object_set_active_reference(struct drm_i915_gem_object *obj) |
| 134 | { |
| 135 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 136 | __set_bit(I915_BO_ACTIVE_REF, &obj->flags); |
| 137 | } |
| 138 | |
| 139 | static inline void |
| 140 | i915_gem_object_clear_active_reference(struct drm_i915_gem_object *obj) |
| 141 | { |
| 142 | lockdep_assert_held(&obj->base.dev->struct_mutex); |
| 143 | __clear_bit(I915_BO_ACTIVE_REF, &obj->flags); |
| 144 | } |
| 145 | |
| 146 | void __i915_gem_object_release_unless_active(struct drm_i915_gem_object *obj); |
| 147 | |
| 148 | static inline bool |
| 149 | i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj) |
| 150 | { |
| 151 | return READ_ONCE(obj->framebuffer_references); |
| 152 | } |
| 153 | |
| 154 | static inline unsigned int |
| 155 | i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj) |
| 156 | { |
| 157 | return obj->tiling_and_stride & TILING_MASK; |
| 158 | } |
| 159 | |
| 160 | static inline bool |
| 161 | i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj) |
| 162 | { |
| 163 | return i915_gem_object_get_tiling(obj) != I915_TILING_NONE; |
| 164 | } |
| 165 | |
| 166 | static inline unsigned int |
| 167 | i915_gem_object_get_stride(const struct drm_i915_gem_object *obj) |
| 168 | { |
| 169 | return obj->tiling_and_stride & STRIDE_MASK; |
| 170 | } |
| 171 | |
| 172 | static inline unsigned int |
| 173 | i915_gem_tile_height(unsigned int tiling) |
| 174 | { |
| 175 | GEM_BUG_ON(!tiling); |
| 176 | return tiling == I915_TILING_Y ? 32 : 8; |
| 177 | } |
| 178 | |
| 179 | static inline unsigned int |
| 180 | i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj) |
| 181 | { |
| 182 | return i915_gem_tile_height(i915_gem_object_get_tiling(obj)); |
| 183 | } |
| 184 | |
| 185 | static inline unsigned int |
| 186 | i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj) |
| 187 | { |
| 188 | return (i915_gem_object_get_stride(obj) * |
| 189 | i915_gem_object_get_tile_height(obj)); |
| 190 | } |
| 191 | |
| 192 | int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj, |
| 193 | unsigned int tiling, unsigned int stride); |
| 194 | |
| 195 | static inline struct intel_engine_cs * |
| 196 | i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj) |
| 197 | { |
| 198 | struct intel_engine_cs *engine = NULL; |
| 199 | struct dma_fence *fence; |
| 200 | |
| 201 | rcu_read_lock(); |
| 202 | fence = reservation_object_get_excl_rcu(obj->resv); |
| 203 | rcu_read_unlock(); |
| 204 | |
| 205 | if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence)) |
| 206 | engine = to_request(fence)->engine; |
| 207 | dma_fence_put(fence); |
| 208 | |
| 209 | return engine; |
| 210 | } |
| 211 | |
| 212 | void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj, |
| 213 | unsigned int cache_level); |
| 214 | void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj); |
| 215 | |
| 216 | void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj, |
| 217 | struct sg_table *pages, |
| 218 | bool needs_clflush); |
| 219 | |
| 220 | #endif |