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 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/cpu.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/sysctl.h> 39 40 #include <machine/bus.h> 41 #include <machine/cpu.h> 42 43 #include <dev/extres/clk/clk.h> 44 #include <dev/ofw/ofw_bus_subr.h> 45 46 #include "tegra_soctherm_if.h" 47 48 enum therm_info { 49 CORETEMP_TEMP, 50 CORETEMP_DELTA, 51 CORETEMP_RESOLUTION, 52 CORETEMP_TJMAX, 53 }; 54 55 struct tegra124_coretemp_softc { 56 device_t dev; 57 int overheat_log; 58 int core_max_temp; 59 int cpu_id; 60 device_t tsens_dev; 61 intptr_t tsens_id; 62 }; 63 64 static int 65 coretemp_get_val_sysctl(SYSCTL_HANDLER_ARGS) 66 { 67 device_t dev; 68 int val, temp, rv; 69 struct tegra124_coretemp_softc *sc; 70 enum therm_info type; 71 char stemp[16]; 72 73 dev = (device_t) arg1; 74 sc = device_get_softc(dev); 75 type = arg2; 76 77 rv = TEGRA_SOCTHERM_GET_TEMPERATURE(sc->tsens_dev, sc->dev, 78 sc->tsens_id, &temp); 79 if (rv != 0) { 80 device_printf(sc->dev, 81 "Cannot read temperature sensor %d: %d\n", 82 sc->tsens_id, rv); 83 return (rv); 84 } 85 86 switch (type) { 87 case CORETEMP_TEMP: 88 val = temp / 100; 89 val += 2731; 90 break; 91 case CORETEMP_DELTA: 92 val = (sc->core_max_temp - temp) / 1000; 93 break; 94 case CORETEMP_RESOLUTION: 95 val = 1; 96 break; 97 case CORETEMP_TJMAX: 98 val = sc->core_max_temp / 100; 99 val += 2731; 100 break; 101 } 102 103 if ((temp > sc->core_max_temp) && !sc->overheat_log) { 104 sc->overheat_log = 1; 105 106 /* 107 * Check for Critical Temperature Status and Critical 108 * Temperature Log. It doesn't really matter if the 109 * current temperature is invalid because the "Critical 110 * Temperature Log" bit will tell us if the Critical 111 * Temperature has * been reached in past. It's not 112 * directly related to the current temperature. 113 * 114 * If we reach a critical level, allow devctl(4) 115 * to catch this and shutdown the system. 116 */ 117 device_printf(dev, "critical temperature detected, " 118 "suggest system shutdown\n"); 119 snprintf(stemp, sizeof(stemp), "%d", val); 120 devctl_notify("coretemp", "Thermal", stemp, 121 "notify=0xcc"); 122 } else { 123 sc->overheat_log = 0; 124 } 125 126 return (sysctl_handle_int(oidp, 0, val, req)); 127 } 128 129 static int 130 tegra124_coretemp_ofw_parse(struct tegra124_coretemp_softc *sc) 131 { 132 int rv, ncells; 133 phandle_t node, xnode; 134 pcell_t *cells; 135 136 node = OF_peer(0); 137 node = ofw_bus_find_child(node, "thermal-zones"); 138 if (node <= 0) { 139 device_printf(sc->dev, "Cannot find 'thermal-zones'.\n"); 140 return (ENXIO); 141 } 142 143 node = ofw_bus_find_child(node, "cpu"); 144 if (node <= 0) { 145 device_printf(sc->dev, "Cannot find 'cpu'\n"); 146 return (ENXIO); 147 } 148 rv = ofw_bus_parse_xref_list_alloc(node, "thermal-sensors", 149 "#thermal-sensor-cells", 0, &xnode, &ncells, &cells); 150 if (rv != 0) { 151 device_printf(sc->dev, 152 "Cannot parse 'thermal-sensors' property.\n"); 153 return (ENXIO); 154 } 155 if (ncells != 1) { 156 device_printf(sc->dev, 157 "Invalid format of 'thermal-sensors' property(%d).\n", 158 ncells); 159 return (ENXIO); 160 } 161 162 sc->tsens_id = 0x100 + sc->cpu_id; //cells[0]; 163 OF_prop_free(cells); 164 165 sc->tsens_dev = OF_device_from_xref(xnode); 166 if (sc->tsens_dev == NULL) { 167 device_printf(sc->dev, 168 "Cannot find thermal sensors device."); 169 return (ENXIO); 170 } 171 return (0); 172 } 173 174 static void 175 tegra124_coretemp_identify(driver_t *driver, device_t parent) 176 { 177 phandle_t root; 178 179 root = OF_finddevice("/"); 180 if (!ofw_bus_node_is_compatible(root, "nvidia,tegra124")) 181 return; 182 if (device_find_child(parent, "tegra124_coretemp", -1) != NULL) 183 return; 184 if (BUS_ADD_CHILD(parent, 0, "tegra124_coretemp", -1) == NULL) 185 device_printf(parent, "add child failed\n"); 186 } 187 188 static int 189 tegra124_coretemp_probe(device_t dev) 190 { 191 192 device_set_desc(dev, "CPU Thermal Sensor"); 193 return (0); 194 } 195 196 static int 197 tegra124_coretemp_attach(device_t dev) 198 { 199 struct tegra124_coretemp_softc *sc; 200 device_t pdev; 201 struct sysctl_oid *oid; 202 struct sysctl_ctx_list *ctx; 203 int rv; 204 205 sc = device_get_softc(dev); 206 sc->dev = dev; 207 sc->cpu_id = device_get_unit(dev); 208 sc->core_max_temp = 102000; 209 pdev = device_get_parent(dev); 210 211 rv = tegra124_coretemp_ofw_parse(sc); 212 if (rv != 0) 213 return (rv); 214 215 ctx = device_get_sysctl_ctx(dev); 216 217 oid = SYSCTL_ADD_NODE(ctx, 218 SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)), OID_AUTO, 219 "coretemp", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 220 "Per-CPU thermal information"); 221 222 /* 223 * Add the MIBs to dev.cpu.N and dev.cpu.N.coretemp. 224 */ 225 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)), 226 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 227 dev, CORETEMP_TEMP, coretemp_get_val_sysctl, "IK", 228 "Current temperature"); 229 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "delta", 230 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_DELTA, 231 coretemp_get_val_sysctl, "I", 232 "Delta between TCC activation and current temperature"); 233 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "resolution", 234 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_RESOLUTION, 235 coretemp_get_val_sysctl, "I", 236 "Resolution of CPU thermal sensor"); 237 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "tjmax", 238 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_TJMAX, 239 coretemp_get_val_sysctl, "IK", 240 "TCC activation temperature"); 241 242 return (0); 243 } 244 245 static int 246 tegra124_coretemp_detach(device_t dev) 247 { 248 return (0); 249 } 250 251 static device_method_t tegra124_coretemp_methods[] = { 252 /* Device interface */ 253 DEVMETHOD(device_identify, tegra124_coretemp_identify), 254 DEVMETHOD(device_probe, tegra124_coretemp_probe), 255 DEVMETHOD(device_attach, tegra124_coretemp_attach), 256 DEVMETHOD(device_detach, tegra124_coretemp_detach), 257 258 DEVMETHOD_END 259 }; 260 261 static DEFINE_CLASS_0(tegra124_coretemp, tegra124_coretemp_driver, 262 tegra124_coretemp_methods, sizeof(struct tegra124_coretemp_softc)); 263 DRIVER_MODULE(tegra124_coretemp, cpu, tegra124_coretemp_driver, NULL, NULL); 264