Changeset View
Standalone View
sys/powerpc/pseries/rtas_dev.c
| Show All 31 Lines | |||||
| #include <sys/bus.h> | #include <sys/bus.h> | ||||
| #include <sys/conf.h> | #include <sys/conf.h> | ||||
| #include <sys/clock.h> | #include <sys/clock.h> | ||||
| #include <sys/cpu.h> | #include <sys/cpu.h> | ||||
| #include <sys/eventhandler.h> | #include <sys/eventhandler.h> | ||||
| #include <sys/kernel.h> | #include <sys/kernel.h> | ||||
| #include <sys/reboot.h> | #include <sys/reboot.h> | ||||
| #include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
| #include <sys/taskqueue.h> | |||||
| #include <vm/vm.h> | |||||
| #include <vm/pmap.h> | |||||
| #include <dev/ofw/ofw_bus.h> | #include <dev/ofw/ofw_bus.h> | ||||
| #include <dev/ofw/ofw_bus_subr.h> | |||||
| #include <dev/ofw/openfirm.h> | #include <dev/ofw/openfirm.h> | ||||
| #include <machine/bus.h> | |||||
| #include <machine/intr_machdep.h> | |||||
| #include <machine/rtas.h> | #include <machine/rtas.h> | ||||
| #include "clock_if.h" | #include "clock_if.h" | ||||
| //#define RTAS_DEBUG | |||||
| #ifdef RTAS_DEBUG | |||||
| #define RTAS_DEVPRINTF(dev, msg, ...) device_printf(dev, msg, ##__VA_ARGS__) | |||||
| #define RTAS_DPRINTF(msg, ...) printf(msg, ##__VA_ARGS__) | |||||
| #else | |||||
| #define RTAS_DEVPRINTF(dev, msg, ...) | |||||
| #define RTAS_DPRINTF(msg, ...) | |||||
| #endif | |||||
| static int rtasdev_probe(device_t); | static int rtasdev_probe(device_t); | ||||
| static int rtasdev_attach(device_t); | static int rtasdev_attach(device_t); | ||||
| static void rtasdev_find_children(device_t); | |||||
| /* clock interface */ | /* clock interface */ | ||||
| static int rtas_gettime(device_t dev, struct timespec *ts); | static int rtas_gettime(device_t dev, struct timespec *ts); | ||||
| static int rtas_settime(device_t dev, struct timespec *ts); | static int rtas_settime(device_t dev, struct timespec *ts); | ||||
| /* bus interface */ | |||||
| static const struct ofw_bus_devinfo * | |||||
| rtasbus_get_devinfo(device_t dev, device_t child); | |||||
| static struct resource * | |||||
| rtasbus_alloc_resource(device_t, device_t, int, int, rman_res_t, | |||||
| rman_res_t, rman_res_t, u_int); | |||||
| static int rtasbus_print_child(device_t dev, device_t child); | |||||
| static struct resource_list * | |||||
| rtasbus_get_resource_list(device_t dev, device_t child); | |||||
| static void rtas_shutdown(void *arg, int howto); | static void rtas_shutdown(void *arg, int howto); | ||||
| static void rtas_handle_event(const void *buf, size_t buflen); | |||||
| static bool rtas_handle_epow(const struct rtas_event_v6 *); | |||||
| /* | |||||
| * The root rtas node. | |||||
| */ | |||||
| struct rtas_devinfo { | |||||
| struct ofw_bus_devinfo rdi_obdinfo; | |||||
| struct resource_list rdi_resources; | |||||
| int rdi_base_irq; | |||||
| }; | |||||
| static device_method_t rtasdev_methods[] = { | static device_method_t rtasdev_methods[] = { | ||||
| /* Device interface */ | /* Device interface */ | ||||
| DEVMETHOD(device_probe, rtasdev_probe), | DEVMETHOD(device_probe, rtasdev_probe), | ||||
| DEVMETHOD(device_attach, rtasdev_attach), | DEVMETHOD(device_attach, rtasdev_attach), | ||||
| /* clock interface */ | /* clock interface */ | ||||
| DEVMETHOD(clock_gettime, rtas_gettime), | DEVMETHOD(clock_gettime, rtas_gettime), | ||||
| DEVMETHOD(clock_settime, rtas_settime), | DEVMETHOD(clock_settime, rtas_settime), | ||||
| { 0, 0 }, | /* Bus interface */ | ||||
| DEVMETHOD(bus_add_child, bus_generic_add_child), | |||||
| DEVMETHOD(bus_child_pnpinfo, ofw_bus_gen_child_pnpinfo), | |||||
| DEVMETHOD(bus_print_child, rtasbus_print_child), | |||||
| DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), | |||||
| DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), | |||||
| DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), | |||||
| DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), | |||||
| DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), | |||||
| DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), | |||||
| DEVMETHOD(bus_get_resource_list, rtasbus_get_resource_list), | |||||
| /* ofw_bus interface */ | |||||
| DEVMETHOD(ofw_bus_get_devinfo, rtasbus_get_devinfo), | |||||
| DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), | |||||
| DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), | |||||
| DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), | |||||
| DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), | |||||
| DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), | |||||
| DEVMETHOD_END | |||||
| }; | }; | ||||
| struct rtas_softc { | |||||
| struct mtx sc_mtx; | |||||
| phandle_t sc_phandle; | |||||
| /* tod clock */ | |||||
| cell_t sc_gtod_token; /* get-time-of-day */ | |||||
| /* event log */ | |||||
| cell_t sc_es_token; /* event-scan */ | |||||
| struct callout sc_es_callout; | |||||
| struct task sc_es_task; | |||||
| int sc_es_interval; | |||||
| void *sc_buffer; | |||||
| size_t sc_buflen; | |||||
| }; | |||||
| static driver_t rtasdev_driver = { | static driver_t rtasdev_driver = { | ||||
| "rtas", | "rtas", | ||||
| rtasdev_methods, | rtasdev_methods, | ||||
| 0 | sizeof(struct rtas_softc), | ||||
| }; | }; | ||||
| DRIVER_MODULE(rtasdev, ofwbus, rtasdev_driver, 0, 0); | DRIVER_MODULE(rtasdev, ofwbus, rtasdev_driver, 0, 0); | ||||
| /* | |||||
| * The exception event handler; this attaches to children of /event-sources. | |||||
| */ | |||||
| static int rtas_esrc_probe(device_t); | |||||
| static int rtas_esrc_attach(device_t); | |||||
| static void rtas_esrc_ithread(void *); | |||||
| static device_method_t rtas_esrc_methods[] = { | |||||
| /* Device interface */ | |||||
| DEVMETHOD(device_probe, rtas_esrc_probe), | |||||
| DEVMETHOD(device_attach, rtas_esrc_attach), | |||||
| DEVMETHOD_END | |||||
| }; | |||||
| struct rtas_esrc_softc { | |||||
| struct mtx sc_mtx; | |||||
| cell_t sc_token; | |||||
| int sc_irq_rid; | |||||
| struct resource *sc_irq; | |||||
| void *sc_intr_cookie; | |||||
| void *sc_buffer; | |||||
| size_t sc_buflen; | |||||
| }; | |||||
| static driver_t rtas_esrc_driver = { | |||||
| "rtas_esrc", | |||||
| rtas_esrc_methods, | |||||
| sizeof(struct rtas_esrc_softc), | |||||
| }; | |||||
| DRIVER_MODULE(rtas_esrc, rtas, rtas_esrc_driver, 0, 0); | |||||
| /* event log interface */ | |||||
| static void rtas_event_init(device_t, struct rtas_softc *); | |||||
| static void rtas_event_callout(void *); | |||||
| static void rtas_event_task(void *, int); | |||||
| static int | static int | ||||
| rtasdev_probe(device_t dev) | rtasdev_probe(device_t dev) | ||||
| { | { | ||||
| const char *name = ofw_bus_get_name(dev); | const char *name; | ||||
| if (strcmp(name, "rtas") != 0) | name = ofw_bus_get_name(dev); | ||||
| if (name == NULL || strcmp(name, "rtas") != 0) | |||||
| return (ENXIO); | return (ENXIO); | ||||
| if (!rtas_exists()) | if (!rtas_exists()) | ||||
| return (ENXIO); | return (ENXIO); | ||||
| device_set_desc(dev, "Run-Time Abstraction Services"); | device_set_desc(dev, "Run-Time Abstraction Services"); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| static int | static int | ||||
| rtasdev_attach(device_t dev) | rtasdev_attach(device_t dev) | ||||
| { | { | ||||
| if (rtas_token_lookup("get-time-of-day") != -1) | struct rtas_softc *sc; | ||||
| sc = device_get_softc(dev); | |||||
| sc->sc_phandle = OF_finddevice("/rtas"); | |||||
| if (sc->sc_phandle == -1) { | |||||
| device_printf(dev, "cannot find RTAS device\n"); | |||||
| return (ENXIO); | |||||
| } | |||||
| mtx_init(&sc->sc_mtx, "rtas", NULL, MTX_DEF); | |||||
| callout_init_mtx(&sc->sc_es_callout, &sc->sc_mtx, 0); | |||||
| TASK_INIT(&sc->sc_es_task, 0, rtas_event_task, dev); | |||||
| /* Attach the tod clock */ | |||||
| sc->sc_gtod_token = rtas_token_lookup("get-time-of-day"); | |||||
| if (sc->sc_gtod_token != -1) | |||||
| clock_register(dev, 2000); | clock_register(dev, 2000); | ||||
| /* Attach the periodic log scanner */ | |||||
| rtas_event_init(dev, sc); | |||||
| /* Attach event sources */ | |||||
| rtasdev_find_children(dev); | |||||
| EVENTHANDLER_REGISTER(shutdown_final, rtas_shutdown, NULL, | EVENTHANDLER_REGISTER(shutdown_final, rtas_shutdown, NULL, | ||||
| SHUTDOWN_PRI_LAST); | SHUTDOWN_PRI_LAST); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| static void | |||||
| rtasdev_find_children(device_t dev) | |||||
| { | |||||
| struct rtas_devinfo *dinfo; | |||||
| cell_t irq[2]; | |||||
| device_t cdev; | |||||
| phandle_t event_sources, child; | |||||
| int err; | |||||
| event_sources = OF_finddevice("/event-sources"); | |||||
| if (event_sources == -1) | |||||
| return; | |||||
| powerpc_register_pic(root_pic, OF_xref_from_node(event_sources), | |||||
| 1 << 24 /* 24-bit XIRR field */, 1 /* Number of IPIs */, FALSE); | |||||
| for (child = OF_child(event_sources); child != 0; | |||||
| child = OF_peer(child)) { | |||||
| dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); | |||||
| if (ofw_bus_gen_setup_devinfo(&dinfo->rdi_obdinfo, child) != 0) { | |||||
| free(dinfo, M_DEVBUF); | |||||
| continue; | |||||
| } | |||||
| /* | |||||
| * We need to save the underlying IRQ so we can pass it to | |||||
| * check-exception later. | |||||
| */ | |||||
| err = OF_getencprop(child, "interrupts", &irq[0], sizeof(irq)); | |||||
| if (err == -1) { | |||||
| device_printf(dev, "<%s>: failed to fetch interrupts\n", | |||||
| dinfo->rdi_obdinfo.obd_name); | |||||
| ofw_bus_gen_destroy_devinfo(&dinfo->rdi_obdinfo); | |||||
| free(dinfo, M_DEVBUF); | |||||
| continue; | |||||
| } | |||||
| dinfo->rdi_base_irq = irq[0]; | |||||
| resource_list_init(&dinfo->rdi_resources); | |||||
| ofw_bus_intr_to_rl(dev, child, &dinfo->rdi_resources, NULL); | |||||
| cdev = device_add_child(dev, NULL, DEVICE_UNIT_ANY); | |||||
| if (cdev == NULL) { | |||||
| device_printf(dev, "<%s>: device_add_child failed\n", | |||||
| dinfo->rdi_obdinfo.obd_name); | |||||
| ofw_bus_gen_destroy_devinfo(&dinfo->rdi_obdinfo); | |||||
| free(dinfo, M_DEVBUF); | |||||
| continue; | |||||
| } | |||||
| device_set_ivars(cdev, dinfo); | |||||
| } | |||||
| bus_attach_children(dev); | |||||
| } | |||||
| static struct resource_list * | |||||
| rtasbus_get_resource_list(device_t dev, device_t child) | |||||
| { | |||||
| struct rtas_devinfo *dinfo; | |||||
| dinfo = device_get_ivars(child); | |||||
| return (&dinfo->rdi_resources); | |||||
| } | |||||
| static struct resource * | |||||
| rtasbus_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) | |||||
| { | |||||
| struct rtas_devinfo *dinfo; | |||||
| RTAS_DEVPRINTF(child, "alloc_resource, type=%d\n", type); | |||||
| dinfo = device_get_ivars(child); | |||||
| if (type != SYS_RES_IRQ) | |||||
| return (NULL); | |||||
| return (resource_list_alloc(&dinfo->rdi_resources, bus, child, type, | |||||
| rid, start, end, count, flags)); | |||||
| } | |||||
| static const struct ofw_bus_devinfo * | |||||
| rtasbus_get_devinfo(device_t dev, device_t child) | |||||
| { | |||||
| return (device_get_ivars(child)); | |||||
| } | |||||
| static int | static int | ||||
| rtasbus_print_child(device_t dev, device_t child) | |||||
| { | |||||
| struct rtas_devinfo *dinfo; | |||||
| struct resource_list *rl; | |||||
| int retval = 0; | |||||
| dinfo = device_get_ivars(child); | |||||
| rl = &dinfo->rdi_resources; | |||||
| retval += bus_print_child_header(dev, child); | |||||
| retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd"); | |||||
| retval += bus_print_child_footer(dev, child); | |||||
| return (retval); | |||||
| } | |||||
| static int | |||||
| rtas_gettime(device_t dev, struct timespec *ts) { | rtas_gettime(device_t dev, struct timespec *ts) { | ||||
| struct clocktime ct; | struct clocktime ct; | ||||
| struct rtas_softc *sc; | |||||
| cell_t tod[8]; | cell_t tod[8]; | ||||
| cell_t token; | |||||
| int error; | int error; | ||||
| token = rtas_token_lookup("get-time-of-day"); | sc = device_get_softc(dev); | ||||
| if (token == -1) | |||||
| if (sc->sc_gtod_token == -1) | |||||
| return (ENXIO); | return (ENXIO); | ||||
| error = rtas_call_method(token, 0, 8, &tod[0], &tod[1], &tod[2], | error = rtas_call_method(sc->sc_gtod_token, 0, 8, &tod[0], &tod[1], | ||||
| &tod[3], &tod[4], &tod[5], &tod[6], &tod[7]); | &tod[2], &tod[3], &tod[4], &tod[5], &tod[6], &tod[7]); | ||||
| if (error < 0) | if (error < 0) | ||||
| return (ENXIO); | return (ENXIO); | ||||
| if (tod[0] != 0) | if (tod[0] != 0) | ||||
| return ((tod[0] == -1) ? ENXIO : EAGAIN); | return ((tod[0] == -1) ? ENXIO : EAGAIN); | ||||
| ct.year = tod[1]; | ct.year = tod[1]; | ||||
| ct.mon = tod[2]; | ct.mon = tod[2]; | ||||
| ct.day = tod[3]; | ct.day = tod[3]; | ||||
| ct.hour = tod[4]; | ct.hour = tod[4]; | ||||
| ct.min = tod[5]; | ct.min = tod[5]; | ||||
| ct.sec = tod[6]; | ct.sec = tod[6]; | ||||
| ct.nsec = tod[7]; | ct.nsec = tod[7]; | ||||
| return (clock_ct_to_ts(&ct, ts)); | return (clock_ct_to_ts(&ct, ts)); | ||||
| } | } | ||||
| static int | static int | ||||
| rtas_settime(device_t dev, struct timespec *ts) | rtas_settime(device_t dev, struct timespec *ts) | ||||
| { | { | ||||
| struct clocktime ct; | struct clocktime ct; | ||||
| cell_t token, status; | struct rtas_softc *sc; | ||||
| cell_t status; | |||||
| int error; | int error; | ||||
| token = rtas_token_lookup("set-time-of-day"); | sc = device_get_softc(dev); | ||||
| if (token == -1) | |||||
| if (sc->sc_gtod_token == -1) | |||||
| return (ENXIO); | return (ENXIO); | ||||
| clock_ts_to_ct(ts, &ct); | clock_ts_to_ct(ts, &ct); | ||||
| error = rtas_call_method(token, 7, 1, ct.year, ct.mon, ct.day, ct.hour, | error = rtas_call_method(sc->sc_gtod_token, 7, 1, ct.year, ct.mon, | ||||
| ct.min, ct.sec, ct.nsec, &status); | ct.day, ct.hour, ct.min, ct.sec, ct.nsec, &status); | ||||
| if (error < 0) | if (error < 0) | ||||
| return (ENXIO); | return (ENXIO); | ||||
| if (status != 0) | if (status != 0) | ||||
| return (((int)status < 0) ? ENXIO : EAGAIN); | return (((int)status < 0) ? ENXIO : EAGAIN); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| Show All 9 Lines | if ((howto & RB_POWEROFF) != 0) { | ||||
| rtas_call_method(token, 2, 1, 0, 0, &status); | rtas_call_method(token, 2, 1, 0, 0, &status); | ||||
| } else if ((howto & RB_HALT) == 0) { | } else if ((howto & RB_HALT) == 0) { | ||||
| token = rtas_token_lookup("system-reboot"); | token = rtas_token_lookup("system-reboot"); | ||||
| if (token == -1) | if (token == -1) | ||||
| return; | return; | ||||
| rtas_call_method(token, 0, 1, &status); | rtas_call_method(token, 0, 1, &status); | ||||
| } | |||||
| } | |||||
| static void | |||||
| rtas_event_init(device_t dev, struct rtas_softc *sc) | |||||
| { | |||||
| cell_t scan_rate; | |||||
| int err; | |||||
| sc->sc_es_token = rtas_token_lookup("event-scan"); | |||||
| if (sc->sc_es_token == -1) | |||||
| return; | |||||
| err = OF_getencprop(sc->sc_phandle, "rtas-event-scan-rate", &scan_rate, | |||||
| sizeof(scan_rate)); | |||||
| if (err == -1 || scan_rate <= 0) { | |||||
| device_printf(dev, "cannot fetch rtas-event-scan-rate, " | |||||
| "event log scan disabled\n"); | |||||
| return; | |||||
| } | } | ||||
| sc->sc_buflen = RTAS_ES_BUFSIZE; | |||||
| sc->sc_buffer = contigmalloc(sc->sc_buflen, M_DEVBUF, 0, | |||||
| 0, BUS_SPACE_MAXADDR_32BIT, 8, 256*1024*1024); | |||||
| if (sc->sc_buffer == NULL) { | |||||
| device_printf(dev, "cannot allocate event log buffer, " | |||||
| "event log scan disabled\n"); | |||||
| return; | |||||
| } | } | ||||
| RTAS_DEVPRINTF(dev, "event-scan every 1/%u sec\n", | |||||
| (unsigned)scan_rate); | |||||
| sc->sc_es_interval = hz / scan_rate; | |||||
| callout_reset(&sc->sc_es_callout, sc->sc_es_interval, | |||||
| rtas_event_callout, dev); | |||||
| } | |||||
| static void | |||||
| rtas_event_callout(void *arg) | |||||
| { | |||||
| device_t dev; | |||||
| struct rtas_softc *sc; | |||||
| dev = arg; | |||||
| sc = device_get_softc(dev); | |||||
| taskqueue_enqueue(taskqueue_thread, &sc->sc_es_task); | |||||
| } | |||||
| static void | |||||
| rtas_event_task(void *arg, int pending) | |||||
| { | |||||
| device_t dev; | |||||
| struct rtas_softc *sc; | |||||
| int ret; | |||||
| cell_t status, event_mask, critical, bufaddr, buflen; | |||||
| dev = arg; | |||||
| sc = device_get_softc(dev); | |||||
| event_mask = -1; | |||||
| critical = 0; | |||||
| bufaddr = (cell_t)vtophys(sc->sc_buffer); | |||||
| buflen = sc->sc_buflen; | |||||
| ret = rtas_call_method(sc->sc_es_token, 4, 1, event_mask, critical, | |||||
| bufaddr, buflen, &status); | |||||
| if (ret < 0) { | |||||
| device_printf(dev, "event-scan failed, status=%d\n", | |||||
| (int)status); | |||||
| goto done; | |||||
| } | |||||
| rtas_handle_event(sc->sc_buffer, sc->sc_buflen); | |||||
| done: | |||||
| callout_reset(&sc->sc_es_callout, sc->sc_es_interval, | |||||
| rtas_event_callout, dev); | |||||
| } | |||||
| static int | |||||
| rtas_esrc_probe(device_t dev) | |||||
| { | |||||
| const char *name; | |||||
| RTAS_DEVPRINTF(dev, "probe\n"); | |||||
| name = ofw_bus_get_name(dev); | |||||
| if (strcmp(name, "epow-events") == 0) | |||||
| device_set_desc(dev, "Environmental and Power Warnings"); | |||||
| else if (strcmp(name, "hot-plug-events") == 0) | |||||
| device_set_desc(dev, "Hot Plug Events"); | |||||
| else | |||||
| return (ENXIO); | |||||
| return (0); | |||||
| } | |||||
| static int | |||||
| rtas_esrc_attach(device_t dev) | |||||
| { | |||||
| struct rtas_esrc_softc *sc; | |||||
| int err; | |||||
| RTAS_DEVPRINTF(dev, "attach\n"); | |||||
| sc = device_get_softc(dev); | |||||
| sc->sc_token = rtas_token_lookup("check-exception"); | |||||
| if (sc->sc_token == -1) { | |||||
| device_printf(dev, "cannot look up check-exception token\n"); | |||||
| return (ENXIO); | |||||
| } | |||||
| sc->sc_buflen = RTAS_ES_BUFSIZE; | |||||
| sc->sc_buffer = contigmalloc(sc->sc_buflen, M_DEVBUF, 0, | |||||
| 0, BUS_SPACE_MAXADDR_32BIT, 8, 256*1024*1024); | |||||
| if (sc->sc_buffer == NULL) { | |||||
| device_printf(dev, "cannot allocate buffer\n"); | |||||
| return (ENXIO); | |||||
| } | |||||
| sc->sc_irq_rid = 0; | |||||
| sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid, | |||||
| RF_SHAREABLE | RF_ACTIVE); | |||||
| if (sc->sc_irq == NULL) { | |||||
| device_printf(dev, "cannot allocate IRQ\n"); | |||||
| return (ENXIO); | |||||
| } | |||||
| err = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE, | |||||
| NULL, rtas_esrc_ithread, dev, &sc->sc_intr_cookie); | |||||
| if (err != 0) { | |||||
| device_printf(dev, "bus_setup_intr failed: %d\n", err); | |||||
| return (ENXIO); | |||||
| } | |||||
| return (0); | |||||
| } | |||||
| static void | |||||
| rtas_esrc_ithread(void *arg) | |||||
| { | |||||
| device_t dev; | |||||
| struct rtas_esrc_softc *sc; | |||||
| struct rtas_devinfo *dinfo; | |||||
| int ret; | |||||
| cell_t status, voff, addl, mask, critical, bufaddr, buflen; | |||||
| dev = arg; | |||||
| sc = device_get_softc(dev); | |||||
| dinfo = device_get_ivars(dev); | |||||
| RTAS_DEVPRINTF(dev, "interrupt! irq=%d\n", dinfo->rdi_base_irq); | |||||
| voff = 0; | |||||
adrian: as mentioned on irc, yeah, you likely can just put this whole chunk into a task and schedule it… | |||||
| addl = dinfo->rdi_base_irq; | |||||
| mask = RTAS_EV_CLASS_MASK(RTAS_EV_CLASS_EPOW); | |||||
| critical = 0; | |||||
| bufaddr = (cell_t)vtophys(sc->sc_buffer); | |||||
adrianUnsubmitted Not Done Inline Actionsok, so it looks like you're using contigmalloc, vtophys, cpu_flush_dcache, etc instead of using busdma to do all of this. Since you're not allocating temporary memory/mapping via busdma, I think you should also add a comment in the softc definition above that the memory is pre-allocated /only/ for handling the interrupt notification and must not be used for anything else, nor overlapping calls to this task/function. adrian: ok, so it looks like you're using contigmalloc, vtophys, cpu_flush_dcache, etc instead of using… | |||||
| buflen = sizeof(sc->sc_buflen); | |||||
| do { | |||||
| ret = rtas_call_method(sc->sc_token, 6, 1, voff, addl, mask, | |||||
adrianUnsubmitted Not Done Inline ActionsWhat's "6" and "1" here? it looks like this driver is full of them, so it would be nice for a follow-up commit turning them into define'd values with some description about what they are (my guess is '6' here is the method id.) I currently have no idea where to go look to find the method ids, so a link to a PDF and section in the PDF would be good too. adrian: What's "6" and "1" here? it looks like this driver is full of them, so it would be nice for a… | |||||
ivyAuthorUnsubmitted Done Inline Actionsthis is the standard Open Firmware calling convention: you pass the function to call, the number of inputs and outputs, and then the input and output values. RTAS is only slightly different because you have to do a token lookup first rather than just passing the function name (see line 426), which is what sc_token is. ivy: this is the standard Open Firmware calling convention: you pass the function to call, the… | |||||
| critical, bufaddr, buflen, &status); | |||||
| if (ret < 0) { | |||||
| device_printf(dev, "call-exception failed: %d\n", ret); | |||||
| return; | |||||
| } | |||||
| RTAS_DEVPRINTF(dev, "status=%d\n", (int)status); | |||||
| if (status == 0) { | |||||
| cpu_flush_dcache(sc->sc_buffer, sc->sc_buflen); | |||||
| rtas_handle_event(sc->sc_buffer, sc->sc_buflen); | |||||
| } | |||||
| } while (status == 0); | |||||
| } | |||||
Not Done Inline ActionsDo you expect to need to extend this in the future, adding other event type handling? If so, maybe consider a jump table struct, or a switch here, to make it easier. Also, that buffer pointer math looks error prone, so consider simplifying that. You could simply pass the whole buffer down instead, and use a union to get the vendor data you need. jhibbits: Do you expect to need to extend this in the future, adding other event type handling? If so… | |||||
Done Inline Actionsthe only other one i think we definitely want to handle is hotplug events, but i'm not planning on looking at that any time soon. so a jump table is probably over kill, but i'll try a switch and see if it looks nicer. ivy: the only other one i think we definitely want to handle is hotplug events, but i'm not planning… | |||||
| static void | |||||
| rtas_handle_event(const void *buf, size_t buflen) | |||||
| { | |||||
| const struct rtas_event_v6 *event; | |||||
| bool handled; | |||||
| event = buf; | |||||
| if (event->re_version != RTAS_V6_VERSION) | |||||
| return; | |||||
| /* | |||||
| * Ensure the total size of the event is within our buffer, | |||||
| * which is guaranteed by the spec. | |||||
| */ | |||||
| if (buflen < sizeof(*event) || | |||||
| (buflen - sizeof(*event)) < be32toh(event->re_extlen)) | |||||
| return; | |||||
| /* See if we have a type-specific handler */ | |||||
| switch (event->re_type) { | |||||
| case RTAS_V6_TYPE_EPOW: | |||||
| handled = rtas_handle_epow(event); | |||||
| break; | |||||
| default: | |||||
| handled = false; | |||||
| break; | |||||
| } | |||||
| if (handled) | |||||
| return; | |||||
| /* If we don't recognise the event, log it and do nothing */ | |||||
| printf("RTAS: Event: severity=%s, disposition=%s, initiator=%s, " | |||||
| "target=%s, type=%s\n", | |||||
| rtas_name_severity(event->re_severity), | |||||
| rtas_name_disposition(event->re_disposition), | |||||
| rtas_name_subsystem(event->re_initiator), | |||||
| rtas_name_subsystem(event->re_target), | |||||
| rtas_name_type(event->re_type)); | |||||
| } | |||||
| static bool | |||||
| rtas_handle_epow(const struct rtas_event_v6 *event) | |||||
| { | |||||
| const struct rtas_extlog_plt_main_b *mainb; | |||||
| const struct rtas_extlog_epow_v6 *epow; | |||||
| const char *reason, *type; | |||||
| mainb = rtas_find_plt_section(event, RTAS_V6_SECTION_MAIN_B_ID, | |||||
| RTAS_V6_SECTION_MAIN_B_SIZE); | |||||
| if (mainb == NULL) | |||||
| return (false); /* This is not a platform event log */ | |||||
| /* 0xA0 - 0xAF: External environment */ | |||||
| if (mainb->pmb_subsystem < 0xA0 || mainb->pmb_subsystem > 0xAF) | |||||
| return (false); | |||||
| epow = rtas_find_plt_section(event, RTAS_V6_SECTION_EPOW_ID, | |||||
| RTAS_V6_SECTION_EPOW_SIZE); | |||||
| if (epow == NULL) | |||||
| return (false); | |||||
| switch (epow->ep_modifier) { | |||||
| case EPOW_MOD_NORMAL_SHUTDOWN: | |||||
| reason = "normal shutdown"; | |||||
| break; | |||||
| case EPOW_MOD_UTILITY_POWER_LOSS: | |||||
| reason = "utility power loss"; | |||||
| break; | |||||
| case EPOW_MOD_CRITICAL_FUNCTION_LOSS: | |||||
| reason = "critical function loss"; | |||||
| break; | |||||
| case EPOW_MOD_TEMP_TOO_HIGH: | |||||
| reason = "ambient temperature too high"; | |||||
| break; | |||||
| default: | |||||
Not Done Inline ActionsIs the EPOW shutdown request a request, or an order? Unless this is the hypervisor saying that the partition will be shut down, I think this could instead be propagated up to devd (devctl_notify()) to let the user decide how to handle it, and add an entry to devd.conf (or, better yet, create a pseries devd.conf) to default to shutdown. jhibbits: Is the EPOW shutdown request a request, or an order? Unless this is the hypervisor saying that… | |||||
Done Inline Actionsit depends on the type/reason code, but i think in all cases, the user will want to shut down here:
i'm not opposed to making this a devd event, but i don't think any other architecture works that way, and we'd want to make sure the event was at least roughly portable between platforms, so it's probably out of scope for this diff. incidentally, OPAL does this the same way, see sys/powerpc/powernv/opal_dev.c:opal_handle_shutdown_message(). cc: @imp ivy: it depends on the type/reason code, but i think in all cases, the user will want to shut down… | |||||
Not Done Inline ActionsOPAL does it this way because the message is reporting that the machine is going down shortly, there's no way to stop it. The only safe thing to do in that case is to shutdown from the kernel, and hope that it cleans up before power is removed. jhibbits: OPAL does it this way because the message is reporting that the machine is going down shortly… | |||||
| reason = "unknown"; | |||||
| break; | |||||
| } | |||||
| switch (epow->ep_ext_modifier) { | |||||
| case EPOW_EXTMOD_SYSTEM_SHUTDOWN: | |||||
| type = "system-wide"; | |||||
| break; | |||||
| case EPOW_EXTMOD_PARTITION_SHUTDOWN: | |||||
| type = "partition-specific"; | |||||
| break; | |||||
| default: | |||||
| type = "unknown"; | |||||
| break; | |||||
| } | |||||
| /* | |||||
| * According to the spec: | |||||
| * | |||||
| * * SYSTEM_SHUTDOWN means the system must begin to shut down after | |||||
| * an OS defined delay, which is 10 minutes by default. | |||||
| * | |||||
| * * SYSTEM_HALT means to shut down within 20 seconds. | |||||
| * | |||||
| * * POWER_OFF means the system must halt immediately and we are only | |||||
| * guaranteed 4ms of remaining power. | |||||
| * | |||||
| * We don't currently have a good way to indiciate SYSTEM_SHUTDOWN | |||||
| * to userland, so treat both SHUTDOWN and HALT as an immediate | |||||
| * shutdown. | |||||
| */ | |||||
| switch (epow->ep_sensor_value) { | |||||
| case EPOW_ACTION_SYSTEM_SHUTDOWN: | |||||
| case EPOW_ACTION_SYSTEM_HALT: | |||||
| printf("RTAS: EPOW %s shutdown request, reason=%s\n", | |||||
| type, reason); | |||||
| shutdown_nice(RB_POWEROFF); | |||||
| return (true); | |||||
| case EPOW_ACTION_POWER_OFF: | |||||
| printf("RTAS: EPOW %s emergency halt, reason=%s\n", | |||||
| type, reason); | |||||
| kern_reboot(RB_POWEROFF); | |||||
| return (true); | |||||
| default: | |||||
| return (false); | |||||
| } | |||||
| } | |||||
as mentioned on irc, yeah, you likely can just put this whole chunk into a task and schedule it on the existing taskqueue (which if I'm not mistaken is the global taskqueue.)