iommu-helper.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * IOMMU helper functions for the free area management
  3. */
  4. #include <linux/export.h>
  5. #include <linux/bitmap.h>
  6. #include <linux/bug.h>
  7. int iommu_is_span_boundary(unsigned int index, unsigned int nr,
  8. unsigned long shift,
  9. unsigned long boundary_size)
  10. {
  11. BUG_ON(!is_power_of_2(boundary_size));
  12. shift = (shift + index) & (boundary_size - 1);
  13. return shift + nr > boundary_size;
  14. }
  15. unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
  16. unsigned long start, unsigned int nr,
  17. unsigned long shift, unsigned long boundary_size,
  18. unsigned long align_mask)
  19. {
  20. unsigned long index;
  21. /* We don't want the last of the limit */
  22. size -= 1;
  23. again:
  24. index = bitmap_find_next_zero_area(map, size, start, nr, align_mask);
  25. if (index < size) {
  26. if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
  27. start = ALIGN(shift + index, boundary_size) - shift;
  28. goto again;
  29. }
  30. bitmap_set(map, index, nr);
  31. return index;
  32. }
  33. return -1;
  34. }
  35. EXPORT_SYMBOL(iommu_area_alloc);