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