dma.h 11 KB

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