mv_xor.c 35 KB

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