2 * Copyright 1998 Massachusetts Institute of Technology
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
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
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
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
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
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/limits.h>
68 #include <sys/malloc.h>
69 #include <sys/mutex.h>
70 #include <sys/bus.h> /* XXX debugging */
71 #include <machine/bus.h>
73 #include <sys/sysctl.h>
80 * We use a linked list rather than a bitmap because we need to be able to
81 * represent potentially huge objects (like all of a processor's physical
82 * address space). That is also why the indices are defined to have type
83 * `unsigned long' -- that being the largest integral type in ISO C (1990).
84 * The 1999 version of C allows `long long'; we may need to switch to that
85 * at some point in the future, particularly if we want to support 36-bit
86 * addresses on IA32 hardware.
90 TAILQ_ENTRY(resource_i) r_link;
91 LIST_ENTRY(resource_i) r_sharelink;
92 LIST_HEAD(, resource_i) *r_sharehead;
93 u_long r_start; /* index of the first entry in this resource */
94 u_long r_end; /* index of the last entry (inclusive) */
96 void *r_virtual; /* virtual address of this resource */
97 struct device *r_dev; /* device which has allocated this resource */
98 struct rman *r_rm; /* resource manager from whence this came */
99 int r_rid; /* optional rid for this resource. */
102 static int rman_debug = 0;
103 TUNABLE_INT("debug.rman_debug", &rman_debug);
104 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
105 &rman_debug, 0, "rman debug");
107 #define DPRINTF(params) if (rman_debug) printf params
109 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
111 struct rman_head rman_head;
112 static struct mtx rman_mtx; /* mutex to protect rman_head */
113 static int int_rman_activate_resource(struct rman *rm, struct resource_i *r,
114 struct resource_i **whohas);
115 static int int_rman_deactivate_resource(struct resource_i *r);
116 static int int_rman_release_resource(struct rman *rm, struct resource_i *r);
118 static __inline struct resource_i *
119 int_alloc_resource(int malloc_flag)
121 struct resource_i *r;
123 r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
131 rman_init(struct rman *rm)
137 TAILQ_INIT(&rman_head);
138 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
141 if (rm->rm_start == 0 && rm->rm_end == 0)
143 if (rm->rm_type == RMAN_UNINIT)
145 if (rm->rm_type == RMAN_GAUGE)
146 panic("implement RMAN_GAUGE");
148 TAILQ_INIT(&rm->rm_list);
149 rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
150 if (rm->rm_mtx == NULL)
152 mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
155 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
156 mtx_unlock(&rman_mtx);
161 rman_manage_region(struct rman *rm, u_long start, u_long end)
163 struct resource_i *r, *s, *t;
166 DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
167 rm->rm_descr, start, end));
168 if (start < rm->rm_start || end > rm->rm_end)
170 r = int_alloc_resource(M_NOWAIT);
177 mtx_lock(rm->rm_mtx);
179 /* Skip entries before us. */
180 TAILQ_FOREACH(s, &rm->rm_list, r_link) {
181 if (s->r_end == ULONG_MAX)
183 if (s->r_end + 1 >= r->r_start)
187 /* If we ran off the end of the list, insert at the tail. */
189 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
191 /* Check for any overlap with the current region. */
192 if (r->r_start <= s->r_end && r->r_end >= s->r_start) {
197 /* Check for any overlap with the next region. */
198 t = TAILQ_NEXT(s, r_link);
199 if (t && r->r_start <= t->r_end && r->r_end >= t->r_start) {
205 * See if this region can be merged with the next region. If
206 * not, clear the pointer.
208 if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
211 /* See if we can merge with the current region. */
212 if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
213 /* Can we merge all 3 regions? */
216 TAILQ_REMOVE(&rm->rm_list, t, r_link);
223 } else if (t != NULL) {
224 /* Can we merge with just the next region? */
225 t->r_start = r->r_start;
227 } else if (s->r_end < r->r_start) {
228 TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link);
230 TAILQ_INSERT_BEFORE(s, r, r_link);
234 mtx_unlock(rm->rm_mtx);
239 rman_init_from_resource(struct rman *rm, struct resource *r)
243 if ((rv = rman_init(rm)) != 0)
245 return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
249 rman_fini(struct rman *rm)
251 struct resource_i *r;
253 mtx_lock(rm->rm_mtx);
254 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
255 if (r->r_flags & RF_ALLOCATED) {
256 mtx_unlock(rm->rm_mtx);
262 * There really should only be one of these if we are in this
263 * state and the code is working properly, but it can't hurt.
265 while (!TAILQ_EMPTY(&rm->rm_list)) {
266 r = TAILQ_FIRST(&rm->rm_list);
267 TAILQ_REMOVE(&rm->rm_list, r, r_link);
270 mtx_unlock(rm->rm_mtx);
272 TAILQ_REMOVE(&rman_head, rm, rm_link);
273 mtx_unlock(&rman_mtx);
274 mtx_destroy(rm->rm_mtx);
275 free(rm->rm_mtx, M_RMAN);
281 rman_first_free_region(struct rman *rm, u_long *start, u_long *end)
283 struct resource_i *r;
285 mtx_lock(rm->rm_mtx);
286 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
287 if (!(r->r_flags & RF_ALLOCATED)) {
290 mtx_unlock(rm->rm_mtx);
294 mtx_unlock(rm->rm_mtx);
299 rman_last_free_region(struct rman *rm, u_long *start, u_long *end)
301 struct resource_i *r;
303 mtx_lock(rm->rm_mtx);
304 TAILQ_FOREACH_REVERSE(r, &rm->rm_list, resource_head, r_link) {
305 if (!(r->r_flags & RF_ALLOCATED)) {
308 mtx_unlock(rm->rm_mtx);
312 mtx_unlock(rm->rm_mtx);
316 /* Shrink or extend one or both ends of an allocated resource. */
318 rman_adjust_resource(struct resource *rr, u_long start, u_long end)
320 struct resource_i *r, *s, *t, *new;
323 /* Not supported for shared resources. */
325 if (r->r_flags & (RF_TIMESHARE | RF_SHAREABLE))
329 * This does not support wholesale moving of a resource. At
330 * least part of the desired new range must overlap with the
333 if (end < r->r_start || r->r_end < start)
337 * Find the two resource regions immediately adjacent to the
338 * allocated resource.
341 mtx_lock(rm->rm_mtx);
343 TAILQ_FOREACH(s, &rm->rm_list, r_link) {
348 panic("resource not in list");
350 s = TAILQ_PREV(r, resource_head, r_link);
351 t = TAILQ_NEXT(r, r_link);
352 KASSERT(s == NULL || s->r_end + 1 == r->r_start,
353 ("prev resource mismatch"));
354 KASSERT(t == NULL || r->r_end + 1 == t->r_start,
355 ("next resource mismatch"));
358 * See if the changes are permitted. Shrinking is always allowed,
359 * but growing requires sufficient room in the adjacent region.
361 if (start < r->r_start && (s == NULL || (s->r_flags & RF_ALLOCATED) ||
362 s->r_start > start)) {
363 mtx_unlock(rm->rm_mtx);
366 if (end > r->r_end && (t == NULL || (t->r_flags & RF_ALLOCATED) ||
368 mtx_unlock(rm->rm_mtx);
373 * While holding the lock, grow either end of the resource as
374 * needed and shrink either end if the shrinking does not require
375 * allocating a new resource. We can safely drop the lock and then
376 * insert a new range to handle the shrinking case afterwards.
378 if (start < r->r_start ||
379 (start > r->r_start && s != NULL && !(s->r_flags & RF_ALLOCATED))) {
380 KASSERT(s->r_flags == 0, ("prev is busy"));
382 if (s->r_start == start) {
383 TAILQ_REMOVE(&rm->rm_list, s, r_link);
386 s->r_end = start - 1;
388 if (end > r->r_end ||
389 (end < r->r_end && t != NULL && !(t->r_flags & RF_ALLOCATED))) {
390 KASSERT(t->r_flags == 0, ("next is busy"));
392 if (t->r_end == end) {
393 TAILQ_REMOVE(&rm->rm_list, t, r_link);
396 t->r_start = end + 1;
398 mtx_unlock(rm->rm_mtx);
401 * Handle the shrinking cases that require allocating a new
402 * resource to hold the newly-free region. We have to recheck
403 * if we still need this new region after acquiring the lock.
405 if (start > r->r_start) {
406 new = int_alloc_resource(M_WAITOK);
407 new->r_start = r->r_start;
408 new->r_end = start - 1;
410 mtx_lock(rm->rm_mtx);
412 s = TAILQ_PREV(r, resource_head, r_link);
413 if (s != NULL && !(s->r_flags & RF_ALLOCATED)) {
414 s->r_end = start - 1;
417 TAILQ_INSERT_BEFORE(r, new, r_link);
418 mtx_unlock(rm->rm_mtx);
420 if (end < r->r_end) {
421 new = int_alloc_resource(M_WAITOK);
422 new->r_start = end + 1;
423 new->r_end = r->r_end;
425 mtx_lock(rm->rm_mtx);
427 t = TAILQ_NEXT(r, r_link);
428 if (t != NULL && !(t->r_flags & RF_ALLOCATED)) {
429 t->r_start = end + 1;
432 TAILQ_INSERT_AFTER(&rm->rm_list, r, new, r_link);
433 mtx_unlock(rm->rm_mtx);
439 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
440 u_long count, u_long bound, u_int flags,
444 struct resource_i *r, *s, *rv;
445 u_long rstart, rend, amask, bmask;
449 DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
450 "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
452 dev == NULL ? "<null>" : device_get_nameunit(dev)));
453 want_activate = (flags & RF_ACTIVE);
456 mtx_lock(rm->rm_mtx);
458 for (r = TAILQ_FIRST(&rm->rm_list);
459 r && r->r_end < start;
460 r = TAILQ_NEXT(r, r_link))
464 DPRINTF(("could not find a region\n"));
468 amask = (1ul << RF_ALIGNMENT(flags)) - 1;
469 /* If bound is 0, bmask will also be 0 */
470 bmask = ~(bound - 1);
472 * First try to find an acceptable totally-unshared region.
474 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
475 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
476 if (s->r_start + count - 1 > end) {
477 DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
481 if (s->r_flags & RF_ALLOCATED) {
482 DPRINTF(("region is allocated\n"));
485 rstart = ulmax(s->r_start, start);
487 * Try to find a region by adjusting to boundary and alignment
488 * until both conditions are satisfied. This is not an optimal
489 * algorithm, but in most cases it isn't really bad, either.
492 rstart = (rstart + amask) & ~amask;
493 if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
494 rstart += bound - (rstart & ~bmask);
495 } while ((rstart & amask) != 0 && rstart < end &&
497 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
499 DPRINTF(("adjusted start exceeds end\n"));
502 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
503 rstart, rend, (rend - rstart + 1), count));
505 if ((rend - rstart + 1) >= count) {
506 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
507 rstart, rend, (rend - rstart + 1)));
508 if ((s->r_end - s->r_start + 1) == count) {
509 DPRINTF(("candidate region is entire chunk\n"));
511 rv->r_flags |= RF_ALLOCATED | flags;
517 * If s->r_start < rstart and
518 * s->r_end > rstart + count - 1, then
519 * we need to split the region into three pieces
520 * (the middle one will get returned to the user).
521 * Otherwise, we are allocating at either the
522 * beginning or the end of s, so we only need to
523 * split it in two. The first case requires
524 * two new allocations; the second requires but one.
526 rv = int_alloc_resource(M_NOWAIT);
529 rv->r_start = rstart;
530 rv->r_end = rstart + count - 1;
531 rv->r_flags = flags | RF_ALLOCATED;
535 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
536 DPRINTF(("splitting region in three parts: "
537 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
538 s->r_start, rv->r_start - 1,
539 rv->r_start, rv->r_end,
540 rv->r_end + 1, s->r_end));
542 * We are allocating in the middle.
544 r = int_alloc_resource(M_NOWAIT);
550 r->r_start = rv->r_end + 1;
552 r->r_flags = s->r_flags;
554 s->r_end = rv->r_start - 1;
555 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
557 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
559 } else if (s->r_start == rv->r_start) {
560 DPRINTF(("allocating from the beginning\n"));
562 * We are allocating at the beginning.
564 s->r_start = rv->r_end + 1;
565 TAILQ_INSERT_BEFORE(s, rv, r_link);
567 DPRINTF(("allocating at the end\n"));
569 * We are allocating at the end.
571 s->r_end = rv->r_start - 1;
572 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
580 * Now find an acceptable shared region, if the client's requirements
581 * allow sharing. By our implementation restriction, a candidate
582 * region must match exactly by both size and sharing type in order
583 * to be considered compatible with the client's request. (The
584 * former restriction could probably be lifted without too much
585 * additional work, but this does not seem warranted.)
587 DPRINTF(("no unshared regions found\n"));
588 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
591 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
592 if (s->r_start > end)
594 if ((s->r_flags & flags) != flags)
596 rstart = ulmax(s->r_start, start);
597 rend = ulmin(s->r_end, ulmax(start + count - 1, end));
598 if (s->r_start >= start && s->r_end <= end
599 && (s->r_end - s->r_start + 1) == count &&
600 (s->r_start & amask) == 0 &&
601 ((s->r_start ^ s->r_end) & bmask) == 0) {
602 rv = int_alloc_resource(M_NOWAIT);
605 rv->r_start = s->r_start;
606 rv->r_end = s->r_end;
607 rv->r_flags = s->r_flags &
608 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
611 if (s->r_sharehead == NULL) {
612 s->r_sharehead = malloc(sizeof *s->r_sharehead,
613 M_RMAN, M_NOWAIT | M_ZERO);
614 if (s->r_sharehead == NULL) {
619 LIST_INIT(s->r_sharehead);
620 LIST_INSERT_HEAD(s->r_sharehead, s,
622 s->r_flags |= RF_FIRSTSHARE;
624 rv->r_sharehead = s->r_sharehead;
625 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
631 * We couldn't find anything.
635 * If the user specified RF_ACTIVE in the initial flags,
636 * which is reflected in `want_activate', we attempt to atomically
637 * activate the resource. If this fails, we release the resource
638 * and indicate overall failure. (This behavior probably doesn't
639 * make sense for RF_TIMESHARE-type resources.)
641 if (rv && want_activate) {
642 struct resource_i *whohas;
643 if (int_rman_activate_resource(rm, rv, &whohas)) {
644 int_rman_release_resource(rm, rv);
649 mtx_unlock(rm->rm_mtx);
650 return (rv == NULL ? NULL : &rv->r_r);
654 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
655 u_int flags, struct device *dev)
658 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
663 int_rman_activate_resource(struct rman *rm, struct resource_i *r,
664 struct resource_i **whohas)
666 struct resource_i *s;
670 * If we are not timesharing, then there is nothing much to do.
671 * If we already have the resource, then there is nothing at all to do.
672 * If we are not on a sharing list with anybody else, then there is
675 if ((r->r_flags & RF_TIMESHARE) == 0
676 || (r->r_flags & RF_ACTIVE) != 0
677 || r->r_sharehead == NULL) {
678 r->r_flags |= RF_ACTIVE;
683 for (s = LIST_FIRST(r->r_sharehead); s && ok;
684 s = LIST_NEXT(s, r_sharelink)) {
685 if ((s->r_flags & RF_ACTIVE) != 0) {
691 r->r_flags |= RF_ACTIVE;
698 rman_activate_resource(struct resource *re)
701 struct resource_i *r, *whohas;
706 mtx_lock(rm->rm_mtx);
707 rv = int_rman_activate_resource(rm, r, &whohas);
708 mtx_unlock(rm->rm_mtx);
713 rman_await_resource(struct resource *re, int pri, int timo)
716 struct resource_i *r, *whohas;
721 mtx_lock(rm->rm_mtx);
723 rv = int_rman_activate_resource(rm, r, &whohas);
725 return (rv); /* returns with mutex held */
727 if (r->r_sharehead == NULL)
728 panic("rman_await_resource");
729 whohas->r_flags |= RF_WANTED;
730 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
732 mtx_unlock(rm->rm_mtx);
739 int_rman_deactivate_resource(struct resource_i *r)
742 r->r_flags &= ~RF_ACTIVE;
743 if (r->r_flags & RF_WANTED) {
744 r->r_flags &= ~RF_WANTED;
745 wakeup(r->r_sharehead);
751 rman_deactivate_resource(struct resource *r)
756 mtx_lock(rm->rm_mtx);
757 int_rman_deactivate_resource(r->__r_i);
758 mtx_unlock(rm->rm_mtx);
763 int_rman_release_resource(struct rman *rm, struct resource_i *r)
765 struct resource_i *s, *t;
767 if (r->r_flags & RF_ACTIVE)
768 int_rman_deactivate_resource(r);
771 * Check for a sharing list first. If there is one, then we don't
772 * have to think as hard.
774 if (r->r_sharehead) {
776 * If a sharing list exists, then we know there are at
779 * If we are in the main circleq, appoint someone else.
781 LIST_REMOVE(r, r_sharelink);
782 s = LIST_FIRST(r->r_sharehead);
783 if (r->r_flags & RF_FIRSTSHARE) {
784 s->r_flags |= RF_FIRSTSHARE;
785 TAILQ_INSERT_BEFORE(r, s, r_link);
786 TAILQ_REMOVE(&rm->rm_list, r, r_link);
790 * Make sure that the sharing list goes away completely
791 * if the resource is no longer being shared at all.
793 if (LIST_NEXT(s, r_sharelink) == NULL) {
794 free(s->r_sharehead, M_RMAN);
795 s->r_sharehead = NULL;
796 s->r_flags &= ~RF_FIRSTSHARE;
802 * Look at the adjacent resources in the list and see if our
803 * segment can be merged with any of them. If either of the
804 * resources is allocated or is not exactly adjacent then they
805 * cannot be merged with our segment.
807 s = TAILQ_PREV(r, resource_head, r_link);
808 if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
809 s->r_end + 1 != r->r_start))
811 t = TAILQ_NEXT(r, r_link);
812 if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
813 r->r_end + 1 != t->r_start))
816 if (s != NULL && t != NULL) {
818 * Merge all three segments.
821 TAILQ_REMOVE(&rm->rm_list, r, r_link);
822 TAILQ_REMOVE(&rm->rm_list, t, r_link);
824 } else if (s != NULL) {
826 * Merge previous segment with ours.
829 TAILQ_REMOVE(&rm->rm_list, r, r_link);
830 } else if (t != NULL) {
832 * Merge next segment with ours.
834 t->r_start = r->r_start;
835 TAILQ_REMOVE(&rm->rm_list, r, r_link);
838 * At this point, we know there is nothing we
839 * can potentially merge with, because on each
840 * side, there is either nothing there or what is
841 * there is still allocated. In that case, we don't
842 * want to remove r from the list; we simply want to
843 * change it to an unallocated region and return
844 * without freeing anything.
846 r->r_flags &= ~RF_ALLOCATED;
857 rman_release_resource(struct resource *re)
860 struct resource_i *r;
865 mtx_lock(rm->rm_mtx);
866 rv = int_rman_release_resource(rm, r);
867 mtx_unlock(rm->rm_mtx);
872 rman_make_alignment_flags(uint32_t size)
877 * Find the hightest bit set, and add one if more than one bit
878 * set. We're effectively computing the ceil(log2(size)) here.
880 for (i = 31; i > 0; i--)
883 if (~(1 << i) & size)
886 return(RF_ALIGNMENT_LOG2(i));
890 rman_set_start(struct resource *r, u_long start)
892 r->__r_i->r_start = start;
896 rman_get_start(struct resource *r)
898 return (r->__r_i->r_start);
902 rman_set_end(struct resource *r, u_long end)
904 r->__r_i->r_end = end;
908 rman_get_end(struct resource *r)
910 return (r->__r_i->r_end);
914 rman_get_size(struct resource *r)
916 return (r->__r_i->r_end - r->__r_i->r_start + 1);
920 rman_get_flags(struct resource *r)
922 return (r->__r_i->r_flags);
926 rman_set_virtual(struct resource *r, void *v)
928 r->__r_i->r_virtual = v;
932 rman_get_virtual(struct resource *r)
934 return (r->__r_i->r_virtual);
938 rman_set_bustag(struct resource *r, bus_space_tag_t t)
944 rman_get_bustag(struct resource *r)
946 return (r->r_bustag);
950 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
956 rman_get_bushandle(struct resource *r)
958 return (r->r_bushandle);
962 rman_set_rid(struct resource *r, int rid)
964 r->__r_i->r_rid = rid;
968 rman_get_rid(struct resource *r)
970 return (r->__r_i->r_rid);
974 rman_set_device(struct resource *r, struct device *dev)
976 r->__r_i->r_dev = dev;
980 rman_get_device(struct resource *r)
982 return (r->__r_i->r_dev);
986 rman_is_region_manager(struct resource *r, struct rman *rm)
989 return (r->__r_i->r_rm == rm);
993 * Sysctl interface for scanning the resource lists.
995 * We take two input parameters; the index into the list of resource
996 * managers, and the resource offset into the list.
999 sysctl_rman(SYSCTL_HANDLER_ARGS)
1001 int *name = (int *)arg1;
1002 u_int namelen = arg2;
1003 int rman_idx, res_idx;
1005 struct resource_i *res;
1006 struct resource_i *sres;
1008 struct u_resource ures;
1014 if (bus_data_generation_check(name[0]))
1020 * Find the indexed resource manager
1022 mtx_lock(&rman_mtx);
1023 TAILQ_FOREACH(rm, &rman_head, rm_link) {
1024 if (rman_idx-- == 0)
1027 mtx_unlock(&rman_mtx);
1032 * If the resource index is -1, we want details on the
1035 if (res_idx == -1) {
1036 bzero(&urm, sizeof(urm));
1037 urm.rm_handle = (uintptr_t)rm;
1038 if (rm->rm_descr != NULL)
1039 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
1040 urm.rm_start = rm->rm_start;
1041 urm.rm_size = rm->rm_end - rm->rm_start + 1;
1042 urm.rm_type = rm->rm_type;
1044 error = SYSCTL_OUT(req, &urm, sizeof(urm));
1049 * Find the indexed resource and return it.
1051 mtx_lock(rm->rm_mtx);
1052 TAILQ_FOREACH(res, &rm->rm_list, r_link) {
1053 if (res->r_sharehead != NULL) {
1054 LIST_FOREACH(sres, res->r_sharehead, r_sharelink)
1055 if (res_idx-- == 0) {
1060 else if (res_idx-- == 0)
1063 mtx_unlock(rm->rm_mtx);
1067 bzero(&ures, sizeof(ures));
1068 ures.r_handle = (uintptr_t)res;
1069 ures.r_parent = (uintptr_t)res->r_rm;
1070 ures.r_device = (uintptr_t)res->r_dev;
1071 if (res->r_dev != NULL) {
1072 if (device_get_name(res->r_dev) != NULL) {
1073 snprintf(ures.r_devname, RM_TEXTLEN,
1075 device_get_name(res->r_dev),
1076 device_get_unit(res->r_dev));
1078 strlcpy(ures.r_devname, "nomatch",
1082 ures.r_devname[0] = '\0';
1084 ures.r_start = res->r_start;
1085 ures.r_size = res->r_end - res->r_start + 1;
1086 ures.r_flags = res->r_flags;
1088 mtx_unlock(rm->rm_mtx);
1089 error = SYSCTL_OUT(req, &ures, sizeof(ures));
1093 static SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
1094 "kernel resource manager");
1098 dump_rman_header(struct rman *rm)
1103 db_printf("rman %p: %s (0x%lx-0x%lx full range)\n",
1104 rm, rm->rm_descr, rm->rm_start, rm->rm_end);
1108 dump_rman(struct rman *rm)
1110 struct resource_i *r;
1111 const char *devname;
1115 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
1116 if (r->r_dev != NULL) {
1117 devname = device_get_nameunit(r->r_dev);
1118 if (devname == NULL)
1119 devname = "nomatch";
1122 db_printf(" 0x%lx-0x%lx ", r->r_start, r->r_end);
1123 if (devname != NULL)
1124 db_printf("(%s)\n", devname);
1126 db_printf("----\n");
1132 DB_SHOW_COMMAND(rman, db_show_rman)
1136 dump_rman_header((struct rman *)addr);
1137 dump_rman((struct rman *)addr);
1141 DB_SHOW_COMMAND(rmans, db_show_rmans)
1145 TAILQ_FOREACH(rm, &rman_head, rm_link) {
1146 dump_rman_header(rm);
1150 DB_SHOW_ALL_COMMAND(rman, db_show_all_rman)
1154 TAILQ_FOREACH(rm, &rman_head, rm_link) {
1155 dump_rman_header(rm);
1159 DB_SHOW_ALIAS(allrman, db_show_all_rman);