2 * Copyright (c) 2006 by Marius Strobl <marius@FreeBSD.org>.
3 * Copyright (c) 2006 Kip Macy <kmacy@FreeBSD.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/ofw/openfirm.h>
44 #include <machine/bus.h>
45 #include <machine/hypervisorvar.h>
46 #include <machine/hv_api.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/nexusvar.h>
49 #include <machine/resource.h>
51 #include <machine/mdesc_bus.h>
52 #include <machine/cddl/mdesc.h>
53 #include <machine/cddl/mdesc_impl.h>
58 #define SUN4V_REG_SPEC2CFG_HDL(x) ((x >> 32) & ~(0xfull << 28))
60 static device_probe_t vnex_probe;
61 static device_attach_t vnex_attach;
62 static bus_print_child_t vnex_print_child;
63 static bus_add_child_t vnex_add_child;
64 static bus_probe_nomatch_t vnex_probe_nomatch;
65 static bus_setup_intr_t vnex_setup_intr;
66 static bus_teardown_intr_t vnex_teardown_intr;
67 static bus_get_resource_list_t vnex_get_resource_list;
68 static mdesc_bus_get_devinfo_t vnex_get_devinfo;
70 static struct vnex_devinfo * vnex_setup_dinfo(device_t, mde_cookie_t node);
71 static void vnex_destroy_dinfo(struct vnex_devinfo *);
72 static int vnex_print_res(struct vnex_devinfo *);
75 struct mdesc_bus_devinfo vndi_mbdinfo;
76 struct resource_list vndi_rl;
78 /* Some common properties. */
79 struct nexus_regs *vndi_reg;
84 struct rman sc_intr_rman;
85 struct rman sc_mem_rman;
88 static device_method_t vnex_methods[] = {
89 /* Device interface */
90 DEVMETHOD(device_probe, vnex_probe),
91 DEVMETHOD(device_attach, vnex_attach),
92 DEVMETHOD(device_detach, bus_generic_detach),
93 DEVMETHOD(device_shutdown, bus_generic_shutdown),
94 DEVMETHOD(device_suspend, bus_generic_suspend),
95 DEVMETHOD(device_resume, bus_generic_resume),
98 DEVMETHOD(bus_print_child, vnex_print_child),
99 DEVMETHOD(bus_probe_nomatch, vnex_probe_nomatch),
100 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
101 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
102 DEVMETHOD(bus_add_child, vnex_add_child),
103 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
104 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
105 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
106 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
107 DEVMETHOD(bus_setup_intr, vnex_setup_intr),
108 DEVMETHOD(bus_teardown_intr, vnex_teardown_intr),
109 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
110 DEVMETHOD(bus_get_resource_list, vnex_get_resource_list),
112 /* ofw_bus interface */
113 /* mdesc_bus interface */
114 DEVMETHOD(mdesc_bus_get_devinfo, vnex_get_devinfo),
115 DEVMETHOD(mdesc_bus_get_compat, mdesc_bus_gen_get_compat),
116 DEVMETHOD(mdesc_bus_get_name, mdesc_bus_gen_get_name),
117 DEVMETHOD(mdesc_bus_get_type, mdesc_bus_gen_get_type),
123 static driver_t vnex_driver = {
126 sizeof(struct vnex_softc),
130 static devclass_t vnex_devclass;
131 DRIVER_MODULE(vnex, nexus, vnex_driver, vnex_devclass, 0, 0);
135 vnex_probe(device_t dev)
137 if (strcmp(ofw_bus_get_name(dev), "virtual-devices"))
140 device_set_desc(dev, "virtual nexus device");
146 vnex_attach(device_t dev)
148 struct vnex_devinfo *vndi;
149 struct vnex_softc *sc;
152 mde_cookie_t rootnode, *listp = NULL;
153 int i, listsz, num_nodes, num_devices;
157 node = ofw_bus_get_node(dev);
159 panic("%s: ofw_bus_get_node failed.", __func__);
161 sc = device_get_softc(dev);
162 sc->sc_intr_rman.rm_type = RMAN_ARRAY;
163 sc->sc_intr_rman.rm_descr = "Interrupts";
164 sc->sc_mem_rman.rm_type = RMAN_ARRAY;
165 sc->sc_mem_rman.rm_descr = "Device Memory";
166 if (rman_init(&sc->sc_intr_rman) != 0 ||
167 rman_init(&sc->sc_mem_rman) != 0 ||
168 rman_manage_region(&sc->sc_intr_rman, 0, IV_MAX - 1) != 0 ||
169 rman_manage_region(&sc->sc_mem_rman, 0ULL, ~0ULL) != 0)
170 panic("%s: failed to set up rmans.", __func__);
172 if ((mdp = md_get()) == NULL)
175 num_nodes = md_node_count(mdp);
176 listsz = num_nodes * sizeof(mde_cookie_t);
177 listp = (mde_cookie_t *)malloc(listsz, M_DEVBUF, M_WAITOK);
178 rootnode = md_root_node(mdp);
181 * scan the machine description for virtual devices
183 num_devices = md_scan_dag(mdp, rootnode,
184 md_find_name(mdp, "virtual-device"),
185 md_find_name(mdp, "fwd"), listp);
187 for (i = 0; i < num_devices; i++) {
188 if ((vndi = vnex_setup_dinfo(dev, listp[i])) == NULL)
191 cdev = device_add_child(dev, NULL, -1);
193 device_printf(dev, "<%s>: device_add_child failed\n",
194 vndi->vndi_mbdinfo.mbd_name);
195 vnex_destroy_dinfo(vndi);
198 device_set_ivars(cdev, vndi);
200 bus_generic_attach(dev);
201 free(listp, M_DEVBUF);
207 vnex_add_child(device_t dev, u_int order, const char *name, int unit)
210 struct vnex_devinfo *vndi;
212 cdev = device_add_child_ordered(dev, order, name, unit);
216 vndi = malloc(sizeof(*vndi), M_DEVBUF, M_WAITOK | M_ZERO);
217 vndi->vndi_mbdinfo.mbd_name = strdup(name, M_OFWPROP);
218 resource_list_init(&vndi->vndi_rl);
219 device_set_ivars(cdev, vndi);
225 vnex_print_child(device_t dev, device_t child)
229 rv = bus_print_child_header(dev, child);
230 rv += vnex_print_res(device_get_ivars(child));
231 rv += bus_print_child_footer(dev, child);
236 vnex_probe_nomatch(device_t dev, device_t child)
240 device_printf(dev, "<%s>", mdesc_bus_get_name(child));
241 vnex_print_res(device_get_ivars(child));
242 type = mdesc_bus_get_type(child);
243 printf(" type %s (no driver attached)\n",
244 type != NULL ? type : "unknown");
249 vnex_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
250 driver_filter_t *filt,driver_intr_t *intr, void *arg, void **cookiep)
259 panic("%s: NULL interrupt resource!", __func__);
261 if ((error = bus_get_resource(dev, SYS_RES_MEMORY, 0, ®, &nreg)))
264 if ((error = bus_get_resource(child, SYS_RES_IRQ, 0, &ino, &nino)))
267 cfg = SUN4V_REG_SPEC2CFG_HDL(reg);
269 if (hv_intr_devino_to_sysino(cfg, (uint32_t)ino, &ihdl) != H_EOK) {
276 if (hv_intr_settarget(ihdl, cpuid) != H_EOK) {
281 if (hv_intr_setstate(ihdl, HV_INTR_IDLE_STATE) != H_EOK) {
286 if (hv_intr_setenabled(ihdl, HV_INTR_ENABLED) != H_EOK) {
291 if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
294 /* We depend here on rman_activate_resource() being idempotent. */
295 if ((error = rman_activate_resource(res)))
298 error = inthand_add(device_get_nameunit(child), ihdl,
299 filt, intr, arg, flags, cookiep);
301 printf("inthandler added\n");
308 vnex_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
311 inthand_remove(rman_get_start(r), ih);
315 static struct resource_list *
316 vnex_get_resource_list(device_t dev, device_t child)
318 struct vnex_devinfo *vndi;
320 vndi = device_get_ivars(child);
321 return (&vndi->vndi_rl);
324 static const struct mdesc_bus_devinfo *
325 vnex_get_devinfo(device_t dev, device_t child)
327 struct vnex_devinfo *vndi;
329 vndi = device_get_ivars(child);
330 return (&vndi->vndi_mbdinfo);
333 static struct vnex_devinfo *
334 vnex_setup_dinfo(device_t dev, mde_cookie_t node)
336 struct vnex_devinfo *vndi;
338 vndi = malloc(sizeof(*vndi), M_DEVBUF, M_WAITOK | M_ZERO);
339 if (mdesc_bus_gen_setup_devinfo(&vndi->vndi_mbdinfo, node) != 0) {
340 free(vndi, M_DEVBUF);
348 vnex_destroy_dinfo(struct vnex_devinfo *vndi)
351 resource_list_free(&vndi->vndi_rl);
352 mdesc_bus_gen_destroy_devinfo(&vndi->vndi_mbdinfo);
353 free(vndi, M_DEVBUF);
358 vnex_print_res(struct vnex_devinfo *vndi)
363 rv += resource_list_print_type(&vndi->vndi_rl, "mem", SYS_RES_MEMORY,
365 rv += resource_list_print_type(&vndi->vndi_rl, "irq", SYS_RES_IRQ,