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