Fix path.
[dragonfly.git] / sys / kern / subr_rman.c
blob4685c74e899af785cc78e802a32f202dbc98174c
1 /*
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
14 * warranty.
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
27 * SUCH DAMAGE.
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.12 2007/05/20 07:43:24 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
41 * the allocation.
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
53 * implemented yet.
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
58 * permitted.
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/lock.h>
65 #include <sys/malloc.h>
66 #include <sys/bus.h> /* XXX debugging */
67 #include <sys/rman.h>
69 #ifdef RMAN_DEBUG
70 #define DPRINTF(params) kprintf params
71 #else
72 #define DPRINTF(params)
73 #endif
75 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
77 struct rman_head rman_head;
78 static struct lwkt_token rman_tok; /* mutex to protect rman_head */
79 static int int_rman_activate_resource(struct rman *rm, struct resource *r,
80 struct resource **whohas);
81 static int int_rman_deactivate_resource(struct resource *r);
82 static int int_rman_release_resource(struct rman *rm, struct resource *r);
84 #define CIRCLEQ_TERMCOND(var, head) (var == (void *)&(head))
86 int
87 rman_init(struct rman *rm)
89 static int once;
90 lwkt_tokref ilock;
92 if (once == 0) {
93 once = 1;
94 TAILQ_INIT(&rman_head);
95 lwkt_token_init(&rman_tok);
98 if (rm->rm_type == RMAN_UNINIT)
99 panic("rman_init");
100 if (rm->rm_type == RMAN_GAUGE)
101 panic("implement RMAN_GAUGE");
103 CIRCLEQ_INIT(&rm->rm_list);
104 rm->rm_slock = kmalloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT);
105 if (rm->rm_slock == NULL)
106 return ENOMEM;
107 lwkt_token_init(rm->rm_slock);
109 lwkt_gettoken(&ilock, &rman_tok);
110 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
111 lwkt_reltoken(&ilock);
112 return 0;
116 * NB: this interface is not robust against programming errors which
117 * add multiple copies of the same region.
120 rman_manage_region(struct rman *rm, u_long start, u_long end)
122 struct resource *r, *s;
123 lwkt_tokref ilock;
125 DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
126 rm->rm_descr, start, end));
127 r = kmalloc(sizeof *r, M_RMAN, M_NOWAIT);
128 if (r == 0)
129 return ENOMEM;
130 bzero(r, sizeof *r);
131 r->r_sharehead = 0;
132 r->r_start = start;
133 r->r_end = end;
134 r->r_flags = 0;
135 r->r_dev = 0;
136 r->r_rm = rm;
138 lwkt_gettoken(&ilock, rm->rm_slock);
139 for (s = CIRCLEQ_FIRST(&rm->rm_list);
140 !CIRCLEQ_TERMCOND(s, rm->rm_list) && s->r_end < r->r_start;
141 s = CIRCLEQ_NEXT(s, r_link))
144 if (CIRCLEQ_TERMCOND(s, rm->rm_list)) {
145 CIRCLEQ_INSERT_TAIL(&rm->rm_list, r, r_link);
146 } else {
147 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, r, r_link);
150 lwkt_reltoken(&ilock);
151 return 0;
155 rman_fini(struct rman *rm)
157 struct resource *r;
158 lwkt_tokref ilock;
160 lwkt_gettoken(&ilock, rm->rm_slock);
161 CIRCLEQ_FOREACH(r, &rm->rm_list, r_link) {
162 if (r->r_flags & RF_ALLOCATED) {
163 lwkt_reltoken(&ilock);
164 return EBUSY;
169 * There really should only be one of these if we are in this
170 * state and the code is working properly, but it can't hurt.
172 while (!CIRCLEQ_EMPTY(&rm->rm_list)) {
173 r = CIRCLEQ_FIRST(&rm->rm_list);
174 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
175 kfree(r, M_RMAN);
177 lwkt_reltoken(&ilock);
178 /* XXX what's the point of this if we are going to free the struct? */
179 lwkt_gettoken(&ilock, &rman_tok);
180 TAILQ_REMOVE(&rman_head, rm, rm_link);
181 lwkt_reltoken(&ilock);
182 kfree(rm->rm_slock, M_RMAN);
184 return 0;
187 struct resource *
188 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
189 u_int flags, struct device *dev)
191 u_int want_activate;
192 struct resource *r, *s, *rv;
193 u_long rstart, rend;
194 lwkt_tokref ilock;
196 rv = 0;
198 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
199 "%#lx, flags %u, device %s\n", rm->rm_descr, start, end,
200 count, flags,
201 dev == NULL ? "<null>" : device_get_nameunit(dev)));
202 want_activate = (flags & RF_ACTIVE);
203 flags &= ~RF_ACTIVE;
205 lwkt_gettoken(&ilock, rm->rm_slock);
207 for (r = CIRCLEQ_FIRST(&rm->rm_list);
208 !CIRCLEQ_TERMCOND(r, rm->rm_list) && r->r_end < start;
209 r = CIRCLEQ_NEXT(r, r_link))
212 if (CIRCLEQ_TERMCOND(r, rm->rm_list)) {
213 DPRINTF(("could not find a region\n"));
214 goto out;
218 * First try to find an acceptable totally-unshared region.
220 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
221 s = CIRCLEQ_NEXT(s, r_link)) {
222 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
223 if (s->r_start > end) {
224 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n",
225 s->r_start, end));
226 break;
228 if (s->r_flags & RF_ALLOCATED) {
229 DPRINTF(("region is allocated\n"));
230 continue;
232 rstart = max(s->r_start, start);
233 rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
234 ~((1ul << RF_ALIGNMENT(flags)) - 1);
235 rend = min(s->r_end, max(start + count, end));
236 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
237 rstart, rend, (rend - rstart + 1), count));
239 if ((rend - rstart + 1) >= count) {
240 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
241 rstart, rend, (rend - rstart + 1)));
242 if ((s->r_end - s->r_start + 1) == count) {
243 DPRINTF(("candidate region is entire chunk\n"));
244 rv = s;
245 rv->r_flags |= RF_ALLOCATED | flags;
246 rv->r_dev = dev;
247 goto out;
251 * If s->r_start < rstart and
252 * s->r_end > rstart + count - 1, then
253 * we need to split the region into three pieces
254 * (the middle one will get returned to the user).
255 * Otherwise, we are allocating at either the
256 * beginning or the end of s, so we only need to
257 * split it in two. The first case requires
258 * two new allocations; the second requires but one.
260 rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT);
261 if (rv == 0)
262 goto out;
263 bzero(rv, sizeof *rv);
264 rv->r_start = rstart;
265 rv->r_end = rstart + count - 1;
266 rv->r_flags = flags | RF_ALLOCATED;
267 rv->r_dev = dev;
268 rv->r_sharehead = 0;
269 rv->r_rm = rm;
271 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
272 DPRINTF(("splitting region in three parts: "
273 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
274 s->r_start, rv->r_start - 1,
275 rv->r_start, rv->r_end,
276 rv->r_end + 1, s->r_end));
278 * We are allocating in the middle.
280 r = kmalloc(sizeof *r, M_RMAN, M_NOWAIT);
281 if (r == 0) {
282 kfree(rv, M_RMAN);
283 rv = 0;
284 goto out;
286 bzero(r, sizeof *r);
287 r->r_start = rv->r_end + 1;
288 r->r_end = s->r_end;
289 r->r_flags = s->r_flags;
290 r->r_dev = 0;
291 r->r_sharehead = 0;
292 r->r_rm = rm;
293 s->r_end = rv->r_start - 1;
294 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
295 r_link);
296 CIRCLEQ_INSERT_AFTER(&rm->rm_list, rv, r,
297 r_link);
298 } else if (s->r_start == rv->r_start) {
299 DPRINTF(("allocating from the beginning\n"));
301 * We are allocating at the beginning.
303 s->r_start = rv->r_end + 1;
304 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, rv,
305 r_link);
306 } else {
307 DPRINTF(("allocating at the end\n"));
309 * We are allocating at the end.
311 s->r_end = rv->r_start - 1;
312 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
313 r_link);
315 goto out;
320 * Now find an acceptable shared region, if the client's requirements
321 * allow sharing. By our implementation restriction, a candidate
322 * region must match exactly by both size and sharing type in order
323 * to be considered compatible with the client's request. (The
324 * former restriction could probably be lifted without too much
325 * additional work, but this does not seem warranted.)
327 DPRINTF(("no unshared regions found\n"));
328 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
329 goto out;
331 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
332 s = CIRCLEQ_NEXT(s, r_link)) {
333 if (s->r_start > end)
334 break;
335 if ((s->r_flags & flags) != flags)
336 continue;
337 rstart = max(s->r_start, start);
338 rend = min(s->r_end, max(start + count, end));
339 if (s->r_start >= start && s->r_end <= end
340 && (s->r_end - s->r_start + 1) == count) {
341 rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT);
342 if (rv == 0)
343 goto out;
344 bzero(rv, sizeof *rv);
345 rv->r_start = s->r_start;
346 rv->r_end = s->r_end;
347 rv->r_flags = s->r_flags &
348 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
349 rv->r_dev = dev;
350 rv->r_rm = rm;
351 if (s->r_sharehead == 0) {
352 s->r_sharehead = kmalloc(sizeof *s->r_sharehead,
353 M_RMAN, M_NOWAIT);
354 if (s->r_sharehead == 0) {
355 kfree(rv, M_RMAN);
356 rv = 0;
357 goto out;
359 bzero(s->r_sharehead, sizeof *s->r_sharehead);
360 LIST_INIT(s->r_sharehead);
361 LIST_INSERT_HEAD(s->r_sharehead, s,
362 r_sharelink);
363 s->r_flags |= RF_FIRSTSHARE;
365 rv->r_sharehead = s->r_sharehead;
366 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
367 goto out;
372 * We couldn't find anything.
374 out:
376 * If the user specified RF_ACTIVE in the initial flags,
377 * which is reflected in `want_activate', we attempt to atomically
378 * activate the resource. If this fails, we release the resource
379 * and indicate overall failure. (This behavior probably doesn't
380 * make sense for RF_TIMESHARE-type resources.)
382 if (rv && want_activate) {
383 struct resource *whohas;
384 if (int_rman_activate_resource(rm, rv, &whohas)) {
385 int_rman_release_resource(rm, rv);
386 rv = 0;
389 lwkt_reltoken(&ilock);
390 return (rv);
393 static int
394 int_rman_activate_resource(struct rman *rm, struct resource *r,
395 struct resource **whohas)
397 struct resource *s;
398 int ok;
401 * If we are not timesharing, then there is nothing much to do.
402 * If we already have the resource, then there is nothing at all to do.
403 * If we are not on a sharing list with anybody else, then there is
404 * little to do.
406 if ((r->r_flags & RF_TIMESHARE) == 0
407 || (r->r_flags & RF_ACTIVE) != 0
408 || r->r_sharehead == 0) {
409 r->r_flags |= RF_ACTIVE;
410 return 0;
413 ok = 1;
414 for (s = LIST_FIRST(r->r_sharehead); s && ok;
415 s = LIST_NEXT(s, r_sharelink)) {
416 if ((s->r_flags & RF_ACTIVE) != 0) {
417 ok = 0;
418 *whohas = s;
421 if (ok) {
422 r->r_flags |= RF_ACTIVE;
423 return 0;
425 return EBUSY;
429 rman_activate_resource(struct resource *r)
431 int rv;
432 struct resource *whohas;
433 lwkt_tokref ilock;
434 struct rman *rm;
436 rm = r->r_rm;
437 lwkt_gettoken(&ilock, rm->rm_slock);
438 rv = int_rman_activate_resource(rm, r, &whohas);
439 lwkt_reltoken(&ilock);
440 return rv;
443 #if 0
445 /* XXX */
447 rman_await_resource(struct resource *r, lwkt_tokref_t ilock, int slpflags, int timo)
449 int rv;
450 struct resource *whohas;
451 struct rman *rm;
453 rm = r->r_rm;
454 for (;;) {
455 lwkt_gettoken(ilock, rm->rm_slock);
456 rv = int_rman_activate_resource(rm, r, &whohas);
457 if (rv != EBUSY)
458 return (rv); /* returns with ilock held */
460 if (r->r_sharehead == 0)
461 panic("rman_await_resource");
463 * A critical section will hopefully will prevent a race
464 * between lwkt_reltoken and tsleep where a process
465 * could conceivably get in and release the resource
466 * before we have a chance to sleep on it. YYY
468 crit_enter();
469 whohas->r_flags |= RF_WANTED;
470 rv = tsleep(r->r_sharehead, slpflags, "rmwait", timo);
471 if (rv) {
472 lwkt_reltoken(ilock);
473 crit_exit();
474 return rv;
476 crit_exit();
480 #endif
482 static int
483 int_rman_deactivate_resource(struct resource *r)
485 struct rman *rm;
487 rm = r->r_rm;
488 r->r_flags &= ~RF_ACTIVE;
489 if (r->r_flags & RF_WANTED) {
490 r->r_flags &= ~RF_WANTED;
491 wakeup(r->r_sharehead);
493 return 0;
497 rman_deactivate_resource(struct resource *r)
499 lwkt_tokref ilock;
500 struct rman *rm;
502 rm = r->r_rm;
503 lwkt_gettoken(&ilock, rm->rm_slock);
504 int_rman_deactivate_resource(r);
505 lwkt_reltoken(&ilock);
506 return 0;
509 static int
510 int_rman_release_resource(struct rman *rm, struct resource *r)
512 struct resource *s, *t;
514 if (r->r_flags & RF_ACTIVE)
515 int_rman_deactivate_resource(r);
518 * Check for a sharing list first. If there is one, then we don't
519 * have to think as hard.
521 if (r->r_sharehead) {
523 * If a sharing list exists, then we know there are at
524 * least two sharers.
526 * If we are in the main circleq, appoint someone else.
528 LIST_REMOVE(r, r_sharelink);
529 s = LIST_FIRST(r->r_sharehead);
530 if (r->r_flags & RF_FIRSTSHARE) {
531 s->r_flags |= RF_FIRSTSHARE;
532 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, r, s, r_link);
533 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
537 * Make sure that the sharing list goes away completely
538 * if the resource is no longer being shared at all.
540 if (LIST_NEXT(s, r_sharelink) == 0) {
541 kfree(s->r_sharehead, M_RMAN);
542 s->r_sharehead = 0;
543 s->r_flags &= ~RF_FIRSTSHARE;
545 goto out;
549 * Look at the adjacent resources in the list and see if our
550 * segment can be merged with any of them.
552 s = CIRCLEQ_PREV(r, r_link);
553 t = CIRCLEQ_NEXT(r, r_link);
555 if (s != (void *)&rm->rm_list && (s->r_flags & RF_ALLOCATED) == 0
556 && t != (void *)&rm->rm_list && (t->r_flags & RF_ALLOCATED) == 0) {
558 * Merge all three segments.
560 s->r_end = t->r_end;
561 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
562 CIRCLEQ_REMOVE(&rm->rm_list, t, r_link);
563 kfree(t, M_RMAN);
564 } else if (s != (void *)&rm->rm_list
565 && (s->r_flags & RF_ALLOCATED) == 0) {
567 * Merge previous segment with ours.
569 s->r_end = r->r_end;
570 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
571 } else if (t != (void *)&rm->rm_list
572 && (t->r_flags & RF_ALLOCATED) == 0) {
574 * Merge next segment with ours.
576 t->r_start = r->r_start;
577 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
578 } else {
580 * At this point, we know there is nothing we
581 * can potentially merge with, because on each
582 * side, there is either nothing there or what is
583 * there is still allocated. In that case, we don't
584 * want to remove r from the list; we simply want to
585 * change it to an unallocated region and return
586 * without freeing anything.
588 r->r_flags &= ~RF_ALLOCATED;
589 return 0;
592 out:
593 kfree(r, M_RMAN);
594 return 0;
598 rman_release_resource(struct resource *r)
600 struct rman *rm = r->r_rm;
601 lwkt_tokref ilock;
602 int rv;
604 lwkt_gettoken(&ilock, rm->rm_slock);
605 rv = int_rman_release_resource(rm, r);
606 lwkt_reltoken(&ilock);
607 return (rv);
610 uint32_t
611 rman_make_alignment_flags(uint32_t size)
613 int i;
616 * Find the hightest bit set, and add one if more than one bit
617 * set. We're effectively computing the ceil(log2(size)) here.
619 for (i = 32; i > 0; i--)
620 if ((1 << i) & size)
621 break;
622 if (~(1 << i) & size)
623 i++;
625 return(RF_ALIGNMENT_LOG2(i));