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 *r, 85 struct resource **whohas); 86 static int int_rman_deactivate_resource(struct resource *r); 87 static int int_rman_release_resource(struct rman *rm, struct resource *r); 88 89 int 90 rman_init(struct rman *rm) 91 { 92 static int once; 93 94 if (once == 0) { 95 once = 1; 96 TAILQ_INIT(&rman_head); 97 mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF); 98 } 99 100 if (rm->rm_type == RMAN_UNINIT) 101 panic("rman_init"); 102 if (rm->rm_type == RMAN_GAUGE) 103 panic("implement RMAN_GAUGE"); 104 105 TAILQ_INIT(&rm->rm_list); 106 rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO); 107 if (rm->rm_mtx == 0) 108 return ENOMEM; 109 mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF); 110 111 mtx_lock(&rman_mtx); 112 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link); 113 mtx_unlock(&rman_mtx); 114 return 0; 115 } 116 117 /* 118 * NB: this interface is not robust against programming errors which 119 * add multiple copies of the same region. 120 */ 121 int 122 rman_manage_region(struct rman *rm, u_long start, u_long end) 123 { 124 struct resource *r, *s; 125 126 r = malloc(sizeof *r, M_RMAN, M_NOWAIT | M_ZERO); 127 if (r == 0) 128 return ENOMEM; 129 r->r_start = start; 130 r->r_end = end; 131 r->r_rm = rm; 132 133 mtx_lock(rm->rm_mtx); 134 for (s = TAILQ_FIRST(&rm->rm_list); 135 s && s->r_end < r->r_start; 136 s = TAILQ_NEXT(s, r_link)) 137 ; 138 139 if (s == NULL) { 140 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link); 141 } else { 142 TAILQ_INSERT_BEFORE(s, r, r_link); 143 } 144 145 mtx_unlock(rm->rm_mtx); 146 return 0; 147 } 148 149 int 150 rman_fini(struct rman *rm) 151 { 152 struct resource *r; 153 154 mtx_lock(rm->rm_mtx); 155 TAILQ_FOREACH(r, &rm->rm_list, r_link) { 156 if (r->r_flags & RF_ALLOCATED) { 157 mtx_unlock(rm->rm_mtx); 158 return EBUSY; 159 } 160 } 161 162 /* 163 * There really should only be one of these if we are in this 164 * state and the code is working properly, but it can't hurt. 165 */ 166 while (!TAILQ_EMPTY(&rm->rm_list)) { 167 r = TAILQ_FIRST(&rm->rm_list); 168 TAILQ_REMOVE(&rm->rm_list, r, r_link); 169 free(r, M_RMAN); 170 } 171 mtx_unlock(rm->rm_mtx); 172 mtx_lock(&rman_mtx); 173 TAILQ_REMOVE(&rman_head, rm, rm_link); 174 mtx_unlock(&rman_mtx); 175 mtx_destroy(rm->rm_mtx); 176 free(rm->rm_mtx, M_RMAN); 177 178 return 0; 179 } 180 181 struct resource * 182 rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end, 183 u_long count, u_long bound, u_int flags, 184 struct device *dev) 185 { 186 u_int want_activate; 187 struct resource *r, *s, *rv; 188 u_long rstart, rend, amask, bmask; 189 190 rv = 0; 191 192 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length " 193 "%#lx, flags %u, device %s\n", rm->rm_descr, start, end, count, 194 flags, dev == NULL ? "<null>" : device_get_nameunit(dev))); 195 want_activate = (flags & RF_ACTIVE); 196 flags &= ~RF_ACTIVE; 197 198 mtx_lock(rm->rm_mtx); 199 200 for (r = TAILQ_FIRST(&rm->rm_list); 201 r && r->r_end < start; 202 r = TAILQ_NEXT(r, r_link)) 203 ; 204 205 if (r == NULL) { 206 DPRINTF(("could not find a region\n")); 207 goto out; 208 } 209 210 amask = (1ul << RF_ALIGNMENT(flags)) - 1; 211 /* If bound is 0, bmask will also be 0 */ 212 bmask = ~(bound - 1); 213 /* 214 * First try to find an acceptable totally-unshared region. 215 */ 216 for (s = r; s; s = TAILQ_NEXT(s, r_link)) { 217 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end)); 218 if (s->r_start > end) { 219 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end)); 220 break; 221 } 222 if (s->r_flags & RF_ALLOCATED) { 223 DPRINTF(("region is allocated\n")); 224 continue; 225 } 226 rstart = ulmax(s->r_start, start); 227 /* 228 * Try to find a region by adjusting to boundary and alignment 229 * until both conditions are satisfied. This is not an optimal 230 * algorithm, but in most cases it isn't really bad, either. 231 */ 232 do { 233 rstart = (rstart + amask) & ~amask; 234 if (((rstart ^ (rstart + count - 1)) & bmask) != 0) 235 rstart += bound - (rstart & ~bmask); 236 } while ((rstart & amask) != 0 && rstart < end && 237 rstart < s->r_end); 238 rend = ulmin(s->r_end, ulmax(rstart + count - 1, end)); 239 if (rstart > rend) { 240 DPRINTF(("adjusted start exceeds end\n")); 241 continue; 242 } 243 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n", 244 rstart, rend, (rend - rstart + 1), count)); 245 246 if ((rend - rstart + 1) >= count) { 247 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n", 248 rend, rstart, (rend - rstart + 1))); 249 if ((s->r_end - s->r_start + 1) == count) { 250 DPRINTF(("candidate region is entire chunk\n")); 251 rv = s; 252 rv->r_flags |= RF_ALLOCATED | flags; 253 rv->r_dev = dev; 254 goto out; 255 } 256 257 /* 258 * If s->r_start < rstart and 259 * s->r_end > rstart + count - 1, then 260 * we need to split the region into three pieces 261 * (the middle one will get returned to the user). 262 * Otherwise, we are allocating at either the 263 * beginning or the end of s, so we only need to 264 * split it in two. The first case requires 265 * two new allocations; the second requires but one. 266 */ 267 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO); 268 if (rv == 0) 269 goto out; 270 rv->r_start = rstart; 271 rv->r_end = rstart + count - 1; 272 rv->r_flags = flags | RF_ALLOCATED; 273 rv->r_dev = dev; 274 rv->r_rm = rm; 275 276 if (s->r_start < rv->r_start && s->r_end > rv->r_end) { 277 DPRINTF(("splitting region in three parts: " 278 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n", 279 s->r_start, rv->r_start - 1, 280 rv->r_start, rv->r_end, 281 rv->r_end + 1, s->r_end)); 282 /* 283 * We are allocating in the middle. 284 */ 285 r = malloc(sizeof *r, M_RMAN, M_NOWAIT|M_ZERO); 286 if (r == 0) { 287 free(rv, M_RMAN); 288 rv = 0; 289 goto out; 290 } 291 r->r_start = rv->r_end + 1; 292 r->r_end = s->r_end; 293 r->r_flags = s->r_flags; 294 r->r_rm = rm; 295 s->r_end = rv->r_start - 1; 296 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv, 297 r_link); 298 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r, 299 r_link); 300 } else if (s->r_start == rv->r_start) { 301 DPRINTF(("allocating from the beginning\n")); 302 /* 303 * We are allocating at the beginning. 304 */ 305 s->r_start = rv->r_end + 1; 306 TAILQ_INSERT_BEFORE(s, rv, r_link); 307 } else { 308 DPRINTF(("allocating at the end\n")); 309 /* 310 * We are allocating at the end. 311 */ 312 s->r_end = rv->r_start - 1; 313 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv, 314 r_link); 315 } 316 goto out; 317 } 318 } 319 320 /* 321 * Now find an acceptable shared region, if the client's requirements 322 * allow sharing. By our implementation restriction, a candidate 323 * region must match exactly by both size and sharing type in order 324 * to be considered compatible with the client's request. (The 325 * former restriction could probably be lifted without too much 326 * additional work, but this does not seem warranted.) 327 */ 328 DPRINTF(("no unshared regions found\n")); 329 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0) 330 goto out; 331 332 for (s = r; s; s = TAILQ_NEXT(s, r_link)) { 333 if (s->r_start > end) 334 break; 335 if ((s->r_flags & flags) != flags) 336 continue; 337 rstart = ulmax(s->r_start, start); 338 rend = ulmin(s->r_end, ulmax(start + count - 1, end)); 339 if (s->r_start >= start && s->r_end <= end 340 && (s->r_end - s->r_start + 1) == count && 341 (s->r_start & amask) == 0 && 342 ((s->r_start ^ s->r_end) & bmask) == 0) { 343 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO); 344 if (rv == 0) 345 goto out; 346 rv->r_start = s->r_start; 347 rv->r_end = s->r_end; 348 rv->r_flags = s->r_flags & 349 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE); 350 rv->r_dev = dev; 351 rv->r_rm = rm; 352 if (s->r_sharehead == 0) { 353 s->r_sharehead = malloc(sizeof *s->r_sharehead, 354 M_RMAN, M_NOWAIT | M_ZERO); 355 if (s->r_sharehead == 0) { 356 free(rv, M_RMAN); 357 rv = 0; 358 goto out; 359 } 360 LIST_INIT(s->r_sharehead); 361 LIST_INSERT_HEAD(s->r_sharehead, s, 362 r_sharelink); 363 s->r_flags |= RF_FIRSTSHARE; 364 } 365 rv->r_sharehead = s->r_sharehead; 366 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink); 367 goto out; 368 } 369 } 370 371 /* 372 * We couldn't find anything. 373 */ 374 out: 375 /* 376 * If the user specified RF_ACTIVE in the initial flags, 377 * which is reflected in `want_activate', we attempt to atomically 378 * activate the resource. If this fails, we release the resource 379 * and indicate overall failure. (This behavior probably doesn't 380 * make sense for RF_TIMESHARE-type resources.) 381 */ 382 if (rv && want_activate) { 383 struct resource *whohas; 384 if (int_rman_activate_resource(rm, rv, &whohas)) { 385 int_rman_release_resource(rm, rv); 386 rv = 0; 387 } 388 } 389 390 mtx_unlock(rm->rm_mtx); 391 return (rv); 392 } 393 394 struct resource * 395 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count, 396 u_int flags, struct device *dev) 397 { 398 399 return (rman_reserve_resource_bound(rm, start, end, count, 0, flags, 400 dev)); 401 } 402 403 static int 404 int_rman_activate_resource(struct rman *rm, struct resource *r, 405 struct resource **whohas) 406 { 407 struct resource *s; 408 int ok; 409 410 /* 411 * If we are not timesharing, then there is nothing much to do. 412 * If we already have the resource, then there is nothing at all to do. 413 * If we are not on a sharing list with anybody else, then there is 414 * little to do. 415 */ 416 if ((r->r_flags & RF_TIMESHARE) == 0 417 || (r->r_flags & RF_ACTIVE) != 0 418 || r->r_sharehead == 0) { 419 r->r_flags |= RF_ACTIVE; 420 return 0; 421 } 422 423 ok = 1; 424 for (s = LIST_FIRST(r->r_sharehead); s && ok; 425 s = LIST_NEXT(s, r_sharelink)) { 426 if ((s->r_flags & RF_ACTIVE) != 0) { 427 ok = 0; 428 *whohas = s; 429 } 430 } 431 if (ok) { 432 r->r_flags |= RF_ACTIVE; 433 return 0; 434 } 435 return EBUSY; 436 } 437 438 int 439 rman_activate_resource(struct resource *r) 440 { 441 int rv; 442 struct resource *whohas; 443 struct rman *rm; 444 445 rm = r->r_rm; 446 mtx_lock(rm->rm_mtx); 447 rv = int_rman_activate_resource(rm, r, &whohas); 448 mtx_unlock(rm->rm_mtx); 449 return rv; 450 } 451 452 int 453 rman_await_resource(struct resource *r, int pri, int timo) 454 { 455 int rv; 456 struct resource *whohas; 457 struct rman *rm; 458 459 rm = r->r_rm; 460 mtx_lock(rm->rm_mtx); 461 for (;;) { 462 rv = int_rman_activate_resource(rm, r, &whohas); 463 if (rv != EBUSY) 464 return (rv); /* returns with mutex held */ 465 466 if (r->r_sharehead == 0) 467 panic("rman_await_resource"); 468 whohas->r_flags |= RF_WANTED; 469 rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo); 470 if (rv) { 471 mtx_unlock(rm->rm_mtx); 472 return (rv); 473 } 474 } 475 } 476 477 static int 478 int_rman_deactivate_resource(struct resource *r) 479 { 480 481 r->r_flags &= ~RF_ACTIVE; 482 if (r->r_flags & RF_WANTED) { 483 r->r_flags &= ~RF_WANTED; 484 wakeup(r->r_sharehead); 485 } 486 return 0; 487 } 488 489 int 490 rman_deactivate_resource(struct resource *r) 491 { 492 struct rman *rm; 493 494 rm = r->r_rm; 495 mtx_lock(rm->rm_mtx); 496 int_rman_deactivate_resource(r); 497 mtx_unlock(rm->rm_mtx); 498 return 0; 499 } 500 501 static int 502 int_rman_release_resource(struct rman *rm, struct resource *r) 503 { 504 struct resource *s, *t; 505 506 if (r->r_flags & RF_ACTIVE) 507 int_rman_deactivate_resource(r); 508 509 /* 510 * Check for a sharing list first. If there is one, then we don't 511 * have to think as hard. 512 */ 513 if (r->r_sharehead) { 514 /* 515 * If a sharing list exists, then we know there are at 516 * least two sharers. 517 * 518 * If we are in the main circleq, appoint someone else. 519 */ 520 LIST_REMOVE(r, r_sharelink); 521 s = LIST_FIRST(r->r_sharehead); 522 if (r->r_flags & RF_FIRSTSHARE) { 523 s->r_flags |= RF_FIRSTSHARE; 524 TAILQ_INSERT_BEFORE(r, s, r_link); 525 TAILQ_REMOVE(&rm->rm_list, r, r_link); 526 } 527 528 /* 529 * Make sure that the sharing list goes away completely 530 * if the resource is no longer being shared at all. 531 */ 532 if (LIST_NEXT(s, r_sharelink) == 0) { 533 free(s->r_sharehead, M_RMAN); 534 s->r_sharehead = 0; 535 s->r_flags &= ~RF_FIRSTSHARE; 536 } 537 goto out; 538 } 539 540 /* 541 * Look at the adjacent resources in the list and see if our 542 * segment can be merged with any of them. 543 */ 544 s = TAILQ_PREV(r, resource_head, r_link); 545 t = TAILQ_NEXT(r, r_link); 546 547 if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0 548 && t != NULL && (t->r_flags & RF_ALLOCATED) == 0) { 549 /* 550 * Merge all three segments. 551 */ 552 s->r_end = t->r_end; 553 TAILQ_REMOVE(&rm->rm_list, r, r_link); 554 TAILQ_REMOVE(&rm->rm_list, t, r_link); 555 free(t, M_RMAN); 556 } else if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0) { 557 /* 558 * Merge previous segment with ours. 559 */ 560 s->r_end = r->r_end; 561 TAILQ_REMOVE(&rm->rm_list, r, r_link); 562 } else if (t != NULL && (t->r_flags & RF_ALLOCATED) == 0) { 563 /* 564 * Merge next segment with ours. 565 */ 566 t->r_start = r->r_start; 567 TAILQ_REMOVE(&rm->rm_list, r, r_link); 568 } else { 569 /* 570 * At this point, we know there is nothing we 571 * can potentially merge with, because on each 572 * side, there is either nothing there or what is 573 * there is still allocated. In that case, we don't 574 * want to remove r from the list; we simply want to 575 * change it to an unallocated region and return 576 * without freeing anything. 577 */ 578 r->r_flags &= ~RF_ALLOCATED; 579 return 0; 580 } 581 582 out: 583 free(r, M_RMAN); 584 return 0; 585 } 586 587 int 588 rman_release_resource(struct resource *r) 589 { 590 int rv; 591 struct rman *rm = r->r_rm; 592 593 mtx_lock(rm->rm_mtx); 594 rv = int_rman_release_resource(rm, r); 595 mtx_unlock(rm->rm_mtx); 596 return (rv); 597 } 598 599 uint32_t 600 rman_make_alignment_flags(uint32_t size) 601 { 602 int i; 603 604 /* 605 * Find the hightest bit set, and add one if more than one bit 606 * set. We're effectively computing the ceil(log2(size)) here. 607 */ 608 for (i = 31; i > 0; i--) 609 if ((1 << i) & size) 610 break; 611 if (~(1 << i) & size) 612 i++; 613 614 return(RF_ALIGNMENT_LOG2(i)); 615 } 616 617 u_long 618 rman_get_start(struct resource *r) 619 { 620 return (r->r_start); 621 } 622 623 u_long 624 rman_get_end(struct resource *r) 625 { 626 return (r->r_end); 627 } 628 629 u_long 630 rman_get_size(struct resource *r) 631 { 632 return (r->r_end - r->r_start + 1); 633 } 634 635 u_int 636 rman_get_flags(struct resource *r) 637 { 638 return (r->r_flags); 639 } 640 641 void 642 rman_set_virtual(struct resource *r, void *v) 643 { 644 r->r_virtual = v; 645 } 646 647 void * 648 rman_get_virtual(struct resource *r) 649 { 650 return (r->r_virtual); 651 } 652 653 void 654 rman_set_bustag(struct resource *r, bus_space_tag_t t) 655 { 656 r->r_bustag = t; 657 } 658 659 bus_space_tag_t 660 rman_get_bustag(struct resource *r) 661 { 662 return (r->r_bustag); 663 } 664 665 void 666 rman_set_bushandle(struct resource *r, bus_space_handle_t h) 667 { 668 r->r_bushandle = h; 669 } 670 671 bus_space_handle_t 672 rman_get_bushandle(struct resource *r) 673 { 674 return (r->r_bushandle); 675 } 676 677 void 678 rman_set_rid(struct resource *r, int rid) 679 { 680 r->r_rid = rid; 681 } 682 683 void 684 rman_set_start(struct resource *r, u_long start) 685 { 686 r->r_start = start; 687 } 688 689 void 690 rman_set_end(struct resource *r, u_long end) 691 { 692 r->r_end = end; 693 } 694 695 int 696 rman_get_rid(struct resource *r) 697 { 698 return (r->r_rid); 699 } 700 701 struct device * 702 rman_get_device(struct resource *r) 703 { 704 return (r->r_dev); 705 } 706