therm_throt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. #define THERMAL_THROTTLING_EVENT 0
  35. #define POWER_LIMIT_EVENT 1
  36. /*
  37. * Current thermal event state:
  38. */
  39. struct _thermal_state {
  40. bool new_event;
  41. int event;
  42. u64 next_check;
  43. unsigned long count;
  44. unsigned long last_count;
  45. };
  46. struct thermal_state {
  47. struct _thermal_state core_throttle;
  48. struct _thermal_state core_power_limit;
  49. struct _thermal_state package_throttle;
  50. struct _thermal_state package_power_limit;
  51. struct _thermal_state core_thresh0;
  52. struct _thermal_state core_thresh1;
  53. };
  54. /* Callback to handle core threshold interrupts */
  55. int (*platform_thermal_notify)(__u64 msr_val);
  56. EXPORT_SYMBOL(platform_thermal_notify);
  57. static DEFINE_PER_CPU(struct thermal_state, thermal_state);
  58. static atomic_t therm_throt_en = ATOMIC_INIT(0);
  59. static u32 lvtthmr_init __read_mostly;
  60. #ifdef CONFIG_SYSFS
  61. #define define_therm_throt_sysdev_one_ro(_name) \
  62. static SYSDEV_ATTR(_name, 0444, \
  63. therm_throt_sysdev_show_##_name, \
  64. NULL) \
  65. #define define_therm_throt_sysdev_show_func(event, name) \
  66. \
  67. static ssize_t therm_throt_sysdev_show_##event##_##name( \
  68. struct sys_device *dev, \
  69. struct sysdev_attribute *attr, \
  70. char *buf) \
  71. { \
  72. unsigned int cpu = dev->id; \
  73. ssize_t ret; \
  74. \
  75. preempt_disable(); /* CPU hotplug */ \
  76. if (cpu_online(cpu)) { \
  77. ret = sprintf(buf, "%lu\n", \
  78. per_cpu(thermal_state, cpu).event.name); \
  79. } else \
  80. ret = 0; \
  81. preempt_enable(); \
  82. \
  83. return ret; \
  84. }
  85. define_therm_throt_sysdev_show_func(core_throttle, count);
  86. define_therm_throt_sysdev_one_ro(core_throttle_count);
  87. define_therm_throt_sysdev_show_func(core_power_limit, count);
  88. define_therm_throt_sysdev_one_ro(core_power_limit_count);
  89. define_therm_throt_sysdev_show_func(package_throttle, count);
  90. define_therm_throt_sysdev_one_ro(package_throttle_count);
  91. define_therm_throt_sysdev_show_func(package_power_limit, count);
  92. define_therm_throt_sysdev_one_ro(package_power_limit_count);
  93. static struct attribute *thermal_throttle_attrs[] = {
  94. &attr_core_throttle_count.attr,
  95. NULL
  96. };
  97. static struct attribute_group thermal_attr_group = {
  98. .attrs = thermal_throttle_attrs,
  99. .name = "thermal_throttle"
  100. };
  101. #endif /* CONFIG_SYSFS */
  102. #define CORE_LEVEL 0
  103. #define PACKAGE_LEVEL 1
  104. /***
  105. * therm_throt_process - Process thermal throttling event from interrupt
  106. * @curr: Whether the condition is current or not (boolean), since the
  107. * thermal interrupt normally gets called both when the thermal
  108. * event begins and once the event has ended.
  109. *
  110. * This function is called by the thermal interrupt after the
  111. * IRQ has been acknowledged.
  112. *
  113. * It will take care of rate limiting and printing messages to the syslog.
  114. *
  115. * Returns: 0 : Event should NOT be further logged, i.e. still in
  116. * "timeout" from previous log message.
  117. * 1 : Event should be logged further, and a message has been
  118. * printed to the syslog.
  119. */
  120. static int therm_throt_process(bool new_event, int event, int level)
  121. {
  122. struct _thermal_state *state;
  123. unsigned int this_cpu = smp_processor_id();
  124. bool old_event;
  125. u64 now;
  126. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  127. now = get_jiffies_64();
  128. if (level == CORE_LEVEL) {
  129. if (event == THERMAL_THROTTLING_EVENT)
  130. state = &pstate->core_throttle;
  131. else if (event == POWER_LIMIT_EVENT)
  132. state = &pstate->core_power_limit;
  133. else
  134. return 0;
  135. } else if (level == PACKAGE_LEVEL) {
  136. if (event == THERMAL_THROTTLING_EVENT)
  137. state = &pstate->package_throttle;
  138. else if (event == POWER_LIMIT_EVENT)
  139. state = &pstate->package_power_limit;
  140. else
  141. return 0;
  142. } else
  143. return 0;
  144. old_event = state->new_event;
  145. state->new_event = new_event;
  146. if (new_event)
  147. state->count++;
  148. if (time_before64(now, state->next_check) &&
  149. state->count != state->last_count)
  150. return 0;
  151. state->next_check = now + CHECK_INTERVAL;
  152. state->last_count = state->count;
  153. /* if we just entered the thermal event */
  154. if (new_event) {
  155. if (event == THERMAL_THROTTLING_EVENT)
  156. printk(KERN_CRIT "CPU%d: %s temperature above threshold, cpu clock throttled (total events = %lu)\n",
  157. this_cpu,
  158. level == CORE_LEVEL ? "Core" : "Package",
  159. state->count);
  160. else
  161. printk(KERN_CRIT "CPU%d: %s power limit notification (total events = %lu)\n",
  162. this_cpu,
  163. level == CORE_LEVEL ? "Core" : "Package",
  164. state->count);
  165. return 1;
  166. }
  167. if (old_event) {
  168. if (event == THERMAL_THROTTLING_EVENT)
  169. printk(KERN_INFO "CPU%d: %s temperature/speed normal\n",
  170. this_cpu,
  171. level == CORE_LEVEL ? "Core" : "Package");
  172. else
  173. printk(KERN_INFO "CPU%d: %s power limit normal\n",
  174. this_cpu,
  175. level == CORE_LEVEL ? "Core" : "Package");
  176. return 1;
  177. }
  178. return 0;
  179. }
  180. static int thresh_event_valid(int event)
  181. {
  182. struct _thermal_state *state;
  183. unsigned int this_cpu = smp_processor_id();
  184. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  185. u64 now = get_jiffies_64();
  186. state = (event == 0) ? &pstate->core_thresh0 : &pstate->core_thresh1;
  187. if (time_before64(now, state->next_check))
  188. return 0;
  189. state->next_check = now + CHECK_INTERVAL;
  190. return 1;
  191. }
  192. #ifdef CONFIG_SYSFS
  193. /* Add/Remove thermal_throttle interface for CPU device: */
  194. static __cpuinit int thermal_throttle_add_dev(struct sys_device *sys_dev,
  195. unsigned int cpu)
  196. {
  197. int err;
  198. struct cpuinfo_x86 *c = &cpu_data(cpu);
  199. err = sysfs_create_group(&sys_dev->kobj, &thermal_attr_group);
  200. if (err)
  201. return err;
  202. if (cpu_has(c, X86_FEATURE_PLN))
  203. err = sysfs_add_file_to_group(&sys_dev->kobj,
  204. &attr_core_power_limit_count.attr,
  205. thermal_attr_group.name);
  206. if (cpu_has(c, X86_FEATURE_PTS)) {
  207. err = sysfs_add_file_to_group(&sys_dev->kobj,
  208. &attr_package_throttle_count.attr,
  209. thermal_attr_group.name);
  210. if (cpu_has(c, X86_FEATURE_PLN))
  211. err = sysfs_add_file_to_group(&sys_dev->kobj,
  212. &attr_package_power_limit_count.attr,
  213. thermal_attr_group.name);
  214. }
  215. return err;
  216. }
  217. static __cpuinit void thermal_throttle_remove_dev(struct sys_device *sys_dev)
  218. {
  219. sysfs_remove_group(&sys_dev->kobj, &thermal_attr_group);
  220. }
  221. /* Mutex protecting device creation against CPU hotplug: */
  222. static DEFINE_MUTEX(therm_cpu_lock);
  223. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  224. static __cpuinit int
  225. thermal_throttle_cpu_callback(struct notifier_block *nfb,
  226. unsigned long action,
  227. void *hcpu)
  228. {
  229. unsigned int cpu = (unsigned long)hcpu;
  230. struct sys_device *sys_dev;
  231. int err = 0;
  232. sys_dev = get_cpu_sysdev(cpu);
  233. switch (action) {
  234. case CPU_UP_PREPARE:
  235. case CPU_UP_PREPARE_FROZEN:
  236. mutex_lock(&therm_cpu_lock);
  237. err = thermal_throttle_add_dev(sys_dev, cpu);
  238. mutex_unlock(&therm_cpu_lock);
  239. WARN_ON(err);
  240. break;
  241. case CPU_UP_CANCELED:
  242. case CPU_UP_CANCELED_FROZEN:
  243. case CPU_DEAD:
  244. case CPU_DEAD_FROZEN:
  245. mutex_lock(&therm_cpu_lock);
  246. thermal_throttle_remove_dev(sys_dev);
  247. mutex_unlock(&therm_cpu_lock);
  248. break;
  249. }
  250. return notifier_from_errno(err);
  251. }
  252. static struct notifier_block thermal_throttle_cpu_notifier __cpuinitdata =
  253. {
  254. .notifier_call = thermal_throttle_cpu_callback,
  255. };
  256. static __init int thermal_throttle_init_device(void)
  257. {
  258. unsigned int cpu = 0;
  259. int err;
  260. if (!atomic_read(&therm_throt_en))
  261. return 0;
  262. register_hotcpu_notifier(&thermal_throttle_cpu_notifier);
  263. #ifdef CONFIG_HOTPLUG_CPU
  264. mutex_lock(&therm_cpu_lock);
  265. #endif
  266. /* connect live CPUs to sysfs */
  267. for_each_online_cpu(cpu) {
  268. err = thermal_throttle_add_dev(get_cpu_sysdev(cpu), cpu);
  269. WARN_ON(err);
  270. }
  271. #ifdef CONFIG_HOTPLUG_CPU
  272. mutex_unlock(&therm_cpu_lock);
  273. #endif
  274. return 0;
  275. }
  276. device_initcall(thermal_throttle_init_device);
  277. #endif /* CONFIG_SYSFS */
  278. /*
  279. * Set up the most two significant bit to notify mce log that this thermal
  280. * event type.
  281. * This is a temp solution. May be changed in the future with mce log
  282. * infrasture.
  283. */
  284. #define CORE_THROTTLED (0)
  285. #define CORE_POWER_LIMIT ((__u64)1 << 62)
  286. #define PACKAGE_THROTTLED ((__u64)2 << 62)
  287. #define PACKAGE_POWER_LIMIT ((__u64)3 << 62)
  288. static void notify_thresholds(__u64 msr_val)
  289. {
  290. /* check whether the interrupt handler is defined;
  291. * otherwise simply return
  292. */
  293. if (!platform_thermal_notify)
  294. return;
  295. /* lower threshold reached */
  296. if ((msr_val & THERM_LOG_THRESHOLD0) && thresh_event_valid(0))
  297. platform_thermal_notify(msr_val);
  298. /* higher threshold reached */
  299. if ((msr_val & THERM_LOG_THRESHOLD1) && thresh_event_valid(1))
  300. platform_thermal_notify(msr_val);
  301. }
  302. /* Thermal transition interrupt handler */
  303. static void intel_thermal_interrupt(void)
  304. {
  305. __u64 msr_val;
  306. rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
  307. /* Check for violation of core thermal thresholds*/
  308. notify_thresholds(msr_val);
  309. if (therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
  310. THERMAL_THROTTLING_EVENT,
  311. CORE_LEVEL) != 0)
  312. mce_log_therm_throt_event(CORE_THROTTLED | msr_val);
  313. if (this_cpu_has(X86_FEATURE_PLN))
  314. if (therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
  315. POWER_LIMIT_EVENT,
  316. CORE_LEVEL) != 0)
  317. mce_log_therm_throt_event(CORE_POWER_LIMIT | msr_val);
  318. if (this_cpu_has(X86_FEATURE_PTS)) {
  319. rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
  320. if (therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
  321. THERMAL_THROTTLING_EVENT,
  322. PACKAGE_LEVEL) != 0)
  323. mce_log_therm_throt_event(PACKAGE_THROTTLED | msr_val);
  324. if (this_cpu_has(X86_FEATURE_PLN))
  325. if (therm_throt_process(msr_val &
  326. PACKAGE_THERM_STATUS_POWER_LIMIT,
  327. POWER_LIMIT_EVENT,
  328. PACKAGE_LEVEL) != 0)
  329. mce_log_therm_throt_event(PACKAGE_POWER_LIMIT
  330. | msr_val);
  331. }
  332. }
  333. static void unexpected_thermal_interrupt(void)
  334. {
  335. printk(KERN_ERR "CPU%d: Unexpected LVT thermal interrupt!\n",
  336. smp_processor_id());
  337. }
  338. static void (*smp_thermal_vector)(void) = unexpected_thermal_interrupt;
  339. asmlinkage void smp_thermal_interrupt(struct pt_regs *regs)
  340. {
  341. exit_idle();
  342. irq_enter();
  343. inc_irq_stat(irq_thermal_count);
  344. smp_thermal_vector();
  345. irq_exit();
  346. /* Ack only at the end to avoid potential reentry */
  347. ack_APIC_irq();
  348. }
  349. /* Thermal monitoring depends on APIC, ACPI and clock modulation */
  350. static int intel_thermal_supported(struct cpuinfo_x86 *c)
  351. {
  352. if (!cpu_has_apic)
  353. return 0;
  354. if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
  355. return 0;
  356. return 1;
  357. }
  358. void __init mcheck_intel_therm_init(void)
  359. {
  360. /*
  361. * This function is only called on boot CPU. Save the init thermal
  362. * LVT value on BSP and use that value to restore APs' thermal LVT
  363. * entry BIOS programmed later
  364. */
  365. if (intel_thermal_supported(&boot_cpu_data))
  366. lvtthmr_init = apic_read(APIC_LVTTHMR);
  367. }
  368. void intel_init_thermal(struct cpuinfo_x86 *c)
  369. {
  370. unsigned int cpu = smp_processor_id();
  371. int tm2 = 0;
  372. u32 l, h;
  373. if (!intel_thermal_supported(c))
  374. return;
  375. /*
  376. * First check if its enabled already, in which case there might
  377. * be some SMM goo which handles it, so we can't even put a handler
  378. * since it might be delivered via SMI already:
  379. */
  380. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  381. h = lvtthmr_init;
  382. /*
  383. * The initial value of thermal LVT entries on all APs always reads
  384. * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
  385. * sequence to them and LVT registers are reset to 0s except for
  386. * the mask bits which are set to 1s when APs receive INIT IPI.
  387. * If BIOS takes over the thermal interrupt and sets its interrupt
  388. * delivery mode to SMI (not fixed), it restores the value that the
  389. * BIOS has programmed on AP based on BSP's info we saved since BIOS
  390. * is always setting the same value for all threads/cores.
  391. */
  392. if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
  393. apic_write(APIC_LVTTHMR, lvtthmr_init);
  394. if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
  395. printk(KERN_DEBUG
  396. "CPU%d: Thermal monitoring handled by SMI\n", cpu);
  397. return;
  398. }
  399. /* Check whether a vector already exists */
  400. if (h & APIC_VECTOR_MASK) {
  401. printk(KERN_DEBUG
  402. "CPU%d: Thermal LVT vector (%#x) already installed\n",
  403. cpu, (h & APIC_VECTOR_MASK));
  404. return;
  405. }
  406. /* early Pentium M models use different method for enabling TM2 */
  407. if (cpu_has(c, X86_FEATURE_TM2)) {
  408. if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
  409. rdmsr(MSR_THERM2_CTL, l, h);
  410. if (l & MSR_THERM2_CTL_TM_SELECT)
  411. tm2 = 1;
  412. } else if (l & MSR_IA32_MISC_ENABLE_TM2)
  413. tm2 = 1;
  414. }
  415. /* We'll mask the thermal vector in the lapic till we're ready: */
  416. h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
  417. apic_write(APIC_LVTTHMR, h);
  418. rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
  419. if (cpu_has(c, X86_FEATURE_PLN))
  420. wrmsr(MSR_IA32_THERM_INTERRUPT,
  421. l | (THERM_INT_LOW_ENABLE
  422. | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);
  423. else
  424. wrmsr(MSR_IA32_THERM_INTERRUPT,
  425. l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
  426. if (cpu_has(c, X86_FEATURE_PTS)) {
  427. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  428. if (cpu_has(c, X86_FEATURE_PLN))
  429. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  430. l | (PACKAGE_THERM_INT_LOW_ENABLE
  431. | PACKAGE_THERM_INT_HIGH_ENABLE
  432. | PACKAGE_THERM_INT_PLN_ENABLE), h);
  433. else
  434. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  435. l | (PACKAGE_THERM_INT_LOW_ENABLE
  436. | PACKAGE_THERM_INT_HIGH_ENABLE), h);
  437. }
  438. smp_thermal_vector = intel_thermal_interrupt;
  439. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  440. wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
  441. /* Unmask the thermal vector: */
  442. l = apic_read(APIC_LVTTHMR);
  443. apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
  444. printk_once(KERN_INFO "CPU0: Thermal monitoring enabled (%s)\n",
  445. tm2 ? "TM2" : "TM1");
  446. /* enable thermal throttle processing */
  447. atomic_set(&therm_throt_en, 1);
  448. }