2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2011 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/mutex.h>
36 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/kthread.h>
40 #include <sys/malloc.h>
41 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <sys/queue.h>
45 #include "powermac_thermal.h"
47 /* A 10 second timer for spinning down fans. */
48 #define FAN_HYSTERESIS_TIMER 10
50 static void fan_management_proc(void);
51 static void pmac_therm_manage_fans(void);
53 static struct proc *pmac_them_proc;
54 static int enable_pmac_thermal = 1;
56 static struct kproc_desc pmac_therm_kp = {
62 SYSINIT(pmac_therm_setup, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start,
64 SYSCTL_INT(_machdep, OID_AUTO, manage_fans, CTLFLAG_RW | CTLFLAG_TUN,
65 &enable_pmac_thermal, 1, "Enable automatic fan management");
66 static MALLOC_DEFINE(M_PMACTHERM, "pmactherm", "Powermac Thermal Management");
72 SLIST_ENTRY(pmac_fan_le) entries;
75 struct pmac_therm *sensor;
77 #define MAX_CRITICAL_COUNT 6
79 SLIST_ENTRY(pmac_sens_le) entries;
81 static SLIST_HEAD(pmac_fans, pmac_fan_le) fans = SLIST_HEAD_INITIALIZER(fans);
82 static SLIST_HEAD(pmac_sensors, pmac_sens_le) sensors =
83 SLIST_HEAD_INITIALIZER(sensors);
86 fan_management_proc(void)
88 /* Nothing to manage? */
89 if (SLIST_EMPTY(&fans))
93 pmac_therm_manage_fans();
94 pause("pmac_therm", hz);
99 pmac_therm_manage_fans(void)
101 struct pmac_sens_le *sensor;
102 struct pmac_fan_le *fan;
103 int average_excess, max_excess_zone, frac_excess;
105 int nsens, nsens_zone;
108 if (!enable_pmac_thermal)
111 /* Read all the sensors */
112 SLIST_FOREACH(sensor, &sensors, entries) {
113 temp = sensor->sensor->read(sensor->sensor);
114 if (temp > 0) /* Use the previous temp in case of error */
115 sensor->last_val = temp;
117 if (sensor->last_val > sensor->sensor->max_temp) {
118 sensor->critical_count++;
119 printf("WARNING: Current temperature (%s: %d.%d C) "
120 "exceeds critical temperature (%d.%d C); "
122 sensor->sensor->name,
123 (sensor->last_val - ZERO_C_TO_K) / 10,
124 (sensor->last_val - ZERO_C_TO_K) % 10,
125 (sensor->sensor->max_temp - ZERO_C_TO_K) / 10,
126 (sensor->sensor->max_temp - ZERO_C_TO_K) % 10,
127 sensor->critical_count);
128 if (sensor->critical_count >= MAX_CRITICAL_COUNT) {
129 printf("WARNING: %s temperature exceeded "
130 "critical temperature %d times in a row; "
132 sensor->sensor->name,
133 sensor->critical_count);
134 shutdown_nice(RB_POWEROFF);
137 if (sensor->critical_count > 0)
138 sensor->critical_count--;
142 /* Set all the fans */
143 SLIST_FOREACH(fan, &fans, entries) {
144 nsens = nsens_zone = 0;
145 average_excess = max_excess_zone = 0;
146 SLIST_FOREACH(sensor, &sensors, entries) {
147 temp = imin(sensor->last_val,
148 sensor->sensor->max_temp);
149 frac_excess = (temp -
150 sensor->sensor->target_temp)*100 /
151 (sensor->sensor->max_temp - temp + 1);
154 if (sensor->sensor->zone == fan->fan->zone) {
155 max_excess_zone = imax(max_excess_zone,
159 average_excess += frac_excess;
162 average_excess /= nsens;
164 /* If there are no sensors in this zone, use the average */
166 max_excess_zone = average_excess;
167 /* No sensors at all? Use default */
169 fan->fan->set(fan->fan, fan->fan->default_rpm);
174 * Scale the fan linearly in the max temperature in its
177 max_excess_zone = imin(max_excess_zone, 100);
178 fan_speed = max_excess_zone *
179 (fan->fan->max_rpm - fan->fan->min_rpm)/100 +
181 if (fan_speed >= fan->last_val) {
182 fan->timer = FAN_HYSTERESIS_TIMER;
183 fan->last_val = fan_speed;
186 if (fan->timer == 0) {
187 fan->last_val = fan_speed;
188 fan->timer = FAN_HYSTERESIS_TIMER;
191 fan->fan->set(fan->fan, fan->last_val);
196 pmac_thermal_fan_register(struct pmac_fan *fan)
198 struct pmac_fan_le *list_entry;
200 list_entry = malloc(sizeof(struct pmac_fan_le), M_PMACTHERM,
202 list_entry->fan = fan;
204 SLIST_INSERT_HEAD(&fans, list_entry, entries);
208 pmac_thermal_sensor_register(struct pmac_therm *sensor)
210 struct pmac_sens_le *list_entry;
212 list_entry = malloc(sizeof(struct pmac_sens_le), M_PMACTHERM,
214 list_entry->sensor = sensor;
215 list_entry->last_val = 0;
216 list_entry->critical_count = 0;
218 SLIST_INSERT_HEAD(&sensors, list_entry, entries);