pci-dma.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. /* Number of entries preallocated for DMA-API debugging */
  48. #define PREALLOC_DMA_DEBUG_ENTRIES 65536
  49. void __init pci_iommu_alloc(void)
  50. {
  51. struct iommu_table_entry *p;
  52. sort_iommu_table(__iommu_table, __iommu_table_end);
  53. check_iommu_entries(__iommu_table, __iommu_table_end);
  54. for (p = __iommu_table; p < __iommu_table_end; p++) {
  55. if (p && p->detect && p->detect() > 0) {
  56. p->flags |= IOMMU_DETECTED;
  57. if (p->early_init)
  58. p->early_init();
  59. if (p->flags & IOMMU_FINISH_IF_DETECTED)
  60. break;
  61. }
  62. }
  63. }
  64. bool arch_dma_alloc_attrs(struct device **dev, gfp_t *gfp)
  65. {
  66. if (!*dev)
  67. *dev = &x86_dma_fallback_dev;
  68. if (!is_device_dma_capable(*dev))
  69. return false;
  70. return true;
  71. }
  72. EXPORT_SYMBOL(arch_dma_alloc_attrs);
  73. /*
  74. * See <Documentation/x86/x86_64/boot-options.txt> for the iommu kernel
  75. * parameter documentation.
  76. */
  77. static __init int iommu_setup(char *p)
  78. {
  79. iommu_merge = 1;
  80. if (!p)
  81. return -EINVAL;
  82. while (*p) {
  83. if (!strncmp(p, "off", 3))
  84. no_iommu = 1;
  85. /* gart_parse_options has more force support */
  86. if (!strncmp(p, "force", 5))
  87. force_iommu = 1;
  88. if (!strncmp(p, "noforce", 7)) {
  89. iommu_merge = 0;
  90. force_iommu = 0;
  91. }
  92. if (!strncmp(p, "biomerge", 8)) {
  93. iommu_merge = 1;
  94. force_iommu = 1;
  95. }
  96. if (!strncmp(p, "panic", 5))
  97. panic_on_overflow = 1;
  98. if (!strncmp(p, "nopanic", 7))
  99. panic_on_overflow = 0;
  100. if (!strncmp(p, "merge", 5)) {
  101. iommu_merge = 1;
  102. force_iommu = 1;
  103. }
  104. if (!strncmp(p, "nomerge", 7))
  105. iommu_merge = 0;
  106. if (!strncmp(p, "forcesac", 8))
  107. iommu_sac_force = 1;
  108. if (!strncmp(p, "allowdac", 8))
  109. forbid_dac = 0;
  110. if (!strncmp(p, "nodac", 5))
  111. forbid_dac = 1;
  112. if (!strncmp(p, "usedac", 6)) {
  113. forbid_dac = -1;
  114. return 1;
  115. }
  116. #ifdef CONFIG_SWIOTLB
  117. if (!strncmp(p, "soft", 4))
  118. swiotlb = 1;
  119. #endif
  120. if (!strncmp(p, "pt", 2))
  121. iommu_pass_through = 1;
  122. gart_parse_options(p);
  123. #ifdef CONFIG_CALGARY_IOMMU
  124. if (!strncmp(p, "calgary", 7))
  125. use_calgary = 1;
  126. #endif /* CONFIG_CALGARY_IOMMU */
  127. p += strcspn(p, ",");
  128. if (*p == ',')
  129. ++p;
  130. }
  131. return 0;
  132. }
  133. early_param("iommu", iommu_setup);
  134. int arch_dma_supported(struct device *dev, u64 mask)
  135. {
  136. #ifdef CONFIG_PCI
  137. if (mask > 0xffffffff && forbid_dac > 0) {
  138. dev_info(dev, "PCI: Disallowing DAC for device\n");
  139. return 0;
  140. }
  141. #endif
  142. /* Tell the device to use SAC when IOMMU force is on. This
  143. allows the driver to use cheaper accesses in some cases.
  144. Problem with this is that if we overflow the IOMMU area and
  145. return DAC as fallback address the device may not handle it
  146. correctly.
  147. As a special case some controllers have a 39bit address
  148. mode that is as efficient as 32bit (aic79xx). Don't force
  149. SAC for these. Assume all masks <= 40 bits are of this
  150. type. Normally this doesn't make any difference, but gives
  151. more gentle handling of IOMMU overflow. */
  152. if (iommu_sac_force && (mask >= DMA_BIT_MASK(40))) {
  153. dev_info(dev, "Force SAC with mask %Lx\n", mask);
  154. return 0;
  155. }
  156. return 1;
  157. }
  158. EXPORT_SYMBOL(arch_dma_supported);
  159. static int __init pci_iommu_init(void)
  160. {
  161. struct iommu_table_entry *p;
  162. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  163. #ifdef CONFIG_PCI
  164. dma_debug_add_bus(&pci_bus_type);
  165. #endif
  166. x86_init.iommu.iommu_init();
  167. for (p = __iommu_table; p < __iommu_table_end; p++) {
  168. if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
  169. p->late_init();
  170. }
  171. return 0;
  172. }
  173. /* Must execute after PCI subsystem */
  174. rootfs_initcall(pci_iommu_init);
  175. #ifdef CONFIG_PCI
  176. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  177. static void via_no_dac(struct pci_dev *dev)
  178. {
  179. if (forbid_dac == 0) {
  180. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  181. forbid_dac = 1;
  182. }
  183. }
  184. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  185. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  186. #endif