dma.h 11 KB

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