xref: /freebsd/sys/kern/subr_rman.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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 
30 /*
31  * The kernel resource manager.  This code is responsible for keeping track
32  * of hardware resources which are apportioned out to various drivers.
33  * It does not actually assign those resources, and it is not expected
34  * that end-device drivers will call into this code directly.  Rather,
35  * the code which implements the buses that those devices are attached to,
36  * and the code which manages CPU resources, will call this code, and the
37  * end-device drivers will make upcalls to that code to actually perform
38  * the allocation.
39  *
40  * There are two sorts of resources managed by this code.  The first is
41  * the more familiar array (RMAN_ARRAY) type; resources in this class
42  * consist of a sequence of individually-allocatable objects which have
43  * been numbered in some well-defined order.  Most of the resources
44  * are of this type, as it is the most familiar.  The second type is
45  * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
46  * resources in which each instance is indistinguishable from every
47  * other instance).  The principal anticipated application of gauges
48  * is in the context of power consumption, where a bus may have a specific
49  * power budget which all attached devices share.  RMAN_GAUGE is not
50  * implemented yet.
51  *
52  * For array resources, we make one simplifying assumption: two clients
53  * sharing the same resource must use the same range of indices.  That
54  * is to say, sharing of overlapping-but-not-identical regions is not
55  * permitted.
56  */
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
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/mutex.h>
67 #include <sys/bus.h>		/* XXX debugging */
68 #include <machine/bus.h>
69 #include <sys/rman.h>
70 #include <sys/sysctl.h>
71 
72 /*
73  * We use a linked list rather than a bitmap because we need to be able to
74  * represent potentially huge objects (like all of a processor's physical
75  * address space).  That is also why the indices are defined to have type
76  * `unsigned long' -- that being the largest integral type in ISO C (1990).
77  * The 1999 version of C allows `long long'; we may need to switch to that
78  * at some point in the future, particularly if we want to support 36-bit
79  * addresses on IA32 hardware.
80  */
81 struct resource_i {
82 	struct resource		r_r;
83 	TAILQ_ENTRY(resource_i)	r_link;
84 	LIST_ENTRY(resource_i)	r_sharelink;
85 	LIST_HEAD(, resource_i)	*r_sharehead;
86 	u_long	r_start;	/* index of the first entry in this resource */
87 	u_long	r_end;		/* index of the last entry (inclusive) */
88 	u_int	r_flags;
89 	void	*r_virtual;	/* virtual address of this resource */
90 	struct	device *r_dev;	/* device which has allocated this resource */
91 	struct	rman *r_rm;	/* resource manager from whence this came */
92 	int	r_rid;		/* optional rid for this resource. */
93 };
94 
95 int     rman_debug = 0;
96 TUNABLE_INT("debug.rman_debug", &rman_debug);
97 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
98     &rman_debug, 0, "rman debug");
99 
100 #define DPRINTF(params) if (rman_debug) printf params
101 
102 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
103 
104 struct	rman_head rman_head;
105 static	struct mtx rman_mtx; /* mutex to protect rman_head */
106 static	int int_rman_activate_resource(struct rman *rm, struct resource_i *r,
107 				       struct resource_i **whohas);
108 static	int int_rman_deactivate_resource(struct resource_i *r);
109 static	int int_rman_release_resource(struct rman *rm, struct resource_i *r);
110 
111 static __inline struct resource_i *
112 int_alloc_resource(int malloc_flag)
113 {
114 	struct resource_i *r;
115 
116 	r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
117 	if (r != NULL) {
118 		r->r_r.__r_i = r;
119 	}
120 	return (r);
121 }
122 
123 int
124 rman_init(struct rman *rm)
125 {
126 	static int once = 0;
127 
128 	if (once == 0) {
129 		once = 1;
130 		TAILQ_INIT(&rman_head);
131 		mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
132 	}
133 
134 	if (rm->rm_type == RMAN_UNINIT)
135 		panic("rman_init");
136 	if (rm->rm_type == RMAN_GAUGE)
137 		panic("implement RMAN_GAUGE");
138 
139 	TAILQ_INIT(&rm->rm_list);
140 	rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
141 	if (rm->rm_mtx == NULL)
142 		return ENOMEM;
143 	mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
144 
145 	mtx_lock(&rman_mtx);
146 	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
147 	mtx_unlock(&rman_mtx);
148 	return 0;
149 }
150 
151 /*
152  * NB: this interface is not robust against programming errors which
153  * add multiple copies of the same region.
154  */
155 int
156 rman_manage_region(struct rman *rm, u_long start, u_long end)
157 {
158 	struct resource_i *r, *s;
159 
160 	DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
161 	    rm->rm_descr, start, end));
162 	r = int_alloc_resource(M_NOWAIT);
163 	if (r == NULL)
164 		return ENOMEM;
165 	r->r_start = start;
166 	r->r_end = end;
167 	r->r_rm = rm;
168 
169 	mtx_lock(rm->rm_mtx);
170 	for (s = TAILQ_FIRST(&rm->rm_list);
171 	     s && s->r_end < r->r_start;
172 	     s = TAILQ_NEXT(s, r_link))
173 		;
174 
175 	if (s == NULL) {
176 		TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
177 	} else {
178 		TAILQ_INSERT_BEFORE(s, r, r_link);
179 	}
180 
181 	mtx_unlock(rm->rm_mtx);
182 	return 0;
183 }
184 
185 int
186 rman_init_from_resource(struct rman *rm, struct resource *r)
187 {
188 	int rv;
189 
190 	if ((rv = rman_init(rm)) != 0)
191 		return (rv);
192 	return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
193 }
194 
195 int
196 rman_fini(struct rman *rm)
197 {
198 	struct resource_i *r;
199 
200 	mtx_lock(rm->rm_mtx);
201 	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
202 		if (r->r_flags & RF_ALLOCATED) {
203 			mtx_unlock(rm->rm_mtx);
204 			return EBUSY;
205 		}
206 	}
207 
208 	/*
209 	 * There really should only be one of these if we are in this
210 	 * state and the code is working properly, but it can't hurt.
211 	 */
212 	while (!TAILQ_EMPTY(&rm->rm_list)) {
213 		r = TAILQ_FIRST(&rm->rm_list);
214 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
215 		free(r, M_RMAN);
216 	}
217 	mtx_unlock(rm->rm_mtx);
218 	mtx_lock(&rman_mtx);
219 	TAILQ_REMOVE(&rman_head, rm, rm_link);
220 	mtx_unlock(&rman_mtx);
221 	mtx_destroy(rm->rm_mtx);
222 	free(rm->rm_mtx, M_RMAN);
223 
224 	return 0;
225 }
226 
227 struct resource *
228 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
229 		      u_long count, u_long bound,  u_int flags,
230 		      struct device *dev)
231 {
232 	u_int	want_activate;
233 	struct	resource_i *r, *s, *rv;
234 	u_long	rstart, rend, amask, bmask;
235 
236 	rv = NULL;
237 
238 	DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
239 	       "%#lx, flags %u, device %s\n", rm->rm_descr, start, end, count,
240 	       flags, dev == NULL ? "<null>" : device_get_nameunit(dev)));
241 	want_activate = (flags & RF_ACTIVE);
242 	flags &= ~RF_ACTIVE;
243 
244 	mtx_lock(rm->rm_mtx);
245 
246 	for (r = TAILQ_FIRST(&rm->rm_list);
247 	     r && r->r_end < start;
248 	     r = TAILQ_NEXT(r, r_link))
249 		;
250 
251 	if (r == NULL) {
252 		DPRINTF(("could not find a region\n"));
253 		goto out;
254 	}
255 
256 	amask = (1ul << RF_ALIGNMENT(flags)) - 1;
257 	/* If bound is 0, bmask will also be 0 */
258 	bmask = ~(bound - 1);
259 	/*
260 	 * First try to find an acceptable totally-unshared region.
261 	 */
262 	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
263 		DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
264 		if (s->r_start + count - 1 > end) {
265 			DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
266 			    s->r_start, end));
267 			break;
268 		}
269 		if (s->r_flags & RF_ALLOCATED) {
270 			DPRINTF(("region is allocated\n"));
271 			continue;
272 		}
273 		rstart = ulmax(s->r_start, start);
274 		/*
275 		 * Try to find a region by adjusting to boundary and alignment
276 		 * until both conditions are satisfied. This is not an optimal
277 		 * algorithm, but in most cases it isn't really bad, either.
278 		 */
279 		do {
280 			rstart = (rstart + amask) & ~amask;
281 			if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
282 				rstart += bound - (rstart & ~bmask);
283 		} while ((rstart & amask) != 0 && rstart < end &&
284 		    rstart < s->r_end);
285 		rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
286 		if (rstart > rend) {
287 			DPRINTF(("adjusted start exceeds end\n"));
288 			continue;
289 		}
290 		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
291 		       rstart, rend, (rend - rstart + 1), count));
292 
293 		if ((rend - rstart + 1) >= count) {
294 			DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
295 			       rstart, rend, (rend - rstart + 1)));
296 			if ((s->r_end - s->r_start + 1) == count) {
297 				DPRINTF(("candidate region is entire chunk\n"));
298 				rv = s;
299 				rv->r_flags |= RF_ALLOCATED | flags;
300 				rv->r_dev = dev;
301 				goto out;
302 			}
303 
304 			/*
305 			 * If s->r_start < rstart and
306 			 *    s->r_end > rstart + count - 1, then
307 			 * we need to split the region into three pieces
308 			 * (the middle one will get returned to the user).
309 			 * Otherwise, we are allocating at either the
310 			 * beginning or the end of s, so we only need to
311 			 * split it in two.  The first case requires
312 			 * two new allocations; the second requires but one.
313 			 */
314 			rv = int_alloc_resource(M_NOWAIT);
315 			if (rv == NULL)
316 				goto out;
317 			rv->r_start = rstart;
318 			rv->r_end = rstart + count - 1;
319 			rv->r_flags = flags | RF_ALLOCATED;
320 			rv->r_dev = dev;
321 			rv->r_rm = rm;
322 
323 			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
324 				DPRINTF(("splitting region in three parts: "
325 				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
326 				       s->r_start, rv->r_start - 1,
327 				       rv->r_start, rv->r_end,
328 				       rv->r_end + 1, s->r_end));
329 				/*
330 				 * We are allocating in the middle.
331 				 */
332 				r = int_alloc_resource(M_NOWAIT);
333 				if (r == NULL) {
334 					free(rv, M_RMAN);
335 					rv = NULL;
336 					goto out;
337 				}
338 				r->r_start = rv->r_end + 1;
339 				r->r_end = s->r_end;
340 				r->r_flags = s->r_flags;
341 				r->r_rm = rm;
342 				s->r_end = rv->r_start - 1;
343 				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
344 						     r_link);
345 				TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
346 						     r_link);
347 			} else if (s->r_start == rv->r_start) {
348 				DPRINTF(("allocating from the beginning\n"));
349 				/*
350 				 * We are allocating at the beginning.
351 				 */
352 				s->r_start = rv->r_end + 1;
353 				TAILQ_INSERT_BEFORE(s, rv, r_link);
354 			} else {
355 				DPRINTF(("allocating at the end\n"));
356 				/*
357 				 * We are allocating at the end.
358 				 */
359 				s->r_end = rv->r_start - 1;
360 				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
361 						     r_link);
362 			}
363 			goto out;
364 		}
365 	}
366 
367 	/*
368 	 * Now find an acceptable shared region, if the client's requirements
369 	 * allow sharing.  By our implementation restriction, a candidate
370 	 * region must match exactly by both size and sharing type in order
371 	 * to be considered compatible with the client's request.  (The
372 	 * former restriction could probably be lifted without too much
373 	 * additional work, but this does not seem warranted.)
374 	 */
375 	DPRINTF(("no unshared regions found\n"));
376 	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
377 		goto out;
378 
379 	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
380 		if (s->r_start > end)
381 			break;
382 		if ((s->r_flags & flags) != flags)
383 			continue;
384 		rstart = ulmax(s->r_start, start);
385 		rend = ulmin(s->r_end, ulmax(start + count - 1, end));
386 		if (s->r_start >= start && s->r_end <= end
387 		    && (s->r_end - s->r_start + 1) == count &&
388 		    (s->r_start & amask) == 0 &&
389 		    ((s->r_start ^ s->r_end) & bmask) == 0) {
390 			rv = int_alloc_resource(M_NOWAIT);
391 			if (rv == NULL)
392 				goto out;
393 			rv->r_start = s->r_start;
394 			rv->r_end = s->r_end;
395 			rv->r_flags = s->r_flags &
396 				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
397 			rv->r_dev = dev;
398 			rv->r_rm = rm;
399 			if (s->r_sharehead == NULL) {
400 				s->r_sharehead = malloc(sizeof *s->r_sharehead,
401 						M_RMAN, M_NOWAIT | M_ZERO);
402 				if (s->r_sharehead == NULL) {
403 					free(rv, M_RMAN);
404 					rv = NULL;
405 					goto out;
406 				}
407 				LIST_INIT(s->r_sharehead);
408 				LIST_INSERT_HEAD(s->r_sharehead, s,
409 						 r_sharelink);
410 				s->r_flags |= RF_FIRSTSHARE;
411 			}
412 			rv->r_sharehead = s->r_sharehead;
413 			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
414 			goto out;
415 		}
416 	}
417 
418 	/*
419 	 * We couldn't find anything.
420 	 */
421 out:
422 	/*
423 	 * If the user specified RF_ACTIVE in the initial flags,
424 	 * which is reflected in `want_activate', we attempt to atomically
425 	 * activate the resource.  If this fails, we release the resource
426 	 * and indicate overall failure.  (This behavior probably doesn't
427 	 * make sense for RF_TIMESHARE-type resources.)
428 	 */
429 	if (rv && want_activate) {
430 		struct resource_i *whohas;
431 		if (int_rman_activate_resource(rm, rv, &whohas)) {
432 			int_rman_release_resource(rm, rv);
433 			rv = NULL;
434 		}
435 	}
436 
437 	mtx_unlock(rm->rm_mtx);
438 	return (rv == NULL ? NULL : &rv->r_r);
439 }
440 
441 struct resource *
442 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
443 		      u_int flags, struct device *dev)
444 {
445 
446 	return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
447 	    dev));
448 }
449 
450 static int
451 int_rman_activate_resource(struct rman *rm, struct resource_i *r,
452 			   struct resource_i **whohas)
453 {
454 	struct resource_i *s;
455 	int ok;
456 
457 	/*
458 	 * If we are not timesharing, then there is nothing much to do.
459 	 * If we already have the resource, then there is nothing at all to do.
460 	 * If we are not on a sharing list with anybody else, then there is
461 	 * little to do.
462 	 */
463 	if ((r->r_flags & RF_TIMESHARE) == 0
464 	    || (r->r_flags & RF_ACTIVE) != 0
465 	    || r->r_sharehead == NULL) {
466 		r->r_flags |= RF_ACTIVE;
467 		return 0;
468 	}
469 
470 	ok = 1;
471 	for (s = LIST_FIRST(r->r_sharehead); s && ok;
472 	     s = LIST_NEXT(s, r_sharelink)) {
473 		if ((s->r_flags & RF_ACTIVE) != 0) {
474 			ok = 0;
475 			*whohas = s;
476 		}
477 	}
478 	if (ok) {
479 		r->r_flags |= RF_ACTIVE;
480 		return 0;
481 	}
482 	return EBUSY;
483 }
484 
485 int
486 rman_activate_resource(struct resource *re)
487 {
488 	int rv;
489 	struct resource_i *r, *whohas;
490 	struct rman *rm;
491 
492 	r = re->__r_i;
493 	rm = r->r_rm;
494 	mtx_lock(rm->rm_mtx);
495 	rv = int_rman_activate_resource(rm, r, &whohas);
496 	mtx_unlock(rm->rm_mtx);
497 	return rv;
498 }
499 
500 int
501 rman_await_resource(struct resource *re, int pri, int timo)
502 {
503 	int	rv;
504 	struct	resource_i *r, *whohas;
505 	struct	rman *rm;
506 
507 	r = re->__r_i;
508 	rm = r->r_rm;
509 	mtx_lock(rm->rm_mtx);
510 	for (;;) {
511 		rv = int_rman_activate_resource(rm, r, &whohas);
512 		if (rv != EBUSY)
513 			return (rv);	/* returns with mutex held */
514 
515 		if (r->r_sharehead == NULL)
516 			panic("rman_await_resource");
517 		whohas->r_flags |= RF_WANTED;
518 		rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
519 		if (rv) {
520 			mtx_unlock(rm->rm_mtx);
521 			return (rv);
522 		}
523 	}
524 }
525 
526 static int
527 int_rman_deactivate_resource(struct resource_i *r)
528 {
529 
530 	r->r_flags &= ~RF_ACTIVE;
531 	if (r->r_flags & RF_WANTED) {
532 		r->r_flags &= ~RF_WANTED;
533 		wakeup(r->r_sharehead);
534 	}
535 	return 0;
536 }
537 
538 int
539 rman_deactivate_resource(struct resource *r)
540 {
541 	struct	rman *rm;
542 
543 	rm = r->__r_i->r_rm;
544 	mtx_lock(rm->rm_mtx);
545 	int_rman_deactivate_resource(r->__r_i);
546 	mtx_unlock(rm->rm_mtx);
547 	return 0;
548 }
549 
550 static int
551 int_rman_release_resource(struct rman *rm, struct resource_i *r)
552 {
553 	struct	resource_i *s, *t;
554 
555 	if (r->r_flags & RF_ACTIVE)
556 		int_rman_deactivate_resource(r);
557 
558 	/*
559 	 * Check for a sharing list first.  If there is one, then we don't
560 	 * have to think as hard.
561 	 */
562 	if (r->r_sharehead) {
563 		/*
564 		 * If a sharing list exists, then we know there are at
565 		 * least two sharers.
566 		 *
567 		 * If we are in the main circleq, appoint someone else.
568 		 */
569 		LIST_REMOVE(r, r_sharelink);
570 		s = LIST_FIRST(r->r_sharehead);
571 		if (r->r_flags & RF_FIRSTSHARE) {
572 			s->r_flags |= RF_FIRSTSHARE;
573 			TAILQ_INSERT_BEFORE(r, s, r_link);
574 			TAILQ_REMOVE(&rm->rm_list, r, r_link);
575 		}
576 
577 		/*
578 		 * Make sure that the sharing list goes away completely
579 		 * if the resource is no longer being shared at all.
580 		 */
581 		if (LIST_NEXT(s, r_sharelink) == NULL) {
582 			free(s->r_sharehead, M_RMAN);
583 			s->r_sharehead = NULL;
584 			s->r_flags &= ~RF_FIRSTSHARE;
585 		}
586 		goto out;
587 	}
588 
589 	/*
590 	 * Look at the adjacent resources in the list and see if our
591 	 * segment can be merged with any of them.  If either of the
592 	 * resources is allocated or is not exactly adjacent then they
593 	 * cannot be merged with our segment.
594 	 */
595 	s = TAILQ_PREV(r, resource_head, r_link);
596 	if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
597 	    s->r_end + 1 != r->r_start))
598 		s = NULL;
599 	t = TAILQ_NEXT(r, r_link);
600 	if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
601 	    r->r_end + 1 != t->r_start))
602 		t = NULL;
603 
604 	if (s != NULL && t != NULL) {
605 		/*
606 		 * Merge all three segments.
607 		 */
608 		s->r_end = t->r_end;
609 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
610 		TAILQ_REMOVE(&rm->rm_list, t, r_link);
611 		free(t, M_RMAN);
612 	} else if (s != NULL) {
613 		/*
614 		 * Merge previous segment with ours.
615 		 */
616 		s->r_end = r->r_end;
617 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
618 	} else if (t != NULL) {
619 		/*
620 		 * Merge next segment with ours.
621 		 */
622 		t->r_start = r->r_start;
623 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
624 	} else {
625 		/*
626 		 * At this point, we know there is nothing we
627 		 * can potentially merge with, because on each
628 		 * side, there is either nothing there or what is
629 		 * there is still allocated.  In that case, we don't
630 		 * want to remove r from the list; we simply want to
631 		 * change it to an unallocated region and return
632 		 * without freeing anything.
633 		 */
634 		r->r_flags &= ~RF_ALLOCATED;
635 		return 0;
636 	}
637 
638 out:
639 	free(r, M_RMAN);
640 	return 0;
641 }
642 
643 int
644 rman_release_resource(struct resource *re)
645 {
646 	int	rv;
647 	struct	resource_i *r;
648 	struct	rman *rm;
649 
650 	r = re->__r_i;
651 	rm = r->r_rm;
652 	mtx_lock(rm->rm_mtx);
653 	rv = int_rman_release_resource(rm, r);
654 	mtx_unlock(rm->rm_mtx);
655 	return (rv);
656 }
657 
658 uint32_t
659 rman_make_alignment_flags(uint32_t size)
660 {
661 	int	i;
662 
663 	/*
664 	 * Find the hightest bit set, and add one if more than one bit
665 	 * set.  We're effectively computing the ceil(log2(size)) here.
666 	 */
667 	for (i = 31; i > 0; i--)
668 		if ((1 << i) & size)
669 			break;
670 	if (~(1 << i) & size)
671 		i++;
672 
673 	return(RF_ALIGNMENT_LOG2(i));
674 }
675 
676 u_long
677 rman_get_start(struct resource *r)
678 {
679 	return (r->__r_i->r_start);
680 }
681 
682 u_long
683 rman_get_end(struct resource *r)
684 {
685 	return (r->__r_i->r_end);
686 }
687 
688 u_long
689 rman_get_size(struct resource *r)
690 {
691 	return (r->__r_i->r_end - r->__r_i->r_start + 1);
692 }
693 
694 u_int
695 rman_get_flags(struct resource *r)
696 {
697 	return (r->__r_i->r_flags);
698 }
699 
700 void
701 rman_set_virtual(struct resource *r, void *v)
702 {
703 	r->__r_i->r_virtual = v;
704 }
705 
706 void *
707 rman_get_virtual(struct resource *r)
708 {
709 	return (r->__r_i->r_virtual);
710 }
711 
712 void
713 rman_set_bustag(struct resource *r, bus_space_tag_t t)
714 {
715 	r->r_bustag = t;
716 }
717 
718 bus_space_tag_t
719 rman_get_bustag(struct resource *r)
720 {
721 	return (r->r_bustag);
722 }
723 
724 void
725 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
726 {
727 	r->r_bushandle = h;
728 }
729 
730 bus_space_handle_t
731 rman_get_bushandle(struct resource *r)
732 {
733 	return (r->r_bushandle);
734 }
735 
736 void
737 rman_set_rid(struct resource *r, int rid)
738 {
739 	r->__r_i->r_rid = rid;
740 }
741 
742 void
743 rman_set_start(struct resource *r, u_long start)
744 {
745 	r->__r_i->r_start = start;
746 }
747 
748 void
749 rman_set_end(struct resource *r, u_long end)
750 {
751 	r->__r_i->r_end = end;
752 }
753 
754 int
755 rman_get_rid(struct resource *r)
756 {
757 	return (r->__r_i->r_rid);
758 }
759 
760 struct device *
761 rman_get_device(struct resource *r)
762 {
763 	return (r->__r_i->r_dev);
764 }
765 
766 void
767 rman_set_device(struct resource *r, struct device *dev)
768 {
769 	r->__r_i->r_dev = dev;
770 }
771 
772 int
773 rman_is_region_manager(struct resource *r, struct rman *rm)
774 {
775 
776 	return (r->__r_i->r_rm == rm);
777 }
778 
779 /*
780  * Sysctl interface for scanning the resource lists.
781  *
782  * We take two input parameters; the index into the list of resource
783  * managers, and the resource offset into the list.
784  */
785 static int
786 sysctl_rman(SYSCTL_HANDLER_ARGS)
787 {
788 	int			*name = (int *)arg1;
789 	u_int			namelen = arg2;
790 	int			rman_idx, res_idx;
791 	struct rman		*rm;
792 	struct resource_i	*res;
793 	struct u_rman		urm;
794 	struct u_resource	ures;
795 	int			error;
796 
797 	if (namelen != 3)
798 		return (EINVAL);
799 
800 	if (bus_data_generation_check(name[0]))
801 		return (EINVAL);
802 	rman_idx = name[1];
803 	res_idx = name[2];
804 
805 	/*
806 	 * Find the indexed resource manager
807 	 */
808 	mtx_lock(&rman_mtx);
809 	TAILQ_FOREACH(rm, &rman_head, rm_link) {
810 		if (rman_idx-- == 0)
811 			break;
812 	}
813 	mtx_unlock(&rman_mtx);
814 	if (rm == NULL)
815 		return (ENOENT);
816 
817 	/*
818 	 * If the resource index is -1, we want details on the
819 	 * resource manager.
820 	 */
821 	if (res_idx == -1) {
822 		bzero(&urm, sizeof(urm));
823 		urm.rm_handle = (uintptr_t)rm;
824 		strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
825 		urm.rm_start = rm->rm_start;
826 		urm.rm_size = rm->rm_end - rm->rm_start + 1;
827 		urm.rm_type = rm->rm_type;
828 
829 		error = SYSCTL_OUT(req, &urm, sizeof(urm));
830 		return (error);
831 	}
832 
833 	/*
834 	 * Find the indexed resource and return it.
835 	 */
836 	mtx_lock(rm->rm_mtx);
837 	TAILQ_FOREACH(res, &rm->rm_list, r_link) {
838 		if (res_idx-- == 0) {
839 			bzero(&ures, sizeof(ures));
840 			ures.r_handle = (uintptr_t)res;
841 			ures.r_parent = (uintptr_t)res->r_rm;
842 			ures.r_device = (uintptr_t)res->r_dev;
843 			if (res->r_dev != NULL) {
844 				if (device_get_name(res->r_dev) != NULL) {
845 					snprintf(ures.r_devname, RM_TEXTLEN,
846 					    "%s%d",
847 					    device_get_name(res->r_dev),
848 					    device_get_unit(res->r_dev));
849 				} else {
850 					strlcpy(ures.r_devname, "nomatch",
851 					    RM_TEXTLEN);
852 				}
853 			} else {
854 				ures.r_devname[0] = '\0';
855 			}
856 			ures.r_start = res->r_start;
857 			ures.r_size = res->r_end - res->r_start + 1;
858 			ures.r_flags = res->r_flags;
859 
860 			mtx_unlock(rm->rm_mtx);
861 			error = SYSCTL_OUT(req, &ures, sizeof(ures));
862 			return (error);
863 		}
864 	}
865 	mtx_unlock(rm->rm_mtx);
866 	return (ENOENT);
867 }
868 
869 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
870     "kernel resource manager");
871