We only need to determine the softlink_dir when no filesystem is
[dragonfly.git] / sys / kern / subr_rman.c
blob86d0aede55f4730fcf5de87cf6785ff9d66810f9
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.14 2008/01/05 14:02:38 swildner 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>
68 #include <sys/sysctl.h>
70 int rman_debug = 0;
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))
88 int
89 rman_init(struct rman *rm)
91 static int once;
92 lwkt_tokref ilock;
94 if (once == 0) {
95 once = 1;
96 TAILQ_INIT(&rman_head);
97 lwkt_token_init(&rman_tok);
100 if (rm->rm_type == RMAN_UNINIT)
101 panic("rman_init");
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)
108 return ENOMEM;
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);
114 return 0;
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;
125 lwkt_tokref ilock;
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 | M_ZERO);
130 if (r == 0)
131 return ENOMEM;
132 r->r_sharehead = 0;
133 r->r_start = start;
134 r->r_end = end;
135 r->r_flags = 0;
136 r->r_dev = 0;
137 r->r_rm = rm;
139 lwkt_gettoken(&ilock, rm->rm_slock);
140 for (s = CIRCLEQ_FIRST(&rm->rm_list);
141 !CIRCLEQ_TERMCOND(s, rm->rm_list) && s->r_end < r->r_start;
142 s = CIRCLEQ_NEXT(s, r_link))
145 if (CIRCLEQ_TERMCOND(s, rm->rm_list)) {
146 CIRCLEQ_INSERT_TAIL(&rm->rm_list, r, r_link);
147 } else {
148 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, r, r_link);
151 lwkt_reltoken(&ilock);
152 return 0;
156 rman_fini(struct rman *rm)
158 struct resource *r;
159 lwkt_tokref ilock;
161 lwkt_gettoken(&ilock, rm->rm_slock);
162 CIRCLEQ_FOREACH(r, &rm->rm_list, r_link) {
163 if (r->r_flags & RF_ALLOCATED) {
164 lwkt_reltoken(&ilock);
165 return EBUSY;
170 * There really should only be one of these if we are in this
171 * state and the code is working properly, but it can't hurt.
173 while (!CIRCLEQ_EMPTY(&rm->rm_list)) {
174 r = CIRCLEQ_FIRST(&rm->rm_list);
175 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
176 kfree(r, M_RMAN);
178 lwkt_reltoken(&ilock);
179 /* XXX what's the point of this if we are going to free the struct? */
180 lwkt_gettoken(&ilock, &rman_tok);
181 TAILQ_REMOVE(&rman_head, rm, rm_link);
182 lwkt_reltoken(&ilock);
183 kfree(rm->rm_slock, M_RMAN);
185 return 0;
188 struct resource *
189 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
190 u_int flags, struct device *dev)
192 u_int want_activate;
193 struct resource *r, *s, *rv;
194 u_long rstart, rend;
195 lwkt_tokref ilock;
197 rv = 0;
199 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
200 "%#lx, flags %u, device %s\n", rm->rm_descr, start, end,
201 count, flags,
202 dev == NULL ? "<null>" : device_get_nameunit(dev)));
203 want_activate = (flags & RF_ACTIVE);
204 flags &= ~RF_ACTIVE;
206 lwkt_gettoken(&ilock, rm->rm_slock);
208 for (r = CIRCLEQ_FIRST(&rm->rm_list);
209 !CIRCLEQ_TERMCOND(r, rm->rm_list) && r->r_end < start;
210 r = CIRCLEQ_NEXT(r, r_link))
213 if (CIRCLEQ_TERMCOND(r, rm->rm_list)) {
214 DPRINTF(("could not find a region\n"));
215 goto out;
219 * First try to find an acceptable totally-unshared region.
221 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list);
222 s = CIRCLEQ_NEXT(s, r_link)) {
223 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
224 if (s->r_start > end) {
225 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n",
226 s->r_start, end));
227 break;
229 if (s->r_flags & RF_ALLOCATED) {
230 DPRINTF(("region is allocated\n"));
231 continue;
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));
237 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
238 rstart, rend, (rend - rstart + 1), count));
240 if ((rend - rstart + 1) >= count) {
241 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
242 rstart, rend, (rend - rstart + 1)));
243 if ((s->r_end - s->r_start + 1) == count) {
244 DPRINTF(("candidate region is entire chunk\n"));
245 rv = s;
246 rv->r_flags |= RF_ALLOCATED | flags;
247 rv->r_dev = dev;
248 goto out;
252 * If s->r_start < rstart and
253 * s->r_end > rstart + count - 1, then
254 * we need to split the region into three pieces
255 * (the middle one will get returned to the user).
256 * Otherwise, we are allocating at either the
257 * beginning or the end of s, so we only need to
258 * split it in two. The first case requires
259 * two new allocations; the second requires but one.
261 rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
262 if (rv == 0)
263 goto out;
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,
281 M_NOWAIT | M_ZERO);
282 if (r == 0) {
283 kfree(rv, M_RMAN);
284 rv = 0;
285 goto out;
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 | M_ZERO);
342 if (rv == 0)
343 goto out;
344 rv->r_start = s->r_start;
345 rv->r_end = s->r_end;
346 rv->r_flags = s->r_flags &
347 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
348 rv->r_dev = dev;
349 rv->r_rm = rm;
350 if (s->r_sharehead == 0) {
351 s->r_sharehead = kmalloc(sizeof *s->r_sharehead,
352 M_RMAN,
353 M_NOWAIT | M_ZERO);
354 if (s->r_sharehead == 0) {
355 kfree(rv, M_RMAN);
356 rv = 0;
357 goto out;
359 LIST_INIT(s->r_sharehead);
360 LIST_INSERT_HEAD(s->r_sharehead, s,
361 r_sharelink);
362 s->r_flags |= RF_FIRSTSHARE;
364 rv->r_sharehead = s->r_sharehead;
365 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
366 goto out;
371 * We couldn't find anything.
373 out:
375 * If the user specified RF_ACTIVE in the initial flags,
376 * which is reflected in `want_activate', we attempt to atomically
377 * activate the resource. If this fails, we release the resource
378 * and indicate overall failure. (This behavior probably doesn't
379 * make sense for RF_TIMESHARE-type resources.)
381 if (rv && want_activate) {
382 struct resource *whohas;
383 if (int_rman_activate_resource(rm, rv, &whohas)) {
384 int_rman_release_resource(rm, rv);
385 rv = 0;
388 lwkt_reltoken(&ilock);
389 return (rv);
392 static int
393 int_rman_activate_resource(struct rman *rm, struct resource *r,
394 struct resource **whohas)
396 struct resource *s;
397 int ok;
400 * If we are not timesharing, then there is nothing much to do.
401 * If we already have the resource, then there is nothing at all to do.
402 * If we are not on a sharing list with anybody else, then there is
403 * little to do.
405 if ((r->r_flags & RF_TIMESHARE) == 0
406 || (r->r_flags & RF_ACTIVE) != 0
407 || r->r_sharehead == 0) {
408 r->r_flags |= RF_ACTIVE;
409 return 0;
412 ok = 1;
413 for (s = LIST_FIRST(r->r_sharehead); s && ok;
414 s = LIST_NEXT(s, r_sharelink)) {
415 if ((s->r_flags & RF_ACTIVE) != 0) {
416 ok = 0;
417 *whohas = s;
420 if (ok) {
421 r->r_flags |= RF_ACTIVE;
422 return 0;
424 return EBUSY;
428 rman_activate_resource(struct resource *r)
430 int rv;
431 struct resource *whohas;
432 lwkt_tokref ilock;
433 struct rman *rm;
435 rm = r->r_rm;
436 lwkt_gettoken(&ilock, rm->rm_slock);
437 rv = int_rman_activate_resource(rm, r, &whohas);
438 lwkt_reltoken(&ilock);
439 return rv;
442 #if 0
444 /* XXX */
446 rman_await_resource(struct resource *r, lwkt_tokref_t ilock, int slpflags, int timo)
448 int rv;
449 struct resource *whohas;
450 struct rman *rm;
452 rm = r->r_rm;
453 for (;;) {
454 lwkt_gettoken(ilock, rm->rm_slock);
455 rv = int_rman_activate_resource(rm, r, &whohas);
456 if (rv != EBUSY)
457 return (rv); /* returns with ilock held */
459 if (r->r_sharehead == 0)
460 panic("rman_await_resource");
462 * A critical section will hopefully will prevent a race
463 * between lwkt_reltoken and tsleep where a process
464 * could conceivably get in and release the resource
465 * before we have a chance to sleep on it. YYY
467 crit_enter();
468 whohas->r_flags |= RF_WANTED;
469 rv = tsleep(r->r_sharehead, slpflags, "rmwait", timo);
470 if (rv) {
471 lwkt_reltoken(ilock);
472 crit_exit();
473 return rv;
475 crit_exit();
479 #endif
481 static int
482 int_rman_deactivate_resource(struct resource *r)
484 struct rman *rm;
486 rm = r->r_rm;
487 r->r_flags &= ~RF_ACTIVE;
488 if (r->r_flags & RF_WANTED) {
489 r->r_flags &= ~RF_WANTED;
490 wakeup(r->r_sharehead);
492 return 0;
496 rman_deactivate_resource(struct resource *r)
498 lwkt_tokref ilock;
499 struct rman *rm;
501 rm = r->r_rm;
502 lwkt_gettoken(&ilock, rm->rm_slock);
503 int_rman_deactivate_resource(r);
504 lwkt_reltoken(&ilock);
505 return 0;
508 static int
509 int_rman_release_resource(struct rman *rm, struct resource *r)
511 struct resource *s, *t;
513 if (r->r_flags & RF_ACTIVE)
514 int_rman_deactivate_resource(r);
517 * Check for a sharing list first. If there is one, then we don't
518 * have to think as hard.
520 if (r->r_sharehead) {
522 * If a sharing list exists, then we know there are at
523 * least two sharers.
525 * If we are in the main circleq, appoint someone else.
527 LIST_REMOVE(r, r_sharelink);
528 s = LIST_FIRST(r->r_sharehead);
529 if (r->r_flags & RF_FIRSTSHARE) {
530 s->r_flags |= RF_FIRSTSHARE;
531 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, r, s, r_link);
532 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
536 * Make sure that the sharing list goes away completely
537 * if the resource is no longer being shared at all.
539 if (LIST_NEXT(s, r_sharelink) == 0) {
540 kfree(s->r_sharehead, M_RMAN);
541 s->r_sharehead = 0;
542 s->r_flags &= ~RF_FIRSTSHARE;
544 goto out;
548 * Look at the adjacent resources in the list and see if our
549 * segment can be merged with any of them.
551 s = CIRCLEQ_PREV(r, r_link);
552 t = CIRCLEQ_NEXT(r, r_link);
554 if (s != (void *)&rm->rm_list && (s->r_flags & RF_ALLOCATED) == 0
555 && t != (void *)&rm->rm_list && (t->r_flags & RF_ALLOCATED) == 0) {
557 * Merge all three segments.
559 s->r_end = t->r_end;
560 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
561 CIRCLEQ_REMOVE(&rm->rm_list, t, r_link);
562 kfree(t, M_RMAN);
563 } else if (s != (void *)&rm->rm_list
564 && (s->r_flags & RF_ALLOCATED) == 0) {
566 * Merge previous segment with ours.
568 s->r_end = r->r_end;
569 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
570 } else if (t != (void *)&rm->rm_list
571 && (t->r_flags & RF_ALLOCATED) == 0) {
573 * Merge next segment with ours.
575 t->r_start = r->r_start;
576 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
577 } else {
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;
588 return 0;
591 out:
592 kfree(r, M_RMAN);
593 return 0;
597 rman_release_resource(struct resource *r)
599 struct rman *rm = r->r_rm;
600 lwkt_tokref ilock;
601 int rv;
603 lwkt_gettoken(&ilock, rm->rm_slock);
604 rv = int_rman_release_resource(rm, r);
605 lwkt_reltoken(&ilock);
606 return (rv);
609 uint32_t
610 rman_make_alignment_flags(uint32_t size)
612 int i;
615 * Find the hightest bit set, and add one if more than one bit
616 * set. We're effectively computing the ceil(log2(size)) here.
618 for (i = 32; i > 0; i--)
619 if ((1 << i) & size)
620 break;
621 if (~(1 << i) & size)
622 i++;
624 return(RF_ALIGNMENT_LOG2(i));