xref: /freebsd/sys/dev/vnic/mrml_bridge.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/resource.h>
37 #include <sys/rman.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/fdt/simplebus.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 
48 static MALLOC_DEFINE(M_MRMLB, "MRML bridge", "Cavium MRML bridge");
49 
50 static device_probe_t mrmlb_fdt_probe;
51 static device_attach_t mrmlb_fdt_attach;
52 
53 static struct resource * mrmlb_ofw_bus_alloc_res(device_t, device_t, int, int *,
54     rman_res_t, rman_res_t, rman_res_t, u_int);
55 
56 static const struct ofw_bus_devinfo * mrmlb_ofw_get_devinfo(device_t, device_t);
57 
58 static device_method_t mrmlbus_fdt_methods[] = {
59 	/* Device interface */
60 	DEVMETHOD(device_probe,		mrmlb_fdt_probe),
61 	DEVMETHOD(device_attach,	mrmlb_fdt_attach),
62 
63 	/* Bus interface */
64 	DEVMETHOD(bus_alloc_resource,		mrmlb_ofw_bus_alloc_res),
65 	DEVMETHOD(bus_release_resource,		bus_generic_release_resource),
66 	DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
67 
68 	/* ofw_bus interface */
69 	DEVMETHOD(ofw_bus_get_devinfo,	mrmlb_ofw_get_devinfo),
70 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
71 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
72 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
73 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
74 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
75 
76 	DEVMETHOD_END
77 };
78 
79 DEFINE_CLASS_0(mrmlbus, mrmlbus_fdt_driver, mrmlbus_fdt_methods,
80     sizeof(struct simplebus_softc));
81 
82 EARLY_DRIVER_MODULE(mrmlbus, pcib, mrmlbus_fdt_driver, 0, 0,
83     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
84 MODULE_VERSION(mrmlbus, 1);
85 
86 static int mrmlb_ofw_fill_ranges(phandle_t, struct simplebus_softc *);
87 static int mrmlb_ofw_bus_attach(device_t);
88 
89 static int
90 mrmlb_fdt_probe(device_t dev)
91 {
92 
93 	if (!ofw_bus_status_okay(dev))
94 		return (ENXIO);
95 
96 	if (!ofw_bus_is_compatible(dev, "cavium,thunder-8890-mrml-bridge"))
97 		return (ENXIO);
98 
99 	device_set_desc(dev, "Cavium ThunderX MRML bridge");
100 	return (BUS_PROBE_SPECIFIC);
101 }
102 
103 static int
104 mrmlb_fdt_attach(device_t dev)
105 {
106 	int err;
107 
108 	err = mrmlb_ofw_bus_attach(dev);
109 	if (err != 0)
110 		return (err);
111 
112 	return (bus_generic_attach(dev));
113 }
114 
115 /* OFW bus interface */
116 struct mrmlb_ofw_devinfo {
117 	struct ofw_bus_devinfo	di_dinfo;
118 	struct resource_list	di_rl;
119 };
120 
121 static const struct ofw_bus_devinfo *
122 mrmlb_ofw_get_devinfo(device_t bus __unused, device_t child)
123 {
124 	struct mrmlb_ofw_devinfo *di;
125 
126 	di = device_get_ivars(child);
127 	return (&di->di_dinfo);
128 }
129 
130 static struct resource *
131 mrmlb_ofw_bus_alloc_res(device_t bus, device_t child, int type, int *rid,
132     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
133 {
134 	struct simplebus_softc *sc;
135 	struct mrmlb_ofw_devinfo *di;
136 	struct resource_list_entry *rle;
137 	int i;
138 
139 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
140 		if ((di = device_get_ivars(child)) == NULL)
141 			return (NULL);
142 		if (type == SYS_RES_IOPORT)
143 		    type = SYS_RES_MEMORY;
144 
145 		/* Find defaults for this rid */
146 		rle = resource_list_find(&di->di_rl, type, *rid);
147 		if (rle == NULL)
148 			return (NULL);
149 
150 		start = rle->start;
151 		end = rle->end;
152 		count = rle->count;
153 	}
154 
155 	sc = device_get_softc(bus);
156 
157 	if (type == SYS_RES_MEMORY) {
158 		/* Remap through ranges property */
159 		for (i = 0; i < sc->nranges; i++) {
160 			if (start >= sc->ranges[i].bus && end <
161 			    sc->ranges[i].bus + sc->ranges[i].size) {
162 				start -= sc->ranges[i].bus;
163 				start += sc->ranges[i].host;
164 				end -= sc->ranges[i].bus;
165 				end += sc->ranges[i].host;
166 				break;
167 			}
168 		}
169 
170 		if (i == sc->nranges && sc->nranges != 0) {
171 			device_printf(bus, "Could not map resource "
172 			    "%#lx-%#lx\n", start, end);
173 			return (NULL);
174 		}
175 	}
176 
177 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
178 	    count, flags));
179 }
180 
181 /* Helper functions */
182 
183 static int
184 mrmlb_ofw_fill_ranges(phandle_t node, struct simplebus_softc *sc)
185 {
186 	int host_address_cells;
187 	cell_t *base_ranges;
188 	ssize_t nbase_ranges;
189 	int err;
190 	int i, j, k;
191 
192 	err = OF_searchencprop(OF_parent(node), "#address-cells",
193 	    &host_address_cells, sizeof(host_address_cells));
194 	if (err <= 0)
195 		return (-1);
196 
197 	nbase_ranges = OF_getproplen(node, "ranges");
198 	if (nbase_ranges < 0)
199 		return (-1);
200 	sc->nranges = nbase_ranges / sizeof(cell_t) /
201 	    (sc->acells + host_address_cells + sc->scells);
202 	if (sc->nranges == 0)
203 		return (0);
204 
205 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
206 	    M_MRMLB, M_WAITOK);
207 	base_ranges = malloc(nbase_ranges, M_MRMLB, M_WAITOK);
208 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
209 
210 	for (i = 0, j = 0; i < sc->nranges; i++) {
211 		sc->ranges[i].bus = 0;
212 		for (k = 0; k < sc->acells; k++) {
213 			sc->ranges[i].bus <<= 32;
214 			sc->ranges[i].bus |= base_ranges[j++];
215 		}
216 		sc->ranges[i].host = 0;
217 		for (k = 0; k < host_address_cells; k++) {
218 			sc->ranges[i].host <<= 32;
219 			sc->ranges[i].host |= base_ranges[j++];
220 		}
221 		sc->ranges[i].size = 0;
222 		for (k = 0; k < sc->scells; k++) {
223 			sc->ranges[i].size <<= 32;
224 			sc->ranges[i].size |= base_ranges[j++];
225 		}
226 	}
227 
228 	free(base_ranges, M_MRMLB);
229 	return (sc->nranges);
230 }
231 
232 static int
233 mrmlb_ofw_bus_attach(device_t dev)
234 {
235 	struct simplebus_softc *sc;
236 	struct mrmlb_ofw_devinfo *di;
237 	device_t child;
238 	phandle_t parent, node;
239 
240 	parent = ofw_bus_get_node(dev);
241 	simplebus_init(dev, parent);
242 
243 	sc = device_get_softc(dev);
244 
245 	if (mrmlb_ofw_fill_ranges(parent, sc) < 0) {
246 		device_printf(dev, "could not get ranges\n");
247 		return (ENXIO);
248 	}
249 	/* Iterate through all bus subordinates */
250 	for (node = OF_child(parent); node > 0; node = OF_peer(node)) {
251 		/* Allocate and populate devinfo. */
252 		di = malloc(sizeof(*di), M_MRMLB, M_WAITOK | M_ZERO);
253 		if (ofw_bus_gen_setup_devinfo(&di->di_dinfo, node) != 0) {
254 			free(di, M_MRMLB);
255 			continue;
256 		}
257 
258 		/* Initialize and populate resource list. */
259 		resource_list_init(&di->di_rl);
260 		ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells,
261 		    &di->di_rl);
262 		ofw_bus_intr_to_rl(dev, node, &di->di_rl, NULL);
263 
264 		/* Add newbus device for this FDT node */
265 		child = device_add_child(dev, NULL, -1);
266 		if (child == NULL) {
267 			resource_list_free(&di->di_rl);
268 			ofw_bus_gen_destroy_devinfo(&di->di_dinfo);
269 			free(di, M_MRMLB);
270 			continue;
271 		}
272 
273 		device_set_ivars(child, di);
274 	}
275 
276 	return (0);
277 }
278