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 #define __RMAN_RESOURCE_VISIBLE
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/mutex.h>
68 #include <sys/bus.h> /* XXX debugging */
69 #include <machine/bus.h>
71 #include <sys/sysctl.h>
74 TUNABLE_INT("debug.rman_debug", &rman_debug);
75 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
76 &rman_debug, 0, "rman debug");
78 #define DPRINTF(params) if (rman_debug) printf params
80 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
82 struct rman_head rman_head;
83 static struct mtx rman_mtx; /* mutex to protect rman_head */
84 static int int_rman_activate_resource(struct rman *rm, struct resource_i *r,
85 struct resource_i **whohas);
86 static int int_rman_deactivate_resource(struct resource_i *r);
87 static int int_rman_release_resource(struct rman *rm, struct resource_i *r);
89 static __inline struct resource_i *
90 int_alloc_resource(int malloc_flag)
94 r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
102 rman_init(struct rman *rm)
108 TAILQ_INIT(&rman_head);
109 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
112 if (rm->rm_type == RMAN_UNINIT)
114 if (rm->rm_type == RMAN_GAUGE)
115 panic("implement RMAN_GAUGE");
117 TAILQ_INIT(&rm->rm_list);
118 rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
121 mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
124 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
125 mtx_unlock(&rman_mtx);
130 * NB: this interface is not robust against programming errors which
131 * add multiple copies of the same region.
134 rman_manage_region(struct rman *rm, u_long start, u_long end)
136 struct resource_i *r, *s;
138 DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
139 rm->rm_descr, start, end));
140 r = int_alloc_resource(M_NOWAIT);
147 mtx_lock(rm->rm_mtx);
148 for (s = TAILQ_FIRST(&rm->rm_list);
149 s && s->r_end < r->r_start;
150 s = TAILQ_NEXT(s, r_link))
154 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
156 TAILQ_INSERT_BEFORE(s, r, r_link);
159 mtx_unlock(rm->rm_mtx);
164 rman_fini(struct rman *rm)
166 struct resource_i *r;
168 mtx_lock(rm->rm_mtx);
169 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
170 if (r->r_flags & RF_ALLOCATED) {
171 mtx_unlock(rm->rm_mtx);
177 * There really should only be one of these if we are in this
178 * state and the code is working properly, but it can't hurt.
180 while (!TAILQ_EMPTY(&rm->rm_list)) {
181 r = TAILQ_FIRST(&rm->rm_list);
182 TAILQ_REMOVE(&rm->rm_list, r, r_link);
185 mtx_unlock(rm->rm_mtx);
187 TAILQ_REMOVE(&rman_head, rm, rm_link);
188 mtx_unlock(&rman_mtx);
189 mtx_destroy(rm->rm_mtx);
190 free(rm->rm_mtx, M_RMAN);
196 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
197 u_long count, u_long bound, u_int flags,
201 struct resource_i *r, *s, *rv;
202 u_long rstart, rend, amask, bmask;
206 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
207 "%#lx, flags %u, device %s\n", rm->rm_descr, start, end, count,
208 flags, dev == NULL ? "<null>" : device_get_nameunit(dev)));
209 want_activate = (flags & RF_ACTIVE);
212 mtx_lock(rm->rm_mtx);
214 for (r = TAILQ_FIRST(&rm->rm_list);
215 r && r->r_end < start;
216 r = TAILQ_NEXT(r, r_link))
220 DPRINTF(("could not find a region\n"));
224 amask = (1ul << RF_ALIGNMENT(flags)) - 1;
225 /* If bound is 0, bmask will also be 0 */
226 bmask = ~(bound - 1);
228 * First try to find an acceptable totally-unshared region.
230 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
231 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
232 if (s->r_start + count - 1 > end) {
233 DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
237 if (s->r_flags & RF_ALLOCATED) {
238 DPRINTF(("region is allocated\n"));
241 rstart = ulmax(s->r_start, start);
243 * Try to find a region by adjusting to boundary and alignment
244 * until both conditions are satisfied. This is not an optimal
245 * algorithm, but in most cases it isn't really bad, either.
248 rstart = (rstart + amask) & ~amask;
249 if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
250 rstart += bound - (rstart & ~bmask);
251 } while ((rstart & amask) != 0 && rstart < end &&
253 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
255 DPRINTF(("adjusted start exceeds end\n"));
258 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
259 rstart, rend, (rend - rstart + 1), count));
261 if ((rend - rstart + 1) >= count) {
262 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
263 rstart, rend, (rend - rstart + 1)));
264 if ((s->r_end - s->r_start + 1) == count) {
265 DPRINTF(("candidate region is entire chunk\n"));
267 rv->r_flags |= RF_ALLOCATED | flags;
273 * If s->r_start < rstart and
274 * s->r_end > rstart + count - 1, then
275 * we need to split the region into three pieces
276 * (the middle one will get returned to the user).
277 * Otherwise, we are allocating at either the
278 * beginning or the end of s, so we only need to
279 * split it in two. The first case requires
280 * two new allocations; the second requires but one.
282 rv = int_alloc_resource(M_NOWAIT);
285 rv->r_start = rstart;
286 rv->r_end = rstart + count - 1;
287 rv->r_flags = flags | RF_ALLOCATED;
291 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
292 DPRINTF(("splitting region in three parts: "
293 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
294 s->r_start, rv->r_start - 1,
295 rv->r_start, rv->r_end,
296 rv->r_end + 1, s->r_end));
298 * We are allocating in the middle.
300 r = int_alloc_resource(M_NOWAIT);
306 r->r_start = rv->r_end + 1;
308 r->r_flags = s->r_flags;
310 s->r_end = rv->r_start - 1;
311 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
313 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
315 } else if (s->r_start == rv->r_start) {
316 DPRINTF(("allocating from the beginning\n"));
318 * We are allocating at the beginning.
320 s->r_start = rv->r_end + 1;
321 TAILQ_INSERT_BEFORE(s, rv, r_link);
323 DPRINTF(("allocating at the end\n"));
325 * We are allocating at the end.
327 s->r_end = rv->r_start - 1;
328 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
336 * Now find an acceptable shared region, if the client's requirements
337 * allow sharing. By our implementation restriction, a candidate
338 * region must match exactly by both size and sharing type in order
339 * to be considered compatible with the client's request. (The
340 * former restriction could probably be lifted without too much
341 * additional work, but this does not seem warranted.)
343 DPRINTF(("no unshared regions found\n"));
344 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
347 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
348 if (s->r_start > end)
350 if ((s->r_flags & flags) != flags)
352 rstart = ulmax(s->r_start, start);
353 rend = ulmin(s->r_end, ulmax(start + count - 1, end));
354 if (s->r_start >= start && s->r_end <= end
355 && (s->r_end - s->r_start + 1) == count &&
356 (s->r_start & amask) == 0 &&
357 ((s->r_start ^ s->r_end) & bmask) == 0) {
358 rv = int_alloc_resource(M_NOWAIT);
361 rv->r_start = s->r_start;
362 rv->r_end = s->r_end;
363 rv->r_flags = s->r_flags &
364 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
367 if (s->r_sharehead == 0) {
368 s->r_sharehead = malloc(sizeof *s->r_sharehead,
369 M_RMAN, M_NOWAIT | M_ZERO);
370 if (s->r_sharehead == 0) {
375 LIST_INIT(s->r_sharehead);
376 LIST_INSERT_HEAD(s->r_sharehead, s,
378 s->r_flags |= RF_FIRSTSHARE;
380 rv->r_sharehead = s->r_sharehead;
381 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
387 * We couldn't find anything.
391 * If the user specified RF_ACTIVE in the initial flags,
392 * which is reflected in `want_activate', we attempt to atomically
393 * activate the resource. If this fails, we release the resource
394 * and indicate overall failure. (This behavior probably doesn't
395 * make sense for RF_TIMESHARE-type resources.)
397 if (rv && want_activate) {
398 struct resource_i *whohas;
399 if (int_rman_activate_resource(rm, rv, &whohas)) {
400 int_rman_release_resource(rm, rv);
405 mtx_unlock(rm->rm_mtx);
410 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
411 u_int flags, struct device *dev)
414 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
419 int_rman_activate_resource(struct rman *rm, struct resource_i *r,
420 struct resource_i **whohas)
422 struct resource_i *s;
426 * If we are not timesharing, then there is nothing much to do.
427 * If we already have the resource, then there is nothing at all to do.
428 * If we are not on a sharing list with anybody else, then there is
431 if ((r->r_flags & RF_TIMESHARE) == 0
432 || (r->r_flags & RF_ACTIVE) != 0
433 || r->r_sharehead == 0) {
434 r->r_flags |= RF_ACTIVE;
439 for (s = LIST_FIRST(r->r_sharehead); s && ok;
440 s = LIST_NEXT(s, r_sharelink)) {
441 if ((s->r_flags & RF_ACTIVE) != 0) {
447 r->r_flags |= RF_ACTIVE;
454 rman_activate_resource(struct resource *re)
457 struct resource_i *r, *whohas;
462 mtx_lock(rm->rm_mtx);
463 rv = int_rman_activate_resource(rm, r, &whohas);
464 mtx_unlock(rm->rm_mtx);
469 rman_await_resource(struct resource *re, int pri, int timo)
472 struct resource_i *r, *whohas;
477 mtx_lock(rm->rm_mtx);
479 rv = int_rman_activate_resource(rm, r, &whohas);
481 return (rv); /* returns with mutex held */
483 if (r->r_sharehead == 0)
484 panic("rman_await_resource");
485 whohas->r_flags |= RF_WANTED;
486 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
488 mtx_unlock(rm->rm_mtx);
495 int_rman_deactivate_resource(struct resource_i *r)
498 r->r_flags &= ~RF_ACTIVE;
499 if (r->r_flags & RF_WANTED) {
500 r->r_flags &= ~RF_WANTED;
501 wakeup(r->r_sharehead);
507 rman_deactivate_resource(struct resource *r)
512 mtx_lock(rm->rm_mtx);
513 int_rman_deactivate_resource(r->__r_i);
514 mtx_unlock(rm->rm_mtx);
519 int_rman_release_resource(struct rman *rm, struct resource_i *r)
521 struct resource_i *s, *t;
523 if (r->r_flags & RF_ACTIVE)
524 int_rman_deactivate_resource(r);
527 * Check for a sharing list first. If there is one, then we don't
528 * have to think as hard.
530 if (r->r_sharehead) {
532 * If a sharing list exists, then we know there are at
535 * If we are in the main circleq, appoint someone else.
537 LIST_REMOVE(r, r_sharelink);
538 s = LIST_FIRST(r->r_sharehead);
539 if (r->r_flags & RF_FIRSTSHARE) {
540 s->r_flags |= RF_FIRSTSHARE;
541 TAILQ_INSERT_BEFORE(r, s, r_link);
542 TAILQ_REMOVE(&rm->rm_list, r, r_link);
546 * Make sure that the sharing list goes away completely
547 * if the resource is no longer being shared at all.
549 if (LIST_NEXT(s, r_sharelink) == 0) {
550 free(s->r_sharehead, M_RMAN);
552 s->r_flags &= ~RF_FIRSTSHARE;
558 * Look at the adjacent resources in the list and see if our
559 * segment can be merged with any of them. If either of the
560 * resources is allocated or is not exactly adjacent then they
561 * cannot be merged with our segment.
563 s = TAILQ_PREV(r, resource_head, r_link);
564 if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
565 s->r_end + 1 != r->r_start))
567 t = TAILQ_NEXT(r, r_link);
568 if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
569 r->r_end + 1 != t->r_start))
572 if (s != NULL && t != NULL) {
574 * Merge all three segments.
577 TAILQ_REMOVE(&rm->rm_list, r, r_link);
578 TAILQ_REMOVE(&rm->rm_list, t, r_link);
580 } else if (s != NULL) {
582 * Merge previous segment with ours.
585 TAILQ_REMOVE(&rm->rm_list, r, r_link);
586 } else if (t != NULL) {
588 * Merge next segment with ours.
590 t->r_start = r->r_start;
591 TAILQ_REMOVE(&rm->rm_list, r, r_link);
594 * At this point, we know there is nothing we
595 * can potentially merge with, because on each
596 * side, there is either nothing there or what is
597 * there is still allocated. In that case, we don't
598 * want to remove r from the list; we simply want to
599 * change it to an unallocated region and return
600 * without freeing anything.
602 r->r_flags &= ~RF_ALLOCATED;
612 rman_release_resource(struct resource *re)
615 struct resource_i *r;
620 mtx_lock(rm->rm_mtx);
621 rv = int_rman_release_resource(rm, r);
622 mtx_unlock(rm->rm_mtx);
627 rman_make_alignment_flags(uint32_t size)
632 * Find the hightest bit set, and add one if more than one bit
633 * set. We're effectively computing the ceil(log2(size)) here.
635 for (i = 31; i > 0; i--)
638 if (~(1 << i) & size)
641 return(RF_ALIGNMENT_LOG2(i));
645 rman_get_start(struct resource *r)
647 return (r->__r_i->r_start);
651 rman_get_end(struct resource *r)
653 return (r->__r_i->r_end);
657 rman_get_size(struct resource *r)
659 return (r->__r_i->r_end - r->__r_i->r_start + 1);
663 rman_get_flags(struct resource *r)
665 return (r->__r_i->r_flags);
669 rman_set_virtual(struct resource *r, void *v)
671 r->__r_i->r_virtual = v;
675 rman_get_virtual(struct resource *r)
677 return (r->__r_i->r_virtual);
681 rman_set_bustag(struct resource *r, bus_space_tag_t t)
687 rman_get_bustag(struct resource *r)
689 return (r->r_bustag);
693 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
699 rman_get_bushandle(struct resource *r)
701 return (r->r_bushandle);
705 rman_set_rid(struct resource *r, int rid)
707 r->__r_i->r_rid = rid;
711 rman_set_start(struct resource *r, u_long start)
713 r->__r_i->r_start = start;
717 rman_set_end(struct resource *r, u_long end)
719 r->__r_i->r_end = end;
723 rman_get_rid(struct resource *r)
725 return (r->__r_i->r_rid);
729 rman_get_device(struct resource *r)
731 return (r->__r_i->r_dev);
735 rman_set_device(struct resource *r, struct device *dev)
737 r->__r_i->r_dev = dev;
741 * Sysctl interface for scanning the resource lists.
743 * We take two input parameters; the index into the list of resource
744 * managers, and the resource offset into the list.
747 sysctl_rman(SYSCTL_HANDLER_ARGS)
749 int *name = (int *)arg1;
750 u_int namelen = arg2;
751 int rman_idx, res_idx;
753 struct resource_i *res;
755 struct u_resource ures;
761 if (bus_data_generation_check(name[0]))
767 * Find the indexed resource manager
769 TAILQ_FOREACH(rm, &rman_head, rm_link) {
777 * If the resource index is -1, we want details on the
781 bzero(&urm, sizeof(urm));
782 urm.rm_handle = (uintptr_t)rm;
783 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
784 urm.rm_start = rm->rm_start;
785 urm.rm_size = rm->rm_end - rm->rm_start + 1;
786 urm.rm_type = rm->rm_type;
788 error = SYSCTL_OUT(req, &urm, sizeof(urm));
793 * Find the indexed resource and return it.
795 TAILQ_FOREACH(res, &rm->rm_list, r_link) {
796 if (res_idx-- == 0) {
797 bzero(&ures, sizeof(ures));
798 ures.r_handle = (uintptr_t)res;
799 ures.r_parent = (uintptr_t)res->r_rm;
800 ures.r_device = (uintptr_t)res->r_dev;
801 if (res->r_dev != NULL) {
802 if (device_get_name(res->r_dev) != NULL) {
803 snprintf(ures.r_devname, RM_TEXTLEN,
805 device_get_name(res->r_dev),
806 device_get_unit(res->r_dev));
808 strlcpy(ures.r_devname, "nomatch",
812 ures.r_devname[0] = '\0';
814 ures.r_start = res->r_start;
815 ures.r_size = res->r_end - res->r_start + 1;
816 ures.r_flags = res->r_flags;
818 error = SYSCTL_OUT(req, &ures, sizeof(ures));
825 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
826 "kernel resource manager");