therm_throt.c 9.4 KB

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