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