iop-adma.c 40 KB

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