pci-dma.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/dma-direct.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 <asm/proto.h>
  10. #include <asm/dma.h>
  11. #include <asm/iommu.h>
  12. #include <asm/gart.h>
  13. #include <asm/calgary.h>
  14. #include <asm/x86_init.h>
  15. #include <asm/iommu_table.h>
  16. static int forbid_dac __read_mostly;
  17. const struct dma_map_ops *dma_ops = &dma_direct_ops;
  18. EXPORT_SYMBOL(dma_ops);
  19. static int iommu_sac_force __read_mostly;
  20. #ifdef CONFIG_IOMMU_DEBUG
  21. int panic_on_overflow __read_mostly = 1;
  22. int force_iommu __read_mostly = 1;
  23. #else
  24. int panic_on_overflow __read_mostly = 0;
  25. int force_iommu __read_mostly = 0;
  26. #endif
  27. int iommu_merge __read_mostly = 0;
  28. int no_iommu __read_mostly;
  29. /* Set this to 1 if there is a HW IOMMU in the system */
  30. int iommu_detected __read_mostly = 0;
  31. /*
  32. * This variable becomes 1 if iommu=pt is passed on the kernel command line.
  33. * If this variable is 1, IOMMU implementations do no DMA translation for
  34. * devices and allow every device to access to whole physical memory. This is
  35. * useful if a user wants to use an IOMMU only for KVM device assignment to
  36. * guests and not for driver dma translation.
  37. */
  38. int iommu_pass_through __read_mostly;
  39. extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
  40. /* Dummy device used for NULL arguments (normally ISA). */
  41. struct device x86_dma_fallback_dev = {
  42. .init_name = "fallback device",
  43. .coherent_dma_mask = ISA_DMA_BIT_MASK,
  44. .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask,
  45. };
  46. EXPORT_SYMBOL(x86_dma_fallback_dev);
  47. void __init pci_iommu_alloc(void)
  48. {
  49. struct iommu_table_entry *p;
  50. sort_iommu_table(__iommu_table, __iommu_table_end);
  51. check_iommu_entries(__iommu_table, __iommu_table_end);
  52. for (p = __iommu_table; p < __iommu_table_end; p++) {
  53. if (p && p->detect && p->detect() > 0) {
  54. p->flags |= IOMMU_DETECTED;
  55. if (p->early_init)
  56. p->early_init();
  57. if (p->flags & IOMMU_FINISH_IF_DETECTED)
  58. break;
  59. }
  60. }
  61. }
  62. bool arch_dma_alloc_attrs(struct device **dev)
  63. {
  64. if (!*dev)
  65. *dev = &x86_dma_fallback_dev;
  66. if (!is_device_dma_capable(*dev))
  67. return false;
  68. return true;
  69. }
  70. EXPORT_SYMBOL(arch_dma_alloc_attrs);
  71. /*
  72. * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  73. * parameter documentation.
  74. */
  75. static __init int iommu_setup(char *p)
  76. {
  77. iommu_merge = 1;
  78. if (!p)
  79. return -EINVAL;
  80. while (*p) {
  81. if (!strncmp(p, "off", 3))
  82. no_iommu = 1;
  83. /* gart_parse_options has more force support */
  84. if (!strncmp(p, "force", 5))
  85. force_iommu = 1;
  86. if (!strncmp(p, "noforce", 7)) {
  87. iommu_merge = 0;
  88. force_iommu = 0;
  89. }
  90. if (!strncmp(p, "biomerge", 8)) {
  91. iommu_merge = 1;
  92. force_iommu = 1;
  93. }
  94. if (!strncmp(p, "panic", 5))
  95. panic_on_overflow = 1;
  96. if (!strncmp(p, "nopanic", 7))
  97. panic_on_overflow = 0;
  98. if (!strncmp(p, "merge", 5)) {
  99. iommu_merge = 1;
  100. force_iommu = 1;
  101. }
  102. if (!strncmp(p, "nomerge", 7))
  103. iommu_merge = 0;
  104. if (!strncmp(p, "forcesac", 8))
  105. iommu_sac_force = 1;
  106. if (!strncmp(p, "allowdac", 8))
  107. forbid_dac = 0;
  108. if (!strncmp(p, "nodac", 5))
  109. forbid_dac = 1;
  110. if (!strncmp(p, "usedac", 6)) {
  111. forbid_dac = -1;
  112. return 1;
  113. }
  114. #ifdef CONFIG_SWIOTLB
  115. if (!strncmp(p, "soft", 4))
  116. swiotlb = 1;
  117. #endif
  118. if (!strncmp(p, "pt", 2))
  119. iommu_pass_through = 1;
  120. gart_parse_options(p);
  121. #ifdef CONFIG_CALGARY_IOMMU
  122. if (!strncmp(p, "calgary", 7))
  123. use_calgary = 1;
  124. #endif /* CONFIG_CALGARY_IOMMU */
  125. p += strcspn(p, ",");
  126. if (*p == ',')
  127. ++p;
  128. }
  129. return 0;
  130. }
  131. early_param("iommu", iommu_setup);
  132. int arch_dma_supported(struct device *dev, u64 mask)
  133. {
  134. #ifdef CONFIG_PCI
  135. if (mask > 0xffffffff && forbid_dac > 0) {
  136. dev_info(dev, "PCI: Disallowing DAC for device\n");
  137. return 0;
  138. }
  139. #endif
  140. /* Tell the device to use SAC when IOMMU force is on. This
  141. allows the driver to use cheaper accesses in some cases.
  142. Problem with this is that if we overflow the IOMMU area and
  143. return DAC as fallback address the device may not handle it
  144. correctly.
  145. As a special case some controllers have a 39bit address
  146. mode that is as efficient as 32bit (aic79xx). Don't force
  147. SAC for these. Assume all masks <= 40 bits are of this
  148. type. Normally this doesn't make any difference, but gives
  149. more gentle handling of IOMMU overflow. */
  150. if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
  151. dev_info(dev, "Force SAC with mask %Lx\n", mask);
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. EXPORT_SYMBOL(arch_dma_supported);
  157. static int __init pci_iommu_init(void)
  158. {
  159. struct iommu_table_entry *p;
  160. #ifdef CONFIG_PCI
  161. dma_debug_add_bus(&pci_bus_type);
  162. #endif
  163. x86_init.iommu.iommu_init();
  164. for (p = __iommu_table; p < __iommu_table_end; p++) {
  165. if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
  166. p->late_init();
  167. }
  168. return 0;
  169. }
  170. /* Must execute after PCI subsystem */
  171. rootfs_initcall(pci_iommu_init);
  172. #ifdef CONFIG_PCI
  173. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  174. static void via_no_dac(struct pci_dev *dev)
  175. {
  176. if (forbid_dac == 0) {
  177. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  178. forbid_dac = 1;
  179. }
  180. }
  181. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  182. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  183. #endif