2 * Copyright (c) 2006, Intel Corporation.
4 * This file is released under the GPLv2.
6 <<<<<<< HEAD:drivers/pci/iova.c
7 * Copyright (C) 2006 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * Copyright (C) 2006-2008 Intel Corporation
10 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
11 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/pci/iova.c
17 init_iova_domain(struct iova_domain
*iovad
, unsigned long pfn_32bit
)
19 spin_lock_init(&iovad
->iova_alloc_lock
);
20 spin_lock_init(&iovad
->iova_rbtree_lock
);
21 iovad
->rbroot
= RB_ROOT
;
22 iovad
->cached32_node
= NULL
;
23 iovad
->dma_32bit_pfn
= pfn_32bit
;
26 static struct rb_node
*
27 __get_cached_rbnode(struct iova_domain
*iovad
, unsigned long *limit_pfn
)
29 if ((*limit_pfn
!= iovad
->dma_32bit_pfn
) ||
30 (iovad
->cached32_node
== NULL
))
31 return rb_last(&iovad
->rbroot
);
33 struct rb_node
*prev_node
= rb_prev(iovad
->cached32_node
);
34 struct iova
*curr_iova
=
35 container_of(iovad
->cached32_node
, struct iova
, node
);
36 *limit_pfn
= curr_iova
->pfn_lo
- 1;
42 __cached_rbnode_insert_update(struct iova_domain
*iovad
,
43 unsigned long limit_pfn
, struct iova
*new)
45 if (limit_pfn
!= iovad
->dma_32bit_pfn
)
47 iovad
->cached32_node
= &new->node
;
51 __cached_rbnode_delete_update(struct iova_domain
*iovad
, struct iova
*free
)
53 struct iova
*cached_iova
;
56 if (!iovad
->cached32_node
)
58 curr
= iovad
->cached32_node
;
59 cached_iova
= container_of(curr
, struct iova
, node
);
61 if (free
->pfn_lo
>= cached_iova
->pfn_lo
)
62 iovad
->cached32_node
= rb_next(&free
->node
);
65 /* Computes the padding size required, to make the
66 * the start address naturally aligned on its size
69 iova_get_pad_size(int size
, unsigned int limit_pfn
)
71 unsigned int pad_size
= 0;
72 unsigned int order
= ilog2(size
);
75 pad_size
= (limit_pfn
+ 1) % (1 << order
);
80 static int __alloc_iova_range(struct iova_domain
*iovad
, unsigned long size
,
81 unsigned long limit_pfn
, struct iova
*new, bool size_aligned
)
83 struct rb_node
*curr
= NULL
;
85 unsigned long saved_pfn
;
86 unsigned int pad_size
= 0;
88 /* Walk the tree backwards */
89 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
90 saved_pfn
= limit_pfn
;
91 curr
= __get_cached_rbnode(iovad
, &limit_pfn
);
93 struct iova
*curr_iova
= container_of(curr
, struct iova
, node
);
94 if (limit_pfn
< curr_iova
->pfn_lo
)
96 else if (limit_pfn
< curr_iova
->pfn_hi
)
97 goto adjust_limit_pfn
;
100 pad_size
= iova_get_pad_size(size
, limit_pfn
);
101 if ((curr_iova
->pfn_hi
+ size
+ pad_size
) <= limit_pfn
)
102 break; /* found a free slot */
105 limit_pfn
= curr_iova
->pfn_lo
- 1;
107 curr
= rb_prev(curr
);
112 pad_size
= iova_get_pad_size(size
, limit_pfn
);
113 if ((IOVA_START_PFN
+ size
+ pad_size
) > limit_pfn
) {
114 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
119 /* pfn_lo will point to size aligned address if size_aligned is set */
120 new->pfn_lo
= limit_pfn
- (size
+ pad_size
) + 1;
121 new->pfn_hi
= new->pfn_lo
+ size
- 1;
123 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
128 iova_insert_rbtree(struct rb_root
*root
, struct iova
*iova
)
130 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
131 /* Figure out where to put new node */
133 struct iova
*this = container_of(*new, struct iova
, node
);
136 if (iova
->pfn_lo
< this->pfn_lo
)
137 new = &((*new)->rb_left
);
138 else if (iova
->pfn_lo
> this->pfn_lo
)
139 new = &((*new)->rb_right
);
141 BUG(); /* this should not happen */
143 /* Add new node and rebalance tree. */
144 rb_link_node(&iova
->node
, parent
, new);
145 rb_insert_color(&iova
->node
, root
);
149 * alloc_iova - allocates an iova
150 * @iovad - iova domain in question
151 * @size - size of page frames to allocate
152 * @limit_pfn - max limit address
153 * @size_aligned - set if size_aligned address range is required
154 * This function allocates an iova in the range limit_pfn to IOVA_START_PFN
155 * looking from limit_pfn instead from IOVA_START_PFN. If the size_aligned
156 * flag is set then the allocated address iova->pfn_lo will be naturally
157 * aligned on roundup_power_of_two(size).
160 alloc_iova(struct iova_domain
*iovad
, unsigned long size
,
161 unsigned long limit_pfn
,
165 struct iova
*new_iova
;
168 new_iova
= alloc_iova_mem();
172 /* If size aligned is set then round the size to
173 * to next power of two.
176 size
= __roundup_pow_of_two(size
);
178 spin_lock_irqsave(&iovad
->iova_alloc_lock
, flags
);
179 ret
= __alloc_iova_range(iovad
, size
, limit_pfn
, new_iova
,
183 spin_unlock_irqrestore(&iovad
->iova_alloc_lock
, flags
);
184 free_iova_mem(new_iova
);
188 /* Insert the new_iova into domain rbtree by holding writer lock */
189 spin_lock(&iovad
->iova_rbtree_lock
);
190 iova_insert_rbtree(&iovad
->rbroot
, new_iova
);
191 __cached_rbnode_insert_update(iovad
, limit_pfn
, new_iova
);
192 spin_unlock(&iovad
->iova_rbtree_lock
);
194 spin_unlock_irqrestore(&iovad
->iova_alloc_lock
, flags
);
200 * find_iova - find's an iova for a given pfn
201 * @iovad - iova domain in question.
202 * pfn - page frame number
203 * This function finds and returns an iova belonging to the
204 * given doamin which matches the given pfn.
206 struct iova
*find_iova(struct iova_domain
*iovad
, unsigned long pfn
)
209 struct rb_node
*node
;
211 /* Take the lock so that no other thread is manipulating the rbtree */
212 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
213 node
= iovad
->rbroot
.rb_node
;
215 struct iova
*iova
= container_of(node
, struct iova
, node
);
217 /* If pfn falls within iova's range, return iova */
218 if ((pfn
>= iova
->pfn_lo
) && (pfn
<= iova
->pfn_hi
)) {
219 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
220 /* We are not holding the lock while this iova
221 * is referenced by the caller as the same thread
222 * which called this function also calls __free_iova()
223 * and it is by desing that only one thread can possibly
224 * reference a particular iova and hence no conflict.
229 if (pfn
< iova
->pfn_lo
)
230 node
= node
->rb_left
;
231 else if (pfn
> iova
->pfn_lo
)
232 node
= node
->rb_right
;
235 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
240 * __free_iova - frees the given iova
241 * @iovad: iova domain in question.
242 * @iova: iova in question.
243 * Frees the given iova belonging to the giving domain
246 __free_iova(struct iova_domain
*iovad
, struct iova
*iova
)
250 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
251 __cached_rbnode_delete_update(iovad
, iova
);
252 rb_erase(&iova
->node
, &iovad
->rbroot
);
253 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
258 * free_iova - finds and frees the iova for a given pfn
259 * @iovad: - iova domain in question.
260 * @pfn: - pfn that is allocated previously
261 * This functions finds an iova for a given pfn and then
262 * frees the iova from that domain.
265 free_iova(struct iova_domain
*iovad
, unsigned long pfn
)
267 struct iova
*iova
= find_iova(iovad
, pfn
);
269 __free_iova(iovad
, iova
);
274 * put_iova_domain - destroys the iova doamin
275 * @iovad: - iova domain in question.
276 * All the iova's in that domain are destroyed.
278 void put_iova_domain(struct iova_domain
*iovad
)
280 struct rb_node
*node
;
283 spin_lock_irqsave(&iovad
->iova_rbtree_lock
, flags
);
284 node
= rb_first(&iovad
->rbroot
);
286 struct iova
*iova
= container_of(node
, struct iova
, node
);
287 rb_erase(node
, &iovad
->rbroot
);
289 node
= rb_first(&iovad
->rbroot
);
291 spin_unlock_irqrestore(&iovad
->iova_rbtree_lock
, flags
);
295 __is_range_overlap(struct rb_node
*node
,
296 unsigned long pfn_lo
, unsigned long pfn_hi
)
298 struct iova
*iova
= container_of(node
, struct iova
, node
);
300 if ((pfn_lo
<= iova
->pfn_hi
) && (pfn_hi
>= iova
->pfn_lo
))
306 __insert_new_range(struct iova_domain
*iovad
,
307 unsigned long pfn_lo
, unsigned long pfn_hi
)
311 iova
= alloc_iova_mem();
315 iova
->pfn_hi
= pfn_hi
;
316 iova
->pfn_lo
= pfn_lo
;
317 iova_insert_rbtree(&iovad
->rbroot
, iova
);
322 __adjust_overlap_range(struct iova
*iova
,
323 unsigned long *pfn_lo
, unsigned long *pfn_hi
)
325 if (*pfn_lo
< iova
->pfn_lo
)
326 iova
->pfn_lo
= *pfn_lo
;
327 if (*pfn_hi
> iova
->pfn_hi
)
328 *pfn_lo
= iova
->pfn_hi
+ 1;
332 * reserve_iova - reserves an iova in the given range
333 * @iovad: - iova domain pointer
334 * @pfn_lo: - lower page frame address
335 * @pfn_hi:- higher pfn adderss
336 * This function allocates reserves the address range from pfn_lo to pfn_hi so
337 * that this address is not dished out as part of alloc_iova.
340 reserve_iova(struct iova_domain
*iovad
,
341 unsigned long pfn_lo
, unsigned long pfn_hi
)
343 struct rb_node
*node
;
346 unsigned int overlap
= 0;
348 spin_lock_irqsave(&iovad
->iova_alloc_lock
, flags
);
349 spin_lock(&iovad
->iova_rbtree_lock
);
350 for (node
= rb_first(&iovad
->rbroot
); node
; node
= rb_next(node
)) {
351 if (__is_range_overlap(node
, pfn_lo
, pfn_hi
)) {
352 iova
= container_of(node
, struct iova
, node
);
353 __adjust_overlap_range(iova
, &pfn_lo
, &pfn_hi
);
354 if ((pfn_lo
>= iova
->pfn_lo
) &&
355 (pfn_hi
<= iova
->pfn_hi
))
363 /* We are here either becasue this is the first reserver node
364 * or need to insert remaining non overlap addr range
366 iova
= __insert_new_range(iovad
, pfn_lo
, pfn_hi
);
369 spin_unlock(&iovad
->iova_rbtree_lock
);
370 spin_unlock_irqrestore(&iovad
->iova_alloc_lock
, flags
);
375 * copy_reserved_iova - copies the reserved between domains
376 * @from: - source doamin from where to copy
377 * @to: - destination domin where to copy
378 * This function copies reserved iova's from one doamin to
382 copy_reserved_iova(struct iova_domain
*from
, struct iova_domain
*to
)
385 struct rb_node
*node
;
387 spin_lock_irqsave(&from
->iova_alloc_lock
, flags
);
388 spin_lock(&from
->iova_rbtree_lock
);
389 for (node
= rb_first(&from
->rbroot
); node
; node
= rb_next(node
)) {
390 struct iova
*iova
= container_of(node
, struct iova
, node
);
391 struct iova
*new_iova
;
392 new_iova
= reserve_iova(to
, iova
->pfn_lo
, iova
->pfn_hi
);
394 printk(KERN_ERR
"Reserve iova range %lx@%lx failed\n",
395 iova
->pfn_lo
, iova
->pfn_lo
);
397 spin_unlock(&from
->iova_rbtree_lock
);
398 spin_unlock_irqrestore(&from
->iova_alloc_lock
, flags
);