1 /*- 2 * Copyright (C) 2018 Justin Hibbits 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 19 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 21 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 #include <sys/param.h> 27 #include <sys/kernel.h> 28 #include <sys/lock.h> 29 #include <sys/module.h> 30 #include <sys/mutex.h> 31 #include <sys/proc.h> 32 #include <sys/sysctl.h> 33 #include <sys/systm.h> 34 #include <sys/endian.h> 35 36 #include <vm/vm.h> 37 #include <vm/pmap.h> 38 39 #include <machine/bus.h> 40 41 #include <dev/ofw/openfirm.h> 42 #include <dev/ofw/ofw_bus.h> 43 #include <dev/ofw/ofw_bus_subr.h> 44 45 #include "opal.h" 46 47 struct opal_sensor_softc { 48 device_t sc_dev; 49 struct mtx sc_mtx; 50 uint32_t sc_handle; 51 uint32_t sc_min_handle; 52 uint32_t sc_max_handle; 53 char *sc_label; 54 int sc_type; 55 }; 56 57 #define SENSOR_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 58 #define SENSOR_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 59 #define SENSOR_LOCK_INIT(_sc) \ 60 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \ 61 "opal-sensor", MTX_DEF) 62 63 /* 64 * A bit confusing, maybe. There are two types of nodes with compatible strings 65 * of "ibm,opal-sensor". One hangs off /ibm,opal/, named "sensors", the other 66 * hangs off of this node. For newbus attachments, we have one node (opalsens) 67 * attach from opal0, and the others (opal_sensor) attach from opalsens. These 68 * are the real sensors. 69 */ 70 enum opal_sensor_type { 71 OPAL_SENSOR_TEMP = 0, /* From OPAL: degC */ 72 OPAL_SENSOR_FAN = 1, /* From OPAL: RPM */ 73 OPAL_SENSOR_POWER = 2, /* From OPAL: W */ 74 OPAL_SENSOR_IN = 3, /* From OPAL: mV */ 75 OPAL_SENSOR_ENERGY = 4, /* From OPAL: uJ */ 76 OPAL_SENSOR_CURR = 5, /* From OPAL: mA */ 77 OPAL_SENSOR_MAX 78 }; 79 80 /* This must be kept sorted with the enum above. */ 81 const char *opal_sensor_types[] = { 82 "temp", 83 "fan", 84 "power", 85 "in", 86 "energy", 87 "curr" 88 }; 89 90 /* 91 * Retrieve the raw value from OPAL. This will be cooked by the sysctl handler. 92 */ 93 static int 94 opal_sensor_get_val(struct opal_sensor_softc *sc, uint32_t key, uint64_t *val) 95 { 96 struct opal_msg msg; 97 uint32_t val32; 98 int rv, token; 99 100 token = opal_alloc_async_token(); 101 SENSOR_LOCK(sc); 102 rv = opal_call(OPAL_SENSOR_READ, key, token, vtophys(&val32)); 103 104 if (rv == OPAL_ASYNC_COMPLETION) { 105 /* Sleep a little to let things settle. */ 106 DELAY(100); 107 bzero(&msg, sizeof(msg)); 108 rv = opal_wait_completion(&msg, sizeof(msg), token); 109 110 if (rv == OPAL_SUCCESS) 111 val32 = msg.params[0]; 112 } 113 SENSOR_UNLOCK(sc); 114 115 if (rv == OPAL_SUCCESS) 116 *val = be32toh(val32); 117 else 118 rv = EIO; 119 120 opal_free_async_token(token); 121 return (rv); 122 } 123 124 static int 125 opal_sensor_sysctl(SYSCTL_HANDLER_ARGS) 126 { 127 struct opal_sensor_softc *sc; 128 int error, result; 129 uint32_t sensor; 130 uint64_t sensval; 131 132 sc = arg1; 133 sensor = arg2; 134 135 error = opal_sensor_get_val(sc, sensor, &sensval); 136 137 if (error) 138 return (error); 139 140 result = sensval; 141 142 switch (sc->sc_type) { 143 case OPAL_SENSOR_TEMP: 144 result = result * 10 + 2731; /* Convert to K */ 145 break; 146 case OPAL_SENSOR_POWER: 147 result = result * 1000; /* Convert to mW */ 148 break; 149 } 150 151 error = sysctl_handle_int(oidp, &result, 0, req); 152 153 return (error); 154 } 155 156 static int 157 opal_sensor_probe(device_t dev) 158 { 159 if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor")) 160 return (ENXIO); 161 162 device_set_desc(dev, "OPAL sensor"); 163 return (BUS_PROBE_GENERIC); 164 } 165 166 static int 167 opal_sensor_attach(device_t dev) 168 { 169 struct opal_sensor_softc *sc; 170 struct sysctl_ctx_list *ctx; 171 struct sysctl_oid *tree; 172 char type[8]; 173 phandle_t node; 174 cell_t sensor_id; 175 int i; 176 177 sc = device_get_softc(dev); 178 sc->sc_dev = dev; 179 180 node = ofw_bus_get_node(dev); 181 182 if (OF_getencprop(node, "sensor-data", &sensor_id, sizeof(sensor_id)) < 0) { 183 device_printf(dev, "Missing sensor ID\n"); 184 return (ENXIO); 185 } 186 if (OF_getprop(node, "sensor-type", type, sizeof(type)) < 0) { 187 device_printf(dev, "Missing sensor type\n"); 188 return (ENXIO); 189 } 190 191 sc->sc_type = -1; 192 for (i = 0; i < OPAL_SENSOR_MAX; i++) { 193 if (strcmp(type, opal_sensor_types[i]) == 0) { 194 sc->sc_type = i; 195 break; 196 } 197 } 198 if (sc->sc_type == -1) { 199 device_printf(dev, "Unknown sensor type '%s'\n", type); 200 return (ENXIO); 201 } 202 203 ctx = device_get_sysctl_ctx(dev); 204 tree = device_get_sysctl_tree(dev); 205 206 sc->sc_handle = sensor_id; 207 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 208 "sensor", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, 209 sensor_id, opal_sensor_sysctl, 210 (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", "current value"); 211 212 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "type", 213 CTLFLAG_RD, __DECONST(char *, opal_sensor_types[sc->sc_type]), 214 0, ""); 215 216 OF_getprop_alloc(node, "label", (void **)&sc->sc_label); 217 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "label", 218 CTLFLAG_RD, sc->sc_label, 0, ""); 219 220 if (OF_getencprop(node, "sensor-data-min", 221 &sensor_id, sizeof(sensor_id)) > 0) { 222 sc->sc_min_handle = sensor_id; 223 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 224 "sensor_min", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 225 sc, sensor_id, opal_sensor_sysctl, 226 (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", 227 "minimum value"); 228 } 229 230 if (OF_getencprop(node, "sensor-data-max", 231 &sensor_id, sizeof(sensor_id)) > 0) { 232 sc->sc_max_handle = sensor_id; 233 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 234 "sensor_max", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 235 sc, sensor_id, opal_sensor_sysctl, 236 (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I", 237 "maximum value"); 238 } 239 240 SENSOR_LOCK_INIT(sc); 241 242 return (0); 243 } 244 245 static device_method_t opal_sensor_methods[] = { 246 DEVMETHOD(device_probe, opal_sensor_probe), 247 DEVMETHOD(device_attach, opal_sensor_attach), 248 }; 249 250 static driver_t opal_sensor_driver = { 251 "opal_sensor", 252 opal_sensor_methods, 253 sizeof(struct opal_sensor_softc) 254 }; 255 256 DRIVER_MODULE(opal_sensor, opalsens, opal_sensor_driver, NULL, NULL); 257 258 static int 259 opalsens_probe(device_t dev) 260 { 261 262 if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor")) 263 return (ENXIO); 264 265 device_set_desc(dev, "OPAL Sensors"); 266 return (BUS_PROBE_GENERIC); 267 } 268 269 static int 270 opalsens_attach(device_t dev) 271 { 272 phandle_t child; 273 device_t cdev; 274 struct ofw_bus_devinfo *dinfo; 275 276 for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 277 child = OF_peer(child)) { 278 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 279 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { 280 free(dinfo, M_DEVBUF); 281 continue; 282 } 283 cdev = device_add_child(dev, NULL, -1); 284 if (cdev == NULL) { 285 device_printf(dev, "<%s>: device_add_child failed\n", 286 dinfo->obd_name); 287 ofw_bus_gen_destroy_devinfo(dinfo); 288 free(dinfo, M_DEVBUF); 289 continue; 290 } 291 device_set_ivars(cdev, dinfo); 292 } 293 294 return (bus_generic_attach(dev)); 295 } 296 297 static const struct ofw_bus_devinfo * 298 opalsens_get_devinfo(device_t dev, device_t child) 299 { 300 return (device_get_ivars(child)); 301 } 302 303 static device_method_t opalsens_methods[] = { 304 /* Device interface */ 305 DEVMETHOD(device_probe, opalsens_probe), 306 DEVMETHOD(device_attach, opalsens_attach), 307 308 /* ofw_bus interface */ 309 DEVMETHOD(ofw_bus_get_devinfo, opalsens_get_devinfo), 310 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 311 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 312 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 313 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 314 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 315 316 DEVMETHOD_END 317 }; 318 319 static driver_t opalsens_driver = { 320 "opalsens", 321 opalsens_methods, 322 0 323 }; 324 325 DRIVER_MODULE(opalsens, opal, opalsens_driver, NULL, NULL); 326