pci-swiotlb-xen.c 2.5 KB

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