therm_throt.c 8.4 KB

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