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
86 TAILQ_ENTRY(resource_i) r_link;
87 LIST_ENTRY(resource_i) r_sharelink;
88 LIST_HEAD(, resource_i) *r_sharehead;
89 rman_res_t r_start; /* index of the first entry in this resource */
90 rman_res_t r_end; /* index of the last entry (inclusive) */
92 void *r_virtual; /* virtual address of this resource */
93 void *r_irq_cookie; /* interrupt cookie for this (interrupt) resource */
94 device_t r_dev; /* device which has allocated this resource */
95 struct rman *r_rm; /* resource manager from whence this came */
96 int r_rid; /* optional rid for this resource. */
99 static int rman_debug = 0;
100 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RWTUN,
101 &rman_debug, 0, "rman debug");
103 #define DPRINTF(params) if (rman_debug) printf params
105 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
107 struct rman_head rman_head;
108 static struct mtx rman_mtx; /* mutex to protect rman_head */
109 static int int_rman_release_resource(struct rman *rm, struct resource_i *r);
111 static __inline struct resource_i *
112 int_alloc_resource(int malloc_flag)
114 struct resource_i *r;
116 r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
124 rman_init(struct rman *rm)
130 TAILQ_INIT(&rman_head);
131 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
134 if (rm->rm_start == 0 && rm->rm_end == 0)
136 if (rm->rm_type == RMAN_UNINIT)
138 if (rm->rm_type == RMAN_GAUGE)
139 panic("implement RMAN_GAUGE");
141 TAILQ_INIT(&rm->rm_list);
142 rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
143 if (rm->rm_mtx == NULL)
145 mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
148 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
149 mtx_unlock(&rman_mtx);
154 rman_manage_region(struct rman *rm, rman_res_t start, rman_res_t end)
156 struct resource_i *r, *s, *t;
159 DPRINTF(("rman_manage_region: <%s> request: start %#jx, end %#jx\n",
160 rm->rm_descr, start, end));
161 if (start < rm->rm_start || end > rm->rm_end)
163 r = int_alloc_resource(M_NOWAIT);
170 mtx_lock(rm->rm_mtx);
172 /* Skip entries before us. */
173 TAILQ_FOREACH(s, &rm->rm_list, r_link) {
176 if (s->r_end + 1 >= r->r_start)
180 /* If we ran off the end of the list, insert at the tail. */
182 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
184 /* Check for any overlap with the current region. */
185 if (r->r_start <= s->r_end && r->r_end >= s->r_start) {
190 /* Check for any overlap with the next region. */
191 t = TAILQ_NEXT(s, r_link);
192 if (t && r->r_start <= t->r_end && r->r_end >= t->r_start) {
198 * See if this region can be merged with the next region. If
199 * not, clear the pointer.
201 if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
204 /* See if we can merge with the current region. */
205 if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
206 /* Can we merge all 3 regions? */
209 TAILQ_REMOVE(&rm->rm_list, t, r_link);
216 } else if (t != NULL) {
217 /* Can we merge with just the next region? */
218 t->r_start = r->r_start;
220 } else if (s->r_end < r->r_start) {
221 TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link);
223 TAILQ_INSERT_BEFORE(s, r, r_link);
227 mtx_unlock(rm->rm_mtx);
232 rman_init_from_resource(struct rman *rm, struct resource *r)
236 if ((rv = rman_init(rm)) != 0)
238 return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
242 rman_fini(struct rman *rm)
244 struct resource_i *r;
246 mtx_lock(rm->rm_mtx);
247 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
248 if (r->r_flags & RF_ALLOCATED) {
249 mtx_unlock(rm->rm_mtx);
255 * There really should only be one of these if we are in this
256 * state and the code is working properly, but it can't hurt.
258 while (!TAILQ_EMPTY(&rm->rm_list)) {
259 r = TAILQ_FIRST(&rm->rm_list);
260 TAILQ_REMOVE(&rm->rm_list, r, r_link);
263 mtx_unlock(rm->rm_mtx);
265 TAILQ_REMOVE(&rman_head, rm, rm_link);
266 mtx_unlock(&rman_mtx);
267 mtx_destroy(rm->rm_mtx);
268 free(rm->rm_mtx, M_RMAN);
274 rman_first_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end)
276 struct resource_i *r;
278 mtx_lock(rm->rm_mtx);
279 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
280 if (!(r->r_flags & RF_ALLOCATED)) {
283 mtx_unlock(rm->rm_mtx);
287 mtx_unlock(rm->rm_mtx);
292 rman_last_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end)
294 struct resource_i *r;
296 mtx_lock(rm->rm_mtx);
297 TAILQ_FOREACH_REVERSE(r, &rm->rm_list, resource_head, r_link) {
298 if (!(r->r_flags & RF_ALLOCATED)) {
301 mtx_unlock(rm->rm_mtx);
305 mtx_unlock(rm->rm_mtx);
309 /* Shrink or extend one or both ends of an allocated resource. */
311 rman_adjust_resource(struct resource *rr, rman_res_t start, rman_res_t end)
313 struct resource_i *r, *s, *t, *new;
316 /* Not supported for shared resources. */
318 if (r->r_flags & RF_SHAREABLE)
322 * This does not support wholesale moving of a resource. At
323 * least part of the desired new range must overlap with the
326 if (end < r->r_start || r->r_end < start)
330 * Find the two resource regions immediately adjacent to the
331 * allocated resource.
334 mtx_lock(rm->rm_mtx);
336 TAILQ_FOREACH(s, &rm->rm_list, r_link) {
341 panic("resource not in list");
343 s = TAILQ_PREV(r, resource_head, r_link);
344 t = TAILQ_NEXT(r, r_link);
345 KASSERT(s == NULL || s->r_end + 1 == r->r_start,
346 ("prev resource mismatch"));
347 KASSERT(t == NULL || r->r_end + 1 == t->r_start,
348 ("next resource mismatch"));
351 * See if the changes are permitted. Shrinking is always allowed,
352 * but growing requires sufficient room in the adjacent region.
354 if (start < r->r_start && (s == NULL || (s->r_flags & RF_ALLOCATED) ||
355 s->r_start > start)) {
356 mtx_unlock(rm->rm_mtx);
359 if (end > r->r_end && (t == NULL || (t->r_flags & RF_ALLOCATED) ||
361 mtx_unlock(rm->rm_mtx);
366 * While holding the lock, grow either end of the resource as
367 * needed and shrink either end if the shrinking does not require
368 * allocating a new resource. We can safely drop the lock and then
369 * insert a new range to handle the shrinking case afterwards.
371 if (start < r->r_start ||
372 (start > r->r_start && s != NULL && !(s->r_flags & RF_ALLOCATED))) {
373 KASSERT(s->r_flags == 0, ("prev is busy"));
375 if (s->r_start == start) {
376 TAILQ_REMOVE(&rm->rm_list, s, r_link);
379 s->r_end = start - 1;
381 if (end > r->r_end ||
382 (end < r->r_end && t != NULL && !(t->r_flags & RF_ALLOCATED))) {
383 KASSERT(t->r_flags == 0, ("next is busy"));
385 if (t->r_end == end) {
386 TAILQ_REMOVE(&rm->rm_list, t, r_link);
389 t->r_start = end + 1;
391 mtx_unlock(rm->rm_mtx);
394 * Handle the shrinking cases that require allocating a new
395 * resource to hold the newly-free region. We have to recheck
396 * if we still need this new region after acquiring the lock.
398 if (start > r->r_start) {
399 new = int_alloc_resource(M_WAITOK);
400 new->r_start = r->r_start;
401 new->r_end = start - 1;
403 mtx_lock(rm->rm_mtx);
405 s = TAILQ_PREV(r, resource_head, r_link);
406 if (s != NULL && !(s->r_flags & RF_ALLOCATED)) {
407 s->r_end = start - 1;
410 TAILQ_INSERT_BEFORE(r, new, r_link);
411 mtx_unlock(rm->rm_mtx);
413 if (end < r->r_end) {
414 new = int_alloc_resource(M_WAITOK);
415 new->r_start = end + 1;
416 new->r_end = r->r_end;
418 mtx_lock(rm->rm_mtx);
420 t = TAILQ_NEXT(r, r_link);
421 if (t != NULL && !(t->r_flags & RF_ALLOCATED)) {
422 t->r_start = end + 1;
425 TAILQ_INSERT_AFTER(&rm->rm_list, r, new, r_link);
426 mtx_unlock(rm->rm_mtx);
431 #define SHARE_TYPE(f) (f & (RF_SHAREABLE | RF_PREFETCHABLE))
434 rman_reserve_resource_bound(struct rman *rm, rman_res_t start, rman_res_t end,
435 rman_res_t count, rman_res_t bound, u_int flags,
439 struct resource_i *r, *s, *rv;
440 rman_res_t rstart, rend, amask, bmask;
444 DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#jx, %#jx], "
445 "length %#jx, flags %x, device %s\n", rm->rm_descr, start, end,
447 dev == NULL ? "<null>" : device_get_nameunit(dev)));
448 KASSERT(count != 0, ("%s: attempted to allocate an empty range",
450 KASSERT((flags & RF_FIRSTSHARE) == 0,
451 ("invalid flags %#x", flags));
452 new_rflags = (flags & ~RF_FIRSTSHARE) | RF_ALLOCATED;
454 mtx_lock(rm->rm_mtx);
456 r = TAILQ_FIRST(&rm->rm_list);
458 DPRINTF(("NULL list head\n"));
460 DPRINTF(("rman_reserve_resource_bound: trying %#jx <%#jx,%#jx>\n",
461 r->r_end, start, count-1));
463 for (r = TAILQ_FIRST(&rm->rm_list);
464 r && r->r_end < start + count - 1;
465 r = TAILQ_NEXT(r, r_link)) {
467 DPRINTF(("rman_reserve_resource_bound: tried %#jx <%#jx,%#jx>\n",
468 r->r_end, start, count-1));
472 DPRINTF(("could not find a region\n"));
476 amask = (1ull << RF_ALIGNMENT(flags)) - 1;
477 KASSERT(start <= RM_MAX_END - amask,
478 ("start (%#jx) + amask (%#jx) would wrap around", start, amask));
480 /* If bound is 0, bmask will also be 0 */
481 bmask = ~(bound - 1);
483 * First try to find an acceptable totally-unshared region.
485 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
486 DPRINTF(("considering [%#jx, %#jx]\n", s->r_start, s->r_end));
488 * The resource list is sorted, so there is no point in
489 * searching further once r_start is too large.
491 if (s->r_start > end - (count - 1)) {
492 DPRINTF(("s->r_start (%#jx) + count - 1> end (%#jx)\n",
496 if (s->r_start > RM_MAX_END - amask) {
497 DPRINTF(("s->r_start (%#jx) + amask (%#jx) too large\n",
501 if (s->r_flags & RF_ALLOCATED) {
502 DPRINTF(("region is allocated\n"));
505 rstart = ummax(s->r_start, start);
507 * Try to find a region by adjusting to boundary and alignment
508 * until both conditions are satisfied. This is not an optimal
509 * algorithm, but in most cases it isn't really bad, either.
512 rstart = (rstart + amask) & ~amask;
513 if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
514 rstart += bound - (rstart & ~bmask);
515 } while ((rstart & amask) != 0 && rstart < end &&
517 rend = ummin(s->r_end, ummax(rstart + count - 1, end));
519 DPRINTF(("adjusted start exceeds end\n"));
522 DPRINTF(("truncated region: [%#jx, %#jx]; size %#jx (requested %#jx)\n",
523 rstart, rend, (rend - rstart + 1), count));
525 if ((rend - rstart) >= (count - 1)) {
526 DPRINTF(("candidate region: [%#jx, %#jx], size %#jx\n",
527 rstart, rend, (rend - rstart + 1)));
528 if ((s->r_end - s->r_start + 1) == count) {
529 DPRINTF(("candidate region is entire chunk\n"));
531 rv->r_flags = new_rflags;
537 * If s->r_start < rstart and
538 * s->r_end > rstart + count - 1, then
539 * we need to split the region into three pieces
540 * (the middle one will get returned to the user).
541 * Otherwise, we are allocating at either the
542 * beginning or the end of s, so we only need to
543 * split it in two. The first case requires
544 * two new allocations; the second requires but one.
546 rv = int_alloc_resource(M_NOWAIT);
549 rv->r_start = rstart;
550 rv->r_end = rstart + count - 1;
551 rv->r_flags = new_rflags;
555 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
556 DPRINTF(("splitting region in three parts: "
557 "[%#jx, %#jx]; [%#jx, %#jx]; [%#jx, %#jx]\n",
558 s->r_start, rv->r_start - 1,
559 rv->r_start, rv->r_end,
560 rv->r_end + 1, s->r_end));
562 * We are allocating in the middle.
564 r = int_alloc_resource(M_NOWAIT);
570 r->r_start = rv->r_end + 1;
572 r->r_flags = s->r_flags;
574 s->r_end = rv->r_start - 1;
575 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
577 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
579 } else if (s->r_start == rv->r_start) {
580 DPRINTF(("allocating from the beginning\n"));
582 * We are allocating at the beginning.
584 s->r_start = rv->r_end + 1;
585 TAILQ_INSERT_BEFORE(s, rv, r_link);
587 DPRINTF(("allocating at the end\n"));
589 * We are allocating at the end.
591 s->r_end = rv->r_start - 1;
592 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
600 * Now find an acceptable shared region, if the client's requirements
601 * allow sharing. By our implementation restriction, a candidate
602 * region must match exactly by both size and sharing type in order
603 * to be considered compatible with the client's request. (The
604 * former restriction could probably be lifted without too much
605 * additional work, but this does not seem warranted.)
607 DPRINTF(("no unshared regions found\n"));
608 if ((flags & RF_SHAREABLE) == 0)
611 for (s = r; s && s->r_end <= end; s = TAILQ_NEXT(s, r_link)) {
612 if (SHARE_TYPE(s->r_flags) == SHARE_TYPE(flags) &&
613 s->r_start >= start &&
614 (s->r_end - s->r_start + 1) == count &&
615 (s->r_start & amask) == 0 &&
616 ((s->r_start ^ s->r_end) & bmask) == 0) {
617 rv = int_alloc_resource(M_NOWAIT);
620 rv->r_start = s->r_start;
621 rv->r_end = s->r_end;
622 rv->r_flags = new_rflags;
625 if (s->r_sharehead == NULL) {
626 s->r_sharehead = malloc(sizeof *s->r_sharehead,
627 M_RMAN, M_NOWAIT | M_ZERO);
628 if (s->r_sharehead == NULL) {
633 LIST_INIT(s->r_sharehead);
634 LIST_INSERT_HEAD(s->r_sharehead, s,
636 s->r_flags |= RF_FIRSTSHARE;
638 rv->r_sharehead = s->r_sharehead;
639 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
644 * We couldn't find anything.
648 mtx_unlock(rm->rm_mtx);
649 return (rv == NULL ? NULL : &rv->r_r);
653 rman_reserve_resource(struct rman *rm, rman_res_t start, rman_res_t end,
654 rman_res_t count, u_int flags, device_t dev)
657 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
662 rman_activate_resource(struct resource *re)
664 struct resource_i *r;
669 mtx_lock(rm->rm_mtx);
670 r->r_flags |= RF_ACTIVE;
671 mtx_unlock(rm->rm_mtx);
676 rman_deactivate_resource(struct resource *r)
681 mtx_lock(rm->rm_mtx);
682 r->__r_i->r_flags &= ~RF_ACTIVE;
683 mtx_unlock(rm->rm_mtx);
688 int_rman_release_resource(struct rman *rm, struct resource_i *r)
690 struct resource_i *s, *t;
692 if (r->r_flags & RF_ACTIVE)
693 r->r_flags &= ~RF_ACTIVE;
696 * Check for a sharing list first. If there is one, then we don't
697 * have to think as hard.
699 if (r->r_sharehead) {
701 * If a sharing list exists, then we know there are at
704 * If we are in the main circleq, appoint someone else.
706 LIST_REMOVE(r, r_sharelink);
707 s = LIST_FIRST(r->r_sharehead);
708 if (r->r_flags & RF_FIRSTSHARE) {
709 s->r_flags |= RF_FIRSTSHARE;
710 TAILQ_INSERT_BEFORE(r, s, r_link);
711 TAILQ_REMOVE(&rm->rm_list, r, r_link);
715 * Make sure that the sharing list goes away completely
716 * if the resource is no longer being shared at all.
718 if (LIST_NEXT(s, r_sharelink) == NULL) {
719 free(s->r_sharehead, M_RMAN);
720 s->r_sharehead = NULL;
721 s->r_flags &= ~RF_FIRSTSHARE;
727 * Look at the adjacent resources in the list and see if our
728 * segment can be merged with any of them. If either of the
729 * resources is allocated or is not exactly adjacent then they
730 * cannot be merged with our segment.
732 s = TAILQ_PREV(r, resource_head, r_link);
733 if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
734 s->r_end + 1 != r->r_start))
736 t = TAILQ_NEXT(r, r_link);
737 if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
738 r->r_end + 1 != t->r_start))
741 if (s != NULL && t != NULL) {
743 * Merge all three segments.
746 TAILQ_REMOVE(&rm->rm_list, r, r_link);
747 TAILQ_REMOVE(&rm->rm_list, t, r_link);
749 } else if (s != NULL) {
751 * Merge previous segment with ours.
754 TAILQ_REMOVE(&rm->rm_list, r, r_link);
755 } else if (t != NULL) {
757 * Merge next segment with ours.
759 t->r_start = r->r_start;
760 TAILQ_REMOVE(&rm->rm_list, r, r_link);
763 * At this point, we know there is nothing we
764 * can potentially merge with, because on each
765 * side, there is either nothing there or what is
766 * there is still allocated. In that case, we don't
767 * want to remove r from the list; we simply want to
768 * change it to an unallocated region and return
769 * without freeing anything.
771 r->r_flags &= ~RF_ALLOCATED;
782 rman_release_resource(struct resource *re)
785 struct resource_i *r;
790 mtx_lock(rm->rm_mtx);
791 rv = int_rman_release_resource(rm, r);
792 mtx_unlock(rm->rm_mtx);
797 rman_make_alignment_flags(uint32_t size)
802 * Find the hightest bit set, and add one if more than one bit
803 * set. We're effectively computing the ceil(log2(size)) here.
805 for (i = 31; i > 0; i--)
808 if (~(1 << i) & size)
811 return(RF_ALIGNMENT_LOG2(i));
815 rman_set_start(struct resource *r, rman_res_t start)
818 r->__r_i->r_start = start;
822 rman_get_start(struct resource *r)
825 return (r->__r_i->r_start);
829 rman_set_end(struct resource *r, rman_res_t end)
832 r->__r_i->r_end = end;
836 rman_get_end(struct resource *r)
839 return (r->__r_i->r_end);
843 rman_get_size(struct resource *r)
846 return (r->__r_i->r_end - r->__r_i->r_start + 1);
850 rman_get_flags(struct resource *r)
853 return (r->__r_i->r_flags);
857 rman_set_virtual(struct resource *r, void *v)
860 r->__r_i->r_virtual = v;
864 rman_get_virtual(struct resource *r)
867 return (r->__r_i->r_virtual);
871 rman_set_irq_cookie(struct resource *r, void *c)
874 r->__r_i->r_irq_cookie = c;
878 rman_get_irq_cookie(struct resource *r)
881 return (r->__r_i->r_irq_cookie);
885 rman_set_bustag(struct resource *r, bus_space_tag_t t)
892 rman_get_bustag(struct resource *r)
895 return (r->r_bustag);
899 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
906 rman_get_bushandle(struct resource *r)
909 return (r->r_bushandle);
913 rman_set_mapping(struct resource *r, struct resource_map *map)
916 KASSERT(rman_get_size(r) == map->r_size,
917 ("rman_set_mapping: size mismatch"));
918 rman_set_bustag(r, map->r_bustag);
919 rman_set_bushandle(r, map->r_bushandle);
920 rman_set_virtual(r, map->r_vaddr);
924 rman_get_mapping(struct resource *r, struct resource_map *map)
927 map->r_bustag = rman_get_bustag(r);
928 map->r_bushandle = rman_get_bushandle(r);
929 map->r_size = rman_get_size(r);
930 map->r_vaddr = rman_get_virtual(r);
934 rman_set_rid(struct resource *r, int rid)
937 r->__r_i->r_rid = rid;
941 rman_get_rid(struct resource *r)
944 return (r->__r_i->r_rid);
948 rman_set_device(struct resource *r, device_t dev)
951 r->__r_i->r_dev = dev;
955 rman_get_device(struct resource *r)
958 return (r->__r_i->r_dev);
962 rman_is_region_manager(struct resource *r, struct rman *rm)
965 return (r->__r_i->r_rm == rm);
969 * Sysctl interface for scanning the resource lists.
971 * We take two input parameters; the index into the list of resource
972 * managers, and the resource offset into the list.
975 sysctl_rman(SYSCTL_HANDLER_ARGS)
977 int *name = (int *)arg1;
978 u_int namelen = arg2;
979 int rman_idx, res_idx;
981 struct resource_i *res;
982 struct resource_i *sres;
984 struct u_resource ures;
990 if (bus_data_generation_check(name[0]))
996 * Find the indexed resource manager
999 TAILQ_FOREACH(rm, &rman_head, rm_link) {
1000 if (rman_idx-- == 0)
1003 mtx_unlock(&rman_mtx);
1008 * If the resource index is -1, we want details on the
1011 if (res_idx == -1) {
1012 bzero(&urm, sizeof(urm));
1013 urm.rm_handle = (uintptr_t)rm;
1014 if (rm->rm_descr != NULL)
1015 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
1016 urm.rm_start = rm->rm_start;
1017 urm.rm_size = rm->rm_end - rm->rm_start + 1;
1018 urm.rm_type = rm->rm_type;
1020 error = SYSCTL_OUT(req, &urm, sizeof(urm));
1025 * Find the indexed resource and return it.
1027 mtx_lock(rm->rm_mtx);
1028 TAILQ_FOREACH(res, &rm->rm_list, r_link) {
1029 if (res->r_sharehead != NULL) {
1030 LIST_FOREACH(sres, res->r_sharehead, r_sharelink)
1031 if (res_idx-- == 0) {
1036 else if (res_idx-- == 0)
1039 mtx_unlock(rm->rm_mtx);
1043 bzero(&ures, sizeof(ures));
1044 ures.r_handle = (uintptr_t)res;
1045 ures.r_parent = (uintptr_t)res->r_rm;
1046 ures.r_device = (uintptr_t)res->r_dev;
1047 if (res->r_dev != NULL) {
1048 if (device_get_name(res->r_dev) != NULL) {
1049 snprintf(ures.r_devname, RM_TEXTLEN,
1051 device_get_name(res->r_dev),
1052 device_get_unit(res->r_dev));
1054 strlcpy(ures.r_devname, "nomatch",
1058 ures.r_devname[0] = '\0';
1060 ures.r_start = res->r_start;
1061 ures.r_size = res->r_end - res->r_start + 1;
1062 ures.r_flags = res->r_flags;
1064 mtx_unlock(rm->rm_mtx);
1065 error = SYSCTL_OUT(req, &ures, sizeof(ures));
1069 static SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD | CTLFLAG_MPSAFE,
1071 "kernel resource manager");
1075 dump_rman_header(struct rman *rm)
1080 db_printf("rman %p: %s (0x%jx-0x%jx full range)\n",
1081 rm, rm->rm_descr, (rman_res_t)rm->rm_start, (rman_res_t)rm->rm_end);
1085 dump_rman(struct rman *rm)
1087 struct resource_i *r;
1088 const char *devname;
1092 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
1093 if (r->r_dev != NULL) {
1094 devname = device_get_nameunit(r->r_dev);
1095 if (devname == NULL)
1096 devname = "nomatch";
1099 db_printf(" 0x%jx-0x%jx (RID=%d) ",
1100 r->r_start, r->r_end, r->r_rid);
1101 if (devname != NULL)
1102 db_printf("(%s)\n", devname);
1104 db_printf("----\n");
1110 DB_SHOW_COMMAND(rman, db_show_rman)
1114 dump_rman_header((struct rman *)addr);
1115 dump_rman((struct rman *)addr);
1119 DB_SHOW_COMMAND(rmans, db_show_rmans)
1123 TAILQ_FOREACH(rm, &rman_head, rm_link) {
1124 dump_rman_header(rm);
1128 DB_SHOW_ALL_COMMAND(rman, db_show_all_rman)
1132 TAILQ_FOREACH(rm, &rman_head, rm_link) {
1133 dump_rman_header(rm);
1137 DB_SHOW_ALIAS(allrman, db_show_all_rman);