pci-swiotlb-xen.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Glue code to lib/swiotlb-xen.c */
  3. #include <linux/dma-mapping.h>
  4. #include <linux/pci.h>
  5. #include <xen/swiotlb-xen.h>
  6. #include <asm/xen/hypervisor.h>
  7. #include <xen/xen.h>
  8. #include <asm/iommu_table.h>
  9. #include <asm/xen/swiotlb-xen.h>
  10. #ifdef CONFIG_X86_64
  11. #include <asm/iommu.h>
  12. #include <asm/dma.h>
  13. #endif
  14. #include <linux/export.h>
  15. int xen_swiotlb __read_mostly;
  16. /*
  17. * pci_xen_swiotlb_detect - set xen_swiotlb to 1 if necessary
  18. *
  19. * This returns non-zero if we are forced to use xen_swiotlb (by the boot
  20. * option).
  21. */
  22. int __init pci_xen_swiotlb_detect(void)
  23. {
  24. if (!xen_pv_domain())
  25. return 0;
  26. /* If running as PV guest, either iommu=soft, or swiotlb=force will
  27. * activate this IOMMU. If running as PV privileged, activate it
  28. * irregardless.
  29. */
  30. if (xen_initial_domain() || swiotlb || swiotlb_force == SWIOTLB_FORCE)
  31. xen_swiotlb = 1;
  32. /* If we are running under Xen, we MUST disable the native SWIOTLB.
  33. * Don't worry about swiotlb_force flag activating the native, as
  34. * the 'swiotlb' flag is the only one turning it on. */
  35. swiotlb = 0;
  36. #ifdef CONFIG_X86_64
  37. /* pci_swiotlb_detect_4gb turns on native SWIOTLB if no_iommu == 0
  38. * (so no iommu=X command line over-writes).
  39. * Considering that PV guests do not want the *native SWIOTLB* but
  40. * only Xen SWIOTLB it is not useful to us so set no_iommu=1 here.
  41. */
  42. if (max_pfn > MAX_DMA32_PFN)
  43. no_iommu = 1;
  44. #endif
  45. return xen_swiotlb;
  46. }
  47. void __init pci_xen_swiotlb_init(void)
  48. {
  49. if (xen_swiotlb) {
  50. xen_swiotlb_init(1, true /* early */);
  51. dma_ops = &xen_swiotlb_dma_ops;
  52. #ifdef CONFIG_PCI
  53. /* Make sure ACS will be enabled */
  54. pci_request_acs();
  55. #endif
  56. }
  57. }
  58. int pci_xen_swiotlb_init_late(void)
  59. {
  60. int rc;
  61. if (xen_swiotlb)
  62. return 0;
  63. rc = xen_swiotlb_init(1, false /* late */);
  64. if (rc)
  65. return rc;
  66. dma_ops = &xen_swiotlb_dma_ops;
  67. #ifdef CONFIG_PCI
  68. /* Make sure ACS will be enabled */
  69. pci_request_acs();
  70. #endif
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(pci_xen_swiotlb_init_late);
  74. IOMMU_INIT_FINISH(pci_xen_swiotlb_detect,
  75. NULL,
  76. pci_xen_swiotlb_init,
  77. NULL);