mxge: properly remove the sysctls
[dragonfly.git] / sys / kern / subr_rman.c
blob2ff224f579f4899ea6d8b994d8755aaf0167de40
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.15 2008/09/30 12:20:29 hasso 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 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 TAILQ_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 | M_ZERO);
128 if (r == 0)
129 return ENOMEM;
130 r->r_sharehead = 0;
131 r->r_start = start;
132 r->r_end = end;
133 r->r_flags = 0;
134 r->r_dev = 0;
135 r->r_rm = rm;
137 lwkt_gettoken(&ilock, rm->rm_slock);
138 for (s = TAILQ_FIRST(&rm->rm_list);
139 s && s->r_end < r->r_start;
140 s = TAILQ_NEXT(s, r_link))
143 if (s == NULL)
144 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
145 else
146 TAILQ_INSERT_BEFORE(s, r, r_link);
148 lwkt_reltoken(&ilock);
149 return 0;
153 rman_fini(struct rman *rm)
155 struct resource *r;
156 lwkt_tokref ilock;
158 lwkt_gettoken(&ilock, rm->rm_slock);
159 TAILQ_FOREACH(r, &rm->rm_list, r_link) {
160 if (r->r_flags & RF_ALLOCATED) {
161 lwkt_reltoken(&ilock);
162 return EBUSY;
167 * There really should only be one of these if we are in this
168 * state and the code is working properly, but it can't hurt.
170 while (!TAILQ_EMPTY(&rm->rm_list)) {
171 r = TAILQ_FIRST(&rm->rm_list);
172 TAILQ_REMOVE(&rm->rm_list, r, r_link);
173 kfree(r, M_RMAN);
175 lwkt_reltoken(&ilock);
176 /* XXX what's the point of this if we are going to free the struct? */
177 lwkt_gettoken(&ilock, &rman_tok);
178 TAILQ_REMOVE(&rman_head, rm, rm_link);
179 lwkt_reltoken(&ilock);
180 kfree(rm->rm_slock, M_RMAN);
182 return 0;
185 struct resource *
186 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
187 u_int flags, struct device *dev)
189 u_int want_activate;
190 struct resource *r, *s, *rv;
191 u_long rstart, rend;
192 lwkt_tokref ilock;
194 rv = 0;
196 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
197 "%#lx, flags %u, device %s\n", rm->rm_descr, start, end,
198 count, flags,
199 dev == NULL ? "<null>" : device_get_nameunit(dev)));
200 want_activate = (flags & RF_ACTIVE);
201 flags &= ~RF_ACTIVE;
203 lwkt_gettoken(&ilock, rm->rm_slock);
205 for (r = TAILQ_FIRST(&rm->rm_list);
206 r && r->r_end < start;
207 r = TAILQ_NEXT(r, r_link))
210 if (r == NULL) {
211 DPRINTF(("could not find a region\n"));
212 goto out;
216 * First try to find an acceptable totally-unshared region.
218 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
219 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
220 if (s->r_start > end) {
221 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n",
222 s->r_start, end));
223 break;
225 if (s->r_flags & RF_ALLOCATED) {
226 DPRINTF(("region is allocated\n"));
227 continue;
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));
233 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
234 rstart, rend, (rend - rstart + 1), count));
236 if ((rend - rstart + 1) >= count) {
237 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
238 rstart, rend, (rend - rstart + 1)));
239 if ((s->r_end - s->r_start + 1) == count) {
240 DPRINTF(("candidate region is entire chunk\n"));
241 rv = s;
242 rv->r_flags |= RF_ALLOCATED | flags;
243 rv->r_dev = dev;
244 goto out;
248 * If s->r_start < rstart and
249 * s->r_end > rstart + count - 1, then
250 * we need to split the region into three pieces
251 * (the middle one will get returned to the user).
252 * Otherwise, we are allocating at either the
253 * beginning or the end of s, so we only need to
254 * split it in two. The first case requires
255 * two new allocations; the second requires but one.
257 rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
258 if (rv == 0)
259 goto out;
260 rv->r_start = rstart;
261 rv->r_end = rstart + count - 1;
262 rv->r_flags = flags | RF_ALLOCATED;
263 rv->r_dev = dev;
264 rv->r_sharehead = 0;
265 rv->r_rm = rm;
267 if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
268 DPRINTF(("splitting region in three parts: "
269 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
270 s->r_start, rv->r_start - 1,
271 rv->r_start, rv->r_end,
272 rv->r_end + 1, s->r_end));
274 * We are allocating in the middle.
276 r = kmalloc(sizeof *r, M_RMAN,
277 M_NOWAIT | M_ZERO);
278 if (r == 0) {
279 kfree(rv, M_RMAN);
280 rv = 0;
281 goto out;
283 r->r_start = rv->r_end + 1;
284 r->r_end = s->r_end;
285 r->r_flags = s->r_flags;
286 r->r_dev = 0;
287 r->r_sharehead = 0;
288 r->r_rm = rm;
289 s->r_end = rv->r_start - 1;
290 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
291 r_link);
292 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
293 r_link);
294 } else if (s->r_start == rv->r_start) {
295 DPRINTF(("allocating from the beginning\n"));
297 * We are allocating at the beginning.
299 s->r_start = rv->r_end + 1;
300 TAILQ_INSERT_BEFORE(s, rv, r_link);
301 } else {
302 DPRINTF(("allocating at the end\n"));
304 * We are allocating at the end.
306 s->r_end = rv->r_start - 1;
307 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
308 r_link);
310 goto out;
315 * Now find an acceptable shared region, if the client's requirements
316 * allow sharing. By our implementation restriction, a candidate
317 * region must match exactly by both size and sharing type in order
318 * to be considered compatible with the client's request. (The
319 * former restriction could probably be lifted without too much
320 * additional work, but this does not seem warranted.)
322 DPRINTF(("no unshared regions found\n"));
323 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
324 goto out;
326 for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
327 if (s->r_start > end)
328 break;
329 if ((s->r_flags & flags) != flags)
330 continue;
331 rstart = max(s->r_start, start);
332 rend = min(s->r_end, max(start + count, end));
333 if (s->r_start >= start && s->r_end <= end
334 && (s->r_end - s->r_start + 1) == count) {
335 rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
336 if (rv == 0)
337 goto out;
338 rv->r_start = s->r_start;
339 rv->r_end = s->r_end;
340 rv->r_flags = s->r_flags &
341 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
342 rv->r_dev = dev;
343 rv->r_rm = rm;
344 if (s->r_sharehead == 0) {
345 s->r_sharehead = kmalloc(sizeof *s->r_sharehead,
346 M_RMAN,
347 M_NOWAIT | M_ZERO);
348 if (s->r_sharehead == 0) {
349 kfree(rv, M_RMAN);
350 rv = 0;
351 goto out;
353 LIST_INIT(s->r_sharehead);
354 LIST_INSERT_HEAD(s->r_sharehead, s,
355 r_sharelink);
356 s->r_flags |= RF_FIRSTSHARE;
358 rv->r_sharehead = s->r_sharehead;
359 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
360 goto out;
365 * We couldn't find anything.
367 out:
369 * If the user specified RF_ACTIVE in the initial flags,
370 * which is reflected in `want_activate', we attempt to atomically
371 * activate the resource. If this fails, we release the resource
372 * and indicate overall failure. (This behavior probably doesn't
373 * make sense for RF_TIMESHARE-type resources.)
375 if (rv && want_activate) {
376 struct resource *whohas;
377 if (int_rman_activate_resource(rm, rv, &whohas)) {
378 int_rman_release_resource(rm, rv);
379 rv = 0;
382 lwkt_reltoken(&ilock);
383 return (rv);
386 static int
387 int_rman_activate_resource(struct rman *rm, struct resource *r,
388 struct resource **whohas)
390 struct resource *s;
391 int ok;
394 * If we are not timesharing, then there is nothing much to do.
395 * If we already have the resource, then there is nothing at all to do.
396 * If we are not on a sharing list with anybody else, then there is
397 * little to do.
399 if ((r->r_flags & RF_TIMESHARE) == 0
400 || (r->r_flags & RF_ACTIVE) != 0
401 || r->r_sharehead == 0) {
402 r->r_flags |= RF_ACTIVE;
403 return 0;
406 ok = 1;
407 for (s = LIST_FIRST(r->r_sharehead); s && ok;
408 s = LIST_NEXT(s, r_sharelink)) {
409 if ((s->r_flags & RF_ACTIVE) != 0) {
410 ok = 0;
411 *whohas = s;
414 if (ok) {
415 r->r_flags |= RF_ACTIVE;
416 return 0;
418 return EBUSY;
422 rman_activate_resource(struct resource *r)
424 int rv;
425 struct resource *whohas;
426 lwkt_tokref ilock;
427 struct rman *rm;
429 rm = r->r_rm;
430 lwkt_gettoken(&ilock, rm->rm_slock);
431 rv = int_rman_activate_resource(rm, r, &whohas);
432 lwkt_reltoken(&ilock);
433 return rv;
436 #if 0
438 /* XXX */
440 rman_await_resource(struct resource *r, lwkt_tokref_t ilock, int slpflags, int timo)
442 int rv;
443 struct resource *whohas;
444 struct rman *rm;
446 rm = r->r_rm;
447 for (;;) {
448 lwkt_gettoken(ilock, rm->rm_slock);
449 rv = int_rman_activate_resource(rm, r, &whohas);
450 if (rv != EBUSY)
451 return (rv); /* returns with ilock held */
453 if (r->r_sharehead == 0)
454 panic("rman_await_resource");
456 * A critical section will hopefully will prevent a race
457 * between lwkt_reltoken and tsleep where a process
458 * could conceivably get in and release the resource
459 * before we have a chance to sleep on it. YYY
461 crit_enter();
462 whohas->r_flags |= RF_WANTED;
463 rv = tsleep(r->r_sharehead, slpflags, "rmwait", timo);
464 if (rv) {
465 lwkt_reltoken(ilock);
466 crit_exit();
467 return rv;
469 crit_exit();
473 #endif
475 static int
476 int_rman_deactivate_resource(struct resource *r)
478 struct rman *rm;
480 rm = r->r_rm;
481 r->r_flags &= ~RF_ACTIVE;
482 if (r->r_flags & RF_WANTED) {
483 r->r_flags &= ~RF_WANTED;
484 wakeup(r->r_sharehead);
486 return 0;
490 rman_deactivate_resource(struct resource *r)
492 lwkt_tokref ilock;
493 struct rman *rm;
495 rm = r->r_rm;
496 lwkt_gettoken(&ilock, rm->rm_slock);
497 int_rman_deactivate_resource(r);
498 lwkt_reltoken(&ilock);
499 return 0;
502 static int
503 int_rman_release_resource(struct rman *rm, struct resource *r)
505 struct resource *s, *t;
507 if (r->r_flags & RF_ACTIVE)
508 int_rman_deactivate_resource(r);
511 * Check for a sharing list first. If there is one, then we don't
512 * have to think as hard.
514 if (r->r_sharehead) {
516 * If a sharing list exists, then we know there are at
517 * least two sharers.
519 * If we are in the main circleq, appoint someone else.
521 LIST_REMOVE(r, r_sharelink);
522 s = LIST_FIRST(r->r_sharehead);
523 if (r->r_flags & RF_FIRSTSHARE) {
524 s->r_flags |= RF_FIRSTSHARE;
525 TAILQ_INSERT_BEFORE(r, s, r_link);
526 TAILQ_REMOVE(&rm->rm_list, r, r_link);
530 * Make sure that the sharing list goes away completely
531 * if the resource is no longer being shared at all.
533 if (LIST_NEXT(s, r_sharelink) == 0) {
534 kfree(s->r_sharehead, M_RMAN);
535 s->r_sharehead = 0;
536 s->r_flags &= ~RF_FIRSTSHARE;
538 goto out;
542 * Look at the adjacent resources in the list and see if our
543 * segment can be merged with any of them.
545 s = TAILQ_PREV(r, resource_head, r_link);
546 t = TAILQ_NEXT(r, r_link);
548 if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0
549 && t != NULL && (t->r_flags & RF_ALLOCATED) == 0) {
551 * Merge all three segments.
553 s->r_end = t->r_end;
554 TAILQ_REMOVE(&rm->rm_list, r, r_link);
555 TAILQ_REMOVE(&rm->rm_list, t, r_link);
556 kfree(t, M_RMAN);
557 } else if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0) {
559 * Merge previous segment with ours.
561 s->r_end = r->r_end;
562 TAILQ_REMOVE(&rm->rm_list, r, r_link);
563 } else if (t != NULL && (t->r_flags & RF_ALLOCATED) == 0) {
565 * Merge next segment with ours.
567 t->r_start = r->r_start;
568 TAILQ_REMOVE(&rm->rm_list, r, r_link);
569 } else {
571 * At this point, we know there is nothing we
572 * can potentially merge with, because on each
573 * side, there is either nothing there or what is
574 * there is still allocated. In that case, we don't
575 * want to remove r from the list; we simply want to
576 * change it to an unallocated region and return
577 * without freeing anything.
579 r->r_flags &= ~RF_ALLOCATED;
580 return 0;
583 out:
584 kfree(r, M_RMAN);
585 return 0;
589 rman_release_resource(struct resource *r)
591 struct rman *rm = r->r_rm;
592 lwkt_tokref ilock;
593 int rv;
595 lwkt_gettoken(&ilock, rm->rm_slock);
596 rv = int_rman_release_resource(rm, r);
597 lwkt_reltoken(&ilock);
598 return (rv);
601 uint32_t
602 rman_make_alignment_flags(uint32_t size)
604 int i;
607 * Find the hightest bit set, and add one if more than one bit
608 * set. We're effectively computing the ceil(log2(size)) here.
610 for (i = 32; i > 0; i--)
611 if ((1 << i) & size)
612 break;
613 if (~(1 << i) & size)
614 i++;
616 return(RF_ALIGNMENT_LOG2(i));
620 * Sysctl interface for scanning the resource lists.
622 * We take two input parameters; the index into the list of resource
623 * managers, and the resource offset into the list.
625 static int
626 sysctl_rman(SYSCTL_HANDLER_ARGS)
628 int *name = (int *)arg1;
629 u_int namelen = arg2;
630 int rman_idx, res_idx;
631 struct rman *rm;
632 struct resource *res;
633 struct u_rman urm;
634 struct u_resource ures;
635 int error;
637 if (namelen != 3)
638 return (EINVAL);
640 if (bus_data_generation_check(name[0]))
641 return (EINVAL);
642 rman_idx = name[1];
643 res_idx = name[2];
646 * Find the indexed resource manager
648 TAILQ_FOREACH(rm, &rman_head, rm_link) {
649 if (rman_idx-- == 0)
650 break;
652 if (rm == NULL)
653 return (ENOENT);
656 * If the resource index is -1, we want details on the
657 * resource manager.
659 if (res_idx == -1) {
660 urm.rm_handle = (uintptr_t)rm;
661 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
662 urm.rm_start = rm->rm_start;
663 urm.rm_size = rm->rm_end - rm->rm_start + 1;
664 urm.rm_type = rm->rm_type;
666 error = SYSCTL_OUT(req, &urm, sizeof(urm));
667 return (error);
671 * Find the indexed resource and return it.
673 TAILQ_FOREACH(res, &rm->rm_list, r_link) {
674 if (res_idx-- == 0) {
675 ures.r_handle = (uintptr_t)res;
676 ures.r_parent = (uintptr_t)res->r_rm;
677 ures.r_device = (uintptr_t)res->r_dev;
678 if (res->r_dev != NULL) {
679 if (device_get_name(res->r_dev) != NULL) {
680 ksnprintf(ures.r_devname, RM_TEXTLEN,
681 "%s%d",
682 device_get_name(res->r_dev),
683 device_get_unit(res->r_dev));
684 } else {
685 strlcpy(ures.r_devname, "nomatch",
686 RM_TEXTLEN);
688 } else {
689 ures.r_devname[0] = '\0';
691 ures.r_start = res->r_start;
692 ures.r_size = res->r_end - res->r_start + 1;
693 ures.r_flags = res->r_flags;
695 error = SYSCTL_OUT(req, &ures, sizeof(ures));
696 return (error);
699 return (ENOENT);
702 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
703 "kernel resource manager");