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, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 31 | unsigned int size) |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 32 | { |
| 33 | fifo->buffer = buffer; |
| 34 | fifo->size = size; |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 35 | |
| 36 | kfifo_reset(fifo); |
| 37 | } |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 40 | * kfifo_init - initialize a FIFO using a preallocated buffer |
| 41 | * @fifo: the fifo to assign the buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * @buffer: the preallocated buffer to be used. |
| 43 | * @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] | 44 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 46 | void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* size must be a power of 2 */ |
vignesh babu | f84d5a7 | 2007-07-15 23:41:34 -0700 | [diff] [blame] | 49 | BUG_ON(!is_power_of_2(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 51 | _kfifo_init(fifo, buffer, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | EXPORT_SYMBOL(kfifo_init); |
| 54 | |
| 55 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 56 | * kfifo_alloc - allocates a new FIFO internal buffer |
| 57 | * @fifo: the fifo to assign then new buffer |
| 58 | * @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] | 59 | * @gfp_mask: get_free_pages mask, passed to kmalloc() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 61 | * This function dynamically allocates a new fifo internal buffer |
| 62 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * The size will be rounded-up to a power of 2. |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 64 | * The buffer will be release with kfifo_free(). |
| 65 | * Return 0 if no error, otherwise the an error code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 67 | int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | unsigned char *buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * 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] | 73 | * wrap' technique works only in this case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Robert P. J. Day | b33112d | 2009-06-16 15:33:34 -0700 | [diff] [blame] | 75 | if (!is_power_of_2(size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | BUG_ON(size > 0x80000000); |
| 77 | size = roundup_pow_of_two(size); |
| 78 | } |
| 79 | |
| 80 | buffer = kmalloc(size, gfp_mask); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 81 | if (!buffer) { |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 82 | _kfifo_init(fifo, 0, 0); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 83 | return -ENOMEM; |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 86 | _kfifo_init(fifo, buffer, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 88 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | EXPORT_SYMBOL(kfifo_alloc); |
| 91 | |
| 92 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 93 | * kfifo_free - frees the FIFO internal buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * @fifo: the fifo to be freed. |
| 95 | */ |
| 96 | void kfifo_free(struct kfifo *fifo) |
| 97 | { |
| 98 | kfree(fifo->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | EXPORT_SYMBOL(kfifo_free); |
| 101 | |
| 102 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 103 | * kfifo_in - puts some data into the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * @fifo: the fifo to be used. |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 105 | * @from: the data to be added. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * @len: the length of the data to be added. |
| 107 | * |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 108 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * the FIFO depending on the free space, and returns the number of |
| 110 | * bytes copied. |
| 111 | * |
| 112 | * Note that with only one concurrent reader and one concurrent |
| 113 | * writer, you don't need extra locking to use these functions. |
| 114 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 115 | unsigned int kfifo_in(struct kfifo *fifo, |
| 116 | const unsigned char *from, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | unsigned int l; |
| 119 | |
| 120 | len = min(len, fifo->size - fifo->in + fifo->out); |
| 121 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Ensure that we sample the fifo->out index -before- we |
| 124 | * start putting bytes into the kfifo. |
| 125 | */ |
| 126 | |
| 127 | smp_mb(); |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | /* first put the data starting from fifo->in to buffer end */ |
| 130 | l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 131 | memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), from, l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | /* then put the rest (if any) at the beginning of the buffer */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 134 | memcpy(fifo->buffer, from + l, len - l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 136 | /* |
| 137 | * Ensure that we add the bytes to the kfifo -before- |
| 138 | * we update the fifo->in index. |
| 139 | */ |
| 140 | |
| 141 | smp_wmb(); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | fifo->in += len; |
| 144 | |
| 145 | return len; |
| 146 | } |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 147 | EXPORT_SYMBOL(kfifo_in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 150 | * kfifo_out - gets some data from the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * @fifo: the fifo to be used. |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 152 | * @to: where the data must be copied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * @len: the size of the destination buffer. |
| 154 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 155 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 156 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * |
| 158 | * Note that with only one concurrent reader and one concurrent |
| 159 | * writer, you don't need extra locking to use these functions. |
| 160 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 161 | unsigned int kfifo_out(struct kfifo *fifo, |
| 162 | unsigned char *to, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | unsigned int l; |
| 165 | |
| 166 | len = min(len, fifo->in - fifo->out); |
| 167 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 168 | /* |
| 169 | * Ensure that we sample the fifo->in index -before- we |
| 170 | * start removing bytes from the kfifo. |
| 171 | */ |
| 172 | |
| 173 | smp_rmb(); |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* first get the data from fifo->out until the end of the buffer */ |
| 176 | l = min(len, fifo->size - (fifo->out & (fifo->size - 1))); |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 177 | memcpy(to, fifo->buffer + (fifo->out & (fifo->size - 1)), l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | /* then get the rest (if any) from the beginning of the buffer */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 180 | memcpy(to + l, fifo->buffer, len - l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Paul E. McKenney | a45bce4 | 2006-09-29 02:00:11 -0700 | [diff] [blame] | 182 | /* |
| 183 | * Ensure that we remove the bytes from the kfifo -before- |
| 184 | * we update the fifo->out index. |
| 185 | */ |
| 186 | |
| 187 | smp_mb(); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | fifo->out += len; |
| 190 | |
| 191 | return len; |
| 192 | } |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 193 | EXPORT_SYMBOL(kfifo_out); |