dma.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2018 Álvaro Fernández Rojas <noltari@gmail.com>
  4. * Copyright (C) 2015 - 2018 Texas Instruments Incorporated <www.ti.com>
  5. * Written by Mugunthan V N <mugunthanvnm@ti.com>
  6. *
  7. */
  8. #ifndef _DMA_H_
  9. #define _DMA_H_
  10. #include <linux/bitops.h>
  11. #include <linux/errno.h>
  12. #include <linux/types.h>
  13. /*
  14. * enum dma_direction - dma transfer direction indicator
  15. * @DMA_MEM_TO_MEM: Memcpy mode
  16. * @DMA_MEM_TO_DEV: From Memory to Device
  17. * @DMA_DEV_TO_MEM: From Device to Memory
  18. * @DMA_DEV_TO_DEV: From Device to Device
  19. */
  20. enum dma_direction {
  21. DMA_MEM_TO_MEM,
  22. DMA_MEM_TO_DEV,
  23. DMA_DEV_TO_MEM,
  24. DMA_DEV_TO_DEV,
  25. };
  26. #define DMA_SUPPORTS_MEM_TO_MEM BIT(0)
  27. #define DMA_SUPPORTS_MEM_TO_DEV BIT(1)
  28. #define DMA_SUPPORTS_DEV_TO_MEM BIT(2)
  29. #define DMA_SUPPORTS_DEV_TO_DEV BIT(3)
  30. /*
  31. * struct dma_dev_priv - information about a device used by the uclass
  32. *
  33. * @supported: mode of transfers that DMA can support, should be
  34. * one/multiple of DMA_SUPPORTS_*
  35. */
  36. struct dma_dev_priv {
  37. u32 supported;
  38. };
  39. #ifdef CONFIG_DMA_CHANNELS
  40. /**
  41. * A DMA is a feature of computer systems that allows certain hardware
  42. * subsystems to access main system memory, independent of the CPU.
  43. * DMA channels are typically generated externally to the HW module
  44. * consuming them, by an entity this API calls a DMA provider. This API
  45. * provides a standard means for drivers to enable and disable DMAs, and to
  46. * copy, send and receive data using DMA.
  47. *
  48. * A driver that implements UCLASS_DMA is a DMA provider. A provider will
  49. * often implement multiple separate DMAs, since the hardware it manages
  50. * often has this capability. dma_uclass.h describes the interface which
  51. * DMA providers must implement.
  52. *
  53. * DMA consumers/clients are the HW modules driven by the DMA channels. This
  54. * header file describes the API used by drivers for those HW modules.
  55. *
  56. * DMA consumer DMA_MEM_TO_DEV (transmit) usage example (based on networking).
  57. * Note. dma_send() is sync operation always - it'll start transfer and will
  58. * poll for it to complete:
  59. * - get/request dma channel
  60. * struct dma dma_tx;
  61. * ret = dma_get_by_name(common->dev, "tx0", &dma_tx);
  62. * if (ret) ...
  63. *
  64. * - enable dma channel
  65. * ret = dma_enable(&dma_tx);
  66. * if (ret) ...
  67. *
  68. * - dma transmit DMA_MEM_TO_DEV.
  69. * struct ti_drv_packet_data packet_data;
  70. *
  71. * packet_data.opt1 = val1;
  72. * packet_data.opt2 = val2;
  73. * ret = dma_send(&dma_tx, packet, length, &packet_data);
  74. * if (ret) ..
  75. *
  76. * DMA consumer DMA_DEV_TO_MEM (receive) usage example (based on networking).
  77. * Note. dma_receive() is sync operation always - it'll start transfer
  78. * (if required) and will poll for it to complete (or for any previously
  79. * configured dev2mem transfer to complete):
  80. * - get/request dma channel
  81. * struct dma dma_rx;
  82. * ret = dma_get_by_name(common->dev, "rx0", &dma_rx);
  83. * if (ret) ...
  84. *
  85. * - enable dma channel
  86. * ret = dma_enable(&dma_rx);
  87. * if (ret) ...
  88. *
  89. * - dma receive DMA_DEV_TO_MEM.
  90. * struct ti_drv_packet_data packet_data;
  91. *
  92. * len = dma_receive(&dma_rx, (void **)packet, &packet_data);
  93. * if (ret < 0) ...
  94. *
  95. * DMA consumer DMA_DEV_TO_MEM (receive) zero-copy usage example (based on
  96. * networking). Networking subsystem allows to configure and use few receive
  97. * buffers (dev2mem), as Networking RX DMA channels usually implemented
  98. * as streaming interface
  99. * - get/request dma channel
  100. * struct dma dma_rx;
  101. * ret = dma_get_by_name(common->dev, "rx0", &dma_rx);
  102. * if (ret) ...
  103. *
  104. * for (i = 0; i < RX_DESC_NUM; i++) {
  105. * ret = dma_prepare_rcv_buf(&dma_rx,
  106. * net_rx_packets[i],
  107. * RX_BUF_SIZE);
  108. * if (ret) ...
  109. * }
  110. *
  111. * - enable dma channel
  112. * ret = dma_enable(&dma_rx);
  113. * if (ret) ...
  114. *
  115. * - dma receive DMA_DEV_TO_MEM.
  116. * struct ti_drv_packet_data packet_data;
  117. *
  118. * len = dma_receive(&dma_rx, (void **)packet, &packet_data);
  119. * if (ret < 0) ..
  120. *
  121. * -- process packet --
  122. *
  123. * - return buffer back to DAM channel
  124. * ret = dma_prepare_rcv_buf(&dma_rx,
  125. * net_rx_packets[rx_next],
  126. * RX_BUF_SIZE);
  127. */
  128. struct udevice;
  129. /**
  130. * struct dma - A handle to (allowing control of) a single DMA.
  131. *
  132. * Clients provide storage for DMA handles. The content of the structure is
  133. * managed solely by the DMA API and DMA drivers. A DMA struct is
  134. * initialized by "get"ing the DMA struct. The DMA struct is passed to all
  135. * other DMA APIs to identify which DMA channel to operate upon.
  136. *
  137. * @dev: The device which implements the DMA channel.
  138. * @id: The DMA channel ID within the provider.
  139. *
  140. * Currently, the DMA API assumes that a single integer ID is enough to
  141. * identify and configure any DMA channel for any DMA provider. If this
  142. * assumption becomes invalid in the future, the struct could be expanded to
  143. * either (a) add more fields to allow DMA providers to store additional
  144. * information, or (b) replace the id field with an opaque pointer, which the
  145. * provider would dynamically allocated during its .of_xlate op, and process
  146. * during is .request op. This may require the addition of an extra op to clean
  147. * up the allocation.
  148. */
  149. struct dma {
  150. struct udevice *dev;
  151. /*
  152. * Written by of_xlate. We assume a single id is enough for now. In the
  153. * future, we might add more fields here.
  154. */
  155. unsigned long id;
  156. };
  157. # if CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DMA)
  158. /**
  159. * dma_get_by_index - Get/request a DMA by integer index.
  160. *
  161. * This looks up and requests a DMA. The index is relative to the client
  162. * device; each device is assumed to have n DMAs associated with it somehow,
  163. * and this function finds and requests one of them. The mapping of client
  164. * device DMA indices to provider DMAs may be via device-tree properties,
  165. * board-provided mapping tables, or some other mechanism.
  166. *
  167. * @dev: The client device.
  168. * @index: The index of the DMA to request, within the client's list of
  169. * DMA channels.
  170. * @dma: A pointer to a DMA struct to initialize.
  171. * @return 0 if OK, or a negative error code.
  172. */
  173. int dma_get_by_index(struct udevice *dev, int index, struct dma *dma);
  174. /**
  175. * dma_get_by_name - Get/request a DMA by name.
  176. *
  177. * This looks up and requests a DMA. The name is relative to the client
  178. * device; each device is assumed to have n DMAs associated with it somehow,
  179. * and this function finds and requests one of them. The mapping of client
  180. * device DMA names to provider DMAs may be via device-tree properties,
  181. * board-provided mapping tables, or some other mechanism.
  182. *
  183. * @dev: The client device.
  184. * @name: The name of the DMA to request, within the client's list of
  185. * DMA channels.
  186. * @dma: A pointer to a DMA struct to initialize.
  187. * @return 0 if OK, or a negative error code.
  188. */
  189. int dma_get_by_name(struct udevice *dev, const char *name, struct dma *dma);
  190. # else
  191. static inline int dma_get_by_index(struct udevice *dev, int index,
  192. struct dma *dma)
  193. {
  194. return -ENOSYS;
  195. }
  196. static inline int dma_get_by_name(struct udevice *dev, const char *name,
  197. struct dma *dma)
  198. {
  199. return -ENOSYS;
  200. }
  201. # endif
  202. /**
  203. * dma_request - Request a DMA by provider-specific ID.
  204. *
  205. * This requests a DMA using a provider-specific ID. Generally, this function
  206. * should not be used, since dma_get_by_index/name() provide an interface that
  207. * better separates clients from intimate knowledge of DMA providers.
  208. * However, this function may be useful in core SoC-specific code.
  209. *
  210. * @dev: The DMA provider device.
  211. * @dma: A pointer to a DMA struct to initialize. The caller must
  212. * have already initialized any field in this struct which the
  213. * DMA provider uses to identify the DMA channel.
  214. * @return 0 if OK, or a negative error code.
  215. */
  216. int dma_request(struct udevice *dev, struct dma *dma);
  217. /**
  218. * dma_free - Free a previously requested DMA.
  219. *
  220. * @dma: A DMA struct that was previously successfully requested by
  221. * dma_request/get_by_*().
  222. * @return 0 if OK, or a negative error code.
  223. */
  224. int dma_free(struct dma *dma);
  225. /**
  226. * dma_enable() - Enable (turn on) a DMA channel.
  227. *
  228. * @dma: A DMA struct that was previously successfully requested by
  229. * dma_request/get_by_*().
  230. * @return zero on success, or -ve error code.
  231. */
  232. int dma_enable(struct dma *dma);
  233. /**
  234. * dma_disable() - Disable (turn off) a DMA channel.
  235. *
  236. * @dma: A DMA struct that was previously successfully requested by
  237. * dma_request/get_by_*().
  238. * @return zero on success, or -ve error code.
  239. */
  240. int dma_disable(struct dma *dma);
  241. /**
  242. * dma_prepare_rcv_buf() - Prepare/add receive DMA buffer.
  243. *
  244. * It allows to implement zero-copy async DMA_DEV_TO_MEM (receive) transactions
  245. * if supported by DMA providers.
  246. *
  247. * @dma: A DMA struct that was previously successfully requested by
  248. * dma_request/get_by_*().
  249. * @dst: The receive buffer pointer.
  250. * @size: The receive buffer size
  251. * @return zero on success, or -ve error code.
  252. */
  253. int dma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size);
  254. /**
  255. * dma_receive() - Receive a DMA transfer.
  256. *
  257. * @dma: A DMA struct that was previously successfully requested by
  258. * dma_request/get_by_*().
  259. * @dst: The destination pointer.
  260. * @metadata: DMA driver's channel specific data
  261. * @return length of received data on success, or zero - no data,
  262. * or -ve error code.
  263. */
  264. int dma_receive(struct dma *dma, void **dst, void *metadata);
  265. /**
  266. * dma_send() - Send a DMA transfer.
  267. *
  268. * @dma: A DMA struct that was previously successfully requested by
  269. * dma_request/get_by_*().
  270. * @src: The source pointer.
  271. * @len: Length of the data to be sent (number of bytes).
  272. * @metadata: DMA driver's channel specific data
  273. * @return zero on success, or -ve error code.
  274. */
  275. int dma_send(struct dma *dma, void *src, size_t len, void *metadata);
  276. /**
  277. * dma_get_cfg() - Get DMA channel configuration for client's use
  278. *
  279. * @dma: The DMA Channel to manipulate
  280. * @cfg_id: DMA provider specific ID to identify what
  281. * configuration data client needs
  282. * @cfg_data: Pointer to store pointer to DMA driver specific
  283. * configuration data for the given cfg_id (output param)
  284. * @return zero on success, or -ve error code.
  285. */
  286. int dma_get_cfg(struct dma *dma, u32 cfg_id, void **cfg_data);
  287. #endif /* CONFIG_DMA_CHANNELS */
  288. #if CONFIG_IS_ENABLED(DMA)
  289. /*
  290. * dma_get_device - get a DMA device which supports transfer
  291. * type of transfer_type
  292. *
  293. * @transfer_type - transfer type should be one/multiple of
  294. * DMA_SUPPORTS_*
  295. * @devp - udevice pointer to return the found device
  296. * @return - will return on success and devp will hold the
  297. * pointer to the device
  298. */
  299. int dma_get_device(u32 transfer_type, struct udevice **devp);
  300. /*
  301. * dma_memcpy - try to use DMA to do a mem copy which will be
  302. * much faster than CPU mem copy
  303. *
  304. * @dst - destination pointer
  305. * @src - souce pointer
  306. * @len - data length to be copied
  307. * @return - on successful transfer returns no of bytes
  308. transferred and on failure return error code.
  309. */
  310. int dma_memcpy(void *dst, void *src, size_t len);
  311. #else
  312. static inline int dma_get_device(u32 transfer_type, struct udevice **devp)
  313. {
  314. return -ENOSYS;
  315. }
  316. static inline int dma_memcpy(void *dst, void *src, size_t len)
  317. {
  318. return -ENOSYS;
  319. }
  320. #endif /* CONFIG_DMA */
  321. #endif /* _DMA_H_ */