therm_throt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Thermal throttle event support code (such as syslog messaging and rate
  3. * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
  4. *
  5. * This allows consistent reporting of CPU thermal throttle events.
  6. *
  7. * Maintains a counter in /sys that keeps track of the number of thermal
  8. * events, such that the user knows how bad the thermal problem might be
  9. * (since the logging to syslog and mcelog is rate limited).
  10. *
  11. * Author: Dmitriy Zavin (dmitriyz@google.com)
  12. *
  13. * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
  14. * Inspired by Ross Biro's and Al Borchers' counter code.
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/notifier.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/kernel.h>
  20. #include <linux/percpu.h>
  21. #include <linux/sysdev.h>
  22. #include <linux/types.h>
  23. #include <linux/init.h>
  24. #include <linux/smp.h>
  25. #include <linux/cpu.h>
  26. #include <asm/therm_throt.h>
  27. #include <asm/processor.h>
  28. #include <asm/system.h>
  29. #include <asm/apic.h>
  30. #include <asm/idle.h>
  31. #include <asm/mce.h>
  32. #include <asm/msr.h>
  33. /* How long to wait between reporting thermal events */
  34. #define CHECK_INTERVAL (300 * HZ)
  35. static DEFINE_PER_CPU(__u64, next_check) = INITIAL_JIFFIES;
  36. static DEFINE_PER_CPU(unsigned long, thermal_throttle_count);
  37. atomic_t therm_throt_en = ATOMIC_INIT(0);
  38. #ifdef CONFIG_SYSFS
  39. #define define_therm_throt_sysdev_one_ro(_name) \
  40. static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
  41. #define define_therm_throt_sysdev_show_func(name) \
  42. static ssize_t therm_throt_sysdev_show_##name(struct sys_device *dev, \
  43. struct sysdev_attribute *attr, \
  44. char *buf) \
  45. { \
  46. unsigned int cpu = dev->id; \
  47. ssize_t ret; \
  48. \
  49. preempt_disable(); /* CPU hotplug */ \
  50. if (cpu_online(cpu)) \
  51. ret = sprintf(buf, "%lu\n", \
  52. per_cpu(thermal_throttle_##name, cpu)); \
  53. else \
  54. ret = 0; \
  55. preempt_enable(); \
  56. \
  57. return ret; \
  58. }
  59. define_therm_throt_sysdev_show_func(count);
  60. define_therm_throt_sysdev_one_ro(count);
  61. static struct attribute *thermal_throttle_attrs[] = {
  62. &attr_count.attr,
  63. NULL
  64. };
  65. static struct attribute_group thermal_throttle_attr_group = {
  66. .attrs = thermal_throttle_attrs,
  67. .name = "thermal_throttle"
  68. };
  69. #endif /* CONFIG_SYSFS */
  70. /***
  71. * therm_throt_process - Process thermal throttling event from interrupt
  72. * @curr: Whether the condition is current or not (boolean), since the
  73. * thermal interrupt normally gets called both when the thermal
  74. * event begins and once the event has ended.
  75. *
  76. * This function is called by the thermal interrupt after the
  77. * IRQ has been acknowledged.
  78. *
  79. * It will take care of rate limiting and printing messages to the syslog.
  80. *
  81. * Returns: 0 : Event should NOT be further logged, i.e. still in
  82. * "timeout" from previous log message.
  83. * 1 : Event should be logged further, and a message has been
  84. * printed to the syslog.
  85. */
  86. int therm_throt_process(int curr)
  87. {
  88. unsigned int cpu = smp_processor_id();
  89. __u64 tmp_jiffs = get_jiffies_64();
  90. if (curr)
  91. __get_cpu_var(thermal_throttle_count)++;
  92. if (time_before64(tmp_jiffs, __get_cpu_var(next_check)))
  93. return 0;
  94. __get_cpu_var(next_check) = tmp_jiffs + CHECK_INTERVAL;
  95. /* if we just entered the thermal event */
  96. if (curr) {
  97. printk(KERN_CRIT "CPU%d: Temperature above threshold, "
  98. "cpu clock throttled (total events = %lu)\n", cpu,
  99. __get_cpu_var(thermal_throttle_count));
  100. add_taint(TAINT_MACHINE_CHECK);
  101. } else {
  102. printk(KERN_CRIT "CPU%d: Temperature/speed normal\n", cpu);
  103. }
  104. return 1;
  105. }
  106. #ifdef CONFIG_SYSFS
  107. /* Add/Remove thermal_throttle interface for CPU device: */
  108. static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
  109. {
  110. return sysfs_create_group(&sys_dev->kobj,
  111. &thermal_throttle_attr_group);
  112. }
  113. static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
  114. {
  115. sysfs_remove_group(&sys_dev->kobj, &thermal_throttle_attr_group);
  116. }
  117. /* Mutex protecting device creation against CPU hotplug: */
  118. static DEFINE_MUTEX(therm_cpu_lock);
  119. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  120. static __cpuinit int
  121. thermal_throttle_cpu_callback(struct notifier_block *nfb,
  122. unsigned long action,
  123. void *hcpu)
  124. {
  125. unsigned int cpu = (unsigned long)hcpu;
  126. struct sys_device *sys_dev;
  127. int err = 0;
  128. sys_dev = get_cpu_sysdev(cpu);
  129. switch (action) {
  130. case CPU_UP_PREPARE:
  131. case CPU_UP_PREPARE_FROZEN:
  132. mutex_lock(&therm_cpu_lock);
  133. err = thermal_throttle_add_dev(sys_dev);
  134. mutex_unlock(&therm_cpu_lock);
  135. WARN_ON(err);
  136. break;
  137. case CPU_UP_CANCELED:
  138. case CPU_UP_CANCELED_FROZEN:
  139. case CPU_DEAD:
  140. case CPU_DEAD_FROZEN:
  141. mutex_lock(&therm_cpu_lock);
  142. thermal_throttle_remove_dev(sys_dev);
  143. mutex_unlock(&therm_cpu_lock);
  144. break;
  145. }
  146. return err ? NOTIFY_BAD : NOTIFY_OK;
  147. }
  148. static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
  149. {
  150. .notifier_call = thermal_throttle_cpu_callback,
  151. };
  152. static __init int thermal_throttle_init_device(void)
  153. {
  154. unsigned int cpu = 0;
  155. int err;
  156. if (!atomic_read(&therm_throt_en))
  157. return 0;
  158. register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  159. #ifdef CONFIG_HOTPLUG_CPU
  160. mutex_lock(&therm_cpu_lock);
  161. #endif
  162. /* connect live CPUs to sysfs */
  163. for_each_online_cpu(cpu) {
  164. err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
  165. WARN_ON(err);
  166. }
  167. #ifdef CONFIG_HOTPLUG_CPU
  168. mutex_unlock(&therm_cpu_lock);
  169. #endif
  170. return 0;
  171. }
  172. device_initcall(thermal_throttle_init_device);
  173. #endif /* CONFIG_SYSFS */
  174. /* Thermal transition interrupt handler */
  175. static void intel_thermal_interrupt(void)
  176. {
  177. __u64 msr_val;
  178. rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
  179. if (therm_throt_process(msr_val & THERM_STATUS_PROCHOT))
  180. mce_log_therm_throt_event(msr_val);
  181. }
  182. static void unexpected_thermal_interrupt(void)
  183. {
  184. printk(KERN_ERR "CPU%d: Unexpected LVT TMR interrupt!\n",
  185. smp_processor_id());
  186. add_taint(TAINT_MACHINE_CHECK);
  187. }
  188. static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
  189. asmlinkage void smp_thermal_interrupt(struct pt_regs *regs)
  190. {
  191. exit_idle();
  192. irq_enter();
  193. inc_irq_stat(irq_thermal_count);
  194. smp_thermal_vector();
  195. irq_exit();
  196. /* Ack only at the end to avoid potential reentry */
  197. ack_APIC_irq();
  198. }
  199. void intel_init_thermal(struct cpuinfo_x86 *c)
  200. {
  201. unsigned int cpu = smp_processor_id();
  202. int tm2 = 0;
  203. u32 l, h;
  204. /* Thermal monitoring depends on ACPI and clock modulation*/
  205. if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
  206. return;
  207. /*
  208. * First check if its enabled already, in which case there might
  209. * be some SMM goo which handles it, so we can't even put a handler
  210. * since it might be delivered via SMI already:
  211. */
  212. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  213. h = apic_read(APIC_LVTTHMR);
  214. if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
  215. printk(KERN_DEBUG
  216. "CPU%d: Thermal monitoring handled by SMI\n", cpu);
  217. return;
  218. }
  219. if (cpu_has(c, X86_FEATURE_TM2) && (l & MSR_IA32_MISC_ENABLE_TM2))
  220. tm2 = 1;
  221. /* Check whether a vector already exists */
  222. if (h & APIC_VECTOR_MASK) {
  223. printk(KERN_DEBUG
  224. "CPU%d: Thermal LVT vector (%#x) already installed\n",
  225. cpu, (h & APIC_VECTOR_MASK));
  226. return;
  227. }
  228. /* We'll mask the thermal vector in the lapic till we're ready: */
  229. h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
  230. apic_write(APIC_LVTTHMR, h);
  231. rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
  232. wrmsr(MSR_IA32_THERM_INTERRUPT,
  233. l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
  234. smp_thermal_vector = intel_thermal_interrupt;
  235. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  236. wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
  237. /* Unmask the thermal vector: */
  238. l = apic_read(APIC_LVTTHMR);
  239. apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
  240. printk(KERN_INFO "CPU%d: Thermal monitoring enabled (%s)\n",
  241. cpu, tm2 ? "TM2" : "TM1");
  242. /* enable thermal throttle processing */
  243. atomic_set(&therm_throt_en, 1);
  244. }