xref: /freebsd/sys/powerpc/powermac/powermac_thermal.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
184d77676SNathan Whitehorn /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
484d77676SNathan Whitehorn  * Copyright (c) 2009-2011 Nathan Whitehorn
584d77676SNathan Whitehorn  * All rights reserved.
684d77676SNathan Whitehorn  *
784d77676SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
884d77676SNathan Whitehorn  * modification, are permitted provided that the following conditions
984d77676SNathan Whitehorn  * are met:
1084d77676SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
1184d77676SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
1284d77676SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
1384d77676SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
1484d77676SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
1584d77676SNathan Whitehorn  *
1684d77676SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1784d77676SNathan Whitehorn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1884d77676SNathan Whitehorn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1984d77676SNathan Whitehorn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2084d77676SNathan Whitehorn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2184d77676SNathan Whitehorn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2284d77676SNathan Whitehorn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2384d77676SNathan Whitehorn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2484d77676SNathan Whitehorn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2584d77676SNathan Whitehorn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2684d77676SNathan Whitehorn  * SUCH DAMAGE.
2784d77676SNathan Whitehorn  */
2884d77676SNathan Whitehorn 
2984d77676SNathan Whitehorn #include <sys/param.h>
3084d77676SNathan Whitehorn #include <sys/kernel.h>
3184d77676SNathan Whitehorn #include <sys/lock.h>
3284d77676SNathan Whitehorn #include <sys/mutex.h>
3384d77676SNathan Whitehorn #include <sys/systm.h>
3484d77676SNathan Whitehorn 
3584d77676SNathan Whitehorn #include <sys/types.h>
3684d77676SNathan Whitehorn #include <sys/kthread.h>
3784d77676SNathan Whitehorn #include <sys/malloc.h>
3884d77676SNathan Whitehorn #include <sys/reboot.h>
3984d77676SNathan Whitehorn #include <sys/sysctl.h>
4084d77676SNathan Whitehorn #include <sys/queue.h>
4184d77676SNathan Whitehorn 
4284d77676SNathan Whitehorn #include "powermac_thermal.h"
4384d77676SNathan Whitehorn 
447f745b2bSJustin Hibbits /* A 10 second timer for spinning down fans. */
457f745b2bSJustin Hibbits #define FAN_HYSTERESIS_TIMER	10
467f745b2bSJustin Hibbits 
4784d77676SNathan Whitehorn static void fan_management_proc(void);
4884d77676SNathan Whitehorn static void pmac_therm_manage_fans(void);
4984d77676SNathan Whitehorn 
5084d77676SNathan Whitehorn static struct proc *pmac_them_proc;
5184d77676SNathan Whitehorn static int enable_pmac_thermal = 1;
5284d77676SNathan Whitehorn 
5384d77676SNathan Whitehorn static struct kproc_desc pmac_therm_kp = {
5484d77676SNathan Whitehorn 	"pmac_thermal",
5584d77676SNathan Whitehorn 	fan_management_proc,
5684d77676SNathan Whitehorn 	&pmac_them_proc
5784d77676SNathan Whitehorn };
5884d77676SNathan Whitehorn 
5984d77676SNathan Whitehorn SYSINIT(pmac_therm_setup, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start,
6084d77676SNathan Whitehorn     &pmac_therm_kp);
61dc1c5138SZhenlei Huang SYSCTL_INT(_machdep, OID_AUTO, manage_fans, CTLFLAG_RWTUN,
6284d77676SNathan Whitehorn     &enable_pmac_thermal, 1, "Enable automatic fan management");
63d745c852SEd Schouten static MALLOC_DEFINE(M_PMACTHERM, "pmactherm", "Powermac Thermal Management");
6484d77676SNathan Whitehorn 
6584d77676SNathan Whitehorn struct pmac_fan_le {
6684d77676SNathan Whitehorn 	struct pmac_fan			*fan;
6784d77676SNathan Whitehorn 	int				last_val;
687f745b2bSJustin Hibbits 	int				timer;
6984d77676SNathan Whitehorn 	SLIST_ENTRY(pmac_fan_le)	entries;
7084d77676SNathan Whitehorn };
7184d77676SNathan Whitehorn struct pmac_sens_le {
7284d77676SNathan Whitehorn 	struct pmac_therm		*sensor;
7384d77676SNathan Whitehorn 	int				last_val;
74a3550e73SNathan Whitehorn #define MAX_CRITICAL_COUNT 6
75a3550e73SNathan Whitehorn 	int				critical_count;
7684d77676SNathan Whitehorn 	SLIST_ENTRY(pmac_sens_le)	entries;
7784d77676SNathan Whitehorn };
7884d77676SNathan Whitehorn static SLIST_HEAD(pmac_fans, pmac_fan_le) fans = SLIST_HEAD_INITIALIZER(fans);
7984d77676SNathan Whitehorn static SLIST_HEAD(pmac_sensors, pmac_sens_le) sensors =
8084d77676SNathan Whitehorn     SLIST_HEAD_INITIALIZER(sensors);
8184d77676SNathan Whitehorn 
8284d77676SNathan Whitehorn static void
fan_management_proc(void)8384d77676SNathan Whitehorn fan_management_proc(void)
8484d77676SNathan Whitehorn {
8584d77676SNathan Whitehorn 	/* Nothing to manage? */
8684d77676SNathan Whitehorn 	if (SLIST_EMPTY(&fans))
879d2a3635SNathan Whitehorn 		kproc_exit(0);
8884d77676SNathan Whitehorn 
8984d77676SNathan Whitehorn 	while (1) {
9084d77676SNathan Whitehorn 		pmac_therm_manage_fans();
9184d77676SNathan Whitehorn 		pause("pmac_therm", hz);
9284d77676SNathan Whitehorn 	}
9384d77676SNathan Whitehorn }
9484d77676SNathan Whitehorn 
9584d77676SNathan Whitehorn static void
pmac_therm_manage_fans(void)9684d77676SNathan Whitehorn pmac_therm_manage_fans(void)
9784d77676SNathan Whitehorn {
9884d77676SNathan Whitehorn 	struct pmac_sens_le *sensor;
9984d77676SNathan Whitehorn 	struct pmac_fan_le *fan;
10084d77676SNathan Whitehorn 	int average_excess, max_excess_zone, frac_excess;
1017f745b2bSJustin Hibbits 	int fan_speed;
10284d77676SNathan Whitehorn 	int nsens, nsens_zone;
103d015abb7SNathan Whitehorn 	int temp;
10484d77676SNathan Whitehorn 
10584d77676SNathan Whitehorn 	if (!enable_pmac_thermal)
10684d77676SNathan Whitehorn 		return;
10784d77676SNathan Whitehorn 
10884d77676SNathan Whitehorn 	/* Read all the sensors */
10984d77676SNathan Whitehorn 	SLIST_FOREACH(sensor, &sensors, entries) {
110d015abb7SNathan Whitehorn 		temp = sensor->sensor->read(sensor->sensor);
111d015abb7SNathan Whitehorn 		if (temp > 0) /* Use the previous temp in case of error */
112d015abb7SNathan Whitehorn 			sensor->last_val = temp;
113d015abb7SNathan Whitehorn 
11484d77676SNathan Whitehorn 		if (sensor->last_val > sensor->sensor->max_temp) {
115a3550e73SNathan Whitehorn 			sensor->critical_count++;
11684d77676SNathan Whitehorn 			printf("WARNING: Current temperature (%s: %d.%d C) "
117a3550e73SNathan Whitehorn 			    "exceeds critical temperature (%d.%d C); "
118a3550e73SNathan Whitehorn 			    "count=%d\n",
119a3550e73SNathan Whitehorn 			    sensor->sensor->name,
1207736fb18SAndreas Tobler 			    (sensor->last_val - ZERO_C_TO_K) / 10,
1217736fb18SAndreas Tobler 			    (sensor->last_val - ZERO_C_TO_K) % 10,
1227736fb18SAndreas Tobler 			    (sensor->sensor->max_temp - ZERO_C_TO_K) / 10,
123a3550e73SNathan Whitehorn 			    (sensor->sensor->max_temp - ZERO_C_TO_K) % 10,
124a3550e73SNathan Whitehorn 			    sensor->critical_count);
125a3550e73SNathan Whitehorn 			if (sensor->critical_count >= MAX_CRITICAL_COUNT) {
126a3550e73SNathan Whitehorn 				printf("WARNING: %s temperature exceeded "
127a3550e73SNathan Whitehorn 				    "critical temperature %d times in a row; "
128a3550e73SNathan Whitehorn 				    "shutting down!\n",
129a3550e73SNathan Whitehorn 				    sensor->sensor->name,
130a3550e73SNathan Whitehorn 				    sensor->critical_count);
13184d77676SNathan Whitehorn 				shutdown_nice(RB_POWEROFF);
13284d77676SNathan Whitehorn 			}
133a3550e73SNathan Whitehorn 		} else {
134a3550e73SNathan Whitehorn 			if (sensor->critical_count > 0)
135a3550e73SNathan Whitehorn 				sensor->critical_count--;
136a3550e73SNathan Whitehorn 		}
13784d77676SNathan Whitehorn 	}
13884d77676SNathan Whitehorn 
13984d77676SNathan Whitehorn 	/* Set all the fans */
14084d77676SNathan Whitehorn 	SLIST_FOREACH(fan, &fans, entries) {
14184d77676SNathan Whitehorn 		nsens = nsens_zone = 0;
14284d77676SNathan Whitehorn 		average_excess = max_excess_zone = 0;
14384d77676SNathan Whitehorn 		SLIST_FOREACH(sensor, &sensors, entries) {
1447f745b2bSJustin Hibbits 			temp = imin(sensor->last_val,
1457f745b2bSJustin Hibbits 			    sensor->sensor->max_temp);
1467f745b2bSJustin Hibbits 			frac_excess = (temp -
14784d77676SNathan Whitehorn 			    sensor->sensor->target_temp)*100 /
1487f745b2bSJustin Hibbits 			    (sensor->sensor->max_temp - temp + 1);
149cbfd4d0cSNathan Whitehorn 			if (frac_excess < 0)
150cbfd4d0cSNathan Whitehorn 				frac_excess = 0;
15184d77676SNathan Whitehorn 			if (sensor->sensor->zone == fan->fan->zone) {
15284d77676SNathan Whitehorn 				max_excess_zone = imax(max_excess_zone,
15384d77676SNathan Whitehorn 				    frac_excess);
15484d77676SNathan Whitehorn 				nsens_zone++;
15584d77676SNathan Whitehorn 			}
15684d77676SNathan Whitehorn 			average_excess += frac_excess;
15784d77676SNathan Whitehorn 			nsens++;
15884d77676SNathan Whitehorn 		}
15984d77676SNathan Whitehorn 		average_excess /= nsens;
16084d77676SNathan Whitehorn 
16184d77676SNathan Whitehorn 		/* If there are no sensors in this zone, use the average */
16284d77676SNathan Whitehorn 		if (nsens_zone == 0)
16384d77676SNathan Whitehorn 			max_excess_zone = average_excess;
16484d77676SNathan Whitehorn 		/* No sensors at all? Use default */
16584d77676SNathan Whitehorn 		if (nsens == 0) {
16684d77676SNathan Whitehorn 			fan->fan->set(fan->fan, fan->fan->default_rpm);
16784d77676SNathan Whitehorn 			continue;
16884d77676SNathan Whitehorn 		}
16984d77676SNathan Whitehorn 
17084d77676SNathan Whitehorn 		/*
17184d77676SNathan Whitehorn 		 * Scale the fan linearly in the max temperature in its
17284d77676SNathan Whitehorn 		 * thermal zone.
17384d77676SNathan Whitehorn 		 */
1747f745b2bSJustin Hibbits 		max_excess_zone = imin(max_excess_zone, 100);
1757f745b2bSJustin Hibbits 		fan_speed = max_excess_zone *
17684d77676SNathan Whitehorn 		    (fan->fan->max_rpm - fan->fan->min_rpm)/100 +
1777f745b2bSJustin Hibbits 		    fan->fan->min_rpm;
1787f745b2bSJustin Hibbits 		if (fan_speed >= fan->last_val) {
1797f745b2bSJustin Hibbits 		    fan->timer = FAN_HYSTERESIS_TIMER;
1807f745b2bSJustin Hibbits 		    fan->last_val = fan_speed;
1817f745b2bSJustin Hibbits 		} else {
1827f745b2bSJustin Hibbits 		    fan->timer--;
1837f745b2bSJustin Hibbits 		    if (fan->timer == 0) {
1847f745b2bSJustin Hibbits 		    	fan->last_val = fan_speed;
1857f745b2bSJustin Hibbits 		    	fan->timer = FAN_HYSTERESIS_TIMER;
1867f745b2bSJustin Hibbits 		    }
1877f745b2bSJustin Hibbits 		}
1887f745b2bSJustin Hibbits 		fan->fan->set(fan->fan, fan->last_val);
18984d77676SNathan Whitehorn 	}
19084d77676SNathan Whitehorn }
19184d77676SNathan Whitehorn 
19284d77676SNathan Whitehorn void
pmac_thermal_fan_register(struct pmac_fan * fan)19384d77676SNathan Whitehorn pmac_thermal_fan_register(struct pmac_fan *fan)
19484d77676SNathan Whitehorn {
19584d77676SNathan Whitehorn 	struct pmac_fan_le *list_entry;
19684d77676SNathan Whitehorn 
19784d77676SNathan Whitehorn 	list_entry = malloc(sizeof(struct pmac_fan_le), M_PMACTHERM,
19884d77676SNathan Whitehorn 	    M_ZERO | M_WAITOK);
19984d77676SNathan Whitehorn 	list_entry->fan = fan;
20084d77676SNathan Whitehorn 
20184d77676SNathan Whitehorn 	SLIST_INSERT_HEAD(&fans, list_entry, entries);
20284d77676SNathan Whitehorn }
20384d77676SNathan Whitehorn 
20484d77676SNathan Whitehorn void
pmac_thermal_sensor_register(struct pmac_therm * sensor)20584d77676SNathan Whitehorn pmac_thermal_sensor_register(struct pmac_therm *sensor)
20684d77676SNathan Whitehorn {
20784d77676SNathan Whitehorn 	struct pmac_sens_le *list_entry;
20884d77676SNathan Whitehorn 
20984d77676SNathan Whitehorn 	list_entry = malloc(sizeof(struct pmac_sens_le), M_PMACTHERM,
21084d77676SNathan Whitehorn 	    M_ZERO | M_WAITOK);
21184d77676SNathan Whitehorn 	list_entry->sensor = sensor;
212a3550e73SNathan Whitehorn 	list_entry->last_val = 0;
213a3550e73SNathan Whitehorn 	list_entry->critical_count = 0;
21484d77676SNathan Whitehorn 
21584d77676SNathan Whitehorn 	SLIST_INSERT_HEAD(&sensors, list_entry, entries);
21684d77676SNathan Whitehorn }
217