iop-adma.c 40 KB

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