iop-adma.c 40 KB

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