| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * IOMMU helper functions for the free area management | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-16 21:29:17 -05:00
										 |  |  | #include <linux/export.h>
 | 
					
						
							| 
									
										
										
										
											2009-12-15 16:48:28 -08:00
										 |  |  | #include <linux/bitmap.h>
 | 
					
						
							| 
									
										
										
										
											2012-01-20 18:35:53 -05:00
										 |  |  | #include <linux/bug.h>
 | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-04 14:29:27 -08:00
										 |  |  | int iommu_is_span_boundary(unsigned int index, unsigned int nr, | 
					
						
							|  |  |  | 			   unsigned long shift, | 
					
						
							|  |  |  | 			   unsigned long boundary_size) | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-03-04 14:29:27 -08:00
										 |  |  | 	BUG_ON(!is_power_of_2(boundary_size)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											2009-12-15 16:48:28 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* We don't want the last of the limit */ | 
					
						
							|  |  |  | 	size -= 1; | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | again: | 
					
						
							| 
									
										
										
										
											2009-12-15 16:48:28 -08:00
										 |  |  | 	index = bitmap_find_next_zero_area(map, size, start, nr, align_mask); | 
					
						
							|  |  |  | 	if (index < size) { | 
					
						
							| 
									
										
										
										
											2008-03-04 14:29:27 -08:00
										 |  |  | 		if (iommu_is_span_boundary(index, nr, shift, boundary_size)) { | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | 			/* we could do more effectively */ | 
					
						
							|  |  |  | 			start = index + 1; | 
					
						
							|  |  |  | 			goto again; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-12-15 16:48:28 -08:00
										 |  |  | 		bitmap_set(map, index, nr); | 
					
						
							|  |  |  | 		return index; | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-12-15 16:48:28 -08:00
										 |  |  | 	return -1; | 
					
						
							| 
									
										
										
										
											2008-02-04 22:28:07 -08:00
										 |  |  | } | 
					
						
							|  |  |  | EXPORT_SYMBOL(iommu_area_alloc); |