iop-adma.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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 = kzalloc(sizeof(u8) * IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  696. if (!src)
  697. return -ENOMEM;
  698. dest = kzalloc(sizeof(u8) * 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. memset(dest, 0, IOP_ADMA_TEST_SIZE);
  707. /* Start copy, using first DMA channel */
  708. dma_chan = container_of(device->common.channels.next,
  709. struct dma_chan,
  710. device_node);
  711. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  712. err = -ENODEV;
  713. goto out;
  714. }
  715. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  716. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  717. src_dma = dma_map_single(dma_chan->device->dev, src,
  718. IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
  719. tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  720. IOP_ADMA_TEST_SIZE,
  721. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  722. cookie = iop_adma_tx_submit(tx);
  723. iop_adma_issue_pending(dma_chan);
  724. msleep(1);
  725. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
  726. DMA_SUCCESS) {
  727. dev_printk(KERN_ERR, dma_chan->device->dev,
  728. "Self-test copy timed out, disabling\n");
  729. err = -ENODEV;
  730. goto free_resources;
  731. }
  732. iop_chan = to_iop_adma_chan(dma_chan);
  733. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  734. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  735. if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
  736. dev_printk(KERN_ERR, dma_chan->device->dev,
  737. "Self-test copy failed compare, disabling\n");
  738. err = -ENODEV;
  739. goto free_resources;
  740. }
  741. free_resources:
  742. iop_adma_free_chan_resources(dma_chan);
  743. out:
  744. kfree(src);
  745. kfree(dest);
  746. return err;
  747. }
  748. #define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
  749. static int __devinit
  750. iop_adma_xor_zero_sum_self_test(struct iop_adma_device *device)
  751. {
  752. int i, src_idx;
  753. struct page *dest;
  754. struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
  755. struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  756. dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  757. dma_addr_t dma_addr, dest_dma;
  758. struct dma_async_tx_descriptor *tx;
  759. struct dma_chan *dma_chan;
  760. dma_cookie_t cookie;
  761. u8 cmp_byte = 0;
  762. u32 cmp_word;
  763. u32 zero_sum_result;
  764. int err = 0;
  765. struct iop_adma_chan *iop_chan;
  766. dev_dbg(device->common.dev, "%s\n", __func__);
  767. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  768. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  769. if (!xor_srcs[src_idx])
  770. while (src_idx--) {
  771. __free_page(xor_srcs[src_idx]);
  772. return -ENOMEM;
  773. }
  774. }
  775. dest = alloc_page(GFP_KERNEL);
  776. if (!dest)
  777. while (src_idx--) {
  778. __free_page(xor_srcs[src_idx]);
  779. return -ENOMEM;
  780. }
  781. /* Fill in src buffers */
  782. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  783. u8 *ptr = page_address(xor_srcs[src_idx]);
  784. for (i = 0; i < PAGE_SIZE; i++)
  785. ptr[i] = (1 << src_idx);
  786. }
  787. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
  788. cmp_byte ^= (u8) (1 << src_idx);
  789. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  790. (cmp_byte << 8) | cmp_byte;
  791. memset(page_address(dest), 0, PAGE_SIZE);
  792. dma_chan = container_of(device->common.channels.next,
  793. struct dma_chan,
  794. device_node);
  795. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  796. err = -ENODEV;
  797. goto out;
  798. }
  799. /* test xor */
  800. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
  801. PAGE_SIZE, DMA_FROM_DEVICE);
  802. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  803. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  804. 0, PAGE_SIZE, DMA_TO_DEVICE);
  805. tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  806. IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE,
  807. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  808. cookie = iop_adma_tx_submit(tx);
  809. iop_adma_issue_pending(dma_chan);
  810. msleep(8);
  811. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
  812. DMA_SUCCESS) {
  813. dev_printk(KERN_ERR, dma_chan->device->dev,
  814. "Self-test xor timed out, disabling\n");
  815. err = -ENODEV;
  816. goto free_resources;
  817. }
  818. iop_chan = to_iop_adma_chan(dma_chan);
  819. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  820. PAGE_SIZE, DMA_FROM_DEVICE);
  821. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  822. u32 *ptr = page_address(dest);
  823. if (ptr[i] != cmp_word) {
  824. dev_printk(KERN_ERR, dma_chan->device->dev,
  825. "Self-test xor failed compare, disabling\n");
  826. err = -ENODEV;
  827. goto free_resources;
  828. }
  829. }
  830. dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
  831. PAGE_SIZE, DMA_TO_DEVICE);
  832. /* skip zero sum if the capability is not present */
  833. if (!dma_has_cap(DMA_ZERO_SUM, dma_chan->device->cap_mask))
  834. goto free_resources;
  835. /* zero sum the sources with the destintation page */
  836. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  837. zero_sum_srcs[i] = xor_srcs[i];
  838. zero_sum_srcs[i] = dest;
  839. zero_sum_result = 1;
  840. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  841. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  842. zero_sum_srcs[i], 0, PAGE_SIZE,
  843. DMA_TO_DEVICE);
  844. tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
  845. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  846. &zero_sum_result,
  847. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  848. cookie = iop_adma_tx_submit(tx);
  849. iop_adma_issue_pending(dma_chan);
  850. msleep(8);
  851. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  852. dev_printk(KERN_ERR, dma_chan->device->dev,
  853. "Self-test zero sum timed out, disabling\n");
  854. err = -ENODEV;
  855. goto free_resources;
  856. }
  857. if (zero_sum_result != 0) {
  858. dev_printk(KERN_ERR, dma_chan->device->dev,
  859. "Self-test zero sum failed compare, disabling\n");
  860. err = -ENODEV;
  861. goto free_resources;
  862. }
  863. /* test memset */
  864. dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
  865. PAGE_SIZE, DMA_FROM_DEVICE);
  866. tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  867. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  868. cookie = iop_adma_tx_submit(tx);
  869. iop_adma_issue_pending(dma_chan);
  870. msleep(8);
  871. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  872. dev_printk(KERN_ERR, dma_chan->device->dev,
  873. "Self-test memset timed out, disabling\n");
  874. err = -ENODEV;
  875. goto free_resources;
  876. }
  877. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  878. u32 *ptr = page_address(dest);
  879. if (ptr[i]) {
  880. dev_printk(KERN_ERR, dma_chan->device->dev,
  881. "Self-test memset failed compare, disabling\n");
  882. err = -ENODEV;
  883. goto free_resources;
  884. }
  885. }
  886. /* test for non-zero parity sum */
  887. zero_sum_result = 0;
  888. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  889. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  890. zero_sum_srcs[i], 0, PAGE_SIZE,
  891. DMA_TO_DEVICE);
  892. tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
  893. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  894. &zero_sum_result,
  895. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  896. cookie = iop_adma_tx_submit(tx);
  897. iop_adma_issue_pending(dma_chan);
  898. msleep(8);
  899. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  900. dev_printk(KERN_ERR, dma_chan->device->dev,
  901. "Self-test non-zero sum timed out, disabling\n");
  902. err = -ENODEV;
  903. goto free_resources;
  904. }
  905. if (zero_sum_result != 1) {
  906. dev_printk(KERN_ERR, dma_chan->device->dev,
  907. "Self-test non-zero sum failed compare, disabling\n");
  908. err = -ENODEV;
  909. goto free_resources;
  910. }
  911. free_resources:
  912. iop_adma_free_chan_resources(dma_chan);
  913. out:
  914. src_idx = IOP_ADMA_NUM_SRC_TEST;
  915. while (src_idx--)
  916. __free_page(xor_srcs[src_idx]);
  917. __free_page(dest);
  918. return err;
  919. }
  920. static int __devexit iop_adma_remove(struct platform_device *dev)
  921. {
  922. struct iop_adma_device *device = platform_get_drvdata(dev);
  923. struct dma_chan *chan, *_chan;
  924. struct iop_adma_chan *iop_chan;
  925. int i;
  926. struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
  927. dma_async_device_unregister(&device->common);
  928. for (i = 0; i < 3; i++) {
  929. unsigned int irq;
  930. irq = platform_get_irq(dev, i);
  931. free_irq(irq, device);
  932. }
  933. dma_free_coherent(&dev->dev, plat_data->pool_size,
  934. device->dma_desc_pool_virt, device->dma_desc_pool);
  935. do {
  936. struct resource *res;
  937. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  938. release_mem_region(res->start, res->end - res->start);
  939. } while (0);
  940. list_for_each_entry_safe(chan, _chan, &device->common.channels,
  941. device_node) {
  942. iop_chan = to_iop_adma_chan(chan);
  943. list_del(&chan->device_node);
  944. kfree(iop_chan);
  945. }
  946. kfree(device);
  947. return 0;
  948. }
  949. static int __devinit iop_adma_probe(struct platform_device *pdev)
  950. {
  951. struct resource *res;
  952. int ret = 0, i;
  953. struct iop_adma_device *adev;
  954. struct iop_adma_chan *iop_chan;
  955. struct dma_device *dma_dev;
  956. struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
  957. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  958. if (!res)
  959. return -ENODEV;
  960. if (!devm_request_mem_region(&pdev->dev, res->start,
  961. res->end - res->start, pdev->name))
  962. return -EBUSY;
  963. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  964. if (!adev)
  965. return -ENOMEM;
  966. dma_dev = &adev->common;
  967. /* allocate coherent memory for hardware descriptors
  968. * note: writecombine gives slightly better performance, but
  969. * requires that we explicitly flush the writes
  970. */
  971. if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
  972. plat_data->pool_size,
  973. &adev->dma_desc_pool,
  974. GFP_KERNEL)) == NULL) {
  975. ret = -ENOMEM;
  976. goto err_free_adev;
  977. }
  978. dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
  979. __func__, adev->dma_desc_pool_virt,
  980. (void *) adev->dma_desc_pool);
  981. adev->id = plat_data->hw_id;
  982. /* discover transaction capabilites from the platform data */
  983. dma_dev->cap_mask = plat_data->cap_mask;
  984. adev->pdev = pdev;
  985. platform_set_drvdata(pdev, adev);
  986. INIT_LIST_HEAD(&dma_dev->channels);
  987. /* set base routines */
  988. dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
  989. dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
  990. dma_dev->device_is_tx_complete = iop_adma_is_complete;
  991. dma_dev->device_issue_pending = iop_adma_issue_pending;
  992. dma_dev->dev = &pdev->dev;
  993. /* set prep routines based on capability */
  994. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  995. dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
  996. if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
  997. dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
  998. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  999. dma_dev->max_xor = iop_adma_get_max_xor();
  1000. dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
  1001. }
  1002. if (dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask))
  1003. dma_dev->device_prep_dma_zero_sum =
  1004. iop_adma_prep_dma_zero_sum;
  1005. if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
  1006. dma_dev->device_prep_dma_interrupt =
  1007. iop_adma_prep_dma_interrupt;
  1008. iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
  1009. if (!iop_chan) {
  1010. ret = -ENOMEM;
  1011. goto err_free_dma;
  1012. }
  1013. iop_chan->device = adev;
  1014. iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
  1015. res->end - res->start);
  1016. if (!iop_chan->mmr_base) {
  1017. ret = -ENOMEM;
  1018. goto err_free_iop_chan;
  1019. }
  1020. tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
  1021. iop_chan);
  1022. /* clear errors before enabling interrupts */
  1023. iop_adma_device_clear_err_status(iop_chan);
  1024. for (i = 0; i < 3; i++) {
  1025. irq_handler_t handler[] = { iop_adma_eot_handler,
  1026. iop_adma_eoc_handler,
  1027. iop_adma_err_handler };
  1028. int irq = platform_get_irq(pdev, i);
  1029. if (irq < 0) {
  1030. ret = -ENXIO;
  1031. goto err_free_iop_chan;
  1032. } else {
  1033. ret = devm_request_irq(&pdev->dev, irq,
  1034. handler[i], 0, pdev->name, iop_chan);
  1035. if (ret)
  1036. goto err_free_iop_chan;
  1037. }
  1038. }
  1039. spin_lock_init(&iop_chan->lock);
  1040. INIT_LIST_HEAD(&iop_chan->chain);
  1041. INIT_LIST_HEAD(&iop_chan->all_slots);
  1042. INIT_RCU_HEAD(&iop_chan->common.rcu);
  1043. iop_chan->common.device = dma_dev;
  1044. list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
  1045. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  1046. ret = iop_adma_memcpy_self_test(adev);
  1047. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  1048. if (ret)
  1049. goto err_free_iop_chan;
  1050. }
  1051. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
  1052. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
  1053. ret = iop_adma_xor_zero_sum_self_test(adev);
  1054. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  1055. if (ret)
  1056. goto err_free_iop_chan;
  1057. }
  1058. dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
  1059. "( %s%s%s%s%s%s%s%s%s%s)\n",
  1060. dma_has_cap(DMA_PQ_XOR, dma_dev->cap_mask) ? "pq_xor " : "",
  1061. dma_has_cap(DMA_PQ_UPDATE, dma_dev->cap_mask) ? "pq_update " : "",
  1062. dma_has_cap(DMA_PQ_ZERO_SUM, dma_dev->cap_mask) ? "pq_zero_sum " : "",
  1063. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  1064. dma_has_cap(DMA_DUAL_XOR, dma_dev->cap_mask) ? "dual_xor " : "",
  1065. dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask) ? "xor_zero_sum " : "",
  1066. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
  1067. dma_has_cap(DMA_MEMCPY_CRC32C, dma_dev->cap_mask) ? "cpy+crc " : "",
  1068. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  1069. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  1070. dma_async_device_register(dma_dev);
  1071. goto out;
  1072. err_free_iop_chan:
  1073. kfree(iop_chan);
  1074. err_free_dma:
  1075. dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
  1076. adev->dma_desc_pool_virt, adev->dma_desc_pool);
  1077. err_free_adev:
  1078. kfree(adev);
  1079. out:
  1080. return ret;
  1081. }
  1082. static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
  1083. {
  1084. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1085. dma_cookie_t cookie;
  1086. int slot_cnt, slots_per_op;
  1087. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1088. spin_lock_bh(&iop_chan->lock);
  1089. slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
  1090. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1091. if (sw_desc) {
  1092. grp_start = sw_desc->group_head;
  1093. list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
  1094. async_tx_ack(&sw_desc->async_tx);
  1095. iop_desc_init_memcpy(grp_start, 0);
  1096. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1097. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1098. iop_desc_set_memcpy_src_addr(grp_start, 0);
  1099. cookie = iop_chan->common.cookie;
  1100. cookie++;
  1101. if (cookie <= 1)
  1102. cookie = 2;
  1103. /* initialize the completed cookie to be less than
  1104. * the most recently used cookie
  1105. */
  1106. iop_chan->completed_cookie = cookie - 1;
  1107. iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
  1108. /* channel should not be busy */
  1109. BUG_ON(iop_chan_is_busy(iop_chan));
  1110. /* clear any prior error-status bits */
  1111. iop_adma_device_clear_err_status(iop_chan);
  1112. /* disable operation */
  1113. iop_chan_disable(iop_chan);
  1114. /* set the descriptor address */
  1115. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1116. /* 1/ don't add pre-chained descriptors
  1117. * 2/ dummy read to flush next_desc write
  1118. */
  1119. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1120. /* run the descriptor */
  1121. iop_chan_enable(iop_chan);
  1122. } else
  1123. dev_printk(KERN_ERR, iop_chan->device->common.dev,
  1124. "failed to allocate null descriptor\n");
  1125. spin_unlock_bh(&iop_chan->lock);
  1126. }
  1127. static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
  1128. {
  1129. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1130. dma_cookie_t cookie;
  1131. int slot_cnt, slots_per_op;
  1132. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1133. spin_lock_bh(&iop_chan->lock);
  1134. slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
  1135. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1136. if (sw_desc) {
  1137. grp_start = sw_desc->group_head;
  1138. list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
  1139. async_tx_ack(&sw_desc->async_tx);
  1140. iop_desc_init_null_xor(grp_start, 2, 0);
  1141. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1142. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1143. iop_desc_set_xor_src_addr(grp_start, 0, 0);
  1144. iop_desc_set_xor_src_addr(grp_start, 1, 0);
  1145. cookie = iop_chan->common.cookie;
  1146. cookie++;
  1147. if (cookie <= 1)
  1148. cookie = 2;
  1149. /* initialize the completed cookie to be less than
  1150. * the most recently used cookie
  1151. */
  1152. iop_chan->completed_cookie = cookie - 1;
  1153. iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
  1154. /* channel should not be busy */
  1155. BUG_ON(iop_chan_is_busy(iop_chan));
  1156. /* clear any prior error-status bits */
  1157. iop_adma_device_clear_err_status(iop_chan);
  1158. /* disable operation */
  1159. iop_chan_disable(iop_chan);
  1160. /* set the descriptor address */
  1161. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1162. /* 1/ don't add pre-chained descriptors
  1163. * 2/ dummy read to flush next_desc write
  1164. */
  1165. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1166. /* run the descriptor */
  1167. iop_chan_enable(iop_chan);
  1168. } else
  1169. dev_printk(KERN_ERR, iop_chan->device->common.dev,
  1170. "failed to allocate null descriptor\n");
  1171. spin_unlock_bh(&iop_chan->lock);
  1172. }
  1173. static struct platform_driver iop_adma_driver = {
  1174. .probe = iop_adma_probe,
  1175. .remove = iop_adma_remove,
  1176. .driver = {
  1177. .owner = THIS_MODULE,
  1178. .name = "iop-adma",
  1179. },
  1180. };
  1181. static int __init iop_adma_init (void)
  1182. {
  1183. return platform_driver_register(&iop_adma_driver);
  1184. }
  1185. /* it's currently unsafe to unload this module */
  1186. #if 0
  1187. static void __exit iop_adma_exit (void)
  1188. {
  1189. platform_driver_unregister(&iop_adma_driver);
  1190. return;
  1191. }
  1192. module_exit(iop_adma_exit);
  1193. #endif
  1194. module_init(iop_adma_init);
  1195. MODULE_AUTHOR("Intel Corporation");
  1196. MODULE_DESCRIPTION("IOP ADMA Engine Driver");
  1197. MODULE_LICENSE("GPL");