b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) // SPDX-License-Identifier: GPL-2.0
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 2) /*
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 3) * IOMMU helper functions for the free area management
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 4) */
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 5)
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 6) #include <linux/bitmap.h>
79c1879ee5473 (Christoph Hellwig 2018-04-03 15:41:07 +0200 7) #include <linux/iommu-helper.h>
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 8)
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 9) unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 10) unsigned long start, unsigned int nr,
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 11) unsigned long shift, unsigned long boundary_size,
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 12) unsigned long align_mask)
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 13) {
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 14) unsigned long index;
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 15)
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 16) /* We don't want the last of the limit */
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 17) size -= 1;
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 18) again:
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 19) index = bitmap_find_next_zero_area(map, size, start, nr, align_mask);
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 20) if (index < size) {
3715863aa142c (FUJITA Tomonori 2008-03-04 14:29:27 -0800 21) if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
f003a1f182bb8 (Sebastian Ott 2016-08-02 14:04:13 -0700 22) start = ALIGN(shift + index, boundary_size) - shift;
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 23) goto again;
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 24) }
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 25) bitmap_set(map, index, nr);
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 26) return index;
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 27) }
a66022c457755 (Akinobu Mita 2009-12-15 16:48:28 -0800 28) return -1;
0291df8cc9dac (FUJITA Tomonori 2008-02-04 22:28:07 -0800 29) }