jazzdma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Mips Jazz DMA controller support
  4. * Copyright (C) 1995, 1996 by Andreas Busse
  5. *
  6. * NOTE: Some of the argument checking could be removed when
  7. * things have settled down. Also, instead of returning 0xffffffff
  8. * on failure of vdma_alloc() one could leave page #0 unused
  9. * and return the more usual NULL pointer as logical address.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/export.h>
  14. #include <linux/errno.h>
  15. #include <linux/mm.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/gfp.h>
  19. #include <linux/dma-direct.h>
  20. #include <linux/dma-noncoherent.h>
  21. #include <asm/mipsregs.h>
  22. #include <asm/jazz.h>
  23. #include <asm/io.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/dma.h>
  26. #include <asm/jazzdma.h>
  27. #include <asm/pgtable.h>
  28. /*
  29. * Set this to one to enable additional vdma debug code.
  30. */
  31. #define CONF_DEBUG_VDMA 0
  32. static VDMA_PGTBL_ENTRY *pgtbl;
  33. static DEFINE_SPINLOCK(vdma_lock);
  34. /*
  35. * Debug stuff
  36. */
  37. #define vdma_debug ((CONF_DEBUG_VDMA) ? debuglvl : 0)
  38. static int debuglvl = 3;
  39. /*
  40. * Initialize the pagetable with a one-to-one mapping of
  41. * the first 16 Mbytes of main memory and declare all
  42. * entries to be unused. Using this method will at least
  43. * allow some early device driver operations to work.
  44. */
  45. static inline void vdma_pgtbl_init(void)
  46. {
  47. unsigned long paddr = 0;
  48. int i;
  49. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  50. pgtbl[i].frame = paddr;
  51. pgtbl[i].owner = VDMA_PAGE_EMPTY;
  52. paddr += VDMA_PAGESIZE;
  53. }
  54. }
  55. /*
  56. * Initialize the Jazz R4030 dma controller
  57. */
  58. static int __init vdma_init(void)
  59. {
  60. /*
  61. * Allocate 32k of memory for DMA page tables. This needs to be page
  62. * aligned and should be uncached to avoid cache flushing after every
  63. * update.
  64. */
  65. pgtbl = (VDMA_PGTBL_ENTRY *)__get_free_pages(GFP_KERNEL | GFP_DMA,
  66. get_order(VDMA_PGTBL_SIZE));
  67. BUG_ON(!pgtbl);
  68. dma_cache_wback_inv((unsigned long)pgtbl, VDMA_PGTBL_SIZE);
  69. pgtbl = (VDMA_PGTBL_ENTRY *)CKSEG1ADDR((unsigned long)pgtbl);
  70. /*
  71. * Clear the R4030 translation table
  72. */
  73. vdma_pgtbl_init();
  74. r4030_write_reg32(JAZZ_R4030_TRSTBL_BASE,
  75. CPHYSADDR((unsigned long)pgtbl));
  76. r4030_write_reg32(JAZZ_R4030_TRSTBL_LIM, VDMA_PGTBL_SIZE);
  77. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  78. printk(KERN_INFO "VDMA: R4030 DMA pagetables initialized.\n");
  79. return 0;
  80. }
  81. arch_initcall(vdma_init);
  82. /*
  83. * Allocate DMA pagetables using a simple first-fit algorithm
  84. */
  85. unsigned long vdma_alloc(unsigned long paddr, unsigned long size)
  86. {
  87. int first, last, pages, frame, i;
  88. unsigned long laddr, flags;
  89. /* check arguments */
  90. if (paddr > 0x1fffffff) {
  91. if (vdma_debug)
  92. printk("vdma_alloc: Invalid physical address: %08lx\n",
  93. paddr);
  94. return VDMA_ERROR; /* invalid physical address */
  95. }
  96. if (size > 0x400000 || size == 0) {
  97. if (vdma_debug)
  98. printk("vdma_alloc: Invalid size: %08lx\n", size);
  99. return VDMA_ERROR; /* invalid physical address */
  100. }
  101. spin_lock_irqsave(&vdma_lock, flags);
  102. /*
  103. * Find free chunk
  104. */
  105. pages = VDMA_PAGE(paddr + size) - VDMA_PAGE(paddr) + 1;
  106. first = 0;
  107. while (1) {
  108. while (pgtbl[first].owner != VDMA_PAGE_EMPTY &&
  109. first < VDMA_PGTBL_ENTRIES) first++;
  110. if (first + pages > VDMA_PGTBL_ENTRIES) { /* nothing free */
  111. spin_unlock_irqrestore(&vdma_lock, flags);
  112. return VDMA_ERROR;
  113. }
  114. last = first + 1;
  115. while (pgtbl[last].owner == VDMA_PAGE_EMPTY
  116. && last - first < pages)
  117. last++;
  118. if (last - first == pages)
  119. break; /* found */
  120. first = last + 1;
  121. }
  122. /*
  123. * Mark pages as allocated
  124. */
  125. laddr = (first << 12) + (paddr & (VDMA_PAGESIZE - 1));
  126. frame = paddr & ~(VDMA_PAGESIZE - 1);
  127. for (i = first; i < last; i++) {
  128. pgtbl[i].frame = frame;
  129. pgtbl[i].owner = laddr;
  130. frame += VDMA_PAGESIZE;
  131. }
  132. /*
  133. * Update translation table and return logical start address
  134. */
  135. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  136. if (vdma_debug > 1)
  137. printk("vdma_alloc: Allocated %d pages starting from %08lx\n",
  138. pages, laddr);
  139. if (vdma_debug > 2) {
  140. printk("LADDR: ");
  141. for (i = first; i < last; i++)
  142. printk("%08x ", i << 12);
  143. printk("\nPADDR: ");
  144. for (i = first; i < last; i++)
  145. printk("%08x ", pgtbl[i].frame);
  146. printk("\nOWNER: ");
  147. for (i = first; i < last; i++)
  148. printk("%08x ", pgtbl[i].owner);
  149. printk("\n");
  150. }
  151. spin_unlock_irqrestore(&vdma_lock, flags);
  152. return laddr;
  153. }
  154. EXPORT_SYMBOL(vdma_alloc);
  155. /*
  156. * Free previously allocated dma translation pages
  157. * Note that this does NOT change the translation table,
  158. * it just marks the free'd pages as unused!
  159. */
  160. int vdma_free(unsigned long laddr)
  161. {
  162. int i;
  163. i = laddr >> 12;
  164. if (pgtbl[i].owner != laddr) {
  165. printk
  166. ("vdma_free: trying to free other's dma pages, laddr=%8lx\n",
  167. laddr);
  168. return -1;
  169. }
  170. while (i < VDMA_PGTBL_ENTRIES && pgtbl[i].owner == laddr) {
  171. pgtbl[i].owner = VDMA_PAGE_EMPTY;
  172. i++;
  173. }
  174. if (vdma_debug > 1)
  175. printk("vdma_free: freed %ld pages starting from %08lx\n",
  176. i - (laddr >> 12), laddr);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(vdma_free);
  180. /*
  181. * Map certain page(s) to another physical address.
  182. * Caller must have allocated the page(s) before.
  183. */
  184. int vdma_remap(unsigned long laddr, unsigned long paddr, unsigned long size)
  185. {
  186. int first, pages;
  187. if (laddr > 0xffffff) {
  188. if (vdma_debug)
  189. printk
  190. ("vdma_map: Invalid logical address: %08lx\n",
  191. laddr);
  192. return -EINVAL; /* invalid logical address */
  193. }
  194. if (paddr > 0x1fffffff) {
  195. if (vdma_debug)
  196. printk
  197. ("vdma_map: Invalid physical address: %08lx\n",
  198. paddr);
  199. return -EINVAL; /* invalid physical address */
  200. }
  201. pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  202. first = laddr >> 12;
  203. if (vdma_debug)
  204. printk("vdma_remap: first=%x, pages=%x\n", first, pages);
  205. if (first + pages > VDMA_PGTBL_ENTRIES) {
  206. if (vdma_debug)
  207. printk("vdma_alloc: Invalid size: %08lx\n", size);
  208. return -EINVAL;
  209. }
  210. paddr &= ~(VDMA_PAGESIZE - 1);
  211. while (pages > 0 && first < VDMA_PGTBL_ENTRIES) {
  212. if (pgtbl[first].owner != laddr) {
  213. if (vdma_debug)
  214. printk("Trying to remap other's pages.\n");
  215. return -EPERM; /* not owner */
  216. }
  217. pgtbl[first].frame = paddr;
  218. paddr += VDMA_PAGESIZE;
  219. first++;
  220. pages--;
  221. }
  222. /*
  223. * Update translation table
  224. */
  225. r4030_write_reg32(JAZZ_R4030_TRSTBL_INV, 0);
  226. if (vdma_debug > 2) {
  227. int i;
  228. pages = (((paddr & (VDMA_PAGESIZE - 1)) + size) >> 12) + 1;
  229. first = laddr >> 12;
  230. printk("LADDR: ");
  231. for (i = first; i < first + pages; i++)
  232. printk("%08x ", i << 12);
  233. printk("\nPADDR: ");
  234. for (i = first; i < first + pages; i++)
  235. printk("%08x ", pgtbl[i].frame);
  236. printk("\nOWNER: ");
  237. for (i = first; i < first + pages; i++)
  238. printk("%08x ", pgtbl[i].owner);
  239. printk("\n");
  240. }
  241. return 0;
  242. }
  243. /*
  244. * Translate a physical address to a logical address.
  245. * This will return the logical address of the first
  246. * match.
  247. */
  248. unsigned long vdma_phys2log(unsigned long paddr)
  249. {
  250. int i;
  251. int frame;
  252. frame = paddr & ~(VDMA_PAGESIZE - 1);
  253. for (i = 0; i < VDMA_PGTBL_ENTRIES; i++) {
  254. if (pgtbl[i].frame == frame)
  255. break;
  256. }
  257. if (i == VDMA_PGTBL_ENTRIES)
  258. return ~0UL;
  259. return (i << 12) + (paddr & (VDMA_PAGESIZE - 1));
  260. }
  261. EXPORT_SYMBOL(vdma_phys2log);
  262. /*
  263. * Translate a logical DMA address to a physical address
  264. */
  265. unsigned long vdma_log2phys(unsigned long laddr)
  266. {
  267. return pgtbl[laddr >> 12].frame + (laddr & (VDMA_PAGESIZE - 1));
  268. }
  269. EXPORT_SYMBOL(vdma_log2phys);
  270. /*
  271. * Print DMA statistics
  272. */
  273. void vdma_stats(void)
  274. {
  275. int i;
  276. printk("vdma_stats: CONFIG: %08x\n",
  277. r4030_read_reg32(JAZZ_R4030_CONFIG));
  278. printk("R4030 translation table base: %08x\n",
  279. r4030_read_reg32(JAZZ_R4030_TRSTBL_BASE));
  280. printk("R4030 translation table limit: %08x\n",
  281. r4030_read_reg32(JAZZ_R4030_TRSTBL_LIM));
  282. printk("vdma_stats: INV_ADDR: %08x\n",
  283. r4030_read_reg32(JAZZ_R4030_INV_ADDR));
  284. printk("vdma_stats: R_FAIL_ADDR: %08x\n",
  285. r4030_read_reg32(JAZZ_R4030_R_FAIL_ADDR));
  286. printk("vdma_stats: M_FAIL_ADDR: %08x\n",
  287. r4030_read_reg32(JAZZ_R4030_M_FAIL_ADDR));
  288. printk("vdma_stats: IRQ_SOURCE: %08x\n",
  289. r4030_read_reg32(JAZZ_R4030_IRQ_SOURCE));
  290. printk("vdma_stats: I386_ERROR: %08x\n",
  291. r4030_read_reg32(JAZZ_R4030_I386_ERROR));
  292. printk("vdma_chnl_modes: ");
  293. for (i = 0; i < 8; i++)
  294. printk("%04x ",
  295. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  296. (i << 5)));
  297. printk("\n");
  298. printk("vdma_chnl_enables: ");
  299. for (i = 0; i < 8; i++)
  300. printk("%04x ",
  301. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  302. (i << 5)));
  303. printk("\n");
  304. }
  305. /*
  306. * DMA transfer functions
  307. */
  308. /*
  309. * Enable a DMA channel. Also clear any error conditions.
  310. */
  311. void vdma_enable(int channel)
  312. {
  313. int status;
  314. if (vdma_debug)
  315. printk("vdma_enable: channel %d\n", channel);
  316. /*
  317. * Check error conditions first
  318. */
  319. status = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  320. if (status & 0x400)
  321. printk("VDMA: Channel %d: Address error!\n", channel);
  322. if (status & 0x200)
  323. printk("VDMA: Channel %d: Memory error!\n", channel);
  324. /*
  325. * Clear all interrupt flags
  326. */
  327. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  328. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  329. (channel << 5)) | R4030_TC_INTR
  330. | R4030_MEM_INTR | R4030_ADDR_INTR);
  331. /*
  332. * Enable the desired channel
  333. */
  334. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  335. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  336. (channel << 5)) |
  337. R4030_CHNL_ENABLE);
  338. }
  339. EXPORT_SYMBOL(vdma_enable);
  340. /*
  341. * Disable a DMA channel
  342. */
  343. void vdma_disable(int channel)
  344. {
  345. if (vdma_debug) {
  346. int status =
  347. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  348. (channel << 5));
  349. printk("vdma_disable: channel %d\n", channel);
  350. printk("VDMA: channel %d status: %04x (%s) mode: "
  351. "%02x addr: %06x count: %06x\n",
  352. channel, status,
  353. ((status & 0x600) ? "ERROR" : "OK"),
  354. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_MODE +
  355. (channel << 5)),
  356. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_ADDR +
  357. (channel << 5)),
  358. (unsigned) r4030_read_reg32(JAZZ_R4030_CHNL_COUNT +
  359. (channel << 5)));
  360. }
  361. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  362. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  363. (channel << 5)) &
  364. ~R4030_CHNL_ENABLE);
  365. /*
  366. * After disabling a DMA channel a remote bus register should be
  367. * read to ensure that the current DMA acknowledge cycle is completed.
  368. */
  369. *((volatile unsigned int *) JAZZ_DUMMY_DEVICE);
  370. }
  371. EXPORT_SYMBOL(vdma_disable);
  372. /*
  373. * Set DMA mode. This function accepts the mode values used
  374. * to set a PC-style DMA controller. For the SCSI and FDC
  375. * channels, we also set the default modes each time we're
  376. * called.
  377. * NOTE: The FAST and BURST dma modes are supported by the
  378. * R4030 Rev. 2 and PICA chipsets only. I leave them disabled
  379. * for now.
  380. */
  381. void vdma_set_mode(int channel, int mode)
  382. {
  383. if (vdma_debug)
  384. printk("vdma_set_mode: channel %d, mode 0x%x\n", channel,
  385. mode);
  386. switch (channel) {
  387. case JAZZ_SCSI_DMA: /* scsi */
  388. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  389. /* R4030_MODE_FAST | */
  390. /* R4030_MODE_BURST | */
  391. R4030_MODE_INTR_EN |
  392. R4030_MODE_WIDTH_16 |
  393. R4030_MODE_ATIME_80);
  394. break;
  395. case JAZZ_FLOPPY_DMA: /* floppy */
  396. r4030_write_reg32(JAZZ_R4030_CHNL_MODE + (channel << 5),
  397. /* R4030_MODE_FAST | */
  398. /* R4030_MODE_BURST | */
  399. R4030_MODE_INTR_EN |
  400. R4030_MODE_WIDTH_8 |
  401. R4030_MODE_ATIME_120);
  402. break;
  403. case JAZZ_AUDIOL_DMA:
  404. case JAZZ_AUDIOR_DMA:
  405. printk("VDMA: Audio DMA not supported yet.\n");
  406. break;
  407. default:
  408. printk
  409. ("VDMA: vdma_set_mode() called with unsupported channel %d!\n",
  410. channel);
  411. }
  412. switch (mode) {
  413. case DMA_MODE_READ:
  414. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  415. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  416. (channel << 5)) &
  417. ~R4030_CHNL_WRITE);
  418. break;
  419. case DMA_MODE_WRITE:
  420. r4030_write_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5),
  421. r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE +
  422. (channel << 5)) |
  423. R4030_CHNL_WRITE);
  424. break;
  425. default:
  426. printk
  427. ("VDMA: vdma_set_mode() called with unknown dma mode 0x%x\n",
  428. mode);
  429. }
  430. }
  431. EXPORT_SYMBOL(vdma_set_mode);
  432. /*
  433. * Set Transfer Address
  434. */
  435. void vdma_set_addr(int channel, long addr)
  436. {
  437. if (vdma_debug)
  438. printk("vdma_set_addr: channel %d, addr %lx\n", channel,
  439. addr);
  440. r4030_write_reg32(JAZZ_R4030_CHNL_ADDR + (channel << 5), addr);
  441. }
  442. EXPORT_SYMBOL(vdma_set_addr);
  443. /*
  444. * Set Transfer Count
  445. */
  446. void vdma_set_count(int channel, int count)
  447. {
  448. if (vdma_debug)
  449. printk("vdma_set_count: channel %d, count %08x\n", channel,
  450. (unsigned) count);
  451. r4030_write_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5), count);
  452. }
  453. EXPORT_SYMBOL(vdma_set_count);
  454. /*
  455. * Get Residual
  456. */
  457. int vdma_get_residue(int channel)
  458. {
  459. int residual;
  460. residual = r4030_read_reg32(JAZZ_R4030_CHNL_COUNT + (channel << 5));
  461. if (vdma_debug)
  462. printk("vdma_get_residual: channel %d: residual=%d\n",
  463. channel, residual);
  464. return residual;
  465. }
  466. /*
  467. * Get DMA channel enable register
  468. */
  469. int vdma_get_enable(int channel)
  470. {
  471. int enable;
  472. enable = r4030_read_reg32(JAZZ_R4030_CHNL_ENABLE + (channel << 5));
  473. if (vdma_debug)
  474. printk("vdma_get_enable: channel %d: enable=%d\n", channel,
  475. enable);
  476. return enable;
  477. }
  478. static void *jazz_dma_alloc(struct device *dev, size_t size,
  479. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  480. {
  481. void *ret;
  482. ret = dma_direct_alloc(dev, size, dma_handle, gfp, attrs);
  483. if (!ret)
  484. return NULL;
  485. *dma_handle = vdma_alloc(virt_to_phys(ret), size);
  486. if (*dma_handle == VDMA_ERROR) {
  487. dma_direct_free(dev, size, ret, *dma_handle, attrs);
  488. return NULL;
  489. }
  490. if (!(attrs & DMA_ATTR_NON_CONSISTENT)) {
  491. dma_cache_wback_inv((unsigned long)ret, size);
  492. ret = (void *)UNCAC_ADDR(ret);
  493. }
  494. return ret;
  495. }
  496. static void jazz_dma_free(struct device *dev, size_t size, void *vaddr,
  497. dma_addr_t dma_handle, unsigned long attrs)
  498. {
  499. vdma_free(dma_handle);
  500. if (!(attrs & DMA_ATTR_NON_CONSISTENT))
  501. vaddr = (void *)CAC_ADDR((unsigned long)vaddr);
  502. return dma_direct_free(dev, size, vaddr, dma_handle, attrs);
  503. }
  504. static dma_addr_t jazz_dma_map_page(struct device *dev, struct page *page,
  505. unsigned long offset, size_t size, enum dma_data_direction dir,
  506. unsigned long attrs)
  507. {
  508. phys_addr_t phys = page_to_phys(page) + offset;
  509. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  510. arch_sync_dma_for_device(dev, phys, size, dir);
  511. return vdma_alloc(phys, size);
  512. }
  513. static void jazz_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  514. size_t size, enum dma_data_direction dir, unsigned long attrs)
  515. {
  516. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  517. arch_sync_dma_for_cpu(dev, vdma_log2phys(dma_addr), size, dir);
  518. vdma_free(dma_addr);
  519. }
  520. static int jazz_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  521. int nents, enum dma_data_direction dir, unsigned long attrs)
  522. {
  523. int i;
  524. struct scatterlist *sg;
  525. for_each_sg(sglist, sg, nents, i) {
  526. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  527. arch_sync_dma_for_device(dev, sg_phys(sg), sg->length,
  528. dir);
  529. sg->dma_address = vdma_alloc(sg_phys(sg), sg->length);
  530. if (sg->dma_address == VDMA_ERROR)
  531. return 0;
  532. sg_dma_len(sg) = sg->length;
  533. }
  534. return nents;
  535. }
  536. static void jazz_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  537. int nents, enum dma_data_direction dir, unsigned long attrs)
  538. {
  539. int i;
  540. struct scatterlist *sg;
  541. for_each_sg(sglist, sg, nents, i) {
  542. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  543. arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length,
  544. dir);
  545. vdma_free(sg->dma_address);
  546. }
  547. }
  548. static void jazz_dma_sync_single_for_device(struct device *dev,
  549. dma_addr_t addr, size_t size, enum dma_data_direction dir)
  550. {
  551. arch_sync_dma_for_device(dev, vdma_log2phys(addr), size, dir);
  552. }
  553. static void jazz_dma_sync_single_for_cpu(struct device *dev,
  554. dma_addr_t addr, size_t size, enum dma_data_direction dir)
  555. {
  556. arch_sync_dma_for_cpu(dev, vdma_log2phys(addr), size, dir);
  557. }
  558. static void jazz_dma_sync_sg_for_device(struct device *dev,
  559. struct scatterlist *sgl, int nents, enum dma_data_direction dir)
  560. {
  561. struct scatterlist *sg;
  562. int i;
  563. for_each_sg(sgl, sg, nents, i)
  564. arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
  565. }
  566. static void jazz_dma_sync_sg_for_cpu(struct device *dev,
  567. struct scatterlist *sgl, int nents, enum dma_data_direction dir)
  568. {
  569. struct scatterlist *sg;
  570. int i;
  571. for_each_sg(sgl, sg, nents, i)
  572. arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
  573. }
  574. static int jazz_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  575. {
  576. return dma_addr == VDMA_ERROR;
  577. }
  578. const struct dma_map_ops jazz_dma_ops = {
  579. .alloc = jazz_dma_alloc,
  580. .free = jazz_dma_free,
  581. .mmap = arch_dma_mmap,
  582. .map_page = jazz_dma_map_page,
  583. .unmap_page = jazz_dma_unmap_page,
  584. .map_sg = jazz_dma_map_sg,
  585. .unmap_sg = jazz_dma_unmap_sg,
  586. .sync_single_for_cpu = jazz_dma_sync_single_for_cpu,
  587. .sync_single_for_device = jazz_dma_sync_single_for_device,
  588. .sync_sg_for_cpu = jazz_dma_sync_sg_for_cpu,
  589. .sync_sg_for_device = jazz_dma_sync_sg_for_device,
  590. .dma_supported = dma_direct_supported,
  591. .cache_sync = arch_dma_cache_sync,
  592. .mapping_error = jazz_dma_mapping_error,
  593. };
  594. EXPORT_SYMBOL(jazz_dma_ops);