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/ofw/ofw_subr.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 (c) 2015 Ian Lepore <ian@freebsd.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * The initial ofw_reg_to_paddr() implementation has been copied from powerpc
   27  * ofw_machdep.c OF_decode_addr(). It was added by Marcel Moolenaar, who did not
   28  * assert copyright with the addition but still deserves credit for the work.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD$");
   33 
   34 #include <sys/param.h>
   35 #include <sys/boot.h>
   36 #include <sys/bus.h>
   37 #include <sys/libkern.h>
   38 #include <sys/reboot.h>
   39 #include <sys/rman.h>
   40 
   41 #include <machine/bus.h>
   42 
   43 #include <dev/ofw/openfirm.h>
   44 #include <dev/ofw/ofw_pci.h>
   45 #include <dev/ofw/ofw_subr.h>
   46 
   47 static void
   48 get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
   49 {
   50         char type[64];
   51         uint32_t addr, size;
   52         int pci, res;
   53 
   54         res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr));
   55         if (res == -1)
   56                 addr = 2;
   57         res = OF_getencprop(node, "#size-cells", &size, sizeof(size));
   58         if (res == -1)
   59                 size = 1;
   60         pci = 0;
   61         if (addr == 3 && size == 2) {
   62                 res = OF_getprop(node, "device_type", type, sizeof(type));
   63                 if (res != -1) {
   64                         type[sizeof(type) - 1] = '\0';
   65                         if (strcmp(type, "pci") == 0 ||
   66                             strcmp(type, "pciex")== 0)
   67                                 pci = 1;
   68                 }
   69         }
   70         if (addrp != NULL)
   71                 *addrp = addr;
   72         if (sizep != NULL)
   73                 *sizep = size;
   74         if (pcip != NULL)
   75                 *pcip = pci;
   76 }
   77 
   78 int
   79 ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr,
   80     bus_size_t *psize, pcell_t *ppci_hi)
   81 {
   82         static pcell_t cell[256];
   83         pcell_t pci_hi;
   84         uint64_t addr, raddr, baddr;
   85         uint64_t size, rsize;
   86         uint32_t c, nbridge, naddr, nsize;
   87         phandle_t bridge, parent;
   88         u_int spc, rspc;
   89         int pci, pcib, res;
   90 
   91         /* Sanity checking. */
   92         if (dev == 0)
   93                 return (EINVAL);
   94         bridge = OF_parent(dev);
   95         if (bridge == 0)
   96                 return (EINVAL);
   97         if (regno < 0)
   98                 return (EINVAL);
   99         if (paddr == NULL || psize == NULL)
  100                 return (EINVAL);
  101 
  102         get_addr_props(bridge, &naddr, &nsize, &pci);
  103         res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg",
  104             cell, sizeof(cell));
  105         if (res == -1)
  106                 return (ENXIO);
  107         if (res % sizeof(cell[0]))
  108                 return (ENXIO);
  109         res /= sizeof(cell[0]);
  110         regno *= naddr + nsize;
  111         if (regno + naddr + nsize > res)
  112                 return (EINVAL);
  113         pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI;
  114         spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK;
  115         addr = 0;
  116         for (c = 0; c < naddr; c++)
  117                 addr = ((uint64_t)addr << 32) | cell[regno++];
  118         size = 0;
  119         for (c = 0; c < nsize; c++)
  120                 size = ((uint64_t)size << 32) | cell[regno++];
  121         /*
  122          * Map the address range in the bridge's decoding window as given
  123          * by the "ranges" property. If a node doesn't have such property
  124          * or the property is empty, we assume an identity mapping.  The
  125          * standard says a missing property indicates no possible mapping.
  126          * This code is more liberal since the intended use is to get a
  127          * console running early, and a printf to warn of malformed data
  128          * is probably futile before the console is fully set up.
  129          */
  130         parent = OF_parent(bridge);
  131         while (parent != 0) {
  132                 get_addr_props(parent, &nbridge, NULL, &pcib);
  133                 res = OF_getencprop(bridge, "ranges", cell, sizeof(cell));
  134                 if (res < 1)
  135                         goto next;
  136                 if (res % sizeof(cell[0]))
  137                         return (ENXIO);
  138                 /* Capture pci_hi if we just transitioned onto a PCI bus. */
  139                 if (pcib && pci_hi == OFW_PADDR_NOT_PCI) {
  140                         pci_hi = cell[0];
  141                         spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK;
  142                 }
  143                 res /= sizeof(cell[0]);
  144                 regno = 0;
  145                 while (regno < res) {
  146                         rspc = (pci ? cell[regno] : OFW_PADDR_NOT_PCI) &
  147                             OFW_PCI_PHYS_HI_SPACEMASK;
  148                         if (rspc != spc) {
  149                                 regno += naddr + nbridge + nsize;
  150                                 continue;
  151                         }
  152                         raddr = 0;
  153                         for (c = 0; c < naddr; c++)
  154                                 raddr = ((uint64_t)raddr << 32) | cell[regno++];
  155                         rspc = (pcib)
  156                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
  157                             : OFW_PADDR_NOT_PCI;
  158                         baddr = 0;
  159                         for (c = 0; c < nbridge; c++)
  160                                 baddr = ((uint64_t)baddr << 32) | cell[regno++];
  161                         rsize = 0;
  162                         for (c = 0; c < nsize; c++)
  163                                 rsize = ((uint64_t)rsize << 32) | cell[regno++];
  164                         if (addr < raddr || addr >= raddr + rsize)
  165                                 continue;
  166                         addr = addr - raddr + baddr;
  167                         if (rspc != OFW_PADDR_NOT_PCI)
  168                                 spc = rspc;
  169                 }
  170         next:
  171                 bridge = parent;
  172                 parent = OF_parent(bridge);
  173                 get_addr_props(bridge, &naddr, &nsize, &pci);
  174         }
  175 
  176         KASSERT(addr <= BUS_SPACE_MAXADDR,
  177             ("Bus address is too large: %jx", (uintmax_t)addr));
  178         KASSERT(size <= BUS_SPACE_MAXSIZE,
  179             ("Bus size is too large: %jx", (uintmax_t)size));
  180 
  181         *paddr = addr;
  182         *psize = size;
  183         if (ppci_hi != NULL)
  184                 *ppci_hi = pci_hi;
  185 
  186         return (0);
  187 }
  188 
  189 /*
  190  * This is intended to be called early on, right after the OF system is
  191  * initialized, so pmap may not be up yet.
  192  */
  193 int
  194 ofw_parse_bootargs(void)
  195 {
  196         phandle_t chosen;
  197         char buf[2048];         /* early stack supposedly big enough */
  198         int err;
  199 
  200         chosen = OF_finddevice("/chosen");
  201         if (chosen == -1)
  202                 return (chosen);
  203 
  204         if ((err = OF_getprop(chosen, "bootargs", buf, sizeof(buf))) != -1) {
  205                 boothowto |= boot_parse_cmdline(buf);
  206                 return (0);
  207         }
  208 
  209         return (err);
  210 }

Cache object: 4054b50c3e538eeaa9ef3cb828841df3


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