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/powerpc/powerpc/ofw_machdep.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) 1996 Wolfgang Solfrank.
    3  * Copyright (C) 1996 TooLs GmbH.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. All advertising materials mentioning features or use of this software
   15  *    must display the following acknowledgement:
   16  *      This product includes software developed by TooLs GmbH.
   17  * 4. The name of TooLs GmbH may not be used to endorse or promote products
   18  *    derived from this software without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
   21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   23  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   30  *
   31  * $NetBSD: ofw_machdep.c,v 1.5 2000/05/23 13:25:43 tsubai Exp $
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD$");
   36 
   37 #include <sys/param.h>
   38 #include <sys/bus.h>
   39 #include <sys/systm.h>
   40 #include <sys/conf.h>
   41 #include <sys/disk.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/malloc.h>
   44 #include <sys/stat.h>
   45 
   46 #include <net/ethernet.h>
   47 
   48 #include <dev/ofw/openfirm.h>
   49 #include <dev/ofw/ofw_pci.h>
   50 
   51 #include <vm/vm.h>
   52 #include <vm/vm_param.h>
   53 #include <vm/vm_page.h>
   54 
   55 #include <machine/bus.h>
   56 #include <machine/md_var.h>
   57 #include <machine/powerpc.h>
   58 #include <machine/ofw_machdep.h>
   59 #include <powerpc/ofw/ofw_pci.h>
   60 
   61 #define OFMEM_REGIONS   32
   62 static struct mem_region OFmem[OFMEM_REGIONS + 1], OFavail[OFMEM_REGIONS + 3];
   63 static struct mem_region OFfree[OFMEM_REGIONS + 3];
   64 
   65 extern register_t ofmsr[5];
   66 extern struct   pcpu __pcpu[MAXCPU];
   67 extern struct   pmap ofw_pmap;
   68 static int      (*ofwcall)(void *);
   69 
   70 /*
   71  * Saved SPRG0-3 from OpenFirmware. Will be restored prior to the callback.
   72  */
   73 register_t      ofw_sprg0_save;
   74 
   75 static __inline void
   76 ofw_sprg_prepare(void)
   77 {
   78         /*
   79          * Assume that interrupt are disabled at this point, or
   80          * SPRG1-3 could be trashed
   81          */
   82         __asm __volatile("mfsprg0 %0\n\t"
   83                          "mtsprg0 %1\n\t"
   84                          "mtsprg1 %2\n\t"
   85                          "mtsprg2 %3\n\t"
   86                          "mtsprg3 %4\n\t"
   87                          : "=&r"(ofw_sprg0_save)
   88                          : "r"(ofmsr[1]),
   89                          "r"(ofmsr[2]),
   90                          "r"(ofmsr[3]),
   91                          "r"(ofmsr[4]));
   92 }
   93 
   94 static __inline void
   95 ofw_sprg_restore(void)
   96 {
   97         /*
   98          * Note that SPRG1-3 contents are irrelevant. They are scratch
   99          * registers used in the early portion of trap handling when
  100          * interrupts are disabled.
  101          *
  102          * PCPU data cannot be used until this routine is called !
  103          */
  104         __asm __volatile("mtsprg0 %0" :: "r"(ofw_sprg0_save));
  105 }
  106 
  107 /*
  108  * Memory region utilities: determine if two regions overlap,
  109  * and merge two overlapping regions into one
  110  */
  111 static int
  112 memr_overlap(struct mem_region *r1, struct mem_region *r2)
  113 {
  114         if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
  115             (r2->mr_start + r2->mr_size) < r1->mr_start)
  116                 return (FALSE);
  117         
  118         return (TRUE);  
  119 }
  120 
  121 static void
  122 memr_merge(struct mem_region *from, struct mem_region *to)
  123 {
  124         int end;
  125         end = imax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
  126         to->mr_start = imin(from->mr_start, to->mr_start);
  127         to->mr_size = end - to->mr_start;
  128 }
  129 
  130 /*
  131  * This is called during powerpc_init, before the system is really initialized.
  132  * It shall provide the total and the available regions of RAM.
  133  * Both lists must have a zero-size entry as terminator.
  134  * The available regions need not take the kernel into account, but needs
  135  * to provide space for two additional entry beyond the terminating one.
  136  */
  137 void
  138 mem_regions(struct mem_region **memp, int *memsz,
  139                 struct mem_region **availp, int *availsz)
  140 {
  141         int phandle;
  142         int asz, msz, fsz;
  143         int i, j;
  144         int still_merging;
  145         
  146         /*
  147          * Get memory.
  148          */
  149         if ((phandle = OF_finddevice("/memory")) == -1
  150             || (msz = OF_getprop(phandle, "reg",
  151                           OFmem, sizeof OFmem[0] * OFMEM_REGIONS))
  152                <= 0
  153             || (asz = OF_getprop(phandle, "available",
  154                           OFavail, sizeof OFavail[0] * OFMEM_REGIONS))
  155                <= 0)
  156                 panic("no memory?");
  157         *memp = OFmem;
  158         *memsz = msz / sizeof(struct mem_region);
  159 
  160         /*
  161          * OFavail may have overlapping regions - collapse these
  162          * and copy out remaining regions to OFfree
  163          */
  164         asz /= sizeof(struct mem_region);
  165         do {
  166                 still_merging = FALSE;
  167                 for (i = 0; i < asz; i++) {
  168                         if (OFavail[i].mr_size == 0)
  169                                 continue;
  170                         for (j = i+1; j < asz; j++) {
  171                                 if (OFavail[j].mr_size == 0)
  172                                         continue;
  173                                 if (memr_overlap(&OFavail[j], &OFavail[i])) {
  174                                         memr_merge(&OFavail[j], &OFavail[i]);
  175                                         /* mark inactive */
  176                                         OFavail[j].mr_size = 0;
  177                                         still_merging = TRUE;
  178                                 }
  179                         }
  180                 }
  181         } while (still_merging == TRUE);
  182 
  183         /* evict inactive ranges */
  184         for (i = 0, fsz = 0; i < asz; i++) {
  185                 if (OFavail[i].mr_size != 0) {
  186                         OFfree[fsz] = OFavail[i];
  187                         fsz++;
  188                 }
  189         }
  190 
  191         *availp = OFfree;
  192         *availsz = fsz;
  193 }
  194 
  195 void
  196 set_openfirm_callback(int (*openfirm)(void *))
  197 {
  198 
  199         ofwcall = openfirm;
  200 }
  201 
  202 int
  203 openfirmware(void *args)
  204 {
  205         long    oldmsr;
  206         int     result;
  207         u_int   srsave[16];
  208         u_int   i;
  209 
  210         __asm __volatile(       "\t"
  211                 "sync\n\t"
  212                 "mfmsr  %0\n\t"
  213                 "mtmsr  %1\n\t"
  214                 "isync\n"
  215                 : "=r" (oldmsr)
  216                 : "r" (ofmsr[0])
  217         );
  218 
  219         ofw_sprg_prepare();
  220 
  221         if (pmap_bootstrapped) {
  222                 /*
  223                  * Swap the kernel's address space with Open Firmware's
  224                  */
  225                 for (i = 0; i < 16; i++) {
  226                         srsave[i] = mfsrin(i << ADDR_SR_SHFT);
  227                         mtsrin(i << ADDR_SR_SHFT, ofw_pmap.pm_sr[i]);
  228                 }
  229 
  230                 /*
  231                  * Clear battable[] translations
  232                  */
  233                 __asm __volatile("mtdbatu 2, %0\n"
  234                                  "mtdbatu 3, %0" : : "r" (0));
  235                 isync();
  236         }
  237 
  238         result = ofwcall(args);
  239 
  240         if (pmap_bootstrapped) {
  241                 /*
  242                  * Restore the kernel's addr space. The isync() doesn;t
  243                  * work outside the loop unless mtsrin() is open-coded
  244                  * in an asm statement :(
  245                  */
  246                 for (i = 0; i < 16; i++) {
  247                         mtsrin(i << ADDR_SR_SHFT, srsave[i]);
  248                         isync();
  249                 }
  250         }
  251 
  252         ofw_sprg_restore();
  253 
  254         __asm(  "\t"
  255                 "mtmsr  %0\n\t"
  256                 "isync\n"
  257                 : : "r" (oldmsr)
  258         );
  259 
  260         return (result);
  261 }
  262 
  263 void
  264 OF_halt()
  265 {
  266         int retval;     /* dummy, this may not be needed */
  267 
  268         OF_interpret("shut-down", 1, &retval);
  269         for (;;);       /* just in case */
  270 }
  271 
  272 void
  273 OF_reboot()
  274 {
  275         int retval;     /* dummy, this may not be needed */
  276 
  277         OF_interpret("reset-all", 1, &retval);
  278         for (;;);       /* just in case */
  279 }
  280 
  281 void
  282 OF_getetheraddr(device_t dev, u_char *addr)
  283 {
  284         phandle_t       node;
  285 
  286         node = ofw_pci_find_node(dev);
  287         OF_getprop(node, "local-mac-address", addr, ETHER_ADDR_LEN);
  288 }
  289 
  290 /*
  291  * Return a bus handle and bus tag that corresponds to the register
  292  * numbered regno for the device referenced by the package handle
  293  * dev. This function is intended to be used by console drivers in
  294  * early boot only. It works by mapping the address of the device's
  295  * register in the address space of its parent and recursively walk
  296  * the device tree upward this way.
  297  */
  298 static void
  299 OF_get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
  300 {
  301         char name[16];
  302         uint32_t addr, size;
  303         int pci, res;
  304 
  305         res = OF_getprop(node, "#address-cells", &addr, sizeof(addr));
  306         if (res == -1)
  307                 addr = 2;
  308         res = OF_getprop(node, "#size-cells", &size, sizeof(size));
  309         if (res == -1)
  310                 size = 1;
  311         pci = 0;
  312         if (addr == 3 && size == 2) {
  313                 res = OF_getprop(node, "name", name, sizeof(name));
  314                 if (res != -1) {
  315                         name[sizeof(name) - 1] = '\0';
  316                         pci = (strcmp(name, "pci") == 0) ? 1 : 0;
  317                 }
  318         }
  319         if (addrp != NULL)
  320                 *addrp = addr;
  321         if (sizep != NULL)
  322                 *sizep = size;
  323         if (pcip != NULL)
  324                 *pcip = pci;
  325 }
  326 
  327 int
  328 OF_decode_addr(phandle_t dev, int regno, bus_space_tag_t *tag,
  329     bus_space_handle_t *handle)
  330 {
  331         uint32_t cell[32];
  332         bus_addr_t addr, raddr, baddr;
  333         bus_size_t size, rsize;
  334         uint32_t c, nbridge, naddr, nsize;
  335         phandle_t bridge, parent;
  336         u_int spc, rspc;
  337         int pci, pcib, res;
  338 
  339         /* Sanity checking. */
  340         if (dev == 0)
  341                 return (EINVAL);
  342         bridge = OF_parent(dev);
  343         if (bridge == 0)
  344                 return (EINVAL);
  345         if (regno < 0)
  346                 return (EINVAL);
  347         if (tag == NULL || handle == NULL)
  348                 return (EINVAL);
  349 
  350         /* Get the requested register. */
  351         OF_get_addr_props(bridge, &naddr, &nsize, &pci);
  352         res = OF_getprop(dev, (pci) ? "assigned-addresses" : "reg",
  353             cell, sizeof(cell));
  354         if (res == -1)
  355                 return (ENXIO);
  356         if (res % sizeof(cell[0]))
  357                 return (ENXIO);
  358         res /= sizeof(cell[0]);
  359         regno *= naddr + nsize;
  360         if (regno + naddr + nsize > res)
  361                 return (EINVAL);
  362         spc = (pci) ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK : ~0;
  363         addr = 0;
  364         for (c = 0; c < naddr; c++)
  365                 addr = ((uint64_t)addr << 32) | cell[regno++];
  366         size = 0;
  367         for (c = 0; c < nsize; c++)
  368                 size = ((uint64_t)size << 32) | cell[regno++];
  369 
  370         /*
  371          * Map the address range in the bridge's decoding window as given
  372          * by the "ranges" property. If a node doesn't have such property
  373          * then no mapping is done.
  374          */
  375         parent = OF_parent(bridge);
  376         while (parent != 0) {
  377                 OF_get_addr_props(parent, &nbridge, NULL, &pcib);
  378                 res = OF_getprop(bridge, "ranges", cell, sizeof(cell));
  379                 if (res == -1)
  380                         goto next;
  381                 if (res % sizeof(cell[0]))
  382                         return (ENXIO);
  383                 res /= sizeof(cell[0]);
  384                 regno = 0;
  385                 while (regno < res) {
  386                         rspc = (pci)
  387                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
  388                             : ~0;
  389                         if (rspc != spc) {
  390                                 regno += naddr + nbridge + nsize;
  391                                 continue;
  392                         }
  393                         raddr = 0;
  394                         for (c = 0; c < naddr; c++)
  395                                 raddr = ((uint64_t)raddr << 32) | cell[regno++];
  396                         rspc = (pcib)
  397                             ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
  398                             : ~0;
  399                         baddr = 0;
  400                         for (c = 0; c < nbridge; c++)
  401                                 baddr = ((uint64_t)baddr << 32) | cell[regno++];
  402                         rsize = 0;
  403                         for (c = 0; c < nsize; c++)
  404                                 rsize = ((uint64_t)rsize << 32) | cell[regno++];
  405                         if (addr < raddr || addr >= raddr + rsize)
  406                                 continue;
  407                         addr = addr - raddr + baddr;
  408                         if (rspc != ~0)
  409                                 spc = rspc;
  410                 }
  411 
  412         next:
  413                 bridge = parent;
  414                 parent = OF_parent(bridge);
  415                 OF_get_addr_props(bridge, &naddr, &nsize, &pci);
  416         }
  417 
  418         /* Default to memory mapped I/O. */
  419         *tag = PPC_BUS_SPACE_MEM;
  420         if (spc == OFW_PCI_PHYS_HI_SPACE_IO)
  421                 *tag = PPC_BUS_SPACE_IO;
  422         return (bus_space_map(*tag, addr, size, 0, handle));
  423 }
  424 
  425 int
  426 mem_valid(vm_offset_t addr, int len)
  427 {
  428         int i;
  429 
  430         for (i = 0; i < OFMEM_REGIONS; i++)
  431                 if ((addr >= OFmem[i].mr_start) 
  432                     && (addr + len < OFmem[i].mr_start + OFmem[i].mr_size))
  433                         return (0);
  434 
  435         return (EFAULT);
  436 }

Cache object: bfc2068cca0536c00694a76b87b9b455


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