iommu-helper.c 1.0 KB
Newer Older
1 2 3 4 5
/*
 * IOMMU helper functions for the free area management
 */

#include <linux/module.h>
A
Akinobu Mita 已提交
6
#include <linux/bitmap.h>
7
#include <linux/bug.h>
8

9 10 11
int iommu_is_span_boundary(unsigned int index, unsigned int nr,
			   unsigned long shift,
			   unsigned long boundary_size)
12
{
13 14
	BUG_ON(!is_power_of_2(boundary_size));

15 16 17 18 19 20 21 22 23 24
	shift = (shift + index) & (boundary_size - 1);
	return shift + nr > boundary_size;
}

unsigned long 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)
{
	unsigned long index;
A
Akinobu Mita 已提交
25 26 27

	/* We don't want the last of the limit */
	size -= 1;
28
again:
A
Akinobu Mita 已提交
29 30
	index = bitmap_find_next_zero_area(map, size, start, nr, align_mask);
	if (index < size) {
31
		if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
32 33 34 35
			/* we could do more effectively */
			start = index + 1;
			goto again;
		}
A
Akinobu Mita 已提交
36 37
		bitmap_set(map, index, nr);
		return index;
38
	}
A
Akinobu Mita 已提交
39
	return -1;
40 41
}
EXPORT_SYMBOL(iommu_area_alloc);