powermac_thermal.c revision 222460
1/*- 2 * Copyright (c) 2009-2011 Nathan Whitehorn 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/powerpc/powermac/powermac_thermal.c 222460 2011-05-29 19:53:11Z nwhitehorn $"); 29 30#include <sys/param.h> 31#include <sys/kernel.h> 32#include <sys/lock.h> 33#include <sys/mutex.h> 34#include <sys/systm.h> 35 36#include <sys/types.h> 37#include <sys/kthread.h> 38#include <sys/malloc.h> 39#include <sys/reboot.h> 40#include <sys/sysctl.h> 41#include <sys/queue.h> 42 43#include "powermac_thermal.h" 44 45static void fan_management_proc(void); 46static void pmac_therm_manage_fans(void); 47 48static struct proc *pmac_them_proc; 49static int enable_pmac_thermal = 1; 50 51static struct kproc_desc pmac_therm_kp = { 52 "pmac_thermal", 53 fan_management_proc, 54 &pmac_them_proc 55}; 56 57SYSINIT(pmac_therm_setup, SI_SUB_KTHREAD_IDLE, SI_ORDER_ANY, kproc_start, 58 &pmac_therm_kp); 59SYSCTL_INT(_machdep, OID_AUTO, manage_fans, CTLFLAG_RW | CTLFLAG_TUN, 60 &enable_pmac_thermal, 1, "Enable automatic fan management"); 61MALLOC_DEFINE(M_PMACTHERM, "pmactherm", "Powermac Thermal Management"); 62 63struct pmac_fan_le { 64 struct pmac_fan *fan; 65 int last_val; 66 SLIST_ENTRY(pmac_fan_le) entries; 67}; 68struct pmac_sens_le { 69 struct pmac_therm *sensor; 70 int last_val; 71 SLIST_ENTRY(pmac_sens_le) entries; 72}; 73static SLIST_HEAD(pmac_fans, pmac_fan_le) fans = SLIST_HEAD_INITIALIZER(fans); 74static SLIST_HEAD(pmac_sensors, pmac_sens_le) sensors = 75 SLIST_HEAD_INITIALIZER(sensors); 76 77static void 78fan_management_proc(void) 79{ 80 /* Nothing to manage? */ 81 if (SLIST_EMPTY(&fans)) 82 return; 83 84 while (1) { 85 pmac_therm_manage_fans(); 86 pause("pmac_therm", hz); 87 } 88} 89 90static void 91pmac_therm_manage_fans(void) 92{ 93 struct pmac_sens_le *sensor; 94 struct pmac_fan_le *fan; 95 int average_excess, max_excess_zone, frac_excess; 96 int nsens, nsens_zone; 97 98 if (!enable_pmac_thermal) 99 return; 100 101 /* Read all the sensors */ 102 SLIST_FOREACH(sensor, &sensors, entries) { 103 sensor->last_val = sensor->sensor->read(sensor->sensor); 104 if (sensor->last_val > sensor->sensor->max_temp) { 105 printf("WARNING: Current temperature (%s: %d.%d C) " 106 "exceeds critical temperature (%d.%d C)! " 107 "Shutting down!\n", sensor->sensor->name, 108 sensor->last_val / 10, sensor->last_val % 10, 109 sensor->sensor->max_temp / 10, 110 sensor->sensor->max_temp % 10); 111 shutdown_nice(RB_POWEROFF); 112 } 113 } 114 115 /* Set all the fans */ 116 SLIST_FOREACH(fan, &fans, entries) { 117 nsens = nsens_zone = 0; 118 average_excess = max_excess_zone = 0; 119 SLIST_FOREACH(sensor, &sensors, entries) { 120 frac_excess = (sensor->last_val - 121 sensor->sensor->target_temp)*100 / 122 (sensor->sensor->max_temp - 123 sensor->sensor->target_temp); 124 if (frac_excess < 0) 125 frac_excess = 0; 126 if (sensor->sensor->zone == fan->fan->zone) { 127 max_excess_zone = imax(max_excess_zone, 128 frac_excess); 129 nsens_zone++; 130 } 131 average_excess += frac_excess; 132 nsens++; 133 } 134 average_excess /= nsens; 135 136 /* If there are no sensors in this zone, use the average */ 137 if (nsens_zone == 0) 138 max_excess_zone = average_excess; 139 /* No sensors at all? Use default */ 140 if (nsens == 0) { 141 fan->fan->set(fan->fan, fan->fan->default_rpm); 142 continue; 143 } 144 145 /* 146 * Scale the fan linearly in the max temperature in its 147 * thermal zone. 148 */ 149 fan->fan->set(fan->fan, max_excess_zone * 150 (fan->fan->max_rpm - fan->fan->min_rpm)/100 + 151 fan->fan->min_rpm); 152 } 153} 154 155void 156pmac_thermal_fan_register(struct pmac_fan *fan) 157{ 158 struct pmac_fan_le *list_entry; 159 160 list_entry = malloc(sizeof(struct pmac_fan_le), M_PMACTHERM, 161 M_ZERO | M_WAITOK); 162 list_entry->fan = fan; 163 164 SLIST_INSERT_HEAD(&fans, list_entry, entries); 165} 166 167void 168pmac_thermal_sensor_register(struct pmac_therm *sensor) 169{ 170 struct pmac_sens_le *list_entry; 171 172 list_entry = malloc(sizeof(struct pmac_sens_le), M_PMACTHERM, 173 M_ZERO | M_WAITOK); 174 list_entry->sensor = sensor; 175 176 SLIST_INSERT_HEAD(&sensors, list_entry, entries); 177} 178 179