buffer-dma.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2013-2015 Analog Devices Inc.
  4. * Author: Lars-Peter Clausen <[email protected]>
  5. */
  6. #ifndef __INDUSTRIALIO_DMA_BUFFER_H__
  7. #define __INDUSTRIALIO_DMA_BUFFER_H__
  8. #include <linux/list.h>
  9. #include <linux/kref.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mutex.h>
  12. #include <linux/iio/buffer_impl.h>
  13. struct iio_dma_buffer_queue;
  14. struct iio_dma_buffer_ops;
  15. struct device;
  16. /**
  17. * enum iio_block_state - State of a struct iio_dma_buffer_block
  18. * @IIO_BLOCK_STATE_DEQUEUED: Block is not queued
  19. * @IIO_BLOCK_STATE_QUEUED: Block is on the incoming queue
  20. * @IIO_BLOCK_STATE_ACTIVE: Block is currently being processed by the DMA
  21. * @IIO_BLOCK_STATE_DONE: Block is on the outgoing queue
  22. * @IIO_BLOCK_STATE_DEAD: Block has been marked as to be freed
  23. */
  24. enum iio_block_state {
  25. IIO_BLOCK_STATE_DEQUEUED,
  26. IIO_BLOCK_STATE_QUEUED,
  27. IIO_BLOCK_STATE_ACTIVE,
  28. IIO_BLOCK_STATE_DONE,
  29. IIO_BLOCK_STATE_DEAD,
  30. };
  31. /**
  32. * struct iio_dma_buffer_block - IIO buffer block
  33. * @head: List head
  34. * @size: Total size of the block in bytes
  35. * @bytes_used: Number of bytes that contain valid data
  36. * @vaddr: Virutal address of the blocks memory
  37. * @phys_addr: Physical address of the blocks memory
  38. * @queue: Parent DMA buffer queue
  39. * @kref: kref used to manage the lifetime of block
  40. * @state: Current state of the block
  41. */
  42. struct iio_dma_buffer_block {
  43. /* May only be accessed by the owner of the block */
  44. struct list_head head;
  45. size_t bytes_used;
  46. /*
  47. * Set during allocation, constant thereafter. May be accessed read-only
  48. * by anybody holding a reference to the block.
  49. */
  50. void *vaddr;
  51. dma_addr_t phys_addr;
  52. size_t size;
  53. struct iio_dma_buffer_queue *queue;
  54. /* Must not be accessed outside the core. */
  55. struct kref kref;
  56. /*
  57. * Must not be accessed outside the core. Access needs to hold
  58. * queue->list_lock if the block is not owned by the core.
  59. */
  60. enum iio_block_state state;
  61. };
  62. /**
  63. * struct iio_dma_buffer_queue_fileio - FileIO state for the DMA buffer
  64. * @blocks: Buffer blocks used for fileio
  65. * @active_block: Block being used in read()
  66. * @pos: Read offset in the active block
  67. * @block_size: Size of each block
  68. */
  69. struct iio_dma_buffer_queue_fileio {
  70. struct iio_dma_buffer_block *blocks[2];
  71. struct iio_dma_buffer_block *active_block;
  72. size_t pos;
  73. size_t block_size;
  74. };
  75. /**
  76. * struct iio_dma_buffer_queue - DMA buffer base structure
  77. * @buffer: IIO buffer base structure
  78. * @dev: Parent device
  79. * @ops: DMA buffer callbacks
  80. * @lock: Protects the incoming list, active and the fields in the fileio
  81. * substruct
  82. * @list_lock: Protects lists that contain blocks which can be modified in
  83. * atomic context as well as blocks on those lists. This is the outgoing queue
  84. * list and typically also a list of active blocks in the part that handles
  85. * the DMA controller
  86. * @incoming: List of buffers on the incoming queue
  87. * @outgoing: List of buffers on the outgoing queue
  88. * @active: Whether the buffer is currently active
  89. * @fileio: FileIO state
  90. */
  91. struct iio_dma_buffer_queue {
  92. struct iio_buffer buffer;
  93. struct device *dev;
  94. const struct iio_dma_buffer_ops *ops;
  95. struct mutex lock;
  96. spinlock_t list_lock;
  97. struct list_head incoming;
  98. struct list_head outgoing;
  99. bool active;
  100. struct iio_dma_buffer_queue_fileio fileio;
  101. };
  102. /**
  103. * struct iio_dma_buffer_ops - DMA buffer callback operations
  104. * @submit: Called when a block is submitted to the DMA controller
  105. * @abort: Should abort all pending transfers
  106. */
  107. struct iio_dma_buffer_ops {
  108. int (*submit)(struct iio_dma_buffer_queue *queue,
  109. struct iio_dma_buffer_block *block);
  110. void (*abort)(struct iio_dma_buffer_queue *queue);
  111. };
  112. void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block);
  113. void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue,
  114. struct list_head *list);
  115. int iio_dma_buffer_enable(struct iio_buffer *buffer,
  116. struct iio_dev *indio_dev);
  117. int iio_dma_buffer_disable(struct iio_buffer *buffer,
  118. struct iio_dev *indio_dev);
  119. int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n,
  120. char __user *user_buffer);
  121. size_t iio_dma_buffer_data_available(struct iio_buffer *buffer);
  122. int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd);
  123. int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length);
  124. int iio_dma_buffer_request_update(struct iio_buffer *buffer);
  125. int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue,
  126. struct device *dma_dev, const struct iio_dma_buffer_ops *ops);
  127. void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue);
  128. void iio_dma_buffer_release(struct iio_dma_buffer_queue *queue);
  129. #endif