diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-11-14 14:42:31 +0900 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-11-14 14:42:31 +0900 |
commit | 549608eadb31eac5d579ed70a21ac722bdf72861 (patch) | |
tree | 0ecc4b03ed9feca47b7b5beb2f84d27f0c6f9d8d | |
parent | 2f466d33f5f60542d3d82c0477de5863b22c94b9 (diff) | |
parent | 86e0a0bdf81c2dfa2a5a258dbb52f49c40ebc197 (diff) |
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
Pull thermal management updates from Zhang Rui:
"This time we only have a few changes as there are no soc thermal
changes from Eduardo. The only big change is the introduction of
TMON, a tool to help visualize, tune, and test the thermal subsystem.
The rest is mostly cleanups and fixes all over.
Specifics:
- introduce TMON, a tool base on thermal sysfs I/F. It can be used
to visualize, tune and test the thermal subsystem.
- fix a zone/cooling device binding problem, when both thermal zone
bind parameters and .bind() callback are available"
* 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux:
tools/thermal: Introduce tmon, a tool for thermal subsystem
thermal: Fix binding problem when there is thermal zone params
thermal: cpu_cooling: fix return value check in cpufreq_cooling_register()
Thermal: Check for validity before doing kfree
thermal/intel_powerclamp: Add newer CPU models
Thermal: Tidy up error handling in powerclamp_init
thermal: Kconfig: cosmetic fixes
ACPI/thermal : Remove zone disabled warning
typo in drivers/thermal/Kconfig: lpatform instead of platform
-rw-r--r-- | drivers/acpi/thermal.c | 10 | ||||
-rw-r--r-- | drivers/thermal/Kconfig | 7 | ||||
-rw-r--r-- | drivers/thermal/cpu_cooling.c | 4 | ||||
-rw-r--r-- | drivers/thermal/intel_powerclamp.c | 29 | ||||
-rw-r--r-- | drivers/thermal/thermal_core.c | 10 | ||||
-rw-r--r-- | tools/Makefile | 15 | ||||
-rw-r--r-- | tools/thermal/tmon/Makefile | 47 | ||||
-rw-r--r-- | tools/thermal/tmon/README | 50 | ||||
-rw-r--r-- | tools/thermal/tmon/pid.c | 131 | ||||
-rw-r--r-- | tools/thermal/tmon/sysfs.c | 596 | ||||
-rw-r--r-- | tools/thermal/tmon/tmon.8 | 142 | ||||
-rw-r--r-- | tools/thermal/tmon/tmon.c | 352 | ||||
-rw-r--r-- | tools/thermal/tmon/tmon.h | 204 | ||||
-rw-r--r-- | tools/thermal/tmon/tui.c | 638 |
14 files changed, 2216 insertions, 19 deletions
diff --git a/drivers/acpi/thermal.c b/drivers/acpi/thermal.c index e600b5dbfcb..0d9f46b5ae6 100644 --- a/drivers/acpi/thermal.c +++ b/drivers/acpi/thermal.c @@ -514,10 +514,9 @@ static void acpi_thermal_check(void *data) { struct acpi_thermal *tz = data; - if (!tz->tz_enabled) { - pr_warn("thermal zone is disabled \n"); + if (!tz->tz_enabled) return; - } + thermal_zone_device_update(tz->thermal_zone); } @@ -569,9 +568,10 @@ static int thermal_set_mode(struct thermal_zone_device *thermal, */ if (mode == THERMAL_DEVICE_ENABLED) enable = 1; - else if (mode == THERMAL_DEVICE_DISABLED) + else if (mode == THERMAL_DEVICE_DISABLED) { enable = 0; - else + pr_warn("thermal zone will be disabled\n"); + } else return -EINVAL; if (enable != tz->tz_enabled) { diff --git a/drivers/thermal/Kconfig b/drivers/thermal/Kconfig index 5ef59676506..f35a1f75b15 100644 --- a/drivers/thermal/Kconfig +++ b/drivers/thermal/Kconfig @@ -56,7 +56,7 @@ config THERMAL_DEFAULT_GOV_USER_SPACE select THERMAL_GOV_USER_SPACE help Select this if you want to let the user space manage the - lpatform thermals. + platform thermals. endchoice @@ -69,6 +69,7 @@ config THERMAL_GOV_STEP_WISE bool "Step_wise thermal governor" help Enable this to manage platform thermals using a simple linear + governor. config THERMAL_GOV_USER_SPACE bool "User_space thermal governor" @@ -116,14 +117,14 @@ config SPEAR_THERMAL depends on OF help Enable this to plug the SPEAr thermal sensor driver into the Linux - thermal framework + thermal framework. config RCAR_THERMAL tristate "Renesas R-Car thermal driver" depends on ARCH_SHMOBILE help Enable this to plug the R-Car thermal sensor driver into the Linux - thermal framework + thermal framework. config KIRKWOOD_THERMAL tristate "Temperature sensor on Marvell Kirkwood SoCs" diff --git a/drivers/thermal/cpu_cooling.c b/drivers/thermal/cpu_cooling.c index d17902886c3..02a46f23d14 100644 --- a/drivers/thermal/cpu_cooling.c +++ b/drivers/thermal/cpu_cooling.c @@ -469,10 +469,10 @@ cpufreq_cooling_register(const struct cpumask *clip_cpus) cool_dev = thermal_cooling_device_register(dev_name, cpufreq_dev, &cpufreq_cooling_ops); - if (!cool_dev) { + if (IS_ERR(cool_dev)) { release_idr(&cpufreq_idr, cpufreq_dev->id); kfree(cpufreq_dev); - return ERR_PTR(-EINVAL); + return cool_dev; } cpufreq_dev->cool_dev = cool_dev; cpufreq_dev->cpufreq_state = 0; diff --git a/drivers/thermal/intel_powerclamp.c b/drivers/thermal/intel_powerclamp.c index b40b37cd25e..8f181b3f842 100644 --- a/drivers/thermal/intel_powerclamp.c +++ b/drivers/thermal/intel_powerclamp.c @@ -675,6 +675,11 @@ static const struct x86_cpu_id intel_powerclamp_ids[] = { { X86_VENDOR_INTEL, 6, 0x2e}, { X86_VENDOR_INTEL, 6, 0x2f}, { X86_VENDOR_INTEL, 6, 0x3a}, + { X86_VENDOR_INTEL, 6, 0x3c}, + { X86_VENDOR_INTEL, 6, 0x3e}, + { X86_VENDOR_INTEL, 6, 0x3f}, + { X86_VENDOR_INTEL, 6, 0x45}, + { X86_VENDOR_INTEL, 6, 0x46}, {} }; MODULE_DEVICE_TABLE(x86cpu, intel_powerclamp_ids); @@ -758,21 +763,39 @@ static int powerclamp_init(void) /* probe cpu features and ids here */ retval = powerclamp_probe(); if (retval) - return retval; + goto exit_free; + /* set default limit, maybe adjusted during runtime based on feedback */ window_size = 2; register_hotcpu_notifier(&powerclamp_cpu_notifier); + powerclamp_thread = alloc_percpu(struct task_struct *); + if (!powerclamp_thread) { + retval = -ENOMEM; + goto exit_unregister; + } + cooling_dev = thermal_cooling_device_register("intel_powerclamp", NULL, &powerclamp_cooling_ops); - if (IS_ERR(cooling_dev)) - return -ENODEV; + if (IS_ERR(cooling_dev)) { + retval = -ENODEV; + goto exit_free_thread; + } if (!duration) duration = jiffies_to_msecs(DEFAULT_DURATION_JIFFIES); + powerclamp_create_debug_files(); return 0; + +exit_free_thread: + free_percpu(powerclamp_thread); +exit_unregister: + unregister_hotcpu_notifier(&powerclamp_cpu_notifier); +exit_free: + kfree(cpu_clamping_mask); + return retval; } module_init(powerclamp_init); diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 4962a6aaf29..03a567199bb 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -247,10 +247,11 @@ static void bind_cdev(struct thermal_cooling_device *cdev) if (!pos->tzp && !pos->ops->bind) continue; - if (!pos->tzp && pos->ops->bind) { + if (pos->ops->bind) { ret = pos->ops->bind(pos, cdev); if (ret) print_bind_err_msg(pos, cdev, ret); + continue; } tzp = pos->tzp; @@ -282,8 +283,8 @@ static void bind_tz(struct thermal_zone_device *tz) mutex_lock(&thermal_list_lock); - /* If there is no platform data, try to use ops->bind */ - if (!tzp && tz->ops->bind) { + /* If there is ops->bind, try to use ops->bind */ + if (tz->ops->bind) { list_for_each_entry(pos, &thermal_cdev_list, node) { ret = tz->ops->bind(tz, pos); if (ret) @@ -1038,7 +1039,8 @@ static void thermal_release(struct device *dev) sizeof("thermal_zone") - 1)) { tz = to_thermal_zone(dev); kfree(tz); - } else { + } else if(!strncmp(dev_name(dev), "cooling_device", + sizeof("cooling_device") - 1)){ cdev = to_cooling_device(dev); kfree(cdev); } diff --git a/tools/Makefile b/tools/Makefile index 41067f30421..a9b02008443 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -15,6 +15,7 @@ help: @echo ' net - misc networking tools' @echo ' vm - misc vm tools' @echo ' x86_energy_perf_policy - Intel energy policy tool' + @echo ' tmon - thermal monitoring and tuning tool' @echo '' @echo 'You can do:' @echo ' $$ make -C tools/ <tool>_install' @@ -50,6 +51,9 @@ selftests: FORCE turbostat x86_energy_perf_policy: FORCE $(call descend,power/x86/$@) +tmon: FORCE + $(call descend,thermal/$@) + cpupower_install: $(call descend,power/$(@:_install=),install) @@ -62,9 +66,13 @@ selftests_install: turbostat_install x86_energy_perf_policy_install: $(call descend,power/x86/$(@:_install=),install) +tmon_install: + $(call descend,thermal/$(@:_install=),install) + install: cgroup_install cpupower_install firewire_install lguest_install \ perf_install selftests_install turbostat_install usb_install \ - virtio_install vm_install net_install x86_energy_perf_policy_install + virtio_install vm_install net_install x86_energy_perf_policy_install \ + tmon cpupower_clean: $(call descend,power/cpupower,clean) @@ -84,8 +92,11 @@ selftests_clean: turbostat_clean x86_energy_perf_policy_clean: $(call descend,power/x86/$(@:_clean=),clean) +tmon_clean: + $(call descend,thermal/tmon,clean) + clean: cgroup_clean cpupower_clean firewire_clean lguest_clean perf_clean \ selftests_clean turbostat_clean usb_clean virtio_clean \ - vm_clean net_clean x86_energy_perf_policy_clean + vm_clean net_clean x86_energy_perf_policy_clean tmon_clean .PHONY: FORCE diff --git a/tools/thermal/tmon/Makefile b/tools/thermal/tmon/Makefile new file mode 100644 index 00000000000..447321104ec --- /dev/null +++ b/tools/thermal/tmon/Makefile @@ -0,0 +1,47 @@ +VERSION = 1.0 + +BINDIR=usr/bin +WARNFLAGS=-Wall -Wshadow -W -Wformat -Wimplicit-function-declaration -Wimplicit-int +CFLAGS= -O1 ${WARNFLAGS} -fstack-protector +CC=gcc + +CFLAGS+=-D VERSION=\"$(VERSION)\" +LDFLAGS+= +TARGET=tmon + +INSTALL_PROGRAM=install -m 755 -p +DEL_FILE=rm -f + +INSTALL_CONFIGFILE=install -m 644 -p +CONFIG_FILE= +CONFIG_PATH= + + +OBJS = tmon.o tui.o sysfs.o pid.o +OBJS += + +tmon: $(OBJS) Makefile tmon.h + $(CC) ${CFLAGS} $(LDFLAGS) $(OBJS) -o $(TARGET) -lm -lpanel -lncursesw -lpthread + +valgrind: tmon + sudo valgrind -v --track-origins=yes --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./$(TARGET) 1> /dev/null + +install: + - mkdir -p $(INSTALL_ROOT)/$(BINDIR) + - $(INSTALL_PROGRAM) "$(TARGET)" "$(INSTALL_ROOT)/$(BINDIR)/$(TARGET)" + - mkdir -p $(INSTALL_ROOT)/$(CONFIG_PATH) + - $(INSTALL_CONFIGFILE) "$(CONFIG_FILE)" "$(INSTALL_ROOT)/$(CONFIG_PATH)" + +uninstall: + $(DEL_FILE) "$(INSTALL_ROOT)/$(BINDIR)/$(TARGET)" + $(CONFIG_FILE) "$(CONFIG_PATH)" + + +clean: + find . -name "*.o" | xargs $(DEL_FILE) + rm -f $(TARGET) + +dist: + git tag v$(VERSION) + git archive --format=tar --prefix="$(TARGET)-$(VERSION)/" v$(VERSION) | \ + gzip > $(TARGET)-$(VERSION).tar.gz diff --git a/tools/thermal/tmon/README b/tools/thermal/tmon/README new file mode 100644 index 00000000000..457949897a8 --- /dev/null +++ b/tools/thermal/tmon/README @@ -0,0 +1,50 @@ +TMON - A Monitoring and Testing Tool for Linux kernel thermal subsystem + +Why TMON? +========== +Increasingly, Linux is running on thermally constrained devices. The simple +thermal relationship between processor and fan has become past for modern +computers. + +As hardware vendors cope with the thermal constraints on their products, more +and more sensors are added, new cooling capabilities are introduced. The +complexity of the thermal relationship can grow exponentially among cooling +devices, zones, sensors, and trip points. They can also change dynamically. + +To expose such relationship to the userspace, Linux generic thermal layer +introduced sysfs entry at /sys/class/thermal with a matrix of symbolic +links, trip point bindings, and device instances. To traverse such +matrix by hand is not a trivial task. Testing is also difficult in that +thermal conditions are often exception cases that hard to reach in +normal operations. + +TMON is conceived as a tool to help visualize, tune, and test the +complex thermal subsystem. + +Files +===== + tmon.c : main function for set up and configurations. + tui.c : handles ncurses based user interface + sysfs.c : access to the generic thermal sysfs + pid.c : a proportional-integral-derivative (PID) controller + that can be used for thermal relationship training. + +Requirements +============ +Depends on ncurses + +Build +========= +$ make +$ sudo ./tmon -h +Usage: tmon [OPTION...] + -c, --control cooling device in control + -d, --daemon run as daemon, no TUI + -l, --log log data to /var/tmp/tmon.log + -h, --help show this help message + -t, --time-interval set time interval for sampling + -v, --version show version + -g, --debug debug message in syslog + +1. For monitoring only: +$ sudo ./tmon diff --git a/tools/thermal/tmon/pid.c b/tools/thermal/tmon/pid.c new file mode 100644 index 00000000000..fd7e9e9d6f4 --- /dev/null +++ b/tools/thermal/tmon/pid.c @@ -0,0 +1,131 @@ +/* + * pid.c PID controller for testing cooling devices + * + * + * + * Copyright (C) 2012 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author Name Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <sys/types.h> +#include <dirent.h> +#include <libintl.h> +#include <ctype.h> +#include <assert.h> +#include <time.h> +#include <limits.h> +#include <math.h> +#include <sys/stat.h> +#include <syslog.h> + +#include "tmon.h" + +/************************************************************************** + * PID (Proportional-Integral-Derivative) controller is commonly used in + * linear control system, consider the the process. + * G(s) = U(s)/E(s) + * kp = proportional gain + * ki = integral gain + * kd = derivative gain + * Ts + * We use type C Alan Bradley equation which takes set point off the + * output dependency in P and D term. + * + * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k] + * - 2*x[k-1]+x[k-2])/Ts + * + * + ***********************************************************************/ +struct pid_params p_param; +/* cached data from previous loop */ +static double xk_1, xk_2; /* input temperature x[k-#] */ + +/* + * TODO: make PID parameters tuned automatically, + * 1. use CPU burn to produce open loop unit step response + * 2. calculate PID based on Ziegler-Nichols rule + * + * add a flag for tuning PID + */ +int init_thermal_controller(void) +{ + int ret = 0; + + /* init pid params */ + p_param.ts = ticktime; + /* TODO: get it from TUI tuning tab */ + p_param.kp = .36; + p_param.ki = 5.0; + p_param.kd = 0.19; + + p_param.t_target = target_temp_user; + + return ret; +} + +void controller_reset(void) +{ + /* TODO: relax control data when not over thermal limit */ + syslog(LOG_DEBUG, "TC inactive, relax p-state\n"); + p_param.y_k = 0.0; + xk_1 = 0.0; + xk_2 = 0.0; + set_ctrl_state(0); +} + +/* To be called at time interval Ts. Type C PID controller. + * y[k] = y[k-1] - kp*(x[k] - x[k-1]) + Ki*Ts*e[k] - Kd*(x[k] + * - 2*x[k-1]+x[k-2])/Ts + * TODO: add low pass filter for D term + */ +#define GUARD_BAND (2) +void controller_handler(const double xk, double *yk) +{ + double ek; + double p_term, i_term, d_term; + + ek = p_param.t_target - xk; /* error */ + if (ek >= 3.0) { + syslog(LOG_DEBUG, "PID: %3.1f Below set point %3.1f, stop\n", + xk, p_param.t_target); + controller_reset(); + *yk = 0.0; + return; + } + /* compute intermediate PID terms */ + p_term = -p_param.kp * (xk - xk_1); + i_term = p_param.kp * p_param.ki * p_param.ts * ek; + d_term = -p_param.kp * p_param.kd * (xk - 2 * xk_1 + xk_2) / p_param.ts; + /* compute output */ + *yk += p_term + i_term + d_term; + /* update sample data */ + xk_1 = xk; + xk_2 = xk_1; + + /* clamp output adjustment range */ + if (*yk < -LIMIT_HIGH) + *yk = -LIMIT_HIGH; + else if (*yk > -LIMIT_LOW) + *yk = -LIMIT_LOW; + + p_param.y_k = *yk; + + set_ctrl_state(lround(fabs(p_param.y_k))); + +} diff --git a/tools/thermal/tmon/sysfs.c b/tools/thermal/tmon/sysfs.c new file mode 100644 index 00000000000..dfe454855cd --- /dev/null +++ b/tools/thermal/tmon/sysfs.c @@ -0,0 +1,596 @@ +/* + * sysfs.c sysfs ABI access functions for TMON program + * + * Copyright (C) 2013 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version + * 2 or later as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * Author: Jacob Pan <jacob.jun.pan@linux.intel.com> + * + */ +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> +#include <dirent.h> +#include <libintl.h> +#include <ctype.h> +#include <time.h> +#include <syslog.h> +#include <sys/time.h> +#include <errno.h> + +#include "tmon.h" + +struct tmon_platform_data ptdata; +const char *trip_type_name[] = { + "critical", + "hot", + "passive", + "active", +}; + +int sysfs_set_ulong(char *path, char *filename, unsigned long val) +{ + FILE *fd; + int ret = -1; + char filepath[256]; + + snprintf(filepath, 256, "%s/%s", path, filename); + + fd = fopen(filepath, "w"); + if (!fd) { + syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath); + return ret; + } + ret = fprintf(fd, "%lu", val); + fclose(fd); + + return 0; +} + +/* history of thermal data, used for control algo */ +#define NR_THERMAL_RECORDS 3 +struct thermal_data_record trec[NR_THERMAL_RECORDS]; +int cur_thermal_record; /* index to the trec array */ + +static int sysfs_get_ulong(char *path, char *filename, unsigned long *p_ulong) +{ + FILE *fd; + int ret = -1; + char filepath[256]; + + snprintf(filepath, 256, "%s/%s", path, filename); + + fd = fopen(filepath, "r"); + if (!fd) { + syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath); + return ret; + } + ret = fscanf(fd, "%lu", p_ulong); + fclose(fd); + + return 0; +} + +static int sysfs_get_string(char *path, char *filename, char *str) +{ + FILE *fd; + int ret = -1; + char filepath[256]; + + snprintf(filepath, 256, "%s/%s", path, filename); + + fd = fopen(filepath, "r"); + if (!fd) { + syslog(LOG_ERR, "Err: open %s: %s\n", __func__, filepath); + return ret; + } + ret = fscanf(fd, "%256s", str); + fclose(fd); + + return ret; +} + +/* get states of the cooling device instance */ +static int probe_cdev(struct cdev_info *cdi, char *path) +{ + sysfs_get_string(path, "type", cdi->type); + sysfs_get_ulong(path, "max_state", &cdi->max_state); + sysfs_get_ulong(path, "cur_state", &cdi->cur_state); + + syslog(LOG_INFO, "%s: %s: type %s, max %lu, curr %lu inst %d\n", + __func__, path, + cdi->type, cdi->max_state, cdi->cur_state, cdi->instance); + + return 0; +} + +static int str_to_trip_type(char *name) +{ + int i; + + for (i = 0; i < NR_THERMAL_TRIP_TYPE; i++) { + if (!strcmp(name, trip_type_name[i])) + return i; + } + + return -ENOENT; +} + +/* scan and fill in trip point info for a thermal zone and trip point id */ +static int get_trip_point_data(char *tz_path, int tzid, int tpid) +{ + char filename[256]; + char temp_str[256]; + int trip_type; + + if (tpid >= MAX_NR_TRIP) + return -EINVAL; + /* check trip point type */ + snprintf(filename, sizeof(filename), "trip_point_%d_type", tpid); + sysfs_get_string(tz_path, filename, temp_str); + trip_type = str_to_trip_type(temp_str); + if (trip_type < 0) { + syslog(LOG_ERR, "%s:%s no matching type\n", __func__, temp_str); + return -ENOENT; + } + ptdata.tzi[tzid].tp[tpid].type = trip_type; + syslog(LOG_INFO, "%s:tz:%d tp:%d:type:%s type id %d\n", __func__, tzid, + tpid, temp_str, trip_type); + + /* TODO: check attribute */ + + return 0; +} + +/* return instance id for file format such as trip_point_4_temp */ +static int get_instance_id(char *name, int pos, int skip) +{ + char *ch; + int i = 0; + + ch = strtok(name, "_"); + while (ch != NULL) { + ++i; + syslog(LOG_INFO, "%s:%s:%s:%d", __func__, name, ch, i); + ch = strtok(NULL, "_"); + if (pos == i) + return atol(ch + skip); + } + + return -1; +} + +/* Find trip point info of a thermal zone */ +static int find_tzone_tp(char *tz_name, char *d_name, struct tz_info *tzi, + int tz_id) +{ + int tp_id; + unsigned long temp_ulong; + + if (strstr(d_name, "trip_point") && + strstr(d_name, "temp")) { + /* check if trip point temp is non-zero + * ignore 0/invalid trip points + */ + sysfs_get_ulong(tz_name, d_name, &temp_ulong); + if (temp_ulong < MAX_TEMP_KC) { + tzi->nr_trip_pts++; + /* found a valid trip point */ + tp_id = get_instance_id(d_name, 2, 0); + syslog(LOG_DEBUG, "tzone %s trip %d temp %lu tpnode %s", + tz_name, tp_id, temp_ulong, d_name); + if (tp_id < 0 || tp_id >= MAX_NR_TRIP) { + syslog(LOG_ERR, "Failed to find TP inst %s\n", + d_name); + return -1; + } + get_trip_point_data(tz_name, tz_id, tp_id); + tzi->tp[tp_id].temp = temp_ulong; + } + } + + return 0; +} + +/* check cooling devices for binding info. */ +static int find_tzone_cdev(struct dirent *nl, char *tz_name, + struct tz_info *tzi, int tz_id, int cid) +{ + unsigned long trip_instance = 0; + char cdev_name_linked[256]; + char cdev_name[256]; + char cdev_trip_name[256]; + int cdev_id; + + if (nl->d_type == DT_LNK) { + syslog(LOG_DEBUG, "TZ%d: cdev: %s cid %d\n", tz_id, nl->d_name, + cid); + tzi->nr_cdev++; + if (tzi->nr_cdev > ptdata.nr_cooling_dev) { + syslog(LOG_ERR, "Err: Too many cdev? %d\n", + tzi->nr_cdev); + return -EINVAL; + } + /* find the link to real cooling device record binding */ + snprintf(cdev_name, 256, "%s/%s", tz_name, nl->d_name); + memset(cdev_name_linked, 0, sizeof(cdev_name_linked)); + if (readlink(cdev_name, cdev_name_linked, + sizeof(cdev_name_linked) - 1) != -1) { + cdev_id = get_instance_id(cdev_name_linked, 1, + sizeof("device") - 1); + syslog(LOG_DEBUG, "cdev %s linked to %s : %d\n", + cdev_name, cdev_name_linked, cdev_id); + tzi->cdev_binding |= (1 << cdev_id); + + /* find the trip point in which the cdev is binded to + * in this tzone + */ + snprintf(cdev_trip_name, 256, "%s%s", nl->d_name, + "_trip_point"); + sysfs_get_ulong(tz_name, cdev_trip_name, + &trip_instance); + /* validate trip point range, e.g. trip could return -1 + * when passive is enabled + */ + if (trip_instance > MAX_NR_TRIP) + trip_instance = 0; + tzi->trip_binding[cdev_id] |= 1 << trip_instance; + syslog(LOG_DEBUG, "cdev %s -> trip:%lu: 0x%lx %d\n", + cdev_name, trip_instance, + tzi->trip_binding[cdev_id], + cdev_id); + + + } + return 0; + } + + return -ENODEV; +} + + + +/***************************************************************************** + * Before calling scan_tzones, thermal sysfs must be probed to determine + * the number of thermal zones and cooling devices. + * We loop through each thermal zone and fill in tz_info struct, i.e. + * ptdata.tzi[] +root@jacob-chiefriver:~# tree -d /sys/class/thermal/thermal_zone0 +/sys/class/thermal/thermal_zone0 +|-- cdev0 -> ../cooling_device4 +|-- cdev1 -> ../cooling_device3 +|-- cdev10 -> ../cooling_device7 +|-- cdev11 -> ../cooling_device6 +|-- cdev12 -> ../cooling_device5 +|-- cdev2 -> ../cooling_device2 +|-- cdev3 -> ../cooling_device1 +|-- cdev4 -> ../cooling_device0 +|-- cdev5 -> ../cooling_device12 +|-- cdev6 -> ../cooling_device11 +|-- cdev7 -> ../cooling_device10 +|-- cdev8 -> ../cooling_device9 +|-- cdev9 -> ../cooling_device8 +|-- device -> ../../../LNXSYSTM:00/device:62/LNXTHERM:00 +|-- power +`-- subsystem -> ../../../../class/thermal +*****************************************************************************/ +static int scan_tzones(void) +{ + DIR *dir; + struct dirent **namelist; + char tz_name[256]; + int i, j, n, k = 0; + + if (!ptdata.nr_tz_sensor) + return -1; + + for (i = 0; i <= ptdata.max_tz_instance; i++) { + memset(tz_name, 0, sizeof(tz_name)); + snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, i); + + dir = opendir(tz_name); + if (!dir) { + syslog(LOG_INFO, "Thermal zone %s skipped\n", tz_name); + continue; + } + /* keep track of valid tzones */ + n = scandir(tz_name, &namelist, 0, alphasort); + if (n < 0) + syslog(LOG_ERR, "scandir failed in %s", tz_name); + else { + sysfs_get_string(tz_name, "type", ptdata.tzi[k].type); + ptdata.tzi[k].instance = i; + /* detect trip points and cdev attached to this tzone */ + j = 0; /* index for cdev */ + ptdata.tzi[k].nr_cdev = 0; + ptdata.tzi[k].nr_trip_pts = 0; + while (n--) { + char *temp_str; + + if (find_tzone_tp(tz_name, namelist[n]->d_name, + &ptdata.tzi[k], k)) + break; + temp_str = strstr(namelist[n]->d_name, "cdev"); + if (!temp_str) { + free(namelist[n]); + continue; + } + if (!find_tzone_cdev(namelist[n], tz_name, + &ptdata.tzi[k], i, j)) + j++; /* increment cdev index */ + free(namelist[n]); + } + free(namelist); + } + /*TODO: reverse trip points */ + closedir(dir); + syslog(LOG_INFO, "TZ %d has %d cdev\n", i, + ptdata.tzi[k].nr_cdev); + k++; + } + + return 0; +} + +static int scan_cdevs(void) +{ + DIR *dir; + struct dirent **namelist; + char cdev_name[256]; + int i, n, k = 0; + + if (!ptdata.nr_cooling_dev) { + fprintf(stderr, "No cooling devices found\n"); + return 0; + } + for (i = 0; i <= ptdata.max_cdev_instance; i++) { + memset(cdev_name, 0, sizeof(cdev_name)); + snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, i); + + dir = opendir(cdev_name); + if (!dir) { + syslog(LOG_INFO, "Cooling dev %s skipped\n", cdev_name); + /* there is a gap in cooling device id, check again + * for the same index. + */ + continue; + } + + n = scandir(cdev_name, &namelist, 0, alphasort); + if (n < 0) + syslog(LOG_ERR, "scandir failed in %s", cdev_name); + else { + sysfs_get_string(cdev_name, "type", ptdata.cdi[k].type); + ptdata.cdi[k].instance = i; + if (strstr(ptdata.cdi[k].type, ctrl_cdev)) { + ptdata.cdi[k].flag |= CDEV_FLAG_IN_CONTROL; + syslog(LOG_DEBUG, "control cdev id %d\n", i); + } + while (n--) + free(namelist[n]); + free(namelist); + } + closedir(dir); + k++; + } + return 0; +} + + +int probe_thermal_sysfs(void) +{ + DIR *dir; + struct dirent **namelist; + int n; + + dir = opendir(THERMAL_SYSFS); + if (!dir) { + fprintf(stderr, "\nNo thermal sysfs, exit\n"); + return -1; + } + n = scandir(THERMAL_SYSFS, &namelist, 0, alphasort); + if (n < 0) + syslog(LOG_ERR, "scandir failed in thermal sysfs"); + else { + /* detect number of thermal zones and cooling devices */ + while (n--) { + int inst; + + if (strstr(namelist[n]->d_name, CDEV)) { + inst = get_instance_id(namelist[n]->d_name, 1, + sizeof("device") - 1); + /* keep track of the max cooling device since + * there may be gaps. + */ + if (inst > ptdata.max_cdev_instance) + ptdata.max_cdev_instance = inst; + + syslog(LOG_DEBUG, "found cdev: %s %d %d\n", + namelist[n]->d_name, + ptdata.nr_cooling_dev, + ptdata.max_cdev_instance); + ptdata.nr_cooling_dev++; + } else if (strstr(namelist[n]->d_name, TZONE)) { + inst = get_instance_id(namelist[n]->d_name, 1, + sizeof("zone") - 1); + if (inst > ptdata.max_tz_instance) + ptdata.max_tz_instance = inst; + + syslog(LOG_DEBUG, "found tzone: %s %d %d\n", + namelist[n]->d_name, + ptdata.nr_tz_sensor, + ptdata.max_tz_instance); + ptdata.nr_tz_sensor++; + } + free(namelist[n]); + } + free(namelist); + } + syslog(LOG_INFO, "found %d tzone(s), %d cdev(s), target zone %d\n", + ptdata.nr_tz_sensor, ptdata.nr_cooling_dev, + target_thermal_zone); + closedir(dir); + + if (!ptdata.nr_tz_sensor) { + fprintf(stderr, "\nNo thermal zones found, exit\n\n"); + return -1; + } + + ptdata.tzi = calloc(sizeof(struct tz_info), ptdata.max_tz_instance+1); + if (!ptdata.tzi) { + fprintf(stderr, "Err: allocate tz_info\n"); + return -1; + } + + /* we still show thermal zone information if there is no cdev */ + if (ptdata.nr_cooling_dev) { + ptdata.cdi = calloc(sizeof(struct cdev_info), + ptdata.max_cdev_instance + 1); + if (!ptdata.cdi) { + free(ptdata.tzi); + fprintf(stderr, "Err: allocate cdev_info\n"); + return -1; + } + } + + /* now probe tzones */ + if (scan_tzones()) + return -1; + if (scan_cdevs()) + return -1; + return 0; +} + +/* convert sysfs zone instance to zone array index */ +int zone_instance_to_index(int zone_inst) +{ + int i; + + for (i = 0; i < ptdata.nr_tz_sensor; i++) + if (ptdata.tzi[i].instance == zone_inst) + return i; + return -ENOENT; +} + +/* read temperature of all thermal zones */ +int update_thermal_data() +{ + int i; + char tz_name[256]; + static unsigned long samples; + + if (!ptdata.nr_tz_sensor) { + syslog(LOG_ERR, "No thermal zones found!\n"); + return -1; + } + + /* circular buffer for keeping historic data */ + if (cur_thermal_record >= NR_THERMAL_RECORDS) + cur_thermal_record = 0; + gettimeofday(&trec[cur_thermal_record].tv, NULL); + if (tmon_log) { + fprintf(tmon_log, "%lu ", ++samples); + fprintf(tmon_log, "%3.1f ", p_param.t_target); + } + for (i = 0; i < ptdata.nr_tz_sensor; i++) { + memset(tz_name, 0, sizeof(tz_name)); + snprintf(tz_name, 256, "%s/%s%d", THERMAL_SYSFS, TZONE, + ptdata.tzi[i].instance); + sysfs_get_ulong(tz_name, "temp", + &trec[cur_thermal_record].temp[i]); + if (tmon_log) + fprintf(tmon_log, "%lu ", + trec[cur_thermal_record].temp[i]/1000); + } + for (i = 0; i < ptdata.nr_cooling_dev; i++) { + char cdev_name[256]; + unsigned long val; + + snprintf(cdev_name, 256, "%s/%s%d", THERMAL_SYSFS, CDEV, + ptdata.cdi[i].instance); + probe_cdev(&ptdata.cdi[i], cdev_name); + val = ptdata.cdi[i].cur_state; + if (val > 1000000) + val = 0; + if (tmon_log) + fprintf(tmon_log, "%lu ", val); + } + + if (tmon_log) { + fprintf(tmon_log, "\n"); + fflush(tmon_log); + } + + return 0; +} + +void set_ctrl_state(unsigned long state) +{ + char ctrl_cdev_path[256]; + int i; |