vdevice.c revision 255643
1/*-
2 * Copyright (c) 2011 Nathan Whitehorn
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 <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/powerpc/pseries/vdevice.c 255643 2013-09-17 17:37:04Z nwhitehorn $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/malloc.h>
35#include <sys/bus.h>
36#include <sys/cpu.h>
37#include <machine/bus.h>
38#include <machine/intr_machdep.h>
39
40#include <dev/ofw/openfirm.h>
41#include <dev/ofw/ofw_bus.h>
42#include <dev/ofw/ofw_bus_subr.h>
43
44static int	vdevice_probe(device_t);
45static int	vdevice_attach(device_t);
46static const struct ofw_bus_devinfo *vdevice_get_devinfo(device_t dev,
47    device_t child);
48static int	vdevice_print_child(device_t dev, device_t child);
49static struct resource_list *vdevice_get_resource_list (device_t, device_t);
50
51/*
52 * VDevice devinfo
53 */
54struct vdevice_devinfo {
55	struct ofw_bus_devinfo mdi_obdinfo;
56	struct resource_list mdi_resources;
57};
58
59static device_method_t vdevice_methods[] = {
60	/* Device interface */
61	DEVMETHOD(device_probe,		vdevice_probe),
62	DEVMETHOD(device_attach,	vdevice_attach),
63
64	/* Bus interface */
65	DEVMETHOD(bus_add_child,	bus_generic_add_child),
66	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
67	DEVMETHOD(bus_print_child,	vdevice_print_child),
68	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
69	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
70	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
71	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
72	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
73	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
74	DEVMETHOD(bus_get_resource_list, vdevice_get_resource_list),
75
76	/* ofw_bus interface */
77	DEVMETHOD(ofw_bus_get_devinfo,	vdevice_get_devinfo),
78	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
79	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
80	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
81	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
82	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
83
84	DEVMETHOD_END
85};
86
87static driver_t vdevice_driver = {
88	"vdevice",
89	vdevice_methods,
90	0
91};
92
93static devclass_t vdevice_devclass;
94
95DRIVER_MODULE(vdevice, nexus, vdevice_driver, vdevice_devclass, 0, 0);
96
97static int
98vdevice_probe(device_t dev)
99{
100	const char	*name;
101
102	name = ofw_bus_get_name(dev);
103
104	if (name == NULL || strcmp(name, "vdevice") != 0)
105		return (ENXIO);
106
107	if (!ofw_bus_is_compatible(dev, "IBM,vdevice"))
108		return (ENXIO);
109
110	device_set_desc(dev, "POWER Hypervisor Virtual Device Root");
111
112	return (0);
113}
114
115static int
116vdevice_attach(device_t dev)
117{
118	phandle_t root, child;
119	device_t cdev;
120	int icells, i, nintr, *intr;
121	phandle_t iparent;
122	struct vdevice_devinfo *dinfo;
123
124	root = ofw_bus_get_node(dev);
125
126	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
127		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
128
129                if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo,
130		    child) != 0) {
131                        free(dinfo, M_DEVBUF);
132                        continue;
133                }
134		resource_list_init(&dinfo->mdi_resources);
135
136		if (OF_searchprop(child, "#interrupt-cells", &icells,
137		    sizeof(icells)) <= 0)
138			icells = 2;
139		if (OF_getprop(child, "interrupt-parent", &iparent,
140		    sizeof(iparent)) <= 0)
141			iparent = -1;
142		nintr = OF_getprop_alloc(child, "interrupts", sizeof(*intr),
143		    (void **)&intr);
144		if (nintr > 0) {
145			for (i = 0; i < nintr; i += icells) {
146				u_int irq = intr[i];
147				if (iparent != -1)
148					irq = MAP_IRQ(iparent, intr[i]);
149
150				resource_list_add(&dinfo->mdi_resources,
151				    SYS_RES_IRQ, i, irq, irq, i);
152			}
153		}
154
155                cdev = device_add_child(dev, NULL, -1);
156                if (cdev == NULL) {
157                        device_printf(dev, "<%s>: device_add_child failed\n",
158                            dinfo->mdi_obdinfo.obd_name);
159                        ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
160                        free(dinfo, M_DEVBUF);
161                        continue;
162                }
163		device_set_ivars(cdev, dinfo);
164	}
165
166	return (bus_generic_attach(dev));
167}
168
169static const struct ofw_bus_devinfo *
170vdevice_get_devinfo(device_t dev, device_t child)
171{
172	return (device_get_ivars(child));
173}
174
175static int
176vdevice_print_child(device_t dev, device_t child)
177{
178	struct vdevice_devinfo *dinfo;
179	struct resource_list *rl;
180	int retval = 0;
181
182	dinfo = device_get_ivars(child);
183	rl = &dinfo->mdi_resources;
184
185	retval += bus_print_child_header(dev, child);
186
187	retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
188
189	retval += bus_print_child_footer(dev, child);
190
191	return (retval);
192}
193
194static struct resource_list *
195vdevice_get_resource_list (device_t dev, device_t child)
196{
197        struct vdevice_devinfo *dinfo;
198
199        dinfo = device_get_ivars(child);
200        return (&dinfo->mdi_resources);
201}
202
203