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