#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/reboot.h>
#include <sys/sensors.h>
#include <sys/kthread.h>
#include <macppc/dev/thermal.h>
#define FAN_HYSTERESIS_TIMER 10
void thermal_thread_init(void);
void thermal_thread_create(void *);
void thermal_thread_loop(void *);
void thermal_manage_fans(void);
int thermal_enable = 0;
struct thermal_fan_le {
struct thermal_fan *fan;
int last_val;
int timer;
SLIST_ENTRY(thermal_fan_le) entries;
};
struct thermal_sens_le {
struct thermal_temp *sensor;
int last_val;
#define MAX_CRITICAL_COUNT 6
int critical_count;
SLIST_ENTRY(thermal_sens_le) entries;
};
SLIST_HEAD(thermal_fans, thermal_fan_le) fans =
SLIST_HEAD_INITIALIZER(fans);
SLIST_HEAD(thermal_sensors, thermal_sens_le) sensors =
SLIST_HEAD_INITIALIZER(sensors);
void
thermal_thread_init(void)
{
if (thermal_enable)
return;
thermal_enable = 1;
kthread_create_deferred(thermal_thread_create, &thermal_enable);
}
void
thermal_thread_create(void *arg)
{
if (kthread_create(thermal_thread_loop, &thermal_enable, NULL,
"thermal")) {
printf("thermal kernel thread can't be created!\n");
thermal_enable = 0;
}
}
void
thermal_thread_loop(void *arg)
{
while (thermal_enable) {
thermal_manage_fans();
tsleep_nsec(&thermal_enable, 0, "thermal", SEC_TO_NSEC(1));
}
kthread_exit(0);
}
void
thermal_manage_fans(void)
{
struct thermal_sens_le *sensor;
struct thermal_fan_le *fan;
int64_t average_excess, max_excess_zone, frac_excess;
int fan_speed;
int nsens, nsens_zone;
int temp;
SLIST_FOREACH(sensor, &sensors, entries) {
temp = sensor->sensor->read(sensor->sensor);
if (temp > 0)
sensor->last_val = temp;
if (sensor->last_val > sensor->sensor->max_temp) {
sensor->critical_count++;
printf("WARNING: Current temperature (%s: %d.%d C) "
"exceeds critical temperature (%lld.%lld C); "
"count=%d\n",
sensor->sensor->name,
(sensor->last_val - ZERO_C_TO_MUK)/1000000,
(sensor->last_val - ZERO_C_TO_MUK)%1000000,
(sensor->sensor->max_temp - ZERO_C_TO_MUK)/1000000,
(sensor->sensor->max_temp - ZERO_C_TO_MUK)%1000000,
sensor->critical_count);
if (sensor->critical_count >= MAX_CRITICAL_COUNT) {
printf("WARNING: %s temperature exceeded "
"critical temperature %d times in a row; "
"shutting down!\n",
sensor->sensor->name,
sensor->critical_count);
reboot(RB_HALT | RB_POWERDOWN | RB_TIMEBAD);
}
} else {
if (sensor->critical_count > 0)
sensor->critical_count--;
}
}
SLIST_FOREACH(fan, &fans, entries) {
nsens = nsens_zone = 0;
average_excess = max_excess_zone = 0;
SLIST_FOREACH(sensor, &sensors, entries) {
temp = ulmin(sensor->last_val,
sensor->sensor->max_temp);
frac_excess = (temp -
sensor->sensor->target_temp)*100 /
(sensor->sensor->max_temp - temp + 1);
if (frac_excess < 0)
frac_excess = 0;
if (sensor->sensor->zone == fan->fan->zone) {
max_excess_zone = ulmax(max_excess_zone,
frac_excess);
nsens_zone++;
}
average_excess += frac_excess;
nsens++;
}
if (nsens == 0) {
fan->fan->set(fan->fan, fan->fan->default_rpm);
continue;
}
average_excess /= nsens;
if (nsens_zone == 0)
max_excess_zone = average_excess;
max_excess_zone = ulmin(max_excess_zone, 100);
fan_speed = max_excess_zone *
(fan->fan->max_rpm - fan->fan->min_rpm)/100 +
fan->fan->min_rpm;
if (fan_speed >= fan->last_val) {
fan->timer = FAN_HYSTERESIS_TIMER;
fan->last_val = fan_speed;
} else {
fan->timer--;
if (fan->timer == 0) {
fan->last_val = fan_speed;
fan->timer = FAN_HYSTERESIS_TIMER;
}
}
fan->fan->set(fan->fan, fan->last_val);
}
}
void
thermal_fan_register(struct thermal_fan *fan)
{
struct thermal_fan_le *list_entry;
thermal_thread_init();
list_entry = malloc(sizeof(struct thermal_fan_le), M_DEVBUF,
M_ZERO | M_WAITOK);
list_entry->fan = fan;
SLIST_INSERT_HEAD(&fans, list_entry, entries);
}
void
thermal_sensor_register(struct thermal_temp *sensor)
{
struct thermal_sens_le *list_entry;
thermal_thread_init();
list_entry = malloc(sizeof(struct thermal_sens_le), M_DEVBUF,
M_ZERO | M_WAITOK);
list_entry->sensor = sensor;
list_entry->last_val = 0;
list_entry->critical_count = 0;
SLIST_INSERT_HEAD(&sensors, list_entry, entries);
}