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> 35*fd4d7757SWarner Losh #include <sys/boot.h> 36bc7b9300SIan Lepore #include <sys/bus.h> 37bc7b9300SIan Lepore #include <sys/libkern.h> 389ed9b267SJustin Hibbits #include <sys/reboot.h> 39910905c7SZbigniew Bodek #include <sys/rman.h> 40bc7b9300SIan Lepore 41bc7b9300SIan Lepore #include <machine/bus.h> 42bc7b9300SIan Lepore 43bc7b9300SIan Lepore #include <dev/ofw/openfirm.h> 44a259e55bSZbigniew Bodek #include <dev/ofw/ofw_pci.h> 4536e9c2ceSZbigniew Bodek #include <dev/ofw/ofw_subr.h> 46bc7b9300SIan Lepore 47bc7b9300SIan Lepore static void 48bc7b9300SIan Lepore get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip) 49bc7b9300SIan Lepore { 50bc7b9300SIan Lepore char type[64]; 51bc7b9300SIan Lepore uint32_t addr, size; 52bc7b9300SIan Lepore int pci, res; 53bc7b9300SIan Lepore 54bc7b9300SIan Lepore res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr)); 55bc7b9300SIan Lepore if (res == -1) 56bc7b9300SIan Lepore addr = 2; 57bc7b9300SIan Lepore res = OF_getencprop(node, "#size-cells", &size, sizeof(size)); 58bc7b9300SIan Lepore if (res == -1) 59bc7b9300SIan Lepore size = 1; 60bc7b9300SIan Lepore pci = 0; 61bc7b9300SIan Lepore if (addr == 3 && size == 2) { 62bc7b9300SIan Lepore res = OF_getprop(node, "device_type", type, sizeof(type)); 63bc7b9300SIan Lepore if (res != -1) { 64bc7b9300SIan Lepore type[sizeof(type) - 1] = '\0'; 656f3fcd76SJustin Hibbits if (strcmp(type, "pci") == 0 || 666f3fcd76SJustin Hibbits strcmp(type, "pciex")== 0) 676f3fcd76SJustin Hibbits pci = 1; 68bc7b9300SIan Lepore } 69bc7b9300SIan Lepore } 70bc7b9300SIan Lepore if (addrp != NULL) 71bc7b9300SIan Lepore *addrp = addr; 72bc7b9300SIan Lepore if (sizep != NULL) 73bc7b9300SIan Lepore *sizep = size; 74bc7b9300SIan Lepore if (pcip != NULL) 75bc7b9300SIan Lepore *pcip = pci; 76bc7b9300SIan Lepore } 77bc7b9300SIan Lepore 78bc7b9300SIan Lepore int 79bc7b9300SIan Lepore ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr, 80bc7b9300SIan Lepore bus_size_t *psize, pcell_t *ppci_hi) 81bc7b9300SIan Lepore { 82bc7b9300SIan Lepore pcell_t cell[32], pci_hi; 83b958a08eSAndrew Turner uint64_t addr, raddr, baddr; 84b958a08eSAndrew Turner uint64_t size, rsize; 85bc7b9300SIan Lepore uint32_t c, nbridge, naddr, nsize; 86bc7b9300SIan Lepore phandle_t bridge, parent; 87bc7b9300SIan Lepore u_int spc, rspc; 88bc7b9300SIan Lepore int pci, pcib, res; 89bc7b9300SIan Lepore 90bc7b9300SIan Lepore /* Sanity checking. */ 91bc7b9300SIan Lepore if (dev == 0) 92bc7b9300SIan Lepore return (EINVAL); 93bc7b9300SIan Lepore bridge = OF_parent(dev); 94bc7b9300SIan Lepore if (bridge == 0) 95bc7b9300SIan Lepore return (EINVAL); 96bc7b9300SIan Lepore if (regno < 0) 97bc7b9300SIan Lepore return (EINVAL); 98bc7b9300SIan Lepore if (paddr == NULL || psize == NULL) 99bc7b9300SIan Lepore return (EINVAL); 100bc7b9300SIan Lepore 101bc7b9300SIan Lepore get_addr_props(bridge, &naddr, &nsize, &pci); 102bc7b9300SIan Lepore res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg", 103bc7b9300SIan Lepore cell, sizeof(cell)); 104bc7b9300SIan Lepore if (res == -1) 105bc7b9300SIan Lepore return (ENXIO); 106bc7b9300SIan Lepore if (res % sizeof(cell[0])) 107bc7b9300SIan Lepore return (ENXIO); 108bc7b9300SIan Lepore res /= sizeof(cell[0]); 109bc7b9300SIan Lepore regno *= naddr + nsize; 110bc7b9300SIan Lepore if (regno + naddr + nsize > res) 111bc7b9300SIan Lepore return (EINVAL); 112bc7b9300SIan Lepore pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI; 113bc7b9300SIan Lepore spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 114bc7b9300SIan Lepore addr = 0; 115bc7b9300SIan Lepore for (c = 0; c < naddr; c++) 116bc7b9300SIan Lepore addr = ((uint64_t)addr << 32) | cell[regno++]; 117bc7b9300SIan Lepore size = 0; 118bc7b9300SIan Lepore for (c = 0; c < nsize; c++) 119bc7b9300SIan Lepore size = ((uint64_t)size << 32) | cell[regno++]; 120bc7b9300SIan Lepore /* 121bc7b9300SIan Lepore * Map the address range in the bridge's decoding window as given 122bc7b9300SIan Lepore * by the "ranges" property. If a node doesn't have such property 123bc7b9300SIan Lepore * or the property is empty, we assume an identity mapping. The 124bc7b9300SIan Lepore * standard says a missing property indicates no possible mapping. 125bc7b9300SIan Lepore * This code is more liberal since the intended use is to get a 126bc7b9300SIan Lepore * console running early, and a printf to warn of malformed data 127bc7b9300SIan Lepore * is probably futile before the console is fully set up. 128bc7b9300SIan Lepore */ 129bc7b9300SIan Lepore parent = OF_parent(bridge); 130bc7b9300SIan Lepore while (parent != 0) { 131bc7b9300SIan Lepore get_addr_props(parent, &nbridge, NULL, &pcib); 132bc7b9300SIan Lepore res = OF_getencprop(bridge, "ranges", cell, sizeof(cell)); 133bc7b9300SIan Lepore if (res < 1) 134bc7b9300SIan Lepore goto next; 135bc7b9300SIan Lepore if (res % sizeof(cell[0])) 136bc7b9300SIan Lepore return (ENXIO); 137bc7b9300SIan Lepore /* Capture pci_hi if we just transitioned onto a PCI bus. */ 138bc7b9300SIan Lepore if (pcib && pci_hi == OFW_PADDR_NOT_PCI) { 139bc7b9300SIan Lepore pci_hi = cell[0]; 140bc7b9300SIan Lepore spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK; 141bc7b9300SIan Lepore } 142bc7b9300SIan Lepore res /= sizeof(cell[0]); 143bc7b9300SIan Lepore regno = 0; 144bc7b9300SIan Lepore while (regno < res) { 14567bcc941SIan Lepore rspc = (pci ? cell[regno] : OFW_PADDR_NOT_PCI) & 14667bcc941SIan Lepore OFW_PCI_PHYS_HI_SPACEMASK; 147bc7b9300SIan Lepore if (rspc != spc) { 148bc7b9300SIan Lepore regno += naddr + nbridge + nsize; 149bc7b9300SIan Lepore continue; 150bc7b9300SIan Lepore } 151bc7b9300SIan Lepore raddr = 0; 152bc7b9300SIan Lepore for (c = 0; c < naddr; c++) 153bc7b9300SIan Lepore raddr = ((uint64_t)raddr << 32) | cell[regno++]; 154bc7b9300SIan Lepore rspc = (pcib) 155bc7b9300SIan Lepore ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK 156bc7b9300SIan Lepore : OFW_PADDR_NOT_PCI; 157bc7b9300SIan Lepore baddr = 0; 158bc7b9300SIan Lepore for (c = 0; c < nbridge; c++) 159bc7b9300SIan Lepore baddr = ((uint64_t)baddr << 32) | cell[regno++]; 160bc7b9300SIan Lepore rsize = 0; 161bc7b9300SIan Lepore for (c = 0; c < nsize; c++) 162bc7b9300SIan Lepore rsize = ((uint64_t)rsize << 32) | cell[regno++]; 163bc7b9300SIan Lepore if (addr < raddr || addr >= raddr + rsize) 164bc7b9300SIan Lepore continue; 165bc7b9300SIan Lepore addr = addr - raddr + baddr; 166bc7b9300SIan Lepore if (rspc != OFW_PADDR_NOT_PCI) 167bc7b9300SIan Lepore spc = rspc; 168bc7b9300SIan Lepore } 169bc7b9300SIan Lepore next: 170bc7b9300SIan Lepore bridge = parent; 171bc7b9300SIan Lepore parent = OF_parent(bridge); 172bc7b9300SIan Lepore get_addr_props(bridge, &naddr, &nsize, &pci); 173bc7b9300SIan Lepore } 174bc7b9300SIan Lepore 175b958a08eSAndrew Turner KASSERT(addr <= BUS_SPACE_MAXADDR, 176ec064639SJustin Hibbits ("Bus address is too large: %jx", (uintmax_t)addr)); 177b958a08eSAndrew Turner KASSERT(size <= BUS_SPACE_MAXSIZE, 1787900c60aSAndrew Turner ("Bus size is too large: %jx", (uintmax_t)size)); 179b958a08eSAndrew Turner 180bc7b9300SIan Lepore *paddr = addr; 181bc7b9300SIan Lepore *psize = size; 182bc7b9300SIan Lepore if (ppci_hi != NULL) 183bc7b9300SIan Lepore *ppci_hi = pci_hi; 184bc7b9300SIan Lepore 185bc7b9300SIan Lepore return (0); 186bc7b9300SIan Lepore } 1879ed9b267SJustin Hibbits 1889ed9b267SJustin Hibbits /* 1899ed9b267SJustin Hibbits * This is intended to be called early on, right after the OF system is 1909ed9b267SJustin Hibbits * initialized, so pmap may not be up yet. 1919ed9b267SJustin Hibbits */ 1929ed9b267SJustin Hibbits int 1939ed9b267SJustin Hibbits ofw_parse_bootargs(void) 1949ed9b267SJustin Hibbits { 1959ed9b267SJustin Hibbits phandle_t chosen; 1969ed9b267SJustin Hibbits char buf[2048]; /* early stack supposedly big enough */ 1979ed9b267SJustin Hibbits int err; 1989ed9b267SJustin Hibbits 1999ed9b267SJustin Hibbits chosen = OF_finddevice("/chosen"); 200108117ccSOleksandr Tymoshenko if (chosen == -1) 2019ed9b267SJustin Hibbits return (chosen); 2029ed9b267SJustin Hibbits 2039ed9b267SJustin Hibbits if ((err = OF_getprop(chosen, "bootargs", buf, sizeof(buf))) != -1) { 204eed42ff1SWarner Losh boothowto |= boot_parse_cmdline(buf); 2059ed9b267SJustin Hibbits return (0); 2069ed9b267SJustin Hibbits } 2079ed9b267SJustin Hibbits 2089ed9b267SJustin Hibbits return (err); 2099ed9b267SJustin Hibbits } 210