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