xref: /OK3568_Linux_fs/kernel/lib/iommu-helper.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * IOMMU helper functions for the free area management
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/bitmap.h>
7*4882a593Smuzhiyun #include <linux/iommu-helper.h>
8*4882a593Smuzhiyun 
iommu_area_alloc(unsigned long * map,unsigned long size,unsigned long start,unsigned int nr,unsigned long shift,unsigned long boundary_size,unsigned long align_mask)9*4882a593Smuzhiyun unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
10*4882a593Smuzhiyun 			       unsigned long start, unsigned int nr,
11*4882a593Smuzhiyun 			       unsigned long shift, unsigned long boundary_size,
12*4882a593Smuzhiyun 			       unsigned long align_mask)
13*4882a593Smuzhiyun {
14*4882a593Smuzhiyun 	unsigned long index;
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun 	/* We don't want the last of the limit */
17*4882a593Smuzhiyun 	size -= 1;
18*4882a593Smuzhiyun again:
19*4882a593Smuzhiyun 	index = bitmap_find_next_zero_area(map, size, start, nr, align_mask);
20*4882a593Smuzhiyun 	if (index < size) {
21*4882a593Smuzhiyun 		if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
22*4882a593Smuzhiyun 			start = ALIGN(shift + index, boundary_size) - shift;
23*4882a593Smuzhiyun 			goto again;
24*4882a593Smuzhiyun 		}
25*4882a593Smuzhiyun 		bitmap_set(map, index, nr);
26*4882a593Smuzhiyun 		return index;
27*4882a593Smuzhiyun 	}
28*4882a593Smuzhiyun 	return -1;
29*4882a593Smuzhiyun }
30