Blame view

lib/iommu-helper.c 1.99 KB
0291df8cc   FUJITA Tomonori   iommu sg: add IOM...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /*
   * IOMMU helper functions for the free area management
   */
  
  #include <linux/module.h>
  #include <linux/bitops.h>
  
  static unsigned long find_next_zero_area(unsigned long *map,
  					 unsigned long size,
  					 unsigned long start,
  					 unsigned int nr,
  					 unsigned long align_mask)
  {
  	unsigned long index, end, i;
  again:
  	index = find_next_zero_bit(map, size, start);
  
  	/* Align allocation */
  	index = (index + align_mask) & ~align_mask;
  
  	end = index + nr;
  	if (end >= size)
  		return -1;
  	for (i = index; i < end; i++) {
  		if (test_bit(i, map)) {
  			start = i+1;
  			goto again;
  		}
  	}
  	return index;
  }
d26dbc5cf   FUJITA Tomonori   iommu: export iom...
32
  void iommu_area_reserve(unsigned long *map, unsigned long i, int len)
0291df8cc   FUJITA Tomonori   iommu sg: add IOM...
33
34
35
36
37
38
39
  {
  	unsigned long end = i + len;
  	while (i < end) {
  		__set_bit(i, map);
  		i++;
  	}
  }
3715863aa   FUJITA Tomonori   iommu: export iom...
40
41
42
  int iommu_is_span_boundary(unsigned int index, unsigned int nr,
  			   unsigned long shift,
  			   unsigned long boundary_size)
0291df8cc   FUJITA Tomonori   iommu sg: add IOM...
43
  {
3715863aa   FUJITA Tomonori   iommu: export iom...
44
  	BUG_ON(!is_power_of_2(boundary_size));
0291df8cc   FUJITA Tomonori   iommu sg: add IOM...
45
46
47
48
49
50
51
52
53
54
55
56
57
  	shift = (shift + index) & (boundary_size - 1);
  	return shift + nr > boundary_size;
  }
  
  unsigned long iommu_area_alloc(unsigned long *map, unsigned long size,
  			       unsigned long start, unsigned int nr,
  			       unsigned long shift, unsigned long boundary_size,
  			       unsigned long align_mask)
  {
  	unsigned long index;
  again:
  	index = find_next_zero_area(map, size, start, nr, align_mask);
  	if (index != -1) {
3715863aa   FUJITA Tomonori   iommu: export iom...
58
  		if (iommu_is_span_boundary(index, nr, shift, boundary_size)) {
0291df8cc   FUJITA Tomonori   iommu sg: add IOM...
59
60
61
62
  			/* we could do more effectively */
  			start = index + 1;
  			goto again;
  		}
d26dbc5cf   FUJITA Tomonori   iommu: export iom...
63
  		iommu_area_reserve(map, index, nr);
0291df8cc   FUJITA Tomonori   iommu sg: add IOM...
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  	}
  	return index;
  }
  EXPORT_SYMBOL(iommu_area_alloc);
  
  void iommu_area_free(unsigned long *map, unsigned long start, unsigned int nr)
  {
  	unsigned long end = start + nr;
  
  	while (start < end) {
  		__clear_bit(start, map);
  		start++;
  	}
  }
  EXPORT_SYMBOL(iommu_area_free);
56d936607   Joerg Roedel   introduce generic...
79
80
81
82
83
84
85
86
87
  
  unsigned long iommu_num_pages(unsigned long addr, unsigned long len,
  			      unsigned long io_page_size)
  {
  	unsigned long size = (addr & (io_page_size - 1)) + len;
  
  	return DIV_ROUND_UP(size, io_page_size);
  }
  EXPORT_SYMBOL(iommu_num_pages);