jazzdma.c 17 KB

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