xref: /dragonfly/sys/kern/subr_rman.c (revision 1847e88f)
1 /*
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/kern/subr_rman.c,v 1.10.2.1 2001/06/05 08:06:08 imp Exp $
30  * $DragonFly: src/sys/kern/subr_rman.c,v 1.7 2005/06/06 15:02:28 dillon Exp $
31  */
32 
33 /*
34  * The kernel resource manager.  This code is responsible for keeping track
35  * of hardware resources which are apportioned out to various drivers.
36  * It does not actually assign those resources, and it is not expected
37  * that end-device drivers will call into this code directly.  Rather,
38  * the code which implements the buses that those devices are attached to,
39  * and the code which manages CPU resources, will call this code, and the
40  * end-device drivers will make upcalls to that code to actually perform
41  * the allocation.
42  *
43  * There are two sorts of resources managed by this code.  The first is
44  * the more familiar array (RMAN_ARRAY) type; resources in this class
45  * consist of a sequence of individually-allocatable objects which have
46  * been numbered in some well-defined order.  Most of the resources
47  * are of this type, as it is the most familiar.  The second type is
48  * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
49  * resources in which each instance is indistinguishable from every
50  * other instance).  The principal anticipated application of gauges
51  * is in the context of power consumption, where a bus may have a specific
52  * power budget which all attached devices share.  RMAN_GAUGE is not
53  * implemented yet.
54  *
55  * For array resources, we make one simplifying assumption: two clients
56  * sharing the same resource must use the same range of indices.  That
57  * is to say, sharing of overlapping-but-not-identical regions is not
58  * permitted.
59  */
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/lock.h>
65 #include <sys/malloc.h>
66 #include <sys/bus.h>		/* XXX debugging */
67 #include <machine/bus.h>
68 #include <sys/rman.h>
69 
70 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
71 
72 struct	rman_head rman_head;
73 static	struct lwkt_token rman_tok; /* mutex to protect rman_head */
74 static	int int_rman_activate_resource(struct rman *rm, struct resource *r,
75 				       struct resource **whohas);
76 static	int int_rman_deactivate_resource(struct resource *r);
77 static	int int_rman_release_resource(struct rman *rm, struct resource *r);
78 
79 #define	CIRCLEQ_TERMCOND(var, head)	(var == (void *)&(head))
80 
81 int
82 rman_init(struct rman *rm)
83 {
84 	static int once;
85 	lwkt_tokref ilock;
86 
87 	if (once == 0) {
88 		once = 1;
89 		TAILQ_INIT(&rman_head);
90 		lwkt_token_init(&rman_tok);
91 	}
92 
93 	if (rm->rm_type == RMAN_UNINIT)
94 		panic("rman_init");
95 	if (rm->rm_type == RMAN_GAUGE)
96 		panic("implement RMAN_GAUGE");
97 
98 	CIRCLEQ_INIT(&rm->rm_list);
99 	rm->rm_slock = malloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT);
100 	if (rm->rm_slock == NULL)
101 		return ENOMEM;
102 	lwkt_token_init(rm->rm_slock);
103 
104 	lwkt_gettoken(&ilock, &rman_tok);
105 	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
106 	lwkt_reltoken(&ilock);
107 	return 0;
108 }
109 
110 /*
111  * NB: this interface is not robust against programming errors which
112  * add multiple copies of the same region.
113  */
114 int
115 rman_manage_region(struct rman *rm, u_long start, u_long end)
116 {
117 	struct resource *r, *s;
118 	lwkt_tokref ilock;
119 
120 	r = malloc(sizeof *r, M_RMAN, M_NOWAIT);
121 	if (r == 0)
122 		return ENOMEM;
123 	bzero(r, sizeof *r);
124 	r->r_sharehead = 0;
125 	r->r_start = start;
126 	r->r_end = end;
127 	r->r_flags = 0;
128 	r->r_dev = 0;
129 	r->r_rm = rm;
130 
131 	lwkt_gettoken(&ilock, rm->rm_slock);
132 	for (s = CIRCLEQ_FIRST(&rm->rm_list);
133 	     !CIRCLEQ_TERMCOND(s, rm->rm_list) && s->r_end < r->r_start;
134 	     s = CIRCLEQ_NEXT(s, r_link))
135 		;
136 
137 	if (CIRCLEQ_TERMCOND(s, rm->rm_list)) {
138 		CIRCLEQ_INSERT_TAIL(&rm->rm_list, r, r_link);
139 	} else {
140 		CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, r, r_link);
141 	}
142 
143 	lwkt_reltoken(&ilock);
144 	return 0;
145 }
146 
147 int
148 rman_fini(struct rman *rm)
149 {
150 	struct resource *r;
151 	lwkt_tokref ilock;
152 
153 	lwkt_gettoken(&ilock, rm->rm_slock);
154 	CIRCLEQ_FOREACH(r, &rm->rm_list, r_link) {
155 		if (r->r_flags & RF_ALLOCATED) {
156 			lwkt_reltoken(&ilock);
157 			return EBUSY;
158 		}
159 	}
160 
161 	/*
162 	 * There really should only be one of these if we are in this
163 	 * state and the code is working properly, but it can't hurt.
164 	 */
165 	while (!CIRCLEQ_EMPTY(&rm->rm_list)) {
166 		r = CIRCLEQ_FIRST(&rm->rm_list);
167 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
168 		free(r, M_RMAN);
169 	}
170 	lwkt_reltoken(&ilock);
171 	/* XXX what's the point of this if we are going to free the struct? */
172 	lwkt_gettoken(&ilock, &rman_tok);
173 	TAILQ_REMOVE(&rman_head, rm, rm_link);
174 	lwkt_reltoken(&ilock);
175 	free(rm->rm_slock, M_RMAN);
176 
177 	return 0;
178 }
179 
180 struct resource *
181 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
182 		      u_int flags, struct device *dev)
183 {
184 	u_int	want_activate;
185 	struct	resource *r, *s, *rv;
186 	u_long	rstart, rend;
187 	lwkt_tokref ilock;
188 
189 	rv = 0;
190 
191 #ifdef RMAN_DEBUG
192 	printf("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
193 	       "%#lx, flags %u, device %s%d\n", rm->rm_descr, start, end,
194 	       count, flags, device_get_name(dev), device_get_unit(dev));
195 #endif /* RMAN_DEBUG */
196 	want_activate = (flags & RF_ACTIVE);
197 	flags &= ~RF_ACTIVE;
198 
199 	lwkt_gettoken(&ilock, rm->rm_slock);
200 
201 	for (r = CIRCLEQ_FIRST(&rm->rm_list);
202 	     !CIRCLEQ_TERMCOND(r, rm->rm_list) && r->r_end < start;
203 	     r = CIRCLEQ_NEXT(r, r_link))
204 		;
205 
206 	if (CIRCLEQ_TERMCOND(r, rm->rm_list)) {
207 #ifdef RMAN_DEBUG
208 		printf("could not find a region\n");
209 #endif
210 		goto out;
211 	}
212 
213 	/*
214 	 * First try to find an acceptable totally-unshared region.
215 	 */
216 	for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
217 	     s = CIRCLEQ_NEXT(s, r_link)) {
218 #ifdef RMAN_DEBUG
219 		printf("considering [%#lx, %#lx]\n", s->r_start, s->r_end);
220 #endif /* RMAN_DEBUG */
221 		if (s->r_start > end) {
222 #ifdef RMAN_DEBUG
223 			printf("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end);
224 #endif /* RMAN_DEBUG */
225 			break;
226 		}
227 		if (s->r_flags & RF_ALLOCATED) {
228 #ifdef RMAN_DEBUG
229 			printf("region is allocated\n");
230 #endif /* RMAN_DEBUG */
231 			continue;
232 		}
233 		rstart = max(s->r_start, start);
234 		rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
235 		    ~((1ul << RF_ALIGNMENT(flags)) - 1);
236 		rend = min(s->r_end, max(start + count, end));
237 #ifdef RMAN_DEBUG
238 		printf("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
239 		       rstart, rend, (rend - rstart + 1), count);
240 #endif /* RMAN_DEBUG */
241 
242 		if ((rend - rstart + 1) >= count) {
243 #ifdef RMAN_DEBUG
244 			printf("candidate region: [%#lx, %#lx], size %#lx\n",
245 			       rend, rstart, (rend - rstart + 1));
246 #endif /* RMAN_DEBUG */
247 			if ((s->r_end - s->r_start + 1) == count) {
248 #ifdef RMAN_DEBUG
249 				printf("candidate region is entire chunk\n");
250 #endif /* RMAN_DEBUG */
251 				rv = s;
252 				rv->r_flags |= RF_ALLOCATED | flags;
253 				rv->r_dev = dev;
254 				goto out;
255 			}
256 
257 			/*
258 			 * If s->r_start < rstart and
259 			 *    s->r_end > rstart + count - 1, then
260 			 * we need to split the region into three pieces
261 			 * (the middle one will get returned to the user).
262 			 * Otherwise, we are allocating at either the
263 			 * beginning or the end of s, so we only need to
264 			 * split it in two.  The first case requires
265 			 * two new allocations; the second requires but one.
266 			 */
267 			rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT);
268 			if (rv == 0)
269 				goto out;
270 			bzero(rv, sizeof *rv);
271 			rv->r_start = rstart;
272 			rv->r_end = rstart + count - 1;
273 			rv->r_flags = flags | RF_ALLOCATED;
274 			rv->r_dev = dev;
275 			rv->r_sharehead = 0;
276 			rv->r_rm = rm;
277 
278 			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
279 #ifdef RMAN_DEBUG
280 				printf("splitting region in three parts: "
281 				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
282 				       s->r_start, rv->r_start - 1,
283 				       rv->r_start, rv->r_end,
284 				       rv->r_end + 1, s->r_end);
285 #endif /* RMAN_DEBUG */
286 				/*
287 				 * We are allocating in the middle.
288 				 */
289 				r = malloc(sizeof *r, M_RMAN, M_NOWAIT);
290 				if (r == 0) {
291 					free(rv, M_RMAN);
292 					rv = 0;
293 					goto out;
294 				}
295 				bzero(r, sizeof *r);
296 				r->r_start = rv->r_end + 1;
297 				r->r_end = s->r_end;
298 				r->r_flags = s->r_flags;
299 				r->r_dev = 0;
300 				r->r_sharehead = 0;
301 				r->r_rm = rm;
302 				s->r_end = rv->r_start - 1;
303 				CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
304 						     r_link);
305 				CIRCLEQ_INSERT_AFTER(&rm->rm_list, rv, r,
306 						     r_link);
307 			} else if (s->r_start == rv->r_start) {
308 #ifdef RMAN_DEBUG
309 				printf("allocating from the beginning\n");
310 #endif /* RMAN_DEBUG */
311 				/*
312 				 * We are allocating at the beginning.
313 				 */
314 				s->r_start = rv->r_end + 1;
315 				CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, rv,
316 						      r_link);
317 			} else {
318 #ifdef RMAN_DEBUG
319 				printf("allocating at the end\n");
320 #endif /* RMAN_DEBUG */
321 				/*
322 				 * We are allocating at the end.
323 				 */
324 				s->r_end = rv->r_start - 1;
325 				CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
326 						     r_link);
327 			}
328 			goto out;
329 		}
330 	}
331 
332 	/*
333 	 * Now find an acceptable shared region, if the client's requirements
334 	 * allow sharing.  By our implementation restriction, a candidate
335 	 * region must match exactly by both size and sharing type in order
336 	 * to be considered compatible with the client's request.  (The
337 	 * former restriction could probably be lifted without too much
338 	 * additional work, but this does not seem warranted.)
339 	 */
340 #ifdef RMAN_DEBUG
341 	printf("no unshared regions found\n");
342 #endif /* RMAN_DEBUG */
343 	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
344 		goto out;
345 
346 	for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
347 	     s = CIRCLEQ_NEXT(s, r_link)) {
348 		if (s->r_start > end)
349 			break;
350 		if ((s->r_flags & flags) != flags)
351 			continue;
352 		rstart = max(s->r_start, start);
353 		rend = min(s->r_end, max(start + count, end));
354 		if (s->r_start >= start && s->r_end <= end
355 		    && (s->r_end - s->r_start + 1) == count) {
356 			rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT);
357 			if (rv == 0)
358 				goto out;
359 			bzero(rv, sizeof *rv);
360 			rv->r_start = s->r_start;
361 			rv->r_end = s->r_end;
362 			rv->r_flags = s->r_flags &
363 				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
364 			rv->r_dev = dev;
365 			rv->r_rm = rm;
366 			if (s->r_sharehead == 0) {
367 				s->r_sharehead = malloc(sizeof *s->r_sharehead,
368 							M_RMAN, M_NOWAIT);
369 				if (s->r_sharehead == 0) {
370 					free(rv, M_RMAN);
371 					rv = 0;
372 					goto out;
373 				}
374 				bzero(s->r_sharehead, sizeof *s->r_sharehead);
375 				LIST_INIT(s->r_sharehead);
376 				LIST_INSERT_HEAD(s->r_sharehead, s,
377 						 r_sharelink);
378 				s->r_flags |= RF_FIRSTSHARE;
379 			}
380 			rv->r_sharehead = s->r_sharehead;
381 			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
382 			goto out;
383 		}
384 	}
385 
386 	/*
387 	 * We couldn't find anything.
388 	 */
389 out:
390 	/*
391 	 * If the user specified RF_ACTIVE in the initial flags,
392 	 * which is reflected in `want_activate', we attempt to atomically
393 	 * activate the resource.  If this fails, we release the resource
394 	 * and indicate overall failure.  (This behavior probably doesn't
395 	 * make sense for RF_TIMESHARE-type resources.)
396 	 */
397 	if (rv && want_activate) {
398 		struct resource *whohas;
399 		if (int_rman_activate_resource(rm, rv, &whohas)) {
400 			int_rman_release_resource(rm, rv);
401 			rv = 0;
402 		}
403 	}
404 	lwkt_reltoken(&ilock);
405 	return (rv);
406 }
407 
408 static int
409 int_rman_activate_resource(struct rman *rm, struct resource *r,
410 			   struct resource **whohas)
411 {
412 	struct resource *s;
413 	int ok;
414 
415 	/*
416 	 * If we are not timesharing, then there is nothing much to do.
417 	 * If we already have the resource, then there is nothing at all to do.
418 	 * If we are not on a sharing list with anybody else, then there is
419 	 * little to do.
420 	 */
421 	if ((r->r_flags & RF_TIMESHARE) == 0
422 	    || (r->r_flags & RF_ACTIVE) != 0
423 	    || r->r_sharehead == 0) {
424 		r->r_flags |= RF_ACTIVE;
425 		return 0;
426 	}
427 
428 	ok = 1;
429 	for (s = LIST_FIRST(r->r_sharehead); s && ok;
430 	     s = LIST_NEXT(s, r_sharelink)) {
431 		if ((s->r_flags & RF_ACTIVE) != 0) {
432 			ok = 0;
433 			*whohas = s;
434 		}
435 	}
436 	if (ok) {
437 		r->r_flags |= RF_ACTIVE;
438 		return 0;
439 	}
440 	return EBUSY;
441 }
442 
443 int
444 rman_activate_resource(struct resource *r)
445 {
446 	int rv;
447 	struct resource *whohas;
448 	lwkt_tokref ilock;
449 	struct rman *rm;
450 
451 	rm = r->r_rm;
452 	lwkt_gettoken(&ilock, rm->rm_slock);
453 	rv = int_rman_activate_resource(rm, r, &whohas);
454 	lwkt_reltoken(&ilock);
455 	return rv;
456 }
457 
458 #if 0
459 
460 /* XXX */
461 int
462 rman_await_resource(struct resource *r, lwkt_tokref_t ilock, int slpflags, int timo)
463 {
464 	int	rv;
465 	struct	resource *whohas;
466 	struct	rman *rm;
467 
468 	rm = r->r_rm;
469 	for (;;) {
470 		lwkt_gettoken(ilock, rm->rm_slock);
471 		rv = int_rman_activate_resource(rm, r, &whohas);
472 		if (rv != EBUSY)
473 			return (rv);	/* returns with ilock held */
474 
475 		if (r->r_sharehead == 0)
476 			panic("rman_await_resource");
477 		/*
478 		 * A critical section will hopefully will prevent a race
479 		 * between lwkt_reltoken and tsleep where a process
480 		 * could conceivably get in and release the resource
481 		 * before we have a chance to sleep on it. YYY
482 		 */
483 		crit_enter();
484 		whohas->r_flags |= RF_WANTED;
485 		rv = tsleep(r->r_sharehead, slpflags, "rmwait", timo);
486 		if (rv) {
487 			lwkt_reltoken(ilock);
488 			crit_exit();
489 			return rv;
490 		}
491 		crit_exit();
492 	}
493 }
494 
495 #endif
496 
497 static int
498 int_rman_deactivate_resource(struct resource *r)
499 {
500 	struct	rman *rm;
501 
502 	rm = r->r_rm;
503 	r->r_flags &= ~RF_ACTIVE;
504 	if (r->r_flags & RF_WANTED) {
505 		r->r_flags &= ~RF_WANTED;
506 		wakeup(r->r_sharehead);
507 	}
508 	return 0;
509 }
510 
511 int
512 rman_deactivate_resource(struct resource *r)
513 {
514 	lwkt_tokref ilock;
515 	struct rman *rm;
516 
517 	rm = r->r_rm;
518 	lwkt_gettoken(&ilock, rm->rm_slock);
519 	int_rman_deactivate_resource(r);
520 	lwkt_reltoken(&ilock);
521 	return 0;
522 }
523 
524 static int
525 int_rman_release_resource(struct rman *rm, struct resource *r)
526 {
527 	struct	resource *s, *t;
528 
529 	if (r->r_flags & RF_ACTIVE)
530 		int_rman_deactivate_resource(r);
531 
532 	/*
533 	 * Check for a sharing list first.  If there is one, then we don't
534 	 * have to think as hard.
535 	 */
536 	if (r->r_sharehead) {
537 		/*
538 		 * If a sharing list exists, then we know there are at
539 		 * least two sharers.
540 		 *
541 		 * If we are in the main circleq, appoint someone else.
542 		 */
543 		LIST_REMOVE(r, r_sharelink);
544 		s = LIST_FIRST(r->r_sharehead);
545 		if (r->r_flags & RF_FIRSTSHARE) {
546 			s->r_flags |= RF_FIRSTSHARE;
547 			CIRCLEQ_INSERT_BEFORE(&rm->rm_list, r, s, r_link);
548 			CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
549 		}
550 
551 		/*
552 		 * Make sure that the sharing list goes away completely
553 		 * if the resource is no longer being shared at all.
554 		 */
555 		if (LIST_NEXT(s, r_sharelink) == 0) {
556 			free(s->r_sharehead, M_RMAN);
557 			s->r_sharehead = 0;
558 			s->r_flags &= ~RF_FIRSTSHARE;
559 		}
560 		goto out;
561 	}
562 
563 	/*
564 	 * Look at the adjacent resources in the list and see if our
565 	 * segment can be merged with any of them.
566 	 */
567 	s = CIRCLEQ_PREV(r, r_link);
568 	t = CIRCLEQ_NEXT(r, r_link);
569 
570 	if (s != (void *)&rm->rm_list && (s->r_flags & RF_ALLOCATED) == 0
571 	    && t != (void *)&rm->rm_list && (t->r_flags & RF_ALLOCATED) == 0) {
572 		/*
573 		 * Merge all three segments.
574 		 */
575 		s->r_end = t->r_end;
576 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
577 		CIRCLEQ_REMOVE(&rm->rm_list, t, r_link);
578 		free(t, M_RMAN);
579 	} else if (s != (void *)&rm->rm_list
580 		   && (s->r_flags & RF_ALLOCATED) == 0) {
581 		/*
582 		 * Merge previous segment with ours.
583 		 */
584 		s->r_end = r->r_end;
585 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
586 	} else if (t != (void *)&rm->rm_list
587 		   && (t->r_flags & RF_ALLOCATED) == 0) {
588 		/*
589 		 * Merge next segment with ours.
590 		 */
591 		t->r_start = r->r_start;
592 		CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
593 	} else {
594 		/*
595 		 * At this point, we know there is nothing we
596 		 * can potentially merge with, because on each
597 		 * side, there is either nothing there or what is
598 		 * there is still allocated.  In that case, we don't
599 		 * want to remove r from the list; we simply want to
600 		 * change it to an unallocated region and return
601 		 * without freeing anything.
602 		 */
603 		r->r_flags &= ~RF_ALLOCATED;
604 		return 0;
605 	}
606 
607 out:
608 	free(r, M_RMAN);
609 	return 0;
610 }
611 
612 int
613 rman_release_resource(struct resource *r)
614 {
615 	struct	rman *rm = r->r_rm;
616 	lwkt_tokref ilock;
617 	int	rv;
618 
619 	lwkt_gettoken(&ilock, rm->rm_slock);
620 	rv = int_rman_release_resource(rm, r);
621 	lwkt_reltoken(&ilock);
622 	return (rv);
623 }
624 
625 uint32_t
626 rman_make_alignment_flags(uint32_t size)
627 {
628 	int	i;
629 
630 	/*
631 	 * Find the hightest bit set, and add one if more than one bit
632 	 * set.  We're effectively computing the ceil(log2(size)) here.
633 	 */
634 	for (i = 32; i > 0; i--)
635 		if ((1 << i) & size)
636 			break;
637 	if (~(1 << i) & size)
638 		i++;
639 
640 	return(RF_ALIGNMENT_LOG2(i));
641 }
642