dma.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * OpenRISC Linux
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * DMA mapping callbacks...
  18. * As alloc_coherent is the only DMA callback being used currently, that's
  19. * the only thing implemented properly. The rest need looking into...
  20. */
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dma-debug.h>
  23. #include <linux/export.h>
  24. #include <asm/cpuinfo.h>
  25. #include <asm/spr_defs.h>
  26. #include <asm/tlbflush.h>
  27. static int
  28. page_set_nocache(pte_t *pte, unsigned long addr,
  29. unsigned long next, struct mm_walk *walk)
  30. {
  31. unsigned long cl;
  32. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  33. pte_val(*pte) |= _PAGE_CI;
  34. /*
  35. * Flush the page out of the TLB so that the new page flags get
  36. * picked up next time there's an access
  37. */
  38. flush_tlb_page(NULL, addr);
  39. /* Flush page out of dcache */
  40. for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo->dcache_block_size)
  41. mtspr(SPR_DCBFR, cl);
  42. return 0;
  43. }
  44. static int
  45. page_clear_nocache(pte_t *pte, unsigned long addr,
  46. unsigned long next, struct mm_walk *walk)
  47. {
  48. pte_val(*pte) &= ~_PAGE_CI;
  49. /*
  50. * Flush the page out of the TLB so that the new page flags get
  51. * picked up next time there's an access
  52. */
  53. flush_tlb_page(NULL, addr);
  54. return 0;
  55. }
  56. /*
  57. * Alloc "coherent" memory, which for OpenRISC means simply uncached.
  58. *
  59. * This function effectively just calls __get_free_pages, sets the
  60. * cache-inhibit bit on those pages, and makes sure that the pages are
  61. * flushed out of the cache before they are used.
  62. *
  63. * If the NON_CONSISTENT attribute is set, then this function just
  64. * returns "normal", cachable memory.
  65. *
  66. * There are additional flags WEAK_ORDERING and WRITE_COMBINE to take
  67. * into consideration here, too. All current known implementations of
  68. * the OR1K support only strongly ordered memory accesses, so that flag
  69. * is being ignored for now; uncached but write-combined memory is a
  70. * missing feature of the OR1K.
  71. */
  72. static void *
  73. or1k_dma_alloc(struct device *dev, size_t size,
  74. dma_addr_t *dma_handle, gfp_t gfp,
  75. unsigned long attrs)
  76. {
  77. unsigned long va;
  78. void *page;
  79. struct mm_walk walk = {
  80. .pte_entry = page_set_nocache,
  81. .mm = &init_mm
  82. };
  83. page = alloc_pages_exact(size, gfp);
  84. if (!page)
  85. return NULL;
  86. /* This gives us the real physical address of the first page. */
  87. *dma_handle = __pa(page);
  88. va = (unsigned long)page;
  89. if ((attrs & DMA_ATTR_NON_CONSISTENT) == 0) {
  90. /*
  91. * We need to iterate through the pages, clearing the dcache for
  92. * them and setting the cache-inhibit bit.
  93. */
  94. if (walk_page_range(va, va + size, &walk)) {
  95. free_pages_exact(page, size);
  96. return NULL;
  97. }
  98. }
  99. return (void *)va;
  100. }
  101. static void
  102. or1k_dma_free(struct device *dev, size_t size, void *vaddr,
  103. dma_addr_t dma_handle, unsigned long attrs)
  104. {
  105. unsigned long va = (unsigned long)vaddr;
  106. struct mm_walk walk = {
  107. .pte_entry = page_clear_nocache,
  108. .mm = &init_mm
  109. };
  110. if ((attrs & DMA_ATTR_NON_CONSISTENT) == 0) {
  111. /* walk_page_range shouldn't be able to fail here */
  112. WARN_ON(walk_page_range(va, va + size, &walk));
  113. }
  114. free_pages_exact(vaddr, size);
  115. }
  116. static dma_addr_t
  117. or1k_map_page(struct device *dev, struct page *page,
  118. unsigned long offset, size_t size,
  119. enum dma_data_direction dir,
  120. unsigned long attrs)
  121. {
  122. unsigned long cl;
  123. dma_addr_t addr = page_to_phys(page) + offset;
  124. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  125. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  126. return addr;
  127. switch (dir) {
  128. case DMA_TO_DEVICE:
  129. /* Flush the dcache for the requested range */
  130. for (cl = addr; cl < addr + size;
  131. cl += cpuinfo->dcache_block_size)
  132. mtspr(SPR_DCBFR, cl);
  133. break;
  134. case DMA_FROM_DEVICE:
  135. /* Invalidate the dcache for the requested range */
  136. for (cl = addr; cl < addr + size;
  137. cl += cpuinfo->dcache_block_size)
  138. mtspr(SPR_DCBIR, cl);
  139. break;
  140. default:
  141. /*
  142. * NOTE: If dir == DMA_BIDIRECTIONAL then there's no need to
  143. * flush nor invalidate the cache here as the area will need
  144. * to be manually synced anyway.
  145. */
  146. break;
  147. }
  148. return addr;
  149. }
  150. static void
  151. or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
  152. size_t size, enum dma_data_direction dir,
  153. unsigned long attrs)
  154. {
  155. /* Nothing special to do here... */
  156. }
  157. static int
  158. or1k_map_sg(struct device *dev, struct scatterlist *sg,
  159. int nents, enum dma_data_direction dir,
  160. unsigned long attrs)
  161. {
  162. struct scatterlist *s;
  163. int i;
  164. for_each_sg(sg, s, nents, i) {
  165. s->dma_address = or1k_map_page(dev, sg_page(s), s->offset,
  166. s->length, dir, 0);
  167. }
  168. return nents;
  169. }
  170. static void
  171. or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
  172. int nents, enum dma_data_direction dir,
  173. unsigned long attrs)
  174. {
  175. struct scatterlist *s;
  176. int i;
  177. for_each_sg(sg, s, nents, i) {
  178. or1k_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, 0);
  179. }
  180. }
  181. static void
  182. or1k_sync_single_for_cpu(struct device *dev,
  183. dma_addr_t dma_handle, size_t size,
  184. enum dma_data_direction dir)
  185. {
  186. unsigned long cl;
  187. dma_addr_t addr = dma_handle;
  188. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  189. /* Invalidate the dcache for the requested range */
  190. for (cl = addr; cl < addr + size; cl += cpuinfo->dcache_block_size)
  191. mtspr(SPR_DCBIR, cl);
  192. }
  193. static void
  194. or1k_sync_single_for_device(struct device *dev,
  195. dma_addr_t dma_handle, size_t size,
  196. enum dma_data_direction dir)
  197. {
  198. unsigned long cl;
  199. dma_addr_t addr = dma_handle;
  200. struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
  201. /* Flush the dcache for the requested range */
  202. for (cl = addr; cl < addr + size; cl += cpuinfo->dcache_block_size)
  203. mtspr(SPR_DCBFR, cl);
  204. }
  205. const struct dma_map_ops or1k_dma_map_ops = {
  206. .alloc = or1k_dma_alloc,
  207. .free = or1k_dma_free,
  208. .map_page = or1k_map_page,
  209. .unmap_page = or1k_unmap_page,
  210. .map_sg = or1k_map_sg,
  211. .unmap_sg = or1k_unmap_sg,
  212. .sync_single_for_cpu = or1k_sync_single_for_cpu,
  213. .sync_single_for_device = or1k_sync_single_for_device,
  214. };
  215. EXPORT_SYMBOL(or1k_dma_map_ops);
  216. /* Number of entries preallocated for DMA-API debugging */
  217. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  218. static int __init dma_init(void)
  219. {
  220. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  221. return 0;
  222. }
  223. fs_initcall(dma_init);