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/dev/fdt/simplebus.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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2013 Nathan Whitehorn
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/module.h>
   34 #include <sys/bus.h>
   35 #include <sys/conf.h>
   36 #include <sys/kernel.h>
   37 #include <sys/rman.h>
   38 
   39 #include <dev/ofw/openfirm.h>
   40 #include <dev/ofw/ofw_bus.h>
   41 #include <dev/ofw/ofw_bus_subr.h>
   42 
   43 #include <dev/fdt/simplebus.h>
   44 
   45 /*
   46  * Bus interface.
   47  */
   48 static int              simplebus_probe(device_t dev);
   49 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
   50     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
   51 static void             simplebus_probe_nomatch(device_t bus, device_t child);
   52 static int              simplebus_print_child(device_t bus, device_t child);
   53 static device_t         simplebus_add_child(device_t dev, u_int order,
   54     const char *name, int unit);
   55 static struct resource_list *simplebus_get_resource_list(device_t bus,
   56     device_t child);
   57 
   58 static ssize_t          simplebus_get_property(device_t bus, device_t child,
   59     const char *propname, void *propvalue, size_t size,
   60     device_property_type_t type);
   61 /*
   62  * ofw_bus interface
   63  */
   64 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
   65     device_t child);
   66 
   67 /*
   68  * Driver methods.
   69  */
   70 static device_method_t  simplebus_methods[] = {
   71         /* Device interface */
   72         DEVMETHOD(device_probe,         simplebus_probe),
   73         DEVMETHOD(device_attach,        simplebus_attach),
   74         DEVMETHOD(device_detach,        simplebus_detach),
   75         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
   76         DEVMETHOD(device_suspend,       bus_generic_suspend),
   77         DEVMETHOD(device_resume,        bus_generic_resume),
   78 
   79         /* Bus interface */
   80         DEVMETHOD(bus_add_child,        simplebus_add_child),
   81         DEVMETHOD(bus_print_child,      simplebus_print_child),
   82         DEVMETHOD(bus_probe_nomatch,    simplebus_probe_nomatch),
   83         DEVMETHOD(bus_read_ivar,        bus_generic_read_ivar),
   84         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
   85         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
   86         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
   87         DEVMETHOD(bus_alloc_resource,   simplebus_alloc_resource),
   88         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
   89         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
   90         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
   91         DEVMETHOD(bus_adjust_resource,  bus_generic_adjust_resource),
   92         DEVMETHOD(bus_set_resource,     bus_generic_rl_set_resource),
   93         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
   94         DEVMETHOD(bus_child_pnpinfo,    ofw_bus_gen_child_pnpinfo),
   95         DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
   96         DEVMETHOD(bus_get_property,     simplebus_get_property),
   97         DEVMETHOD(bus_get_device_path,  ofw_bus_gen_get_device_path),
   98 
   99         /* ofw_bus interface */
  100         DEVMETHOD(ofw_bus_get_devinfo,  simplebus_get_devinfo),
  101         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
  102         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
  103         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
  104         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
  105         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
  106 
  107         DEVMETHOD_END
  108 };
  109 
  110 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
  111     sizeof(struct simplebus_softc));
  112 
  113 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, 0, 0, BUS_PASS_BUS);
  114 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, 0, 0,
  115     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
  116 
  117 static int
  118 simplebus_probe(device_t dev)
  119 {
  120  
  121         if (!ofw_bus_status_okay(dev))
  122                 return (ENXIO);
  123         /*
  124          * XXX We should attach only to pure' compatible = "simple-bus"',
  125          * without any other compatible string.
  126          * For now, filter only know cases:
  127          * "syscon", "simple-bus"; is handled by fdt/syscon driver
  128          * "simple-mfd", "simple-bus"; is handled by fdt/simple-mfd driver
  129          */
  130         if (ofw_bus_is_compatible(dev, "syscon") ||
  131             ofw_bus_is_compatible(dev, "simple-mfd"))
  132                 return (ENXIO);
  133 
  134         /*
  135          * FDT data puts a "simple-bus" compatible string on many things that
  136          * have children but aren't really buses in our world.  Without a
  137          * ranges property we will fail to attach, so just fail to probe too.
  138          */
  139         if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
  140             ofw_bus_has_prop(dev, "ranges")) &&
  141             (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
  142              "soc") != 0))
  143                 return (ENXIO);
  144 
  145         device_set_desc(dev, "Flattened device tree simple bus");
  146 
  147         return (BUS_PROBE_GENERIC);
  148 }
  149 
  150 int
  151 simplebus_attach_impl(device_t dev)
  152 {
  153         struct          simplebus_softc *sc;
  154         phandle_t       node;
  155 
  156         sc = device_get_softc(dev);
  157         simplebus_init(dev, 0);
  158         if ((sc->flags & SB_FLAG_NO_RANGES) == 0 &&
  159             simplebus_fill_ranges(sc->node, sc) < 0) {
  160                 device_printf(dev, "could not get ranges\n");
  161                 return (ENXIO);
  162         }
  163 
  164         /*
  165          * In principle, simplebus could have an interrupt map, but ignore that
  166          * for now
  167          */
  168 
  169         for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
  170                 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
  171 
  172         return (0);
  173 }
  174 
  175 int
  176 simplebus_attach(device_t dev)
  177 {
  178         int     rv;
  179 
  180         rv = simplebus_attach_impl(dev);
  181         if (rv != 0)
  182                 return (rv);
  183 
  184         return (bus_generic_attach(dev));
  185 }
  186 
  187 int
  188 simplebus_detach(device_t dev)
  189 {
  190         struct          simplebus_softc *sc;
  191 
  192         sc = device_get_softc(dev);
  193         if (sc->ranges != NULL)
  194                 free(sc->ranges, M_DEVBUF);
  195 
  196         return (bus_generic_detach(dev));
  197 }
  198 
  199 void
  200 simplebus_init(device_t dev, phandle_t node)
  201 {
  202         struct simplebus_softc *sc;
  203 
  204         sc = device_get_softc(dev);
  205         if (node == 0)
  206                 node = ofw_bus_get_node(dev);
  207         sc->dev = dev;
  208         sc->node = node;
  209 
  210         /*
  211          * Some important numbers
  212          */
  213         sc->acells = 2;
  214         OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
  215         sc->scells = 1;
  216         OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
  217 }
  218 
  219 int
  220 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
  221 {
  222         int host_address_cells;
  223         cell_t *base_ranges;
  224         ssize_t nbase_ranges;
  225         int err;
  226         int i, j, k;
  227 
  228         err = OF_searchencprop(OF_parent(node), "#address-cells",
  229             &host_address_cells, sizeof(host_address_cells));
  230         if (err <= 0)
  231                 return (-1);
  232 
  233         nbase_ranges = OF_getproplen(node, "ranges");
  234         if (nbase_ranges < 0)
  235                 return (-1);
  236         sc->nranges = nbase_ranges / sizeof(cell_t) /
  237             (sc->acells + host_address_cells + sc->scells);
  238         if (sc->nranges == 0)
  239                 return (0);
  240 
  241         sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
  242             M_DEVBUF, M_WAITOK);
  243         base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
  244         OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
  245 
  246         for (i = 0, j = 0; i < sc->nranges; i++) {
  247                 sc->ranges[i].bus = 0;
  248                 for (k = 0; k < sc->acells; k++) {
  249                         sc->ranges[i].bus <<= 32;
  250                         sc->ranges[i].bus |= base_ranges[j++];
  251                 }
  252                 sc->ranges[i].host = 0;
  253                 for (k = 0; k < host_address_cells; k++) {
  254                         sc->ranges[i].host <<= 32;
  255                         sc->ranges[i].host |= base_ranges[j++];
  256                 }
  257                 sc->ranges[i].size = 0;
  258                 for (k = 0; k < sc->scells; k++) {
  259                         sc->ranges[i].size <<= 32;
  260                         sc->ranges[i].size |= base_ranges[j++];
  261                 }
  262         }
  263 
  264         free(base_ranges, M_DEVBUF);
  265         return (sc->nranges);
  266 }
  267 
  268 struct simplebus_devinfo *
  269 simplebus_setup_dinfo(device_t dev, phandle_t node,
  270     struct simplebus_devinfo *di)
  271 {
  272         struct simplebus_softc *sc;
  273         struct simplebus_devinfo *ndi;
  274 
  275         sc = device_get_softc(dev);
  276         if (di == NULL)
  277                 ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
  278         else
  279                 ndi = di;
  280         if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
  281                 if (di == NULL)
  282                         free(ndi, M_DEVBUF);
  283                 return (NULL);
  284         }
  285 
  286         resource_list_init(&ndi->rl);
  287         ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
  288         ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
  289 
  290         return (ndi);
  291 }
  292 
  293 device_t
  294 simplebus_add_device(device_t dev, phandle_t node, u_int order,
  295     const char *name, int unit, struct simplebus_devinfo *di)
  296 {
  297         struct simplebus_devinfo *ndi;
  298         device_t cdev;
  299 
  300         if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
  301                 return (NULL);
  302         cdev = device_add_child_ordered(dev, order, name, unit);
  303         if (cdev == NULL) {
  304                 device_printf(dev, "<%s>: device_add_child failed\n",
  305                     ndi->obdinfo.obd_name);
  306                 resource_list_free(&ndi->rl);
  307                 ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
  308                 if (di == NULL)
  309                         free(ndi, M_DEVBUF);
  310                 return (NULL);
  311         }
  312         device_set_ivars(cdev, ndi);
  313 
  314         return(cdev);
  315 }
  316 
  317 static device_t
  318 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
  319 {
  320         device_t cdev;
  321         struct simplebus_devinfo *ndi;
  322 
  323         cdev = device_add_child_ordered(dev, order, name, unit);
  324         if (cdev == NULL)
  325                 return (NULL);
  326 
  327         ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
  328         ndi->obdinfo.obd_node = -1;
  329         resource_list_init(&ndi->rl);
  330         device_set_ivars(cdev, ndi);
  331 
  332         return (cdev);
  333 }
  334 
  335 static const struct ofw_bus_devinfo *
  336 simplebus_get_devinfo(device_t bus __unused, device_t child)
  337 {
  338         struct simplebus_devinfo *ndi;
  339         
  340         ndi = device_get_ivars(child);
  341         if (ndi == NULL)
  342                 return (NULL);
  343         return (&ndi->obdinfo);
  344 }
  345 
  346 static struct resource_list *
  347 simplebus_get_resource_list(device_t bus __unused, device_t child)
  348 {
  349         struct simplebus_devinfo *ndi;
  350 
  351         ndi = device_get_ivars(child);
  352         if (ndi == NULL)
  353                 return (NULL);
  354         return (&ndi->rl);
  355 }
  356 
  357 static ssize_t
  358 simplebus_get_property(device_t bus, device_t child, const char *propname,
  359     void *propvalue, size_t size, device_property_type_t type)
  360 {
  361         phandle_t node, xref;
  362         ssize_t ret, i;
  363         uint32_t *buffer;
  364         uint64_t val;
  365 
  366         switch (type) {
  367         case DEVICE_PROP_ANY:
  368         case DEVICE_PROP_BUFFER:
  369         case DEVICE_PROP_UINT32:
  370         case DEVICE_PROP_UINT64:
  371         case DEVICE_PROP_HANDLE:
  372                 break;
  373         default:
  374                 return (-1);
  375         }
  376 
  377         node = ofw_bus_get_node(child);
  378         if (propvalue == NULL || size == 0)
  379                 return (OF_getproplen(node, propname));
  380 
  381         /*
  382          * Integer values are stored in BE format.
  383          * If caller declared that the underlying property type is uint32_t
  384          * we need to do the conversion to match host endianness.
  385          */
  386         if (type == DEVICE_PROP_UINT32)
  387                 return (OF_getencprop(node, propname, propvalue, size));
  388 
  389         /*
  390          * uint64_t also requires endianness handling.
  391          * In FDT every 8 byte value is stored using two uint32_t variables
  392          * in BE format. Now, since the upper bits are stored as the first
  393          * of the pair, both halves require swapping.
  394          */
  395          if (type == DEVICE_PROP_UINT64) {
  396                 ret = OF_getencprop(node, propname, propvalue, size);
  397                 if (ret <= 0) {
  398                         return (ret);
  399                 }
  400 
  401                 buffer = (uint32_t *)propvalue;
  402 
  403                 for (i = 0; i < size / 4; i += 2) {
  404                         val = (uint64_t)buffer[i] << 32 | buffer[i + 1];
  405                         ((uint64_t *)buffer)[i / 2] = val;
  406                 }
  407                 return (ret);
  408         }
  409 
  410         if (type == DEVICE_PROP_HANDLE) {
  411                 if (size < sizeof(node))
  412                         return (-1);
  413                 ret = OF_getencprop(node, propname, &xref, sizeof(xref));
  414                 if (ret <= 0)
  415                         return (ret);
  416 
  417                 node = OF_node_from_xref(xref);
  418                 if (propvalue != NULL)
  419                         *(uint32_t *)propvalue = node;
  420                 return (ret);
  421         }
  422 
  423         return (OF_getprop(node, propname, propvalue, size));
  424 }
  425 
  426 static struct resource *
  427 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
  428     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
  429 {
  430         struct simplebus_softc *sc;
  431         struct simplebus_devinfo *di;
  432         struct resource_list_entry *rle;
  433         int j;
  434 
  435         sc = device_get_softc(bus);
  436 
  437         /*
  438          * Request for the default allocation with a given rid: use resource
  439          * list stored in the local device info.
  440          */
  441         if (RMAN_IS_DEFAULT_RANGE(start, end)) {
  442                 if ((di = device_get_ivars(child)) == NULL)
  443                         return (NULL);
  444 
  445                 if (type == SYS_RES_IOPORT)
  446                         type = SYS_RES_MEMORY;
  447 
  448                 rle = resource_list_find(&di->rl, type, *rid);
  449                 if (rle == NULL) {
  450                         if (bootverbose)
  451                                 device_printf(bus, "no default resources for "
  452                                     "rid = %d, type = %d\n", *rid, type);
  453                         return (NULL);
  454                 }
  455                 start = rle->start;
  456                 end = rle->end;
  457                 count = rle->count;
  458         }
  459 
  460         if (type == SYS_RES_MEMORY) {
  461                 /* Remap through ranges property */
  462                 for (j = 0; j < sc->nranges; j++) {
  463                         if (start >= sc->ranges[j].bus && end <
  464                             sc->ranges[j].bus + sc->ranges[j].size) {
  465                                 start -= sc->ranges[j].bus;
  466                                 start += sc->ranges[j].host;
  467                                 end -= sc->ranges[j].bus;
  468                                 end += sc->ranges[j].host;
  469                                 break;
  470                         }
  471                 }
  472                 if (j == sc->nranges && sc->nranges != 0) {
  473                         if (bootverbose)
  474                                 device_printf(bus, "Could not map resource "
  475                                     "%#jx-%#jx\n", start, end);
  476 
  477                         return (NULL);
  478                 }
  479         }
  480 
  481         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
  482             count, flags));
  483 }
  484 
  485 static int
  486 simplebus_print_res(struct simplebus_devinfo *di)
  487 {
  488         int rv;
  489 
  490         if (di == NULL)
  491                 return (0);
  492         rv = 0;
  493         rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
  494         rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
  495         return (rv);
  496 }
  497 
  498 static void
  499 simplebus_probe_nomatch(device_t bus, device_t child)
  500 {
  501         const char *name, *type, *compat;
  502 
  503         if (!bootverbose)
  504                 return;
  505 
  506         compat = ofw_bus_get_compat(child);
  507         if (compat == NULL)
  508                 return;
  509         name = ofw_bus_get_name(child);
  510         type = ofw_bus_get_type(child);
  511 
  512         device_printf(bus, "<%s>", name != NULL ? name : "unknown");
  513         simplebus_print_res(device_get_ivars(child));
  514         if (!ofw_bus_status_okay(child))
  515                 printf(" disabled");
  516         if (type)
  517                 printf(" type %s", type);
  518         printf(" compat %s (no driver attached)\n", compat);
  519 }
  520 
  521 static int
  522 simplebus_print_child(device_t bus, device_t child)
  523 {
  524         int rv;
  525 
  526         rv = bus_print_child_header(bus, child);
  527         rv += simplebus_print_res(device_get_ivars(child));
  528         if (!ofw_bus_status_okay(child))
  529                 rv += printf(" disabled");
  530         rv += bus_print_child_footer(bus, child);
  531         return (rv);
  532 }

Cache object: 24c5a595e6c349ac9d21a6b170b0eea2


[ 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.