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