subr_rman.c revision 67261
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 67261 2000-10-17 22:08:03Z imp $ 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/bus.h> /* XXX debugging */ 66#include <machine/bus.h> 67#include <sys/rman.h> 68 69#ifdef RMAN_DEBUG 70#define DPRINTF(params) printf##params 71#else 72#define DPRINTF(params) 73#endif 74 75static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager"); 76 77struct rman_head rman_head; 78#ifndef NULL_SIMPLELOCKS 79static struct simplelock rman_lock; /* mutex to protect rman_head */ 80#endif 81static int int_rman_activate_resource(struct rman *rm, struct resource *r, 82 struct resource **whohas); 83static int int_rman_deactivate_resource(struct resource *r); 84static int int_rman_release_resource(struct rman *rm, struct resource *r); 85 86#define CIRCLEQ_TERMCOND(var, head) (var == (void *)&(head)) 87 88int 89rman_init(struct rman *rm) 90{ 91 static int once; 92 93 if (once == 0) { 94 once = 1; 95 TAILQ_INIT(&rman_head); 96 simple_lock_init(&rman_lock); 97 } 98 99 if (rm->rm_type == RMAN_UNINIT) 100 panic("rman_init"); 101 if (rm->rm_type == RMAN_GAUGE) 102 panic("implement RMAN_GAUGE"); 103 104 CIRCLEQ_INIT(&rm->rm_list); 105 rm->rm_slock = malloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT); 106 if (rm->rm_slock == 0) 107 return ENOMEM; 108 simple_lock_init(rm->rm_slock); 109 110 simple_lock(&rman_lock); 111 TAILQ_INSERT_TAIL(&rman_head, rm, rm_link); 112 simple_unlock(&rman_lock); 113 return 0; 114} 115 116/* 117 * NB: this interface is not robust against programming errors which 118 * add multiple copies of the same region. 119 */ 120int 121rman_manage_region(struct rman *rm, u_long start, u_long end) 122{ 123 struct resource *r, *s; 124 125 r = malloc(sizeof *r, M_RMAN, M_NOWAIT); 126 if (r == 0) 127 return ENOMEM; 128 bzero(r, sizeof *r); 129 r->r_sharehead = 0; 130 r->r_start = start; 131 r->r_end = end; 132 r->r_flags = 0; 133 r->r_dev = 0; 134 r->r_rm = rm; 135 136 simple_lock(rm->rm_slock); 137 for (s = CIRCLEQ_FIRST(&rm->rm_list); 138 !CIRCLEQ_TERMCOND(s, rm->rm_list) && s->r_end < r->r_start; 139 s = CIRCLEQ_NEXT(s, r_link)) 140 ; 141 142 if (CIRCLEQ_TERMCOND(s, rm->rm_list)) { 143 CIRCLEQ_INSERT_TAIL(&rm->rm_list, r, r_link); 144 } else { 145 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, r, r_link); 146 } 147 148 simple_unlock(rm->rm_slock); 149 return 0; 150} 151 152int 153rman_fini(struct rman *rm) 154{ 155 struct resource *r; 156 157 simple_lock(rm->rm_slock); 158 CIRCLEQ_FOREACH(r, &rm->rm_list, r_link) { 159 if (r->r_flags & RF_ALLOCATED) { 160 simple_unlock(rm->rm_slock); 161 return EBUSY; 162 } 163 } 164 165 /* 166 * There really should only be one of these if we are in this 167 * state and the code is working properly, but it can't hurt. 168 */ 169 while (!CIRCLEQ_EMPTY(&rm->rm_list)) { 170 r = CIRCLEQ_FIRST(&rm->rm_list); 171 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link); 172 free(r, M_RMAN); 173 } 174 simple_unlock(rm->rm_slock); 175 simple_lock(&rman_lock); 176 TAILQ_REMOVE(&rman_head, rm, rm_link); 177 simple_unlock(&rman_lock); 178 free(rm->rm_slock, M_RMAN); 179 180 return 0; 181} 182 183struct resource * 184rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count, 185 u_int flags, struct device *dev) 186{ 187 u_int want_activate; 188 struct resource *r, *s, *rv; 189 u_long rstart, rend; 190 191 rv = 0; 192 193 DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length " 194 "%#lx, flags %u, device %s%d\n", rm->rm_descr, start, end, 195 count, flags, device_get_name(dev), device_get_unit(dev))); 196 want_activate = (flags & RF_ACTIVE); 197 flags &= ~RF_ACTIVE; 198 199 simple_lock(rm->rm_slock); 200 201 for (r = CIRCLEQ_FIRST(&rm->rm_list); 202 !CIRCLEQ_TERMCOND(r, rm->rm_list) && r->r_end < start; 203 r = CIRCLEQ_NEXT(r, r_link)) 204 ; 205 206 if (CIRCLEQ_TERMCOND(r, rm->rm_list)) { 207 DPRINTF(("could not find a region\n")); 208 goto out; 209 } 210 211 /* 212 * First try to find an acceptable totally-unshared region. 213 */ 214 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list); 215 s = CIRCLEQ_NEXT(s, r_link)) { 216 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end)); 217 if (s->r_start > end) { 218 DPRINTF(("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end)); 219 break; 220 } 221 if (s->r_flags & RF_ALLOCATED) { 222 DPRINTF(("region is allocated\n")); 223 continue; 224 } 225 rstart = max(s->r_start, start); 226 rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) & 227 ~((1ul << RF_ALIGNMENT(flags)) - 1); 228 rend = min(s->r_end, max(rstart + count, end)); 229 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n", 230 rstart, rend, (rend - rstart + 1), count)); 231 232 if ((rend - rstart + 1) >= count) { 233 DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n", 234 rend, rstart, (rend - rstart + 1))); 235 if ((s->r_end - s->r_start + 1) == count) { 236 DPRINTF(("candidate region is entire chunk\n")); 237 rv = s; 238 rv->r_flags |= RF_ALLOCATED | flags; 239 rv->r_dev = dev; 240 goto out; 241 } 242 243 /* 244 * If s->r_start < rstart and 245 * s->r_end > rstart + count - 1, then 246 * we need to split the region into three pieces 247 * (the middle one will get returned to the user). 248 * Otherwise, we are allocating at either the 249 * beginning or the end of s, so we only need to 250 * split it in two. The first case requires 251 * two new allocations; the second requires but one. 252 */ 253 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT); 254 if (rv == 0) 255 goto out; 256 bzero(rv, sizeof *rv); 257 rv->r_start = rstart; 258 rv->r_end = rstart + count - 1; 259 rv->r_flags = flags | RF_ALLOCATED; 260 rv->r_dev = dev; 261 rv->r_sharehead = 0; 262 rv->r_rm = rm; 263 264 if (s->r_start < rv->r_start && s->r_end > rv->r_end) { 265 DPRINTF(("splitting region in three parts: " 266 "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n", 267 s->r_start, rv->r_start - 1, 268 rv->r_start, rv->r_end, 269 rv->r_end + 1, s->r_end)); 270 /* 271 * We are allocating in the middle. 272 */ 273 r = malloc(sizeof *r, M_RMAN, M_NOWAIT); 274 if (r == 0) { 275 free(rv, M_RMAN); 276 rv = 0; 277 goto out; 278 } 279 bzero(r, sizeof *r); 280 r->r_start = rv->r_end + 1; 281 r->r_end = s->r_end; 282 r->r_flags = s->r_flags; 283 r->r_dev = 0; 284 r->r_sharehead = 0; 285 r->r_rm = rm; 286 s->r_end = rv->r_start - 1; 287 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv, 288 r_link); 289 CIRCLEQ_INSERT_AFTER(&rm->rm_list, rv, r, 290 r_link); 291 } else if (s->r_start == rv->r_start) { 292 DPRINTF(("allocating from the beginning\n")); 293 /* 294 * We are allocating at the beginning. 295 */ 296 s->r_start = rv->r_end + 1; 297 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, s, rv, 298 r_link); 299 } else { 300 DPRINTF(("allocating at the end\n")); 301 /* 302 * We are allocating at the end. 303 */ 304 s->r_end = rv->r_start - 1; 305 CIRCLEQ_INSERT_AFTER(&rm->rm_list, s, rv, 306 r_link); 307 } 308 goto out; 309 } 310 } 311 312 /* 313 * Now find an acceptable shared region, if the client's requirements 314 * allow sharing. By our implementation restriction, a candidate 315 * region must match exactly by both size and sharing type in order 316 * to be considered compatible with the client's request. (The 317 * former restriction could probably be lifted without too much 318 * additional work, but this does not seem warranted.) 319 */ 320 DPRINTF(("no unshared regions found\n")); 321 if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0) 322 goto out; 323 324 for (s = r; !CIRCLEQ_TERMCOND(s, rm->rm_list); 325 s = CIRCLEQ_NEXT(s, r_link)) { 326 if (s->r_start > end) 327 break; 328 if ((s->r_flags & flags) != flags) 329 continue; 330 rstart = max(s->r_start, start); 331 rend = min(s->r_end, max(start + count, end)); 332 if (s->r_start >= start && s->r_end <= end 333 && (s->r_end - s->r_start + 1) == count) { 334 rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT); 335 if (rv == 0) 336 goto out; 337 bzero(rv, sizeof *rv); 338 rv->r_start = s->r_start; 339 rv->r_end = s->r_end; 340 rv->r_flags = s->r_flags & 341 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE); 342 rv->r_dev = dev; 343 rv->r_rm = rm; 344 if (s->r_sharehead == 0) { 345 s->r_sharehead = malloc(sizeof *s->r_sharehead, 346 M_RMAN, M_NOWAIT); 347 if (s->r_sharehead == 0) { 348 free(rv, M_RMAN); 349 rv = 0; 350 goto out; 351 } 352 bzero(s->r_sharehead, sizeof *s->r_sharehead); 353 LIST_INIT(s->r_sharehead); 354 LIST_INSERT_HEAD(s->r_sharehead, s, 355 r_sharelink); 356 s->r_flags |= RF_FIRSTSHARE; 357 } 358 rv->r_sharehead = s->r_sharehead; 359 LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink); 360 goto out; 361 } 362 } 363 364 /* 365 * We couldn't find anything. 366 */ 367out: 368 /* 369 * If the user specified RF_ACTIVE in the initial flags, 370 * which is reflected in `want_activate', we attempt to atomically 371 * activate the resource. If this fails, we release the resource 372 * and indicate overall failure. (This behavior probably doesn't 373 * make sense for RF_TIMESHARE-type resources.) 374 */ 375 if (rv && want_activate) { 376 struct resource *whohas; 377 if (int_rman_activate_resource(rm, rv, &whohas)) { 378 int_rman_release_resource(rm, rv); 379 rv = 0; 380 } 381 } 382 383 simple_unlock(rm->rm_slock); 384 return (rv); 385} 386 387static int 388int_rman_activate_resource(struct rman *rm, struct resource *r, 389 struct resource **whohas) 390{ 391 struct resource *s; 392 int ok; 393 394 /* 395 * If we are not timesharing, then there is nothing much to do. 396 * If we already have the resource, then there is nothing at all to do. 397 * If we are not on a sharing list with anybody else, then there is 398 * little to do. 399 */ 400 if ((r->r_flags & RF_TIMESHARE) == 0 401 || (r->r_flags & RF_ACTIVE) != 0 402 || r->r_sharehead == 0) { 403 r->r_flags |= RF_ACTIVE; 404 return 0; 405 } 406 407 ok = 1; 408 for (s = LIST_FIRST(r->r_sharehead); s && ok; 409 s = LIST_NEXT(s, r_sharelink)) { 410 if ((s->r_flags & RF_ACTIVE) != 0) { 411 ok = 0; 412 *whohas = s; 413 } 414 } 415 if (ok) { 416 r->r_flags |= RF_ACTIVE; 417 return 0; 418 } 419 return EBUSY; 420} 421 422int 423rman_activate_resource(struct resource *r) 424{ 425 int rv; 426 struct resource *whohas; 427 struct rman *rm; 428 429 rm = r->r_rm; 430 simple_lock(rm->rm_slock); 431 rv = int_rman_activate_resource(rm, r, &whohas); 432 simple_unlock(rm->rm_slock); 433 return rv; 434} 435 436int 437rman_await_resource(struct resource *r, int pri, int timo) 438{ 439 int rv, s; 440 struct resource *whohas; 441 struct rman *rm; 442 443 rm = r->r_rm; 444 for (;;) { 445 simple_lock(rm->rm_slock); 446 rv = int_rman_activate_resource(rm, r, &whohas); 447 if (rv != EBUSY) 448 return (rv); /* returns with simplelock */ 449 450 if (r->r_sharehead == 0) 451 panic("rman_await_resource"); 452 /* 453 * splhigh hopefully will prevent a race between 454 * simple_unlock and tsleep where a process 455 * could conceivably get in and release the resource 456 * before we have a chance to sleep on it. 457 */ 458 s = splhigh(); 459 whohas->r_flags |= RF_WANTED; 460 simple_unlock(rm->rm_slock); 461 rv = tsleep(r->r_sharehead, pri, "rmwait", timo); 462 if (rv) { 463 splx(s); 464 return rv; 465 } 466 simple_lock(rm->rm_slock); 467 splx(s); 468 } 469} 470 471static int 472int_rman_deactivate_resource(struct resource *r) 473{ 474 struct rman *rm; 475 476 rm = r->r_rm; 477 r->r_flags &= ~RF_ACTIVE; 478 if (r->r_flags & RF_WANTED) { 479 r->r_flags &= ~RF_WANTED; 480 wakeup(r->r_sharehead); 481 } 482 return 0; 483} 484 485int 486rman_deactivate_resource(struct resource *r) 487{ 488 struct rman *rm; 489 490 rm = r->r_rm; 491 simple_lock(rm->rm_slock); 492 int_rman_deactivate_resource(r); 493 simple_unlock(rm->rm_slock); 494 return 0; 495} 496 497static int 498int_rman_release_resource(struct rman *rm, struct resource *r) 499{ 500 struct resource *s, *t; 501 502 if (r->r_flags & RF_ACTIVE) 503 int_rman_deactivate_resource(r); 504 505 /* 506 * Check for a sharing list first. If there is one, then we don't 507 * have to think as hard. 508 */ 509 if (r->r_sharehead) { 510 /* 511 * If a sharing list exists, then we know there are at 512 * least two sharers. 513 * 514 * If we are in the main circleq, appoint someone else. 515 */ 516 LIST_REMOVE(r, r_sharelink); 517 s = LIST_FIRST(r->r_sharehead); 518 if (r->r_flags & RF_FIRSTSHARE) { 519 s->r_flags |= RF_FIRSTSHARE; 520 CIRCLEQ_INSERT_BEFORE(&rm->rm_list, r, s, r_link); 521 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link); 522 } 523 524 /* 525 * Make sure that the sharing list goes away completely 526 * if the resource is no longer being shared at all. 527 */ 528 if (LIST_NEXT(s, r_sharelink) == 0) { 529 free(s->r_sharehead, M_RMAN); 530 s->r_sharehead = 0; 531 s->r_flags &= ~RF_FIRSTSHARE; 532 } 533 goto out; 534 } 535 536 /* 537 * Look at the adjacent resources in the list and see if our 538 * segment can be merged with any of them. 539 */ 540 s = CIRCLEQ_PREV(r, r_link); 541 t = CIRCLEQ_NEXT(r, r_link); 542 543 if (s != (void *)&rm->rm_list && (s->r_flags & RF_ALLOCATED) == 0 544 && t != (void *)&rm->rm_list && (t->r_flags & RF_ALLOCATED) == 0) { 545 /* 546 * Merge all three segments. 547 */ 548 s->r_end = t->r_end; 549 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link); 550 CIRCLEQ_REMOVE(&rm->rm_list, t, r_link); 551 free(t, M_RMAN); 552 } else if (s != (void *)&rm->rm_list 553 && (s->r_flags & RF_ALLOCATED) == 0) { 554 /* 555 * Merge previous segment with ours. 556 */ 557 s->r_end = r->r_end; 558 CIRCLEQ_REMOVE(&rm->rm_list, r, r_link); 559 } else if (t != (void *)&rm->rm_list 560 && (t->r_flags & RF_ALLOCATED) == 0) { 561 /* 562 * Merge next segment with ours. 563 */ 564 t->r_start = r->r_start; 565 CIRCLEQ_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 simple_lock(rm->rm_slock); 592 rv = int_rman_release_resource(rm, r); 593 simple_unlock(rm->rm_slock); 594 return (rv); 595} 596 597uint32_t 598rman_make_alignment_flags(uint32_t size) 599{ 600 int i; 601 int count; 602 603 for (i = 0, count = 0; i < 32 && size > 0x01; i++) { 604 count += size & 1; 605 size >>= 1; 606 } 607 608 if (count > 0) 609 i ++; 610 611 if (i > 31) 612 i = 0; 613 614 return(RF_ALIGNMENT_LOG2(i)); 615 } 616