pci-dma.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <asm/proto.h>
  11. #include <asm/dma.h>
  12. #include <asm/iommu.h>
  13. #include <asm/gart.h>
  14. #include <asm/x86_init.h>
  15. #include <asm/iommu_table.h>
  16. static bool disable_dac_quirk __read_mostly;
  17. const struct dma_map_ops *dma_ops;
  18. EXPORT_SYMBOL(dma_ops);
  19. #ifdef CONFIG_IOMMU_DEBUG
  20. int panic_on_overflow __read_mostly = 1;
  21. int force_iommu __read_mostly = 1;
  22. #else
  23. int panic_on_overflow __read_mostly = 0;
  24. int force_iommu __read_mostly = 0;
  25. #endif
  26. int iommu_merge __read_mostly = 0;
  27. int no_iommu __read_mostly;
  28. /* Set this to 1 if there is a HW IOMMU in the system */
  29. int iommu_detected __read_mostly = 0;
  30. extern struct iommu_table_entry __iommu_table[], __iommu_table_end[];
  31. void __init pci_iommu_alloc(void)
  32. {
  33. struct iommu_table_entry *p;
  34. sort_iommu_table(__iommu_table, __iommu_table_end);
  35. check_iommu_entries(__iommu_table, __iommu_table_end);
  36. for (p = __iommu_table; p < __iommu_table_end; p++) {
  37. if (p && p->detect && p->detect() > 0) {
  38. p->flags |= IOMMU_DETECTED;
  39. if (p->early_init)
  40. p->early_init();
  41. if (p->flags & IOMMU_FINISH_IF_DETECTED)
  42. break;
  43. }
  44. }
  45. }
  46. /*
  47. * See <Documentation/x86/x86_64/boot-options.rst> for the iommu kernel
  48. * parameter documentation.
  49. */
  50. static __init int iommu_setup(char *p)
  51. {
  52. iommu_merge = 1;
  53. if (!p)
  54. return -EINVAL;
  55. while (*p) {
  56. if (!strncmp(p, "off", 3))
  57. no_iommu = 1;
  58. /* gart_parse_options has more force support */
  59. if (!strncmp(p, "force", 5))
  60. force_iommu = 1;
  61. if (!strncmp(p, "noforce", 7)) {
  62. iommu_merge = 0;
  63. force_iommu = 0;
  64. }
  65. if (!strncmp(p, "biomerge", 8)) {
  66. iommu_merge = 1;
  67. force_iommu = 1;
  68. }
  69. if (!strncmp(p, "panic", 5))
  70. panic_on_overflow = 1;
  71. if (!strncmp(p, "nopanic", 7))
  72. panic_on_overflow = 0;
  73. if (!strncmp(p, "merge", 5)) {
  74. iommu_merge = 1;
  75. force_iommu = 1;
  76. }
  77. if (!strncmp(p, "nomerge", 7))
  78. iommu_merge = 0;
  79. if (!strncmp(p, "forcesac", 8))
  80. pr_warn("forcesac option ignored.\n");
  81. if (!strncmp(p, "allowdac", 8))
  82. pr_warn("allowdac option ignored.\n");
  83. if (!strncmp(p, "nodac", 5))
  84. pr_warn("nodac option ignored.\n");
  85. if (!strncmp(p, "usedac", 6)) {
  86. disable_dac_quirk = true;
  87. return 1;
  88. }
  89. #ifdef CONFIG_SWIOTLB
  90. if (!strncmp(p, "soft", 4))
  91. swiotlb = 1;
  92. #endif
  93. if (!strncmp(p, "pt", 2))
  94. iommu_set_default_passthrough(true);
  95. if (!strncmp(p, "nopt", 4))
  96. iommu_set_default_translated(true);
  97. gart_parse_options(p);
  98. p += strcspn(p, ",");
  99. if (*p == ',')
  100. ++p;
  101. }
  102. return 0;
  103. }
  104. early_param("iommu", iommu_setup);
  105. static int __init pci_iommu_init(void)
  106. {
  107. struct iommu_table_entry *p;
  108. x86_init.iommu.iommu_init();
  109. for (p = __iommu_table; p < __iommu_table_end; p++) {
  110. if (p && (p->flags & IOMMU_DETECTED) && p->late_init)
  111. p->late_init();
  112. }
  113. return 0;
  114. }
  115. /* Must execute after PCI subsystem */
  116. rootfs_initcall(pci_iommu_init);
  117. #ifdef CONFIG_PCI
  118. /* Many VIA bridges seem to corrupt data for DAC. Disable it here */
  119. static int via_no_dac_cb(struct pci_dev *pdev, void *data)
  120. {
  121. pdev->dev.bus_dma_limit = DMA_BIT_MASK(32);
  122. return 0;
  123. }
  124. static void via_no_dac(struct pci_dev *dev)
  125. {
  126. if (!disable_dac_quirk) {
  127. dev_info(&dev->dev, "disabling DAC on VIA PCI bridge\n");
  128. pci_walk_bus(dev->subordinate, via_no_dac_cb, NULL);
  129. }
  130. }
  131. DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
  132. PCI_CLASS_BRIDGE_PCI, 8, via_no_dac);
  133. #endif