blob: 8b1ab6222562ee9d973e51595c4bd9be9835c196 [file] [log] [blame]
FUJITA Tomonori0291df82008-02-04 22:28:07 -08001/*
2 * IOMMU helper functions for the free area management
3 */
4
5#include <linux/module.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)) {
FUJITA Tomonori0291df82008-02-04 22:28:07 -080032 /* we could do more effectively */
33 start = index + 1;
34 goto again;
35 }
Akinobu Mitaa66022c2009-12-15 16:48:28 -080036 bitmap_set(map, index, nr);
37 return index;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080038 }
Akinobu Mitaa66022c2009-12-15 16:48:28 -080039 return -1;
FUJITA Tomonori0291df82008-02-04 22:28:07 -080040}
41EXPORT_SYMBOL(iommu_area_alloc);