20
20
#include <linux/mm.h>
21
21
#include <linux/mutex.h>
22
22
#include <linux/pci.h>
23
+ #include <linux/swiotlb.h>
23
24
#include <linux/scatterlist.h>
24
25
#include <linux/vmalloc.h>
25
26
#include <linux/crash_dump.h>
27
+ #include <linux/dma-direct.h>
26
28
27
29
struct iommu_dma_msi_page {
28
30
struct list_head list ;
@@ -499,6 +501,31 @@ static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr,
499
501
iommu_dma_free_iova (cookie , dma_addr , size , iotlb_gather .freelist );
500
502
}
501
503
504
+ static void __iommu_dma_unmap_swiotlb (struct device * dev , dma_addr_t dma_addr ,
505
+ size_t size , enum dma_data_direction dir ,
506
+ unsigned long attrs )
507
+ {
508
+ struct iommu_domain * domain = iommu_get_dma_domain (dev );
509
+ struct iommu_dma_cookie * cookie = domain -> iova_cookie ;
510
+ struct iova_domain * iovad = & cookie -> iovad ;
511
+ phys_addr_t phys ;
512
+
513
+ phys = iommu_iova_to_phys (domain , dma_addr );
514
+ if (WARN_ON (!phys ))
515
+ return ;
516
+
517
+ __iommu_dma_unmap (dev , dma_addr , size );
518
+
519
+ if (unlikely (is_swiotlb_buffer (phys )))
520
+ swiotlb_tbl_unmap_single (dev , phys , size ,
521
+ iova_align (iovad , size ), dir , attrs );
522
+ }
523
+
524
+ static bool dev_is_untrusted (struct device * dev )
525
+ {
526
+ return dev_is_pci (dev ) && to_pci_dev (dev )-> untrusted ;
527
+ }
528
+
502
529
static dma_addr_t __iommu_dma_map (struct device * dev , phys_addr_t phys ,
503
530
size_t size , int prot , u64 dma_mask )
504
531
{
@@ -524,6 +551,54 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
524
551
return iova + iova_off ;
525
552
}
526
553
554
+ static dma_addr_t __iommu_dma_map_swiotlb (struct device * dev , phys_addr_t phys ,
555
+ size_t org_size , dma_addr_t dma_mask , bool coherent ,
556
+ enum dma_data_direction dir , unsigned long attrs )
557
+ {
558
+ int prot = dma_info_to_prot (dir , coherent , attrs );
559
+ struct iommu_domain * domain = iommu_get_dma_domain (dev );
560
+ struct iommu_dma_cookie * cookie = domain -> iova_cookie ;
561
+ struct iova_domain * iovad = & cookie -> iovad ;
562
+ size_t aligned_size = org_size ;
563
+ void * padding_start ;
564
+ size_t padding_size ;
565
+ dma_addr_t iova ;
566
+
567
+ /*
568
+ * If both the physical buffer start address and size are
569
+ * page aligned, we don't need to use a bounce page.
570
+ */
571
+ if (IS_ENABLED (CONFIG_SWIOTLB ) && dev_is_untrusted (dev ) &&
572
+ iova_offset (iovad , phys | org_size )) {
573
+ aligned_size = iova_align (iovad , org_size );
574
+ phys = swiotlb_tbl_map_single (dev , phys , org_size ,
575
+ aligned_size , dir , attrs );
576
+
577
+ if (phys == DMA_MAPPING_ERROR )
578
+ return DMA_MAPPING_ERROR ;
579
+
580
+ /* Cleanup the padding area. */
581
+ padding_start = phys_to_virt (phys );
582
+ padding_size = aligned_size ;
583
+
584
+ if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC ) &&
585
+ (dir == DMA_TO_DEVICE ||
586
+ dir == DMA_BIDIRECTIONAL )) {
587
+ padding_start += org_size ;
588
+ padding_size -= org_size ;
589
+ }
590
+
591
+ memset (padding_start , 0 , padding_size );
592
+ }
593
+
594
+ iova = __iommu_dma_map (dev , phys , aligned_size , prot , dma_mask );
595
+ if ((iova == DMA_MAPPING_ERROR ) && is_swiotlb_buffer (phys ))
596
+ swiotlb_tbl_unmap_single (dev , phys , org_size ,
597
+ aligned_size , dir , attrs );
598
+
599
+ return iova ;
600
+ }
601
+
527
602
static void __iommu_dma_free_pages (struct page * * pages , int count )
528
603
{
529
604
while (count -- )
@@ -697,23 +772,31 @@ static void iommu_dma_sync_single_for_cpu(struct device *dev,
697
772
{
698
773
phys_addr_t phys ;
699
774
700
- if (dev_is_dma_coherent (dev ))
775
+ if (dev_is_dma_coherent (dev ) && ! dev_is_untrusted ( dev ) )
701
776
return ;
702
777
703
778
phys = iommu_iova_to_phys (iommu_get_dma_domain (dev ), dma_handle );
704
- arch_sync_dma_for_cpu (phys , size , dir );
779
+ if (!dev_is_dma_coherent (dev ))
780
+ arch_sync_dma_for_cpu (phys , size , dir );
781
+
782
+ if (is_swiotlb_buffer (phys ))
783
+ swiotlb_tbl_sync_single (dev , phys , size , dir , SYNC_FOR_CPU );
705
784
}
706
785
707
786
static void iommu_dma_sync_single_for_device (struct device * dev ,
708
787
dma_addr_t dma_handle , size_t size , enum dma_data_direction dir )
709
788
{
710
789
phys_addr_t phys ;
711
790
712
- if (dev_is_dma_coherent (dev ))
791
+ if (dev_is_dma_coherent (dev ) && ! dev_is_untrusted ( dev ) )
713
792
return ;
714
793
715
794
phys = iommu_iova_to_phys (iommu_get_dma_domain (dev ), dma_handle );
716
- arch_sync_dma_for_device (phys , size , dir );
795
+ if (is_swiotlb_buffer (phys ))
796
+ swiotlb_tbl_sync_single (dev , phys , size , dir , SYNC_FOR_DEVICE );
797
+
798
+ if (!dev_is_dma_coherent (dev ))
799
+ arch_sync_dma_for_device (phys , size , dir );
717
800
}
718
801
719
802
static void iommu_dma_sync_sg_for_cpu (struct device * dev ,
@@ -723,11 +806,17 @@ static void iommu_dma_sync_sg_for_cpu(struct device *dev,
723
806
struct scatterlist * sg ;
724
807
int i ;
725
808
726
- if (dev_is_dma_coherent (dev ))
809
+ if (dev_is_dma_coherent (dev ) && ! dev_is_untrusted ( dev ) )
727
810
return ;
728
811
729
- for_each_sg (sgl , sg , nelems , i )
730
- arch_sync_dma_for_cpu (sg_phys (sg ), sg -> length , dir );
812
+ for_each_sg (sgl , sg , nelems , i ) {
813
+ if (!dev_is_dma_coherent (dev ))
814
+ arch_sync_dma_for_cpu (sg_phys (sg ), sg -> length , dir );
815
+
816
+ if (is_swiotlb_buffer (sg_phys (sg )))
817
+ swiotlb_tbl_sync_single (dev , sg_phys (sg ), sg -> length ,
818
+ dir , SYNC_FOR_CPU );
819
+ }
731
820
}
732
821
733
822
static void iommu_dma_sync_sg_for_device (struct device * dev ,
@@ -737,11 +826,17 @@ static void iommu_dma_sync_sg_for_device(struct device *dev,
737
826
struct scatterlist * sg ;
738
827
int i ;
739
828
740
- if (dev_is_dma_coherent (dev ))
829
+ if (dev_is_dma_coherent (dev ) && ! dev_is_untrusted ( dev ) )
741
830
return ;
742
831
743
- for_each_sg (sgl , sg , nelems , i )
744
- arch_sync_dma_for_device (sg_phys (sg ), sg -> length , dir );
832
+ for_each_sg (sgl , sg , nelems , i ) {
833
+ if (is_swiotlb_buffer (sg_phys (sg )))
834
+ swiotlb_tbl_sync_single (dev , sg_phys (sg ), sg -> length ,
835
+ dir , SYNC_FOR_DEVICE );
836
+
837
+ if (!dev_is_dma_coherent (dev ))
838
+ arch_sync_dma_for_device (sg_phys (sg ), sg -> length , dir );
839
+ }
745
840
}
746
841
747
842
static dma_addr_t iommu_dma_map_page (struct device * dev , struct page * page ,
@@ -750,10 +845,10 @@ static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
750
845
{
751
846
phys_addr_t phys = page_to_phys (page ) + offset ;
752
847
bool coherent = dev_is_dma_coherent (dev );
753
- int prot = dma_info_to_prot (dir , coherent , attrs );
754
848
dma_addr_t dma_handle ;
755
849
756
- dma_handle = __iommu_dma_map (dev , phys , size , prot , dma_get_mask (dev ));
850
+ dma_handle = __iommu_dma_map_swiotlb (dev , phys , size , dma_get_mask (dev ),
851
+ coherent , dir , attrs );
757
852
if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC ) &&
758
853
dma_handle != DMA_MAPPING_ERROR )
759
854
arch_sync_dma_for_device (phys , size , dir );
@@ -765,7 +860,7 @@ static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
765
860
{
766
861
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC ))
767
862
iommu_dma_sync_single_for_cpu (dev , dma_handle , size , dir );
768
- __iommu_dma_unmap (dev , dma_handle , size );
863
+ __iommu_dma_unmap_swiotlb (dev , dma_handle , size , dir , attrs );
769
864
}
770
865
771
866
/*
@@ -843,6 +938,39 @@ static void __invalidate_sg(struct scatterlist *sg, int nents)
843
938
}
844
939
}
845
940
941
+ static void iommu_dma_unmap_sg_swiotlb (struct device * dev , struct scatterlist * sg ,
942
+ int nents , enum dma_data_direction dir , unsigned long attrs )
943
+ {
944
+ struct scatterlist * s ;
945
+ int i ;
946
+
947
+ for_each_sg (sg , s , nents , i )
948
+ __iommu_dma_unmap_swiotlb (dev , sg_dma_address (s ),
949
+ sg_dma_len (s ), dir , attrs );
950
+ }
951
+
952
+ static int iommu_dma_map_sg_swiotlb (struct device * dev , struct scatterlist * sg ,
953
+ int nents , enum dma_data_direction dir , unsigned long attrs )
954
+ {
955
+ struct scatterlist * s ;
956
+ int i ;
957
+
958
+ for_each_sg (sg , s , nents , i ) {
959
+ sg_dma_address (s ) = __iommu_dma_map_swiotlb (dev , sg_phys (s ),
960
+ s -> length , dma_get_mask (dev ),
961
+ dev_is_dma_coherent (dev ), dir , attrs );
962
+ if (sg_dma_address (s ) == DMA_MAPPING_ERROR )
963
+ goto out_unmap ;
964
+ sg_dma_len (s ) = s -> length ;
965
+ }
966
+
967
+ return nents ;
968
+
969
+ out_unmap :
970
+ iommu_dma_unmap_sg_swiotlb (dev , sg , i , dir , attrs | DMA_ATTR_SKIP_CPU_SYNC );
971
+ return 0 ;
972
+ }
973
+
846
974
/*
847
975
* The DMA API client is passing in a scatterlist which could describe
848
976
* any old buffer layout, but the IOMMU API requires everything to be
@@ -869,6 +997,9 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
869
997
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC ))
870
998
iommu_dma_sync_sg_for_device (dev , sg , nents , dir );
871
999
1000
+ if (dev_is_untrusted (dev ))
1001
+ return iommu_dma_map_sg_swiotlb (dev , sg , nents , dir , attrs );
1002
+
872
1003
/*
873
1004
* Work out how much IOVA space we need, and align the segments to
874
1005
* IOVA granules for the IOMMU driver to handle. With some clever
@@ -938,6 +1069,11 @@ static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
938
1069
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC ))
939
1070
iommu_dma_sync_sg_for_cpu (dev , sg , nents , dir );
940
1071
1072
+ if (dev_is_untrusted (dev )) {
1073
+ iommu_dma_unmap_sg_swiotlb (dev , sg , nents , dir , attrs );
1074
+ return ;
1075
+ }
1076
+
941
1077
/*
942
1078
* The scatterlist segments are mapped into a single
943
1079
* contiguous IOVA allocation, so this is incredibly easy.
0 commit comments