iop-adma.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /*
  2. * offload engine driver for the Intel Xscale series of i/o processors
  3. * Copyright © 2006, Intel Corporation.
  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. */
  19. /*
  20. * This driver supports the asynchrounous DMA copy and RAID engines available
  21. * on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/async_tx.h>
  26. #include <linux/delay.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/memory.h>
  32. #include <linux/ioport.h>
  33. #include <asm/arch/adma.h>
  34. #define to_iop_adma_chan(chan) container_of(chan, struct iop_adma_chan, common)
  35. #define to_iop_adma_device(dev) \
  36. container_of(dev, struct iop_adma_device, common)
  37. #define tx_to_iop_adma_slot(tx) \
  38. container_of(tx, struct iop_adma_desc_slot, async_tx)
  39. /**
  40. * iop_adma_free_slots - flags descriptor slots for reuse
  41. * @slot: Slot to free
  42. * Caller must hold &iop_chan->lock while calling this function
  43. */
  44. static void iop_adma_free_slots(struct iop_adma_desc_slot *slot)
  45. {
  46. int stride = slot->slots_per_op;
  47. while (stride--) {
  48. slot->slots_per_op = 0;
  49. slot = list_entry(slot->slot_node.next,
  50. struct iop_adma_desc_slot,
  51. slot_node);
  52. }
  53. }
  54. static dma_cookie_t
  55. iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
  56. struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
  57. {
  58. BUG_ON(desc->async_tx.cookie < 0);
  59. if (desc->async_tx.cookie > 0) {
  60. cookie = desc->async_tx.cookie;
  61. desc->async_tx.cookie = 0;
  62. /* call the callback (must not sleep or submit new
  63. * operations to this channel)
  64. */
  65. if (desc->async_tx.callback)
  66. desc->async_tx.callback(
  67. desc->async_tx.callback_param);
  68. /* unmap dma addresses
  69. * (unmap_single vs unmap_page?)
  70. */
  71. if (desc->group_head && desc->unmap_len) {
  72. struct iop_adma_desc_slot *unmap = desc->group_head;
  73. struct device *dev =
  74. &iop_chan->device->pdev->dev;
  75. u32 len = unmap->unmap_len;
  76. u32 src_cnt = unmap->unmap_src_cnt;
  77. dma_addr_t addr = iop_desc_get_dest_addr(unmap,
  78. iop_chan);
  79. dma_unmap_page(dev, addr, len, DMA_FROM_DEVICE);
  80. while (src_cnt--) {
  81. addr = iop_desc_get_src_addr(unmap,
  82. iop_chan,
  83. src_cnt);
  84. dma_unmap_page(dev, addr, len,
  85. DMA_TO_DEVICE);
  86. }
  87. desc->group_head = NULL;
  88. }
  89. }
  90. /* run dependent operations */
  91. async_tx_run_dependencies(&desc->async_tx);
  92. return cookie;
  93. }
  94. static int
  95. iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
  96. struct iop_adma_chan *iop_chan)
  97. {
  98. /* the client is allowed to attach dependent operations
  99. * until 'ack' is set
  100. */
  101. if (!async_tx_test_ack(&desc->async_tx))
  102. return 0;
  103. /* leave the last descriptor in the chain
  104. * so we can append to it
  105. */
  106. if (desc->chain_node.next == &iop_chan->chain)
  107. return 1;
  108. dev_dbg(iop_chan->device->common.dev,
  109. "\tfree slot: %d slots_per_op: %d\n",
  110. desc->idx, desc->slots_per_op);
  111. list_del(&desc->chain_node);
  112. iop_adma_free_slots(desc);
  113. return 0;
  114. }
  115. static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
  116. {
  117. struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
  118. dma_cookie_t cookie = 0;
  119. u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
  120. int busy = iop_chan_is_busy(iop_chan);
  121. int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
  122. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  123. /* free completed slots from the chain starting with
  124. * the oldest descriptor
  125. */
  126. list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
  127. chain_node) {
  128. pr_debug("\tcookie: %d slot: %d busy: %d "
  129. "this_desc: %#x next_desc: %#x ack: %d\n",
  130. iter->async_tx.cookie, iter->idx, busy,
  131. iter->async_tx.phys, iop_desc_get_next_desc(iter),
  132. async_tx_test_ack(&iter->async_tx));
  133. prefetch(_iter);
  134. prefetch(&_iter->async_tx);
  135. /* do not advance past the current descriptor loaded into the
  136. * hardware channel, subsequent descriptors are either in
  137. * process or have not been submitted
  138. */
  139. if (seen_current)
  140. break;
  141. /* stop the search if we reach the current descriptor and the
  142. * channel is busy, or if it appears that the current descriptor
  143. * needs to be re-read (i.e. has been appended to)
  144. */
  145. if (iter->async_tx.phys == current_desc) {
  146. BUG_ON(seen_current++);
  147. if (busy || iop_desc_get_next_desc(iter))
  148. break;
  149. }
  150. /* detect the start of a group transaction */
  151. if (!slot_cnt && !slots_per_op) {
  152. slot_cnt = iter->slot_cnt;
  153. slots_per_op = iter->slots_per_op;
  154. if (slot_cnt <= slots_per_op) {
  155. slot_cnt = 0;
  156. slots_per_op = 0;
  157. }
  158. }
  159. if (slot_cnt) {
  160. pr_debug("\tgroup++\n");
  161. if (!grp_start)
  162. grp_start = iter;
  163. slot_cnt -= slots_per_op;
  164. }
  165. /* all the members of a group are complete */
  166. if (slots_per_op != 0 && slot_cnt == 0) {
  167. struct iop_adma_desc_slot *grp_iter, *_grp_iter;
  168. int end_of_chain = 0;
  169. pr_debug("\tgroup end\n");
  170. /* collect the total results */
  171. if (grp_start->xor_check_result) {
  172. u32 zero_sum_result = 0;
  173. slot_cnt = grp_start->slot_cnt;
  174. grp_iter = grp_start;
  175. list_for_each_entry_from(grp_iter,
  176. &iop_chan->chain, chain_node) {
  177. zero_sum_result |=
  178. iop_desc_get_zero_result(grp_iter);
  179. pr_debug("\titer%d result: %d\n",
  180. grp_iter->idx, zero_sum_result);
  181. slot_cnt -= slots_per_op;
  182. if (slot_cnt == 0)
  183. break;
  184. }
  185. pr_debug("\tgrp_start->xor_check_result: %p\n",
  186. grp_start->xor_check_result);
  187. *grp_start->xor_check_result = zero_sum_result;
  188. }
  189. /* clean up the group */
  190. slot_cnt = grp_start->slot_cnt;
  191. grp_iter = grp_start;
  192. list_for_each_entry_safe_from(grp_iter, _grp_iter,
  193. &iop_chan->chain, chain_node) {
  194. cookie = iop_adma_run_tx_complete_actions(
  195. grp_iter, iop_chan, cookie);
  196. slot_cnt -= slots_per_op;
  197. end_of_chain = iop_adma_clean_slot(grp_iter,
  198. iop_chan);
  199. if (slot_cnt == 0 || end_of_chain)
  200. break;
  201. }
  202. /* the group should be complete at this point */
  203. BUG_ON(slot_cnt);
  204. slots_per_op = 0;
  205. grp_start = NULL;
  206. if (end_of_chain)
  207. break;
  208. else
  209. continue;
  210. } else if (slots_per_op) /* wait for group completion */
  211. continue;
  212. /* write back zero sum results (single descriptor case) */
  213. if (iter->xor_check_result && iter->async_tx.cookie)
  214. *iter->xor_check_result =
  215. iop_desc_get_zero_result(iter);
  216. cookie = iop_adma_run_tx_complete_actions(
  217. iter, iop_chan, cookie);
  218. if (iop_adma_clean_slot(iter, iop_chan))
  219. break;
  220. }
  221. BUG_ON(!seen_current);
  222. if (cookie > 0) {
  223. iop_chan->completed_cookie = cookie;
  224. pr_debug("\tcompleted cookie %d\n", cookie);
  225. }
  226. }
  227. static void
  228. iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
  229. {
  230. spin_lock_bh(&iop_chan->lock);
  231. __iop_adma_slot_cleanup(iop_chan);
  232. spin_unlock_bh(&iop_chan->lock);
  233. }
  234. static void iop_adma_tasklet(unsigned long data)
  235. {
  236. struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
  237. spin_lock(&iop_chan->lock);
  238. __iop_adma_slot_cleanup(iop_chan);
  239. spin_unlock(&iop_chan->lock);
  240. }
  241. static struct iop_adma_desc_slot *
  242. iop_adma_alloc_slots(struct iop_adma_chan *iop_chan, int num_slots,
  243. int slots_per_op)
  244. {
  245. struct iop_adma_desc_slot *iter, *_iter, *alloc_start = NULL;
  246. LIST_HEAD(chain);
  247. int slots_found, retry = 0;
  248. /* start search from the last allocated descrtiptor
  249. * if a contiguous allocation can not be found start searching
  250. * from the beginning of the list
  251. */
  252. retry:
  253. slots_found = 0;
  254. if (retry == 0)
  255. iter = iop_chan->last_used;
  256. else
  257. iter = list_entry(&iop_chan->all_slots,
  258. struct iop_adma_desc_slot,
  259. slot_node);
  260. list_for_each_entry_safe_continue(
  261. iter, _iter, &iop_chan->all_slots, slot_node) {
  262. prefetch(_iter);
  263. prefetch(&_iter->async_tx);
  264. if (iter->slots_per_op) {
  265. /* give up after finding the first busy slot
  266. * on the second pass through the list
  267. */
  268. if (retry)
  269. break;
  270. slots_found = 0;
  271. continue;
  272. }
  273. /* start the allocation if the slot is correctly aligned */
  274. if (!slots_found++) {
  275. if (iop_desc_is_aligned(iter, slots_per_op))
  276. alloc_start = iter;
  277. else {
  278. slots_found = 0;
  279. continue;
  280. }
  281. }
  282. if (slots_found == num_slots) {
  283. struct iop_adma_desc_slot *alloc_tail = NULL;
  284. struct iop_adma_desc_slot *last_used = NULL;
  285. iter = alloc_start;
  286. while (num_slots) {
  287. int i;
  288. dev_dbg(iop_chan->device->common.dev,
  289. "allocated slot: %d "
  290. "(desc %p phys: %#x) slots_per_op %d\n",
  291. iter->idx, iter->hw_desc,
  292. iter->async_tx.phys, slots_per_op);
  293. /* pre-ack all but the last descriptor */
  294. if (num_slots != slots_per_op)
  295. async_tx_ack(&iter->async_tx);
  296. list_add_tail(&iter->chain_node, &chain);
  297. alloc_tail = iter;
  298. iter->async_tx.cookie = 0;
  299. iter->slot_cnt = num_slots;
  300. iter->xor_check_result = NULL;
  301. for (i = 0; i < slots_per_op; i++) {
  302. iter->slots_per_op = slots_per_op - i;
  303. last_used = iter;
  304. iter = list_entry(iter->slot_node.next,
  305. struct iop_adma_desc_slot,
  306. slot_node);
  307. }
  308. num_slots -= slots_per_op;
  309. }
  310. alloc_tail->group_head = alloc_start;
  311. alloc_tail->async_tx.cookie = -EBUSY;
  312. list_splice(&chain, &alloc_tail->async_tx.tx_list);
  313. iop_chan->last_used = last_used;
  314. iop_desc_clear_next_desc(alloc_start);
  315. iop_desc_clear_next_desc(alloc_tail);
  316. return alloc_tail;
  317. }
  318. }
  319. if (!retry++)
  320. goto retry;
  321. /* try to free some slots if the allocation fails */
  322. tasklet_schedule(&iop_chan->irq_tasklet);
  323. return NULL;
  324. }
  325. static dma_cookie_t
  326. iop_desc_assign_cookie(struct iop_adma_chan *iop_chan,
  327. struct iop_adma_desc_slot *desc)
  328. {
  329. dma_cookie_t cookie = iop_chan->common.cookie;
  330. cookie++;
  331. if (cookie < 0)
  332. cookie = 1;
  333. iop_chan->common.cookie = desc->async_tx.cookie = cookie;
  334. return cookie;
  335. }
  336. static void iop_adma_check_threshold(struct iop_adma_chan *iop_chan)
  337. {
  338. dev_dbg(iop_chan->device->common.dev, "pending: %d\n",
  339. iop_chan->pending);
  340. if (iop_chan->pending >= IOP_ADMA_THRESHOLD) {
  341. iop_chan->pending = 0;
  342. iop_chan_append(iop_chan);
  343. }
  344. }
  345. static dma_cookie_t
  346. iop_adma_tx_submit(struct dma_async_tx_descriptor *tx)
  347. {
  348. struct iop_adma_desc_slot *sw_desc = tx_to_iop_adma_slot(tx);
  349. struct iop_adma_chan *iop_chan = to_iop_adma_chan(tx->chan);
  350. struct iop_adma_desc_slot *grp_start, *old_chain_tail;
  351. int slot_cnt;
  352. int slots_per_op;
  353. dma_cookie_t cookie;
  354. grp_start = sw_desc->group_head;
  355. slot_cnt = grp_start->slot_cnt;
  356. slots_per_op = grp_start->slots_per_op;
  357. spin_lock_bh(&iop_chan->lock);
  358. cookie = iop_desc_assign_cookie(iop_chan, sw_desc);
  359. old_chain_tail = list_entry(iop_chan->chain.prev,
  360. struct iop_adma_desc_slot, chain_node);
  361. list_splice_init(&sw_desc->async_tx.tx_list,
  362. &old_chain_tail->chain_node);
  363. /* fix up the hardware chain */
  364. iop_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
  365. /* 1/ don't add pre-chained descriptors
  366. * 2/ dummy read to flush next_desc write
  367. */
  368. BUG_ON(iop_desc_get_next_desc(sw_desc));
  369. /* increment the pending count by the number of slots
  370. * memcpy operations have a 1:1 (slot:operation) relation
  371. * other operations are heavier and will pop the threshold
  372. * more often.
  373. */
  374. iop_chan->pending += slot_cnt;
  375. iop_adma_check_threshold(iop_chan);
  376. spin_unlock_bh(&iop_chan->lock);
  377. dev_dbg(iop_chan->device->common.dev, "%s cookie: %d slot: %d\n",
  378. __func__, sw_desc->async_tx.cookie, sw_desc->idx);
  379. return cookie;
  380. }
  381. static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan);
  382. static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan);
  383. /* returns the number of allocated descriptors */
  384. static int iop_adma_alloc_chan_resources(struct dma_chan *chan)
  385. {
  386. char *hw_desc;
  387. int idx;
  388. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  389. struct iop_adma_desc_slot *slot = NULL;
  390. int init = iop_chan->slots_allocated ? 0 : 1;
  391. struct iop_adma_platform_data *plat_data =
  392. iop_chan->device->pdev->dev.platform_data;
  393. int num_descs_in_pool = plat_data->pool_size/IOP_ADMA_SLOT_SIZE;
  394. /* Allocate descriptor slots */
  395. do {
  396. idx = iop_chan->slots_allocated;
  397. if (idx == num_descs_in_pool)
  398. break;
  399. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  400. if (!slot) {
  401. printk(KERN_INFO "IOP ADMA Channel only initialized"
  402. " %d descriptor slots", idx);
  403. break;
  404. }
  405. hw_desc = (char *) iop_chan->device->dma_desc_pool_virt;
  406. slot->hw_desc = (void *) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
  407. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  408. slot->async_tx.tx_submit = iop_adma_tx_submit;
  409. INIT_LIST_HEAD(&slot->chain_node);
  410. INIT_LIST_HEAD(&slot->slot_node);
  411. INIT_LIST_HEAD(&slot->async_tx.tx_list);
  412. hw_desc = (char *) iop_chan->device->dma_desc_pool;
  413. slot->async_tx.phys =
  414. (dma_addr_t) &hw_desc[idx * IOP_ADMA_SLOT_SIZE];
  415. slot->idx = idx;
  416. spin_lock_bh(&iop_chan->lock);
  417. iop_chan->slots_allocated++;
  418. list_add_tail(&slot->slot_node, &iop_chan->all_slots);
  419. spin_unlock_bh(&iop_chan->lock);
  420. } while (iop_chan->slots_allocated < num_descs_in_pool);
  421. if (idx && !iop_chan->last_used)
  422. iop_chan->last_used = list_entry(iop_chan->all_slots.next,
  423. struct iop_adma_desc_slot,
  424. slot_node);
  425. dev_dbg(iop_chan->device->common.dev,
  426. "allocated %d descriptor slots last_used: %p\n",
  427. iop_chan->slots_allocated, iop_chan->last_used);
  428. /* initialize the channel and the chain with a null operation */
  429. if (init) {
  430. if (dma_has_cap(DMA_MEMCPY,
  431. iop_chan->device->common.cap_mask))
  432. iop_chan_start_null_memcpy(iop_chan);
  433. else if (dma_has_cap(DMA_XOR,
  434. iop_chan->device->common.cap_mask))
  435. iop_chan_start_null_xor(iop_chan);
  436. else
  437. BUG();
  438. }
  439. return (idx > 0) ? idx : -ENOMEM;
  440. }
  441. static struct dma_async_tx_descriptor *
  442. iop_adma_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
  443. {
  444. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  445. struct iop_adma_desc_slot *sw_desc, *grp_start;
  446. int slot_cnt, slots_per_op;
  447. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  448. spin_lock_bh(&iop_chan->lock);
  449. slot_cnt = iop_chan_interrupt_slot_count(&slots_per_op, iop_chan);
  450. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  451. if (sw_desc) {
  452. grp_start = sw_desc->group_head;
  453. iop_desc_init_interrupt(grp_start, iop_chan);
  454. grp_start->unmap_len = 0;
  455. sw_desc->async_tx.flags = flags;
  456. }
  457. spin_unlock_bh(&iop_chan->lock);
  458. return sw_desc ? &sw_desc->async_tx : NULL;
  459. }
  460. static struct dma_async_tx_descriptor *
  461. iop_adma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  462. dma_addr_t dma_src, size_t len, unsigned long flags)
  463. {
  464. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  465. struct iop_adma_desc_slot *sw_desc, *grp_start;
  466. int slot_cnt, slots_per_op;
  467. if (unlikely(!len))
  468. return NULL;
  469. BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
  470. dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
  471. __func__, len);
  472. spin_lock_bh(&iop_chan->lock);
  473. slot_cnt = iop_chan_memcpy_slot_count(len, &slots_per_op);
  474. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  475. if (sw_desc) {
  476. grp_start = sw_desc->group_head;
  477. iop_desc_init_memcpy(grp_start, flags);
  478. iop_desc_set_byte_count(grp_start, iop_chan, len);
  479. iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
  480. iop_desc_set_memcpy_src_addr(grp_start, dma_src);
  481. sw_desc->unmap_src_cnt = 1;
  482. sw_desc->unmap_len = len;
  483. sw_desc->async_tx.flags = flags;
  484. }
  485. spin_unlock_bh(&iop_chan->lock);
  486. return sw_desc ? &sw_desc->async_tx : NULL;
  487. }
  488. static struct dma_async_tx_descriptor *
  489. iop_adma_prep_dma_memset(struct dma_chan *chan, dma_addr_t dma_dest,
  490. int value, size_t len, unsigned long flags)
  491. {
  492. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  493. struct iop_adma_desc_slot *sw_desc, *grp_start;
  494. int slot_cnt, slots_per_op;
  495. if (unlikely(!len))
  496. return NULL;
  497. BUG_ON(unlikely(len > IOP_ADMA_MAX_BYTE_COUNT));
  498. dev_dbg(iop_chan->device->common.dev, "%s len: %u\n",
  499. __func__, len);
  500. spin_lock_bh(&iop_chan->lock);
  501. slot_cnt = iop_chan_memset_slot_count(len, &slots_per_op);
  502. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  503. if (sw_desc) {
  504. grp_start = sw_desc->group_head;
  505. iop_desc_init_memset(grp_start, flags);
  506. iop_desc_set_byte_count(grp_start, iop_chan, len);
  507. iop_desc_set_block_fill_val(grp_start, value);
  508. iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
  509. sw_desc->unmap_src_cnt = 1;
  510. sw_desc->unmap_len = len;
  511. sw_desc->async_tx.flags = flags;
  512. }
  513. spin_unlock_bh(&iop_chan->lock);
  514. return sw_desc ? &sw_desc->async_tx : NULL;
  515. }
  516. static struct dma_async_tx_descriptor *
  517. iop_adma_prep_dma_xor(struct dma_chan *chan, dma_addr_t dma_dest,
  518. dma_addr_t *dma_src, unsigned int src_cnt, size_t len,
  519. unsigned long flags)
  520. {
  521. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  522. struct iop_adma_desc_slot *sw_desc, *grp_start;
  523. int slot_cnt, slots_per_op;
  524. if (unlikely(!len))
  525. return NULL;
  526. BUG_ON(unlikely(len > IOP_ADMA_XOR_MAX_BYTE_COUNT));
  527. dev_dbg(iop_chan->device->common.dev,
  528. "%s src_cnt: %d len: %u flags: %lx\n",
  529. __func__, src_cnt, len, flags);
  530. spin_lock_bh(&iop_chan->lock);
  531. slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
  532. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  533. if (sw_desc) {
  534. grp_start = sw_desc->group_head;
  535. iop_desc_init_xor(grp_start, src_cnt, flags);
  536. iop_desc_set_byte_count(grp_start, iop_chan, len);
  537. iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
  538. sw_desc->unmap_src_cnt = src_cnt;
  539. sw_desc->unmap_len = len;
  540. sw_desc->async_tx.flags = flags;
  541. while (src_cnt--)
  542. iop_desc_set_xor_src_addr(grp_start, src_cnt,
  543. dma_src[src_cnt]);
  544. }
  545. spin_unlock_bh(&iop_chan->lock);
  546. return sw_desc ? &sw_desc->async_tx : NULL;
  547. }
  548. static struct dma_async_tx_descriptor *
  549. iop_adma_prep_dma_zero_sum(struct dma_chan *chan, dma_addr_t *dma_src,
  550. unsigned int src_cnt, size_t len, u32 *result,
  551. unsigned long flags)
  552. {
  553. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  554. struct iop_adma_desc_slot *sw_desc, *grp_start;
  555. int slot_cnt, slots_per_op;
  556. if (unlikely(!len))
  557. return NULL;
  558. dev_dbg(iop_chan->device->common.dev, "%s src_cnt: %d len: %u\n",
  559. __func__, src_cnt, len);
  560. spin_lock_bh(&iop_chan->lock);
  561. slot_cnt = iop_chan_zero_sum_slot_count(len, src_cnt, &slots_per_op);
  562. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  563. if (sw_desc) {
  564. grp_start = sw_desc->group_head;
  565. iop_desc_init_zero_sum(grp_start, src_cnt, flags);
  566. iop_desc_set_zero_sum_byte_count(grp_start, len);
  567. grp_start->xor_check_result = result;
  568. pr_debug("\t%s: grp_start->xor_check_result: %p\n",
  569. __func__, grp_start->xor_check_result);
  570. sw_desc->unmap_src_cnt = src_cnt;
  571. sw_desc->unmap_len = len;
  572. sw_desc->async_tx.flags = flags;
  573. while (src_cnt--)
  574. iop_desc_set_zero_sum_src_addr(grp_start, src_cnt,
  575. dma_src[src_cnt]);
  576. }
  577. spin_unlock_bh(&iop_chan->lock);
  578. return sw_desc ? &sw_desc->async_tx : NULL;
  579. }
  580. static void iop_adma_free_chan_resources(struct dma_chan *chan)
  581. {
  582. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  583. struct iop_adma_desc_slot *iter, *_iter;
  584. int in_use_descs = 0;
  585. iop_adma_slot_cleanup(iop_chan);
  586. spin_lock_bh(&iop_chan->lock);
  587. list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
  588. chain_node) {
  589. in_use_descs++;
  590. list_del(&iter->chain_node);
  591. }
  592. list_for_each_entry_safe_reverse(
  593. iter, _iter, &iop_chan->all_slots, slot_node) {
  594. list_del(&iter->slot_node);
  595. kfree(iter);
  596. iop_chan->slots_allocated--;
  597. }
  598. iop_chan->last_used = NULL;
  599. dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
  600. __func__, iop_chan->slots_allocated);
  601. spin_unlock_bh(&iop_chan->lock);
  602. /* one is ok since we left it on there on purpose */
  603. if (in_use_descs > 1)
  604. printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
  605. in_use_descs - 1);
  606. }
  607. /**
  608. * iop_adma_is_complete - poll the status of an ADMA transaction
  609. * @chan: ADMA channel handle
  610. * @cookie: ADMA transaction identifier
  611. */
  612. static enum dma_status iop_adma_is_complete(struct dma_chan *chan,
  613. dma_cookie_t cookie,
  614. dma_cookie_t *done,
  615. dma_cookie_t *used)
  616. {
  617. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  618. dma_cookie_t last_used;
  619. dma_cookie_t last_complete;
  620. enum dma_status ret;
  621. last_used = chan->cookie;
  622. last_complete = iop_chan->completed_cookie;
  623. if (done)
  624. *done = last_complete;
  625. if (used)
  626. *used = last_used;
  627. ret = dma_async_is_complete(cookie, last_complete, last_used);
  628. if (ret == DMA_SUCCESS)
  629. return ret;
  630. iop_adma_slot_cleanup(iop_chan);
  631. last_used = chan->cookie;
  632. last_complete = iop_chan->completed_cookie;
  633. if (done)
  634. *done = last_complete;
  635. if (used)
  636. *used = last_used;
  637. return dma_async_is_complete(cookie, last_complete, last_used);
  638. }
  639. static irqreturn_t iop_adma_eot_handler(int irq, void *data)
  640. {
  641. struct iop_adma_chan *chan = data;
  642. dev_dbg(chan->device->common.dev, "%s\n", __func__);
  643. tasklet_schedule(&chan->irq_tasklet);
  644. iop_adma_device_clear_eot_status(chan);
  645. return IRQ_HANDLED;
  646. }
  647. static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
  648. {
  649. struct iop_adma_chan *chan = data;
  650. dev_dbg(chan->device->common.dev, "%s\n", __func__);
  651. tasklet_schedule(&chan->irq_tasklet);
  652. iop_adma_device_clear_eoc_status(chan);
  653. return IRQ_HANDLED;
  654. }
  655. static irqreturn_t iop_adma_err_handler(int irq, void *data)
  656. {
  657. struct iop_adma_chan *chan = data;
  658. unsigned long status = iop_chan_get_status(chan);
  659. dev_printk(KERN_ERR, chan->device->common.dev,
  660. "error ( %s%s%s%s%s%s%s)\n",
  661. iop_is_err_int_parity(status, chan) ? "int_parity " : "",
  662. iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
  663. iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
  664. iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
  665. iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
  666. iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
  667. iop_is_err_split_tx(status, chan) ? "split_tx " : "");
  668. iop_adma_device_clear_err_status(chan);
  669. BUG();
  670. return IRQ_HANDLED;
  671. }
  672. static void iop_adma_issue_pending(struct dma_chan *chan)
  673. {
  674. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  675. if (iop_chan->pending) {
  676. iop_chan->pending = 0;
  677. iop_chan_append(iop_chan);
  678. }
  679. }
  680. /*
  681. * Perform a transaction to verify the HW works.
  682. */
  683. #define IOP_ADMA_TEST_SIZE 2000
  684. static int __devinit iop_adma_memcpy_self_test(struct iop_adma_device *device)
  685. {
  686. int i;
  687. void *src, *dest;
  688. dma_addr_t src_dma, dest_dma;
  689. struct dma_chan *dma_chan;
  690. dma_cookie_t cookie;
  691. struct dma_async_tx_descriptor *tx;
  692. int err = 0;
  693. struct iop_adma_chan *iop_chan;
  694. dev_dbg(device->common.dev, "%s\n", __func__);
  695. src = kmalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  696. if (!src)
  697. return -ENOMEM;
  698. dest = kzalloc(IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  699. if (!dest) {
  700. kfree(src);
  701. return -ENOMEM;
  702. }
  703. /* Fill in src buffer */
  704. for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
  705. ((u8 *) src)[i] = (u8)i;
  706. /* Start copy, using first DMA channel */
  707. dma_chan = container_of(device->common.channels.next,
  708. struct dma_chan,
  709. device_node);
  710. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  711. err = -ENODEV;
  712. goto out;
  713. }
  714. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  715. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  716. src_dma = dma_map_single(dma_chan->device->dev, src,
  717. IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
  718. tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  719. IOP_ADMA_TEST_SIZE,
  720. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  721. cookie = iop_adma_tx_submit(tx);
  722. iop_adma_issue_pending(dma_chan);
  723. msleep(1);
  724. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
  725. DMA_SUCCESS) {
  726. dev_printk(KERN_ERR, dma_chan->device->dev,
  727. "Self-test copy timed out, disabling\n");
  728. err = -ENODEV;
  729. goto free_resources;
  730. }
  731. iop_chan = to_iop_adma_chan(dma_chan);
  732. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  733. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  734. if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
  735. dev_printk(KERN_ERR, dma_chan->device->dev,
  736. "Self-test copy failed compare, disabling\n");
  737. err = -ENODEV;
  738. goto free_resources;
  739. }
  740. free_resources:
  741. iop_adma_free_chan_resources(dma_chan);
  742. out:
  743. kfree(src);
  744. kfree(dest);
  745. return err;
  746. }
  747. #define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
  748. static int __devinit
  749. iop_adma_xor_zero_sum_self_test(struct iop_adma_device *device)
  750. {
  751. int i, src_idx;
  752. struct page *dest;
  753. struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
  754. struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  755. dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  756. dma_addr_t dma_addr, dest_dma;
  757. struct dma_async_tx_descriptor *tx;
  758. struct dma_chan *dma_chan;
  759. dma_cookie_t cookie;
  760. u8 cmp_byte = 0;
  761. u32 cmp_word;
  762. u32 zero_sum_result;
  763. int err = 0;
  764. struct iop_adma_chan *iop_chan;
  765. dev_dbg(device->common.dev, "%s\n", __func__);
  766. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  767. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  768. if (!xor_srcs[src_idx])
  769. while (src_idx--) {
  770. __free_page(xor_srcs[src_idx]);
  771. return -ENOMEM;
  772. }
  773. }
  774. dest = alloc_page(GFP_KERNEL);
  775. if (!dest)
  776. while (src_idx--) {
  777. __free_page(xor_srcs[src_idx]);
  778. return -ENOMEM;
  779. }
  780. /* Fill in src buffers */
  781. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  782. u8 *ptr = page_address(xor_srcs[src_idx]);
  783. for (i = 0; i < PAGE_SIZE; i++)
  784. ptr[i] = (1 << src_idx);
  785. }
  786. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
  787. cmp_byte ^= (u8) (1 << src_idx);
  788. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  789. (cmp_byte << 8) | cmp_byte;
  790. memset(page_address(dest), 0, PAGE_SIZE);
  791. dma_chan = container_of(device->common.channels.next,
  792. struct dma_chan,
  793. device_node);
  794. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  795. err = -ENODEV;
  796. goto out;
  797. }
  798. /* test xor */
  799. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
  800. PAGE_SIZE, DMA_FROM_DEVICE);
  801. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  802. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  803. 0, PAGE_SIZE, DMA_TO_DEVICE);
  804. tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  805. IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE,
  806. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  807. cookie = iop_adma_tx_submit(tx);
  808. iop_adma_issue_pending(dma_chan);
  809. msleep(8);
  810. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
  811. DMA_SUCCESS) {
  812. dev_printk(KERN_ERR, dma_chan->device->dev,
  813. "Self-test xor timed out, disabling\n");
  814. err = -ENODEV;
  815. goto free_resources;
  816. }
  817. iop_chan = to_iop_adma_chan(dma_chan);
  818. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  819. PAGE_SIZE, DMA_FROM_DEVICE);
  820. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  821. u32 *ptr = page_address(dest);
  822. if (ptr[i] != cmp_word) {
  823. dev_printk(KERN_ERR, dma_chan->device->dev,
  824. "Self-test xor failed compare, disabling\n");
  825. err = -ENODEV;
  826. goto free_resources;
  827. }
  828. }
  829. dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
  830. PAGE_SIZE, DMA_TO_DEVICE);
  831. /* skip zero sum if the capability is not present */
  832. if (!dma_has_cap(DMA_ZERO_SUM, dma_chan->device->cap_mask))
  833. goto free_resources;
  834. /* zero sum the sources with the destintation page */
  835. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  836. zero_sum_srcs[i] = xor_srcs[i];
  837. zero_sum_srcs[i] = dest;
  838. zero_sum_result = 1;
  839. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  840. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  841. zero_sum_srcs[i], 0, PAGE_SIZE,
  842. DMA_TO_DEVICE);
  843. tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
  844. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  845. &zero_sum_result,
  846. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  847. cookie = iop_adma_tx_submit(tx);
  848. iop_adma_issue_pending(dma_chan);
  849. msleep(8);
  850. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  851. dev_printk(KERN_ERR, dma_chan->device->dev,
  852. "Self-test zero sum timed out, disabling\n");
  853. err = -ENODEV;
  854. goto free_resources;
  855. }
  856. if (zero_sum_result != 0) {
  857. dev_printk(KERN_ERR, dma_chan->device->dev,
  858. "Self-test zero sum failed compare, disabling\n");
  859. err = -ENODEV;
  860. goto free_resources;
  861. }
  862. /* test memset */
  863. dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
  864. PAGE_SIZE, DMA_FROM_DEVICE);
  865. tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  866. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  867. cookie = iop_adma_tx_submit(tx);
  868. iop_adma_issue_pending(dma_chan);
  869. msleep(8);
  870. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  871. dev_printk(KERN_ERR, dma_chan->device->dev,
  872. "Self-test memset timed out, disabling\n");
  873. err = -ENODEV;
  874. goto free_resources;
  875. }
  876. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  877. u32 *ptr = page_address(dest);
  878. if (ptr[i]) {
  879. dev_printk(KERN_ERR, dma_chan->device->dev,
  880. "Self-test memset failed compare, disabling\n");
  881. err = -ENODEV;
  882. goto free_resources;
  883. }
  884. }
  885. /* test for non-zero parity sum */
  886. zero_sum_result = 0;
  887. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  888. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  889. zero_sum_srcs[i], 0, PAGE_SIZE,
  890. DMA_TO_DEVICE);
  891. tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
  892. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  893. &zero_sum_result,
  894. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  895. cookie = iop_adma_tx_submit(tx);
  896. iop_adma_issue_pending(dma_chan);
  897. msleep(8);
  898. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  899. dev_printk(KERN_ERR, dma_chan->device->dev,
  900. "Self-test non-zero sum timed out, disabling\n");
  901. err = -ENODEV;
  902. goto free_resources;
  903. }
  904. if (zero_sum_result != 1) {
  905. dev_printk(KERN_ERR, dma_chan->device->dev,
  906. "Self-test non-zero sum failed compare, disabling\n");
  907. err = -ENODEV;
  908. goto free_resources;
  909. }
  910. free_resources:
  911. iop_adma_free_chan_resources(dma_chan);
  912. out:
  913. src_idx = IOP_ADMA_NUM_SRC_TEST;
  914. while (src_idx--)
  915. __free_page(xor_srcs[src_idx]);
  916. __free_page(dest);
  917. return err;
  918. }
  919. static int __devexit iop_adma_remove(struct platform_device *dev)
  920. {
  921. struct iop_adma_device *device = platform_get_drvdata(dev);
  922. struct dma_chan *chan, *_chan;
  923. struct iop_adma_chan *iop_chan;
  924. int i;
  925. struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
  926. dma_async_device_unregister(&device->common);
  927. for (i = 0; i < 3; i++) {
  928. unsigned int irq;
  929. irq = platform_get_irq(dev, i);
  930. free_irq(irq, device);
  931. }
  932. dma_free_coherent(&dev->dev, plat_data->pool_size,
  933. device->dma_desc_pool_virt, device->dma_desc_pool);
  934. do {
  935. struct resource *res;
  936. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  937. release_mem_region(res->start, res->end - res->start);
  938. } while (0);
  939. list_for_each_entry_safe(chan, _chan, &device->common.channels,
  940. device_node) {
  941. iop_chan = to_iop_adma_chan(chan);
  942. list_del(&chan->device_node);
  943. kfree(iop_chan);
  944. }
  945. kfree(device);
  946. return 0;
  947. }
  948. static int __devinit iop_adma_probe(struct platform_device *pdev)
  949. {
  950. struct resource *res;
  951. int ret = 0, i;
  952. struct iop_adma_device *adev;
  953. struct iop_adma_chan *iop_chan;
  954. struct dma_device *dma_dev;
  955. struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
  956. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  957. if (!res)
  958. return -ENODEV;
  959. if (!devm_request_mem_region(&pdev->dev, res->start,
  960. res->end - res->start, pdev->name))
  961. return -EBUSY;
  962. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  963. if (!adev)
  964. return -ENOMEM;
  965. dma_dev = &adev->common;
  966. /* allocate coherent memory for hardware descriptors
  967. * note: writecombine gives slightly better performance, but
  968. * requires that we explicitly flush the writes
  969. */
  970. if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
  971. plat_data->pool_size,
  972. &adev->dma_desc_pool,
  973. GFP_KERNEL)) == NULL) {
  974. ret = -ENOMEM;
  975. goto err_free_adev;
  976. }
  977. dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
  978. __func__, adev->dma_desc_pool_virt,
  979. (void *) adev->dma_desc_pool);
  980. adev->id = plat_data->hw_id;
  981. /* discover transaction capabilites from the platform data */
  982. dma_dev->cap_mask = plat_data->cap_mask;
  983. adev->pdev = pdev;
  984. platform_set_drvdata(pdev, adev);
  985. INIT_LIST_HEAD(&dma_dev->channels);
  986. /* set base routines */
  987. dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
  988. dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
  989. dma_dev->device_is_tx_complete = iop_adma_is_complete;
  990. dma_dev->device_issue_pending = iop_adma_issue_pending;
  991. dma_dev->dev = &pdev->dev;
  992. /* set prep routines based on capability */
  993. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  994. dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
  995. if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
  996. dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
  997. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  998. dma_dev->max_xor = iop_adma_get_max_xor();
  999. dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
  1000. }
  1001. if (dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask))
  1002. dma_dev->device_prep_dma_zero_sum =
  1003. iop_adma_prep_dma_zero_sum;
  1004. if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
  1005. dma_dev->device_prep_dma_interrupt =
  1006. iop_adma_prep_dma_interrupt;
  1007. iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
  1008. if (!iop_chan) {
  1009. ret = -ENOMEM;
  1010. goto err_free_dma;
  1011. }
  1012. iop_chan->device = adev;
  1013. iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
  1014. res->end - res->start);
  1015. if (!iop_chan->mmr_base) {
  1016. ret = -ENOMEM;
  1017. goto err_free_iop_chan;
  1018. }
  1019. tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
  1020. iop_chan);
  1021. /* clear errors before enabling interrupts */
  1022. iop_adma_device_clear_err_status(iop_chan);
  1023. for (i = 0; i < 3; i++) {
  1024. irq_handler_t handler[] = { iop_adma_eot_handler,
  1025. iop_adma_eoc_handler,
  1026. iop_adma_err_handler };
  1027. int irq = platform_get_irq(pdev, i);
  1028. if (irq < 0) {
  1029. ret = -ENXIO;
  1030. goto err_free_iop_chan;
  1031. } else {
  1032. ret = devm_request_irq(&pdev->dev, irq,
  1033. handler[i], 0, pdev->name, iop_chan);
  1034. if (ret)
  1035. goto err_free_iop_chan;
  1036. }
  1037. }
  1038. spin_lock_init(&iop_chan->lock);
  1039. INIT_LIST_HEAD(&iop_chan->chain);
  1040. INIT_LIST_HEAD(&iop_chan->all_slots);
  1041. INIT_RCU_HEAD(&iop_chan->common.rcu);
  1042. iop_chan->common.device = dma_dev;
  1043. list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
  1044. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  1045. ret = iop_adma_memcpy_self_test(adev);
  1046. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  1047. if (ret)
  1048. goto err_free_iop_chan;
  1049. }
  1050. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
  1051. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
  1052. ret = iop_adma_xor_zero_sum_self_test(adev);
  1053. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  1054. if (ret)
  1055. goto err_free_iop_chan;
  1056. }
  1057. dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
  1058. "( %s%s%s%s%s%s%s%s%s%s)\n",
  1059. dma_has_cap(DMA_PQ_XOR, dma_dev->cap_mask) ? "pq_xor " : "",
  1060. dma_has_cap(DMA_PQ_UPDATE, dma_dev->cap_mask) ? "pq_update " : "",
  1061. dma_has_cap(DMA_PQ_ZERO_SUM, dma_dev->cap_mask) ? "pq_zero_sum " : "",
  1062. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  1063. dma_has_cap(DMA_DUAL_XOR, dma_dev->cap_mask) ? "dual_xor " : "",
  1064. dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask) ? "xor_zero_sum " : "",
  1065. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
  1066. dma_has_cap(DMA_MEMCPY_CRC32C, dma_dev->cap_mask) ? "cpy+crc " : "",
  1067. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  1068. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  1069. dma_async_device_register(dma_dev);
  1070. goto out;
  1071. err_free_iop_chan:
  1072. kfree(iop_chan);
  1073. err_free_dma:
  1074. dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
  1075. adev->dma_desc_pool_virt, adev->dma_desc_pool);
  1076. err_free_adev:
  1077. kfree(adev);
  1078. out:
  1079. return ret;
  1080. }
  1081. static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
  1082. {
  1083. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1084. dma_cookie_t cookie;
  1085. int slot_cnt, slots_per_op;
  1086. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1087. spin_lock_bh(&iop_chan->lock);
  1088. slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
  1089. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1090. if (sw_desc) {
  1091. grp_start = sw_desc->group_head;
  1092. list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
  1093. async_tx_ack(&sw_desc->async_tx);
  1094. iop_desc_init_memcpy(grp_start, 0);
  1095. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1096. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1097. iop_desc_set_memcpy_src_addr(grp_start, 0);
  1098. cookie = iop_chan->common.cookie;
  1099. cookie++;
  1100. if (cookie <= 1)
  1101. cookie = 2;
  1102. /* initialize the completed cookie to be less than
  1103. * the most recently used cookie
  1104. */
  1105. iop_chan->completed_cookie = cookie - 1;
  1106. iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
  1107. /* channel should not be busy */
  1108. BUG_ON(iop_chan_is_busy(iop_chan));
  1109. /* clear any prior error-status bits */
  1110. iop_adma_device_clear_err_status(iop_chan);
  1111. /* disable operation */
  1112. iop_chan_disable(iop_chan);
  1113. /* set the descriptor address */
  1114. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1115. /* 1/ don't add pre-chained descriptors
  1116. * 2/ dummy read to flush next_desc write
  1117. */
  1118. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1119. /* run the descriptor */
  1120. iop_chan_enable(iop_chan);
  1121. } else
  1122. dev_printk(KERN_ERR, iop_chan->device->common.dev,
  1123. "failed to allocate null descriptor\n");
  1124. spin_unlock_bh(&iop_chan->lock);
  1125. }
  1126. static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
  1127. {
  1128. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1129. dma_cookie_t cookie;
  1130. int slot_cnt, slots_per_op;
  1131. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1132. spin_lock_bh(&iop_chan->lock);
  1133. slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
  1134. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1135. if (sw_desc) {
  1136. grp_start = sw_desc->group_head;
  1137. list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
  1138. async_tx_ack(&sw_desc->async_tx);
  1139. iop_desc_init_null_xor(grp_start, 2, 0);
  1140. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1141. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1142. iop_desc_set_xor_src_addr(grp_start, 0, 0);
  1143. iop_desc_set_xor_src_addr(grp_start, 1, 0);
  1144. cookie = iop_chan->common.cookie;
  1145. cookie++;
  1146. if (cookie <= 1)
  1147. cookie = 2;
  1148. /* initialize the completed cookie to be less than
  1149. * the most recently used cookie
  1150. */
  1151. iop_chan->completed_cookie = cookie - 1;
  1152. iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
  1153. /* channel should not be busy */
  1154. BUG_ON(iop_chan_is_busy(iop_chan));
  1155. /* clear any prior error-status bits */
  1156. iop_adma_device_clear_err_status(iop_chan);
  1157. /* disable operation */
  1158. iop_chan_disable(iop_chan);
  1159. /* set the descriptor address */
  1160. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1161. /* 1/ don't add pre-chained descriptors
  1162. * 2/ dummy read to flush next_desc write
  1163. */
  1164. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1165. /* run the descriptor */
  1166. iop_chan_enable(iop_chan);
  1167. } else
  1168. dev_printk(KERN_ERR, iop_chan->device->common.dev,
  1169. "failed to allocate null descriptor\n");
  1170. spin_unlock_bh(&iop_chan->lock);
  1171. }
  1172. static struct platform_driver iop_adma_driver = {
  1173. .probe = iop_adma_probe,
  1174. .remove = iop_adma_remove,
  1175. .driver = {
  1176. .owner = THIS_MODULE,
  1177. .name = "iop-adma",
  1178. },
  1179. };
  1180. static int __init iop_adma_init (void)
  1181. {
  1182. return platform_driver_register(&iop_adma_driver);
  1183. }
  1184. /* it's currently unsafe to unload this module */
  1185. #if 0
  1186. static void __exit iop_adma_exit (void)
  1187. {
  1188. platform_driver_unregister(&iop_adma_driver);
  1189. return;
  1190. }
  1191. module_exit(iop_adma_exit);
  1192. #endif
  1193. module_init(iop_adma_init);
  1194. MODULE_AUTHOR("Intel Corporation");
  1195. MODULE_DESCRIPTION("IOP ADMA Engine Driver");
  1196. MODULE_LICENSE("GPL");