iop-adma.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * offload engine driver for the Intel Xscale series of i/o processors
  3. * Copyright © 2006, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. /*
  20. * This driver supports the asynchrounous DMA copy and RAID engines available
  21. * on the Intel Xscale(R) family of I/O Processors (IOP 32x, 33x, 134x)
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/async_tx.h>
  26. #include <linux/delay.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/memory.h>
  32. #include <linux/ioport.h>
  33. #include <asm/arch/adma.h>
  34. #define to_iop_adma_chan(chan) container_of(chan, struct iop_adma_chan, common)
  35. #define to_iop_adma_device(dev) \
  36. container_of(dev, struct iop_adma_device, common)
  37. #define tx_to_iop_adma_slot(tx) \
  38. container_of(tx, struct iop_adma_desc_slot, async_tx)
  39. /**
  40. * iop_adma_free_slots - flags descriptor slots for reuse
  41. * @slot: Slot to free
  42. * Caller must hold &iop_chan->lock while calling this function
  43. */
  44. static void iop_adma_free_slots(struct iop_adma_desc_slot *slot)
  45. {
  46. int stride = slot->slots_per_op;
  47. while (stride--) {
  48. slot->slots_per_op = 0;
  49. slot = list_entry(slot->slot_node.next,
  50. struct iop_adma_desc_slot,
  51. slot_node);
  52. }
  53. }
  54. static dma_cookie_t
  55. iop_adma_run_tx_complete_actions(struct iop_adma_desc_slot *desc,
  56. struct iop_adma_chan *iop_chan, dma_cookie_t cookie)
  57. {
  58. BUG_ON(desc->async_tx.cookie < 0);
  59. if (desc->async_tx.cookie > 0) {
  60. cookie = desc->async_tx.cookie;
  61. desc->async_tx.cookie = 0;
  62. /* call the callback (must not sleep or submit new
  63. * operations to this channel)
  64. */
  65. if (desc->async_tx.callback)
  66. desc->async_tx.callback(
  67. desc->async_tx.callback_param);
  68. /* unmap dma addresses
  69. * (unmap_single vs unmap_page?)
  70. */
  71. if (desc->group_head && desc->unmap_len) {
  72. struct iop_adma_desc_slot *unmap = desc->group_head;
  73. struct device *dev =
  74. &iop_chan->device->pdev->dev;
  75. u32 len = unmap->unmap_len;
  76. u32 src_cnt = unmap->unmap_src_cnt;
  77. dma_addr_t addr = iop_desc_get_dest_addr(unmap,
  78. iop_chan);
  79. dma_unmap_page(dev, addr, len, DMA_FROM_DEVICE);
  80. while (src_cnt--) {
  81. addr = iop_desc_get_src_addr(unmap,
  82. iop_chan,
  83. src_cnt);
  84. dma_unmap_page(dev, addr, len,
  85. DMA_TO_DEVICE);
  86. }
  87. desc->group_head = NULL;
  88. }
  89. }
  90. /* run dependent operations */
  91. async_tx_run_dependencies(&desc->async_tx);
  92. return cookie;
  93. }
  94. static int
  95. iop_adma_clean_slot(struct iop_adma_desc_slot *desc,
  96. struct iop_adma_chan *iop_chan)
  97. {
  98. /* the client is allowed to attach dependent operations
  99. * until 'ack' is set
  100. */
  101. if (!desc->async_tx.ack)
  102. return 0;
  103. /* leave the last descriptor in the chain
  104. * so we can append to it
  105. */
  106. if (desc->chain_node.next == &iop_chan->chain)
  107. return 1;
  108. dev_dbg(iop_chan->device->common.dev,
  109. "\tfree slot: %d slots_per_op: %d\n",
  110. desc->idx, desc->slots_per_op);
  111. list_del(&desc->chain_node);
  112. iop_adma_free_slots(desc);
  113. return 0;
  114. }
  115. static void __iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
  116. {
  117. struct iop_adma_desc_slot *iter, *_iter, *grp_start = NULL;
  118. dma_cookie_t cookie = 0;
  119. u32 current_desc = iop_chan_get_current_descriptor(iop_chan);
  120. int busy = iop_chan_is_busy(iop_chan);
  121. int seen_current = 0, slot_cnt = 0, slots_per_op = 0;
  122. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  123. /* free completed slots from the chain starting with
  124. * the oldest descriptor
  125. */
  126. list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
  127. chain_node) {
  128. pr_debug("\tcookie: %d slot: %d busy: %d "
  129. "this_desc: %#x next_desc: %#x ack: %d\n",
  130. iter->async_tx.cookie, iter->idx, busy,
  131. iter->async_tx.phys, iop_desc_get_next_desc(iter),
  132. iter->async_tx.ack);
  133. prefetch(_iter);
  134. prefetch(&_iter->async_tx);
  135. /* do not advance past the current descriptor loaded into the
  136. * hardware channel, subsequent descriptors are either in
  137. * process or have not been submitted
  138. */
  139. if (seen_current)
  140. break;
  141. /* stop the search if we reach the current descriptor and the
  142. * channel is busy, or if it appears that the current descriptor
  143. * needs to be re-read (i.e. has been appended to)
  144. */
  145. if (iter->async_tx.phys == current_desc) {
  146. BUG_ON(seen_current++);
  147. if (busy || iop_desc_get_next_desc(iter))
  148. break;
  149. }
  150. /* detect the start of a group transaction */
  151. if (!slot_cnt && !slots_per_op) {
  152. slot_cnt = iter->slot_cnt;
  153. slots_per_op = iter->slots_per_op;
  154. if (slot_cnt <= slots_per_op) {
  155. slot_cnt = 0;
  156. slots_per_op = 0;
  157. }
  158. }
  159. if (slot_cnt) {
  160. pr_debug("\tgroup++\n");
  161. if (!grp_start)
  162. grp_start = iter;
  163. slot_cnt -= slots_per_op;
  164. }
  165. /* all the members of a group are complete */
  166. if (slots_per_op != 0 && slot_cnt == 0) {
  167. struct iop_adma_desc_slot *grp_iter, *_grp_iter;
  168. int end_of_chain = 0;
  169. pr_debug("\tgroup end\n");
  170. /* collect the total results */
  171. if (grp_start->xor_check_result) {
  172. u32 zero_sum_result = 0;
  173. slot_cnt = grp_start->slot_cnt;
  174. grp_iter = grp_start;
  175. list_for_each_entry_from(grp_iter,
  176. &iop_chan->chain, chain_node) {
  177. zero_sum_result |=
  178. iop_desc_get_zero_result(grp_iter);
  179. pr_debug("\titer%d result: %d\n",
  180. grp_iter->idx, zero_sum_result);
  181. slot_cnt -= slots_per_op;
  182. if (slot_cnt == 0)
  183. break;
  184. }
  185. pr_debug("\tgrp_start->xor_check_result: %p\n",
  186. grp_start->xor_check_result);
  187. *grp_start->xor_check_result = zero_sum_result;
  188. }
  189. /* clean up the group */
  190. slot_cnt = grp_start->slot_cnt;
  191. grp_iter = grp_start;
  192. list_for_each_entry_safe_from(grp_iter, _grp_iter,
  193. &iop_chan->chain, chain_node) {
  194. cookie = iop_adma_run_tx_complete_actions(
  195. grp_iter, iop_chan, cookie);
  196. slot_cnt -= slots_per_op;
  197. end_of_chain = iop_adma_clean_slot(grp_iter,
  198. iop_chan);
  199. if (slot_cnt == 0 || end_of_chain)
  200. break;
  201. }
  202. /* the group should be complete at this point */
  203. BUG_ON(slot_cnt);
  204. slots_per_op = 0;
  205. grp_start = NULL;
  206. if (end_of_chain)
  207. break;
  208. else
  209. continue;
  210. } else if (slots_per_op) /* wait for group completion */
  211. continue;
  212. /* write back zero sum results (single descriptor case) */
  213. if (iter->xor_check_result && iter->async_tx.cookie)
  214. *iter->xor_check_result =
  215. iop_desc_get_zero_result(iter);
  216. cookie = iop_adma_run_tx_complete_actions(
  217. iter, iop_chan, cookie);
  218. if (iop_adma_clean_slot(iter, iop_chan))
  219. break;
  220. }
  221. BUG_ON(!seen_current);
  222. iop_chan_idle(busy, iop_chan);
  223. if (cookie > 0) {
  224. iop_chan->completed_cookie = cookie;
  225. pr_debug("\tcompleted cookie %d\n", cookie);
  226. }
  227. }
  228. static void
  229. iop_adma_slot_cleanup(struct iop_adma_chan *iop_chan)
  230. {
  231. spin_lock_bh(&iop_chan->lock);
  232. __iop_adma_slot_cleanup(iop_chan);
  233. spin_unlock_bh(&iop_chan->lock);
  234. }
  235. static void iop_adma_tasklet(unsigned long data)
  236. {
  237. struct iop_adma_chan *iop_chan = (struct iop_adma_chan *) data;
  238. spin_lock(&iop_chan->lock);
  239. __iop_adma_slot_cleanup(iop_chan);
  240. spin_unlock(&iop_chan->lock);
  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. __func__, 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", __func__);
  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. __func__, 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. __func__, 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. __func__, src_cnt, len, flags);
  530. spin_lock_bh(&iop_chan->lock);
  531. slot_cnt = iop_chan_xor_slot_count(len, src_cnt, &slots_per_op);
  532. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  533. if (sw_desc) {
  534. grp_start = sw_desc->group_head;
  535. iop_desc_init_xor(grp_start, src_cnt, flags);
  536. iop_desc_set_byte_count(grp_start, iop_chan, len);
  537. iop_desc_set_dest_addr(grp_start, iop_chan, dma_dest);
  538. sw_desc->unmap_src_cnt = src_cnt;
  539. sw_desc->unmap_len = len;
  540. 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. __func__, 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. __func__, 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_free_chan_resources(struct dma_chan *chan)
  579. {
  580. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  581. struct iop_adma_desc_slot *iter, *_iter;
  582. int in_use_descs = 0;
  583. iop_adma_slot_cleanup(iop_chan);
  584. spin_lock_bh(&iop_chan->lock);
  585. list_for_each_entry_safe(iter, _iter, &iop_chan->chain,
  586. chain_node) {
  587. in_use_descs++;
  588. list_del(&iter->chain_node);
  589. }
  590. list_for_each_entry_safe_reverse(
  591. iter, _iter, &iop_chan->all_slots, slot_node) {
  592. list_del(&iter->slot_node);
  593. kfree(iter);
  594. iop_chan->slots_allocated--;
  595. }
  596. iop_chan->last_used = NULL;
  597. dev_dbg(iop_chan->device->common.dev, "%s slots_allocated %d\n",
  598. __func__, iop_chan->slots_allocated);
  599. spin_unlock_bh(&iop_chan->lock);
  600. /* one is ok since we left it on there on purpose */
  601. if (in_use_descs > 1)
  602. printk(KERN_ERR "IOP: Freeing %d in use descriptors!\n",
  603. in_use_descs - 1);
  604. }
  605. /**
  606. * iop_adma_is_complete - poll the status of an ADMA transaction
  607. * @chan: ADMA channel handle
  608. * @cookie: ADMA transaction identifier
  609. */
  610. static enum dma_status iop_adma_is_complete(struct dma_chan *chan,
  611. dma_cookie_t cookie,
  612. dma_cookie_t *done,
  613. dma_cookie_t *used)
  614. {
  615. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  616. dma_cookie_t last_used;
  617. dma_cookie_t last_complete;
  618. enum dma_status ret;
  619. last_used = chan->cookie;
  620. last_complete = iop_chan->completed_cookie;
  621. if (done)
  622. *done = last_complete;
  623. if (used)
  624. *used = last_used;
  625. ret = dma_async_is_complete(cookie, last_complete, last_used);
  626. if (ret == DMA_SUCCESS)
  627. return ret;
  628. iop_adma_slot_cleanup(iop_chan);
  629. last_used = chan->cookie;
  630. last_complete = iop_chan->completed_cookie;
  631. if (done)
  632. *done = last_complete;
  633. if (used)
  634. *used = last_used;
  635. return dma_async_is_complete(cookie, last_complete, last_used);
  636. }
  637. static irqreturn_t iop_adma_eot_handler(int irq, void *data)
  638. {
  639. struct iop_adma_chan *chan = data;
  640. dev_dbg(chan->device->common.dev, "%s\n", __func__);
  641. tasklet_schedule(&chan->irq_tasklet);
  642. iop_adma_device_clear_eot_status(chan);
  643. return IRQ_HANDLED;
  644. }
  645. static irqreturn_t iop_adma_eoc_handler(int irq, void *data)
  646. {
  647. struct iop_adma_chan *chan = data;
  648. dev_dbg(chan->device->common.dev, "%s\n", __func__);
  649. tasklet_schedule(&chan->irq_tasklet);
  650. iop_adma_device_clear_eoc_status(chan);
  651. return IRQ_HANDLED;
  652. }
  653. static irqreturn_t iop_adma_err_handler(int irq, void *data)
  654. {
  655. struct iop_adma_chan *chan = data;
  656. unsigned long status = iop_chan_get_status(chan);
  657. dev_printk(KERN_ERR, chan->device->common.dev,
  658. "error ( %s%s%s%s%s%s%s)\n",
  659. iop_is_err_int_parity(status, chan) ? "int_parity " : "",
  660. iop_is_err_mcu_abort(status, chan) ? "mcu_abort " : "",
  661. iop_is_err_int_tabort(status, chan) ? "int_tabort " : "",
  662. iop_is_err_int_mabort(status, chan) ? "int_mabort " : "",
  663. iop_is_err_pci_tabort(status, chan) ? "pci_tabort " : "",
  664. iop_is_err_pci_mabort(status, chan) ? "pci_mabort " : "",
  665. iop_is_err_split_tx(status, chan) ? "split_tx " : "");
  666. iop_adma_device_clear_err_status(chan);
  667. BUG();
  668. return IRQ_HANDLED;
  669. }
  670. static void iop_adma_issue_pending(struct dma_chan *chan)
  671. {
  672. struct iop_adma_chan *iop_chan = to_iop_adma_chan(chan);
  673. if (iop_chan->pending) {
  674. iop_chan->pending = 0;
  675. iop_chan_append(iop_chan);
  676. }
  677. }
  678. /*
  679. * Perform a transaction to verify the HW works.
  680. */
  681. #define IOP_ADMA_TEST_SIZE 2000
  682. static int __devinit iop_adma_memcpy_self_test(struct iop_adma_device *device)
  683. {
  684. int i;
  685. void *src, *dest;
  686. dma_addr_t src_dma, dest_dma;
  687. struct dma_chan *dma_chan;
  688. dma_cookie_t cookie;
  689. struct dma_async_tx_descriptor *tx;
  690. int err = 0;
  691. struct iop_adma_chan *iop_chan;
  692. dev_dbg(device->common.dev, "%s\n", __func__);
  693. src = kzalloc(sizeof(u8) * IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  694. if (!src)
  695. return -ENOMEM;
  696. dest = kzalloc(sizeof(u8) * IOP_ADMA_TEST_SIZE, GFP_KERNEL);
  697. if (!dest) {
  698. kfree(src);
  699. return -ENOMEM;
  700. }
  701. /* Fill in src buffer */
  702. for (i = 0; i < IOP_ADMA_TEST_SIZE; i++)
  703. ((u8 *) src)[i] = (u8)i;
  704. memset(dest, 0, IOP_ADMA_TEST_SIZE);
  705. /* Start copy, using first DMA channel */
  706. dma_chan = container_of(device->common.channels.next,
  707. struct dma_chan,
  708. device_node);
  709. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  710. err = -ENODEV;
  711. goto out;
  712. }
  713. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  714. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  715. src_dma = dma_map_single(dma_chan->device->dev, src,
  716. IOP_ADMA_TEST_SIZE, DMA_TO_DEVICE);
  717. tx = iop_adma_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  718. IOP_ADMA_TEST_SIZE, 1);
  719. cookie = iop_adma_tx_submit(tx);
  720. iop_adma_issue_pending(dma_chan);
  721. async_tx_ack(tx);
  722. msleep(1);
  723. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
  724. DMA_SUCCESS) {
  725. dev_printk(KERN_ERR, dma_chan->device->dev,
  726. "Self-test copy timed out, disabling\n");
  727. err = -ENODEV;
  728. goto free_resources;
  729. }
  730. iop_chan = to_iop_adma_chan(dma_chan);
  731. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  732. IOP_ADMA_TEST_SIZE, DMA_FROM_DEVICE);
  733. if (memcmp(src, dest, IOP_ADMA_TEST_SIZE)) {
  734. dev_printk(KERN_ERR, dma_chan->device->dev,
  735. "Self-test copy failed compare, disabling\n");
  736. err = -ENODEV;
  737. goto free_resources;
  738. }
  739. free_resources:
  740. iop_adma_free_chan_resources(dma_chan);
  741. out:
  742. kfree(src);
  743. kfree(dest);
  744. return err;
  745. }
  746. #define IOP_ADMA_NUM_SRC_TEST 4 /* must be <= 15 */
  747. static int __devinit
  748. iop_adma_xor_zero_sum_self_test(struct iop_adma_device *device)
  749. {
  750. int i, src_idx;
  751. struct page *dest;
  752. struct page *xor_srcs[IOP_ADMA_NUM_SRC_TEST];
  753. struct page *zero_sum_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  754. dma_addr_t dma_srcs[IOP_ADMA_NUM_SRC_TEST + 1];
  755. dma_addr_t dma_addr, dest_dma;
  756. struct dma_async_tx_descriptor *tx;
  757. struct dma_chan *dma_chan;
  758. dma_cookie_t cookie;
  759. u8 cmp_byte = 0;
  760. u32 cmp_word;
  761. u32 zero_sum_result;
  762. int err = 0;
  763. struct iop_adma_chan *iop_chan;
  764. dev_dbg(device->common.dev, "%s\n", __func__);
  765. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  766. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  767. if (!xor_srcs[src_idx])
  768. while (src_idx--) {
  769. __free_page(xor_srcs[src_idx]);
  770. return -ENOMEM;
  771. }
  772. }
  773. dest = alloc_page(GFP_KERNEL);
  774. if (!dest)
  775. while (src_idx--) {
  776. __free_page(xor_srcs[src_idx]);
  777. return -ENOMEM;
  778. }
  779. /* Fill in src buffers */
  780. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++) {
  781. u8 *ptr = page_address(xor_srcs[src_idx]);
  782. for (i = 0; i < PAGE_SIZE; i++)
  783. ptr[i] = (1 << src_idx);
  784. }
  785. for (src_idx = 0; src_idx < IOP_ADMA_NUM_SRC_TEST; src_idx++)
  786. cmp_byte ^= (u8) (1 << src_idx);
  787. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  788. (cmp_byte << 8) | cmp_byte;
  789. memset(page_address(dest), 0, PAGE_SIZE);
  790. dma_chan = container_of(device->common.channels.next,
  791. struct dma_chan,
  792. device_node);
  793. if (iop_adma_alloc_chan_resources(dma_chan) < 1) {
  794. err = -ENODEV;
  795. goto out;
  796. }
  797. /* test xor */
  798. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0,
  799. PAGE_SIZE, DMA_FROM_DEVICE);
  800. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  801. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  802. 0, PAGE_SIZE, DMA_TO_DEVICE);
  803. tx = iop_adma_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  804. IOP_ADMA_NUM_SRC_TEST, PAGE_SIZE, 1);
  805. cookie = iop_adma_tx_submit(tx);
  806. iop_adma_issue_pending(dma_chan);
  807. async_tx_ack(tx);
  808. msleep(8);
  809. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) !=
  810. DMA_SUCCESS) {
  811. dev_printk(KERN_ERR, dma_chan->device->dev,
  812. "Self-test xor timed out, disabling\n");
  813. err = -ENODEV;
  814. goto free_resources;
  815. }
  816. iop_chan = to_iop_adma_chan(dma_chan);
  817. dma_sync_single_for_cpu(&iop_chan->device->pdev->dev, dest_dma,
  818. PAGE_SIZE, DMA_FROM_DEVICE);
  819. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  820. u32 *ptr = page_address(dest);
  821. if (ptr[i] != cmp_word) {
  822. dev_printk(KERN_ERR, dma_chan->device->dev,
  823. "Self-test xor failed compare, disabling\n");
  824. err = -ENODEV;
  825. goto free_resources;
  826. }
  827. }
  828. dma_sync_single_for_device(&iop_chan->device->pdev->dev, dest_dma,
  829. PAGE_SIZE, DMA_TO_DEVICE);
  830. /* skip zero sum if the capability is not present */
  831. if (!dma_has_cap(DMA_ZERO_SUM, dma_chan->device->cap_mask))
  832. goto free_resources;
  833. /* zero sum the sources with the destintation page */
  834. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST; i++)
  835. zero_sum_srcs[i] = xor_srcs[i];
  836. zero_sum_srcs[i] = dest;
  837. zero_sum_result = 1;
  838. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  839. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  840. zero_sum_srcs[i], 0, PAGE_SIZE,
  841. DMA_TO_DEVICE);
  842. tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
  843. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  844. &zero_sum_result, 1);
  845. cookie = iop_adma_tx_submit(tx);
  846. iop_adma_issue_pending(dma_chan);
  847. async_tx_ack(tx);
  848. msleep(8);
  849. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  850. dev_printk(KERN_ERR, dma_chan->device->dev,
  851. "Self-test zero sum timed out, disabling\n");
  852. err = -ENODEV;
  853. goto free_resources;
  854. }
  855. if (zero_sum_result != 0) {
  856. dev_printk(KERN_ERR, dma_chan->device->dev,
  857. "Self-test zero sum failed compare, disabling\n");
  858. err = -ENODEV;
  859. goto free_resources;
  860. }
  861. /* test memset */
  862. dma_addr = dma_map_page(dma_chan->device->dev, dest, 0,
  863. PAGE_SIZE, DMA_FROM_DEVICE);
  864. tx = iop_adma_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE, 1);
  865. cookie = iop_adma_tx_submit(tx);
  866. iop_adma_issue_pending(dma_chan);
  867. async_tx_ack(tx);
  868. msleep(8);
  869. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  870. dev_printk(KERN_ERR, dma_chan->device->dev,
  871. "Self-test memset timed out, disabling\n");
  872. err = -ENODEV;
  873. goto free_resources;
  874. }
  875. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  876. u32 *ptr = page_address(dest);
  877. if (ptr[i]) {
  878. dev_printk(KERN_ERR, dma_chan->device->dev,
  879. "Self-test memset failed compare, disabling\n");
  880. err = -ENODEV;
  881. goto free_resources;
  882. }
  883. }
  884. /* test for non-zero parity sum */
  885. zero_sum_result = 0;
  886. for (i = 0; i < IOP_ADMA_NUM_SRC_TEST + 1; i++)
  887. dma_srcs[i] = dma_map_page(dma_chan->device->dev,
  888. zero_sum_srcs[i], 0, PAGE_SIZE,
  889. DMA_TO_DEVICE);
  890. tx = iop_adma_prep_dma_zero_sum(dma_chan, dma_srcs,
  891. IOP_ADMA_NUM_SRC_TEST + 1, PAGE_SIZE,
  892. &zero_sum_result, 1);
  893. cookie = iop_adma_tx_submit(tx);
  894. iop_adma_issue_pending(dma_chan);
  895. async_tx_ack(tx);
  896. msleep(8);
  897. if (iop_adma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  898. dev_printk(KERN_ERR, dma_chan->device->dev,
  899. "Self-test non-zero sum timed out, disabling\n");
  900. err = -ENODEV;
  901. goto free_resources;
  902. }
  903. if (zero_sum_result != 1) {
  904. dev_printk(KERN_ERR, dma_chan->device->dev,
  905. "Self-test non-zero sum failed compare, disabling\n");
  906. err = -ENODEV;
  907. goto free_resources;
  908. }
  909. free_resources:
  910. iop_adma_free_chan_resources(dma_chan);
  911. out:
  912. src_idx = IOP_ADMA_NUM_SRC_TEST;
  913. while (src_idx--)
  914. __free_page(xor_srcs[src_idx]);
  915. __free_page(dest);
  916. return err;
  917. }
  918. static int __devexit iop_adma_remove(struct platform_device *dev)
  919. {
  920. struct iop_adma_device *device = platform_get_drvdata(dev);
  921. struct dma_chan *chan, *_chan;
  922. struct iop_adma_chan *iop_chan;
  923. int i;
  924. struct iop_adma_platform_data *plat_data = dev->dev.platform_data;
  925. dma_async_device_unregister(&device->common);
  926. for (i = 0; i < 3; i++) {
  927. unsigned int irq;
  928. irq = platform_get_irq(dev, i);
  929. free_irq(irq, device);
  930. }
  931. dma_free_coherent(&dev->dev, plat_data->pool_size,
  932. device->dma_desc_pool_virt, device->dma_desc_pool);
  933. do {
  934. struct resource *res;
  935. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  936. release_mem_region(res->start, res->end - res->start);
  937. } while (0);
  938. list_for_each_entry_safe(chan, _chan, &device->common.channels,
  939. device_node) {
  940. iop_chan = to_iop_adma_chan(chan);
  941. list_del(&chan->device_node);
  942. kfree(iop_chan);
  943. }
  944. kfree(device);
  945. return 0;
  946. }
  947. static int __devinit iop_adma_probe(struct platform_device *pdev)
  948. {
  949. struct resource *res;
  950. int ret = 0, i;
  951. struct iop_adma_device *adev;
  952. struct iop_adma_chan *iop_chan;
  953. struct dma_device *dma_dev;
  954. struct iop_adma_platform_data *plat_data = pdev->dev.platform_data;
  955. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  956. if (!res)
  957. return -ENODEV;
  958. if (!devm_request_mem_region(&pdev->dev, res->start,
  959. res->end - res->start, pdev->name))
  960. return -EBUSY;
  961. adev = kzalloc(sizeof(*adev), GFP_KERNEL);
  962. if (!adev)
  963. return -ENOMEM;
  964. dma_dev = &adev->common;
  965. /* allocate coherent memory for hardware descriptors
  966. * note: writecombine gives slightly better performance, but
  967. * requires that we explicitly flush the writes
  968. */
  969. if ((adev->dma_desc_pool_virt = dma_alloc_writecombine(&pdev->dev,
  970. plat_data->pool_size,
  971. &adev->dma_desc_pool,
  972. GFP_KERNEL)) == NULL) {
  973. ret = -ENOMEM;
  974. goto err_free_adev;
  975. }
  976. dev_dbg(&pdev->dev, "%s: allocted descriptor pool virt %p phys %p\n",
  977. __func__, adev->dma_desc_pool_virt,
  978. (void *) adev->dma_desc_pool);
  979. adev->id = plat_data->hw_id;
  980. /* discover transaction capabilites from the platform data */
  981. dma_dev->cap_mask = plat_data->cap_mask;
  982. adev->pdev = pdev;
  983. platform_set_drvdata(pdev, adev);
  984. INIT_LIST_HEAD(&dma_dev->channels);
  985. /* set base routines */
  986. dma_dev->device_alloc_chan_resources = iop_adma_alloc_chan_resources;
  987. dma_dev->device_free_chan_resources = iop_adma_free_chan_resources;
  988. dma_dev->device_is_tx_complete = iop_adma_is_complete;
  989. dma_dev->device_issue_pending = iop_adma_issue_pending;
  990. dma_dev->dev = &pdev->dev;
  991. /* set prep routines based on capability */
  992. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  993. dma_dev->device_prep_dma_memcpy = iop_adma_prep_dma_memcpy;
  994. if (dma_has_cap(DMA_MEMSET, dma_dev->cap_mask))
  995. dma_dev->device_prep_dma_memset = iop_adma_prep_dma_memset;
  996. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  997. dma_dev->max_xor = iop_adma_get_max_xor();
  998. dma_dev->device_prep_dma_xor = iop_adma_prep_dma_xor;
  999. }
  1000. if (dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask))
  1001. dma_dev->device_prep_dma_zero_sum =
  1002. iop_adma_prep_dma_zero_sum;
  1003. if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
  1004. dma_dev->device_prep_dma_interrupt =
  1005. iop_adma_prep_dma_interrupt;
  1006. iop_chan = kzalloc(sizeof(*iop_chan), GFP_KERNEL);
  1007. if (!iop_chan) {
  1008. ret = -ENOMEM;
  1009. goto err_free_dma;
  1010. }
  1011. iop_chan->device = adev;
  1012. iop_chan->mmr_base = devm_ioremap(&pdev->dev, res->start,
  1013. res->end - res->start);
  1014. if (!iop_chan->mmr_base) {
  1015. ret = -ENOMEM;
  1016. goto err_free_iop_chan;
  1017. }
  1018. tasklet_init(&iop_chan->irq_tasklet, iop_adma_tasklet, (unsigned long)
  1019. iop_chan);
  1020. /* clear errors before enabling interrupts */
  1021. iop_adma_device_clear_err_status(iop_chan);
  1022. for (i = 0; i < 3; i++) {
  1023. irq_handler_t handler[] = { iop_adma_eot_handler,
  1024. iop_adma_eoc_handler,
  1025. iop_adma_err_handler };
  1026. int irq = platform_get_irq(pdev, i);
  1027. if (irq < 0) {
  1028. ret = -ENXIO;
  1029. goto err_free_iop_chan;
  1030. } else {
  1031. ret = devm_request_irq(&pdev->dev, irq,
  1032. handler[i], 0, pdev->name, iop_chan);
  1033. if (ret)
  1034. goto err_free_iop_chan;
  1035. }
  1036. }
  1037. spin_lock_init(&iop_chan->lock);
  1038. init_timer(&iop_chan->cleanup_watchdog);
  1039. iop_chan->cleanup_watchdog.data = (unsigned long) iop_chan;
  1040. iop_chan->cleanup_watchdog.function = iop_adma_tasklet;
  1041. INIT_LIST_HEAD(&iop_chan->chain);
  1042. INIT_LIST_HEAD(&iop_chan->all_slots);
  1043. INIT_RCU_HEAD(&iop_chan->common.rcu);
  1044. iop_chan->common.device = dma_dev;
  1045. list_add_tail(&iop_chan->common.device_node, &dma_dev->channels);
  1046. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  1047. ret = iop_adma_memcpy_self_test(adev);
  1048. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  1049. if (ret)
  1050. goto err_free_iop_chan;
  1051. }
  1052. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask) ||
  1053. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask)) {
  1054. ret = iop_adma_xor_zero_sum_self_test(adev);
  1055. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  1056. if (ret)
  1057. goto err_free_iop_chan;
  1058. }
  1059. dev_printk(KERN_INFO, &pdev->dev, "Intel(R) IOP: "
  1060. "( %s%s%s%s%s%s%s%s%s%s)\n",
  1061. dma_has_cap(DMA_PQ_XOR, dma_dev->cap_mask) ? "pq_xor " : "",
  1062. dma_has_cap(DMA_PQ_UPDATE, dma_dev->cap_mask) ? "pq_update " : "",
  1063. dma_has_cap(DMA_PQ_ZERO_SUM, dma_dev->cap_mask) ? "pq_zero_sum " : "",
  1064. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  1065. dma_has_cap(DMA_DUAL_XOR, dma_dev->cap_mask) ? "dual_xor " : "",
  1066. dma_has_cap(DMA_ZERO_SUM, dma_dev->cap_mask) ? "xor_zero_sum " : "",
  1067. dma_has_cap(DMA_MEMSET, dma_dev->cap_mask) ? "fill " : "",
  1068. dma_has_cap(DMA_MEMCPY_CRC32C, dma_dev->cap_mask) ? "cpy+crc " : "",
  1069. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  1070. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  1071. dma_async_device_register(dma_dev);
  1072. goto out;
  1073. err_free_iop_chan:
  1074. kfree(iop_chan);
  1075. err_free_dma:
  1076. dma_free_coherent(&adev->pdev->dev, plat_data->pool_size,
  1077. adev->dma_desc_pool_virt, adev->dma_desc_pool);
  1078. err_free_adev:
  1079. kfree(adev);
  1080. out:
  1081. return ret;
  1082. }
  1083. static void iop_chan_start_null_memcpy(struct iop_adma_chan *iop_chan)
  1084. {
  1085. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1086. dma_cookie_t cookie;
  1087. int slot_cnt, slots_per_op;
  1088. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1089. spin_lock_bh(&iop_chan->lock);
  1090. slot_cnt = iop_chan_memcpy_slot_count(0, &slots_per_op);
  1091. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1092. if (sw_desc) {
  1093. grp_start = sw_desc->group_head;
  1094. list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
  1095. sw_desc->async_tx.ack = 1;
  1096. iop_desc_init_memcpy(grp_start, 0);
  1097. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1098. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1099. iop_desc_set_memcpy_src_addr(grp_start, 0);
  1100. cookie = iop_chan->common.cookie;
  1101. cookie++;
  1102. if (cookie <= 1)
  1103. cookie = 2;
  1104. /* initialize the completed cookie to be less than
  1105. * the most recently used cookie
  1106. */
  1107. iop_chan->completed_cookie = cookie - 1;
  1108. iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
  1109. /* channel should not be busy */
  1110. BUG_ON(iop_chan_is_busy(iop_chan));
  1111. /* clear any prior error-status bits */
  1112. iop_adma_device_clear_err_status(iop_chan);
  1113. /* disable operation */
  1114. iop_chan_disable(iop_chan);
  1115. /* set the descriptor address */
  1116. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1117. /* 1/ don't add pre-chained descriptors
  1118. * 2/ dummy read to flush next_desc write
  1119. */
  1120. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1121. /* run the descriptor */
  1122. iop_chan_enable(iop_chan);
  1123. } else
  1124. dev_printk(KERN_ERR, iop_chan->device->common.dev,
  1125. "failed to allocate null descriptor\n");
  1126. spin_unlock_bh(&iop_chan->lock);
  1127. }
  1128. static void iop_chan_start_null_xor(struct iop_adma_chan *iop_chan)
  1129. {
  1130. struct iop_adma_desc_slot *sw_desc, *grp_start;
  1131. dma_cookie_t cookie;
  1132. int slot_cnt, slots_per_op;
  1133. dev_dbg(iop_chan->device->common.dev, "%s\n", __func__);
  1134. spin_lock_bh(&iop_chan->lock);
  1135. slot_cnt = iop_chan_xor_slot_count(0, 2, &slots_per_op);
  1136. sw_desc = iop_adma_alloc_slots(iop_chan, slot_cnt, slots_per_op);
  1137. if (sw_desc) {
  1138. grp_start = sw_desc->group_head;
  1139. list_splice_init(&sw_desc->async_tx.tx_list, &iop_chan->chain);
  1140. sw_desc->async_tx.ack = 1;
  1141. iop_desc_init_null_xor(grp_start, 2, 0);
  1142. iop_desc_set_byte_count(grp_start, iop_chan, 0);
  1143. iop_desc_set_dest_addr(grp_start, iop_chan, 0);
  1144. iop_desc_set_xor_src_addr(grp_start, 0, 0);
  1145. iop_desc_set_xor_src_addr(grp_start, 1, 0);
  1146. cookie = iop_chan->common.cookie;
  1147. cookie++;
  1148. if (cookie <= 1)
  1149. cookie = 2;
  1150. /* initialize the completed cookie to be less than
  1151. * the most recently used cookie
  1152. */
  1153. iop_chan->completed_cookie = cookie - 1;
  1154. iop_chan->common.cookie = sw_desc->async_tx.cookie = cookie;
  1155. /* channel should not be busy */
  1156. BUG_ON(iop_chan_is_busy(iop_chan));
  1157. /* clear any prior error-status bits */
  1158. iop_adma_device_clear_err_status(iop_chan);
  1159. /* disable operation */
  1160. iop_chan_disable(iop_chan);
  1161. /* set the descriptor address */
  1162. iop_chan_set_next_descriptor(iop_chan, sw_desc->async_tx.phys);
  1163. /* 1/ don't add pre-chained descriptors
  1164. * 2/ dummy read to flush next_desc write
  1165. */
  1166. BUG_ON(iop_desc_get_next_desc(sw_desc));
  1167. /* run the descriptor */
  1168. iop_chan_enable(iop_chan);
  1169. } else
  1170. dev_printk(KERN_ERR, iop_chan->device->common.dev,
  1171. "failed to allocate null descriptor\n");
  1172. spin_unlock_bh(&iop_chan->lock);
  1173. }
  1174. static struct platform_driver iop_adma_driver = {
  1175. .probe = iop_adma_probe,
  1176. .remove = iop_adma_remove,
  1177. .driver = {
  1178. .owner = THIS_MODULE,
  1179. .name = "iop-adma",
  1180. },
  1181. };
  1182. static int __init iop_adma_init (void)
  1183. {
  1184. return platform_driver_register(&iop_adma_driver);
  1185. }
  1186. /* it's currently unsafe to unload this module */
  1187. #if 0
  1188. static void __exit iop_adma_exit (void)
  1189. {
  1190. platform_driver_unregister(&iop_adma_driver);
  1191. return;
  1192. }
  1193. module_exit(iop_adma_exit);
  1194. #endif
  1195. module_init(iop_adma_init);
  1196. MODULE_AUTHOR("Intel Corporation");
  1197. MODULE_DESCRIPTION("IOP ADMA Engine Driver");
  1198. MODULE_LICENSE("GPL");