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