pci-dma.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/dma-mapping.h>
  3. #include <linux/dma-debug.h>
  4. #include <linux/dmar.h>
  5. #include <linux/export.h>
  6. #include <linux/bootmem.h>
  7. #include <linux/gfp.h>
  8. #include <linux/pci.h>
  9. #include <linux/kmemleak.h>
  10. #include <asm/proto.h>
  11. #include <asm/dma.h>
  12. #include <asm/iommu.h>
  13. #include <asm/gart.h>
  14. #include <asm/calgary.h>
  15. #include <asm/x86_init.h>
  16. #include <asm/iommu_table.h>
  17. static int forbid_dac __read_mostly;
  18. const struct dma_map_ops *dma_ops = &nommu_dma_ops;
  19. EXPORT_SYMBOL(dma_ops);
  20. static int iommu_sac_force __read_mostly;
  21. #ifdef CONFIG_IOMMU_DEBUG
  22. int panic_on_overflow __read_mostly = 1;
  23. int force_iommu __read_mostly = 1;
  24. #else
  25. int panic_on_overflow __read_mostly = 0;
  26. int force_iommu __read_mostly = 0;
  27. #endif
  28. int iommu_merge __read_mostly = 0;
  29. int no_iommu __read_mostly;
  30. /* Set this to 1 if there is a HW IOMMU in the system */
  31. int iommu_detected __read_mostly = 0;
  32. /*
  33. * This variable becomes 1 if iommu=pt is passed on the kernel command line.
  34. * If this variable is 1, IOMMU implementations do no DMA translation for
  35. * devices and allow every device to access to whole physical memory. This is
  36. * useful if a user wants to use an IOMMU only for KVM device assignment to
  37. * guests and not for driver dma translation.
  38. */
  39. int iommu_pass_through __read_mostly;
  40. extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
  41. /* Dummy device used for NULL arguments (normally ISA). */
  42. struct device x86_dma_fallback_dev = {
  43. .init_name = "fallback device",
  44. .coherent_dma_mask = ISA_DMA_BIT_MASK,
  45. .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
  46. };
  47. EXPORT_SYMBOL(x86_dma_fallback_dev);
  48. /* Number of entries preallocated for DMA-API debugging */
  49. #define PREALLOC_DMA_DEBUG_ENTRIES 65536
  50. void __init pci_iommu_alloc(void)
  51. {
  52. struct iommu_table_entry *p;
  53. sort_iommu_table(__iommu_table, __iommu_table_end);
  54. check_iommu_entries(__iommu_table, __iommu_table_end);
  55. for (p = __iommu_table; p < __iommu_table_end; p++) {
  56. if (p && p->detect && p->detect() > 0) {
  57. p->flags |= IOMMU_DETECTED;
  58. if (p->early_init)
  59. p->early_init();
  60. if (p->flags & IOMMU_FINISH_IF_DETECTED)
  61. break;
  62. }
  63. }
  64. }
  65. void *dma_generic_alloc_coherent(struct device *dev, size_t size,
  66. dma_addr_t *dma_addr, gfp_t flag,
  67. unsigned long attrs)
  68. {
  69. unsigned long dma_mask;
  70. struct page *page;
  71. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  72. dma_addr_t addr;
  73. dma_mask = dma_alloc_coherent_mask(dev, flag);
  74. flag &= ~__GFP_ZERO;
  75. again:
  76. page = NULL;
  77. /* CMA can be used only in the context which permits sleeping */
  78. if (gfpflags_allow_blocking(flag)) {
  79. page = dma_alloc_from_contiguous(dev, count, get_order(size),
  80. flag);
  81. if (page) {
  82. addr = phys_to_dma(dev, page_to_phys(page));
  83. if (addr + size > dma_mask) {
  84. dma_release_from_contiguous(dev, page, count);
  85. page = NULL;
  86. }
  87. }
  88. }
  89. /* fallback */
  90. if (!page)
  91. page = alloc_pages_node(dev_to_node(dev), flag, get_order(size));
  92. if (!page)
  93. return NULL;
  94. addr = phys_to_dma(dev, page_to_phys(page));
  95. if (addr + size > dma_mask) {
  96. __free_pages(page, get_order(size));
  97. if (dma_mask < DMA_BIT_MASK(32) && !(flag & GFP_DMA)) {
  98. flag = (flag & ~GFP_DMA32) | GFP_DMA;
  99. goto again;
  100. }
  101. return NULL;
  102. }
  103. memset(page_address(page), 0, size);
  104. *dma_addr = addr;
  105. return page_address(page);
  106. }
  107. void dma_generic_free_coherent(struct device *dev, size_t size, void *vaddr,
  108. dma_addr_t dma_addr, unsigned long attrs)
  109. {
  110. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  111. struct page *page = virt_to_page(vaddr);
  112. if (!dma_release_from_contiguous(dev, page, count))
  113. free_pages((unsigned long)vaddr, get_order(size));
  114. }
  115. bool arch_dma_alloc_attrs(struct device **dev, gfp_t *gfp)
  116. {
  117. if (!*dev)
  118. *dev = &x86_dma_fallback_dev;
  119. *gfp &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
  120. *gfp = dma_alloc_coherent_gfp_flags(*dev, *gfp);
  121. if (!is_device_dma_capable(*dev))
  122. return false;
  123. return true;
  124. }
  125. EXPORT_SYMBOL(arch_dma_alloc_attrs);
  126. /*
  127. * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  128. * parameter documentation.
  129. */
  130. static __init int iommu_setup(char *p)
  131. {
  132. iommu_merge = 1;
  133. if (!p)
  134. return -EINVAL;
  135. while (*p) {
  136. if (!strncmp(p, "off", 3))
  137. no_iommu = 1;
  138. /* gart_parse_options has more force support */
  139. if (!strncmp(p, "force", 5))
  140. force_iommu = 1;
  141. if (!strncmp(p, "noforce", 7)) {
  142. iommu_merge = 0;
  143. force_iommu = 0;
  144. }
  145. if (!strncmp(p, "biomerge", 8)) {
  146. iommu_merge = 1;
  147. force_iommu = 1;
  148. }
  149. if (!strncmp(p, "panic", 5))
  150. panic_on_overflow = 1;
  151. if (!strncmp(p, "nopanic", 7))
  152. panic_on_overflow = 0;
  153. if (!strncmp(p, "merge", 5)) {
  154. iommu_merge = 1;
  155. force_iommu = 1;
  156. }
  157. if (!strncmp(p, "nomerge", 7))
  158. iommu_merge = 0;
  159. if (!strncmp(p, "forcesac", 8))
  160. iommu_sac_force = 1;
  161. if (!strncmp(p, "allowdac", 8))
  162. forbid_dac = 0;
  163. if (!strncmp(p, "nodac", 5))
  164. forbid_dac = 1;
  165. if (!strncmp(p, "usedac", 6)) {
  166. forbid_dac = -1;
  167. return 1;
  168. }
  169. #ifdef CONFIG_SWIOTLB
  170. if (!strncmp(p, "soft", 4))
  171. swiotlb = 1;
  172. #endif
  173. if (!strncmp(p, "pt", 2))
  174. iommu_pass_through = 1;
  175. gart_parse_options(p);
  176. #ifdef CONFIG_CALGARY_IOMMU
  177. if (!strncmp(p, "calgary", 7))
  178. use_calgary = 1;
  179. #endif /* CONFIG_CALGARY_IOMMU */
  180. p += strcspn(p, ",");
  181. if (*p == ',')
  182. ++p;
  183. }
  184. return 0;
  185. }
  186. early_param("iommu", iommu_setup);
  187. int x86_dma_supported(struct device *dev, u64 mask)
  188. {
  189. #ifdef CONFIG_PCI
  190. if (mask > 0xffffffff && forbid_dac > 0) {
  191. dev_info(dev, "PCI: Disallowing DAC for device\n");
  192. return 0;
  193. }
  194. #endif
  195. /* Copied from i386. Doesn't make much sense, because it will
  196. only work for pci_alloc_coherent.
  197. The caller just has to use GFP_DMA in this case. */
  198. if (mask < DMA_BIT_MASK(24))
  199. return 0;
  200. /* Tell the device to use SAC when IOMMU force is on. This
  201. allows the driver to use cheaper accesses in some cases.
  202. Problem with this is that if we overflow the IOMMU area and
  203. return DAC as fallback address the device may not handle it
  204. correctly.
  205. As a special case some controllers have a 39bit address
  206. mode that is as efficient as 32bit (aic79xx). Don't force
  207. SAC for these. Assume all masks <= 40 bits are of this
  208. type. Normally this doesn't make any difference, but gives
  209. more gentle handling of IOMMU overflow. */
  210. if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
  211. dev_info(dev, "Force SAC with mask %Lx\n", mask);
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. static int __init pci_iommu_init(void)
  217. {
  218. struct iommu_table_entry *p;
  219. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  220. #ifdef CONFIG_PCI
  221. dma_debug_add_bus(&pci_bus_type);
  222. #endif
  223. x86_init.iommu.iommu_init();
  224. for (p = __iommu_table; p < __iommu_table_end; p++) {
  225. if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
  226. p->late_init();
  227. }
  228. return 0;
  229. }
  230. /* Must execute after PCI subsystem */
  231. rootfs_initcall(pci_iommu_init);
  232. #ifdef CONFIG_PCI
  233. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  234. static void via_no_dac(struct pci_dev *dev)
  235. {
  236. if (forbid_dac == 0) {
  237. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  238. forbid_dac = 1;
  239. }
  240. }
  241. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  242. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  243. #endif