pci-dma.c 10.0 KB

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