2 * seqno-fence, using a dma-buf to synchronize fencing
4 * Copyright (C) 2012 Texas Instruments
5 * Copyright (C) 2012 Canonical Ltd
7 * Rob Clark <robdclark@gmail.com>
8 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 #ifndef __LINUX_SEQNO_FENCE_H
21 #define __LINUX_SEQNO_FENCE_H
23 #include <linux/fence.h>
24 #include <linux/dma-buf.h>
26 enum seqno_fence_condition
{
27 SEQNO_FENCE_WAIT_GEQUAL
,
28 SEQNO_FENCE_WAIT_NONZERO
34 const struct fence_ops
*ops
;
35 struct dma_buf
*sync_buf
;
37 enum seqno_fence_condition condition
;
40 extern const struct fence_ops seqno_fence_ops
;
43 * to_seqno_fence - cast a fence to a seqno_fence
44 * @fence: fence to cast to a seqno_fence
46 * Returns NULL if the fence is not a seqno_fence,
47 * or the seqno_fence otherwise.
49 static inline struct seqno_fence
*
50 to_seqno_fence(struct fence
*fence
)
52 if (fence
->ops
!= &seqno_fence_ops
)
54 return container_of(fence
, struct seqno_fence
, base
);
58 * seqno_fence_init - initialize a seqno fence
59 * @fence: seqno_fence to initialize
60 * @lock: pointer to spinlock to use for fence
61 * @sync_buf: buffer containing the memory location to signal on
62 * @context: the execution context this fence is a part of
63 * @seqno_ofs: the offset within @sync_buf
64 * @seqno: the sequence # to signal on
65 * @cond: fence wait condition
66 * @ops: the fence_ops for operations on this seqno fence
68 * This function initializes a struct seqno_fence with passed parameters,
69 * and takes a reference on sync_buf which is released on fence destruction.
71 * A seqno_fence is a dma_fence which can complete in software when
72 * enable_signaling is called, but it also completes when
73 * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true
75 * The seqno_fence will take a refcount on the sync_buf until it's
76 * destroyed, but actual lifetime of sync_buf may be longer if one of the
77 * callers take a reference to it.
79 * Certain hardware have instructions to insert this type of wait condition
80 * in the command stream, so no intervention from software would be needed.
81 * This type of fence can be destroyed before completed, however a reference
82 * on the sync_buf dma-buf can be taken. It is encouraged to re-use the same
83 * dma-buf for sync_buf, since mapping or unmapping the sync_buf to the
84 * device's vm can be expensive.
86 * It is recommended for creators of seqno_fence to call fence_signal
87 * before destruction. This will prevent possible issues from wraparound at
88 * time of issue vs time of check, since users can check fence_is_signaled
89 * before submitting instructions for the hardware to wait on the fence.
90 * However, when ops.enable_signaling is not called, it doesn't have to be
91 * done as soon as possible, just before there's any real danger of seqno
95 seqno_fence_init(struct seqno_fence
*fence
, spinlock_t
*lock
,
96 struct dma_buf
*sync_buf
, uint32_t context
,
97 uint32_t seqno_ofs
, uint32_t seqno
,
98 enum seqno_fence_condition cond
,
99 const struct fence_ops
*ops
)
101 BUG_ON(!fence
|| !sync_buf
|| !ops
);
102 BUG_ON(!ops
->wait
|| !ops
->enable_signaling
||
103 !ops
->get_driver_name
|| !ops
->get_timeline_name
);
106 * ops is used in fence_init for get_driver_name, so needs to be
110 fence_init(&fence
->base
, &seqno_fence_ops
, lock
, context
, seqno
);
111 get_dma_buf(sync_buf
);
112 fence
->sync_buf
= sync_buf
;
113 fence
->seqno_ofs
= seqno_ofs
;
114 fence
->condition
= cond
;
117 #endif /* __LINUX_SEQNO_FENCE_H */