dmaengine.h 46 KB

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