aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/iommu-helper.h7
-rw-r--r--lib/Makefile1
-rw-r--r--lib/iommu-helper.c80
3 files changed, 88 insertions, 0 deletions
diff --git a/include/linux/iommu-helper.h b/include/linux/iommu-helper.h
new file mode 100644
index 000000000000..4dd4c04ff2f4
--- /dev/null
+++ b/include/linux/iommu-helper.h
@@ -0,0 +1,7 @@
1extern unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
2 unsigned long start, unsigned int nr,
3 unsigned long shift,
4 unsigned long boundary_size,
5 unsigned long align_mask);
6extern void iommu_area_free(unsigned long *map, unsigned long start,
7 unsigned int nr);
diff --git a/lib/Makefile b/lib/Makefile
index 543f2502b60a..a18062e4633f 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -65,6 +65,7 @@ obj-$(CONFIG_SMP) += pcounter.o
65obj-$(CONFIG_AUDIT_GENERIC) += audit.o 65obj-$(CONFIG_AUDIT_GENERIC) += audit.o
66 66
67obj-$(CONFIG_SWIOTLB) += swiotlb.o 67obj-$(CONFIG_SWIOTLB) += swiotlb.o
68obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o
68obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o 69obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o
69 70
70lib-$(CONFIG_GENERIC_BUG) += bug.o 71lib-$(CONFIG_GENERIC_BUG) += bug.o
diff --git a/lib/iommu-helper.c b/lib/iommu-helper.c
new file mode 100644
index 000000000000..495575a59ca6
--- /dev/null
+++ b/lib/iommu-helper.c
@@ -0,0 +1,80 @@
1/*
2 * IOMMU helper functions for the free area management
3 */
4
5#include <linux/module.h>
6#include <linux/bitops.h>
7
8static unsigned long find_next_zero_area(unsigned long *map,
9 unsigned long size,
10 unsigned long start,
11 unsigned int nr,
12 unsigned long align_mask)
13{
14 unsigned long index, end, i;
15again:
16 index = find_next_zero_bit(map, size, start);
17
18 /* Align allocation */
19 index = (index + align_mask) & ~align_mask;
20
21 end = index + nr;
22 if (end >= size)
23 return -1;
24 for (i = index; i < end; i++) {
25 if (test_bit(i, map)) {
26 start = i+1;
27 goto again;
28 }
29 }
30 return index;
31}
32
33static inline void set_bit_area(unsigned long *map, unsigned long i,
34 int len)
35{
36 unsigned long end = i + len;
37 while (i < end) {
38 __set_bit(i, map);
39 i++;
40 }
41}
42
43static inline int is_span_boundary(unsigned int index, unsigned int nr,
44 unsigned long shift,
45 unsigned long boundary_size)
46{
47 shift = (shift + index) & (boundary_size - 1);
48 return shift + nr > boundary_size;
49}
50
51unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
52 unsigned long start, unsigned int nr,
53 unsigned long shift, unsigned long boundary_size,
54 unsigned long align_mask)
55{
56 unsigned long index;
57again:
58 index = find_next_zero_area(map, size, start, nr, align_mask);
59 if (index != -1) {
60 if (is_span_boundary(index, nr, shift, boundary_size)) {
61 /* we could do more effectively */
62 start = index + 1;
63 goto again;
64 }
65 set_bit_area(map, index, nr);
66 }
67 return index;
68}
69EXPORT_SYMBOL(iommu_area_alloc);
70
71void iommu_area_free(unsigned long *map, unsigned long start, unsigned int nr)
72{
73 unsigned long end = start + nr;
74
75 while (start < end) {
76 __clear_bit(start, map);
77 start++;
78 }
79}
80EXPORT_SYMBOL(iommu_area_free);