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
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/mutex.h>
67 #include <sys/bus.h> /* XXX debugging */
68 #include <machine/bus.h>
70 #include <sys/sysctl.h>
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.
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) */
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. */
96 TUNABLE_INT("debug.rman_debug", &rman_debug);
97 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
98 &rman_debug, 0, "rman debug");
100 #define DPRINTF(params) if (rman_debug) printf params
102 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
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);
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_type == RMAN_UNINIT)
136 if (rm->rm_type == RMAN_GAUGE)
137 panic("implement RMAN_GAUGE");
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)
143 mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
146 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
147 mtx_unlock(&rman_mtx);
152 * NB: this interface is not robust against programming errors which
153 * add multiple copies of the same region.
156 rman_manage_region(struct rman *rm, u_long start, u_long end)
158 struct resource_i *r, *s;
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);
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))
176 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
178 TAILQ_INSERT_BEFORE(s, r, r_link);
181 mtx_unlock(rm->rm_mtx);
186 rman_init_from_resource(struct rman *rm, struct resource *r)
190 if ((rv = rman_init(rm)) != 0)
192 return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
196 rman_fini(struct rman *rm)
198 struct resource_i *r;
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);
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.
212 while (!TAILQ_EMPTY(&rm->rm_list)) {
213 r = TAILQ_FIRST(&rm->rm_list);
214 TAILQ_REMOVE(&rm->rm_list, r, r_link);
217 mtx_unlock(rm->rm_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);
228 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
229 u_long count, u_long bound, u_int flags,
233 struct resource_i *r, *s, *rv;
234 u_long rstart, rend, amask, bmask;
238 DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
239 "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
241 dev == NULL ? "<null>" : device_get_nameunit(dev)));
242 want_activate = (flags & RF_ACTIVE);
245 mtx_lock(rm->rm_mtx);
247 for (r = TAILQ_FIRST(&rm->rm_list);
248 r && r->r_end < start;
249 r = TAILQ_NEXT(r, r_link))
253 DPRINTF(("could not find a region\n"));
257 amask = (1ul << RF_ALIGNMENT(flags)) - 1;
258 /* If bound is 0, bmask will also be 0 */
259 bmask = ~(bound - 1);
261 * First try to find an acceptable totally-unshared region.
263 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
264 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
265 if (s->r_start + count - 1 > end) {
266 DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
270 if (s->r_flags & RF_ALLOCATED) {
271 DPRINTF(("region is allocated\n"));
274 rstart = ulmax(s->r_start, start);
276 * Try to find a region by adjusting to boundary and alignment
277 * until both conditions are satisfied. This is not an optimal
278 * algorithm, but in most cases it isn't really bad, either.
281 rstart = (rstart + amask) & ~amask;
282 if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
283 rstart += bound - (rstart & ~bmask);
284 } while ((rstart & amask) != 0 && rstart < end &&
286 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
288 DPRINTF(("adjusted start exceeds end\n"));
291 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
292 rstart, rend, (rend - rstart + 1), count));
294 if ((rend - rstart + 1) >= count) {
295 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
296 rstart, rend, (rend - rstart + 1)));
297 if ((s->r_end - s->r_start + 1) == count) {
298 DPRINTF(("candidate region is entire chunk\n"));
300 rv->r_flags |= RF_ALLOCATED | flags;
306 * If s->r_start < rstart and
307 * s->r_end > rstart + count - 1, then
308 * we need to split the region into three pieces
309 * (the middle one will get returned to the user).
310 * Otherwise, we are allocating at either the
311 * beginning or the end of s, so we only need to
312 * split it in two. The first case requires
313 * two new allocations; the second requires but one.
315 rv = int_alloc_resource(M_NOWAIT);
318 rv->r_start = rstart;
319 rv->r_end = rstart + count - 1;
320 rv->r_flags = flags | RF_ALLOCATED;
324 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
325 DPRINTF(("splitting region in three parts: "
326 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
327 s->r_start, rv->r_start - 1,
328 rv->r_start, rv->r_end,
329 rv->r_end + 1, s->r_end));
331 * We are allocating in the middle.
333 r = int_alloc_resource(M_NOWAIT);
339 r->r_start = rv->r_end + 1;
341 r->r_flags = s->r_flags;
343 s->r_end = rv->r_start - 1;
344 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
346 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
348 } else if (s->r_start == rv->r_start) {
349 DPRINTF(("allocating from the beginning\n"));
351 * We are allocating at the beginning.
353 s->r_start = rv->r_end + 1;
354 TAILQ_INSERT_BEFORE(s, rv, r_link);
356 DPRINTF(("allocating at the end\n"));
358 * We are allocating at the end.
360 s->r_end = rv->r_start - 1;
361 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
369 * Now find an acceptable shared region, if the client's requirements
370 * allow sharing. By our implementation restriction, a candidate
371 * region must match exactly by both size and sharing type in order
372 * to be considered compatible with the client's request. (The
373 * former restriction could probably be lifted without too much
374 * additional work, but this does not seem warranted.)
376 DPRINTF(("no unshared regions found\n"));
377 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
380 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
381 if (s->r_start > end)
383 if ((s->r_flags & flags) != flags)
385 rstart = ulmax(s->r_start, start);
386 rend = ulmin(s->r_end, ulmax(start + count - 1, end));
387 if (s->r_start >= start && s->r_end <= end
388 && (s->r_end - s->r_start + 1) == count &&
389 (s->r_start & amask) == 0 &&
390 ((s->r_start ^ s->r_end) & bmask) == 0) {
391 rv = int_alloc_resource(M_NOWAIT);
394 rv->r_start = s->r_start;
395 rv->r_end = s->r_end;
396 rv->r_flags = s->r_flags &
397 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
400 if (s->r_sharehead == NULL) {
401 s->r_sharehead = malloc(sizeof *s->r_sharehead,
402 M_RMAN, M_NOWAIT | M_ZERO);
403 if (s->r_sharehead == NULL) {
408 LIST_INIT(s->r_sharehead);
409 LIST_INSERT_HEAD(s->r_sharehead, s,
411 s->r_flags |= RF_FIRSTSHARE;
413 rv->r_sharehead = s->r_sharehead;
414 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
420 * We couldn't find anything.
424 * If the user specified RF_ACTIVE in the initial flags,
425 * which is reflected in `want_activate', we attempt to atomically
426 * activate the resource. If this fails, we release the resource
427 * and indicate overall failure. (This behavior probably doesn't
428 * make sense for RF_TIMESHARE-type resources.)
430 if (rv && want_activate) {
431 struct resource_i *whohas;
432 if (int_rman_activate_resource(rm, rv, &whohas)) {
433 int_rman_release_resource(rm, rv);
438 mtx_unlock(rm->rm_mtx);
439 return (rv == NULL ? NULL : &rv->r_r);
443 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
444 u_int flags, struct device *dev)
447 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
452 int_rman_activate_resource(struct rman *rm, struct resource_i *r,
453 struct resource_i **whohas)
455 struct resource_i *s;
459 * If we are not timesharing, then there is nothing much to do.
460 * If we already have the resource, then there is nothing at all to do.
461 * If we are not on a sharing list with anybody else, then there is
464 if ((r->r_flags & RF_TIMESHARE) == 0
465 || (r->r_flags & RF_ACTIVE) != 0
466 || r->r_sharehead == NULL) {
467 r->r_flags |= RF_ACTIVE;
472 for (s = LIST_FIRST(r->r_sharehead); s && ok;
473 s = LIST_NEXT(s, r_sharelink)) {
474 if ((s->r_flags & RF_ACTIVE) != 0) {
480 r->r_flags |= RF_ACTIVE;
487 rman_activate_resource(struct resource *re)
490 struct resource_i *r, *whohas;
495 mtx_lock(rm->rm_mtx);
496 rv = int_rman_activate_resource(rm, r, &whohas);
497 mtx_unlock(rm->rm_mtx);
502 rman_await_resource(struct resource *re, int pri, int timo)
505 struct resource_i *r, *whohas;
510 mtx_lock(rm->rm_mtx);
512 rv = int_rman_activate_resource(rm, r, &whohas);
514 return (rv); /* returns with mutex held */
516 if (r->r_sharehead == NULL)
517 panic("rman_await_resource");
518 whohas->r_flags |= RF_WANTED;
519 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
521 mtx_unlock(rm->rm_mtx);
528 int_rman_deactivate_resource(struct resource_i *r)
531 r->r_flags &= ~RF_ACTIVE;
532 if (r->r_flags & RF_WANTED) {
533 r->r_flags &= ~RF_WANTED;
534 wakeup(r->r_sharehead);
540 rman_deactivate_resource(struct resource *r)
545 mtx_lock(rm->rm_mtx);
546 int_rman_deactivate_resource(r->__r_i);
547 mtx_unlock(rm->rm_mtx);
552 int_rman_release_resource(struct rman *rm, struct resource_i *r)
554 struct resource_i *s, *t;
556 if (r->r_flags & RF_ACTIVE)
557 int_rman_deactivate_resource(r);
560 * Check for a sharing list first. If there is one, then we don't
561 * have to think as hard.
563 if (r->r_sharehead) {
565 * If a sharing list exists, then we know there are at
568 * If we are in the main circleq, appoint someone else.
570 LIST_REMOVE(r, r_sharelink);
571 s = LIST_FIRST(r->r_sharehead);
572 if (r->r_flags & RF_FIRSTSHARE) {
573 s->r_flags |= RF_FIRSTSHARE;
574 TAILQ_INSERT_BEFORE(r, s, r_link);
575 TAILQ_REMOVE(&rm->rm_list, r, r_link);
579 * Make sure that the sharing list goes away completely
580 * if the resource is no longer being shared at all.
582 if (LIST_NEXT(s, r_sharelink) == NULL) {
583 free(s->r_sharehead, M_RMAN);
584 s->r_sharehead = NULL;
585 s->r_flags &= ~RF_FIRSTSHARE;
591 * Look at the adjacent resources in the list and see if our
592 * segment can be merged with any of them. If either of the
593 * resources is allocated or is not exactly adjacent then they
594 * cannot be merged with our segment.
596 s = TAILQ_PREV(r, resource_head, r_link);
597 if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
598 s->r_end + 1 != r->r_start))
600 t = TAILQ_NEXT(r, r_link);
601 if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
602 r->r_end + 1 != t->r_start))
605 if (s != NULL && t != NULL) {
607 * Merge all three segments.
610 TAILQ_REMOVE(&rm->rm_list, r, r_link);
611 TAILQ_REMOVE(&rm->rm_list, t, r_link);
613 } else if (s != NULL) {
615 * Merge previous segment with ours.
618 TAILQ_REMOVE(&rm->rm_list, r, r_link);
619 } else if (t != NULL) {
621 * Merge next segment with ours.
623 t->r_start = r->r_start;
624 TAILQ_REMOVE(&rm->rm_list, r, r_link);
627 * At this point, we know there is nothing we
628 * can potentially merge with, because on each
629 * side, there is either nothing there or what is
630 * there is still allocated. In that case, we don't
631 * want to remove r from the list; we simply want to
632 * change it to an unallocated region and return
633 * without freeing anything.
635 r->r_flags &= ~RF_ALLOCATED;
645 rman_release_resource(struct resource *re)
648 struct resource_i *r;
653 mtx_lock(rm->rm_mtx);
654 rv = int_rman_release_resource(rm, r);
655 mtx_unlock(rm->rm_mtx);
660 rman_make_alignment_flags(uint32_t size)
665 * Find the hightest bit set, and add one if more than one bit
666 * set. We're effectively computing the ceil(log2(size)) here.
668 for (i = 31; i > 0; i--)
671 if (~(1 << i) & size)
674 return(RF_ALIGNMENT_LOG2(i));
678 rman_get_start(struct resource *r)
680 return (r->__r_i->r_start);
684 rman_get_end(struct resource *r)
686 return (r->__r_i->r_end);
690 rman_get_size(struct resource *r)
692 return (r->__r_i->r_end - r->__r_i->r_start + 1);
696 rman_get_flags(struct resource *r)
698 return (r->__r_i->r_flags);
702 rman_set_virtual(struct resource *r, void *v)
704 r->__r_i->r_virtual = v;
708 rman_get_virtual(struct resource *r)
710 return (r->__r_i->r_virtual);
714 rman_set_bustag(struct resource *r, bus_space_tag_t t)
720 rman_get_bustag(struct resource *r)
722 return (r->r_bustag);
726 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
732 rman_get_bushandle(struct resource *r)
734 return (r->r_bushandle);
738 rman_set_rid(struct resource *r, int rid)
740 r->__r_i->r_rid = rid;
744 rman_set_start(struct resource *r, u_long start)
746 r->__r_i->r_start = start;
750 rman_set_end(struct resource *r, u_long end)
752 r->__r_i->r_end = end;
756 rman_get_rid(struct resource *r)
758 return (r->__r_i->r_rid);
762 rman_get_device(struct resource *r)
764 return (r->__r_i->r_dev);
768 rman_set_device(struct resource *r, struct device *dev)
770 r->__r_i->r_dev = dev;
774 rman_is_region_manager(struct resource *r, struct rman *rm)
777 return (r->__r_i->r_rm == rm);
781 * Sysctl interface for scanning the resource lists.
783 * We take two input parameters; the index into the list of resource
784 * managers, and the resource offset into the list.
787 sysctl_rman(SYSCTL_HANDLER_ARGS)
789 int *name = (int *)arg1;
790 u_int namelen = arg2;
791 int rman_idx, res_idx;
793 struct resource_i *res;
795 struct u_resource ures;
801 if (bus_data_generation_check(name[0]))
807 * Find the indexed resource manager
810 TAILQ_FOREACH(rm, &rman_head, rm_link) {
814 mtx_unlock(&rman_mtx);
819 * If the resource index is -1, we want details on the
823 bzero(&urm, sizeof(urm));
824 urm.rm_handle = (uintptr_t)rm;
825 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
826 urm.rm_start = rm->rm_start;
827 urm.rm_size = rm->rm_end - rm->rm_start + 1;
828 urm.rm_type = rm->rm_type;
830 error = SYSCTL_OUT(req, &urm, sizeof(urm));
835 * Find the indexed resource and return it.
837 mtx_lock(rm->rm_mtx);
838 TAILQ_FOREACH(res, &rm->rm_list, r_link) {
839 if (res_idx-- == 0) {
840 bzero(&ures, sizeof(ures));
841 ures.r_handle = (uintptr_t)res;
842 ures.r_parent = (uintptr_t)res->r_rm;
843 ures.r_device = (uintptr_t)res->r_dev;
844 if (res->r_dev != NULL) {
845 if (device_get_name(res->r_dev) != NULL) {
846 snprintf(ures.r_devname, RM_TEXTLEN,
848 device_get_name(res->r_dev),
849 device_get_unit(res->r_dev));
851 strlcpy(ures.r_devname, "nomatch",
855 ures.r_devname[0] = '\0';
857 ures.r_start = res->r_start;
858 ures.r_size = res->r_end - res->r_start + 1;
859 ures.r_flags = res->r_flags;
861 mtx_unlock(rm->rm_mtx);
862 error = SYSCTL_OUT(req, &ures, sizeof(ures));
866 mtx_unlock(rm->rm_mtx);
870 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
871 "kernel resource manager");