iommu-helper.c 1006 B

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