1919#include <linux/iommu.h>
2020#include <linux/iova.h>
2121#include <linux/irq.h>
22+ #include <linux/kernel.h>
2223#include <linux/list_sort.h>
2324#include <linux/memremap.h>
2425#include <linux/mm.h>
2526#include <linux/mutex.h>
2627#include <linux/of_iommu.h>
2728#include <linux/pci.h>
29+ #include <linux/pfn.h>
2830#include <linux/scatterlist.h>
2931#include <linux/spinlock.h>
3032#include <linux/swiotlb.h>
@@ -740,6 +742,9 @@ static struct page **__iommu_dma_alloc_pages(struct device *dev,
740742{
741743 struct page * * pages ;
742744 unsigned int i = 0 , nid = dev_to_node (dev );
745+ unsigned int j ;
746+ unsigned long min_order = __fls (order_mask );
747+ unsigned int min_order_size = 1U << min_order ;
743748
744749 order_mask &= (2U << MAX_ORDER ) - 1 ;
745750 if (!order_mask )
@@ -776,15 +781,38 @@ static struct page **__iommu_dma_alloc_pages(struct device *dev,
776781 split_page (page , order );
777782 break ;
778783 }
779- if (!page ) {
780- __iommu_dma_free_pages (pages , i );
781- return NULL ;
784+
785+ /*
786+ * If we have no valid page here we might be trying to allocate
787+ * the last block consisting of 1<<order pages (to guarantee
788+ * alignment) but actually need less pages than that.
789+ * In that case we just try to allocate the entire block and
790+ * directly free the spillover pages again.
791+ */
792+ if (!page && !order_mask && count < min_order_size ) {
793+ page = alloc_pages_node (nid , gfp , min_order );
794+ if (!page )
795+ goto free_pages ;
796+ split_page (page , min_order );
797+
798+ for (j = count ; j < min_order_size ; ++ j )
799+ __free_page (page + j );
800+
801+ order_size = count ;
782802 }
803+
804+ if (!page )
805+ goto free_pages ;
806+
783807 count -= order_size ;
784808 while (order_size -- )
785809 pages [i ++ ] = page ++ ;
786810 }
787811 return pages ;
812+
813+ free_pages :
814+ __iommu_dma_free_pages (pages , i );
815+ return NULL ;
788816}
789817
790818/*
@@ -801,16 +829,28 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
801829 bool coherent = dev_is_dma_coherent (dev );
802830 int ioprot = dma_info_to_prot (DMA_BIDIRECTIONAL , coherent , attrs );
803831 unsigned int count , min_size , alloc_sizes = domain -> pgsize_bitmap ;
832+ struct sg_append_table sgt_append = {};
833+ struct scatterlist * last_sg ;
804834 struct page * * pages ;
805835 dma_addr_t iova ;
836+ phys_addr_t orig_s_phys ;
837+ size_t orig_s_len , orig_s_off , s_iova_off , iova_size ;
806838 ssize_t ret ;
807839
808840 if (static_branch_unlikely (& iommu_deferred_attach_enabled ) &&
809841 iommu_deferred_attach (dev , domain ))
810842 return NULL ;
811843
812844 min_size = alloc_sizes & - alloc_sizes ;
813- if (min_size < PAGE_SIZE ) {
845+ if (iovad -> granule > PAGE_SIZE ) {
846+ if (size < iovad -> granule ) {
847+ /* ensure a single contiguous allocation */
848+ min_size = ALIGN (size , PAGE_SIZE * (1U <<get_order (size )));
849+ alloc_sizes = min_size ;
850+ }
851+
852+ size = PAGE_ALIGN (size );
853+ } else if (min_size < PAGE_SIZE ) {
814854 min_size = PAGE_SIZE ;
815855 alloc_sizes |= PAGE_SIZE ;
816856 } else {
@@ -825,8 +865,8 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
825865 if (!pages )
826866 return NULL ;
827867
828- size = iova_align (iovad , size );
829- iova = iommu_dma_alloc_iova (domain , size , dev -> coherent_dma_mask , dev );
868+ iova_size = iova_align (iovad , size );
869+ iova = iommu_dma_alloc_iova (domain , iova_size , dev -> coherent_dma_mask , dev );
830870 if (!iova )
831871 goto out_free_pages ;
832872
@@ -837,8 +877,12 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
837877 */
838878 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM | __GFP_COMP );
839879
840- if (sg_alloc_table_from_pages (sgt , pages , count , 0 , size , gfp ))
880+ /* append_table is only used to get a pointer to the last entry */
881+ if (sg_alloc_append_table_from_pages (& sgt_append , pages , count , 0 ,
882+ iova_size , UINT_MAX , 0 , gfp ))
841883 goto out_free_iova ;
884+ memcpy (sgt , & sgt_append .sgt , sizeof (* sgt ));
885+ last_sg = sgt_append .prv ;
842886
843887 if (!(ioprot & IOMMU_CACHE )) {
844888 struct scatterlist * sg ;
@@ -847,20 +891,58 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
847891 for_each_sg (sgt -> sgl , sg , sgt -> orig_nents , i )
848892 arch_dma_prep_coherent (sg_page (sg ), sg -> length );
849893 }
850-
894+ if (iovad -> granule > PAGE_SIZE ) {
895+ if (size < iovad -> granule ) {
896+ /*
897+ * we only have a single sg list entry here that is
898+ * likely not aligned to iovad->granule. adjust the
899+ * entry to represent the encapsulating IOMMU page
900+ * and then later restore everything to its original
901+ * values, similar to the impedance matching done in
902+ * iommu_dma_map_sg.
903+ */
904+ orig_s_phys = sg_phys (sgt -> sgl );
905+ orig_s_len = sgt -> sgl -> length ;
906+ orig_s_off = sgt -> sgl -> offset ;
907+ s_iova_off = iova_offset (iovad , orig_s_phys );
908+
909+ sg_set_page (sgt -> sgl ,
910+ pfn_to_page (PHYS_PFN (orig_s_phys - s_iova_off )),
911+ iova_align (iovad , orig_s_len + s_iova_off ),
912+ sgt -> sgl -> offset & ~s_iova_off );
913+ } else {
914+ /*
915+ * convince iommu_map_sg_atomic to map the last block
916+ * even though it may be too small.
917+ */
918+ orig_s_len = last_sg -> length ;
919+ last_sg -> length = iova_align (iovad , last_sg -> length );
920+ }
921+ }
851922 ret = iommu_map_sg (domain , iova , sgt -> sgl , sgt -> orig_nents , ioprot ,
852923 gfp );
853- if (ret < 0 || ret < size )
924+ if (ret < 0 || ret < iova_size )
854925 goto out_free_sg ;
926+ if (iovad -> granule > PAGE_SIZE ) {
927+ if (size < iovad -> granule ) {
928+ sg_set_page (sgt -> sgl ,
929+ pfn_to_page (PHYS_PFN (orig_s_phys )),
930+ orig_s_len , orig_s_off );
931+
932+ iova += s_iova_off ;
933+ } else {
934+ last_sg -> length = orig_s_len ;
935+ }
936+ }
855937
856938 sgt -> sgl -> dma_address = iova ;
857- sgt -> sgl -> dma_length = size ;
939+ sgt -> sgl -> dma_length = iova_size ;
858940 return pages ;
859941
860942out_free_sg :
861943 sg_free_table (sgt );
862944out_free_iova :
863- iommu_dma_free_iova (cookie , iova , size , NULL );
945+ iommu_dma_free_iova (cookie , iova , iova_size , NULL );
864946out_free_pages :
865947 __iommu_dma_free_pages (pages , count );
866948 return NULL ;
@@ -1098,8 +1180,9 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
10981180 continue ;
10991181 }
11001182
1101- s -> offset += s_iova_off ;
1102- s -> length = s_length ;
1183+ sg_set_page (s ,
1184+ pfn_to_page (PHYS_PFN (sg_phys (s ) + s_iova_off )),
1185+ s_length , s_iova_off & ~PAGE_MASK );
11031186
11041187 /*
11051188 * Now fill in the real DMA data. If...
@@ -1138,16 +1221,18 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
11381221static void __invalidate_sg (struct scatterlist * sg , int nents )
11391222{
11401223 struct scatterlist * s ;
1224+ phys_addr_t orig_paddr ;
11411225 int i ;
11421226
11431227 for_each_sg (sg , s , nents , i ) {
11441228 if (sg_is_dma_bus_address (s )) {
11451229 sg_dma_unmark_bus_address (s );
1146- } else {
1147- if (sg_dma_address (s ) != DMA_MAPPING_ERROR )
1148- s -> offset += sg_dma_address (s );
1149- if (sg_dma_len (s ))
1150- s -> length = sg_dma_len (s );
1230+ } else if (sg_dma_len (s )) {
1231+ orig_paddr = sg_phys (s ) + sg_dma_address (s );
1232+ sg_set_page (s ,
1233+ pfn_to_page (PHYS_PFN (orig_paddr )),
1234+ sg_dma_len (s ),
1235+ sg_dma_address (s ) & ~PAGE_MASK );
11511236 }
11521237 sg_dma_address (s ) = DMA_MAPPING_ERROR ;
11531238 sg_dma_len (s ) = 0 ;
@@ -1228,7 +1313,8 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
12281313 * stashing the unaligned parts in the as-yet-unused DMA fields.
12291314 */
12301315 for_each_sg (sg , s , nents , i ) {
1231- size_t s_iova_off = iova_offset (iovad , s -> offset );
1316+ phys_addr_t s_phys = sg_phys (s );
1317+ size_t s_iova_off = iova_offset (iovad , s_phys );
12321318 size_t s_length = s -> length ;
12331319 size_t pad_len = (mask - iova_len + 1 ) & mask ;
12341320
@@ -1258,10 +1344,9 @@ static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
12581344
12591345 sg_dma_address (s ) = s_iova_off ;
12601346 sg_dma_len (s ) = s_length ;
1261- s -> offset -= s_iova_off ;
12621347 s_length = iova_align (iovad , s_length + s_iova_off );
1263- s -> length = s_length ;
1264-
1348+ sg_set_page ( s , pfn_to_page ( PHYS_PFN ( s_phys - s_iova_off )),
1349+ s_length , s -> offset & ~ s_iova_off );
12651350 /*
12661351 * Due to the alignment of our single IOVA allocation, we can
12671352 * depend on these assumptions about the segment boundary mask:
@@ -1522,9 +1607,15 @@ static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
15221607 void * cpu_addr , dma_addr_t dma_addr , size_t size ,
15231608 unsigned long attrs )
15241609{
1610+ struct iommu_domain * domain = iommu_get_dma_domain (dev );
1611+ struct iommu_dma_cookie * cookie = domain -> iova_cookie ;
1612+ struct iova_domain * iovad = & cookie -> iovad ;
15251613 struct page * page ;
15261614 int ret ;
15271615
1616+ if (iovad -> granule > PAGE_SIZE )
1617+ return - ENXIO ;
1618+
15281619 if (is_vmalloc_addr (cpu_addr )) {
15291620 struct page * * pages = dma_common_find_pages (cpu_addr );
15301621
0 commit comments