xref: /linux/drivers/gpu/drm/i915/i915_active.h (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
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
52*88026282SJani Nikula  * @active: the active tracker
53*88026282SJani Nikula  * @fence: initial fence to track, can be NULL
54*88026282SJani Nikula  * @fn: 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
__i915_active_fence_init(struct i915_active_fence * active,void * fence,dma_fence_func_t fn)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
80*88026282SJani Nikula  * @active: the active tracker
81*88026282SJani Nikula  * @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
92*88026282SJani Nikula  * @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 *
i915_active_fence_get(struct i915_active_fence * active)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
114*88026282SJani Nikula  * @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
i915_active_fence_isset(const struct i915_active_fence * active)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),
155c3b14760SMatthew Auld 			unsigned long flags,
156ae303004SChris Wilson 			struct lock_class_key *mkey,
157ae303004SChris Wilson 			struct lock_class_key *wkey);
158ae303004SChris Wilson 
159ae303004SChris Wilson /* Specialise each class of i915_active to avoid impossible lockdep cycles. */
160c3b14760SMatthew Auld #define i915_active_init(ref, active, retire, flags) do {			\
161ae303004SChris Wilson 	static struct lock_class_key __mkey;					\
162ae303004SChris Wilson 	static struct lock_class_key __wkey;					\
16312c255b5SChris Wilson 										\
164c3b14760SMatthew Auld 	__i915_active_init(ref, active, retire, flags, &__mkey, &__wkey);	\
16512c255b5SChris Wilson } while (0)
16664d6c500SChris Wilson 
167ad5c99e0SMaarten Lankhorst int i915_active_add_request(struct i915_active *ref, struct i915_request *rq);
168d19d71fcSChris Wilson 
169e3793468SChris Wilson struct dma_fence *
170e3793468SChris Wilson i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f);
1712850748eSChris Wilson 
172d75a92a8SChris Wilson int __i915_active_wait(struct i915_active *ref, int state);
i915_active_wait(struct i915_active * ref)173d75a92a8SChris Wilson static inline int i915_active_wait(struct i915_active *ref)
174d75a92a8SChris Wilson {
175d75a92a8SChris Wilson 	return __i915_active_wait(ref, TASK_INTERRUPTIBLE);
176d75a92a8SChris Wilson }
17764d6c500SChris Wilson 
17829e6ecf3SChris Wilson int i915_sw_fence_await_active(struct i915_sw_fence *fence,
17929e6ecf3SChris Wilson 			       struct i915_active *ref,
18029e6ecf3SChris Wilson 			       unsigned int flags);
18129e6ecf3SChris Wilson int i915_request_await_active(struct i915_request *rq,
18229e6ecf3SChris Wilson 			      struct i915_active *ref,
18329e6ecf3SChris Wilson 			      unsigned int flags);
184442dbc5cSChris Wilson #define I915_ACTIVE_AWAIT_EXCL BIT(0)
185442dbc5cSChris Wilson #define I915_ACTIVE_AWAIT_ACTIVE BIT(1)
1863b0a0579SChris Wilson #define I915_ACTIVE_AWAIT_BARRIER BIT(2)
18764d6c500SChris Wilson 
18812c255b5SChris Wilson int i915_active_acquire(struct i915_active *ref);
1895d934137SChris Wilson int i915_active_acquire_for_context(struct i915_active *ref, u64 idx);
190b1e3177bSChris Wilson bool i915_active_acquire_if_busy(struct i915_active *ref);
1915d934137SChris Wilson 
19264d6c500SChris Wilson void i915_active_release(struct i915_active *ref);
19379c7a28eSChris Wilson 
__i915_active_acquire(struct i915_active * ref)194e5429340SChris Wilson static inline void __i915_active_acquire(struct i915_active *ref)
195e5429340SChris Wilson {
196e5429340SChris Wilson 	GEM_BUG_ON(!atomic_read(&ref->count));
197e5429340SChris Wilson 	atomic_inc(&ref->count);
198e5429340SChris Wilson }
199e5429340SChris Wilson 
20064d6c500SChris Wilson static inline bool
i915_active_is_idle(const struct i915_active * ref)20164d6c500SChris Wilson i915_active_is_idle(const struct i915_active *ref)
20264d6c500SChris Wilson {
20312c255b5SChris Wilson 	return !atomic_read(&ref->count);
20464d6c500SChris Wilson }
20564d6c500SChris Wilson 
20664d6c500SChris Wilson void i915_active_fini(struct i915_active *ref);
20764d6c500SChris Wilson 
208ce476c80SChris Wilson int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
209ce476c80SChris Wilson 					    struct intel_engine_cs *engine);
210ce476c80SChris Wilson void i915_active_acquire_barrier(struct i915_active *ref);
211d8af05ffSChris Wilson void i915_request_add_active_barriers(struct i915_request *rq);
212ce476c80SChris Wilson 
213164a4128SChris Wilson void i915_active_print(struct i915_active *ref, struct drm_printer *m);
21438813767SChris Wilson void i915_active_unlock_wait(struct i915_active *ref);
215164a4128SChris Wilson 
216229007e0SChris Wilson struct i915_active *i915_active_create(void);
217229007e0SChris Wilson struct i915_active *i915_active_get(struct i915_active *ref);
218229007e0SChris Wilson void i915_active_put(struct i915_active *ref);
219229007e0SChris Wilson 
__i915_request_await_exclusive(struct i915_request * rq,struct i915_active * active)220af5c6fcfSChris Wilson static inline int __i915_request_await_exclusive(struct i915_request *rq,
221af5c6fcfSChris Wilson 						 struct i915_active *active)
222af5c6fcfSChris Wilson {
223af5c6fcfSChris Wilson 	struct dma_fence *fence;
224af5c6fcfSChris Wilson 	int err = 0;
225af5c6fcfSChris Wilson 
226af5c6fcfSChris Wilson 	fence = i915_active_fence_get(&active->excl);
227af5c6fcfSChris Wilson 	if (fence) {
228af5c6fcfSChris Wilson 		err = i915_request_await_dma_fence(rq, fence);
229af5c6fcfSChris Wilson 		dma_fence_put(fence);
230af5c6fcfSChris Wilson 	}
231af5c6fcfSChris Wilson 
232af5c6fcfSChris Wilson 	return err;
233af5c6fcfSChris Wilson }
234af5c6fcfSChris Wilson 
235512ba03eSDaniel Vetter void i915_active_module_exit(void);
236512ba03eSDaniel Vetter int i915_active_module_init(void);
237512ba03eSDaniel Vetter 
23864d6c500SChris Wilson #endif /* _I915_ACTIVE_H_ */
239