mv_xor_v2.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. /*
  2. * Copyright (C) 2015-2016 Marvell International Ltd.
  3. * This program is free software: you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation, either version 2 of the
  6. * License, or any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/msi.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include "dmaengine.h"
  24. /* DMA Engine Registers */
  25. #define MV_XOR_V2_DMA_DESQ_BALR_OFF 0x000
  26. #define MV_XOR_V2_DMA_DESQ_BAHR_OFF 0x004
  27. #define MV_XOR_V2_DMA_DESQ_SIZE_OFF 0x008
  28. #define MV_XOR_V2_DMA_DESQ_DONE_OFF 0x00C
  29. #define MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK 0x7FFF
  30. #define MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT 0
  31. #define MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_MASK 0x1FFF
  32. #define MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_SHIFT 16
  33. #define MV_XOR_V2_DMA_DESQ_ARATTR_OFF 0x010
  34. #define MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK 0x3F3F
  35. #define MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE 0x202
  36. #define MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE 0x3C3C
  37. #define MV_XOR_V2_DMA_IMSG_CDAT_OFF 0x014
  38. #define MV_XOR_V2_DMA_IMSG_THRD_OFF 0x018
  39. #define MV_XOR_V2_DMA_IMSG_THRD_MASK 0x7FFF
  40. #define MV_XOR_V2_DMA_IMSG_THRD_SHIFT 0x0
  41. #define MV_XOR_V2_DMA_IMSG_TIMER_EN BIT(18)
  42. #define MV_XOR_V2_DMA_DESQ_AWATTR_OFF 0x01C
  43. /* Same flags as MV_XOR_V2_DMA_DESQ_ARATTR_OFF */
  44. #define MV_XOR_V2_DMA_DESQ_ALLOC_OFF 0x04C
  45. #define MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_MASK 0xFFFF
  46. #define MV_XOR_V2_DMA_DESQ_ALLOC_WRPTR_SHIFT 16
  47. #define MV_XOR_V2_DMA_IMSG_BALR_OFF 0x050
  48. #define MV_XOR_V2_DMA_IMSG_BAHR_OFF 0x054
  49. #define MV_XOR_V2_DMA_DESQ_CTRL_OFF 0x100
  50. #define MV_XOR_V2_DMA_DESQ_CTRL_32B 1
  51. #define MV_XOR_V2_DMA_DESQ_CTRL_128B 7
  52. #define MV_XOR_V2_DMA_DESQ_STOP_OFF 0x800
  53. #define MV_XOR_V2_DMA_DESQ_DEALLOC_OFF 0x804
  54. #define MV_XOR_V2_DMA_DESQ_ADD_OFF 0x808
  55. #define MV_XOR_V2_DMA_IMSG_TMOT 0x810
  56. #define MV_XOR_V2_DMA_IMSG_TIMER_THRD_MASK 0x1FFF
  57. #define MV_XOR_V2_DMA_IMSG_TIMER_THRD_SHIFT 0
  58. /* XOR Global registers */
  59. #define MV_XOR_V2_GLOB_BW_CTRL 0x4
  60. #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_SHIFT 0
  61. #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_VAL 64
  62. #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_SHIFT 8
  63. #define MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_VAL 8
  64. #define MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_SHIFT 12
  65. #define MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_VAL 4
  66. #define MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_SHIFT 16
  67. #define MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_VAL 4
  68. #define MV_XOR_V2_GLOB_PAUSE 0x014
  69. #define MV_XOR_V2_GLOB_PAUSE_AXI_TIME_DIS_VAL 0x8
  70. #define MV_XOR_V2_GLOB_SYS_INT_CAUSE 0x200
  71. #define MV_XOR_V2_GLOB_SYS_INT_MASK 0x204
  72. #define MV_XOR_V2_GLOB_MEM_INT_CAUSE 0x220
  73. #define MV_XOR_V2_GLOB_MEM_INT_MASK 0x224
  74. #define MV_XOR_V2_MIN_DESC_SIZE 32
  75. #define MV_XOR_V2_EXT_DESC_SIZE 128
  76. #define MV_XOR_V2_DESC_RESERVED_SIZE 12
  77. #define MV_XOR_V2_DESC_BUFF_D_ADDR_SIZE 12
  78. #define MV_XOR_V2_CMD_LINE_NUM_MAX_D_BUF 8
  79. /*
  80. * Descriptors queue size. With 32 bytes descriptors, up to 2^14
  81. * descriptors are allowed, with 128 bytes descriptors, up to 2^12
  82. * descriptors are allowed. This driver uses 128 bytes descriptors,
  83. * but experimentation has shown that a set of 1024 descriptors is
  84. * sufficient to reach a good level of performance.
  85. */
  86. #define MV_XOR_V2_DESC_NUM 1024
  87. /*
  88. * Threshold values for descriptors and timeout, determined by
  89. * experimentation as giving a good level of performance.
  90. */
  91. #define MV_XOR_V2_DONE_IMSG_THRD 0x14
  92. #define MV_XOR_V2_TIMER_THRD 0xB0
  93. /**
  94. * struct mv_xor_v2_descriptor - DMA HW descriptor
  95. * @desc_id: used by S/W and is not affected by H/W.
  96. * @flags: error and status flags
  97. * @crc32_result: CRC32 calculation result
  98. * @desc_ctrl: operation mode and control flags
  99. * @buff_size: amount of bytes to be processed
  100. * @fill_pattern_src_addr: Fill-Pattern or Source-Address and
  101. * AW-Attributes
  102. * @data_buff_addr: Source (and might be RAID6 destination)
  103. * addresses of data buffers in RAID5 and RAID6
  104. * @reserved: reserved
  105. */
  106. struct mv_xor_v2_descriptor {
  107. u16 desc_id;
  108. u16 flags;
  109. u32 crc32_result;
  110. u32 desc_ctrl;
  111. /* Definitions for desc_ctrl */
  112. #define DESC_NUM_ACTIVE_D_BUF_SHIFT 22
  113. #define DESC_OP_MODE_SHIFT 28
  114. #define DESC_OP_MODE_NOP 0 /* Idle operation */
  115. #define DESC_OP_MODE_MEMCPY 1 /* Pure-DMA operation */
  116. #define DESC_OP_MODE_MEMSET 2 /* Mem-Fill operation */
  117. #define DESC_OP_MODE_MEMINIT 3 /* Mem-Init operation */
  118. #define DESC_OP_MODE_MEM_COMPARE 4 /* Mem-Compare operation */
  119. #define DESC_OP_MODE_CRC32 5 /* CRC32 calculation */
  120. #define DESC_OP_MODE_XOR 6 /* RAID5 (XOR) operation */
  121. #define DESC_OP_MODE_RAID6 7 /* RAID6 P&Q-generation */
  122. #define DESC_OP_MODE_RAID6_REC 8 /* RAID6 Recovery */
  123. #define DESC_Q_BUFFER_ENABLE BIT(16)
  124. #define DESC_P_BUFFER_ENABLE BIT(17)
  125. #define DESC_IOD BIT(27)
  126. u32 buff_size;
  127. u32 fill_pattern_src_addr[4];
  128. u32 data_buff_addr[MV_XOR_V2_DESC_BUFF_D_ADDR_SIZE];
  129. u32 reserved[MV_XOR_V2_DESC_RESERVED_SIZE];
  130. };
  131. /**
  132. * struct mv_xor_v2_device - implements a xor device
  133. * @lock: lock for the engine
  134. * @dma_base: memory mapped DMA register base
  135. * @glob_base: memory mapped global register base
  136. * @irq_tasklet:
  137. * @free_sw_desc: linked list of free SW descriptors
  138. * @dmadev: dma device
  139. * @dmachan: dma channel
  140. * @hw_desq: HW descriptors queue
  141. * @hw_desq_virt: virtual address of DESCQ
  142. * @sw_desq: SW descriptors queue
  143. * @desc_size: HW descriptor size
  144. * @npendings: number of pending descriptors (for which tx_submit has
  145. * been called, but not yet issue_pending)
  146. */
  147. struct mv_xor_v2_device {
  148. spinlock_t lock;
  149. void __iomem *dma_base;
  150. void __iomem *glob_base;
  151. struct clk *clk;
  152. struct clk *reg_clk;
  153. struct tasklet_struct irq_tasklet;
  154. struct list_head free_sw_desc;
  155. struct dma_device dmadev;
  156. struct dma_chan dmachan;
  157. dma_addr_t hw_desq;
  158. struct mv_xor_v2_descriptor *hw_desq_virt;
  159. struct mv_xor_v2_sw_desc *sw_desq;
  160. int desc_size;
  161. unsigned int npendings;
  162. unsigned int hw_queue_idx;
  163. struct msi_desc *msi_desc;
  164. };
  165. /**
  166. * struct mv_xor_v2_sw_desc - implements a xor SW descriptor
  167. * @idx: descriptor index
  168. * @async_tx: support for the async_tx api
  169. * @hw_desc: assosiated HW descriptor
  170. * @free_list: node of the free SW descriprots list
  171. */
  172. struct mv_xor_v2_sw_desc {
  173. int idx;
  174. struct dma_async_tx_descriptor async_tx;
  175. struct mv_xor_v2_descriptor hw_desc;
  176. struct list_head free_list;
  177. };
  178. /*
  179. * Fill the data buffers to a HW descriptor
  180. */
  181. static void mv_xor_v2_set_data_buffers(struct mv_xor_v2_device *xor_dev,
  182. struct mv_xor_v2_descriptor *desc,
  183. dma_addr_t src, int index)
  184. {
  185. int arr_index = ((index >> 1) * 3);
  186. /*
  187. * Fill the buffer's addresses to the descriptor.
  188. *
  189. * The format of the buffers address for 2 sequential buffers
  190. * X and X + 1:
  191. *
  192. * First word: Buffer-DX-Address-Low[31:0]
  193. * Second word: Buffer-DX+1-Address-Low[31:0]
  194. * Third word: DX+1-Buffer-Address-High[47:32] [31:16]
  195. * DX-Buffer-Address-High[47:32] [15:0]
  196. */
  197. if ((index & 0x1) == 0) {
  198. desc->data_buff_addr[arr_index] = lower_32_bits(src);
  199. desc->data_buff_addr[arr_index + 2] &= ~0xFFFF;
  200. desc->data_buff_addr[arr_index + 2] |=
  201. upper_32_bits(src) & 0xFFFF;
  202. } else {
  203. desc->data_buff_addr[arr_index + 1] =
  204. lower_32_bits(src);
  205. desc->data_buff_addr[arr_index + 2] &= ~0xFFFF0000;
  206. desc->data_buff_addr[arr_index + 2] |=
  207. (upper_32_bits(src) & 0xFFFF) << 16;
  208. }
  209. }
  210. /*
  211. * notify the engine of new descriptors, and update the available index.
  212. */
  213. static void mv_xor_v2_add_desc_to_desq(struct mv_xor_v2_device *xor_dev,
  214. int num_of_desc)
  215. {
  216. /* write the number of new descriptors in the DESQ. */
  217. writel(num_of_desc, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ADD_OFF);
  218. }
  219. /*
  220. * free HW descriptors
  221. */
  222. static void mv_xor_v2_free_desc_from_desq(struct mv_xor_v2_device *xor_dev,
  223. int num_of_desc)
  224. {
  225. /* write the number of new descriptors in the DESQ. */
  226. writel(num_of_desc, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DEALLOC_OFF);
  227. }
  228. /*
  229. * Set descriptor size
  230. * Return the HW descriptor size in bytes
  231. */
  232. static int mv_xor_v2_set_desc_size(struct mv_xor_v2_device *xor_dev)
  233. {
  234. writel(MV_XOR_V2_DMA_DESQ_CTRL_128B,
  235. xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_CTRL_OFF);
  236. return MV_XOR_V2_EXT_DESC_SIZE;
  237. }
  238. /*
  239. * Set the IMSG threshold
  240. */
  241. static inline
  242. void mv_xor_v2_enable_imsg_thrd(struct mv_xor_v2_device *xor_dev)
  243. {
  244. u32 reg;
  245. /* Configure threshold of number of descriptors, and enable timer */
  246. reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_THRD_OFF);
  247. reg &= (~MV_XOR_V2_DMA_IMSG_THRD_MASK << MV_XOR_V2_DMA_IMSG_THRD_SHIFT);
  248. reg |= (MV_XOR_V2_DONE_IMSG_THRD << MV_XOR_V2_DMA_IMSG_THRD_SHIFT);
  249. reg |= MV_XOR_V2_DMA_IMSG_TIMER_EN;
  250. writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_THRD_OFF);
  251. /* Configure Timer Threshold */
  252. reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_TMOT);
  253. reg &= (~MV_XOR_V2_DMA_IMSG_TIMER_THRD_MASK <<
  254. MV_XOR_V2_DMA_IMSG_TIMER_THRD_SHIFT);
  255. reg |= (MV_XOR_V2_TIMER_THRD << MV_XOR_V2_DMA_IMSG_TIMER_THRD_SHIFT);
  256. writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_TMOT);
  257. }
  258. static irqreturn_t mv_xor_v2_interrupt_handler(int irq, void *data)
  259. {
  260. struct mv_xor_v2_device *xor_dev = data;
  261. unsigned int ndescs;
  262. u32 reg;
  263. reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DONE_OFF);
  264. ndescs = ((reg >> MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT) &
  265. MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK);
  266. /* No descriptors to process */
  267. if (!ndescs)
  268. return IRQ_NONE;
  269. /* schedule a tasklet to handle descriptors callbacks */
  270. tasklet_schedule(&xor_dev->irq_tasklet);
  271. return IRQ_HANDLED;
  272. }
  273. /*
  274. * submit a descriptor to the DMA engine
  275. */
  276. static dma_cookie_t
  277. mv_xor_v2_tx_submit(struct dma_async_tx_descriptor *tx)
  278. {
  279. void *dest_hw_desc;
  280. dma_cookie_t cookie;
  281. struct mv_xor_v2_sw_desc *sw_desc =
  282. container_of(tx, struct mv_xor_v2_sw_desc, async_tx);
  283. struct mv_xor_v2_device *xor_dev =
  284. container_of(tx->chan, struct mv_xor_v2_device, dmachan);
  285. dev_dbg(xor_dev->dmadev.dev,
  286. "%s sw_desc %p: async_tx %p\n",
  287. __func__, sw_desc, &sw_desc->async_tx);
  288. /* assign coookie */
  289. spin_lock_bh(&xor_dev->lock);
  290. cookie = dma_cookie_assign(tx);
  291. /* copy the HW descriptor from the SW descriptor to the DESQ */
  292. dest_hw_desc = xor_dev->hw_desq_virt + xor_dev->hw_queue_idx;
  293. memcpy(dest_hw_desc, &sw_desc->hw_desc, xor_dev->desc_size);
  294. xor_dev->npendings++;
  295. xor_dev->hw_queue_idx++;
  296. if (xor_dev->hw_queue_idx >= MV_XOR_V2_DESC_NUM)
  297. xor_dev->hw_queue_idx = 0;
  298. spin_unlock_bh(&xor_dev->lock);
  299. return cookie;
  300. }
  301. /*
  302. * Prepare a SW descriptor
  303. */
  304. static struct mv_xor_v2_sw_desc *
  305. mv_xor_v2_prep_sw_desc(struct mv_xor_v2_device *xor_dev)
  306. {
  307. struct mv_xor_v2_sw_desc *sw_desc;
  308. bool found = false;
  309. /* Lock the channel */
  310. spin_lock_bh(&xor_dev->lock);
  311. if (list_empty(&xor_dev->free_sw_desc)) {
  312. spin_unlock_bh(&xor_dev->lock);
  313. /* schedule tasklet to free some descriptors */
  314. tasklet_schedule(&xor_dev->irq_tasklet);
  315. return NULL;
  316. }
  317. list_for_each_entry(sw_desc, &xor_dev->free_sw_desc, free_list) {
  318. if (async_tx_test_ack(&sw_desc->async_tx)) {
  319. found = true;
  320. break;
  321. }
  322. }
  323. if (!found) {
  324. spin_unlock_bh(&xor_dev->lock);
  325. return NULL;
  326. }
  327. list_del(&sw_desc->free_list);
  328. /* Release the channel */
  329. spin_unlock_bh(&xor_dev->lock);
  330. return sw_desc;
  331. }
  332. /*
  333. * Prepare a HW descriptor for a memcpy operation
  334. */
  335. static struct dma_async_tx_descriptor *
  336. mv_xor_v2_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
  337. dma_addr_t src, size_t len, unsigned long flags)
  338. {
  339. struct mv_xor_v2_sw_desc *sw_desc;
  340. struct mv_xor_v2_descriptor *hw_descriptor;
  341. struct mv_xor_v2_device *xor_dev;
  342. xor_dev = container_of(chan, struct mv_xor_v2_device, dmachan);
  343. dev_dbg(xor_dev->dmadev.dev,
  344. "%s len: %zu src %pad dest %pad flags: %ld\n",
  345. __func__, len, &src, &dest, flags);
  346. sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
  347. if (!sw_desc)
  348. return NULL;
  349. sw_desc->async_tx.flags = flags;
  350. /* set the HW descriptor */
  351. hw_descriptor = &sw_desc->hw_desc;
  352. /* save the SW descriptor ID to restore when operation is done */
  353. hw_descriptor->desc_id = sw_desc->idx;
  354. /* Set the MEMCPY control word */
  355. hw_descriptor->desc_ctrl =
  356. DESC_OP_MODE_MEMCPY << DESC_OP_MODE_SHIFT;
  357. if (flags & DMA_PREP_INTERRUPT)
  358. hw_descriptor->desc_ctrl |= DESC_IOD;
  359. /* Set source address */
  360. hw_descriptor->fill_pattern_src_addr[0] = lower_32_bits(src);
  361. hw_descriptor->fill_pattern_src_addr[1] =
  362. upper_32_bits(src) & 0xFFFF;
  363. /* Set Destination address */
  364. hw_descriptor->fill_pattern_src_addr[2] = lower_32_bits(dest);
  365. hw_descriptor->fill_pattern_src_addr[3] =
  366. upper_32_bits(dest) & 0xFFFF;
  367. /* Set buffers size */
  368. hw_descriptor->buff_size = len;
  369. /* return the async tx descriptor */
  370. return &sw_desc->async_tx;
  371. }
  372. /*
  373. * Prepare a HW descriptor for a XOR operation
  374. */
  375. static struct dma_async_tx_descriptor *
  376. mv_xor_v2_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  377. unsigned int src_cnt, size_t len, unsigned long flags)
  378. {
  379. struct mv_xor_v2_sw_desc *sw_desc;
  380. struct mv_xor_v2_descriptor *hw_descriptor;
  381. struct mv_xor_v2_device *xor_dev =
  382. container_of(chan, struct mv_xor_v2_device, dmachan);
  383. int i;
  384. if (src_cnt > MV_XOR_V2_CMD_LINE_NUM_MAX_D_BUF || src_cnt < 1)
  385. return NULL;
  386. dev_dbg(xor_dev->dmadev.dev,
  387. "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
  388. __func__, src_cnt, len, &dest, flags);
  389. sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
  390. if (!sw_desc)
  391. return NULL;
  392. sw_desc->async_tx.flags = flags;
  393. /* set the HW descriptor */
  394. hw_descriptor = &sw_desc->hw_desc;
  395. /* save the SW descriptor ID to restore when operation is done */
  396. hw_descriptor->desc_id = sw_desc->idx;
  397. /* Set the XOR control word */
  398. hw_descriptor->desc_ctrl =
  399. DESC_OP_MODE_XOR << DESC_OP_MODE_SHIFT;
  400. hw_descriptor->desc_ctrl |= DESC_P_BUFFER_ENABLE;
  401. if (flags & DMA_PREP_INTERRUPT)
  402. hw_descriptor->desc_ctrl |= DESC_IOD;
  403. /* Set the data buffers */
  404. for (i = 0; i < src_cnt; i++)
  405. mv_xor_v2_set_data_buffers(xor_dev, hw_descriptor, src[i], i);
  406. hw_descriptor->desc_ctrl |=
  407. src_cnt << DESC_NUM_ACTIVE_D_BUF_SHIFT;
  408. /* Set Destination address */
  409. hw_descriptor->fill_pattern_src_addr[2] = lower_32_bits(dest);
  410. hw_descriptor->fill_pattern_src_addr[3] =
  411. upper_32_bits(dest) & 0xFFFF;
  412. /* Set buffers size */
  413. hw_descriptor->buff_size = len;
  414. /* return the async tx descriptor */
  415. return &sw_desc->async_tx;
  416. }
  417. /*
  418. * Prepare a HW descriptor for interrupt operation.
  419. */
  420. static struct dma_async_tx_descriptor *
  421. mv_xor_v2_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
  422. {
  423. struct mv_xor_v2_sw_desc *sw_desc;
  424. struct mv_xor_v2_descriptor *hw_descriptor;
  425. struct mv_xor_v2_device *xor_dev =
  426. container_of(chan, struct mv_xor_v2_device, dmachan);
  427. sw_desc = mv_xor_v2_prep_sw_desc(xor_dev);
  428. if (!sw_desc)
  429. return NULL;
  430. /* set the HW descriptor */
  431. hw_descriptor = &sw_desc->hw_desc;
  432. /* save the SW descriptor ID to restore when operation is done */
  433. hw_descriptor->desc_id = sw_desc->idx;
  434. /* Set the INTERRUPT control word */
  435. hw_descriptor->desc_ctrl =
  436. DESC_OP_MODE_NOP << DESC_OP_MODE_SHIFT;
  437. hw_descriptor->desc_ctrl |= DESC_IOD;
  438. /* return the async tx descriptor */
  439. return &sw_desc->async_tx;
  440. }
  441. /*
  442. * push pending transactions to hardware
  443. */
  444. static void mv_xor_v2_issue_pending(struct dma_chan *chan)
  445. {
  446. struct mv_xor_v2_device *xor_dev =
  447. container_of(chan, struct mv_xor_v2_device, dmachan);
  448. spin_lock_bh(&xor_dev->lock);
  449. /*
  450. * update the engine with the number of descriptors to
  451. * process
  452. */
  453. mv_xor_v2_add_desc_to_desq(xor_dev, xor_dev->npendings);
  454. xor_dev->npendings = 0;
  455. spin_unlock_bh(&xor_dev->lock);
  456. }
  457. static inline
  458. int mv_xor_v2_get_pending_params(struct mv_xor_v2_device *xor_dev,
  459. int *pending_ptr)
  460. {
  461. u32 reg;
  462. reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_DONE_OFF);
  463. /* get the next pending descriptor index */
  464. *pending_ptr = ((reg >> MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_SHIFT) &
  465. MV_XOR_V2_DMA_DESQ_DONE_READ_PTR_MASK);
  466. /* get the number of descriptors pending handle */
  467. return ((reg >> MV_XOR_V2_DMA_DESQ_DONE_PENDING_SHIFT) &
  468. MV_XOR_V2_DMA_DESQ_DONE_PENDING_MASK);
  469. }
  470. /*
  471. * handle the descriptors after HW process
  472. */
  473. static void mv_xor_v2_tasklet(unsigned long data)
  474. {
  475. struct mv_xor_v2_device *xor_dev = (struct mv_xor_v2_device *) data;
  476. int pending_ptr, num_of_pending, i;
  477. struct mv_xor_v2_sw_desc *next_pending_sw_desc = NULL;
  478. dev_dbg(xor_dev->dmadev.dev, "%s %d\n", __func__, __LINE__);
  479. /* get the pending descriptors parameters */
  480. num_of_pending = mv_xor_v2_get_pending_params(xor_dev, &pending_ptr);
  481. /* loop over free descriptors */
  482. for (i = 0; i < num_of_pending; i++) {
  483. struct mv_xor_v2_descriptor *next_pending_hw_desc =
  484. xor_dev->hw_desq_virt + pending_ptr;
  485. /* get the SW descriptor related to the HW descriptor */
  486. next_pending_sw_desc =
  487. &xor_dev->sw_desq[next_pending_hw_desc->desc_id];
  488. /* call the callback */
  489. if (next_pending_sw_desc->async_tx.cookie > 0) {
  490. /*
  491. * update the channel's completed cookie - no
  492. * lock is required the IMSG threshold provide
  493. * the locking
  494. */
  495. dma_cookie_complete(&next_pending_sw_desc->async_tx);
  496. dma_descriptor_unmap(&next_pending_sw_desc->async_tx);
  497. dmaengine_desc_get_callback_invoke(
  498. &next_pending_sw_desc->async_tx, NULL);
  499. }
  500. dma_run_dependencies(&next_pending_sw_desc->async_tx);
  501. /* Lock the channel */
  502. spin_lock_bh(&xor_dev->lock);
  503. /* add the SW descriptor to the free descriptors list */
  504. list_add(&next_pending_sw_desc->free_list,
  505. &xor_dev->free_sw_desc);
  506. /* Release the channel */
  507. spin_unlock_bh(&xor_dev->lock);
  508. /* increment the next descriptor */
  509. pending_ptr++;
  510. if (pending_ptr >= MV_XOR_V2_DESC_NUM)
  511. pending_ptr = 0;
  512. }
  513. if (num_of_pending != 0) {
  514. /* free the descriptores */
  515. mv_xor_v2_free_desc_from_desq(xor_dev, num_of_pending);
  516. }
  517. }
  518. /*
  519. * Set DMA Interrupt-message (IMSG) parameters
  520. */
  521. static void mv_xor_v2_set_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  522. {
  523. struct mv_xor_v2_device *xor_dev = dev_get_drvdata(desc->dev);
  524. writel(msg->address_lo,
  525. xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_BALR_OFF);
  526. writel(msg->address_hi & 0xFFFF,
  527. xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_BAHR_OFF);
  528. writel(msg->data,
  529. xor_dev->dma_base + MV_XOR_V2_DMA_IMSG_CDAT_OFF);
  530. }
  531. static int mv_xor_v2_descq_init(struct mv_xor_v2_device *xor_dev)
  532. {
  533. u32 reg;
  534. /* write the DESQ size to the DMA engine */
  535. writel(MV_XOR_V2_DESC_NUM,
  536. xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_SIZE_OFF);
  537. /* write the DESQ address to the DMA enngine*/
  538. writel(lower_32_bits(xor_dev->hw_desq),
  539. xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_BALR_OFF);
  540. writel(upper_32_bits(xor_dev->hw_desq),
  541. xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_BAHR_OFF);
  542. /*
  543. * This is a temporary solution, until we activate the
  544. * SMMU. Set the attributes for reading & writing data buffers
  545. * & descriptors to:
  546. *
  547. * - OuterShareable - Snoops will be performed on CPU caches
  548. * - Enable cacheable - Bufferable, Modifiable, Other Allocate
  549. * and Allocate
  550. */
  551. reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ARATTR_OFF);
  552. reg &= ~MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK;
  553. reg |= MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE |
  554. MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE;
  555. writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_ARATTR_OFF);
  556. reg = readl(xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_AWATTR_OFF);
  557. reg &= ~MV_XOR_V2_DMA_DESQ_ATTR_CACHE_MASK;
  558. reg |= MV_XOR_V2_DMA_DESQ_ATTR_OUTER_SHAREABLE |
  559. MV_XOR_V2_DMA_DESQ_ATTR_CACHEABLE;
  560. writel(reg, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_AWATTR_OFF);
  561. /* BW CTRL - set values to optimize the XOR performance:
  562. *
  563. * - Set WrBurstLen & RdBurstLen - the unit will issue
  564. * maximum of 256B write/read transactions.
  565. * - Limit the number of outstanding write & read data
  566. * (OBB/IBB) requests to the maximal value.
  567. */
  568. reg = ((MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_VAL <<
  569. MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_RD_SHIFT) |
  570. (MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_VAL <<
  571. MV_XOR_V2_GLOB_BW_CTRL_NUM_OSTD_WR_SHIFT) |
  572. (MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_VAL <<
  573. MV_XOR_V2_GLOB_BW_CTRL_RD_BURST_LEN_SHIFT) |
  574. (MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_VAL <<
  575. MV_XOR_V2_GLOB_BW_CTRL_WR_BURST_LEN_SHIFT));
  576. writel(reg, xor_dev->glob_base + MV_XOR_V2_GLOB_BW_CTRL);
  577. /* Disable the AXI timer feature */
  578. reg = readl(xor_dev->glob_base + MV_XOR_V2_GLOB_PAUSE);
  579. reg |= MV_XOR_V2_GLOB_PAUSE_AXI_TIME_DIS_VAL;
  580. writel(reg, xor_dev->glob_base + MV_XOR_V2_GLOB_PAUSE);
  581. /* enable the DMA engine */
  582. writel(0, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_STOP_OFF);
  583. return 0;
  584. }
  585. static int mv_xor_v2_suspend(struct platform_device *dev, pm_message_t state)
  586. {
  587. struct mv_xor_v2_device *xor_dev = platform_get_drvdata(dev);
  588. /* Set this bit to disable to stop the XOR unit. */
  589. writel(0x1, xor_dev->dma_base + MV_XOR_V2_DMA_DESQ_STOP_OFF);
  590. return 0;
  591. }
  592. static int mv_xor_v2_resume(struct platform_device *dev)
  593. {
  594. struct mv_xor_v2_device *xor_dev = platform_get_drvdata(dev);
  595. mv_xor_v2_set_desc_size(xor_dev);
  596. mv_xor_v2_enable_imsg_thrd(xor_dev);
  597. mv_xor_v2_descq_init(xor_dev);
  598. return 0;
  599. }
  600. static int mv_xor_v2_probe(struct platform_device *pdev)
  601. {
  602. struct mv_xor_v2_device *xor_dev;
  603. struct resource *res;
  604. int i, ret = 0;
  605. struct dma_device *dma_dev;
  606. struct mv_xor_v2_sw_desc *sw_desc;
  607. struct msi_desc *msi_desc;
  608. BUILD_BUG_ON(sizeof(struct mv_xor_v2_descriptor) !=
  609. MV_XOR_V2_EXT_DESC_SIZE);
  610. xor_dev = devm_kzalloc(&pdev->dev, sizeof(*xor_dev), GFP_KERNEL);
  611. if (!xor_dev)
  612. return -ENOMEM;
  613. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  614. xor_dev->dma_base = devm_ioremap_resource(&pdev->dev, res);
  615. if (IS_ERR(xor_dev->dma_base))
  616. return PTR_ERR(xor_dev->dma_base);
  617. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  618. xor_dev->glob_base = devm_ioremap_resource(&pdev->dev, res);
  619. if (IS_ERR(xor_dev->glob_base))
  620. return PTR_ERR(xor_dev->glob_base);
  621. platform_set_drvdata(pdev, xor_dev);
  622. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
  623. if (ret)
  624. return ret;
  625. xor_dev->reg_clk = devm_clk_get(&pdev->dev, "reg");
  626. if (PTR_ERR(xor_dev->reg_clk) != -ENOENT) {
  627. if (!IS_ERR(xor_dev->reg_clk)) {
  628. ret = clk_prepare_enable(xor_dev->reg_clk);
  629. if (ret)
  630. return ret;
  631. } else {
  632. return PTR_ERR(xor_dev->reg_clk);
  633. }
  634. }
  635. xor_dev->clk = devm_clk_get(&pdev->dev, NULL);
  636. if (IS_ERR(xor_dev->clk) && PTR_ERR(xor_dev->clk) == -EPROBE_DEFER) {
  637. ret = EPROBE_DEFER;
  638. goto disable_reg_clk;
  639. }
  640. if (!IS_ERR(xor_dev->clk)) {
  641. ret = clk_prepare_enable(xor_dev->clk);
  642. if (ret)
  643. goto disable_reg_clk;
  644. }
  645. ret = platform_msi_domain_alloc_irqs(&pdev->dev, 1,
  646. mv_xor_v2_set_msi_msg);
  647. if (ret)
  648. goto disable_clk;
  649. msi_desc = first_msi_entry(&pdev->dev);
  650. if (!msi_desc)
  651. goto free_msi_irqs;
  652. xor_dev->msi_desc = msi_desc;
  653. ret = devm_request_irq(&pdev->dev, msi_desc->irq,
  654. mv_xor_v2_interrupt_handler, 0,
  655. dev_name(&pdev->dev), xor_dev);
  656. if (ret)
  657. goto free_msi_irqs;
  658. tasklet_init(&xor_dev->irq_tasklet, mv_xor_v2_tasklet,
  659. (unsigned long) xor_dev);
  660. xor_dev->desc_size = mv_xor_v2_set_desc_size(xor_dev);
  661. dma_cookie_init(&xor_dev->dmachan);
  662. /*
  663. * allocate coherent memory for hardware descriptors
  664. * note: writecombine gives slightly better performance, but
  665. * requires that we explicitly flush the writes
  666. */
  667. xor_dev->hw_desq_virt =
  668. dma_alloc_coherent(&pdev->dev,
  669. xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
  670. &xor_dev->hw_desq, GFP_KERNEL);
  671. if (!xor_dev->hw_desq_virt) {
  672. ret = -ENOMEM;
  673. goto free_msi_irqs;
  674. }
  675. /* alloc memory for the SW descriptors */
  676. xor_dev->sw_desq = devm_kcalloc(&pdev->dev,
  677. MV_XOR_V2_DESC_NUM, sizeof(*sw_desc),
  678. GFP_KERNEL);
  679. if (!xor_dev->sw_desq) {
  680. ret = -ENOMEM;
  681. goto free_hw_desq;
  682. }
  683. spin_lock_init(&xor_dev->lock);
  684. /* init the free SW descriptors list */
  685. INIT_LIST_HEAD(&xor_dev->free_sw_desc);
  686. /* add all SW descriptors to the free list */
  687. for (i = 0; i < MV_XOR_V2_DESC_NUM; i++) {
  688. struct mv_xor_v2_sw_desc *sw_desc =
  689. xor_dev->sw_desq + i;
  690. sw_desc->idx = i;
  691. dma_async_tx_descriptor_init(&sw_desc->async_tx,
  692. &xor_dev->dmachan);
  693. sw_desc->async_tx.tx_submit = mv_xor_v2_tx_submit;
  694. async_tx_ack(&sw_desc->async_tx);
  695. list_add(&sw_desc->free_list,
  696. &xor_dev->free_sw_desc);
  697. }
  698. dma_dev = &xor_dev->dmadev;
  699. /* set DMA capabilities */
  700. dma_cap_zero(dma_dev->cap_mask);
  701. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  702. dma_cap_set(DMA_XOR, dma_dev->cap_mask);
  703. dma_cap_set(DMA_INTERRUPT, dma_dev->cap_mask);
  704. /* init dma link list */
  705. INIT_LIST_HEAD(&dma_dev->channels);
  706. /* set base routines */
  707. dma_dev->device_tx_status = dma_cookie_status;
  708. dma_dev->device_issue_pending = mv_xor_v2_issue_pending;
  709. dma_dev->dev = &pdev->dev;
  710. dma_dev->device_prep_dma_memcpy = mv_xor_v2_prep_dma_memcpy;
  711. dma_dev->device_prep_dma_interrupt = mv_xor_v2_prep_dma_interrupt;
  712. dma_dev->max_xor = 8;
  713. dma_dev->device_prep_dma_xor = mv_xor_v2_prep_dma_xor;
  714. xor_dev->dmachan.device = dma_dev;
  715. list_add_tail(&xor_dev->dmachan.device_node,
  716. &dma_dev->channels);
  717. mv_xor_v2_enable_imsg_thrd(xor_dev);
  718. mv_xor_v2_descq_init(xor_dev);
  719. ret = dma_async_device_register(dma_dev);
  720. if (ret)
  721. goto free_hw_desq;
  722. dev_notice(&pdev->dev, "Marvell Version 2 XOR driver\n");
  723. return 0;
  724. free_hw_desq:
  725. dma_free_coherent(&pdev->dev,
  726. xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
  727. xor_dev->hw_desq_virt, xor_dev->hw_desq);
  728. free_msi_irqs:
  729. platform_msi_domain_free_irqs(&pdev->dev);
  730. disable_clk:
  731. clk_disable_unprepare(xor_dev->clk);
  732. disable_reg_clk:
  733. clk_disable_unprepare(xor_dev->reg_clk);
  734. return ret;
  735. }
  736. static int mv_xor_v2_remove(struct platform_device *pdev)
  737. {
  738. struct mv_xor_v2_device *xor_dev = platform_get_drvdata(pdev);
  739. dma_async_device_unregister(&xor_dev->dmadev);
  740. dma_free_coherent(&pdev->dev,
  741. xor_dev->desc_size * MV_XOR_V2_DESC_NUM,
  742. xor_dev->hw_desq_virt, xor_dev->hw_desq);
  743. devm_free_irq(&pdev->dev, xor_dev->msi_desc->irq, xor_dev);
  744. platform_msi_domain_free_irqs(&pdev->dev);
  745. tasklet_kill(&xor_dev->irq_tasklet);
  746. clk_disable_unprepare(xor_dev->clk);
  747. return 0;
  748. }
  749. #ifdef CONFIG_OF
  750. static const struct of_device_id mv_xor_v2_dt_ids[] = {
  751. { .compatible = "marvell,xor-v2", },
  752. {},
  753. };
  754. MODULE_DEVICE_TABLE(of, mv_xor_v2_dt_ids);
  755. #endif
  756. static struct platform_driver mv_xor_v2_driver = {
  757. .probe = mv_xor_v2_probe,
  758. .suspend = mv_xor_v2_suspend,
  759. .resume = mv_xor_v2_resume,
  760. .remove = mv_xor_v2_remove,
  761. .driver = {
  762. .name = "mv_xor_v2",
  763. .of_match_table = of_match_ptr(mv_xor_v2_dt_ids),
  764. },
  765. };
  766. module_platform_driver(mv_xor_v2_driver);
  767. MODULE_DESCRIPTION("DMA engine driver for Marvell's Version 2 of XOR engine");
  768. MODULE_LICENSE("GPL");