subr_rman.c revision 110753
1246057Sganbold/* 2263711Sganbold * Copyright 1998 Massachusetts Institute of Technology 3246057Sganbold * 4246057Sganbold * Permission to use, copy, modify, and distribute this software and 5246057Sganbold * its documentation for any purpose and without fee is hereby 6246057Sganbold * granted, provided that both the above copyright notice and this 7246057Sganbold * permission notice appear in all copies, that both the above 8246057Sganbold * copyright notice and this permission notice appear in all 9246057Sganbold * supporting documentation, and that the name of M.I.T. not be used 10246057Sganbold * in advertising or publicity pertaining to distribution of the 11246057Sganbold * software without specific, written prior permission. M.I.T. makes 12246057Sganbold * no representations about the suitability of this software for any 13246057Sganbold * purpose. It is provided "as is" without express or implied 14246057Sganbold * warranty. 15246057Sganbold * 16246057Sganbold * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17246057Sganbold * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18246057Sganbold * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19246057Sganbold * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20246057Sganbold * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21246057Sganbold * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22246057Sganbold * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23246057Sganbold * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24246057Sganbold * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25246057Sganbold * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26246057Sganbold * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27246057Sganbold * SUCH DAMAGE. 28246057Sganbold * 29246057Sganbold * $FreeBSD: head/sys/kern/subr_rman.c 110753 2003-02-12 07:00:59Z imp $ 30246057Sganbold */ 31246057Sganbold 32246057Sganbold/* 33246057Sganbold * The kernel resource manager. This code is responsible for keeping track 34246057Sganbold * of hardware resources which are apportioned out to various drivers. 35246057Sganbold * It does not actually assign those resources, and it is not expected 36246057Sganbold * that end-device drivers will call into this code directly. Rather, 37246057Sganbold * the code which implements the buses that those devices are attached to, 38246057Sganbold * and the code which manages CPU resources, will call this code, and the 39246057Sganbold * end-device drivers will make upcalls to that code to actually perform 40246057Sganbold * the allocation. 41246057Sganbold * 42246057Sganbold * There are two sorts of resources managed by this code. The first is 43246057Sganbold * the more familiar array (RMAN_ARRAY) type; resources in this class 44246057Sganbold * consist of a sequence of individually-allocatable objects which have 45295464Sandrew * been numbered in some well-defined order. Most of the resources 46246057Sganbold * are of this type, as it is the most familiar. The second type is 47246057Sganbold * called a gauge (RMAN_GAUGE), and models fungible resources (i.e., 48295464Sandrew * resources in which each instance is indistinguishable from every 49246057Sganbold * other instance). The principal anticipated application of gauges 50246057Sganbold * is in the context of power consumption, where a bus may have a specific 51246057Sganbold * power budget which all attached devices share. RMAN_GAUGE is not 52246057Sganbold * implemented yet. 53246057Sganbold * 54246057Sganbold * For array resources, we make one simplifying assumption: two clients 55246057Sganbold * sharing the same resource must use the same range of indices. That 56246057Sganbold * is to say, sharing of overlapping-but-not-identical regions is not 57246057Sganbold * permitted. 58246057Sganbold */ 59246057Sganbold 60246057Sganbold#include <sys/param.h> 61308274Smanu#include <sys/systm.h> 62297627Sjmcneill#include <sys/kernel.h> 63297627Sjmcneill#include <sys/lock.h> 64300728Sjmcneill#include <sys/malloc.h> 65246375Sganbold#include <sys/mutex.h> 66246057Sganbold#include <sys/bus.h> /* XXX debugging */ 67246057Sganbold#include <machine/bus.h> 68246057Sganbold#include <sys/rman.h> 69246057Sganbold#include <sys/sysctl.h> 70246057Sganbold 71246057Sganboldint rman_debug = 0; 72246057SganboldTUNABLE_INT("debug.rman_debug", &rman_debug); 73246057SganboldSYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW, 74246057Sganbold &rman_debug, 0, "rman debug"); 75246057Sganbold 76246375Sganbold#define DPRINTF(params) if (rman_debug) printf params 77246057Sganbold 78246057Sganboldstatic MALLOC_DEFINE(M_RMAN, "rman", "Resource manager"); 79296284Sjmcneill 80296284Sjmcneillstruct rman_head rman_head; 81296284Sjmcneillstatic struct mtx rman_mtx; /* mutex to protect rman_head */ 82246057Sganboldstatic int int_rman_activate_resource(struct rman *rm, struct resource *r, 83246057Sganbold struct resource **whohas); 84246057Sganboldstatic int int_rman_deactivate_resource(struct resource *r); 85246057Sganboldstatic int int_rman_release_resource(struct rman *rm, struct resource *r); 86246057Sganbold 87246057Sganboldint 88246057Sganboldrman_init(struct rman *rm) 89246057Sganbold{ 90246057Sganbold static int once; 91297627Sjmcneill 92297627Sjmcneill if (once == 0) { 93297627Sjmcneill once = 1; 94297627Sjmcneill TAILQ_INIT(&rman_head); 95300728Sjmcneill mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF); 96297627Sjmcneill } 97297627Sjmcneill 98296284Sjmcneill if (rm->rm_type == RMAN_UNINIT) 99296284Sjmcneill panic("rman_init"); 100296284Sjmcneill if (rm->rm_type == RMAN_GAUGE) 101296284Sjmcneill panic("implement RMAN_GAUGE"); 102296284Sjmcneill 103296284Sjmcneill TAILQ_INIT(&rm->rm_list); 104296284Sjmcneill rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO); 105296284Sjmcneill if (rm->rm_mtx == 0) 106296284Sjmcneill return ENOMEM; 107297627Sjmcneill mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF); 108296284Sjmcneill 109296284Sjmcneill mtx_lock(&rman_mtx); 110295464Sandrew TAILQ_INSERT_TAIL(&rman_head, rm, rm_link); 111296284Sjmcneill mtx_unlock(&rman_mtx); 112305436Smanu return 0; 113296284Sjmcneill} 114296284Sjmcneill 115299113Sjmcneill/* 116299688Smanu * NB: this interface is not robust against programming errors which 117296284Sjmcneill * add multiple copies of the same region. 118295464Sandrew */ 119295464Sandrewint 120246057Sganboldrman_manage_region(struct rman *rm, u_long start, u_long end) 121246057Sganbold{ 122246057Sganbold struct resource *r, *s; 123261410Sian 124261410Sian r = malloc(sizeof *r, M_RMAN, M_NOWAIT | M_ZERO); 125261410Sian if (r == 0) 126261410Sian return ENOMEM; 127295464Sandrew r->r_start = start; 128246057Sganbold r->r_end = end; 129246057Sganbold r->r_rm = rm; 130246057Sganbold 131246057Sganbold mtx_lock(rm->rm_mtx); 132246057Sganbold for (s = TAILQ_FIRST(&rm->rm_list); 133246057Sganbold s && s->r_end < r->r_start; 134246057Sganbold s = TAILQ_NEXT(s, r_link)) 135246057Sganbold ; 136246057Sganbold 137246057Sganbold if (s == NULL) { 138297627Sjmcneill TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link); 139297627Sjmcneill } else { 140296284Sjmcneill TAILQ_INSERT_BEFORE(s, r, r_link); 141246057Sganbold } 142246057Sganbold 143246057Sganbold mtx_unlock(rm->rm_mtx); 144246057Sganbold return 0; 145246057Sganbold} 146296284Sjmcneill 147296284Sjmcneillint 148246057Sganboldrman_fini(struct rman *rm) 149246057Sganbold{ 150246057Sganbold struct resource *r; 151246057Sganbold 152276717Shselasky mtx_lock(rm->rm_mtx); 153246057Sganbold TAILQ_FOREACH(r, &rm->rm_list, r_link) { 154246057Sganbold if (r->r_flags & RF_ALLOCATED) { 155246057Sganbold mtx_unlock(rm->rm_mtx); 156246057Sganbold return EBUSY; 157246057Sganbold } 158246057Sganbold } 159246057Sganbold 160246057Sganbold /* 161246057Sganbold * There really should only be one of these if we are in this 162246057Sganbold * state and the code is working properly, but it can't hurt. 163246057Sganbold */ 164246057Sganbold while (!TAILQ_EMPTY(&rm->rm_list)) { 165246057Sganbold r = TAILQ_FIRST(&rm->rm_list); 166246057Sganbold TAILQ_REMOVE(&rm->rm_list, r, r_link); 167246057Sganbold free(r, M_RMAN); 168246057Sganbold } 169246057Sganbold mtx_unlock(rm->rm_mtx); 170246057Sganbold mtx_lock(&rman_mtx); 171246057Sganbold TAILQ_REMOVE(&rman_head, rm, rm_link); 172246057Sganbold mtx_unlock(&rman_mtx); 173246057Sganbold mtx_destroy(rm->rm_mtx); 174246057Sganbold free(rm->rm_mtx, M_RMAN); 175246057Sganbold 176246057Sganbold return 0; 177246057Sganbold} 178246057Sganbold 179246057Sganboldstruct resource * 180246057Sganboldrman_reserve_resource_bound(struct rman *rm, u_long start, u_long end, 181246057Sganbold u_long count, u_long bound, u_int flags, 182246057Sganbold struct device *dev) 183246057Sganbold{ 184246057Sganbold u_int want_activate; 185246057Sganbold struct resource *r, *s, *rv; 186246057Sganbold u_long rstart, rend, amask, bmask; 187246057Sganbold 188246057Sganbold rv = 0; 189246057Sganbold 190246057Sganbold DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length " 191246057Sganbold "%#lx, flags %u, device %s\n", rm->rm_descr, start, end, count, 192246057Sganbold flags, dev == NULL ? "<null>" : device_get_nameunit(dev))); 193246057Sganbold want_activate = (flags & RF_ACTIVE); 194246057Sganbold flags &= ~RF_ACTIVE; 195246057Sganbold 196246057Sganbold mtx_lock(rm->rm_mtx); 197246057Sganbold 198246057Sganbold for (r = TAILQ_FIRST(&rm->rm_list); 199246057Sganbold r && r->r_end < start; 200246057Sganbold r = TAILQ_NEXT(r, r_link)) 201246057Sganbold ; 202246057Sganbold 203246057Sganbold if (r == NULL) { 204246057Sganbold DPRINTF(("could not find a region\n")); 205246057Sganbold goto out; 206246057Sganbold } 207297627Sjmcneill 208308324Smmel amask = (1ul << RF_ALIGNMENT(flags)) - 1; 209297627Sjmcneill /* If bound is 0, bmask will also be 0 */ 210297627Sjmcneill bmask = ~(bound - 1); 211297627Sjmcneill /* 212297627Sjmcneill * First try to find an acceptable totally-unshared region. 213297627Sjmcneill */ 214297627Sjmcneill for (s = r; s; s = TAILQ_NEXT(s, r_link)) { 215297627Sjmcneill DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end)); 216246057Sganbold if (s->r_start > end) { 217308324Smmel DPRINTF(("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end)); 218297627Sjmcneill break; 219297627Sjmcneill } 220296284Sjmcneill if (s->r_flags & RF_ALLOCATED) { 221296284Sjmcneill DPRINTF(("region is allocated\n")); 222297627Sjmcneill continue; 223297627Sjmcneill } 224297627Sjmcneill rstart = ulmax(s->r_start, start); 225297627Sjmcneill /* 226297627Sjmcneill * Try to find a region by adjusting to boundary and alignment 227246057Sganbold * until both conditions are satisfied. This is not an optimal 228300728Sjmcneill * algorithm, but in most cases it isn't really bad, either. 229308324Smmel */ 230300728Sjmcneill do { 231300728Sjmcneill rstart = (rstart + amask) & ~amask; 232300728Sjmcneill if (((rstart ^ (rstart + count - 1)) & bmask) != 0) 233300728Sjmcneill rstart += bound - (rstart & ~bmask); 234332025Smmel } while ((rstart & amask) != 0 && rstart < end && 235300728Sjmcneill rstart < s->r_end); 236300728Sjmcneill rend = ulmin(s->r_end, ulmax(rstart + count, end)); 237300728Sjmcneill if (rstart > rend) { 238300728Sjmcneill DPRINTF(("adjusted start exceeds end\n")); 239300728Sjmcneill continue; 240246057Sganbold } 241246057Sganbold DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n", 242246057Sganbold rstart, rend, (rend - rstart + 1), count)); 243246057Sganbold 244246057Sganbold if ((rend - rstart + 1) >= count) { 245246057Sganbold DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n", 246246057Sganbold rend, rstart, (rend - rstart + 1))); 247246057Sganbold if ((s->r_end - s->r_start + 1) == count) { 248246057Sganbold DPRINTF(("candidate region is entire chunk\n")); 249296284Sjmcneill rv = s; 250296284Sjmcneill rv->r_flags |= RF_ALLOCATED | flags; 251296284Sjmcneill rv->r_dev = dev; 252296284Sjmcneill goto out; 253296284Sjmcneill } 254246057Sganbold 255246057Sganbold /* 256246057Sganbold * If s->r_start < rstart and 257246057Sganbold * s->r_end > rstart + count - 1, then 258246057Sganbold * we need to split the region into three pieces 259246057Sganbold * (the middle one will get returned to the user). 260246057Sganbold * Otherwise, we are allocating at either the 261246057Sganbold * beginning or the end of s, so we only need to 262246057Sganbold * split it in two. The first case requires 263246057Sganbold * two new allocations; the second requires but one. 264246057Sganbold */ 265246057Sganbold rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO); 266309755Smanu if (rv == 0) 267309755Smanu goto out; 268297627Sjmcneill rv->r_start = rstart; 269309755Smanu rv->r_end = rstart + count - 1; 270246057Sganbold rv->r_flags = flags | RF_ALLOCATED; 271246057Sganbold rv->r_dev = dev; 272246057Sganbold rv->r_rm = rm; 273246057Sganbold 274246057Sganbold if (s->r_start < rv->r_start && s->r_end > rv->r_end) { 275246057Sganbold DPRINTF(("splitting region in three parts: " 276246057Sganbold "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n", 277297627Sjmcneill s->r_start, rv->r_start - 1, 278297627Sjmcneill rv->r_start, rv->r_end, 279296284Sjmcneill rv->r_end + 1, s->r_end)); 280246057Sganbold /* 281246057Sganbold * We are allocating in the middle. 282246057Sganbold */ 283296284Sjmcneill r = malloc(sizeof *r, M_RMAN, M_NOWAIT|M_ZERO); 284296284Sjmcneill if (r == 0) { 285246057Sganbold free(rv, M_RMAN); 286246057Sganbold rv = 0; 287246057Sganbold goto out; 288246057Sganbold } 289246057Sganbold r->r_start = rv->r_end + 1; 290246057Sganbold r->r_end = s->r_end; 291246057Sganbold r->r_flags = s->r_flags; 292246057Sganbold r->r_rm = rm; 293246057Sganbold s->r_end = rv->r_start - 1; 294246057Sganbold TAILQ_INSERT_AFTER(&rm->rm_list, s, rv, 295246057Sganbold r_link); 296246057Sganbold TAILQ_INSERT_AFTER(&rm->rm_list, rv, r, 297246057Sganbold r_link); 298246057Sganbold } else if (s->r_start == rv->r_start) { 299246057Sganbold DPRINTF(("allocating from the beginning\n")); 300246057Sganbold /* 301246057Sganbold * We are allocating at the beginning. 302246057Sganbold */ 303246057Sganbold s->r_start = rv->r_end + 1; 304246057Sganbold TAILQ_INSERT_BEFORE(s, rv, r_link); 305246057Sganbold } else { 306246057Sganbold DPRINTF(("allocating at the end\n")); 307246057Sganbold /* 308246057Sganbold * We are allocating at the end. 309246057Sganbold */ 310246057Sganbold s->r_end = rv->r_start - 1; 311246057Sganbold TAILQ_INSERT_AFTER(&rm->rm_list, s, rv, 312246057Sganbold r_link); 313246057Sganbold } 314246057Sganbold goto out; 315296284Sjmcneill } 316296284Sjmcneill } 317296284Sjmcneill 318296284Sjmcneill /* 319296284Sjmcneill * Now find an acceptable shared region, if the client's requirements 320246057Sganbold * allow sharing. By our implementation restriction, a candidate 321246057Sganbold * region must match exactly by both size and sharing type in order 322246057Sganbold * to be considered compatible with the client's request. (The 323246057Sganbold * former restriction could probably be lifted without too much 324246057Sganbold * additional work, but this does not seem warranted.) 325246057Sganbold */ 326246057Sganbold DPRINTF(("no unshared regions found\n")); 327246057Sganbold if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0) 328246057Sganbold goto out; 329246057Sganbold 330309755Smanu for (s = r; s; s = TAILQ_NEXT(s, r_link)) { 331309755Smanu if (s->r_start > end) 332309755Smanu break; 333309755Smanu if ((s->r_flags & flags) != flags) 334246057Sganbold continue; 335297627Sjmcneill rstart = ulmax(s->r_start, start); 336297627Sjmcneill rend = ulmin(s->r_end, ulmax(start + count, end)); 337297627Sjmcneill if (s->r_start >= start && s->r_end <= end 338297627Sjmcneill && (s->r_end - s->r_start + 1) == count && 339297627Sjmcneill (s->r_start & amask) == 0 && 340297627Sjmcneill ((s->r_start ^ s->r_end) & bmask) == 0) { 341246057Sganbold rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO); 342246057Sganbold if (rv == 0) 343246057Sganbold goto out; 344246057Sganbold rv->r_start = s->r_start; 345246057Sganbold rv->r_end = s->r_end; 346246057Sganbold rv->r_flags = s->r_flags & 347246057Sganbold (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE); 348246057Sganbold rv->r_dev = dev; 349246057Sganbold rv->r_rm = rm; 350246057Sganbold if (s->r_sharehead == 0) { 351246057Sganbold s->r_sharehead = malloc(sizeof *s->r_sharehead, 352246057Sganbold M_RMAN, M_NOWAIT | M_ZERO); 353246057Sganbold if (s->r_sharehead == 0) { 354246057Sganbold free(rv, M_RMAN); 355246057Sganbold rv = 0; 356246057Sganbold goto out; 357246057Sganbold } 358246057Sganbold LIST_INIT(s->r_sharehead); 359304562Smanu LIST_INSERT_HEAD(s->r_sharehead, s, 360246057Sganbold r_sharelink); 361246057Sganbold s->r_flags |= RF_FIRSTSHARE; 362246057Sganbold } 363246057Sganbold rv->r_sharehead = s->r_sharehead; 364346524Sian LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink); 365346524Sian 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 struct rman *rm; 479 480 rm = r->r_rm; 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 489int 490rman_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 501static int 502int_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 582out: 583 free(r, M_RMAN); 584 return 0; 585} 586 587int 588rman_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 599uint32_t 600rman_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 617u_long 618rman_get_start(struct resource *r) 619{ 620 return (r->r_start); 621} 622 623u_long 624rman_get_end(struct resource *r) 625{ 626 return (r->r_end); 627} 628 629u_long 630rman_get_size(struct resource *r) 631{ 632 return (r->r_end - r->r_start + 1); 633} 634 635u_int 636rman_get_flags(struct resource *r) 637{ 638 return (r->r_flags); 639} 640 641void 642rman_set_virtual(struct resource *r, void *v) 643{ 644 r->r_virtual = v; 645} 646 647void * 648rman_get_virtual(struct resource *r) 649{ 650 return (r->r_virtual); 651} 652 653void 654rman_set_bustag(struct resource *r, bus_space_tag_t t) 655{ 656 r->r_bustag = t; 657} 658 659bus_space_tag_t 660rman_get_bustag(struct resource *r) 661{ 662 return (r->r_bustag); 663} 664 665void 666rman_set_bushandle(struct resource *r, bus_space_handle_t h) 667{ 668 r->r_bushandle = h; 669} 670 671bus_space_handle_t 672rman_get_bushandle(struct resource *r) 673{ 674 return (r->r_bushandle); 675} 676 677void 678rman_set_rid(struct resource *r, int rid) 679{ 680 r->r_rid = rid; 681} 682 683int 684rman_get_rid(struct resource *r) 685{ 686 return (r->r_rid); 687} 688 689struct device * 690rman_get_device(struct resource *r) 691{ 692 return (r->r_dev); 693} 694