mv_xor.c 42 KB

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