Peter Hurley | e571107 | 2012-11-02 08:16:33 -0400 | [diff] [blame] | 1 | /* |
| 2 | * DMA-able FIFO implementation |
| 3 | * |
| 4 | * Copyright (C) 2012 Peter Hurley <peter@hurleysoftware.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software Foundation, |
| 18 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/bug.h> |
| 25 | |
| 26 | #include "dma_fifo.h" |
| 27 | |
| 28 | #ifdef DEBUG_TRACING |
| 29 | #define df_trace(s, args...) pr_debug(s, ##args) |
| 30 | #else |
| 31 | #define df_trace(s, args...) |
| 32 | #endif |
| 33 | |
| 34 | #define FAIL(fifo, condition, format...) ({ \ |
| 35 | fifo->corrupt = !!(condition); \ |
Peter Hurley | 1b94242 | 2012-11-27 09:30:45 -0500 | [diff] [blame] | 36 | WARN(fifo->corrupt, format); \ |
Peter Hurley | e571107 | 2012-11-02 08:16:33 -0400 | [diff] [blame] | 37 | }) |
| 38 | |
| 39 | /* |
| 40 | * private helper fn to determine if check is in open interval (lo,hi) |
| 41 | */ |
| 42 | static bool addr_check(unsigned check, unsigned lo, unsigned hi) |
| 43 | { |
| 44 | return check - (lo + 1) < (hi - 1) - lo; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * dma_fifo_init: initialize the fifo to a valid but inoperative state |
| 49 | * @fifo: address of in-place "struct dma_fifo" object |
| 50 | */ |
| 51 | void dma_fifo_init(struct dma_fifo *fifo) |
| 52 | { |
| 53 | memset(fifo, 0, sizeof(*fifo)); |
| 54 | INIT_LIST_HEAD(&fifo->pending); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * dma_fifo_alloc - initialize and allocate dma_fifo |
| 59 | * @fifo: address of in-place "struct dma_fifo" object |
| 60 | * @size: 'apparent' size, in bytes, of fifo |
| 61 | * @align: dma alignment to maintain (should be at least cpu cache alignment), |
| 62 | * must be power of 2 |
| 63 | * @tx_limit: maximum # of bytes transmissable per dma (rounded down to |
| 64 | * multiple of alignment, but at least align size) |
| 65 | * @open_limit: maximum # of outstanding dma transactions allowed |
| 66 | * @gfp_mask: get_free_pages mask, passed to kmalloc() |
| 67 | * |
| 68 | * The 'apparent' size will be rounded up to next greater aligned size. |
| 69 | * Returns 0 if no error, otherwise an error code |
| 70 | */ |
| 71 | int dma_fifo_alloc(struct dma_fifo *fifo, int size, unsigned align, |
| 72 | int tx_limit, int open_limit, gfp_t gfp_mask) |
| 73 | { |
| 74 | int capacity; |
| 75 | |
| 76 | if (!is_power_of_2(align) || size < 0) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | size = round_up(size, align); |
| 80 | capacity = size + align * open_limit + align * DMA_FIFO_GUARD; |
| 81 | fifo->data = kmalloc(capacity, gfp_mask); |
| 82 | if (!fifo->data) |
| 83 | return -ENOMEM; |
| 84 | |
| 85 | fifo->in = 0; |
| 86 | fifo->out = 0; |
| 87 | fifo->done = 0; |
| 88 | fifo->size = size; |
| 89 | fifo->avail = size; |
| 90 | fifo->align = align; |
| 91 | fifo->tx_limit = max_t(int, round_down(tx_limit, align), align); |
| 92 | fifo->open = 0; |
| 93 | fifo->open_limit = open_limit; |
| 94 | fifo->guard = size + align * open_limit; |
| 95 | fifo->capacity = capacity; |
| 96 | fifo->corrupt = 0; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * dma_fifo_free - frees the fifo |
| 103 | * @fifo: address of in-place "struct dma_fifo" to free |
| 104 | * |
| 105 | * Also reinits the fifo to a valid but inoperative state. This |
| 106 | * allows the fifo to be reused with a different target requiring |
| 107 | * different fifo parameters. |
| 108 | */ |
| 109 | void dma_fifo_free(struct dma_fifo *fifo) |
| 110 | { |
| 111 | struct dma_pending *pending, *next; |
| 112 | |
| 113 | if (fifo->data == NULL) |
| 114 | return; |
| 115 | |
| 116 | list_for_each_entry_safe(pending, next, &fifo->pending, link) |
| 117 | list_del_init(&pending->link); |
| 118 | kfree(fifo->data); |
| 119 | fifo->data = NULL; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * dma_fifo_reset - dumps the fifo contents and reinits for reuse |
| 124 | * @fifo: address of in-place "struct dma_fifo" to reset |
| 125 | */ |
| 126 | void dma_fifo_reset(struct dma_fifo *fifo) |
| 127 | { |
| 128 | struct dma_pending *pending, *next; |
| 129 | |
| 130 | if (fifo->data == NULL) |
| 131 | return; |
| 132 | |
| 133 | list_for_each_entry_safe(pending, next, &fifo->pending, link) |
| 134 | list_del_init(&pending->link); |
| 135 | fifo->in = 0; |
| 136 | fifo->out = 0; |
| 137 | fifo->done = 0; |
| 138 | fifo->avail = fifo->size; |
| 139 | fifo->open = 0; |
| 140 | fifo->corrupt = 0; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * dma_fifo_in - copies data into the fifo |
| 145 | * @fifo: address of in-place "struct dma_fifo" to write to |
| 146 | * @src: buffer to copy from |
| 147 | * @n: # of bytes to copy |
| 148 | * |
| 149 | * Returns the # of bytes actually copied, which can be less than requested if |
| 150 | * the fifo becomes full. If < 0, return is error code. |
| 151 | */ |
| 152 | int dma_fifo_in(struct dma_fifo *fifo, const void *src, int n) |
| 153 | { |
| 154 | int ofs, l; |
| 155 | |
| 156 | if (fifo->data == NULL) |
| 157 | return -ENOENT; |
| 158 | if (fifo->corrupt) |
| 159 | return -ENXIO; |
| 160 | |
| 161 | if (n > fifo->avail) |
| 162 | n = fifo->avail; |
| 163 | if (n <= 0) |
| 164 | return 0; |
| 165 | |
| 166 | ofs = fifo->in % fifo->capacity; |
| 167 | l = min(n, fifo->capacity - ofs); |
| 168 | memcpy(fifo->data + ofs, src, l); |
| 169 | memcpy(fifo->data, src + l, n - l); |
| 170 | |
| 171 | if (FAIL(fifo, addr_check(fifo->done, fifo->in, fifo->in + n) || |
| 172 | fifo->avail < n, |
| 173 | "fifo corrupt: in:%u out:%u done:%u n:%d avail:%d", |
| 174 | fifo->in, fifo->out, fifo->done, n, fifo->avail)) |
| 175 | return -ENXIO; |
| 176 | |
| 177 | fifo->in += n; |
| 178 | fifo->avail -= n; |
| 179 | |
| 180 | df_trace("in:%u out:%u done:%u n:%d avail:%d", fifo->in, fifo->out, |
| 181 | fifo->done, n, fifo->avail); |
| 182 | |
| 183 | return n; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * dma_fifo_out_pend - gets address/len of next avail read and marks as pended |
| 188 | * @fifo: address of in-place "struct dma_fifo" to read from |
| 189 | * @pended: address of structure to fill with read address/len |
| 190 | * The data/len fields will be NULL/0 if no dma is pended. |
| 191 | * |
| 192 | * Returns the # of used bytes remaining in fifo (ie, if > 0, more data |
| 193 | * remains in the fifo that was not pended). If < 0, return is error code. |
| 194 | */ |
| 195 | int dma_fifo_out_pend(struct dma_fifo *fifo, struct dma_pending *pended) |
| 196 | { |
| 197 | unsigned len, n, ofs, l, limit; |
| 198 | |
| 199 | if (fifo->data == NULL) |
| 200 | return -ENOENT; |
| 201 | if (fifo->corrupt) |
| 202 | return -ENXIO; |
| 203 | |
| 204 | pended->len = 0; |
| 205 | pended->data = NULL; |
| 206 | pended->out = fifo->out; |
| 207 | |
| 208 | len = fifo->in - fifo->out; |
| 209 | if (!len) |
| 210 | return -ENODATA; |
| 211 | if (fifo->open == fifo->open_limit) |
| 212 | return -EAGAIN; |
| 213 | |
| 214 | n = len; |
| 215 | ofs = fifo->out % fifo->capacity; |
| 216 | l = fifo->capacity - ofs; |
| 217 | limit = min_t(unsigned, l, fifo->tx_limit); |
| 218 | if (n > limit) { |
| 219 | n = limit; |
| 220 | fifo->out += limit; |
| 221 | } else if (ofs + n > fifo->guard) { |
| 222 | fifo->out += l; |
| 223 | fifo->in = fifo->out; |
| 224 | } else { |
| 225 | fifo->out += round_up(n, fifo->align); |
| 226 | fifo->in = fifo->out; |
| 227 | } |
| 228 | |
| 229 | df_trace("in: %u out: %u done: %u n: %d len: %u avail: %d", fifo->in, |
| 230 | fifo->out, fifo->done, n, len, fifo->avail); |
| 231 | |
| 232 | pended->len = n; |
| 233 | pended->data = fifo->data + ofs; |
| 234 | pended->next = fifo->out; |
| 235 | list_add_tail(&pended->link, &fifo->pending); |
| 236 | ++fifo->open; |
| 237 | |
| 238 | if (FAIL(fifo, fifo->open > fifo->open_limit, |
| 239 | "past open limit:%d (limit:%d)", |
| 240 | fifo->open, fifo->open_limit)) |
| 241 | return -ENXIO; |
| 242 | if (FAIL(fifo, fifo->out & (fifo->align - 1), |
| 243 | "fifo out unaligned:%u (align:%u)", |
| 244 | fifo->out, fifo->align)) |
| 245 | return -ENXIO; |
| 246 | |
| 247 | return len - n; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * dma_fifo_out_complete - marks pended dma as completed |
| 252 | * @fifo: address of in-place "struct dma_fifo" which was read from |
| 253 | * @complete: address of structure for previously pended dma to mark completed |
| 254 | */ |
| 255 | int dma_fifo_out_complete(struct dma_fifo *fifo, struct dma_pending *complete) |
| 256 | { |
| 257 | struct dma_pending *pending, *next, *tmp; |
| 258 | |
| 259 | if (fifo->data == NULL) |
| 260 | return -ENOENT; |
| 261 | if (fifo->corrupt) |
| 262 | return -ENXIO; |
| 263 | if (list_empty(&fifo->pending) && fifo->open == 0) |
| 264 | return -EINVAL; |
| 265 | |
| 266 | if (FAIL(fifo, list_empty(&fifo->pending) != (fifo->open == 0), |
| 267 | "pending list disagrees with open count:%d", |
| 268 | fifo->open)) |
| 269 | return -ENXIO; |
| 270 | |
| 271 | tmp = complete->data; |
| 272 | *tmp = *complete; |
| 273 | list_replace(&complete->link, &tmp->link); |
| 274 | dp_mark_completed(tmp); |
| 275 | |
| 276 | /* Only update the fifo in the original pended order */ |
| 277 | list_for_each_entry_safe(pending, next, &fifo->pending, link) { |
| 278 | if (!dp_is_completed(pending)) { |
| 279 | df_trace("still pending: saved out: %u len: %d", |
| 280 | pending->out, pending->len); |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | if (FAIL(fifo, pending->out != fifo->done || |
| 285 | addr_check(fifo->in, fifo->done, pending->next), |
| 286 | "in:%u out:%u done:%u saved:%u next:%u", |
| 287 | fifo->in, fifo->out, fifo->done, pending->out, |
| 288 | pending->next)) |
| 289 | return -ENXIO; |
| 290 | |
| 291 | list_del_init(&pending->link); |
| 292 | fifo->done = pending->next; |
| 293 | fifo->avail += pending->len; |
| 294 | --fifo->open; |
| 295 | |
| 296 | df_trace("in: %u out: %u done: %u len: %u avail: %d", fifo->in, |
| 297 | fifo->out, fifo->done, pending->len, fifo->avail); |
| 298 | } |
| 299 | |
| 300 | if (FAIL(fifo, fifo->open < 0, "open dma:%d < 0", fifo->open)) |
| 301 | return -ENXIO; |
| 302 | if (FAIL(fifo, fifo->avail > fifo->size, "fifo avail:%d > size:%d", |
| 303 | fifo->avail, fifo->size)) |
| 304 | return -ENXIO; |
| 305 | |
| 306 | return 0; |
| 307 | } |