dmaengine.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. * The full GNU General Public License is included in this distribution in the
  15. * file called COPYING.
  16. */
  17. #ifndef LINUX_DMAENGINE_H
  18. #define LINUX_DMAENGINE_H
  19. #include <linux/device.h>
  20. #include <linux/err.h>
  21. #include <linux/uio.h>
  22. #include <linux/bug.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/types.h>
  26. #include <asm/page.h>
  27. /**
  28. * typedef dma_cookie_t - an opaque DMA cookie
  29. *
  30. * if dma_cookie_t is >0 it's a DMA request cookie, <0 it's an error code
  31. */
  32. typedef s32 dma_cookie_t;
  33. #define DMA_MIN_COOKIE 1
  34. static inline int dma_submit_error(dma_cookie_t cookie)
  35. {
  36. return cookie < 0 ? cookie : 0;
  37. }
  38. /**
  39. * enum dma_status - DMA transaction status
  40. * @DMA_COMPLETE: transaction completed
  41. * @DMA_IN_PROGRESS: transaction not yet processed
  42. * @DMA_PAUSED: transaction is paused
  43. * @DMA_ERROR: transaction failed
  44. */
  45. enum dma_status {
  46. DMA_COMPLETE,
  47. DMA_IN_PROGRESS,
  48. DMA_PAUSED,
  49. DMA_ERROR,
  50. };
  51. /**
  52. * enum dma_transaction_type - DMA transaction types/indexes
  53. *
  54. * Note: The DMA_ASYNC_TX capability is not to be set by drivers. It is
  55. * automatically set as dma devices are registered.
  56. */
  57. enum dma_transaction_type {
  58. DMA_MEMCPY,
  59. DMA_XOR,
  60. DMA_PQ,
  61. DMA_XOR_VAL,
  62. DMA_PQ_VAL,
  63. DMA_MEMSET,
  64. DMA_MEMSET_SG,
  65. DMA_INTERRUPT,
  66. DMA_SG,
  67. DMA_PRIVATE,
  68. DMA_ASYNC_TX,
  69. DMA_SLAVE,
  70. DMA_CYCLIC,
  71. DMA_INTERLEAVE,
  72. /* last transaction type for creation of the capabilities mask */
  73. DMA_TX_TYPE_END,
  74. };
  75. /**
  76. * enum dma_transfer_direction - dma transfer mode and direction indicator
  77. * @DMA_MEM_TO_MEM: Async/Memcpy mode
  78. * @DMA_MEM_TO_DEV: Slave mode & From Memory to Device
  79. * @DMA_DEV_TO_MEM: Slave mode & From Device to Memory
  80. * @DMA_DEV_TO_DEV: Slave mode & From Device to Device
  81. */
  82. enum dma_transfer_direction {
  83. DMA_MEM_TO_MEM,
  84. DMA_MEM_TO_DEV,
  85. DMA_DEV_TO_MEM,
  86. DMA_DEV_TO_DEV,
  87. DMA_TRANS_NONE,
  88. };
  89. /**
  90. * Interleaved Transfer Request
  91. * ----------------------------
  92. * A chunk is collection of contiguous bytes to be transfered.
  93. * The gap(in bytes) between two chunks is called inter-chunk-gap(ICG).
  94. * ICGs may or maynot change between chunks.
  95. * A FRAME is the smallest series of contiguous {chunk,icg} pairs,
  96. * that when repeated an integral number of times, specifies the transfer.
  97. * A transfer template is specification of a Frame, the number of times
  98. * it is to be repeated and other per-transfer attributes.
  99. *
  100. * Practically, a client driver would have ready a template for each
  101. * type of transfer it is going to need during its lifetime and
  102. * set only 'src_start' and 'dst_start' before submitting the requests.
  103. *
  104. *
  105. * | Frame-1 | Frame-2 | ~ | Frame-'numf' |
  106. * |====....==.===...=...|====....==.===...=...| ~ |====....==.===...=...|
  107. *
  108. * == Chunk size
  109. * ... ICG
  110. */
  111. /**
  112. * struct data_chunk - Element of scatter-gather list that makes a frame.
  113. * @size: Number of bytes to read from source.
  114. * size_dst := fn(op, size_src), so doesn't mean much for destination.
  115. * @icg: Number of bytes to jump after last src/dst address of this
  116. * chunk and before first src/dst address for next chunk.
  117. * Ignored for dst(assumed 0), if dst_inc is true and dst_sgl is false.
  118. * Ignored for src(assumed 0), if src_inc is true and src_sgl is false.
  119. * @dst_icg: Number of bytes to jump after last dst address of this
  120. * chunk and before the first dst address for next chunk.
  121. * Ignored if dst_inc is true and dst_sgl is false.
  122. * @src_icg: Number of bytes to jump after last src address of this
  123. * chunk and before the first src address for next chunk.
  124. * Ignored if src_inc is true and src_sgl is false.
  125. */
  126. struct data_chunk {
  127. size_t size;
  128. size_t icg;
  129. size_t dst_icg;
  130. size_t src_icg;
  131. };
  132. /**
  133. * struct dma_interleaved_template - Template to convey DMAC the transfer pattern
  134. * and attributes.
  135. * @src_start: Bus address of source for the first chunk.
  136. * @dst_start: Bus address of destination for the first chunk.
  137. * @dir: Specifies the type of Source and Destination.
  138. * @src_inc: If the source address increments after reading from it.
  139. * @dst_inc: If the destination address increments after writing to it.
  140. * @src_sgl: If the 'icg' of sgl[] applies to Source (scattered read).
  141. * Otherwise, source is read contiguously (icg ignored).
  142. * Ignored if src_inc is false.
  143. * @dst_sgl: If the 'icg' of sgl[] applies to Destination (scattered write).
  144. * Otherwise, destination is filled contiguously (icg ignored).
  145. * Ignored if dst_inc is false.
  146. * @numf: Number of frames in this template.
  147. * @frame_size: Number of chunks in a frame i.e, size of sgl[].
  148. * @sgl: Array of {chunk,icg} pairs that make up a frame.
  149. */
  150. struct dma_interleaved_template {
  151. dma_addr_t src_start;
  152. dma_addr_t dst_start;
  153. enum dma_transfer_direction dir;
  154. bool src_inc;
  155. bool dst_inc;
  156. bool src_sgl;
  157. bool dst_sgl;
  158. size_t numf;
  159. size_t frame_size;
  160. struct data_chunk sgl[0];
  161. };
  162. /**
  163. * enum dma_ctrl_flags - DMA flags to augment operation preparation,
  164. * control completion, and communicate status.
  165. * @DMA_PREP_INTERRUPT - trigger an interrupt (callback) upon completion of
  166. * this transaction
  167. * @DMA_CTRL_ACK - if clear, the descriptor cannot be reused until the client
  168. * acknowledges receipt, i.e. has has a chance to establish any dependency
  169. * chains
  170. * @DMA_PREP_PQ_DISABLE_P - prevent generation of P while generating Q
  171. * @DMA_PREP_PQ_DISABLE_Q - prevent generation of Q while generating P
  172. * @DMA_PREP_CONTINUE - indicate to a driver that it is reusing buffers as
  173. * sources that were the result of a previous operation, in the case of a PQ
  174. * operation it continues the calculation with new sources
  175. * @DMA_PREP_FENCE - tell the driver that subsequent operations depend
  176. * on the result of this operation
  177. * @DMA_CTRL_REUSE: client can reuse the descriptor and submit again till
  178. * cleared or freed
  179. */
  180. enum dma_ctrl_flags {
  181. DMA_PREP_INTERRUPT = (1 << 0),
  182. DMA_CTRL_ACK = (1 << 1),
  183. DMA_PREP_PQ_DISABLE_P = (1 << 2),
  184. DMA_PREP_PQ_DISABLE_Q = (1 << 3),
  185. DMA_PREP_CONTINUE = (1 << 4),
  186. DMA_PREP_FENCE = (1 << 5),
  187. DMA_CTRL_REUSE = (1 << 6),
  188. };
  189. /**
  190. * enum sum_check_bits - bit position of pq_check_flags
  191. */
  192. enum sum_check_bits {
  193. SUM_CHECK_P = 0,
  194. SUM_CHECK_Q = 1,
  195. };
  196. /**
  197. * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
  198. * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
  199. * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
  200. */
  201. enum sum_check_flags {
  202. SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
  203. SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
  204. };
  205. /**
  206. * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
  207. * See linux/cpumask.h
  208. */
  209. typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
  210. /**
  211. * struct dma_chan_percpu - the per-CPU part of struct dma_chan
  212. * @memcpy_count: transaction counter
  213. * @bytes_transferred: byte counter
  214. */
  215. struct dma_chan_percpu {
  216. /* stats */
  217. unsigned long memcpy_count;
  218. unsigned long bytes_transferred;
  219. };
  220. /**
  221. * struct dma_router - DMA router structure
  222. * @dev: pointer to the DMA router device
  223. * @route_free: function to be called when the route can be disconnected
  224. */
  225. struct dma_router {
  226. struct device *dev;
  227. void (*route_free)(struct device *dev, void *route_data);
  228. };
  229. /**
  230. * struct dma_chan - devices supply DMA channels, clients use them
  231. * @device: ptr to the dma device who supplies this channel, always !%NULL
  232. * @cookie: last cookie value returned to client
  233. * @completed_cookie: last completed cookie for this channel
  234. * @chan_id: channel ID for sysfs
  235. * @dev: class device for sysfs
  236. * @device_node: used to add this to the device chan list
  237. * @local: per-cpu pointer to a struct dma_chan_percpu
  238. * @client_count: how many clients are using this channel
  239. * @table_count: number of appearances in the mem-to-mem allocation table
  240. * @router: pointer to the DMA router structure
  241. * @route_data: channel specific data for the router
  242. * @private: private data for certain client-channel associations
  243. */
  244. struct dma_chan {
  245. struct dma_device *device;
  246. dma_cookie_t cookie;
  247. dma_cookie_t completed_cookie;
  248. /* sysfs */
  249. int chan_id;
  250. struct dma_chan_dev *dev;
  251. struct list_head device_node;
  252. struct dma_chan_percpu __percpu *local;
  253. int client_count;
  254. int table_count;
  255. /* DMA router */
  256. struct dma_router *router;
  257. void *route_data;
  258. void *private;
  259. };
  260. /**
  261. * struct dma_chan_dev - relate sysfs device node to backing channel device
  262. * @chan: driver channel device
  263. * @device: sysfs device
  264. * @dev_id: parent dma_device dev_id
  265. * @idr_ref: reference count to gate release of dma_device dev_id
  266. */
  267. struct dma_chan_dev {
  268. struct dma_chan *chan;
  269. struct device device;
  270. int dev_id;
  271. atomic_t *idr_ref;
  272. };
  273. /**
  274. * enum dma_slave_buswidth - defines bus width of the DMA slave
  275. * device, source or target buses
  276. */
  277. enum dma_slave_buswidth {
  278. DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
  279. DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
  280. DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
  281. DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
  282. DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
  283. DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
  284. DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
  285. DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
  286. DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
  287. };
  288. /**
  289. * struct dma_slave_config - dma slave channel runtime config
  290. * @direction: whether the data shall go in or out on this slave
  291. * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
  292. * legal values. DEPRECATED, drivers should use the direction argument
  293. * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
  294. * the dir field in the dma_interleaved_template structure.
  295. * @src_addr: this is the physical address where DMA slave data
  296. * should be read (RX), if the source is memory this argument is
  297. * ignored.
  298. * @dst_addr: this is the physical address where DMA slave data
  299. * should be written (TX), if the source is memory this argument
  300. * is ignored.
  301. * @src_addr_width: this is the width in bytes of the source (RX)
  302. * register where DMA data shall be read. If the source
  303. * is memory this may be ignored depending on architecture.
  304. * Legal values: 1, 2, 4, 8.
  305. * @dst_addr_width: same as src_addr_width but for destination
  306. * target (TX) mutatis mutandis.
  307. * @src_maxburst: the maximum number of words (note: words, as in
  308. * units of the src_addr_width member, not bytes) that can be sent
  309. * in one burst to the device. Typically something like half the
  310. * FIFO depth on I/O peripherals so you don't overflow it. This
  311. * may or may not be applicable on memory sources.
  312. * @dst_maxburst: same as src_maxburst but for destination target
  313. * mutatis mutandis.
  314. * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
  315. * with 'true' if peripheral should be flow controller. Direction will be
  316. * selected at Runtime.
  317. * @slave_id: Slave requester id. Only valid for slave channels. The dma
  318. * slave peripheral will have unique id as dma requester which need to be
  319. * pass as slave config.
  320. *
  321. * This struct is passed in as configuration data to a DMA engine
  322. * in order to set up a certain channel for DMA transport at runtime.
  323. * The DMA device/engine has to provide support for an additional
  324. * callback in the dma_device structure, device_config and this struct
  325. * will then be passed in as an argument to the function.
  326. *
  327. * The rationale for adding configuration information to this struct is as
  328. * follows: if it is likely that more than one DMA slave controllers in
  329. * the world will support the configuration option, then make it generic.
  330. * If not: if it is fixed so that it be sent in static from the platform
  331. * data, then prefer to do that.
  332. */
  333. struct dma_slave_config {
  334. enum dma_transfer_direction direction;
  335. dma_addr_t src_addr;
  336. dma_addr_t dst_addr;
  337. enum dma_slave_buswidth src_addr_width;
  338. enum dma_slave_buswidth dst_addr_width;
  339. u32 src_maxburst;
  340. u32 dst_maxburst;
  341. bool device_fc;
  342. unsigned int slave_id;
  343. };
  344. /**
  345. * enum dma_residue_granularity - Granularity of the reported transfer residue
  346. * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
  347. * DMA channel is only able to tell whether a descriptor has been completed or
  348. * not, which means residue reporting is not supported by this channel. The
  349. * residue field of the dma_tx_state field will always be 0.
  350. * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
  351. * completed segment of the transfer (For cyclic transfers this is after each
  352. * period). This is typically implemented by having the hardware generate an
  353. * interrupt after each transferred segment and then the drivers updates the
  354. * outstanding residue by the size of the segment. Another possibility is if
  355. * the hardware supports scatter-gather and the segment descriptor has a field
  356. * which gets set after the segment has been completed. The driver then counts
  357. * the number of segments without the flag set to compute the residue.
  358. * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
  359. * burst. This is typically only supported if the hardware has a progress
  360. * register of some sort (E.g. a register with the current read/write address
  361. * or a register with the amount of bursts/beats/bytes that have been
  362. * transferred or still need to be transferred).
  363. */
  364. enum dma_residue_granularity {
  365. DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
  366. DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
  367. DMA_RESIDUE_GRANULARITY_BURST = 2,
  368. };
  369. /* struct dma_slave_caps - expose capabilities of a slave channel only
  370. *
  371. * @src_addr_widths: bit mask of src addr widths the channel supports
  372. * @dst_addr_widths: bit mask of dstn addr widths the channel supports
  373. * @directions: bit mask of slave direction the channel supported
  374. * since the enum dma_transfer_direction is not defined as bits for each
  375. * type of direction, the dma controller should fill (1 << <TYPE>) and same
  376. * should be checked by controller as well
  377. * @cmd_pause: true, if pause and thereby resume is supported
  378. * @cmd_terminate: true, if terminate cmd is supported
  379. * @residue_granularity: granularity of the reported transfer residue
  380. * @descriptor_reuse: if a descriptor can be reused by client and
  381. * resubmitted multiple times
  382. */
  383. struct dma_slave_caps {
  384. u32 src_addr_widths;
  385. u32 dst_addr_widths;
  386. u32 directions;
  387. bool cmd_pause;
  388. bool cmd_terminate;
  389. enum dma_residue_granularity residue_granularity;
  390. bool descriptor_reuse;
  391. };
  392. static inline const char *dma_chan_name(struct dma_chan *chan)
  393. {
  394. return dev_name(&chan->dev->device);
  395. }
  396. void dma_chan_cleanup(struct kref *kref);
  397. /**
  398. * typedef dma_filter_fn - callback filter for dma_request_channel
  399. * @chan: channel to be reviewed
  400. * @filter_param: opaque parameter passed through dma_request_channel
  401. *
  402. * When this optional parameter is specified in a call to dma_request_channel a
  403. * suitable channel is passed to this routine for further dispositioning before
  404. * being returned. Where 'suitable' indicates a non-busy channel that
  405. * satisfies the given capability mask. It returns 'true' to indicate that the
  406. * channel is suitable.
  407. */
  408. typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
  409. typedef void (*dma_async_tx_callback)(void *dma_async_param);
  410. struct dmaengine_unmap_data {
  411. u8 map_cnt;
  412. u8 to_cnt;
  413. u8 from_cnt;
  414. u8 bidi_cnt;
  415. struct device *dev;
  416. struct kref kref;
  417. size_t len;
  418. dma_addr_t addr[0];
  419. };
  420. /**
  421. * struct dma_async_tx_descriptor - async transaction descriptor
  422. * ---dma generic offload fields---
  423. * @cookie: tracking cookie for this transaction, set to -EBUSY if
  424. * this tx is sitting on a dependency list
  425. * @flags: flags to augment operation preparation, control completion, and
  426. * communicate status
  427. * @phys: physical address of the descriptor
  428. * @chan: target channel for this operation
  429. * @tx_submit: accept the descriptor, assign ordered cookie and mark the
  430. * descriptor pending. To be pushed on .issue_pending() call
  431. * @callback: routine to call after this operation is complete
  432. * @callback_param: general parameter to pass to the callback routine
  433. * ---async_tx api specific fields---
  434. * @next: at completion submit this descriptor
  435. * @parent: pointer to the next level up in the dependency chain
  436. * @lock: protect the parent and next pointers
  437. */
  438. struct dma_async_tx_descriptor {
  439. dma_cookie_t cookie;
  440. enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
  441. dma_addr_t phys;
  442. struct dma_chan *chan;
  443. dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
  444. int (*desc_free)(struct dma_async_tx_descriptor *tx);
  445. dma_async_tx_callback callback;
  446. void *callback_param;
  447. struct dmaengine_unmap_data *unmap;
  448. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  449. struct dma_async_tx_descriptor *next;
  450. struct dma_async_tx_descriptor *parent;
  451. spinlock_t lock;
  452. #endif
  453. };
  454. #ifdef CONFIG_DMA_ENGINE
  455. static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  456. struct dmaengine_unmap_data *unmap)
  457. {
  458. kref_get(&unmap->kref);
  459. tx->unmap = unmap;
  460. }
  461. struct dmaengine_unmap_data *
  462. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
  463. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
  464. #else
  465. static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  466. struct dmaengine_unmap_data *unmap)
  467. {
  468. }
  469. static inline struct dmaengine_unmap_data *
  470. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  471. {
  472. return NULL;
  473. }
  474. static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  475. {
  476. }
  477. #endif
  478. static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
  479. {
  480. if (tx->unmap) {
  481. dmaengine_unmap_put(tx->unmap);
  482. tx->unmap = NULL;
  483. }
  484. }
  485. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  486. static inline void txd_lock(struct dma_async_tx_descriptor *txd)
  487. {
  488. }
  489. static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
  490. {
  491. }
  492. static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  493. {
  494. BUG();
  495. }
  496. static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  497. {
  498. }
  499. static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
  500. {
  501. }
  502. static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  503. {
  504. return NULL;
  505. }
  506. static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  507. {
  508. return NULL;
  509. }
  510. #else
  511. static inline void txd_lock(struct dma_async_tx_descriptor *txd)
  512. {
  513. spin_lock_bh(&txd->lock);
  514. }
  515. static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
  516. {
  517. spin_unlock_bh(&txd->lock);
  518. }
  519. static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  520. {
  521. txd->next = next;
  522. next->parent = txd;
  523. }
  524. static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  525. {
  526. txd->parent = NULL;
  527. }
  528. static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
  529. {
  530. txd->next = NULL;
  531. }
  532. static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  533. {
  534. return txd->parent;
  535. }
  536. static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  537. {
  538. return txd->next;
  539. }
  540. #endif
  541. /**
  542. * struct dma_tx_state - filled in to report the status of
  543. * a transfer.
  544. * @last: last completed DMA cookie
  545. * @used: last issued DMA cookie (i.e. the one in progress)
  546. * @residue: the remaining number of bytes left to transmit
  547. * on the selected transfer for states DMA_IN_PROGRESS and
  548. * DMA_PAUSED if this is implemented in the driver, else 0
  549. */
  550. struct dma_tx_state {
  551. dma_cookie_t last;
  552. dma_cookie_t used;
  553. u32 residue;
  554. };
  555. /**
  556. * enum dmaengine_alignment - defines alignment of the DMA async tx
  557. * buffers
  558. */
  559. enum dmaengine_alignment {
  560. DMAENGINE_ALIGN_1_BYTE = 0,
  561. DMAENGINE_ALIGN_2_BYTES = 1,
  562. DMAENGINE_ALIGN_4_BYTES = 2,
  563. DMAENGINE_ALIGN_8_BYTES = 3,
  564. DMAENGINE_ALIGN_16_BYTES = 4,
  565. DMAENGINE_ALIGN_32_BYTES = 5,
  566. DMAENGINE_ALIGN_64_BYTES = 6,
  567. };
  568. /**
  569. * struct dma_device - info on the entity supplying DMA services
  570. * @chancnt: how many DMA channels are supported
  571. * @privatecnt: how many DMA channels are requested by dma_request_channel
  572. * @channels: the list of struct dma_chan
  573. * @global_node: list_head for global dma_device_list
  574. * @cap_mask: one or more dma_capability flags
  575. * @max_xor: maximum number of xor sources, 0 if no capability
  576. * @max_pq: maximum number of PQ sources and PQ-continue capability
  577. * @copy_align: alignment shift for memcpy operations
  578. * @xor_align: alignment shift for xor operations
  579. * @pq_align: alignment shift for pq operations
  580. * @fill_align: alignment shift for memset operations
  581. * @dev_id: unique device ID
  582. * @dev: struct device reference for dma mapping api
  583. * @src_addr_widths: bit mask of src addr widths the device supports
  584. * @dst_addr_widths: bit mask of dst addr widths the device supports
  585. * @directions: bit mask of slave direction the device supports since
  586. * the enum dma_transfer_direction is not defined as bits for
  587. * each type of direction, the dma controller should fill (1 <<
  588. * <TYPE>) and same should be checked by controller as well
  589. * @residue_granularity: granularity of the transfer residue reported
  590. * by tx_status
  591. * @device_alloc_chan_resources: allocate resources and return the
  592. * number of allocated descriptors
  593. * @device_free_chan_resources: release DMA channel's resources
  594. * @device_prep_dma_memcpy: prepares a memcpy operation
  595. * @device_prep_dma_xor: prepares a xor operation
  596. * @device_prep_dma_xor_val: prepares a xor validation operation
  597. * @device_prep_dma_pq: prepares a pq operation
  598. * @device_prep_dma_pq_val: prepares a pqzero_sum operation
  599. * @device_prep_dma_memset: prepares a memset operation
  600. * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
  601. * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
  602. * @device_prep_slave_sg: prepares a slave dma operation
  603. * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
  604. * The function takes a buffer of size buf_len. The callback function will
  605. * be called after period_len bytes have been transferred.
  606. * @device_prep_interleaved_dma: Transfer expression in a generic way.
  607. * @device_config: Pushes a new configuration to a channel, return 0 or an error
  608. * code
  609. * @device_pause: Pauses any transfer happening on a channel. Returns
  610. * 0 or an error code
  611. * @device_resume: Resumes any transfer on a channel previously
  612. * paused. Returns 0 or an error code
  613. * @device_terminate_all: Aborts all transfers on a channel. Returns 0
  614. * or an error code
  615. * @device_tx_status: poll for transaction completion, the optional
  616. * txstate parameter can be supplied with a pointer to get a
  617. * struct with auxiliary transfer status information, otherwise the call
  618. * will just return a simple status code
  619. * @device_issue_pending: push pending transactions to hardware
  620. */
  621. struct dma_device {
  622. unsigned int chancnt;
  623. unsigned int privatecnt;
  624. struct list_head channels;
  625. struct list_head global_node;
  626. dma_cap_mask_t cap_mask;
  627. unsigned short max_xor;
  628. unsigned short max_pq;
  629. enum dmaengine_alignment copy_align;
  630. enum dmaengine_alignment xor_align;
  631. enum dmaengine_alignment pq_align;
  632. enum dmaengine_alignment fill_align;
  633. #define DMA_HAS_PQ_CONTINUE (1 << 15)
  634. int dev_id;
  635. struct device *dev;
  636. u32 src_addr_widths;
  637. u32 dst_addr_widths;
  638. u32 directions;
  639. enum dma_residue_granularity residue_granularity;
  640. int (*device_alloc_chan_resources)(struct dma_chan *chan);
  641. void (*device_free_chan_resources)(struct dma_chan *chan);
  642. struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
  643. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  644. size_t len, unsigned long flags);
  645. struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
  646. struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  647. unsigned int src_cnt, size_t len, unsigned long flags);
  648. struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
  649. struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
  650. size_t len, enum sum_check_flags *result, unsigned long flags);
  651. struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
  652. struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  653. unsigned int src_cnt, const unsigned char *scf,
  654. size_t len, unsigned long flags);
  655. struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
  656. struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  657. unsigned int src_cnt, const unsigned char *scf, size_t len,
  658. enum sum_check_flags *pqres, unsigned long flags);
  659. struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
  660. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  661. unsigned long flags);
  662. struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
  663. struct dma_chan *chan, struct scatterlist *sg,
  664. unsigned int nents, int value, unsigned long flags);
  665. struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
  666. struct dma_chan *chan, unsigned long flags);
  667. struct dma_async_tx_descriptor *(*device_prep_dma_sg)(
  668. struct dma_chan *chan,
  669. struct scatterlist *dst_sg, unsigned int dst_nents,
  670. struct scatterlist *src_sg, unsigned int src_nents,
  671. unsigned long flags);
  672. struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
  673. struct dma_chan *chan, struct scatterlist *sgl,
  674. unsigned int sg_len, enum dma_transfer_direction direction,
  675. unsigned long flags, void *context);
  676. struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
  677. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  678. size_t period_len, enum dma_transfer_direction direction,
  679. unsigned long flags);
  680. struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
  681. struct dma_chan *chan, struct dma_interleaved_template *xt,
  682. unsigned long flags);
  683. int (*device_config)(struct dma_chan *chan,
  684. struct dma_slave_config *config);
  685. int (*device_pause)(struct dma_chan *chan);
  686. int (*device_resume)(struct dma_chan *chan);
  687. int (*device_terminate_all)(struct dma_chan *chan);
  688. enum dma_status (*device_tx_status)(struct dma_chan *chan,
  689. dma_cookie_t cookie,
  690. struct dma_tx_state *txstate);
  691. void (*device_issue_pending)(struct dma_chan *chan);
  692. };
  693. static inline int dmaengine_slave_config(struct dma_chan *chan,
  694. struct dma_slave_config *config)
  695. {
  696. if (chan->device->device_config)
  697. return chan->device->device_config(chan, config);
  698. return -ENOSYS;
  699. }
  700. static inline bool is_slave_direction(enum dma_transfer_direction direction)
  701. {
  702. return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
  703. }
  704. static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
  705. struct dma_chan *chan, dma_addr_t buf, size_t len,
  706. enum dma_transfer_direction dir, unsigned long flags)
  707. {
  708. struct scatterlist sg;
  709. sg_init_table(&sg, 1);
  710. sg_dma_address(&sg) = buf;
  711. sg_dma_len(&sg) = len;
  712. return chan->device->device_prep_slave_sg(chan, &sg, 1,
  713. dir, flags, NULL);
  714. }
  715. static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
  716. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  717. enum dma_transfer_direction dir, unsigned long flags)
  718. {
  719. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  720. dir, flags, NULL);
  721. }
  722. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  723. struct rio_dma_ext;
  724. static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
  725. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  726. enum dma_transfer_direction dir, unsigned long flags,
  727. struct rio_dma_ext *rio_ext)
  728. {
  729. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  730. dir, flags, rio_ext);
  731. }
  732. #endif
  733. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
  734. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  735. size_t period_len, enum dma_transfer_direction dir,
  736. unsigned long flags)
  737. {
  738. return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
  739. period_len, dir, flags);
  740. }
  741. static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
  742. struct dma_chan *chan, struct dma_interleaved_template *xt,
  743. unsigned long flags)
  744. {
  745. return chan->device->device_prep_interleaved_dma(chan, xt, flags);
  746. }
  747. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
  748. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  749. unsigned long flags)
  750. {
  751. if (!chan || !chan->device)
  752. return NULL;
  753. return chan->device->device_prep_dma_memset(chan, dest, value,
  754. len, flags);
  755. }
  756. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_sg(
  757. struct dma_chan *chan,
  758. struct scatterlist *dst_sg, unsigned int dst_nents,
  759. struct scatterlist *src_sg, unsigned int src_nents,
  760. unsigned long flags)
  761. {
  762. return chan->device->device_prep_dma_sg(chan, dst_sg, dst_nents,
  763. src_sg, src_nents, flags);
  764. }
  765. static inline int dmaengine_terminate_all(struct dma_chan *chan)
  766. {
  767. if (chan->device->device_terminate_all)
  768. return chan->device->device_terminate_all(chan);
  769. return -ENOSYS;
  770. }
  771. static inline int dmaengine_pause(struct dma_chan *chan)
  772. {
  773. if (chan->device->device_pause)
  774. return chan->device->device_pause(chan);
  775. return -ENOSYS;
  776. }
  777. static inline int dmaengine_resume(struct dma_chan *chan)
  778. {
  779. if (chan->device->device_resume)
  780. return chan->device->device_resume(chan);
  781. return -ENOSYS;
  782. }
  783. static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
  784. dma_cookie_t cookie, struct dma_tx_state *state)
  785. {
  786. return chan->device->device_tx_status(chan, cookie, state);
  787. }
  788. static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
  789. {
  790. return desc->tx_submit(desc);
  791. }
  792. static inline bool dmaengine_check_align(enum dmaengine_alignment align,
  793. size_t off1, size_t off2, size_t len)
  794. {
  795. size_t mask;
  796. if (!align)
  797. return true;
  798. mask = (1 << align) - 1;
  799. if (mask & (off1 | off2 | len))
  800. return false;
  801. return true;
  802. }
  803. static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
  804. size_t off2, size_t len)
  805. {
  806. return dmaengine_check_align(dev->copy_align, off1, off2, len);
  807. }
  808. static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
  809. size_t off2, size_t len)
  810. {
  811. return dmaengine_check_align(dev->xor_align, off1, off2, len);
  812. }
  813. static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
  814. size_t off2, size_t len)
  815. {
  816. return dmaengine_check_align(dev->pq_align, off1, off2, len);
  817. }
  818. static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
  819. size_t off2, size_t len)
  820. {
  821. return dmaengine_check_align(dev->fill_align, off1, off2, len);
  822. }
  823. static inline void
  824. dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
  825. {
  826. dma->max_pq = maxpq;
  827. if (has_pq_continue)
  828. dma->max_pq |= DMA_HAS_PQ_CONTINUE;
  829. }
  830. static inline bool dmaf_continue(enum dma_ctrl_flags flags)
  831. {
  832. return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
  833. }
  834. static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
  835. {
  836. enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
  837. return (flags & mask) == mask;
  838. }
  839. static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
  840. {
  841. return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
  842. }
  843. static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
  844. {
  845. return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
  846. }
  847. /* dma_maxpq - reduce maxpq in the face of continued operations
  848. * @dma - dma device with PQ capability
  849. * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
  850. *
  851. * When an engine does not support native continuation we need 3 extra
  852. * source slots to reuse P and Q with the following coefficients:
  853. * 1/ {00} * P : remove P from Q', but use it as a source for P'
  854. * 2/ {01} * Q : use Q to continue Q' calculation
  855. * 3/ {00} * Q : subtract Q from P' to cancel (2)
  856. *
  857. * In the case where P is disabled we only need 1 extra source:
  858. * 1/ {01} * Q : use Q to continue Q' calculation
  859. */
  860. static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
  861. {
  862. if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
  863. return dma_dev_to_maxpq(dma);
  864. else if (dmaf_p_disabled_continue(flags))
  865. return dma_dev_to_maxpq(dma) - 1;
  866. else if (dmaf_continue(flags))
  867. return dma_dev_to_maxpq(dma) - 3;
  868. BUG();
  869. }
  870. static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
  871. size_t dir_icg)
  872. {
  873. if (inc) {
  874. if (dir_icg)
  875. return dir_icg;
  876. else if (sgl)
  877. return icg;
  878. }
  879. return 0;
  880. }
  881. static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
  882. struct data_chunk *chunk)
  883. {
  884. return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
  885. chunk->icg, chunk->dst_icg);
  886. }
  887. static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
  888. struct data_chunk *chunk)
  889. {
  890. return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
  891. chunk->icg, chunk->src_icg);
  892. }
  893. /* --- public DMA engine API --- */
  894. #ifdef CONFIG_DMA_ENGINE
  895. void dmaengine_get(void);
  896. void dmaengine_put(void);
  897. #else
  898. static inline void dmaengine_get(void)
  899. {
  900. }
  901. static inline void dmaengine_put(void)
  902. {
  903. }
  904. #endif
  905. #ifdef CONFIG_ASYNC_TX_DMA
  906. #define async_dmaengine_get() dmaengine_get()
  907. #define async_dmaengine_put() dmaengine_put()
  908. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  909. #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
  910. #else
  911. #define async_dma_find_channel(type) dma_find_channel(type)
  912. #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
  913. #else
  914. static inline void async_dmaengine_get(void)
  915. {
  916. }
  917. static inline void async_dmaengine_put(void)
  918. {
  919. }
  920. static inline struct dma_chan *
  921. async_dma_find_channel(enum dma_transaction_type type)
  922. {
  923. return NULL;
  924. }
  925. #endif /* CONFIG_ASYNC_TX_DMA */
  926. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  927. struct dma_chan *chan);
  928. static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
  929. {
  930. tx->flags |= DMA_CTRL_ACK;
  931. }
  932. static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
  933. {
  934. tx->flags &= ~DMA_CTRL_ACK;
  935. }
  936. static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
  937. {
  938. return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
  939. }
  940. #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
  941. static inline void
  942. __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  943. {
  944. set_bit(tx_type, dstp->bits);
  945. }
  946. #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
  947. static inline void
  948. __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  949. {
  950. clear_bit(tx_type, dstp->bits);
  951. }
  952. #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
  953. static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
  954. {
  955. bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
  956. }
  957. #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
  958. static inline int
  959. __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
  960. {
  961. return test_bit(tx_type, srcp->bits);
  962. }
  963. #define for_each_dma_cap_mask(cap, mask) \
  964. for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
  965. /**
  966. * dma_async_issue_pending - flush pending transactions to HW
  967. * @chan: target DMA channel
  968. *
  969. * This allows drivers to push copies to HW in batches,
  970. * reducing MMIO writes where possible.
  971. */
  972. static inline void dma_async_issue_pending(struct dma_chan *chan)
  973. {
  974. chan->device->device_issue_pending(chan);
  975. }
  976. /**
  977. * dma_async_is_tx_complete - poll for transaction completion
  978. * @chan: DMA channel
  979. * @cookie: transaction identifier to check status of
  980. * @last: returns last completed cookie, can be NULL
  981. * @used: returns last issued cookie, can be NULL
  982. *
  983. * If @last and @used are passed in, upon return they reflect the driver
  984. * internal state and can be used with dma_async_is_complete() to check
  985. * the status of multiple cookies without re-checking hardware state.
  986. */
  987. static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  988. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  989. {
  990. struct dma_tx_state state;
  991. enum dma_status status;
  992. status = chan->device->device_tx_status(chan, cookie, &state);
  993. if (last)
  994. *last = state.last;
  995. if (used)
  996. *used = state.used;
  997. return status;
  998. }
  999. /**
  1000. * dma_async_is_complete - test a cookie against chan state
  1001. * @cookie: transaction identifier to test status of
  1002. * @last_complete: last know completed transaction
  1003. * @last_used: last cookie value handed out
  1004. *
  1005. * dma_async_is_complete() is used in dma_async_is_tx_complete()
  1006. * the test logic is separated for lightweight testing of multiple cookies
  1007. */
  1008. static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  1009. dma_cookie_t last_complete, dma_cookie_t last_used)
  1010. {
  1011. if (last_complete <= last_used) {
  1012. if ((cookie <= last_complete) || (cookie > last_used))
  1013. return DMA_COMPLETE;
  1014. } else {
  1015. if ((cookie <= last_complete) && (cookie > last_used))
  1016. return DMA_COMPLETE;
  1017. }
  1018. return DMA_IN_PROGRESS;
  1019. }
  1020. static inline void
  1021. dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
  1022. {
  1023. if (st) {
  1024. st->last = last;
  1025. st->used = used;
  1026. st->residue = residue;
  1027. }
  1028. }
  1029. #ifdef CONFIG_DMA_ENGINE
  1030. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
  1031. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
  1032. enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
  1033. void dma_issue_pending_all(void);
  1034. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1035. dma_filter_fn fn, void *fn_param);
  1036. struct dma_chan *dma_request_slave_channel_reason(struct device *dev,
  1037. const char *name);
  1038. struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
  1039. void dma_release_channel(struct dma_chan *chan);
  1040. int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
  1041. #else
  1042. static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  1043. {
  1044. return NULL;
  1045. }
  1046. static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  1047. {
  1048. return DMA_COMPLETE;
  1049. }
  1050. static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  1051. {
  1052. return DMA_COMPLETE;
  1053. }
  1054. static inline void dma_issue_pending_all(void)
  1055. {
  1056. }
  1057. static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1058. dma_filter_fn fn, void *fn_param)
  1059. {
  1060. return NULL;
  1061. }
  1062. static inline struct dma_chan *dma_request_slave_channel_reason(
  1063. struct device *dev, const char *name)
  1064. {
  1065. return ERR_PTR(-ENODEV);
  1066. }
  1067. static inline struct dma_chan *dma_request_slave_channel(struct device *dev,
  1068. const char *name)
  1069. {
  1070. return NULL;
  1071. }
  1072. static inline void dma_release_channel(struct dma_chan *chan)
  1073. {
  1074. }
  1075. static inline int dma_get_slave_caps(struct dma_chan *chan,
  1076. struct dma_slave_caps *caps)
  1077. {
  1078. return -ENXIO;
  1079. }
  1080. #endif
  1081. static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
  1082. {
  1083. struct dma_slave_caps caps;
  1084. dma_get_slave_caps(tx->chan, &caps);
  1085. if (caps.descriptor_reuse) {
  1086. tx->flags |= DMA_CTRL_REUSE;
  1087. return 0;
  1088. } else {
  1089. return -EPERM;
  1090. }
  1091. }
  1092. static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
  1093. {
  1094. tx->flags &= ~DMA_CTRL_REUSE;
  1095. }
  1096. static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
  1097. {
  1098. return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
  1099. }
  1100. static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
  1101. {
  1102. /* this is supported for reusable desc, so check that */
  1103. if (dmaengine_desc_test_reuse(desc))
  1104. return desc->desc_free(desc);
  1105. else
  1106. return -EPERM;
  1107. }
  1108. /* --- DMA device --- */
  1109. int dma_async_device_register(struct dma_device *device);
  1110. void dma_async_device_unregister(struct dma_device *device);
  1111. void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
  1112. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
  1113. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
  1114. #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
  1115. #define dma_request_slave_channel_compat(mask, x, y, dev, name) \
  1116. __dma_request_slave_channel_compat(&(mask), x, y, dev, name)
  1117. static inline struct dma_chan
  1118. *__dma_request_slave_channel_compat(const dma_cap_mask_t *mask,
  1119. dma_filter_fn fn, void *fn_param,
  1120. struct device *dev, const char *name)
  1121. {
  1122. struct dma_chan *chan;
  1123. chan = dma_request_slave_channel(dev, name);
  1124. if (chan)
  1125. return chan;
  1126. if (!fn || !fn_param)
  1127. return NULL;
  1128. return __dma_request_channel(mask, fn, fn_param);
  1129. }
  1130. #endif /* DMAENGINE_H */