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