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: src/sys/kern/subr_rman.c,v 1.35.2.2 2005/01/31 23:26:17 imp Exp $");
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 *r,
85 struct resource **whohas);
86 static int int_rman_deactivate_resource(struct resource *r);
87 static int int_rman_release_resource(struct rman *rm, struct resource *r);
90 rman_init(struct rman *rm)
96 TAILQ_INIT(&rman_head);
97 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
100 if (rm->rm_type == RMAN_UNINIT)
102 if (rm->rm_type == RMAN_GAUGE)
103 panic("implement RMAN_GAUGE");
105 TAILQ_INIT(&rm->rm_list);
106 rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
109 mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
112 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
113 mtx_unlock(&rman_mtx);
118 * NB: this interface is not robust against programming errors which
119 * add multiple copies of the same region.
122 rman_manage_region(struct rman *rm, u_long start, u_long end)
124 struct resource *r, *s;
126 DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
127 rm->rm_descr, start, end));
128 r = malloc(sizeof *r, M_RMAN, M_NOWAIT | M_ZERO);
135 mtx_lock(rm->rm_mtx);
136 for (s = TAILQ_FIRST(&rm->rm_list);
137 s && s->r_end < r->r_start;
138 s = TAILQ_NEXT(s, r_link))
142 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
144 TAILQ_INSERT_BEFORE(s, r, r_link);
147 mtx_unlock(rm->rm_mtx);
152 rman_fini(struct rman *rm)
156 mtx_lock(rm->rm_mtx);
157 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
158 if (r->r_flags & RF_ALLOCATED) {
159 mtx_unlock(rm->rm_mtx);
165 * There really should only be one of these if we are in this
166 * state and the code is working properly, but it can't hurt.
168 while (!TAILQ_EMPTY(&rm->rm_list)) {
169 r = TAILQ_FIRST(&rm->rm_list);
170 TAILQ_REMOVE(&rm->rm_list, r, r_link);
173 mtx_unlock(rm->rm_mtx);
175 TAILQ_REMOVE(&rman_head, rm, rm_link);
176 mtx_unlock(&rman_mtx);
177 mtx_destroy(rm->rm_mtx);
178 free(rm->rm_mtx, M_RMAN);
184 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
185 u_long count, u_long bound, u_int flags,
189 struct resource *r, *s, *rv;
190 u_long rstart, rend, amask, bmask;
194 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
195 "%#lx, flags %u, device %s\n", rm->rm_descr, start, end, count,
196 flags, dev == NULL ? "<null>" : device_get_nameunit(dev)));
197 want_activate = (flags & RF_ACTIVE);
200 mtx_lock(rm->rm_mtx);
202 for (r = TAILQ_FIRST(&rm->rm_list);
203 r && r->r_end < start;
204 r = TAILQ_NEXT(r, r_link))
208 DPRINTF(("could not find a region\n"));
212 amask = (1ul << RF_ALIGNMENT(flags)) - 1;
213 /* If bound is 0, bmask will also be 0 */
214 bmask = ~(bound - 1);
216 * First try to find an acceptable totally-unshared region.
218 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
219 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
220 if (s->r_start > end) {
221 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end));
224 if (s->r_flags & RF_ALLOCATED) {
225 DPRINTF(("region is allocated\n"));
228 rstart = ulmax(s->r_start, start);
230 * Try to find a region by adjusting to boundary and alignment
231 * until both conditions are satisfied. This is not an optimal
232 * algorithm, but in most cases it isn't really bad, either.
235 rstart = (rstart + amask) & ~amask;
236 if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
237 rstart += bound - (rstart & ~bmask);
238 } while ((rstart & amask) != 0 && rstart < end &&
240 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
242 DPRINTF(("adjusted start exceeds end\n"));
245 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
246 rstart, rend, (rend - rstart + 1), count));
248 if ((rend - rstart + 1) >= count) {
249 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
250 rend, rstart, (rend - rstart + 1)));
251 if ((s->r_end - s->r_start + 1) == count) {
252 DPRINTF(("candidate region is entire chunk\n"));
254 rv->r_flags |= RF_ALLOCATED | flags;
260 * If s->r_start < rstart and
261 * s->r_end > rstart + count - 1, then
262 * we need to split the region into three pieces
263 * (the middle one will get returned to the user).
264 * Otherwise, we are allocating at either the
265 * beginning or the end of s, so we only need to
266 * split it in two. The first case requires
267 * two new allocations; the second requires but one.
269 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
272 rv->r_start = rstart;
273 rv->r_end = rstart + count - 1;
274 rv->r_flags = flags | RF_ALLOCATED;
278 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
279 DPRINTF(("splitting region in three parts: "
280 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
281 s->r_start, rv->r_start - 1,
282 rv->r_start, rv->r_end,
283 rv->r_end + 1, s->r_end));
285 * We are allocating in the middle.
287 r = malloc(sizeof *r, M_RMAN, M_NOWAIT|M_ZERO);
293 r->r_start = rv->r_end + 1;
295 r->r_flags = s->r_flags;
297 s->r_end = rv->r_start - 1;
298 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
300 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
302 } else if (s->r_start == rv->r_start) {
303 DPRINTF(("allocating from the beginning\n"));
305 * We are allocating at the beginning.
307 s->r_start = rv->r_end + 1;
308 TAILQ_INSERT_BEFORE(s, rv, r_link);
310 DPRINTF(("allocating at the end\n"));
312 * We are allocating at the end.
314 s->r_end = rv->r_start - 1;
315 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
323 * Now find an acceptable shared region, if the client's requirements
324 * allow sharing. By our implementation restriction, a candidate
325 * region must match exactly by both size and sharing type in order
326 * to be considered compatible with the client's request. (The
327 * former restriction could probably be lifted without too much
328 * additional work, but this does not seem warranted.)
330 DPRINTF(("no unshared regions found\n"));
331 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
334 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
335 if (s->r_start > end)
337 if ((s->r_flags & flags) != flags)
339 rstart = ulmax(s->r_start, start);
340 rend = ulmin(s->r_end, ulmax(start + count - 1, end));
341 if (s->r_start >= start && s->r_end <= end
342 && (s->r_end - s->r_start + 1) == count &&
343 (s->r_start & amask) == 0 &&
344 ((s->r_start ^ s->r_end) & bmask) == 0) {
345 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
348 rv->r_start = s->r_start;
349 rv->r_end = s->r_end;
350 rv->r_flags = s->r_flags &
351 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
354 if (s->r_sharehead == 0) {
355 s->r_sharehead = malloc(sizeof *s->r_sharehead,
356 M_RMAN, M_NOWAIT | M_ZERO);
357 if (s->r_sharehead == 0) {
362 LIST_INIT(s->r_sharehead);
363 LIST_INSERT_HEAD(s->r_sharehead, s,
365 s->r_flags |= RF_FIRSTSHARE;
367 rv->r_sharehead = s->r_sharehead;
368 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
374 * We couldn't find anything.
378 * If the user specified RF_ACTIVE in the initial flags,
379 * which is reflected in `want_activate', we attempt to atomically
380 * activate the resource. If this fails, we release the resource
381 * and indicate overall failure. (This behavior probably doesn't
382 * make sense for RF_TIMESHARE-type resources.)
384 if (rv && want_activate) {
385 struct resource *whohas;
386 if (int_rman_activate_resource(rm, rv, &whohas)) {
387 int_rman_release_resource(rm, rv);
392 mtx_unlock(rm->rm_mtx);
397 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
398 u_int flags, struct device *dev)
401 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
406 int_rman_activate_resource(struct rman *rm, struct resource *r,
407 struct resource **whohas)
413 * If we are not timesharing, then there is nothing much to do.
414 * If we already have the resource, then there is nothing at all to do.
415 * If we are not on a sharing list with anybody else, then there is
418 if ((r->r_flags & RF_TIMESHARE) == 0
419 || (r->r_flags & RF_ACTIVE) != 0
420 || r->r_sharehead == 0) {
421 r->r_flags |= RF_ACTIVE;
426 for (s = LIST_FIRST(r->r_sharehead); s && ok;
427 s = LIST_NEXT(s, r_sharelink)) {
428 if ((s->r_flags & RF_ACTIVE) != 0) {
434 r->r_flags |= RF_ACTIVE;
441 rman_activate_resource(struct resource *r)
444 struct resource *whohas;
448 mtx_lock(rm->rm_mtx);
449 rv = int_rman_activate_resource(rm, r, &whohas);
450 mtx_unlock(rm->rm_mtx);
455 rman_await_resource(struct resource *r, int pri, int timo)
458 struct resource *whohas;
462 mtx_lock(rm->rm_mtx);
464 rv = int_rman_activate_resource(rm, r, &whohas);
466 return (rv); /* returns with mutex held */
468 if (r->r_sharehead == 0)
469 panic("rman_await_resource");
470 whohas->r_flags |= RF_WANTED;
471 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
473 mtx_unlock(rm->rm_mtx);
480 int_rman_deactivate_resource(struct resource *r)
483 r->r_flags &= ~RF_ACTIVE;
484 if (r->r_flags & RF_WANTED) {
485 r->r_flags &= ~RF_WANTED;
486 wakeup(r->r_sharehead);
492 rman_deactivate_resource(struct resource *r)
497 mtx_lock(rm->rm_mtx);
498 int_rman_deactivate_resource(r);
499 mtx_unlock(rm->rm_mtx);
504 int_rman_release_resource(struct rman *rm, struct resource *r)
506 struct resource *s, *t;
508 if (r->r_flags & RF_ACTIVE)
509 int_rman_deactivate_resource(r);
512 * Check for a sharing list first. If there is one, then we don't
513 * have to think as hard.
515 if (r->r_sharehead) {
517 * If a sharing list exists, then we know there are at
520 * If we are in the main circleq, appoint someone else.
522 LIST_REMOVE(r, r_sharelink);
523 s = LIST_FIRST(r->r_sharehead);
524 if (r->r_flags & RF_FIRSTSHARE) {
525 s->r_flags |= RF_FIRSTSHARE;
526 TAILQ_INSERT_BEFORE(r, s, r_link);
527 TAILQ_REMOVE(&rm->rm_list, r, r_link);
531 * Make sure that the sharing list goes away completely
532 * if the resource is no longer being shared at all.
534 if (LIST_NEXT(s, r_sharelink) == 0) {
535 free(s->r_sharehead, M_RMAN);
537 s->r_flags &= ~RF_FIRSTSHARE;
543 * Look at the adjacent resources in the list and see if our
544 * segment can be merged with any of them. If either of the
545 * resources is allocated or is not exactly adjacent then they
546 * cannot be merged with our segment.
548 s = TAILQ_PREV(r, resource_head, r_link);
549 if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
550 s->r_end + 1 != r->r_start))
552 t = TAILQ_NEXT(r, r_link);
553 if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
554 r->r_end + 1 != t->r_start))
557 if (s != NULL && t != NULL) {
559 * Merge all three segments.
562 TAILQ_REMOVE(&rm->rm_list, r, r_link);
563 TAILQ_REMOVE(&rm->rm_list, t, r_link);
565 } else if (s != NULL) {
567 * Merge previous segment with ours.
570 TAILQ_REMOVE(&rm->rm_list, r, r_link);
571 } else if (t != NULL) {
573 * Merge next segment with ours.
575 t->r_start = r->r_start;
576 TAILQ_REMOVE(&rm->rm_list, r, r_link);
579 * At this point, we know there is nothing we
580 * can potentially merge with, because on each
581 * side, there is either nothing there or what is
582 * there is still allocated. In that case, we don't
583 * want to remove r from the list; we simply want to
584 * change it to an unallocated region and return
585 * without freeing anything.
587 r->r_flags &= ~RF_ALLOCATED;
597 rman_release_resource(struct resource *r)
600 struct rman *rm = r->r_rm;
602 mtx_lock(rm->rm_mtx);
603 rv = int_rman_release_resource(rm, r);
604 mtx_unlock(rm->rm_mtx);
609 rman_make_alignment_flags(uint32_t size)
614 * Find the hightest bit set, and add one if more than one bit
615 * set. We're effectively computing the ceil(log2(size)) here.
617 for (i = 31; i > 0; i--)
620 if (~(1 << i) & size)
623 return(RF_ALIGNMENT_LOG2(i));
627 rman_get_start(struct resource *r)
633 rman_get_end(struct resource *r)
639 rman_get_size(struct resource *r)
641 return (r->r_end - r->r_start + 1);
645 rman_get_flags(struct resource *r)
651 rman_set_virtual(struct resource *r, void *v)
657 rman_get_virtual(struct resource *r)
659 return (r->r_virtual);
663 rman_set_bustag(struct resource *r, bus_space_tag_t t)
669 rman_get_bustag(struct resource *r)
671 return (r->r_bustag);
675 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
681 rman_get_bushandle(struct resource *r)
683 return (r->r_bushandle);
687 rman_set_rid(struct resource *r, int rid)
693 rman_set_start(struct resource *r, u_long start)
699 rman_set_end(struct resource *r, u_long end)
705 rman_get_rid(struct resource *r)
711 rman_get_device(struct resource *r)