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