xref: /freebsd/sys/dev/vnic/mrml_bridge.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*
2  * Copyright (C) 2016 Cavium Inc.
3  * All rights reserved.
4  *
5  * Developed by Semihalf.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include "opt_platform.h"
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/resource.h>
39 #include <sys/rman.h>
40 #include <sys/socket.h>
41 #include <sys/queue.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/fdt/simplebus.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 
50 static MALLOC_DEFINE(M_MRMLB, "MRML bridge", "Cavium MRML bridge");
51 
52 static device_probe_t mrmlb_fdt_probe;
53 static device_attach_t mrmlb_fdt_attach;
54 
55 static struct resource * mrmlb_ofw_bus_alloc_res(device_t, device_t, int, int *,
56     rman_res_t, rman_res_t, rman_res_t, u_int);
57 
58 static const struct ofw_bus_devinfo * mrmlb_ofw_get_devinfo(device_t, device_t);
59 
60 static device_method_t mrmlbus_fdt_methods[] = {
61 	/* Device interface */
62 	DEVMETHOD(device_probe,		mrmlb_fdt_probe),
63 	DEVMETHOD(device_attach,	mrmlb_fdt_attach),
64 
65 	/* Bus interface */
66 	DEVMETHOD(bus_alloc_resource,		mrmlb_ofw_bus_alloc_res),
67 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
68 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
69 
70 	/* ofw_bus interface */
71 	DEVMETHOD(ofw_bus_get_devinfo,	mrmlb_ofw_get_devinfo),
72 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
73 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
74 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
75 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
76 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
77 
78 	DEVMETHOD_END
79 };
80 
81 DEFINE_CLASS_0(mrmlbus, mrmlbus_fdt_driver, mrmlbus_fdt_methods,
82     sizeof(struct simplebus_softc));
83 
84 EARLY_DRIVER_MODULE(mrmlbus, pcib, mrmlbus_fdt_driver, 0, 0,
85     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
86 MODULE_VERSION(mrmlbus, 1);
87 
88 static int mrmlb_ofw_fill_ranges(phandle_t, struct simplebus_softc *);
89 static int mrmlb_ofw_bus_attach(device_t);
90 
91 static int
92 mrmlb_fdt_probe(device_t dev)
93 {
94 
95 	if (!ofw_bus_status_okay(dev))
96 		return (ENXIO);
97 
98 	if (!ofw_bus_is_compatible(dev, "cavium,thunder-8890-mrml-bridge"))
99 		return (ENXIO);
100 
101 	device_set_desc(dev, "Cavium ThunderX MRML bridge");
102 	return (BUS_PROBE_SPECIFIC);
103 }
104 
105 static int
106 mrmlb_fdt_attach(device_t dev)
107 {
108 	int err;
109 
110 	err = mrmlb_ofw_bus_attach(dev);
111 	if (err != 0)
112 		return (err);
113 
114 	return (bus_generic_attach(dev));
115 }
116 
117 /* OFW bus interface */
118 struct mrmlb_ofw_devinfo {
119 	struct ofw_bus_devinfo	di_dinfo;
120 	struct resource_list	di_rl;
121 };
122 
123 static const struct ofw_bus_devinfo *
124 mrmlb_ofw_get_devinfo(device_t bus __unused, device_t child)
125 {
126 	struct mrmlb_ofw_devinfo *di;
127 
128 	di = device_get_ivars(child);
129 	return (&di->di_dinfo);
130 }
131 
132 static struct resource *
133 mrmlb_ofw_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
134     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
135 {
136 	struct simplebus_softc *sc;
137 	struct mrmlb_ofw_devinfo *di;
138 	struct resource_list_entry *rle;
139 	int i;
140 
141 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
142 		if ((di = device_get_ivars(child)) == NULL)
143 			return (NULL);
144 		if (type == SYS_RES_IOPORT)
145 		    type = SYS_RES_MEMORY;
146 
147 		/* Find defaults for this rid */
148 		rle = resource_list_find(&di->di_rl, type, *rid);
149 		if (rle == NULL)
150 			return (NULL);
151 
152 		start = rle->start;
153 		end = rle->end;
154 		count = rle->count;
155 	}
156 
157 	sc = device_get_softc(bus);
158 
159 	if (type == SYS_RES_MEMORY) {
160 		/* Remap through ranges property */
161 		for (i = 0; i < sc->nranges; i++) {
162 			if (start >= sc->ranges[i].bus && end <
163 			    sc->ranges[i].bus + sc->ranges[i].size) {
164 				start -= sc->ranges[i].bus;
165 				start += sc->ranges[i].host;
166 				end -= sc->ranges[i].bus;
167 				end += sc->ranges[i].host;
168 				break;
169 			}
170 		}
171 
172 		if (i == sc->nranges && sc->nranges != 0) {
173 			device_printf(bus, "Could not map resource "
174 			    "%#lx-%#lx\n", start, end);
175 			return (NULL);
176 		}
177 	}
178 
179 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
180 	    count, flags));
181 }
182 
183 /* Helper functions */
184 
185 static int
186 mrmlb_ofw_fill_ranges(phandle_t node, struct simplebus_softc *sc)
187 {
188 	int host_address_cells;
189 	cell_t *base_ranges;
190 	ssize_t nbase_ranges;
191 	int err;
192 	int i, j, k;
193 
194 	err = OF_searchencprop(OF_parent(node), "#address-cells",
195 	    &host_address_cells, sizeof(host_address_cells));
196 	if (err <= 0)
197 		return (-1);
198 
199 	nbase_ranges = OF_getproplen(node, "ranges");
200 	if (nbase_ranges < 0)
201 		return (-1);
202 	sc->nranges = nbase_ranges / sizeof(cell_t) /
203 	    (sc->acells + host_address_cells + sc->scells);
204 	if (sc->nranges == 0)
205 		return (0);
206 
207 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
208 	    M_MRMLB, M_WAITOK);
209 	base_ranges = malloc(nbase_ranges, M_MRMLB, M_WAITOK);
210 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
211 
212 	for (i = 0, j = 0; i < sc->nranges; i++) {
213 		sc->ranges[i].bus = 0;
214 		for (k = 0; k < sc->acells; k++) {
215 			sc->ranges[i].bus <<= 32;
216 			sc->ranges[i].bus |= base_ranges[j++];
217 		}
218 		sc->ranges[i].host = 0;
219 		for (k = 0; k < host_address_cells; k++) {
220 			sc->ranges[i].host <<= 32;
221 			sc->ranges[i].host |= base_ranges[j++];
222 		}
223 		sc->ranges[i].size = 0;
224 		for (k = 0; k < sc->scells; k++) {
225 			sc->ranges[i].size <<= 32;
226 			sc->ranges[i].size |= base_ranges[j++];
227 		}
228 	}
229 
230 	free(base_ranges, M_MRMLB);
231 	return (sc->nranges);
232 }
233 
234 static int
235 mrmlb_ofw_bus_attach(device_t dev)
236 {
237 	struct simplebus_softc *sc;
238 	struct mrmlb_ofw_devinfo *di;
239 	device_t child;
240 	phandle_t parent, node;
241 
242 	parent = ofw_bus_get_node(dev);
243 	simplebus_init(dev, parent);
244 
245 	sc = device_get_softc(dev);
246 
247 	if (mrmlb_ofw_fill_ranges(parent, sc) < 0) {
248 		device_printf(dev, "could not get ranges\n");
249 		return (ENXIO);
250 	}
251 	/* Iterate through all bus subordinates */
252 	for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
253 		/* Allocate and populate devinfo. */
254 		di = malloc(sizeof(*di), M_MRMLB, M_WAITOK | M_ZERO);
255 		if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {
256 			free(di, M_MRMLB);
257 			continue;
258 		}
259 
260 		/* Initialize and populate resource list. */
261 		resource_list_init(&di->di_rl);
262 		ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,
263 		    &di->di_rl);
264 		ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);
265 
266 		/* Add newbus device for this FDT node */
267 		child = device_add_child(dev, NULL, -1);
268 		if (child == NULL) {
269 			resource_list_free(&di->di_rl);
270 			ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
271 			free(di, M_MRMLB);
272 			continue;
273 		}
274 
275 		device_set_ivars(child, di);
276 	}
277 
278 	return (0);
279 }
280