1 /* 2 * Copyright (c) 2019 Jonathan Gray <jsg@openbsd.org> 3 * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 #ifndef _LINUX_DMA_FENCE_H_ 26 #define _LINUX_DMA_FENCE_H_ 27 28 #include <linux/err.h> 29 #include <linux/wait.h> 30 #include <linux/list.h> 31 #include <linux/bitops.h> 32 #include <linux/kref.h> 33 #include <linux/sched.h> 34 #include <linux/printk.h> 35 #include <linux/rcupdate.h> 36 37 #define DMA_FENCE_TRACE(fence, fmt, args...) do {} while(0) 38 39 struct dma_fence_cb; 40 41 struct dma_fence { 42 struct kref refcount; 43 struct lock *lock; 44 const struct dma_fence_ops *ops; 45 struct rcu_head rcu; 46 struct list_head cb_list; 47 u64 context; 48 unsigned seqno; 49 unsigned long flags; 50 ktime_t timestamp; 51 int error; 52 }; 53 54 enum dma_fence_flag_bits { 55 DMA_FENCE_FLAG_SIGNALED_BIT, 56 DMA_FENCE_FLAG_TIMESTAMP_BIT, 57 DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, 58 DMA_FENCE_FLAG_USER_BITS, /* must always be last member */ 59 }; 60 61 typedef void (*dma_fence_func_t)(struct dma_fence *fence, 62 struct dma_fence_cb *cb); 63 64 struct dma_fence_cb { 65 struct list_head node; 66 dma_fence_func_t func; 67 }; 68 69 struct dma_fence_ops { 70 const char * (*get_driver_name)(struct dma_fence *fence); 71 const char * (*get_timeline_name)(struct dma_fence *fence); 72 bool (*enable_signaling)(struct dma_fence *fence); 73 bool (*signaled)(struct dma_fence *fence); 74 signed long (*wait)(struct dma_fence *fence, 75 bool intr, signed long timeout); 76 void (*release)(struct dma_fence *fence); 77 78 int (*fill_driver_data)(struct dma_fence *fence, void *data, int size); 79 void (*fence_value_str)(struct dma_fence *fence, char *str, int size); 80 void (*timeline_value_str)(struct dma_fence *fence, 81 char *str, int size); 82 }; 83 84 void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, 85 spinlock_t *lock, u64 context, unsigned seqno); 86 87 void dma_fence_release(struct kref *kref); 88 89 static inline struct dma_fence * 90 dma_fence_get(struct dma_fence *fence) 91 { 92 if (fence) 93 kref_get(&fence->refcount); 94 return fence; 95 } 96 97 static inline struct dma_fence * 98 dma_fence_get_rcu(struct dma_fence *fence) 99 { 100 if (fence) 101 kref_get(&fence->refcount); 102 return fence; 103 } 104 105 static inline void 106 dma_fence_put(struct dma_fence *fence) 107 { 108 if (fence) 109 kref_put(&fence->refcount, dma_fence_release); 110 } 111 112 int dma_fence_signal(struct dma_fence *fence); 113 int dma_fence_signal_locked(struct dma_fence *fence); 114 115 static inline bool 116 dma_fence_is_signaled(struct dma_fence *fence) 117 { 118 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 119 return true; 120 121 if (fence->ops->signaled && fence->ops->signaled(fence)) { 122 dma_fence_signal(fence); 123 return true; 124 } 125 126 return false; 127 } 128 129 void dma_fence_enable_sw_signaling(struct dma_fence *fence); 130 131 signed long dma_fence_default_wait(struct dma_fence *fence, 132 bool intr, signed long timeout); 133 signed long dma_fence_wait_timeout(struct dma_fence *, 134 bool intr, signed long timeout); 135 136 static inline signed long 137 dma_fence_wait(struct dma_fence *fence, bool intr) 138 { 139 signed long ret; 140 141 ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT); 142 143 if (ret < 0) 144 return ret; 145 146 return 0; 147 } 148 149 int dma_fence_add_callback(struct dma_fence *fence, 150 struct dma_fence_cb *cb, 151 dma_fence_func_t func); 152 153 bool dma_fence_remove_callback(struct dma_fence *fence, 154 struct dma_fence_cb *cb); 155 156 u64 dma_fence_context_alloc(unsigned num); 157 158 static inline bool 159 dma_fence_is_later(struct dma_fence *a, struct dma_fence *b) 160 { 161 return (a->seqno > b->seqno); 162 } 163 164 static inline void 165 dma_fence_set_error(struct dma_fence *fence, int error) 166 { 167 fence->error = error; 168 } 169 170 static inline struct dma_fence * 171 dma_fence_get_rcu_safe(struct dma_fence **dfp) 172 { 173 struct dma_fence *fence; 174 if (dfp == NULL) 175 return NULL; 176 fence = *dfp; 177 if (fence) 178 kref_get(&fence->refcount); 179 return fence; 180 } 181 182 void dma_fence_free(struct dma_fence *fence); 183 184 #endif /* _LINUX_DMA_FENCE_H_ */ 185