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/bus.h> 36 #include <sys/libkern.h> 37 #include <sys/reboot.h> 38 #include <sys/rman.h> 39 40 #include <machine/bus.h> 41 42 #include <dev/ofw/openfirm.h> 43 #include <dev/ofw/ofw_pci.h> 44 #include <dev/ofw/ofw_subr.h> 45 46 static void 47 get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip) 48 { 49 char type[64]; 50 uint32_t addr, size; 51 int pci, res; 52 53 res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr)); 54 if (res == -1) 55 addr = 2; 56 res = OF_getencprop(node, "#size-cells", &size, sizeof(size)); 57 if (res == -1) 58 size = 1; 59 pci = 0; 60 if (addr == 3 && size == 2) { 61 res = OF_getprop(node, "device_type", type, sizeof(type)); 62 if (res != -1) { 63 type[sizeof(type) - 1] = '\0'; 64 pci = (strcmp(type, "pci") == 0) ? 1 : 0; 65 } 66 } 67 if (addrp != NULL) 68 *addrp = addr; 69 if (sizep != NULL) 70 *sizep = size; 71 if (pcip != NULL) 72 *pcip = pci; 73 } 74 75 int 76 ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr, 77 bus_size_t *psize, pcell_t *ppci_hi) 78 { 79 pcell_t cell[32], pci_hi; 80 uint64_t addr, raddr, baddr; 81 uint64_t size, rsize; 82 uint32_t c, nbridge, naddr, nsize; 83 phandle_t bridge, parent; 84 u_int spc, rspc; 85 int pci, pcib, res; 86 87 /* Sanity checking. */ 88 if (dev == 0) 89 return (EINVAL); 90 bridge = OF_parent(dev); 91 if (bridge == 0) 92 return (EINVAL); 93 if (regno < 0) 94 return (EINVAL); 95 if (paddr == NULL || psize == NULL) 96 return (EINVAL); 97 98 get_addr_props(bridge, &naddr, &nsize, &pci); 99 res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg", 100 cell, sizeof(cell)); 101 if (res == -1) 102 return (ENXIO); 103 if (res % sizeof(cell[0])) 104 return (ENXIO); 105 res /= sizeof(cell[0]); 106 regno *= naddr + nsize; 107 if (regno + naddr + nsize > res) 108 return (EINVAL); 109 pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI; 110 spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 111 addr = 0; 112 for (c = 0; c < naddr; c++) 113 addr = ((uint64_t)addr << 32) | cell[regno++]; 114 size = 0; 115 for (c = 0; c < nsize; c++) 116 size = ((uint64_t)size << 32) | cell[regno++]; 117 /* 118 * Map the address range in the bridge's decoding window as given 119 * by the "ranges" property. If a node doesn't have such property 120 * or the property is empty, we assume an identity mapping. The 121 * standard says a missing property indicates no possible mapping. 122 * This code is more liberal since the intended use is to get a 123 * console running early, and a printf to warn of malformed data 124 * is probably futile before the console is fully set up. 125 */ 126 parent = OF_parent(bridge); 127 while (parent != 0) { 128 get_addr_props(parent, &nbridge, NULL, &pcib); 129 res = OF_getencprop(bridge, "ranges", cell, sizeof(cell)); 130 if (res < 1) 131 goto next; 132 if (res % sizeof(cell[0])) 133 return (ENXIO); 134 /* Capture pci_hi if we just transitioned onto a PCI bus. */ 135 if (pcib && pci_hi == OFW_PADDR_NOT_PCI) { 136 pci_hi = cell[0]; 137 spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 138 } 139 res /= sizeof(cell[0]); 140 regno = 0; 141 while (regno < res) { 142 rspc = (pci ? cell[regno] : OFW_PADDR_NOT_PCI) & 143 OFW_PCI_PHYS_HI_SPACEMASK; 144 if (rspc != spc) { 145 regno += naddr + nbridge + nsize; 146 continue; 147 } 148 raddr = 0; 149 for (c = 0; c < naddr; c++) 150 raddr = ((uint64_t)raddr << 32) | cell[regno++]; 151 rspc = (pcib) 152 ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK 153 : OFW_PADDR_NOT_PCI; 154 baddr = 0; 155 for (c = 0; c < nbridge; c++) 156 baddr = ((uint64_t)baddr << 32) | cell[regno++]; 157 rsize = 0; 158 for (c = 0; c < nsize; c++) 159 rsize = ((uint64_t)rsize << 32) | cell[regno++]; 160 if (addr < raddr || addr >= raddr + rsize) 161 continue; 162 addr = addr - raddr + baddr; 163 if (rspc != OFW_PADDR_NOT_PCI) 164 spc = rspc; 165 } 166 next: 167 bridge = parent; 168 parent = OF_parent(bridge); 169 get_addr_props(bridge, &naddr, &nsize, &pci); 170 } 171 172 KASSERT(addr <= BUS_SPACE_MAXADDR, 173 ("Bus sddress is too large: %jx", (uintmax_t)addr)); 174 KASSERT(size <= BUS_SPACE_MAXSIZE, 175 ("Bus size is too large: %jx", (uintmax_t)size)); 176 177 *paddr = addr; 178 *psize = size; 179 if (ppci_hi != NULL) 180 *ppci_hi = pci_hi; 181 182 return (0); 183 } 184 185 /* Parse cmd line args as env - copied from xlp_machdep. */ 186 /* XXX-BZ this should really be centrally provided for all (boot) code. */ 187 static void 188 _parse_bootargs(char *cmdline) 189 { 190 char *n, *v; 191 192 while ((v = strsep(&cmdline, " \n")) != NULL) { 193 if (*v == '\0') 194 continue; 195 if (*v == '-') { 196 while (*v != '\0') { 197 v++; 198 switch (*v) { 199 case 'a': boothowto |= RB_ASKNAME; break; 200 /* Someone should simulate that ;-) */ 201 case 'C': boothowto |= RB_CDROM; break; 202 case 'd': boothowto |= RB_KDB; break; 203 case 'D': boothowto |= RB_MULTIPLE; break; 204 case 'm': boothowto |= RB_MUTE; break; 205 case 'g': boothowto |= RB_GDB; break; 206 case 'h': boothowto |= RB_SERIAL; break; 207 case 'p': boothowto |= RB_PAUSE; break; 208 case 'r': boothowto |= RB_DFLTROOT; break; 209 case 's': boothowto |= RB_SINGLE; break; 210 case 'v': boothowto |= RB_VERBOSE; break; 211 } 212 } 213 } else { 214 n = strsep(&v, "="); 215 if (v == NULL) 216 kern_setenv(n, "1"); 217 else 218 kern_setenv(n, v); 219 } 220 } 221 } 222 223 /* 224 * This is intended to be called early on, right after the OF system is 225 * initialized, so pmap may not be up yet. 226 */ 227 int 228 ofw_parse_bootargs(void) 229 { 230 phandle_t chosen; 231 char buf[2048]; /* early stack supposedly big enough */ 232 int err; 233 234 chosen = OF_finddevice("/chosen"); 235 if (chosen == -1) 236 return (chosen); 237 238 if ((err = OF_getprop(chosen, "bootargs", buf, sizeof(buf))) != -1) { 239 _parse_bootargs(buf); 240 return (0); 241 } 242 243 return (err); 244 } 245