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