dmaengine.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. #ifndef DMAENGINE_H
  22. #define DMAENGINE_H
  23. #include <linux/device.h>
  24. #include <linux/uio.h>
  25. #include <linux/kref.h>
  26. #include <linux/completion.h>
  27. #include <linux/rcupdate.h>
  28. #include <linux/dma-mapping.h>
  29. /**
  30. * enum dma_event - resource PNP/power managment events
  31. * @DMA_RESOURCE_SUSPEND: DMA device going into low power state
  32. * @DMA_RESOURCE_RESUME: DMA device returning to full power
  33. * @DMA_RESOURCE_ADDED: DMA device added to the system
  34. * @DMA_RESOURCE_REMOVED: DMA device removed from the system
  35. */
  36. enum dma_event {
  37. DMA_RESOURCE_SUSPEND,
  38. DMA_RESOURCE_RESUME,
  39. DMA_RESOURCE_ADDED,
  40. DMA_RESOURCE_REMOVED,
  41. };
  42. /**
  43. * typedef dma_cookie_t - an opaque DMA cookie
  44. *
  45. * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
  46. */
  47. typedef s32 dma_cookie_t;
  48. #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
  49. /**
  50. * enum dma_status - DMA transaction status
  51. * @DMA_SUCCESS: transaction completed successfully
  52. * @DMA_IN_PROGRESS: transaction not yet processed
  53. * @DMA_ERROR: transaction failed
  54. */
  55. enum dma_status {
  56. DMA_SUCCESS,
  57. DMA_IN_PROGRESS,
  58. DMA_ERROR,
  59. };
  60. /**
  61. * enum dma_transaction_type - DMA transaction types/indexes
  62. */
  63. enum dma_transaction_type {
  64. DMA_MEMCPY,
  65. DMA_XOR,
  66. DMA_PQ_XOR,
  67. DMA_DUAL_XOR,
  68. DMA_PQ_UPDATE,
  69. DMA_ZERO_SUM,
  70. DMA_PQ_ZERO_SUM,
  71. DMA_MEMSET,
  72. DMA_MEMCPY_CRC32C,
  73. DMA_INTERRUPT,
  74. };
  75. /* last transaction type for creation of the capabilities mask */
  76. #define DMA_TX_TYPE_END (DMA_INTERRUPT + 1)
  77. /**
  78. * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
  79. * See linux/cpumask.h
  80. */
  81. typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
  82. /**
  83. * struct dma_chan_percpu - the per-CPU part of struct dma_chan
  84. * @refcount: local_t used for open-coded "bigref" counting
  85. * @memcpy_count: transaction counter
  86. * @bytes_transferred: byte counter
  87. */
  88. struct dma_chan_percpu {
  89. local_t refcount;
  90. /* stats */
  91. unsigned long memcpy_count;
  92. unsigned long bytes_transferred;
  93. };
  94. /**
  95. * struct dma_chan - devices supply DMA channels, clients use them
  96. * @client: ptr to the client user of this chan, will be %NULL when unused
  97. * @device: ptr to the dma device who supplies this channel, always !%NULL
  98. * @cookie: last cookie value returned to client
  99. * @chan_id: channel ID for sysfs
  100. * @class_dev: class device for sysfs
  101. * @refcount: kref, used in "bigref" slow-mode
  102. * @slow_ref: indicates that the DMA channel is free
  103. * @rcu: the DMA channel's RCU head
  104. * @client_node: used to add this to the client chan list
  105. * @device_node: used to add this to the device chan list
  106. * @local: per-cpu pointer to a struct dma_chan_percpu
  107. */
  108. struct dma_chan {
  109. struct dma_client *client;
  110. struct dma_device *device;
  111. dma_cookie_t cookie;
  112. /* sysfs */
  113. int chan_id;
  114. struct class_device class_dev;
  115. struct kref refcount;
  116. int slow_ref;
  117. struct rcu_head rcu;
  118. struct list_head client_node;
  119. struct list_head device_node;
  120. struct dma_chan_percpu *local;
  121. };
  122. void dma_chan_cleanup(struct kref *kref);
  123. static inline void dma_chan_get(struct dma_chan *chan)
  124. {
  125. if (unlikely(chan->slow_ref))
  126. kref_get(&chan->refcount);
  127. else {
  128. local_inc(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
  129. put_cpu();
  130. }
  131. }
  132. static inline void dma_chan_put(struct dma_chan *chan)
  133. {
  134. if (unlikely(chan->slow_ref))
  135. kref_put(&chan->refcount, dma_chan_cleanup);
  136. else {
  137. local_dec(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
  138. put_cpu();
  139. }
  140. }
  141. /*
  142. * typedef dma_event_callback - function pointer to a DMA event callback
  143. */
  144. typedef void (*dma_event_callback) (struct dma_client *client,
  145. struct dma_chan *chan, enum dma_event event);
  146. /**
  147. * struct dma_client - info on the entity making use of DMA services
  148. * @event_callback: func ptr to call when something happens
  149. * @chan_count: number of chans allocated
  150. * @chans_desired: number of chans requested. Can be +/- chan_count
  151. * @lock: protects access to the channels list
  152. * @channels: the list of DMA channels allocated
  153. * @global_node: list_head for global dma_client_list
  154. */
  155. struct dma_client {
  156. dma_event_callback event_callback;
  157. unsigned int chan_count;
  158. unsigned int chans_desired;
  159. spinlock_t lock;
  160. struct list_head channels;
  161. struct list_head global_node;
  162. };
  163. typedef void (*dma_async_tx_callback)(void *dma_async_param);
  164. /**
  165. * struct dma_async_tx_descriptor - async transaction descriptor
  166. * ---dma generic offload fields---
  167. * @cookie: tracking cookie for this transaction, set to -EBUSY if
  168. * this tx is sitting on a dependency list
  169. * @ack: the descriptor can not be reused until the client acknowledges
  170. * receipt, i.e. has has a chance to establish any dependency chains
  171. * @phys: physical address of the descriptor
  172. * @tx_list: driver common field for operations that require multiple
  173. * descriptors
  174. * @chan: target channel for this operation
  175. * @tx_submit: set the prepared descriptor(s) to be executed by the engine
  176. * @tx_set_dest: set a destination address in a hardware descriptor
  177. * @tx_set_src: set a source address in a hardware descriptor
  178. * @callback: routine to call after this operation is complete
  179. * @callback_param: general parameter to pass to the callback routine
  180. * ---async_tx api specific fields---
  181. * @depend_list: at completion this list of transactions are submitted
  182. * @depend_node: allow this transaction to be executed after another
  183. * transaction has completed, possibly on another channel
  184. * @parent: pointer to the next level up in the dependency chain
  185. * @lock: protect the dependency list
  186. */
  187. struct dma_async_tx_descriptor {
  188. dma_cookie_t cookie;
  189. int ack;
  190. dma_addr_t phys;
  191. struct list_head tx_list;
  192. struct dma_chan *chan;
  193. dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
  194. void (*tx_set_dest)(dma_addr_t addr,
  195. struct dma_async_tx_descriptor *tx, int index);
  196. void (*tx_set_src)(dma_addr_t addr,
  197. struct dma_async_tx_descriptor *tx, int index);
  198. dma_async_tx_callback callback;
  199. void *callback_param;
  200. struct list_head depend_list;
  201. struct list_head depend_node;
  202. struct dma_async_tx_descriptor *parent;
  203. spinlock_t lock;
  204. };
  205. /**
  206. * struct dma_device - info on the entity supplying DMA services
  207. * @chancnt: how many DMA channels are supported
  208. * @channels: the list of struct dma_chan
  209. * @global_node: list_head for global dma_device_list
  210. * @cap_mask: one or more dma_capability flags
  211. * @max_xor: maximum number of xor sources, 0 if no capability
  212. * @refcount: reference count
  213. * @done: IO completion struct
  214. * @dev_id: unique device ID
  215. * @dev: struct device reference for dma mapping api
  216. * @device_alloc_chan_resources: allocate resources and return the
  217. * number of allocated descriptors
  218. * @device_free_chan_resources: release DMA channel's resources
  219. * @device_prep_dma_memcpy: prepares a memcpy operation
  220. * @device_prep_dma_xor: prepares a xor operation
  221. * @device_prep_dma_zero_sum: prepares a zero_sum operation
  222. * @device_prep_dma_memset: prepares a memset operation
  223. * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
  224. * @device_dependency_added: async_tx notifies the channel about new deps
  225. * @device_issue_pending: push pending transactions to hardware
  226. */
  227. struct dma_device {
  228. unsigned int chancnt;
  229. struct list_head channels;
  230. struct list_head global_node;
  231. dma_cap_mask_t cap_mask;
  232. int max_xor;
  233. struct kref refcount;
  234. struct completion done;
  235. int dev_id;
  236. struct device *dev;
  237. int (*device_alloc_chan_resources)(struct dma_chan *chan);
  238. void (*device_free_chan_resources)(struct dma_chan *chan);
  239. struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
  240. struct dma_chan *chan, size_t len, int int_en);
  241. struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
  242. struct dma_chan *chan, unsigned int src_cnt, size_t len,
  243. int int_en);
  244. struct dma_async_tx_descriptor *(*device_prep_dma_zero_sum)(
  245. struct dma_chan *chan, unsigned int src_cnt, size_t len,
  246. u32 *result, int int_en);
  247. struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
  248. struct dma_chan *chan, int value, size_t len, int int_en);
  249. struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
  250. struct dma_chan *chan);
  251. void (*device_dependency_added)(struct dma_chan *chan);
  252. enum dma_status (*device_is_tx_complete)(struct dma_chan *chan,
  253. dma_cookie_t cookie, dma_cookie_t *last,
  254. dma_cookie_t *used);
  255. void (*device_issue_pending)(struct dma_chan *chan);
  256. };
  257. /* --- public DMA engine API --- */
  258. struct dma_client *dma_async_client_register(dma_event_callback event_callback);
  259. void dma_async_client_unregister(struct dma_client *client);
  260. void dma_async_client_chan_request(struct dma_client *client,
  261. unsigned int number);
  262. dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
  263. void *dest, void *src, size_t len);
  264. dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
  265. struct page *page, unsigned int offset, void *kdata, size_t len);
  266. dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
  267. struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
  268. unsigned int src_off, size_t len);
  269. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  270. struct dma_chan *chan);
  271. static inline void
  272. async_tx_ack(struct dma_async_tx_descriptor *tx)
  273. {
  274. tx->ack = 1;
  275. }
  276. #define first_dma_cap(mask) __first_dma_cap(&(mask))
  277. static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
  278. {
  279. return min_t(int, DMA_TX_TYPE_END,
  280. find_first_bit(srcp->bits, DMA_TX_TYPE_END));
  281. }
  282. #define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
  283. static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
  284. {
  285. return min_t(int, DMA_TX_TYPE_END,
  286. find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
  287. }
  288. #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
  289. static inline void
  290. __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  291. {
  292. set_bit(tx_type, dstp->bits);
  293. }
  294. #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
  295. static inline int
  296. __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
  297. {
  298. return test_bit(tx_type, srcp->bits);
  299. }
  300. #define for_each_dma_cap_mask(cap, mask) \
  301. for ((cap) = first_dma_cap(mask); \
  302. (cap) < DMA_TX_TYPE_END; \
  303. (cap) = next_dma_cap((cap), (mask)))
  304. /**
  305. * dma_async_issue_pending - flush pending transactions to HW
  306. * @chan: target DMA channel
  307. *
  308. * This allows drivers to push copies to HW in batches,
  309. * reducing MMIO writes where possible.
  310. */
  311. static inline void dma_async_issue_pending(struct dma_chan *chan)
  312. {
  313. return chan->device->device_issue_pending(chan);
  314. }
  315. #define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
  316. /**
  317. * dma_async_is_tx_complete - poll for transaction completion
  318. * @chan: DMA channel
  319. * @cookie: transaction identifier to check status of
  320. * @last: returns last completed cookie, can be NULL
  321. * @used: returns last issued cookie, can be NULL
  322. *
  323. * If @last and @used are passed in, upon return they reflect the driver
  324. * internal state and can be used with dma_async_is_complete() to check
  325. * the status of multiple cookies without re-checking hardware state.
  326. */
  327. static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  328. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  329. {
  330. return chan->device->device_is_tx_complete(chan, cookie, last, used);
  331. }
  332. #define dma_async_memcpy_complete(chan, cookie, last, used)\
  333. dma_async_is_tx_complete(chan, cookie, last, used)
  334. /**
  335. * dma_async_is_complete - test a cookie against chan state
  336. * @cookie: transaction identifier to test status of
  337. * @last_complete: last know completed transaction
  338. * @last_used: last cookie value handed out
  339. *
  340. * dma_async_is_complete() is used in dma_async_memcpy_complete()
  341. * the test logic is seperated for lightweight testing of multiple cookies
  342. */
  343. static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  344. dma_cookie_t last_complete, dma_cookie_t last_used)
  345. {
  346. if (last_complete <= last_used) {
  347. if ((cookie <= last_complete) || (cookie > last_used))
  348. return DMA_SUCCESS;
  349. } else {
  350. if ((cookie <= last_complete) && (cookie > last_used))
  351. return DMA_SUCCESS;
  352. }
  353. return DMA_IN_PROGRESS;
  354. }
  355. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
  356. /* --- DMA device --- */
  357. int dma_async_device_register(struct dma_device *device);
  358. void dma_async_device_unregister(struct dma_device *device);
  359. /* --- Helper iov-locking functions --- */
  360. struct dma_page_list {
  361. char *base_address;
  362. int nr_pages;
  363. struct page **pages;
  364. };
  365. struct dma_pinned_list {
  366. int nr_iovecs;
  367. struct dma_page_list page_list[0];
  368. };
  369. struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
  370. void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
  371. dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
  372. struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
  373. dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
  374. struct dma_pinned_list *pinned_list, struct page *page,
  375. unsigned int offset, size_t len);
  376. #endif /* DMAENGINE_H */