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