blob: 6e2becf547c5e4aba74eb9c3a16d050df2cc970a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Dmitriy Zavin15d5f832006-09-26 10:52:42 +02002/*
Dmitriy Zavin3222b362006-09-26 10:52:42 +02003 * Thermal throttle event support code (such as syslog messaging and rate
4 * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
Ingo Molnarcb6f3c12009-04-08 12:31:19 +02005 *
Dmitriy Zavin3222b362006-09-26 10:52:42 +02006 * This allows consistent reporting of CPU thermal throttle events.
7 *
8 * Maintains a counter in /sys that keeps track of the number of thermal
9 * events, such that the user knows how bad the thermal problem might be
Borislav Petkov9b052ea2017-01-23 19:35:07 +010010 * (since the logging to syslog is rate limited).
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020011 *
12 * Author: Dmitriy Zavin (dmitriyz@google.com)
13 *
14 * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
Dmitriy Zavin3222b362006-09-26 10:52:42 +020015 * Inspired by Ross Biro's and Al Borchers' counter code.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020016 */
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +090017#include <linux/interrupt.h>
Ingo Molnarcb6f3c12009-04-08 12:31:19 +020018#include <linux/notifier.h>
19#include <linux/jiffies.h>
Hidetoshi Seto895287c2009-06-15 17:26:10 +090020#include <linux/kernel.h>
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020021#include <linux/percpu.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040022#include <linux/export.h>
Hidetoshi Seto895287c2009-06-15 17:26:10 +090023#include <linux/types.h>
24#include <linux/init.h>
25#include <linux/smp.h>
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020026#include <linux/cpu.h>
Ingo Molnarcb6f3c12009-04-08 12:31:19 +020027
Hidetoshi Seto895287c2009-06-15 17:26:10 +090028#include <asm/processor.h>
Borislav Petkov68b5e4322018-11-09 23:13:13 +010029#include <asm/traps.h>
Hidetoshi Seto895287c2009-06-15 17:26:10 +090030#include <asm/apic.h>
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +090031#include <asm/mce.h>
Hidetoshi Seto895287c2009-06-15 17:26:10 +090032#include <asm/msr.h>
Seiji Aguchicf910e82013-06-20 11:46:53 -040033#include <asm/trace/irq_vectors.h>
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020034
Borislav Petkov3bfaf952018-12-05 21:05:13 +010035#include "internal.h"
36
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020037/* How long to wait between reporting thermal events */
Ingo Molnarcb6f3c12009-04-08 12:31:19 +020038#define CHECK_INTERVAL (300 * HZ)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +020039
Fenghua Yu01991142010-07-29 17:13:45 -070040#define THERMAL_THROTTLING_EVENT 0
41#define POWER_LIMIT_EVENT 1
42
Ingo Molnar39676842009-09-22 15:50:24 +020043/*
Fenghua Yu01991142010-07-29 17:13:45 -070044 * Current thermal event state:
Ingo Molnar39676842009-09-22 15:50:24 +020045 */
Fenghua Yu55d435a2010-07-29 17:13:44 -070046struct _thermal_state {
Fenghua Yu01991142010-07-29 17:13:45 -070047 bool new_event;
48 int event;
Ingo Molnar39676842009-09-22 15:50:24 +020049 u64 next_check;
Fenghua Yu01991142010-07-29 17:13:45 -070050 unsigned long count;
51 unsigned long last_count;
Ingo Molnar39676842009-09-22 15:50:24 +020052};
53
Fenghua Yu55d435a2010-07-29 17:13:44 -070054struct thermal_state {
Fenghua Yu01991142010-07-29 17:13:45 -070055 struct _thermal_state core_throttle;
56 struct _thermal_state core_power_limit;
57 struct _thermal_state package_throttle;
58 struct _thermal_state package_power_limit;
R, Durgadoss9e76a972011-01-03 17:22:04 +053059 struct _thermal_state core_thresh0;
60 struct _thermal_state core_thresh1;
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +000061 struct _thermal_state pkg_thresh0;
62 struct _thermal_state pkg_thresh1;
Fenghua Yu55d435a2010-07-29 17:13:44 -070063};
64
R, Durgadoss9e76a972011-01-03 17:22:04 +053065/* Callback to handle core threshold interrupts */
66int (*platform_thermal_notify)(__u64 msr_val);
Fenghua Yuf21bbec2011-01-20 20:12:40 -080067EXPORT_SYMBOL(platform_thermal_notify);
R, Durgadoss9e76a972011-01-03 17:22:04 +053068
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +000069/* Callback to handle core package threshold_interrupts */
70int (*platform_thermal_package_notify)(__u64 msr_val);
71EXPORT_SYMBOL_GPL(platform_thermal_package_notify);
72
73/* Callback support of rate control, return true, if
74 * callback has rate control */
75bool (*platform_thermal_package_rate_control)(void);
76EXPORT_SYMBOL_GPL(platform_thermal_package_rate_control);
77
78
Ingo Molnar39676842009-09-22 15:50:24 +020079static DEFINE_PER_CPU(struct thermal_state, thermal_state);
80
81static atomic_t therm_throt_en = ATOMIC_INIT(0);
Dmitriy Zavin3222b362006-09-26 10:52:42 +020082
Yong Wanga2202aa2009-11-10 09:38:24 +080083static u32 lvtthmr_init __read_mostly;
84
Dmitriy Zavin3222b362006-09-26 10:52:42 +020085#ifdef CONFIG_SYSFS
Kay Sievers8a25a2f2011-12-21 14:29:42 -080086#define define_therm_throt_device_one_ro(_name) \
87 static DEVICE_ATTR(_name, 0444, \
88 therm_throt_device_show_##_name, \
Fenghua Yu55d435a2010-07-29 17:13:44 -070089 NULL) \
Dmitriy Zavin3222b362006-09-26 10:52:42 +020090
Kay Sievers8a25a2f2011-12-21 14:29:42 -080091#define define_therm_throt_device_show_func(event, name) \
Ingo Molnar39676842009-09-22 15:50:24 +020092 \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080093static ssize_t therm_throt_device_show_##event##_##name( \
94 struct device *dev, \
95 struct device_attribute *attr, \
Ingo Molnar39676842009-09-22 15:50:24 +020096 char *buf) \
Ingo Molnarcb6f3c12009-04-08 12:31:19 +020097{ \
98 unsigned int cpu = dev->id; \
99 ssize_t ret; \
100 \
101 preempt_disable(); /* CPU hotplug */ \
Fenghua Yu55d435a2010-07-29 17:13:44 -0700102 if (cpu_online(cpu)) { \
Ingo Molnarcb6f3c12009-04-08 12:31:19 +0200103 ret = sprintf(buf, "%lu\n", \
Fenghua Yu01991142010-07-29 17:13:45 -0700104 per_cpu(thermal_state, cpu).event.name); \
Fenghua Yu55d435a2010-07-29 17:13:44 -0700105 } else \
Ingo Molnarcb6f3c12009-04-08 12:31:19 +0200106 ret = 0; \
107 preempt_enable(); \
108 \
109 return ret; \
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200110}
111
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800112define_therm_throt_device_show_func(core_throttle, count);
113define_therm_throt_device_one_ro(core_throttle_count);
Fenghua Yu55d435a2010-07-29 17:13:44 -0700114
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800115define_therm_throt_device_show_func(core_power_limit, count);
116define_therm_throt_device_one_ro(core_power_limit_count);
Fenghua Yu01991142010-07-29 17:13:45 -0700117
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800118define_therm_throt_device_show_func(package_throttle, count);
119define_therm_throt_device_one_ro(package_throttle_count);
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200120
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800121define_therm_throt_device_show_func(package_power_limit, count);
122define_therm_throt_device_one_ro(package_power_limit_count);
Fenghua Yu01991142010-07-29 17:13:45 -0700123
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200124static struct attribute *thermal_throttle_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800125 &dev_attr_core_throttle_count.attr,
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200126 NULL
127};
128
Arvind Yadav45bd07a2017-07-20 17:00:32 +0530129static const struct attribute_group thermal_attr_group = {
Ingo Molnarcb6f3c12009-04-08 12:31:19 +0200130 .attrs = thermal_throttle_attrs,
131 .name = "thermal_throttle"
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200132};
133#endif /* CONFIG_SYSFS */
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200134
Fenghua Yu01991142010-07-29 17:13:45 -0700135#define CORE_LEVEL 0
136#define PACKAGE_LEVEL 1
137
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200138/***
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200139 * therm_throt_process - Process thermal throttling event from interrupt
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200140 * @curr: Whether the condition is current or not (boolean), since the
141 * thermal interrupt normally gets called both when the thermal
142 * event begins and once the event has ended.
143 *
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200144 * This function is called by the thermal interrupt after the
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200145 * IRQ has been acknowledged.
146 *
147 * It will take care of rate limiting and printing messages to the syslog.
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200148 */
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100149static void therm_throt_process(bool new_event, int event, int level)
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200150{
Fenghua Yu55d435a2010-07-29 17:13:44 -0700151 struct _thermal_state *state;
Fenghua Yu01991142010-07-29 17:13:45 -0700152 unsigned int this_cpu = smp_processor_id();
153 bool old_event;
Ingo Molnar39676842009-09-22 15:50:24 +0200154 u64 now;
Fenghua Yu01991142010-07-29 17:13:45 -0700155 struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
Ingo Molnar39676842009-09-22 15:50:24 +0200156
Ingo Molnar39676842009-09-22 15:50:24 +0200157 now = get_jiffies_64();
Fenghua Yu01991142010-07-29 17:13:45 -0700158 if (level == CORE_LEVEL) {
159 if (event == THERMAL_THROTTLING_EVENT)
160 state = &pstate->core_throttle;
161 else if (event == POWER_LIMIT_EVENT)
162 state = &pstate->core_power_limit;
163 else
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100164 return;
Fenghua Yu01991142010-07-29 17:13:45 -0700165 } else if (level == PACKAGE_LEVEL) {
166 if (event == THERMAL_THROTTLING_EVENT)
167 state = &pstate->package_throttle;
168 else if (event == POWER_LIMIT_EVENT)
169 state = &pstate->package_power_limit;
170 else
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100171 return;
Fenghua Yu01991142010-07-29 17:13:45 -0700172 } else
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100173 return;
Ingo Molnar39676842009-09-22 15:50:24 +0200174
Fenghua Yu01991142010-07-29 17:13:45 -0700175 old_event = state->new_event;
176 state->new_event = new_event;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200177
Fenghua Yu01991142010-07-29 17:13:45 -0700178 if (new_event)
179 state->count++;
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200180
Ingo Molnarb417c9f2009-09-22 15:50:24 +0200181 if (time_before64(now, state->next_check) &&
Fenghua Yu01991142010-07-29 17:13:45 -0700182 state->count != state->last_count)
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100183 return;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200184
Ingo Molnar39676842009-09-22 15:50:24 +0200185 state->next_check = now + CHECK_INTERVAL;
Fenghua Yu01991142010-07-29 17:13:45 -0700186 state->last_count = state->count;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200187
188 /* if we just entered the thermal event */
Fenghua Yu01991142010-07-29 17:13:45 -0700189 if (new_event) {
190 if (event == THERMAL_THROTTLING_EVENT)
Chen Yucong1b74dde2016-02-02 11:45:02 +0800191 pr_crit("CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",
Fenghua Yu01991142010-07-29 17:13:45 -0700192 this_cpu,
193 level == CORE_LEVEL ? "Core" : "Package",
194 state->count);
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100195 return;
Hugh Dickins4e5c25d2009-08-16 15:54:37 +0100196 }
Fenghua Yu01991142010-07-29 17:13:45 -0700197 if (old_event) {
198 if (event == THERMAL_THROTTLING_EVENT)
Chen Yucong1b74dde2016-02-02 11:45:02 +0800199 pr_info("CPU%d: %s temperature/speed normal\n", this_cpu,
Fenghua Yu01991142010-07-29 17:13:45 -0700200 level == CORE_LEVEL ? "Core" : "Package");
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100201 return;
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200202 }
Dmitriy Zavin15d5f832006-09-26 10:52:42 +0200203}
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200204
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000205static int thresh_event_valid(int level, int event)
R, Durgadoss9e76a972011-01-03 17:22:04 +0530206{
207 struct _thermal_state *state;
208 unsigned int this_cpu = smp_processor_id();
209 struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
210 u64 now = get_jiffies_64();
211
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000212 if (level == PACKAGE_LEVEL)
213 state = (event == 0) ? &pstate->pkg_thresh0 :
214 &pstate->pkg_thresh1;
215 else
216 state = (event == 0) ? &pstate->core_thresh0 :
217 &pstate->core_thresh1;
R, Durgadoss9e76a972011-01-03 17:22:04 +0530218
219 if (time_before64(now, state->next_check))
220 return 0;
221
222 state->next_check = now + CHECK_INTERVAL;
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000223
R, Durgadoss9e76a972011-01-03 17:22:04 +0530224 return 1;
225}
226
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400227static bool int_pln_enable;
228static int __init int_pln_enable_setup(char *s)
229{
230 int_pln_enable = true;
231
232 return 1;
233}
234__setup("int_pln_enable", int_pln_enable_setup);
235
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200236#ifdef CONFIG_SYSFS
Ingo Molnarcb6f3c12009-04-08 12:31:19 +0200237/* Add/Remove thermal_throttle interface for CPU device: */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400238static int thermal_throttle_add_dev(struct device *dev, unsigned int cpu)
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200239{
Fenghua Yu55d435a2010-07-29 17:13:44 -0700240 int err;
Sergey Senozhatsky51e3c1b2010-08-20 10:36:34 +0300241 struct cpuinfo_x86 *c = &cpu_data(cpu);
Fenghua Yu55d435a2010-07-29 17:13:44 -0700242
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800243 err = sysfs_create_group(&dev->kobj, &thermal_attr_group);
Fenghua Yu55d435a2010-07-29 17:13:44 -0700244 if (err)
245 return err;
246
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400247 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800248 err = sysfs_add_file_to_group(&dev->kobj,
249 &dev_attr_core_power_limit_count.attr,
Fenghua Yu01991142010-07-29 17:13:45 -0700250 thermal_attr_group.name);
Jin Dongmingb62be8e2010-08-26 17:29:05 +0900251 if (cpu_has(c, X86_FEATURE_PTS)) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800252 err = sysfs_add_file_to_group(&dev->kobj,
253 &dev_attr_package_throttle_count.attr,
Fenghua Yu01991142010-07-29 17:13:45 -0700254 thermal_attr_group.name);
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400255 if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800256 err = sysfs_add_file_to_group(&dev->kobj,
257 &dev_attr_package_power_limit_count.attr,
Fenghua Yu01991142010-07-29 17:13:45 -0700258 thermal_attr_group.name);
Jin Dongmingb62be8e2010-08-26 17:29:05 +0900259 }
Fenghua Yu55d435a2010-07-29 17:13:44 -0700260
261 return err;
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200262}
263
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400264static void thermal_throttle_remove_dev(struct device *dev)
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200265{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800266 sysfs_remove_group(&dev->kobj, &thermal_attr_group);
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200267}
268
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200269/* Get notified when a cpu comes on/off. Be hotplug friendly. */
Thomas Gleixner33d97302016-11-21 13:15:32 +0100270static int thermal_throttle_online(unsigned int cpu)
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200271{
Sebastian Andrzej Siewiord6526e72016-11-17 19:35:22 +0100272 struct device *dev = get_cpu_device(cpu);
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200273
Sebastian Andrzej Siewiord6526e72016-11-17 19:35:22 +0100274 return thermal_throttle_add_dev(dev, cpu);
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200275}
276
Thomas Gleixner33d97302016-11-21 13:15:32 +0100277static int thermal_throttle_offline(unsigned int cpu)
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200278{
Sebastian Andrzej Siewiord6526e72016-11-17 19:35:22 +0100279 struct device *dev = get_cpu_device(cpu);
280
281 thermal_throttle_remove_dev(dev);
282 return 0;
283}
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200284
285static __init int thermal_throttle_init_device(void)
286{
Thomas Gleixner33d97302016-11-21 13:15:32 +0100287 int ret;
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200288
289 if (!atomic_read(&therm_throt_en))
290 return 0;
291
Thomas Gleixner33d97302016-11-21 13:15:32 +0100292 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online",
293 thermal_throttle_online,
294 thermal_throttle_offline);
295 return ret < 0 ? ret : 0;
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200296}
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200297device_initcall(thermal_throttle_init_device);
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900298
Dmitriy Zavin3222b362006-09-26 10:52:42 +0200299#endif /* CONFIG_SYSFS */
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900300
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000301static void notify_package_thresholds(__u64 msr_val)
302{
303 bool notify_thres_0 = false;
304 bool notify_thres_1 = false;
305
306 if (!platform_thermal_package_notify)
307 return;
308
309 /* lower threshold check */
310 if (msr_val & THERM_LOG_THRESHOLD0)
311 notify_thres_0 = true;
312 /* higher threshold check */
313 if (msr_val & THERM_LOG_THRESHOLD1)
314 notify_thres_1 = true;
315
316 if (!notify_thres_0 && !notify_thres_1)
317 return;
318
319 if (platform_thermal_package_rate_control &&
320 platform_thermal_package_rate_control()) {
321 /* Rate control is implemented in callback */
322 platform_thermal_package_notify(msr_val);
323 return;
324 }
325
326 /* lower threshold reached */
327 if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0))
328 platform_thermal_package_notify(msr_val);
329 /* higher threshold reached */
330 if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1))
331 platform_thermal_package_notify(msr_val);
332}
333
R, Durgadoss9e76a972011-01-03 17:22:04 +0530334static void notify_thresholds(__u64 msr_val)
335{
336 /* check whether the interrupt handler is defined;
337 * otherwise simply return
338 */
339 if (!platform_thermal_notify)
340 return;
341
342 /* lower threshold reached */
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000343 if ((msr_val & THERM_LOG_THRESHOLD0) &&
344 thresh_event_valid(CORE_LEVEL, 0))
R, Durgadoss9e76a972011-01-03 17:22:04 +0530345 platform_thermal_notify(msr_val);
346 /* higher threshold reached */
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000347 if ((msr_val & THERM_LOG_THRESHOLD1) &&
348 thresh_event_valid(CORE_LEVEL, 1))
R, Durgadoss9e76a972011-01-03 17:22:04 +0530349 platform_thermal_notify(msr_val);
350}
351
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900352/* Thermal transition interrupt handler */
Hidetoshi Seto8363fc82009-06-15 17:26:36 +0900353static void intel_thermal_interrupt(void)
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900354{
355 __u64 msr_val;
356
Srinivas Pandruvadaa2121162016-03-23 21:07:39 -0700357 if (static_cpu_has(X86_FEATURE_HWP))
358 wrmsrl_safe(MSR_HWP_STATUS, 0);
359
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900360 rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
Fenghua Yu01991142010-07-29 17:13:45 -0700361
R, Durgadoss9e76a972011-01-03 17:22:04 +0530362 /* Check for violation of core thermal thresholds*/
363 notify_thresholds(msr_val);
364
Borislav Petkov9b052ea2017-01-23 19:35:07 +0100365 therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
366 THERMAL_THROTTLING_EVENT,
367 CORE_LEVEL);
Fenghua Yu01991142010-07-29 17:13:45 -0700368
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400369 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
Fenghua Yu29e9bf12011-11-04 13:31:23 -0700370 therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
Fenghua Yu01991142010-07-29 17:13:45 -0700371 POWER_LIMIT_EVENT,
Fenghua Yu29e9bf12011-11-04 13:31:23 -0700372 CORE_LEVEL);
Fenghua Yu55d435a2010-07-29 17:13:44 -0700373
Christoph Lameterfe504212011-03-12 12:50:46 +0100374 if (this_cpu_has(X86_FEATURE_PTS)) {
Fenghua Yu55d435a2010-07-29 17:13:44 -0700375 rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
Srinivas Pandruvada25cdce12013-05-17 23:42:01 +0000376 /* check violations of package thermal thresholds */
377 notify_package_thresholds(msr_val);
Fenghua Yu29e9bf12011-11-04 13:31:23 -0700378 therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
Fenghua Yu01991142010-07-29 17:13:45 -0700379 THERMAL_THROTTLING_EVENT,
Fenghua Yu29e9bf12011-11-04 13:31:23 -0700380 PACKAGE_LEVEL);
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400381 if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
Fenghua Yu29e9bf12011-11-04 13:31:23 -0700382 therm_throt_process(msr_val &
Fenghua Yu01991142010-07-29 17:13:45 -0700383 PACKAGE_THERM_STATUS_POWER_LIMIT,
384 POWER_LIMIT_EVENT,
Fenghua Yu29e9bf12011-11-04 13:31:23 -0700385 PACKAGE_LEVEL);
Fenghua Yu55d435a2010-07-29 17:13:44 -0700386 }
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900387}
388
389static void unexpected_thermal_interrupt(void)
390{
Chen Yucong1b74dde2016-02-02 11:45:02 +0800391 pr_err("CPU%d: Unexpected LVT thermal interrupt!\n",
392 smp_processor_id());
Hidetoshi Setoa65c88d2009-06-15 17:25:27 +0900393}
394
395static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
396
Borislav Petkov68b5e4322018-11-09 23:13:13 +0100397asmlinkage __visible void __irq_entry smp_thermal_interrupt(struct pt_regs *regs)
Seiji Aguchicf910e82013-06-20 11:46:53 -0400398{
399 entering_irq();
400 trace_thermal_apic_entry(THERMAL_APIC_VECTOR);
Thomas Gleixner0f42ae22017-08-28 08:47:28 +0200401 inc_irq_stat(irq_thermal_count);
402 smp_thermal_vector();
Seiji Aguchicf910e82013-06-20 11:46:53 -0400403 trace_thermal_apic_exit(THERMAL_APIC_VECTOR);
404 exiting_ack_irq();
405}
406
Hidetoshi Seto70fe4402009-12-14 17:57:00 +0900407/* Thermal monitoring depends on APIC, ACPI and clock modulation */
408static int intel_thermal_supported(struct cpuinfo_x86 *c)
409{
Borislav Petkov93984fb2016-04-04 22:25:00 +0200410 if (!boot_cpu_has(X86_FEATURE_APIC))
Hidetoshi Seto70fe4402009-12-14 17:57:00 +0900411 return 0;
412 if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
413 return 0;
414 return 1;
415}
416
Yong Wangce6b5d72009-11-11 15:51:25 +0800417void __init mcheck_intel_therm_init(void)
Yong Wanga2202aa2009-11-10 09:38:24 +0800418{
419 /*
420 * This function is only called on boot CPU. Save the init thermal
421 * LVT value on BSP and use that value to restore APs' thermal LVT
422 * entry BIOS programmed later
423 */
Hidetoshi Seto70fe4402009-12-14 17:57:00 +0900424 if (intel_thermal_supported(&boot_cpu_data))
Yong Wanga2202aa2009-11-10 09:38:24 +0800425 lvtthmr_init = apic_read(APIC_LVTTHMR);
426}
427
Hidetoshi Setocffd3772009-11-12 15:52:40 +0900428void intel_init_thermal(struct cpuinfo_x86 *c)
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900429{
430 unsigned int cpu = smp_processor_id();
431 int tm2 = 0;
432 u32 l, h;
433
Hidetoshi Seto70fe4402009-12-14 17:57:00 +0900434 if (!intel_thermal_supported(c))
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900435 return;
436
437 /*
438 * First check if its enabled already, in which case there might
439 * be some SMM goo which handles it, so we can't even put a handler
440 * since it might be delivered via SMI already:
441 */
442 rdmsr(MSR_IA32_MISC_ENABLE, l, h);
Yong Wanga2202aa2009-11-10 09:38:24 +0800443
Youquan Songe503f9e2011-04-22 00:22:43 +0800444 h = lvtthmr_init;
Yong Wanga2202aa2009-11-10 09:38:24 +0800445 /*
446 * The initial value of thermal LVT entries on all APs always reads
447 * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
448 * sequence to them and LVT registers are reset to 0s except for
449 * the mask bits which are set to 1s when APs receive INIT IPI.
Youquan Songe503f9e2011-04-22 00:22:43 +0800450 * If BIOS takes over the thermal interrupt and sets its interrupt
451 * delivery mode to SMI (not fixed), it restores the value that the
452 * BIOS has programmed on AP based on BSP's info we saved since BIOS
453 * is always setting the same value for all threads/cores.
Yong Wanga2202aa2009-11-10 09:38:24 +0800454 */
Youquan Songe503f9e2011-04-22 00:22:43 +0800455 if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
456 apic_write(APIC_LVTTHMR, lvtthmr_init);
Yong Wanga2202aa2009-11-10 09:38:24 +0800457
Yong Wanga2202aa2009-11-10 09:38:24 +0800458
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900459 if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
Rakib Mullickd286c3a2014-09-19 01:22:15 +0600460 if (system_state == SYSTEM_BOOTING)
Chen Yucong1b74dde2016-02-02 11:45:02 +0800461 pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu);
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900462 return;
463 }
464
Bartlomiej Zolnierkiewiczf3a08672009-07-29 00:04:59 +0200465 /* early Pentium M models use different method for enabling TM2 */
466 if (cpu_has(c, X86_FEATURE_TM2)) {
467 if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
468 rdmsr(MSR_THERM2_CTL, l, h);
469 if (l & MSR_THERM2_CTL_TM_SELECT)
470 tm2 = 1;
471 } else if (l & MSR_IA32_MISC_ENABLE_TM2)
472 tm2 = 1;
473 }
474
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900475 /* We'll mask the thermal vector in the lapic till we're ready: */
476 h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
477 apic_write(APIC_LVTTHMR, h);
478
479 rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400480 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
Fenghua Yu01991142010-07-29 17:13:45 -0700481 wrmsr(MSR_IA32_THERM_INTERRUPT,
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400482 (l | (THERM_INT_LOW_ENABLE
483 | THERM_INT_HIGH_ENABLE)) & ~THERM_INT_PLN_ENABLE, h);
484 else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
485 wrmsr(MSR_IA32_THERM_INTERRUPT,
486 l | (THERM_INT_LOW_ENABLE
Fenghua Yu01991142010-07-29 17:13:45 -0700487 | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);
488 else
489 wrmsr(MSR_IA32_THERM_INTERRUPT,
490 l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900491
Fenghua Yu55d435a2010-07-29 17:13:44 -0700492 if (cpu_has(c, X86_FEATURE_PTS)) {
493 rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400494 if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
Fenghua Yu01991142010-07-29 17:13:45 -0700495 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
Fenghua Yu6bb2ff82013-05-21 15:35:17 -0400496 (l | (PACKAGE_THERM_INT_LOW_ENABLE
497 | PACKAGE_THERM_INT_HIGH_ENABLE))
498 & ~PACKAGE_THERM_INT_PLN_ENABLE, h);
499 else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
500 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
501 l | (PACKAGE_THERM_INT_LOW_ENABLE
Fenghua Yu01991142010-07-29 17:13:45 -0700502 | PACKAGE_THERM_INT_HIGH_ENABLE
503 | PACKAGE_THERM_INT_PLN_ENABLE), h);
504 else
505 wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
506 l | (PACKAGE_THERM_INT_LOW_ENABLE
507 | PACKAGE_THERM_INT_HIGH_ENABLE), h);
Fenghua Yu55d435a2010-07-29 17:13:44 -0700508 }
509
Hidetoshi Seto8363fc82009-06-15 17:26:36 +0900510 smp_thermal_vector = intel_thermal_interrupt;
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900511
512 rdmsr(MSR_IA32_MISC_ENABLE, l, h);
513 wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
514
515 /* Unmask the thermal vector: */
516 l = apic_read(APIC_LVTTHMR);
517 apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
518
Chen Yucong1b74dde2016-02-02 11:45:02 +0800519 pr_info_once("CPU0: Thermal monitoring enabled (%s)\n",
520 tm2 ? "TM2" : "TM1");
Hidetoshi Seto895287c2009-06-15 17:26:10 +0900521
522 /* enable thermal throttle processing */
523 atomic_set(&therm_throt_en, 1);
524}