provider.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. DMAengine controller documentation
  2. ==================================
  3. Hardware Introduction
  4. +++++++++++++++++++++
  5. Most of the Slave DMA controllers have the same general principles of
  6. operations.
  7. They have a given number of channels to use for the DMA transfers, and
  8. a given number of requests lines.
  9. Requests and channels are pretty much orthogonal. Channels can be used
  10. to serve several to any requests. To simplify, channels are the
  11. entities that will be doing the copy, and requests what endpoints are
  12. involved.
  13. The request lines actually correspond to physical lines going from the
  14. DMA-eligible devices to the controller itself. Whenever the device
  15. will want to start a transfer, it will assert a DMA request (DRQ) by
  16. asserting that request line.
  17. A very simple DMA controller would only take into account a single
  18. parameter: the transfer size. At each clock cycle, it would transfer a
  19. byte of data from one buffer to another, until the transfer size has
  20. been reached.
  21. That wouldn't work well in the real world, since slave devices might
  22. require a specific number of bits to be transferred in a single
  23. cycle. For example, we may want to transfer as much data as the
  24. physical bus allows to maximize performances when doing a simple
  25. memory copy operation, but our audio device could have a narrower FIFO
  26. that requires data to be written exactly 16 or 24 bits at a time. This
  27. is why most if not all of the DMA controllers can adjust this, using a
  28. parameter called the transfer width.
  29. Moreover, some DMA controllers, whenever the RAM is used as a source
  30. or destination, can group the reads or writes in memory into a buffer,
  31. so instead of having a lot of small memory accesses, which is not
  32. really efficient, you'll get several bigger transfers. This is done
  33. using a parameter called the burst size, that defines how many single
  34. reads/writes it's allowed to do without the controller splitting the
  35. transfer into smaller sub-transfers.
  36. Our theoretical DMA controller would then only be able to do transfers
  37. that involve a single contiguous block of data. However, some of the
  38. transfers we usually have are not, and want to copy data from
  39. non-contiguous buffers to a contiguous buffer, which is called
  40. scatter-gather.
  41. DMAEngine, at least for mem2dev transfers, require support for
  42. scatter-gather. So we're left with two cases here: either we have a
  43. quite simple DMA controller that doesn't support it, and we'll have to
  44. implement it in software, or we have a more advanced DMA controller,
  45. that implements in hardware scatter-gather.
  46. The latter are usually programmed using a collection of chunks to
  47. transfer, and whenever the transfer is started, the controller will go
  48. over that collection, doing whatever we programmed there.
  49. This collection is usually either a table or a linked list. You will
  50. then push either the address of the table and its number of elements,
  51. or the first item of the list to one channel of the DMA controller,
  52. and whenever a DRQ will be asserted, it will go through the collection
  53. to know where to fetch the data from.
  54. Either way, the format of this collection is completely dependent on
  55. your hardware. Each DMA controller will require a different structure,
  56. but all of them will require, for every chunk, at least the source and
  57. destination addresses, whether it should increment these addresses or
  58. not and the three parameters we saw earlier: the burst size, the
  59. transfer width and the transfer size.
  60. The one last thing is that usually, slave devices won't issue DRQ by
  61. default, and you have to enable this in your slave device driver first
  62. whenever you're willing to use DMA.
  63. These were just the general memory-to-memory (also called mem2mem) or
  64. memory-to-device (mem2dev) kind of transfers. Most devices often
  65. support other kind of transfers or memory operations that dmaengine
  66. support and will be detailed later in this document.
  67. DMA Support in Linux
  68. ++++++++++++++++++++
  69. Historically, DMA controller drivers have been implemented using the
  70. async TX API, to offload operations such as memory copy, XOR,
  71. cryptography, etc., basically any memory to memory operation.
  72. Over time, the need for memory to device transfers arose, and
  73. dmaengine was extended. Nowadays, the async TX API is written as a
  74. layer on top of dmaengine, and acts as a client. Still, dmaengine
  75. accommodates that API in some cases, and made some design choices to
  76. ensure that it stayed compatible.
  77. For more information on the Async TX API, please look the relevant
  78. documentation file in Documentation/crypto/async-tx-api.txt.
  79. DMAEngine Registration
  80. ++++++++++++++++++++++
  81. struct dma_device Initialization
  82. --------------------------------
  83. Just like any other kernel framework, the whole DMAEngine registration
  84. relies on the driver filling a structure and registering against the
  85. framework. In our case, that structure is dma_device.
  86. The first thing you need to do in your driver is to allocate this
  87. structure. Any of the usual memory allocators will do, but you'll also
  88. need to initialize a few fields in there:
  89. * channels: should be initialized as a list using the
  90. INIT_LIST_HEAD macro for example
  91. * src_addr_widths:
  92. - should contain a bitmask of the supported source transfer width
  93. * dst_addr_widths:
  94. - should contain a bitmask of the supported destination transfer
  95. width
  96. * directions:
  97. - should contain a bitmask of the supported slave directions
  98. (i.e. excluding mem2mem transfers)
  99. * residue_granularity:
  100. - Granularity of the transfer residue reported to dma_set_residue.
  101. - This can be either:
  102. + Descriptor
  103. -> Your device doesn't support any kind of residue
  104. reporting. The framework will only know that a particular
  105. transaction descriptor is done.
  106. + Segment
  107. -> Your device is able to report which chunks have been
  108. transferred
  109. + Burst
  110. -> Your device is able to report which burst have been
  111. transferred
  112. * dev: should hold the pointer to the struct device associated
  113. to your current driver instance.
  114. Supported transaction types
  115. ---------------------------
  116. The next thing you need is to set which transaction types your device
  117. (and driver) supports.
  118. Our dma_device structure has a field called cap_mask that holds the
  119. various types of transaction supported, and you need to modify this
  120. mask using the dma_cap_set function, with various flags depending on
  121. transaction types you support as an argument.
  122. All those capabilities are defined in the dma_transaction_type enum,
  123. in include/linux/dmaengine.h
  124. Currently, the types available are:
  125. * DMA_MEMCPY
  126. - The device is able to do memory to memory copies
  127. * DMA_XOR
  128. - The device is able to perform XOR operations on memory areas
  129. - Used to accelerate XOR intensive tasks, such as RAID5
  130. * DMA_XOR_VAL
  131. - The device is able to perform parity check using the XOR
  132. algorithm against a memory buffer.
  133. * DMA_PQ
  134. - The device is able to perform RAID6 P+Q computations, P being a
  135. simple XOR, and Q being a Reed-Solomon algorithm.
  136. * DMA_PQ_VAL
  137. - The device is able to perform parity check using RAID6 P+Q
  138. algorithm against a memory buffer.
  139. * DMA_INTERRUPT
  140. - The device is able to trigger a dummy transfer that will
  141. generate periodic interrupts
  142. - Used by the client drivers to register a callback that will be
  143. called on a regular basis through the DMA controller interrupt
  144. * DMA_SG
  145. - The device supports memory to memory scatter-gather
  146. transfers.
  147. - Even though a plain memcpy can look like a particular case of a
  148. scatter-gather transfer, with a single chunk to transfer, it's a
  149. distinct transaction type in the mem2mem transfers case
  150. * DMA_PRIVATE
  151. - The devices only supports slave transfers, and as such isn't
  152. available for async transfers.
  153. * DMA_ASYNC_TX
  154. - Must not be set by the device, and will be set by the framework
  155. if needed
  156. - /* TODO: What is it about? */
  157. * DMA_SLAVE
  158. - The device can handle device to memory transfers, including
  159. scatter-gather transfers.
  160. - While in the mem2mem case we were having two distinct types to
  161. deal with a single chunk to copy or a collection of them, here,
  162. we just have a single transaction type that is supposed to
  163. handle both.
  164. - If you want to transfer a single contiguous memory buffer,
  165. simply build a scatter list with only one item.
  166. * DMA_CYCLIC
  167. - The device can handle cyclic transfers.
  168. - A cyclic transfer is a transfer where the chunk collection will
  169. loop over itself, with the last item pointing to the first.
  170. - It's usually used for audio transfers, where you want to operate
  171. on a single ring buffer that you will fill with your audio data.
  172. * DMA_INTERLEAVE
  173. - The device supports interleaved transfer.
  174. - These transfers can transfer data from a non-contiguous buffer
  175. to a non-contiguous buffer, opposed to DMA_SLAVE that can
  176. transfer data from a non-contiguous data set to a continuous
  177. destination buffer.
  178. - It's usually used for 2d content transfers, in which case you
  179. want to transfer a portion of uncompressed data directly to the
  180. display to print it
  181. These various types will also affect how the source and destination
  182. addresses change over time.
  183. Addresses pointing to RAM are typically incremented (or decremented)
  184. after each transfer. In case of a ring buffer, they may loop
  185. (DMA_CYCLIC). Addresses pointing to a device's register (e.g. a FIFO)
  186. are typically fixed.
  187. Device operations
  188. -----------------
  189. Our dma_device structure also requires a few function pointers in
  190. order to implement the actual logic, now that we described what
  191. operations we were able to perform.
  192. The functions that we have to fill in there, and hence have to
  193. implement, obviously depend on the transaction types you reported as
  194. supported.
  195. * device_alloc_chan_resources
  196. * device_free_chan_resources
  197. - These functions will be called whenever a driver will call
  198. dma_request_channel or dma_release_channel for the first/last
  199. time on the channel associated to that driver.
  200. - They are in charge of allocating/freeing all the needed
  201. resources in order for that channel to be useful for your
  202. driver.
  203. - These functions can sleep.
  204. * device_prep_dma_*
  205. - These functions are matching the capabilities you registered
  206. previously.
  207. - These functions all take the buffer or the scatterlist relevant
  208. for the transfer being prepared, and should create a hardware
  209. descriptor or a list of hardware descriptors from it
  210. - These functions can be called from an interrupt context
  211. - Any allocation you might do should be using the GFP_NOWAIT
  212. flag, in order not to potentially sleep, but without depleting
  213. the emergency pool either.
  214. - Drivers should try to pre-allocate any memory they might need
  215. during the transfer setup at probe time to avoid putting to
  216. much pressure on the nowait allocator.
  217. - It should return a unique instance of the
  218. dma_async_tx_descriptor structure, that further represents this
  219. particular transfer.
  220. - This structure can be initialized using the function
  221. dma_async_tx_descriptor_init.
  222. - You'll also need to set two fields in this structure:
  223. + flags:
  224. TODO: Can it be modified by the driver itself, or
  225. should it be always the flags passed in the arguments
  226. + tx_submit: A pointer to a function you have to implement,
  227. that is supposed to push the current
  228. transaction descriptor to a pending queue, waiting
  229. for issue_pending to be called.
  230. * device_issue_pending
  231. - Takes the first transaction descriptor in the pending queue,
  232. and starts the transfer. Whenever that transfer is done, it
  233. should move to the next transaction in the list.
  234. - This function can be called in an interrupt context
  235. * device_tx_status
  236. - Should report the bytes left to go over on the given channel
  237. - Should only care about the transaction descriptor passed as
  238. argument, not the currently active one on a given channel
  239. - The tx_state argument might be NULL
  240. - Should use dma_set_residue to report it
  241. - In the case of a cyclic transfer, it should only take into
  242. account the current period.
  243. - This function can be called in an interrupt context.
  244. * device_config
  245. - Reconfigures the channel with the configuration given as
  246. argument
  247. - This command should NOT perform synchronously, or on any
  248. currently queued transfers, but only on subsequent ones
  249. - In this case, the function will receive a dma_slave_config
  250. structure pointer as an argument, that will detail which
  251. configuration to use.
  252. - Even though that structure contains a direction field, this
  253. field is deprecated in favor of the direction argument given to
  254. the prep_* functions
  255. - This call is mandatory for slave operations only. This should NOT be
  256. set or expected to be set for memcpy operations.
  257. If a driver support both, it should use this call for slave
  258. operations only and not for memcpy ones.
  259. * device_pause
  260. - Pauses a transfer on the channel
  261. - This command should operate synchronously on the channel,
  262. pausing right away the work of the given channel
  263. * device_resume
  264. - Resumes a transfer on the channel
  265. - This command should operate synchronously on the channel,
  266. pausing right away the work of the given channel
  267. * device_terminate_all
  268. - Aborts all the pending and ongoing transfers on the channel
  269. - This command should operate synchronously on the channel,
  270. terminating right away all the channels
  271. Misc notes (stuff that should be documented, but don't really know
  272. where to put them)
  273. ------------------------------------------------------------------
  274. * dma_run_dependencies
  275. - Should be called at the end of an async TX transfer, and can be
  276. ignored in the slave transfers case.
  277. - Makes sure that dependent operations are run before marking it
  278. as complete.
  279. * dma_cookie_t
  280. - it's a DMA transaction ID that will increment over time.
  281. - Not really relevant any more since the introduction of virt-dma
  282. that abstracts it away.
  283. * DMA_CTRL_ACK
  284. - If clear, the descriptor cannot be reused by provider until the
  285. client acknowledges receipt, i.e. has has a chance to establish any
  286. dependency chains
  287. - This can be acked by invoking async_tx_ack()
  288. - If set, does not mean descriptor can be reused
  289. * DMA_CTRL_REUSE
  290. - If set, the descriptor can be reused after being completed. It should
  291. not be freed by provider if this flag is set.
  292. - The descriptor should be prepared for reuse by invoking
  293. dmaengine_desc_set_reuse() which will set DMA_CTRL_REUSE.
  294. - dmaengine_desc_set_reuse() will succeed only when channel support
  295. reusable descriptor as exhibited by capablities
  296. - As a consequence, if a device driver wants to skip the dma_map_sg() and
  297. dma_unmap_sg() in between 2 transfers, because the DMA'd data wasn't used,
  298. it can resubmit the transfer right after its completion.
  299. - Descriptor can be freed in few ways
  300. - Clearing DMA_CTRL_REUSE by invoking dmaengine_desc_clear_reuse()
  301. and submitting for last txn
  302. - Explicitly invoking dmaengine_desc_free(), this can succeed only
  303. when DMA_CTRL_REUSE is already set
  304. - Terminating the channel
  305. General Design Notes
  306. --------------------
  307. Most of the DMAEngine drivers you'll see are based on a similar design
  308. that handles the end of transfer interrupts in the handler, but defer
  309. most work to a tasklet, including the start of a new transfer whenever
  310. the previous transfer ended.
  311. This is a rather inefficient design though, because the inter-transfer
  312. latency will be not only the interrupt latency, but also the
  313. scheduling latency of the tasklet, which will leave the channel idle
  314. in between, which will slow down the global transfer rate.
  315. You should avoid this kind of practice, and instead of electing a new
  316. transfer in your tasklet, move that part to the interrupt handler in
  317. order to have a shorter idle window (that we can't really avoid
  318. anyway).
  319. Glossary
  320. --------
  321. Burst: A number of consecutive read or write operations
  322. that can be queued to buffers before being flushed to
  323. memory.
  324. Chunk: A contiguous collection of bursts
  325. Transfer: A collection of chunks (be it contiguous or not)