Path: blob/main/sys/arm/nvidia/tegra124/tegra124_coretemp.c
39507 views
/*-1* Copyright (c) 2016 Michal Meloun <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/param.h>27#include <sys/systm.h>28#include <sys/bus.h>29#include <sys/cpu.h>30#include <sys/kernel.h>31#include <sys/lock.h>32#include <sys/malloc.h>33#include <sys/module.h>34#include <sys/sysctl.h>3536#include <machine/bus.h>37#include <machine/cpu.h>3839#include <dev/clk/clk.h>40#include <dev/ofw/ofw_bus_subr.h>4142#include "tegra_soctherm_if.h"4344enum therm_info {45CORETEMP_TEMP,46CORETEMP_DELTA,47CORETEMP_RESOLUTION,48CORETEMP_TJMAX,49};5051struct tegra124_coretemp_softc {52device_t dev;53int overheat_log;54int core_max_temp;55int cpu_id;56device_t tsens_dev;57intptr_t tsens_id;58};5960static int61coretemp_get_val_sysctl(SYSCTL_HANDLER_ARGS)62{63device_t dev;64int val, temp, rv;65struct tegra124_coretemp_softc *sc;66enum therm_info type;67char stemp[16];6869dev = (device_t) arg1;70sc = device_get_softc(dev);71type = arg2;7273rv = TEGRA_SOCTHERM_GET_TEMPERATURE(sc->tsens_dev, sc->dev,74sc->tsens_id, &temp);75if (rv != 0) {76device_printf(sc->dev,77"Cannot read temperature sensor %d: %d\n",78sc->tsens_id, rv);79return (rv);80}8182switch (type) {83case CORETEMP_TEMP:84val = temp / 100;85val += 2731;86break;87case CORETEMP_DELTA:88val = (sc->core_max_temp - temp) / 1000;89break;90case CORETEMP_RESOLUTION:91val = 1;92break;93case CORETEMP_TJMAX:94val = sc->core_max_temp / 100;95val += 2731;96break;97}9899if ((temp > sc->core_max_temp) && !sc->overheat_log) {100sc->overheat_log = 1;101102/*103* Check for Critical Temperature Status and Critical104* Temperature Log. It doesn't really matter if the105* current temperature is invalid because the "Critical106* Temperature Log" bit will tell us if the Critical107* Temperature has * been reached in past. It's not108* directly related to the current temperature.109*110* If we reach a critical level, allow devctl(4)111* to catch this and shutdown the system.112*/113device_printf(dev, "critical temperature detected, "114"suggest system shutdown\n");115snprintf(stemp, sizeof(stemp), "%d", val);116devctl_notify("coretemp", "Thermal", stemp,117"notify=0xcc");118} else {119sc->overheat_log = 0;120}121122return (sysctl_handle_int(oidp, 0, val, req));123}124125static int126tegra124_coretemp_ofw_parse(struct tegra124_coretemp_softc *sc)127{128int rv, ncells;129phandle_t node, xnode;130pcell_t *cells;131132node = OF_peer(0);133node = ofw_bus_find_child(node, "thermal-zones");134if (node <= 0) {135device_printf(sc->dev, "Cannot find 'thermal-zones'.\n");136return (ENXIO);137}138139node = ofw_bus_find_child(node, "cpu");140if (node <= 0) {141device_printf(sc->dev, "Cannot find 'cpu'\n");142return (ENXIO);143}144rv = ofw_bus_parse_xref_list_alloc(node, "thermal-sensors",145"#thermal-sensor-cells", 0, &xnode, &ncells, &cells);146if (rv != 0) {147device_printf(sc->dev,148"Cannot parse 'thermal-sensors' property.\n");149return (ENXIO);150}151if (ncells != 1) {152device_printf(sc->dev,153"Invalid format of 'thermal-sensors' property(%d).\n",154ncells);155return (ENXIO);156}157158sc->tsens_id = 0x100 + sc->cpu_id; //cells[0];159OF_prop_free(cells);160161sc->tsens_dev = OF_device_from_xref(xnode);162if (sc->tsens_dev == NULL) {163device_printf(sc->dev,164"Cannot find thermal sensors device.");165return (ENXIO);166}167return (0);168}169170static void171tegra124_coretemp_identify(driver_t *driver, device_t parent)172{173phandle_t root;174175root = OF_finddevice("/");176if (!ofw_bus_node_is_compatible(root, "nvidia,tegra124"))177return;178if (device_find_child(parent, "tegra124_coretemp", DEVICE_UNIT_ANY) != NULL)179return;180if (BUS_ADD_CHILD(parent, 0, "tegra124_coretemp", DEVICE_UNIT_ANY) == NULL)181device_printf(parent, "add child failed\n");182}183184static int185tegra124_coretemp_probe(device_t dev)186{187188device_set_desc(dev, "CPU Thermal Sensor");189return (0);190}191192static int193tegra124_coretemp_attach(device_t dev)194{195struct tegra124_coretemp_softc *sc;196device_t pdev;197struct sysctl_oid *oid;198struct sysctl_ctx_list *ctx;199int rv;200201sc = device_get_softc(dev);202sc->dev = dev;203sc->cpu_id = device_get_unit(dev);204sc->core_max_temp = 102000;205pdev = device_get_parent(dev);206207rv = tegra124_coretemp_ofw_parse(sc);208if (rv != 0)209return (rv);210211ctx = device_get_sysctl_ctx(dev);212213oid = SYSCTL_ADD_NODE(ctx,214SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)), OID_AUTO,215"coretemp", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,216"Per-CPU thermal information");217218/*219* Add the MIBs to dev.cpu.N and dev.cpu.N.coretemp.220*/221SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),222OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,223dev, CORETEMP_TEMP, coretemp_get_val_sysctl, "IK",224"Current temperature");225SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "delta",226CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_DELTA,227coretemp_get_val_sysctl, "I",228"Delta between TCC activation and current temperature");229SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "resolution",230CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_RESOLUTION,231coretemp_get_val_sysctl, "I",232"Resolution of CPU thermal sensor");233SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "tjmax",234CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, CORETEMP_TJMAX,235coretemp_get_val_sysctl, "IK",236"TCC activation temperature");237238return (0);239}240241static int242tegra124_coretemp_detach(device_t dev)243{244return (0);245}246247static device_method_t tegra124_coretemp_methods[] = {248/* Device interface */249DEVMETHOD(device_identify, tegra124_coretemp_identify),250DEVMETHOD(device_probe, tegra124_coretemp_probe),251DEVMETHOD(device_attach, tegra124_coretemp_attach),252DEVMETHOD(device_detach, tegra124_coretemp_detach),253254DEVMETHOD_END255};256257static DEFINE_CLASS_0(tegra124_coretemp, tegra124_coretemp_driver,258tegra124_coretemp_methods, sizeof(struct tegra124_coretemp_softc));259DRIVER_MODULE(tegra124_coretemp, cpu, tegra124_coretemp_driver, NULL, NULL);260261262