blob: 92a9f243c0e25d79eb002ea77de8015b0a917090 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
FUJITA Tomonori0291df82008-02-04 22:28:07 -08002/*
3 * IOMMU helper functions for the free area management
4 */
5
Akinobu Mitaa66022c2009-12-15 16:48:28 -08006#include <linux/bitmap.h>
Christoph Hellwig79c18792018-04-03 15:41:07 +02007#include <linux/iommu-helper.h>
FUJITA Tomonori0291df82008-02-04 22:28:07 -08008
9unsigned 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 Mitaa66022c2009-12-15 16:48:28 -080015
16 /* We don't want the last of the limit */
17 size -= 1;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080018again:
Akinobu Mitaa66022c2009-12-15 16:48:28 -080019 index = bitmap_find_next_zero_area(map, size, start, nr, align_mask);
20 if (index < size) {
FUJITA Tomonori37158632008-03-04 14:29:27 -080021 if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
Sebastian Ottf003a1f2016-08-02 14:04:13 -070022 start = ALIGN(shift + index, boundary_size) - shift;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080023 goto again;
24 }
Akinobu Mitaa66022c2009-12-15 16:48:28 -080025 bitmap_set(map, index, nr);
26 return index;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080027 }
Akinobu Mitaa66022c2009-12-15 16:48:28 -080028 return -1;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080029}