xref: /freebsd/sys/dev/fdt/simplebus.c (revision 68e6422c6c91170a615e77028683a157e8e39d05)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 Nathan Whitehorn
5  * All rights reserved.
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 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/rman.h>
36 
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 
41 #include <dev/fdt/simplebus.h>
42 
43 #include "pci_if.h"
44 #include "pcib_if.h"
45 
46 /*
47  * Bus interface.
48  */
49 static int		simplebus_probe(device_t dev);
50 static struct resource *simplebus_alloc_resource(device_t, device_t, int,
51     int *, rman_res_t, rman_res_t, rman_res_t, u_int);
52 static void		simplebus_probe_nomatch(device_t bus, device_t child);
53 static int		simplebus_print_child(device_t bus, device_t child);
54 static device_t		simplebus_add_child(device_t dev, u_int order,
55     const char *name, int unit);
56 static struct resource_list *simplebus_get_resource_list(device_t bus,
57     device_t child);
58 
59 static ssize_t		simplebus_get_property(device_t bus, device_t child,
60     const char *propname, void *propvalue, size_t size,
61     device_property_type_t type);
62 /*
63  * ofw_bus interface
64  */
65 static const struct ofw_bus_devinfo *simplebus_get_devinfo(device_t bus,
66     device_t child);
67 
68 /*
69  * PCI interface for MSI interrupts
70  */
71 static pci_get_id_t simplebus_get_id;
72 static pci_alloc_msi_t simplebus_alloc_msi;
73 
74 /*
75  * PCIB interface
76  */
77 static pcib_alloc_msi_t simplebus_pcib_alloc_msi;
78 static pcib_release_msi_t simplebus_pcib_release_msi;
79 static pcib_alloc_msix_t simplebus_pcib_alloc_msix;
80 static pcib_release_msix_t simplebus_pcib_release_msix;
81 static pcib_map_msi_t simplebus_pcib_map_msi;
82 
83 /*
84  * Driver methods.
85  */
86 static device_method_t	simplebus_methods[] = {
87 	/* Device interface */
88 	DEVMETHOD(device_probe,		simplebus_probe),
89 	DEVMETHOD(device_attach,	simplebus_attach),
90 	DEVMETHOD(device_detach,	simplebus_detach),
91 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
92 	DEVMETHOD(device_suspend,	bus_generic_suspend),
93 	DEVMETHOD(device_resume,	bus_generic_resume),
94 
95 	/* Bus interface */
96 	DEVMETHOD(bus_add_child,	simplebus_add_child),
97 	DEVMETHOD(bus_print_child,	simplebus_print_child),
98 	DEVMETHOD(bus_probe_nomatch,	simplebus_probe_nomatch),
99 	DEVMETHOD(bus_read_ivar,	bus_generic_read_ivar),
100 	DEVMETHOD(bus_write_ivar,	bus_generic_write_ivar),
101 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
102 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
103 	DEVMETHOD(bus_alloc_resource,	simplebus_alloc_resource),
104 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
105 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
106 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
107 	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
108 	DEVMETHOD(bus_map_resource,	bus_generic_map_resource),
109 	DEVMETHOD(bus_unmap_resource,	bus_generic_unmap_resource),
110 	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
111 	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
112 	DEVMETHOD(bus_delete_resource,	bus_generic_rl_delete_resource),
113 	DEVMETHOD(bus_child_pnpinfo,	ofw_bus_gen_child_pnpinfo),
114 	DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
115 	DEVMETHOD(bus_get_property,	simplebus_get_property),
116 	DEVMETHOD(bus_get_device_path,  ofw_bus_gen_get_device_path),
117 
118 	/* ofw_bus interface */
119 	DEVMETHOD(ofw_bus_get_devinfo,	simplebus_get_devinfo),
120 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
121 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
122 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
123 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
124 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
125 
126 	/* PCI interface for MSI interrupts */
127 	DEVMETHOD(pci_get_id,		simplebus_get_id),
128 	DEVMETHOD(pci_alloc_msi,	simplebus_alloc_msi),
129 
130 	/* PCIB interface */
131 	DEVMETHOD(pcib_alloc_msi,	simplebus_pcib_alloc_msi),
132 	DEVMETHOD(pcib_release_msi,	simplebus_pcib_release_msi),
133 	DEVMETHOD(pcib_alloc_msix,	simplebus_pcib_alloc_msix),
134 	DEVMETHOD(pcib_release_msix,	simplebus_pcib_release_msix),
135 	DEVMETHOD(pcib_map_msi,		simplebus_pcib_map_msi),
136 
137 	DEVMETHOD_END
138 };
139 
140 DEFINE_CLASS_0(simplebus, simplebus_driver, simplebus_methods,
141     sizeof(struct simplebus_softc));
142 
143 EARLY_DRIVER_MODULE(simplebus, ofwbus, simplebus_driver, 0, 0, BUS_PASS_BUS);
144 EARLY_DRIVER_MODULE(simplebus, simplebus, simplebus_driver, 0, 0,
145     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
146 
147 static int
simplebus_probe(device_t dev)148 simplebus_probe(device_t dev)
149 {
150 
151 	if (!ofw_bus_status_okay(dev))
152 		return (ENXIO);
153 	/*
154 	 * XXX We should attach only to pure' compatible = "simple-bus"',
155 	 * without any other compatible string.
156 	 * For now, filter only know cases:
157 	 * "syscon", "simple-bus"; is handled by fdt/syscon driver
158 	 * "simple-mfd", "simple-bus"; is handled by fdt/simple-mfd driver
159 	 */
160 	if (ofw_bus_is_compatible(dev, "syscon") ||
161 	    ofw_bus_is_compatible(dev, "simple-mfd"))
162 		return (ENXIO);
163 
164 	/*
165 	 * FDT data puts a "simple-bus" compatible string on many things that
166 	 * have children but aren't really buses in our world.  Without a
167 	 * ranges property we will fail to attach, so just fail to probe too.
168 	 */
169 	if (!(ofw_bus_is_compatible(dev, "simple-bus") &&
170 	    ofw_bus_has_prop(dev, "ranges")) &&
171 	    (ofw_bus_get_type(dev) == NULL || strcmp(ofw_bus_get_type(dev),
172 	     "soc") != 0))
173 		return (ENXIO);
174 
175 	device_set_desc(dev, "Flattened device tree simple bus");
176 
177 	return (BUS_PROBE_GENERIC);
178 }
179 
180 int
simplebus_attach_impl(device_t dev)181 simplebus_attach_impl(device_t dev)
182 {
183 	struct		simplebus_softc *sc;
184 	phandle_t	node;
185 
186 	sc = device_get_softc(dev);
187 	simplebus_init(dev, 0);
188 	if ((sc->flags & SB_FLAG_NO_RANGES) == 0 &&
189 	    simplebus_fill_ranges(sc->node, sc) < 0) {
190 		device_printf(dev, "could not get ranges\n");
191 		return (ENXIO);
192 	}
193 
194 	/*
195 	 * In principle, simplebus could have an interrupt map, but ignore that
196 	 * for now
197 	 */
198 
199 	for (node = OF_child(sc->node); node > 0; node = OF_peer(node))
200 		simplebus_add_device(dev, node, 0, NULL, -1, NULL);
201 
202 	return (0);
203 }
204 
205 int
simplebus_attach(device_t dev)206 simplebus_attach(device_t dev)
207 {
208 	int	rv;
209 
210 	rv = simplebus_attach_impl(dev);
211 	if (rv != 0)
212 		return (rv);
213 
214 	bus_attach_children(dev);
215 	return (0);
216 }
217 
218 int
simplebus_detach(device_t dev)219 simplebus_detach(device_t dev)
220 {
221 	struct		simplebus_softc *sc;
222 	int	rv;
223 
224 	rv = bus_generic_detach(dev);
225 	if (rv != 0)
226 		return (rv);
227 
228 	sc = device_get_softc(dev);
229 	if (sc->ranges != NULL)
230 		free(sc->ranges, M_DEVBUF);
231 
232 	return (0);
233 }
234 
235 void
simplebus_init(device_t dev,phandle_t node)236 simplebus_init(device_t dev, phandle_t node)
237 {
238 	struct simplebus_softc *sc;
239 
240 	sc = device_get_softc(dev);
241 	if (node == 0)
242 		node = ofw_bus_get_node(dev);
243 	sc->dev = dev;
244 	sc->node = node;
245 
246 	/*
247 	 * Some important numbers
248 	 */
249 	sc->acells = 2;
250 	OF_getencprop(node, "#address-cells", &sc->acells, sizeof(sc->acells));
251 	sc->scells = 1;
252 	OF_getencprop(node, "#size-cells", &sc->scells, sizeof(sc->scells));
253 }
254 
255 int
simplebus_fill_ranges(phandle_t node,struct simplebus_softc * sc)256 simplebus_fill_ranges(phandle_t node, struct simplebus_softc *sc)
257 {
258 	int host_address_cells;
259 	cell_t *base_ranges;
260 	ssize_t nbase_ranges;
261 	int err;
262 	int i, j, k;
263 
264 	err = OF_searchencprop(OF_parent(node), "#address-cells",
265 	    &host_address_cells, sizeof(host_address_cells));
266 	if (err <= 0)
267 		return (-1);
268 
269 	nbase_ranges = OF_getproplen(node, "ranges");
270 	if (nbase_ranges < 0)
271 		return (-1);
272 	sc->nranges = nbase_ranges / sizeof(cell_t) /
273 	    (sc->acells + host_address_cells + sc->scells);
274 	if (sc->nranges == 0)
275 		return (0);
276 
277 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
278 	    M_DEVBUF, M_WAITOK);
279 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
280 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
281 
282 	for (i = 0, j = 0; i < sc->nranges; i++) {
283 		sc->ranges[i].bus = 0;
284 		for (k = 0; k < sc->acells; k++) {
285 			sc->ranges[i].bus <<= 32;
286 			sc->ranges[i].bus |= base_ranges[j++];
287 		}
288 		sc->ranges[i].host = 0;
289 		for (k = 0; k < host_address_cells; k++) {
290 			sc->ranges[i].host <<= 32;
291 			sc->ranges[i].host |= base_ranges[j++];
292 		}
293 		sc->ranges[i].size = 0;
294 		for (k = 0; k < sc->scells; k++) {
295 			sc->ranges[i].size <<= 32;
296 			sc->ranges[i].size |= base_ranges[j++];
297 		}
298 	}
299 
300 	free(base_ranges, M_DEVBUF);
301 	return (sc->nranges);
302 }
303 
304 struct simplebus_devinfo *
simplebus_setup_dinfo(device_t dev,phandle_t node,struct simplebus_devinfo * di)305 simplebus_setup_dinfo(device_t dev, phandle_t node,
306     struct simplebus_devinfo *di)
307 {
308 	struct simplebus_softc *sc;
309 	struct simplebus_devinfo *ndi;
310 
311 	sc = device_get_softc(dev);
312 	if (di == NULL)
313 		ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
314 	else
315 		ndi = di;
316 	if (ofw_bus_gen_setup_devinfo(&ndi->obdinfo, node) != 0) {
317 		if (di == NULL)
318 			free(ndi, M_DEVBUF);
319 		return (NULL);
320 	}
321 
322 	resource_list_init(&ndi->rl);
323 	ofw_bus_reg_to_rl(dev, node, sc->acells, sc->scells, &ndi->rl);
324 	ofw_bus_intr_to_rl(dev, node, &ndi->rl, NULL);
325 
326 	return (ndi);
327 }
328 
329 device_t
simplebus_add_device(device_t dev,phandle_t node,u_int order,const char * name,int unit,struct simplebus_devinfo * di)330 simplebus_add_device(device_t dev, phandle_t node, u_int order,
331     const char *name, int unit, struct simplebus_devinfo *di)
332 {
333 	struct simplebus_devinfo *ndi;
334 	device_t cdev;
335 
336 	if ((ndi = simplebus_setup_dinfo(dev, node, di)) == NULL)
337 		return (NULL);
338 	cdev = device_add_child_ordered(dev, order, name, unit);
339 	if (cdev == NULL) {
340 		device_printf(dev, "<%s>: device_add_child failed\n",
341 		    ndi->obdinfo.obd_name);
342 		resource_list_free(&ndi->rl);
343 		ofw_bus_gen_destroy_devinfo(&ndi->obdinfo);
344 		if (di == NULL)
345 			free(ndi, M_DEVBUF);
346 		return (NULL);
347 	}
348 	device_set_ivars(cdev, ndi);
349 
350 	return(cdev);
351 }
352 
353 static device_t
simplebus_add_child(device_t dev,u_int order,const char * name,int unit)354 simplebus_add_child(device_t dev, u_int order, const char *name, int unit)
355 {
356 	device_t cdev;
357 	struct simplebus_devinfo *ndi;
358 
359 	cdev = device_add_child_ordered(dev, order, name, unit);
360 	if (cdev == NULL)
361 		return (NULL);
362 
363 	ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
364 	ndi->obdinfo.obd_node = -1;
365 	resource_list_init(&ndi->rl);
366 	device_set_ivars(cdev, ndi);
367 
368 	return (cdev);
369 }
370 
371 static const struct ofw_bus_devinfo *
simplebus_get_devinfo(device_t bus __unused,device_t child)372 simplebus_get_devinfo(device_t bus __unused, device_t child)
373 {
374         struct simplebus_devinfo *ndi;
375 
376         ndi = device_get_ivars(child);
377 	if (ndi == NULL)
378 		return (NULL);
379         return (&ndi->obdinfo);
380 }
381 
382 static struct resource_list *
simplebus_get_resource_list(device_t bus __unused,device_t child)383 simplebus_get_resource_list(device_t bus __unused, device_t child)
384 {
385 	struct simplebus_devinfo *ndi;
386 
387 	ndi = device_get_ivars(child);
388 	if (ndi == NULL)
389 		return (NULL);
390 	return (&ndi->rl);
391 }
392 
393 static ssize_t
simplebus_get_property(device_t bus,device_t child,const char * propname,void * propvalue,size_t size,device_property_type_t type)394 simplebus_get_property(device_t bus, device_t child, const char *propname,
395     void *propvalue, size_t size, device_property_type_t type)
396 {
397 	phandle_t node, xref;
398 	ssize_t ret, i;
399 	uint32_t *buffer;
400 	uint64_t val;
401 
402 	switch (type) {
403 	case DEVICE_PROP_ANY:
404 	case DEVICE_PROP_BUFFER:
405 	case DEVICE_PROP_UINT32:
406 	case DEVICE_PROP_UINT64:
407 	case DEVICE_PROP_HANDLE:
408 		break;
409 	default:
410 		return (-1);
411 	}
412 
413 	node = ofw_bus_get_node(child);
414 	if (propvalue == NULL || size == 0)
415 		return (OF_getproplen(node, propname));
416 
417 	/*
418 	 * Integer values are stored in BE format.
419 	 * If caller declared that the underlying property type is uint32_t
420 	 * we need to do the conversion to match host endianness.
421 	 */
422 	if (type == DEVICE_PROP_UINT32)
423 		return (OF_getencprop(node, propname, propvalue, size));
424 
425 	/*
426 	 * uint64_t also requires endianness handling.
427 	 * In FDT every 8 byte value is stored using two uint32_t variables
428 	 * in BE format. Now, since the upper bits are stored as the first
429 	 * of the pair, both halves require swapping.
430 	 */
431 	 if (type == DEVICE_PROP_UINT64) {
432 		ret = OF_getencprop(node, propname, propvalue, size);
433 		if (ret <= 0) {
434 			return (ret);
435 		}
436 
437 		buffer = (uint32_t *)propvalue;
438 
439 		for (i = 0; i < size / 4; i += 2) {
440 			val = (uint64_t)buffer[i] << 32 | buffer[i + 1];
441 			((uint64_t *)buffer)[i / 2] = val;
442 		}
443 		return (ret);
444 	}
445 
446 	if (type == DEVICE_PROP_HANDLE) {
447 		if (size < sizeof(node))
448 			return (-1);
449 		ret = OF_getencprop(node, propname, &xref, sizeof(xref));
450 		if (ret <= 0)
451 			return (ret);
452 
453 		node = OF_node_from_xref(xref);
454 		if (propvalue != NULL)
455 			*(uint32_t *)propvalue = node;
456 		return (ret);
457 	}
458 
459 	return (OF_getprop(node, propname, propvalue, size));
460 }
461 
462 static struct resource *
simplebus_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)463 simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
464     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
465 {
466 	struct simplebus_softc *sc;
467 	struct simplebus_devinfo *di;
468 	struct resource_list_entry *rle;
469 	int j;
470 
471 	sc = device_get_softc(bus);
472 
473 	/*
474 	 * Request for the default allocation with a given rid: use resource
475 	 * list stored in the local device info.
476 	 */
477 	if (RMAN_IS_DEFAULT_RANGE(start, end)) {
478 		if ((di = device_get_ivars(child)) == NULL)
479 			return (NULL);
480 
481 		rle = resource_list_find(&di->rl, type, *rid);
482 		if (rle == NULL) {
483 			if (bootverbose)
484 				device_printf(bus, "no default resources for "
485 				    "rid = %d, type = %d\n", *rid, type);
486 			return (NULL);
487 		}
488 		start = rle->start;
489 		end = rle->end;
490 		count = rle->count;
491         }
492 
493 	if (type == SYS_RES_MEMORY) {
494 		/* Remap through ranges property */
495 		for (j = 0; j < sc->nranges; j++) {
496 			if (start >= sc->ranges[j].bus && end <
497 			    sc->ranges[j].bus + sc->ranges[j].size) {
498 				start -= sc->ranges[j].bus;
499 				start += sc->ranges[j].host;
500 				end -= sc->ranges[j].bus;
501 				end += sc->ranges[j].host;
502 				break;
503 			}
504 		}
505 		if (j == sc->nranges && sc->nranges != 0) {
506 			if (bootverbose)
507 				device_printf(bus, "Could not map resource "
508 				    "%#jx-%#jx\n", start, end);
509 
510 			return (NULL);
511 		}
512 	}
513 
514 	return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
515 	    count, flags));
516 }
517 
518 static int
simplebus_print_res(struct simplebus_devinfo * di)519 simplebus_print_res(struct simplebus_devinfo *di)
520 {
521 	int rv;
522 
523 	if (di == NULL)
524 		return (0);
525 	rv = 0;
526 	rv += resource_list_print_type(&di->rl, "mem", SYS_RES_MEMORY, "%#jx");
527 	rv += resource_list_print_type(&di->rl, "irq", SYS_RES_IRQ, "%jd");
528 	return (rv);
529 }
530 
531 static void
simplebus_probe_nomatch(device_t bus,device_t child)532 simplebus_probe_nomatch(device_t bus, device_t child)
533 {
534 	const char *name, *type, *compat;
535 
536 	if (!bootverbose)
537 		return;
538 
539 	compat = ofw_bus_get_compat(child);
540 	if (compat == NULL)
541 		return;
542 	name = ofw_bus_get_name(child);
543 	type = ofw_bus_get_type(child);
544 
545 	device_printf(bus, "<%s>", name != NULL ? name : "unknown");
546 	simplebus_print_res(device_get_ivars(child));
547 	if (!ofw_bus_status_okay(child))
548 		printf(" disabled");
549 	if (type)
550 		printf(" type %s", type);
551 	printf(" compat %s (no driver attached)\n", compat);
552 }
553 
554 static int
simplebus_print_child(device_t bus,device_t child)555 simplebus_print_child(device_t bus, device_t child)
556 {
557 	int rv;
558 
559 	rv = bus_print_child_header(bus, child);
560 	rv += simplebus_print_res(device_get_ivars(child));
561 	if (!ofw_bus_status_okay(child))
562 		rv += printf(" disabled");
563 	rv += bus_print_child_footer(bus, child);
564 	return (rv);
565 }
566 
567 static int
simplebus_get_id(device_t dev,device_t child,enum pci_id_type type,uintptr_t * id)568 simplebus_get_id(device_t dev, device_t child, enum pci_id_type type,
569     uintptr_t *id)
570 {
571 	phandle_t node, xref;
572 	pcell_t *cells;
573 	uintptr_t rid;
574 	int error, ncells;
575 
576 	if (type != PCI_ID_MSI)
577 		return (EINVAL);
578 
579 	node = ofw_bus_get_node(child);
580 	error = ofw_bus_parse_xref_list_alloc(node, "msi-parent", "#msi-cells",
581 	    0, &xref, &ncells, &cells);
582 	if (error != 0)
583 		return (error);
584 
585 	rid = 0;
586 	if (ncells > 0)
587 		rid = cells[0];
588 
589 	*id = rid;
590 	return (0);
591 }
592 
593 static int
simplebus_alloc_msi(device_t bus,device_t child,int * count)594 simplebus_alloc_msi(device_t bus, device_t child, int *count)
595 {
596 	struct simplebus_devinfo *ndi;
597 	struct resource_list_entry *rle;
598 	int error, i, irq_count, *irqs;
599 
600 	if (*count < 1)
601 		return (EINVAL);
602 
603 	ndi = device_get_ivars(child);
604 	if (ndi == NULL)
605 		return (ENXIO);
606 
607 	/* Only MSI or non-MSI for now */
608 	rle = resource_list_find(&ndi->rl, SYS_RES_IRQ, 0);
609 	if (rle != NULL && rle->res != NULL)
610 		return (ENXIO);
611 
612 	irq_count = *count;
613 	irqs = mallocarray(irq_count, sizeof(int), M_DEVBUF, M_WAITOK | M_ZERO);
614 
615 	error = PCIB_ALLOC_MSI(bus, child, irq_count, irq_count, irqs);
616 	if (error != 0)
617 		goto out;
618 
619 	for (i = 0; i < irq_count; i++) {
620 		error = bus_generic_rl_set_resource(bus, child, SYS_RES_IRQ,
621 		    i + 1, irqs[i], 1);
622 		if (error != 0)
623 			break;
624 	}
625 
626 	/* Clean up resources if something failed */
627 	if (error != 0) {
628 		for (int j = 0; j < i; j++) {
629 			bus_generic_rl_delete_resource(bus, child, SYS_RES_IRQ,
630 			    j + 1);
631 		}
632 	}
633 out:
634 	free(irqs, M_DEVBUF);
635 	return (error);
636 }
637 
638 static int
simplebus_pcib_alloc_msi(device_t dev,device_t child,int count,int maxcount,int * irqs)639 simplebus_pcib_alloc_msi(device_t dev, device_t child, int count, int maxcount,
640     int *irqs)
641 {
642 	return (PCIB_ALLOC_MSI(device_get_parent(dev), child, count, maxcount,
643 	    irqs));
644 }
645 
646 static int
simplebus_pcib_release_msi(device_t dev,device_t child,int count,int * irqs)647 simplebus_pcib_release_msi(device_t dev, device_t child, int count, int *irqs)
648 {
649 	return (PCIB_RELEASE_MSI(device_get_parent(dev), child, count, irqs));
650 }
651 
652 static int
simplebus_pcib_alloc_msix(device_t dev,device_t child,int * irq)653 simplebus_pcib_alloc_msix(device_t dev, device_t child, int *irq)
654 {
655 	return (PCIB_ALLOC_MSIX(device_get_parent(dev), child, irq));
656 }
657 
658 static int
simplebus_pcib_release_msix(device_t dev,device_t child,int irq)659 simplebus_pcib_release_msix(device_t dev, device_t child, int irq)
660 {
661 	return (PCIB_RELEASE_MSIX(device_get_parent(dev), child, irq));
662 }
663 
664 static int
simplebus_pcib_map_msi(device_t dev,device_t child,int irq,uint64_t * addr,uint32_t * data)665 simplebus_pcib_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
666     uint32_t *data)
667 {
668 	return (PCIB_MAP_MSI(device_get_parent(dev), child, irq, addr,
669 	    data));
670 }
671