iop-adma.c 39 KB

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