pci-dma.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * DMA coherent memory allocation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * Copyright (C) 2002 - 2005 Tensilica Inc.
  10. * Copyright (C) 2015 Cadence Design Systems Inc.
  11. *
  12. * Based on version for i386.
  13. *
  14. * Chris Zankel <chris@zankel.net>
  15. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  16. */
  17. #include <linux/gfp.h>
  18. #include <linux/highmem.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/string.h>
  23. #include <linux/types.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/io.h>
  26. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  27. enum dma_data_direction dir)
  28. {
  29. switch (dir) {
  30. case DMA_BIDIRECTIONAL:
  31. __flush_invalidate_dcache_range((unsigned long)vaddr, size);
  32. break;
  33. case DMA_FROM_DEVICE:
  34. __invalidate_dcache_range((unsigned long)vaddr, size);
  35. break;
  36. case DMA_TO_DEVICE:
  37. __flush_dcache_range((unsigned long)vaddr, size);
  38. break;
  39. case DMA_NONE:
  40. BUG();
  41. break;
  42. }
  43. }
  44. EXPORT_SYMBOL(dma_cache_sync);
  45. static void do_cache_op(dma_addr_t dma_handle, size_t size,
  46. void (*fn)(unsigned long, unsigned long))
  47. {
  48. unsigned long off = dma_handle & (PAGE_SIZE - 1);
  49. unsigned long pfn = PFN_DOWN(dma_handle);
  50. struct page *page = pfn_to_page(pfn);
  51. if (!PageHighMem(page))
  52. fn((unsigned long)bus_to_virt(dma_handle), size);
  53. else
  54. while (size > 0) {
  55. size_t sz = min_t(size_t, size, PAGE_SIZE - off);
  56. void *vaddr = kmap_atomic(page);
  57. fn((unsigned long)vaddr + off, sz);
  58. kunmap_atomic(vaddr);
  59. off = 0;
  60. ++page;
  61. size -= sz;
  62. }
  63. }
  64. static void xtensa_sync_single_for_cpu(struct device *dev,
  65. dma_addr_t dma_handle, size_t size,
  66. enum dma_data_direction dir)
  67. {
  68. switch (dir) {
  69. case DMA_BIDIRECTIONAL:
  70. case DMA_FROM_DEVICE:
  71. do_cache_op(dma_handle, size, __invalidate_dcache_range);
  72. break;
  73. case DMA_NONE:
  74. BUG();
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. static void xtensa_sync_single_for_device(struct device *dev,
  81. dma_addr_t dma_handle, size_t size,
  82. enum dma_data_direction dir)
  83. {
  84. switch (dir) {
  85. case DMA_BIDIRECTIONAL:
  86. case DMA_TO_DEVICE:
  87. if (XCHAL_DCACHE_IS_WRITEBACK)
  88. do_cache_op(dma_handle, size, __flush_dcache_range);
  89. break;
  90. case DMA_NONE:
  91. BUG();
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. static void xtensa_sync_sg_for_cpu(struct device *dev,
  98. struct scatterlist *sg, int nents,
  99. enum dma_data_direction dir)
  100. {
  101. struct scatterlist *s;
  102. int i;
  103. for_each_sg(sg, s, nents, i) {
  104. xtensa_sync_single_for_cpu(dev, sg_dma_address(s),
  105. sg_dma_len(s), dir);
  106. }
  107. }
  108. static void xtensa_sync_sg_for_device(struct device *dev,
  109. struct scatterlist *sg, int nents,
  110. enum dma_data_direction dir)
  111. {
  112. struct scatterlist *s;
  113. int i;
  114. for_each_sg(sg, s, nents, i) {
  115. xtensa_sync_single_for_device(dev, sg_dma_address(s),
  116. sg_dma_len(s), dir);
  117. }
  118. }
  119. /*
  120. * Note: We assume that the full memory space is always mapped to 'kseg'
  121. * Otherwise we have to use page attributes (not implemented).
  122. */
  123. static void *xtensa_dma_alloc(struct device *dev, size_t size,
  124. dma_addr_t *handle, gfp_t flag,
  125. unsigned long attrs)
  126. {
  127. unsigned long ret;
  128. unsigned long uncached = 0;
  129. /* ignore region speicifiers */
  130. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  131. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  132. flag |= GFP_DMA;
  133. ret = (unsigned long)__get_free_pages(flag, get_order(size));
  134. if (ret == 0)
  135. return NULL;
  136. /* We currently don't support coherent memory outside KSEG */
  137. BUG_ON(ret < XCHAL_KSEG_CACHED_VADDR ||
  138. ret > XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE - 1);
  139. uncached = ret + XCHAL_KSEG_BYPASS_VADDR - XCHAL_KSEG_CACHED_VADDR;
  140. *handle = virt_to_bus((void *)ret);
  141. __invalidate_dcache_range(ret, size);
  142. return (void *)uncached;
  143. }
  144. static void xtensa_dma_free(struct device *hwdev, size_t size, void *vaddr,
  145. dma_addr_t dma_handle, unsigned long attrs)
  146. {
  147. unsigned long addr = (unsigned long)vaddr +
  148. XCHAL_KSEG_CACHED_VADDR - XCHAL_KSEG_BYPASS_VADDR;
  149. BUG_ON(addr < XCHAL_KSEG_CACHED_VADDR ||
  150. addr > XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE - 1);
  151. free_pages(addr, get_order(size));
  152. }
  153. static dma_addr_t xtensa_map_page(struct device *dev, struct page *page,
  154. unsigned long offset, size_t size,
  155. enum dma_data_direction dir,
  156. unsigned long attrs)
  157. {
  158. dma_addr_t dma_handle = page_to_phys(page) + offset;
  159. xtensa_sync_single_for_device(dev, dma_handle, size, dir);
  160. return dma_handle;
  161. }
  162. static void xtensa_unmap_page(struct device *dev, dma_addr_t dma_handle,
  163. size_t size, enum dma_data_direction dir,
  164. unsigned long attrs)
  165. {
  166. xtensa_sync_single_for_cpu(dev, dma_handle, size, dir);
  167. }
  168. static int xtensa_map_sg(struct device *dev, struct scatterlist *sg,
  169. int nents, enum dma_data_direction dir,
  170. unsigned long attrs)
  171. {
  172. struct scatterlist *s;
  173. int i;
  174. for_each_sg(sg, s, nents, i) {
  175. s->dma_address = xtensa_map_page(dev, sg_page(s), s->offset,
  176. s->length, dir, attrs);
  177. }
  178. return nents;
  179. }
  180. static void xtensa_unmap_sg(struct device *dev,
  181. struct scatterlist *sg, int nents,
  182. enum dma_data_direction dir,
  183. unsigned long attrs)
  184. {
  185. struct scatterlist *s;
  186. int i;
  187. for_each_sg(sg, s, nents, i) {
  188. xtensa_unmap_page(dev, sg_dma_address(s),
  189. sg_dma_len(s), dir, attrs);
  190. }
  191. }
  192. int xtensa_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  193. {
  194. return 0;
  195. }
  196. struct dma_map_ops xtensa_dma_map_ops = {
  197. .alloc = xtensa_dma_alloc,
  198. .free = xtensa_dma_free,
  199. .map_page = xtensa_map_page,
  200. .unmap_page = xtensa_unmap_page,
  201. .map_sg = xtensa_map_sg,
  202. .unmap_sg = xtensa_unmap_sg,
  203. .sync_single_for_cpu = xtensa_sync_single_for_cpu,
  204. .sync_single_for_device = xtensa_sync_single_for_device,
  205. .sync_sg_for_cpu = xtensa_sync_sg_for_cpu,
  206. .sync_sg_for_device = xtensa_sync_sg_for_device,
  207. .mapping_error = xtensa_dma_mapping_error,
  208. };
  209. EXPORT_SYMBOL(xtensa_dma_map_ops);
  210. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  211. static int __init xtensa_dma_init(void)
  212. {
  213. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  214. return 0;
  215. }
  216. fs_initcall(xtensa_dma_init);