mv_xor_v2.c 25 KB

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