industrialio-buffer-dma.c 21 KB

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