933d134edab5182a198400e732b1b8e094d63f9f
[dragonfly.git] / sys / kern / subr_rman.c
1 /*
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
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.
15  * 
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.
28  *
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 $
31  */
32
33 /*
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.
42  *
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.
54  *
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.
59  */
60
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>
69
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");
74
75 #define DPRINTF(params) if (rman_debug) kprintf params
76
77 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
78
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);
85
86 #define CIRCLEQ_TERMCOND(var, head)     (var == (void *)&(head))
87
88 int
89 rman_init(struct rman *rm)
90 {
91         static int once;
92         lwkt_tokref ilock;
93
94         if (once == 0) {
95                 once = 1;
96                 TAILQ_INIT(&rman_head);
97                 lwkt_token_init(&rman_tok);
98         }
99
100         if (rm->rm_type == RMAN_UNINIT)
101                 panic("rman_init");
102         if (rm->rm_type == RMAN_GAUGE)
103                 panic("implement RMAN_GAUGE");
104
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);
110
111         lwkt_gettoken(&ilock, &rman_tok);
112         TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
113         lwkt_reltoken(&ilock);
114         return 0;
115 }
116
117 /*
118  * NB: this interface is not robust against programming errors which
119  * add multiple copies of the same region.
120  */
121 int
122 rman_manage_region(struct rman *rm, u_long start, u_long end)
123 {
124         struct resource *r, *s;
125         lwkt_tokref ilock;
126
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;
138
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))
143                 ;
144
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);
149         }
150
151         lwkt_reltoken(&ilock);
152         return 0;
153 }
154
155 int
156 rman_fini(struct rman *rm)
157 {
158         struct resource *r;
159         lwkt_tokref ilock;
160
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;
166                 }
167         }
168
169         /*
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.
172          */
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);
177         }
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);
184
185         return 0;
186 }
187
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)
191 {
192         u_int   want_activate;
193         struct  resource *r, *s, *rv;
194         u_long  rstart, rend;
195         lwkt_tokref ilock;
196
197         rv = 0;
198
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;
205
206         lwkt_gettoken(&ilock, rm->rm_slock);
207
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))
211                 ;
212
213         if (CIRCLEQ_TERMCOND(r, rm->rm_list)) {
214                 DPRINTF(("could not find a region\n"));
215                 goto out;
216         }
217
218         /*
219          * First try to find an acceptable totally-unshared region.
220          */
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;
228                 }
229                 if (s->r_flags & RF_ALLOCATED) {
230                         DPRINTF(("region is allocated\n"));
231                         continue;
232                 }
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));
239
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;
249                         }
250
251                         /*
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.
260                          */
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;
270                         
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));
277                                 /*
278                                  * We are allocating in the middle.
279                                  */
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;
286                                 }
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"));
300                                 /*
301                                  * We are allocating at the beginning.
302                                  */
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"));
308                                 /*
309                                  * We are allocating at the end.
310                                  */
311                                 s->r_end = rv->r_start - 1;
312                                 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv,
313                                                      r_link);
314                         }
315                         goto out;
316                 }
317         }
318
319         /*
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.)
326          */
327         DPRINTF(("no unshared regions found\n"));
328         if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
329                 goto out;
330
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;
358                                 }
359                                 LIST_INIT(s->r_sharehead);
360                                 LIST_INSERT_HEAD(s->r_sharehead, s, 
361                                                  r_sharelink);
362                                 s->r_flags |= RF_FIRSTSHARE;
363                         }
364                         rv->r_sharehead = s->r_sharehead;
365                         LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
366                         goto out;
367                 }
368         }
369
370         /*
371          * We couldn't find anything.
372          */
373 out:
374         /*
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.)
380          */
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;
386                 }
387         }
388         lwkt_reltoken(&ilock);
389         return (rv);
390 }
391
392 static int
393 int_rman_activate_resource(struct rman *rm, struct resource *r,
394                            struct resource **whohas)
395 {
396         struct resource *s;
397         int ok;
398
399         /*
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.
404          */
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;
410         }
411
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;
418                 }
419         }
420         if (ok) {
421                 r->r_flags |= RF_ACTIVE;
422                 return 0;
423         }
424         return EBUSY;
425 }
426
427 int
428 rman_activate_resource(struct resource *r)
429 {
430         int rv;
431         struct resource *whohas;
432         lwkt_tokref ilock;
433         struct rman *rm;
434
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;
440 }
441
442 #if 0
443
444 /* XXX */
445 int
446 rman_await_resource(struct resource *r, lwkt_tokref_t ilock, int slpflags, int timo)
447 {
448         int     rv;
449         struct  resource *whohas;
450         struct  rman *rm;
451
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 */
458
459                 if (r->r_sharehead == 0)
460                         panic("rman_await_resource");
461                 /*
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
466                  */
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;
474                 }
475                 crit_exit();
476         }
477 }
478
479 #endif
480
481 static int
482 int_rman_deactivate_resource(struct resource *r)
483 {
484         struct  rman *rm;
485
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);
491         }
492         return 0;
493 }
494
495 int
496 rman_deactivate_resource(struct resource *r)
497 {
498         lwkt_tokref ilock;
499         struct rman *rm;
500
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;
506 }
507
508 static int
509 int_rman_release_resource(struct rman *rm, struct resource *r)
510 {
511         struct  resource *s, *t;
512
513         if (r->r_flags & RF_ACTIVE)
514                 int_rman_deactivate_resource(r);
515
516         /*
517          * Check for a sharing list first.  If there is one, then we don't
518          * have to think as hard.
519          */
520         if (r->r_sharehead) {
521                 /*
522                  * If a sharing list exists, then we know there are at
523                  * least two sharers.
524                  *
525                  * If we are in the main circleq, appoint someone else.
526                  */
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);
533                 }
534
535                 /*
536                  * Make sure that the sharing list goes away completely
537                  * if the resource is no longer being shared at all.
538                  */
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;
543                 }
544                 goto out;
545         }
546
547         /*
548          * Look at the adjacent resources in the list and see if our
549          * segment can be merged with any of them.
550          */
551         s = CIRCLEQ_PREV(r, r_link);
552         t = CIRCLEQ_NEXT(r, r_link);
553
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) {
556                 /*
557                  * Merge all three segments.
558                  */
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) {
565                 /*
566                  * Merge previous segment with ours.
567                  */
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) {
572                 /*
573                  * Merge next segment with ours.
574                  */
575                 t->r_start = r->r_start;
576                 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link);
577         } else {
578                 /*
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.
586                  */
587                 r->r_flags &= ~RF_ALLOCATED;
588                 return 0;
589         }
590
591 out:
592         kfree(r, M_RMAN);
593         return 0;
594 }
595
596 int
597 rman_release_resource(struct resource *r)
598 {
599         struct  rman *rm = r->r_rm;
600         lwkt_tokref ilock;
601         int     rv;
602
603         lwkt_gettoken(&ilock, rm->rm_slock);
604         rv = int_rman_release_resource(rm, r);
605         lwkt_reltoken(&ilock);
606         return (rv);
607 }
608
609 uint32_t
610 rman_make_alignment_flags(uint32_t size)
611 {
612         int     i;
613
614         /*
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.
617          */
618         for (i = 32; i > 0; i--)
619                 if ((1 << i) & size)
620                         break;
621         if (~(1 << i) & size)
622                 i++;
623
624         return(RF_ALIGNMENT_LOG2(i));
625 }
626
627 /*
628  * Sysctl interface for scanning the resource lists.
629  *
630  * We take two input parameters; the index into the list of resource
631  * managers, and the resource offset into the list.
632  */
633 static int
634 sysctl_rman(SYSCTL_HANDLER_ARGS)
635 {
636         int                     *name = (int *)arg1;
637         u_int                   namelen = arg2;
638         int                     rman_idx, res_idx;
639         struct rman             *rm;
640         struct resource         *res;
641         struct u_rman           urm;
642         struct u_resource       ures;
643         int                     error;
644
645         if (namelen != 3)
646                 return (EINVAL);
647
648         if (bus_data_generation_check(name[0]))
649                 return (EINVAL);
650         rman_idx = name[1];
651         res_idx = name[2];
652
653         /*
654          * Find the indexed resource manager
655          */
656         TAILQ_FOREACH(rm, &rman_head, rm_link) {
657                 if (rman_idx-- == 0)
658                         break;
659         }
660         if (rm == NULL)
661                 return (ENOENT);
662
663         /*
664          * If the resource index is -1, we want details on the
665          * resource manager.
666          */
667         if (res_idx == -1) {
668                 urm.rm_handle = (uintptr_t)rm;
669                 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
670                 urm.rm_start = rm->rm_start;
671                 urm.rm_size = rm->rm_end - rm->rm_start + 1;
672                 urm.rm_type = rm->rm_type;
673
674                 error = SYSCTL_OUT(req, &urm, sizeof(urm));
675                 return (error);
676         }
677
678         /*
679          * Find the indexed resource and return it.
680          */
681         CIRCLEQ_FOREACH(res, &rm->rm_list, r_link) {
682                 if (res_idx-- == 0) {
683                         ures.r_handle = (uintptr_t)res;
684                         ures.r_parent = (uintptr_t)res->r_rm;
685                         ures.r_device = (uintptr_t)res->r_dev;
686                         if (res->r_dev != NULL) {
687                                 if (device_get_name(res->r_dev) != NULL) {
688                                         ksnprintf(ures.r_devname, RM_TEXTLEN,
689                                             "%s%d",
690                                             device_get_name(res->r_dev),
691                                             device_get_unit(res->r_dev));
692                                 } else {
693                                         strlcpy(ures.r_devname, "nomatch",
694                                             RM_TEXTLEN);
695                                 }
696                         } else {
697                                 ures.r_devname[0] = '\0';
698                         }
699                         ures.r_start = res->r_start;
700                         ures.r_size = res->r_end - res->r_start + 1;
701                         ures.r_flags = res->r_flags;
702
703                         error = SYSCTL_OUT(req, &ures, sizeof(ures));
704                         return (error);
705                 }
706         }
707         return (ENOENT);
708 }
709
710 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
711     "kernel resource manager");