dma-default.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
  7. * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
  8. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/string.h>
  16. #include <linux/gfp.h>
  17. #include <linux/highmem.h>
  18. #include <asm/cache.h>
  19. #include <asm/cpu-type.h>
  20. #include <asm/io.h>
  21. #include <dma-coherence.h>
  22. #ifdef CONFIG_DMA_MAYBE_COHERENT
  23. int coherentio = 0; /* User defined DMA coherency from command line. */
  24. EXPORT_SYMBOL_GPL(coherentio);
  25. int hw_coherentio = 0; /* Actual hardware supported DMA coherency setting. */
  26. static int __init setcoherentio(char *str)
  27. {
  28. coherentio = 1;
  29. pr_info("Hardware DMA cache coherency (command line)\n");
  30. return 0;
  31. }
  32. early_param("coherentio", setcoherentio);
  33. static int __init setnocoherentio(char *str)
  34. {
  35. coherentio = 0;
  36. pr_info("Software DMA cache coherency (command line)\n");
  37. return 0;
  38. }
  39. early_param("nocoherentio", setnocoherentio);
  40. #endif
  41. static inline struct page *dma_addr_to_page(struct device *dev,
  42. dma_addr_t dma_addr)
  43. {
  44. return pfn_to_page(
  45. plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT);
  46. }
  47. /*
  48. * The affected CPUs below in 'cpu_needs_post_dma_flush()' can
  49. * speculatively fill random cachelines with stale data at any time,
  50. * requiring an extra flush post-DMA.
  51. *
  52. * Warning on the terminology - Linux calls an uncached area coherent;
  53. * MIPS terminology calls memory areas with hardware maintained coherency
  54. * coherent.
  55. */
  56. static inline int cpu_needs_post_dma_flush(struct device *dev)
  57. {
  58. return !plat_device_is_coherent(dev) &&
  59. (boot_cpu_type() == CPU_R10000 ||
  60. boot_cpu_type() == CPU_R12000 ||
  61. boot_cpu_type() == CPU_BMIPS5000);
  62. }
  63. static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
  64. {
  65. gfp_t dma_flag;
  66. /* ignore region specifiers */
  67. gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  68. #ifdef CONFIG_ISA
  69. if (dev == NULL)
  70. dma_flag = __GFP_DMA;
  71. else
  72. #endif
  73. #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
  74. if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
  75. dma_flag = __GFP_DMA;
  76. else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  77. dma_flag = __GFP_DMA32;
  78. else
  79. #endif
  80. #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
  81. if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  82. dma_flag = __GFP_DMA32;
  83. else
  84. #endif
  85. #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
  86. if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  87. dma_flag = __GFP_DMA;
  88. else
  89. #endif
  90. dma_flag = 0;
  91. /* Don't invoke OOM killer */
  92. gfp |= __GFP_NORETRY;
  93. return gfp | dma_flag;
  94. }
  95. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  96. dma_addr_t * dma_handle, gfp_t gfp)
  97. {
  98. void *ret;
  99. gfp = massage_gfp_flags(dev, gfp);
  100. ret = (void *) __get_free_pages(gfp, get_order(size));
  101. if (ret != NULL) {
  102. memset(ret, 0, size);
  103. *dma_handle = plat_map_dma_mem(dev, ret, size);
  104. }
  105. return ret;
  106. }
  107. EXPORT_SYMBOL(dma_alloc_noncoherent);
  108. static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
  109. dma_addr_t * dma_handle, gfp_t gfp, struct dma_attrs *attrs)
  110. {
  111. void *ret;
  112. if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
  113. return ret;
  114. gfp = massage_gfp_flags(dev, gfp);
  115. ret = (void *) __get_free_pages(gfp, get_order(size));
  116. if (ret) {
  117. memset(ret, 0, size);
  118. *dma_handle = plat_map_dma_mem(dev, ret, size);
  119. if (!plat_device_is_coherent(dev)) {
  120. dma_cache_wback_inv((unsigned long) ret, size);
  121. if (!hw_coherentio)
  122. ret = UNCAC_ADDR(ret);
  123. }
  124. }
  125. return ret;
  126. }
  127. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  128. dma_addr_t dma_handle)
  129. {
  130. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  131. free_pages((unsigned long) vaddr, get_order(size));
  132. }
  133. EXPORT_SYMBOL(dma_free_noncoherent);
  134. static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  135. dma_addr_t dma_handle, struct dma_attrs *attrs)
  136. {
  137. unsigned long addr = (unsigned long) vaddr;
  138. int order = get_order(size);
  139. if (dma_release_from_coherent(dev, order, vaddr))
  140. return;
  141. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  142. if (!plat_device_is_coherent(dev) && !hw_coherentio)
  143. addr = CAC_ADDR(addr);
  144. free_pages(addr, get_order(size));
  145. }
  146. static inline void __dma_sync_virtual(void *addr, size_t size,
  147. enum dma_data_direction direction)
  148. {
  149. switch (direction) {
  150. case DMA_TO_DEVICE:
  151. dma_cache_wback((unsigned long)addr, size);
  152. break;
  153. case DMA_FROM_DEVICE:
  154. dma_cache_inv((unsigned long)addr, size);
  155. break;
  156. case DMA_BIDIRECTIONAL:
  157. dma_cache_wback_inv((unsigned long)addr, size);
  158. break;
  159. default:
  160. BUG();
  161. }
  162. }
  163. /*
  164. * A single sg entry may refer to multiple physically contiguous
  165. * pages. But we still need to process highmem pages individually.
  166. * If highmem is not configured then the bulk of this loop gets
  167. * optimized out.
  168. */
  169. static inline void __dma_sync(struct page *page,
  170. unsigned long offset, size_t size, enum dma_data_direction direction)
  171. {
  172. size_t left = size;
  173. do {
  174. size_t len = left;
  175. if (PageHighMem(page)) {
  176. void *addr;
  177. if (offset + len > PAGE_SIZE) {
  178. if (offset >= PAGE_SIZE) {
  179. page += offset >> PAGE_SHIFT;
  180. offset &= ~PAGE_MASK;
  181. }
  182. len = PAGE_SIZE - offset;
  183. }
  184. addr = kmap_atomic(page);
  185. __dma_sync_virtual(addr + offset, len, direction);
  186. kunmap_atomic(addr);
  187. } else
  188. __dma_sync_virtual(page_address(page) + offset,
  189. size, direction);
  190. offset = 0;
  191. page++;
  192. left -= len;
  193. } while (left);
  194. }
  195. static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  196. size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
  197. {
  198. if (cpu_needs_post_dma_flush(dev))
  199. __dma_sync(dma_addr_to_page(dev, dma_addr),
  200. dma_addr & ~PAGE_MASK, size, direction);
  201. plat_unmap_dma_mem(dev, dma_addr, size, direction);
  202. }
  203. static int mips_dma_map_sg(struct device *dev, struct scatterlist *sg,
  204. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  205. {
  206. int i;
  207. for (i = 0; i < nents; i++, sg++) {
  208. if (!plat_device_is_coherent(dev))
  209. __dma_sync(sg_page(sg), sg->offset, sg->length,
  210. direction);
  211. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  212. sg->dma_length = sg->length;
  213. #endif
  214. sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
  215. sg->offset;
  216. }
  217. return nents;
  218. }
  219. static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
  220. unsigned long offset, size_t size, enum dma_data_direction direction,
  221. struct dma_attrs *attrs)
  222. {
  223. if (!plat_device_is_coherent(dev))
  224. __dma_sync(page, offset, size, direction);
  225. return plat_map_dma_mem_page(dev, page) + offset;
  226. }
  227. static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  228. int nhwentries, enum dma_data_direction direction,
  229. struct dma_attrs *attrs)
  230. {
  231. int i;
  232. for (i = 0; i < nhwentries; i++, sg++) {
  233. if (!plat_device_is_coherent(dev) &&
  234. direction != DMA_TO_DEVICE)
  235. __dma_sync(sg_page(sg), sg->offset, sg->length,
  236. direction);
  237. plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
  238. }
  239. }
  240. static void mips_dma_sync_single_for_cpu(struct device *dev,
  241. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  242. {
  243. if (cpu_needs_post_dma_flush(dev))
  244. __dma_sync(dma_addr_to_page(dev, dma_handle),
  245. dma_handle & ~PAGE_MASK, size, direction);
  246. }
  247. static void mips_dma_sync_single_for_device(struct device *dev,
  248. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  249. {
  250. if (!plat_device_is_coherent(dev))
  251. __dma_sync(dma_addr_to_page(dev, dma_handle),
  252. dma_handle & ~PAGE_MASK, size, direction);
  253. }
  254. static void mips_dma_sync_sg_for_cpu(struct device *dev,
  255. struct scatterlist *sg, int nelems, enum dma_data_direction direction)
  256. {
  257. int i;
  258. if (cpu_needs_post_dma_flush(dev))
  259. for (i = 0; i < nelems; i++, sg++)
  260. __dma_sync(sg_page(sg), sg->offset, sg->length,
  261. direction);
  262. }
  263. static void mips_dma_sync_sg_for_device(struct device *dev,
  264. struct scatterlist *sg, int nelems, enum dma_data_direction direction)
  265. {
  266. int i;
  267. if (!plat_device_is_coherent(dev))
  268. for (i = 0; i < nelems; i++, sg++)
  269. __dma_sync(sg_page(sg), sg->offset, sg->length,
  270. direction);
  271. }
  272. int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  273. {
  274. return 0;
  275. }
  276. int mips_dma_supported(struct device *dev, u64 mask)
  277. {
  278. return plat_dma_supported(dev, mask);
  279. }
  280. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  281. enum dma_data_direction direction)
  282. {
  283. BUG_ON(direction == DMA_NONE);
  284. if (!plat_device_is_coherent(dev))
  285. __dma_sync_virtual(vaddr, size, direction);
  286. }
  287. EXPORT_SYMBOL(dma_cache_sync);
  288. static struct dma_map_ops mips_default_dma_map_ops = {
  289. .alloc = mips_dma_alloc_coherent,
  290. .free = mips_dma_free_coherent,
  291. .map_page = mips_dma_map_page,
  292. .unmap_page = mips_dma_unmap_page,
  293. .map_sg = mips_dma_map_sg,
  294. .unmap_sg = mips_dma_unmap_sg,
  295. .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
  296. .sync_single_for_device = mips_dma_sync_single_for_device,
  297. .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
  298. .sync_sg_for_device = mips_dma_sync_sg_for_device,
  299. .mapping_error = mips_dma_mapping_error,
  300. .dma_supported = mips_dma_supported
  301. };
  302. struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
  303. EXPORT_SYMBOL(mips_dma_map_ops);
  304. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  305. static int __init mips_dma_init(void)
  306. {
  307. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  308. return 0;
  309. }
  310. fs_initcall(mips_dma_init);