The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_rman.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: src/sys/kern/subr_rman.c,v 1.10.2.1 2001/06/05 08:06:08 imp 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/bus.h>            /* XXX debugging */
   66 #include <sys/rman.h>
   67 #include <sys/sysctl.h>
   68 
   69 int     rman_debug = 0;
   70 TUNABLE_INT("debug.rman_debug", &rman_debug);
   71 SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
   72     &rman_debug, 0, "rman debug");
   73 
   74 #define DPRINTF(params) if (rman_debug) kprintf params
   75 
   76 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
   77 
   78 struct  rman_head rman_head;
   79 static  struct lwkt_token rman_tok; /* 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, int cpuid)
   87 {
   88         static int once;
   89 
   90         if (once == 0) {
   91                 once = 1;
   92                 TAILQ_INIT(&rman_head);
   93                 lwkt_token_init(&rman_tok, "rman");
   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_slock = kmalloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT);
  103         if (rm->rm_slock == NULL)
  104                 return ENOMEM;
  105         lwkt_token_init(rm->rm_slock, "rmanslock");
  106 
  107         rm->rm_cpuid = cpuid;
  108 
  109         lwkt_gettoken(&rman_tok);
  110         TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
  111         lwkt_reltoken(&rman_tok);
  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  */
  119 int
  120 rman_manage_region(struct rman *rm, u_long start, u_long end)
  121 {
  122         struct resource *r, *s;
  123 
  124         DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
  125             rm->rm_descr, start, end));
  126         r = kmalloc(sizeof *r, M_RMAN, M_NOWAIT | M_ZERO);
  127         if (r == NULL)
  128                 return ENOMEM;
  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         lwkt_gettoken(rm->rm_slock);
  137         for (s = TAILQ_FIRST(&rm->rm_list);
  138              s && s->r_end < r->r_start;
  139              s = TAILQ_NEXT(s, r_link))
  140                 ;
  141 
  142         if (s == NULL)
  143                 TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
  144         else
  145                 TAILQ_INSERT_BEFORE(s, r, r_link);
  146 
  147         lwkt_reltoken(rm->rm_slock);
  148         return 0;
  149 }
  150 
  151 int
  152 rman_fini(struct rman *rm)
  153 {
  154         struct resource *r;
  155 
  156         lwkt_gettoken(rm->rm_slock);
  157         TAILQ_FOREACH(r, &rm->rm_list, r_link) {
  158                 if (r->r_flags & RF_ALLOCATED) {
  159                         lwkt_reltoken(rm->rm_slock);
  160                         return EBUSY;
  161                 }
  162         }
  163 
  164         /*
  165          * There really should only be one of these if we are in this
  166          * state and the code is working properly, but it can't hurt.
  167          */
  168         while (!TAILQ_EMPTY(&rm->rm_list)) {
  169                 r = TAILQ_FIRST(&rm->rm_list);
  170                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
  171                 kfree(r, M_RMAN);
  172         }
  173         lwkt_reltoken(rm->rm_slock);
  174 
  175         /* XXX what's the point of this if we are going to free the struct? */
  176         lwkt_gettoken(&rman_tok);
  177         TAILQ_REMOVE(&rman_head, rm, rm_link);
  178         lwkt_reltoken(&rman_tok);
  179         kfree(rm->rm_slock, M_RMAN);
  180 
  181         return 0;
  182 }
  183 
  184 struct resource *
  185 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
  186                       u_int flags, struct device *dev)
  187 {
  188         u_int   want_activate;
  189         struct  resource *r, *s, *rv;
  190         u_long  rstart, rend;
  191 
  192         rv = NULL;
  193 
  194         DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
  195                "%#lx, flags %u, device %s\n", rm->rm_descr, start, end,
  196                count, flags,
  197                dev == NULL ? "<null>" : device_get_nameunit(dev)));
  198         want_activate = (flags & RF_ACTIVE);
  199         flags &= ~RF_ACTIVE;
  200 
  201         lwkt_gettoken(rm->rm_slock);
  202 
  203         for (r = TAILQ_FIRST(&rm->rm_list);
  204              r && r->r_end < start;
  205              r = TAILQ_NEXT(r, r_link))
  206                 ;
  207 
  208         if (r == NULL) {
  209                 DPRINTF(("could not find a region\n"));
  210                 goto out;
  211         }
  212 
  213         /*
  214          * First try to find an acceptable totally-unshared region.
  215          */
  216         for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
  217                 DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
  218                 if (s->r_start > end) {
  219                         DPRINTF(("s->r_start (%#lx) > end (%#lx)\n",
  220                             s->r_start, end));
  221                         break;
  222                 }
  223                 if (s->r_flags & RF_ALLOCATED) {
  224                         DPRINTF(("region is allocated\n"));
  225                         continue;
  226                 }
  227                 rstart = max(s->r_start, start);
  228                 rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
  229                     ~((1ul << RF_ALIGNMENT(flags)) - 1);
  230                 rend = min(s->r_end, max(start + count, end));
  231                 DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
  232                        rstart, rend, (rend - rstart + 1), count));
  233 
  234                 if ((rend - rstart + 1) >= count) {
  235                         DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
  236                                rstart, rend, (rend - rstart + 1)));
  237                         if ((s->r_end - s->r_start + 1) == count) {
  238                                 DPRINTF(("candidate region is entire chunk\n"));
  239                                 rv = s;
  240                                 rv->r_flags |= RF_ALLOCATED | flags;
  241                                 rv->r_dev = dev;
  242                                 goto out;
  243                         }
  244 
  245                         /*
  246                          * If s->r_start < rstart and
  247                          *    s->r_end > rstart + count - 1, then
  248                          * we need to split the region into three pieces
  249                          * (the middle one will get returned to the user).
  250                          * Otherwise, we are allocating at either the
  251                          * beginning or the end of s, so we only need to
  252                          * split it in two.  The first case requires
  253                          * two new allocations; the second requires but one.
  254                          */
  255                         rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
  256                         if (rv == NULL)
  257                                 goto out;
  258                         rv->r_start = rstart;
  259                         rv->r_end = rstart + count - 1;
  260                         rv->r_flags = flags | RF_ALLOCATED;
  261                         rv->r_dev = dev;
  262                         rv->r_sharehead = 0;
  263                         rv->r_rm = rm;
  264                         
  265                         if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
  266                                 DPRINTF(("splitting region in three parts: "
  267                                        "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
  268                                        s->r_start, rv->r_start - 1,
  269                                        rv->r_start, rv->r_end,
  270                                        rv->r_end + 1, s->r_end));
  271                                 /*
  272                                  * We are allocating in the middle.
  273                                  */
  274                                 r = kmalloc(sizeof *r, M_RMAN,
  275                                     M_NOWAIT | M_ZERO);
  276                                 if (r == NULL) {
  277                                         kfree(rv, M_RMAN);
  278                                         rv = NULL;
  279                                         goto out;
  280                                 }
  281                                 r->r_start = rv->r_end + 1;
  282                                 r->r_end = s->r_end;
  283                                 r->r_flags = s->r_flags;
  284                                 r->r_dev = 0;
  285                                 r->r_sharehead = 0;
  286                                 r->r_rm = rm;
  287                                 s->r_end = rv->r_start - 1;
  288                                 TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
  289                                                      r_link);
  290                                 TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
  291                                                      r_link);
  292                         } else if (s->r_start == rv->r_start) {
  293                                 DPRINTF(("allocating from the beginning\n"));
  294                                 /*
  295                                  * We are allocating at the beginning.
  296                                  */
  297                                 s->r_start = rv->r_end + 1;
  298                                 TAILQ_INSERT_BEFORE(s, rv, 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                                 TAILQ_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; s; s = TAILQ_NEXT(s, r_link)) {
  325                 if (s->r_start > end)
  326                         break;
  327                 if ((s->r_flags & flags) != flags)
  328                         continue;
  329                 rstart = max(s->r_start, start);
  330                 rend = min(s->r_end, max(start + count, end));
  331                 if (s->r_start >= start && s->r_end <= end
  332                     && (s->r_end - s->r_start + 1) == count) {
  333                         rv = kmalloc(sizeof *rv, M_RMAN, M_NOWAIT | M_ZERO);
  334                         if (rv == NULL)
  335                                 goto out;
  336                         rv->r_start = s->r_start;
  337                         rv->r_end = s->r_end;
  338                         rv->r_flags = s->r_flags & 
  339                                 (RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
  340                         rv->r_dev = dev;
  341                         rv->r_rm = rm;
  342                         if (s->r_sharehead == 0) {
  343                                 s->r_sharehead = kmalloc(sizeof *s->r_sharehead,
  344                                                         M_RMAN,
  345                                                         M_NOWAIT | M_ZERO);
  346                                 if (s->r_sharehead == 0) {
  347                                         kfree(rv, M_RMAN);
  348                                         rv = NULL;
  349                                         goto out;
  350                                 }
  351                                 LIST_INIT(s->r_sharehead);
  352                                 LIST_INSERT_HEAD(s->r_sharehead, s, 
  353                                                  r_sharelink);
  354                                 s->r_flags |= RF_FIRSTSHARE;
  355                         }
  356                         rv->r_sharehead = s->r_sharehead;
  357                         LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
  358                         goto out;
  359                 }
  360         }
  361 
  362         /*
  363          * We couldn't find anything.
  364          */
  365 out:
  366         /*
  367          * If the user specified RF_ACTIVE in the initial flags,
  368          * which is reflected in `want_activate', we attempt to atomically
  369          * activate the resource.  If this fails, we release the resource
  370          * and indicate overall failure.  (This behavior probably doesn't
  371          * make sense for RF_TIMESHARE-type resources.)
  372          */
  373         if (rv && want_activate) {
  374                 struct resource *whohas;
  375                 if (int_rman_activate_resource(rm, rv, &whohas)) {
  376                         int_rman_release_resource(rm, rv);
  377                         rv = NULL;
  378                 }
  379         }
  380         lwkt_reltoken(rm->rm_slock);
  381         return (rv);
  382 }
  383 
  384 static int
  385 int_rman_activate_resource(struct rman *rm, struct resource *r,
  386                            struct resource **whohas)
  387 {
  388         struct resource *s;
  389         int ok;
  390 
  391         /*
  392          * If we are not timesharing, then there is nothing much to do.
  393          * If we already have the resource, then there is nothing at all to do.
  394          * If we are not on a sharing list with anybody else, then there is
  395          * little to do.
  396          */
  397         if ((r->r_flags & RF_TIMESHARE) == 0
  398             || (r->r_flags & RF_ACTIVE) != 0
  399             || r->r_sharehead == 0) {
  400                 r->r_flags |= RF_ACTIVE;
  401                 return 0;
  402         }
  403 
  404         ok = 1;
  405         for (s = LIST_FIRST(r->r_sharehead); s && ok;
  406              s = LIST_NEXT(s, r_sharelink)) {
  407                 if ((s->r_flags & RF_ACTIVE) != 0) {
  408                         ok = 0;
  409                         *whohas = s;
  410                 }
  411         }
  412         if (ok) {
  413                 r->r_flags |= RF_ACTIVE;
  414                 return 0;
  415         }
  416         return EBUSY;
  417 }
  418 
  419 int
  420 rman_activate_resource(struct resource *r)
  421 {
  422         int rv;
  423         struct resource *whohas;
  424         struct rman *rm;
  425 
  426         rm = r->r_rm;
  427         lwkt_gettoken(rm->rm_slock);
  428         rv = int_rman_activate_resource(rm, r, &whohas);
  429         lwkt_reltoken(rm->rm_slock);
  430         return rv;
  431 }
  432 
  433 #if 0
  434 
  435 /* XXX */
  436 int
  437 rman_await_resource(struct resource *r, int slpflags, int timo)
  438 {
  439         int     rv;
  440         struct  resource *whohas;
  441         struct  rman *rm;
  442 
  443         rm = r->r_rm;
  444         for (;;) {
  445                 lwkt_gettoken(rm->rm_slock);
  446                 rv = int_rman_activate_resource(rm, r, &whohas);
  447                 if (rv != EBUSY)
  448                         return (rv);    /* returns with ilock held */
  449 
  450                 if (r->r_sharehead == 0)
  451                         panic("rman_await_resource");
  452                 /*
  453                  * A critical section will hopefully will prevent a race 
  454                  * between lwkt_reltoken and tsleep where a process
  455                  * could conceivably get in and release the resource
  456                  * before we have a chance to sleep on it. YYY
  457                  */
  458                 crit_enter();
  459                 whohas->r_flags |= RF_WANTED;
  460                 rv = tsleep(r->r_sharehead, slpflags, "rmwait", timo);
  461                 if (rv) {
  462                         lwkt_reltoken(rm->rm_slock);
  463                         crit_exit();
  464                         return rv;
  465                 }
  466                 crit_exit();
  467         }
  468 }
  469 
  470 #endif
  471 
  472 static int
  473 int_rman_deactivate_resource(struct resource *r)
  474 {
  475         r->r_flags &= ~RF_ACTIVE;
  476         if (r->r_flags & RF_WANTED) {
  477                 r->r_flags &= ~RF_WANTED;
  478                 wakeup(r->r_sharehead);
  479         }
  480         return 0;
  481 }
  482 
  483 int
  484 rman_deactivate_resource(struct resource *r)
  485 {
  486         struct rman *rm;
  487 
  488         rm = r->r_rm;
  489         lwkt_gettoken(rm->rm_slock);
  490         int_rman_deactivate_resource(r);
  491         lwkt_reltoken(rm->rm_slock);
  492         return 0;
  493 }
  494 
  495 static int
  496 int_rman_release_resource(struct rman *rm, struct resource *r)
  497 {
  498         struct  resource *s, *t;
  499 
  500         if (r->r_flags & RF_ACTIVE)
  501                 int_rman_deactivate_resource(r);
  502 
  503         /*
  504          * Check for a sharing list first.  If there is one, then we don't
  505          * have to think as hard.
  506          */
  507         if (r->r_sharehead) {
  508                 /*
  509                  * If a sharing list exists, then we know there are at
  510                  * least two sharers.
  511                  *
  512                  * If we are in the main circleq, appoint someone else.
  513                  */
  514                 LIST_REMOVE(r, r_sharelink);
  515                 s = LIST_FIRST(r->r_sharehead);
  516                 if (r->r_flags & RF_FIRSTSHARE) {
  517                         s->r_flags |= RF_FIRSTSHARE;
  518                         TAILQ_INSERT_BEFORE(r, s, r_link);
  519                         TAILQ_REMOVE(&rm->rm_list, r, r_link);
  520                 }
  521 
  522                 /*
  523                  * Make sure that the sharing list goes away completely
  524                  * if the resource is no longer being shared at all.
  525                  */
  526                 if (LIST_NEXT(s, r_sharelink) == 0) {
  527                         kfree(s->r_sharehead, M_RMAN);
  528                         s->r_sharehead = 0;
  529                         s->r_flags &= ~RF_FIRSTSHARE;
  530                 }
  531                 goto out;
  532         }
  533 
  534         /*
  535          * Look at the adjacent resources in the list and see if our
  536          * segment can be merged with any of them.
  537          */
  538         s = TAILQ_PREV(r, resource_head, r_link);
  539         t = TAILQ_NEXT(r, r_link);
  540 
  541         if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0
  542             && t != NULL && (t->r_flags & RF_ALLOCATED) == 0) {
  543                 /*
  544                  * Merge all three segments.
  545                  */
  546                 s->r_end = t->r_end;
  547                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
  548                 TAILQ_REMOVE(&rm->rm_list, t, r_link);
  549                 kfree(t, M_RMAN);
  550         } else if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0) {
  551                 /*
  552                  * Merge previous segment with ours.
  553                  */
  554                 s->r_end = r->r_end;
  555                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
  556         } else if (t != NULL && (t->r_flags & RF_ALLOCATED) == 0) {
  557                 /*
  558                  * Merge next segment with ours.
  559                  */
  560                 t->r_start = r->r_start;
  561                 TAILQ_REMOVE(&rm->rm_list, r, r_link);
  562         } else {
  563                 /*
  564                  * At this point, we know there is nothing we
  565                  * can potentially merge with, because on each
  566                  * side, there is either nothing there or what is
  567                  * there is still allocated.  In that case, we don't
  568                  * want to remove r from the list; we simply want to
  569                  * change it to an unallocated region and return
  570                  * without freeing anything.
  571                  */
  572                 r->r_flags &= ~RF_ALLOCATED;
  573                 return 0;
  574         }
  575 
  576 out:
  577         kfree(r, M_RMAN);
  578         return 0;
  579 }
  580 
  581 int
  582 rman_release_resource(struct resource *r)
  583 {
  584         struct  rman *rm = r->r_rm;
  585         int     rv;
  586 
  587         lwkt_gettoken(rm->rm_slock);
  588         rv = int_rman_release_resource(rm, r);
  589         lwkt_reltoken(rm->rm_slock);
  590         return (rv);
  591 }
  592 
  593 uint32_t
  594 rman_make_alignment_flags(uint32_t size)
  595 {
  596         int     i;
  597 
  598         /*
  599          * Find the hightest bit set, and add one if more than one bit
  600          * set.  We're effectively computing the ceil(log2(size)) here.
  601          */
  602         for (i = 32; i > 0; i--)
  603                 if ((1 << i) & size)
  604                         break;
  605         if (~(1 << i) & size)
  606                 i++;
  607 
  608         return(RF_ALIGNMENT_LOG2(i));
  609 }
  610 
  611 /*
  612  * Sysctl interface for scanning the resource lists.
  613  *
  614  * We take two input parameters; the index into the list of resource
  615  * managers, and the resource offset into the list.
  616  */
  617 static int
  618 sysctl_rman(SYSCTL_HANDLER_ARGS)
  619 {
  620         int                     *name = (int *)arg1;
  621         u_int                   namelen = arg2;
  622         int                     rman_idx, res_idx;
  623         struct rman             *rm;
  624         struct resource         *res;
  625         struct u_rman           urm;
  626         struct u_resource       ures;
  627         int                     error;
  628 
  629         if (namelen != 3)
  630                 return (EINVAL);
  631 
  632         if (bus_data_generation_check(name[0]))
  633                 return (EINVAL);
  634         rman_idx = name[1];
  635         res_idx = name[2];
  636 
  637         /*
  638          * Find the indexed resource manager
  639          */
  640         TAILQ_FOREACH(rm, &rman_head, rm_link) {
  641                 if (rman_idx-- == 0)
  642                         break;
  643         }
  644         if (rm == NULL)
  645                 return (ENOENT);
  646 
  647         /*
  648          * If the resource index is -1, we want details on the
  649          * resource manager.
  650          */
  651         if (res_idx == -1) {
  652                 urm.rm_handle = (uintptr_t)rm;
  653                 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
  654                 urm.rm_start = rm->rm_start;
  655                 urm.rm_size = rm->rm_end - rm->rm_start + 1;
  656                 urm.rm_type = rm->rm_type;
  657 
  658                 error = SYSCTL_OUT(req, &urm, sizeof(urm));
  659                 return (error);
  660         }
  661 
  662         /*
  663          * Find the indexed resource and return it.
  664          */
  665         TAILQ_FOREACH(res, &rm->rm_list, r_link) {
  666                 if (res_idx-- == 0) {
  667                         ures.r_handle = (uintptr_t)res;
  668                         ures.r_parent = (uintptr_t)res->r_rm;
  669                         ures.r_device = (uintptr_t)res->r_dev;
  670                         if (res->r_dev != NULL) {
  671                                 if (device_get_name(res->r_dev) != NULL) {
  672                                         ksnprintf(ures.r_devname, RM_TEXTLEN,
  673                                             "%s%d",
  674                                             device_get_name(res->r_dev),
  675                                             device_get_unit(res->r_dev));
  676                                 } else {
  677                                         strlcpy(ures.r_devname, "nomatch",
  678                                             RM_TEXTLEN);
  679                                 }
  680                         } else {
  681                                 ures.r_devname[0] = '\0';
  682                         }
  683                         ures.r_start = res->r_start;
  684                         ures.r_size = res->r_end - res->r_start + 1;
  685                         ures.r_flags = res->r_flags;
  686 
  687                         error = SYSCTL_OUT(req, &ures, sizeof(ures));
  688                         return (error);
  689                 }
  690         }
  691         return (ENOENT);
  692 }
  693 
  694 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
  695     "kernel resource manager");

Cache object: 9a1f17eb526872287a9c094909166447


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.