iop-adma.c 40 KB

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