pci-dma.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. On TILEPro this is
  23. * uncached memory; on TILE-Gx it is hash-for-home memory.
  24. */
  25. #ifdef __tilepro__
  26. #define PAGE_HOME_DMA PAGE_HOME_UNCACHED
  27. #else
  28. #define PAGE_HOME_DMA PAGE_HOME_HASH
  29. #endif
  30. void *dma_alloc_coherent(struct device *dev,
  31. size_t size,
  32. dma_addr_t *dma_handle,
  33. gfp_t gfp)
  34. {
  35. u64 dma_mask = dev->coherent_dma_mask ?: DMA_BIT_MASK(32);
  36. int node = dev_to_node(dev);
  37. int order = get_order(size);
  38. struct page *pg;
  39. dma_addr_t addr;
  40. gfp |= __GFP_ZERO;
  41. /*
  42. * By forcing NUMA node 0 for 32-bit masks we ensure that the
  43. * high 32 bits of the resulting PA will be zero. If the mask
  44. * size is, e.g., 24, we may still not be able to guarantee a
  45. * suitable memory address, in which case we will return NULL.
  46. * But such devices are uncommon.
  47. */
  48. if (dma_mask <= DMA_BIT_MASK(32))
  49. node = 0;
  50. pg = homecache_alloc_pages_node(node, gfp, order, PAGE_HOME_DMA);
  51. if (pg == NULL)
  52. return NULL;
  53. addr = page_to_phys(pg);
  54. if (addr + size > dma_mask) {
  55. __homecache_free_pages(pg, order);
  56. return NULL;
  57. }
  58. *dma_handle = addr;
  59. return page_address(pg);
  60. }
  61. EXPORT_SYMBOL(dma_alloc_coherent);
  62. /*
  63. * Free memory that was allocated with dma_alloc_coherent.
  64. */
  65. void dma_free_coherent(struct device *dev, size_t size,
  66. void *vaddr, dma_addr_t dma_handle)
  67. {
  68. homecache_free_pages((unsigned long)vaddr, get_order(size));
  69. }
  70. EXPORT_SYMBOL(dma_free_coherent);
  71. /*
  72. * The map routines "map" the specified address range for DMA
  73. * accesses. The memory belongs to the device after this call is
  74. * issued, until it is unmapped with dma_unmap_single.
  75. *
  76. * We don't need to do any mapping, we just flush the address range
  77. * out of the cache and return a DMA address.
  78. *
  79. * The unmap routines do whatever is necessary before the processor
  80. * accesses the memory again, and must be called before the driver
  81. * touches the memory. We can get away with a cache invalidate if we
  82. * can count on nothing having been touched.
  83. */
  84. /* Set up a single page for DMA access. */
  85. static void __dma_prep_page(struct page *page, unsigned long offset,
  86. size_t size, enum dma_data_direction direction)
  87. {
  88. /*
  89. * Flush the page from cache if necessary.
  90. * On tilegx, data is delivered to hash-for-home L3; on tilepro,
  91. * data is delivered direct to memory.
  92. *
  93. * NOTE: If we were just doing DMA_TO_DEVICE we could optimize
  94. * this to be a "flush" not a "finv" and keep some of the
  95. * state in cache across the DMA operation, but it doesn't seem
  96. * worth creating the necessary flush_buffer_xxx() infrastructure.
  97. */
  98. int home = page_home(page);
  99. switch (home) {
  100. case PAGE_HOME_HASH:
  101. #ifdef __tilegx__
  102. return;
  103. #endif
  104. break;
  105. case PAGE_HOME_UNCACHED:
  106. #ifdef __tilepro__
  107. return;
  108. #endif
  109. break;
  110. case PAGE_HOME_IMMUTABLE:
  111. /* Should be going to the device only. */
  112. BUG_ON(direction == DMA_FROM_DEVICE ||
  113. direction == DMA_BIDIRECTIONAL);
  114. return;
  115. case PAGE_HOME_INCOHERENT:
  116. /* Incoherent anyway, so no need to work hard here. */
  117. return;
  118. default:
  119. BUG_ON(home < 0 || home >= NR_CPUS);
  120. break;
  121. }
  122. homecache_finv_page(page);
  123. #ifdef DEBUG_ALIGNMENT
  124. /* Warn if the region isn't cacheline aligned. */
  125. if (offset & (L2_CACHE_BYTES - 1) || (size & (L2_CACHE_BYTES - 1)))
  126. pr_warn("Unaligned DMA to non-hfh memory: PA %#llx/%#lx\n",
  127. PFN_PHYS(page_to_pfn(page)) + offset, size);
  128. #endif
  129. }
  130. /* Make the page ready to be read by the core. */
  131. static void __dma_complete_page(struct page *page, unsigned long offset,
  132. size_t size, enum dma_data_direction direction)
  133. {
  134. #ifdef __tilegx__
  135. switch (page_home(page)) {
  136. case PAGE_HOME_HASH:
  137. /* I/O device delivered data the way the cpu wanted it. */
  138. break;
  139. case PAGE_HOME_INCOHERENT:
  140. /* Incoherent anyway, so no need to work hard here. */
  141. break;
  142. case PAGE_HOME_IMMUTABLE:
  143. /* Extra read-only copies are not a problem. */
  144. break;
  145. default:
  146. /* Flush the bogus hash-for-home I/O entries to memory. */
  147. homecache_finv_map_page(page, PAGE_HOME_HASH);
  148. break;
  149. }
  150. #endif
  151. }
  152. static void __dma_prep_pa_range(dma_addr_t dma_addr, size_t size,
  153. enum dma_data_direction direction)
  154. {
  155. struct page *page = pfn_to_page(PFN_DOWN(dma_addr));
  156. unsigned long offset = dma_addr & (PAGE_SIZE - 1);
  157. size_t bytes = min(size, (size_t)(PAGE_SIZE - offset));
  158. while (size != 0) {
  159. __dma_prep_page(page, offset, bytes, direction);
  160. size -= bytes;
  161. ++page;
  162. offset = 0;
  163. bytes = min((size_t)PAGE_SIZE, size);
  164. }
  165. }
  166. static void __dma_complete_pa_range(dma_addr_t dma_addr, size_t size,
  167. enum dma_data_direction direction)
  168. {
  169. struct page *page = pfn_to_page(PFN_DOWN(dma_addr));
  170. unsigned long offset = dma_addr & (PAGE_SIZE - 1);
  171. size_t bytes = min(size, (size_t)(PAGE_SIZE - offset));
  172. while (size != 0) {
  173. __dma_complete_page(page, offset, bytes, direction);
  174. size -= bytes;
  175. ++page;
  176. offset = 0;
  177. bytes = min((size_t)PAGE_SIZE, size);
  178. }
  179. }
  180. /*
  181. * dma_map_single can be passed any memory address, and there appear
  182. * to be no alignment constraints.
  183. *
  184. * There is a chance that the start of the buffer will share a cache
  185. * line with some other data that has been touched in the meantime.
  186. */
  187. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  188. enum dma_data_direction direction)
  189. {
  190. dma_addr_t dma_addr = __pa(ptr);
  191. BUG_ON(!valid_dma_direction(direction));
  192. WARN_ON(size == 0);
  193. __dma_prep_pa_range(dma_addr, size, direction);
  194. return dma_addr;
  195. }
  196. EXPORT_SYMBOL(dma_map_single);
  197. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  198. enum dma_data_direction direction)
  199. {
  200. BUG_ON(!valid_dma_direction(direction));
  201. __dma_complete_pa_range(dma_addr, size, direction);
  202. }
  203. EXPORT_SYMBOL(dma_unmap_single);
  204. int dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
  205. enum dma_data_direction direction)
  206. {
  207. struct scatterlist *sg;
  208. int i;
  209. BUG_ON(!valid_dma_direction(direction));
  210. WARN_ON(nents == 0 || sglist->length == 0);
  211. for_each_sg(sglist, sg, nents, i) {
  212. sg->dma_address = sg_phys(sg);
  213. __dma_prep_pa_range(sg->dma_address, sg->length, direction);
  214. }
  215. return nents;
  216. }
  217. EXPORT_SYMBOL(dma_map_sg);
  218. void dma_unmap_sg(struct device *dev, struct scatterlist *sglist, int nents,
  219. enum dma_data_direction direction)
  220. {
  221. struct scatterlist *sg;
  222. int i;
  223. BUG_ON(!valid_dma_direction(direction));
  224. for_each_sg(sglist, sg, nents, i) {
  225. sg->dma_address = sg_phys(sg);
  226. __dma_complete_pa_range(sg->dma_address, sg->length,
  227. direction);
  228. }
  229. }
  230. EXPORT_SYMBOL(dma_unmap_sg);
  231. dma_addr_t dma_map_page(struct device *dev, struct page *page,
  232. unsigned long offset, size_t size,
  233. enum dma_data_direction direction)
  234. {
  235. BUG_ON(!valid_dma_direction(direction));
  236. BUG_ON(offset + size > PAGE_SIZE);
  237. __dma_prep_page(page, offset, size, direction);
  238. return page_to_pa(page) + offset;
  239. }
  240. EXPORT_SYMBOL(dma_map_page);
  241. void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  242. enum dma_data_direction direction)
  243. {
  244. BUG_ON(!valid_dma_direction(direction));
  245. __dma_complete_page(pfn_to_page(PFN_DOWN(dma_address)),
  246. dma_address & PAGE_OFFSET, size, direction);
  247. }
  248. EXPORT_SYMBOL(dma_unmap_page);
  249. void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  250. size_t size, enum dma_data_direction direction)
  251. {
  252. BUG_ON(!valid_dma_direction(direction));
  253. __dma_complete_pa_range(dma_handle, size, direction);
  254. }
  255. EXPORT_SYMBOL(dma_sync_single_for_cpu);
  256. void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  257. size_t size, enum dma_data_direction direction)
  258. {
  259. __dma_prep_pa_range(dma_handle, size, direction);
  260. }
  261. EXPORT_SYMBOL(dma_sync_single_for_device);
  262. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sglist,
  263. int nelems, enum dma_data_direction direction)
  264. {
  265. struct scatterlist *sg;
  266. int i;
  267. BUG_ON(!valid_dma_direction(direction));
  268. WARN_ON(nelems == 0 || sglist->length == 0);
  269. for_each_sg(sglist, sg, nelems, i) {
  270. dma_sync_single_for_cpu(dev, sg->dma_address,
  271. sg_dma_len(sg), direction);
  272. }
  273. }
  274. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  275. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist,
  276. int nelems, enum dma_data_direction direction)
  277. {
  278. struct scatterlist *sg;
  279. int i;
  280. BUG_ON(!valid_dma_direction(direction));
  281. WARN_ON(nelems == 0 || sglist->length == 0);
  282. for_each_sg(sglist, sg, nelems, i) {
  283. dma_sync_single_for_device(dev, sg->dma_address,
  284. sg_dma_len(sg), direction);
  285. }
  286. }
  287. EXPORT_SYMBOL(dma_sync_sg_for_device);
  288. void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  289. unsigned long offset, size_t size,
  290. enum dma_data_direction direction)
  291. {
  292. dma_sync_single_for_cpu(dev, dma_handle + offset, size, direction);
  293. }
  294. EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
  295. void dma_sync_single_range_for_device(struct device *dev,
  296. dma_addr_t dma_handle,
  297. unsigned long offset, size_t size,
  298. enum dma_data_direction direction)
  299. {
  300. dma_sync_single_for_device(dev, dma_handle + offset, size, direction);
  301. }
  302. EXPORT_SYMBOL(dma_sync_single_range_for_device);
  303. /*
  304. * dma_alloc_noncoherent() is #defined to return coherent memory,
  305. * so there's no need to do any flushing here.
  306. */
  307. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  308. enum dma_data_direction direction)
  309. {
  310. }
  311. EXPORT_SYMBOL(dma_cache_sync);