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