Path: blob/master/arch/x86/kernel/cpu/mcheck/therm_throt.c
10775 views
/*1* Thermal throttle event support code (such as syslog messaging and rate2* limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).3*4* This allows consistent reporting of CPU thermal throttle events.5*6* Maintains a counter in /sys that keeps track of the number of thermal7* events, such that the user knows how bad the thermal problem might be8* (since the logging to syslog and mcelog is rate limited).9*10* Author: Dmitriy Zavin ([email protected])11*12* Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.13* Inspired by Ross Biro's and Al Borchers' counter code.14*/15#include <linux/interrupt.h>16#include <linux/notifier.h>17#include <linux/jiffies.h>18#include <linux/kernel.h>19#include <linux/percpu.h>20#include <linux/sysdev.h>21#include <linux/types.h>22#include <linux/init.h>23#include <linux/smp.h>24#include <linux/cpu.h>2526#include <asm/processor.h>27#include <asm/system.h>28#include <asm/apic.h>29#include <asm/idle.h>30#include <asm/mce.h>31#include <asm/msr.h>3233/* How long to wait between reporting thermal events */34#define CHECK_INTERVAL (300 * HZ)3536#define THERMAL_THROTTLING_EVENT 037#define POWER_LIMIT_EVENT 13839/*40* Current thermal event state:41*/42struct _thermal_state {43bool new_event;44int event;45u64 next_check;46unsigned long count;47unsigned long last_count;48};4950struct thermal_state {51struct _thermal_state core_throttle;52struct _thermal_state core_power_limit;53struct _thermal_state package_throttle;54struct _thermal_state package_power_limit;55struct _thermal_state core_thresh0;56struct _thermal_state core_thresh1;57};5859/* Callback to handle core threshold interrupts */60int (*platform_thermal_notify)(__u64 msr_val);61EXPORT_SYMBOL(platform_thermal_notify);6263static DEFINE_PER_CPU(struct thermal_state, thermal_state);6465static atomic_t therm_throt_en = ATOMIC_INIT(0);6667static u32 lvtthmr_init __read_mostly;6869#ifdef CONFIG_SYSFS70#define define_therm_throt_sysdev_one_ro(_name) \71static SYSDEV_ATTR(_name, 0444, \72therm_throt_sysdev_show_##_name, \73NULL) \7475#define define_therm_throt_sysdev_show_func(event, name) \76\77static ssize_t therm_throt_sysdev_show_##event##_##name( \78struct sys_device *dev, \79struct sysdev_attribute *attr, \80char *buf) \81{ \82unsigned int cpu = dev->id; \83ssize_t ret; \84\85preempt_disable(); /* CPU hotplug */ \86if (cpu_online(cpu)) { \87ret = sprintf(buf, "%lu\n", \88per_cpu(thermal_state, cpu).event.name); \89} else \90ret = 0; \91preempt_enable(); \92\93return ret; \94}9596define_therm_throt_sysdev_show_func(core_throttle, count);97define_therm_throt_sysdev_one_ro(core_throttle_count);9899define_therm_throt_sysdev_show_func(core_power_limit, count);100define_therm_throt_sysdev_one_ro(core_power_limit_count);101102define_therm_throt_sysdev_show_func(package_throttle, count);103define_therm_throt_sysdev_one_ro(package_throttle_count);104105define_therm_throt_sysdev_show_func(package_power_limit, count);106define_therm_throt_sysdev_one_ro(package_power_limit_count);107108static struct attribute *thermal_throttle_attrs[] = {109&attr_core_throttle_count.attr,110NULL111};112113static struct attribute_group thermal_attr_group = {114.attrs = thermal_throttle_attrs,115.name = "thermal_throttle"116};117#endif /* CONFIG_SYSFS */118119#define CORE_LEVEL 0120#define PACKAGE_LEVEL 1121122/***123* therm_throt_process - Process thermal throttling event from interrupt124* @curr: Whether the condition is current or not (boolean), since the125* thermal interrupt normally gets called both when the thermal126* event begins and once the event has ended.127*128* This function is called by the thermal interrupt after the129* IRQ has been acknowledged.130*131* It will take care of rate limiting and printing messages to the syslog.132*133* Returns: 0 : Event should NOT be further logged, i.e. still in134* "timeout" from previous log message.135* 1 : Event should be logged further, and a message has been136* printed to the syslog.137*/138static int therm_throt_process(bool new_event, int event, int level)139{140struct _thermal_state *state;141unsigned int this_cpu = smp_processor_id();142bool old_event;143u64 now;144struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);145146now = get_jiffies_64();147if (level == CORE_LEVEL) {148if (event == THERMAL_THROTTLING_EVENT)149state = &pstate->core_throttle;150else if (event == POWER_LIMIT_EVENT)151state = &pstate->core_power_limit;152else153return 0;154} else if (level == PACKAGE_LEVEL) {155if (event == THERMAL_THROTTLING_EVENT)156state = &pstate->package_throttle;157else if (event == POWER_LIMIT_EVENT)158state = &pstate->package_power_limit;159else160return 0;161} else162return 0;163164old_event = state->new_event;165state->new_event = new_event;166167if (new_event)168state->count++;169170if (time_before64(now, state->next_check) &&171state->count != state->last_count)172return 0;173174state->next_check = now + CHECK_INTERVAL;175state->last_count = state->count;176177/* if we just entered the thermal event */178if (new_event) {179if (event == THERMAL_THROTTLING_EVENT)180printk(KERN_CRIT "CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",181this_cpu,182level == CORE_LEVEL ? "Core" : "Package",183state->count);184else185printk(KERN_CRIT "CPU%d: %s power limit notification (total events = %lu)\n",186this_cpu,187level == CORE_LEVEL ? "Core" : "Package",188state->count);189return 1;190}191if (old_event) {192if (event == THERMAL_THROTTLING_EVENT)193printk(KERN_INFO "CPU%d: %s temperature/speed normal\n",194this_cpu,195level == CORE_LEVEL ? "Core" : "Package");196else197printk(KERN_INFO "CPU%d: %s power limit normal\n",198this_cpu,199level == CORE_LEVEL ? "Core" : "Package");200return 1;201}202203return 0;204}205206static int thresh_event_valid(int event)207{208struct _thermal_state *state;209unsigned int this_cpu = smp_processor_id();210struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);211u64 now = get_jiffies_64();212213state = (event == 0) ? &pstate->core_thresh0 : &pstate->core_thresh1;214215if (time_before64(now, state->next_check))216return 0;217218state->next_check = now + CHECK_INTERVAL;219return 1;220}221222#ifdef CONFIG_SYSFS223/* Add/Remove thermal_throttle interface for CPU device: */224static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev,225unsigned int cpu)226{227int err;228struct cpuinfo_x86 *c = &cpu_data(cpu);229230err = sysfs_create_group(&sys_dev->kobj, &thermal_attr_group);231if (err)232return err;233234if (cpu_has(c, X86_FEATURE_PLN))235err = sysfs_add_file_to_group(&sys_dev->kobj,236&attr_core_power_limit_count.attr,237thermal_attr_group.name);238if (cpu_has(c, X86_FEATURE_PTS)) {239err = sysfs_add_file_to_group(&sys_dev->kobj,240&attr_package_throttle_count.attr,241thermal_attr_group.name);242if (cpu_has(c, X86_FEATURE_PLN))243err = sysfs_add_file_to_group(&sys_dev->kobj,244&attr_package_power_limit_count.attr,245thermal_attr_group.name);246}247248return err;249}250251static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)252{253sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);254}255256/* Mutex protecting device creation against CPU hotplug: */257static DEFINE_MUTEX(therm_cpu_lock);258259/* Get notified when a cpu comes on/off. Be hotplug friendly. */260static __cpuinit int261thermal_throttle_cpu_callback(struct notifier_block *nfb,262unsigned long action,263void *hcpu)264{265unsigned int cpu = (unsigned long)hcpu;266struct sys_device *sys_dev;267int err = 0;268269sys_dev = get_cpu_sysdev(cpu);270271switch (action) {272case CPU_UP_PREPARE:273case CPU_UP_PREPARE_FROZEN:274mutex_lock(&therm_cpu_lock);275err = thermal_throttle_add_dev(sys_dev, cpu);276mutex_unlock(&therm_cpu_lock);277WARN_ON(err);278break;279case CPU_UP_CANCELED:280case CPU_UP_CANCELED_FROZEN:281case CPU_DEAD:282case CPU_DEAD_FROZEN:283mutex_lock(&therm_cpu_lock);284thermal_throttle_remove_dev(sys_dev);285mutex_unlock(&therm_cpu_lock);286break;287}288return notifier_from_errno(err);289}290291static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =292{293.notifier_call = thermal_throttle_cpu_callback,294};295296static __init int thermal_throttle_init_device(void)297{298unsigned int cpu = 0;299int err;300301if (!atomic_read(&therm_throt_en))302return 0;303304register_hotcpu_notifier(&thermal_throttle_cpu_notifier);305306#ifdef CONFIG_HOTPLUG_CPU307mutex_lock(&therm_cpu_lock);308#endif309/* connect live CPUs to sysfs */310for_each_online_cpu(cpu) {311err = thermal_throttle_add_dev(get_cpu_sysdev(cpu), cpu);312WARN_ON(err);313}314#ifdef CONFIG_HOTPLUG_CPU315mutex_unlock(&therm_cpu_lock);316#endif317318return 0;319}320device_initcall(thermal_throttle_init_device);321322#endif /* CONFIG_SYSFS */323324/*325* Set up the most two significant bit to notify mce log that this thermal326* event type.327* This is a temp solution. May be changed in the future with mce log328* infrasture.329*/330#define CORE_THROTTLED (0)331#define CORE_POWER_LIMIT ((__u64)1 << 62)332#define PACKAGE_THROTTLED ((__u64)2 << 62)333#define PACKAGE_POWER_LIMIT ((__u64)3 << 62)334335static void notify_thresholds(__u64 msr_val)336{337/* check whether the interrupt handler is defined;338* otherwise simply return339*/340if (!platform_thermal_notify)341return;342343/* lower threshold reached */344if ((msr_val & THERM_LOG_THRESHOLD0) && thresh_event_valid(0))345platform_thermal_notify(msr_val);346/* higher threshold reached */347if ((msr_val & THERM_LOG_THRESHOLD1) && thresh_event_valid(1))348platform_thermal_notify(msr_val);349}350351/* Thermal transition interrupt handler */352static void intel_thermal_interrupt(void)353{354__u64 msr_val;355356rdmsrl(MSR_IA32_THERM_STATUS, msr_val);357358/* Check for violation of core thermal thresholds*/359notify_thresholds(msr_val);360361if (therm_throt_process(msr_val & THERM_STATUS_PROCHOT,362THERMAL_THROTTLING_EVENT,363CORE_LEVEL) != 0)364mce_log_therm_throt_event(CORE_THROTTLED | msr_val);365366if (this_cpu_has(X86_FEATURE_PLN))367if (therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,368POWER_LIMIT_EVENT,369CORE_LEVEL) != 0)370mce_log_therm_throt_event(CORE_POWER_LIMIT | msr_val);371372if (this_cpu_has(X86_FEATURE_PTS)) {373rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);374if (therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,375THERMAL_THROTTLING_EVENT,376PACKAGE_LEVEL) != 0)377mce_log_therm_throt_event(PACKAGE_THROTTLED | msr_val);378if (this_cpu_has(X86_FEATURE_PLN))379if (therm_throt_process(msr_val &380PACKAGE_THERM_STATUS_POWER_LIMIT,381POWER_LIMIT_EVENT,382PACKAGE_LEVEL) != 0)383mce_log_therm_throt_event(PACKAGE_POWER_LIMIT384| msr_val);385}386}387388static void unexpected_thermal_interrupt(void)389{390printk(KERN_ERR "CPU%d: Unexpected LVT thermal interrupt!\n",391smp_processor_id());392}393394static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;395396asmlinkage void smp_thermal_interrupt(struct pt_regs *regs)397{398exit_idle();399irq_enter();400inc_irq_stat(irq_thermal_count);401smp_thermal_vector();402irq_exit();403/* Ack only at the end to avoid potential reentry */404ack_APIC_irq();405}406407/* Thermal monitoring depends on APIC, ACPI and clock modulation */408static int intel_thermal_supported(struct cpuinfo_x86 *c)409{410if (!cpu_has_apic)411return 0;412if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))413return 0;414return 1;415}416417void __init mcheck_intel_therm_init(void)418{419/*420* This function is only called on boot CPU. Save the init thermal421* LVT value on BSP and use that value to restore APs' thermal LVT422* entry BIOS programmed later423*/424if (intel_thermal_supported(&boot_cpu_data))425lvtthmr_init = apic_read(APIC_LVTTHMR);426}427428void intel_init_thermal(struct cpuinfo_x86 *c)429{430unsigned int cpu = smp_processor_id();431int tm2 = 0;432u32 l, h;433434if (!intel_thermal_supported(c))435return;436437/*438* First check if its enabled already, in which case there might439* be some SMM goo which handles it, so we can't even put a handler440* since it might be delivered via SMI already:441*/442rdmsr(MSR_IA32_MISC_ENABLE, l, h);443444h = lvtthmr_init;445/*446* The initial value of thermal LVT entries on all APs always reads447* 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI448* sequence to them and LVT registers are reset to 0s except for449* the mask bits which are set to 1s when APs receive INIT IPI.450* If BIOS takes over the thermal interrupt and sets its interrupt451* delivery mode to SMI (not fixed), it restores the value that the452* BIOS has programmed on AP based on BSP's info we saved since BIOS453* is always setting the same value for all threads/cores.454*/455if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)456apic_write(APIC_LVTTHMR, lvtthmr_init);457458459if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {460printk(KERN_DEBUG461"CPU%d: Thermal monitoring handled by SMI\n", cpu);462return;463}464465/* Check whether a vector already exists */466if (h & APIC_VECTOR_MASK) {467printk(KERN_DEBUG468"CPU%d: Thermal LVT vector (%#x) already installed\n",469cpu, (h & APIC_VECTOR_MASK));470return;471}472473/* early Pentium M models use different method for enabling TM2 */474if (cpu_has(c, X86_FEATURE_TM2)) {475if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {476rdmsr(MSR_THERM2_CTL, l, h);477if (l & MSR_THERM2_CTL_TM_SELECT)478tm2 = 1;479} else if (l & MSR_IA32_MISC_ENABLE_TM2)480tm2 = 1;481}482483/* We'll mask the thermal vector in the lapic till we're ready: */484h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;485apic_write(APIC_LVTTHMR, h);486487rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);488if (cpu_has(c, X86_FEATURE_PLN))489wrmsr(MSR_IA32_THERM_INTERRUPT,490l | (THERM_INT_LOW_ENABLE491| THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);492else493wrmsr(MSR_IA32_THERM_INTERRUPT,494l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);495496if (cpu_has(c, X86_FEATURE_PTS)) {497rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);498if (cpu_has(c, X86_FEATURE_PLN))499wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,500l | (PACKAGE_THERM_INT_LOW_ENABLE501| PACKAGE_THERM_INT_HIGH_ENABLE502| PACKAGE_THERM_INT_PLN_ENABLE), h);503else504wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,505l | (PACKAGE_THERM_INT_LOW_ENABLE506| PACKAGE_THERM_INT_HIGH_ENABLE), h);507}508509smp_thermal_vector = intel_thermal_interrupt;510511rdmsr(MSR_IA32_MISC_ENABLE, l, h);512wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);513514/* Unmask the thermal vector: */515l = apic_read(APIC_LVTTHMR);516apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);517518printk_once(KERN_INFO "CPU0: Thermal monitoring enabled (%s)\n",519tm2 ? "TM2" : "TM1");520521/* enable thermal throttle processing */522atomic_set(&therm_throt_en, 1);523}524525526