iop-adma.c 40 KB

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