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