dma.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /*
  9. * DMA Coherent API Notes
  10. *
  11. * I/O is inherently non-coherent on ARC. So a coherent DMA buffer is
  12. * implemented by accessing it using a kernel virtual address, with
  13. * Cache bit off in the TLB entry.
  14. *
  15. * The default DMA address == Phy address which is 0x8000_0000 based.
  16. */
  17. #include <linux/dma-mapping.h>
  18. #include <asm/cache.h>
  19. #include <asm/cacheflush.h>
  20. static void *arc_dma_alloc(struct device *dev, size_t size,
  21. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  22. {
  23. unsigned long order = get_order(size);
  24. struct page *page;
  25. phys_addr_t paddr;
  26. void *kvaddr;
  27. int need_coh = 1, need_kvaddr = 0;
  28. page = alloc_pages(gfp, order);
  29. if (!page)
  30. return NULL;
  31. /*
  32. * IOC relies on all data (even coherent DMA data) being in cache
  33. * Thus allocate normal cached memory
  34. *
  35. * The gains with IOC are two pronged:
  36. * -For streaming data, elides need for cache maintenance, saving
  37. * cycles in flush code, and bus bandwidth as all the lines of a
  38. * buffer need to be flushed out to memory
  39. * -For coherent data, Read/Write to buffers terminate early in cache
  40. * (vs. always going to memory - thus are faster)
  41. */
  42. if ((is_isa_arcv2() && ioc_enable) ||
  43. (attrs & DMA_ATTR_NON_CONSISTENT))
  44. need_coh = 0;
  45. /*
  46. * - A coherent buffer needs MMU mapping to enforce non-cachability
  47. * - A highmem page needs a virtual handle (hence MMU mapping)
  48. * independent of cachability
  49. */
  50. if (PageHighMem(page) || need_coh)
  51. need_kvaddr = 1;
  52. /* This is linear addr (0x8000_0000 based) */
  53. paddr = page_to_phys(page);
  54. *dma_handle = plat_phys_to_dma(dev, paddr);
  55. /* This is kernel Virtual address (0x7000_0000 based) */
  56. if (need_kvaddr) {
  57. kvaddr = ioremap_nocache(paddr, size);
  58. if (kvaddr == NULL) {
  59. __free_pages(page, order);
  60. return NULL;
  61. }
  62. } else {
  63. kvaddr = (void *)(u32)paddr;
  64. }
  65. /*
  66. * Evict any existing L1 and/or L2 lines for the backing page
  67. * in case it was used earlier as a normal "cached" page.
  68. * Yeah this bit us - STAR 9000898266
  69. *
  70. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  71. * can't be used to efficiently flush L1 and/or L2 which need paddr
  72. * Currently flush_cache_vmap nukes the L1 cache completely which
  73. * will be optimized as a separate commit
  74. */
  75. if (need_coh)
  76. dma_cache_wback_inv(paddr, size);
  77. return kvaddr;
  78. }
  79. static void arc_dma_free(struct device *dev, size_t size, void *vaddr,
  80. dma_addr_t dma_handle, unsigned long attrs)
  81. {
  82. phys_addr_t paddr = plat_dma_to_phys(dev, dma_handle);
  83. struct page *page = virt_to_page(paddr);
  84. int is_non_coh = 1;
  85. is_non_coh = (attrs & DMA_ATTR_NON_CONSISTENT) ||
  86. (is_isa_arcv2() && ioc_enable);
  87. if (PageHighMem(page) || !is_non_coh)
  88. iounmap((void __force __iomem *)vaddr);
  89. __free_pages(page, get_order(size));
  90. }
  91. static int arc_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  92. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  93. unsigned long attrs)
  94. {
  95. unsigned long user_count = vma_pages(vma);
  96. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  97. unsigned long pfn = __phys_to_pfn(plat_dma_to_phys(dev, dma_addr));
  98. unsigned long off = vma->vm_pgoff;
  99. int ret = -ENXIO;
  100. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  101. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  102. return ret;
  103. if (off < count && user_count <= (count - off)) {
  104. ret = remap_pfn_range(vma, vma->vm_start,
  105. pfn + off,
  106. user_count << PAGE_SHIFT,
  107. vma->vm_page_prot);
  108. }
  109. return ret;
  110. }
  111. /*
  112. * streaming DMA Mapping API...
  113. * CPU accesses page via normal paddr, thus needs to explicitly made
  114. * consistent before each use
  115. */
  116. static void _dma_cache_sync(phys_addr_t paddr, size_t size,
  117. enum dma_data_direction dir)
  118. {
  119. switch (dir) {
  120. case DMA_FROM_DEVICE:
  121. dma_cache_inv(paddr, size);
  122. break;
  123. case DMA_TO_DEVICE:
  124. dma_cache_wback(paddr, size);
  125. break;
  126. case DMA_BIDIRECTIONAL:
  127. dma_cache_wback_inv(paddr, size);
  128. break;
  129. default:
  130. pr_err("Invalid DMA dir [%d] for OP @ %pa[p]\n", dir, &paddr);
  131. }
  132. }
  133. static dma_addr_t arc_dma_map_page(struct device *dev, struct page *page,
  134. unsigned long offset, size_t size, enum dma_data_direction dir,
  135. unsigned long attrs)
  136. {
  137. phys_addr_t paddr = page_to_phys(page) + offset;
  138. _dma_cache_sync(paddr, size, dir);
  139. return plat_phys_to_dma(dev, paddr);
  140. }
  141. static int arc_dma_map_sg(struct device *dev, struct scatterlist *sg,
  142. int nents, enum dma_data_direction dir, unsigned long attrs)
  143. {
  144. struct scatterlist *s;
  145. int i;
  146. for_each_sg(sg, s, nents, i)
  147. s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
  148. s->length, dir);
  149. return nents;
  150. }
  151. static void arc_dma_sync_single_for_cpu(struct device *dev,
  152. dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
  153. {
  154. _dma_cache_sync(plat_dma_to_phys(dev, dma_handle), size, DMA_FROM_DEVICE);
  155. }
  156. static void arc_dma_sync_single_for_device(struct device *dev,
  157. dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
  158. {
  159. _dma_cache_sync(plat_dma_to_phys(dev, dma_handle), size, DMA_TO_DEVICE);
  160. }
  161. static void arc_dma_sync_sg_for_cpu(struct device *dev,
  162. struct scatterlist *sglist, int nelems,
  163. enum dma_data_direction dir)
  164. {
  165. int i;
  166. struct scatterlist *sg;
  167. for_each_sg(sglist, sg, nelems, i)
  168. _dma_cache_sync(sg_phys(sg), sg->length, dir);
  169. }
  170. static void arc_dma_sync_sg_for_device(struct device *dev,
  171. struct scatterlist *sglist, int nelems,
  172. enum dma_data_direction dir)
  173. {
  174. int i;
  175. struct scatterlist *sg;
  176. for_each_sg(sglist, sg, nelems, i)
  177. _dma_cache_sync(sg_phys(sg), sg->length, dir);
  178. }
  179. static int arc_dma_supported(struct device *dev, u64 dma_mask)
  180. {
  181. /* Support 32 bit DMA mask exclusively */
  182. return dma_mask == DMA_BIT_MASK(32);
  183. }
  184. struct dma_map_ops arc_dma_ops = {
  185. .alloc = arc_dma_alloc,
  186. .free = arc_dma_free,
  187. .mmap = arc_dma_mmap,
  188. .map_page = arc_dma_map_page,
  189. .map_sg = arc_dma_map_sg,
  190. .sync_single_for_device = arc_dma_sync_single_for_device,
  191. .sync_single_for_cpu = arc_dma_sync_single_for_cpu,
  192. .sync_sg_for_cpu = arc_dma_sync_sg_for_cpu,
  193. .sync_sg_for_device = arc_dma_sync_sg_for_device,
  194. .dma_supported = arc_dma_supported,
  195. };
  196. EXPORT_SYMBOL(arc_dma_ops);