diff --git a/sys/powerpc/powernv/opal.h b/sys/powerpc/powernv/opal.h index 7671ece28122..d7a81e33f809 100644 --- a/sys/powerpc/powernv/opal.h +++ b/sys/powerpc/powernv/opal.h @@ -1,231 +1,253 @@ /*- * Copyright (c) 2015 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _POWERNV_OPAL_H #define _POWERNV_OPAL_H #include #include #include /* Check if OPAL is correctly instantiated. Will try to instantiate it. */ int opal_check(void); /* Call an OPAL method. Any pointers passed must be real-mode accessible! */ int opal_call(uint64_t token, ...); #define OPAL_CONSOLE_WRITE 1 #define OPAL_CONSOLE_READ 2 #define OPAL_RTC_READ 3 #define OPAL_RTC_WRITE 4 #define OPAL_CEC_POWER_DOWN 5 #define OPAL_CEC_REBOOT 6 #define OPAL_READ_NVRAM 7 #define OPAL_WRITE_NVRAM 8 #define OPAL_HANDLE_INTERRUPT 9 #define OPAL_POLL_EVENTS 10 #define OPAL_PCI_CONFIG_READ_BYTE 13 #define OPAL_PCI_CONFIG_READ_HALF_WORD 14 #define OPAL_PCI_CONFIG_READ_WORD 15 #define OPAL_PCI_CONFIG_WRITE_BYTE 16 #define OPAL_PCI_CONFIG_WRITE_HALF_WORD 17 #define OPAL_PCI_CONFIG_WRITE_WORD 18 #define OPAL_PCI_EEH_FREEZE_STATUS 23 #define OPAL_PCI_EEH_FREEZE_CLEAR 26 #define OPAL_PCI_PHB_MMIO_ENABLE 27 #define OPAL_PCI_SET_PHB_MEM_WINDOW 28 #define OPAL_PCI_MAP_PE_MMIO_WINDOW 29 #define OPAL_PCI_SET_XIVE_PE 37 #define OPAL_PCI_RESET 49 #define OPAL_PCI_POLL 62 #define OPAL_SET_XIVE 19 #define OPAL_GET_XIVE 20 #define OPAL_PCI_SET_PE 31 #define OPAL_GET_MSI_32 39 #define OPAL_GET_MSI_64 40 #define OPAL_PCI_MSI_EOI 63 #define OPAL_PCI_GET_PHB_DIAG_DATA2 64 #define OPAL_START_CPU 41 #define OPAL_PCI_MAP_PE_DMA_WINDOW 44 #define OPAL_PCI_MAP_PE_DMA_WINDOW_REAL 45 #define OPAL_RETURN_CPU 69 #define OPAL_REINIT_CPUS 70 #define OPAL_CHECK_TOKEN 80 #define OPAL_GET_MSG 85 #define OPAL_CHECK_ASYNC_COMPLETION 86 #define OPAL_SENSOR_READ 88 #define OPAL_HANDLE_HMI 98 #define OPAL_IPMI_SEND 107 #define OPAL_IPMI_RECV 108 #define OPAL_I2C_REQUEST 109 #define OPAL_FLASH_READ 110 #define OPAL_FLASH_WRITE 111 #define OPAL_FLASH_ERASE 112 #define OPAL_INT_GET_XIRR 122 #define OPAL_INT_SET_CPPR 123 #define OPAL_INT_EOI 124 #define OPAL_INT_SET_MFRR 125 #define OPAL_PCI_TCE_KILL 126 #define OPAL_NMMU_SET_PTCR 127 #define OPAL_XIVE_RESET 128 #define OPAL_XIVE_GET_IRQ_INFO 129 #define OPAL_XIVE_GET_IRQ_CONFIG 130 #define OPAL_XIVE_SET_IRQ_CONFIG 131 #define OPAL_XIVE_GET_QUEUE_INFO 132 #define OPAL_XIVE_SET_QUEUE_INFO 133 #define OPAL_XIVE_DONATE_PAGE 134 #define OPAL_XIVE_ALLOCATE_VP_BLOCK 135 #define OPAL_XIVE_FREE_VP_BLOCK 136 #define OPAL_XIVE_GET_VP_INFO 137 #define OPAL_XIVE_SET_VP_INFO 138 #define OPAL_XIVE_ALLOCATE_IRQ 139 #define OPAL_XIVE_FREE_IRQ 140 #define OPAL_XIVE_SYNC 141 #define OPAL_XIVE_DUMP 142 #define OPAL_SENSOR_GROUP_CLEAR 156 #define OPAL_SENSOR_READ_U64 162 #define OPAL_SENSOR_GROUP_ENABLE 163 #define OPAL_HANDLE_HMI2 166 /* For OPAL_PCI_SET_PE */ #define OPAL_UNMAP_PE 0 #define OPAL_MAP_PE 1 #define OPAL_PCI_BUS_ANY 0 #define OPAL_PCI_BUS_3BITS 2 #define OPAL_PCI_BUS_4BITS 3 #define OPAL_PCI_BUS_5BITS 4 #define OPAL_PCI_BUS_6BITS 5 #define OPAL_PCI_BUS_7BITS 6 #define OPAL_PCI_BUS_ALL 7 /* Match bus number exactly */ #define OPAL_IGNORE_RID_DEVICE_NUMBER 0 #define OPAL_COMPARE_RID_DEVICE_NUMBER 1 #define OPAL_IGNORE_RID_FUNC_NUMBER 0 #define OPAL_COMPARE_RID_FUNC_NUMBER 1 /* For OPAL_PCI_TCE_KILL */ #define OPAL_PCI_TCE_KILL_PAGE 0 #define OPAL_PCI_TCE_KILL_PE 1 #define OPAL_PCI_TCE_KILL_ALL 2 #define OPAL_SUCCESS 0 #define OPAL_PARAMETER -1 #define OPAL_BUSY -2 +#define OPAL_PARTIAL -3 +#define OPAL_CONSTRAINED -4 #define OPAL_CLOSED -5 #define OPAL_HARDWARE -6 #define OPAL_UNSUPPORTED -7 +#define OPAL_PERMISSION -8 +#define OPAL_NO_MEM -9 #define OPAL_RESOURCE -10 +#define OPAL_INTERNAL_ERROR -11 #define OPAL_BUSY_EVENT -12 +#define OPAL_HARDWARE_FROZEN -13 +#define OPAL_WRONG_STATE -14 #define OPAL_ASYNC_COMPLETION -15 #define OPAL_EMPTY -16 +#define OPAL_I2C_TIMEOUT -17 +#define OPAL_I2C_INVALID_CMD -18 +#define OPAL_I2C_LBUS_PARITY -19 +#define OPAL_I2C_BKEND_OVERRUN -20 +#define OPAL_I2C_BKEND_ACCESS -21 +#define OPAL_I2C_ARBT_LOST -22 +#define OPAL_I2C_NACK_RCVD -23 +#define OPAL_I2C_STOP_ERR -24 +#define OPAL_XSCOM_PARTIAL_GOOD -25 +#define OPAL_XSCOM_ADDR_ERROR -26 +#define OPAL_XSCOM_CLOCK_ERROR -27 +#define OPAL_XSCOM_PARITY_ERROR -28 +#define OPAL_XSCOM_TIMEOUT -29 +#define OPAL_XSCOM_CTR_OFFLINED -30 #define OPAL_XIVE_PROVISIONING -31 #define OPAL_XIVE_FREE_ACTIVE -32 +#define OPAL_TIMEOUT -33 #define OPAL_TOKEN_ABSENT 0 #define OPAL_TOKEN_PRESENT 1 #define OPAL_EVENT_OPAL_INTERNAL 0x1 #define OPAL_EVENT_NVRAM 0x2 #define OPAL_EVENT_RTC 0x4 #define OPAL_EVENT_CONSOLE_INPUT 0x8 #define OPAL_EVENT_CONSOLE_OUTPUT 0x10 #define OPAL_EVENT_ERROR_LOG_AVAIL 0x20 #define OPAL_EVENT_ERROR_LOG 0x40 #define OPAL_EVENT_EPOW 0x80 #define OPAL_EVENT_LED_STATUS 0x100 #define OPAL_EVENT_PCI_ERROR 0x200 #define OPAL_EVENT_DUMP_AVAIL 0x400 #define OPAL_EVENT_MSG_PENDING 0x800 #define OPAL_HMI_FLAGS_TB_RESYNC (1ull << 0) #define OPAL_HMI_FLAGS_DEC_LOST (1ull << 1) #define OPAL_HMI_FLAGS_HDEC_LOST (1ull << 2) #define OPAL_HMI_FLAGS_TOD_TB_FAIL (1ull << 3) #define OPAL_HMI_FLAGS_NEW_EVENT (1ull << 63) #define OPAL_XIVE_XICS_MODE_EMU 0 #define OPAL_XIVE_XICS_MODE_EXP 1 #define OPAL_XIVE_SYNC_EAS 0x00000001 #define OPAL_XIVE_SYNC_QUEUE 0x00000002 #define OPAL_XIVE_VP_ENABLED 0x00000001 #define OPAL_XIVE_VP_SINGLE_ESCALATION 0x00000002 #define OPAL_XIVE_EQ_ENABLED 0x00000001 #define OPAL_XIVE_EQ_ALWAYS_NOTIFY 0x00000002 #define OPAL_XIVE_EQ_ESCALATE 0x00000004 struct opal_msg { uint32_t msg_type; uint32_t reserved; uint64_t params[8]; }; enum opal_msg_type { OPAL_MSG_ASYNC_COMP = 0, OPAL_MSG_MEM_ERR = 1, OPAL_MSG_EPOW = 2, OPAL_MSG_SHUTDOWN = 3, OPAL_MSG_HMI_EVT = 4, OPAL_MSG_DPO = 5, OPAL_MSG_PRD = 6, OPAL_MSG_OCC = 7, OPAL_MSG_TYPE_MAX, }; #define OPAL_IPMI_MSG_FORMAT_VERSION_1 1 struct opal_ipmi_msg { uint8_t version; uint8_t netfn; uint8_t cmd; uint8_t data[]; }; int opal_init_async_tokens(int); int opal_alloc_async_token(void); void opal_free_async_token(int); int opal_wait_completion(void *, uint64_t, int); typedef void (*opal_msg_handler_fn)(void *, struct opal_msg *); EVENTHANDLER_DECLARE(OPAL_ASYNC_COMP, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_EPOW, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_SHUTDOWN, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_HMI_EVT, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_DPO, opal_msg_handler_fn); EVENTHANDLER_DECLARE(OPAL_OCC, opal_msg_handler_fn); EVENTHANDLER_LIST_DECLARE(OPAL_ASYNC_COMP); EVENTHANDLER_LIST_DECLARE(OPAL_EPOW); EVENTHANDLER_LIST_DECLARE(OPAL_SHUTDOWN); EVENTHANDLER_LIST_DECLARE(OPAL_HMI_EVT); EVENTHANDLER_LIST_DECLARE(OPAL_DPO); EVENTHANDLER_LIST_DECLARE(OPAL_OCC); #endif diff --git a/sys/powerpc/powernv/opal_dev.c b/sys/powerpc/powernv/opal_dev.c index ccd49bac7a68..e23844fde04d 100644 --- a/sys/powerpc/powernv/opal_dev.c +++ b/sys/powerpc/powernv/opal_dev.c @@ -1,424 +1,438 @@ /*- * Copyright (c) 2015 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "clock_if.h" #include "opal.h" static int opaldev_probe(device_t); static int opaldev_attach(device_t); /* clock interface */ static int opal_gettime(device_t dev, struct timespec *ts); static int opal_settime(device_t dev, struct timespec *ts); /* ofw bus interface */ static const struct ofw_bus_devinfo *opaldev_get_devinfo(device_t dev, device_t child); static void opal_shutdown(void *arg, int howto); static void opal_handle_shutdown_message(void *unused, struct opal_msg *msg); static void opal_intr(void *); static device_method_t opaldev_methods[] = { /* Device interface */ DEVMETHOD(device_probe, opaldev_probe), DEVMETHOD(device_attach, opaldev_attach), /* clock interface */ DEVMETHOD(clock_gettime, opal_gettime), DEVMETHOD(clock_settime, opal_settime), /* Bus interface */ DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), /* ofw_bus interface */ DEVMETHOD(ofw_bus_get_devinfo, opaldev_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 }; static driver_t opaldev_driver = { "opal", opaldev_methods, 0 }; static devclass_t opaldev_devclass; EARLY_DRIVER_MODULE(opaldev, ofwbus, opaldev_driver, opaldev_devclass, 0, 0, BUS_PASS_BUS); static void opal_heartbeat(void); static void opal_handle_messages(void); static struct proc *opal_hb_proc; static struct kproc_desc opal_heartbeat_kp = { "opal_heartbeat", opal_heartbeat, &opal_hb_proc }; SYSINIT(opal_heartbeat_setup, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, &opal_heartbeat_kp); static int opal_heartbeat_ms; EVENTHANDLER_LIST_DEFINE(OPAL_ASYNC_COMP); EVENTHANDLER_LIST_DEFINE(OPAL_EPOW); EVENTHANDLER_LIST_DEFINE(OPAL_SHUTDOWN); EVENTHANDLER_LIST_DEFINE(OPAL_HMI_EVT); EVENTHANDLER_LIST_DEFINE(OPAL_DPO); EVENTHANDLER_LIST_DEFINE(OPAL_OCC); #define OPAL_SOFT_OFF 0 #define OPAL_SOFT_REBOOT 1 static void opal_heartbeat(void) { uint64_t events; if (opal_heartbeat_ms == 0) kproc_exit(0); while (1) { events = 0; /* Turn the OPAL state crank */ opal_call(OPAL_POLL_EVENTS, vtophys(&events)); if (be64toh(events) & OPAL_EVENT_MSG_PENDING) opal_handle_messages(); tsleep(opal_hb_proc, 0, "opal", MSEC_2_TICKS(opal_heartbeat_ms)); } } static int opaldev_probe(device_t dev) { phandle_t iparent; pcell_t *irqs; int i, n_irqs; if (!ofw_bus_is_compatible(dev, "ibm,opal-v3")) return (ENXIO); if (opal_check() != 0) return (ENXIO); device_set_desc(dev, "OPAL Abstraction Firmware"); /* Manually add IRQs before attaching */ if (OF_hasprop(ofw_bus_get_node(dev), "opal-interrupts")) { iparent = OF_finddevice("/interrupt-controller@0"); iparent = OF_xref_from_node(iparent); n_irqs = OF_getproplen(ofw_bus_get_node(dev), "opal-interrupts") / sizeof(*irqs); irqs = malloc(n_irqs * sizeof(*irqs), M_DEVBUF, M_WAITOK); OF_getencprop(ofw_bus_get_node(dev), "opal-interrupts", irqs, n_irqs * sizeof(*irqs)); for (i = 0; i < n_irqs; i++) bus_set_resource(dev, SYS_RES_IRQ, i, ofw_bus_map_intr(dev, iparent, 1, &irqs[i]), 1); free(irqs, M_DEVBUF); } return (BUS_PROBE_SPECIFIC); } static int opaldev_attach(device_t dev) { phandle_t child; device_t cdev; uint64_t junk; int i, rv; uint32_t async_count; struct ofw_bus_devinfo *dinfo; struct resource *irq; /* Test for RTC support and register clock if it works */ rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk)); do { rv = opal_call(OPAL_RTC_READ, vtophys(&junk), vtophys(&junk)); if (rv == OPAL_BUSY_EVENT) rv = opal_call(OPAL_POLL_EVENTS, 0); } while (rv == OPAL_BUSY_EVENT); if (rv == OPAL_SUCCESS) clock_register(dev, 2000); EVENTHANDLER_REGISTER(OPAL_SHUTDOWN, opal_handle_shutdown_message, NULL, EVENTHANDLER_PRI_ANY); EVENTHANDLER_REGISTER(shutdown_final, opal_shutdown, NULL, SHUTDOWN_PRI_LAST); OF_getencprop(ofw_bus_get_node(dev), "ibm,heartbeat-ms", &opal_heartbeat_ms, sizeof(opal_heartbeat_ms)); /* Bind to interrupts */ for (i = 0; (irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, RF_ACTIVE)) != NULL; i++) bus_setup_intr(dev, irq, INTR_TYPE_TTY | INTR_MPSAFE | INTR_ENTROPY, NULL, opal_intr, (void *)rman_get_start(irq), NULL); OF_getencprop(ofw_bus_get_node(dev), "opal-msg-async-num", &async_count, sizeof(async_count)); opal_init_async_tokens(async_count); for (child = OF_child(ofw_bus_get_node(dev)); child != 0; child = OF_peer(child)) { dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { free(dinfo, M_DEVBUF); continue; } cdev = device_add_child(dev, NULL, -1); if (cdev == NULL) { device_printf(dev, "<%s>: device_add_child failed\n", dinfo->obd_name); ofw_bus_gen_destroy_devinfo(dinfo); free(dinfo, M_DEVBUF); continue; } device_set_ivars(cdev, dinfo); } return (bus_generic_attach(dev)); } static int bcd2bin32(int bcd) { int out = 0; out += bcd2bin(bcd & 0xff); out += 100*bcd2bin((bcd & 0x0000ff00) >> 8); out += 10000*bcd2bin((bcd & 0x00ff0000) >> 16); out += 1000000*bcd2bin((bcd & 0xffff0000) >> 24); return (out); } static int bin2bcd32(int bin) { int out = 0; int tmp; tmp = bin % 100; out += bin2bcd(tmp) * 0x1; bin = bin / 100; tmp = bin % 100; out += bin2bcd(tmp) * 0x100; bin = bin / 100; tmp = bin % 100; out += bin2bcd(tmp) * 0x10000; return (out); } static int opal_gettime(device_t dev, struct timespec *ts) { int rv; struct clocktime ct; uint32_t ymd; uint64_t hmsm; rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm)); while (rv == OPAL_BUSY_EVENT) { opal_call(OPAL_POLL_EVENTS, 0); pause("opalrtc", 1); rv = opal_call(OPAL_RTC_READ, vtophys(&ymd), vtophys(&hmsm)); } if (rv != OPAL_SUCCESS) return (ENXIO); hmsm = be64toh(hmsm); ymd = be32toh(ymd); ct.nsec = bcd2bin32((hmsm & 0x000000ffffff0000) >> 16) * 1000; ct.sec = bcd2bin((hmsm & 0x0000ff0000000000) >> 40); ct.min = bcd2bin((hmsm & 0x00ff000000000000) >> 48); ct.hour = bcd2bin((hmsm & 0xff00000000000000) >> 56); ct.day = bcd2bin((ymd & 0x000000ff) >> 0); ct.mon = bcd2bin((ymd & 0x0000ff00) >> 8); ct.year = bcd2bin32((ymd & 0xffff0000) >> 16); return (clock_ct_to_ts(&ct, ts)); } static int opal_settime(device_t dev, struct timespec *ts) { int rv; struct clocktime ct; uint32_t ymd = 0; uint64_t hmsm = 0; clock_ts_to_ct(ts, &ct); ymd |= (uint32_t)bin2bcd(ct.day); ymd |= ((uint32_t)bin2bcd(ct.mon) << 8); ymd |= ((uint32_t)bin2bcd32(ct.year) << 16); hmsm |= ((uint64_t)bin2bcd32(ct.nsec/1000) << 16); hmsm |= ((uint64_t)bin2bcd(ct.sec) << 40); hmsm |= ((uint64_t)bin2bcd(ct.min) << 48); hmsm |= ((uint64_t)bin2bcd(ct.hour) << 56); /* * We do NOT swap endian here, because the values are being sent * via registers instead of indirect via memory. */ do { rv = opal_call(OPAL_RTC_WRITE, ymd, hmsm); if (rv == OPAL_BUSY_EVENT) { rv = opal_call(OPAL_POLL_EVENTS, 0); pause("opalrtc", 1); } } while (rv == OPAL_BUSY_EVENT); if (rv != OPAL_SUCCESS) return (ENXIO); return (0); } static const struct ofw_bus_devinfo * opaldev_get_devinfo(device_t dev, device_t child) { return (device_get_ivars(child)); } static void opal_shutdown(void *arg, int howto) { if (howto & RB_HALT) opal_call(OPAL_CEC_POWER_DOWN, 0 /* Normal power off */); else opal_call(OPAL_CEC_REBOOT); opal_call(OPAL_RETURN_CPU); } static void opal_handle_shutdown_message(void *unused, struct opal_msg *msg) { int howto; switch (be64toh(msg->params[0])) { case OPAL_SOFT_OFF: howto = RB_POWEROFF; break; case OPAL_SOFT_REBOOT: howto = RB_REROOT; break; } shutdown_nice(howto); } static void opal_handle_messages(void) { static struct opal_msg msg; uint64_t rv; uint32_t type; rv = opal_call(OPAL_GET_MSG, vtophys(&msg), sizeof(msg)); - if (rv != OPAL_SUCCESS) + switch (rv) { + case OPAL_SUCCESS: + break; + case OPAL_RESOURCE: + /* no available messages - return */ + return; + case OPAL_PARAMETER: + printf("%s error: invalid buffer. Please file a bug report.\n", __func__); + return; + case OPAL_PARTIAL: + printf("%s error: buffer is too small and messages was discarded. Please file a bug report.\n", __func__); return; + default: + printf("%s opal_call returned unknown result <%lu>\n", __func__, rv); + return; + } type = be32toh(msg.msg_type); switch (type) { case OPAL_MSG_ASYNC_COMP: EVENTHANDLER_DIRECT_INVOKE(OPAL_ASYNC_COMP, &msg); break; case OPAL_MSG_EPOW: EVENTHANDLER_DIRECT_INVOKE(OPAL_EPOW, &msg); break; case OPAL_MSG_SHUTDOWN: EVENTHANDLER_DIRECT_INVOKE(OPAL_SHUTDOWN, &msg); break; case OPAL_MSG_HMI_EVT: EVENTHANDLER_DIRECT_INVOKE(OPAL_HMI_EVT, &msg); break; case OPAL_MSG_DPO: EVENTHANDLER_DIRECT_INVOKE(OPAL_DPO, &msg); break; case OPAL_MSG_OCC: EVENTHANDLER_DIRECT_INVOKE(OPAL_OCC, &msg); break; default: - printf("Unknown OPAL message type %d\n", type); + printf("%s Unknown OPAL message type %d\n", __func__, type); } } static void opal_intr(void *xintr) { uint64_t events = 0; opal_call(OPAL_HANDLE_INTERRUPT, (uint32_t)(uint64_t)xintr, vtophys(&events)); /* Wake up the heartbeat, if it's been setup. */ - if (events != 0 && opal_hb_proc != NULL) + if (be64toh(events) != 0 && opal_hb_proc != NULL) wakeup(opal_hb_proc); } diff --git a/sys/powerpc/powernv/opal_sensor.c b/sys/powerpc/powernv/opal_sensor.c index dae1f97bc989..a95746472975 100644 --- a/sys/powerpc/powernv/opal_sensor.c +++ b/sys/powerpc/powernv/opal_sensor.c @@ -1,331 +1,332 @@ /*- * Copyright (C) 2018 Justin Hibbits * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include "opal.h" struct opal_sensor_softc { device_t sc_dev; struct mtx sc_mtx; uint32_t sc_handle; uint32_t sc_min_handle; uint32_t sc_max_handle; char *sc_label; int sc_type; }; #define SENSOR_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) #define SENSOR_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) #define SENSOR_LOCK_INIT(_sc) \ mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ "opal-sensor", MTX_DEF) /* * A bit confusing, maybe. There are two types of nodes with compatible strings * of "ibm,opal-sensor". One hangs off /ibm,opal/, named "sensors", the other * hangs off of this node. For newbus attachments, we have one node (opalsens) * attach from opal0, and the others (opal_sensor) attach from opalsens. These * are the real sensors. */ enum opal_sensor_type { OPAL_SENSOR_TEMP = 0, /* From OPAL: degC */ OPAL_SENSOR_FAN = 1, /* From OPAL: RPM */ OPAL_SENSOR_POWER = 2, /* From OPAL: W */ OPAL_SENSOR_IN = 3, /* From OPAL: mV */ OPAL_SENSOR_ENERGY = 4, /* From OPAL: uJ */ OPAL_SENSOR_CURR = 5, /* From OPAL: mA */ OPAL_SENSOR_MAX }; /* This must be kept sorted with the enum above. */ const char *opal_sensor_types[] = { "temp", "fan", "power", "in", "energy", "curr" }; /* * Retrieve the raw value from OPAL. This will be cooked by the sysctl handler. */ static int opal_sensor_get_val(struct opal_sensor_softc *sc, uint32_t key, uint64_t *val) { struct opal_msg msg; uint32_t val32; int rv, token; token = opal_alloc_async_token(); SENSOR_LOCK(sc); rv = opal_call(OPAL_SENSOR_READ, key, token, vtophys(&val32)); if (rv == OPAL_ASYNC_COMPLETION) { /* Sleep a little to let things settle. */ DELAY(100); bzero(&msg, sizeof(msg)); rv = opal_wait_completion(&msg, sizeof(msg), token); if (rv == OPAL_SUCCESS) val32 = msg.params[0]; } SENSOR_UNLOCK(sc); if (rv == OPAL_SUCCESS) - *val = val32; + *val = be32toh(val32); else rv = EIO; opal_free_async_token(token); return (rv); } static int opal_sensor_sysctl(SYSCTL_HANDLER_ARGS) { struct opal_sensor_softc *sc; int error, result; uint32_t sensor; uint64_t sensval; sc = arg1; sensor = arg2; error = opal_sensor_get_val(sc, sensor, &sensval); if (error) return (error); result = sensval; switch (sc->sc_type) { case OPAL_SENSOR_TEMP: result = result * 10 + 2731; /* Convert to K */ break; case OPAL_SENSOR_POWER: result = result * 1000; /* Convert to mW */ break; } error = sysctl_handle_int(oidp, &result, 0, req); return (error); } static int opal_sensor_probe(device_t dev) { if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor")) return (ENXIO); device_set_desc(dev, "OPAL sensor"); return (BUS_PROBE_GENERIC); } static int opal_sensor_attach(device_t dev) { struct opal_sensor_softc *sc; struct sysctl_ctx_list *ctx; struct sysctl_oid *tree; char type[8]; phandle_t node; cell_t sensor_id; int i; sc = device_get_softc(dev); sc->sc_dev = dev; node = ofw_bus_get_node(dev); if (OF_getencprop(node, "sensor-data", &sensor_id, sizeof(sensor_id)) < 0) { device_printf(dev, "Missing sensor ID\n"); return (ENXIO); } if (OF_getprop(node, "sensor-type", type, sizeof(type)) < 0) { device_printf(dev, "Missing sensor type\n"); return (ENXIO); } sc->sc_type = -1; for (i = 0; i < OPAL_SENSOR_MAX; i++) { if (strcmp(type, opal_sensor_types[i]) == 0) { sc->sc_type = i; break; } } if (sc->sc_type == -1) { device_printf(dev, "Unknown sensor type '%s'\n", type); return (ENXIO); } ctx = device_get_sysctl_ctx(dev); tree = device_get_sysctl_tree(dev); sc->sc_handle = sensor_id; SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "sensor", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, sensor_id, opal_sensor_sysctl, (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", "current value"); SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "type", CTLFLAG_RD, __DECONST(char *, opal_sensor_types[sc->sc_type]), 0, ""); OF_getprop_alloc(node, "label", (void **)&sc->sc_label); SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "label", CTLFLAG_RD, sc->sc_label, 0, ""); - if (OF_getprop(node, "sensor-data-min", + if (OF_getencprop(node, "sensor-data-min", &sensor_id, sizeof(sensor_id)) > 0) { sc->sc_min_handle = sensor_id; SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "sensor_min", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, sensor_id, opal_sensor_sysctl, (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", "minimum value"); } - if (OF_getprop(node, "sensor-data-max", + if (OF_getencprop(node, "sensor-data-max", &sensor_id, sizeof(sensor_id)) > 0) { sc->sc_max_handle = sensor_id; SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "sensor_max", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, sensor_id, opal_sensor_sysctl, (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", "maximum value"); } SENSOR_LOCK_INIT(sc); return (0); } static device_method_t opal_sensor_methods[] = { DEVMETHOD(device_probe, opal_sensor_probe), DEVMETHOD(device_attach, opal_sensor_attach), }; static driver_t opal_sensor_driver = { "opal_sensor", opal_sensor_methods, sizeof(struct opal_sensor_softc) }; static devclass_t opal_sensor_devclass; DRIVER_MODULE(opal_sensor, opalsens, opal_sensor_driver, opal_sensor_devclass, NULL, NULL); static int opalsens_probe(device_t dev) { if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor")) return (ENXIO); device_set_desc(dev, "OPAL Sensors"); return (BUS_PROBE_GENERIC); } static int opalsens_attach(device_t dev) { phandle_t child; device_t cdev; struct ofw_bus_devinfo *dinfo; for (child = OF_child(ofw_bus_get_node(dev)); child != 0; child = OF_peer(child)) { dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { free(dinfo, M_DEVBUF); continue; } cdev = device_add_child(dev, NULL, -1); if (cdev == NULL) { device_printf(dev, "<%s>: device_add_child failed\n", dinfo->obd_name); ofw_bus_gen_destroy_devinfo(dinfo); free(dinfo, M_DEVBUF); continue; } device_set_ivars(cdev, dinfo); } return (bus_generic_attach(dev)); } static const struct ofw_bus_devinfo * opalsens_get_devinfo(device_t dev, device_t child) { return (device_get_ivars(child)); } static device_method_t opalsens_methods[] = { /* Device interface */ DEVMETHOD(device_probe, opalsens_probe), DEVMETHOD(device_attach, opalsens_attach), /* ofw_bus interface */ DEVMETHOD(ofw_bus_get_devinfo, opalsens_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 }; static driver_t opalsens_driver = { "opalsens", opalsens_methods, 0 }; static devclass_t opalsens_devclass; DRIVER_MODULE(opalsens, opal, opalsens_driver, opalsens_devclass, NULL, NULL);