dma.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <linux/dma-attrs.h>
  25. #include <asm/cpuinfo.h>
  26. #include <asm/spr_defs.h>
  27. #include <asm/tlbflush.h>
  28. static int
  29. page_set_nocache(pte_t *pte, unsigned long addr,
  30. unsigned long next, struct mm_walk *walk)
  31. {
  32. unsigned long cl;
  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. struct dma_attrs *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 (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs)) {
  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, struct dma_attrs *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 (!dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs)) {
  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. struct dma_attrs *attrs)
  121. {
  122. unsigned long cl;
  123. dma_addr_t addr = page_to_phys(page) + offset;
  124. switch (dir) {
  125. case DMA_TO_DEVICE:
  126. /* Flush the dcache for the requested range */
  127. for (cl = addr; cl < addr + size;
  128. cl += cpuinfo.dcache_block_size)
  129. mtspr(SPR_DCBFR, cl);
  130. break;
  131. case DMA_FROM_DEVICE:
  132. /* Invalidate the dcache for the requested range */
  133. for (cl = addr; cl < addr + size;
  134. cl += cpuinfo.dcache_block_size)
  135. mtspr(SPR_DCBIR, cl);
  136. break;
  137. default:
  138. /*
  139. * NOTE: If dir == DMA_BIDIRECTIONAL then there's no need to
  140. * flush nor invalidate the cache here as the area will need
  141. * to be manually synced anyway.
  142. */
  143. break;
  144. }
  145. return addr;
  146. }
  147. static void
  148. or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
  149. size_t size, enum dma_data_direction dir,
  150. struct dma_attrs *attrs)
  151. {
  152. /* Nothing special to do here... */
  153. }
  154. static int
  155. or1k_map_sg(struct device *dev, struct scatterlist *sg,
  156. int nents, enum dma_data_direction dir,
  157. struct dma_attrs *attrs)
  158. {
  159. struct scatterlist *s;
  160. int i;
  161. for_each_sg(sg, s, nents, i) {
  162. s->dma_address = or1k_map_page(dev, sg_page(s), s->offset,
  163. s->length, dir, NULL);
  164. }
  165. return nents;
  166. }
  167. static void
  168. or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
  169. int nents, enum dma_data_direction dir,
  170. struct dma_attrs *attrs)
  171. {
  172. struct scatterlist *s;
  173. int i;
  174. for_each_sg(sg, s, nents, i) {
  175. or1k_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, NULL);
  176. }
  177. }
  178. static void
  179. or1k_sync_single_for_cpu(struct device *dev,
  180. dma_addr_t dma_handle, size_t size,
  181. enum dma_data_direction dir)
  182. {
  183. unsigned long cl;
  184. dma_addr_t addr = dma_handle;
  185. /* Invalidate the dcache for the requested range */
  186. for (cl = addr; cl < addr + size; cl += cpuinfo.dcache_block_size)
  187. mtspr(SPR_DCBIR, cl);
  188. }
  189. static void
  190. or1k_sync_single_for_device(struct device *dev,
  191. dma_addr_t dma_handle, size_t size,
  192. enum dma_data_direction dir)
  193. {
  194. unsigned long cl;
  195. dma_addr_t addr = dma_handle;
  196. /* Flush the dcache for the requested range */
  197. for (cl = addr; cl < addr + size; cl += cpuinfo.dcache_block_size)
  198. mtspr(SPR_DCBFR, cl);
  199. }
  200. struct dma_map_ops or1k_dma_map_ops = {
  201. .alloc = or1k_dma_alloc,
  202. .free = or1k_dma_free,
  203. .map_page = or1k_map_page,
  204. .unmap_page = or1k_unmap_page,
  205. .map_sg = or1k_map_sg,
  206. .unmap_sg = or1k_unmap_sg,
  207. .sync_single_for_cpu = or1k_sync_single_for_cpu,
  208. .sync_single_for_device = or1k_sync_single_for_device,
  209. };
  210. EXPORT_SYMBOL(or1k_dma_map_ops);
  211. /* Number of entries preallocated for DMA-API debugging */
  212. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  213. static int __init dma_init(void)
  214. {
  215. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  216. return 0;
  217. }
  218. fs_initcall(dma_init);