mv_xor.c 33 KB

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