| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 1 | /* | 
| David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 2 |  * Copyright © 2006-2009, Intel Corporation. | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 3 |  * | 
| David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 4 |  * This program is free software; you can redistribute it and/or modify it | 
 | 5 |  * under the terms and conditions of the GNU General Public License, | 
 | 6 |  * version 2, as published by the Free Software Foundation. | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 7 |  * | 
| David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 8 |  * This program is distributed in the hope it will be useful, but WITHOUT | 
 | 9 |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
 | 10 |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for | 
 | 11 |  * more details. | 
 | 12 |  * | 
 | 13 |  * You should have received a copy of the GNU General Public License along with | 
 | 14 |  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
 | 15 |  * Place - Suite 330, Boston, MA 02111-1307 USA. | 
 | 16 |  * | 
| mark gross | 98bcef5 | 2008-02-23 15:23:35 -0800 | [diff] [blame] | 17 |  * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 18 |  */ | 
 | 19 |  | 
| Kay, Allen M | 3871794 | 2008-09-09 18:37:29 +0300 | [diff] [blame] | 20 | #include <linux/iova.h> | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 21 |  | 
 | 22 | void | 
| David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 23 | init_iova_domain(struct iova_domain *iovad, unsigned long pfn_32bit) | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 24 | { | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 25 | 	spin_lock_init(&iovad->iova_rbtree_lock); | 
 | 26 | 	iovad->rbroot = RB_ROOT; | 
 | 27 | 	iovad->cached32_node = NULL; | 
| David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 28 | 	iovad->dma_32bit_pfn = pfn_32bit; | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 29 | } | 
 | 30 |  | 
 | 31 | static struct rb_node * | 
 | 32 | __get_cached_rbnode(struct iova_domain *iovad, unsigned long *limit_pfn) | 
 | 33 | { | 
| David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 34 | 	if ((*limit_pfn != iovad->dma_32bit_pfn) || | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 35 | 		(iovad->cached32_node == NULL)) | 
 | 36 | 		return rb_last(&iovad->rbroot); | 
 | 37 | 	else { | 
 | 38 | 		struct rb_node *prev_node = rb_prev(iovad->cached32_node); | 
 | 39 | 		struct iova *curr_iova = | 
 | 40 | 			container_of(iovad->cached32_node, struct iova, node); | 
 | 41 | 		*limit_pfn = curr_iova->pfn_lo - 1; | 
 | 42 | 		return prev_node; | 
 | 43 | 	} | 
 | 44 | } | 
 | 45 |  | 
 | 46 | static void | 
 | 47 | __cached_rbnode_insert_update(struct iova_domain *iovad, | 
 | 48 | 	unsigned long limit_pfn, struct iova *new) | 
 | 49 | { | 
| David Miller | f661197 | 2008-02-06 01:36:23 -0800 | [diff] [blame] | 50 | 	if (limit_pfn != iovad->dma_32bit_pfn) | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 51 | 		return; | 
 | 52 | 	iovad->cached32_node = &new->node; | 
 | 53 | } | 
 | 54 |  | 
 | 55 | static void | 
 | 56 | __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free) | 
 | 57 | { | 
 | 58 | 	struct iova *cached_iova; | 
 | 59 | 	struct rb_node *curr; | 
 | 60 |  | 
 | 61 | 	if (!iovad->cached32_node) | 
 | 62 | 		return; | 
 | 63 | 	curr = iovad->cached32_node; | 
 | 64 | 	cached_iova = container_of(curr, struct iova, node); | 
 | 65 |  | 
| Chris Wright | 1c9fc3d | 2011-05-28 13:15:04 -0500 | [diff] [blame] | 66 | 	if (free->pfn_lo >= cached_iova->pfn_lo) { | 
 | 67 | 		struct rb_node *node = rb_next(&free->node); | 
 | 68 | 		struct iova *iova = container_of(node, struct iova, node); | 
 | 69 |  | 
 | 70 | 		/* only cache if it's below 32bit pfn */ | 
 | 71 | 		if (node && iova->pfn_lo < iovad->dma_32bit_pfn) | 
 | 72 | 			iovad->cached32_node = node; | 
 | 73 | 		else | 
 | 74 | 			iovad->cached32_node = NULL; | 
 | 75 | 	} | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 76 | } | 
 | 77 |  | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 78 | /* Computes the padding size required, to make the | 
 | 79 |  * the start address naturally aligned on its size | 
 | 80 |  */ | 
 | 81 | static int | 
 | 82 | iova_get_pad_size(int size, unsigned int limit_pfn) | 
 | 83 | { | 
 | 84 | 	unsigned int pad_size = 0; | 
 | 85 | 	unsigned int order = ilog2(size); | 
 | 86 |  | 
 | 87 | 	if (order) | 
 | 88 | 		pad_size = (limit_pfn + 1) % (1 << order); | 
 | 89 |  | 
 | 90 | 	return pad_size; | 
 | 91 | } | 
 | 92 |  | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 93 | static int __alloc_and_insert_iova_range(struct iova_domain *iovad, | 
 | 94 | 		unsigned long size, unsigned long limit_pfn, | 
 | 95 | 			struct iova *new, bool size_aligned) | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 96 | { | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 97 | 	struct rb_node *prev, *curr = NULL; | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 98 | 	unsigned long flags; | 
 | 99 | 	unsigned long saved_pfn; | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 100 | 	unsigned int pad_size = 0; | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 101 |  | 
 | 102 | 	/* Walk the tree backwards */ | 
 | 103 | 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | 
 | 104 | 	saved_pfn = limit_pfn; | 
 | 105 | 	curr = __get_cached_rbnode(iovad, &limit_pfn); | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 106 | 	prev = curr; | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 107 | 	while (curr) { | 
 | 108 | 		struct iova *curr_iova = container_of(curr, struct iova, node); | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 109 |  | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 110 | 		if (limit_pfn < curr_iova->pfn_lo) | 
 | 111 | 			goto move_left; | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 112 | 		else if (limit_pfn < curr_iova->pfn_hi) | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 113 | 			goto adjust_limit_pfn; | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 114 | 		else { | 
 | 115 | 			if (size_aligned) | 
 | 116 | 				pad_size = iova_get_pad_size(size, limit_pfn); | 
 | 117 | 			if ((curr_iova->pfn_hi + size + pad_size) <= limit_pfn) | 
 | 118 | 				break;	/* found a free slot */ | 
 | 119 | 		} | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 120 | adjust_limit_pfn: | 
 | 121 | 		limit_pfn = curr_iova->pfn_lo - 1; | 
 | 122 | move_left: | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 123 | 		prev = curr; | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 124 | 		curr = rb_prev(curr); | 
 | 125 | 	} | 
 | 126 |  | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 127 | 	if (!curr) { | 
 | 128 | 		if (size_aligned) | 
 | 129 | 			pad_size = iova_get_pad_size(size, limit_pfn); | 
 | 130 | 		if ((IOVA_START_PFN + size + pad_size) > limit_pfn) { | 
 | 131 | 			spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
 | 132 | 			return -ENOMEM; | 
 | 133 | 		} | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 134 | 	} | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 135 |  | 
 | 136 | 	/* pfn_lo will point to size aligned address if size_aligned is set */ | 
 | 137 | 	new->pfn_lo = limit_pfn - (size + pad_size) + 1; | 
 | 138 | 	new->pfn_hi = new->pfn_lo + size - 1; | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 139 |  | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 140 | 	/* Insert the new_iova into domain rbtree by holding writer lock */ | 
 | 141 | 	/* Add new node and rebalance tree. */ | 
 | 142 | 	{ | 
| David Woodhouse | a15a519 | 2009-07-01 18:49:06 +0100 | [diff] [blame] | 143 | 		struct rb_node **entry, *parent = NULL; | 
 | 144 |  | 
 | 145 | 		/* If we have 'prev', it's a valid place to start the | 
 | 146 | 		   insertion. Otherwise, start from the root. */ | 
 | 147 | 		if (prev) | 
 | 148 | 			entry = &prev; | 
 | 149 | 		else | 
 | 150 | 			entry = &iovad->rbroot.rb_node; | 
 | 151 |  | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 152 | 		/* Figure out where to put new node */ | 
 | 153 | 		while (*entry) { | 
 | 154 | 			struct iova *this = container_of(*entry, | 
 | 155 | 							struct iova, node); | 
 | 156 | 			parent = *entry; | 
 | 157 |  | 
 | 158 | 			if (new->pfn_lo < this->pfn_lo) | 
 | 159 | 				entry = &((*entry)->rb_left); | 
 | 160 | 			else if (new->pfn_lo > this->pfn_lo) | 
 | 161 | 				entry = &((*entry)->rb_right); | 
 | 162 | 			else | 
 | 163 | 				BUG(); /* this should not happen */ | 
 | 164 | 		} | 
 | 165 |  | 
 | 166 | 		/* Add new node and rebalance tree. */ | 
 | 167 | 		rb_link_node(&new->node, parent, entry); | 
 | 168 | 		rb_insert_color(&new->node, &iovad->rbroot); | 
 | 169 | 	} | 
 | 170 | 	__cached_rbnode_insert_update(iovad, saved_pfn, new); | 
 | 171 |  | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 172 | 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 173 |  | 
 | 174 |  | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 175 | 	return 0; | 
 | 176 | } | 
 | 177 |  | 
 | 178 | static void | 
 | 179 | iova_insert_rbtree(struct rb_root *root, struct iova *iova) | 
 | 180 | { | 
 | 181 | 	struct rb_node **new = &(root->rb_node), *parent = NULL; | 
 | 182 | 	/* Figure out where to put new node */ | 
 | 183 | 	while (*new) { | 
 | 184 | 		struct iova *this = container_of(*new, struct iova, node); | 
 | 185 | 		parent = *new; | 
 | 186 |  | 
 | 187 | 		if (iova->pfn_lo < this->pfn_lo) | 
 | 188 | 			new = &((*new)->rb_left); | 
 | 189 | 		else if (iova->pfn_lo > this->pfn_lo) | 
 | 190 | 			new = &((*new)->rb_right); | 
 | 191 | 		else | 
 | 192 | 			BUG(); /* this should not happen */ | 
 | 193 | 	} | 
 | 194 | 	/* Add new node and rebalance tree. */ | 
 | 195 | 	rb_link_node(&iova->node, parent, new); | 
 | 196 | 	rb_insert_color(&iova->node, root); | 
 | 197 | } | 
 | 198 |  | 
 | 199 | /** | 
 | 200 |  * alloc_iova - allocates an iova | 
 | 201 |  * @iovad - iova domain in question | 
 | 202 |  * @size - size of page frames to allocate | 
 | 203 |  * @limit_pfn - max limit address | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 204 |  * @size_aligned - set if size_aligned address range is required | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 205 |  * This function allocates an iova in the range limit_pfn to IOVA_START_PFN | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 206 |  * looking from limit_pfn instead from IOVA_START_PFN. If the size_aligned | 
 | 207 |  * flag is set then the allocated address iova->pfn_lo will be naturally | 
 | 208 |  * aligned on roundup_power_of_two(size). | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 209 |  */ | 
 | 210 | struct iova * | 
 | 211 | alloc_iova(struct iova_domain *iovad, unsigned long size, | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 212 | 	unsigned long limit_pfn, | 
 | 213 | 	bool size_aligned) | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 214 | { | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 215 | 	struct iova *new_iova; | 
 | 216 | 	int ret; | 
 | 217 |  | 
 | 218 | 	new_iova = alloc_iova_mem(); | 
 | 219 | 	if (!new_iova) | 
 | 220 | 		return NULL; | 
 | 221 |  | 
| Keshavamurthy, Anil S | f76aec7 | 2007-10-21 16:41:58 -0700 | [diff] [blame] | 222 | 	/* If size aligned is set then round the size to | 
 | 223 | 	 * to next power of two. | 
 | 224 | 	 */ | 
 | 225 | 	if (size_aligned) | 
 | 226 | 		size = __roundup_pow_of_two(size); | 
 | 227 |  | 
| mark gross | ddf0288 | 2008-03-04 15:22:04 -0800 | [diff] [blame] | 228 | 	ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn, | 
 | 229 | 			new_iova, size_aligned); | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 230 |  | 
 | 231 | 	if (ret) { | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 232 | 		free_iova_mem(new_iova); | 
 | 233 | 		return NULL; | 
 | 234 | 	} | 
 | 235 |  | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 236 | 	return new_iova; | 
 | 237 | } | 
 | 238 |  | 
 | 239 | /** | 
 | 240 |  * find_iova - find's an iova for a given pfn | 
 | 241 |  * @iovad - iova domain in question. | 
 | 242 |  * pfn - page frame number | 
 | 243 |  * This function finds and returns an iova belonging to the | 
 | 244 |  * given doamin which matches the given pfn. | 
 | 245 |  */ | 
 | 246 | struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn) | 
 | 247 | { | 
 | 248 | 	unsigned long flags; | 
 | 249 | 	struct rb_node *node; | 
 | 250 |  | 
 | 251 | 	/* Take the lock so that no other thread is manipulating the rbtree */ | 
 | 252 | 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | 
 | 253 | 	node = iovad->rbroot.rb_node; | 
 | 254 | 	while (node) { | 
 | 255 | 		struct iova *iova = container_of(node, struct iova, node); | 
 | 256 |  | 
 | 257 | 		/* If pfn falls within iova's range, return iova */ | 
 | 258 | 		if ((pfn >= iova->pfn_lo) && (pfn <= iova->pfn_hi)) { | 
 | 259 | 			spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
 | 260 | 			/* We are not holding the lock while this iova | 
 | 261 | 			 * is referenced by the caller as the same thread | 
 | 262 | 			 * which called this function also calls __free_iova() | 
 | 263 | 			 * and it is by desing that only one thread can possibly | 
 | 264 | 			 * reference a particular iova and hence no conflict. | 
 | 265 | 			 */ | 
 | 266 | 			return iova; | 
 | 267 | 		} | 
 | 268 |  | 
 | 269 | 		if (pfn < iova->pfn_lo) | 
 | 270 | 			node = node->rb_left; | 
 | 271 | 		else if (pfn > iova->pfn_lo) | 
 | 272 | 			node = node->rb_right; | 
 | 273 | 	} | 
 | 274 |  | 
 | 275 | 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
 | 276 | 	return NULL; | 
 | 277 | } | 
 | 278 |  | 
 | 279 | /** | 
 | 280 |  * __free_iova - frees the given iova | 
 | 281 |  * @iovad: iova domain in question. | 
 | 282 |  * @iova: iova in question. | 
 | 283 |  * Frees the given iova belonging to the giving domain | 
 | 284 |  */ | 
 | 285 | void | 
 | 286 | __free_iova(struct iova_domain *iovad, struct iova *iova) | 
 | 287 | { | 
 | 288 | 	unsigned long flags; | 
 | 289 |  | 
 | 290 | 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | 
 | 291 | 	__cached_rbnode_delete_update(iovad, iova); | 
 | 292 | 	rb_erase(&iova->node, &iovad->rbroot); | 
 | 293 | 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
 | 294 | 	free_iova_mem(iova); | 
 | 295 | } | 
 | 296 |  | 
 | 297 | /** | 
 | 298 |  * free_iova - finds and frees the iova for a given pfn | 
 | 299 |  * @iovad: - iova domain in question. | 
 | 300 |  * @pfn: - pfn that is allocated previously | 
 | 301 |  * This functions finds an iova for a given pfn and then | 
 | 302 |  * frees the iova from that domain. | 
 | 303 |  */ | 
 | 304 | void | 
 | 305 | free_iova(struct iova_domain *iovad, unsigned long pfn) | 
 | 306 | { | 
 | 307 | 	struct iova *iova = find_iova(iovad, pfn); | 
 | 308 | 	if (iova) | 
 | 309 | 		__free_iova(iovad, iova); | 
 | 310 |  | 
 | 311 | } | 
 | 312 |  | 
 | 313 | /** | 
 | 314 |  * put_iova_domain - destroys the iova doamin | 
 | 315 |  * @iovad: - iova domain in question. | 
 | 316 |  * All the iova's in that domain are destroyed. | 
 | 317 |  */ | 
 | 318 | void put_iova_domain(struct iova_domain *iovad) | 
 | 319 | { | 
 | 320 | 	struct rb_node *node; | 
 | 321 | 	unsigned long flags; | 
 | 322 |  | 
 | 323 | 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | 
 | 324 | 	node = rb_first(&iovad->rbroot); | 
 | 325 | 	while (node) { | 
 | 326 | 		struct iova *iova = container_of(node, struct iova, node); | 
 | 327 | 		rb_erase(node, &iovad->rbroot); | 
 | 328 | 		free_iova_mem(iova); | 
 | 329 | 		node = rb_first(&iovad->rbroot); | 
 | 330 | 	} | 
 | 331 | 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
 | 332 | } | 
 | 333 |  | 
 | 334 | static int | 
 | 335 | __is_range_overlap(struct rb_node *node, | 
 | 336 | 	unsigned long pfn_lo, unsigned long pfn_hi) | 
 | 337 | { | 
 | 338 | 	struct iova *iova = container_of(node, struct iova, node); | 
 | 339 |  | 
 | 340 | 	if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo)) | 
 | 341 | 		return 1; | 
 | 342 | 	return 0; | 
 | 343 | } | 
 | 344 |  | 
 | 345 | static struct iova * | 
 | 346 | __insert_new_range(struct iova_domain *iovad, | 
 | 347 | 	unsigned long pfn_lo, unsigned long pfn_hi) | 
 | 348 | { | 
 | 349 | 	struct iova *iova; | 
 | 350 |  | 
 | 351 | 	iova = alloc_iova_mem(); | 
 | 352 | 	if (!iova) | 
 | 353 | 		return iova; | 
 | 354 |  | 
 | 355 | 	iova->pfn_hi = pfn_hi; | 
 | 356 | 	iova->pfn_lo = pfn_lo; | 
 | 357 | 	iova_insert_rbtree(&iovad->rbroot, iova); | 
 | 358 | 	return iova; | 
 | 359 | } | 
 | 360 |  | 
 | 361 | static void | 
 | 362 | __adjust_overlap_range(struct iova *iova, | 
 | 363 | 	unsigned long *pfn_lo, unsigned long *pfn_hi) | 
 | 364 | { | 
 | 365 | 	if (*pfn_lo < iova->pfn_lo) | 
 | 366 | 		iova->pfn_lo = *pfn_lo; | 
 | 367 | 	if (*pfn_hi > iova->pfn_hi) | 
 | 368 | 		*pfn_lo = iova->pfn_hi + 1; | 
 | 369 | } | 
 | 370 |  | 
 | 371 | /** | 
 | 372 |  * reserve_iova - reserves an iova in the given range | 
 | 373 |  * @iovad: - iova domain pointer | 
 | 374 |  * @pfn_lo: - lower page frame address | 
 | 375 |  * @pfn_hi:- higher pfn adderss | 
 | 376 |  * This function allocates reserves the address range from pfn_lo to pfn_hi so | 
 | 377 |  * that this address is not dished out as part of alloc_iova. | 
 | 378 |  */ | 
 | 379 | struct iova * | 
 | 380 | reserve_iova(struct iova_domain *iovad, | 
 | 381 | 	unsigned long pfn_lo, unsigned long pfn_hi) | 
 | 382 | { | 
 | 383 | 	struct rb_node *node; | 
 | 384 | 	unsigned long flags; | 
 | 385 | 	struct iova *iova; | 
 | 386 | 	unsigned int overlap = 0; | 
 | 387 |  | 
| David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 388 | 	spin_lock_irqsave(&iovad->iova_rbtree_lock, flags); | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 389 | 	for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) { | 
 | 390 | 		if (__is_range_overlap(node, pfn_lo, pfn_hi)) { | 
 | 391 | 			iova = container_of(node, struct iova, node); | 
 | 392 | 			__adjust_overlap_range(iova, &pfn_lo, &pfn_hi); | 
 | 393 | 			if ((pfn_lo >= iova->pfn_lo) && | 
 | 394 | 				(pfn_hi <= iova->pfn_hi)) | 
 | 395 | 				goto finish; | 
 | 396 | 			overlap = 1; | 
 | 397 |  | 
 | 398 | 		} else if (overlap) | 
 | 399 | 				break; | 
 | 400 | 	} | 
 | 401 |  | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 402 | 	/* We are here either because this is the first reserver node | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 403 | 	 * or need to insert remaining non overlap addr range | 
 | 404 | 	 */ | 
 | 405 | 	iova = __insert_new_range(iovad, pfn_lo, pfn_hi); | 
 | 406 | finish: | 
 | 407 |  | 
| David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 408 | 	spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags); | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 409 | 	return iova; | 
 | 410 | } | 
 | 411 |  | 
 | 412 | /** | 
 | 413 |  * copy_reserved_iova - copies the reserved between domains | 
 | 414 |  * @from: - source doamin from where to copy | 
 | 415 |  * @to: - destination domin where to copy | 
 | 416 |  * This function copies reserved iova's from one doamin to | 
 | 417 |  * other. | 
 | 418 |  */ | 
 | 419 | void | 
 | 420 | copy_reserved_iova(struct iova_domain *from, struct iova_domain *to) | 
 | 421 | { | 
 | 422 | 	unsigned long flags; | 
 | 423 | 	struct rb_node *node; | 
 | 424 |  | 
| David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 425 | 	spin_lock_irqsave(&from->iova_rbtree_lock, flags); | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 426 | 	for (node = rb_first(&from->rbroot); node; node = rb_next(node)) { | 
 | 427 | 		struct iova *iova = container_of(node, struct iova, node); | 
 | 428 | 		struct iova *new_iova; | 
 | 429 | 		new_iova = reserve_iova(to, iova->pfn_lo, iova->pfn_hi); | 
 | 430 | 		if (!new_iova) | 
 | 431 | 			printk(KERN_ERR "Reserve iova range %lx@%lx failed\n", | 
 | 432 | 				iova->pfn_lo, iova->pfn_lo); | 
 | 433 | 	} | 
| David Woodhouse | 3d39cec | 2009-07-08 15:23:30 +0100 | [diff] [blame] | 434 | 	spin_unlock_irqrestore(&from->iova_rbtree_lock, flags); | 
| Keshavamurthy, Anil S | f8de50e | 2007-10-21 16:41:48 -0700 | [diff] [blame] | 435 | } |