industrialio-buffer-dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Copyright 2013-2015 Analog Devices Inc.
  3. * Author: Lars-Peter Clausen <lars@metafoo.de>
  4. *
  5. * Licensed under the GPL-2.
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/mutex.h>
  13. #include <linux/sched.h>
  14. #include <linux/poll.h>
  15. #include <linux/iio/buffer.h>
  16. #include <linux/iio/buffer_impl.h>
  17. #include <linux/iio/buffer-dma.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/sizes.h>
  20. /*
  21. * For DMA buffers the storage is sub-divided into so called blocks. Each block
  22. * has its own memory buffer. The size of the block is the granularity at which
  23. * memory is exchanged between the hardware and the application. Increasing the
  24. * basic unit of data exchange from one sample to one block decreases the
  25. * management overhead that is associated with each sample. E.g. if we say the
  26. * management overhead for one exchange is x and the unit of exchange is one
  27. * sample the overhead will be x for each sample. Whereas when using a block
  28. * which contains n samples the overhead per sample is reduced to x/n. This
  29. * allows to achieve much higher samplerates than what can be sustained with
  30. * the one sample approach.
  31. *
  32. * Blocks are exchanged between the DMA controller and the application via the
  33. * means of two queues. The incoming queue and the outgoing queue. Blocks on the
  34. * incoming queue are waiting for the DMA controller to pick them up and fill
  35. * them with data. Block on the outgoing queue have been filled with data and
  36. * are waiting for the application to dequeue them and read the data.
  37. *
  38. * A block can be in one of the following states:
  39. * * Owned by the application. In this state the application can read data from
  40. * the block.
  41. * * On the incoming list: Blocks on the incoming list are queued up to be
  42. * processed by the DMA controller.
  43. * * Owned by the DMA controller: The DMA controller is processing the block
  44. * and filling it with data.
  45. * * On the outgoing list: Blocks on the outgoing list have been successfully
  46. * processed by the DMA controller and contain data. They can be dequeued by
  47. * the application.
  48. * * Dead: A block that is dead has been marked as to be freed. It might still
  49. * be owned by either the application or the DMA controller at the moment.
  50. * But once they are done processing it instead of going to either the
  51. * incoming or outgoing queue the block will be freed.
  52. *
  53. * In addition to this blocks are reference counted and the memory associated
  54. * with both the block structure as well as the storage memory for the block
  55. * will be freed when the last reference to the block is dropped. This means a
  56. * block must not be accessed without holding a reference.
  57. *
  58. * The iio_dma_buffer implementation provides a generic infrastructure for
  59. * managing the blocks.
  60. *
  61. * A driver for a specific piece of hardware that has DMA capabilities need to
  62. * implement the submit() callback from the iio_dma_buffer_ops structure. This
  63. * callback is supposed to initiate the DMA transfer copying data from the
  64. * converter to the memory region of the block. Once the DMA transfer has been
  65. * completed the driver must call iio_dma_buffer_block_done() for the completed
  66. * block.
  67. *
  68. * Prior to this it must set the bytes_used field of the block contains
  69. * the actual number of bytes in the buffer. Typically this will be equal to the
  70. * size of the block, but if the DMA hardware has certain alignment requirements
  71. * for the transfer length it might choose to use less than the full size. In
  72. * either case it is expected that bytes_used is a multiple of the bytes per
  73. * datum, i.e. the block must not contain partial samples.
  74. *
  75. * The driver must call iio_dma_buffer_block_done() for each block it has
  76. * received through its submit_block() callback, even if it does not actually
  77. * perform a DMA transfer for the block, e.g. because the buffer was disabled
  78. * before the block transfer was started. In this case it should set bytes_used
  79. * to 0.
  80. *
  81. * In addition it is recommended that a driver implements the abort() callback.
  82. * It will be called when the buffer is disabled and can be used to cancel
  83. * pending and stop active transfers.
  84. *
  85. * The specific driver implementation should use the default callback
  86. * implementations provided by this module for the iio_buffer_access_funcs
  87. * struct. It may overload some callbacks with custom variants if the hardware
  88. * has special requirements that are not handled by the generic functions. If a
  89. * driver chooses to overload a callback it has to ensure that the generic
  90. * callback is called from within the custom callback.
  91. */
  92. static void iio_buffer_block_release(struct kref *kref)
  93. {
  94. struct iio_dma_buffer_block *block = container_of(kref,
  95. struct iio_dma_buffer_block, kref);
  96. WARN_ON(block->state != IIO_BLOCK_STATE_DEAD);
  97. dma_free_coherent(block->queue->dev, PAGE_ALIGN(block->size),
  98. block->vaddr, block->phys_addr);
  99. iio_buffer_put(&block->queue->buffer);
  100. kfree(block);
  101. }
  102. static void iio_buffer_block_get(struct iio_dma_buffer_block *block)
  103. {
  104. kref_get(&block->kref);
  105. }
  106. static void iio_buffer_block_put(struct iio_dma_buffer_block *block)
  107. {
  108. kref_put(&block->kref, iio_buffer_block_release);
  109. }
  110. /*
  111. * dma_free_coherent can sleep, hence we need to take some special care to be
  112. * able to drop a reference from an atomic context.
  113. */
  114. static LIST_HEAD(iio_dma_buffer_dead_blocks);
  115. static DEFINE_SPINLOCK(iio_dma_buffer_dead_blocks_lock);
  116. static void iio_dma_buffer_cleanup_worker(struct work_struct *work)
  117. {
  118. struct iio_dma_buffer_block *block, *_block;
  119. LIST_HEAD(block_list);
  120. spin_lock_irq(&iio_dma_buffer_dead_blocks_lock);
  121. list_splice_tail_init(&iio_dma_buffer_dead_blocks, &block_list);
  122. spin_unlock_irq(&iio_dma_buffer_dead_blocks_lock);
  123. list_for_each_entry_safe(block, _block, &block_list, head)
  124. iio_buffer_block_release(&block->kref);
  125. }
  126. static DECLARE_WORK(iio_dma_buffer_cleanup_work, iio_dma_buffer_cleanup_worker);
  127. static void iio_buffer_block_release_atomic(struct kref *kref)
  128. {
  129. struct iio_dma_buffer_block *block;
  130. unsigned long flags;
  131. block = container_of(kref, struct iio_dma_buffer_block, kref);
  132. spin_lock_irqsave(&iio_dma_buffer_dead_blocks_lock, flags);
  133. list_add_tail(&block->head, &iio_dma_buffer_dead_blocks);
  134. spin_unlock_irqrestore(&iio_dma_buffer_dead_blocks_lock, flags);
  135. schedule_work(&iio_dma_buffer_cleanup_work);
  136. }
  137. /*
  138. * Version of iio_buffer_block_put() that can be called from atomic context
  139. */
  140. static void iio_buffer_block_put_atomic(struct iio_dma_buffer_block *block)
  141. {
  142. kref_put(&block->kref, iio_buffer_block_release_atomic);
  143. }
  144. static struct iio_dma_buffer_queue *iio_buffer_to_queue(struct iio_buffer *buf)
  145. {
  146. return container_of(buf, struct iio_dma_buffer_queue, buffer);
  147. }
  148. static struct iio_dma_buffer_block *iio_dma_buffer_alloc_block(
  149. struct iio_dma_buffer_queue *queue, size_t size)
  150. {
  151. struct iio_dma_buffer_block *block;
  152. block = kzalloc(sizeof(*block), GFP_KERNEL);
  153. if (!block)
  154. return NULL;
  155. block->vaddr = dma_alloc_coherent(queue->dev, PAGE_ALIGN(size),
  156. &block->phys_addr, GFP_KERNEL);
  157. if (!block->vaddr) {
  158. kfree(block);
  159. return NULL;
  160. }
  161. block->size = size;
  162. block->state = IIO_BLOCK_STATE_DEQUEUED;
  163. block->queue = queue;
  164. INIT_LIST_HEAD(&block->head);
  165. kref_init(&block->kref);
  166. iio_buffer_get(&queue->buffer);
  167. return block;
  168. }
  169. static void _iio_dma_buffer_block_done(struct iio_dma_buffer_block *block)
  170. {
  171. struct iio_dma_buffer_queue *queue = block->queue;
  172. /*
  173. * The buffer has already been freed by the application, just drop the
  174. * reference.
  175. */
  176. if (block->state != IIO_BLOCK_STATE_DEAD) {
  177. block->state = IIO_BLOCK_STATE_DONE;
  178. list_add_tail(&block->head, &queue->outgoing);
  179. }
  180. }
  181. /**
  182. * iio_dma_buffer_block_done() - Indicate that a block has been completed
  183. * @block: The completed block
  184. *
  185. * Should be called when the DMA controller has finished handling the block to
  186. * pass back ownership of the block to the queue.
  187. */
  188. void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block)
  189. {
  190. struct iio_dma_buffer_queue *queue = block->queue;
  191. unsigned long flags;
  192. spin_lock_irqsave(&queue->list_lock, flags);
  193. _iio_dma_buffer_block_done(block);
  194. spin_unlock_irqrestore(&queue->list_lock, flags);
  195. iio_buffer_block_put_atomic(block);
  196. wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM);
  197. }
  198. EXPORT_SYMBOL_GPL(iio_dma_buffer_block_done);
  199. /**
  200. * iio_dma_buffer_block_list_abort() - Indicate that a list block has been
  201. * aborted
  202. * @queue: Queue for which to complete blocks.
  203. * @list: List of aborted blocks. All blocks in this list must be from @queue.
  204. *
  205. * Typically called from the abort() callback after the DMA controller has been
  206. * stopped. This will set bytes_used to 0 for each block in the list and then
  207. * hand the blocks back to the queue.
  208. */
  209. void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue,
  210. struct list_head *list)
  211. {
  212. struct iio_dma_buffer_block *block, *_block;
  213. unsigned long flags;
  214. spin_lock_irqsave(&queue->list_lock, flags);
  215. list_for_each_entry_safe(block, _block, list, head) {
  216. list_del(&block->head);
  217. block->bytes_used = 0;
  218. _iio_dma_buffer_block_done(block);
  219. iio_buffer_block_put_atomic(block);
  220. }
  221. spin_unlock_irqrestore(&queue->list_lock, flags);
  222. wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM);
  223. }
  224. EXPORT_SYMBOL_GPL(iio_dma_buffer_block_list_abort);
  225. static bool iio_dma_block_reusable(struct iio_dma_buffer_block *block)
  226. {
  227. /*
  228. * If the core owns the block it can be re-used. This should be the
  229. * default case when enabling the buffer, unless the DMA controller does
  230. * not support abort and has not given back the block yet.
  231. */
  232. switch (block->state) {
  233. case IIO_BLOCK_STATE_DEQUEUED:
  234. case IIO_BLOCK_STATE_QUEUED:
  235. case IIO_BLOCK_STATE_DONE:
  236. return true;
  237. default:
  238. return false;
  239. }
  240. }
  241. /**
  242. * iio_dma_buffer_request_update() - DMA buffer request_update callback
  243. * @buffer: The buffer which to request an update
  244. *
  245. * Should be used as the iio_dma_buffer_request_update() callback for
  246. * iio_buffer_access_ops struct for DMA buffers.
  247. */
  248. int iio_dma_buffer_request_update(struct iio_buffer *buffer)
  249. {
  250. struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
  251. struct iio_dma_buffer_block *block;
  252. bool try_reuse = false;
  253. size_t size;
  254. int ret = 0;
  255. int i;
  256. /*
  257. * Split the buffer into two even parts. This is used as a double
  258. * buffering scheme with usually one block at a time being used by the
  259. * DMA and the other one by the application.
  260. */
  261. size = DIV_ROUND_UP(queue->buffer.bytes_per_datum *
  262. queue->buffer.length, 2);
  263. mutex_lock(&queue->lock);
  264. /* Allocations are page aligned */
  265. if (PAGE_ALIGN(queue->fileio.block_size) == PAGE_ALIGN(size))
  266. try_reuse = true;
  267. queue->fileio.block_size = size;
  268. queue->fileio.active_block = NULL;
  269. spin_lock_irq(&queue->list_lock);
  270. for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
  271. block = queue->fileio.blocks[i];
  272. /* If we can't re-use it free it */
  273. if (block && (!iio_dma_block_reusable(block) || !try_reuse))
  274. block->state = IIO_BLOCK_STATE_DEAD;
  275. }
  276. /*
  277. * At this point all blocks are either owned by the core or marked as
  278. * dead. This means we can reset the lists without having to fear
  279. * corrution.
  280. */
  281. INIT_LIST_HEAD(&queue->outgoing);
  282. spin_unlock_irq(&queue->list_lock);
  283. INIT_LIST_HEAD(&queue->incoming);
  284. for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
  285. if (queue->fileio.blocks[i]) {
  286. block = queue->fileio.blocks[i];
  287. if (block->state == IIO_BLOCK_STATE_DEAD) {
  288. /* Could not reuse it */
  289. iio_buffer_block_put(block);
  290. block = NULL;
  291. } else {
  292. block->size = size;
  293. }
  294. } else {
  295. block = NULL;
  296. }
  297. if (!block) {
  298. block = iio_dma_buffer_alloc_block(queue, size);
  299. if (!block) {
  300. ret = -ENOMEM;
  301. goto out_unlock;
  302. }
  303. queue->fileio.blocks[i] = block;
  304. }
  305. block->state = IIO_BLOCK_STATE_QUEUED;
  306. list_add_tail(&block->head, &queue->incoming);
  307. }
  308. out_unlock:
  309. mutex_unlock(&queue->lock);
  310. return ret;
  311. }
  312. EXPORT_SYMBOL_GPL(iio_dma_buffer_request_update);
  313. static void iio_dma_buffer_submit_block(struct iio_dma_buffer_queue *queue,
  314. struct iio_dma_buffer_block *block)
  315. {
  316. int ret;
  317. /*
  318. * If the hardware has already been removed we put the block into
  319. * limbo. It will neither be on the incoming nor outgoing list, nor will
  320. * it ever complete. It will just wait to be freed eventually.
  321. */
  322. if (!queue->ops)
  323. return;
  324. block->state = IIO_BLOCK_STATE_ACTIVE;
  325. iio_buffer_block_get(block);
  326. ret = queue->ops->submit(queue, block);
  327. if (ret) {
  328. /*
  329. * This is a bit of a problem and there is not much we can do
  330. * other then wait for the buffer to be disabled and re-enabled
  331. * and try again. But it should not really happen unless we run
  332. * out of memory or something similar.
  333. *
  334. * TODO: Implement support in the IIO core to allow buffers to
  335. * notify consumers that something went wrong and the buffer
  336. * should be disabled.
  337. */
  338. iio_buffer_block_put(block);
  339. }
  340. }
  341. /**
  342. * iio_dma_buffer_enable() - Enable DMA buffer
  343. * @buffer: IIO buffer to enable
  344. * @indio_dev: IIO device the buffer is attached to
  345. *
  346. * Needs to be called when the device that the buffer is attached to starts
  347. * sampling. Typically should be the iio_buffer_access_ops enable callback.
  348. *
  349. * This will allocate the DMA buffers and start the DMA transfers.
  350. */
  351. int iio_dma_buffer_enable(struct iio_buffer *buffer,
  352. struct iio_dev *indio_dev)
  353. {
  354. struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
  355. struct iio_dma_buffer_block *block, *_block;
  356. mutex_lock(&queue->lock);
  357. queue->active = true;
  358. list_for_each_entry_safe(block, _block, &queue->incoming, head) {
  359. list_del(&block->head);
  360. iio_dma_buffer_submit_block(queue, block);
  361. }
  362. mutex_unlock(&queue->lock);
  363. return 0;
  364. }
  365. EXPORT_SYMBOL_GPL(iio_dma_buffer_enable);
  366. /**
  367. * iio_dma_buffer_disable() - Disable DMA buffer
  368. * @buffer: IIO DMA buffer to disable
  369. * @indio_dev: IIO device the buffer is attached to
  370. *
  371. * Needs to be called when the device that the buffer is attached to stops
  372. * sampling. Typically should be the iio_buffer_access_ops disable callback.
  373. */
  374. int iio_dma_buffer_disable(struct iio_buffer *buffer,
  375. struct iio_dev *indio_dev)
  376. {
  377. struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
  378. mutex_lock(&queue->lock);
  379. queue->active = false;
  380. if (queue->ops && queue->ops->abort)
  381. queue->ops->abort(queue);
  382. mutex_unlock(&queue->lock);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL_GPL(iio_dma_buffer_disable);
  386. static void iio_dma_buffer_enqueue(struct iio_dma_buffer_queue *queue,
  387. struct iio_dma_buffer_block *block)
  388. {
  389. if (block->state == IIO_BLOCK_STATE_DEAD) {
  390. iio_buffer_block_put(block);
  391. } else if (queue->active) {
  392. iio_dma_buffer_submit_block(queue, block);
  393. } else {
  394. block->state = IIO_BLOCK_STATE_QUEUED;
  395. list_add_tail(&block->head, &queue->incoming);
  396. }
  397. }
  398. static struct iio_dma_buffer_block *iio_dma_buffer_dequeue(
  399. struct iio_dma_buffer_queue *queue)
  400. {
  401. struct iio_dma_buffer_block *block;
  402. spin_lock_irq(&queue->list_lock);
  403. block = list_first_entry_or_null(&queue->outgoing, struct
  404. iio_dma_buffer_block, head);
  405. if (block != NULL) {
  406. list_del(&block->head);
  407. block->state = IIO_BLOCK_STATE_DEQUEUED;
  408. }
  409. spin_unlock_irq(&queue->list_lock);
  410. return block;
  411. }
  412. /**
  413. * iio_dma_buffer_read() - DMA buffer read callback
  414. * @buffer: Buffer to read form
  415. * @n: Number of bytes to read
  416. * @user_buffer: Userspace buffer to copy the data to
  417. *
  418. * Should be used as the read_first_n callback for iio_buffer_access_ops
  419. * struct for DMA buffers.
  420. */
  421. int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n,
  422. char __user *user_buffer)
  423. {
  424. struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
  425. struct iio_dma_buffer_block *block;
  426. int ret;
  427. if (n < buffer->bytes_per_datum)
  428. return -EINVAL;
  429. mutex_lock(&queue->lock);
  430. if (!queue->fileio.active_block) {
  431. block = iio_dma_buffer_dequeue(queue);
  432. if (block == NULL) {
  433. ret = 0;
  434. goto out_unlock;
  435. }
  436. queue->fileio.pos = 0;
  437. queue->fileio.active_block = block;
  438. } else {
  439. block = queue->fileio.active_block;
  440. }
  441. n = rounddown(n, buffer->bytes_per_datum);
  442. if (n > block->bytes_used - queue->fileio.pos)
  443. n = block->bytes_used - queue->fileio.pos;
  444. if (copy_to_user(user_buffer, block->vaddr + queue->fileio.pos, n)) {
  445. ret = -EFAULT;
  446. goto out_unlock;
  447. }
  448. queue->fileio.pos += n;
  449. if (queue->fileio.pos == block->bytes_used) {
  450. queue->fileio.active_block = NULL;
  451. iio_dma_buffer_enqueue(queue, block);
  452. }
  453. ret = n;
  454. out_unlock:
  455. mutex_unlock(&queue->lock);
  456. return ret;
  457. }
  458. EXPORT_SYMBOL_GPL(iio_dma_buffer_read);
  459. /**
  460. * iio_dma_buffer_data_available() - DMA buffer data_available callback
  461. * @buf: Buffer to check for data availability
  462. *
  463. * Should be used as the data_available callback for iio_buffer_access_ops
  464. * struct for DMA buffers.
  465. */
  466. size_t iio_dma_buffer_data_available(struct iio_buffer *buf)
  467. {
  468. struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buf);
  469. struct iio_dma_buffer_block *block;
  470. size_t data_available = 0;
  471. /*
  472. * For counting the available bytes we'll use the size of the block not
  473. * the number of actual bytes available in the block. Otherwise it is
  474. * possible that we end up with a value that is lower than the watermark
  475. * but won't increase since all blocks are in use.
  476. */
  477. mutex_lock(&queue->lock);
  478. if (queue->fileio.active_block)
  479. data_available += queue->fileio.active_block->size;
  480. spin_lock_irq(&queue->list_lock);
  481. list_for_each_entry(block, &queue->outgoing, head)
  482. data_available += block->size;
  483. spin_unlock_irq(&queue->list_lock);
  484. mutex_unlock(&queue->lock);
  485. return data_available;
  486. }
  487. EXPORT_SYMBOL_GPL(iio_dma_buffer_data_available);
  488. /**
  489. * iio_dma_buffer_set_bytes_per_datum() - DMA buffer set_bytes_per_datum callback
  490. * @buffer: Buffer to set the bytes-per-datum for
  491. * @bpd: The new bytes-per-datum value
  492. *
  493. * Should be used as the set_bytes_per_datum callback for iio_buffer_access_ops
  494. * struct for DMA buffers.
  495. */
  496. int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd)
  497. {
  498. buffer->bytes_per_datum = bpd;
  499. return 0;
  500. }
  501. EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);
  502. /**
  503. * iio_dma_buffer_set_length - DMA buffer set_length callback
  504. * @buffer: Buffer to set the length for
  505. * @length: The new buffer length
  506. *
  507. * Should be used as the set_length callback for iio_buffer_access_ops
  508. * struct for DMA buffers.
  509. */
  510. int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length)
  511. {
  512. /* Avoid an invalid state */
  513. if (length < 2)
  514. length = 2;
  515. buffer->length = length;
  516. buffer->watermark = length / 2;
  517. return 0;
  518. }
  519. EXPORT_SYMBOL_GPL(iio_dma_buffer_set_length);
  520. /**
  521. * iio_dma_buffer_init() - Initialize DMA buffer queue
  522. * @queue: Buffer to initialize
  523. * @dev: DMA device
  524. * @ops: DMA buffer queue callback operations
  525. *
  526. * The DMA device will be used by the queue to do DMA memory allocations. So it
  527. * should refer to the device that will perform the DMA to ensure that
  528. * allocations are done from a memory region that can be accessed by the device.
  529. */
  530. int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue,
  531. struct device *dev, const struct iio_dma_buffer_ops *ops)
  532. {
  533. iio_buffer_init(&queue->buffer);
  534. queue->buffer.length = PAGE_SIZE;
  535. queue->buffer.watermark = queue->buffer.length / 2;
  536. queue->dev = dev;
  537. queue->ops = ops;
  538. INIT_LIST_HEAD(&queue->incoming);
  539. INIT_LIST_HEAD(&queue->outgoing);
  540. mutex_init(&queue->lock);
  541. spin_lock_init(&queue->list_lock);
  542. return 0;
  543. }
  544. EXPORT_SYMBOL_GPL(iio_dma_buffer_init);
  545. /**
  546. * iio_dma_buffer_exit() - Cleanup DMA buffer queue
  547. * @queue: Buffer to cleanup
  548. *
  549. * After this function has completed it is safe to free any resources that are
  550. * associated with the buffer and are accessed inside the callback operations.
  551. */
  552. void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue)
  553. {
  554. unsigned int i;
  555. mutex_lock(&queue->lock);
  556. spin_lock_irq(&queue->list_lock);
  557. for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
  558. if (!queue->fileio.blocks[i])
  559. continue;
  560. queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD;
  561. }
  562. INIT_LIST_HEAD(&queue->outgoing);
  563. spin_unlock_irq(&queue->list_lock);
  564. INIT_LIST_HEAD(&queue->incoming);
  565. for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
  566. if (!queue->fileio.blocks[i])
  567. continue;
  568. iio_buffer_block_put(queue->fileio.blocks[i]);
  569. queue->fileio.blocks[i] = NULL;
  570. }
  571. queue->fileio.active_block = NULL;
  572. queue->ops = NULL;
  573. mutex_unlock(&queue->lock);
  574. }
  575. EXPORT_SYMBOL_GPL(iio_dma_buffer_exit);
  576. /**
  577. * iio_dma_buffer_release() - Release final buffer resources
  578. * @queue: Buffer to release
  579. *
  580. * Frees resources that can't yet be freed in iio_dma_buffer_exit(). Should be
  581. * called in the buffers release callback implementation right before freeing
  582. * the memory associated with the buffer.
  583. */
  584. void iio_dma_buffer_release(struct iio_dma_buffer_queue *queue)
  585. {
  586. mutex_destroy(&queue->lock);
  587. }
  588. EXPORT_SYMBOL_GPL(iio_dma_buffer_release);
  589. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  590. MODULE_DESCRIPTION("DMA buffer for the IIO framework");
  591. MODULE_LICENSE("GPL v2");