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