dmaengine.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/config.h>
  24. #ifdef CONFIG_DMA_ENGINE
  25. #include <linux/device.h>
  26. #include <linux/uio.h>
  27. #include <linux/kref.h>
  28. #include <linux/completion.h>
  29. #include <linux/rcupdate.h>
  30. /**
  31. * enum dma_event - resource PNP/power managment events
  32. * @DMA_RESOURCE_SUSPEND: DMA device going into low power state
  33. * @DMA_RESOURCE_RESUME: DMA device returning to full power
  34. * @DMA_RESOURCE_ADDED: DMA device added to the system
  35. * @DMA_RESOURCE_REMOVED: DMA device removed from the system
  36. */
  37. enum dma_event {
  38. DMA_RESOURCE_SUSPEND,
  39. DMA_RESOURCE_RESUME,
  40. DMA_RESOURCE_ADDED,
  41. DMA_RESOURCE_REMOVED,
  42. };
  43. /**
  44. * typedef dma_cookie_t
  45. *
  46. * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
  47. */
  48. typedef s32 dma_cookie_t;
  49. #define dma_submit_error(cookie) ((cookie) < 0 ? 1 : 0)
  50. /**
  51. * enum dma_status - DMA transaction status
  52. * @DMA_SUCCESS: transaction completed successfully
  53. * @DMA_IN_PROGRESS: transaction not yet processed
  54. * @DMA_ERROR: transaction failed
  55. */
  56. enum dma_status {
  57. DMA_SUCCESS,
  58. DMA_IN_PROGRESS,
  59. DMA_ERROR,
  60. };
  61. /**
  62. * struct dma_chan_percpu - the per-CPU part of struct dma_chan
  63. * @refcount: local_t used for open-coded "bigref" counting
  64. * @memcpy_count: transaction counter
  65. * @bytes_transferred: byte counter
  66. */
  67. struct dma_chan_percpu {
  68. local_t refcount;
  69. /* stats */
  70. unsigned long memcpy_count;
  71. unsigned long bytes_transferred;
  72. };
  73. /**
  74. * struct dma_chan - devices supply DMA channels, clients use them
  75. * @client: ptr to the client user of this chan, will be NULL when unused
  76. * @device: ptr to the dma device who supplies this channel, always !NULL
  77. * @cookie: last cookie value returned to client
  78. * @chan_id:
  79. * @class_dev:
  80. * @refcount: kref, used in "bigref" slow-mode
  81. * @slow_ref:
  82. * @rcu:
  83. * @client_node: used to add this to the client chan list
  84. * @device_node: used to add this to the device chan list
  85. * @local: per-cpu pointer to a struct dma_chan_percpu
  86. */
  87. struct dma_chan {
  88. struct dma_client *client;
  89. struct dma_device *device;
  90. dma_cookie_t cookie;
  91. /* sysfs */
  92. int chan_id;
  93. struct class_device class_dev;
  94. struct kref refcount;
  95. int slow_ref;
  96. struct rcu_head rcu;
  97. struct list_head client_node;
  98. struct list_head device_node;
  99. struct dma_chan_percpu *local;
  100. };
  101. void dma_chan_cleanup(struct kref *kref);
  102. static inline void dma_chan_get(struct dma_chan *chan)
  103. {
  104. if (unlikely(chan->slow_ref))
  105. kref_get(&chan->refcount);
  106. else {
  107. local_inc(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
  108. put_cpu();
  109. }
  110. }
  111. static inline void dma_chan_put(struct dma_chan *chan)
  112. {
  113. if (unlikely(chan->slow_ref))
  114. kref_put(&chan->refcount, dma_chan_cleanup);
  115. else {
  116. local_dec(&(per_cpu_ptr(chan->local, get_cpu())->refcount));
  117. put_cpu();
  118. }
  119. }
  120. /*
  121. * typedef dma_event_callback - function pointer to a DMA event callback
  122. */
  123. typedef void (*dma_event_callback) (struct dma_client *client,
  124. struct dma_chan *chan, enum dma_event event);
  125. /**
  126. * struct dma_client - info on the entity making use of DMA services
  127. * @event_callback: func ptr to call when something happens
  128. * @chan_count: number of chans allocated
  129. * @chans_desired: number of chans requested. Can be +/- chan_count
  130. * @lock: protects access to the channels list
  131. * @channels: the list of DMA channels allocated
  132. * @global_node: list_head for global dma_client_list
  133. */
  134. struct dma_client {
  135. dma_event_callback event_callback;
  136. unsigned int chan_count;
  137. unsigned int chans_desired;
  138. spinlock_t lock;
  139. struct list_head channels;
  140. struct list_head global_node;
  141. };
  142. /**
  143. * struct dma_device - info on the entity supplying DMA services
  144. * @chancnt: how many DMA channels are supported
  145. * @channels: the list of struct dma_chan
  146. * @global_node: list_head for global dma_device_list
  147. * @refcount:
  148. * @done:
  149. * @dev_id:
  150. * Other func ptrs: used to make use of this device's capabilities
  151. */
  152. struct dma_device {
  153. unsigned int chancnt;
  154. struct list_head channels;
  155. struct list_head global_node;
  156. struct kref refcount;
  157. struct completion done;
  158. int dev_id;
  159. int (*device_alloc_chan_resources)(struct dma_chan *chan);
  160. void (*device_free_chan_resources)(struct dma_chan *chan);
  161. dma_cookie_t (*device_memcpy_buf_to_buf)(struct dma_chan *chan,
  162. void *dest, void *src, size_t len);
  163. dma_cookie_t (*device_memcpy_buf_to_pg)(struct dma_chan *chan,
  164. struct page *page, unsigned int offset, void *kdata,
  165. size_t len);
  166. dma_cookie_t (*device_memcpy_pg_to_pg)(struct dma_chan *chan,
  167. struct page *dest_pg, unsigned int dest_off,
  168. struct page *src_pg, unsigned int src_off, size_t len);
  169. enum dma_status (*device_memcpy_complete)(struct dma_chan *chan,
  170. dma_cookie_t cookie, dma_cookie_t *last,
  171. dma_cookie_t *used);
  172. void (*device_memcpy_issue_pending)(struct dma_chan *chan);
  173. };
  174. /* --- public DMA engine API --- */
  175. struct dma_client *dma_async_client_register(dma_event_callback event_callback);
  176. void dma_async_client_unregister(struct dma_client *client);
  177. void dma_async_client_chan_request(struct dma_client *client,
  178. unsigned int number);
  179. /**
  180. * dma_async_memcpy_buf_to_buf - offloaded copy between virtual addresses
  181. * @chan: DMA channel to offload copy to
  182. * @dest: destination address (virtual)
  183. * @src: source address (virtual)
  184. * @len: length
  185. *
  186. * Both @dest and @src must be mappable to a bus address according to the
  187. * DMA mapping API rules for streaming mappings.
  188. * Both @dest and @src must stay memory resident (kernel memory or locked
  189. * user space pages)
  190. */
  191. static inline dma_cookie_t dma_async_memcpy_buf_to_buf(struct dma_chan *chan,
  192. void *dest, void *src, size_t len)
  193. {
  194. int cpu = get_cpu();
  195. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  196. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  197. put_cpu();
  198. return chan->device->device_memcpy_buf_to_buf(chan, dest, src, len);
  199. }
  200. /**
  201. * dma_async_memcpy_buf_to_pg - offloaded copy
  202. * @chan: DMA channel to offload copy to
  203. * @page: destination page
  204. * @offset: offset in page to copy to
  205. * @kdata: source address (virtual)
  206. * @len: length
  207. *
  208. * Both @page/@offset and @kdata must be mappable to a bus address according
  209. * to the DMA mapping API rules for streaming mappings.
  210. * Both @page/@offset and @kdata must stay memory resident (kernel memory or
  211. * locked user space pages)
  212. */
  213. static inline dma_cookie_t dma_async_memcpy_buf_to_pg(struct dma_chan *chan,
  214. struct page *page, unsigned int offset, void *kdata, size_t len)
  215. {
  216. int cpu = get_cpu();
  217. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  218. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  219. put_cpu();
  220. return chan->device->device_memcpy_buf_to_pg(chan, page, offset,
  221. kdata, len);
  222. }
  223. /**
  224. * dma_async_memcpy_buf_to_pg - offloaded copy
  225. * @chan: DMA channel to offload copy to
  226. * @dest_page: destination page
  227. * @dest_off: offset in page to copy to
  228. * @src_page: source page
  229. * @src_off: offset in page to copy from
  230. * @len: length
  231. *
  232. * Both @dest_page/@dest_off and @src_page/@src_off must be mappable to a bus
  233. * address according to the DMA mapping API rules for streaming mappings.
  234. * Both @dest_page/@dest_off and @src_page/@src_off must stay memory resident
  235. * (kernel memory or locked user space pages)
  236. */
  237. static inline dma_cookie_t dma_async_memcpy_pg_to_pg(struct dma_chan *chan,
  238. struct page *dest_pg, unsigned int dest_off, struct page *src_pg,
  239. unsigned int src_off, size_t len)
  240. {
  241. int cpu = get_cpu();
  242. per_cpu_ptr(chan->local, cpu)->bytes_transferred += len;
  243. per_cpu_ptr(chan->local, cpu)->memcpy_count++;
  244. put_cpu();
  245. return chan->device->device_memcpy_pg_to_pg(chan, dest_pg, dest_off,
  246. src_pg, src_off, len);
  247. }
  248. /**
  249. * dma_async_memcpy_issue_pending - flush pending copies to HW
  250. * @chan:
  251. *
  252. * This allows drivers to push copies to HW in batches,
  253. * reducing MMIO writes where possible.
  254. */
  255. static inline void dma_async_memcpy_issue_pending(struct dma_chan *chan)
  256. {
  257. return chan->device->device_memcpy_issue_pending(chan);
  258. }
  259. /**
  260. * dma_async_memcpy_complete - poll for transaction completion
  261. * @chan: DMA channel
  262. * @cookie: transaction identifier to check status of
  263. * @last: returns last completed cookie, can be NULL
  264. * @used: returns last issued cookie, can be NULL
  265. *
  266. * If @last and @used are passed in, upon return they reflect the driver
  267. * internal state and can be used with dma_async_is_complete() to check
  268. * the status of multiple cookies without re-checking hardware state.
  269. */
  270. static inline enum dma_status dma_async_memcpy_complete(struct dma_chan *chan,
  271. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  272. {
  273. return chan->device->device_memcpy_complete(chan, cookie, last, used);
  274. }
  275. /**
  276. * dma_async_is_complete - test a cookie against chan state
  277. * @cookie: transaction identifier to test status of
  278. * @last_complete: last know completed transaction
  279. * @last_used: last cookie value handed out
  280. *
  281. * dma_async_is_complete() is used in dma_async_memcpy_complete()
  282. * the test logic is seperated for lightweight testing of multiple cookies
  283. */
  284. static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  285. dma_cookie_t last_complete, dma_cookie_t last_used)
  286. {
  287. if (last_complete <= last_used) {
  288. if ((cookie <= last_complete) || (cookie > last_used))
  289. return DMA_SUCCESS;
  290. } else {
  291. if ((cookie <= last_complete) && (cookie > last_used))
  292. return DMA_SUCCESS;
  293. }
  294. return DMA_IN_PROGRESS;
  295. }
  296. /* --- DMA device --- */
  297. int dma_async_device_register(struct dma_device *device);
  298. void dma_async_device_unregister(struct dma_device *device);
  299. /* --- Helper iov-locking functions --- */
  300. struct dma_page_list {
  301. char *base_address;
  302. int nr_pages;
  303. struct page **pages;
  304. };
  305. struct dma_pinned_list {
  306. int nr_iovecs;
  307. struct dma_page_list page_list[0];
  308. };
  309. struct dma_pinned_list *dma_pin_iovec_pages(struct iovec *iov, size_t len);
  310. void dma_unpin_iovec_pages(struct dma_pinned_list* pinned_list);
  311. dma_cookie_t dma_memcpy_to_iovec(struct dma_chan *chan, struct iovec *iov,
  312. struct dma_pinned_list *pinned_list, unsigned char *kdata, size_t len);
  313. dma_cookie_t dma_memcpy_pg_to_iovec(struct dma_chan *chan, struct iovec *iov,
  314. struct dma_pinned_list *pinned_list, struct page *page,
  315. unsigned int offset, size_t len);
  316. #endif /* CONFIG_DMA_ENGINE */
  317. #endif /* DMAENGINE_H */