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 $
30 * $DragonFly: src/sys/kern/subr_rman.c,v 1.13 2007/09/21 02:28:00 y0netan1 Exp $
34 * The kernel resource manager. This code is responsible for keeping track
35 * of hardware resources which are apportioned out to various drivers.
36 * It does not actually assign those resources, and it is not expected
37 * that end-device drivers will call into this code directly. Rather,
38 * the code which implements the buses that those devices are attached to,
39 * and the code which manages CPU resources, will call this code, and the
40 * end-device drivers will make upcalls to that code to actually perform
43 * There are two sorts of resources managed by this code. The first is
44 * the more familiar array (RMAN_ARRAY) type; resources in this class
45 * consist of a sequence of individually-allocatable objects which have
46 * been numbered in some well-defined order. Most of the resources
47 * are of this type, as it is the most familiar. The second type is
48 * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
49 * resources in which each instance is indistinguishable from every
50 * other instance). The principal anticipated application of gauges
51 * is in the context of power consumption, where a bus may have a specific
52 * power budget which all attached devices share. RMAN_GAUGE is not
55 * For array resources, we make one simplifying assumption: two clients
56 * sharing the same resource must use the same range of indices. That
57 * is to say, sharing of overlapping-but-not-identical regions is not
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/bus.h> /* XXX debugging */
68 #include <sys/sysctl.h>
71 TUNABLE_INT("debug.rman_debug", &rman_debug
);
72 SYSCTL_INT(_debug
, OID_AUTO
, rman_debug
, CTLFLAG_RW
,
73 &rman_debug
, 0, "rman debug");
75 #define DPRINTF(params) if (rman_debug) kprintf params
77 static MALLOC_DEFINE(M_RMAN
, "rman", "Resource manager");
79 struct rman_head rman_head
;
80 static struct lwkt_token rman_tok
; /* mutex to protect rman_head */
81 static int int_rman_activate_resource(struct rman
*rm
, struct resource
*r
,
82 struct resource
**whohas
);
83 static int int_rman_deactivate_resource(struct resource
*r
);
84 static int int_rman_release_resource(struct rman
*rm
, struct resource
*r
);
86 #define CIRCLEQ_TERMCOND(var, head) (var == (void *)&(head))
89 rman_init(struct rman
*rm
)
96 TAILQ_INIT(&rman_head
);
97 lwkt_token_init(&rman_tok
);
100 if (rm
->rm_type
== RMAN_UNINIT
)
102 if (rm
->rm_type
== RMAN_GAUGE
)
103 panic("implement RMAN_GAUGE");
105 CIRCLEQ_INIT(&rm
->rm_list
);
106 rm
->rm_slock
= kmalloc(sizeof *rm
->rm_slock
, M_RMAN
, M_NOWAIT
);
107 if (rm
->rm_slock
== NULL
)
109 lwkt_token_init(rm
->rm_slock
);
111 lwkt_gettoken(&ilock
, &rman_tok
);
112 TAILQ_INSERT_TAIL(&rman_head
, rm
, rm_link
);
113 lwkt_reltoken(&ilock
);
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
;
127 DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
128 rm
->rm_descr
, start
, end
));
129 r
= kmalloc(sizeof *r
, M_RMAN
, M_NOWAIT
);
140 lwkt_gettoken(&ilock
, rm
->rm_slock
);
141 for (s
= CIRCLEQ_FIRST(&rm
->rm_list
);
142 !CIRCLEQ_TERMCOND(s
, rm
->rm_list
) && s
->r_end
< r
->r_start
;
143 s
= CIRCLEQ_NEXT(s
, r_link
))
146 if (CIRCLEQ_TERMCOND(s
, rm
->rm_list
)) {
147 CIRCLEQ_INSERT_TAIL(&rm
->rm_list
, r
, r_link
);
149 CIRCLEQ_INSERT_BEFORE(&rm
->rm_list
, s
, r
, r_link
);
152 lwkt_reltoken(&ilock
);
157 rman_fini(struct rman
*rm
)
162 lwkt_gettoken(&ilock
, rm
->rm_slock
);
163 CIRCLEQ_FOREACH(r
, &rm
->rm_list
, r_link
) {
164 if (r
->r_flags
& RF_ALLOCATED
) {
165 lwkt_reltoken(&ilock
);
171 * There really should only be one of these if we are in this
172 * state and the code is working properly, but it can't hurt.
174 while (!CIRCLEQ_EMPTY(&rm
->rm_list
)) {
175 r
= CIRCLEQ_FIRST(&rm
->rm_list
);
176 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
179 lwkt_reltoken(&ilock
);
180 /* XXX what's the point of this if we are going to free the struct? */
181 lwkt_gettoken(&ilock
, &rman_tok
);
182 TAILQ_REMOVE(&rman_head
, rm
, rm_link
);
183 lwkt_reltoken(&ilock
);
184 kfree(rm
->rm_slock
, M_RMAN
);
190 rman_reserve_resource(struct rman
*rm
, u_long start
, u_long end
, u_long count
,
191 u_int flags
, struct device
*dev
)
194 struct resource
*r
, *s
, *rv
;
200 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
201 "%#lx, flags %u, device %s\n", rm
->rm_descr
, start
, end
,
203 dev
== NULL
? "<null>" : device_get_nameunit(dev
)));
204 want_activate
= (flags
& RF_ACTIVE
);
207 lwkt_gettoken(&ilock
, rm
->rm_slock
);
209 for (r
= CIRCLEQ_FIRST(&rm
->rm_list
);
210 !CIRCLEQ_TERMCOND(r
, rm
->rm_list
) && r
->r_end
< start
;
211 r
= CIRCLEQ_NEXT(r
, r_link
))
214 if (CIRCLEQ_TERMCOND(r
, rm
->rm_list
)) {
215 DPRINTF(("could not find a region\n"));
220 * First try to find an acceptable totally-unshared region.
222 for (s
= r
; !CIRCLEQ_TERMCOND(s
, rm
->rm_list
);
223 s
= CIRCLEQ_NEXT(s
, r_link
)) {
224 DPRINTF(("considering [%#lx, %#lx]\n", s
->r_start
, s
->r_end
));
225 if (s
->r_start
> end
) {
226 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n",
230 if (s
->r_flags
& RF_ALLOCATED
) {
231 DPRINTF(("region is allocated\n"));
234 rstart
= max(s
->r_start
, start
);
235 rstart
= (rstart
+ ((1ul << RF_ALIGNMENT(flags
))) - 1) &
236 ~((1ul << RF_ALIGNMENT(flags
)) - 1);
237 rend
= min(s
->r_end
, max(start
+ count
, end
));
238 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
239 rstart
, rend
, (rend
- rstart
+ 1), count
));
241 if ((rend
- rstart
+ 1) >= count
) {
242 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
243 rstart
, rend
, (rend
- rstart
+ 1)));
244 if ((s
->r_end
- s
->r_start
+ 1) == count
) {
245 DPRINTF(("candidate region is entire chunk\n"));
247 rv
->r_flags
|= RF_ALLOCATED
| flags
;
253 * If s->r_start < rstart and
254 * s->r_end > rstart + count - 1, then
255 * we need to split the region into three pieces
256 * (the middle one will get returned to the user).
257 * Otherwise, we are allocating at either the
258 * beginning or the end of s, so we only need to
259 * split it in two. The first case requires
260 * two new allocations; the second requires but one.
262 rv
= kmalloc(sizeof *rv
, M_RMAN
, M_NOWAIT
);
265 bzero(rv
, sizeof *rv
);
266 rv
->r_start
= rstart
;
267 rv
->r_end
= rstart
+ count
- 1;
268 rv
->r_flags
= flags
| RF_ALLOCATED
;
273 if (s
->r_start
< rv
->r_start
&& s
->r_end
> rv
->r_end
) {
274 DPRINTF(("splitting region in three parts: "
275 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
276 s
->r_start
, rv
->r_start
- 1,
277 rv
->r_start
, rv
->r_end
,
278 rv
->r_end
+ 1, s
->r_end
));
280 * We are allocating in the middle.
282 r
= kmalloc(sizeof *r
, M_RMAN
, M_NOWAIT
);
289 r
->r_start
= rv
->r_end
+ 1;
291 r
->r_flags
= s
->r_flags
;
295 s
->r_end
= rv
->r_start
- 1;
296 CIRCLEQ_INSERT_AFTER(&rm
->rm_list
, s
, rv
,
298 CIRCLEQ_INSERT_AFTER(&rm
->rm_list
, rv
, r
,
300 } else if (s
->r_start
== rv
->r_start
) {
301 DPRINTF(("allocating from the beginning\n"));
303 * We are allocating at the beginning.
305 s
->r_start
= rv
->r_end
+ 1;
306 CIRCLEQ_INSERT_BEFORE(&rm
->rm_list
, s
, rv
,
309 DPRINTF(("allocating at the end\n"));
311 * We are allocating at the end.
313 s
->r_end
= rv
->r_start
- 1;
314 CIRCLEQ_INSERT_AFTER(&rm
->rm_list
, s
, rv
,
322 * Now find an acceptable shared region, if the client's requirements
323 * allow sharing. By our implementation restriction, a candidate
324 * region must match exactly by both size and sharing type in order
325 * to be considered compatible with the client's request. (The
326 * former restriction could probably be lifted without too much
327 * additional work, but this does not seem warranted.)
329 DPRINTF(("no unshared regions found\n"));
330 if ((flags
& (RF_SHAREABLE
| RF_TIMESHARE
)) == 0)
333 for (s
= r
; !CIRCLEQ_TERMCOND(s
, rm
->rm_list
);
334 s
= CIRCLEQ_NEXT(s
, r_link
)) {
335 if (s
->r_start
> end
)
337 if ((s
->r_flags
& flags
) != flags
)
339 rstart
= max(s
->r_start
, start
);
340 rend
= min(s
->r_end
, max(start
+ count
, end
));
341 if (s
->r_start
>= start
&& s
->r_end
<= end
342 && (s
->r_end
- s
->r_start
+ 1) == count
) {
343 rv
= kmalloc(sizeof *rv
, M_RMAN
, M_NOWAIT
);
346 bzero(rv
, sizeof *rv
);
347 rv
->r_start
= s
->r_start
;
348 rv
->r_end
= s
->r_end
;
349 rv
->r_flags
= s
->r_flags
&
350 (RF_ALLOCATED
| RF_SHAREABLE
| RF_TIMESHARE
);
353 if (s
->r_sharehead
== 0) {
354 s
->r_sharehead
= kmalloc(sizeof *s
->r_sharehead
,
356 if (s
->r_sharehead
== 0) {
361 bzero(s
->r_sharehead
, sizeof *s
->r_sharehead
);
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
);
391 lwkt_reltoken(&ilock
);
396 int_rman_activate_resource(struct rman
*rm
, struct resource
*r
,
397 struct resource
**whohas
)
403 * If we are not timesharing, then there is nothing much to do.
404 * If we already have the resource, then there is nothing at all to do.
405 * If we are not on a sharing list with anybody else, then there is
408 if ((r
->r_flags
& RF_TIMESHARE
) == 0
409 || (r
->r_flags
& RF_ACTIVE
) != 0
410 || r
->r_sharehead
== 0) {
411 r
->r_flags
|= RF_ACTIVE
;
416 for (s
= LIST_FIRST(r
->r_sharehead
); s
&& ok
;
417 s
= LIST_NEXT(s
, r_sharelink
)) {
418 if ((s
->r_flags
& RF_ACTIVE
) != 0) {
424 r
->r_flags
|= RF_ACTIVE
;
431 rman_activate_resource(struct resource
*r
)
434 struct resource
*whohas
;
439 lwkt_gettoken(&ilock
, rm
->rm_slock
);
440 rv
= int_rman_activate_resource(rm
, r
, &whohas
);
441 lwkt_reltoken(&ilock
);
449 rman_await_resource(struct resource
*r
, lwkt_tokref_t ilock
, int slpflags
, int timo
)
452 struct resource
*whohas
;
457 lwkt_gettoken(ilock
, rm
->rm_slock
);
458 rv
= int_rman_activate_resource(rm
, r
, &whohas
);
460 return (rv
); /* returns with ilock held */
462 if (r
->r_sharehead
== 0)
463 panic("rman_await_resource");
465 * A critical section will hopefully will prevent a race
466 * between lwkt_reltoken and tsleep where a process
467 * could conceivably get in and release the resource
468 * before we have a chance to sleep on it. YYY
471 whohas
->r_flags
|= RF_WANTED
;
472 rv
= tsleep(r
->r_sharehead
, slpflags
, "rmwait", timo
);
474 lwkt_reltoken(ilock
);
485 int_rman_deactivate_resource(struct resource
*r
)
490 r
->r_flags
&= ~RF_ACTIVE
;
491 if (r
->r_flags
& RF_WANTED
) {
492 r
->r_flags
&= ~RF_WANTED
;
493 wakeup(r
->r_sharehead
);
499 rman_deactivate_resource(struct resource
*r
)
505 lwkt_gettoken(&ilock
, rm
->rm_slock
);
506 int_rman_deactivate_resource(r
);
507 lwkt_reltoken(&ilock
);
512 int_rman_release_resource(struct rman
*rm
, struct resource
*r
)
514 struct resource
*s
, *t
;
516 if (r
->r_flags
& RF_ACTIVE
)
517 int_rman_deactivate_resource(r
);
520 * Check for a sharing list first. If there is one, then we don't
521 * have to think as hard.
523 if (r
->r_sharehead
) {
525 * If a sharing list exists, then we know there are at
528 * If we are in the main circleq, appoint someone else.
530 LIST_REMOVE(r
, r_sharelink
);
531 s
= LIST_FIRST(r
->r_sharehead
);
532 if (r
->r_flags
& RF_FIRSTSHARE
) {
533 s
->r_flags
|= RF_FIRSTSHARE
;
534 CIRCLEQ_INSERT_BEFORE(&rm
->rm_list
, r
, s
, r_link
);
535 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
539 * Make sure that the sharing list goes away completely
540 * if the resource is no longer being shared at all.
542 if (LIST_NEXT(s
, r_sharelink
) == 0) {
543 kfree(s
->r_sharehead
, M_RMAN
);
545 s
->r_flags
&= ~RF_FIRSTSHARE
;
551 * Look at the adjacent resources in the list and see if our
552 * segment can be merged with any of them.
554 s
= CIRCLEQ_PREV(r
, r_link
);
555 t
= CIRCLEQ_NEXT(r
, r_link
);
557 if (s
!= (void *)&rm
->rm_list
&& (s
->r_flags
& RF_ALLOCATED
) == 0
558 && t
!= (void *)&rm
->rm_list
&& (t
->r_flags
& RF_ALLOCATED
) == 0) {
560 * Merge all three segments.
563 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
564 CIRCLEQ_REMOVE(&rm
->rm_list
, t
, r_link
);
566 } else if (s
!= (void *)&rm
->rm_list
567 && (s
->r_flags
& RF_ALLOCATED
) == 0) {
569 * Merge previous segment with ours.
572 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
573 } else if (t
!= (void *)&rm
->rm_list
574 && (t
->r_flags
& RF_ALLOCATED
) == 0) {
576 * Merge next segment with ours.
578 t
->r_start
= r
->r_start
;
579 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
582 * At this point, we know there is nothing we
583 * can potentially merge with, because on each
584 * side, there is either nothing there or what is
585 * there is still allocated. In that case, we don't
586 * want to remove r from the list; we simply want to
587 * change it to an unallocated region and return
588 * without freeing anything.
590 r
->r_flags
&= ~RF_ALLOCATED
;
600 rman_release_resource(struct resource
*r
)
602 struct rman
*rm
= r
->r_rm
;
606 lwkt_gettoken(&ilock
, rm
->rm_slock
);
607 rv
= int_rman_release_resource(rm
, r
);
608 lwkt_reltoken(&ilock
);
613 rman_make_alignment_flags(uint32_t size
)
618 * Find the hightest bit set, and add one if more than one bit
619 * set. We're effectively computing the ceil(log2(size)) here.
621 for (i
= 32; i
> 0; i
--)
624 if (~(1 << i
) & size
)
627 return(RF_ALIGNMENT_LOG2(i
));