Home | History | Annotate | Line # | Download | only in i915
      1  1.3  riastrad /*	$NetBSD: i915_active.h,v 1.3 2021/12/19 11:59:04 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * SPDX-License-Identifier: MIT
      5  1.1  riastrad  *
      6  1.1  riastrad  * Copyright  2019 Intel Corporation
      7  1.1  riastrad  */
      8  1.1  riastrad 
      9  1.1  riastrad #ifndef _I915_ACTIVE_H_
     10  1.1  riastrad #define _I915_ACTIVE_H_
     11  1.1  riastrad 
     12  1.1  riastrad #include <linux/lockdep.h>
     13  1.1  riastrad 
     14  1.1  riastrad #include "i915_active_types.h"
     15  1.1  riastrad #include "i915_request.h"
     16  1.1  riastrad 
     17  1.1  riastrad struct i915_request;
     18  1.1  riastrad struct intel_engine_cs;
     19  1.1  riastrad struct intel_timeline;
     20  1.1  riastrad 
     21  1.1  riastrad /*
     22  1.1  riastrad  * We treat requests as fences. This is not be to confused with our
     23  1.1  riastrad  * "fence registers" but pipeline synchronisation objects ala GL_ARB_sync.
     24  1.1  riastrad  * We use the fences to synchronize access from the CPU with activity on the
     25  1.1  riastrad  * GPU, for example, we should not rewrite an object's PTE whilst the GPU
     26  1.1  riastrad  * is reading them. We also track fences at a higher level to provide
     27  1.1  riastrad  * implicit synchronisation around GEM objects, e.g. set-domain will wait
     28  1.1  riastrad  * for outstanding GPU rendering before marking the object ready for CPU
     29  1.1  riastrad  * access, or a pageflip will wait until the GPU is complete before showing
     30  1.1  riastrad  * the frame on the scanout.
     31  1.1  riastrad  *
     32  1.1  riastrad  * In order to use a fence, the object must track the fence it needs to
     33  1.1  riastrad  * serialise with. For example, GEM objects want to track both read and
     34  1.1  riastrad  * write access so that we can perform concurrent read operations between
     35  1.1  riastrad  * the CPU and GPU engines, as well as waiting for all rendering to
     36  1.1  riastrad  * complete, or waiting for the last GPU user of a "fence register". The
     37  1.1  riastrad  * object then embeds a #i915_active_fence to track the most recent (in
     38  1.1  riastrad  * retirement order) request relevant for the desired mode of access.
     39  1.1  riastrad  * The #i915_active_fence is updated with i915_active_fence_set() to
     40  1.1  riastrad  * track the most recent fence request, typically this is done as part of
     41  1.1  riastrad  * i915_vma_move_to_active().
     42  1.1  riastrad  *
     43  1.1  riastrad  * When the #i915_active_fence completes (is retired), it will
     44  1.1  riastrad  * signal its completion to the owner through a callback as well as mark
     45  1.1  riastrad  * itself as idle (i915_active_fence.request == NULL). The owner
     46  1.1  riastrad  * can then perform any action, such as delayed freeing of an active
     47  1.1  riastrad  * resource including itself.
     48  1.1  riastrad  */
     49  1.1  riastrad 
     50  1.1  riastrad void i915_active_noop(struct dma_fence *fence, struct dma_fence_cb *cb);
     51  1.1  riastrad 
     52  1.1  riastrad /**
     53  1.1  riastrad  * __i915_active_fence_init - prepares the activity tracker for use
     54  1.1  riastrad  * @active - the active tracker
     55  1.1  riastrad  * @fence - initial fence to track, can be NULL
     56  1.1  riastrad  * @func - a callback when then the tracker is retired (becomes idle),
     57  1.1  riastrad  *         can be NULL
     58  1.1  riastrad  *
     59  1.1  riastrad  * i915_active_fence_init() prepares the embedded @active struct for use as
     60  1.1  riastrad  * an activity tracker, that is for tracking the last known active fence
     61  1.1  riastrad  * associated with it. When the last fence becomes idle, when it is retired
     62  1.1  riastrad  * after completion, the optional callback @func is invoked.
     63  1.1  riastrad  */
     64  1.1  riastrad static inline void
     65  1.1  riastrad __i915_active_fence_init(struct i915_active_fence *active,
     66  1.1  riastrad 			 void *fence,
     67  1.1  riastrad 			 dma_fence_func_t fn)
     68  1.1  riastrad {
     69  1.1  riastrad 	RCU_INIT_POINTER(active->fence, fence);
     70  1.1  riastrad 	active->cb.func = fn ?: i915_active_noop;
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad #define INIT_ACTIVE_FENCE(A) \
     74  1.1  riastrad 	__i915_active_fence_init((A), NULL, NULL)
     75  1.1  riastrad 
     76  1.1  riastrad struct dma_fence *
     77  1.1  riastrad __i915_active_fence_set(struct i915_active_fence *active,
     78  1.1  riastrad 			struct dma_fence *fence);
     79  1.1  riastrad 
     80  1.1  riastrad /**
     81  1.1  riastrad  * i915_active_fence_set - updates the tracker to watch the current fence
     82  1.1  riastrad  * @active - the active tracker
     83  1.1  riastrad  * @rq - the request to watch
     84  1.1  riastrad  *
     85  1.1  riastrad  * i915_active_fence_set() watches the given @rq for completion. While
     86  1.1  riastrad  * that @rq is busy, the @active reports busy. When that @rq is signaled
     87  1.1  riastrad  * (or else retired) the @active tracker is updated to report idle.
     88  1.1  riastrad  */
     89  1.1  riastrad int __must_check
     90  1.1  riastrad i915_active_fence_set(struct i915_active_fence *active,
     91  1.1  riastrad 		      struct i915_request *rq);
     92  1.1  riastrad /**
     93  1.1  riastrad  * i915_active_fence_get - return a reference to the active fence
     94  1.1  riastrad  * @active - the active tracker
     95  1.1  riastrad  *
     96  1.1  riastrad  * i915_active_fence_get() returns a reference to the active fence,
     97  1.1  riastrad  * or NULL if the active tracker is idle. The reference is obtained under RCU,
     98  1.1  riastrad  * so no locking is required by the caller.
     99  1.1  riastrad  *
    100  1.1  riastrad  * The reference should be freed with dma_fence_put().
    101  1.1  riastrad  */
    102  1.1  riastrad static inline struct dma_fence *
    103  1.1  riastrad i915_active_fence_get(struct i915_active_fence *active)
    104  1.1  riastrad {
    105  1.1  riastrad 	struct dma_fence *fence;
    106  1.1  riastrad 
    107  1.1  riastrad 	rcu_read_lock();
    108  1.1  riastrad 	fence = dma_fence_get_rcu_safe(&active->fence);
    109  1.1  riastrad 	rcu_read_unlock();
    110  1.1  riastrad 
    111  1.1  riastrad 	return fence;
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad /**
    115  1.1  riastrad  * i915_active_fence_isset - report whether the active tracker is assigned
    116  1.1  riastrad  * @active - the active tracker
    117  1.1  riastrad  *
    118  1.1  riastrad  * i915_active_fence_isset() returns true if the active tracker is currently
    119  1.1  riastrad  * assigned to a fence. Due to the lazy retiring, that fence may be idle
    120  1.1  riastrad  * and this may report stale information.
    121  1.1  riastrad  */
    122  1.1  riastrad static inline bool
    123  1.1  riastrad i915_active_fence_isset(const struct i915_active_fence *active)
    124  1.1  riastrad {
    125  1.1  riastrad 	return rcu_access_pointer(active->fence);
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad /*
    129  1.1  riastrad  * GPU activity tracking
    130  1.1  riastrad  *
    131  1.1  riastrad  * Each set of commands submitted to the GPU compromises a single request that
    132  1.1  riastrad  * signals a fence upon completion. struct i915_request combines the
    133  1.1  riastrad  * command submission, scheduling and fence signaling roles. If we want to see
    134  1.1  riastrad  * if a particular task is complete, we need to grab the fence (struct
    135  1.1  riastrad  * i915_request) for that task and check or wait for it to be signaled. More
    136  1.1  riastrad  * often though we want to track the status of a bunch of tasks, for example
    137  1.1  riastrad  * to wait for the GPU to finish accessing some memory across a variety of
    138  1.1  riastrad  * different command pipelines from different clients. We could choose to
    139  1.1  riastrad  * track every single request associated with the task, but knowing that
    140  1.1  riastrad  * each request belongs to an ordered timeline (later requests within a
    141  1.1  riastrad  * timeline must wait for earlier requests), we need only track the
    142  1.1  riastrad  * latest request in each timeline to determine the overall status of the
    143  1.1  riastrad  * task.
    144  1.1  riastrad  *
    145  1.1  riastrad  * struct i915_active provides this tracking across timelines. It builds a
    146  1.1  riastrad  * composite shared-fence, and is updated as new work is submitted to the task,
    147  1.1  riastrad  * forming a snapshot of the current status. It should be embedded into the
    148  1.1  riastrad  * different resources that need to track their associated GPU activity to
    149  1.1  riastrad  * provide a callback when that GPU activity has ceased, or otherwise to
    150  1.1  riastrad  * provide a serialisation point either for request submission or for CPU
    151  1.1  riastrad  * synchronisation.
    152  1.1  riastrad  */
    153  1.1  riastrad 
    154  1.1  riastrad void __i915_active_init(struct i915_active *ref,
    155  1.1  riastrad 			int (*active)(struct i915_active *ref),
    156  1.1  riastrad 			void (*retire)(struct i915_active *ref),
    157  1.1  riastrad 			struct lock_class_key *mkey,
    158  1.1  riastrad 			struct lock_class_key *wkey);
    159  1.1  riastrad 
    160  1.1  riastrad /* Specialise each class of i915_active to avoid impossible lockdep cycles. */
    161  1.1  riastrad #define i915_active_init(ref, active, retire) do {		\
    162  1.1  riastrad 	static struct lock_class_key __mkey;				\
    163  1.1  riastrad 	static struct lock_class_key __wkey;				\
    164  1.1  riastrad 									\
    165  1.1  riastrad 	__i915_active_init(ref, active, retire, &__mkey, &__wkey);	\
    166  1.1  riastrad } while (0)
    167  1.1  riastrad 
    168  1.1  riastrad int i915_active_ref(struct i915_active *ref,
    169  1.1  riastrad 		    struct intel_timeline *tl,
    170  1.1  riastrad 		    struct dma_fence *fence);
    171  1.1  riastrad 
    172  1.1  riastrad static inline int
    173  1.1  riastrad i915_active_add_request(struct i915_active *ref, struct i915_request *rq)
    174  1.1  riastrad {
    175  1.1  riastrad 	return i915_active_ref(ref, i915_request_timeline(rq), &rq->fence);
    176  1.1  riastrad }
    177  1.1  riastrad 
    178  1.1  riastrad void i915_active_set_exclusive(struct i915_active *ref, struct dma_fence *f);
    179  1.1  riastrad 
    180  1.1  riastrad static inline bool i915_active_has_exclusive(struct i915_active *ref)
    181  1.1  riastrad {
    182  1.1  riastrad 	return rcu_access_pointer(ref->excl.fence);
    183  1.1  riastrad }
    184  1.1  riastrad 
    185  1.1  riastrad int i915_active_wait(struct i915_active *ref);
    186  1.1  riastrad 
    187  1.1  riastrad int i915_request_await_active(struct i915_request *rq, struct i915_active *ref);
    188  1.1  riastrad 
    189  1.1  riastrad int i915_active_acquire(struct i915_active *ref);
    190  1.1  riastrad bool i915_active_acquire_if_busy(struct i915_active *ref);
    191  1.1  riastrad void i915_active_release(struct i915_active *ref);
    192  1.1  riastrad 
    193  1.1  riastrad static inline void __i915_active_acquire(struct i915_active *ref)
    194  1.1  riastrad {
    195  1.1  riastrad 	GEM_BUG_ON(!atomic_read(&ref->count));
    196  1.1  riastrad 	atomic_inc(&ref->count);
    197  1.1  riastrad }
    198  1.1  riastrad 
    199  1.1  riastrad static inline bool
    200  1.1  riastrad i915_active_is_idle(const struct i915_active *ref)
    201  1.1  riastrad {
    202  1.1  riastrad 	return !atomic_read(&ref->count);
    203  1.1  riastrad }
    204  1.1  riastrad 
    205  1.1  riastrad void i915_active_fini(struct i915_active *ref);
    206  1.1  riastrad 
    207  1.1  riastrad int i915_active_acquire_preallocate_barrier(struct i915_active *ref,
    208  1.1  riastrad 					    struct intel_engine_cs *engine);
    209  1.1  riastrad void i915_active_acquire_barrier(struct i915_active *ref);
    210  1.1  riastrad void i915_request_add_active_barriers(struct i915_request *rq);
    211  1.1  riastrad 
    212  1.1  riastrad void i915_active_print(struct i915_active *ref, struct drm_printer *m);
    213  1.1  riastrad void i915_active_unlock_wait(struct i915_active *ref);
    214  1.1  riastrad 
    215  1.1  riastrad #endif /* _I915_ACTIVE_H_ */
    216