xref: /freebsd/sys/powerpc/powernv/opal_sensor.c (revision 405c3050f102b8c74782f0366c8ead927bd07b68)
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/systm.h>
31 #include <sys/module.h>
32 #include <sys/types.h>
33 #include <sys/proc.h>
34 #include <sys/sysctl.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(uint32_t key, uint64_t *val)
95 {
96 	struct opal_msg msg;
97 	uint32_t val32;
98 	int i, rv;
99 
100 	rv = opal_call(OPAL_SENSOR_READ, key, key, vtophys(&val32));
101 
102 	if (rv == OPAL_ASYNC_COMPLETION) {
103 		/* Sleep a little to let things settle. */
104 		DELAY(100);
105 		bzero(&msg, sizeof(msg));
106 		i = 0;
107 		do {
108 			rv = opal_call(OPAL_CHECK_ASYNC_COMPLETION,
109 			    vtophys(&msg), sizeof(msg), key);
110 			/* Sleep for ~100us if necessary. */
111 			if (rv == OPAL_BUSY)
112 				DELAY(100);
113 		} while (rv == OPAL_BUSY && ++i < 10);
114 		if (rv != OPAL_SUCCESS)
115 			return (EIO);
116 		val32 = msg.params[0];
117 	}
118 
119 	if (rv != OPAL_SUCCESS)
120 		return (EIO);
121 
122 	*val = val32;
123 
124 	return (0);
125 }
126 
127 static int
128 opal_sensor_sysctl(SYSCTL_HANDLER_ARGS)
129 {
130 	struct opal_sensor_softc *sc;
131 	int error, result;
132 	uint32_t sensor;
133 	uint64_t sensval;
134 
135 	sc = arg1;
136 	sensor = arg2;
137 
138 	SENSOR_LOCK(sc);
139 	error = opal_sensor_get_val(sensor, &sensval);
140 	SENSOR_UNLOCK(sc);
141 
142 	if (error)
143 		return (error);
144 
145 	result = sensval;
146 
147 	switch (sc->sc_type) {
148 	case OPAL_SENSOR_TEMP:
149 		result = result * 10 + 2731; /* Convert to K */
150 		break;
151 	case OPAL_SENSOR_POWER:
152 		result = result * 1000; /* Convert to mW */
153 		break;
154 	}
155 
156 	error = sysctl_handle_int(oidp, &result, 0, req);
157 
158 	return (error);
159 }
160 
161 static int
162 opal_sensor_probe(device_t dev)
163 {
164 	if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor"))
165 		return (ENXIO);
166 
167 	device_set_desc(dev, "OPAL sensor");
168 	return (BUS_PROBE_GENERIC);
169 }
170 
171 static int
172 opal_sensor_attach(device_t dev)
173 {
174 	struct opal_sensor_softc *sc;
175 	struct sysctl_ctx_list *ctx;
176 	struct sysctl_oid *tree;
177 	char		type[8];
178 	phandle_t	node;
179 	cell_t		sensor_id;
180 	int		i;
181 
182 	sc = device_get_softc(dev);
183 	sc->sc_dev = dev;
184 
185 	node = ofw_bus_get_node(dev);
186 
187 	if (OF_getencprop(node, "sensor-data", &sensor_id, sizeof(sensor_id)) < 0) {
188 		device_printf(dev, "Missing sensor ID\n");
189 		return (ENXIO);
190 	}
191 	if (OF_getprop(node, "sensor-type", type, sizeof(type)) < 0) {
192 		device_printf(dev, "Missing sensor type\n");
193 		return (ENXIO);
194 	}
195 
196 	sc->sc_type = -1;
197 	for (i = 0; i < OPAL_SENSOR_MAX; i++) {
198 		if (strcmp(type, opal_sensor_types[i]) == 0) {
199 			sc->sc_type = i;
200 			break;
201 		}
202 	}
203 	if (sc->sc_type == -1) {
204 		device_printf(dev, "Unknown sensor type '%s'\n", type);
205 		return (ENXIO);
206 	}
207 
208 	ctx = device_get_sysctl_ctx(dev);
209 	tree = device_get_sysctl_tree(dev);
210 
211 	sc->sc_handle = sensor_id;
212 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
213 	    "sensor", CTLTYPE_INT | CTLFLAG_RD, sc, sensor_id,
214 	    opal_sensor_sysctl, (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I",
215 	    "current value");
216 
217 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "type",
218 	    CTLFLAG_RD, __DECONST(char *, opal_sensor_types[sc->sc_type]),
219 	    0, "");
220 
221 	OF_getprop_alloc(node, "label", (void **)&sc->sc_label);
222 	SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "label",
223 	    CTLFLAG_RD, sc->sc_label, 0, "");
224 
225 	if (OF_getprop(node, "sensor-data-min",
226 	    &sensor_id, sizeof(sensor_id)) > 0) {
227 		sc->sc_min_handle = sensor_id;
228 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
229 		    "sensor_min", CTLTYPE_INT | CTLFLAG_RD, sc, sensor_id,
230 		    opal_sensor_sysctl,
231 		    (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I",
232 		    "minimum value");
233 	}
234 
235 	if (OF_getprop(node, "sensor-data-max",
236 	    &sensor_id, sizeof(sensor_id)) > 0) {
237 		sc->sc_max_handle = sensor_id;
238 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
239 		    "sensor_max", CTLTYPE_INT | CTLFLAG_RD, sc, sensor_id,
240 		    opal_sensor_sysctl,
241 		    (sc->sc_type == OPAL_SENSOR_TEMP) ? "IK" : "I",
242 		    "maximum value");
243 	}
244 
245 	SENSOR_LOCK_INIT(sc);
246 
247 	return (0);
248 }
249 
250 static device_method_t opal_sensor_methods[] = {
251 	DEVMETHOD(device_probe,		opal_sensor_probe),
252 	DEVMETHOD(device_attach,		opal_sensor_attach),
253 };
254 
255 static driver_t opal_sensor_driver = {
256         "opal_sensor",
257         opal_sensor_methods,
258         sizeof(struct opal_sensor_softc)
259 };
260 
261 static devclass_t opal_sensor_devclass;
262 
263 DRIVER_MODULE(opal_sensor, opalsens, opal_sensor_driver, opal_sensor_devclass,
264     NULL, NULL);
265 
266 
267 static int
268 opalsens_probe(device_t dev)
269 {
270 
271 	if (!ofw_bus_is_compatible(dev, "ibm,opal-sensor"))
272 		return (ENXIO);
273 
274 	device_set_desc(dev, "OPAL Sensors");
275 	return (BUS_PROBE_GENERIC);
276 }
277 
278 static int
279 opalsens_attach(device_t dev)
280 {
281 	phandle_t child;
282 	device_t cdev;
283 	struct ofw_bus_devinfo *dinfo;
284 
285 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
286 	    child = OF_peer(child)) {
287 		dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
288 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
289 			free(dinfo, M_DEVBUF);
290 			continue;
291 		}
292 		cdev = device_add_child(dev, NULL, -1);
293 		if (cdev == NULL) {
294 			device_printf(dev, "<%s>: device_add_child failed\n",
295 			    dinfo->obd_name);
296 			ofw_bus_gen_destroy_devinfo(dinfo);
297 			free(dinfo, M_DEVBUF);
298 			continue;
299 		}
300 		device_set_ivars(cdev, dinfo);
301 	}
302 
303 	return (bus_generic_attach(dev));
304 }
305 
306 static const struct ofw_bus_devinfo *
307 opalsens_get_devinfo(device_t dev, device_t child)
308 {
309         return (device_get_ivars(child));
310 }
311 
312 static device_method_t opalsens_methods[] = {
313 	/* Device interface */
314 	DEVMETHOD(device_probe,		opalsens_probe),
315 	DEVMETHOD(device_attach,	opalsens_attach),
316 
317 	/* ofw_bus interface */
318 	DEVMETHOD(ofw_bus_get_devinfo,	opalsens_get_devinfo),
319 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
320 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
321 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
322 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
323 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
324 
325 	DEVMETHOD_END
326 };
327 
328 static driver_t opalsens_driver = {
329         "opalsens",
330         opalsens_methods,
331         0
332 };
333 
334 static devclass_t opalsens_devclass;
335 
336 DRIVER_MODULE(opalsens, opal, opalsens_driver, opalsens_devclass, NULL, NULL);
337