therm_throt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/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>
  32. /* How long to wait between reporting thermal events */
  33. #define CHECK_INTERVAL (300 * HZ)
  34. /*
  35. * Current thermal throttling state:
  36. */
  37. struct thermal_state {
  38. bool is_throttled;
  39. u64 next_check;
  40. unsigned long throttle_count;
  41. };
  42. static DEFINE_PER_CPU(struct thermal_state, thermal_state);
  43. static atomic_t therm_throt_en = ATOMIC_INIT(0);
  44. #ifdef CONFIG_SYSFS
  45. #define define_therm_throt_sysdev_one_ro(_name) \
  46. static SYSDEV_ATTR(_name, 0444, therm_throt_sysdev_show_##_name, NULL)
  47. #define define_therm_throt_sysdev_show_func(name) \
  48. \
  49. static ssize_t therm_throt_sysdev_show_##name( \
  50. struct sys_device *dev, \
  51. struct sysdev_attribute *attr, \
  52. char *buf) \
  53. { \
  54. unsigned int cpu = dev->id; \
  55. ssize_t ret; \
  56. \
  57. preempt_disable(); /* CPU hotplug */ \
  58. if (cpu_online(cpu)) \
  59. ret = sprintf(buf, "%lu\n", \
  60. per_cpu(thermal_state, cpu).name); \
  61. else \
  62. ret = 0; \
  63. preempt_enable(); \
  64. \
  65. return ret; \
  66. }
  67. define_therm_throt_sysdev_show_func(throttle_count);
  68. define_therm_throt_sysdev_one_ro(throttle_count);
  69. static struct attribute *thermal_throttle_attrs[] = {
  70. &attr_throttle_count.attr,
  71. NULL
  72. };
  73. static struct attribute_group thermal_throttle_attr_group = {
  74. .attrs = thermal_throttle_attrs,
  75. .name = "thermal_throttle"
  76. };
  77. #endif /* CONFIG_SYSFS */
  78. /***
  79. * therm_throt_process - Process thermal throttling event from interrupt
  80. * @curr: Whether the condition is current or not (boolean), since the
  81. * thermal interrupt normally gets called both when the thermal
  82. * event begins and once the event has ended.
  83. *
  84. * This function is called by the thermal interrupt after the
  85. * IRQ has been acknowledged.
  86. *
  87. * It will take care of rate limiting and printing messages to the syslog.
  88. *
  89. * Returns: 0 : Event should NOT be further logged, i.e. still in
  90. * "timeout" from previous log message.
  91. * 1 : Event should be logged further, and a message has been
  92. * printed to the syslog.
  93. */
  94. static int therm_throt_process(bool is_throttled)
  95. {
  96. struct thermal_state *state;
  97. unsigned int this_cpu;
  98. bool was_throttled;
  99. u64 now;
  100. this_cpu = smp_processor_id();
  101. now = get_jiffies_64();
  102. state = &per_cpu(thermal_state, this_cpu);
  103. was_throttled = state->is_throttled;
  104. state->is_throttled = is_throttled;
  105. if (is_throttled)
  106. state->throttle_count++;
  107. if (!(was_throttled ^ is_throttled) &&
  108. time_before64(now, state->next_check))
  109. return 0;
  110. state->next_check = now + CHECK_INTERVAL;
  111. /* if we just entered the thermal event */
  112. if (is_throttled) {
  113. printk(KERN_CRIT "CPU%d: Temperature above threshold, cpu clock throttled (total events = %lu)\n", this_cpu, state->throttle_count);
  114. add_taint(TAINT_MACHINE_CHECK);
  115. return 1;
  116. }
  117. if (was_throttled) {
  118. printk(KERN_INFO "CPU%d: Temperature/speed normal\n", this_cpu);
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. #ifdef CONFIG_SYSFS
  124. /* Add/Remove thermal_throttle interface for CPU device: */
  125. static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev)
  126. {
  127. return sysfs_create_group(&sys_dev->kobj,
  128. &thermal_throttle_attr_group);
  129. }
  130. static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
  131. {
  132. sysfs_remove_group(&sys_dev->kobj, &thermal_throttle_attr_group);
  133. }
  134. /* Mutex protecting device creation against CPU hotplug: */
  135. static DEFINE_MUTEX(therm_cpu_lock);
  136. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  137. static __cpuinit int
  138. thermal_throttle_cpu_callback(struct notifier_block *nfb,
  139. unsigned long action,
  140. void *hcpu)
  141. {
  142. unsigned int cpu = (unsigned long)hcpu;
  143. struct sys_device *sys_dev;
  144. int err = 0;
  145. sys_dev = get_cpu_sysdev(cpu);
  146. switch (action) {
  147. case CPU_UP_PREPARE:
  148. case CPU_UP_PREPARE_FROZEN:
  149. mutex_lock(&therm_cpu_lock);
  150. err = thermal_throttle_add_dev(sys_dev);
  151. mutex_unlock(&therm_cpu_lock);
  152. WARN_ON(err);
  153. break;
  154. case CPU_UP_CANCELED:
  155. case CPU_UP_CANCELED_FROZEN:
  156. case CPU_DEAD:
  157. case CPU_DEAD_FROZEN:
  158. mutex_lock(&therm_cpu_lock);
  159. thermal_throttle_remove_dev(sys_dev);
  160. mutex_unlock(&therm_cpu_lock);
  161. break;
  162. }
  163. return err ? NOTIFY_BAD : NOTIFY_OK;
  164. }
  165. static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
  166. {
  167. .notifier_call = thermal_throttle_cpu_callback,
  168. };
  169. static __init int thermal_throttle_init_device(void)
  170. {
  171. unsigned int cpu = 0;
  172. int err;
  173. if (!atomic_read(&therm_throt_en))
  174. return 0;
  175. register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  176. #ifdef CONFIG_HOTPLUG_CPU
  177. mutex_lock(&therm_cpu_lock);
  178. #endif
  179. /* connect live CPUs to sysfs */
  180. for_each_online_cpu(cpu) {
  181. err = thermal_throttle_add_dev(get_cpu_sysdev(cpu));
  182. WARN_ON(err);
  183. }
  184. #ifdef CONFIG_HOTPLUG_CPU
  185. mutex_unlock(&therm_cpu_lock);
  186. #endif
  187. return 0;
  188. }
  189. device_initcall(thermal_throttle_init_device);
  190. #endif /* CONFIG_SYSFS */
  191. /* Thermal transition interrupt handler */
  192. static void intel_thermal_interrupt(void)
  193. {
  194. __u64 msr_val;
  195. rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
  196. if (therm_throt_process((msr_val & THERM_STATUS_PROCHOT) != 0))
  197. mce_log_therm_throt_event(msr_val);
  198. }
  199. static void unexpected_thermal_interrupt(void)
  200. {
  201. printk(KERN_ERR "CPU%d: Unexpected LVT TMR interrupt!\n",
  202. smp_processor_id());
  203. add_taint(TAINT_MACHINE_CHECK);
  204. }
  205. static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
  206. asmlinkage void smp_thermal_interrupt(struct pt_regs *regs)
  207. {
  208. exit_idle();
  209. irq_enter();
  210. inc_irq_stat(irq_thermal_count);
  211. smp_thermal_vector();
  212. irq_exit();
  213. /* Ack only at the end to avoid potential reentry */
  214. ack_APIC_irq();
  215. }
  216. void intel_init_thermal(struct cpuinfo_x86 *c)
  217. {
  218. unsigned int cpu = smp_processor_id();
  219. int tm2 = 0;
  220. u32 l, h;
  221. /* Thermal monitoring depends on ACPI and clock modulation*/
  222. if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
  223. return;
  224. /*
  225. * First check if its enabled already, in which case there might
  226. * be some SMM goo which handles it, so we can't even put a handler
  227. * since it might be delivered via SMI already:
  228. */
  229. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  230. h = apic_read(APIC_LVTTHMR);
  231. if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
  232. printk(KERN_DEBUG
  233. "CPU%d: Thermal monitoring handled by SMI\n", cpu);
  234. return;
  235. }
  236. /* Check whether a vector already exists */
  237. if (h & APIC_VECTOR_MASK) {
  238. printk(KERN_DEBUG
  239. "CPU%d: Thermal LVT vector (%#x) already installed\n",
  240. cpu, (h & APIC_VECTOR_MASK));
  241. return;
  242. }
  243. /* early Pentium M models use different method for enabling TM2 */
  244. if (cpu_has(c, X86_FEATURE_TM2)) {
  245. if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
  246. rdmsr(MSR_THERM2_CTL, l, h);
  247. if (l & MSR_THERM2_CTL_TM_SELECT)
  248. tm2 = 1;
  249. } else if (l & MSR_IA32_MISC_ENABLE_TM2)
  250. tm2 = 1;
  251. }
  252. /* We'll mask the thermal vector in the lapic till we're ready: */
  253. h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
  254. apic_write(APIC_LVTTHMR, h);
  255. rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
  256. wrmsr(MSR_IA32_THERM_INTERRUPT,
  257. l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
  258. smp_thermal_vector = intel_thermal_interrupt;
  259. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  260. wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
  261. /* Unmask the thermal vector: */
  262. l = apic_read(APIC_LVTTHMR);
  263. apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
  264. printk(KERN_INFO "CPU%d: Thermal monitoring enabled (%s)\n",
  265. cpu, tm2 ? "TM2" : "TM1");
  266. /* enable thermal throttle processing */
  267. atomic_set(&therm_throt_en, 1);
  268. }