blob: a816f3a806258c8ea08ac787c8eee9841d3204bc [file] [log] [blame]
FUJITA Tomonori0291df82008-02-04 22:28:07 -08001/*
2 * IOMMU helper functions for the free area management
3 */
4
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05005#include <linux/export.h>
Akinobu Mitaa66022c2009-12-15 16:48:28 -08006#include <linux/bitmap.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -05007#include <linux/bug.h>
FUJITA Tomonori0291df82008-02-04 22:28:07 -08008
FUJITA Tomonori37158632008-03-04 14:29:27 -08009int iommu_is_span_boundary(unsigned int index, unsigned int nr,
10 unsigned long shift,
11 unsigned long boundary_size)
FUJITA Tomonori0291df82008-02-04 22:28:07 -080012{
FUJITA Tomonori37158632008-03-04 14:29:27 -080013 BUG_ON(!is_power_of_2(boundary_size));
14
FUJITA Tomonori0291df82008-02-04 22:28:07 -080015 shift = (shift + index) & (boundary_size - 1);
16 return shift + nr > boundary_size;
17}
18
19unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
20 unsigned long start, unsigned int nr,
21 unsigned long shift, unsigned long boundary_size,
22 unsigned long align_mask)
23{
24 unsigned long index;
Akinobu Mitaa66022c2009-12-15 16:48:28 -080025
26 /* We don't want the last of the limit */
27 size -= 1;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080028again:
Akinobu Mitaa66022c2009-12-15 16:48:28 -080029 index = bitmap_find_next_zero_area(map, size, start, nr, align_mask);
30 if (index < size) {
FUJITA Tomonori37158632008-03-04 14:29:27 -080031 if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
Sebastian Ottf003a1f2016-08-02 14:04:13 -070032 start = ALIGN(shift + index, boundary_size) - shift;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080033 goto again;
34 }
Akinobu Mitaa66022c2009-12-15 16:48:28 -080035 bitmap_set(map, index, nr);
36 return index;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080037 }
Akinobu Mitaa66022c2009-12-15 16:48:28 -080038 return -1;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080039}
40EXPORT_SYMBOL(iommu_area_alloc);