dma.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  3. *
  4. * Provide default implementations of the DMA mapping callbacks for
  5. * directly mapped busses.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/dma-debug.h>
  10. #include <linux/gfp.h>
  11. #include <linux/memblock.h>
  12. #include <linux/export.h>
  13. #include <linux/pci.h>
  14. #include <asm/vio.h>
  15. #include <asm/bug.h>
  16. #include <asm/machdep.h>
  17. #include <asm/swiotlb.h>
  18. #include <asm/iommu.h>
  19. /*
  20. * Generic direct DMA implementation
  21. *
  22. * This implementation supports a per-device offset that can be applied if
  23. * the address at which memory is visible to devices is not 0. Platform code
  24. * can set archdata.dma_data to an unsigned long holding the offset. By
  25. * default the offset is PCI_DRAM_OFFSET.
  26. */
  27. static u64 __maybe_unused get_pfn_limit(struct device *dev)
  28. {
  29. u64 pfn = (dev->coherent_dma_mask >> PAGE_SHIFT) + 1;
  30. struct dev_archdata __maybe_unused *sd = &dev->archdata;
  31. #ifdef CONFIG_SWIOTLB
  32. if (sd->max_direct_dma_addr && dev->dma_ops == &powerpc_swiotlb_dma_ops)
  33. pfn = min_t(u64, pfn, sd->max_direct_dma_addr >> PAGE_SHIFT);
  34. #endif
  35. return pfn;
  36. }
  37. static int dma_nommu_dma_supported(struct device *dev, u64 mask)
  38. {
  39. #ifdef CONFIG_PPC64
  40. u64 limit = get_dma_offset(dev) + (memblock_end_of_DRAM() - 1);
  41. /* Limit fits in the mask, we are good */
  42. if (mask >= limit)
  43. return 1;
  44. #ifdef CONFIG_FSL_SOC
  45. /* Freescale gets another chance via ZONE_DMA/ZONE_DMA32, however
  46. * that will have to be refined if/when they support iommus
  47. */
  48. return 1;
  49. #endif
  50. /* Sorry ... */
  51. return 0;
  52. #else
  53. return 1;
  54. #endif
  55. }
  56. void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
  57. dma_addr_t *dma_handle, gfp_t flag,
  58. unsigned long attrs)
  59. {
  60. void *ret;
  61. #ifdef CONFIG_NOT_COHERENT_CACHE
  62. ret = __dma_alloc_coherent(dev, size, dma_handle, flag);
  63. if (ret == NULL)
  64. return NULL;
  65. *dma_handle += get_dma_offset(dev);
  66. return ret;
  67. #else
  68. struct page *page;
  69. int node = dev_to_node(dev);
  70. #ifdef CONFIG_FSL_SOC
  71. u64 pfn = get_pfn_limit(dev);
  72. int zone;
  73. /*
  74. * This code should be OK on other platforms, but we have drivers that
  75. * don't set coherent_dma_mask. As a workaround we just ifdef it. This
  76. * whole routine needs some serious cleanup.
  77. */
  78. zone = dma_pfn_limit_to_zone(pfn);
  79. if (zone < 0) {
  80. dev_err(dev, "%s: No suitable zone for pfn %#llx\n",
  81. __func__, pfn);
  82. return NULL;
  83. }
  84. switch (zone) {
  85. case ZONE_DMA:
  86. flag |= GFP_DMA;
  87. break;
  88. #ifdef CONFIG_ZONE_DMA32
  89. case ZONE_DMA32:
  90. flag |= GFP_DMA32;
  91. break;
  92. #endif
  93. };
  94. #endif /* CONFIG_FSL_SOC */
  95. page = alloc_pages_node(node, flag, get_order(size));
  96. if (page == NULL)
  97. return NULL;
  98. ret = page_address(page);
  99. memset(ret, 0, size);
  100. *dma_handle = __pa(ret) + get_dma_offset(dev);
  101. return ret;
  102. #endif
  103. }
  104. void __dma_nommu_free_coherent(struct device *dev, size_t size,
  105. void *vaddr, dma_addr_t dma_handle,
  106. unsigned long attrs)
  107. {
  108. #ifdef CONFIG_NOT_COHERENT_CACHE
  109. __dma_free_coherent(size, vaddr);
  110. #else
  111. free_pages((unsigned long)vaddr, get_order(size));
  112. #endif
  113. }
  114. static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
  115. dma_addr_t *dma_handle, gfp_t flag,
  116. unsigned long attrs)
  117. {
  118. struct iommu_table *iommu;
  119. /* The coherent mask may be smaller than the real mask, check if
  120. * we can really use the direct ops
  121. */
  122. if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
  123. return __dma_nommu_alloc_coherent(dev, size, dma_handle,
  124. flag, attrs);
  125. /* Ok we can't ... do we have an iommu ? If not, fail */
  126. iommu = get_iommu_table_base(dev);
  127. if (!iommu)
  128. return NULL;
  129. /* Try to use the iommu */
  130. return iommu_alloc_coherent(dev, iommu, size, dma_handle,
  131. dev->coherent_dma_mask, flag,
  132. dev_to_node(dev));
  133. }
  134. static void dma_nommu_free_coherent(struct device *dev, size_t size,
  135. void *vaddr, dma_addr_t dma_handle,
  136. unsigned long attrs)
  137. {
  138. struct iommu_table *iommu;
  139. /* See comments in dma_nommu_alloc_coherent() */
  140. if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
  141. return __dma_nommu_free_coherent(dev, size, vaddr, dma_handle,
  142. attrs);
  143. /* Maybe we used an iommu ... */
  144. iommu = get_iommu_table_base(dev);
  145. /* If we hit that we should have never allocated in the first
  146. * place so how come we are freeing ?
  147. */
  148. if (WARN_ON(!iommu))
  149. return;
  150. iommu_free_coherent(iommu, size, vaddr, dma_handle);
  151. }
  152. int dma_nommu_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  153. void *cpu_addr, dma_addr_t handle, size_t size,
  154. unsigned long attrs)
  155. {
  156. unsigned long pfn;
  157. #ifdef CONFIG_NOT_COHERENT_CACHE
  158. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  159. pfn = __dma_get_coherent_pfn((unsigned long)cpu_addr);
  160. #else
  161. pfn = page_to_pfn(virt_to_page(cpu_addr));
  162. #endif
  163. return remap_pfn_range(vma, vma->vm_start,
  164. pfn + vma->vm_pgoff,
  165. vma->vm_end - vma->vm_start,
  166. vma->vm_page_prot);
  167. }
  168. static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
  169. int nents, enum dma_data_direction direction,
  170. unsigned long attrs)
  171. {
  172. struct scatterlist *sg;
  173. int i;
  174. for_each_sg(sgl, sg, nents, i) {
  175. sg->dma_address = sg_phys(sg) + get_dma_offset(dev);
  176. sg->dma_length = sg->length;
  177. if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
  178. continue;
  179. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  180. }
  181. return nents;
  182. }
  183. static void dma_nommu_unmap_sg(struct device *dev, struct scatterlist *sg,
  184. int nents, enum dma_data_direction direction,
  185. unsigned long attrs)
  186. {
  187. }
  188. static u64 dma_nommu_get_required_mask(struct device *dev)
  189. {
  190. u64 end, mask;
  191. end = memblock_end_of_DRAM() + get_dma_offset(dev);
  192. mask = 1ULL << (fls64(end) - 1);
  193. mask += mask - 1;
  194. return mask;
  195. }
  196. static inline dma_addr_t dma_nommu_map_page(struct device *dev,
  197. struct page *page,
  198. unsigned long offset,
  199. size_t size,
  200. enum dma_data_direction dir,
  201. unsigned long attrs)
  202. {
  203. BUG_ON(dir == DMA_NONE);
  204. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  205. __dma_sync_page(page, offset, size, dir);
  206. return page_to_phys(page) + offset + get_dma_offset(dev);
  207. }
  208. static inline void dma_nommu_unmap_page(struct device *dev,
  209. dma_addr_t dma_address,
  210. size_t size,
  211. enum dma_data_direction direction,
  212. unsigned long attrs)
  213. {
  214. }
  215. #ifdef CONFIG_NOT_COHERENT_CACHE
  216. static inline void dma_nommu_sync_sg(struct device *dev,
  217. struct scatterlist *sgl, int nents,
  218. enum dma_data_direction direction)
  219. {
  220. struct scatterlist *sg;
  221. int i;
  222. for_each_sg(sgl, sg, nents, i)
  223. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  224. }
  225. static inline void dma_nommu_sync_single(struct device *dev,
  226. dma_addr_t dma_handle, size_t size,
  227. enum dma_data_direction direction)
  228. {
  229. __dma_sync(bus_to_virt(dma_handle), size, direction);
  230. }
  231. #endif
  232. const struct dma_map_ops dma_nommu_ops = {
  233. .alloc = dma_nommu_alloc_coherent,
  234. .free = dma_nommu_free_coherent,
  235. .mmap = dma_nommu_mmap_coherent,
  236. .map_sg = dma_nommu_map_sg,
  237. .unmap_sg = dma_nommu_unmap_sg,
  238. .dma_supported = dma_nommu_dma_supported,
  239. .map_page = dma_nommu_map_page,
  240. .unmap_page = dma_nommu_unmap_page,
  241. .get_required_mask = dma_nommu_get_required_mask,
  242. #ifdef CONFIG_NOT_COHERENT_CACHE
  243. .sync_single_for_cpu = dma_nommu_sync_single,
  244. .sync_single_for_device = dma_nommu_sync_single,
  245. .sync_sg_for_cpu = dma_nommu_sync_sg,
  246. .sync_sg_for_device = dma_nommu_sync_sg,
  247. #endif
  248. };
  249. EXPORT_SYMBOL(dma_nommu_ops);
  250. int dma_set_coherent_mask(struct device *dev, u64 mask)
  251. {
  252. if (!dma_supported(dev, mask)) {
  253. /*
  254. * We need to special case the direct DMA ops which can
  255. * support a fallback for coherent allocations. There
  256. * is no dma_op->set_coherent_mask() so we have to do
  257. * things the hard way:
  258. */
  259. if (get_dma_ops(dev) != &dma_nommu_ops ||
  260. get_iommu_table_base(dev) == NULL ||
  261. !dma_iommu_dma_supported(dev, mask))
  262. return -EIO;
  263. }
  264. dev->coherent_dma_mask = mask;
  265. return 0;
  266. }
  267. EXPORT_SYMBOL(dma_set_coherent_mask);
  268. int dma_set_mask(struct device *dev, u64 dma_mask)
  269. {
  270. if (ppc_md.dma_set_mask)
  271. return ppc_md.dma_set_mask(dev, dma_mask);
  272. if (dev_is_pci(dev)) {
  273. struct pci_dev *pdev = to_pci_dev(dev);
  274. struct pci_controller *phb = pci_bus_to_host(pdev->bus);
  275. if (phb->controller_ops.dma_set_mask)
  276. return phb->controller_ops.dma_set_mask(pdev, dma_mask);
  277. }
  278. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  279. return -EIO;
  280. *dev->dma_mask = dma_mask;
  281. return 0;
  282. }
  283. EXPORT_SYMBOL(dma_set_mask);
  284. u64 __dma_get_required_mask(struct device *dev)
  285. {
  286. const struct dma_map_ops *dma_ops = get_dma_ops(dev);
  287. if (unlikely(dma_ops == NULL))
  288. return 0;
  289. if (dma_ops->get_required_mask)
  290. return dma_ops->get_required_mask(dev);
  291. return DMA_BIT_MASK(8 * sizeof(dma_addr_t));
  292. }
  293. u64 dma_get_required_mask(struct device *dev)
  294. {
  295. if (ppc_md.dma_get_required_mask)
  296. return ppc_md.dma_get_required_mask(dev);
  297. if (dev_is_pci(dev)) {
  298. struct pci_dev *pdev = to_pci_dev(dev);
  299. struct pci_controller *phb = pci_bus_to_host(pdev->bus);
  300. if (phb->controller_ops.dma_get_required_mask)
  301. return phb->controller_ops.dma_get_required_mask(pdev);
  302. }
  303. return __dma_get_required_mask(dev);
  304. }
  305. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  306. static int __init dma_init(void)
  307. {
  308. #ifdef CONFIG_PCI
  309. dma_debug_add_bus(&pci_bus_type);
  310. #endif
  311. #ifdef CONFIG_IBMVIO
  312. dma_debug_add_bus(&vio_bus_type);
  313. #endif
  314. return 0;
  315. }
  316. fs_initcall(dma_init);