pci-swiotlb-xen.c 2.6 KB

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