164d6c500SChris Wilson /* 264d6c500SChris Wilson * SPDX-License-Identifier: MIT 364d6c500SChris Wilson * 464d6c500SChris Wilson * Copyright © 2019 Intel Corporation 564d6c500SChris Wilson */ 664d6c500SChris Wilson 764d6c500SChris Wilson #ifndef _I915_ACTIVE_H_ 864d6c500SChris Wilson #define _I915_ACTIVE_H_ 964d6c500SChris Wilson 1021950ee7SChris Wilson #include <linux/lockdep.h> 1121950ee7SChris Wilson 1264d6c500SChris Wilson #include "i915_active_types.h" 1321950ee7SChris Wilson #include "i915_request.h" 1421950ee7SChris Wilson 15b1e3177bSChris Wilson struct i915_request; 16b1e3177bSChris Wilson struct intel_engine_cs; 17b1e3177bSChris Wilson struct intel_timeline; 18b1e3177bSChris Wilson 1921950ee7SChris Wilson /* 2021950ee7SChris Wilson * We treat requests as fences. This is not be to confused with our 2121950ee7SChris Wilson * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync. 2221950ee7SChris Wilson * We use the fences to synchronize access from the CPU with activity on the 2321950ee7SChris Wilson * GPU, for example, we should not rewrite an object's PTE whilst the GPU 2421950ee7SChris Wilson * is reading them. We also track fences at a higher level to provide 2521950ee7SChris Wilson * implicit synchronisation around GEM objects, e.g. set-domain will wait 2621950ee7SChris Wilson * for outstanding GPU rendering before marking the object ready for CPU 2721950ee7SChris Wilson * access, or a pageflip will wait until the GPU is complete before showing 2821950ee7SChris Wilson * the frame on the scanout. 2921950ee7SChris Wilson * 3021950ee7SChris Wilson * In order to use a fence, the object must track the fence it needs to 3121950ee7SChris Wilson * serialise with. For example, GEM objects want to track both read and 3221950ee7SChris Wilson * write access so that we can perform concurrent read operations between 3321950ee7SChris Wilson * the CPU and GPU engines, as well as waiting for all rendering to 3421950ee7SChris Wilson * complete, or waiting for the last GPU user of a "fence register". The 35b1e3177bSChris Wilson * object then embeds a #i915_active_fence to track the most recent (in 3621950ee7SChris Wilson * retirement order) request relevant for the desired mode of access. 37b1e3177bSChris Wilson * The #i915_active_fence is updated with i915_active_fence_set() to 3821950ee7SChris Wilson * track the most recent fence request, typically this is done as part of 3921950ee7SChris Wilson * i915_vma_move_to_active(). 4021950ee7SChris Wilson * 41b1e3177bSChris Wilson * When the #i915_active_fence completes (is retired), it will 4221950ee7SChris Wilson * signal its completion to the owner through a callback as well as mark 43b1e3177bSChris Wilson * itself as idle (i915_active_fence.request == NULL). The owner 4421950ee7SChris Wilson * can then perform any action, such as delayed freeing of an active 4521950ee7SChris Wilson * resource including itself. 4621950ee7SChris Wilson */ 4721950ee7SChris Wilson 48b1e3177bSChris Wilson void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb); 4921950ee7SChris Wilson 5021950ee7SChris Wilson /** 51b1e3177bSChris Wilson * __i915_active_fence_init - prepares the activity tracker for use 5221950ee7SChris Wilson * @active - the active tracker 53b1e3177bSChris Wilson * @fence - initial fence to track, can be NULL 5421950ee7SChris Wilson * @func - a callback when then the tracker is retired (becomes idle), 5521950ee7SChris Wilson * can be NULL 5621950ee7SChris Wilson * 57b1e3177bSChris Wilson * i915_active_fence_init() prepares the embedded @active struct for use as 58b1e3177bSChris Wilson * an activity tracker, that is for tracking the last known active fence 59b1e3177bSChris Wilson * associated with it. When the last fence becomes idle, when it is retired 6021950ee7SChris Wilson * after completion, the optional callback @func is invoked. 6121950ee7SChris Wilson */ 6221950ee7SChris Wilson static inline void 63b1e3177bSChris Wilson __i915_active_fence_init(struct i915_active_fence *active, 64b1e3177bSChris Wilson void *fence, 65b1e3177bSChris Wilson dma_fence_func_t fn) 6621950ee7SChris Wilson { 67b1e3177bSChris Wilson RCU_INIT_POINTER(active->fence, fence); 68b1e3177bSChris Wilson active->cb.func = fn ?: i915_active_noop; 6921950ee7SChris Wilson } 7021950ee7SChris Wilson 71df9f85d8SChris Wilson #define INIT_ACTIVE_FENCE(A) \ 72df9f85d8SChris Wilson __i915_active_fence_init((A), NULL, NULL) 73b1e3177bSChris Wilson 74b1e3177bSChris Wilson struct dma_fence * 75b1e3177bSChris Wilson __i915_active_fence_set(struct i915_active_fence *active, 76b1e3177bSChris Wilson struct dma_fence *fence); 7721950ee7SChris Wilson 7821950ee7SChris Wilson /** 79b1e3177bSChris Wilson * i915_active_fence_set - updates the tracker to watch the current fence 8021950ee7SChris Wilson * @active - the active tracker 81b1e3177bSChris Wilson * @rq - the request to watch 8221950ee7SChris Wilson * 83b1e3177bSChris Wilson * i915_active_fence_set() watches the given @rq for completion. While 84b1e3177bSChris Wilson * that @rq is busy, the @active reports busy. When that @rq is signaled 85b1e3177bSChris Wilson * (or else retired) the @active tracker is updated to report idle. 8621950ee7SChris Wilson */ 8721950ee7SChris Wilson int __must_check 88b1e3177bSChris Wilson i915_active_fence_set(struct i915_active_fence *active, 8921950ee7SChris Wilson struct i915_request *rq); 9021950ee7SChris Wilson /** 91b1e3177bSChris Wilson * i915_active_fence_get - return a reference to the active fence 9221950ee7SChris Wilson * @active - the active tracker 9321950ee7SChris Wilson * 94b1e3177bSChris Wilson * i915_active_fence_get() returns a reference to the active fence, 9521950ee7SChris Wilson * or NULL if the active tracker is idle. The reference is obtained under RCU, 9621950ee7SChris Wilson * so no locking is required by the caller. 9721950ee7SChris Wilson * 98b1e3177bSChris Wilson * The reference should be freed with dma_fence_put(). 9921950ee7SChris Wilson */ 100b1e3177bSChris Wilson static inline struct dma_fence * 101b1e3177bSChris Wilson i915_active_fence_get(struct i915_active_fence *active) 10221950ee7SChris Wilson { 103b1e3177bSChris Wilson struct dma_fence *fence; 10421950ee7SChris Wilson 10521950ee7SChris Wilson rcu_read_lock(); 106b1e3177bSChris Wilson fence = dma_fence_get_rcu_safe(&active->fence); 10721950ee7SChris Wilson rcu_read_unlock(); 10821950ee7SChris Wilson 109b1e3177bSChris Wilson return fence; 11021950ee7SChris Wilson } 11121950ee7SChris Wilson 11221950ee7SChris Wilson /** 113b1e3177bSChris Wilson * i915_active_fence_isset - report whether the active tracker is assigned 11421950ee7SChris Wilson * @active - the active tracker 11521950ee7SChris Wilson * 116b1e3177bSChris Wilson * i915_active_fence_isset() returns true if the active tracker is currently 117b1e3177bSChris Wilson * assigned to a fence. Due to the lazy retiring, that fence may be idle 11821950ee7SChris Wilson * and this may report stale information. 11921950ee7SChris Wilson */ 12021950ee7SChris Wilson static inline bool 121b1e3177bSChris Wilson i915_active_fence_isset(const struct i915_active_fence *active) 12221950ee7SChris Wilson { 123b1e3177bSChris Wilson return rcu_access_pointer(active->fence); 12421950ee7SChris Wilson } 12521950ee7SChris Wilson 12664d6c500SChris Wilson /* 12764d6c500SChris Wilson * GPU activity tracking 12864d6c500SChris Wilson * 12964d6c500SChris Wilson * Each set of commands submitted to the GPU compromises a single request that 13064d6c500SChris Wilson * signals a fence upon completion. struct i915_request combines the 13164d6c500SChris Wilson * command submission, scheduling and fence signaling roles. If we want to see 13264d6c500SChris Wilson * if a particular task is complete, we need to grab the fence (struct 13364d6c500SChris Wilson * i915_request) for that task and check or wait for it to be signaled. More 13464d6c500SChris Wilson * often though we want to track the status of a bunch of tasks, for example 13564d6c500SChris Wilson * to wait for the GPU to finish accessing some memory across a variety of 13664d6c500SChris Wilson * different command pipelines from different clients. We could choose to 13764d6c500SChris Wilson * track every single request associated with the task, but knowing that 13864d6c500SChris Wilson * each request belongs to an ordered timeline (later requests within a 13964d6c500SChris Wilson * timeline must wait for earlier requests), we need only track the 14064d6c500SChris Wilson * latest request in each timeline to determine the overall status of the 14164d6c500SChris Wilson * task. 14264d6c500SChris Wilson * 14364d6c500SChris Wilson * struct i915_active provides this tracking across timelines. It builds a 14464d6c500SChris Wilson * composite shared-fence, and is updated as new work is submitted to the task, 14564d6c500SChris Wilson * forming a snapshot of the current status. It should be embedded into the 14664d6c500SChris Wilson * different resources that need to track their associated GPU activity to 14764d6c500SChris Wilson * provide a callback when that GPU activity has ceased, or otherwise to 14864d6c500SChris Wilson * provide a serialisation point either for request submission or for CPU 14964d6c500SChris Wilson * synchronisation. 15064d6c500SChris Wilson */ 15164d6c500SChris Wilson 152b1e3177bSChris Wilson void __i915_active_init(struct i915_active *ref, 15312c255b5SChris Wilson int (*active)(struct i915_active *ref), 15412c255b5SChris Wilson void (*retire)(struct i915_active *ref), 155ae303004SChris Wilson struct lock_class_key *mkey, 156ae303004SChris Wilson struct lock_class_key *wkey); 157ae303004SChris Wilson 158ae303004SChris Wilson /* Specialise each class of i915_active to avoid impossible lockdep cycles. */ 159b1e3177bSChris Wilson #define i915_active_init(ref, active, retire) do { \ 160ae303004SChris Wilson static struct lock_class_key __mkey; \ 161ae303004SChris Wilson static struct lock_class_key __wkey; \ 16212c255b5SChris Wilson \ 163ae303004SChris Wilson __i915_active_init(ref, active, retire, &__mkey, &__wkey); \ 16412c255b5SChris Wilson } while (0) 16564d6c500SChris Wilson 16664d6c500SChris Wilson int i915_active_ref(struct i915_active *ref, 16725ffd4b1SChris Wilson struct intel_timeline *tl, 168b1e3177bSChris Wilson struct dma_fence *fence); 16964d6c500SChris Wilson 170d19d71fcSChris Wilson static inline int 171d19d71fcSChris Wilson i915_active_add_request(struct i915_active *ref, struct i915_request *rq) 172d19d71fcSChris Wilson { 173b1e3177bSChris Wilson return i915_active_ref(ref, i915_request_timeline(rq), &rq->fence); 174d19d71fcSChris Wilson } 175d19d71fcSChris Wilson 176e3793468SChris Wilson struct dma_fence * 177e3793468SChris Wilson i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f); 1782850748eSChris Wilson 1792850748eSChris Wilson static inline bool i915_active_has_exclusive(struct i915_active *ref) 1802850748eSChris Wilson { 181b1e3177bSChris Wilson return rcu_access_pointer(ref->excl.fence); 1822850748eSChris Wilson } 1832850748eSChris Wilson 184*d75a92a8SChris Wilson int __i915_active_wait(struct i915_active *ref, int state); 185*d75a92a8SChris Wilson static inline int i915_active_wait(struct i915_active *ref) 186*d75a92a8SChris Wilson { 187*d75a92a8SChris Wilson return __i915_active_wait(ref, TASK_INTERRUPTIBLE); 188*d75a92a8SChris Wilson } 18964d6c500SChris Wilson 19029e6ecf3SChris Wilson int i915_sw_fence_await_active(struct i915_sw_fence *fence, 19129e6ecf3SChris Wilson struct i915_active *ref, 19229e6ecf3SChris Wilson unsigned int flags); 19329e6ecf3SChris Wilson int i915_request_await_active(struct i915_request *rq, 19429e6ecf3SChris Wilson struct i915_active *ref, 19529e6ecf3SChris Wilson unsigned int flags); 19629e6ecf3SChris Wilson #define I915_ACTIVE_AWAIT_ALL BIT(0) 19764d6c500SChris Wilson 19812c255b5SChris Wilson int i915_active_acquire(struct i915_active *ref); 199b1e3177bSChris Wilson bool i915_active_acquire_if_busy(struct i915_active *ref); 20064d6c500SChris Wilson void i915_active_release(struct i915_active *ref); 20179c7a28eSChris Wilson 202e5429340SChris Wilson static inline void __i915_active_acquire(struct i915_active *ref) 203e5429340SChris Wilson { 204e5429340SChris Wilson GEM_BUG_ON(!atomic_read(&ref->count)); 205e5429340SChris Wilson atomic_inc(&ref->count); 206e5429340SChris Wilson } 207e5429340SChris Wilson 20864d6c500SChris Wilson static inline bool 20964d6c500SChris Wilson i915_active_is_idle(const struct i915_active *ref) 21064d6c500SChris Wilson { 21112c255b5SChris Wilson return !atomic_read(&ref->count); 21264d6c500SChris Wilson } 21364d6c500SChris Wilson 214a42375afSChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) 21564d6c500SChris Wilson void i915_active_fini(struct i915_active *ref); 216a42375afSChris Wilson #else 217a42375afSChris Wilson static inline void i915_active_fini(struct i915_active *ref) { } 218a42375afSChris Wilson #endif 21964d6c500SChris Wilson 220ce476c80SChris Wilson int i915_active_acquire_preallocate_barrier(struct i915_active *ref, 221ce476c80SChris Wilson struct intel_engine_cs *engine); 222ce476c80SChris Wilson void i915_active_acquire_barrier(struct i915_active *ref); 223d8af05ffSChris Wilson void i915_request_add_active_barriers(struct i915_request *rq); 224ce476c80SChris Wilson 225164a4128SChris Wilson void i915_active_print(struct i915_active *ref, struct drm_printer *m); 22638813767SChris Wilson void i915_active_unlock_wait(struct i915_active *ref); 227164a4128SChris Wilson 22864d6c500SChris Wilson #endif /* _I915_ACTIVE_H_ */ 229