blob: 51e1e854ca5575157d0b84fc6e9077642712a9e1 [file] [log] [blame]
Chris Wilson64d6c502019-02-05 13:00:02 +00001/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright © 2019 Intel Corporation
5 */
6
7#ifndef _I915_ACTIVE_H_
8#define _I915_ACTIVE_H_
9
Chris Wilson21950ee2019-02-05 13:00:05 +000010#include <linux/lockdep.h>
11
Chris Wilson64d6c502019-02-05 13:00:02 +000012#include "i915_active_types.h"
Chris Wilson21950ee2019-02-05 13:00:05 +000013#include "i915_request.h"
14
Chris Wilsonb1e31772019-10-04 14:40:00 +010015struct i915_request;
16struct intel_engine_cs;
17struct intel_timeline;
18
Chris Wilson21950ee2019-02-05 13:00:05 +000019/*
20 * We treat requests as fences. This is not be to confused with our
21 * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
22 * We use the fences to synchronize access from the CPU with activity on the
23 * GPU, for example, we should not rewrite an object's PTE whilst the GPU
24 * is reading them. We also track fences at a higher level to provide
25 * implicit synchronisation around GEM objects, e.g. set-domain will wait
26 * for outstanding GPU rendering before marking the object ready for CPU
27 * access, or a pageflip will wait until the GPU is complete before showing
28 * the frame on the scanout.
29 *
30 * In order to use a fence, the object must track the fence it needs to
31 * serialise with. For example, GEM objects want to track both read and
32 * write access so that we can perform concurrent read operations between
33 * the CPU and GPU engines, as well as waiting for all rendering to
34 * complete, or waiting for the last GPU user of a "fence register". The
Chris Wilsonb1e31772019-10-04 14:40:00 +010035 * object then embeds a #i915_active_fence to track the most recent (in
Chris Wilson21950ee2019-02-05 13:00:05 +000036 * retirement order) request relevant for the desired mode of access.
Chris Wilsonb1e31772019-10-04 14:40:00 +010037 * The #i915_active_fence is updated with i915_active_fence_set() to
Chris Wilson21950ee2019-02-05 13:00:05 +000038 * track the most recent fence request, typically this is done as part of
39 * i915_vma_move_to_active().
40 *
Chris Wilsonb1e31772019-10-04 14:40:00 +010041 * When the #i915_active_fence completes (is retired), it will
Chris Wilson21950ee2019-02-05 13:00:05 +000042 * signal its completion to the owner through a callback as well as mark
Chris Wilsonb1e31772019-10-04 14:40:00 +010043 * itself as idle (i915_active_fence.request == NULL). The owner
Chris Wilson21950ee2019-02-05 13:00:05 +000044 * can then perform any action, such as delayed freeing of an active
45 * resource including itself.
46 */
47
Chris Wilsonb1e31772019-10-04 14:40:00 +010048void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb);
Chris Wilson21950ee2019-02-05 13:00:05 +000049
50/**
Chris Wilsonb1e31772019-10-04 14:40:00 +010051 * __i915_active_fence_init - prepares the activity tracker for use
Chris Wilson21950ee2019-02-05 13:00:05 +000052 * @active - the active tracker
Chris Wilsonb1e31772019-10-04 14:40:00 +010053 * @fence - initial fence to track, can be NULL
Chris Wilson21950ee2019-02-05 13:00:05 +000054 * @func - a callback when then the tracker is retired (becomes idle),
55 * can be NULL
56 *
Chris Wilsonb1e31772019-10-04 14:40:00 +010057 * i915_active_fence_init() prepares the embedded @active struct for use as
58 * an activity tracker, that is for tracking the last known active fence
59 * associated with it. When the last fence becomes idle, when it is retired
Chris Wilson21950ee2019-02-05 13:00:05 +000060 * after completion, the optional callback @func is invoked.
61 */
62static inline void
Chris Wilsonb1e31772019-10-04 14:40:00 +010063__i915_active_fence_init(struct i915_active_fence *active,
Chris Wilsonb1e31772019-10-04 14:40:00 +010064 void *fence,
65 dma_fence_func_t fn)
Chris Wilson21950ee2019-02-05 13:00:05 +000066{
Chris Wilsonb1e31772019-10-04 14:40:00 +010067 RCU_INIT_POINTER(active->fence, fence);
68 active->cb.func = fn ?: i915_active_noop;
Chris Wilson21950ee2019-02-05 13:00:05 +000069}
70
Chris Wilsondf9f85d2019-11-27 13:45:27 +000071#define INIT_ACTIVE_FENCE(A) \
72 __i915_active_fence_init((A), NULL, NULL)
Chris Wilsonb1e31772019-10-04 14:40:00 +010073
74struct dma_fence *
75__i915_active_fence_set(struct i915_active_fence *active,
76 struct dma_fence *fence);
Chris Wilson21950ee2019-02-05 13:00:05 +000077
78/**
Chris Wilsonb1e31772019-10-04 14:40:00 +010079 * i915_active_fence_set - updates the tracker to watch the current fence
Chris Wilson21950ee2019-02-05 13:00:05 +000080 * @active - the active tracker
Chris Wilsonb1e31772019-10-04 14:40:00 +010081 * @rq - the request to watch
Chris Wilson21950ee2019-02-05 13:00:05 +000082 *
Chris Wilsonb1e31772019-10-04 14:40:00 +010083 * i915_active_fence_set() watches the given @rq for completion. While
84 * that @rq is busy, the @active reports busy. When that @rq is signaled
85 * (or else retired) the @active tracker is updated to report idle.
Chris Wilson21950ee2019-02-05 13:00:05 +000086 */
Chris Wilson21950ee2019-02-05 13:00:05 +000087int __must_check
Chris Wilsonb1e31772019-10-04 14:40:00 +010088i915_active_fence_set(struct i915_active_fence *active,
89 struct i915_request *rq);
Chris Wilson21950ee2019-02-05 13:00:05 +000090/**
Chris Wilsonb1e31772019-10-04 14:40:00 +010091 * i915_active_fence_get - return a reference to the active fence
Chris Wilson21950ee2019-02-05 13:00:05 +000092 * @active - the active tracker
93 *
Chris Wilsonb1e31772019-10-04 14:40:00 +010094 * i915_active_fence_get() returns a reference to the active fence,
Chris Wilson21950ee2019-02-05 13:00:05 +000095 * or NULL if the active tracker is idle. The reference is obtained under RCU,
96 * so no locking is required by the caller.
97 *
Chris Wilsonb1e31772019-10-04 14:40:00 +010098 * The reference should be freed with dma_fence_put().
Chris Wilson21950ee2019-02-05 13:00:05 +000099 */
Chris Wilsonb1e31772019-10-04 14:40:00 +0100100static inline struct dma_fence *
101i915_active_fence_get(struct i915_active_fence *active)
Chris Wilson21950ee2019-02-05 13:00:05 +0000102{
Chris Wilsonb1e31772019-10-04 14:40:00 +0100103 struct dma_fence *fence;
Chris Wilson21950ee2019-02-05 13:00:05 +0000104
105 rcu_read_lock();
Chris Wilsonb1e31772019-10-04 14:40:00 +0100106 fence = dma_fence_get_rcu_safe(&active->fence);
Chris Wilson21950ee2019-02-05 13:00:05 +0000107 rcu_read_unlock();
108
Chris Wilsonb1e31772019-10-04 14:40:00 +0100109 return fence;
Chris Wilson21950ee2019-02-05 13:00:05 +0000110}
111
112/**
Chris Wilsonb1e31772019-10-04 14:40:00 +0100113 * i915_active_fence_isset - report whether the active tracker is assigned
Chris Wilson21950ee2019-02-05 13:00:05 +0000114 * @active - the active tracker
115 *
Chris Wilsonb1e31772019-10-04 14:40:00 +0100116 * i915_active_fence_isset() returns true if the active tracker is currently
117 * assigned to a fence. Due to the lazy retiring, that fence may be idle
Chris Wilson21950ee2019-02-05 13:00:05 +0000118 * and this may report stale information.
119 */
120static inline bool
Chris Wilsonb1e31772019-10-04 14:40:00 +0100121i915_active_fence_isset(const struct i915_active_fence *active)
Chris Wilson21950ee2019-02-05 13:00:05 +0000122{
Chris Wilsonb1e31772019-10-04 14:40:00 +0100123 return rcu_access_pointer(active->fence);
Chris Wilson21950ee2019-02-05 13:00:05 +0000124}
125
Chris Wilson64d6c502019-02-05 13:00:02 +0000126/*
127 * GPU activity tracking
128 *
129 * Each set of commands submitted to the GPU compromises a single request that
130 * signals a fence upon completion. struct i915_request combines the
131 * command submission, scheduling and fence signaling roles. If we want to see
132 * if a particular task is complete, we need to grab the fence (struct
133 * i915_request) for that task and check or wait for it to be signaled. More
134 * often though we want to track the status of a bunch of tasks, for example
135 * to wait for the GPU to finish accessing some memory across a variety of
136 * different command pipelines from different clients. We could choose to
137 * track every single request associated with the task, but knowing that
138 * each request belongs to an ordered timeline (later requests within a
139 * timeline must wait for earlier requests), we need only track the
140 * latest request in each timeline to determine the overall status of the
141 * task.
142 *
143 * struct i915_active provides this tracking across timelines. It builds a
144 * composite shared-fence, and is updated as new work is submitted to the task,
145 * forming a snapshot of the current status. It should be embedded into the
146 * different resources that need to track their associated GPU activity to
147 * provide a callback when that GPU activity has ceased, or otherwise to
148 * provide a serialisation point either for request submission or for CPU
149 * synchronisation.
150 */
151
Chris Wilsonb1e31772019-10-04 14:40:00 +0100152void __i915_active_init(struct i915_active *ref,
Chris Wilson12c255b2019-06-21 19:38:00 +0100153 int (*active)(struct i915_active *ref),
154 void (*retire)(struct i915_active *ref),
Chris Wilsonae303002019-12-02 14:01:32 +0000155 struct lock_class_key *mkey,
156 struct lock_class_key *wkey);
157
158/* Specialise each class of i915_active to avoid impossible lockdep cycles. */
Chris Wilsonb1e31772019-10-04 14:40:00 +0100159#define i915_active_init(ref, active, retire) do { \
Chris Wilsonae303002019-12-02 14:01:32 +0000160 static struct lock_class_key __mkey; \
161 static struct lock_class_key __wkey; \
Chris Wilson12c255b2019-06-21 19:38:00 +0100162 \
Chris Wilsonae303002019-12-02 14:01:32 +0000163 __i915_active_init(ref, active, retire, &__mkey, &__wkey); \
Chris Wilson12c255b2019-06-21 19:38:00 +0100164} while (0)
Chris Wilson64d6c502019-02-05 13:00:02 +0000165
166int i915_active_ref(struct i915_active *ref,
Chris Wilson25ffd4b2019-08-16 13:10:00 +0100167 struct intel_timeline *tl,
Chris Wilsonb1e31772019-10-04 14:40:00 +0100168 struct dma_fence *fence);
Chris Wilson64d6c502019-02-05 13:00:02 +0000169
Chris Wilsond19d71f2019-09-19 12:19:10 +0100170static inline int
171i915_active_add_request(struct i915_active *ref, struct i915_request *rq)
172{
Chris Wilsonb1e31772019-10-04 14:40:00 +0100173 return i915_active_ref(ref, i915_request_timeline(rq), &rq->fence);
Chris Wilsond19d71f2019-09-19 12:19:10 +0100174}
175
Chris Wilson28507482019-10-04 14:39:58 +0100176void i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f);
177
178static inline bool i915_active_has_exclusive(struct i915_active *ref)
179{
Chris Wilsonb1e31772019-10-04 14:40:00 +0100180 return rcu_access_pointer(ref->excl.fence);
Chris Wilson28507482019-10-04 14:39:58 +0100181}
182
Chris Wilson64d6c502019-02-05 13:00:02 +0000183int i915_active_wait(struct i915_active *ref);
184
Chris Wilsonb1e31772019-10-04 14:40:00 +0100185int i915_request_await_active(struct i915_request *rq, struct i915_active *ref);
Chris Wilson64d6c502019-02-05 13:00:02 +0000186
Chris Wilson12c255b2019-06-21 19:38:00 +0100187int i915_active_acquire(struct i915_active *ref);
Chris Wilsonb1e31772019-10-04 14:40:00 +0100188bool i915_active_acquire_if_busy(struct i915_active *ref);
Chris Wilson64d6c502019-02-05 13:00:02 +0000189void i915_active_release(struct i915_active *ref);
Chris Wilson79c7a282019-07-25 23:38:43 +0100190
Chris Wilson5b924152020-01-27 15:28:29 +0000191static inline void __i915_active_acquire(struct i915_active *ref)
192{
193 GEM_BUG_ON(!atomic_read(&ref->count));
194 atomic_inc(&ref->count);
195}
196
Chris Wilson64d6c502019-02-05 13:00:02 +0000197static inline bool
198i915_active_is_idle(const struct i915_active *ref)
199{
Chris Wilson12c255b2019-06-21 19:38:00 +0100200 return !atomic_read(&ref->count);
Chris Wilson64d6c502019-02-05 13:00:02 +0000201}
202
Chris Wilsona42375a2019-02-05 13:00:03 +0000203#if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)
Chris Wilson64d6c502019-02-05 13:00:02 +0000204void i915_active_fini(struct i915_active *ref);
Chris Wilsona42375a2019-02-05 13:00:03 +0000205#else
206static inline void i915_active_fini(struct i915_active *ref) { }
207#endif
Chris Wilson64d6c502019-02-05 13:00:02 +0000208
Chris Wilsonce476c82019-06-14 17:46:04 +0100209int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
210 struct intel_engine_cs *engine);
211void i915_active_acquire_barrier(struct i915_active *ref);
Chris Wilsond8af05f2019-08-02 11:00:15 +0100212void i915_request_add_active_barriers(struct i915_request *rq);
Chris Wilsonce476c82019-06-14 17:46:04 +0100213
Chris Wilson164a4122019-10-31 10:11:16 +0000214void i915_active_print(struct i915_active *ref, struct drm_printer *m);
Chris Wilson38813762019-11-01 18:10:22 +0000215void i915_active_unlock_wait(struct i915_active *ref);
Chris Wilson164a4122019-10-31 10:11:16 +0000216
Chris Wilson64d6c502019-02-05 13:00:02 +0000217#endif /* _I915_ACTIVE_H_ */