mv_xor.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * offload engine driver for the Marvell XOR engine
  4. * Copyright (C) 2007, 2008, Marvell International Ltd.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/property.h>
  14. #include <linux/memory.h>
  15. #include <linux/clk.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/platform_data/dma-mv_xor.h>
  21. #include "dmaengine.h"
  22. #include "mv_xor.h"
  23. enum mv_xor_type {
  24. XOR_ORION,
  25. XOR_ARMADA_38X,
  26. XOR_ARMADA_37XX,
  27. };
  28. enum mv_xor_mode {
  29. XOR_MODE_IN_REG,
  30. XOR_MODE_IN_DESC,
  31. };
  32. static void mv_xor_issue_pending(struct dma_chan *chan);
  33. #define to_mv_xor_chan(chan) \
  34. container_of(chan, struct mv_xor_chan, dmachan)
  35. #define to_mv_xor_slot(tx) \
  36. container_of(tx, struct mv_xor_desc_slot, async_tx)
  37. #define mv_chan_to_devp(chan) \
  38. ((chan)->dmadev.dev)
  39. static void mv_desc_init(struct mv_xor_desc_slot *desc,
  40. dma_addr_t addr, u32 byte_count,
  41. enum dma_ctrl_flags flags)
  42. {
  43. struct mv_xor_desc *hw_desc = desc->hw_desc;
  44. hw_desc->status = XOR_DESC_DMA_OWNED;
  45. hw_desc->phy_next_desc = 0;
  46. /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
  47. hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ?
  48. XOR_DESC_EOD_INT_EN : 0;
  49. hw_desc->phy_dest_addr = addr;
  50. hw_desc->byte_count = byte_count;
  51. }
  52. static void mv_desc_set_mode(struct mv_xor_desc_slot *desc)
  53. {
  54. struct mv_xor_desc *hw_desc = desc->hw_desc;
  55. switch (desc->type) {
  56. case DMA_XOR:
  57. case DMA_INTERRUPT:
  58. hw_desc->desc_command |= XOR_DESC_OPERATION_XOR;
  59. break;
  60. case DMA_MEMCPY:
  61. hw_desc->desc_command |= XOR_DESC_OPERATION_MEMCPY;
  62. break;
  63. default:
  64. BUG();
  65. return;
  66. }
  67. }
  68. static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
  69. u32 next_desc_addr)
  70. {
  71. struct mv_xor_desc *hw_desc = desc->hw_desc;
  72. BUG_ON(hw_desc->phy_next_desc);
  73. hw_desc->phy_next_desc = next_desc_addr;
  74. }
  75. static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
  76. int index, dma_addr_t addr)
  77. {
  78. struct mv_xor_desc *hw_desc = desc->hw_desc;
  79. hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
  80. if (desc->type == DMA_XOR)
  81. hw_desc->desc_command |= (1 << index);
  82. }
  83. static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
  84. {
  85. return readl_relaxed(XOR_CURR_DESC(chan));
  86. }
  87. static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
  88. u32 next_desc_addr)
  89. {
  90. writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
  91. }
  92. static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
  93. {
  94. u32 val = readl_relaxed(XOR_INTR_MASK(chan));
  95. val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
  96. writel_relaxed(val, XOR_INTR_MASK(chan));
  97. }
  98. static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
  99. {
  100. u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
  101. intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
  102. return intr_cause;
  103. }
  104. static void mv_chan_clear_eoc_cause(struct mv_xor_chan *chan)
  105. {
  106. u32 val;
  107. val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED;
  108. val = ~(val << (chan->idx * 16));
  109. dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
  110. writel_relaxed(val, XOR_INTR_CAUSE(chan));
  111. }
  112. static void mv_chan_clear_err_status(struct mv_xor_chan *chan)
  113. {
  114. u32 val = 0xFFFF0000 >> (chan->idx * 16);
  115. writel_relaxed(val, XOR_INTR_CAUSE(chan));
  116. }
  117. static void mv_chan_set_mode(struct mv_xor_chan *chan,
  118. u32 op_mode)
  119. {
  120. u32 config = readl_relaxed(XOR_CONFIG(chan));
  121. config &= ~0x7;
  122. config |= op_mode;
  123. #if defined(__BIG_ENDIAN)
  124. config |= XOR_DESCRIPTOR_SWAP;
  125. #else
  126. config &= ~XOR_DESCRIPTOR_SWAP;
  127. #endif
  128. writel_relaxed(config, XOR_CONFIG(chan));
  129. }
  130. static void mv_chan_activate(struct mv_xor_chan *chan)
  131. {
  132. dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
  133. /* writel ensures all descriptors are flushed before activation */
  134. writel(BIT(0), XOR_ACTIVATION(chan));
  135. }
  136. static char mv_chan_is_busy(struct mv_xor_chan *chan)
  137. {
  138. u32 state = readl_relaxed(XOR_ACTIVATION(chan));
  139. state = (state >> 4) & 0x3;
  140. return (state == 1) ? 1 : 0;
  141. }
  142. /*
  143. * mv_chan_start_new_chain - program the engine to operate on new
  144. * chain headed by sw_desc
  145. * Caller must hold &mv_chan->lock while calling this function
  146. */
  147. static void mv_chan_start_new_chain(struct mv_xor_chan *mv_chan,
  148. struct mv_xor_desc_slot *sw_desc)
  149. {
  150. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
  151. __func__, __LINE__, sw_desc);
  152. /* set the hardware chain */
  153. mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
  154. mv_chan->pending++;
  155. mv_xor_issue_pending(&mv_chan->dmachan);
  156. }
  157. static dma_cookie_t
  158. mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
  159. struct mv_xor_chan *mv_chan,
  160. dma_cookie_t cookie)
  161. {
  162. BUG_ON(desc->async_tx.cookie < 0);
  163. if (desc->async_tx.cookie > 0) {
  164. cookie = desc->async_tx.cookie;
  165. dma_descriptor_unmap(&desc->async_tx);
  166. /* call the callback (must not sleep or submit new
  167. * operations to this channel)
  168. */
  169. dmaengine_desc_get_callback_invoke(&desc->async_tx, NULL);
  170. }
  171. /* run dependent operations */
  172. dma_run_dependencies(&desc->async_tx);
  173. return cookie;
  174. }
  175. static int
  176. mv_chan_clean_completed_slots(struct mv_xor_chan *mv_chan)
  177. {
  178. struct mv_xor_desc_slot *iter, *_iter;
  179. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  180. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  181. node) {
  182. if (async_tx_test_ack(&iter->async_tx)) {
  183. list_move_tail(&iter->node, &mv_chan->free_slots);
  184. if (!list_empty(&iter->sg_tx_list)) {
  185. list_splice_tail_init(&iter->sg_tx_list,
  186. &mv_chan->free_slots);
  187. }
  188. }
  189. }
  190. return 0;
  191. }
  192. static int
  193. mv_desc_clean_slot(struct mv_xor_desc_slot *desc,
  194. struct mv_xor_chan *mv_chan)
  195. {
  196. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
  197. __func__, __LINE__, desc, desc->async_tx.flags);
  198. /* the client is allowed to attach dependent operations
  199. * until 'ack' is set
  200. */
  201. if (!async_tx_test_ack(&desc->async_tx)) {
  202. /* move this slot to the completed_slots */
  203. list_move_tail(&desc->node, &mv_chan->completed_slots);
  204. if (!list_empty(&desc->sg_tx_list)) {
  205. list_splice_tail_init(&desc->sg_tx_list,
  206. &mv_chan->completed_slots);
  207. }
  208. } else {
  209. list_move_tail(&desc->node, &mv_chan->free_slots);
  210. if (!list_empty(&desc->sg_tx_list)) {
  211. list_splice_tail_init(&desc->sg_tx_list,
  212. &mv_chan->free_slots);
  213. }
  214. }
  215. return 0;
  216. }
  217. /* This function must be called with the mv_xor_chan spinlock held */
  218. static void mv_chan_slot_cleanup(struct mv_xor_chan *mv_chan)
  219. {
  220. struct mv_xor_desc_slot *iter, *_iter;
  221. dma_cookie_t cookie = 0;
  222. int busy = mv_chan_is_busy(mv_chan);
  223. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  224. int current_cleaned = 0;
  225. struct mv_xor_desc *hw_desc;
  226. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  227. dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
  228. mv_chan_clean_completed_slots(mv_chan);
  229. /* free completed slots from the chain starting with
  230. * the oldest descriptor
  231. */
  232. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  233. node) {
  234. /* clean finished descriptors */
  235. hw_desc = iter->hw_desc;
  236. if (hw_desc->status & XOR_DESC_SUCCESS) {
  237. cookie = mv_desc_run_tx_complete_actions(iter, mv_chan,
  238. cookie);
  239. /* done processing desc, clean slot */
  240. mv_desc_clean_slot(iter, mv_chan);
  241. /* break if we did cleaned the current */
  242. if (iter->async_tx.phys == current_desc) {
  243. current_cleaned = 1;
  244. break;
  245. }
  246. } else {
  247. if (iter->async_tx.phys == current_desc) {
  248. current_cleaned = 0;
  249. break;
  250. }
  251. }
  252. }
  253. if ((busy == 0) && !list_empty(&mv_chan->chain)) {
  254. if (current_cleaned) {
  255. /*
  256. * current descriptor cleaned and removed, run
  257. * from list head
  258. */
  259. iter = list_entry(mv_chan->chain.next,
  260. struct mv_xor_desc_slot,
  261. node);
  262. mv_chan_start_new_chain(mv_chan, iter);
  263. } else {
  264. if (!list_is_last(&iter->node, &mv_chan->chain)) {
  265. /*
  266. * descriptors are still waiting after
  267. * current, trigger them
  268. */
  269. iter = list_entry(iter->node.next,
  270. struct mv_xor_desc_slot,
  271. node);
  272. mv_chan_start_new_chain(mv_chan, iter);
  273. } else {
  274. /*
  275. * some descriptors are still waiting
  276. * to be cleaned
  277. */
  278. tasklet_schedule(&mv_chan->irq_tasklet);
  279. }
  280. }
  281. }
  282. if (cookie > 0)
  283. mv_chan->dmachan.completed_cookie = cookie;
  284. }
  285. static void mv_xor_tasklet(struct tasklet_struct *t)
  286. {
  287. struct mv_xor_chan *chan = from_tasklet(chan, t, irq_tasklet);
  288. spin_lock(&chan->lock);
  289. mv_chan_slot_cleanup(chan);
  290. spin_unlock(&chan->lock);
  291. }
  292. static struct mv_xor_desc_slot *
  293. mv_chan_alloc_slot(struct mv_xor_chan *mv_chan)
  294. {
  295. struct mv_xor_desc_slot *iter;
  296. spin_lock_bh(&mv_chan->lock);
  297. if (!list_empty(&mv_chan->free_slots)) {
  298. iter = list_first_entry(&mv_chan->free_slots,
  299. struct mv_xor_desc_slot,
  300. node);
  301. list_move_tail(&iter->node, &mv_chan->allocated_slots);
  302. spin_unlock_bh(&mv_chan->lock);
  303. /* pre-ack descriptor */
  304. async_tx_ack(&iter->async_tx);
  305. iter->async_tx.cookie = -EBUSY;
  306. return iter;
  307. }
  308. spin_unlock_bh(&mv_chan->lock);
  309. /* try to free some slots if the allocation fails */
  310. tasklet_schedule(&mv_chan->irq_tasklet);
  311. return NULL;
  312. }
  313. /************************ DMA engine API functions ****************************/
  314. static dma_cookie_t
  315. mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
  316. {
  317. struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
  318. struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
  319. struct mv_xor_desc_slot *old_chain_tail;
  320. dma_cookie_t cookie;
  321. int new_hw_chain = 1;
  322. dev_dbg(mv_chan_to_devp(mv_chan),
  323. "%s sw_desc %p: async_tx %p\n",
  324. __func__, sw_desc, &sw_desc->async_tx);
  325. spin_lock_bh(&mv_chan->lock);
  326. cookie = dma_cookie_assign(tx);
  327. if (list_empty(&mv_chan->chain))
  328. list_move_tail(&sw_desc->node, &mv_chan->chain);
  329. else {
  330. new_hw_chain = 0;
  331. old_chain_tail = list_entry(mv_chan->chain.prev,
  332. struct mv_xor_desc_slot,
  333. node);
  334. list_move_tail(&sw_desc->node, &mv_chan->chain);
  335. dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
  336. &old_chain_tail->async_tx.phys);
  337. /* fix up the hardware chain */
  338. mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys);
  339. /* if the channel is not busy */
  340. if (!mv_chan_is_busy(mv_chan)) {
  341. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  342. /*
  343. * and the current desc is the end of the chain before
  344. * the append, then we need to start the channel
  345. */
  346. if (current_desc == old_chain_tail->async_tx.phys)
  347. new_hw_chain = 1;
  348. }
  349. }
  350. if (new_hw_chain)
  351. mv_chan_start_new_chain(mv_chan, sw_desc);
  352. spin_unlock_bh(&mv_chan->lock);
  353. return cookie;
  354. }
  355. /* returns the number of allocated descriptors */
  356. static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
  357. {
  358. void *virt_desc;
  359. dma_addr_t dma_desc;
  360. int idx;
  361. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  362. struct mv_xor_desc_slot *slot = NULL;
  363. int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
  364. /* Allocate descriptor slots */
  365. idx = mv_chan->slots_allocated;
  366. while (idx < num_descs_in_pool) {
  367. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  368. if (!slot) {
  369. dev_info(mv_chan_to_devp(mv_chan),
  370. "channel only initialized %d descriptor slots",
  371. idx);
  372. break;
  373. }
  374. virt_desc = mv_chan->dma_desc_pool_virt;
  375. slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
  376. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  377. slot->async_tx.tx_submit = mv_xor_tx_submit;
  378. INIT_LIST_HEAD(&slot->node);
  379. INIT_LIST_HEAD(&slot->sg_tx_list);
  380. dma_desc = mv_chan->dma_desc_pool;
  381. slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
  382. slot->idx = idx++;
  383. spin_lock_bh(&mv_chan->lock);
  384. mv_chan->slots_allocated = idx;
  385. list_add_tail(&slot->node, &mv_chan->free_slots);
  386. spin_unlock_bh(&mv_chan->lock);
  387. }
  388. dev_dbg(mv_chan_to_devp(mv_chan),
  389. "allocated %d descriptor slots\n",
  390. mv_chan->slots_allocated);
  391. return mv_chan->slots_allocated ? : -ENOMEM;
  392. }
  393. /*
  394. * Check if source or destination is an PCIe/IO address (non-SDRAM) and add
  395. * a new MBus window if necessary. Use a cache for these check so that
  396. * the MMIO mapped registers don't have to be accessed for this check
  397. * to speed up this process.
  398. */
  399. static int mv_xor_add_io_win(struct mv_xor_chan *mv_chan, u32 addr)
  400. {
  401. struct mv_xor_device *xordev = mv_chan->xordev;
  402. void __iomem *base = mv_chan->mmr_high_base;
  403. u32 win_enable;
  404. u32 size;
  405. u8 target, attr;
  406. int ret;
  407. int i;
  408. /* Nothing needs to get done for the Armada 3700 */
  409. if (xordev->xor_type == XOR_ARMADA_37XX)
  410. return 0;
  411. /*
  412. * Loop over the cached windows to check, if the requested area
  413. * is already mapped. If this the case, nothing needs to be done
  414. * and we can return.
  415. */
  416. for (i = 0; i < WINDOW_COUNT; i++) {
  417. if (addr >= xordev->win_start[i] &&
  418. addr <= xordev->win_end[i]) {
  419. /* Window is already mapped */
  420. return 0;
  421. }
  422. }
  423. /*
  424. * The window is not mapped, so we need to create the new mapping
  425. */
  426. /* If no IO window is found that addr has to be located in SDRAM */
  427. ret = mvebu_mbus_get_io_win_info(addr, &size, &target, &attr);
  428. if (ret < 0)
  429. return 0;
  430. /*
  431. * Mask the base addr 'addr' according to 'size' read back from the
  432. * MBus window. Otherwise we might end up with an address located
  433. * somewhere in the middle of this area here.
  434. */
  435. size -= 1;
  436. addr &= ~size;
  437. /*
  438. * Reading one of both enabled register is enough, as they are always
  439. * programmed to the identical values
  440. */
  441. win_enable = readl(base + WINDOW_BAR_ENABLE(0));
  442. /* Set 'i' to the first free window to write the new values to */
  443. i = ffs(~win_enable) - 1;
  444. if (i >= WINDOW_COUNT)
  445. return -ENOMEM;
  446. writel((addr & 0xffff0000) | (attr << 8) | target,
  447. base + WINDOW_BASE(i));
  448. writel(size & 0xffff0000, base + WINDOW_SIZE(i));
  449. /* Fill the caching variables for later use */
  450. xordev->win_start[i] = addr;
  451. xordev->win_end[i] = addr + size;
  452. win_enable |= (1 << i);
  453. win_enable |= 3 << (16 + (2 * i));
  454. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  455. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  456. return 0;
  457. }
  458. static struct dma_async_tx_descriptor *
  459. mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  460. unsigned int src_cnt, size_t len, unsigned long flags)
  461. {
  462. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  463. struct mv_xor_desc_slot *sw_desc;
  464. int ret;
  465. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  466. return NULL;
  467. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  468. dev_dbg(mv_chan_to_devp(mv_chan),
  469. "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
  470. __func__, src_cnt, len, &dest, flags);
  471. /* Check if a new window needs to get added for 'dest' */
  472. ret = mv_xor_add_io_win(mv_chan, dest);
  473. if (ret)
  474. return NULL;
  475. sw_desc = mv_chan_alloc_slot(mv_chan);
  476. if (sw_desc) {
  477. sw_desc->type = DMA_XOR;
  478. sw_desc->async_tx.flags = flags;
  479. mv_desc_init(sw_desc, dest, len, flags);
  480. if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
  481. mv_desc_set_mode(sw_desc);
  482. while (src_cnt--) {
  483. /* Check if a new window needs to get added for 'src' */
  484. ret = mv_xor_add_io_win(mv_chan, src[src_cnt]);
  485. if (ret)
  486. return NULL;
  487. mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]);
  488. }
  489. }
  490. dev_dbg(mv_chan_to_devp(mv_chan),
  491. "%s sw_desc %p async_tx %p \n",
  492. __func__, sw_desc, &sw_desc->async_tx);
  493. return sw_desc ? &sw_desc->async_tx : NULL;
  494. }
  495. static struct dma_async_tx_descriptor *
  496. mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  497. size_t len, unsigned long flags)
  498. {
  499. /*
  500. * A MEMCPY operation is identical to an XOR operation with only
  501. * a single source address.
  502. */
  503. return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
  504. }
  505. static struct dma_async_tx_descriptor *
  506. mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
  507. {
  508. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  509. dma_addr_t src, dest;
  510. size_t len;
  511. src = mv_chan->dummy_src_addr;
  512. dest = mv_chan->dummy_dst_addr;
  513. len = MV_XOR_MIN_BYTE_COUNT;
  514. /*
  515. * We implement the DMA_INTERRUPT operation as a minimum sized
  516. * XOR operation with a single dummy source address.
  517. */
  518. return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
  519. }
  520. static void mv_xor_free_chan_resources(struct dma_chan *chan)
  521. {
  522. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  523. struct mv_xor_desc_slot *iter, *_iter;
  524. int in_use_descs = 0;
  525. spin_lock_bh(&mv_chan->lock);
  526. mv_chan_slot_cleanup(mv_chan);
  527. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  528. node) {
  529. in_use_descs++;
  530. list_move_tail(&iter->node, &mv_chan->free_slots);
  531. }
  532. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  533. node) {
  534. in_use_descs++;
  535. list_move_tail(&iter->node, &mv_chan->free_slots);
  536. }
  537. list_for_each_entry_safe(iter, _iter, &mv_chan->allocated_slots,
  538. node) {
  539. in_use_descs++;
  540. list_move_tail(&iter->node, &mv_chan->free_slots);
  541. }
  542. list_for_each_entry_safe_reverse(
  543. iter, _iter, &mv_chan->free_slots, node) {
  544. list_del(&iter->node);
  545. kfree(iter);
  546. mv_chan->slots_allocated--;
  547. }
  548. dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
  549. __func__, mv_chan->slots_allocated);
  550. spin_unlock_bh(&mv_chan->lock);
  551. if (in_use_descs)
  552. dev_err(mv_chan_to_devp(mv_chan),
  553. "freeing %d in use descriptors!\n", in_use_descs);
  554. }
  555. /**
  556. * mv_xor_status - poll the status of an XOR transaction
  557. * @chan: XOR channel handle
  558. * @cookie: XOR transaction identifier
  559. * @txstate: XOR transactions state holder (or NULL)
  560. */
  561. static enum dma_status mv_xor_status(struct dma_chan *chan,
  562. dma_cookie_t cookie,
  563. struct dma_tx_state *txstate)
  564. {
  565. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  566. enum dma_status ret;
  567. ret = dma_cookie_status(chan, cookie, txstate);
  568. if (ret == DMA_COMPLETE)
  569. return ret;
  570. spin_lock_bh(&mv_chan->lock);
  571. mv_chan_slot_cleanup(mv_chan);
  572. spin_unlock_bh(&mv_chan->lock);
  573. return dma_cookie_status(chan, cookie, txstate);
  574. }
  575. static void mv_chan_dump_regs(struct mv_xor_chan *chan)
  576. {
  577. u32 val;
  578. val = readl_relaxed(XOR_CONFIG(chan));
  579. dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
  580. val = readl_relaxed(XOR_ACTIVATION(chan));
  581. dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
  582. val = readl_relaxed(XOR_INTR_CAUSE(chan));
  583. dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
  584. val = readl_relaxed(XOR_INTR_MASK(chan));
  585. dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
  586. val = readl_relaxed(XOR_ERROR_CAUSE(chan));
  587. dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
  588. val = readl_relaxed(XOR_ERROR_ADDR(chan));
  589. dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
  590. }
  591. static void mv_chan_err_interrupt_handler(struct mv_xor_chan *chan,
  592. u32 intr_cause)
  593. {
  594. if (intr_cause & XOR_INT_ERR_DECODE) {
  595. dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
  596. return;
  597. }
  598. dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n",
  599. chan->idx, intr_cause);
  600. mv_chan_dump_regs(chan);
  601. WARN_ON(1);
  602. }
  603. static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
  604. {
  605. struct mv_xor_chan *chan = data;
  606. u32 intr_cause = mv_chan_get_intr_cause(chan);
  607. dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
  608. if (intr_cause & XOR_INTR_ERRORS)
  609. mv_chan_err_interrupt_handler(chan, intr_cause);
  610. tasklet_schedule(&chan->irq_tasklet);
  611. mv_chan_clear_eoc_cause(chan);
  612. return IRQ_HANDLED;
  613. }
  614. static void mv_xor_issue_pending(struct dma_chan *chan)
  615. {
  616. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  617. if (mv_chan->pending >= MV_XOR_THRESHOLD) {
  618. mv_chan->pending = 0;
  619. mv_chan_activate(mv_chan);
  620. }
  621. }
  622. /*
  623. * Perform a transaction to verify the HW works.
  624. */
  625. static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan)
  626. {
  627. int i, ret;
  628. void *src, *dest;
  629. dma_addr_t src_dma, dest_dma;
  630. struct dma_chan *dma_chan;
  631. dma_cookie_t cookie;
  632. struct dma_async_tx_descriptor *tx;
  633. struct dmaengine_unmap_data *unmap;
  634. int err = 0;
  635. src = kmalloc(PAGE_SIZE, GFP_KERNEL);
  636. if (!src)
  637. return -ENOMEM;
  638. dest = kzalloc(PAGE_SIZE, GFP_KERNEL);
  639. if (!dest) {
  640. kfree(src);
  641. return -ENOMEM;
  642. }
  643. /* Fill in src buffer */
  644. for (i = 0; i < PAGE_SIZE; i++)
  645. ((u8 *) src)[i] = (u8)i;
  646. dma_chan = &mv_chan->dmachan;
  647. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  648. err = -ENODEV;
  649. goto out;
  650. }
  651. unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
  652. if (!unmap) {
  653. err = -ENOMEM;
  654. goto free_resources;
  655. }
  656. src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src),
  657. offset_in_page(src), PAGE_SIZE,
  658. DMA_TO_DEVICE);
  659. unmap->addr[0] = src_dma;
  660. ret = dma_mapping_error(dma_chan->device->dev, src_dma);
  661. if (ret) {
  662. err = -ENOMEM;
  663. goto free_resources;
  664. }
  665. unmap->to_cnt = 1;
  666. dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest),
  667. offset_in_page(dest), PAGE_SIZE,
  668. DMA_FROM_DEVICE);
  669. unmap->addr[1] = dest_dma;
  670. ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
  671. if (ret) {
  672. err = -ENOMEM;
  673. goto free_resources;
  674. }
  675. unmap->from_cnt = 1;
  676. unmap->len = PAGE_SIZE;
  677. tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  678. PAGE_SIZE, 0);
  679. if (!tx) {
  680. dev_err(dma_chan->device->dev,
  681. "Self-test cannot prepare operation, disabling\n");
  682. err = -ENODEV;
  683. goto free_resources;
  684. }
  685. cookie = mv_xor_tx_submit(tx);
  686. if (dma_submit_error(cookie)) {
  687. dev_err(dma_chan->device->dev,
  688. "Self-test submit error, disabling\n");
  689. err = -ENODEV;
  690. goto free_resources;
  691. }
  692. mv_xor_issue_pending(dma_chan);
  693. async_tx_ack(tx);
  694. msleep(1);
  695. if (mv_xor_status(dma_chan, cookie, NULL) !=
  696. DMA_COMPLETE) {
  697. dev_err(dma_chan->device->dev,
  698. "Self-test copy timed out, disabling\n");
  699. err = -ENODEV;
  700. goto free_resources;
  701. }
  702. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  703. PAGE_SIZE, DMA_FROM_DEVICE);
  704. if (memcmp(src, dest, PAGE_SIZE)) {
  705. dev_err(dma_chan->device->dev,
  706. "Self-test copy failed compare, disabling\n");
  707. err = -ENODEV;
  708. goto free_resources;
  709. }
  710. free_resources:
  711. dmaengine_unmap_put(unmap);
  712. mv_xor_free_chan_resources(dma_chan);
  713. out:
  714. kfree(src);
  715. kfree(dest);
  716. return err;
  717. }
  718. #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
  719. static int
  720. mv_chan_xor_self_test(struct mv_xor_chan *mv_chan)
  721. {
  722. int i, src_idx, ret;
  723. struct page *dest;
  724. struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
  725. dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
  726. dma_addr_t dest_dma;
  727. struct dma_async_tx_descriptor *tx;
  728. struct dmaengine_unmap_data *unmap;
  729. struct dma_chan *dma_chan;
  730. dma_cookie_t cookie;
  731. u8 cmp_byte = 0;
  732. u32 cmp_word;
  733. int err = 0;
  734. int src_count = MV_XOR_NUM_SRC_TEST;
  735. for (src_idx = 0; src_idx < src_count; src_idx++) {
  736. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  737. if (!xor_srcs[src_idx]) {
  738. while (src_idx--)
  739. __free_page(xor_srcs[src_idx]);
  740. return -ENOMEM;
  741. }
  742. }
  743. dest = alloc_page(GFP_KERNEL);
  744. if (!dest) {
  745. while (src_idx--)
  746. __free_page(xor_srcs[src_idx]);
  747. return -ENOMEM;
  748. }
  749. /* Fill in src buffers */
  750. for (src_idx = 0; src_idx < src_count; src_idx++) {
  751. u8 *ptr = page_address(xor_srcs[src_idx]);
  752. for (i = 0; i < PAGE_SIZE; i++)
  753. ptr[i] = (1 << src_idx);
  754. }
  755. for (src_idx = 0; src_idx < src_count; src_idx++)
  756. cmp_byte ^= (u8) (1 << src_idx);
  757. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  758. (cmp_byte << 8) | cmp_byte;
  759. memset(page_address(dest), 0, PAGE_SIZE);
  760. dma_chan = &mv_chan->dmachan;
  761. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  762. err = -ENODEV;
  763. goto out;
  764. }
  765. unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
  766. GFP_KERNEL);
  767. if (!unmap) {
  768. err = -ENOMEM;
  769. goto free_resources;
  770. }
  771. /* test xor */
  772. for (i = 0; i < src_count; i++) {
  773. unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  774. 0, PAGE_SIZE, DMA_TO_DEVICE);
  775. dma_srcs[i] = unmap->addr[i];
  776. ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
  777. if (ret) {
  778. err = -ENOMEM;
  779. goto free_resources;
  780. }
  781. unmap->to_cnt++;
  782. }
  783. unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
  784. DMA_FROM_DEVICE);
  785. dest_dma = unmap->addr[src_count];
  786. ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
  787. if (ret) {
  788. err = -ENOMEM;
  789. goto free_resources;
  790. }
  791. unmap->from_cnt = 1;
  792. unmap->len = PAGE_SIZE;
  793. tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  794. src_count, PAGE_SIZE, 0);
  795. if (!tx) {
  796. dev_err(dma_chan->device->dev,
  797. "Self-test cannot prepare operation, disabling\n");
  798. err = -ENODEV;
  799. goto free_resources;
  800. }
  801. cookie = mv_xor_tx_submit(tx);
  802. if (dma_submit_error(cookie)) {
  803. dev_err(dma_chan->device->dev,
  804. "Self-test submit error, disabling\n");
  805. err = -ENODEV;
  806. goto free_resources;
  807. }
  808. mv_xor_issue_pending(dma_chan);
  809. async_tx_ack(tx);
  810. msleep(8);
  811. if (mv_xor_status(dma_chan, cookie, NULL) !=
  812. DMA_COMPLETE) {
  813. dev_err(dma_chan->device->dev,
  814. "Self-test xor timed out, disabling\n");
  815. err = -ENODEV;
  816. goto free_resources;
  817. }
  818. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  819. PAGE_SIZE, DMA_FROM_DEVICE);
  820. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  821. u32 *ptr = page_address(dest);
  822. if (ptr[i] != cmp_word) {
  823. dev_err(dma_chan->device->dev,
  824. "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
  825. i, ptr[i], cmp_word);
  826. err = -ENODEV;
  827. goto free_resources;
  828. }
  829. }
  830. free_resources:
  831. dmaengine_unmap_put(unmap);
  832. mv_xor_free_chan_resources(dma_chan);
  833. out:
  834. src_idx = src_count;
  835. while (src_idx--)
  836. __free_page(xor_srcs[src_idx]);
  837. __free_page(dest);
  838. return err;
  839. }
  840. static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
  841. {
  842. struct dma_chan *chan, *_chan;
  843. struct device *dev = mv_chan->dmadev.dev;
  844. dma_async_device_unregister(&mv_chan->dmadev);
  845. dma_free_coherent(dev, MV_XOR_POOL_SIZE,
  846. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  847. dma_unmap_single(dev, mv_chan->dummy_src_addr,
  848. MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
  849. dma_unmap_single(dev, mv_chan->dummy_dst_addr,
  850. MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
  851. list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
  852. device_node) {
  853. list_del(&chan->device_node);
  854. }
  855. free_irq(mv_chan->irq, mv_chan);
  856. return 0;
  857. }
  858. static struct mv_xor_chan *
  859. mv_xor_channel_add(struct mv_xor_device *xordev,
  860. struct platform_device *pdev,
  861. int idx, dma_cap_mask_t cap_mask, int irq)
  862. {
  863. int ret = 0;
  864. struct mv_xor_chan *mv_chan;
  865. struct dma_device *dma_dev;
  866. mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
  867. if (!mv_chan)
  868. return ERR_PTR(-ENOMEM);
  869. mv_chan->idx = idx;
  870. mv_chan->irq = irq;
  871. if (xordev->xor_type == XOR_ORION)
  872. mv_chan->op_in_desc = XOR_MODE_IN_REG;
  873. else
  874. mv_chan->op_in_desc = XOR_MODE_IN_DESC;
  875. dma_dev = &mv_chan->dmadev;
  876. dma_dev->dev = &pdev->dev;
  877. mv_chan->xordev = xordev;
  878. /*
  879. * These source and destination dummy buffers are used to implement
  880. * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
  881. * Hence, we only need to map the buffers at initialization-time.
  882. */
  883. mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev,
  884. mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
  885. if (dma_mapping_error(dma_dev->dev, mv_chan->dummy_src_addr))
  886. return ERR_PTR(-ENOMEM);
  887. mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev,
  888. mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
  889. if (dma_mapping_error(dma_dev->dev, mv_chan->dummy_dst_addr)) {
  890. ret = -ENOMEM;
  891. goto err_unmap_src;
  892. }
  893. /* allocate coherent memory for hardware descriptors
  894. * note: writecombine gives slightly better performance, but
  895. * requires that we explicitly flush the writes
  896. */
  897. mv_chan->dma_desc_pool_virt =
  898. dma_alloc_wc(&pdev->dev, MV_XOR_POOL_SIZE, &mv_chan->dma_desc_pool,
  899. GFP_KERNEL);
  900. if (!mv_chan->dma_desc_pool_virt) {
  901. ret = -ENOMEM;
  902. goto err_unmap_dst;
  903. }
  904. /* discover transaction capabilities from the platform data */
  905. dma_dev->cap_mask = cap_mask;
  906. INIT_LIST_HEAD(&dma_dev->channels);
  907. /* set base routines */
  908. dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
  909. dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
  910. dma_dev->device_tx_status = mv_xor_status;
  911. dma_dev->device_issue_pending = mv_xor_issue_pending;
  912. /* set prep routines based on capability */
  913. if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
  914. dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt;
  915. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  916. dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
  917. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  918. dma_dev->max_xor = 8;
  919. dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
  920. }
  921. mv_chan->mmr_base = xordev->xor_base;
  922. mv_chan->mmr_high_base = xordev->xor_high_base;
  923. tasklet_setup(&mv_chan->irq_tasklet, mv_xor_tasklet);
  924. /* clear errors before enabling interrupts */
  925. mv_chan_clear_err_status(mv_chan);
  926. ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
  927. 0, dev_name(&pdev->dev), mv_chan);
  928. if (ret)
  929. goto err_free_dma;
  930. mv_chan_unmask_interrupts(mv_chan);
  931. if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
  932. mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_IN_DESC);
  933. else
  934. mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_XOR);
  935. spin_lock_init(&mv_chan->lock);
  936. INIT_LIST_HEAD(&mv_chan->chain);
  937. INIT_LIST_HEAD(&mv_chan->completed_slots);
  938. INIT_LIST_HEAD(&mv_chan->free_slots);
  939. INIT_LIST_HEAD(&mv_chan->allocated_slots);
  940. mv_chan->dmachan.device = dma_dev;
  941. dma_cookie_init(&mv_chan->dmachan);
  942. list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
  943. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  944. ret = mv_chan_memcpy_self_test(mv_chan);
  945. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  946. if (ret)
  947. goto err_free_irq;
  948. }
  949. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  950. ret = mv_chan_xor_self_test(mv_chan);
  951. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  952. if (ret)
  953. goto err_free_irq;
  954. }
  955. dev_info(&pdev->dev, "Marvell XOR (%s): ( %s%s%s)\n",
  956. mv_chan->op_in_desc ? "Descriptor Mode" : "Registers Mode",
  957. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  958. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  959. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  960. ret = dma_async_device_register(dma_dev);
  961. if (ret)
  962. goto err_free_irq;
  963. return mv_chan;
  964. err_free_irq:
  965. free_irq(mv_chan->irq, mv_chan);
  966. err_free_dma:
  967. dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
  968. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  969. err_unmap_dst:
  970. dma_unmap_single(dma_dev->dev, mv_chan->dummy_dst_addr,
  971. MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
  972. err_unmap_src:
  973. dma_unmap_single(dma_dev->dev, mv_chan->dummy_src_addr,
  974. MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
  975. return ERR_PTR(ret);
  976. }
  977. static void
  978. mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
  979. const struct mbus_dram_target_info *dram)
  980. {
  981. void __iomem *base = xordev->xor_high_base;
  982. u32 win_enable = 0;
  983. int i;
  984. for (i = 0; i < 8; i++) {
  985. writel(0, base + WINDOW_BASE(i));
  986. writel(0, base + WINDOW_SIZE(i));
  987. if (i < 4)
  988. writel(0, base + WINDOW_REMAP_HIGH(i));
  989. }
  990. for (i = 0; i < dram->num_cs; i++) {
  991. const struct mbus_dram_window *cs = dram->cs + i;
  992. writel((cs->base & 0xffff0000) |
  993. (cs->mbus_attr << 8) |
  994. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  995. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  996. /* Fill the caching variables for later use */
  997. xordev->win_start[i] = cs->base;
  998. xordev->win_end[i] = cs->base + cs->size - 1;
  999. win_enable |= (1 << i);
  1000. win_enable |= 3 << (16 + (2 * i));
  1001. }
  1002. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  1003. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  1004. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  1005. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  1006. }
  1007. static void
  1008. mv_xor_conf_mbus_windows_a3700(struct mv_xor_device *xordev)
  1009. {
  1010. void __iomem *base = xordev->xor_high_base;
  1011. u32 win_enable = 0;
  1012. int i;
  1013. for (i = 0; i < 8; i++) {
  1014. writel(0, base + WINDOW_BASE(i));
  1015. writel(0, base + WINDOW_SIZE(i));
  1016. if (i < 4)
  1017. writel(0, base + WINDOW_REMAP_HIGH(i));
  1018. }
  1019. /*
  1020. * For Armada3700 open default 4GB Mbus window. The dram
  1021. * related configuration are done at AXIS level.
  1022. */
  1023. writel(0xffff0000, base + WINDOW_SIZE(0));
  1024. win_enable |= 1;
  1025. win_enable |= 3 << 16;
  1026. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  1027. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  1028. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  1029. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  1030. }
  1031. /*
  1032. * Since this XOR driver is basically used only for RAID5, we don't
  1033. * need to care about synchronizing ->suspend with DMA activity,
  1034. * because the DMA engine will naturally be quiet due to the block
  1035. * devices being suspended.
  1036. */
  1037. static int mv_xor_suspend(struct platform_device *pdev, pm_message_t state)
  1038. {
  1039. struct mv_xor_device *xordev = platform_get_drvdata(pdev);
  1040. int i;
  1041. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1042. struct mv_xor_chan *mv_chan = xordev->channels[i];
  1043. if (!mv_chan)
  1044. continue;
  1045. mv_chan->saved_config_reg =
  1046. readl_relaxed(XOR_CONFIG(mv_chan));
  1047. mv_chan->saved_int_mask_reg =
  1048. readl_relaxed(XOR_INTR_MASK(mv_chan));
  1049. }
  1050. return 0;
  1051. }
  1052. static int mv_xor_resume(struct platform_device *dev)
  1053. {
  1054. struct mv_xor_device *xordev = platform_get_drvdata(dev);
  1055. const struct mbus_dram_target_info *dram;
  1056. int i;
  1057. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1058. struct mv_xor_chan *mv_chan = xordev->channels[i];
  1059. if (!mv_chan)
  1060. continue;
  1061. writel_relaxed(mv_chan->saved_config_reg,
  1062. XOR_CONFIG(mv_chan));
  1063. writel_relaxed(mv_chan->saved_int_mask_reg,
  1064. XOR_INTR_MASK(mv_chan));
  1065. }
  1066. if (xordev->xor_type == XOR_ARMADA_37XX) {
  1067. mv_xor_conf_mbus_windows_a3700(xordev);
  1068. return 0;
  1069. }
  1070. dram = mv_mbus_dram_info();
  1071. if (dram)
  1072. mv_xor_conf_mbus_windows(xordev, dram);
  1073. return 0;
  1074. }
  1075. static const struct of_device_id mv_xor_dt_ids[] = {
  1076. { .compatible = "marvell,orion-xor", .data = (void *)XOR_ORION },
  1077. { .compatible = "marvell,armada-380-xor", .data = (void *)XOR_ARMADA_38X },
  1078. { .compatible = "marvell,armada-3700-xor", .data = (void *)XOR_ARMADA_37XX },
  1079. {},
  1080. };
  1081. static unsigned int mv_xor_engine_count;
  1082. static int mv_xor_probe(struct platform_device *pdev)
  1083. {
  1084. const struct mbus_dram_target_info *dram;
  1085. struct mv_xor_device *xordev;
  1086. struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1087. struct resource *res;
  1088. unsigned int max_engines, max_channels;
  1089. int i, ret;
  1090. dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
  1091. xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
  1092. if (!xordev)
  1093. return -ENOMEM;
  1094. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1095. if (!res)
  1096. return -ENODEV;
  1097. xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
  1098. resource_size(res));
  1099. if (!xordev->xor_base)
  1100. return -EBUSY;
  1101. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1102. if (!res)
  1103. return -ENODEV;
  1104. xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
  1105. resource_size(res));
  1106. if (!xordev->xor_high_base)
  1107. return -EBUSY;
  1108. platform_set_drvdata(pdev, xordev);
  1109. /*
  1110. * We need to know which type of XOR device we use before
  1111. * setting up. In non-dt case it can only be the legacy one.
  1112. */
  1113. xordev->xor_type = XOR_ORION;
  1114. if (pdev->dev.of_node)
  1115. xordev->xor_type = (uintptr_t)device_get_match_data(&pdev->dev);
  1116. /*
  1117. * (Re-)program MBUS remapping windows if we are asked to.
  1118. */
  1119. if (xordev->xor_type == XOR_ARMADA_37XX) {
  1120. mv_xor_conf_mbus_windows_a3700(xordev);
  1121. } else {
  1122. dram = mv_mbus_dram_info();
  1123. if (dram)
  1124. mv_xor_conf_mbus_windows(xordev, dram);
  1125. }
  1126. /* Not all platforms can gate the clock, so it is not
  1127. * an error if the clock does not exists.
  1128. */
  1129. xordev->clk = clk_get(&pdev->dev, NULL);
  1130. if (!IS_ERR(xordev->clk))
  1131. clk_prepare_enable(xordev->clk);
  1132. /*
  1133. * We don't want to have more than one channel per CPU in
  1134. * order for async_tx to perform well. So we limit the number
  1135. * of engines and channels so that we take into account this
  1136. * constraint. Note that we also want to use channels from
  1137. * separate engines when possible. For dual-CPU Armada 3700
  1138. * SoC with single XOR engine allow using its both channels.
  1139. */
  1140. max_engines = num_present_cpus();
  1141. if (xordev->xor_type == XOR_ARMADA_37XX)
  1142. max_channels = num_present_cpus();
  1143. else
  1144. max_channels = min_t(unsigned int,
  1145. MV_XOR_MAX_CHANNELS,
  1146. DIV_ROUND_UP(num_present_cpus(), 2));
  1147. if (mv_xor_engine_count >= max_engines)
  1148. return 0;
  1149. if (pdev->dev.of_node) {
  1150. struct device_node *np;
  1151. int i = 0;
  1152. for_each_child_of_node(pdev->dev.of_node, np) {
  1153. struct mv_xor_chan *chan;
  1154. dma_cap_mask_t cap_mask;
  1155. int irq;
  1156. if (i >= max_channels)
  1157. continue;
  1158. dma_cap_zero(cap_mask);
  1159. dma_cap_set(DMA_MEMCPY, cap_mask);
  1160. dma_cap_set(DMA_XOR, cap_mask);
  1161. dma_cap_set(DMA_INTERRUPT, cap_mask);
  1162. irq = irq_of_parse_and_map(np, 0);
  1163. if (!irq) {
  1164. ret = -ENODEV;
  1165. of_node_put(np);
  1166. goto err_channel_add;
  1167. }
  1168. chan = mv_xor_channel_add(xordev, pdev, i,
  1169. cap_mask, irq);
  1170. if (IS_ERR(chan)) {
  1171. ret = PTR_ERR(chan);
  1172. irq_dispose_mapping(irq);
  1173. of_node_put(np);
  1174. goto err_channel_add;
  1175. }
  1176. xordev->channels[i] = chan;
  1177. i++;
  1178. }
  1179. } else if (pdata && pdata->channels) {
  1180. for (i = 0; i < max_channels; i++) {
  1181. struct mv_xor_channel_data *cd;
  1182. struct mv_xor_chan *chan;
  1183. int irq;
  1184. cd = &pdata->channels[i];
  1185. irq = platform_get_irq(pdev, i);
  1186. if (irq < 0) {
  1187. ret = irq;
  1188. goto err_channel_add;
  1189. }
  1190. chan = mv_xor_channel_add(xordev, pdev, i,
  1191. cd->cap_mask, irq);
  1192. if (IS_ERR(chan)) {
  1193. ret = PTR_ERR(chan);
  1194. goto err_channel_add;
  1195. }
  1196. xordev->channels[i] = chan;
  1197. }
  1198. }
  1199. return 0;
  1200. err_channel_add:
  1201. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
  1202. if (xordev->channels[i]) {
  1203. mv_xor_channel_remove(xordev->channels[i]);
  1204. if (pdev->dev.of_node)
  1205. irq_dispose_mapping(xordev->channels[i]->irq);
  1206. }
  1207. if (!IS_ERR(xordev->clk)) {
  1208. clk_disable_unprepare(xordev->clk);
  1209. clk_put(xordev->clk);
  1210. }
  1211. return ret;
  1212. }
  1213. static struct platform_driver mv_xor_driver = {
  1214. .probe = mv_xor_probe,
  1215. .suspend = mv_xor_suspend,
  1216. .resume = mv_xor_resume,
  1217. .driver = {
  1218. .name = MV_XOR_NAME,
  1219. .of_match_table = mv_xor_dt_ids,
  1220. },
  1221. };
  1222. builtin_platform_driver(mv_xor_driver);
  1223. /*
  1224. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  1225. MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
  1226. MODULE_LICENSE("GPL");
  1227. */