mv_xor.c 37 KB

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