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
29 * $FreeBSD: src/sys/kern/subr_rman.c,v 1.10.2.1 2001/06/05 08:06:08 imp Exp $
33 * The kernel resource manager. This code is responsible for keeping track
34 * of hardware resources which are apportioned out to various drivers.
35 * It does not actually assign those resources, and it is not expected
36 * that end-device drivers will call into this code directly. Rather,
37 * the code which implements the buses that those devices are attached to,
38 * and the code which manages CPU resources, will call this code, and the
39 * end-device drivers will make upcalls to that code to actually perform
42 * There are two sorts of resources managed by this code. The first is
43 * the more familiar array (RMAN_ARRAY) type; resources in this class
44 * consist of a sequence of individually-allocatable objects which have
45 * been numbered in some well-defined order. Most of the resources
46 * are of this type, as it is the most familiar. The second type is
47 * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
48 * resources in which each instance is indistinguishable from every
49 * other instance). The principal anticipated application of gauges
50 * is in the context of power consumption, where a bus may have a specific
51 * power budget which all attached devices share. RMAN_GAUGE is not
54 * For array resources, we make one simplifying assumption: two clients
55 * sharing the same resource must use the same range of indices. That
56 * is to say, sharing of overlapping-but-not-identical regions is not
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/bus.h> /* XXX debugging */
66 #include <machine/bus.h>
69 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
71 struct rman_head rman_head;
72 #ifndef NULL_SIMPLELOCKS
73 static struct simplelock rman_lock; /* mutex to protect rman_head */
75 static int int_rman_activate_resource(struct rman *rm, struct resource *r,
76 struct resource **whohas);
77 static int int_rman_deactivate_resource(struct resource *r);
78 static int int_rman_release_resource(struct rman *rm, struct resource *r);
80 #define CIRCLEQ_TERMCOND(var, head) (var == (void *)&(head))
83 rman_init(struct rman *rm)
89 TAILQ_INIT(&rman_head);
90 simple_lock_init(&rman_lock);
93 if (rm->rm_type == RMAN_UNINIT)
95 if (rm->rm_type == RMAN_GAUGE)
96 panic("implement RMAN_GAUGE");
98 CIRCLEQ_INIT(&rm->rm_list);
99 rm->rm_slock = malloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT);
100 if (rm->rm_slock == 0)
102 simple_lock_init(rm->rm_slock);
104 simple_lock(&rman_lock);
105 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
106 simple_unlock(&rman_lock);
111 * NB: this interface is not robust against programming errors which
112 * add multiple copies of the same region.
115 rman_manage_region(struct rman *rm, u_long start, u_long end)
117 struct resource *r, *s;
119 r = malloc(sizeof *r, M_RMAN, M_NOWAIT);
130 simple_lock(rm->rm_slock);
131 for (s = CIRCLEQ_FIRST(&rm->rm_list);
132 !CIRCLEQ_TERMCOND(s, rm->rm_list) && s->r_end < r->r_start;
133 s = CIRCLEQ_NEXT(s, r_link))
136 if (CIRCLEQ_TERMCOND(s, rm->rm_list)) {
137 CIRCLEQ_INSERT_TAIL(&rm->rm_list, r, r_link);
139 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, r, r_link);
142 simple_unlock(rm->rm_slock);
147 rman_fini(struct rman *rm)
151 simple_lock(rm->rm_slock);
152 CIRCLEQ_FOREACH(r, &rm->rm_list, r_link) {
153 if (r->r_flags & RF_ALLOCATED) {
154 simple_unlock(rm->rm_slock);
160 * There really should only be one of these if we are in this
161 * state and the code is working properly, but it can't hurt.
163 while (!CIRCLEQ_EMPTY(&rm->rm_list)) {
164 r = CIRCLEQ_FIRST(&rm->rm_list);
165 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
168 simple_unlock(rm->rm_slock);
169 simple_lock(&rman_lock);
170 TAILQ_REMOVE(&rman_head, rm, rm_link);
171 simple_unlock(&rman_lock);
172 free(rm->rm_slock, M_RMAN);
178 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
179 u_int flags, struct device *dev)
182 struct resource *r, *s, *rv;
188 printf("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
189 "%#lx, flags %u, device %s%d\n", rm->rm_descr, start, end,
190 count, flags, device_get_name(dev), device_get_unit(dev));
191 #endif /* RMAN_DEBUG */
192 want_activate = (flags & RF_ACTIVE);
195 simple_lock(rm->rm_slock);
197 for (r = CIRCLEQ_FIRST(&rm->rm_list);
198 !CIRCLEQ_TERMCOND(r, rm->rm_list) && r->r_end < start;
199 r = CIRCLEQ_NEXT(r, r_link))
202 if (CIRCLEQ_TERMCOND(r, rm->rm_list)) {
204 printf("could not find a region\n");
210 * First try to find an acceptable totally-unshared region.
212 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
213 s = CIRCLEQ_NEXT(s, r_link)) {
215 printf("considering [%#lx, %#lx]\n", s->r_start, s->r_end);
216 #endif /* RMAN_DEBUG */
217 if (s->r_start > end) {
219 printf("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end);
220 #endif /* RMAN_DEBUG */
223 if (s->r_flags & RF_ALLOCATED) {
225 printf("region is allocated\n");
226 #endif /* RMAN_DEBUG */
229 rstart = max(s->r_start, start);
230 rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
231 ~((1ul << RF_ALIGNMENT(flags)) - 1);
232 rend = min(s->r_end, max(start + count, end));
234 printf("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
235 rstart, rend, (rend - rstart + 1), count);
236 #endif /* RMAN_DEBUG */
238 if ((rend - rstart + 1) >= count) {
240 printf("candidate region: [%#lx, %#lx], size %#lx\n",
241 rend, rstart, (rend - rstart + 1));
242 #endif /* RMAN_DEBUG */
243 if ((s->r_end - s->r_start + 1) == count) {
245 printf("candidate region is entire chunk\n");
246 #endif /* RMAN_DEBUG */
248 rv->r_flags |= RF_ALLOCATED | flags;
254 * If s->r_start < rstart and
255 * s->r_end > rstart + count - 1, then
256 * we need to split the region into three pieces
257 * (the middle one will get returned to the user).
258 * Otherwise, we are allocating at either the
259 * beginning or the end of s, so we only need to
260 * split it in two. The first case requires
261 * two new allocations; the second requires but one.
263 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT);
266 bzero(rv, sizeof *rv);
267 rv->r_start = rstart;
268 rv->r_end = rstart + count - 1;
269 rv->r_flags = flags | RF_ALLOCATED;
274 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
276 printf("splitting region in three parts: "
277 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
278 s->r_start, rv->r_start - 1,
279 rv->r_start, rv->r_end,
280 rv->r_end + 1, s->r_end);
281 #endif /* RMAN_DEBUG */
283 * We are allocating in the middle.
285 r = malloc(sizeof *r, M_RMAN, M_NOWAIT);
292 r->r_start = rv->r_end + 1;
294 r->r_flags = s->r_flags;
298 s->r_end = rv->r_start - 1;
299 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
301 CIRCLEQ_INSERT_AFTER(&rm->rm_list, rv, r,
303 } else if (s->r_start == rv->r_start) {
305 printf("allocating from the beginning\n");
306 #endif /* RMAN_DEBUG */
308 * We are allocating at the beginning.
310 s->r_start = rv->r_end + 1;
311 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, rv,
315 printf("allocating at the end\n");
316 #endif /* RMAN_DEBUG */
318 * We are allocating at the end.
320 s->r_end = rv->r_start - 1;
321 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
329 * Now find an acceptable shared region, if the client's requirements
330 * allow sharing. By our implementation restriction, a candidate
331 * region must match exactly by both size and sharing type in order
332 * to be considered compatible with the client's request. (The
333 * former restriction could probably be lifted without too much
334 * additional work, but this does not seem warranted.)
337 printf("no unshared regions found\n");
338 #endif /* RMAN_DEBUG */
339 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
342 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
343 s = CIRCLEQ_NEXT(s, r_link)) {
344 if (s->r_start > end)
346 if ((s->r_flags & flags) != flags)
348 rstart = max(s->r_start, start);
349 rend = min(s->r_end, max(start + count, end));
350 if (s->r_start >= start && s->r_end <= end
351 && (s->r_end - s->r_start + 1) == count) {
352 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT);
355 bzero(rv, sizeof *rv);
356 rv->r_start = s->r_start;
357 rv->r_end = s->r_end;
358 rv->r_flags = s->r_flags &
359 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
362 if (s->r_sharehead == 0) {
363 s->r_sharehead = malloc(sizeof *s->r_sharehead,
365 if (s->r_sharehead == 0) {
370 bzero(s->r_sharehead, sizeof *s->r_sharehead);
371 LIST_INIT(s->r_sharehead);
372 LIST_INSERT_HEAD(s->r_sharehead, s,
374 s->r_flags |= RF_FIRSTSHARE;
376 rv->r_sharehead = s->r_sharehead;
377 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
383 * We couldn't find anything.
387 * If the user specified RF_ACTIVE in the initial flags,
388 * which is reflected in `want_activate', we attempt to atomically
389 * activate the resource. If this fails, we release the resource
390 * and indicate overall failure. (This behavior probably doesn't
391 * make sense for RF_TIMESHARE-type resources.)
393 if (rv && want_activate) {
394 struct resource *whohas;
395 if (int_rman_activate_resource(rm, rv, &whohas)) {
396 int_rman_release_resource(rm, rv);
401 simple_unlock(rm->rm_slock);
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 simple_lock(rm->rm_slock);
449 rv = int_rman_activate_resource(rm, r, &whohas);
450 simple_unlock(rm->rm_slock);
455 rman_await_resource(struct resource *r, int pri, int timo)
458 struct resource *whohas;
463 simple_lock(rm->rm_slock);
464 rv = int_rman_activate_resource(rm, r, &whohas);
466 return (rv); /* returns with simplelock */
468 if (r->r_sharehead == 0)
469 panic("rman_await_resource");
471 * splhigh hopefully will prevent a race between
472 * simple_unlock and tsleep where a process
473 * could conceivably get in and release the resource
474 * before we have a chance to sleep on it.
477 whohas->r_flags |= RF_WANTED;
478 simple_unlock(rm->rm_slock);
479 rv = tsleep(r->r_sharehead, pri, "rmwait", timo);
484 simple_lock(rm->rm_slock);
490 int_rman_deactivate_resource(struct resource *r)
495 r->r_flags &= ~RF_ACTIVE;
496 if (r->r_flags & RF_WANTED) {
497 r->r_flags &= ~RF_WANTED;
498 wakeup(r->r_sharehead);
504 rman_deactivate_resource(struct resource *r)
509 simple_lock(rm->rm_slock);
510 int_rman_deactivate_resource(r);
511 simple_unlock(rm->rm_slock);
516 int_rman_release_resource(struct rman *rm, struct resource *r)
518 struct resource *s, *t;
520 if (r->r_flags & RF_ACTIVE)
521 int_rman_deactivate_resource(r);
524 * Check for a sharing list first. If there is one, then we don't
525 * have to think as hard.
527 if (r->r_sharehead) {
529 * If a sharing list exists, then we know there are at
532 * If we are in the main circleq, appoint someone else.
534 LIST_REMOVE(r, r_sharelink);
535 s = LIST_FIRST(r->r_sharehead);
536 if (r->r_flags & RF_FIRSTSHARE) {
537 s->r_flags |= RF_FIRSTSHARE;
538 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, r, s, r_link);
539 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
543 * Make sure that the sharing list goes away completely
544 * if the resource is no longer being shared at all.
546 if (LIST_NEXT(s, r_sharelink) == 0) {
547 free(s->r_sharehead, M_RMAN);
549 s->r_flags &= ~RF_FIRSTSHARE;
555 * Look at the adjacent resources in the list and see if our
556 * segment can be merged with any of them.
558 s = CIRCLEQ_PREV(r, r_link);
559 t = CIRCLEQ_NEXT(r, r_link);
561 if (s != (void *)&rm->rm_list && (s->r_flags & RF_ALLOCATED) == 0
562 && t != (void *)&rm->rm_list && (t->r_flags & RF_ALLOCATED) == 0) {
564 * Merge all three segments.
567 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
568 CIRCLEQ_REMOVE(&rm->rm_list, t, r_link);
570 } else if (s != (void *)&rm->rm_list
571 && (s->r_flags & RF_ALLOCATED) == 0) {
573 * Merge previous segment with ours.
576 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
577 } else if (t != (void *)&rm->rm_list
578 && (t->r_flags & RF_ALLOCATED) == 0) {
580 * Merge next segment with ours.
582 t->r_start = r->r_start;
583 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
586 * At this point, we know there is nothing we
587 * can potentially merge with, because on each
588 * side, there is either nothing there or what is
589 * there is still allocated. In that case, we don't
590 * want to remove r from the list; we simply want to
591 * change it to an unallocated region and return
592 * without freeing anything.
594 r->r_flags &= ~RF_ALLOCATED;
604 rman_release_resource(struct resource *r)
607 struct rman *rm = r->r_rm;
609 simple_lock(rm->rm_slock);
610 rv = int_rman_release_resource(rm, r);
611 simple_unlock(rm->rm_slock);
616 rman_make_alignment_flags(uint32_t size)
621 * Find the hightest bit set, and add one if more than one bit
622 * set. We're effectively computing the ceil(log2(size)) here.
624 for (i = 32; i > 0; i--)
627 if (~(1 << i) & size)
630 return(RF_ALIGNMENT_LOG2(i));