Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 2 | * A generic kernel FIFO implementation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 4 | * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (C) 2004 Stelian Pop <stelian@popies.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/kfifo.h> |
vignesh babu | f84d5a7 | 2007-07-15 23:41:34 -0700 | [diff] [blame] | 28 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 30 | static void _kfifo_init(struct kfifo *fifo, unsigned char *buffer, |
| 31 | unsigned int size, spinlock_t *lock) |
| 32 | { |
| 33 | fifo->buffer = buffer; |
| 34 | fifo->size = size; |
| 35 | fifo->lock = lock; |
| 36 | |
| 37 | kfifo_reset(fifo); |
| 38 | } |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 41 | * kfifo_init - initialize a FIFO using a preallocated buffer |
| 42 | * @fifo: the fifo to assign the buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * @buffer: the preallocated buffer to be used. |
| 44 | * @size: the size of the internal buffer, this have to be a power of 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * @lock: the lock to be used to protect the fifo buffer |
| 46 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | */ |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 48 | void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size, |
| 49 | spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* size must be a power of 2 */ |
vignesh babu | f84d5a7 | 2007-07-15 23:41:34 -0700 | [diff] [blame] | 52 | BUG_ON(!is_power_of_2(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 54 | _kfifo_init(fifo, buffer, size, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } |
| 56 | EXPORT_SYMBOL(kfifo_init); |
| 57 | |
| 58 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 59 | * kfifo_alloc - allocates a new FIFO internal buffer |
| 60 | * @fifo: the fifo to assign then new buffer |
| 61 | * @size: the size of the buffer to be allocated, this have to be a power of 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * @gfp_mask: get_free_pages mask, passed to kmalloc() |
| 63 | * @lock: the lock to be used to protect the fifo buffer |
| 64 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 65 | * This function dynamically allocates a new fifo internal buffer |
| 66 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * The size will be rounded-up to a power of 2. |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 68 | * The buffer will be release with kfifo_free(). |
| 69 | * Return 0 if no error, otherwise the an error code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 71 | int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask, |
| 72 | spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | unsigned char *buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * round up to the next power of 2, since our 'let the indices |
Robert P. J. Day | b33112d | 2009-06-16 15:33:34 -0700 | [diff] [blame] | 78 | * wrap' technique works only in this case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
Robert P. J. Day | b33112d | 2009-06-16 15:33:34 -0700 | [diff] [blame] | 80 | if (!is_power_of_2(size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | BUG_ON(size > 0x80000000); |
| 82 | size = roundup_pow_of_two(size); |
| 83 | } |
| 84 | |
| 85 | buffer = kmalloc(size, gfp_mask); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 86 | if (!buffer) { |
| 87 | _kfifo_init(fifo, 0, 0, NULL); |
| 88 | return -ENOMEM; |
| 89 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 91 | _kfifo_init(fifo, buffer, size, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 93 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | EXPORT_SYMBOL(kfifo_alloc); |
| 96 | |
| 97 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame^] | 98 | * kfifo_free - frees the FIFO internal buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * @fifo: the fifo to be freed. |
| 100 | */ |
| 101 | void kfifo_free(struct kfifo *fifo) |
| 102 | { |
| 103 | kfree(fifo->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | EXPORT_SYMBOL(kfifo_free); |
| 106 | |
| 107 | /** |
| 108 | * __kfifo_put - puts some data into the FIFO, no locking version |
| 109 | * @fifo: the fifo to be used. |
| 110 | * @buffer: the data to be added. |
| 111 | * @len: the length of the data to be added. |
| 112 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 113 | * This function copies at most @len bytes from the @buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * the FIFO depending on the free space, and returns the number of |
| 115 | * bytes copied. |
| 116 | * |
| 117 | * Note that with only one concurrent reader and one concurrent |
| 118 | * writer, you don't need extra locking to use these functions. |
| 119 | */ |
| 120 | unsigned int __kfifo_put(struct kfifo *fifo, |
Alan Cox | f8a7c1a | 2009-09-19 13:13:17 -0700 | [diff] [blame] | 121 | const unsigned char *buffer, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | unsigned int l; |
| 124 | |
| 125 | len = min(len, fifo->size - fifo->in + fifo->out); |
| 126 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 127 | /* |
| 128 | * Ensure that we sample the fifo->out index -before- we |
| 129 | * start putting bytes into the kfifo. |
| 130 | */ |
| 131 | |
| 132 | smp_mb(); |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* first put the data starting from fifo->in to buffer end */ |
| 135 | l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); |
| 136 | memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l); |
| 137 | |
| 138 | /* then put the rest (if any) at the beginning of the buffer */ |
| 139 | memcpy(fifo->buffer, buffer + l, len - l); |
| 140 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 141 | /* |
| 142 | * Ensure that we add the bytes to the kfifo -before- |
| 143 | * we update the fifo->in index. |
| 144 | */ |
| 145 | |
| 146 | smp_wmb(); |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | fifo->in += len; |
| 149 | |
| 150 | return len; |
| 151 | } |
| 152 | EXPORT_SYMBOL(__kfifo_put); |
| 153 | |
| 154 | /** |
| 155 | * __kfifo_get - gets some data from the FIFO, no locking version |
| 156 | * @fifo: the fifo to be used. |
| 157 | * @buffer: where the data must be copied. |
| 158 | * @len: the size of the destination buffer. |
| 159 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 160 | * This function copies at most @len bytes from the FIFO into the |
| 161 | * @buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | * |
| 163 | * Note that with only one concurrent reader and one concurrent |
| 164 | * writer, you don't need extra locking to use these functions. |
| 165 | */ |
| 166 | unsigned int __kfifo_get(struct kfifo *fifo, |
| 167 | unsigned char *buffer, unsigned int len) |
| 168 | { |
| 169 | unsigned int l; |
| 170 | |
| 171 | len = min(len, fifo->in - fifo->out); |
| 172 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 173 | /* |
| 174 | * Ensure that we sample the fifo->in index -before- we |
| 175 | * start removing bytes from the kfifo. |
| 176 | */ |
| 177 | |
| 178 | smp_rmb(); |
| 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* first get the data from fifo->out until the end of the buffer */ |
| 181 | l = min(len, fifo->size - (fifo->out & (fifo->size - 1))); |
| 182 | memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l); |
| 183 | |
| 184 | /* then get the rest (if any) from the beginning of the buffer */ |
| 185 | memcpy(buffer + l, fifo->buffer, len - l); |
| 186 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 187 | /* |
| 188 | * Ensure that we remove the bytes from the kfifo -before- |
| 189 | * we update the fifo->out index. |
| 190 | */ |
| 191 | |
| 192 | smp_mb(); |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | fifo->out += len; |
| 195 | |
| 196 | return len; |
| 197 | } |
| 198 | EXPORT_SYMBOL(__kfifo_get); |