dmaengine.h 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. * @DMA_PREP_CMD: tell the driver that the data passed to DMA API is command
  180. * data and the descriptor should be in different format from normal
  181. * data descriptors.
  182. */
  183. enum dma_ctrl_flags {
  184. DMA_PREP_INTERRUPT = (1 << 0),
  185. DMA_CTRL_ACK = (1 << 1),
  186. DMA_PREP_PQ_DISABLE_P = (1 << 2),
  187. DMA_PREP_PQ_DISABLE_Q = (1 << 3),
  188. DMA_PREP_CONTINUE = (1 << 4),
  189. DMA_PREP_FENCE = (1 << 5),
  190. DMA_CTRL_REUSE = (1 << 6),
  191. DMA_PREP_CMD = (1 << 7),
  192. };
  193. /**
  194. * enum sum_check_bits - bit position of pq_check_flags
  195. */
  196. enum sum_check_bits {
  197. SUM_CHECK_P = 0,
  198. SUM_CHECK_Q = 1,
  199. };
  200. /**
  201. * enum pq_check_flags - result of async_{xor,pq}_zero_sum operations
  202. * @SUM_CHECK_P_RESULT - 1 if xor zero sum error, 0 otherwise
  203. * @SUM_CHECK_Q_RESULT - 1 if reed-solomon zero sum error, 0 otherwise
  204. */
  205. enum sum_check_flags {
  206. SUM_CHECK_P_RESULT = (1 << SUM_CHECK_P),
  207. SUM_CHECK_Q_RESULT = (1 << SUM_CHECK_Q),
  208. };
  209. /**
  210. * dma_cap_mask_t - capabilities bitmap modeled after cpumask_t.
  211. * See linux/cpumask.h
  212. */
  213. typedef struct { DECLARE_BITMAP(bits, DMA_TX_TYPE_END); } dma_cap_mask_t;
  214. /**
  215. * struct dma_chan_percpu - the per-CPU part of struct dma_chan
  216. * @memcpy_count: transaction counter
  217. * @bytes_transferred: byte counter
  218. */
  219. struct dma_chan_percpu {
  220. /* stats */
  221. unsigned long memcpy_count;
  222. unsigned long bytes_transferred;
  223. };
  224. /**
  225. * struct dma_router - DMA router structure
  226. * @dev: pointer to the DMA router device
  227. * @route_free: function to be called when the route can be disconnected
  228. */
  229. struct dma_router {
  230. struct device *dev;
  231. void (*route_free)(struct device *dev, void *route_data);
  232. };
  233. /**
  234. * struct dma_chan - devices supply DMA channels, clients use them
  235. * @device: ptr to the dma device who supplies this channel, always !%NULL
  236. * @cookie: last cookie value returned to client
  237. * @completed_cookie: last completed cookie for this channel
  238. * @chan_id: channel ID for sysfs
  239. * @dev: class device for sysfs
  240. * @device_node: used to add this to the device chan list
  241. * @local: per-cpu pointer to a struct dma_chan_percpu
  242. * @client_count: how many clients are using this channel
  243. * @table_count: number of appearances in the mem-to-mem allocation table
  244. * @router: pointer to the DMA router structure
  245. * @route_data: channel specific data for the router
  246. * @private: private data for certain client-channel associations
  247. */
  248. struct dma_chan {
  249. struct dma_device *device;
  250. dma_cookie_t cookie;
  251. dma_cookie_t completed_cookie;
  252. /* sysfs */
  253. int chan_id;
  254. struct dma_chan_dev *dev;
  255. struct list_head device_node;
  256. struct dma_chan_percpu __percpu *local;
  257. int client_count;
  258. int table_count;
  259. /* DMA router */
  260. struct dma_router *router;
  261. void *route_data;
  262. void *private;
  263. };
  264. /**
  265. * struct dma_chan_dev - relate sysfs device node to backing channel device
  266. * @chan: driver channel device
  267. * @device: sysfs device
  268. * @dev_id: parent dma_device dev_id
  269. * @idr_ref: reference count to gate release of dma_device dev_id
  270. */
  271. struct dma_chan_dev {
  272. struct dma_chan *chan;
  273. struct device device;
  274. int dev_id;
  275. atomic_t *idr_ref;
  276. };
  277. /**
  278. * enum dma_slave_buswidth - defines bus width of the DMA slave
  279. * device, source or target buses
  280. */
  281. enum dma_slave_buswidth {
  282. DMA_SLAVE_BUSWIDTH_UNDEFINED = 0,
  283. DMA_SLAVE_BUSWIDTH_1_BYTE = 1,
  284. DMA_SLAVE_BUSWIDTH_2_BYTES = 2,
  285. DMA_SLAVE_BUSWIDTH_3_BYTES = 3,
  286. DMA_SLAVE_BUSWIDTH_4_BYTES = 4,
  287. DMA_SLAVE_BUSWIDTH_8_BYTES = 8,
  288. DMA_SLAVE_BUSWIDTH_16_BYTES = 16,
  289. DMA_SLAVE_BUSWIDTH_32_BYTES = 32,
  290. DMA_SLAVE_BUSWIDTH_64_BYTES = 64,
  291. };
  292. /**
  293. * struct dma_slave_config - dma slave channel runtime config
  294. * @direction: whether the data shall go in or out on this slave
  295. * channel, right now. DMA_MEM_TO_DEV and DMA_DEV_TO_MEM are
  296. * legal values. DEPRECATED, drivers should use the direction argument
  297. * to the device_prep_slave_sg and device_prep_dma_cyclic functions or
  298. * the dir field in the dma_interleaved_template structure.
  299. * @src_addr: this is the physical address where DMA slave data
  300. * should be read (RX), if the source is memory this argument is
  301. * ignored.
  302. * @dst_addr: this is the physical address where DMA slave data
  303. * should be written (TX), if the source is memory this argument
  304. * is ignored.
  305. * @src_addr_width: this is the width in bytes of the source (RX)
  306. * register where DMA data shall be read. If the source
  307. * is memory this may be ignored depending on architecture.
  308. * Legal values: 1, 2, 4, 8.
  309. * @dst_addr_width: same as src_addr_width but for destination
  310. * target (TX) mutatis mutandis.
  311. * @src_maxburst: the maximum number of words (note: words, as in
  312. * units of the src_addr_width member, not bytes) that can be sent
  313. * in one burst to the device. Typically something like half the
  314. * FIFO depth on I/O peripherals so you don't overflow it. This
  315. * may or may not be applicable on memory sources.
  316. * @dst_maxburst: same as src_maxburst but for destination target
  317. * mutatis mutandis.
  318. * @src_port_window_size: The length of the register area in words the data need
  319. * to be accessed on the device side. It is only used for devices which is using
  320. * an area instead of a single register to receive the data. Typically the DMA
  321. * loops in this area in order to transfer the data.
  322. * @dst_port_window_size: same as src_port_window_size but for the destination
  323. * port.
  324. * @device_fc: Flow Controller Settings. Only valid for slave channels. Fill
  325. * with 'true' if peripheral should be flow controller. Direction will be
  326. * selected at Runtime.
  327. * @slave_id: Slave requester id. Only valid for slave channels. The dma
  328. * slave peripheral will have unique id as dma requester which need to be
  329. * pass as slave config.
  330. *
  331. * This struct is passed in as configuration data to a DMA engine
  332. * in order to set up a certain channel for DMA transport at runtime.
  333. * The DMA device/engine has to provide support for an additional
  334. * callback in the dma_device structure, device_config and this struct
  335. * will then be passed in as an argument to the function.
  336. *
  337. * The rationale for adding configuration information to this struct is as
  338. * follows: if it is likely that more than one DMA slave controllers in
  339. * the world will support the configuration option, then make it generic.
  340. * If not: if it is fixed so that it be sent in static from the platform
  341. * data, then prefer to do that.
  342. */
  343. struct dma_slave_config {
  344. enum dma_transfer_direction direction;
  345. phys_addr_t src_addr;
  346. phys_addr_t dst_addr;
  347. enum dma_slave_buswidth src_addr_width;
  348. enum dma_slave_buswidth dst_addr_width;
  349. u32 src_maxburst;
  350. u32 dst_maxburst;
  351. u32 src_port_window_size;
  352. u32 dst_port_window_size;
  353. bool device_fc;
  354. unsigned int slave_id;
  355. };
  356. /**
  357. * enum dma_residue_granularity - Granularity of the reported transfer residue
  358. * @DMA_RESIDUE_GRANULARITY_DESCRIPTOR: Residue reporting is not support. The
  359. * DMA channel is only able to tell whether a descriptor has been completed or
  360. * not, which means residue reporting is not supported by this channel. The
  361. * residue field of the dma_tx_state field will always be 0.
  362. * @DMA_RESIDUE_GRANULARITY_SEGMENT: Residue is updated after each successfully
  363. * completed segment of the transfer (For cyclic transfers this is after each
  364. * period). This is typically implemented by having the hardware generate an
  365. * interrupt after each transferred segment and then the drivers updates the
  366. * outstanding residue by the size of the segment. Another possibility is if
  367. * the hardware supports scatter-gather and the segment descriptor has a field
  368. * which gets set after the segment has been completed. The driver then counts
  369. * the number of segments without the flag set to compute the residue.
  370. * @DMA_RESIDUE_GRANULARITY_BURST: Residue is updated after each transferred
  371. * burst. This is typically only supported if the hardware has a progress
  372. * register of some sort (E.g. a register with the current read/write address
  373. * or a register with the amount of bursts/beats/bytes that have been
  374. * transferred or still need to be transferred).
  375. */
  376. enum dma_residue_granularity {
  377. DMA_RESIDUE_GRANULARITY_DESCRIPTOR = 0,
  378. DMA_RESIDUE_GRANULARITY_SEGMENT = 1,
  379. DMA_RESIDUE_GRANULARITY_BURST = 2,
  380. };
  381. /* struct dma_slave_caps - expose capabilities of a slave channel only
  382. *
  383. * @src_addr_widths: bit mask of src addr widths the channel supports
  384. * @dst_addr_widths: bit mask of dstn addr widths the channel supports
  385. * @directions: bit mask of slave direction the channel supported
  386. * since the enum dma_transfer_direction is not defined as bits for each
  387. * type of direction, the dma controller should fill (1 << <TYPE>) and same
  388. * should be checked by controller as well
  389. * @max_burst: max burst capability per-transfer
  390. * @cmd_pause: true, if pause and thereby resume is supported
  391. * @cmd_terminate: true, if terminate cmd is supported
  392. * @residue_granularity: granularity of the reported transfer residue
  393. * @descriptor_reuse: if a descriptor can be reused by client and
  394. * resubmitted multiple times
  395. */
  396. struct dma_slave_caps {
  397. u32 src_addr_widths;
  398. u32 dst_addr_widths;
  399. u32 directions;
  400. u32 max_burst;
  401. bool cmd_pause;
  402. bool cmd_terminate;
  403. enum dma_residue_granularity residue_granularity;
  404. bool descriptor_reuse;
  405. };
  406. static inline const char *dma_chan_name(struct dma_chan *chan)
  407. {
  408. return dev_name(&chan->dev->device);
  409. }
  410. void dma_chan_cleanup(struct kref *kref);
  411. /**
  412. * typedef dma_filter_fn - callback filter for dma_request_channel
  413. * @chan: channel to be reviewed
  414. * @filter_param: opaque parameter passed through dma_request_channel
  415. *
  416. * When this optional parameter is specified in a call to dma_request_channel a
  417. * suitable channel is passed to this routine for further dispositioning before
  418. * being returned. Where 'suitable' indicates a non-busy channel that
  419. * satisfies the given capability mask. It returns 'true' to indicate that the
  420. * channel is suitable.
  421. */
  422. typedef bool (*dma_filter_fn)(struct dma_chan *chan, void *filter_param);
  423. typedef void (*dma_async_tx_callback)(void *dma_async_param);
  424. enum dmaengine_tx_result {
  425. DMA_TRANS_NOERROR = 0, /* SUCCESS */
  426. DMA_TRANS_READ_FAILED, /* Source DMA read failed */
  427. DMA_TRANS_WRITE_FAILED, /* Destination DMA write failed */
  428. DMA_TRANS_ABORTED, /* Op never submitted / aborted */
  429. };
  430. struct dmaengine_result {
  431. enum dmaengine_tx_result result;
  432. u32 residue;
  433. };
  434. typedef void (*dma_async_tx_callback_result)(void *dma_async_param,
  435. const struct dmaengine_result *result);
  436. struct dmaengine_unmap_data {
  437. u8 map_cnt;
  438. u8 to_cnt;
  439. u8 from_cnt;
  440. u8 bidi_cnt;
  441. struct device *dev;
  442. struct kref kref;
  443. size_t len;
  444. dma_addr_t addr[0];
  445. };
  446. /**
  447. * struct dma_async_tx_descriptor - async transaction descriptor
  448. * ---dma generic offload fields---
  449. * @cookie: tracking cookie for this transaction, set to -EBUSY if
  450. * this tx is sitting on a dependency list
  451. * @flags: flags to augment operation preparation, control completion, and
  452. * communicate status
  453. * @phys: physical address of the descriptor
  454. * @chan: target channel for this operation
  455. * @tx_submit: accept the descriptor, assign ordered cookie and mark the
  456. * descriptor pending. To be pushed on .issue_pending() call
  457. * @callback: routine to call after this operation is complete
  458. * @callback_param: general parameter to pass to the callback routine
  459. * ---async_tx api specific fields---
  460. * @next: at completion submit this descriptor
  461. * @parent: pointer to the next level up in the dependency chain
  462. * @lock: protect the parent and next pointers
  463. */
  464. struct dma_async_tx_descriptor {
  465. dma_cookie_t cookie;
  466. enum dma_ctrl_flags flags; /* not a 'long' to pack with cookie */
  467. dma_addr_t phys;
  468. struct dma_chan *chan;
  469. dma_cookie_t (*tx_submit)(struct dma_async_tx_descriptor *tx);
  470. int (*desc_free)(struct dma_async_tx_descriptor *tx);
  471. dma_async_tx_callback callback;
  472. dma_async_tx_callback_result callback_result;
  473. void *callback_param;
  474. struct dmaengine_unmap_data *unmap;
  475. #ifdef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  476. struct dma_async_tx_descriptor *next;
  477. struct dma_async_tx_descriptor *parent;
  478. spinlock_t lock;
  479. #endif
  480. };
  481. #ifdef CONFIG_DMA_ENGINE
  482. static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  483. struct dmaengine_unmap_data *unmap)
  484. {
  485. kref_get(&unmap->kref);
  486. tx->unmap = unmap;
  487. }
  488. struct dmaengine_unmap_data *
  489. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags);
  490. void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap);
  491. #else
  492. static inline void dma_set_unmap(struct dma_async_tx_descriptor *tx,
  493. struct dmaengine_unmap_data *unmap)
  494. {
  495. }
  496. static inline struct dmaengine_unmap_data *
  497. dmaengine_get_unmap_data(struct device *dev, int nr, gfp_t flags)
  498. {
  499. return NULL;
  500. }
  501. static inline void dmaengine_unmap_put(struct dmaengine_unmap_data *unmap)
  502. {
  503. }
  504. #endif
  505. static inline void dma_descriptor_unmap(struct dma_async_tx_descriptor *tx)
  506. {
  507. if (tx->unmap) {
  508. dmaengine_unmap_put(tx->unmap);
  509. tx->unmap = NULL;
  510. }
  511. }
  512. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  513. static inline void txd_lock(struct dma_async_tx_descriptor *txd)
  514. {
  515. }
  516. static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
  517. {
  518. }
  519. static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  520. {
  521. BUG();
  522. }
  523. static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  524. {
  525. }
  526. static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
  527. {
  528. }
  529. static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  530. {
  531. return NULL;
  532. }
  533. static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  534. {
  535. return NULL;
  536. }
  537. #else
  538. static inline void txd_lock(struct dma_async_tx_descriptor *txd)
  539. {
  540. spin_lock_bh(&txd->lock);
  541. }
  542. static inline void txd_unlock(struct dma_async_tx_descriptor *txd)
  543. {
  544. spin_unlock_bh(&txd->lock);
  545. }
  546. static inline void txd_chain(struct dma_async_tx_descriptor *txd, struct dma_async_tx_descriptor *next)
  547. {
  548. txd->next = next;
  549. next->parent = txd;
  550. }
  551. static inline void txd_clear_parent(struct dma_async_tx_descriptor *txd)
  552. {
  553. txd->parent = NULL;
  554. }
  555. static inline void txd_clear_next(struct dma_async_tx_descriptor *txd)
  556. {
  557. txd->next = NULL;
  558. }
  559. static inline struct dma_async_tx_descriptor *txd_parent(struct dma_async_tx_descriptor *txd)
  560. {
  561. return txd->parent;
  562. }
  563. static inline struct dma_async_tx_descriptor *txd_next(struct dma_async_tx_descriptor *txd)
  564. {
  565. return txd->next;
  566. }
  567. #endif
  568. /**
  569. * struct dma_tx_state - filled in to report the status of
  570. * a transfer.
  571. * @last: last completed DMA cookie
  572. * @used: last issued DMA cookie (i.e. the one in progress)
  573. * @residue: the remaining number of bytes left to transmit
  574. * on the selected transfer for states DMA_IN_PROGRESS and
  575. * DMA_PAUSED if this is implemented in the driver, else 0
  576. */
  577. struct dma_tx_state {
  578. dma_cookie_t last;
  579. dma_cookie_t used;
  580. u32 residue;
  581. };
  582. /**
  583. * enum dmaengine_alignment - defines alignment of the DMA async tx
  584. * buffers
  585. */
  586. enum dmaengine_alignment {
  587. DMAENGINE_ALIGN_1_BYTE = 0,
  588. DMAENGINE_ALIGN_2_BYTES = 1,
  589. DMAENGINE_ALIGN_4_BYTES = 2,
  590. DMAENGINE_ALIGN_8_BYTES = 3,
  591. DMAENGINE_ALIGN_16_BYTES = 4,
  592. DMAENGINE_ALIGN_32_BYTES = 5,
  593. DMAENGINE_ALIGN_64_BYTES = 6,
  594. };
  595. /**
  596. * struct dma_slave_map - associates slave device and it's slave channel with
  597. * parameter to be used by a filter function
  598. * @devname: name of the device
  599. * @slave: slave channel name
  600. * @param: opaque parameter to pass to struct dma_filter.fn
  601. */
  602. struct dma_slave_map {
  603. const char *devname;
  604. const char *slave;
  605. void *param;
  606. };
  607. /**
  608. * struct dma_filter - information for slave device/channel to filter_fn/param
  609. * mapping
  610. * @fn: filter function callback
  611. * @mapcnt: number of slave device/channel in the map
  612. * @map: array of channel to filter mapping data
  613. */
  614. struct dma_filter {
  615. dma_filter_fn fn;
  616. int mapcnt;
  617. const struct dma_slave_map *map;
  618. };
  619. /**
  620. * struct dma_device - info on the entity supplying DMA services
  621. * @chancnt: how many DMA channels are supported
  622. * @privatecnt: how many DMA channels are requested by dma_request_channel
  623. * @channels: the list of struct dma_chan
  624. * @global_node: list_head for global dma_device_list
  625. * @filter: information for device/slave to filter function/param mapping
  626. * @cap_mask: one or more dma_capability flags
  627. * @max_xor: maximum number of xor sources, 0 if no capability
  628. * @max_pq: maximum number of PQ sources and PQ-continue capability
  629. * @copy_align: alignment shift for memcpy operations
  630. * @xor_align: alignment shift for xor operations
  631. * @pq_align: alignment shift for pq operations
  632. * @fill_align: alignment shift for memset operations
  633. * @dev_id: unique device ID
  634. * @dev: struct device reference for dma mapping api
  635. * @src_addr_widths: bit mask of src addr widths the device supports
  636. * @dst_addr_widths: bit mask of dst addr widths the device supports
  637. * @directions: bit mask of slave direction the device supports since
  638. * the enum dma_transfer_direction is not defined as bits for
  639. * each type of direction, the dma controller should fill (1 <<
  640. * <TYPE>) and same should be checked by controller as well
  641. * @max_burst: max burst capability per-transfer
  642. * @residue_granularity: granularity of the transfer residue reported
  643. * by tx_status
  644. * @device_alloc_chan_resources: allocate resources and return the
  645. * number of allocated descriptors
  646. * @device_free_chan_resources: release DMA channel's resources
  647. * @device_prep_dma_memcpy: prepares a memcpy operation
  648. * @device_prep_dma_xor: prepares a xor operation
  649. * @device_prep_dma_xor_val: prepares a xor validation operation
  650. * @device_prep_dma_pq: prepares a pq operation
  651. * @device_prep_dma_pq_val: prepares a pqzero_sum operation
  652. * @device_prep_dma_memset: prepares a memset operation
  653. * @device_prep_dma_memset_sg: prepares a memset operation over a scatter list
  654. * @device_prep_dma_interrupt: prepares an end of chain interrupt operation
  655. * @device_prep_slave_sg: prepares a slave dma operation
  656. * @device_prep_dma_cyclic: prepare a cyclic dma operation suitable for audio.
  657. * The function takes a buffer of size buf_len. The callback function will
  658. * be called after period_len bytes have been transferred.
  659. * @device_prep_interleaved_dma: Transfer expression in a generic way.
  660. * @device_prep_dma_imm_data: DMA's 8 byte immediate data to the dst address
  661. * @device_config: Pushes a new configuration to a channel, return 0 or an error
  662. * code
  663. * @device_pause: Pauses any transfer happening on a channel. Returns
  664. * 0 or an error code
  665. * @device_resume: Resumes any transfer on a channel previously
  666. * paused. Returns 0 or an error code
  667. * @device_terminate_all: Aborts all transfers on a channel. Returns 0
  668. * or an error code
  669. * @device_synchronize: Synchronizes the termination of a transfers to the
  670. * current context.
  671. * @device_tx_status: poll for transaction completion, the optional
  672. * txstate parameter can be supplied with a pointer to get a
  673. * struct with auxiliary transfer status information, otherwise the call
  674. * will just return a simple status code
  675. * @device_issue_pending: push pending transactions to hardware
  676. * @descriptor_reuse: a submitted transfer can be resubmitted after completion
  677. */
  678. struct dma_device {
  679. unsigned int chancnt;
  680. unsigned int privatecnt;
  681. struct list_head channels;
  682. struct list_head global_node;
  683. struct dma_filter filter;
  684. dma_cap_mask_t cap_mask;
  685. unsigned short max_xor;
  686. unsigned short max_pq;
  687. enum dmaengine_alignment copy_align;
  688. enum dmaengine_alignment xor_align;
  689. enum dmaengine_alignment pq_align;
  690. enum dmaengine_alignment fill_align;
  691. #define DMA_HAS_PQ_CONTINUE (1 << 15)
  692. int dev_id;
  693. struct device *dev;
  694. u32 src_addr_widths;
  695. u32 dst_addr_widths;
  696. u32 directions;
  697. u32 max_burst;
  698. bool descriptor_reuse;
  699. enum dma_residue_granularity residue_granularity;
  700. int (*device_alloc_chan_resources)(struct dma_chan *chan);
  701. void (*device_free_chan_resources)(struct dma_chan *chan);
  702. struct dma_async_tx_descriptor *(*device_prep_dma_memcpy)(
  703. struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
  704. size_t len, unsigned long flags);
  705. struct dma_async_tx_descriptor *(*device_prep_dma_xor)(
  706. struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  707. unsigned int src_cnt, size_t len, unsigned long flags);
  708. struct dma_async_tx_descriptor *(*device_prep_dma_xor_val)(
  709. struct dma_chan *chan, dma_addr_t *src, unsigned int src_cnt,
  710. size_t len, enum sum_check_flags *result, unsigned long flags);
  711. struct dma_async_tx_descriptor *(*device_prep_dma_pq)(
  712. struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  713. unsigned int src_cnt, const unsigned char *scf,
  714. size_t len, unsigned long flags);
  715. struct dma_async_tx_descriptor *(*device_prep_dma_pq_val)(
  716. struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  717. unsigned int src_cnt, const unsigned char *scf, size_t len,
  718. enum sum_check_flags *pqres, unsigned long flags);
  719. struct dma_async_tx_descriptor *(*device_prep_dma_memset)(
  720. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  721. unsigned long flags);
  722. struct dma_async_tx_descriptor *(*device_prep_dma_memset_sg)(
  723. struct dma_chan *chan, struct scatterlist *sg,
  724. unsigned int nents, int value, unsigned long flags);
  725. struct dma_async_tx_descriptor *(*device_prep_dma_interrupt)(
  726. struct dma_chan *chan, unsigned long flags);
  727. struct dma_async_tx_descriptor *(*device_prep_dma_sg)(
  728. struct dma_chan *chan,
  729. struct scatterlist *dst_sg, unsigned int dst_nents,
  730. struct scatterlist *src_sg, unsigned int src_nents,
  731. unsigned long flags);
  732. struct dma_async_tx_descriptor *(*device_prep_slave_sg)(
  733. struct dma_chan *chan, struct scatterlist *sgl,
  734. unsigned int sg_len, enum dma_transfer_direction direction,
  735. unsigned long flags, void *context);
  736. struct dma_async_tx_descriptor *(*device_prep_dma_cyclic)(
  737. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  738. size_t period_len, enum dma_transfer_direction direction,
  739. unsigned long flags);
  740. struct dma_async_tx_descriptor *(*device_prep_interleaved_dma)(
  741. struct dma_chan *chan, struct dma_interleaved_template *xt,
  742. unsigned long flags);
  743. struct dma_async_tx_descriptor *(*device_prep_dma_imm_data)(
  744. struct dma_chan *chan, dma_addr_t dst, u64 data,
  745. unsigned long flags);
  746. int (*device_config)(struct dma_chan *chan,
  747. struct dma_slave_config *config);
  748. int (*device_pause)(struct dma_chan *chan);
  749. int (*device_resume)(struct dma_chan *chan);
  750. int (*device_terminate_all)(struct dma_chan *chan);
  751. void (*device_synchronize)(struct dma_chan *chan);
  752. enum dma_status (*device_tx_status)(struct dma_chan *chan,
  753. dma_cookie_t cookie,
  754. struct dma_tx_state *txstate);
  755. void (*device_issue_pending)(struct dma_chan *chan);
  756. };
  757. static inline int dmaengine_slave_config(struct dma_chan *chan,
  758. struct dma_slave_config *config)
  759. {
  760. if (chan->device->device_config)
  761. return chan->device->device_config(chan, config);
  762. return -ENOSYS;
  763. }
  764. static inline bool is_slave_direction(enum dma_transfer_direction direction)
  765. {
  766. return (direction == DMA_MEM_TO_DEV) || (direction == DMA_DEV_TO_MEM);
  767. }
  768. static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_single(
  769. struct dma_chan *chan, dma_addr_t buf, size_t len,
  770. enum dma_transfer_direction dir, unsigned long flags)
  771. {
  772. struct scatterlist sg;
  773. sg_init_table(&sg, 1);
  774. sg_dma_address(&sg) = buf;
  775. sg_dma_len(&sg) = len;
  776. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  777. return NULL;
  778. return chan->device->device_prep_slave_sg(chan, &sg, 1,
  779. dir, flags, NULL);
  780. }
  781. static inline struct dma_async_tx_descriptor *dmaengine_prep_slave_sg(
  782. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  783. enum dma_transfer_direction dir, unsigned long flags)
  784. {
  785. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  786. return NULL;
  787. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  788. dir, flags, NULL);
  789. }
  790. #ifdef CONFIG_RAPIDIO_DMA_ENGINE
  791. struct rio_dma_ext;
  792. static inline struct dma_async_tx_descriptor *dmaengine_prep_rio_sg(
  793. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  794. enum dma_transfer_direction dir, unsigned long flags,
  795. struct rio_dma_ext *rio_ext)
  796. {
  797. if (!chan || !chan->device || !chan->device->device_prep_slave_sg)
  798. return NULL;
  799. return chan->device->device_prep_slave_sg(chan, sgl, sg_len,
  800. dir, flags, rio_ext);
  801. }
  802. #endif
  803. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_cyclic(
  804. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  805. size_t period_len, enum dma_transfer_direction dir,
  806. unsigned long flags)
  807. {
  808. if (!chan || !chan->device || !chan->device->device_prep_dma_cyclic)
  809. return NULL;
  810. return chan->device->device_prep_dma_cyclic(chan, buf_addr, buf_len,
  811. period_len, dir, flags);
  812. }
  813. static inline struct dma_async_tx_descriptor *dmaengine_prep_interleaved_dma(
  814. struct dma_chan *chan, struct dma_interleaved_template *xt,
  815. unsigned long flags)
  816. {
  817. if (!chan || !chan->device || !chan->device->device_prep_interleaved_dma)
  818. return NULL;
  819. return chan->device->device_prep_interleaved_dma(chan, xt, flags);
  820. }
  821. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memset(
  822. struct dma_chan *chan, dma_addr_t dest, int value, size_t len,
  823. unsigned long flags)
  824. {
  825. if (!chan || !chan->device || !chan->device->device_prep_dma_memset)
  826. return NULL;
  827. return chan->device->device_prep_dma_memset(chan, dest, value,
  828. len, flags);
  829. }
  830. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_memcpy(
  831. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  832. size_t len, unsigned long flags)
  833. {
  834. if (!chan || !chan->device || !chan->device->device_prep_dma_memcpy)
  835. return NULL;
  836. return chan->device->device_prep_dma_memcpy(chan, dest, src,
  837. len, flags);
  838. }
  839. static inline struct dma_async_tx_descriptor *dmaengine_prep_dma_sg(
  840. struct dma_chan *chan,
  841. struct scatterlist *dst_sg, unsigned int dst_nents,
  842. struct scatterlist *src_sg, unsigned int src_nents,
  843. unsigned long flags)
  844. {
  845. if (!chan || !chan->device || !chan->device->device_prep_dma_sg)
  846. return NULL;
  847. return chan->device->device_prep_dma_sg(chan, dst_sg, dst_nents,
  848. src_sg, src_nents, flags);
  849. }
  850. /**
  851. * dmaengine_terminate_all() - Terminate all active DMA transfers
  852. * @chan: The channel for which to terminate the transfers
  853. *
  854. * This function is DEPRECATED use either dmaengine_terminate_sync() or
  855. * dmaengine_terminate_async() instead.
  856. */
  857. static inline int dmaengine_terminate_all(struct dma_chan *chan)
  858. {
  859. if (chan->device->device_terminate_all)
  860. return chan->device->device_terminate_all(chan);
  861. return -ENOSYS;
  862. }
  863. /**
  864. * dmaengine_terminate_async() - Terminate all active DMA transfers
  865. * @chan: The channel for which to terminate the transfers
  866. *
  867. * Calling this function will terminate all active and pending descriptors
  868. * that have previously been submitted to the channel. It is not guaranteed
  869. * though that the transfer for the active descriptor has stopped when the
  870. * function returns. Furthermore it is possible the complete callback of a
  871. * submitted transfer is still running when this function returns.
  872. *
  873. * dmaengine_synchronize() needs to be called before it is safe to free
  874. * any memory that is accessed by previously submitted descriptors or before
  875. * freeing any resources accessed from within the completion callback of any
  876. * perviously submitted descriptors.
  877. *
  878. * This function can be called from atomic context as well as from within a
  879. * complete callback of a descriptor submitted on the same channel.
  880. *
  881. * If none of the two conditions above apply consider using
  882. * dmaengine_terminate_sync() instead.
  883. */
  884. static inline int dmaengine_terminate_async(struct dma_chan *chan)
  885. {
  886. if (chan->device->device_terminate_all)
  887. return chan->device->device_terminate_all(chan);
  888. return -EINVAL;
  889. }
  890. /**
  891. * dmaengine_synchronize() - Synchronize DMA channel termination
  892. * @chan: The channel to synchronize
  893. *
  894. * Synchronizes to the DMA channel termination to the current context. When this
  895. * function returns it is guaranteed that all transfers for previously issued
  896. * descriptors have stopped and and it is safe to free the memory assoicated
  897. * with them. Furthermore it is guaranteed that all complete callback functions
  898. * for a previously submitted descriptor have finished running and it is safe to
  899. * free resources accessed from within the complete callbacks.
  900. *
  901. * The behavior of this function is undefined if dma_async_issue_pending() has
  902. * been called between dmaengine_terminate_async() and this function.
  903. *
  904. * This function must only be called from non-atomic context and must not be
  905. * called from within a complete callback of a descriptor submitted on the same
  906. * channel.
  907. */
  908. static inline void dmaengine_synchronize(struct dma_chan *chan)
  909. {
  910. might_sleep();
  911. if (chan->device->device_synchronize)
  912. chan->device->device_synchronize(chan);
  913. }
  914. /**
  915. * dmaengine_terminate_sync() - Terminate all active DMA transfers
  916. * @chan: The channel for which to terminate the transfers
  917. *
  918. * Calling this function will terminate all active and pending transfers
  919. * that have previously been submitted to the channel. It is similar to
  920. * dmaengine_terminate_async() but guarantees that the DMA transfer has actually
  921. * stopped and that all complete callbacks have finished running when the
  922. * function returns.
  923. *
  924. * This function must only be called from non-atomic context and must not be
  925. * called from within a complete callback of a descriptor submitted on the same
  926. * channel.
  927. */
  928. static inline int dmaengine_terminate_sync(struct dma_chan *chan)
  929. {
  930. int ret;
  931. ret = dmaengine_terminate_async(chan);
  932. if (ret)
  933. return ret;
  934. dmaengine_synchronize(chan);
  935. return 0;
  936. }
  937. static inline int dmaengine_pause(struct dma_chan *chan)
  938. {
  939. if (chan->device->device_pause)
  940. return chan->device->device_pause(chan);
  941. return -ENOSYS;
  942. }
  943. static inline int dmaengine_resume(struct dma_chan *chan)
  944. {
  945. if (chan->device->device_resume)
  946. return chan->device->device_resume(chan);
  947. return -ENOSYS;
  948. }
  949. static inline enum dma_status dmaengine_tx_status(struct dma_chan *chan,
  950. dma_cookie_t cookie, struct dma_tx_state *state)
  951. {
  952. return chan->device->device_tx_status(chan, cookie, state);
  953. }
  954. static inline dma_cookie_t dmaengine_submit(struct dma_async_tx_descriptor *desc)
  955. {
  956. return desc->tx_submit(desc);
  957. }
  958. static inline bool dmaengine_check_align(enum dmaengine_alignment align,
  959. size_t off1, size_t off2, size_t len)
  960. {
  961. size_t mask;
  962. if (!align)
  963. return true;
  964. mask = (1 << align) - 1;
  965. if (mask & (off1 | off2 | len))
  966. return false;
  967. return true;
  968. }
  969. static inline bool is_dma_copy_aligned(struct dma_device *dev, size_t off1,
  970. size_t off2, size_t len)
  971. {
  972. return dmaengine_check_align(dev->copy_align, off1, off2, len);
  973. }
  974. static inline bool is_dma_xor_aligned(struct dma_device *dev, size_t off1,
  975. size_t off2, size_t len)
  976. {
  977. return dmaengine_check_align(dev->xor_align, off1, off2, len);
  978. }
  979. static inline bool is_dma_pq_aligned(struct dma_device *dev, size_t off1,
  980. size_t off2, size_t len)
  981. {
  982. return dmaengine_check_align(dev->pq_align, off1, off2, len);
  983. }
  984. static inline bool is_dma_fill_aligned(struct dma_device *dev, size_t off1,
  985. size_t off2, size_t len)
  986. {
  987. return dmaengine_check_align(dev->fill_align, off1, off2, len);
  988. }
  989. static inline void
  990. dma_set_maxpq(struct dma_device *dma, int maxpq, int has_pq_continue)
  991. {
  992. dma->max_pq = maxpq;
  993. if (has_pq_continue)
  994. dma->max_pq |= DMA_HAS_PQ_CONTINUE;
  995. }
  996. static inline bool dmaf_continue(enum dma_ctrl_flags flags)
  997. {
  998. return (flags & DMA_PREP_CONTINUE) == DMA_PREP_CONTINUE;
  999. }
  1000. static inline bool dmaf_p_disabled_continue(enum dma_ctrl_flags flags)
  1001. {
  1002. enum dma_ctrl_flags mask = DMA_PREP_CONTINUE | DMA_PREP_PQ_DISABLE_P;
  1003. return (flags & mask) == mask;
  1004. }
  1005. static inline bool dma_dev_has_pq_continue(struct dma_device *dma)
  1006. {
  1007. return (dma->max_pq & DMA_HAS_PQ_CONTINUE) == DMA_HAS_PQ_CONTINUE;
  1008. }
  1009. static inline unsigned short dma_dev_to_maxpq(struct dma_device *dma)
  1010. {
  1011. return dma->max_pq & ~DMA_HAS_PQ_CONTINUE;
  1012. }
  1013. /* dma_maxpq - reduce maxpq in the face of continued operations
  1014. * @dma - dma device with PQ capability
  1015. * @flags - to check if DMA_PREP_CONTINUE and DMA_PREP_PQ_DISABLE_P are set
  1016. *
  1017. * When an engine does not support native continuation we need 3 extra
  1018. * source slots to reuse P and Q with the following coefficients:
  1019. * 1/ {00} * P : remove P from Q', but use it as a source for P'
  1020. * 2/ {01} * Q : use Q to continue Q' calculation
  1021. * 3/ {00} * Q : subtract Q from P' to cancel (2)
  1022. *
  1023. * In the case where P is disabled we only need 1 extra source:
  1024. * 1/ {01} * Q : use Q to continue Q' calculation
  1025. */
  1026. static inline int dma_maxpq(struct dma_device *dma, enum dma_ctrl_flags flags)
  1027. {
  1028. if (dma_dev_has_pq_continue(dma) || !dmaf_continue(flags))
  1029. return dma_dev_to_maxpq(dma);
  1030. else if (dmaf_p_disabled_continue(flags))
  1031. return dma_dev_to_maxpq(dma) - 1;
  1032. else if (dmaf_continue(flags))
  1033. return dma_dev_to_maxpq(dma) - 3;
  1034. BUG();
  1035. }
  1036. static inline size_t dmaengine_get_icg(bool inc, bool sgl, size_t icg,
  1037. size_t dir_icg)
  1038. {
  1039. if (inc) {
  1040. if (dir_icg)
  1041. return dir_icg;
  1042. else if (sgl)
  1043. return icg;
  1044. }
  1045. return 0;
  1046. }
  1047. static inline size_t dmaengine_get_dst_icg(struct dma_interleaved_template *xt,
  1048. struct data_chunk *chunk)
  1049. {
  1050. return dmaengine_get_icg(xt->dst_inc, xt->dst_sgl,
  1051. chunk->icg, chunk->dst_icg);
  1052. }
  1053. static inline size_t dmaengine_get_src_icg(struct dma_interleaved_template *xt,
  1054. struct data_chunk *chunk)
  1055. {
  1056. return dmaengine_get_icg(xt->src_inc, xt->src_sgl,
  1057. chunk->icg, chunk->src_icg);
  1058. }
  1059. /* --- public DMA engine API --- */
  1060. #ifdef CONFIG_DMA_ENGINE
  1061. void dmaengine_get(void);
  1062. void dmaengine_put(void);
  1063. #else
  1064. static inline void dmaengine_get(void)
  1065. {
  1066. }
  1067. static inline void dmaengine_put(void)
  1068. {
  1069. }
  1070. #endif
  1071. #ifdef CONFIG_ASYNC_TX_DMA
  1072. #define async_dmaengine_get() dmaengine_get()
  1073. #define async_dmaengine_put() dmaengine_put()
  1074. #ifndef CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH
  1075. #define async_dma_find_channel(type) dma_find_channel(DMA_ASYNC_TX)
  1076. #else
  1077. #define async_dma_find_channel(type) dma_find_channel(type)
  1078. #endif /* CONFIG_ASYNC_TX_ENABLE_CHANNEL_SWITCH */
  1079. #else
  1080. static inline void async_dmaengine_get(void)
  1081. {
  1082. }
  1083. static inline void async_dmaengine_put(void)
  1084. {
  1085. }
  1086. static inline struct dma_chan *
  1087. async_dma_find_channel(enum dma_transaction_type type)
  1088. {
  1089. return NULL;
  1090. }
  1091. #endif /* CONFIG_ASYNC_TX_DMA */
  1092. void dma_async_tx_descriptor_init(struct dma_async_tx_descriptor *tx,
  1093. struct dma_chan *chan);
  1094. static inline void async_tx_ack(struct dma_async_tx_descriptor *tx)
  1095. {
  1096. tx->flags |= DMA_CTRL_ACK;
  1097. }
  1098. static inline void async_tx_clear_ack(struct dma_async_tx_descriptor *tx)
  1099. {
  1100. tx->flags &= ~DMA_CTRL_ACK;
  1101. }
  1102. static inline bool async_tx_test_ack(struct dma_async_tx_descriptor *tx)
  1103. {
  1104. return (tx->flags & DMA_CTRL_ACK) == DMA_CTRL_ACK;
  1105. }
  1106. #define dma_cap_set(tx, mask) __dma_cap_set((tx), &(mask))
  1107. static inline void
  1108. __dma_cap_set(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  1109. {
  1110. set_bit(tx_type, dstp->bits);
  1111. }
  1112. #define dma_cap_clear(tx, mask) __dma_cap_clear((tx), &(mask))
  1113. static inline void
  1114. __dma_cap_clear(enum dma_transaction_type tx_type, dma_cap_mask_t *dstp)
  1115. {
  1116. clear_bit(tx_type, dstp->bits);
  1117. }
  1118. #define dma_cap_zero(mask) __dma_cap_zero(&(mask))
  1119. static inline void __dma_cap_zero(dma_cap_mask_t *dstp)
  1120. {
  1121. bitmap_zero(dstp->bits, DMA_TX_TYPE_END);
  1122. }
  1123. #define dma_has_cap(tx, mask) __dma_has_cap((tx), &(mask))
  1124. static inline int
  1125. __dma_has_cap(enum dma_transaction_type tx_type, dma_cap_mask_t *srcp)
  1126. {
  1127. return test_bit(tx_type, srcp->bits);
  1128. }
  1129. #define for_each_dma_cap_mask(cap, mask) \
  1130. for_each_set_bit(cap, mask.bits, DMA_TX_TYPE_END)
  1131. /**
  1132. * dma_async_issue_pending - flush pending transactions to HW
  1133. * @chan: target DMA channel
  1134. *
  1135. * This allows drivers to push copies to HW in batches,
  1136. * reducing MMIO writes where possible.
  1137. */
  1138. static inline void dma_async_issue_pending(struct dma_chan *chan)
  1139. {
  1140. chan->device->device_issue_pending(chan);
  1141. }
  1142. /**
  1143. * dma_async_is_tx_complete - poll for transaction completion
  1144. * @chan: DMA channel
  1145. * @cookie: transaction identifier to check status of
  1146. * @last: returns last completed cookie, can be NULL
  1147. * @used: returns last issued cookie, can be NULL
  1148. *
  1149. * If @last and @used are passed in, upon return they reflect the driver
  1150. * internal state and can be used with dma_async_is_complete() to check
  1151. * the status of multiple cookies without re-checking hardware state.
  1152. */
  1153. static inline enum dma_status dma_async_is_tx_complete(struct dma_chan *chan,
  1154. dma_cookie_t cookie, dma_cookie_t *last, dma_cookie_t *used)
  1155. {
  1156. struct dma_tx_state state;
  1157. enum dma_status status;
  1158. status = chan->device->device_tx_status(chan, cookie, &state);
  1159. if (last)
  1160. *last = state.last;
  1161. if (used)
  1162. *used = state.used;
  1163. return status;
  1164. }
  1165. /**
  1166. * dma_async_is_complete - test a cookie against chan state
  1167. * @cookie: transaction identifier to test status of
  1168. * @last_complete: last know completed transaction
  1169. * @last_used: last cookie value handed out
  1170. *
  1171. * dma_async_is_complete() is used in dma_async_is_tx_complete()
  1172. * the test logic is separated for lightweight testing of multiple cookies
  1173. */
  1174. static inline enum dma_status dma_async_is_complete(dma_cookie_t cookie,
  1175. dma_cookie_t last_complete, dma_cookie_t last_used)
  1176. {
  1177. if (last_complete <= last_used) {
  1178. if ((cookie <= last_complete) || (cookie > last_used))
  1179. return DMA_COMPLETE;
  1180. } else {
  1181. if ((cookie <= last_complete) && (cookie > last_used))
  1182. return DMA_COMPLETE;
  1183. }
  1184. return DMA_IN_PROGRESS;
  1185. }
  1186. static inline void
  1187. dma_set_tx_state(struct dma_tx_state *st, dma_cookie_t last, dma_cookie_t used, u32 residue)
  1188. {
  1189. if (st) {
  1190. st->last = last;
  1191. st->used = used;
  1192. st->residue = residue;
  1193. }
  1194. }
  1195. #ifdef CONFIG_DMA_ENGINE
  1196. struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type);
  1197. enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie);
  1198. enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx);
  1199. void dma_issue_pending_all(void);
  1200. struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1201. dma_filter_fn fn, void *fn_param);
  1202. struct dma_chan *dma_request_slave_channel(struct device *dev, const char *name);
  1203. struct dma_chan *dma_request_chan(struct device *dev, const char *name);
  1204. struct dma_chan *dma_request_chan_by_mask(const dma_cap_mask_t *mask);
  1205. void dma_release_channel(struct dma_chan *chan);
  1206. int dma_get_slave_caps(struct dma_chan *chan, struct dma_slave_caps *caps);
  1207. #else
  1208. static inline struct dma_chan *dma_find_channel(enum dma_transaction_type tx_type)
  1209. {
  1210. return NULL;
  1211. }
  1212. static inline enum dma_status dma_sync_wait(struct dma_chan *chan, dma_cookie_t cookie)
  1213. {
  1214. return DMA_COMPLETE;
  1215. }
  1216. static inline enum dma_status dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
  1217. {
  1218. return DMA_COMPLETE;
  1219. }
  1220. static inline void dma_issue_pending_all(void)
  1221. {
  1222. }
  1223. static inline struct dma_chan *__dma_request_channel(const dma_cap_mask_t *mask,
  1224. dma_filter_fn fn, void *fn_param)
  1225. {
  1226. return NULL;
  1227. }
  1228. static inline struct dma_chan *dma_request_slave_channel(struct device *dev,
  1229. const char *name)
  1230. {
  1231. return NULL;
  1232. }
  1233. static inline struct dma_chan *dma_request_chan(struct device *dev,
  1234. const char *name)
  1235. {
  1236. return ERR_PTR(-ENODEV);
  1237. }
  1238. static inline struct dma_chan *dma_request_chan_by_mask(
  1239. const dma_cap_mask_t *mask)
  1240. {
  1241. return ERR_PTR(-ENODEV);
  1242. }
  1243. static inline void dma_release_channel(struct dma_chan *chan)
  1244. {
  1245. }
  1246. static inline int dma_get_slave_caps(struct dma_chan *chan,
  1247. struct dma_slave_caps *caps)
  1248. {
  1249. return -ENXIO;
  1250. }
  1251. #endif
  1252. #define dma_request_slave_channel_reason(dev, name) dma_request_chan(dev, name)
  1253. static inline int dmaengine_desc_set_reuse(struct dma_async_tx_descriptor *tx)
  1254. {
  1255. struct dma_slave_caps caps;
  1256. dma_get_slave_caps(tx->chan, &caps);
  1257. if (caps.descriptor_reuse) {
  1258. tx->flags |= DMA_CTRL_REUSE;
  1259. return 0;
  1260. } else {
  1261. return -EPERM;
  1262. }
  1263. }
  1264. static inline void dmaengine_desc_clear_reuse(struct dma_async_tx_descriptor *tx)
  1265. {
  1266. tx->flags &= ~DMA_CTRL_REUSE;
  1267. }
  1268. static inline bool dmaengine_desc_test_reuse(struct dma_async_tx_descriptor *tx)
  1269. {
  1270. return (tx->flags & DMA_CTRL_REUSE) == DMA_CTRL_REUSE;
  1271. }
  1272. static inline int dmaengine_desc_free(struct dma_async_tx_descriptor *desc)
  1273. {
  1274. /* this is supported for reusable desc, so check that */
  1275. if (dmaengine_desc_test_reuse(desc))
  1276. return desc->desc_free(desc);
  1277. else
  1278. return -EPERM;
  1279. }
  1280. /* --- DMA device --- */
  1281. int dma_async_device_register(struct dma_device *device);
  1282. void dma_async_device_unregister(struct dma_device *device);
  1283. void dma_run_dependencies(struct dma_async_tx_descriptor *tx);
  1284. struct dma_chan *dma_get_slave_channel(struct dma_chan *chan);
  1285. struct dma_chan *dma_get_any_slave_channel(struct dma_device *device);
  1286. #define dma_request_channel(mask, x, y) __dma_request_channel(&(mask), x, y)
  1287. #define dma_request_slave_channel_compat(mask, x, y, dev, name) \
  1288. __dma_request_slave_channel_compat(&(mask), x, y, dev, name)
  1289. static inline struct dma_chan
  1290. *__dma_request_slave_channel_compat(const dma_cap_mask_t *mask,
  1291. dma_filter_fn fn, void *fn_param,
  1292. struct device *dev, const char *name)
  1293. {
  1294. struct dma_chan *chan;
  1295. chan = dma_request_slave_channel(dev, name);
  1296. if (chan)
  1297. return chan;
  1298. if (!fn || !fn_param)
  1299. return NULL;
  1300. return __dma_request_channel(mask, fn, fn_param);
  1301. }
  1302. #endif /* DMAENGINE_H */