pci-dma.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/dma-map-ops.h>
  3. #include <linux/dma-direct.h>
  4. #include <linux/iommu.h>
  5. #include <linux/dmar.h>
  6. #include <linux/export.h>
  7. #include <linux/memblock.h>
  8. #include <linux/gfp.h>
  9. #include <linux/pci.h>
  10. #include <linux/amd-iommu.h>
  11. #include <asm/proto.h>
  12. #include <asm/dma.h>
  13. #include <asm/iommu.h>
  14. #include <asm/gart.h>
  15. #include <asm/x86_init.h>
  16. #include <xen/xen.h>
  17. #include <xen/swiotlb-xen.h>
  18. static bool disable_dac_quirk __read_mostly;
  19. const struct dma_map_ops *dma_ops;
  20. EXPORT_SYMBOL(dma_ops);
  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. #ifdef CONFIG_SWIOTLB
  33. bool x86_swiotlb_enable;
  34. static unsigned int x86_swiotlb_flags;
  35. static void __init pci_swiotlb_detect(void)
  36. {
  37. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  38. if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
  39. x86_swiotlb_enable = true;
  40. /*
  41. * Set swiotlb to 1 so that bounce buffers are allocated and used for
  42. * devices that can't support DMA to encrypted memory.
  43. */
  44. if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
  45. x86_swiotlb_enable = true;
  46. /*
  47. * Guest with guest memory encryption currently perform all DMA through
  48. * bounce buffers as the hypervisor can't access arbitrary VM memory
  49. * that is not explicitly shared with it.
  50. */
  51. if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
  52. x86_swiotlb_enable = true;
  53. x86_swiotlb_flags |= SWIOTLB_FORCE;
  54. }
  55. }
  56. #else
  57. static inline void __init pci_swiotlb_detect(void)
  58. {
  59. }
  60. #define x86_swiotlb_flags 0
  61. #endif /* CONFIG_SWIOTLB */
  62. #ifdef CONFIG_SWIOTLB_XEN
  63. static bool xen_swiotlb_enabled(void)
  64. {
  65. return xen_initial_domain() || x86_swiotlb_enable ||
  66. (IS_ENABLED(CONFIG_XEN_PCIDEV_FRONTEND) && xen_pv_pci_possible);
  67. }
  68. static void __init pci_xen_swiotlb_init(void)
  69. {
  70. if (!xen_swiotlb_enabled())
  71. return;
  72. x86_swiotlb_enable = true;
  73. x86_swiotlb_flags |= SWIOTLB_ANY;
  74. swiotlb_init_remap(true, x86_swiotlb_flags, xen_swiotlb_fixup);
  75. dma_ops = &xen_swiotlb_dma_ops;
  76. if (IS_ENABLED(CONFIG_PCI))
  77. pci_request_acs();
  78. }
  79. #else
  80. static inline void __init pci_xen_swiotlb_init(void)
  81. {
  82. }
  83. #endif /* CONFIG_SWIOTLB_XEN */
  84. void __init pci_iommu_alloc(void)
  85. {
  86. if (xen_pv_domain()) {
  87. pci_xen_swiotlb_init();
  88. return;
  89. }
  90. pci_swiotlb_detect();
  91. gart_iommu_hole_init();
  92. amd_iommu_detect();
  93. detect_intel_iommu();
  94. swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
  95. }
  96. /*
  97. * See <Documentation/arch/x86/x86_64/boot-options.rst> for the iommu kernel
  98. * parameter documentation.
  99. */
  100. static __init int iommu_setup(char *p)
  101. {
  102. iommu_merge = 1;
  103. if (!p)
  104. return -EINVAL;
  105. while (*p) {
  106. if (!strncmp(p, "off", 3))
  107. no_iommu = 1;
  108. /* gart_parse_options has more force support */
  109. if (!strncmp(p, "force", 5))
  110. force_iommu = 1;
  111. if (!strncmp(p, "noforce", 7)) {
  112. iommu_merge = 0;
  113. force_iommu = 0;
  114. }
  115. if (!strncmp(p, "biomerge", 8)) {
  116. iommu_merge = 1;
  117. force_iommu = 1;
  118. }
  119. if (!strncmp(p, "panic", 5))
  120. panic_on_overflow = 1;
  121. if (!strncmp(p, "nopanic", 7))
  122. panic_on_overflow = 0;
  123. if (!strncmp(p, "merge", 5)) {
  124. iommu_merge = 1;
  125. force_iommu = 1;
  126. }
  127. if (!strncmp(p, "nomerge", 7))
  128. iommu_merge = 0;
  129. if (!strncmp(p, "forcesac", 8))
  130. pr_warn("forcesac option ignored.\n");
  131. if (!strncmp(p, "allowdac", 8))
  132. pr_warn("allowdac option ignored.\n");
  133. if (!strncmp(p, "nodac", 5))
  134. pr_warn("nodac option ignored.\n");
  135. if (!strncmp(p, "usedac", 6)) {
  136. disable_dac_quirk = true;
  137. return 1;
  138. }
  139. #ifdef CONFIG_SWIOTLB
  140. if (!strncmp(p, "soft", 4))
  141. x86_swiotlb_enable = true;
  142. #endif
  143. if (!strncmp(p, "pt", 2))
  144. iommu_set_default_passthrough(true);
  145. if (!strncmp(p, "nopt", 4))
  146. iommu_set_default_translated(true);
  147. gart_parse_options(p);
  148. p += strcspn(p, ",");
  149. if (*p == ',')
  150. ++p;
  151. }
  152. return 0;
  153. }
  154. early_param("iommu", iommu_setup);
  155. static int __init pci_iommu_init(void)
  156. {
  157. x86_init.iommu.iommu_init();
  158. #ifdef CONFIG_SWIOTLB
  159. /* An IOMMU turned us off. */
  160. if (x86_swiotlb_enable) {
  161. pr_info("PCI-DMA: Using software bounce buffering for IO (SWIOTLB)\n");
  162. swiotlb_print_info();
  163. } else {
  164. swiotlb_exit();
  165. }
  166. #endif
  167. return 0;
  168. }
  169. /* Must execute after PCI subsystem */
  170. rootfs_initcall(pci_iommu_init);
  171. #ifdef CONFIG_PCI
  172. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  173. static int via_no_dac_cb(struct pci_dev *pdev, void *data)
  174. {
  175. pdev->dev.bus_dma_limit = DMA_BIT_MASK(32);
  176. return 0;
  177. }
  178. static void via_no_dac(struct pci_dev *dev)
  179. {
  180. if (!disable_dac_quirk) {
  181. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  182. pci_walk_bus(dev->subordinate, via_no_dac_cb, NULL);
  183. }
  184. }
  185. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  186. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  187. #endif