pci-dma.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/export.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/homecache.h>
  20. /* Generic DMA mapping functions: */
  21. /*
  22. * Allocate what Linux calls "coherent" memory, which for us just
  23. * means uncached.
  24. */
  25. void *dma_alloc_coherent(struct device *dev,
  26. size_t size,
  27. dma_addr_t *dma_handle,
  28. gfp_t gfp)
  29. {
  30. u64 dma_mask = dev->coherent_dma_mask ?: DMA_BIT_MASK(32);
  31. int node = dev_to_node(dev);
  32. int order = get_order(size);
  33. struct page *pg;
  34. dma_addr_t addr;
  35. gfp |= __GFP_ZERO;
  36. /*
  37. * By forcing NUMA node 0 for 32-bit masks we ensure that the
  38. * high 32 bits of the resulting PA will be zero. If the mask
  39. * size is, e.g., 24, we may still not be able to guarantee a
  40. * suitable memory address, in which case we will return NULL.
  41. * But such devices are uncommon.
  42. */
  43. if (dma_mask <= DMA_BIT_MASK(32))
  44. node = 0;
  45. pg = homecache_alloc_pages_node(node, gfp, order, PAGE_HOME_UNCACHED);
  46. if (pg == NULL)
  47. return NULL;
  48. addr = page_to_phys(pg);
  49. if (addr + size > dma_mask) {
  50. homecache_free_pages(addr, order);
  51. return NULL;
  52. }
  53. *dma_handle = addr;
  54. return page_address(pg);
  55. }
  56. EXPORT_SYMBOL(dma_alloc_coherent);
  57. /*
  58. * Free memory that was allocated with dma_alloc_coherent.
  59. */
  60. void dma_free_coherent(struct device *dev, size_t size,
  61. void *vaddr, dma_addr_t dma_handle)
  62. {
  63. homecache_free_pages((unsigned long)vaddr, get_order(size));
  64. }
  65. EXPORT_SYMBOL(dma_free_coherent);
  66. /*
  67. * The map routines "map" the specified address range for DMA
  68. * accesses. The memory belongs to the device after this call is
  69. * issued, until it is unmapped with dma_unmap_single.
  70. *
  71. * We don't need to do any mapping, we just flush the address range
  72. * out of the cache and return a DMA address.
  73. *
  74. * The unmap routines do whatever is necessary before the processor
  75. * accesses the memory again, and must be called before the driver
  76. * touches the memory. We can get away with a cache invalidate if we
  77. * can count on nothing having been touched.
  78. */
  79. /* Flush a PA range from cache page by page. */
  80. static void __dma_map_pa_range(dma_addr_t dma_addr, size_t size)
  81. {
  82. struct page *page = pfn_to_page(PFN_DOWN(dma_addr));
  83. size_t bytesleft = PAGE_SIZE - (dma_addr & (PAGE_SIZE - 1));
  84. while ((ssize_t)size > 0) {
  85. /* Flush the page. */
  86. homecache_flush_cache(page++, 0);
  87. /* Figure out if we need to continue on the next page. */
  88. size -= bytesleft;
  89. bytesleft = PAGE_SIZE;
  90. }
  91. }
  92. /*
  93. * dma_map_single can be passed any memory address, and there appear
  94. * to be no alignment constraints.
  95. *
  96. * There is a chance that the start of the buffer will share a cache
  97. * line with some other data that has been touched in the meantime.
  98. */
  99. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  100. enum dma_data_direction direction)
  101. {
  102. dma_addr_t dma_addr = __pa(ptr);
  103. BUG_ON(!valid_dma_direction(direction));
  104. WARN_ON(size == 0);
  105. __dma_map_pa_range(dma_addr, size);
  106. return dma_addr;
  107. }
  108. EXPORT_SYMBOL(dma_map_single);
  109. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  110. enum dma_data_direction direction)
  111. {
  112. BUG_ON(!valid_dma_direction(direction));
  113. }
  114. EXPORT_SYMBOL(dma_unmap_single);
  115. int dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
  116. enum dma_data_direction direction)
  117. {
  118. struct scatterlist *sg;
  119. int i;
  120. BUG_ON(!valid_dma_direction(direction));
  121. WARN_ON(nents == 0 || sglist->length == 0);
  122. for_each_sg(sglist, sg, nents, i) {
  123. sg->dma_address = sg_phys(sg);
  124. __dma_map_pa_range(sg->dma_address, sg->length);
  125. }
  126. return nents;
  127. }
  128. EXPORT_SYMBOL(dma_map_sg);
  129. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  130. enum dma_data_direction direction)
  131. {
  132. BUG_ON(!valid_dma_direction(direction));
  133. }
  134. EXPORT_SYMBOL(dma_unmap_sg);
  135. dma_addr_t dma_map_page(struct device *dev, struct page *page,
  136. unsigned long offset, size_t size,
  137. enum dma_data_direction direction)
  138. {
  139. BUG_ON(!valid_dma_direction(direction));
  140. BUG_ON(offset + size > PAGE_SIZE);
  141. homecache_flush_cache(page, 0);
  142. return page_to_pa(page) + offset;
  143. }
  144. EXPORT_SYMBOL(dma_map_page);
  145. void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  146. enum dma_data_direction direction)
  147. {
  148. BUG_ON(!valid_dma_direction(direction));
  149. }
  150. EXPORT_SYMBOL(dma_unmap_page);
  151. void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  152. size_t size, enum dma_data_direction direction)
  153. {
  154. BUG_ON(!valid_dma_direction(direction));
  155. }
  156. EXPORT_SYMBOL(dma_sync_single_for_cpu);
  157. void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  158. size_t size, enum dma_data_direction direction)
  159. {
  160. unsigned long start = PFN_DOWN(dma_handle);
  161. unsigned long end = PFN_DOWN(dma_handle + size - 1);
  162. unsigned long i;
  163. BUG_ON(!valid_dma_direction(direction));
  164. for (i = start; i <= end; ++i)
  165. homecache_flush_cache(pfn_to_page(i), 0);
  166. }
  167. EXPORT_SYMBOL(dma_sync_single_for_device);
  168. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
  169. enum dma_data_direction direction)
  170. {
  171. BUG_ON(!valid_dma_direction(direction));
  172. WARN_ON(nelems == 0 || sg[0].length == 0);
  173. }
  174. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  175. /*
  176. * Flush and invalidate cache for scatterlist.
  177. */
  178. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist,
  179. int nelems, enum dma_data_direction direction)
  180. {
  181. struct scatterlist *sg;
  182. int i;
  183. BUG_ON(!valid_dma_direction(direction));
  184. WARN_ON(nelems == 0 || sglist->length == 0);
  185. for_each_sg(sglist, sg, nelems, i) {
  186. dma_sync_single_for_device(dev, sg->dma_address,
  187. sg_dma_len(sg), direction);
  188. }
  189. }
  190. EXPORT_SYMBOL(dma_sync_sg_for_device);
  191. void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  192. unsigned long offset, size_t size,
  193. enum dma_data_direction direction)
  194. {
  195. dma_sync_single_for_cpu(dev, dma_handle + offset, size, direction);
  196. }
  197. EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
  198. void dma_sync_single_range_for_device(struct device *dev,
  199. dma_addr_t dma_handle,
  200. unsigned long offset, size_t size,
  201. enum dma_data_direction direction)
  202. {
  203. dma_sync_single_for_device(dev, dma_handle + offset, size, direction);
  204. }
  205. EXPORT_SYMBOL(dma_sync_single_range_for_device);
  206. /*
  207. * dma_alloc_noncoherent() returns non-cacheable memory, so there's no
  208. * need to do any flushing here.
  209. */
  210. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  211. enum dma_data_direction direction)
  212. {
  213. }
  214. EXPORT_SYMBOL(dma_cache_sync);