pci-swiotlb.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /*
  17. * pci_swiotlb_detect_override - set swiotlb to 1 if necessary
  18. *
  19. * This returns non-zero if we are forced to use swiotlb (by the boot
  20. * option).
  21. */
  22. int __init pci_swiotlb_detect_override(void)
  23. {
  24. if (swiotlb_force == SWIOTLB_FORCE)
  25. swiotlb = 1;
  26. return swiotlb;
  27. }
  28. IOMMU_INIT_FINISH(pci_swiotlb_detect_override,
  29. pci_xen_swiotlb_detect,
  30. pci_swiotlb_init,
  31. pci_swiotlb_late_init);
  32. /*
  33. * If 4GB or more detected (and iommu=off not set) or if SME is active
  34. * then set swiotlb to 1 and return 1.
  35. */
  36. int __init pci_swiotlb_detect_4gb(void)
  37. {
  38. /* don't initialize swiotlb if iommu=off (no_iommu=1) */
  39. if (!no_iommu && max_possible_pfn > MAX_DMA32_PFN)
  40. swiotlb = 1;
  41. /*
  42. * If SME is active then swiotlb will be set to 1 so that bounce
  43. * buffers are allocated and used for devices that do not support
  44. * the addressing range required for the encryption mask.
  45. */
  46. if (sme_active())
  47. swiotlb = 1;
  48. return swiotlb;
  49. }
  50. IOMMU_INIT(pci_swiotlb_detect_4gb,
  51. pci_swiotlb_detect_override,
  52. pci_swiotlb_init,
  53. pci_swiotlb_late_init);
  54. void __init pci_swiotlb_init(void)
  55. {
  56. if (swiotlb) {
  57. swiotlb_init(0);
  58. dma_ops = &swiotlb_dma_ops;
  59. }
  60. }
  61. void __init pci_swiotlb_late_init(void)
  62. {
  63. /* An IOMMU turned us off. */
  64. if (!swiotlb)
  65. swiotlb_exit();
  66. else {
  67. printk(KERN_INFO "PCI-DMA: "
  68. "Using software bounce buffering for IO (SWIOTLB)\n");
  69. swiotlb_print_info();
  70. }
  71. }