@@ -1069,48 +1069,96 @@ fs_initcall(dma_debug_do_init);
10691069
10701070/* IOMMU */
10711071
1072+ static int extend_iommu_mapping (struct dma_iommu_mapping * mapping );
1073+
10721074static inline dma_addr_t __alloc_iova (struct dma_iommu_mapping * mapping ,
10731075 size_t size )
10741076{
10751077 unsigned int order = get_order (size );
10761078 unsigned int align = 0 ;
10771079 unsigned int count , start ;
10781080 unsigned long flags ;
1081+ dma_addr_t iova ;
1082+ int i ;
10791083
10801084 if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT )
10811085 order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT ;
10821086
1083- count = ((PAGE_ALIGN (size ) >> PAGE_SHIFT ) +
1084- (1 << mapping -> order ) - 1 ) >> mapping -> order ;
1085-
1086- if (order > mapping -> order )
1087- align = (1 << (order - mapping -> order )) - 1 ;
1087+ count = PAGE_ALIGN (size ) >> PAGE_SHIFT ;
1088+ align = (1 << order ) - 1 ;
10881089
10891090 spin_lock_irqsave (& mapping -> lock , flags );
1090- start = bitmap_find_next_zero_area (mapping -> bitmap , mapping -> bits , 0 ,
1091- count , align );
1092- if (start > mapping -> bits ) {
1093- spin_unlock_irqrestore (& mapping -> lock , flags );
1094- return DMA_ERROR_CODE ;
1091+ for (i = 0 ; i < mapping -> nr_bitmaps ; i ++ ) {
1092+ start = bitmap_find_next_zero_area (mapping -> bitmaps [i ],
1093+ mapping -> bits , 0 , count , align );
1094+
1095+ if (start > mapping -> bits )
1096+ continue ;
1097+
1098+ bitmap_set (mapping -> bitmaps [i ], start , count );
1099+ break ;
10951100 }
10961101
1097- bitmap_set (mapping -> bitmap , start , count );
1102+ /*
1103+ * No unused range found. Try to extend the existing mapping
1104+ * and perform a second attempt to reserve an IO virtual
1105+ * address range of size bytes.
1106+ */
1107+ if (i == mapping -> nr_bitmaps ) {
1108+ if (extend_iommu_mapping (mapping )) {
1109+ spin_unlock_irqrestore (& mapping -> lock , flags );
1110+ return DMA_ERROR_CODE ;
1111+ }
1112+
1113+ start = bitmap_find_next_zero_area (mapping -> bitmaps [i ],
1114+ mapping -> bits , 0 , count , align );
1115+
1116+ if (start > mapping -> bits ) {
1117+ spin_unlock_irqrestore (& mapping -> lock , flags );
1118+ return DMA_ERROR_CODE ;
1119+ }
1120+
1121+ bitmap_set (mapping -> bitmaps [i ], start , count );
1122+ }
10981123 spin_unlock_irqrestore (& mapping -> lock , flags );
10991124
1100- return mapping -> base + (start << (mapping -> order + PAGE_SHIFT ));
1125+ iova = mapping -> base + (mapping -> size * i );
1126+ iova += start << PAGE_SHIFT ;
1127+
1128+ return iova ;
11011129}
11021130
11031131static inline void __free_iova (struct dma_iommu_mapping * mapping ,
11041132 dma_addr_t addr , size_t size )
11051133{
1106- unsigned int start = (addr - mapping -> base ) >>
1107- (mapping -> order + PAGE_SHIFT );
1108- unsigned int count = ((size >> PAGE_SHIFT ) +
1109- (1 << mapping -> order ) - 1 ) >> mapping -> order ;
1134+ unsigned int start , count ;
11101135 unsigned long flags ;
1136+ dma_addr_t bitmap_base ;
1137+ u32 bitmap_index ;
1138+
1139+ if (!size )
1140+ return ;
1141+
1142+ bitmap_index = (u32 ) (addr - mapping -> base ) / (u32 ) mapping -> size ;
1143+ BUG_ON (addr < mapping -> base || bitmap_index > mapping -> extensions );
1144+
1145+ bitmap_base = mapping -> base + mapping -> size * bitmap_index ;
1146+
1147+ start = (addr - bitmap_base ) >> PAGE_SHIFT ;
1148+
1149+ if (addr + size > bitmap_base + mapping -> size ) {
1150+ /*
1151+ * The address range to be freed reaches into the iova
1152+ * range of the next bitmap. This should not happen as
1153+ * we don't allow this in __alloc_iova (at the
1154+ * moment).
1155+ */
1156+ BUG ();
1157+ } else
1158+ count = size >> PAGE_SHIFT ;
11111159
11121160 spin_lock_irqsave (& mapping -> lock , flags );
1113- bitmap_clear (mapping -> bitmap , start , count );
1161+ bitmap_clear (mapping -> bitmaps [ bitmap_index ] , start , count );
11141162 spin_unlock_irqrestore (& mapping -> lock , flags );
11151163}
11161164
@@ -1875,8 +1923,7 @@ struct dma_map_ops iommu_coherent_ops = {
18751923 * arm_iommu_create_mapping
18761924 * @bus: pointer to the bus holding the client device (for IOMMU calls)
18771925 * @base: start address of the valid IO address space
1878- * @size: size of the valid IO address space
1879- * @order: accuracy of the IO addresses allocations
1926+ * @size: maximum size of the valid IO address space
18801927 *
18811928 * Creates a mapping structure which holds information about used/unused
18821929 * IO address ranges, which is required to perform memory allocation and
@@ -1886,38 +1933,54 @@ struct dma_map_ops iommu_coherent_ops = {
18861933 * arm_iommu_attach_device function.
18871934 */
18881935struct dma_iommu_mapping *
1889- arm_iommu_create_mapping (struct bus_type * bus , dma_addr_t base , size_t size ,
1890- int order )
1936+ arm_iommu_create_mapping (struct bus_type * bus , dma_addr_t base , size_t size )
18911937{
1892- unsigned int count = size >> ( PAGE_SHIFT + order ) ;
1893- unsigned int bitmap_size = BITS_TO_LONGS (count ) * sizeof (long );
1938+ unsigned int bits = size >> PAGE_SHIFT ;
1939+ unsigned int bitmap_size = BITS_TO_LONGS (bits ) * sizeof (long );
18941940 struct dma_iommu_mapping * mapping ;
1941+ int extensions = 1 ;
18951942 int err = - ENOMEM ;
18961943
1897- if (!count )
1944+ if (!bitmap_size )
18981945 return ERR_PTR (- EINVAL );
18991946
1947+ if (bitmap_size > PAGE_SIZE ) {
1948+ extensions = bitmap_size / PAGE_SIZE ;
1949+ bitmap_size = PAGE_SIZE ;
1950+ }
1951+
19001952 mapping = kzalloc (sizeof (struct dma_iommu_mapping ), GFP_KERNEL );
19011953 if (!mapping )
19021954 goto err ;
19031955
1904- mapping -> bitmap = kzalloc (bitmap_size , GFP_KERNEL );
1905- if (!mapping -> bitmap )
1956+ mapping -> bitmap_size = bitmap_size ;
1957+ mapping -> bitmaps = kzalloc (extensions * sizeof (unsigned long * ),
1958+ GFP_KERNEL );
1959+ if (!mapping -> bitmaps )
19061960 goto err2 ;
19071961
1962+ mapping -> bitmaps [0 ] = kzalloc (bitmap_size , GFP_KERNEL );
1963+ if (!mapping -> bitmaps [0 ])
1964+ goto err3 ;
1965+
1966+ mapping -> nr_bitmaps = 1 ;
1967+ mapping -> extensions = extensions ;
19081968 mapping -> base = base ;
1969+ mapping -> size = bitmap_size << PAGE_SHIFT ;
19091970 mapping -> bits = BITS_PER_BYTE * bitmap_size ;
1910- mapping -> order = order ;
1971+
19111972 spin_lock_init (& mapping -> lock );
19121973
19131974 mapping -> domain = iommu_domain_alloc (bus );
19141975 if (!mapping -> domain )
1915- goto err3 ;
1976+ goto err4 ;
19161977
19171978 kref_init (& mapping -> kref );
19181979 return mapping ;
1980+ err4 :
1981+ kfree (mapping -> bitmaps [0 ]);
19191982err3 :
1920- kfree (mapping -> bitmap );
1983+ kfree (mapping -> bitmaps );
19211984err2 :
19221985 kfree (mapping );
19231986err :
@@ -1927,14 +1990,35 @@ EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
19271990
19281991static void release_iommu_mapping (struct kref * kref )
19291992{
1993+ int i ;
19301994 struct dma_iommu_mapping * mapping =
19311995 container_of (kref , struct dma_iommu_mapping , kref );
19321996
19331997 iommu_domain_free (mapping -> domain );
1934- kfree (mapping -> bitmap );
1998+ for (i = 0 ; i < mapping -> nr_bitmaps ; i ++ )
1999+ kfree (mapping -> bitmaps [i ]);
2000+ kfree (mapping -> bitmaps );
19352001 kfree (mapping );
19362002}
19372003
2004+ static int extend_iommu_mapping (struct dma_iommu_mapping * mapping )
2005+ {
2006+ int next_bitmap ;
2007+
2008+ if (mapping -> nr_bitmaps > mapping -> extensions )
2009+ return - EINVAL ;
2010+
2011+ next_bitmap = mapping -> nr_bitmaps ;
2012+ mapping -> bitmaps [next_bitmap ] = kzalloc (mapping -> bitmap_size ,
2013+ GFP_ATOMIC );
2014+ if (!mapping -> bitmaps [next_bitmap ])
2015+ return - ENOMEM ;
2016+
2017+ mapping -> nr_bitmaps ++ ;
2018+
2019+ return 0 ;
2020+ }
2021+
19382022void arm_iommu_release_mapping (struct dma_iommu_mapping * mapping )
19392023{
19402024 if (mapping )
0 commit comments