xref: /freebsd/sys/arm/nvidia/tegra124/tegra124_coretemp.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*-
2  * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 #include <sys/cpu.h>
32 #include <sys/kernel.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/sysctl.h>
37 
38 #include <machine/bus.h>
39 #include <machine/cpu.h>
40 
41 #include <dev/extres/clk/clk.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include "tegra_soctherm_if.h"
45 
46 enum therm_info {
47 	CORETEMP_TEMP,
48 	CORETEMP_DELTA,
49 	CORETEMP_RESOLUTION,
50 	CORETEMP_TJMAX,
51 };
52 
53 struct tegra124_coretemp_softc {
54 	device_t		dev;
55 	int			overheat_log;
56 	int			core_max_temp;
57 	int			cpu_id;
58 	device_t		tsens_dev;
59 	intptr_t		tsens_id;
60 };
61 
62 static int
63 coretemp_get_val_sysctl(SYSCTL_HANDLER_ARGS)
64 {
65 	device_t dev;
66 	int val, temp, rv;
67 	struct tegra124_coretemp_softc *sc;
68 	enum therm_info type;
69 	char stemp[16];
70 
71 	dev = (device_t) arg1;
72 	sc = device_get_softc(dev);
73 	type = arg2;
74 
75 	rv = TEGRA_SOCTHERM_GET_TEMPERATURE(sc->tsens_dev, sc->dev,
76 	     sc->tsens_id, &temp);
77 	if (rv != 0) {
78 		device_printf(sc->dev,
79 		    "Cannot read temperature sensor %d:  %d\n",
80 		    sc->tsens_id, rv);
81 		return (rv);
82 	}
83 
84 	switch (type) {
85 	case CORETEMP_TEMP:
86 		val = temp / 100;
87 		val +=  2731;
88 		break;
89 	case CORETEMP_DELTA:
90 		val = (sc->core_max_temp - temp) / 1000;
91 		break;
92 	case CORETEMP_RESOLUTION:
93 		val = 1;
94 		break;
95 	case CORETEMP_TJMAX:
96 		val = sc->core_max_temp / 100;
97 		val +=  2731;
98 		break;
99 	}
100 
101 	if ((temp > sc->core_max_temp)  && !sc->overheat_log) {
102 		sc->overheat_log = 1;
103 
104 		/*
105 		 * Check for Critical Temperature Status and Critical
106 		 * Temperature Log.  It doesn't really matter if the
107 		 * current temperature is invalid because the "Critical
108 		 * Temperature Log" bit will tell us if the Critical
109 		 * Temperature has * been reached in past. It's not
110 		 * directly related to the current temperature.
111 		 *
112 		 * If we reach a critical level, allow devctl(4)
113 		 * to catch this and shutdown the system.
114 		 */
115 		device_printf(dev, "critical temperature detected, "
116 		    "suggest system shutdown\n");
117 		snprintf(stemp, sizeof(stemp), "%d", val);
118 		devctl_notify("coretemp", "Thermal", stemp,
119 		    "notify=0xcc");
120 	} else {
121 		sc->overheat_log = 0;
122 	}
123 
124 	return (sysctl_handle_int(oidp, 0, val, req));
125 }
126 
127 static int
128 tegra124_coretemp_ofw_parse(struct tegra124_coretemp_softc *sc)
129 {
130 	int rv, ncells;
131 	phandle_t node, xnode;
132 	pcell_t *cells;
133 
134 	node = OF_peer(0);
135 	node = ofw_bus_find_child(node, "thermal-zones");
136 	if (node <= 0) {
137 		device_printf(sc->dev, "Cannot find 'thermal-zones'.\n");
138 		return (ENXIO);
139 	}
140 
141 	node = ofw_bus_find_child(node, "cpu");
142 	if (node <= 0) {
143 		device_printf(sc->dev, "Cannot find 'cpu'\n");
144 		return (ENXIO);
145 	}
146 	rv = ofw_bus_parse_xref_list_alloc(node, "thermal-sensors",
147 	    "#thermal-sensor-cells", 0, &xnode, &ncells, &cells);
148 	if (rv != 0) {
149 		device_printf(sc->dev,
150 		    "Cannot parse 'thermal-sensors' property.\n");
151 		return (ENXIO);
152 	}
153 	if (ncells != 1) {
154 		device_printf(sc->dev,
155 		    "Invalid format of 'thermal-sensors' property(%d).\n",
156 		    ncells);
157 		return (ENXIO);
158 	}
159 
160 	sc->tsens_id = 0x100 + sc->cpu_id; //cells[0];
161 	OF_prop_free(cells);
162 
163 	sc->tsens_dev = OF_device_from_xref(xnode);
164 	if (sc->tsens_dev == NULL) {
165 		device_printf(sc->dev,
166 		    "Cannot find thermal sensors device.");
167 		return (ENXIO);
168 	}
169 	return (0);
170 }
171 
172 static void
173 tegra124_coretemp_identify(driver_t *driver, device_t parent)
174 {
175 	phandle_t root;
176 
177 	root = OF_finddevice("/");
178 	if (!ofw_bus_node_is_compatible(root, "nvidia,tegra124"))
179 		return;
180 	if (device_find_child(parent, "tegra124_coretemp", -1) != NULL)
181 		return;
182 	if (BUS_ADD_CHILD(parent, 0, "tegra124_coretemp", -1) == NULL)
183 		device_printf(parent, "add child failed\n");
184 }
185 
186 static int
187 tegra124_coretemp_probe(device_t dev)
188 {
189 
190 	device_set_desc(dev, "CPU Thermal Sensor");
191 	return (0);
192 }
193 
194 static int
195 tegra124_coretemp_attach(device_t dev)
196 {
197 	struct tegra124_coretemp_softc *sc;
198 	device_t pdev;
199 	struct sysctl_oid *oid;
200 	struct sysctl_ctx_list *ctx;
201 	int rv;
202 
203 	sc = device_get_softc(dev);
204 	sc->dev = dev;
205 	sc->cpu_id = device_get_unit(dev);
206 	sc->core_max_temp = 102000;
207 	pdev = device_get_parent(dev);
208 
209 	rv = tegra124_coretemp_ofw_parse(sc);
210 	if (rv != 0)
211 		return (rv);
212 
213 	ctx = device_get_sysctl_ctx(dev);
214 
215 	oid = SYSCTL_ADD_NODE(ctx,
216 	    SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)), OID_AUTO,
217 	    "coretemp", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
218 	    "Per-CPU thermal information");
219 
220 	/*
221 	 * Add the MIBs to dev.cpu.N and dev.cpu.N.coretemp.
222 	 */
223 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
224 	    OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
225 	    dev, CORETEMP_TEMP, coretemp_get_val_sysctl, "IK",
226 	    "Current temperature");
227 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "delta",
228 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_DELTA,
229 	    coretemp_get_val_sysctl, "I",
230 	    "Delta between TCC activation and current temperature");
231 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "resolution",
232 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_RESOLUTION,
233 	    coretemp_get_val_sysctl, "I",
234 	    "Resolution of CPU thermal sensor");
235 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "tjmax",
236 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_TJMAX,
237 	    coretemp_get_val_sysctl, "IK",
238 	    "TCC activation temperature");
239 
240 	return (0);
241 }
242 
243 static int
244 tegra124_coretemp_detach(device_t dev)
245 {
246 	return (0);
247 }
248 
249 static device_method_t tegra124_coretemp_methods[] = {
250 	/* Device interface */
251 	DEVMETHOD(device_identify,	tegra124_coretemp_identify),
252 	DEVMETHOD(device_probe,		tegra124_coretemp_probe),
253 	DEVMETHOD(device_attach,	tegra124_coretemp_attach),
254 	DEVMETHOD(device_detach,	tegra124_coretemp_detach),
255 
256 	DEVMETHOD_END
257 };
258 
259 static DEFINE_CLASS_0(tegra124_coretemp, tegra124_coretemp_driver,
260     tegra124_coretemp_methods, sizeof(struct tegra124_coretemp_softc));
261 DRIVER_MODULE(tegra124_coretemp, cpu, tegra124_coretemp_driver, NULL, NULL);
262