kfifo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * A generic kernel FIFO implementation
  3. *
  4. * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
  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
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/export.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/log2.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kfifo.h>
  28. /*
  29. * internal helper to calculate the unused elements in a fifo
  30. */
  31. static inline unsigned int kfifo_unused(struct __kfifo *fifo)
  32. {
  33. return (fifo->mask + 1) - (fifo->in - fifo->out);
  34. }
  35. int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
  36. size_t esize, gfp_t gfp_mask)
  37. {
  38. /*
  39. * round down to the next power of 2, since our 'let the indices
  40. * wrap' technique works only in this case.
  41. */
  42. if (!is_power_of_2(size))
  43. size = rounddown_pow_of_two(size);
  44. fifo->in = 0;
  45. fifo->out = 0;
  46. fifo->esize = esize;
  47. if (size < 2) {
  48. fifo->data = NULL;
  49. fifo->mask = 0;
  50. return -EINVAL;
  51. }
  52. fifo->data = kmalloc(size * esize, gfp_mask);
  53. if (!fifo->data) {
  54. fifo->mask = 0;
  55. return -ENOMEM;
  56. }
  57. fifo->mask = size - 1;
  58. return 0;
  59. }
  60. EXPORT_SYMBOL(__kfifo_alloc);
  61. void __kfifo_free(struct __kfifo *fifo)
  62. {
  63. kfree(fifo->data);
  64. fifo->in = 0;
  65. fifo->out = 0;
  66. fifo->esize = 0;
  67. fifo->data = NULL;
  68. fifo->mask = 0;
  69. }
  70. EXPORT_SYMBOL(__kfifo_free);
  71. int __kfifo_init(struct __kfifo *fifo, void *buffer,
  72. unsigned int size, size_t esize)
  73. {
  74. size /= esize;
  75. if (!is_power_of_2(size))
  76. size = rounddown_pow_of_two(size);
  77. fifo->in = 0;
  78. fifo->out = 0;
  79. fifo->esize = esize;
  80. fifo->data = buffer;
  81. if (size < 2) {
  82. fifo->mask = 0;
  83. return -EINVAL;
  84. }
  85. fifo->mask = size - 1;
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(__kfifo_init);
  89. static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
  90. unsigned int len, unsigned int off)
  91. {
  92. unsigned int size = fifo->mask + 1;
  93. unsigned int esize = fifo->esize;
  94. unsigned int l;
  95. off &= fifo->mask;
  96. if (esize != 1) {
  97. off *= esize;
  98. size *= esize;
  99. len *= esize;
  100. }
  101. l = min(len, size - off);
  102. memcpy(fifo->data + off, src, l);
  103. memcpy(fifo->data, src + l, len - l);
  104. /*
  105. * make sure that the data in the fifo is up to date before
  106. * incrementing the fifo->in index counter
  107. */
  108. smp_wmb();
  109. }
  110. unsigned int __kfifo_in(struct __kfifo *fifo,
  111. const void *buf, unsigned int len)
  112. {
  113. unsigned int l;
  114. l = kfifo_unused(fifo);
  115. if (len > l) {
  116. if ((l % 4) != 0)
  117. pr_info("[SSP] %s - %u, %u, %u\n", __func__,
  118. fifo->mask, fifo->in, fifo->out);
  119. len = l;
  120. }
  121. kfifo_copy_in(fifo, buf, len, fifo->in);
  122. fifo->in += len;
  123. return len;
  124. }
  125. EXPORT_SYMBOL(__kfifo_in);
  126. static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
  127. unsigned int len, unsigned int off)
  128. {
  129. unsigned int size = fifo->mask + 1;
  130. unsigned int esize = fifo->esize;
  131. unsigned int l;
  132. off &= fifo->mask;
  133. if (esize != 1) {
  134. off *= esize;
  135. size *= esize;
  136. len *= esize;
  137. }
  138. l = min(len, size - off);
  139. memcpy(dst, fifo->data + off, l);
  140. memcpy(dst + l, fifo->data, len - l);
  141. /*
  142. * make sure that the data is copied before
  143. * incrementing the fifo->out index counter
  144. */
  145. smp_wmb();
  146. }
  147. unsigned int __kfifo_out_peek(struct __kfifo *fifo,
  148. void *buf, unsigned int len)
  149. {
  150. unsigned int l;
  151. l = fifo->in - fifo->out;
  152. if (len > l)
  153. len = l;
  154. kfifo_copy_out(fifo, buf, len, fifo->out);
  155. return len;
  156. }
  157. EXPORT_SYMBOL(__kfifo_out_peek);
  158. unsigned int __kfifo_out(struct __kfifo *fifo,
  159. void *buf, unsigned int len)
  160. {
  161. len = __kfifo_out_peek(fifo, buf, len);
  162. fifo->out += len;
  163. return len;
  164. }
  165. EXPORT_SYMBOL(__kfifo_out);
  166. static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
  167. const void __user *from, unsigned int len, unsigned int off,
  168. unsigned int *copied)
  169. {
  170. unsigned int size = fifo->mask + 1;
  171. unsigned int esize = fifo->esize;
  172. unsigned int l;
  173. unsigned long ret;
  174. off &= fifo->mask;
  175. if (esize != 1) {
  176. off *= esize;
  177. size *= esize;
  178. len *= esize;
  179. }
  180. l = min(len, size - off);
  181. ret = copy_from_user(fifo->data + off, from, l);
  182. if (unlikely(ret))
  183. ret = DIV_ROUND_UP(ret + len - l, esize);
  184. else {
  185. ret = copy_from_user(fifo->data, from + l, len - l);
  186. if (unlikely(ret))
  187. ret = DIV_ROUND_UP(ret, esize);
  188. }
  189. /*
  190. * make sure that the data in the fifo is up to date before
  191. * incrementing the fifo->in index counter
  192. */
  193. smp_wmb();
  194. *copied = len - ret;
  195. /* return the number of elements which are not copied */
  196. return ret;
  197. }
  198. int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
  199. unsigned long len, unsigned int *copied)
  200. {
  201. unsigned int l;
  202. unsigned long ret;
  203. unsigned int esize = fifo->esize;
  204. int err;
  205. if (esize != 1)
  206. len /= esize;
  207. l = kfifo_unused(fifo);
  208. if (len > l)
  209. len = l;
  210. ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
  211. if (unlikely(ret)) {
  212. len -= ret;
  213. err = -EFAULT;
  214. } else
  215. err = 0;
  216. fifo->in += len;
  217. return err;
  218. }
  219. EXPORT_SYMBOL(__kfifo_from_user);
  220. static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
  221. unsigned int len, unsigned int off, unsigned int *copied)
  222. {
  223. unsigned int l;
  224. unsigned long ret;
  225. unsigned int size = fifo->mask + 1;
  226. unsigned int esize = fifo->esize;
  227. off &= fifo->mask;
  228. if (esize != 1) {
  229. off *= esize;
  230. size *= esize;
  231. len *= esize;
  232. }
  233. l = min(len, size - off);
  234. ret = copy_to_user(to, fifo->data + off, l);
  235. if (unlikely(ret))
  236. ret = DIV_ROUND_UP(ret + len - l, esize);
  237. else {
  238. ret = copy_to_user(to + l, fifo->data, len - l);
  239. if (unlikely(ret))
  240. ret = DIV_ROUND_UP(ret, esize);
  241. }
  242. /*
  243. * make sure that the data is copied before
  244. * incrementing the fifo->out index counter
  245. */
  246. smp_wmb();
  247. *copied = len - ret;
  248. /* return the number of elements which are not copied */
  249. return ret;
  250. }
  251. int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
  252. unsigned long len, unsigned int *copied)
  253. {
  254. unsigned int l;
  255. unsigned long ret;
  256. unsigned int esize = fifo->esize;
  257. int err;
  258. if (esize != 1)
  259. len /= esize;
  260. l = fifo->in - fifo->out;
  261. if (len > l)
  262. len = l;
  263. ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
  264. if (unlikely(ret)) {
  265. len -= ret;
  266. err = -EFAULT;
  267. } else
  268. err = 0;
  269. fifo->out += len;
  270. return err;
  271. }
  272. EXPORT_SYMBOL(__kfifo_to_user);
  273. static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
  274. int nents, unsigned int len)
  275. {
  276. int n;
  277. unsigned int l;
  278. unsigned int off;
  279. struct page *page;
  280. if (!nents)
  281. return 0;
  282. if (!len)
  283. return 0;
  284. n = 0;
  285. page = virt_to_page(buf);
  286. off = offset_in_page(buf);
  287. l = 0;
  288. while (len >= l + PAGE_SIZE - off) {
  289. struct page *npage;
  290. l += PAGE_SIZE;
  291. buf += PAGE_SIZE;
  292. npage = virt_to_page(buf);
  293. if (page_to_phys(page) != page_to_phys(npage) - l) {
  294. sg_set_page(sgl, page, l - off, off);
  295. sgl = sg_next(sgl);
  296. if (++n == nents || sgl == NULL)
  297. return n;
  298. page = npage;
  299. len -= l - off;
  300. l = off = 0;
  301. }
  302. }
  303. sg_set_page(sgl, page, len, off);
  304. return n + 1;
  305. }
  306. static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
  307. int nents, unsigned int len, unsigned int off)
  308. {
  309. unsigned int size = fifo->mask + 1;
  310. unsigned int esize = fifo->esize;
  311. unsigned int l;
  312. unsigned int n;
  313. off &= fifo->mask;
  314. if (esize != 1) {
  315. off *= esize;
  316. size *= esize;
  317. len *= esize;
  318. }
  319. l = min(len, size - off);
  320. n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
  321. n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
  322. return n;
  323. }
  324. unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
  325. struct scatterlist *sgl, int nents, unsigned int len)
  326. {
  327. unsigned int l;
  328. l = kfifo_unused(fifo);
  329. if (len > l)
  330. len = l;
  331. return setup_sgl(fifo, sgl, nents, len, fifo->in);
  332. }
  333. EXPORT_SYMBOL(__kfifo_dma_in_prepare);
  334. unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
  335. struct scatterlist *sgl, int nents, unsigned int len)
  336. {
  337. unsigned int l;
  338. l = fifo->in - fifo->out;
  339. if (len > l)
  340. len = l;
  341. return setup_sgl(fifo, sgl, nents, len, fifo->out);
  342. }
  343. EXPORT_SYMBOL(__kfifo_dma_out_prepare);
  344. unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
  345. {
  346. unsigned int max = (1 << (recsize << 3)) - 1;
  347. if (len > max)
  348. return max;
  349. return len;
  350. }
  351. #define __KFIFO_PEEK(data, out, mask) \
  352. ((data)[(out) & (mask)])
  353. /*
  354. * __kfifo_peek_n internal helper function for determinate the length of
  355. * the next record in the fifo
  356. */
  357. static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
  358. {
  359. unsigned int l;
  360. unsigned int mask = fifo->mask;
  361. unsigned char *data = fifo->data;
  362. l = __KFIFO_PEEK(data, fifo->out, mask);
  363. if (--recsize)
  364. l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
  365. return l;
  366. }
  367. #define __KFIFO_POKE(data, in, mask, val) \
  368. ( \
  369. (data)[(in) & (mask)] = (unsigned char)(val) \
  370. )
  371. /*
  372. * __kfifo_poke_n internal helper function for storeing the length of
  373. * the record into the fifo
  374. */
  375. static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
  376. {
  377. unsigned int mask = fifo->mask;
  378. unsigned char *data = fifo->data;
  379. __KFIFO_POKE(data, fifo->in, mask, n);
  380. if (recsize > 1)
  381. __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
  382. }
  383. unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
  384. {
  385. return __kfifo_peek_n(fifo, recsize);
  386. }
  387. EXPORT_SYMBOL(__kfifo_len_r);
  388. unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
  389. unsigned int len, size_t recsize)
  390. {
  391. if (len + recsize > kfifo_unused(fifo))
  392. return 0;
  393. __kfifo_poke_n(fifo, len, recsize);
  394. kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
  395. fifo->in += len + recsize;
  396. return len;
  397. }
  398. EXPORT_SYMBOL(__kfifo_in_r);
  399. static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
  400. void *buf, unsigned int len, size_t recsize, unsigned int *n)
  401. {
  402. *n = __kfifo_peek_n(fifo, recsize);
  403. if (len > *n)
  404. len = *n;
  405. kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
  406. return len;
  407. }
  408. unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
  409. unsigned int len, size_t recsize)
  410. {
  411. unsigned int n;
  412. if (fifo->in == fifo->out)
  413. return 0;
  414. return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
  415. }
  416. EXPORT_SYMBOL(__kfifo_out_peek_r);
  417. unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
  418. unsigned int len, size_t recsize)
  419. {
  420. unsigned int n;
  421. if (fifo->in == fifo->out)
  422. return 0;
  423. len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
  424. fifo->out += n + recsize;
  425. return len;
  426. }
  427. EXPORT_SYMBOL(__kfifo_out_r);
  428. void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
  429. {
  430. unsigned int n;
  431. n = __kfifo_peek_n(fifo, recsize);
  432. fifo->out += n + recsize;
  433. }
  434. EXPORT_SYMBOL(__kfifo_skip_r);
  435. int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
  436. unsigned long len, unsigned int *copied, size_t recsize)
  437. {
  438. unsigned long ret;
  439. len = __kfifo_max_r(len, recsize);
  440. if (len + recsize > kfifo_unused(fifo)) {
  441. *copied = 0;
  442. return 0;
  443. }
  444. __kfifo_poke_n(fifo, len, recsize);
  445. ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
  446. if (unlikely(ret)) {
  447. *copied = 0;
  448. return -EFAULT;
  449. }
  450. fifo->in += len + recsize;
  451. return 0;
  452. }
  453. EXPORT_SYMBOL(__kfifo_from_user_r);
  454. int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
  455. unsigned long len, unsigned int *copied, size_t recsize)
  456. {
  457. unsigned long ret;
  458. unsigned int n;
  459. if (fifo->in == fifo->out) {
  460. *copied = 0;
  461. return 0;
  462. }
  463. n = __kfifo_peek_n(fifo, recsize);
  464. if (len > n)
  465. len = n;
  466. ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
  467. if (unlikely(ret)) {
  468. *copied = 0;
  469. return -EFAULT;
  470. }
  471. fifo->out += n + recsize;
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(__kfifo_to_user_r);
  475. unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
  476. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
  477. {
  478. if (!nents)
  479. BUG();
  480. len = __kfifo_max_r(len, recsize);
  481. if (len + recsize > kfifo_unused(fifo))
  482. return 0;
  483. return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
  484. }
  485. EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
  486. void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
  487. unsigned int len, size_t recsize)
  488. {
  489. len = __kfifo_max_r(len, recsize);
  490. __kfifo_poke_n(fifo, len, recsize);
  491. fifo->in += len + recsize;
  492. }
  493. EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
  494. unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
  495. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
  496. {
  497. if (!nents)
  498. BUG();
  499. len = __kfifo_max_r(len, recsize);
  500. if (len + recsize > fifo->in - fifo->out)
  501. return 0;
  502. return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
  503. }
  504. EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
  505. void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
  506. {
  507. unsigned int len;
  508. len = __kfifo_peek_n(fifo, recsize);
  509. fifo->out += len + recsize;
  510. }
  511. EXPORT_SYMBOL(__kfifo_dma_out_finish_r);