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