xref: /freebsd/sys/kern/subr_rman.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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, *t;
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 
171 	/* Skip entries before us. */
172 	for (s = TAILQ_FIRST(&rm->rm_list);
173 	     s && s->r_end + 1 < r->r_start;
174 	     s = TAILQ_NEXT(s, r_link))
175 		;
176 
177 	/* If we ran off the end of the list, insert at the tail. */
178 	if (s == NULL) {
179 		TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
180 	} else {
181 		/* Check for any overlap with the current region. */
182 		if (r->r_start <= s->r_end && r->r_end >= s->r_start)
183 			return EBUSY;
184 
185 		/* Check for any overlap with the next region. */
186 		t = TAILQ_NEXT(s, r_link);
187 		if (t && r->r_start <= t->r_end && r->r_end >= t->r_start)
188 			return EBUSY;
189 
190 		/*
191 		 * See if this region can be merged with the next region.  If
192 		 * not, clear the pointer.
193 		 */
194 		if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
195 			t = NULL;
196 
197 		/* See if we can merge with the current region. */
198 		if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
199 			/* Can we merge all 3 regions? */
200 			if (t != NULL) {
201 				s->r_end = t->r_end;
202 				TAILQ_REMOVE(&rm->rm_list, t, r_link);
203 				free(r, M_RMAN);
204 				free(t, M_RMAN);
205 			} else {
206 				s->r_end = r->r_end;
207 				free(r, M_RMAN);
208 			}
209 		} else {
210 			/* Can we merge with just the next region? */
211 			if (t != NULL) {
212 				t->r_start = r->r_start;
213 				free(r, M_RMAN);
214 			} else
215 				TAILQ_INSERT_BEFORE(s, r, r_link);
216 		}
217 	}
218 
219 	mtx_unlock(rm->rm_mtx);
220 	return 0;
221 }
222 
223 int
224 rman_init_from_resource(struct rman *rm, struct resource *r)
225 {
226 	int rv;
227 
228 	if ((rv = rman_init(rm)) != 0)
229 		return (rv);
230 	return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
231 }
232 
233 int
234 rman_fini(struct rman *rm)
235 {
236 	struct resource_i *r;
237 
238 	mtx_lock(rm->rm_mtx);
239 	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
240 		if (r->r_flags & RF_ALLOCATED) {
241 			mtx_unlock(rm->rm_mtx);
242 			return EBUSY;
243 		}
244 	}
245 
246 	/*
247 	 * There really should only be one of these if we are in this
248 	 * state and the code is working properly, but it can't hurt.
249 	 */
250 	while (!TAILQ_EMPTY(&rm->rm_list)) {
251 		r = TAILQ_FIRST(&rm->rm_list);
252 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
253 		free(r, M_RMAN);
254 	}
255 	mtx_unlock(rm->rm_mtx);
256 	mtx_lock(&rman_mtx);
257 	TAILQ_REMOVE(&rman_head, rm, rm_link);
258 	mtx_unlock(&rman_mtx);
259 	mtx_destroy(rm->rm_mtx);
260 	free(rm->rm_mtx, M_RMAN);
261 
262 	return 0;
263 }
264 
265 struct resource *
266 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
267 		      u_long count, u_long bound,  u_int flags,
268 		      struct device *dev)
269 {
270 	u_int	want_activate;
271 	struct	resource_i *r, *s, *rv;
272 	u_long	rstart, rend, amask, bmask;
273 
274 	rv = NULL;
275 
276 	DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
277 	       "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
278 	       count, flags,
279 	       dev == NULL ? "<null>" : device_get_nameunit(dev)));
280 	want_activate = (flags & RF_ACTIVE);
281 	flags &= ~RF_ACTIVE;
282 
283 	mtx_lock(rm->rm_mtx);
284 
285 	for (r = TAILQ_FIRST(&rm->rm_list);
286 	     r && r->r_end < start;
287 	     r = TAILQ_NEXT(r, r_link))
288 		;
289 
290 	if (r == NULL) {
291 		DPRINTF(("could not find a region\n"));
292 		goto out;
293 	}
294 
295 	amask = (1ul << RF_ALIGNMENT(flags)) - 1;
296 	/* If bound is 0, bmask will also be 0 */
297 	bmask = ~(bound - 1);
298 	/*
299 	 * First try to find an acceptable totally-unshared region.
300 	 */
301 	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
302 		DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
303 		if (s->r_start + count - 1 > end) {
304 			DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
305 			    s->r_start, end));
306 			break;
307 		}
308 		if (s->r_flags & RF_ALLOCATED) {
309 			DPRINTF(("region is allocated\n"));
310 			continue;
311 		}
312 		rstart = ulmax(s->r_start, start);
313 		/*
314 		 * Try to find a region by adjusting to boundary and alignment
315 		 * until both conditions are satisfied. This is not an optimal
316 		 * algorithm, but in most cases it isn't really bad, either.
317 		 */
318 		do {
319 			rstart = (rstart + amask) & ~amask;
320 			if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
321 				rstart += bound - (rstart & ~bmask);
322 		} while ((rstart & amask) != 0 && rstart < end &&
323 		    rstart < s->r_end);
324 		rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
325 		if (rstart > rend) {
326 			DPRINTF(("adjusted start exceeds end\n"));
327 			continue;
328 		}
329 		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
330 		       rstart, rend, (rend - rstart + 1), count));
331 
332 		if ((rend - rstart + 1) >= count) {
333 			DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
334 			       rstart, rend, (rend - rstart + 1)));
335 			if ((s->r_end - s->r_start + 1) == count) {
336 				DPRINTF(("candidate region is entire chunk\n"));
337 				rv = s;
338 				rv->r_flags |= RF_ALLOCATED | flags;
339 				rv->r_dev = dev;
340 				goto out;
341 			}
342 
343 			/*
344 			 * If s->r_start < rstart and
345 			 *    s->r_end > rstart + count - 1, then
346 			 * we need to split the region into three pieces
347 			 * (the middle one will get returned to the user).
348 			 * Otherwise, we are allocating at either the
349 			 * beginning or the end of s, so we only need to
350 			 * split it in two.  The first case requires
351 			 * two new allocations; the second requires but one.
352 			 */
353 			rv = int_alloc_resource(M_NOWAIT);
354 			if (rv == NULL)
355 				goto out;
356 			rv->r_start = rstart;
357 			rv->r_end = rstart + count - 1;
358 			rv->r_flags = flags | RF_ALLOCATED;
359 			rv->r_dev = dev;
360 			rv->r_rm = rm;
361 
362 			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
363 				DPRINTF(("splitting region in three parts: "
364 				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
365 				       s->r_start, rv->r_start - 1,
366 				       rv->r_start, rv->r_end,
367 				       rv->r_end + 1, s->r_end));
368 				/*
369 				 * We are allocating in the middle.
370 				 */
371 				r = int_alloc_resource(M_NOWAIT);
372 				if (r == NULL) {
373 					free(rv, M_RMAN);
374 					rv = NULL;
375 					goto out;
376 				}
377 				r->r_start = rv->r_end + 1;
378 				r->r_end = s->r_end;
379 				r->r_flags = s->r_flags;
380 				r->r_rm = rm;
381 				s->r_end = rv->r_start - 1;
382 				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
383 						     r_link);
384 				TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
385 						     r_link);
386 			} else if (s->r_start == rv->r_start) {
387 				DPRINTF(("allocating from the beginning\n"));
388 				/*
389 				 * We are allocating at the beginning.
390 				 */
391 				s->r_start = rv->r_end + 1;
392 				TAILQ_INSERT_BEFORE(s, rv, r_link);
393 			} else {
394 				DPRINTF(("allocating at the end\n"));
395 				/*
396 				 * We are allocating at the end.
397 				 */
398 				s->r_end = rv->r_start - 1;
399 				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
400 						     r_link);
401 			}
402 			goto out;
403 		}
404 	}
405 
406 	/*
407 	 * Now find an acceptable shared region, if the client's requirements
408 	 * allow sharing.  By our implementation restriction, a candidate
409 	 * region must match exactly by both size and sharing type in order
410 	 * to be considered compatible with the client's request.  (The
411 	 * former restriction could probably be lifted without too much
412 	 * additional work, but this does not seem warranted.)
413 	 */
414 	DPRINTF(("no unshared regions found\n"));
415 	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
416 		goto out;
417 
418 	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
419 		if (s->r_start > end)
420 			break;
421 		if ((s->r_flags & flags) != flags)
422 			continue;
423 		rstart = ulmax(s->r_start, start);
424 		rend = ulmin(s->r_end, ulmax(start + count - 1, end));
425 		if (s->r_start >= start && s->r_end <= end
426 		    && (s->r_end - s->r_start + 1) == count &&
427 		    (s->r_start & amask) == 0 &&
428 		    ((s->r_start ^ s->r_end) & bmask) == 0) {
429 			rv = int_alloc_resource(M_NOWAIT);
430 			if (rv == NULL)
431 				goto out;
432 			rv->r_start = s->r_start;
433 			rv->r_end = s->r_end;
434 			rv->r_flags = s->r_flags &
435 				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
436 			rv->r_dev = dev;
437 			rv->r_rm = rm;
438 			if (s->r_sharehead == NULL) {
439 				s->r_sharehead = malloc(sizeof *s->r_sharehead,
440 						M_RMAN, M_NOWAIT | M_ZERO);
441 				if (s->r_sharehead == NULL) {
442 					free(rv, M_RMAN);
443 					rv = NULL;
444 					goto out;
445 				}
446 				LIST_INIT(s->r_sharehead);
447 				LIST_INSERT_HEAD(s->r_sharehead, s,
448 						 r_sharelink);
449 				s->r_flags |= RF_FIRSTSHARE;
450 			}
451 			rv->r_sharehead = s->r_sharehead;
452 			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
453 			goto out;
454 		}
455 	}
456 
457 	/*
458 	 * We couldn't find anything.
459 	 */
460 out:
461 	/*
462 	 * If the user specified RF_ACTIVE in the initial flags,
463 	 * which is reflected in `want_activate', we attempt to atomically
464 	 * activate the resource.  If this fails, we release the resource
465 	 * and indicate overall failure.  (This behavior probably doesn't
466 	 * make sense for RF_TIMESHARE-type resources.)
467 	 */
468 	if (rv && want_activate) {
469 		struct resource_i *whohas;
470 		if (int_rman_activate_resource(rm, rv, &whohas)) {
471 			int_rman_release_resource(rm, rv);
472 			rv = NULL;
473 		}
474 	}
475 
476 	mtx_unlock(rm->rm_mtx);
477 	return (rv == NULL ? NULL : &rv->r_r);
478 }
479 
480 struct resource *
481 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
482 		      u_int flags, struct device *dev)
483 {
484 
485 	return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
486 	    dev));
487 }
488 
489 static int
490 int_rman_activate_resource(struct rman *rm, struct resource_i *r,
491 			   struct resource_i **whohas)
492 {
493 	struct resource_i *s;
494 	int ok;
495 
496 	/*
497 	 * If we are not timesharing, then there is nothing much to do.
498 	 * If we already have the resource, then there is nothing at all to do.
499 	 * If we are not on a sharing list with anybody else, then there is
500 	 * little to do.
501 	 */
502 	if ((r->r_flags & RF_TIMESHARE) == 0
503 	    || (r->r_flags & RF_ACTIVE) != 0
504 	    || r->r_sharehead == NULL) {
505 		r->r_flags |= RF_ACTIVE;
506 		return 0;
507 	}
508 
509 	ok = 1;
510 	for (s = LIST_FIRST(r->r_sharehead); s && ok;
511 	     s = LIST_NEXT(s, r_sharelink)) {
512 		if ((s->r_flags & RF_ACTIVE) != 0) {
513 			ok = 0;
514 			*whohas = s;
515 		}
516 	}
517 	if (ok) {
518 		r->r_flags |= RF_ACTIVE;
519 		return 0;
520 	}
521 	return EBUSY;
522 }
523 
524 int
525 rman_activate_resource(struct resource *re)
526 {
527 	int rv;
528 	struct resource_i *r, *whohas;
529 	struct rman *rm;
530 
531 	r = re->__r_i;
532 	rm = r->r_rm;
533 	mtx_lock(rm->rm_mtx);
534 	rv = int_rman_activate_resource(rm, r, &whohas);
535 	mtx_unlock(rm->rm_mtx);
536 	return rv;
537 }
538 
539 int
540 rman_await_resource(struct resource *re, int pri, int timo)
541 {
542 	int	rv;
543 	struct	resource_i *r, *whohas;
544 	struct	rman *rm;
545 
546 	r = re->__r_i;
547 	rm = r->r_rm;
548 	mtx_lock(rm->rm_mtx);
549 	for (;;) {
550 		rv = int_rman_activate_resource(rm, r, &whohas);
551 		if (rv != EBUSY)
552 			return (rv);	/* returns with mutex held */
553 
554 		if (r->r_sharehead == NULL)
555 			panic("rman_await_resource");
556 		whohas->r_flags |= RF_WANTED;
557 		rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
558 		if (rv) {
559 			mtx_unlock(rm->rm_mtx);
560 			return (rv);
561 		}
562 	}
563 }
564 
565 static int
566 int_rman_deactivate_resource(struct resource_i *r)
567 {
568 
569 	r->r_flags &= ~RF_ACTIVE;
570 	if (r->r_flags & RF_WANTED) {
571 		r->r_flags &= ~RF_WANTED;
572 		wakeup(r->r_sharehead);
573 	}
574 	return 0;
575 }
576 
577 int
578 rman_deactivate_resource(struct resource *r)
579 {
580 	struct	rman *rm;
581 
582 	rm = r->__r_i->r_rm;
583 	mtx_lock(rm->rm_mtx);
584 	int_rman_deactivate_resource(r->__r_i);
585 	mtx_unlock(rm->rm_mtx);
586 	return 0;
587 }
588 
589 static int
590 int_rman_release_resource(struct rman *rm, struct resource_i *r)
591 {
592 	struct	resource_i *s, *t;
593 
594 	if (r->r_flags & RF_ACTIVE)
595 		int_rman_deactivate_resource(r);
596 
597 	/*
598 	 * Check for a sharing list first.  If there is one, then we don't
599 	 * have to think as hard.
600 	 */
601 	if (r->r_sharehead) {
602 		/*
603 		 * If a sharing list exists, then we know there are at
604 		 * least two sharers.
605 		 *
606 		 * If we are in the main circleq, appoint someone else.
607 		 */
608 		LIST_REMOVE(r, r_sharelink);
609 		s = LIST_FIRST(r->r_sharehead);
610 		if (r->r_flags & RF_FIRSTSHARE) {
611 			s->r_flags |= RF_FIRSTSHARE;
612 			TAILQ_INSERT_BEFORE(r, s, r_link);
613 			TAILQ_REMOVE(&rm->rm_list, r, r_link);
614 		}
615 
616 		/*
617 		 * Make sure that the sharing list goes away completely
618 		 * if the resource is no longer being shared at all.
619 		 */
620 		if (LIST_NEXT(s, r_sharelink) == NULL) {
621 			free(s->r_sharehead, M_RMAN);
622 			s->r_sharehead = NULL;
623 			s->r_flags &= ~RF_FIRSTSHARE;
624 		}
625 		goto out;
626 	}
627 
628 	/*
629 	 * Look at the adjacent resources in the list and see if our
630 	 * segment can be merged with any of them.  If either of the
631 	 * resources is allocated or is not exactly adjacent then they
632 	 * cannot be merged with our segment.
633 	 */
634 	s = TAILQ_PREV(r, resource_head, r_link);
635 	if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
636 	    s->r_end + 1 != r->r_start))
637 		s = NULL;
638 	t = TAILQ_NEXT(r, r_link);
639 	if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
640 	    r->r_end + 1 != t->r_start))
641 		t = NULL;
642 
643 	if (s != NULL && t != NULL) {
644 		/*
645 		 * Merge all three segments.
646 		 */
647 		s->r_end = t->r_end;
648 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
649 		TAILQ_REMOVE(&rm->rm_list, t, r_link);
650 		free(t, M_RMAN);
651 	} else if (s != NULL) {
652 		/*
653 		 * Merge previous segment with ours.
654 		 */
655 		s->r_end = r->r_end;
656 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
657 	} else if (t != NULL) {
658 		/*
659 		 * Merge next segment with ours.
660 		 */
661 		t->r_start = r->r_start;
662 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
663 	} else {
664 		/*
665 		 * At this point, we know there is nothing we
666 		 * can potentially merge with, because on each
667 		 * side, there is either nothing there or what is
668 		 * there is still allocated.  In that case, we don't
669 		 * want to remove r from the list; we simply want to
670 		 * change it to an unallocated region and return
671 		 * without freeing anything.
672 		 */
673 		r->r_flags &= ~RF_ALLOCATED;
674 		return 0;
675 	}
676 
677 out:
678 	free(r, M_RMAN);
679 	return 0;
680 }
681 
682 int
683 rman_release_resource(struct resource *re)
684 {
685 	int	rv;
686 	struct	resource_i *r;
687 	struct	rman *rm;
688 
689 	r = re->__r_i;
690 	rm = r->r_rm;
691 	mtx_lock(rm->rm_mtx);
692 	rv = int_rman_release_resource(rm, r);
693 	mtx_unlock(rm->rm_mtx);
694 	return (rv);
695 }
696 
697 uint32_t
698 rman_make_alignment_flags(uint32_t size)
699 {
700 	int	i;
701 
702 	/*
703 	 * Find the hightest bit set, and add one if more than one bit
704 	 * set.  We're effectively computing the ceil(log2(size)) here.
705 	 */
706 	for (i = 31; i > 0; i--)
707 		if ((1 << i) & size)
708 			break;
709 	if (~(1 << i) & size)
710 		i++;
711 
712 	return(RF_ALIGNMENT_LOG2(i));
713 }
714 
715 u_long
716 rman_get_start(struct resource *r)
717 {
718 	return (r->__r_i->r_start);
719 }
720 
721 u_long
722 rman_get_end(struct resource *r)
723 {
724 	return (r->__r_i->r_end);
725 }
726 
727 u_long
728 rman_get_size(struct resource *r)
729 {
730 	return (r->__r_i->r_end - r->__r_i->r_start + 1);
731 }
732 
733 u_int
734 rman_get_flags(struct resource *r)
735 {
736 	return (r->__r_i->r_flags);
737 }
738 
739 void
740 rman_set_virtual(struct resource *r, void *v)
741 {
742 	r->__r_i->r_virtual = v;
743 }
744 
745 void *
746 rman_get_virtual(struct resource *r)
747 {
748 	return (r->__r_i->r_virtual);
749 }
750 
751 void
752 rman_set_bustag(struct resource *r, bus_space_tag_t t)
753 {
754 	r->r_bustag = t;
755 }
756 
757 bus_space_tag_t
758 rman_get_bustag(struct resource *r)
759 {
760 	return (r->r_bustag);
761 }
762 
763 void
764 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
765 {
766 	r->r_bushandle = h;
767 }
768 
769 bus_space_handle_t
770 rman_get_bushandle(struct resource *r)
771 {
772 	return (r->r_bushandle);
773 }
774 
775 void
776 rman_set_rid(struct resource *r, int rid)
777 {
778 	r->__r_i->r_rid = rid;
779 }
780 
781 void
782 rman_set_start(struct resource *r, u_long start)
783 {
784 	r->__r_i->r_start = start;
785 }
786 
787 void
788 rman_set_end(struct resource *r, u_long end)
789 {
790 	r->__r_i->r_end = end;
791 }
792 
793 int
794 rman_get_rid(struct resource *r)
795 {
796 	return (r->__r_i->r_rid);
797 }
798 
799 struct device *
800 rman_get_device(struct resource *r)
801 {
802 	return (r->__r_i->r_dev);
803 }
804 
805 void
806 rman_set_device(struct resource *r, struct device *dev)
807 {
808 	r->__r_i->r_dev = dev;
809 }
810 
811 int
812 rman_is_region_manager(struct resource *r, struct rman *rm)
813 {
814 
815 	return (r->__r_i->r_rm == rm);
816 }
817 
818 /*
819  * Sysctl interface for scanning the resource lists.
820  *
821  * We take two input parameters; the index into the list of resource
822  * managers, and the resource offset into the list.
823  */
824 static int
825 sysctl_rman(SYSCTL_HANDLER_ARGS)
826 {
827 	int			*name = (int *)arg1;
828 	u_int			namelen = arg2;
829 	int			rman_idx, res_idx;
830 	struct rman		*rm;
831 	struct resource_i	*res;
832 	struct u_rman		urm;
833 	struct u_resource	ures;
834 	int			error;
835 
836 	if (namelen != 3)
837 		return (EINVAL);
838 
839 	if (bus_data_generation_check(name[0]))
840 		return (EINVAL);
841 	rman_idx = name[1];
842 	res_idx = name[2];
843 
844 	/*
845 	 * Find the indexed resource manager
846 	 */
847 	mtx_lock(&rman_mtx);
848 	TAILQ_FOREACH(rm, &rman_head, rm_link) {
849 		if (rman_idx-- == 0)
850 			break;
851 	}
852 	mtx_unlock(&rman_mtx);
853 	if (rm == NULL)
854 		return (ENOENT);
855 
856 	/*
857 	 * If the resource index is -1, we want details on the
858 	 * resource manager.
859 	 */
860 	if (res_idx == -1) {
861 		bzero(&urm, sizeof(urm));
862 		urm.rm_handle = (uintptr_t)rm;
863 		strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
864 		urm.rm_start = rm->rm_start;
865 		urm.rm_size = rm->rm_end - rm->rm_start + 1;
866 		urm.rm_type = rm->rm_type;
867 
868 		error = SYSCTL_OUT(req, &urm, sizeof(urm));
869 		return (error);
870 	}
871 
872 	/*
873 	 * Find the indexed resource and return it.
874 	 */
875 	mtx_lock(rm->rm_mtx);
876 	TAILQ_FOREACH(res, &rm->rm_list, r_link) {
877 		if (res_idx-- == 0) {
878 			bzero(&ures, sizeof(ures));
879 			ures.r_handle = (uintptr_t)res;
880 			ures.r_parent = (uintptr_t)res->r_rm;
881 			ures.r_device = (uintptr_t)res->r_dev;
882 			if (res->r_dev != NULL) {
883 				if (device_get_name(res->r_dev) != NULL) {
884 					snprintf(ures.r_devname, RM_TEXTLEN,
885 					    "%s%d",
886 					    device_get_name(res->r_dev),
887 					    device_get_unit(res->r_dev));
888 				} else {
889 					strlcpy(ures.r_devname, "nomatch",
890 					    RM_TEXTLEN);
891 				}
892 			} else {
893 				ures.r_devname[0] = '\0';
894 			}
895 			ures.r_start = res->r_start;
896 			ures.r_size = res->r_end - res->r_start + 1;
897 			ures.r_flags = res->r_flags;
898 
899 			mtx_unlock(rm->rm_mtx);
900 			error = SYSCTL_OUT(req, &ures, sizeof(ures));
901 			return (error);
902 		}
903 	}
904 	mtx_unlock(rm->rm_mtx);
905 	return (ENOENT);
906 }
907 
908 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
909     "kernel resource manager");
910