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.6 2004/03/01 06:33:17 dillon 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 */
67 #include <machine/bus.h>
70 static MALLOC_DEFINE(M_RMAN
, "rman", "Resource manager");
72 struct rman_head rman_head
;
73 static struct lwkt_token rman_tok
; /* mutex to protect rman_head */
74 static int int_rman_activate_resource(struct rman
*rm
, struct resource
*r
,
75 struct resource
**whohas
);
76 static int int_rman_deactivate_resource(struct resource
*r
);
77 static int int_rman_release_resource(struct rman
*rm
, struct resource
*r
);
79 #define CIRCLEQ_TERMCOND(var, head) (var == (void *)&(head))
82 rman_init(struct rman
*rm
)
89 TAILQ_INIT(&rman_head
);
90 lwkt_token_init(&rman_tok
);
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
== NULL
)
102 lwkt_token_init(rm
->rm_slock
);
104 lwkt_gettoken(&ilock
, &rman_tok
);
105 TAILQ_INSERT_TAIL(&rman_head
, rm
, rm_link
);
106 lwkt_reltoken(&ilock
);
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
;
120 r
= malloc(sizeof *r
, M_RMAN
, M_NOWAIT
);
131 lwkt_gettoken(&ilock
, rm
->rm_slock
);
132 for (s
= CIRCLEQ_FIRST(&rm
->rm_list
);
133 !CIRCLEQ_TERMCOND(s
, rm
->rm_list
) && s
->r_end
< r
->r_start
;
134 s
= CIRCLEQ_NEXT(s
, r_link
))
137 if (CIRCLEQ_TERMCOND(s
, rm
->rm_list
)) {
138 CIRCLEQ_INSERT_TAIL(&rm
->rm_list
, r
, r_link
);
140 CIRCLEQ_INSERT_BEFORE(&rm
->rm_list
, s
, r
, r_link
);
143 lwkt_reltoken(&ilock
);
148 rman_fini(struct rman
*rm
)
153 lwkt_gettoken(&ilock
, rm
->rm_slock
);
154 CIRCLEQ_FOREACH(r
, &rm
->rm_list
, r_link
) {
155 if (r
->r_flags
& RF_ALLOCATED
) {
156 lwkt_reltoken(&ilock
);
162 * There really should only be one of these if we are in this
163 * state and the code is working properly, but it can't hurt.
165 while (!CIRCLEQ_EMPTY(&rm
->rm_list
)) {
166 r
= CIRCLEQ_FIRST(&rm
->rm_list
);
167 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
170 lwkt_reltoken(&ilock
);
171 /* XXX what's the point of this if we are going to free the struct? */
172 lwkt_gettoken(&ilock
, &rman_tok
);
173 TAILQ_REMOVE(&rman_head
, rm
, rm_link
);
174 lwkt_reltoken(&ilock
);
175 free(rm
->rm_slock
, M_RMAN
);
181 rman_reserve_resource(struct rman
*rm
, u_long start
, u_long end
, u_long count
,
182 u_int flags
, struct device
*dev
)
185 struct resource
*r
, *s
, *rv
;
192 printf("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
193 "%#lx, flags %u, device %s%d\n", rm
->rm_descr
, start
, end
,
194 count
, flags
, device_get_name(dev
), device_get_unit(dev
));
195 #endif /* RMAN_DEBUG */
196 want_activate
= (flags
& RF_ACTIVE
);
199 lwkt_gettoken(&ilock
, rm
->rm_slock
);
201 for (r
= CIRCLEQ_FIRST(&rm
->rm_list
);
202 !CIRCLEQ_TERMCOND(r
, rm
->rm_list
) && r
->r_end
< start
;
203 r
= CIRCLEQ_NEXT(r
, r_link
))
206 if (CIRCLEQ_TERMCOND(r
, rm
->rm_list
)) {
208 printf("could not find a region\n");
214 * First try to find an acceptable totally-unshared region.
216 for (s
= r
; !CIRCLEQ_TERMCOND(s
, rm
->rm_list
);
217 s
= CIRCLEQ_NEXT(s
, r_link
)) {
219 printf("considering [%#lx, %#lx]\n", s
->r_start
, s
->r_end
);
220 #endif /* RMAN_DEBUG */
221 if (s
->r_start
> end
) {
223 printf("s->r_start (%#lx) > end (%#lx)\n", s
->r_start
, end
);
224 #endif /* RMAN_DEBUG */
227 if (s
->r_flags
& RF_ALLOCATED
) {
229 printf("region is allocated\n");
230 #endif /* RMAN_DEBUG */
233 rstart
= max(s
->r_start
, start
);
234 rstart
= (rstart
+ ((1ul << RF_ALIGNMENT(flags
))) - 1) &
235 ~((1ul << RF_ALIGNMENT(flags
)) - 1);
236 rend
= min(s
->r_end
, max(start
+ count
, end
));
238 printf("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
239 rstart
, rend
, (rend
- rstart
+ 1), count
);
240 #endif /* RMAN_DEBUG */
242 if ((rend
- rstart
+ 1) >= count
) {
244 printf("candidate region: [%#lx, %#lx], size %#lx\n",
245 rend
, rstart
, (rend
- rstart
+ 1));
246 #endif /* RMAN_DEBUG */
247 if ((s
->r_end
- s
->r_start
+ 1) == count
) {
249 printf("candidate region is entire chunk\n");
250 #endif /* RMAN_DEBUG */
252 rv
->r_flags
|= RF_ALLOCATED
| flags
;
258 * If s->r_start < rstart and
259 * s->r_end > rstart + count - 1, then
260 * we need to split the region into three pieces
261 * (the middle one will get returned to the user).
262 * Otherwise, we are allocating at either the
263 * beginning or the end of s, so we only need to
264 * split it in two. The first case requires
265 * two new allocations; the second requires but one.
267 rv
= malloc(sizeof *rv
, M_RMAN
, M_NOWAIT
);
270 bzero(rv
, sizeof *rv
);
271 rv
->r_start
= rstart
;
272 rv
->r_end
= rstart
+ count
- 1;
273 rv
->r_flags
= flags
| RF_ALLOCATED
;
278 if (s
->r_start
< rv
->r_start
&& s
->r_end
> rv
->r_end
) {
280 printf("splitting region in three parts: "
281 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
282 s
->r_start
, rv
->r_start
- 1,
283 rv
->r_start
, rv
->r_end
,
284 rv
->r_end
+ 1, s
->r_end
);
285 #endif /* RMAN_DEBUG */
287 * We are allocating in the middle.
289 r
= malloc(sizeof *r
, M_RMAN
, M_NOWAIT
);
296 r
->r_start
= rv
->r_end
+ 1;
298 r
->r_flags
= s
->r_flags
;
302 s
->r_end
= rv
->r_start
- 1;
303 CIRCLEQ_INSERT_AFTER(&rm
->rm_list
, s
, rv
,
305 CIRCLEQ_INSERT_AFTER(&rm
->rm_list
, rv
, r
,
307 } else if (s
->r_start
== rv
->r_start
) {
309 printf("allocating from the beginning\n");
310 #endif /* RMAN_DEBUG */
312 * We are allocating at the beginning.
314 s
->r_start
= rv
->r_end
+ 1;
315 CIRCLEQ_INSERT_BEFORE(&rm
->rm_list
, s
, rv
,
319 printf("allocating at the end\n");
320 #endif /* RMAN_DEBUG */
322 * We are allocating at the end.
324 s
->r_end
= rv
->r_start
- 1;
325 CIRCLEQ_INSERT_AFTER(&rm
->rm_list
, s
, rv
,
333 * Now find an acceptable shared region, if the client's requirements
334 * allow sharing. By our implementation restriction, a candidate
335 * region must match exactly by both size and sharing type in order
336 * to be considered compatible with the client's request. (The
337 * former restriction could probably be lifted without too much
338 * additional work, but this does not seem warranted.)
341 printf("no unshared regions found\n");
342 #endif /* RMAN_DEBUG */
343 if ((flags
& (RF_SHAREABLE
| RF_TIMESHARE
)) == 0)
346 for (s
= r
; !CIRCLEQ_TERMCOND(s
, rm
->rm_list
);
347 s
= CIRCLEQ_NEXT(s
, r_link
)) {
348 if (s
->r_start
> end
)
350 if ((s
->r_flags
& flags
) != flags
)
352 rstart
= max(s
->r_start
, start
);
353 rend
= min(s
->r_end
, max(start
+ count
, end
));
354 if (s
->r_start
>= start
&& s
->r_end
<= end
355 && (s
->r_end
- s
->r_start
+ 1) == count
) {
356 rv
= malloc(sizeof *rv
, M_RMAN
, M_NOWAIT
);
359 bzero(rv
, sizeof *rv
);
360 rv
->r_start
= s
->r_start
;
361 rv
->r_end
= s
->r_end
;
362 rv
->r_flags
= s
->r_flags
&
363 (RF_ALLOCATED
| RF_SHAREABLE
| RF_TIMESHARE
);
366 if (s
->r_sharehead
== 0) {
367 s
->r_sharehead
= malloc(sizeof *s
->r_sharehead
,
369 if (s
->r_sharehead
== 0) {
374 bzero(s
->r_sharehead
, sizeof *s
->r_sharehead
);
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
*whohas
;
399 if (int_rman_activate_resource(rm
, rv
, &whohas
)) {
400 int_rman_release_resource(rm
, rv
);
404 lwkt_reltoken(&ilock
);
409 int_rman_activate_resource(struct rman
*rm
, struct resource
*r
,
410 struct resource
**whohas
)
416 * If we are not timesharing, then there is nothing much to do.
417 * If we already have the resource, then there is nothing at all to do.
418 * If we are not on a sharing list with anybody else, then there is
421 if ((r
->r_flags
& RF_TIMESHARE
) == 0
422 || (r
->r_flags
& RF_ACTIVE
) != 0
423 || r
->r_sharehead
== 0) {
424 r
->r_flags
|= RF_ACTIVE
;
429 for (s
= LIST_FIRST(r
->r_sharehead
); s
&& ok
;
430 s
= LIST_NEXT(s
, r_sharelink
)) {
431 if ((s
->r_flags
& RF_ACTIVE
) != 0) {
437 r
->r_flags
|= RF_ACTIVE
;
444 rman_activate_resource(struct resource
*r
)
447 struct resource
*whohas
;
452 lwkt_gettoken(&ilock
, rm
->rm_slock
);
453 rv
= int_rman_activate_resource(rm
, r
, &whohas
);
454 lwkt_reltoken(&ilock
);
462 rman_await_resource(struct resource
*r
, lwkt_tokref_t ilock
, int slpflags
, int timo
)
465 struct resource
*whohas
;
470 lwkt_gettoken(ilock
, rm
->rm_slock
);
471 rv
= int_rman_activate_resource(rm
, r
, &whohas
);
473 return (rv
); /* returns with ilock held */
475 if (r
->r_sharehead
== 0)
476 panic("rman_await_resource");
478 * splhigh hopefully will prevent a race between
479 * lwkt_reltoken and tsleep where a process
480 * could conceivably get in and release the resource
481 * before we have a chance to sleep on it. YYY
484 whohas
->r_flags
|= RF_WANTED
;
485 rv
= tsleep(r
->r_sharehead
, slpflags
, "rmwait", timo
);
487 lwkt_reltoken(ilock
);
498 int_rman_deactivate_resource(struct resource
*r
)
503 r
->r_flags
&= ~RF_ACTIVE
;
504 if (r
->r_flags
& RF_WANTED
) {
505 r
->r_flags
&= ~RF_WANTED
;
506 wakeup(r
->r_sharehead
);
512 rman_deactivate_resource(struct resource
*r
)
518 lwkt_gettoken(&ilock
, rm
->rm_slock
);
519 int_rman_deactivate_resource(r
);
520 lwkt_reltoken(&ilock
);
525 int_rman_release_resource(struct rman
*rm
, struct resource
*r
)
527 struct resource
*s
, *t
;
529 if (r
->r_flags
& RF_ACTIVE
)
530 int_rman_deactivate_resource(r
);
533 * Check for a sharing list first. If there is one, then we don't
534 * have to think as hard.
536 if (r
->r_sharehead
) {
538 * If a sharing list exists, then we know there are at
541 * If we are in the main circleq, appoint someone else.
543 LIST_REMOVE(r
, r_sharelink
);
544 s
= LIST_FIRST(r
->r_sharehead
);
545 if (r
->r_flags
& RF_FIRSTSHARE
) {
546 s
->r_flags
|= RF_FIRSTSHARE
;
547 CIRCLEQ_INSERT_BEFORE(&rm
->rm_list
, r
, s
, r_link
);
548 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
552 * Make sure that the sharing list goes away completely
553 * if the resource is no longer being shared at all.
555 if (LIST_NEXT(s
, r_sharelink
) == 0) {
556 free(s
->r_sharehead
, M_RMAN
);
558 s
->r_flags
&= ~RF_FIRSTSHARE
;
564 * Look at the adjacent resources in the list and see if our
565 * segment can be merged with any of them.
567 s
= CIRCLEQ_PREV(r
, r_link
);
568 t
= CIRCLEQ_NEXT(r
, r_link
);
570 if (s
!= (void *)&rm
->rm_list
&& (s
->r_flags
& RF_ALLOCATED
) == 0
571 && t
!= (void *)&rm
->rm_list
&& (t
->r_flags
& RF_ALLOCATED
) == 0) {
573 * Merge all three segments.
576 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
577 CIRCLEQ_REMOVE(&rm
->rm_list
, t
, r_link
);
579 } else if (s
!= (void *)&rm
->rm_list
580 && (s
->r_flags
& RF_ALLOCATED
) == 0) {
582 * Merge previous segment with ours.
585 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
586 } else if (t
!= (void *)&rm
->rm_list
587 && (t
->r_flags
& RF_ALLOCATED
) == 0) {
589 * Merge next segment with ours.
591 t
->r_start
= r
->r_start
;
592 CIRCLEQ_REMOVE(&rm
->rm_list
, r
, r_link
);
595 * At this point, we know there is nothing we
596 * can potentially merge with, because on each
597 * side, there is either nothing there or what is
598 * there is still allocated. In that case, we don't
599 * want to remove r from the list; we simply want to
600 * change it to an unallocated region and return
601 * without freeing anything.
603 r
->r_flags
&= ~RF_ALLOCATED
;
613 rman_release_resource(struct resource
*r
)
615 struct rman
*rm
= r
->r_rm
;
619 lwkt_gettoken(&ilock
, rm
->rm_slock
);
620 rv
= int_rman_release_resource(rm
, r
);
621 lwkt_reltoken(&ilock
);
626 rman_make_alignment_flags(uint32_t size
)
631 * Find the hightest bit set, and add one if more than one bit
632 * set. We're effectively computing the ceil(log2(size)) here.
634 for (i
= 32; i
> 0; i
--)
637 if (~(1 << i
) & size
)
640 return(RF_ALIGNMENT_LOG2(i
));