dma.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_exists) ||
  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_exists);
  87. if (PageHighMem(page) || !is_non_coh)
  88. iounmap((void __force __iomem *)vaddr);
  89. __free_pages(page, get_order(size));
  90. }
  91. /*
  92. * streaming DMA Mapping API...
  93. * CPU accesses page via normal paddr, thus needs to explicitly made
  94. * consistent before each use
  95. */
  96. static void _dma_cache_sync(phys_addr_t paddr, size_t size,
  97. enum dma_data_direction dir)
  98. {
  99. switch (dir) {
  100. case DMA_FROM_DEVICE:
  101. dma_cache_inv(paddr, size);
  102. break;
  103. case DMA_TO_DEVICE:
  104. dma_cache_wback(paddr, size);
  105. break;
  106. case DMA_BIDIRECTIONAL:
  107. dma_cache_wback_inv(paddr, size);
  108. break;
  109. default:
  110. pr_err("Invalid DMA dir [%d] for OP @ %pa[p]\n", dir, &paddr);
  111. }
  112. }
  113. static dma_addr_t arc_dma_map_page(struct device *dev, struct page *page,
  114. unsigned long offset, size_t size, enum dma_data_direction dir,
  115. unsigned long attrs)
  116. {
  117. phys_addr_t paddr = page_to_phys(page) + offset;
  118. _dma_cache_sync(paddr, size, dir);
  119. return plat_phys_to_dma(dev, paddr);
  120. }
  121. static int arc_dma_map_sg(struct device *dev, struct scatterlist *sg,
  122. int nents, enum dma_data_direction dir, unsigned long attrs)
  123. {
  124. struct scatterlist *s;
  125. int i;
  126. for_each_sg(sg, s, nents, i)
  127. s->dma_address = dma_map_page(dev, sg_page(s), s->offset,
  128. s->length, dir);
  129. return nents;
  130. }
  131. static void arc_dma_sync_single_for_cpu(struct device *dev,
  132. dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
  133. {
  134. _dma_cache_sync(plat_dma_to_phys(dev, dma_handle), size, DMA_FROM_DEVICE);
  135. }
  136. static void arc_dma_sync_single_for_device(struct device *dev,
  137. dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
  138. {
  139. _dma_cache_sync(plat_dma_to_phys(dev, dma_handle), size, DMA_TO_DEVICE);
  140. }
  141. static void arc_dma_sync_sg_for_cpu(struct device *dev,
  142. struct scatterlist *sglist, int nelems,
  143. enum dma_data_direction dir)
  144. {
  145. int i;
  146. struct scatterlist *sg;
  147. for_each_sg(sglist, sg, nelems, i)
  148. _dma_cache_sync(sg_phys(sg), sg->length, dir);
  149. }
  150. static void arc_dma_sync_sg_for_device(struct device *dev,
  151. struct scatterlist *sglist, int nelems,
  152. enum dma_data_direction dir)
  153. {
  154. int i;
  155. struct scatterlist *sg;
  156. for_each_sg(sglist, sg, nelems, i)
  157. _dma_cache_sync(sg_phys(sg), sg->length, dir);
  158. }
  159. static int arc_dma_supported(struct device *dev, u64 dma_mask)
  160. {
  161. /* Support 32 bit DMA mask exclusively */
  162. return dma_mask == DMA_BIT_MASK(32);
  163. }
  164. struct dma_map_ops arc_dma_ops = {
  165. .alloc = arc_dma_alloc,
  166. .free = arc_dma_free,
  167. .map_page = arc_dma_map_page,
  168. .map_sg = arc_dma_map_sg,
  169. .sync_single_for_device = arc_dma_sync_single_for_device,
  170. .sync_single_for_cpu = arc_dma_sync_single_for_cpu,
  171. .sync_sg_for_cpu = arc_dma_sync_sg_for_cpu,
  172. .sync_sg_for_device = arc_dma_sync_sg_for_device,
  173. .dma_supported = arc_dma_supported,
  174. };
  175. EXPORT_SYMBOL(arc_dma_ops);