industrialio-buffer-dma.c 26 KB

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