summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/ofw/ofw_subr.c
blob: 8359485efa6548b4c5ff65cf605e7ffe287cc438 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * Copyright (c) 2015 Ian Lepore <ian@freebsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The initial ofw_reg_to_paddr() implementation has been copied from powerpc
 * ofw_machdep.c OF_decode_addr(). It was added by Marcel Moolenaar, who did not
 * assert copyright with the addition but still deserves credit for the work.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/libkern.h>
#include <sys/reboot.h>
#include <sys/rman.h>

#include <machine/bus.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_pci.h>
#include <dev/ofw/ofw_subr.h>

static void
get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
{
	char type[64];
	uint32_t addr, size;
	int pci, res;

	res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr));
	if (res == -1)
		addr = 2;
	res = OF_getencprop(node, "#size-cells", &size, sizeof(size));
	if (res == -1)
		size = 1;
	pci = 0;
	if (addr == 3 && size == 2) {
		res = OF_getprop(node, "device_type", type, sizeof(type));
		if (res != -1) {
			type[sizeof(type) - 1] = '\0';
			pci = (strcmp(type, "pci") == 0) ? 1 : 0;
		}
	}
	if (addrp != NULL)
		*addrp = addr;
	if (sizep != NULL)
		*sizep = size;
	if (pcip != NULL)
		*pcip = pci;
}

int
ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr,
    bus_size_t *psize, pcell_t *ppci_hi)
{
	pcell_t cell[32], pci_hi;
	uint64_t addr, raddr, baddr;
	uint64_t size, rsize;
	uint32_t c, nbridge, naddr, nsize;
	phandle_t bridge, parent;
	u_int spc, rspc;
	int pci, pcib, res;

	/* Sanity checking. */
	if (dev == 0)
		return (EINVAL);
	bridge = OF_parent(dev);
	if (bridge == 0)
		return (EINVAL);
	if (regno < 0)
		return (EINVAL);
	if (paddr == NULL || psize == NULL)
		return (EINVAL);

	get_addr_props(bridge, &naddr, &nsize, &pci);
	res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg",
	    cell, sizeof(cell));
	if (res == -1)
		return (ENXIO);
	if (res % sizeof(cell[0]))
		return (ENXIO);
	res /= sizeof(cell[0]);
	regno *= naddr + nsize;
	if (regno + naddr + nsize > res)
		return (EINVAL);
	pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI;
	spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK;
	addr = 0;
	for (c = 0; c < naddr; c++)
		addr = ((uint64_t)addr << 32) | cell[regno++];
	size = 0;
	for (c = 0; c < nsize; c++)
		size = ((uint64_t)size << 32) | cell[regno++];
	/*
	 * Map the address range in the bridge's decoding window as given
	 * by the "ranges" property. If a node doesn't have such property
	 * or the property is empty, we assume an identity mapping.  The
	 * standard says a missing property indicates no possible mapping.
	 * This code is more liberal since the intended use is to get a
	 * console running early, and a printf to warn of malformed data
	 * is probably futile before the console is fully set up.
	 */
	parent = OF_parent(bridge);
	while (parent != 0) {
		get_addr_props(parent, &nbridge, NULL, &pcib);
		res = OF_getencprop(bridge, "ranges", cell, sizeof(cell));
		if (res < 1)
			goto next;
		if (res % sizeof(cell[0]))
			return (ENXIO);
		/* Capture pci_hi if we just transitioned onto a PCI bus. */
		if (pcib && pci_hi == OFW_PADDR_NOT_PCI) {
			pci_hi = cell[0];
			spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK;
		}
		res /= sizeof(cell[0]);
		regno = 0;
		while (regno < res) {
			rspc = (pci ? cell[regno] : OFW_PADDR_NOT_PCI) &
			    OFW_PCI_PHYS_HI_SPACEMASK;
			if (rspc != spc) {
				regno += naddr + nbridge + nsize;
				continue;
			}
			raddr = 0;
			for (c = 0; c < naddr; c++)
				raddr = ((uint64_t)raddr << 32) | cell[regno++];
			rspc = (pcib)
			    ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
			    : OFW_PADDR_NOT_PCI;
			baddr = 0;
			for (c = 0; c < nbridge; c++)
				baddr = ((uint64_t)baddr << 32) | cell[regno++];
			rsize = 0;
			for (c = 0; c < nsize; c++)
				rsize = ((uint64_t)rsize << 32) | cell[regno++];
			if (addr < raddr || addr >= raddr + rsize)
				continue;
			addr = addr - raddr + baddr;
			if (rspc != OFW_PADDR_NOT_PCI)
				spc = rspc;
		}
	next:
		bridge = parent;
		parent = OF_parent(bridge);
		get_addr_props(bridge, &naddr, &nsize, &pci);
	}

	KASSERT(addr <= BUS_SPACE_MAXADDR,
	    ("Bus sddress is too large: %jx", (uintmax_t)addr));
	KASSERT(size <= BUS_SPACE_MAXSIZE,
	    ("Bus size is too large: %jx", (uintmax_t)size));

	*paddr = addr;
	*psize = size;
	if (ppci_hi != NULL)
		*ppci_hi = pci_hi;

	return (0);
}

/* Parse cmd line args as env - copied from xlp_machdep. */
/* XXX-BZ this should really be centrally provided for all (boot) code. */
static void
_parse_bootargs(char *cmdline)
{
	char *n, *v;

	while ((v = strsep(&cmdline, " \n")) != NULL) {
		if (*v == '\0')
			continue;
		if (*v == '-') {
			while (*v != '\0') {
				v++;
				switch (*v) {
				case 'a': boothowto |= RB_ASKNAME; break;
				/* Someone should simulate that ;-) */
				case 'C': boothowto |= RB_CDROM; break;
				case 'd': boothowto |= RB_KDB; break;
				case 'D': boothowto |= RB_MULTIPLE; break;
				case 'm': boothowto |= RB_MUTE; break;
				case 'g': boothowto |= RB_GDB; break;
				case 'h': boothowto |= RB_SERIAL; break;
				case 'p': boothowto |= RB_PAUSE; break;
				case 'r': boothowto |= RB_DFLTROOT; break;
				case 's': boothowto |= RB_SINGLE; break;
				case 'v': boothowto |= RB_VERBOSE; break;
				}
			}
		} else {
			n = strsep(&v, "=");
			if (v == NULL)
				kern_setenv(n, "1");
			else
				kern_setenv(n, v);
		}
	}
}

/*
 * This is intended to be called early on, right after the OF system is
 * initialized, so pmap may not be up yet.
 */
int
ofw_parse_bootargs(void)
{
	phandle_t chosen;
	char buf[2048];		/* early stack supposedly big enough */
	int err;

	chosen = OF_finddevice("/chosen");
	if (chosen == -1)
		return (chosen);

	if ((err = OF_getprop(chosen, "bootargs", buf, sizeof(buf))) != -1) {
		_parse_bootargs(buf);
		return (0);
	}

	return (err);
}