industrialio-buffer-dma.c 26 KB

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