mv_xor.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/memory.h>
  27. #include <linux/clk.h>
  28. #include <linux/of.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/irqdomain.h>
  31. #include <linux/platform_data/dma-mv_xor.h>
  32. #include "dmaengine.h"
  33. #include "mv_xor.h"
  34. static void mv_xor_issue_pending(struct dma_chan *chan);
  35. #define to_mv_xor_chan(chan) \
  36. container_of(chan, struct mv_xor_chan, dmachan)
  37. #define to_mv_xor_slot(tx) \
  38. container_of(tx, struct mv_xor_desc_slot, async_tx)
  39. #define mv_chan_to_devp(chan) \
  40. ((chan)->dmadev.dev)
  41. static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags)
  42. {
  43. struct mv_xor_desc *hw_desc = desc->hw_desc;
  44. hw_desc->status = (1 << 31);
  45. hw_desc->phy_next_desc = 0;
  46. hw_desc->desc_command = (1 << 31);
  47. }
  48. static u32 mv_desc_get_dest_addr(struct mv_xor_desc_slot *desc)
  49. {
  50. struct mv_xor_desc *hw_desc = desc->hw_desc;
  51. return hw_desc->phy_dest_addr;
  52. }
  53. static u32 mv_desc_get_src_addr(struct mv_xor_desc_slot *desc,
  54. int src_idx)
  55. {
  56. struct mv_xor_desc *hw_desc = desc->hw_desc;
  57. return hw_desc->phy_src_addr[src_idx];
  58. }
  59. static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc,
  60. u32 byte_count)
  61. {
  62. struct mv_xor_desc *hw_desc = desc->hw_desc;
  63. hw_desc->byte_count = byte_count;
  64. }
  65. static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
  66. u32 next_desc_addr)
  67. {
  68. struct mv_xor_desc *hw_desc = desc->hw_desc;
  69. BUG_ON(hw_desc->phy_next_desc);
  70. hw_desc->phy_next_desc = next_desc_addr;
  71. }
  72. static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc)
  73. {
  74. struct mv_xor_desc *hw_desc = desc->hw_desc;
  75. hw_desc->phy_next_desc = 0;
  76. }
  77. static void mv_desc_set_block_fill_val(struct mv_xor_desc_slot *desc, u32 val)
  78. {
  79. desc->value = val;
  80. }
  81. static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc,
  82. dma_addr_t addr)
  83. {
  84. struct mv_xor_desc *hw_desc = desc->hw_desc;
  85. hw_desc->phy_dest_addr = addr;
  86. }
  87. static int mv_chan_memset_slot_count(size_t len)
  88. {
  89. return 1;
  90. }
  91. #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
  92. static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
  93. int index, dma_addr_t addr)
  94. {
  95. struct mv_xor_desc *hw_desc = desc->hw_desc;
  96. hw_desc->phy_src_addr[index] = addr;
  97. if (desc->type == DMA_XOR)
  98. hw_desc->desc_command |= (1 << index);
  99. }
  100. static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
  101. {
  102. return __raw_readl(XOR_CURR_DESC(chan));
  103. }
  104. static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
  105. u32 next_desc_addr)
  106. {
  107. __raw_writel(next_desc_addr, XOR_NEXT_DESC(chan));
  108. }
  109. static void mv_chan_set_dest_pointer(struct mv_xor_chan *chan, u32 desc_addr)
  110. {
  111. __raw_writel(desc_addr, XOR_DEST_POINTER(chan));
  112. }
  113. static void mv_chan_set_block_size(struct mv_xor_chan *chan, u32 block_size)
  114. {
  115. __raw_writel(block_size, XOR_BLOCK_SIZE(chan));
  116. }
  117. static void mv_chan_set_value(struct mv_xor_chan *chan, u32 value)
  118. {
  119. __raw_writel(value, XOR_INIT_VALUE_LOW(chan));
  120. __raw_writel(value, XOR_INIT_VALUE_HIGH(chan));
  121. }
  122. static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
  123. {
  124. u32 val = __raw_readl(XOR_INTR_MASK(chan));
  125. val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
  126. __raw_writel(val, XOR_INTR_MASK(chan));
  127. }
  128. static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
  129. {
  130. u32 intr_cause = __raw_readl(XOR_INTR_CAUSE(chan));
  131. intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
  132. return intr_cause;
  133. }
  134. static int mv_is_err_intr(u32 intr_cause)
  135. {
  136. if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
  137. return 1;
  138. return 0;
  139. }
  140. static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
  141. {
  142. u32 val = ~(1 << (chan->idx * 16));
  143. dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
  144. __raw_writel(val, XOR_INTR_CAUSE(chan));
  145. }
  146. static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan)
  147. {
  148. u32 val = 0xFFFF0000 >> (chan->idx * 16);
  149. __raw_writel(val, XOR_INTR_CAUSE(chan));
  150. }
  151. static int mv_can_chain(struct mv_xor_desc_slot *desc)
  152. {
  153. struct mv_xor_desc_slot *chain_old_tail = list_entry(
  154. desc->chain_node.prev, struct mv_xor_desc_slot, chain_node);
  155. if (chain_old_tail->type != desc->type)
  156. return 0;
  157. if (desc->type == DMA_MEMSET)
  158. return 0;
  159. return 1;
  160. }
  161. static void mv_set_mode(struct mv_xor_chan *chan,
  162. enum dma_transaction_type type)
  163. {
  164. u32 op_mode;
  165. u32 config = __raw_readl(XOR_CONFIG(chan));
  166. switch (type) {
  167. case DMA_XOR:
  168. op_mode = XOR_OPERATION_MODE_XOR;
  169. break;
  170. case DMA_MEMCPY:
  171. op_mode = XOR_OPERATION_MODE_MEMCPY;
  172. break;
  173. case DMA_MEMSET:
  174. op_mode = XOR_OPERATION_MODE_MEMSET;
  175. break;
  176. default:
  177. dev_err(mv_chan_to_devp(chan),
  178. "error: unsupported operation %d\n",
  179. type);
  180. BUG();
  181. return;
  182. }
  183. config &= ~0x7;
  184. config |= op_mode;
  185. __raw_writel(config, XOR_CONFIG(chan));
  186. chan->current_type = type;
  187. }
  188. static void mv_chan_activate(struct mv_xor_chan *chan)
  189. {
  190. u32 activation;
  191. dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
  192. activation = __raw_readl(XOR_ACTIVATION(chan));
  193. activation |= 0x1;
  194. __raw_writel(activation, XOR_ACTIVATION(chan));
  195. }
  196. static char mv_chan_is_busy(struct mv_xor_chan *chan)
  197. {
  198. u32 state = __raw_readl(XOR_ACTIVATION(chan));
  199. state = (state >> 4) & 0x3;
  200. return (state == 1) ? 1 : 0;
  201. }
  202. static int mv_chan_xor_slot_count(size_t len, int src_cnt)
  203. {
  204. return 1;
  205. }
  206. /**
  207. * mv_xor_free_slots - flags descriptor slots for reuse
  208. * @slot: Slot to free
  209. * Caller must hold &mv_chan->lock while calling this function
  210. */
  211. static void mv_xor_free_slots(struct mv_xor_chan *mv_chan,
  212. struct mv_xor_desc_slot *slot)
  213. {
  214. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n",
  215. __func__, __LINE__, slot);
  216. slot->slots_per_op = 0;
  217. }
  218. /*
  219. * mv_xor_start_new_chain - program the engine to operate on new chain headed by
  220. * sw_desc
  221. * Caller must hold &mv_chan->lock while calling this function
  222. */
  223. static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan,
  224. struct mv_xor_desc_slot *sw_desc)
  225. {
  226. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
  227. __func__, __LINE__, sw_desc);
  228. if (sw_desc->type != mv_chan->current_type)
  229. mv_set_mode(mv_chan, sw_desc->type);
  230. if (sw_desc->type == DMA_MEMSET) {
  231. /* for memset requests we need to program the engine, no
  232. * descriptors used.
  233. */
  234. struct mv_xor_desc *hw_desc = sw_desc->hw_desc;
  235. mv_chan_set_dest_pointer(mv_chan, hw_desc->phy_dest_addr);
  236. mv_chan_set_block_size(mv_chan, sw_desc->unmap_len);
  237. mv_chan_set_value(mv_chan, sw_desc->value);
  238. } else {
  239. /* set the hardware chain */
  240. mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
  241. }
  242. mv_chan->pending += sw_desc->slot_cnt;
  243. mv_xor_issue_pending(&mv_chan->dmachan);
  244. }
  245. static dma_cookie_t
  246. mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
  247. struct mv_xor_chan *mv_chan, dma_cookie_t cookie)
  248. {
  249. BUG_ON(desc->async_tx.cookie < 0);
  250. if (desc->async_tx.cookie > 0) {
  251. cookie = desc->async_tx.cookie;
  252. /* call the callback (must not sleep or submit new
  253. * operations to this channel)
  254. */
  255. if (desc->async_tx.callback)
  256. desc->async_tx.callback(
  257. desc->async_tx.callback_param);
  258. /* unmap dma addresses
  259. * (unmap_single vs unmap_page?)
  260. */
  261. if (desc->group_head && desc->unmap_len) {
  262. struct mv_xor_desc_slot *unmap = desc->group_head;
  263. struct device *dev = mv_chan_to_devp(mv_chan);
  264. u32 len = unmap->unmap_len;
  265. enum dma_ctrl_flags flags = desc->async_tx.flags;
  266. u32 src_cnt;
  267. dma_addr_t addr;
  268. dma_addr_t dest;
  269. src_cnt = unmap->unmap_src_cnt;
  270. dest = mv_desc_get_dest_addr(unmap);
  271. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  272. enum dma_data_direction dir;
  273. if (src_cnt > 1) /* is xor ? */
  274. dir = DMA_BIDIRECTIONAL;
  275. else
  276. dir = DMA_FROM_DEVICE;
  277. dma_unmap_page(dev, dest, len, dir);
  278. }
  279. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  280. while (src_cnt--) {
  281. addr = mv_desc_get_src_addr(unmap,
  282. src_cnt);
  283. if (addr == dest)
  284. continue;
  285. dma_unmap_page(dev, addr, len,
  286. DMA_TO_DEVICE);
  287. }
  288. }
  289. desc->group_head = NULL;
  290. }
  291. }
  292. /* run dependent operations */
  293. dma_run_dependencies(&desc->async_tx);
  294. return cookie;
  295. }
  296. static int
  297. mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan)
  298. {
  299. struct mv_xor_desc_slot *iter, *_iter;
  300. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  301. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  302. completed_node) {
  303. if (async_tx_test_ack(&iter->async_tx)) {
  304. list_del(&iter->completed_node);
  305. mv_xor_free_slots(mv_chan, iter);
  306. }
  307. }
  308. return 0;
  309. }
  310. static int
  311. mv_xor_clean_slot(struct mv_xor_desc_slot *desc,
  312. struct mv_xor_chan *mv_chan)
  313. {
  314. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
  315. __func__, __LINE__, desc, desc->async_tx.flags);
  316. list_del(&desc->chain_node);
  317. /* the client is allowed to attach dependent operations
  318. * until 'ack' is set
  319. */
  320. if (!async_tx_test_ack(&desc->async_tx)) {
  321. /* move this slot to the completed_slots */
  322. list_add_tail(&desc->completed_node, &mv_chan->completed_slots);
  323. return 0;
  324. }
  325. mv_xor_free_slots(mv_chan, desc);
  326. return 0;
  327. }
  328. static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
  329. {
  330. struct mv_xor_desc_slot *iter, *_iter;
  331. dma_cookie_t cookie = 0;
  332. int busy = mv_chan_is_busy(mv_chan);
  333. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  334. int seen_current = 0;
  335. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  336. dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
  337. mv_xor_clean_completed_slots(mv_chan);
  338. /* free completed slots from the chain starting with
  339. * the oldest descriptor
  340. */
  341. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  342. chain_node) {
  343. prefetch(_iter);
  344. prefetch(&_iter->async_tx);
  345. /* do not advance past the current descriptor loaded into the
  346. * hardware channel, subsequent descriptors are either in
  347. * process or have not been submitted
  348. */
  349. if (seen_current)
  350. break;
  351. /* stop the search if we reach the current descriptor and the
  352. * channel is busy
  353. */
  354. if (iter->async_tx.phys == current_desc) {
  355. seen_current = 1;
  356. if (busy)
  357. break;
  358. }
  359. cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie);
  360. if (mv_xor_clean_slot(iter, mv_chan))
  361. break;
  362. }
  363. if ((busy == 0) && !list_empty(&mv_chan->chain)) {
  364. struct mv_xor_desc_slot *chain_head;
  365. chain_head = list_entry(mv_chan->chain.next,
  366. struct mv_xor_desc_slot,
  367. chain_node);
  368. mv_xor_start_new_chain(mv_chan, chain_head);
  369. }
  370. if (cookie > 0)
  371. mv_chan->dmachan.completed_cookie = cookie;
  372. }
  373. static void
  374. mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
  375. {
  376. spin_lock_bh(&mv_chan->lock);
  377. __mv_xor_slot_cleanup(mv_chan);
  378. spin_unlock_bh(&mv_chan->lock);
  379. }
  380. static void mv_xor_tasklet(unsigned long data)
  381. {
  382. struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
  383. mv_xor_slot_cleanup(chan);
  384. }
  385. static struct mv_xor_desc_slot *
  386. mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots,
  387. int slots_per_op)
  388. {
  389. struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL;
  390. LIST_HEAD(chain);
  391. int slots_found, retry = 0;
  392. /* start search from the last allocated descrtiptor
  393. * if a contiguous allocation can not be found start searching
  394. * from the beginning of the list
  395. */
  396. retry:
  397. slots_found = 0;
  398. if (retry == 0)
  399. iter = mv_chan->last_used;
  400. else
  401. iter = list_entry(&mv_chan->all_slots,
  402. struct mv_xor_desc_slot,
  403. slot_node);
  404. list_for_each_entry_safe_continue(
  405. iter, _iter, &mv_chan->all_slots, slot_node) {
  406. prefetch(_iter);
  407. prefetch(&_iter->async_tx);
  408. if (iter->slots_per_op) {
  409. /* give up after finding the first busy slot
  410. * on the second pass through the list
  411. */
  412. if (retry)
  413. break;
  414. slots_found = 0;
  415. continue;
  416. }
  417. /* start the allocation if the slot is correctly aligned */
  418. if (!slots_found++)
  419. alloc_start = iter;
  420. if (slots_found == num_slots) {
  421. struct mv_xor_desc_slot *alloc_tail = NULL;
  422. struct mv_xor_desc_slot *last_used = NULL;
  423. iter = alloc_start;
  424. while (num_slots) {
  425. int i;
  426. /* pre-ack all but the last descriptor */
  427. async_tx_ack(&iter->async_tx);
  428. list_add_tail(&iter->chain_node, &chain);
  429. alloc_tail = iter;
  430. iter->async_tx.cookie = 0;
  431. iter->slot_cnt = num_slots;
  432. iter->xor_check_result = NULL;
  433. for (i = 0; i < slots_per_op; i++) {
  434. iter->slots_per_op = slots_per_op - i;
  435. last_used = iter;
  436. iter = list_entry(iter->slot_node.next,
  437. struct mv_xor_desc_slot,
  438. slot_node);
  439. }
  440. num_slots -= slots_per_op;
  441. }
  442. alloc_tail->group_head = alloc_start;
  443. alloc_tail->async_tx.cookie = -EBUSY;
  444. list_splice(&chain, &alloc_tail->tx_list);
  445. mv_chan->last_used = last_used;
  446. mv_desc_clear_next_desc(alloc_start);
  447. mv_desc_clear_next_desc(alloc_tail);
  448. return alloc_tail;
  449. }
  450. }
  451. if (!retry++)
  452. goto retry;
  453. /* try to free some slots if the allocation fails */
  454. tasklet_schedule(&mv_chan->irq_tasklet);
  455. return NULL;
  456. }
  457. /************************ DMA engine API functions ****************************/
  458. static dma_cookie_t
  459. mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
  460. {
  461. struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
  462. struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
  463. struct mv_xor_desc_slot *grp_start, *old_chain_tail;
  464. dma_cookie_t cookie;
  465. int new_hw_chain = 1;
  466. dev_dbg(mv_chan_to_devp(mv_chan),
  467. "%s sw_desc %p: async_tx %p\n",
  468. __func__, sw_desc, &sw_desc->async_tx);
  469. grp_start = sw_desc->group_head;
  470. spin_lock_bh(&mv_chan->lock);
  471. cookie = dma_cookie_assign(tx);
  472. if (list_empty(&mv_chan->chain))
  473. list_splice_init(&sw_desc->tx_list, &mv_chan->chain);
  474. else {
  475. new_hw_chain = 0;
  476. old_chain_tail = list_entry(mv_chan->chain.prev,
  477. struct mv_xor_desc_slot,
  478. chain_node);
  479. list_splice_init(&grp_start->tx_list,
  480. &old_chain_tail->chain_node);
  481. if (!mv_can_chain(grp_start))
  482. goto submit_done;
  483. dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %x\n",
  484. old_chain_tail->async_tx.phys);
  485. /* fix up the hardware chain */
  486. mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
  487. /* if the channel is not busy */
  488. if (!mv_chan_is_busy(mv_chan)) {
  489. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  490. /*
  491. * and the curren desc is the end of the chain before
  492. * the append, then we need to start the channel
  493. */
  494. if (current_desc == old_chain_tail->async_tx.phys)
  495. new_hw_chain = 1;
  496. }
  497. }
  498. if (new_hw_chain)
  499. mv_xor_start_new_chain(mv_chan, grp_start);
  500. submit_done:
  501. spin_unlock_bh(&mv_chan->lock);
  502. return cookie;
  503. }
  504. /* returns the number of allocated descriptors */
  505. static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
  506. {
  507. char *hw_desc;
  508. int idx;
  509. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  510. struct mv_xor_desc_slot *slot = NULL;
  511. int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
  512. /* Allocate descriptor slots */
  513. idx = mv_chan->slots_allocated;
  514. while (idx < num_descs_in_pool) {
  515. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  516. if (!slot) {
  517. printk(KERN_INFO "MV XOR Channel only initialized"
  518. " %d descriptor slots", idx);
  519. break;
  520. }
  521. hw_desc = (char *) mv_chan->dma_desc_pool_virt;
  522. slot->hw_desc = (void *) &hw_desc[idx * MV_XOR_SLOT_SIZE];
  523. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  524. slot->async_tx.tx_submit = mv_xor_tx_submit;
  525. INIT_LIST_HEAD(&slot->chain_node);
  526. INIT_LIST_HEAD(&slot->slot_node);
  527. INIT_LIST_HEAD(&slot->tx_list);
  528. hw_desc = (char *) mv_chan->dma_desc_pool;
  529. slot->async_tx.phys =
  530. (dma_addr_t) &hw_desc[idx * MV_XOR_SLOT_SIZE];
  531. slot->idx = idx++;
  532. spin_lock_bh(&mv_chan->lock);
  533. mv_chan->slots_allocated = idx;
  534. list_add_tail(&slot->slot_node, &mv_chan->all_slots);
  535. spin_unlock_bh(&mv_chan->lock);
  536. }
  537. if (mv_chan->slots_allocated && !mv_chan->last_used)
  538. mv_chan->last_used = list_entry(mv_chan->all_slots.next,
  539. struct mv_xor_desc_slot,
  540. slot_node);
  541. dev_dbg(mv_chan_to_devp(mv_chan),
  542. "allocated %d descriptor slots last_used: %p\n",
  543. mv_chan->slots_allocated, mv_chan->last_used);
  544. return mv_chan->slots_allocated ? : -ENOMEM;
  545. }
  546. static struct dma_async_tx_descriptor *
  547. mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  548. size_t len, unsigned long flags)
  549. {
  550. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  551. struct mv_xor_desc_slot *sw_desc, *grp_start;
  552. int slot_cnt;
  553. dev_dbg(mv_chan_to_devp(mv_chan),
  554. "%s dest: %x src %x len: %u flags: %ld\n",
  555. __func__, dest, src, len, flags);
  556. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  557. return NULL;
  558. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  559. spin_lock_bh(&mv_chan->lock);
  560. slot_cnt = mv_chan_memcpy_slot_count(len);
  561. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  562. if (sw_desc) {
  563. sw_desc->type = DMA_MEMCPY;
  564. sw_desc->async_tx.flags = flags;
  565. grp_start = sw_desc->group_head;
  566. mv_desc_init(grp_start, flags);
  567. mv_desc_set_byte_count(grp_start, len);
  568. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  569. mv_desc_set_src_addr(grp_start, 0, src);
  570. sw_desc->unmap_src_cnt = 1;
  571. sw_desc->unmap_len = len;
  572. }
  573. spin_unlock_bh(&mv_chan->lock);
  574. dev_dbg(mv_chan_to_devp(mv_chan),
  575. "%s sw_desc %p async_tx %p\n",
  576. __func__, sw_desc, sw_desc ? &sw_desc->async_tx : 0);
  577. return sw_desc ? &sw_desc->async_tx : NULL;
  578. }
  579. static struct dma_async_tx_descriptor *
  580. mv_xor_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  581. size_t len, unsigned long flags)
  582. {
  583. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  584. struct mv_xor_desc_slot *sw_desc, *grp_start;
  585. int slot_cnt;
  586. dev_dbg(mv_chan_to_devp(mv_chan),
  587. "%s dest: %x len: %u flags: %ld\n",
  588. __func__, dest, len, flags);
  589. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  590. return NULL;
  591. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  592. spin_lock_bh(&mv_chan->lock);
  593. slot_cnt = mv_chan_memset_slot_count(len);
  594. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  595. if (sw_desc) {
  596. sw_desc->type = DMA_MEMSET;
  597. sw_desc->async_tx.flags = flags;
  598. grp_start = sw_desc->group_head;
  599. mv_desc_init(grp_start, flags);
  600. mv_desc_set_byte_count(grp_start, len);
  601. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  602. mv_desc_set_block_fill_val(grp_start, value);
  603. sw_desc->unmap_src_cnt = 1;
  604. sw_desc->unmap_len = len;
  605. }
  606. spin_unlock_bh(&mv_chan->lock);
  607. dev_dbg(mv_chan_to_devp(mv_chan),
  608. "%s sw_desc %p async_tx %p \n",
  609. __func__, sw_desc, &sw_desc->async_tx);
  610. return sw_desc ? &sw_desc->async_tx : NULL;
  611. }
  612. static struct dma_async_tx_descriptor *
  613. mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  614. unsigned int src_cnt, size_t len, unsigned long flags)
  615. {
  616. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  617. struct mv_xor_desc_slot *sw_desc, *grp_start;
  618. int slot_cnt;
  619. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  620. return NULL;
  621. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  622. dev_dbg(mv_chan_to_devp(mv_chan),
  623. "%s src_cnt: %d len: dest %x %u flags: %ld\n",
  624. __func__, src_cnt, len, dest, flags);
  625. spin_lock_bh(&mv_chan->lock);
  626. slot_cnt = mv_chan_xor_slot_count(len, src_cnt);
  627. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  628. if (sw_desc) {
  629. sw_desc->type = DMA_XOR;
  630. sw_desc->async_tx.flags = flags;
  631. grp_start = sw_desc->group_head;
  632. mv_desc_init(grp_start, flags);
  633. /* the byte count field is the same as in memcpy desc*/
  634. mv_desc_set_byte_count(grp_start, len);
  635. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  636. sw_desc->unmap_src_cnt = src_cnt;
  637. sw_desc->unmap_len = len;
  638. while (src_cnt--)
  639. mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]);
  640. }
  641. spin_unlock_bh(&mv_chan->lock);
  642. dev_dbg(mv_chan_to_devp(mv_chan),
  643. "%s sw_desc %p async_tx %p \n",
  644. __func__, sw_desc, &sw_desc->async_tx);
  645. return sw_desc ? &sw_desc->async_tx : NULL;
  646. }
  647. static void mv_xor_free_chan_resources(struct dma_chan *chan)
  648. {
  649. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  650. struct mv_xor_desc_slot *iter, *_iter;
  651. int in_use_descs = 0;
  652. mv_xor_slot_cleanup(mv_chan);
  653. spin_lock_bh(&mv_chan->lock);
  654. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  655. chain_node) {
  656. in_use_descs++;
  657. list_del(&iter->chain_node);
  658. }
  659. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  660. completed_node) {
  661. in_use_descs++;
  662. list_del(&iter->completed_node);
  663. }
  664. list_for_each_entry_safe_reverse(
  665. iter, _iter, &mv_chan->all_slots, slot_node) {
  666. list_del(&iter->slot_node);
  667. kfree(iter);
  668. mv_chan->slots_allocated--;
  669. }
  670. mv_chan->last_used = NULL;
  671. dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
  672. __func__, mv_chan->slots_allocated);
  673. spin_unlock_bh(&mv_chan->lock);
  674. if (in_use_descs)
  675. dev_err(mv_chan_to_devp(mv_chan),
  676. "freeing %d in use descriptors!\n", in_use_descs);
  677. }
  678. /**
  679. * mv_xor_status - poll the status of an XOR transaction
  680. * @chan: XOR channel handle
  681. * @cookie: XOR transaction identifier
  682. * @txstate: XOR transactions state holder (or NULL)
  683. */
  684. static enum dma_status mv_xor_status(struct dma_chan *chan,
  685. dma_cookie_t cookie,
  686. struct dma_tx_state *txstate)
  687. {
  688. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  689. enum dma_status ret;
  690. ret = dma_cookie_status(chan, cookie, txstate);
  691. if (ret == DMA_SUCCESS) {
  692. mv_xor_clean_completed_slots(mv_chan);
  693. return ret;
  694. }
  695. mv_xor_slot_cleanup(mv_chan);
  696. return dma_cookie_status(chan, cookie, txstate);
  697. }
  698. static void mv_dump_xor_regs(struct mv_xor_chan *chan)
  699. {
  700. u32 val;
  701. val = __raw_readl(XOR_CONFIG(chan));
  702. dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
  703. val = __raw_readl(XOR_ACTIVATION(chan));
  704. dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
  705. val = __raw_readl(XOR_INTR_CAUSE(chan));
  706. dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
  707. val = __raw_readl(XOR_INTR_MASK(chan));
  708. dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
  709. val = __raw_readl(XOR_ERROR_CAUSE(chan));
  710. dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
  711. val = __raw_readl(XOR_ERROR_ADDR(chan));
  712. dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
  713. }
  714. static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan,
  715. u32 intr_cause)
  716. {
  717. if (intr_cause & (1 << 4)) {
  718. dev_dbg(mv_chan_to_devp(chan),
  719. "ignore this error\n");
  720. return;
  721. }
  722. dev_err(mv_chan_to_devp(chan),
  723. "error on chan %d. intr cause 0x%08x\n",
  724. chan->idx, intr_cause);
  725. mv_dump_xor_regs(chan);
  726. BUG();
  727. }
  728. static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
  729. {
  730. struct mv_xor_chan *chan = data;
  731. u32 intr_cause = mv_chan_get_intr_cause(chan);
  732. dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
  733. if (mv_is_err_intr(intr_cause))
  734. mv_xor_err_interrupt_handler(chan, intr_cause);
  735. tasklet_schedule(&chan->irq_tasklet);
  736. mv_xor_device_clear_eoc_cause(chan);
  737. return IRQ_HANDLED;
  738. }
  739. static void mv_xor_issue_pending(struct dma_chan *chan)
  740. {
  741. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  742. if (mv_chan->pending >= MV_XOR_THRESHOLD) {
  743. mv_chan->pending = 0;
  744. mv_chan_activate(mv_chan);
  745. }
  746. }
  747. /*
  748. * Perform a transaction to verify the HW works.
  749. */
  750. #define MV_XOR_TEST_SIZE 2000
  751. static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan)
  752. {
  753. int i;
  754. void *src, *dest;
  755. dma_addr_t src_dma, dest_dma;
  756. struct dma_chan *dma_chan;
  757. dma_cookie_t cookie;
  758. struct dma_async_tx_descriptor *tx;
  759. int err = 0;
  760. src = kmalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
  761. if (!src)
  762. return -ENOMEM;
  763. dest = kzalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
  764. if (!dest) {
  765. kfree(src);
  766. return -ENOMEM;
  767. }
  768. /* Fill in src buffer */
  769. for (i = 0; i < MV_XOR_TEST_SIZE; i++)
  770. ((u8 *) src)[i] = (u8)i;
  771. dma_chan = &mv_chan->dmachan;
  772. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  773. err = -ENODEV;
  774. goto out;
  775. }
  776. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  777. MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
  778. src_dma = dma_map_single(dma_chan->device->dev, src,
  779. MV_XOR_TEST_SIZE, DMA_TO_DEVICE);
  780. tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  781. MV_XOR_TEST_SIZE, 0);
  782. cookie = mv_xor_tx_submit(tx);
  783. mv_xor_issue_pending(dma_chan);
  784. async_tx_ack(tx);
  785. msleep(1);
  786. if (mv_xor_status(dma_chan, cookie, NULL) !=
  787. DMA_SUCCESS) {
  788. dev_err(dma_chan->device->dev,
  789. "Self-test copy timed out, disabling\n");
  790. err = -ENODEV;
  791. goto free_resources;
  792. }
  793. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  794. MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
  795. if (memcmp(src, dest, MV_XOR_TEST_SIZE)) {
  796. dev_err(dma_chan->device->dev,
  797. "Self-test copy failed compare, disabling\n");
  798. err = -ENODEV;
  799. goto free_resources;
  800. }
  801. free_resources:
  802. mv_xor_free_chan_resources(dma_chan);
  803. out:
  804. kfree(src);
  805. kfree(dest);
  806. return err;
  807. }
  808. #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
  809. static int
  810. mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
  811. {
  812. int i, src_idx;
  813. struct page *dest;
  814. struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
  815. dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
  816. dma_addr_t dest_dma;
  817. struct dma_async_tx_descriptor *tx;
  818. struct dma_chan *dma_chan;
  819. dma_cookie_t cookie;
  820. u8 cmp_byte = 0;
  821. u32 cmp_word;
  822. int err = 0;
  823. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
  824. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  825. if (!xor_srcs[src_idx]) {
  826. while (src_idx--)
  827. __free_page(xor_srcs[src_idx]);
  828. return -ENOMEM;
  829. }
  830. }
  831. dest = alloc_page(GFP_KERNEL);
  832. if (!dest) {
  833. while (src_idx--)
  834. __free_page(xor_srcs[src_idx]);
  835. return -ENOMEM;
  836. }
  837. /* Fill in src buffers */
  838. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
  839. u8 *ptr = page_address(xor_srcs[src_idx]);
  840. for (i = 0; i < PAGE_SIZE; i++)
  841. ptr[i] = (1 << src_idx);
  842. }
  843. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++)
  844. cmp_byte ^= (u8) (1 << src_idx);
  845. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  846. (cmp_byte << 8) | cmp_byte;
  847. memset(page_address(dest), 0, PAGE_SIZE);
  848. dma_chan = &mv_chan->dmachan;
  849. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  850. err = -ENODEV;
  851. goto out;
  852. }
  853. /* test xor */
  854. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
  855. DMA_FROM_DEVICE);
  856. for (i = 0; i < MV_XOR_NUM_SRC_TEST; i++)
  857. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  858. 0, PAGE_SIZE, DMA_TO_DEVICE);
  859. tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  860. MV_XOR_NUM_SRC_TEST, PAGE_SIZE, 0);
  861. cookie = mv_xor_tx_submit(tx);
  862. mv_xor_issue_pending(dma_chan);
  863. async_tx_ack(tx);
  864. msleep(8);
  865. if (mv_xor_status(dma_chan, cookie, NULL) !=
  866. DMA_SUCCESS) {
  867. dev_err(dma_chan->device->dev,
  868. "Self-test xor timed out, disabling\n");
  869. err = -ENODEV;
  870. goto free_resources;
  871. }
  872. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  873. PAGE_SIZE, DMA_FROM_DEVICE);
  874. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  875. u32 *ptr = page_address(dest);
  876. if (ptr[i] != cmp_word) {
  877. dev_err(dma_chan->device->dev,
  878. "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
  879. i, ptr[i], cmp_word);
  880. err = -ENODEV;
  881. goto free_resources;
  882. }
  883. }
  884. free_resources:
  885. mv_xor_free_chan_resources(dma_chan);
  886. out:
  887. src_idx = MV_XOR_NUM_SRC_TEST;
  888. while (src_idx--)
  889. __free_page(xor_srcs[src_idx]);
  890. __free_page(dest);
  891. return err;
  892. }
  893. /* This driver does not implement any of the optional DMA operations. */
  894. static int
  895. mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  896. unsigned long arg)
  897. {
  898. return -ENOSYS;
  899. }
  900. static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
  901. {
  902. struct dma_chan *chan, *_chan;
  903. struct device *dev = mv_chan->dmadev.dev;
  904. dma_async_device_unregister(&mv_chan->dmadev);
  905. dma_free_coherent(dev, MV_XOR_POOL_SIZE,
  906. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  907. list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
  908. device_node) {
  909. list_del(&chan->device_node);
  910. }
  911. free_irq(mv_chan->irq, mv_chan);
  912. return 0;
  913. }
  914. static struct mv_xor_chan *
  915. mv_xor_channel_add(struct mv_xor_device *xordev,
  916. struct platform_device *pdev,
  917. int idx, dma_cap_mask_t cap_mask, int irq)
  918. {
  919. int ret = 0;
  920. struct mv_xor_chan *mv_chan;
  921. struct dma_device *dma_dev;
  922. mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
  923. if (!mv_chan) {
  924. ret = -ENOMEM;
  925. goto err_free_dma;
  926. }
  927. mv_chan->idx = idx;
  928. mv_chan->irq = irq;
  929. dma_dev = &mv_chan->dmadev;
  930. /* allocate coherent memory for hardware descriptors
  931. * note: writecombine gives slightly better performance, but
  932. * requires that we explicitly flush the writes
  933. */
  934. mv_chan->dma_desc_pool_virt =
  935. dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
  936. &mv_chan->dma_desc_pool, GFP_KERNEL);
  937. if (!mv_chan->dma_desc_pool_virt)
  938. return ERR_PTR(-ENOMEM);
  939. /* discover transaction capabilites from the platform data */
  940. dma_dev->cap_mask = cap_mask;
  941. INIT_LIST_HEAD(&dma_dev->channels);
  942. /* set base routines */
  943. dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
  944. dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
  945. dma_dev->device_tx_status = mv_xor_status;
  946. dma_dev->device_issue_pending = mv_xor_issue_pending;
  947. dma_dev->device_control = mv_xor_control;
  948. dma_dev->dev = &pdev->dev;
  949. /* set prep routines based on capability */
  950. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  951. dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
  952. if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
  953. dma_dev->device_prep_dma_memset = mv_xor_prep_dma_memset;
  954. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  955. dma_dev->max_xor = 8;
  956. dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
  957. }
  958. mv_chan->mmr_base = xordev->xor_base;
  959. if (!mv_chan->mmr_base) {
  960. ret = -ENOMEM;
  961. goto err_free_dma;
  962. }
  963. tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
  964. mv_chan);
  965. /* clear errors before enabling interrupts */
  966. mv_xor_device_clear_err_status(mv_chan);
  967. ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
  968. 0, dev_name(&pdev->dev), mv_chan);
  969. if (ret)
  970. goto err_free_dma;
  971. mv_chan_unmask_interrupts(mv_chan);
  972. mv_set_mode(mv_chan, DMA_MEMCPY);
  973. spin_lock_init(&mv_chan->lock);
  974. INIT_LIST_HEAD(&mv_chan->chain);
  975. INIT_LIST_HEAD(&mv_chan->completed_slots);
  976. INIT_LIST_HEAD(&mv_chan->all_slots);
  977. mv_chan->dmachan.device = dma_dev;
  978. dma_cookie_init(&mv_chan->dmachan);
  979. list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
  980. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  981. ret = mv_xor_memcpy_self_test(mv_chan);
  982. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  983. if (ret)
  984. goto err_free_irq;
  985. }
  986. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  987. ret = mv_xor_xor_self_test(mv_chan);
  988. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  989. if (ret)
  990. goto err_free_irq;
  991. }
  992. dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s%s)\n",
  993. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  994. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
  995. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  996. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  997. dma_async_device_register(dma_dev);
  998. return mv_chan;
  999. err_free_irq:
  1000. free_irq(mv_chan->irq, mv_chan);
  1001. err_free_dma:
  1002. dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
  1003. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  1004. return ERR_PTR(ret);
  1005. }
  1006. static void
  1007. mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
  1008. const struct mbus_dram_target_info *dram)
  1009. {
  1010. void __iomem *base = xordev->xor_base;
  1011. u32 win_enable = 0;
  1012. int i;
  1013. for (i = 0; i < 8; i++) {
  1014. writel(0, base + WINDOW_BASE(i));
  1015. writel(0, base + WINDOW_SIZE(i));
  1016. if (i < 4)
  1017. writel(0, base + WINDOW_REMAP_HIGH(i));
  1018. }
  1019. for (i = 0; i < dram->num_cs; i++) {
  1020. const struct mbus_dram_window *cs = dram->cs + i;
  1021. writel((cs->base & 0xffff0000) |
  1022. (cs->mbus_attr << 8) |
  1023. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  1024. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  1025. win_enable |= (1 << i);
  1026. win_enable |= 3 << (16 + (2 * i));
  1027. }
  1028. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  1029. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  1030. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  1031. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  1032. }
  1033. static int mv_xor_probe(struct platform_device *pdev)
  1034. {
  1035. const struct mbus_dram_target_info *dram;
  1036. struct mv_xor_device *xordev;
  1037. struct mv_xor_platform_data *pdata = pdev->dev.platform_data;
  1038. struct resource *res;
  1039. int i, ret;
  1040. dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
  1041. xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
  1042. if (!xordev)
  1043. return -ENOMEM;
  1044. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1045. if (!res)
  1046. return -ENODEV;
  1047. xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
  1048. resource_size(res));
  1049. if (!xordev->xor_base)
  1050. return -EBUSY;
  1051. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1052. if (!res)
  1053. return -ENODEV;
  1054. xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
  1055. resource_size(res));
  1056. if (!xordev->xor_high_base)
  1057. return -EBUSY;
  1058. platform_set_drvdata(pdev, xordev);
  1059. /*
  1060. * (Re-)program MBUS remapping windows if we are asked to.
  1061. */
  1062. dram = mv_mbus_dram_info();
  1063. if (dram)
  1064. mv_xor_conf_mbus_windows(xordev, dram);
  1065. /* Not all platforms can gate the clock, so it is not
  1066. * an error if the clock does not exists.
  1067. */
  1068. xordev->clk = clk_get(&pdev->dev, NULL);
  1069. if (!IS_ERR(xordev->clk))
  1070. clk_prepare_enable(xordev->clk);
  1071. if (pdev->dev.of_node) {
  1072. struct device_node *np;
  1073. int i = 0;
  1074. for_each_child_of_node(pdev->dev.of_node, np) {
  1075. dma_cap_mask_t cap_mask;
  1076. int irq;
  1077. dma_cap_zero(cap_mask);
  1078. if (of_property_read_bool(np, "dmacap,memcpy"))
  1079. dma_cap_set(DMA_MEMCPY, cap_mask);
  1080. if (of_property_read_bool(np, "dmacap,xor"))
  1081. dma_cap_set(DMA_XOR, cap_mask);
  1082. if (of_property_read_bool(np, "dmacap,memset"))
  1083. dma_cap_set(DMA_MEMSET, cap_mask);
  1084. if (of_property_read_bool(np, "dmacap,interrupt"))
  1085. dma_cap_set(DMA_INTERRUPT, cap_mask);
  1086. irq = irq_of_parse_and_map(np, 0);
  1087. if (!irq) {
  1088. ret = -ENODEV;
  1089. goto err_channel_add;
  1090. }
  1091. xordev->channels[i] =
  1092. mv_xor_channel_add(xordev, pdev, i,
  1093. cap_mask, irq);
  1094. if (IS_ERR(xordev->channels[i])) {
  1095. ret = PTR_ERR(xordev->channels[i]);
  1096. xordev->channels[i] = NULL;
  1097. irq_dispose_mapping(irq);
  1098. goto err_channel_add;
  1099. }
  1100. i++;
  1101. }
  1102. } else if (pdata && pdata->channels) {
  1103. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1104. struct mv_xor_channel_data *cd;
  1105. int irq;
  1106. cd = &pdata->channels[i];
  1107. if (!cd) {
  1108. ret = -ENODEV;
  1109. goto err_channel_add;
  1110. }
  1111. irq = platform_get_irq(pdev, i);
  1112. if (irq < 0) {
  1113. ret = irq;
  1114. goto err_channel_add;
  1115. }
  1116. xordev->channels[i] =
  1117. mv_xor_channel_add(xordev, pdev, i,
  1118. cd->cap_mask, irq);
  1119. if (IS_ERR(xordev->channels[i])) {
  1120. ret = PTR_ERR(xordev->channels[i]);
  1121. goto err_channel_add;
  1122. }
  1123. }
  1124. }
  1125. return 0;
  1126. err_channel_add:
  1127. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
  1128. if (xordev->channels[i]) {
  1129. mv_xor_channel_remove(xordev->channels[i]);
  1130. if (pdev->dev.of_node)
  1131. irq_dispose_mapping(xordev->channels[i]->irq);
  1132. }
  1133. if (!IS_ERR(xordev->clk)) {
  1134. clk_disable_unprepare(xordev->clk);
  1135. clk_put(xordev->clk);
  1136. }
  1137. return ret;
  1138. }
  1139. static int mv_xor_remove(struct platform_device *pdev)
  1140. {
  1141. struct mv_xor_device *xordev = platform_get_drvdata(pdev);
  1142. int i;
  1143. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1144. if (xordev->channels[i])
  1145. mv_xor_channel_remove(xordev->channels[i]);
  1146. }
  1147. if (!IS_ERR(xordev->clk)) {
  1148. clk_disable_unprepare(xordev->clk);
  1149. clk_put(xordev->clk);
  1150. }
  1151. return 0;
  1152. }
  1153. #ifdef CONFIG_OF
  1154. static struct of_device_id mv_xor_dt_ids[] = {
  1155. { .compatible = "marvell,orion-xor", },
  1156. {},
  1157. };
  1158. MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
  1159. #endif
  1160. static struct platform_driver mv_xor_driver = {
  1161. .probe = mv_xor_probe,
  1162. .remove = mv_xor_remove,
  1163. .driver = {
  1164. .owner = THIS_MODULE,
  1165. .name = MV_XOR_NAME,
  1166. .of_match_table = of_match_ptr(mv_xor_dt_ids),
  1167. },
  1168. };
  1169. static int __init mv_xor_init(void)
  1170. {
  1171. return platform_driver_register(&mv_xor_driver);
  1172. }
  1173. module_init(mv_xor_init);
  1174. /* it's currently unsafe to unload this module */
  1175. #if 0
  1176. static void __exit mv_xor_exit(void)
  1177. {
  1178. platform_driver_unregister(&mv_xor_driver);
  1179. return;
  1180. }
  1181. module_exit(mv_xor_exit);
  1182. #endif
  1183. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  1184. MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
  1185. MODULE_LICENSE("GPL");