ofw_subr.c revision 294927
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: head/sys/dev/ofw/ofw_subr.c 294927 2016-01-27 17:33:31Z andrew $");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/libkern.h>
37
38#include <machine/bus.h>
39
40#include <dev/ofw/openfirm.h>
41#include <dev/ofw/ofw_pci.h>
42#include <dev/ofw/ofw_subr.h>
43
44static void
45get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep, int *pcip)
46{
47	char type[64];
48	uint32_t addr, size;
49	int pci, res;
50
51	res = OF_getencprop(node, "#address-cells", &addr, sizeof(addr));
52	if (res == -1)
53		addr = 2;
54	res = OF_getencprop(node, "#size-cells", &size, sizeof(size));
55	if (res == -1)
56		size = 1;
57	pci = 0;
58	if (addr == 3 && size == 2) {
59		res = OF_getprop(node, "device_type", type, sizeof(type));
60		if (res != -1) {
61			type[sizeof(type) - 1] = '\0';
62			pci = (strcmp(type, "pci") == 0) ? 1 : 0;
63		}
64	}
65	if (addrp != NULL)
66		*addrp = addr;
67	if (sizep != NULL)
68		*sizep = size;
69	if (pcip != NULL)
70		*pcip = pci;
71}
72
73int
74ofw_reg_to_paddr(phandle_t dev, int regno, bus_addr_t *paddr,
75    bus_size_t *psize, pcell_t *ppci_hi)
76{
77	pcell_t cell[32], pci_hi;
78	uint64_t addr, raddr, baddr;
79	uint64_t size, rsize;
80	uint32_t c, nbridge, naddr, nsize;
81	phandle_t bridge, parent;
82	u_int spc, rspc;
83	int pci, pcib, res;
84
85	/* Sanity checking. */
86	if (dev == 0)
87		return (EINVAL);
88	bridge = OF_parent(dev);
89	if (bridge == 0)
90		return (EINVAL);
91	if (regno < 0)
92		return (EINVAL);
93	if (paddr == NULL || psize == NULL)
94		return (EINVAL);
95
96	get_addr_props(bridge, &naddr, &nsize, &pci);
97	res = OF_getencprop(dev, (pci) ? "assigned-addresses" : "reg",
98	    cell, sizeof(cell));
99	if (res == -1)
100		return (ENXIO);
101	if (res % sizeof(cell[0]))
102		return (ENXIO);
103	res /= sizeof(cell[0]);
104	regno *= naddr + nsize;
105	if (regno + naddr + nsize > res)
106		return (EINVAL);
107	pci_hi = pci ? cell[regno] : OFW_PADDR_NOT_PCI;
108	spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK;
109	addr = 0;
110	for (c = 0; c < naddr; c++)
111		addr = ((uint64_t)addr << 32) | cell[regno++];
112	size = 0;
113	for (c = 0; c < nsize; c++)
114		size = ((uint64_t)size << 32) | cell[regno++];
115	/*
116	 * Map the address range in the bridge's decoding window as given
117	 * by the "ranges" property. If a node doesn't have such property
118	 * or the property is empty, we assume an identity mapping.  The
119	 * standard says a missing property indicates no possible mapping.
120	 * This code is more liberal since the intended use is to get a
121	 * console running early, and a printf to warn of malformed data
122	 * is probably futile before the console is fully set up.
123	 */
124	parent = OF_parent(bridge);
125	while (parent != 0) {
126		get_addr_props(parent, &nbridge, NULL, &pcib);
127		res = OF_getencprop(bridge, "ranges", cell, sizeof(cell));
128		if (res < 1)
129			goto next;
130		if (res % sizeof(cell[0]))
131			return (ENXIO);
132		/* Capture pci_hi if we just transitioned onto a PCI bus. */
133		if (pcib && pci_hi == OFW_PADDR_NOT_PCI) {
134			pci_hi = cell[0];
135			spc = pci_hi & OFW_PCI_PHYS_HI_SPACEMASK;
136		}
137		res /= sizeof(cell[0]);
138		regno = 0;
139		while (regno < res) {
140			rspc = (pci ? cell[regno] : OFW_PADDR_NOT_PCI) &
141			    OFW_PCI_PHYS_HI_SPACEMASK;
142			if (rspc != spc) {
143				regno += naddr + nbridge + nsize;
144				continue;
145			}
146			raddr = 0;
147			for (c = 0; c < naddr; c++)
148				raddr = ((uint64_t)raddr << 32) | cell[regno++];
149			rspc = (pcib)
150			    ? cell[regno] & OFW_PCI_PHYS_HI_SPACEMASK
151			    : OFW_PADDR_NOT_PCI;
152			baddr = 0;
153			for (c = 0; c < nbridge; c++)
154				baddr = ((uint64_t)baddr << 32) | cell[regno++];
155			rsize = 0;
156			for (c = 0; c < nsize; c++)
157				rsize = ((uint64_t)rsize << 32) | cell[regno++];
158			if (addr < raddr || addr >= raddr + rsize)
159				continue;
160			addr = addr - raddr + baddr;
161			if (rspc != OFW_PADDR_NOT_PCI)
162				spc = rspc;
163		}
164	next:
165		bridge = parent;
166		parent = OF_parent(bridge);
167		get_addr_props(bridge, &naddr, &nsize, &pci);
168	}
169
170	KASSERT(addr <= BUS_SPACE_MAXADDR,
171	    ("Bus sddress is too large: %jx", (intmax_t)addr));
172	KASSERT(size <= BUS_SPACE_MAXSIZE,
173	    ("Bus size is too large: %jx", (intmax_t)addr));
174
175	*paddr = addr;
176	*psize = size;
177	if (ppci_hi != NULL)
178		*ppci_hi = pci_hi;
179
180	return (0);
181}
182