dma.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <asm/cpuinfo.h>
  24. #include <asm/spr_defs.h>
  25. #include <asm/tlbflush.h>
  26. static int page_set_nocache(pte_t *pte, unsigned long addr,
  27. unsigned long next, struct mm_walk *walk)
  28. {
  29. unsigned long cl;
  30. pte_val(*pte) |= _PAGE_CI;
  31. /*
  32. * Flush the page out of the TLB so that the new page flags get
  33. * picked up next time there's an access
  34. */
  35. flush_tlb_page(NULL, addr);
  36. /* Flush page out of dcache */
  37. for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo.dcache_block_size)
  38. mtspr(SPR_DCBFR, cl);
  39. return 0;
  40. }
  41. static int page_clear_nocache(pte_t *pte, unsigned long addr,
  42. unsigned long next, struct mm_walk *walk)
  43. {
  44. pte_val(*pte) &= ~_PAGE_CI;
  45. /*
  46. * Flush the page out of the TLB so that the new page flags get
  47. * picked up next time there's an access
  48. */
  49. flush_tlb_page(NULL, addr);
  50. return 0;
  51. }
  52. /*
  53. * Alloc "coherent" memory, which for OpenRISC means simply uncached.
  54. *
  55. * This function effectively just calls __get_free_pages, sets the
  56. * cache-inhibit bit on those pages, and makes sure that the pages are
  57. * flushed out of the cache before they are used.
  58. *
  59. */
  60. void *or1k_dma_alloc_coherent(struct device *dev, size_t size,
  61. dma_addr_t *dma_handle, gfp_t gfp)
  62. {
  63. unsigned long va;
  64. void *page;
  65. struct mm_walk walk = {
  66. .pte_entry = page_set_nocache,
  67. .mm = &init_mm
  68. };
  69. page = alloc_pages_exact(size, gfp);
  70. if (!page)
  71. return NULL;
  72. /* This gives us the real physical address of the first page. */
  73. *dma_handle = __pa(page);
  74. va = (unsigned long)page;
  75. /*
  76. * We need to iterate through the pages, clearing the dcache for
  77. * them and setting the cache-inhibit bit.
  78. */
  79. if (walk_page_range(va, va + size, &walk)) {
  80. free_pages_exact(page, size);
  81. return NULL;
  82. }
  83. return (void *)va;
  84. }
  85. void or1k_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  86. dma_addr_t dma_handle)
  87. {
  88. unsigned long va = (unsigned long)vaddr;
  89. struct mm_walk walk = {
  90. .pte_entry = page_clear_nocache,
  91. .mm = &init_mm
  92. };
  93. /* walk_page_range shouldn't be able to fail here */
  94. WARN_ON(walk_page_range(va, va + size, &walk));
  95. free_pages_exact(vaddr, size);
  96. }
  97. dma_addr_t or1k_map_page(struct device *dev, struct page *page,
  98. unsigned long offset, size_t size,
  99. enum dma_data_direction dir,
  100. struct dma_attrs *attrs)
  101. {
  102. unsigned long cl;
  103. dma_addr_t addr = page_to_phys(page) + offset;
  104. switch (dir) {
  105. case DMA_TO_DEVICE:
  106. /* Flush the dcache for the requested range */
  107. for (cl = addr; cl < addr + size;
  108. cl += cpuinfo.dcache_block_size)
  109. mtspr(SPR_DCBFR, cl);
  110. break;
  111. case DMA_FROM_DEVICE:
  112. /* Invalidate the dcache for the requested range */
  113. for (cl = addr; cl < addr + size;
  114. cl += cpuinfo.dcache_block_size)
  115. mtspr(SPR_DCBIR, cl);
  116. break;
  117. default:
  118. /*
  119. * NOTE: If dir == DMA_BIDIRECTIONAL then there's no need to
  120. * flush nor invalidate the cache here as the area will need
  121. * to be manually synced anyway.
  122. */
  123. break;
  124. }
  125. return addr;
  126. }
  127. void or1k_unmap_page(struct device *dev, dma_addr_t dma_handle,
  128. size_t size, enum dma_data_direction dir,
  129. struct dma_attrs *attrs)
  130. {
  131. /* Nothing special to do here... */
  132. }
  133. int or1k_map_sg(struct device *dev, struct scatterlist *sg,
  134. int nents, enum dma_data_direction dir,
  135. struct dma_attrs *attrs)
  136. {
  137. struct scatterlist *s;
  138. int i;
  139. for_each_sg(sg, s, nents, i) {
  140. s->dma_address = or1k_map_page(dev, sg_page(s), s->offset,
  141. s->length, dir, NULL);
  142. }
  143. return nents;
  144. }
  145. void or1k_unmap_sg(struct device *dev, struct scatterlist *sg,
  146. int nents, enum dma_data_direction dir,
  147. struct dma_attrs *attrs)
  148. {
  149. struct scatterlist *s;
  150. int i;
  151. for_each_sg(sg, s, nents, i) {
  152. or1k_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir, NULL);
  153. }
  154. }
  155. void or1k_sync_single_for_cpu(struct device *dev,
  156. dma_addr_t dma_handle, size_t size,
  157. enum dma_data_direction dir)
  158. {
  159. unsigned long cl;
  160. dma_addr_t addr = dma_handle;
  161. /* Invalidate the dcache for the requested range */
  162. for (cl = addr; cl < addr + size; cl += cpuinfo.dcache_block_size)
  163. mtspr(SPR_DCBIR, cl);
  164. }
  165. void or1k_sync_single_for_device(struct device *dev,
  166. dma_addr_t dma_handle, size_t size,
  167. enum dma_data_direction dir)
  168. {
  169. unsigned long cl;
  170. dma_addr_t addr = dma_handle;
  171. /* Flush the dcache for the requested range */
  172. for (cl = addr; cl < addr + size; cl += cpuinfo.dcache_block_size)
  173. mtspr(SPR_DCBFR, cl);
  174. }
  175. /* Number of entries preallocated for DMA-API debugging */
  176. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  177. static int __init dma_init(void)
  178. {
  179. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  180. return 0;
  181. }
  182. fs_initcall(dma_init);