mv_xor_v2.c 25 KB

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