pci-swiotlb.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Glue code to lib/swiotlb.c */
  3. #include <linux/pci.h>
  4. #include <linux/cache.h>
  5. #include <linux/init.h>
  6. #include <linux/swiotlb.h>
  7. #include <linux/bootmem.h>
  8. #include <linux/dma-direct.h>
  9. #include <linux/mem_encrypt.h>
  10. #include <asm/iommu.h>
  11. #include <asm/swiotlb.h>
  12. #include <asm/dma.h>
  13. #include <asm/xen/swiotlb-xen.h>
  14. #include <asm/iommu_table.h>
  15. int swiotlb __read_mostly;
  16. void *x86_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
  17. dma_addr_t *dma_handle, gfp_t flags,
  18. unsigned long attrs)
  19. {
  20. void *vaddr;
  21. /*
  22. * Don't print a warning when the first allocation attempt fails.
  23. * swiotlb_alloc_coherent() will print a warning when the DMA
  24. * memory allocation ultimately failed.
  25. */
  26. flags |= __GFP_NOWARN;
  27. vaddr = dma_generic_alloc_coherent(hwdev, size, dma_handle, flags,
  28. attrs);
  29. if (vaddr)
  30. return vaddr;
  31. return swiotlb_alloc_coherent(hwdev, size, dma_handle, flags);
  32. }
  33. void x86_swiotlb_free_coherent(struct device *dev, size_t size,
  34. void *vaddr, dma_addr_t dma_addr,
  35. unsigned long attrs)
  36. {
  37. if (is_swiotlb_buffer(dma_to_phys(dev, dma_addr)))
  38. swiotlb_free_coherent(dev, size, vaddr, dma_addr);
  39. else
  40. dma_generic_free_coherent(dev, size, vaddr, dma_addr, attrs);
  41. }
  42. static const struct dma_map_ops x86_swiotlb_dma_ops = {
  43. .mapping_error = swiotlb_dma_mapping_error,
  44. .alloc = x86_swiotlb_alloc_coherent,
  45. .free = x86_swiotlb_free_coherent,
  46. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  47. .sync_single_for_device = swiotlb_sync_single_for_device,
  48. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  49. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  50. .map_sg = swiotlb_map_sg_attrs,
  51. .unmap_sg = swiotlb_unmap_sg_attrs,
  52. .map_page = swiotlb_map_page,
  53. .unmap_page = swiotlb_unmap_page,
  54. .dma_supported = NULL,
  55. };
  56. /*
  57. * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
  58. *
  59. * This returns non-zero if we are forced to use swiotlb (by the boot
  60. * option).
  61. */
  62. int __init pci_swiotlb_detect_override(void)
  63. {
  64. if (swiotlb_force == SWIOTLB_FORCE)
  65. swiotlb = 1;
  66. return swiotlb;
  67. }
  68. IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
  69. pci_xen_swiotlb_detect,
  70. pci_swiotlb_init,
  71. pci_swiotlb_late_init);
  72. /*
  73. * If 4GB or more detected (and iommu=off not set) or if SME is active
  74. * then set swiotlb to 1 and return 1.
  75. */
  76. int __init pci_swiotlb_detect_4gb(void)
  77. {
  78. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  79. #ifdef CONFIG_X86_64
  80. if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
  81. swiotlb = 1;
  82. #endif
  83. /*
  84. * If SME is active then swiotlb will be set to 1 so that bounce
  85. * buffers are allocated and used for devices that do not support
  86. * the addressing range required for the encryption mask.
  87. */
  88. if (sme_active())
  89. swiotlb = 1;
  90. return swiotlb;
  91. }
  92. IOMMU_INIT(pci_swiotlb_detect_4gb,
  93. pci_swiotlb_detect_override,
  94. pci_swiotlb_init,
  95. pci_swiotlb_late_init);
  96. void __init pci_swiotlb_init(void)
  97. {
  98. if (swiotlb) {
  99. swiotlb_init(0);
  100. dma_ops = &x86_swiotlb_dma_ops;
  101. }
  102. }
  103. void __init pci_swiotlb_late_init(void)
  104. {
  105. /* An IOMMU turned us off. */
  106. if (!swiotlb)
  107. swiotlb_exit();
  108. else {
  109. printk(KERN_INFO "PCI-DMA: "
  110. "Using software bounce buffering for IO (SWIOTLB)\n");
  111. swiotlb_print_info();
  112. }
  113. }