iop-adma.c 40 KB

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