| Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Virtual DMA channel support for DMAengine |
| 4 | * |
| 5 | * Copyright (C) 2012 Russell King |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 6 | */ |
| 7 | #ifndef VIRT_DMA_H |
| 8 | #define VIRT_DMA_H |
| 9 | |
| 10 | #include <linux/dmaengine.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | |
| 13 | #include "dmaengine.h" |
| 14 | |
| 15 | struct virt_dma_desc { |
| 16 | struct dma_async_tx_descriptor tx; |
| 17 | /* protected by vc.lock */ |
| 18 | struct list_head node; |
| 19 | }; |
| 20 | |
| 21 | struct virt_dma_chan { |
| 22 | struct dma_chan chan; |
| 23 | struct tasklet_struct task; |
| 24 | void (*desc_free)(struct virt_dma_desc *); |
| 25 | |
| 26 | spinlock_t lock; |
| 27 | |
| 28 | /* protected by vc.lock */ |
| Robert Jarzmik | 13bb26a | 2015-10-13 21:54:28 +0200 | [diff] [blame] | 29 | struct list_head desc_allocated; |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 30 | struct list_head desc_submitted; |
| 31 | struct list_head desc_issued; |
| 32 | struct list_head desc_completed; |
| Russell King | 571fa74 | 2012-05-14 15:17:20 +0100 | [diff] [blame] | 33 | |
| 34 | struct virt_dma_desc *cyclic; |
| Peter Ujfalusi | 1c7f072 | 2017-11-14 16:32:04 +0200 | [diff] [blame] | 35 | struct virt_dma_desc *vd_terminated; |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static inline struct virt_dma_chan *to_virt_chan(struct dma_chan *chan) |
| 39 | { |
| 40 | return container_of(chan, struct virt_dma_chan, chan); |
| 41 | } |
| 42 | |
| 43 | void vchan_dma_desc_free_list(struct virt_dma_chan *vc, struct list_head *head); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 44 | void vchan_init(struct virt_dma_chan *vc, struct dma_device *dmadev); |
| Russell King | fe04587 | 2012-05-10 23:39:27 +0100 | [diff] [blame] | 45 | struct virt_dma_desc *vchan_find_desc(struct virt_dma_chan *, dma_cookie_t); |
| Baoyou Xie | 02aa848 | 2016-09-24 12:37:05 +0800 | [diff] [blame] | 46 | extern dma_cookie_t vchan_tx_submit(struct dma_async_tx_descriptor *); |
| 47 | extern int vchan_tx_desc_free(struct dma_async_tx_descriptor *); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * vchan_tx_prep - prepare a descriptor |
| Lars-Peter Clausen | 28ca3e8 | 2015-10-20 13:14:45 +0200 | [diff] [blame] | 51 | * @vc: virtual channel allocating this descriptor |
| 52 | * @vd: virtual descriptor to prepare |
| 53 | * @tx_flags: flags argument passed in to prepare function |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 54 | */ |
| 55 | static inline struct dma_async_tx_descriptor *vchan_tx_prep(struct virt_dma_chan *vc, |
| 56 | struct virt_dma_desc *vd, unsigned long tx_flags) |
| 57 | { |
| Robert Jarzmik | 13bb26a | 2015-10-13 21:54:28 +0200 | [diff] [blame] | 58 | unsigned long flags; |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 59 | |
| 60 | dma_async_tx_descriptor_init(&vd->tx, &vc->chan); |
| 61 | vd->tx.flags = tx_flags; |
| 62 | vd->tx.tx_submit = vchan_tx_submit; |
| Robert Jarzmik | 13bb26a | 2015-10-13 21:54:28 +0200 | [diff] [blame] | 63 | vd->tx.desc_free = vchan_tx_desc_free; |
| 64 | |
| 65 | spin_lock_irqsave(&vc->lock, flags); |
| 66 | list_add_tail(&vd->node, &vc->desc_allocated); |
| 67 | spin_unlock_irqrestore(&vc->lock, flags); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 68 | |
| 69 | return &vd->tx; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * vchan_issue_pending - move submitted descriptors to issued list |
| Lars-Peter Clausen | 28ca3e8 | 2015-10-20 13:14:45 +0200 | [diff] [blame] | 74 | * @vc: virtual channel to update |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 75 | * |
| 76 | * vc.lock must be held by caller |
| 77 | */ |
| 78 | static inline bool vchan_issue_pending(struct virt_dma_chan *vc) |
| 79 | { |
| 80 | list_splice_tail_init(&vc->desc_submitted, &vc->desc_issued); |
| 81 | return !list_empty(&vc->desc_issued); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * vchan_cookie_complete - report completion of a descriptor |
| Lars-Peter Clausen | 28ca3e8 | 2015-10-20 13:14:45 +0200 | [diff] [blame] | 86 | * @vd: virtual descriptor to update |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 87 | * |
| 88 | * vc.lock must be held by caller |
| 89 | */ |
| 90 | static inline void vchan_cookie_complete(struct virt_dma_desc *vd) |
| 91 | { |
| 92 | struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan); |
| Jonas Jensen | af58652 | 2013-12-06 16:42:09 +0100 | [diff] [blame] | 93 | dma_cookie_t cookie; |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 94 | |
| Jonas Jensen | af58652 | 2013-12-06 16:42:09 +0100 | [diff] [blame] | 95 | cookie = vd->tx.cookie; |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 96 | dma_cookie_complete(&vd->tx); |
| 97 | dev_vdbg(vc->chan.device->dev, "txd %p[%x]: marked complete\n", |
| Jonas Jensen | af58652 | 2013-12-06 16:42:09 +0100 | [diff] [blame] | 98 | vd, cookie); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 99 | list_add_tail(&vd->node, &vc->desc_completed); |
| 100 | |
| 101 | tasklet_schedule(&vc->task); |
| 102 | } |
| 103 | |
| 104 | /** |
| Peter Ujfalusi | 6af149d | 2017-11-14 16:32:03 +0200 | [diff] [blame] | 105 | * vchan_vdesc_fini - Free or reuse a descriptor |
| 106 | * @vd: virtual descriptor to free/reuse |
| 107 | */ |
| 108 | static inline void vchan_vdesc_fini(struct virt_dma_desc *vd) |
| 109 | { |
| 110 | struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan); |
| 111 | |
| 112 | if (dmaengine_desc_test_reuse(&vd->tx)) |
| 113 | list_add(&vd->node, &vc->desc_allocated); |
| 114 | else |
| 115 | vc->desc_free(vd); |
| 116 | } |
| 117 | |
| 118 | /** |
| Russell King | 571fa74 | 2012-05-14 15:17:20 +0100 | [diff] [blame] | 119 | * vchan_cyclic_callback - report the completion of a period |
| Lars-Peter Clausen | 28ca3e8 | 2015-10-20 13:14:45 +0200 | [diff] [blame] | 120 | * @vd: virtual descriptor |
| Russell King | 571fa74 | 2012-05-14 15:17:20 +0100 | [diff] [blame] | 121 | */ |
| 122 | static inline void vchan_cyclic_callback(struct virt_dma_desc *vd) |
| 123 | { |
| 124 | struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan); |
| 125 | |
| 126 | vc->cyclic = vd; |
| 127 | tasklet_schedule(&vc->task); |
| 128 | } |
| 129 | |
| 130 | /** |
| Peter Ujfalusi | 1c7f072 | 2017-11-14 16:32:04 +0200 | [diff] [blame] | 131 | * vchan_terminate_vdesc - Disable pending cyclic callback |
| 132 | * @vd: virtual descriptor to be terminated |
| 133 | * |
| 134 | * vc.lock must be held by caller |
| 135 | */ |
| 136 | static inline void vchan_terminate_vdesc(struct virt_dma_desc *vd) |
| 137 | { |
| 138 | struct virt_dma_chan *vc = to_virt_chan(vd->tx.chan); |
| 139 | |
| 140 | /* free up stuck descriptor */ |
| 141 | if (vc->vd_terminated) |
| 142 | vchan_vdesc_fini(vc->vd_terminated); |
| 143 | |
| 144 | vc->vd_terminated = vd; |
| 145 | if (vc->cyclic == vd) |
| 146 | vc->cyclic = NULL; |
| 147 | } |
| 148 | |
| 149 | /** |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 150 | * vchan_next_desc - peek at the next descriptor to be processed |
| Lars-Peter Clausen | 28ca3e8 | 2015-10-20 13:14:45 +0200 | [diff] [blame] | 151 | * @vc: virtual channel to obtain descriptor from |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 152 | * |
| 153 | * vc.lock must be held by caller |
| 154 | */ |
| 155 | static inline struct virt_dma_desc *vchan_next_desc(struct virt_dma_chan *vc) |
| 156 | { |
| Masahiro Yamada | 360af35 | 2016-09-13 03:08:17 +0900 | [diff] [blame] | 157 | return list_first_entry_or_null(&vc->desc_issued, |
| 158 | struct virt_dma_desc, node); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| Jun Nie | 8c8fe97 | 2015-07-10 20:02:49 +0800 | [diff] [blame] | 162 | * vchan_get_all_descriptors - obtain all submitted and issued descriptors |
| Lars-Peter Clausen | 28ca3e8 | 2015-10-20 13:14:45 +0200 | [diff] [blame] | 163 | * @vc: virtual channel to get descriptors from |
| 164 | * @head: list of descriptors found |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 165 | * |
| 166 | * vc.lock must be held by caller |
| 167 | * |
| 168 | * Removes all submitted and issued descriptors from internal lists, and |
| 169 | * provides a list of all descriptors found |
| 170 | */ |
| 171 | static inline void vchan_get_all_descriptors(struct virt_dma_chan *vc, |
| 172 | struct list_head *head) |
| 173 | { |
| Robert Jarzmik | 13bb26a | 2015-10-13 21:54:28 +0200 | [diff] [blame] | 174 | list_splice_tail_init(&vc->desc_allocated, head); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 175 | list_splice_tail_init(&vc->desc_submitted, head); |
| 176 | list_splice_tail_init(&vc->desc_issued, head); |
| 177 | list_splice_tail_init(&vc->desc_completed, head); |
| 178 | } |
| 179 | |
| 180 | static inline void vchan_free_chan_resources(struct virt_dma_chan *vc) |
| 181 | { |
| Robert Jarzmik | 13bb26a | 2015-10-13 21:54:28 +0200 | [diff] [blame] | 182 | struct virt_dma_desc *vd; |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 183 | unsigned long flags; |
| 184 | LIST_HEAD(head); |
| 185 | |
| 186 | spin_lock_irqsave(&vc->lock, flags); |
| 187 | vchan_get_all_descriptors(vc, &head); |
| Robert Jarzmik | 13bb26a | 2015-10-13 21:54:28 +0200 | [diff] [blame] | 188 | list_for_each_entry(vd, &head, node) |
| 189 | dmaengine_desc_clear_reuse(&vd->tx); |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 190 | spin_unlock_irqrestore(&vc->lock, flags); |
| 191 | |
| 192 | vchan_dma_desc_free_list(vc, &head); |
| 193 | } |
| 194 | |
| Lars-Peter Clausen | 2ed0862 | 2015-10-20 11:46:29 +0200 | [diff] [blame] | 195 | /** |
| 196 | * vchan_synchronize() - synchronize callback execution to the current context |
| 197 | * @vc: virtual channel to synchronize |
| 198 | * |
| 199 | * Makes sure that all scheduled or active callbacks have finished running. For |
| 200 | * proper operation the caller has to ensure that no new callbacks are scheduled |
| 201 | * after the invocation of this function started. |
| Peter Ujfalusi | 1c7f072 | 2017-11-14 16:32:04 +0200 | [diff] [blame] | 202 | * Free up the terminated cyclic descriptor to prevent memory leakage. |
| Lars-Peter Clausen | 2ed0862 | 2015-10-20 11:46:29 +0200 | [diff] [blame] | 203 | */ |
| 204 | static inline void vchan_synchronize(struct virt_dma_chan *vc) |
| 205 | { |
| Peter Ujfalusi | 1c7f072 | 2017-11-14 16:32:04 +0200 | [diff] [blame] | 206 | unsigned long flags; |
| 207 | |
| Lars-Peter Clausen | 2ed0862 | 2015-10-20 11:46:29 +0200 | [diff] [blame] | 208 | tasklet_kill(&vc->task); |
| Peter Ujfalusi | 1c7f072 | 2017-11-14 16:32:04 +0200 | [diff] [blame] | 209 | |
| 210 | spin_lock_irqsave(&vc->lock, flags); |
| 211 | if (vc->vd_terminated) { |
| 212 | vchan_vdesc_fini(vc->vd_terminated); |
| 213 | vc->vd_terminated = NULL; |
| 214 | } |
| 215 | spin_unlock_irqrestore(&vc->lock, flags); |
| Lars-Peter Clausen | 2ed0862 | 2015-10-20 11:46:29 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| Russell King | 50437bf | 2012-04-13 12:07:23 +0100 | [diff] [blame] | 218 | #endif |