From ea8a218e87fb4b26ce48717f9051df6d8fe32ab2 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Mon, 4 Feb 2008 22:28:07 -0800 Subject: [PATCH] --- yaml --- r: 83019 b: refs/heads/master c: 0291df8cc9dac09c303d21d5bcd2ad73762c836a h: refs/heads/master i: 83017: 7f482aa5d1882deb4d886d1886c49babdc4126ff 83015: 28f6b511457481a2132b27351730baa1367884c0 v: v3 --- [refs] | 2 +- trunk/include/linux/iommu-helper.h | 7 +++ trunk/lib/Makefile | 1 + trunk/lib/iommu-helper.c | 80 ++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 trunk/include/linux/iommu-helper.h create mode 100644 trunk/lib/iommu-helper.c diff --git a/[refs] b/[refs] index e22a0fb5fc0a..f035bd3a5bff 100644 --- a/[refs] +++ b/[refs] @@ -1,2 +1,2 @@ --- -refs/heads/master: 0c95fdc59640824d7e0b017be295fb912ceef4ab +refs/heads/master: 0291df8cc9dac09c303d21d5bcd2ad73762c836a diff --git a/trunk/include/linux/iommu-helper.h b/trunk/include/linux/iommu-helper.h new file mode 100644 index 000000000000..4dd4c04ff2f4 --- /dev/null +++ b/trunk/include/linux/iommu-helper.h @@ -0,0 +1,7 @@ +extern 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); +extern void iommu_area_free(unsigned long *map, unsigned long start, + unsigned int nr); diff --git a/trunk/lib/Makefile b/trunk/lib/Makefile index 543f2502b60a..a18062e4633f 100644 --- a/trunk/lib/Makefile +++ b/trunk/lib/Makefile @@ -65,6 +65,7 @@ obj-$(CONFIG_SMP) += pcounter.o obj-$(CONFIG_AUDIT_GENERIC) += audit.o obj-$(CONFIG_SWIOTLB) += swiotlb.o +obj-$(CONFIG_IOMMU_HELPER) += iommu-helper.o obj-$(CONFIG_FAULT_INJECTION) += fault-inject.o lib-$(CONFIG_GENERIC_BUG) += bug.o diff --git a/trunk/lib/iommu-helper.c b/trunk/lib/iommu-helper.c new file mode 100644 index 000000000000..495575a59ca6 --- /dev/null +++ b/trunk/lib/iommu-helper.c @@ -0,0 +1,80 @@ +/* + * IOMMU helper functions for the free area management + */ + +#include +#include + +static unsigned long find_next_zero_area(unsigned long *map, + unsigned long size, + unsigned long start, + unsigned int nr, + unsigned long align_mask) +{ + unsigned long index, end, i; +again: + index = find_next_zero_bit(map, size, start); + + /* Align allocation */ + index = (index + align_mask) & ~align_mask; + + end = index + nr; + if (end >= size) + return -1; + for (i = index; i < end; i++) { + if (test_bit(i, map)) { + start = i+1; + goto again; + } + } + return index; +} + +static inline void set_bit_area(unsigned long *map, unsigned long i, + int len) +{ + unsigned long end = i + len; + while (i < end) { + __set_bit(i, map); + i++; + } +} + +static inline int is_span_boundary(unsigned int index, unsigned int nr, + unsigned long shift, + unsigned long boundary_size) +{ + 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; +again: + index = find_next_zero_area(map, size, start, nr, align_mask); + if (index != -1) { + if (is_span_boundary(index, nr, shift, boundary_size)) { + /* we could do more effectively */ + start = index + 1; + goto again; + } + set_bit_area(map, index, nr); + } + return index; +} +EXPORT_SYMBOL(iommu_area_alloc); + +void iommu_area_free(unsigned long *map, unsigned long start, unsigned int nr) +{ + unsigned long end = start + nr; + + while (start < end) { + __clear_bit(start, map); + start++; + } +} +EXPORT_SYMBOL(iommu_area_free);