]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/kern/subr_rman.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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
30 /*
31  * The kernel resource manager.  This code is responsible for keeping track
32  * of hardware resources which are apportioned out to various drivers.
33  * It does not actually assign those resources, and it is not expected
34  * that end-device drivers will call into this code directly.  Rather,
35  * the code which implements the buses that those devices are attached to,
36  * and the code which manages CPU resources, will call this code, and the
37  * end-device drivers will make upcalls to that code to actually perform
38  * the allocation.
39  *
40  * There are two sorts of resources managed by this code.  The first is
41  * the more familiar array (RMAN_ARRAY) type; resources in this class
42  * consist of a sequence of individually-allocatable objects which have
43  * been numbered in some well-defined order.  Most of the resources
44  * are of this type, as it is the most familiar.  The second type is
45  * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
46  * resources in which each instance is indistinguishable from every
47  * other instance).  The principal anticipated application of gauges
48  * is in the context of power consumption, where a bus may have a specific
49  * power budget which all attached devices share.  RMAN_GAUGE is not
50  * implemented yet.
51  *
52  * For array resources, we make one simplifying assumption: two clients
53  * sharing the same resource must use the same range of indices.  That
54  * is to say, sharing of overlapping-but-not-identical regions is not
55  * permitted.
56  */
57
58 #include "opt_ddb.h"
59
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/limits.h>
67 #include <sys/lock.h>
68 #include <sys/malloc.h>
69 #include <sys/mutex.h>
70 #include <sys/bus.h>            /* XXX debugging */
71 #include <machine/bus.h>
72 #include <sys/rman.h>
73 #include <sys/sysctl.h>
74
75 #ifdef DDB
76 #include <ddb/ddb.h>
77 #endif
78
79 /*
80  * We use a linked list rather than a bitmap because we need to be able to
81  * represent potentially huge objects (like all of a processor's physical
82  * address space).  That is also why the indices are defined to have type
83  * `unsigned long' -- that being the largest integral type in ISO C (1990).
84  * The 1999 version of C allows `long long'; we may need to switch to that
85  * at some point in the future, particularly if we want to support 36-bit
86  * addresses on IA32 hardware.
87  */
88 struct resource_i {
89         struct resource         r_r;
90         TAILQ_ENTRY(resource_i) r_link;
91         LIST_ENTRY(resource_i)  r_sharelink;
92         LIST_HEAD(, resource_i) *r_sharehead;
93         u_long  r_start;        /* index of the first entry in this resource */
94         u_long  r_end;          /* index of the last entry (inclusive) */
95         u_int   r_flags;
96         void    *r_virtual;     /* virtual address of this resource */
97         struct  device *r_dev;  /* device which has allocated this resource */
98         struct  rman *r_rm;     /* resource manager from whence this came */
99         int     r_rid;          /* optional rid for this resource. */
100 };
101
102 static int     rman_debug = 0;
103 TUNABLE_INT("debug.rman_debug", &rman_debug);
104 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
105     &rman_debug, 0, "rman debug");
106
107 #define DPRINTF(params) if (rman_debug) printf params
108
109 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
110
111 struct  rman_head rman_head;
112 static  struct mtx rman_mtx; /* mutex to protect rman_head */
113 static  int int_rman_activate_resource(struct rman *rm, struct resource_i *r,
114                                        struct resource_i **whohas);
115 static  int int_rman_deactivate_resource(struct resource_i *r);
116 static  int int_rman_release_resource(struct rman *rm, struct resource_i *r);
117
118 static __inline struct resource_i *
119 int_alloc_resource(int malloc_flag)
120 {
121         struct resource_i *r;
122
123         r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
124         if (r != NULL) {
125                 r->r_r.__r_i = r;
126         }
127         return (r);
128 }
129
130 int
131 rman_init(struct rman *rm)
132 {
133         static int once = 0;
134
135         if (once == 0) {
136                 once = 1;
137                 TAILQ_INIT(&rman_head);
138                 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
139         }
140
141         if (rm->rm_start == 0 && rm->rm_end == 0)
142                 rm->rm_end = ~0ul;
143         if (rm->rm_type == RMAN_UNINIT)
144                 panic("rman_init");
145         if (rm->rm_type == RMAN_GAUGE)
146                 panic("implement RMAN_GAUGE");
147
148         TAILQ_INIT(&rm->rm_list);
149         rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
150         if (rm->rm_mtx == NULL)
151                 return ENOMEM;
152         mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
153
154         mtx_lock(&rman_mtx);
155         TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
156         mtx_unlock(&rman_mtx);
157         return 0;
158 }
159
160 int
161 rman_manage_region(struct rman *rm, u_long start, u_long end)
162 {
163         struct resource_i *r, *s, *t;
164
165         DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
166             rm->rm_descr, start, end));
167         if (start < rm->rm_start || end > rm->rm_end)
168                 return EINVAL;
169         r = int_alloc_resource(M_NOWAIT);
170         if (r == NULL)
171                 return ENOMEM;
172         r->r_start = start;
173         r->r_end = end;
174         r->r_rm = rm;
175
176         mtx_lock(rm->rm_mtx);
177
178         /* Skip entries before us. */
179         TAILQ_FOREACH(s, &rm->rm_list, r_link) {
180                 if (s->r_end == ULONG_MAX)
181                         break;
182                 if (s->r_end + 1 >= r->r_start)
183                         break;
184         }
185
186         /* If we ran off the end of the list, insert at the tail. */
187         if (s == NULL) {
188                 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
189         } else {
190                 /* Check for any overlap with the current region. */
191                 if (r->r_start <= s->r_end && r->r_end >= s->r_start)
192                         return EBUSY;
193
194                 /* Check for any overlap with the next region. */
195                 t = TAILQ_NEXT(s, r_link);
196                 if (t && r->r_start <= t->r_end && r->r_end >= t->r_start)
197                         return EBUSY;
198
199                 /*
200                  * See if this region can be merged with the next region.  If
201                  * not, clear the pointer.
202                  */
203                 if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
204                         t = NULL;
205
206                 /* See if we can merge with the current region. */
207                 if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
208                         /* Can we merge all 3 regions? */
209                         if (t != NULL) {
210                                 s->r_end = t->r_end;
211                                 TAILQ_REMOVE(&rm->rm_list, t, r_link);
212                                 free(r, M_RMAN);
213                                 free(t, M_RMAN);
214                         } else {
215                                 s->r_end = r->r_end;
216                                 free(r, M_RMAN);
217                         }
218                 } else if (t != NULL) {
219                         /* Can we merge with just the next region? */
220                         t->r_start = r->r_start;
221                         free(r, M_RMAN);
222                 } else if (s->r_end < r->r_start) {
223                         TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link);
224                 } else {
225                         TAILQ_INSERT_BEFORE(s, r, r_link);
226                 }
227         }
228
229         mtx_unlock(rm->rm_mtx);
230         return 0;
231 }
232
233 int
234 rman_init_from_resource(struct rman *rm, struct resource *r)
235 {
236         int rv;
237
238         if ((rv = rman_init(rm)) != 0)
239                 return (rv);
240         return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
241 }
242
243 int
244 rman_fini(struct rman *rm)
245 {
246         struct resource_i *r;
247
248         mtx_lock(rm->rm_mtx);
249         TAILQ_FOREACH(r, &rm->rm_list, r_link) {
250                 if (r->r_flags & RF_ALLOCATED) {
251                         mtx_unlock(rm->rm_mtx);
252                         return EBUSY;
253                 }
254         }
255
256         /*
257          * There really should only be one of these if we are in this
258          * state and the code is working properly, but it can't hurt.
259          */
260         while (!TAILQ_EMPTY(&rm->rm_list)) {
261                 r = TAILQ_FIRST(&rm->rm_list);
262                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
263                 free(r, M_RMAN);
264         }
265         mtx_unlock(rm->rm_mtx);
266         mtx_lock(&rman_mtx);
267         TAILQ_REMOVE(&rman_head, rm, rm_link);
268         mtx_unlock(&rman_mtx);
269         mtx_destroy(rm->rm_mtx);
270         free(rm->rm_mtx, M_RMAN);
271
272         return 0;
273 }
274
275 int
276 rman_first_free_region(struct rman *rm, u_long *start, u_long *end)
277 {
278         struct resource_i *r;
279
280         mtx_lock(rm->rm_mtx);
281         TAILQ_FOREACH(r, &rm->rm_list, r_link) {
282                 if (!(r->r_flags & RF_ALLOCATED)) {
283                         *start = r->r_start;
284                         *end = r->r_end;
285                         mtx_unlock(rm->rm_mtx);
286                         return (0);
287                 }
288         }
289         mtx_unlock(rm->rm_mtx);
290         return (ENOENT);
291 }
292
293 int
294 rman_last_free_region(struct rman *rm, u_long *start, u_long *end)
295 {
296         struct resource_i *r;
297
298         mtx_lock(rm->rm_mtx);
299         TAILQ_FOREACH_REVERSE(r, &rm->rm_list, resource_head, r_link) {
300                 if (!(r->r_flags & RF_ALLOCATED)) {
301                         *start = r->r_start;
302                         *end = r->r_end;
303                         mtx_unlock(rm->rm_mtx);
304                         return (0);
305                 }
306         }
307         mtx_unlock(rm->rm_mtx);
308         return (ENOENT);
309 }
310
311 /* Shrink or extend one or both ends of an allocated resource. */
312 int
313 rman_adjust_resource(struct resource *rr, u_long start, u_long end)
314 {
315         struct  resource_i *r, *s, *t, *new;
316         struct  rman *rm;
317
318         /* Not supported for shared resources. */
319         r = rr->__r_i;
320         if (r->r_flags & (RF_TIMESHARE | RF_SHAREABLE))
321                 return (EINVAL);
322
323         /*
324          * This does not support wholesale moving of a resource.  At
325          * least part of the desired new range must overlap with the
326          * existing resource.
327          */
328         if (end < r->r_start || r->r_end < start)
329                 return (EINVAL);
330
331         /*
332          * Find the two resource regions immediately adjacent to the
333          * allocated resource.
334          */
335         rm = r->r_rm;
336         mtx_lock(rm->rm_mtx);
337 #ifdef INVARIANTS
338         TAILQ_FOREACH(s, &rm->rm_list, r_link) {
339                 if (s == r)
340                         break;
341         }
342         if (s == NULL)
343                 panic("resource not in list");
344 #endif
345         s = TAILQ_PREV(r, resource_head, r_link);
346         t = TAILQ_NEXT(r, r_link);
347         KASSERT(s == NULL || s->r_end + 1 == r->r_start,
348             ("prev resource mismatch"));
349         KASSERT(t == NULL || r->r_end + 1 == t->r_start,
350             ("next resource mismatch"));
351
352         /*
353          * See if the changes are permitted.  Shrinking is always allowed,
354          * but growing requires sufficient room in the adjacent region.
355          */
356         if (start < r->r_start && (s == NULL || (s->r_flags & RF_ALLOCATED) ||
357             s->r_start > start)) {
358                 mtx_unlock(rm->rm_mtx);
359                 return (EBUSY);
360         }
361         if (end > r->r_end && (t == NULL || (t->r_flags & RF_ALLOCATED) ||
362             t->r_end < end)) {
363                 mtx_unlock(rm->rm_mtx);
364                 return (EBUSY);
365         }
366
367         /*
368          * While holding the lock, grow either end of the resource as
369          * needed and shrink either end if the shrinking does not require
370          * allocating a new resource.  We can safely drop the lock and then
371          * insert a new range to handle the shrinking case afterwards.
372          */
373         if (start < r->r_start ||
374             (start > r->r_start && s != NULL && !(s->r_flags & RF_ALLOCATED))) {
375                 KASSERT(s->r_flags == 0, ("prev is busy"));
376                 r->r_start = start;
377                 if (s->r_start == start) {
378                         TAILQ_REMOVE(&rm->rm_list, s, r_link);
379                         free(s, M_RMAN);
380                 } else
381                         s->r_end = start - 1;
382         }
383         if (end > r->r_end ||
384             (end < r->r_end && t != NULL && !(t->r_flags & RF_ALLOCATED))) {
385                 KASSERT(t->r_flags == 0, ("next is busy"));
386                 r->r_end = end;
387                 if (t->r_end == end) {
388                         TAILQ_REMOVE(&rm->rm_list, t, r_link);
389                         free(t, M_RMAN);
390                 } else
391                         t->r_start = end + 1;
392         }
393         mtx_unlock(rm->rm_mtx);
394
395         /*
396          * Handle the shrinking cases that require allocating a new
397          * resource to hold the newly-free region.  We have to recheck
398          * if we still need this new region after acquiring the lock.
399          */
400         if (start > r->r_start) {
401                 new = int_alloc_resource(M_WAITOK);
402                 new->r_start = r->r_start;
403                 new->r_end = start - 1;
404                 new->r_rm = rm;
405                 mtx_lock(rm->rm_mtx);
406                 r->r_start = start;
407                 s = TAILQ_PREV(r, resource_head, r_link);
408                 if (s != NULL && !(s->r_flags & RF_ALLOCATED)) {
409                         s->r_end = start - 1;
410                         free(new, M_RMAN);
411                 } else
412                         TAILQ_INSERT_BEFORE(r, new, r_link);
413                 mtx_unlock(rm->rm_mtx);
414         }
415         if (end < r->r_end) {
416                 new = int_alloc_resource(M_WAITOK);
417                 new->r_start = end + 1;
418                 new->r_end = r->r_end;
419                 new->r_rm = rm;
420                 mtx_lock(rm->rm_mtx);
421                 r->r_end = end;
422                 t = TAILQ_NEXT(r, r_link);
423                 if (t != NULL && !(t->r_flags & RF_ALLOCATED)) {
424                         t->r_start = end + 1;
425                         free(new, M_RMAN);
426                 } else
427                         TAILQ_INSERT_AFTER(&rm->rm_list, r, new, r_link);
428                 mtx_unlock(rm->rm_mtx);
429         }
430         return (0);
431 }
432
433 struct resource *
434 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
435                       u_long count, u_long bound,  u_int flags,
436                       struct device *dev)
437 {
438         u_int   want_activate;
439         struct  resource_i *r, *s, *rv;
440         u_long  rstart, rend, amask, bmask;
441
442         rv = NULL;
443
444         DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
445                "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
446                count, flags,
447                dev == NULL ? "<null>" : device_get_nameunit(dev)));
448         want_activate = (flags & RF_ACTIVE);
449         flags &= ~RF_ACTIVE;
450
451         mtx_lock(rm->rm_mtx);
452
453         for (r = TAILQ_FIRST(&rm->rm_list);
454              r && r->r_end < start;
455              r = TAILQ_NEXT(r, r_link))
456                 ;
457
458         if (r == NULL) {
459                 DPRINTF(("could not find a region\n"));
460                 goto out;
461         }
462
463         amask = (1ul << RF_ALIGNMENT(flags)) - 1;
464         /* If bound is 0, bmask will also be 0 */
465         bmask = ~(bound - 1);
466         /*
467          * First try to find an acceptable totally-unshared region.
468          */
469         for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
470                 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
471                 if (s->r_start + count - 1 > end) {
472                         DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
473                             s->r_start, end));
474                         break;
475                 }
476                 if (s->r_flags & RF_ALLOCATED) {
477                         DPRINTF(("region is allocated\n"));
478                         continue;
479                 }
480                 rstart = ulmax(s->r_start, start);
481                 /*
482                  * Try to find a region by adjusting to boundary and alignment
483                  * until both conditions are satisfied. This is not an optimal
484                  * algorithm, but in most cases it isn't really bad, either.
485                  */
486                 do {
487                         rstart = (rstart + amask) & ~amask;
488                         if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
489                                 rstart += bound - (rstart & ~bmask);
490                 } while ((rstart & amask) != 0 && rstart < end &&
491                     rstart < s->r_end);
492                 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
493                 if (rstart > rend) {
494                         DPRINTF(("adjusted start exceeds end\n"));
495                         continue;
496                 }
497                 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
498                        rstart, rend, (rend - rstart + 1), count));
499
500                 if ((rend - rstart + 1) >= count) {
501                         DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
502                                rstart, rend, (rend - rstart + 1)));
503                         if ((s->r_end - s->r_start + 1) == count) {
504                                 DPRINTF(("candidate region is entire chunk\n"));
505                                 rv = s;
506                                 rv->r_flags |= RF_ALLOCATED | flags;
507                                 rv->r_dev = dev;
508                                 goto out;
509                         }
510
511                         /*
512                          * If s->r_start < rstart and
513                          *    s->r_end > rstart + count - 1, then
514                          * we need to split the region into three pieces
515                          * (the middle one will get returned to the user).
516                          * Otherwise, we are allocating at either the
517                          * beginning or the end of s, so we only need to
518                          * split it in two.  The first case requires
519                          * two new allocations; the second requires but one.
520                          */
521                         rv = int_alloc_resource(M_NOWAIT);
522                         if (rv == NULL)
523                                 goto out;
524                         rv->r_start = rstart;
525                         rv->r_end = rstart + count - 1;
526                         rv->r_flags = flags | RF_ALLOCATED;
527                         rv->r_dev = dev;
528                         rv->r_rm = rm;
529
530                         if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
531                                 DPRINTF(("splitting region in three parts: "
532                                        "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
533                                        s->r_start, rv->r_start - 1,
534                                        rv->r_start, rv->r_end,
535                                        rv->r_end + 1, s->r_end));
536                                 /*
537                                  * We are allocating in the middle.
538                                  */
539                                 r = int_alloc_resource(M_NOWAIT);
540                                 if (r == NULL) {
541                                         free(rv, M_RMAN);
542                                         rv = NULL;
543                                         goto out;
544                                 }
545                                 r->r_start = rv->r_end + 1;
546                                 r->r_end = s->r_end;
547                                 r->r_flags = s->r_flags;
548                                 r->r_rm = rm;
549                                 s->r_end = rv->r_start - 1;
550                                 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
551                                                      r_link);
552                                 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
553                                                      r_link);
554                         } else if (s->r_start == rv->r_start) {
555                                 DPRINTF(("allocating from the beginning\n"));
556                                 /*
557                                  * We are allocating at the beginning.
558                                  */
559                                 s->r_start = rv->r_end + 1;
560                                 TAILQ_INSERT_BEFORE(s, rv, r_link);
561                         } else {
562                                 DPRINTF(("allocating at the end\n"));
563                                 /*
564                                  * We are allocating at the end.
565                                  */
566                                 s->r_end = rv->r_start - 1;
567                                 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
568                                                      r_link);
569                         }
570                         goto out;
571                 }
572         }
573
574         /*
575          * Now find an acceptable shared region, if the client's requirements
576          * allow sharing.  By our implementation restriction, a candidate
577          * region must match exactly by both size and sharing type in order
578          * to be considered compatible with the client's request.  (The
579          * former restriction could probably be lifted without too much
580          * additional work, but this does not seem warranted.)
581          */
582         DPRINTF(("no unshared regions found\n"));
583         if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
584                 goto out;
585
586         for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
587                 if (s->r_start > end)
588                         break;
589                 if ((s->r_flags & flags) != flags)
590                         continue;
591                 rstart = ulmax(s->r_start, start);
592                 rend = ulmin(s->r_end, ulmax(start + count - 1, end));
593                 if (s->r_start >= start && s->r_end <= end
594                     && (s->r_end - s->r_start + 1) == count &&
595                     (s->r_start & amask) == 0 &&
596                     ((s->r_start ^ s->r_end) & bmask) == 0) {
597                         rv = int_alloc_resource(M_NOWAIT);
598                         if (rv == NULL)
599                                 goto out;
600                         rv->r_start = s->r_start;
601                         rv->r_end = s->r_end;
602                         rv->r_flags = s->r_flags &
603                                 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
604                         rv->r_dev = dev;
605                         rv->r_rm = rm;
606                         if (s->r_sharehead == NULL) {
607                                 s->r_sharehead = malloc(sizeof *s->r_sharehead,
608                                                 M_RMAN, M_NOWAIT | M_ZERO);
609                                 if (s->r_sharehead == NULL) {
610                                         free(rv, M_RMAN);
611                                         rv = NULL;
612                                         goto out;
613                                 }
614                                 LIST_INIT(s->r_sharehead);
615                                 LIST_INSERT_HEAD(s->r_sharehead, s,
616                                                  r_sharelink);
617                                 s->r_flags |= RF_FIRSTSHARE;
618                         }
619                         rv->r_sharehead = s->r_sharehead;
620                         LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
621                         goto out;
622                 }
623         }
624
625         /*
626          * We couldn't find anything.
627          */
628 out:
629         /*
630          * If the user specified RF_ACTIVE in the initial flags,
631          * which is reflected in `want_activate', we attempt to atomically
632          * activate the resource.  If this fails, we release the resource
633          * and indicate overall failure.  (This behavior probably doesn't
634          * make sense for RF_TIMESHARE-type resources.)
635          */
636         if (rv && want_activate) {
637                 struct resource_i *whohas;
638                 if (int_rman_activate_resource(rm, rv, &whohas)) {
639                         int_rman_release_resource(rm, rv);
640                         rv = NULL;
641                 }
642         }
643
644         mtx_unlock(rm->rm_mtx);
645         return (rv == NULL ? NULL : &rv->r_r);
646 }
647
648 struct resource *
649 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
650                       u_int flags, struct device *dev)
651 {
652
653         return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
654             dev));
655 }
656
657 static int
658 int_rman_activate_resource(struct rman *rm, struct resource_i *r,
659                            struct resource_i **whohas)
660 {
661         struct resource_i *s;
662         int ok;
663
664         /*
665          * If we are not timesharing, then there is nothing much to do.
666          * If we already have the resource, then there is nothing at all to do.
667          * If we are not on a sharing list with anybody else, then there is
668          * little to do.
669          */
670         if ((r->r_flags & RF_TIMESHARE) == 0
671             || (r->r_flags & RF_ACTIVE) != 0
672             || r->r_sharehead == NULL) {
673                 r->r_flags |= RF_ACTIVE;
674                 return 0;
675         }
676
677         ok = 1;
678         for (s = LIST_FIRST(r->r_sharehead); s && ok;
679              s = LIST_NEXT(s, r_sharelink)) {
680                 if ((s->r_flags & RF_ACTIVE) != 0) {
681                         ok = 0;
682                         *whohas = s;
683                 }
684         }
685         if (ok) {
686                 r->r_flags |= RF_ACTIVE;
687                 return 0;
688         }
689         return EBUSY;
690 }
691
692 int
693 rman_activate_resource(struct resource *re)
694 {
695         int rv;
696         struct resource_i *r, *whohas;
697         struct rman *rm;
698
699         r = re->__r_i;
700         rm = r->r_rm;
701         mtx_lock(rm->rm_mtx);
702         rv = int_rman_activate_resource(rm, r, &whohas);
703         mtx_unlock(rm->rm_mtx);
704         return rv;
705 }
706
707 int
708 rman_await_resource(struct resource *re, int pri, int timo)
709 {
710         int     rv;
711         struct  resource_i *r, *whohas;
712         struct  rman *rm;
713
714         r = re->__r_i;
715         rm = r->r_rm;
716         mtx_lock(rm->rm_mtx);
717         for (;;) {
718                 rv = int_rman_activate_resource(rm, r, &whohas);
719                 if (rv != EBUSY)
720                         return (rv);    /* returns with mutex held */
721
722                 if (r->r_sharehead == NULL)
723                         panic("rman_await_resource");
724                 whohas->r_flags |= RF_WANTED;
725                 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
726                 if (rv) {
727                         mtx_unlock(rm->rm_mtx);
728                         return (rv);
729                 }
730         }
731 }
732
733 static int
734 int_rman_deactivate_resource(struct resource_i *r)
735 {
736
737         r->r_flags &= ~RF_ACTIVE;
738         if (r->r_flags & RF_WANTED) {
739                 r->r_flags &= ~RF_WANTED;
740                 wakeup(r->r_sharehead);
741         }
742         return 0;
743 }
744
745 int
746 rman_deactivate_resource(struct resource *r)
747 {
748         struct  rman *rm;
749
750         rm = r->__r_i->r_rm;
751         mtx_lock(rm->rm_mtx);
752         int_rman_deactivate_resource(r->__r_i);
753         mtx_unlock(rm->rm_mtx);
754         return 0;
755 }
756
757 static int
758 int_rman_release_resource(struct rman *rm, struct resource_i *r)
759 {
760         struct  resource_i *s, *t;
761
762         if (r->r_flags & RF_ACTIVE)
763                 int_rman_deactivate_resource(r);
764
765         /*
766          * Check for a sharing list first.  If there is one, then we don't
767          * have to think as hard.
768          */
769         if (r->r_sharehead) {
770                 /*
771                  * If a sharing list exists, then we know there are at
772                  * least two sharers.
773                  *
774                  * If we are in the main circleq, appoint someone else.
775                  */
776                 LIST_REMOVE(r, r_sharelink);
777                 s = LIST_FIRST(r->r_sharehead);
778                 if (r->r_flags & RF_FIRSTSHARE) {
779                         s->r_flags |= RF_FIRSTSHARE;
780                         TAILQ_INSERT_BEFORE(r, s, r_link);
781                         TAILQ_REMOVE(&rm->rm_list, r, r_link);
782                 }
783
784                 /*
785                  * Make sure that the sharing list goes away completely
786                  * if the resource is no longer being shared at all.
787                  */
788                 if (LIST_NEXT(s, r_sharelink) == NULL) {
789                         free(s->r_sharehead, M_RMAN);
790                         s->r_sharehead = NULL;
791                         s->r_flags &= ~RF_FIRSTSHARE;
792                 }
793                 goto out;
794         }
795
796         /*
797          * Look at the adjacent resources in the list and see if our
798          * segment can be merged with any of them.  If either of the
799          * resources is allocated or is not exactly adjacent then they
800          * cannot be merged with our segment.
801          */
802         s = TAILQ_PREV(r, resource_head, r_link);
803         if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
804             s->r_end + 1 != r->r_start))
805                 s = NULL;
806         t = TAILQ_NEXT(r, r_link);
807         if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
808             r->r_end + 1 != t->r_start))
809                 t = NULL;
810
811         if (s != NULL && t != NULL) {
812                 /*
813                  * Merge all three segments.
814                  */
815                 s->r_end = t->r_end;
816                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
817                 TAILQ_REMOVE(&rm->rm_list, t, r_link);
818                 free(t, M_RMAN);
819         } else if (s != NULL) {
820                 /*
821                  * Merge previous segment with ours.
822                  */
823                 s->r_end = r->r_end;
824                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
825         } else if (t != NULL) {
826                 /*
827                  * Merge next segment with ours.
828                  */
829                 t->r_start = r->r_start;
830                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
831         } else {
832                 /*
833                  * At this point, we know there is nothing we
834                  * can potentially merge with, because on each
835                  * side, there is either nothing there or what is
836                  * there is still allocated.  In that case, we don't
837                  * want to remove r from the list; we simply want to
838                  * change it to an unallocated region and return
839                  * without freeing anything.
840                  */
841                 r->r_flags &= ~RF_ALLOCATED;
842                 r->r_dev = NULL;
843                 return 0;
844         }
845
846 out:
847         free(r, M_RMAN);
848         return 0;
849 }
850
851 int
852 rman_release_resource(struct resource *re)
853 {
854         int     rv;
855         struct  resource_i *r;
856         struct  rman *rm;
857
858         r = re->__r_i;
859         rm = r->r_rm;
860         mtx_lock(rm->rm_mtx);
861         rv = int_rman_release_resource(rm, r);
862         mtx_unlock(rm->rm_mtx);
863         return (rv);
864 }
865
866 uint32_t
867 rman_make_alignment_flags(uint32_t size)
868 {
869         int     i;
870
871         /*
872          * Find the hightest bit set, and add one if more than one bit
873          * set.  We're effectively computing the ceil(log2(size)) here.
874          */
875         for (i = 31; i > 0; i--)
876                 if ((1 << i) & size)
877                         break;
878         if (~(1 << i) & size)
879                 i++;
880
881         return(RF_ALIGNMENT_LOG2(i));
882 }
883
884 void
885 rman_set_start(struct resource *r, u_long start)
886 {
887         r->__r_i->r_start = start;
888 }
889
890 u_long
891 rman_get_start(struct resource *r)
892 {
893         return (r->__r_i->r_start);
894 }
895
896 void
897 rman_set_end(struct resource *r, u_long end)
898 {
899         r->__r_i->r_end = end;
900 }
901
902 u_long
903 rman_get_end(struct resource *r)
904 {
905         return (r->__r_i->r_end);
906 }
907
908 u_long
909 rman_get_size(struct resource *r)
910 {
911         return (r->__r_i->r_end - r->__r_i->r_start + 1);
912 }
913
914 u_int
915 rman_get_flags(struct resource *r)
916 {
917         return (r->__r_i->r_flags);
918 }
919
920 void
921 rman_set_virtual(struct resource *r, void *v)
922 {
923         r->__r_i->r_virtual = v;
924 }
925
926 void *
927 rman_get_virtual(struct resource *r)
928 {
929         return (r->__r_i->r_virtual);
930 }
931
932 void
933 rman_set_bustag(struct resource *r, bus_space_tag_t t)
934 {
935         r->r_bustag = t;
936 }
937
938 bus_space_tag_t
939 rman_get_bustag(struct resource *r)
940 {
941         return (r->r_bustag);
942 }
943
944 void
945 rman_set_bushandle(struct resource *r, bus_space_handle_t h)
946 {
947         r->r_bushandle = h;
948 }
949
950 bus_space_handle_t
951 rman_get_bushandle(struct resource *r)
952 {
953         return (r->r_bushandle);
954 }
955
956 void
957 rman_set_rid(struct resource *r, int rid)
958 {
959         r->__r_i->r_rid = rid;
960 }
961
962 int
963 rman_get_rid(struct resource *r)
964 {
965         return (r->__r_i->r_rid);
966 }
967
968 void
969 rman_set_device(struct resource *r, struct device *dev)
970 {
971         r->__r_i->r_dev = dev;
972 }
973
974 struct device *
975 rman_get_device(struct resource *r)
976 {
977         return (r->__r_i->r_dev);
978 }
979
980 int
981 rman_is_region_manager(struct resource *r, struct rman *rm)
982 {
983
984         return (r->__r_i->r_rm == rm);
985 }
986
987 /*
988  * Sysctl interface for scanning the resource lists.
989  *
990  * We take two input parameters; the index into the list of resource
991  * managers, and the resource offset into the list.
992  */
993 static int
994 sysctl_rman(SYSCTL_HANDLER_ARGS)
995 {
996         int                     *name = (int *)arg1;
997         u_int                   namelen = arg2;
998         int                     rman_idx, res_idx;
999         struct rman             *rm;
1000         struct resource_i       *res;
1001         struct resource_i       *sres;
1002         struct u_rman           urm;
1003         struct u_resource       ures;
1004         int                     error;
1005
1006         if (namelen != 3)
1007                 return (EINVAL);
1008
1009         if (bus_data_generation_check(name[0]))
1010                 return (EINVAL);
1011         rman_idx = name[1];
1012         res_idx = name[2];
1013
1014         /*
1015          * Find the indexed resource manager
1016          */
1017         mtx_lock(&rman_mtx);
1018         TAILQ_FOREACH(rm, &rman_head, rm_link) {
1019                 if (rman_idx-- == 0)
1020                         break;
1021         }
1022         mtx_unlock(&rman_mtx);
1023         if (rm == NULL)
1024                 return (ENOENT);
1025
1026         /*
1027          * If the resource index is -1, we want details on the
1028          * resource manager.
1029          */
1030         if (res_idx == -1) {
1031                 bzero(&urm, sizeof(urm));
1032                 urm.rm_handle = (uintptr_t)rm;
1033                 if (rm->rm_descr != NULL)
1034                         strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
1035                 urm.rm_start = rm->rm_start;
1036                 urm.rm_size = rm->rm_end - rm->rm_start + 1;
1037                 urm.rm_type = rm->rm_type;
1038
1039                 error = SYSCTL_OUT(req, &urm, sizeof(urm));
1040                 return (error);
1041         }
1042
1043         /*
1044          * Find the indexed resource and return it.
1045          */
1046         mtx_lock(rm->rm_mtx);
1047         TAILQ_FOREACH(res, &rm->rm_list, r_link) {
1048                 if (res->r_sharehead != NULL) {
1049                         LIST_FOREACH(sres, res->r_sharehead, r_sharelink)
1050                                 if (res_idx-- == 0) {
1051                                         res = sres;
1052                                         goto found;
1053                                 }
1054                 }
1055                 else if (res_idx-- == 0)
1056                                 goto found;
1057         }
1058         mtx_unlock(rm->rm_mtx);
1059         return (ENOENT);
1060
1061 found:
1062         bzero(&ures, sizeof(ures));
1063         ures.r_handle = (uintptr_t)res;
1064         ures.r_parent = (uintptr_t)res->r_rm;
1065         ures.r_device = (uintptr_t)res->r_dev;
1066         if (res->r_dev != NULL) {
1067                 if (device_get_name(res->r_dev) != NULL) {
1068                         snprintf(ures.r_devname, RM_TEXTLEN,
1069                             "%s%d",
1070                             device_get_name(res->r_dev),
1071                             device_get_unit(res->r_dev));
1072                 } else {
1073                         strlcpy(ures.r_devname, "nomatch",
1074                             RM_TEXTLEN);
1075                 }
1076         } else {
1077                 ures.r_devname[0] = '\0';
1078         }
1079         ures.r_start = res->r_start;
1080         ures.r_size = res->r_end - res->r_start + 1;
1081         ures.r_flags = res->r_flags;
1082
1083         mtx_unlock(rm->rm_mtx);
1084         error = SYSCTL_OUT(req, &ures, sizeof(ures));
1085         return (error);
1086 }
1087
1088 static SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
1089     "kernel resource manager");
1090
1091 #ifdef DDB
1092 static void
1093 dump_rman_header(struct rman *rm)
1094 {
1095
1096         if (db_pager_quit)
1097                 return;
1098         db_printf("rman %p: %s (0x%lx-0x%lx full range)\n",
1099             rm, rm->rm_descr, rm->rm_start, rm->rm_end);
1100 }
1101
1102 static void
1103 dump_rman(struct rman *rm)
1104 {
1105         struct resource_i *r;
1106         const char *devname;
1107
1108         if (db_pager_quit)
1109                 return;
1110         TAILQ_FOREACH(r, &rm->rm_list, r_link) {
1111                 if (r->r_dev != NULL) {
1112                         devname = device_get_nameunit(r->r_dev);
1113                         if (devname == NULL)
1114                                 devname = "nomatch";
1115                 } else
1116                         devname = NULL;
1117                 db_printf("    0x%lx-0x%lx ", r->r_start, r->r_end);
1118                 if (devname != NULL)
1119                         db_printf("(%s)\n", devname);
1120                 else
1121                         db_printf("----\n");
1122                 if (db_pager_quit)
1123                         return;
1124         }
1125 }
1126
1127 DB_SHOW_COMMAND(rman, db_show_rman)
1128 {
1129
1130         if (have_addr) {
1131                 dump_rman_header((struct rman *)addr);
1132                 dump_rman((struct rman *)addr);
1133         }
1134 }
1135
1136 DB_SHOW_COMMAND(rmans, db_show_rmans)
1137 {
1138         struct rman *rm;
1139
1140         TAILQ_FOREACH(rm, &rman_head, rm_link) {
1141                 dump_rman_header(rm);
1142         }
1143 }
1144
1145 DB_SHOW_ALL_COMMAND(rman, db_show_all_rman)
1146 {
1147         struct rman *rm;
1148
1149         TAILQ_FOREACH(rm, &rman_head, rm_link) {
1150                 dump_rman_header(rm);
1151                 dump_rman(rm);
1152         }
1153 }
1154 DB_SHOW_ALIAS(allrman, db_show_all_rman);
1155 #endif