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