1 /*-
2 * Copyright (c) 2011-2012 Semihalf.
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
27 #include "opt_platform.h"
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/bus.h>
33 #include <sys/module.h>
34 #include <sys/smp.h>
35
36 #include <machine/bus.h>
37
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/ofw_subr.h>
41
42 #include "qman.h"
43 #include "portals.h"
44
45 #define FQMAN_DEVSTR "Freescale Queue Manager"
46
47 static int qman_fdt_probe(device_t);
48
49 static device_method_t qman_methods[] = {
50 /* Device interface */
51 DEVMETHOD(device_probe, qman_fdt_probe),
52 DEVMETHOD(device_attach, qman_attach),
53 DEVMETHOD(device_detach, qman_detach),
54
55 DEVMETHOD(device_suspend, qman_suspend),
56 DEVMETHOD(device_resume, qman_resume),
57 DEVMETHOD(device_shutdown, qman_shutdown),
58
59 { 0, 0 }
60 };
61
62 static driver_t qman_driver = {
63 "qman",
64 qman_methods,
65 sizeof(struct qman_softc),
66 };
67
68 EARLY_DRIVER_MODULE(qman, simplebus, qman_driver, 0, 0, BUS_PASS_SUPPORTDEV);
69
70 static int
qman_fdt_probe(device_t dev)71 qman_fdt_probe(device_t dev)
72 {
73
74 if (!ofw_bus_is_compatible(dev, "fsl,qman"))
75 return (ENXIO);
76
77 device_set_desc(dev, FQMAN_DEVSTR);
78
79 return (BUS_PROBE_DEFAULT);
80 }
81
82 /*
83 * QMAN Portals
84 */
85 #define QMAN_PORT_DEVSTR "Freescale Queue Manager - Portals"
86
87 static device_probe_t qman_portals_fdt_probe;
88 static device_attach_t qman_portals_fdt_attach;
89
90 static device_method_t qm_portals_methods[] = {
91 /* Device interface */
92 DEVMETHOD(device_probe, qman_portals_fdt_probe),
93 DEVMETHOD(device_attach, qman_portals_fdt_attach),
94 DEVMETHOD(device_detach, qman_portals_detach),
95
96 { 0, 0 }
97 };
98
99 static driver_t qm_portals_driver = {
100 "qman-portals",
101 qm_portals_methods,
102 sizeof(struct dpaa_portals_softc),
103 };
104
105 EARLY_DRIVER_MODULE(qman_portals, ofwbus, qm_portals_driver, 0, 0,
106 BUS_PASS_BUS);
107
108 static void
get_addr_props(phandle_t node,uint32_t * addrp,uint32_t * sizep)109 get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep)
110 {
111
112 *addrp = 2;
113 *sizep = 1;
114 OF_getencprop(node, "#address-cells", addrp, sizeof(*addrp));
115 OF_getencprop(node, "#size-cells", sizep, sizeof(*sizep));
116 }
117
118 static int
qman_portals_fdt_probe(device_t dev)119 qman_portals_fdt_probe(device_t dev)
120 {
121 phandle_t node;
122
123 if (ofw_bus_is_compatible(dev, "simple-bus")) {
124 node = ofw_bus_get_node(dev);
125 for (node = OF_child(node); node > 0; node = OF_peer(node)) {
126 if (ofw_bus_node_is_compatible(node, "fsl,qman-portal"))
127 break;
128 }
129 if (node <= 0)
130 return (ENXIO);
131 } else if (!ofw_bus_is_compatible(dev, "fsl,qman-portals"))
132 return (ENXIO);
133
134 device_set_desc(dev, QMAN_PORT_DEVSTR);
135
136 return (BUS_PROBE_DEFAULT);
137 }
138
139 static phandle_t
qman_portal_find_cpu(int cpu)140 qman_portal_find_cpu(int cpu)
141 {
142 phandle_t node;
143 pcell_t reg;
144
145 node = OF_finddevice("/cpus");
146 if (node == -1)
147 return (-1);
148
149 for (node = OF_child(node); node != 0; node = OF_peer(node)) {
150 if (OF_getprop(node, "reg", ®, sizeof(reg)) <= 0)
151 continue;
152 if (reg == cpu)
153 return (node);
154 }
155 return (-1);
156 }
157
158 static int
qman_portals_fdt_attach(device_t dev)159 qman_portals_fdt_attach(device_t dev)
160 {
161 struct dpaa_portals_softc *sc;
162 phandle_t node, child, cpu_node;
163 vm_paddr_t portal_pa, portal_par_pa;
164 vm_size_t portal_size;
165 uint32_t addr, paddr, size;
166 ihandle_t cpu;
167 int cpu_num, cpus, intr_rid;
168 struct dpaa_portals_devinfo di;
169 struct ofw_bus_devinfo ofw_di = {};
170 cell_t *range;
171 int nrange;
172 int i;
173
174 cpus = 0;
175 sc = device_get_softc(dev);
176 sc->sc_dev = dev;
177
178 node = ofw_bus_get_node(dev);
179
180 /* Get this node's range */
181 get_addr_props(ofw_bus_get_node(device_get_parent(dev)), &paddr, &size);
182 get_addr_props(node, &addr, &size);
183
184 nrange = OF_getencprop_alloc_multi(node, "ranges",
185 sizeof(*range), (void **)&range);
186 if (nrange < addr + paddr + size)
187 return (ENXIO);
188 portal_pa = portal_par_pa = 0;
189 portal_size = 0;
190 for (i = 0; i < addr; i++) {
191 portal_pa <<= 32;
192 portal_pa |= range[i];
193 }
194 for (; i < paddr + addr; i++) {
195 portal_par_pa <<= 32;
196 portal_par_pa |= range[i];
197 }
198 portal_pa += portal_par_pa;
199 for (; i < size + paddr + addr; i++) {
200 portal_size = (uintmax_t)portal_size << 32;
201 portal_size |= range[i];
202 }
203 OF_prop_free(range);
204 sc->sc_dp_size = portal_size;
205 sc->sc_dp_pa = portal_pa;
206
207 /* Find portals tied to CPUs */
208 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
209 if (cpus >= mp_ncpus)
210 break;
211 if (!ofw_bus_node_is_compatible(child, "fsl,qman-portal")) {
212 continue;
213 }
214 /* Checkout related cpu */
215 if (OF_getprop(child, "cpu-handle", (void *)&cpu,
216 sizeof(cpu)) <= 0) {
217 cpu = qman_portal_find_cpu(cpus);
218 if (cpu <= 0)
219 continue;
220 }
221 /* Acquire cpu number */
222 cpu_node = OF_instance_to_package(cpu);
223 if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) {
224 device_printf(dev, "Could not retrieve CPU number.\n");
225 return (ENXIO);
226 }
227
228 cpus++;
229
230 if (ofw_bus_gen_setup_devinfo(&ofw_di, child) != 0) {
231 device_printf(dev, "could not set up devinfo\n");
232 continue;
233 }
234
235 resource_list_init(&di.di_res);
236 if (ofw_bus_reg_to_rl(dev, child, addr, size, &di.di_res)) {
237 device_printf(dev, "%s: could not process 'reg' "
238 "property\n", ofw_di.obd_name);
239 ofw_bus_gen_destroy_devinfo(&ofw_di);
240 continue;
241 }
242 if (ofw_bus_intr_to_rl(dev, child, &di.di_res, &intr_rid)) {
243 device_printf(dev, "%s: could not process "
244 "'interrupts' property\n", ofw_di.obd_name);
245 resource_list_free(&di.di_res);
246 ofw_bus_gen_destroy_devinfo(&ofw_di);
247 continue;
248 }
249 di.di_intr_rid = intr_rid;
250
251 if (dpaa_portal_alloc_res(dev, &di, cpu_num))
252 goto err;
253 }
254
255 ofw_bus_gen_destroy_devinfo(&ofw_di);
256
257 return (qman_portals_attach(dev));
258 err:
259 resource_list_free(&di.di_res);
260 ofw_bus_gen_destroy_devinfo(&ofw_di);
261 qman_portals_detach(dev);
262 return (ENXIO);
263 }
264