mv_xor.c 35 KB

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