pci-swiotlb.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/pci.h>
  3. #include <linux/cache.h>
  4. #include <linux/init.h>
  5. #include <linux/swiotlb.h>
  6. #include <linux/memblock.h>
  7. #include <linux/dma-direct.h>
  8. #include <linux/mem_encrypt.h>
  9. #include <asm/iommu.h>
  10. #include <asm/swiotlb.h>
  11. #include <asm/dma.h>
  12. #include <asm/xen/swiotlb-xen.h>
  13. #include <asm/iommu_table.h>
  14. int swiotlb __read_mostly;
  15. /*
  16. * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
  17. *
  18. * This returns non-zero if we are forced to use swiotlb (by the boot
  19. * option).
  20. */
  21. int __init pci_swiotlb_detect_override(void)
  22. {
  23. if (swiotlb_force == SWIOTLB_FORCE)
  24. swiotlb = 1;
  25. return swiotlb;
  26. }
  27. IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
  28. pci_xen_swiotlb_detect,
  29. pci_swiotlb_init,
  30. pci_swiotlb_late_init);
  31. /*
  32. * If 4GB or more detected (and iommu=off not set) or if SME is active
  33. * then set swiotlb to 1 and return 1.
  34. */
  35. int __init pci_swiotlb_detect_4gb(void)
  36. {
  37. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  38. if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
  39. swiotlb = 1;
  40. /*
  41. * If SME is active then swiotlb will be set to 1 so that bounce
  42. * buffers are allocated and used for devices that do not support
  43. * the addressing range required for the encryption mask.
  44. */
  45. if (sme_active())
  46. swiotlb = 1;
  47. return swiotlb;
  48. }
  49. IOMMU_INIT(pci_swiotlb_detect_4gb,
  50. pci_swiotlb_detect_override,
  51. pci_swiotlb_init,
  52. pci_swiotlb_late_init);
  53. void __init pci_swiotlb_init(void)
  54. {
  55. if (swiotlb)
  56. swiotlb_init(0);
  57. }
  58. void __init pci_swiotlb_late_init(void)
  59. {
  60. /* An IOMMU turned us off. */
  61. if (!swiotlb)
  62. swiotlb_exit();
  63. else {
  64. printk(KERN_INFO "PCI-DMA: "
  65. "Using software bounce buffering for IO (SWIOTLB)\n");
  66. swiotlb_print_info();
  67. }
  68. }