dmaengine.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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_state - resource PNP/power management state
  31. * @DMA_RESOURCE_SUSPEND: DMA device going into low power state
  32. * @DMA_RESOURCE_RESUME: DMA device returning to full power
  33. * @DMA_RESOURCE_AVAILABLE: DMA device available to the system
  34. * @DMA_RESOURCE_REMOVED: DMA device removed from the system
  35. */
  36. enum dma_state {
  37. DMA_RESOURCE_SUSPEND,
  38. DMA_RESOURCE_RESUME,
  39. DMA_RESOURCE_AVAILABLE,
  40. DMA_RESOURCE_REMOVED,
  41. };
  42. /**
  43. * enum dma_state_client - state of the channel in the client
  44. * @DMA_ACK: client would like to use, or was using this channel
  45. * @DMA_DUP: client has already seen this channel, or is not using this channel
  46. * @DMA_NAK: client does not want to see any more channels
  47. */
  48. enum dma_state_client {
  49. DMA_ACK,
  50. DMA_DUP,
  51. DMA_NAK,
  52. };
  53. /**
  54. * typedef dma_cookie_t - an opaque DMA cookie
  55. *
  56. * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
  57. */
  58. typedef s32 dma_cookie_t;
  59. #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
  60. /**
  61. * enum dma_status - DMA transaction status
  62. * @DMA_SUCCESS: transaction completed successfully
  63. * @DMA_IN_PROGRESS: transaction not yet processed
  64. * @DMA_ERROR: transaction failed
  65. */
  66. enum dma_status {
  67. DMA_SUCCESS,
  68. DMA_IN_PROGRESS,
  69. DMA_ERROR,
  70. };
  71. /**
  72. * enum dma_transaction_type - DMA transaction types/indexes
  73. */
  74. enum dma_transaction_type {
  75. DMA_MEMCPY,
  76. DMA_XOR,
  77. DMA_PQ_XOR,
  78. DMA_DUAL_XOR,
  79. DMA_PQ_UPDATE,
  80. DMA_ZERO_SUM,
  81. DMA_PQ_ZERO_SUM,
  82. DMA_MEMSET,
  83. DMA_MEMCPY_CRC32C,
  84. DMA_INTERRUPT,
  85. DMA_SLAVE,
  86. };
  87. /* last transaction type for creation of the capabilities mask */
  88. #define DMA_TX_TYPE_END (DMA_SLAVE + 1)
  89. /**
  90. * enum dma_slave_width - DMA slave register access width.
  91. * @DMA_SLAVE_WIDTH_8BIT: Do 8-bit slave register accesses
  92. * @DMA_SLAVE_WIDTH_16BIT: Do 16-bit slave register accesses
  93. * @DMA_SLAVE_WIDTH_32BIT: Do 32-bit slave register accesses
  94. */
  95. enum dma_slave_width {
  96. DMA_SLAVE_WIDTH_8BIT,
  97. DMA_SLAVE_WIDTH_16BIT,
  98. DMA_SLAVE_WIDTH_32BIT,
  99. };
  100. /**
  101. * enum dma_ctrl_flags - DMA flags to augment operation preparation,
  102. * control completion, and communicate status.
  103. * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
  104. * this transaction
  105. * @DMA_CTRL_ACK - the descriptor cannot be reused until the client
  106. * acknowledges receipt, i.e. has has a chance to establish any
  107. * dependency chains
  108. * @DMA_COMPL_SKIP_SRC_UNMAP - set to disable dma-unmapping the source buffer(s)
  109. * @DMA_COMPL_SKIP_DEST_UNMAP - set to disable dma-unmapping the destination(s)
  110. */
  111. enum dma_ctrl_flags {
  112. DMA_PREP_INTERRUPT = (1 << 0),
  113. DMA_CTRL_ACK = (1 << 1),
  114. DMA_COMPL_SKIP_SRC_UNMAP = (1 << 2),
  115. DMA_COMPL_SKIP_DEST_UNMAP = (1 << 3),
  116. };
  117. /**
  118. * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
  119. * See linux/cpumask.h
  120. */
  121. typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
  122. /**
  123. * struct dma_slave - Information about a DMA slave
  124. * @dev: device acting as DMA slave
  125. * @dma_dev: required DMA master device. If non-NULL, the client can not be
  126. * bound to other masters than this.
  127. * @tx_reg: physical address of data register used for
  128. * memory-to-peripheral transfers
  129. * @rx_reg: physical address of data register used for
  130. * peripheral-to-memory transfers
  131. * @reg_width: peripheral register width
  132. *
  133. * If dma_dev is non-NULL, the client can not be bound to other DMA
  134. * masters than the one corresponding to this device. The DMA master
  135. * driver may use this to determine if there is controller-specific
  136. * data wrapped around this struct. Drivers of platform code that sets
  137. * the dma_dev field must therefore make sure to use an appropriate
  138. * controller-specific dma slave structure wrapping this struct.
  139. */
  140. struct dma_slave {
  141. struct device *dev;
  142. struct device *dma_dev;
  143. dma_addr_t tx_reg;
  144. dma_addr_t rx_reg;
  145. enum dma_slave_width reg_width;
  146. };
  147. /**
  148. * struct dma_chan_percpu - the per-CPU part of struct dma_chan
  149. * @refcount: local_t used for open-coded "bigref" counting
  150. * @memcpy_count: transaction counter
  151. * @bytes_transferred: byte counter
  152. */
  153. struct dma_chan_percpu {
  154. local_t refcount;
  155. /* stats */
  156. unsigned long memcpy_count;
  157. unsigned long bytes_transferred;
  158. };
  159. /**
  160. * struct dma_chan - devices supply DMA channels, clients use them
  161. * @device: ptr to the dma device who supplies this channel, always !%NULL
  162. * @cookie: last cookie value returned to client
  163. * @chan_id: channel ID for sysfs
  164. * @class_dev: class device for sysfs
  165. * @refcount: kref, used in "bigref" slow-mode
  166. * @slow_ref: indicates that the DMA channel is free
  167. * @rcu: the DMA channel's RCU head
  168. * @device_node: used to add this to the device chan list
  169. * @local: per-cpu pointer to a struct dma_chan_percpu
  170. * @client-count: how many clients are using this channel
  171. */
  172. struct dma_chan {
  173. struct dma_device *device;
  174. dma_cookie_t cookie;
  175. /* sysfs */
  176. int chan_id;
  177. struct device dev;
  178. struct kref refcount;
  179. int slow_ref;
  180. struct rcu_head rcu;
  181. struct list_head device_node;
  182. struct dma_chan_percpu *local;
  183. int client_count;
  184. };
  185. #define to_dma_chan(p) container_of(p, struct dma_chan, dev)
  186. void dma_chan_cleanup(struct kref *kref);
  187. static inline void dma_chan_get(struct dma_chan *chan)
  188. {
  189. if (unlikely(chan->slow_ref))
  190. kref_get(&chan->refcount);
  191. else {
  192. local_inc(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
  193. put_cpu();
  194. }
  195. }
  196. static inline void dma_chan_put(struct dma_chan *chan)
  197. {
  198. if (unlikely(chan->slow_ref))
  199. kref_put(&chan->refcount, dma_chan_cleanup);
  200. else {
  201. local_dec(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
  202. put_cpu();
  203. }
  204. }
  205. /*
  206. * typedef dma_event_callback - function pointer to a DMA event callback
  207. * For each channel added to the system this routine is called for each client.
  208. * If the client would like to use the channel it returns '1' to signal (ack)
  209. * the dmaengine core to take out a reference on the channel and its
  210. * corresponding device. A client must not 'ack' an available channel more
  211. * than once. When a channel is removed all clients are notified. If a client
  212. * is using the channel it must 'ack' the removal. A client must not 'ack' a
  213. * removed channel more than once.
  214. * @client - 'this' pointer for the client context
  215. * @chan - channel to be acted upon
  216. * @state - available or removed
  217. */
  218. struct dma_client;
  219. typedef enum dma_state_client (*dma_event_callback) (struct dma_client *client,
  220. struct dma_chan *chan, enum dma_state state);
  221. /**
  222. * struct dma_client - info on the entity making use of DMA services
  223. * @event_callback: func ptr to call when something happens
  224. * @cap_mask: only return channels that satisfy the requested capabilities
  225. * a value of zero corresponds to any capability
  226. * @slave: data for preparing slave transfer. Must be non-NULL iff the
  227. * DMA_SLAVE capability is requested.
  228. * @global_node: list_head for global dma_client_list
  229. */
  230. struct dma_client {
  231. dma_event_callback event_callback;
  232. dma_cap_mask_t cap_mask;
  233. struct dma_slave *slave;
  234. struct list_head global_node;
  235. };
  236. typedef void (*dma_async_tx_callback)(void *dma_async_param);
  237. /**
  238. * struct dma_async_tx_descriptor - async transaction descriptor
  239. * ---dma generic offload fields---
  240. * @cookie: tracking cookie for this transaction, set to -EBUSY if
  241. * this tx is sitting on a dependency list
  242. * @flags: flags to augment operation preparation, control completion, and
  243. * communicate status
  244. * @phys: physical address of the descriptor
  245. * @tx_list: driver common field for operations that require multiple
  246. * descriptors
  247. * @chan: target channel for this operation
  248. * @tx_submit: set the prepared descriptor(s) to be executed by the engine
  249. * @callback: routine to call after this operation is complete
  250. * @callback_param: general parameter to pass to the callback routine
  251. * ---async_tx api specific fields---
  252. * @next: at completion submit this descriptor
  253. * @parent: pointer to the next level up in the dependency chain
  254. * @lock: protect the parent and next pointers
  255. */
  256. struct dma_async_tx_descriptor {
  257. dma_cookie_t cookie;
  258. enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
  259. dma_addr_t phys;
  260. struct list_head tx_list;
  261. struct dma_chan *chan;
  262. dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
  263. dma_async_tx_callback callback;
  264. void *callback_param;
  265. struct dma_async_tx_descriptor *next;
  266. struct dma_async_tx_descriptor *parent;
  267. spinlock_t lock;
  268. };
  269. /**
  270. * struct dma_device - info on the entity supplying DMA services
  271. * @chancnt: how many DMA channels are supported
  272. * @channels: the list of struct dma_chan
  273. * @global_node: list_head for global dma_device_list
  274. * @cap_mask: one or more dma_capability flags
  275. * @max_xor: maximum number of xor sources, 0 if no capability
  276. * @refcount: reference count
  277. * @done: IO completion struct
  278. * @dev_id: unique device ID
  279. * @dev: struct device reference for dma mapping api
  280. * @device_alloc_chan_resources: allocate resources and return the
  281. * number of allocated descriptors
  282. * @device_free_chan_resources: release DMA channel's resources
  283. * @device_prep_dma_memcpy: prepares a memcpy operation
  284. * @device_prep_dma_xor: prepares a xor operation
  285. * @device_prep_dma_zero_sum: prepares a zero_sum operation
  286. * @device_prep_dma_memset: prepares a memset operation
  287. * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
  288. * @device_prep_slave_sg: prepares a slave dma operation
  289. * @device_terminate_all: terminate all pending operations
  290. * @device_issue_pending: push pending transactions to hardware
  291. */
  292. struct dma_device {
  293. unsigned int chancnt;
  294. struct list_head channels;
  295. struct list_head global_node;
  296. dma_cap_mask_t cap_mask;
  297. int max_xor;
  298. struct kref refcount;
  299. struct completion done;
  300. int dev_id;
  301. struct device *dev;
  302. int (*device_alloc_chan_resources)(struct dma_chan *chan,
  303. struct dma_client *client);
  304. void (*device_free_chan_resources)(struct dma_chan *chan);
  305. struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
  306. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  307. size_t len, unsigned long flags);
  308. struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
  309. struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  310. unsigned int src_cnt, size_t len, unsigned long flags);
  311. struct dma_async_tx_descriptor *(*device_prep_dma_zero_sum)(
  312. struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
  313. size_t len, u32 *result, unsigned long flags);
  314. struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
  315. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  316. unsigned long flags);
  317. struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
  318. struct dma_chan *chan, unsigned long flags);
  319. struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
  320. struct dma_chan *chan, struct scatterlist *sgl,
  321. unsigned int sg_len, enum dma_data_direction direction,
  322. unsigned long flags);
  323. void (*device_terminate_all)(struct dma_chan *chan);
  324. enum dma_status (*device_is_tx_complete)(struct dma_chan *chan,
  325. dma_cookie_t cookie, dma_cookie_t *last,
  326. dma_cookie_t *used);
  327. void (*device_issue_pending)(struct dma_chan *chan);
  328. };
  329. /* --- public DMA engine API --- */
  330. void dma_async_client_register(struct dma_client *client);
  331. void dma_async_client_unregister(struct dma_client *client);
  332. void dma_async_client_chan_request(struct dma_client *client);
  333. dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
  334. void *dest, void *src, size_t len);
  335. dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
  336. struct page *page, unsigned int offset, void *kdata, size_t len);
  337. dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
  338. struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
  339. unsigned int src_off, size_t len);
  340. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  341. struct dma_chan *chan);
  342. static inline void
  343. async_tx_ack(struct dma_async_tx_descriptor *tx)
  344. {
  345. tx->flags |= DMA_CTRL_ACK;
  346. }
  347. static inline int
  348. async_tx_test_ack(struct dma_async_tx_descriptor *tx)
  349. {
  350. return tx->flags & DMA_CTRL_ACK;
  351. }
  352. #define first_dma_cap(mask) __first_dma_cap(&(mask))
  353. static inline int __first_dma_cap(const dma_cap_mask_t *srcp)
  354. {
  355. return min_t(int, DMA_TX_TYPE_END,
  356. find_first_bit(srcp->bits, DMA_TX_TYPE_END));
  357. }
  358. #define next_dma_cap(n, mask) __next_dma_cap((n), &(mask))
  359. static inline int __next_dma_cap(int n, const dma_cap_mask_t *srcp)
  360. {
  361. return min_t(int, DMA_TX_TYPE_END,
  362. find_next_bit(srcp->bits, DMA_TX_TYPE_END, n+1));
  363. }
  364. #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
  365. static inline void
  366. __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  367. {
  368. set_bit(tx_type, dstp->bits);
  369. }
  370. #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
  371. static inline int
  372. __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
  373. {
  374. return test_bit(tx_type, srcp->bits);
  375. }
  376. #define for_each_dma_cap_mask(cap, mask) \
  377. for ((cap) = first_dma_cap(mask); \
  378. (cap) < DMA_TX_TYPE_END; \
  379. (cap) = next_dma_cap((cap), (mask)))
  380. /**
  381. * dma_async_issue_pending - flush pending transactions to HW
  382. * @chan: target DMA channel
  383. *
  384. * This allows drivers to push copies to HW in batches,
  385. * reducing MMIO writes where possible.
  386. */
  387. static inline void dma_async_issue_pending(struct dma_chan *chan)
  388. {
  389. chan->device->device_issue_pending(chan);
  390. }
  391. #define dma_async_memcpy_issue_pending(chan) dma_async_issue_pending(chan)
  392. /**
  393. * dma_async_is_tx_complete - poll for transaction completion
  394. * @chan: DMA channel
  395. * @cookie: transaction identifier to check status of
  396. * @last: returns last completed cookie, can be NULL
  397. * @used: returns last issued cookie, can be NULL
  398. *
  399. * If @last and @used are passed in, upon return they reflect the driver
  400. * internal state and can be used with dma_async_is_complete() to check
  401. * the status of multiple cookies without re-checking hardware state.
  402. */
  403. static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  404. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  405. {
  406. return chan->device->device_is_tx_complete(chan, cookie, last, used);
  407. }
  408. #define dma_async_memcpy_complete(chan, cookie, last, used)\
  409. dma_async_is_tx_complete(chan, cookie, last, used)
  410. /**
  411. * dma_async_is_complete - test a cookie against chan state
  412. * @cookie: transaction identifier to test status of
  413. * @last_complete: last know completed transaction
  414. * @last_used: last cookie value handed out
  415. *
  416. * dma_async_is_complete() is used in dma_async_memcpy_complete()
  417. * the test logic is separated for lightweight testing of multiple cookies
  418. */
  419. static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  420. dma_cookie_t last_complete, dma_cookie_t last_used)
  421. {
  422. if (last_complete <= last_used) {
  423. if ((cookie <= last_complete) || (cookie > last_used))
  424. return DMA_SUCCESS;
  425. } else {
  426. if ((cookie <= last_complete) && (cookie > last_used))
  427. return DMA_SUCCESS;
  428. }
  429. return DMA_IN_PROGRESS;
  430. }
  431. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
  432. /* --- DMA device --- */
  433. int dma_async_device_register(struct dma_device *device);
  434. void dma_async_device_unregister(struct dma_device *device);
  435. /* --- Helper iov-locking functions --- */
  436. struct dma_page_list {
  437. char __user *base_address;
  438. int nr_pages;
  439. struct page **pages;
  440. };
  441. struct dma_pinned_list {
  442. int nr_iovecs;
  443. struct dma_page_list page_list[0];
  444. };
  445. struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
  446. void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
  447. dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
  448. struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
  449. dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
  450. struct dma_pinned_list *pinned_list, struct page *page,
  451. unsigned int offset, size_t len);
  452. #endif /* DMAENGINE_H */