| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * processor_thermal.c - Passive cooling submodule of the ACPI processor driver | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | 
|  | 5 | *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | 
|  | 6 | *  Copyright (C) 2004       Dominik Brodowski <linux@brodo.de> | 
|  | 7 | *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> | 
|  | 8 | *  			- Added processor hotplug support | 
|  | 9 | * | 
|  | 10 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|  | 11 | * | 
|  | 12 | *  This program is free software; you can redistribute it and/or modify | 
|  | 13 | *  it under the terms of the GNU General Public License as published by | 
|  | 14 | *  the Free Software Foundation; either version 2 of the License, or (at | 
|  | 15 | *  your option) any later version. | 
|  | 16 | * | 
|  | 17 | *  This program is distributed in the hope that it will be useful, but | 
|  | 18 | *  WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 19 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 20 | *  General Public License for more details. | 
|  | 21 | * | 
|  | 22 | *  You should have received a copy of the GNU General Public License along | 
|  | 23 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 24 | *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | 
|  | 25 | * | 
|  | 26 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <linux/kernel.h> | 
|  | 30 | #include <linux/module.h> | 
|  | 31 | #include <linux/init.h> | 
|  | 32 | #include <linux/cpufreq.h> | 
|  | 33 | #include <linux/proc_fs.h> | 
|  | 34 | #include <linux/seq_file.h> | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 35 | #include <linux/sysdev.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | #include <asm/uaccess.h> | 
|  | 38 |  | 
|  | 39 | #include <acpi/acpi_bus.h> | 
|  | 40 | #include <acpi/processor.h> | 
|  | 41 | #include <acpi/acpi_drivers.h> | 
|  | 42 |  | 
|  | 43 | #define ACPI_PROCESSOR_COMPONENT        0x01000000 | 
|  | 44 | #define ACPI_PROCESSOR_CLASS            "processor" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define _COMPONENT              ACPI_PROCESSOR_COMPONENT | 
| Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 46 | ACPI_MODULE_NAME("processor_thermal"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
|  | 48 | /* -------------------------------------------------------------------------- | 
|  | 49 | Limit Interface | 
|  | 50 | -------------------------------------------------------------------------- */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 51 | static int acpi_processor_apply_limit(struct acpi_processor *pr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 53 | int result = 0; | 
|  | 54 | u16 px = 0; | 
|  | 55 | u16 tx = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | if (!pr) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 59 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | if (!pr->flags.limit) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 62 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | if (pr->flags.throttling) { | 
|  | 65 | if (pr->limit.user.tx > tx) | 
|  | 66 | tx = pr->limit.user.tx; | 
|  | 67 | if (pr->limit.thermal.tx > tx) | 
|  | 68 | tx = pr->limit.thermal.tx; | 
|  | 69 |  | 
|  | 70 | result = acpi_processor_set_throttling(pr, tx); | 
|  | 71 | if (result) | 
|  | 72 | goto end; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | pr->limit.state.px = px; | 
|  | 76 | pr->limit.state.tx = tx; | 
|  | 77 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 78 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
|  | 79 | "Processor [%d] limit set to (P%d:T%d)\n", pr->id, | 
|  | 80 | pr->limit.state.px, pr->limit.state.tx)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 82 | end: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (result) | 
| Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 84 | printk(KERN_ERR PREFIX "Unable to set limit\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 |  | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 86 | return result; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #ifdef CONFIG_CPU_FREQ | 
|  | 90 |  | 
|  | 91 | /* If a passive cooling situation is detected, primarily CPUfreq is used, as it | 
|  | 92 | * offers (in most cases) voltage scaling in addition to frequency scaling, and | 
|  | 93 | * thus a cubic (instead of linear) reduction of energy. Also, we allow for | 
|  | 94 | * _any_ cpufreq driver and not only the acpi-cpufreq driver. | 
|  | 95 | */ | 
|  | 96 |  | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 97 | #define CPUFREQ_THERMAL_MIN_STEP 0 | 
|  | 98 | #define CPUFREQ_THERMAL_MAX_STEP 3 | 
|  | 99 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 100 | static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_pctg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | static unsigned int acpi_thermal_cpufreq_is_init = 0; | 
|  | 102 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static int cpu_has_cpufreq(unsigned int cpu) | 
|  | 104 | { | 
|  | 105 | struct cpufreq_policy policy; | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 106 | if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu)) | 
| Thomas Renninger | 75b245b3 | 2005-12-21 01:29:00 -0500 | [diff] [blame] | 107 | return 0; | 
|  | 108 | return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | static int acpi_thermal_cpufreq_increase(unsigned int cpu) | 
|  | 112 | { | 
|  | 113 | if (!cpu_has_cpufreq(cpu)) | 
|  | 114 | return -ENODEV; | 
|  | 115 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 116 | if (per_cpu(cpufreq_thermal_reduction_pctg, cpu) < | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 117 | CPUFREQ_THERMAL_MAX_STEP) { | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 118 | per_cpu(cpufreq_thermal_reduction_pctg, cpu)++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | cpufreq_update_policy(cpu); | 
|  | 120 | return 0; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | return -ERANGE; | 
|  | 124 | } | 
|  | 125 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | static int acpi_thermal_cpufreq_decrease(unsigned int cpu) | 
|  | 127 | { | 
|  | 128 | if (!cpu_has_cpufreq(cpu)) | 
|  | 129 | return -ENODEV; | 
|  | 130 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 131 | if (per_cpu(cpufreq_thermal_reduction_pctg, cpu) > | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 132 | (CPUFREQ_THERMAL_MIN_STEP + 1)) | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 133 | per_cpu(cpufreq_thermal_reduction_pctg, cpu)--; | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 134 | else | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 135 | per_cpu(cpufreq_thermal_reduction_pctg, cpu) = 0; | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 136 | cpufreq_update_policy(cpu); | 
|  | 137 | /* We reached max freq again and can leave passive mode */ | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 138 | return !per_cpu(cpufreq_thermal_reduction_pctg, cpu); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 141 | static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb, | 
|  | 142 | unsigned long event, void *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { | 
|  | 144 | struct cpufreq_policy *policy = data; | 
|  | 145 | unsigned long max_freq = 0; | 
|  | 146 |  | 
|  | 147 | if (event != CPUFREQ_ADJUST) | 
|  | 148 | goto out; | 
|  | 149 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 150 | max_freq = ( | 
|  | 151 | policy->cpuinfo.max_freq * | 
|  | 152 | (100 - per_cpu(cpufreq_thermal_reduction_pctg, policy->cpu) * 20) | 
|  | 153 | ) / 100; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 |  | 
|  | 155 | cpufreq_verify_within_limits(policy, 0, max_freq); | 
|  | 156 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 157 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return 0; | 
|  | 159 | } | 
|  | 160 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | static struct notifier_block acpi_thermal_cpufreq_notifier_block = { | 
|  | 162 | .notifier_call = acpi_thermal_cpufreq_notifier, | 
|  | 163 | }; | 
|  | 164 |  | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 165 | static int cpufreq_get_max_state(unsigned int cpu) | 
|  | 166 | { | 
|  | 167 | if (!cpu_has_cpufreq(cpu)) | 
|  | 168 | return 0; | 
|  | 169 |  | 
|  | 170 | return CPUFREQ_THERMAL_MAX_STEP; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | static int cpufreq_get_cur_state(unsigned int cpu) | 
|  | 174 | { | 
|  | 175 | if (!cpu_has_cpufreq(cpu)) | 
|  | 176 | return 0; | 
|  | 177 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 178 | return per_cpu(cpufreq_thermal_reduction_pctg, cpu); | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
|  | 181 | static int cpufreq_set_cur_state(unsigned int cpu, int state) | 
|  | 182 | { | 
|  | 183 | if (!cpu_has_cpufreq(cpu)) | 
|  | 184 | return 0; | 
|  | 185 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 186 | per_cpu(cpufreq_thermal_reduction_pctg, cpu) = state; | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 187 | cpufreq_update_policy(cpu); | 
|  | 188 | return 0; | 
|  | 189 | } | 
|  | 190 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 191 | void acpi_thermal_cpufreq_init(void) | 
|  | 192 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | int i; | 
|  | 194 |  | 
| Mike Travis | c938ac2 | 2008-03-05 08:31:29 -0800 | [diff] [blame] | 195 | for (i = 0; i < nr_cpu_ids; i++) | 
|  | 196 | if (cpu_present(i)) | 
|  | 197 | per_cpu(cpufreq_thermal_reduction_pctg, i) = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 199 | i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block, | 
|  | 200 | CPUFREQ_POLICY_NOTIFIER); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (!i) | 
|  | 202 | acpi_thermal_cpufreq_is_init = 1; | 
|  | 203 | } | 
|  | 204 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 205 | void acpi_thermal_cpufreq_exit(void) | 
|  | 206 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (acpi_thermal_cpufreq_is_init) | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 208 | cpufreq_unregister_notifier | 
|  | 209 | (&acpi_thermal_cpufreq_notifier_block, | 
|  | 210 | CPUFREQ_POLICY_NOTIFIER); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 |  | 
|  | 212 | acpi_thermal_cpufreq_is_init = 0; | 
|  | 213 | } | 
|  | 214 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 215 | #else				/* ! CONFIG_CPU_FREQ */ | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 216 | static int cpufreq_get_max_state(unsigned int cpu) | 
|  | 217 | { | 
|  | 218 | return 0; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | static int cpufreq_get_cur_state(unsigned int cpu) | 
|  | 222 | { | 
|  | 223 | return 0; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | static int cpufreq_set_cur_state(unsigned int cpu, int state) | 
|  | 227 | { | 
|  | 228 | return 0; | 
|  | 229 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 231 | static int acpi_thermal_cpufreq_increase(unsigned int cpu) | 
|  | 232 | { | 
|  | 233 | return -ENODEV; | 
|  | 234 | } | 
|  | 235 | static int acpi_thermal_cpufreq_decrease(unsigned int cpu) | 
|  | 236 | { | 
|  | 237 | return -ENODEV; | 
|  | 238 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 |  | 
|  | 240 | #endif | 
|  | 241 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 242 | int acpi_processor_set_thermal_limit(acpi_handle handle, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 244 | int result = 0; | 
|  | 245 | struct acpi_processor *pr = NULL; | 
|  | 246 | struct acpi_device *device = NULL; | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 247 | int tx = 0, max_tx_px = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 |  | 
|  | 250 | if ((type < ACPI_PROCESSOR_LIMIT_NONE) | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 251 | || (type > ACPI_PROCESSOR_LIMIT_DECREMENT)) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 252 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 |  | 
|  | 254 | result = acpi_bus_get_device(handle, &device); | 
|  | 255 | if (result) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 256 | return result; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 |  | 
| Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 258 | pr = acpi_driver_data(device); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (!pr) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 260 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 |  | 
|  | 262 | /* Thermal limits are always relative to the current Px/Tx state. */ | 
|  | 263 | if (pr->flags.throttling) | 
|  | 264 | pr->limit.thermal.tx = pr->throttling.state; | 
|  | 265 |  | 
|  | 266 | /* | 
|  | 267 | * Our default policy is to only use throttling at the lowest | 
|  | 268 | * performance state. | 
|  | 269 | */ | 
|  | 270 |  | 
|  | 271 | tx = pr->limit.thermal.tx; | 
|  | 272 |  | 
|  | 273 | switch (type) { | 
|  | 274 |  | 
|  | 275 | case ACPI_PROCESSOR_LIMIT_NONE: | 
|  | 276 | do { | 
|  | 277 | result = acpi_thermal_cpufreq_decrease(pr->id); | 
|  | 278 | } while (!result); | 
|  | 279 | tx = 0; | 
|  | 280 | break; | 
|  | 281 |  | 
|  | 282 | case ACPI_PROCESSOR_LIMIT_INCREMENT: | 
|  | 283 | /* if going up: P-states first, T-states later */ | 
|  | 284 |  | 
|  | 285 | result = acpi_thermal_cpufreq_increase(pr->id); | 
|  | 286 | if (!result) | 
|  | 287 | goto end; | 
|  | 288 | else if (result == -ERANGE) | 
|  | 289 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 290 | "At maximum performance state\n")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
|  | 292 | if (pr->flags.throttling) { | 
|  | 293 | if (tx == (pr->throttling.state_count - 1)) | 
|  | 294 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 295 | "At maximum throttling state\n")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | else | 
|  | 297 | tx++; | 
|  | 298 | } | 
|  | 299 | break; | 
|  | 300 |  | 
|  | 301 | case ACPI_PROCESSOR_LIMIT_DECREMENT: | 
|  | 302 | /* if going down: T-states first, P-states later */ | 
|  | 303 |  | 
|  | 304 | if (pr->flags.throttling) { | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 305 | if (tx == 0) { | 
|  | 306 | max_tx_px = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 308 | "At minimum throttling state\n")); | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 309 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | tx--; | 
|  | 311 | goto end; | 
|  | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | result = acpi_thermal_cpufreq_decrease(pr->id); | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 316 | if (result) { | 
|  | 317 | /* | 
|  | 318 | * We only could get -ERANGE, 1 or 0. | 
|  | 319 | * In the first two cases we reached max freq again. | 
|  | 320 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 322 | "At minimum performance state\n")); | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 323 | max_tx_px = 1; | 
|  | 324 | } else | 
|  | 325 | max_tx_px = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 |  | 
|  | 327 | break; | 
|  | 328 | } | 
|  | 329 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 330 | end: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (pr->flags.throttling) { | 
|  | 332 | pr->limit.thermal.px = 0; | 
|  | 333 | pr->limit.thermal.tx = tx; | 
|  | 334 |  | 
|  | 335 | result = acpi_processor_apply_limit(pr); | 
|  | 336 | if (result) | 
| Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 337 | printk(KERN_ERR PREFIX "Unable to set thermal limit\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 |  | 
|  | 339 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n", | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 340 | pr->limit.thermal.px, pr->limit.thermal.tx)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } else | 
|  | 342 | result = 0; | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 343 | if (max_tx_px) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 344 | return 1; | 
| Thomas Renninger | 1cbf4c5 | 2004-09-16 11:07:00 -0400 | [diff] [blame] | 345 | else | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 346 | return result; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 349 | int acpi_processor_get_limit_info(struct acpi_processor *pr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 |  | 
|  | 352 | if (!pr) | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 353 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 |  | 
|  | 355 | if (pr->flags.throttling) | 
|  | 356 | pr->flags.limit = 1; | 
|  | 357 |  | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 358 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
| Zhang Rui | d9460fd22 | 2008-01-17 15:51:23 +0800 | [diff] [blame] | 361 | /* thermal coolign device callbacks */ | 
|  | 362 | static int acpi_processor_max_state(struct acpi_processor *pr) | 
|  | 363 | { | 
|  | 364 | int max_state = 0; | 
|  | 365 |  | 
|  | 366 | /* | 
|  | 367 | * There exists four states according to | 
|  | 368 | * cpufreq_thermal_reduction_ptg. 0, 1, 2, 3 | 
|  | 369 | */ | 
|  | 370 | max_state += cpufreq_get_max_state(pr->id); | 
|  | 371 | if (pr->flags.throttling) | 
|  | 372 | max_state += (pr->throttling.state_count -1); | 
|  | 373 |  | 
|  | 374 | return max_state; | 
|  | 375 | } | 
|  | 376 | static int | 
|  | 377 | processor_get_max_state(struct thermal_cooling_device *cdev, char *buf) | 
|  | 378 | { | 
|  | 379 | struct acpi_device *device = cdev->devdata; | 
|  | 380 | struct acpi_processor *pr = acpi_driver_data(device); | 
|  | 381 |  | 
|  | 382 | if (!device || !pr) | 
|  | 383 | return -EINVAL; | 
|  | 384 |  | 
|  | 385 | return sprintf(buf, "%d\n", acpi_processor_max_state(pr)); | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | static int | 
|  | 389 | processor_get_cur_state(struct thermal_cooling_device *cdev, char *buf) | 
|  | 390 | { | 
|  | 391 | struct acpi_device *device = cdev->devdata; | 
|  | 392 | struct acpi_processor *pr = acpi_driver_data(device); | 
|  | 393 | int cur_state; | 
|  | 394 |  | 
|  | 395 | if (!device || !pr) | 
|  | 396 | return -EINVAL; | 
|  | 397 |  | 
|  | 398 | cur_state = cpufreq_get_cur_state(pr->id); | 
|  | 399 | if (pr->flags.throttling) | 
|  | 400 | cur_state += pr->throttling.state; | 
|  | 401 |  | 
|  | 402 | return sprintf(buf, "%d\n", cur_state); | 
|  | 403 | } | 
|  | 404 |  | 
|  | 405 | static int | 
|  | 406 | processor_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state) | 
|  | 407 | { | 
|  | 408 | struct acpi_device *device = cdev->devdata; | 
|  | 409 | struct acpi_processor *pr = acpi_driver_data(device); | 
|  | 410 | int result = 0; | 
|  | 411 | int max_pstate; | 
|  | 412 |  | 
|  | 413 | if (!device || !pr) | 
|  | 414 | return -EINVAL; | 
|  | 415 |  | 
|  | 416 | max_pstate = cpufreq_get_max_state(pr->id); | 
|  | 417 |  | 
|  | 418 | if (state > acpi_processor_max_state(pr)) | 
|  | 419 | return -EINVAL; | 
|  | 420 |  | 
|  | 421 | if (state <= max_pstate) { | 
|  | 422 | if (pr->flags.throttling && pr->throttling.state) | 
|  | 423 | result = acpi_processor_set_throttling(pr, 0); | 
|  | 424 | cpufreq_set_cur_state(pr->id, state); | 
|  | 425 | } else { | 
|  | 426 | cpufreq_set_cur_state(pr->id, max_pstate); | 
|  | 427 | result = acpi_processor_set_throttling(pr, | 
|  | 428 | state - max_pstate); | 
|  | 429 | } | 
|  | 430 | return result; | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | struct thermal_cooling_device_ops processor_cooling_ops = { | 
|  | 434 | .get_max_state = processor_get_max_state, | 
|  | 435 | .get_cur_state = processor_get_cur_state, | 
|  | 436 | .set_cur_state = processor_set_cur_state, | 
|  | 437 | }; | 
|  | 438 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* /proc interface */ | 
|  | 440 |  | 
|  | 441 | static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset) | 
|  | 442 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 443 | struct acpi_processor *pr = (struct acpi_processor *)seq->private; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 |  | 
|  | 446 | if (!pr) | 
|  | 447 | goto end; | 
|  | 448 |  | 
|  | 449 | if (!pr->flags.limit) { | 
|  | 450 | seq_puts(seq, "<not supported>\n"); | 
|  | 451 | goto end; | 
|  | 452 | } | 
|  | 453 |  | 
|  | 454 | seq_printf(seq, "active limit:            P%d:T%d\n" | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 455 | "user limit:              P%d:T%d\n" | 
|  | 456 | "thermal limit:           P%d:T%d\n", | 
|  | 457 | pr->limit.state.px, pr->limit.state.tx, | 
|  | 458 | pr->limit.user.px, pr->limit.user.tx, | 
|  | 459 | pr->limit.thermal.px, pr->limit.thermal.tx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 461 | end: | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 462 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } | 
|  | 464 |  | 
|  | 465 | static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file) | 
|  | 466 | { | 
|  | 467 | return single_open(file, acpi_processor_limit_seq_show, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 468 | PDE(inode)->data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
| Adrian Bunk | 757b186 | 2006-01-07 13:19:00 -0500 | [diff] [blame] | 471 | static ssize_t acpi_processor_write_limit(struct file * file, | 
|  | 472 | const char __user * buffer, | 
|  | 473 | size_t count, loff_t * data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 475 | int result = 0; | 
| Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 476 | struct seq_file *m = file->private_data; | 
|  | 477 | struct acpi_processor *pr = m->private; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 478 | char limit_string[25] = { '\0' }; | 
|  | 479 | int px = 0; | 
|  | 480 | int tx = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 |  | 
|  | 483 | if (!pr || (count > sizeof(limit_string) - 1)) { | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 484 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | } | 
|  | 486 |  | 
|  | 487 | if (copy_from_user(limit_string, buffer, count)) { | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 488 | return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } | 
|  | 490 |  | 
|  | 491 | limit_string[count] = '\0'; | 
|  | 492 |  | 
|  | 493 | if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) { | 
| Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 494 | printk(KERN_ERR PREFIX "Invalid data format\n"); | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 495 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } | 
|  | 497 |  | 
|  | 498 | if (pr->flags.throttling) { | 
|  | 499 | if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) { | 
| Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 500 | printk(KERN_ERR PREFIX "Invalid tx\n"); | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 501 | return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } | 
|  | 503 | pr->limit.user.tx = tx; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | result = acpi_processor_apply_limit(pr); | 
|  | 507 |  | 
| Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 508 | return count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } | 
|  | 510 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | struct file_operations acpi_processor_limit_fops = { | 
| Denis V. Lunev | cf7acfa | 2008-04-29 01:02:27 -0700 | [diff] [blame] | 512 | .owner = THIS_MODULE, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 513 | .open = acpi_processor_limit_open_fs, | 
|  | 514 | .read = seq_read, | 
| Arjan van de Ven | d479e90 | 2006-01-06 16:47:00 -0500 | [diff] [blame] | 515 | .write = acpi_processor_write_limit, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 516 | .llseek = seq_lseek, | 
|  | 517 | .release = single_release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | }; |