mv_xor.c 33 KB

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