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 <sys/cdefs.h> 59 __FBSDID("$FreeBSD$"); 60 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 /* 74 * We use a linked list rather than a bitmap because we need to be able to 75 * represent potentially huge objects (like all of a processor's physical 76 * address space). That is also why the indices are defined to have type 77 * `unsigned long' -- that being the largest integral type in ISO C (1990). 78 * The 1999 version of C allows `long long'; we may need to switch to that 79 * at some point in the future, particularly if we want to support 36-bit 80 * addresses on IA32 hardware. 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 u_long r_start; /* index of the first entry in this resource */ 88 u_long r_end; /* index of the last entry (inclusive) */ 89 u_int r_flags; 90 void *r_virtual; /* virtual address of this resource */ 91 struct device *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 }; 95 96 int rman_debug = 0; 97 TUNABLE_INT("debug.rman_debug", &rman_debug); 98 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW, 99 &rman_debug, 0, "rman debug"); 100 101 #define DPRINTF(params) if (rman_debug) printf params 102 103 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager"); 104 105 struct rman_head rman_head; 106 static struct mtx rman_mtx; /* mutex to protect rman_head */ 107 static int int_rman_activate_resource(struct rman *rm, struct resource_i *r, 108 struct resource_i **whohas); 109 static int int_rman_deactivate_resource(struct resource_i *r); 110 static int int_rman_release_resource(struct rman *rm, struct resource_i *r); 111 112 static __inline struct resource_i * 113 int_alloc_resource(int malloc_flag) 114 { 115 struct resource_i *r; 116 117 r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO); 118 if (r != NULL) { 119 r->r_r.__r_i = r; 120 } 121 return (r); 122 } 123 124 int 125 rman_init(struct rman *rm) 126 { 127 static int once = 0; 128 129 if (once == 0) { 130 once = 1; 131 TAILQ_INIT(&rman_head); 132 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF); 133 } 134 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 /* 153 * NB: this interface is not robust against programming errors which 154 * add multiple copies of the same region. 155 */ 156 int 157 rman_manage_region(struct rman *rm, u_long start, u_long end) 158 { 159 struct resource_i *r, *s, *t; 160 161 DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n", 162 rm->rm_descr, start, end)); 163 r = int_alloc_resource(M_NOWAIT); 164 if (r == NULL) 165 return ENOMEM; 166 r->r_start = start; 167 r->r_end = end; 168 r->r_rm = rm; 169 170 mtx_lock(rm->rm_mtx); 171 172 /* Skip entries before us. */ 173 TAILQ_FOREACH(s, &rm->rm_list, r_link) { 174 if (s->r_end == ULONG_MAX) 175 break; 176 if (s->r_end + 1 >= r->r_start) 177 break; 178 } 179 180 /* If we ran off the end of the list, insert at the tail. */ 181 if (s == NULL) { 182 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link); 183 } else { 184 /* Check for any overlap with the current region. */ 185 if (r->r_start <= s->r_end && r->r_end >= s->r_start) 186 return EBUSY; 187 188 /* Check for any overlap with the next region. */ 189 t = TAILQ_NEXT(s, r_link); 190 if (t && r->r_start <= t->r_end && r->r_end >= t->r_start) 191 return EBUSY; 192 193 /* 194 * See if this region can be merged with the next region. If 195 * not, clear the pointer. 196 */ 197 if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0)) 198 t = NULL; 199 200 /* See if we can merge with the current region. */ 201 if (s->r_end + 1 == r->r_start && s->r_flags == 0) { 202 /* Can we merge all 3 regions? */ 203 if (t != NULL) { 204 s->r_end = t->r_end; 205 TAILQ_REMOVE(&rm->rm_list, t, r_link); 206 free(r, M_RMAN); 207 free(t, M_RMAN); 208 } else { 209 s->r_end = r->r_end; 210 free(r, M_RMAN); 211 } 212 } else if (t != NULL) { 213 /* Can we merge with just the next region? */ 214 t->r_start = r->r_start; 215 free(r, M_RMAN); 216 } else if (s->r_end < r->r_start) { 217 TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link); 218 } else { 219 TAILQ_INSERT_BEFORE(s, r, r_link); 220 } 221 } 222 223 mtx_unlock(rm->rm_mtx); 224 return 0; 225 } 226 227 int 228 rman_init_from_resource(struct rman *rm, struct resource *r) 229 { 230 int rv; 231 232 if ((rv = rman_init(rm)) != 0) 233 return (rv); 234 return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end)); 235 } 236 237 int 238 rman_fini(struct rman *rm) 239 { 240 struct resource_i *r; 241 242 mtx_lock(rm->rm_mtx); 243 TAILQ_FOREACH(r, &rm->rm_list, r_link) { 244 if (r->r_flags & RF_ALLOCATED) { 245 mtx_unlock(rm->rm_mtx); 246 return EBUSY; 247 } 248 } 249 250 /* 251 * There really should only be one of these if we are in this 252 * state and the code is working properly, but it can't hurt. 253 */ 254 while (!TAILQ_EMPTY(&rm->rm_list)) { 255 r = TAILQ_FIRST(&rm->rm_list); 256 TAILQ_REMOVE(&rm->rm_list, r, r_link); 257 free(r, M_RMAN); 258 } 259 mtx_unlock(rm->rm_mtx); 260 mtx_lock(&rman_mtx); 261 TAILQ_REMOVE(&rman_head, rm, rm_link); 262 mtx_unlock(&rman_mtx); 263 mtx_destroy(rm->rm_mtx); 264 free(rm->rm_mtx, M_RMAN); 265 266 return 0; 267 } 268 269 struct resource * 270 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end, 271 u_long count, u_long bound, u_int flags, 272 struct device *dev) 273 { 274 u_int want_activate; 275 struct resource_i *r, *s, *rv; 276 u_long rstart, rend, amask, bmask; 277 278 rv = NULL; 279 280 DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], " 281 "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end, 282 count, flags, 283 dev == NULL ? "<null>" : device_get_nameunit(dev))); 284 want_activate = (flags & RF_ACTIVE); 285 flags &= ~RF_ACTIVE; 286 287 mtx_lock(rm->rm_mtx); 288 289 for (r = TAILQ_FIRST(&rm->rm_list); 290 r && r->r_end < start; 291 r = TAILQ_NEXT(r, r_link)) 292 ; 293 294 if (r == NULL) { 295 DPRINTF(("could not find a region\n")); 296 goto out; 297 } 298 299 amask = (1ul << RF_ALIGNMENT(flags)) - 1; 300 /* If bound is 0, bmask will also be 0 */ 301 bmask = ~(bound - 1); 302 /* 303 * First try to find an acceptable totally-unshared region. 304 */ 305 for (s = r; s; s = TAILQ_NEXT(s, r_link)) { 306 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end)); 307 if (s->r_start + count - 1 > end) { 308 DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n", 309 s->r_start, end)); 310 break; 311 } 312 if (s->r_flags & RF_ALLOCATED) { 313 DPRINTF(("region is allocated\n")); 314 continue; 315 } 316 rstart = ulmax(s->r_start, start); 317 /* 318 * Try to find a region by adjusting to boundary and alignment 319 * until both conditions are satisfied. This is not an optimal 320 * algorithm, but in most cases it isn't really bad, either. 321 */ 322 do { 323 rstart = (rstart + amask) & ~amask; 324 if (((rstart ^ (rstart + count - 1)) & bmask) != 0) 325 rstart += bound - (rstart & ~bmask); 326 } while ((rstart & amask) != 0 && rstart < end && 327 rstart < s->r_end); 328 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end)); 329 if (rstart > rend) { 330 DPRINTF(("adjusted start exceeds end\n")); 331 continue; 332 } 333 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n", 334 rstart, rend, (rend - rstart + 1), count)); 335 336 if ((rend - rstart + 1) >= count) { 337 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n", 338 rstart, rend, (rend - rstart + 1))); 339 if ((s->r_end - s->r_start + 1) == count) { 340 DPRINTF(("candidate region is entire chunk\n")); 341 rv = s; 342 rv->r_flags |= RF_ALLOCATED | flags; 343 rv->r_dev = dev; 344 goto out; 345 } 346 347 /* 348 * If s->r_start < rstart and 349 * s->r_end > rstart + count - 1, then 350 * we need to split the region into three pieces 351 * (the middle one will get returned to the user). 352 * Otherwise, we are allocating at either the 353 * beginning or the end of s, so we only need to 354 * split it in two. The first case requires 355 * two new allocations; the second requires but one. 356 */ 357 rv = int_alloc_resource(M_NOWAIT); 358 if (rv == NULL) 359 goto out; 360 rv->r_start = rstart; 361 rv->r_end = rstart + count - 1; 362 rv->r_flags = flags | RF_ALLOCATED; 363 rv->r_dev = dev; 364 rv->r_rm = rm; 365 366 if (s->r_start < rv->r_start && s->r_end > rv->r_end) { 367 DPRINTF(("splitting region in three parts: " 368 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n", 369 s->r_start, rv->r_start - 1, 370 rv->r_start, rv->r_end, 371 rv->r_end + 1, s->r_end)); 372 /* 373 * We are allocating in the middle. 374 */ 375 r = int_alloc_resource(M_NOWAIT); 376 if (r == NULL) { 377 free(rv, M_RMAN); 378 rv = NULL; 379 goto out; 380 } 381 r->r_start = rv->r_end + 1; 382 r->r_end = s->r_end; 383 r->r_flags = s->r_flags; 384 r->r_rm = rm; 385 s->r_end = rv->r_start - 1; 386 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv, 387 r_link); 388 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r, 389 r_link); 390 } else if (s->r_start == rv->r_start) { 391 DPRINTF(("allocating from the beginning\n")); 392 /* 393 * We are allocating at the beginning. 394 */ 395 s->r_start = rv->r_end + 1; 396 TAILQ_INSERT_BEFORE(s, rv, r_link); 397 } else { 398 DPRINTF(("allocating at the end\n")); 399 /* 400 * We are allocating at the end. 401 */ 402 s->r_end = rv->r_start - 1; 403 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv, 404 r_link); 405 } 406 goto out; 407 } 408 } 409 410 /* 411 * Now find an acceptable shared region, if the client's requirements 412 * allow sharing. By our implementation restriction, a candidate 413 * region must match exactly by both size and sharing type in order 414 * to be considered compatible with the client's request. (The 415 * former restriction could probably be lifted without too much 416 * additional work, but this does not seem warranted.) 417 */ 418 DPRINTF(("no unshared regions found\n")); 419 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0) 420 goto out; 421 422 for (s = r; s; s = TAILQ_NEXT(s, r_link)) { 423 if (s->r_start > end) 424 break; 425 if ((s->r_flags & flags) != flags) 426 continue; 427 rstart = ulmax(s->r_start, start); 428 rend = ulmin(s->r_end, ulmax(start + count - 1, end)); 429 if (s->r_start >= start && s->r_end <= end 430 && (s->r_end - s->r_start + 1) == count && 431 (s->r_start & amask) == 0 && 432 ((s->r_start ^ s->r_end) & bmask) == 0) { 433 rv = int_alloc_resource(M_NOWAIT); 434 if (rv == NULL) 435 goto out; 436 rv->r_start = s->r_start; 437 rv->r_end = s->r_end; 438 rv->r_flags = s->r_flags & 439 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE); 440 rv->r_dev = dev; 441 rv->r_rm = rm; 442 if (s->r_sharehead == NULL) { 443 s->r_sharehead = malloc(sizeof *s->r_sharehead, 444 M_RMAN, M_NOWAIT | M_ZERO); 445 if (s->r_sharehead == NULL) { 446 free(rv, M_RMAN); 447 rv = NULL; 448 goto out; 449 } 450 LIST_INIT(s->r_sharehead); 451 LIST_INSERT_HEAD(s->r_sharehead, s, 452 r_sharelink); 453 s->r_flags |= RF_FIRSTSHARE; 454 } 455 rv->r_sharehead = s->r_sharehead; 456 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink); 457 goto out; 458 } 459 } 460 461 /* 462 * We couldn't find anything. 463 */ 464 out: 465 /* 466 * If the user specified RF_ACTIVE in the initial flags, 467 * which is reflected in `want_activate', we attempt to atomically 468 * activate the resource. If this fails, we release the resource 469 * and indicate overall failure. (This behavior probably doesn't 470 * make sense for RF_TIMESHARE-type resources.) 471 */ 472 if (rv && want_activate) { 473 struct resource_i *whohas; 474 if (int_rman_activate_resource(rm, rv, &whohas)) { 475 int_rman_release_resource(rm, rv); 476 rv = NULL; 477 } 478 } 479 480 mtx_unlock(rm->rm_mtx); 481 return (rv == NULL ? NULL : &rv->r_r); 482 } 483 484 struct resource * 485 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count, 486 u_int flags, struct device *dev) 487 { 488 489 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags, 490 dev)); 491 } 492 493 static int 494 int_rman_activate_resource(struct rman *rm, struct resource_i *r, 495 struct resource_i **whohas) 496 { 497 struct resource_i *s; 498 int ok; 499 500 /* 501 * If we are not timesharing, then there is nothing much to do. 502 * If we already have the resource, then there is nothing at all to do. 503 * If we are not on a sharing list with anybody else, then there is 504 * little to do. 505 */ 506 if ((r->r_flags & RF_TIMESHARE) == 0 507 || (r->r_flags & RF_ACTIVE) != 0 508 || r->r_sharehead == NULL) { 509 r->r_flags |= RF_ACTIVE; 510 return 0; 511 } 512 513 ok = 1; 514 for (s = LIST_FIRST(r->r_sharehead); s && ok; 515 s = LIST_NEXT(s, r_sharelink)) { 516 if ((s->r_flags & RF_ACTIVE) != 0) { 517 ok = 0; 518 *whohas = s; 519 } 520 } 521 if (ok) { 522 r->r_flags |= RF_ACTIVE; 523 return 0; 524 } 525 return EBUSY; 526 } 527 528 int 529 rman_activate_resource(struct resource *re) 530 { 531 int rv; 532 struct resource_i *r, *whohas; 533 struct rman *rm; 534 535 r = re->__r_i; 536 rm = r->r_rm; 537 mtx_lock(rm->rm_mtx); 538 rv = int_rman_activate_resource(rm, r, &whohas); 539 mtx_unlock(rm->rm_mtx); 540 return rv; 541 } 542 543 int 544 rman_await_resource(struct resource *re, int pri, int timo) 545 { 546 int rv; 547 struct resource_i *r, *whohas; 548 struct rman *rm; 549 550 r = re->__r_i; 551 rm = r->r_rm; 552 mtx_lock(rm->rm_mtx); 553 for (;;) { 554 rv = int_rman_activate_resource(rm, r, &whohas); 555 if (rv != EBUSY) 556 return (rv); /* returns with mutex held */ 557 558 if (r->r_sharehead == NULL) 559 panic("rman_await_resource"); 560 whohas->r_flags |= RF_WANTED; 561 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo); 562 if (rv) { 563 mtx_unlock(rm->rm_mtx); 564 return (rv); 565 } 566 } 567 } 568 569 static int 570 int_rman_deactivate_resource(struct resource_i *r) 571 { 572 573 r->r_flags &= ~RF_ACTIVE; 574 if (r->r_flags & RF_WANTED) { 575 r->r_flags &= ~RF_WANTED; 576 wakeup(r->r_sharehead); 577 } 578 return 0; 579 } 580 581 int 582 rman_deactivate_resource(struct resource *r) 583 { 584 struct rman *rm; 585 586 rm = r->__r_i->r_rm; 587 mtx_lock(rm->rm_mtx); 588 int_rman_deactivate_resource(r->__r_i); 589 mtx_unlock(rm->rm_mtx); 590 return 0; 591 } 592 593 static int 594 int_rman_release_resource(struct rman *rm, struct resource_i *r) 595 { 596 struct resource_i *s, *t; 597 598 if (r->r_flags & RF_ACTIVE) 599 int_rman_deactivate_resource(r); 600 601 /* 602 * Check for a sharing list first. If there is one, then we don't 603 * have to think as hard. 604 */ 605 if (r->r_sharehead) { 606 /* 607 * If a sharing list exists, then we know there are at 608 * least two sharers. 609 * 610 * If we are in the main circleq, appoint someone else. 611 */ 612 LIST_REMOVE(r, r_sharelink); 613 s = LIST_FIRST(r->r_sharehead); 614 if (r->r_flags & RF_FIRSTSHARE) { 615 s->r_flags |= RF_FIRSTSHARE; 616 TAILQ_INSERT_BEFORE(r, s, r_link); 617 TAILQ_REMOVE(&rm->rm_list, r, r_link); 618 } 619 620 /* 621 * Make sure that the sharing list goes away completely 622 * if the resource is no longer being shared at all. 623 */ 624 if (LIST_NEXT(s, r_sharelink) == NULL) { 625 free(s->r_sharehead, M_RMAN); 626 s->r_sharehead = NULL; 627 s->r_flags &= ~RF_FIRSTSHARE; 628 } 629 goto out; 630 } 631 632 /* 633 * Look at the adjacent resources in the list and see if our 634 * segment can be merged with any of them. If either of the 635 * resources is allocated or is not exactly adjacent then they 636 * cannot be merged with our segment. 637 */ 638 s = TAILQ_PREV(r, resource_head, r_link); 639 if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 || 640 s->r_end + 1 != r->r_start)) 641 s = NULL; 642 t = TAILQ_NEXT(r, r_link); 643 if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 || 644 r->r_end + 1 != t->r_start)) 645 t = NULL; 646 647 if (s != NULL && t != NULL) { 648 /* 649 * Merge all three segments. 650 */ 651 s->r_end = t->r_end; 652 TAILQ_REMOVE(&rm->rm_list, r, r_link); 653 TAILQ_REMOVE(&rm->rm_list, t, r_link); 654 free(t, M_RMAN); 655 } else if (s != NULL) { 656 /* 657 * Merge previous segment with ours. 658 */ 659 s->r_end = r->r_end; 660 TAILQ_REMOVE(&rm->rm_list, r, r_link); 661 } else if (t != NULL) { 662 /* 663 * Merge next segment with ours. 664 */ 665 t->r_start = r->r_start; 666 TAILQ_REMOVE(&rm->rm_list, r, r_link); 667 } else { 668 /* 669 * At this point, we know there is nothing we 670 * can potentially merge with, because on each 671 * side, there is either nothing there or what is 672 * there is still allocated. In that case, we don't 673 * want to remove r from the list; we simply want to 674 * change it to an unallocated region and return 675 * without freeing anything. 676 */ 677 r->r_flags &= ~RF_ALLOCATED; 678 return 0; 679 } 680 681 out: 682 free(r, M_RMAN); 683 return 0; 684 } 685 686 int 687 rman_release_resource(struct resource *re) 688 { 689 int rv; 690 struct resource_i *r; 691 struct rman *rm; 692 693 r = re->__r_i; 694 rm = r->r_rm; 695 mtx_lock(rm->rm_mtx); 696 rv = int_rman_release_resource(rm, r); 697 mtx_unlock(rm->rm_mtx); 698 return (rv); 699 } 700 701 uint32_t 702 rman_make_alignment_flags(uint32_t size) 703 { 704 int i; 705 706 /* 707 * Find the hightest bit set, and add one if more than one bit 708 * set. We're effectively computing the ceil(log2(size)) here. 709 */ 710 for (i = 31; i > 0; i--) 711 if ((1 << i) & size) 712 break; 713 if (~(1 << i) & size) 714 i++; 715 716 return(RF_ALIGNMENT_LOG2(i)); 717 } 718 719 u_long 720 rman_get_start(struct resource *r) 721 { 722 return (r->__r_i->r_start); 723 } 724 725 u_long 726 rman_get_end(struct resource *r) 727 { 728 return (r->__r_i->r_end); 729 } 730 731 u_long 732 rman_get_size(struct resource *r) 733 { 734 return (r->__r_i->r_end - r->__r_i->r_start + 1); 735 } 736 737 u_int 738 rman_get_flags(struct resource *r) 739 { 740 return (r->__r_i->r_flags); 741 } 742 743 void 744 rman_set_virtual(struct resource *r, void *v) 745 { 746 r->__r_i->r_virtual = v; 747 } 748 749 void * 750 rman_get_virtual(struct resource *r) 751 { 752 return (r->__r_i->r_virtual); 753 } 754 755 void 756 rman_set_bustag(struct resource *r, bus_space_tag_t t) 757 { 758 r->r_bustag = t; 759 } 760 761 bus_space_tag_t 762 rman_get_bustag(struct resource *r) 763 { 764 return (r->r_bustag); 765 } 766 767 void 768 rman_set_bushandle(struct resource *r, bus_space_handle_t h) 769 { 770 r->r_bushandle = h; 771 } 772 773 bus_space_handle_t 774 rman_get_bushandle(struct resource *r) 775 { 776 return (r->r_bushandle); 777 } 778 779 void 780 rman_set_rid(struct resource *r, int rid) 781 { 782 r->__r_i->r_rid = rid; 783 } 784 785 void 786 rman_set_start(struct resource *r, u_long start) 787 { 788 r->__r_i->r_start = start; 789 } 790 791 void 792 rman_set_end(struct resource *r, u_long end) 793 { 794 r->__r_i->r_end = end; 795 } 796 797 int 798 rman_get_rid(struct resource *r) 799 { 800 return (r->__r_i->r_rid); 801 } 802 803 struct device * 804 rman_get_device(struct resource *r) 805 { 806 return (r->__r_i->r_dev); 807 } 808 809 void 810 rman_set_device(struct resource *r, struct device *dev) 811 { 812 r->__r_i->r_dev = dev; 813 } 814 815 int 816 rman_is_region_manager(struct resource *r, struct rman *rm) 817 { 818 819 return (r->__r_i->r_rm == rm); 820 } 821 822 /* 823 * Sysctl interface for scanning the resource lists. 824 * 825 * We take two input parameters; the index into the list of resource 826 * managers, and the resource offset into the list. 827 */ 828 static int 829 sysctl_rman(SYSCTL_HANDLER_ARGS) 830 { 831 int *name = (int *)arg1; 832 u_int namelen = arg2; 833 int rman_idx, res_idx; 834 struct rman *rm; 835 struct resource_i *res; 836 struct u_rman urm; 837 struct u_resource ures; 838 int error; 839 840 if (namelen != 3) 841 return (EINVAL); 842 843 if (bus_data_generation_check(name[0])) 844 return (EINVAL); 845 rman_idx = name[1]; 846 res_idx = name[2]; 847 848 /* 849 * Find the indexed resource manager 850 */ 851 mtx_lock(&rman_mtx); 852 TAILQ_FOREACH(rm, &rman_head, rm_link) { 853 if (rman_idx-- == 0) 854 break; 855 } 856 mtx_unlock(&rman_mtx); 857 if (rm == NULL) 858 return (ENOENT); 859 860 /* 861 * If the resource index is -1, we want details on the 862 * resource manager. 863 */ 864 if (res_idx == -1) { 865 bzero(&urm, sizeof(urm)); 866 urm.rm_handle = (uintptr_t)rm; 867 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN); 868 urm.rm_start = rm->rm_start; 869 urm.rm_size = rm->rm_end - rm->rm_start + 1; 870 urm.rm_type = rm->rm_type; 871 872 error = SYSCTL_OUT(req, &urm, sizeof(urm)); 873 return (error); 874 } 875 876 /* 877 * Find the indexed resource and return it. 878 */ 879 mtx_lock(rm->rm_mtx); 880 TAILQ_FOREACH(res, &rm->rm_list, r_link) { 881 if (res_idx-- == 0) { 882 bzero(&ures, sizeof(ures)); 883 ures.r_handle = (uintptr_t)res; 884 ures.r_parent = (uintptr_t)res->r_rm; 885 ures.r_device = (uintptr_t)res->r_dev; 886 if (res->r_dev != NULL) { 887 if (device_get_name(res->r_dev) != NULL) { 888 snprintf(ures.r_devname, RM_TEXTLEN, 889 "%s%d", 890 device_get_name(res->r_dev), 891 device_get_unit(res->r_dev)); 892 } else { 893 strlcpy(ures.r_devname, "nomatch", 894 RM_TEXTLEN); 895 } 896 } else { 897 ures.r_devname[0] = '\0'; 898 } 899 ures.r_start = res->r_start; 900 ures.r_size = res->r_end - res->r_start + 1; 901 ures.r_flags = res->r_flags; 902 903 mtx_unlock(rm->rm_mtx); 904 error = SYSCTL_OUT(req, &ures, sizeof(ures)); 905 return (error); 906 } 907 } 908 mtx_unlock(rm->rm_mtx); 909 return (ENOENT); 910 } 911 912 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman, 913 "kernel resource manager"); 914