Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 2 | /* |
| 3 | * seqno-fence, using a dma-buf to synchronize fencing |
| 4 | * |
| 5 | * Copyright (C) 2012 Texas Instruments |
| 6 | * Copyright (C) 2012 Canonical Ltd |
| 7 | * Authors: |
| 8 | * Rob Clark <robdclark@gmail.com> |
| 9 | * Maarten Lankhorst <maarten.lankhorst@canonical.com> |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef __LINUX_SEQNO_FENCE_H |
| 13 | #define __LINUX_SEQNO_FENCE_H |
| 14 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 15 | #include <linux/dma-fence.h> |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 16 | #include <linux/dma-buf.h> |
| 17 | |
| 18 | enum seqno_fence_condition { |
| 19 | SEQNO_FENCE_WAIT_GEQUAL, |
| 20 | SEQNO_FENCE_WAIT_NONZERO |
| 21 | }; |
| 22 | |
| 23 | struct seqno_fence { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 24 | struct dma_fence base; |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 25 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 26 | const struct dma_fence_ops *ops; |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 27 | struct dma_buf *sync_buf; |
| 28 | uint32_t seqno_ofs; |
| 29 | enum seqno_fence_condition condition; |
| 30 | }; |
| 31 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 32 | extern const struct dma_fence_ops seqno_fence_ops; |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * to_seqno_fence - cast a fence to a seqno_fence |
| 36 | * @fence: fence to cast to a seqno_fence |
| 37 | * |
| 38 | * Returns NULL if the fence is not a seqno_fence, |
| 39 | * or the seqno_fence otherwise. |
| 40 | */ |
| 41 | static inline struct seqno_fence * |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 42 | to_seqno_fence(struct dma_fence *fence) |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 43 | { |
| 44 | if (fence->ops != &seqno_fence_ops) |
| 45 | return NULL; |
| 46 | return container_of(fence, struct seqno_fence, base); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * seqno_fence_init - initialize a seqno fence |
| 51 | * @fence: seqno_fence to initialize |
| 52 | * @lock: pointer to spinlock to use for fence |
| 53 | * @sync_buf: buffer containing the memory location to signal on |
| 54 | * @context: the execution context this fence is a part of |
| 55 | * @seqno_ofs: the offset within @sync_buf |
| 56 | * @seqno: the sequence # to signal on |
Thierry Reding | 1f58d94 | 2014-08-08 13:06:30 +0200 | [diff] [blame] | 57 | * @cond: fence wait condition |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 58 | * @ops: the fence_ops for operations on this seqno fence |
| 59 | * |
| 60 | * This function initializes a struct seqno_fence with passed parameters, |
| 61 | * and takes a reference on sync_buf which is released on fence destruction. |
| 62 | * |
| 63 | * A seqno_fence is a dma_fence which can complete in software when |
| 64 | * enable_signaling is called, but it also completes when |
| 65 | * (s32)((sync_buf)[seqno_ofs] - seqno) >= 0 is true |
| 66 | * |
| 67 | * The seqno_fence will take a refcount on the sync_buf until it's |
| 68 | * destroyed, but actual lifetime of sync_buf may be longer if one of the |
| 69 | * callers take a reference to it. |
| 70 | * |
| 71 | * Certain hardware have instructions to insert this type of wait condition |
| 72 | * in the command stream, so no intervention from software would be needed. |
| 73 | * This type of fence can be destroyed before completed, however a reference |
| 74 | * on the sync_buf dma-buf can be taken. It is encouraged to re-use the same |
| 75 | * dma-buf for sync_buf, since mapping or unmapping the sync_buf to the |
| 76 | * device's vm can be expensive. |
| 77 | * |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 78 | * It is recommended for creators of seqno_fence to call dma_fence_signal() |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 79 | * before destruction. This will prevent possible issues from wraparound at |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 80 | * time of issue vs time of check, since users can check dma_fence_is_signaled() |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 81 | * before submitting instructions for the hardware to wait on the fence. |
| 82 | * However, when ops.enable_signaling is not called, it doesn't have to be |
| 83 | * done as soon as possible, just before there's any real danger of seqno |
| 84 | * wraparound. |
| 85 | */ |
| 86 | static inline void |
| 87 | seqno_fence_init(struct seqno_fence *fence, spinlock_t *lock, |
| 88 | struct dma_buf *sync_buf, uint32_t context, |
| 89 | uint32_t seqno_ofs, uint32_t seqno, |
| 90 | enum seqno_fence_condition cond, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 91 | const struct dma_fence_ops *ops) |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 92 | { |
| 93 | BUG_ON(!fence || !sync_buf || !ops); |
| 94 | BUG_ON(!ops->wait || !ops->enable_signaling || |
| 95 | !ops->get_driver_name || !ops->get_timeline_name); |
| 96 | |
| 97 | /* |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 98 | * ops is used in dma_fence_init for get_driver_name, so needs to be |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 99 | * initialized first |
| 100 | */ |
| 101 | fence->ops = ops; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 102 | dma_fence_init(&fence->base, &seqno_fence_ops, lock, context, seqno); |
Maarten Lankhorst | 606b23a | 2014-07-01 12:57:20 +0200 | [diff] [blame] | 103 | get_dma_buf(sync_buf); |
| 104 | fence->sync_buf = sync_buf; |
| 105 | fence->seqno_ofs = seqno_ofs; |
| 106 | fence->condition = cond; |
| 107 | } |
| 108 | |
| 109 | #endif /* __LINUX_SEQNO_FENCE_H */ |