therm_throt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Thermal throttle event support code (such as syslog messaging and rate
  4. * limiting) that was factored out from x86_64 (mce_intel.c) and i386 (p4.c).
  5. *
  6. * This allows consistent reporting of CPU thermal throttle events.
  7. *
  8. * Maintains a counter in /sys that keeps track of the number of thermal
  9. * events, such that the user knows how bad the thermal problem might be
  10. * (since the logging to syslog is rate limited).
  11. *
  12. * Author: Dmitriy Zavin (dmitriyz@google.com)
  13. *
  14. * Credits: Adapted from Zwane Mwaikambo's original code in mce_intel.c.
  15. * Inspired by Ross Biro's and Al Borchers' counter code.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/notifier.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/kernel.h>
  21. #include <linux/percpu.h>
  22. #include <linux/export.h>
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/cpu.h>
  27. #include <asm/processor.h>
  28. #include <asm/thermal.h>
  29. #include <asm/traps.h>
  30. #include <asm/apic.h>
  31. #include <asm/irq.h>
  32. #include <asm/msr.h>
  33. #include "intel_hfi.h"
  34. #include "thermal_interrupt.h"
  35. /* How long to wait between reporting thermal events */
  36. #define CHECK_INTERVAL (300 * HZ)
  37. #define THERMAL_THROTTLING_EVENT 0
  38. #define POWER_LIMIT_EVENT 1
  39. /**
  40. * struct _thermal_state - Represent the current thermal event state
  41. * @next_check: Stores the next timestamp, when it is allowed
  42. * to log the next warning message.
  43. * @last_interrupt_time: Stores the timestamp for the last threshold
  44. * high event.
  45. * @therm_work: Delayed workqueue structure
  46. * @count: Stores the current running count for thermal
  47. * or power threshold interrupts.
  48. * @last_count: Stores the previous running count for thermal
  49. * or power threshold interrupts.
  50. * @max_time_ms: This shows the maximum amount of time CPU was
  51. * in throttled state for a single thermal
  52. * threshold high to low state.
  53. * @total_time_ms: This is a cumulative time during which CPU was
  54. * in the throttled state.
  55. * @rate_control_active: Set when a throttling message is logged.
  56. * This is used for the purpose of rate-control.
  57. * @new_event: Stores the last high/low status of the
  58. * THERM_STATUS_PROCHOT or
  59. * THERM_STATUS_POWER_LIMIT.
  60. * @level: Stores whether this _thermal_state instance is
  61. * for a CORE level or for PACKAGE level.
  62. * @sample_index: Index for storing the next sample in the buffer
  63. * temp_samples[].
  64. * @sample_count: Total number of samples collected in the buffer
  65. * temp_samples[].
  66. * @average: The last moving average of temperature samples
  67. * @baseline_temp: Temperature at which thermal threshold high
  68. * interrupt was generated.
  69. * @temp_samples: Storage for temperature samples to calculate
  70. * moving average.
  71. *
  72. * This structure is used to represent data related to thermal state for a CPU.
  73. * There is a separate storage for core and package level for each CPU.
  74. */
  75. struct _thermal_state {
  76. u64 next_check;
  77. u64 last_interrupt_time;
  78. struct delayed_work therm_work;
  79. unsigned long count;
  80. unsigned long last_count;
  81. unsigned long max_time_ms;
  82. unsigned long total_time_ms;
  83. bool rate_control_active;
  84. bool new_event;
  85. u8 level;
  86. u8 sample_index;
  87. u8 sample_count;
  88. u8 average;
  89. u8 baseline_temp;
  90. u8 temp_samples[3];
  91. };
  92. struct thermal_state {
  93. struct _thermal_state core_throttle;
  94. struct _thermal_state core_power_limit;
  95. struct _thermal_state package_throttle;
  96. struct _thermal_state package_power_limit;
  97. struct _thermal_state core_thresh0;
  98. struct _thermal_state core_thresh1;
  99. struct _thermal_state pkg_thresh0;
  100. struct _thermal_state pkg_thresh1;
  101. };
  102. /* Callback to handle core threshold interrupts */
  103. int (*platform_thermal_notify)(__u64 msr_val);
  104. EXPORT_SYMBOL(platform_thermal_notify);
  105. /* Callback to handle core package threshold_interrupts */
  106. int (*platform_thermal_package_notify)(__u64 msr_val);
  107. EXPORT_SYMBOL_GPL(platform_thermal_package_notify);
  108. /* Callback support of rate control, return true, if
  109. * callback has rate control */
  110. bool (*platform_thermal_package_rate_control)(void);
  111. EXPORT_SYMBOL_GPL(platform_thermal_package_rate_control);
  112. static DEFINE_PER_CPU(struct thermal_state, thermal_state);
  113. static atomic_t therm_throt_en = ATOMIC_INIT(0);
  114. static u32 lvtthmr_init __read_mostly;
  115. #ifdef CONFIG_SYSFS
  116. #define define_therm_throt_device_one_ro(_name) \
  117. static DEVICE_ATTR(_name, 0444, \
  118. therm_throt_device_show_##_name, \
  119. NULL) \
  120. #define define_therm_throt_device_show_func(event, name) \
  121. \
  122. static ssize_t therm_throt_device_show_##event##_##name( \
  123. struct device *dev, \
  124. struct device_attribute *attr, \
  125. char *buf) \
  126. { \
  127. unsigned int cpu = dev->id; \
  128. ssize_t ret; \
  129. \
  130. preempt_disable(); /* CPU hotplug */ \
  131. if (cpu_online(cpu)) { \
  132. ret = sprintf(buf, "%lu\n", \
  133. per_cpu(thermal_state, cpu).event.name); \
  134. } else \
  135. ret = 0; \
  136. preempt_enable(); \
  137. \
  138. return ret; \
  139. }
  140. define_therm_throt_device_show_func(core_throttle, count);
  141. define_therm_throt_device_one_ro(core_throttle_count);
  142. define_therm_throt_device_show_func(core_power_limit, count);
  143. define_therm_throt_device_one_ro(core_power_limit_count);
  144. define_therm_throt_device_show_func(package_throttle, count);
  145. define_therm_throt_device_one_ro(package_throttle_count);
  146. define_therm_throt_device_show_func(package_power_limit, count);
  147. define_therm_throt_device_one_ro(package_power_limit_count);
  148. define_therm_throt_device_show_func(core_throttle, max_time_ms);
  149. define_therm_throt_device_one_ro(core_throttle_max_time_ms);
  150. define_therm_throt_device_show_func(package_throttle, max_time_ms);
  151. define_therm_throt_device_one_ro(package_throttle_max_time_ms);
  152. define_therm_throt_device_show_func(core_throttle, total_time_ms);
  153. define_therm_throt_device_one_ro(core_throttle_total_time_ms);
  154. define_therm_throt_device_show_func(package_throttle, total_time_ms);
  155. define_therm_throt_device_one_ro(package_throttle_total_time_ms);
  156. static struct attribute *thermal_throttle_attrs[] = {
  157. &dev_attr_core_throttle_count.attr,
  158. &dev_attr_core_throttle_max_time_ms.attr,
  159. &dev_attr_core_throttle_total_time_ms.attr,
  160. NULL
  161. };
  162. static const struct attribute_group thermal_attr_group = {
  163. .attrs = thermal_throttle_attrs,
  164. .name = "thermal_throttle"
  165. };
  166. #endif /* CONFIG_SYSFS */
  167. #define THERM_THROT_POLL_INTERVAL HZ
  168. #define THERM_STATUS_PROCHOT_LOG BIT(1)
  169. static u64 therm_intr_core_clear_mask;
  170. static u64 therm_intr_pkg_clear_mask;
  171. static void thermal_intr_init_core_clear_mask(void)
  172. {
  173. if (therm_intr_core_clear_mask)
  174. return;
  175. /*
  176. * Reference: Intel SDM Volume 4
  177. * "Table 2-2. IA-32 Architectural MSRs", MSR 0x19C
  178. * IA32_THERM_STATUS.
  179. */
  180. /*
  181. * Bit 1, 3, 5: CPUID.01H:EDX[22] = 1. This driver will not
  182. * enable interrupts, when 0 as it checks for X86_FEATURE_ACPI.
  183. */
  184. therm_intr_core_clear_mask = (BIT(1) | BIT(3) | BIT(5));
  185. /*
  186. * Bit 7 and 9: Thermal Threshold #1 and #2 log
  187. * If CPUID.01H:ECX[8] = 1
  188. */
  189. if (boot_cpu_has(X86_FEATURE_TM2))
  190. therm_intr_core_clear_mask |= (BIT(7) | BIT(9));
  191. /* Bit 11: Power Limitation log (R/WC0) If CPUID.06H:EAX[4] = 1 */
  192. if (boot_cpu_has(X86_FEATURE_PLN))
  193. therm_intr_core_clear_mask |= BIT(11);
  194. /*
  195. * Bit 13: Current Limit log (R/WC0) If CPUID.06H:EAX[7] = 1
  196. * Bit 15: Cross Domain Limit log (R/WC0) If CPUID.06H:EAX[7] = 1
  197. */
  198. if (boot_cpu_has(X86_FEATURE_HWP))
  199. therm_intr_core_clear_mask |= (BIT(13) | BIT(15));
  200. }
  201. static void thermal_intr_init_pkg_clear_mask(void)
  202. {
  203. if (therm_intr_pkg_clear_mask)
  204. return;
  205. /*
  206. * Reference: Intel SDM Volume 4
  207. * "Table 2-2. IA-32 Architectural MSRs", MSR 0x1B1
  208. * IA32_PACKAGE_THERM_STATUS.
  209. */
  210. /* All bits except BIT 26 depend on CPUID.06H: EAX[6] = 1 */
  211. if (boot_cpu_has(X86_FEATURE_PTS))
  212. therm_intr_pkg_clear_mask = (BIT(1) | BIT(3) | BIT(5) | BIT(7) | BIT(9) | BIT(11));
  213. /*
  214. * Intel SDM Volume 2A: Thermal and Power Management Leaf
  215. * Bit 26: CPUID.06H: EAX[19] = 1
  216. */
  217. if (boot_cpu_has(X86_FEATURE_HFI))
  218. therm_intr_pkg_clear_mask |= BIT(26);
  219. }
  220. /*
  221. * Clear the bits in package thermal status register for bit = 1
  222. * in bitmask
  223. */
  224. void thermal_clear_package_intr_status(int level, u64 bit_mask)
  225. {
  226. u64 msr_val;
  227. int msr;
  228. if (level == CORE_LEVEL) {
  229. msr = MSR_IA32_THERM_STATUS;
  230. msr_val = therm_intr_core_clear_mask;
  231. } else {
  232. msr = MSR_IA32_PACKAGE_THERM_STATUS;
  233. msr_val = therm_intr_pkg_clear_mask;
  234. }
  235. msr_val &= ~bit_mask;
  236. wrmsrl(msr, msr_val);
  237. }
  238. EXPORT_SYMBOL_GPL(thermal_clear_package_intr_status);
  239. static void get_therm_status(int level, bool *proc_hot, u8 *temp)
  240. {
  241. int msr;
  242. u64 msr_val;
  243. if (level == CORE_LEVEL)
  244. msr = MSR_IA32_THERM_STATUS;
  245. else
  246. msr = MSR_IA32_PACKAGE_THERM_STATUS;
  247. rdmsrl(msr, msr_val);
  248. if (msr_val & THERM_STATUS_PROCHOT_LOG)
  249. *proc_hot = true;
  250. else
  251. *proc_hot = false;
  252. *temp = (msr_val >> 16) & 0x7F;
  253. }
  254. static void __maybe_unused throttle_active_work(struct work_struct *work)
  255. {
  256. struct _thermal_state *state = container_of(to_delayed_work(work),
  257. struct _thermal_state, therm_work);
  258. unsigned int i, avg, this_cpu = smp_processor_id();
  259. u64 now = get_jiffies_64();
  260. bool hot;
  261. u8 temp;
  262. get_therm_status(state->level, &hot, &temp);
  263. /* temperature value is offset from the max so lesser means hotter */
  264. if (!hot && temp > state->baseline_temp) {
  265. if (state->rate_control_active)
  266. pr_info("CPU%d: %s temperature/speed normal (total events = %lu)\n",
  267. this_cpu,
  268. state->level == CORE_LEVEL ? "Core" : "Package",
  269. state->count);
  270. state->rate_control_active = false;
  271. return;
  272. }
  273. if (time_before64(now, state->next_check) &&
  274. state->rate_control_active)
  275. goto re_arm;
  276. state->next_check = now + CHECK_INTERVAL;
  277. if (state->count != state->last_count) {
  278. /* There was one new thermal interrupt */
  279. state->last_count = state->count;
  280. state->average = 0;
  281. state->sample_count = 0;
  282. state->sample_index = 0;
  283. }
  284. state->temp_samples[state->sample_index] = temp;
  285. state->sample_count++;
  286. state->sample_index = (state->sample_index + 1) % ARRAY_SIZE(state->temp_samples);
  287. if (state->sample_count < ARRAY_SIZE(state->temp_samples))
  288. goto re_arm;
  289. avg = 0;
  290. for (i = 0; i < ARRAY_SIZE(state->temp_samples); ++i)
  291. avg += state->temp_samples[i];
  292. avg /= ARRAY_SIZE(state->temp_samples);
  293. if (state->average > avg) {
  294. pr_warn("CPU%d: %s temperature is above threshold, cpu clock is throttled (total events = %lu)\n",
  295. this_cpu,
  296. state->level == CORE_LEVEL ? "Core" : "Package",
  297. state->count);
  298. state->rate_control_active = true;
  299. }
  300. state->average = avg;
  301. re_arm:
  302. thermal_clear_package_intr_status(state->level, THERM_STATUS_PROCHOT_LOG);
  303. schedule_delayed_work_on(this_cpu, &state->therm_work, THERM_THROT_POLL_INTERVAL);
  304. }
  305. /***
  306. * therm_throt_process - Process thermal throttling event from interrupt
  307. * @curr: Whether the condition is current or not (boolean), since the
  308. * thermal interrupt normally gets called both when the thermal
  309. * event begins and once the event has ended.
  310. *
  311. * This function is called by the thermal interrupt after the
  312. * IRQ has been acknowledged.
  313. *
  314. * It will take care of rate limiting and printing messages to the syslog.
  315. */
  316. static void therm_throt_process(bool new_event, int event, int level)
  317. {
  318. struct _thermal_state *state;
  319. unsigned int this_cpu = smp_processor_id();
  320. bool old_event;
  321. u64 now;
  322. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  323. now = get_jiffies_64();
  324. if (level == CORE_LEVEL) {
  325. if (event == THERMAL_THROTTLING_EVENT)
  326. state = &pstate->core_throttle;
  327. else if (event == POWER_LIMIT_EVENT)
  328. state = &pstate->core_power_limit;
  329. else
  330. return;
  331. } else if (level == PACKAGE_LEVEL) {
  332. if (event == THERMAL_THROTTLING_EVENT)
  333. state = &pstate->package_throttle;
  334. else if (event == POWER_LIMIT_EVENT)
  335. state = &pstate->package_power_limit;
  336. else
  337. return;
  338. } else
  339. return;
  340. old_event = state->new_event;
  341. state->new_event = new_event;
  342. if (new_event)
  343. state->count++;
  344. if (event != THERMAL_THROTTLING_EVENT)
  345. return;
  346. if (new_event && !state->last_interrupt_time) {
  347. bool hot;
  348. u8 temp;
  349. get_therm_status(state->level, &hot, &temp);
  350. /*
  351. * Ignore short temperature spike as the system is not close
  352. * to PROCHOT. 10C offset is large enough to ignore. It is
  353. * already dropped from the high threshold temperature.
  354. */
  355. if (temp > 10)
  356. return;
  357. state->baseline_temp = temp;
  358. state->last_interrupt_time = now;
  359. schedule_delayed_work_on(this_cpu, &state->therm_work, THERM_THROT_POLL_INTERVAL);
  360. } else if (old_event && state->last_interrupt_time) {
  361. unsigned long throttle_time;
  362. throttle_time = jiffies_delta_to_msecs(now - state->last_interrupt_time);
  363. if (throttle_time > state->max_time_ms)
  364. state->max_time_ms = throttle_time;
  365. state->total_time_ms += throttle_time;
  366. state->last_interrupt_time = 0;
  367. }
  368. }
  369. static int thresh_event_valid(int level, int event)
  370. {
  371. struct _thermal_state *state;
  372. unsigned int this_cpu = smp_processor_id();
  373. struct thermal_state *pstate = &per_cpu(thermal_state, this_cpu);
  374. u64 now = get_jiffies_64();
  375. if (level == PACKAGE_LEVEL)
  376. state = (event == 0) ? &pstate->pkg_thresh0 :
  377. &pstate->pkg_thresh1;
  378. else
  379. state = (event == 0) ? &pstate->core_thresh0 :
  380. &pstate->core_thresh1;
  381. if (time_before64(now, state->next_check))
  382. return 0;
  383. state->next_check = now + CHECK_INTERVAL;
  384. return 1;
  385. }
  386. static bool int_pln_enable;
  387. static int __init int_pln_enable_setup(char *s)
  388. {
  389. int_pln_enable = true;
  390. return 1;
  391. }
  392. __setup("int_pln_enable", int_pln_enable_setup);
  393. #ifdef CONFIG_SYSFS
  394. /* Add/Remove thermal_throttle interface for CPU device: */
  395. static int thermal_throttle_add_dev(struct device *dev, unsigned int cpu)
  396. {
  397. int err;
  398. struct cpuinfo_x86 *c = &cpu_data(cpu);
  399. err = sysfs_create_group(&dev->kobj, &thermal_attr_group);
  400. if (err)
  401. return err;
  402. if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) {
  403. err = sysfs_add_file_to_group(&dev->kobj,
  404. &dev_attr_core_power_limit_count.attr,
  405. thermal_attr_group.name);
  406. if (err)
  407. goto del_group;
  408. }
  409. if (cpu_has(c, X86_FEATURE_PTS)) {
  410. err = sysfs_add_file_to_group(&dev->kobj,
  411. &dev_attr_package_throttle_count.attr,
  412. thermal_attr_group.name);
  413. if (err)
  414. goto del_group;
  415. err = sysfs_add_file_to_group(&dev->kobj,
  416. &dev_attr_package_throttle_max_time_ms.attr,
  417. thermal_attr_group.name);
  418. if (err)
  419. goto del_group;
  420. err = sysfs_add_file_to_group(&dev->kobj,
  421. &dev_attr_package_throttle_total_time_ms.attr,
  422. thermal_attr_group.name);
  423. if (err)
  424. goto del_group;
  425. if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable) {
  426. err = sysfs_add_file_to_group(&dev->kobj,
  427. &dev_attr_package_power_limit_count.attr,
  428. thermal_attr_group.name);
  429. if (err)
  430. goto del_group;
  431. }
  432. }
  433. return 0;
  434. del_group:
  435. sysfs_remove_group(&dev->kobj, &thermal_attr_group);
  436. return err;
  437. }
  438. static void thermal_throttle_remove_dev(struct device *dev)
  439. {
  440. sysfs_remove_group(&dev->kobj, &thermal_attr_group);
  441. }
  442. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  443. static int thermal_throttle_online(unsigned int cpu)
  444. {
  445. struct thermal_state *state = &per_cpu(thermal_state, cpu);
  446. struct device *dev = get_cpu_device(cpu);
  447. u32 l;
  448. state->package_throttle.level = PACKAGE_LEVEL;
  449. state->core_throttle.level = CORE_LEVEL;
  450. INIT_DELAYED_WORK(&state->package_throttle.therm_work, throttle_active_work);
  451. INIT_DELAYED_WORK(&state->core_throttle.therm_work, throttle_active_work);
  452. /*
  453. * The first CPU coming online will enable the HFI. Usually this causes
  454. * hardware to issue an HFI thermal interrupt. Such interrupt will reach
  455. * the CPU once we enable the thermal vector in the local APIC.
  456. */
  457. intel_hfi_online(cpu);
  458. /* Unmask the thermal vector after the above workqueues are initialized. */
  459. l = apic_read(APIC_LVTTHMR);
  460. apic_write(APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
  461. return thermal_throttle_add_dev(dev, cpu);
  462. }
  463. static int thermal_throttle_offline(unsigned int cpu)
  464. {
  465. struct thermal_state *state = &per_cpu(thermal_state, cpu);
  466. struct device *dev = get_cpu_device(cpu);
  467. u32 l;
  468. /* Mask the thermal vector before draining evtl. pending work */
  469. l = apic_read(APIC_LVTTHMR);
  470. apic_write(APIC_LVTTHMR, l | APIC_LVT_MASKED);
  471. intel_hfi_offline(cpu);
  472. cancel_delayed_work_sync(&state->package_throttle.therm_work);
  473. cancel_delayed_work_sync(&state->core_throttle.therm_work);
  474. state->package_throttle.rate_control_active = false;
  475. state->core_throttle.rate_control_active = false;
  476. thermal_throttle_remove_dev(dev);
  477. return 0;
  478. }
  479. static __init int thermal_throttle_init_device(void)
  480. {
  481. int ret;
  482. if (!atomic_read(&therm_throt_en))
  483. return 0;
  484. intel_hfi_init();
  485. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/therm:online",
  486. thermal_throttle_online,
  487. thermal_throttle_offline);
  488. return ret < 0 ? ret : 0;
  489. }
  490. device_initcall(thermal_throttle_init_device);
  491. #endif /* CONFIG_SYSFS */
  492. static void notify_package_thresholds(__u64 msr_val)
  493. {
  494. bool notify_thres_0 = false;
  495. bool notify_thres_1 = false;
  496. if (!platform_thermal_package_notify)
  497. return;
  498. /* lower threshold check */
  499. if (msr_val & THERM_LOG_THRESHOLD0)
  500. notify_thres_0 = true;
  501. /* higher threshold check */
  502. if (msr_val & THERM_LOG_THRESHOLD1)
  503. notify_thres_1 = true;
  504. if (!notify_thres_0 && !notify_thres_1)
  505. return;
  506. if (platform_thermal_package_rate_control &&
  507. platform_thermal_package_rate_control()) {
  508. /* Rate control is implemented in callback */
  509. platform_thermal_package_notify(msr_val);
  510. return;
  511. }
  512. /* lower threshold reached */
  513. if (notify_thres_0 && thresh_event_valid(PACKAGE_LEVEL, 0))
  514. platform_thermal_package_notify(msr_val);
  515. /* higher threshold reached */
  516. if (notify_thres_1 && thresh_event_valid(PACKAGE_LEVEL, 1))
  517. platform_thermal_package_notify(msr_val);
  518. }
  519. static void notify_thresholds(__u64 msr_val)
  520. {
  521. /* check whether the interrupt handler is defined;
  522. * otherwise simply return
  523. */
  524. if (!platform_thermal_notify)
  525. return;
  526. /* lower threshold reached */
  527. if ((msr_val & THERM_LOG_THRESHOLD0) &&
  528. thresh_event_valid(CORE_LEVEL, 0))
  529. platform_thermal_notify(msr_val);
  530. /* higher threshold reached */
  531. if ((msr_val & THERM_LOG_THRESHOLD1) &&
  532. thresh_event_valid(CORE_LEVEL, 1))
  533. platform_thermal_notify(msr_val);
  534. }
  535. void __weak notify_hwp_interrupt(void)
  536. {
  537. wrmsrl_safe(MSR_HWP_STATUS, 0);
  538. }
  539. /* Thermal transition interrupt handler */
  540. void intel_thermal_interrupt(void)
  541. {
  542. __u64 msr_val;
  543. if (static_cpu_has(X86_FEATURE_HWP))
  544. notify_hwp_interrupt();
  545. rdmsrl(MSR_IA32_THERM_STATUS, msr_val);
  546. /* Check for violation of core thermal thresholds*/
  547. notify_thresholds(msr_val);
  548. therm_throt_process(msr_val & THERM_STATUS_PROCHOT,
  549. THERMAL_THROTTLING_EVENT,
  550. CORE_LEVEL);
  551. if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
  552. therm_throt_process(msr_val & THERM_STATUS_POWER_LIMIT,
  553. POWER_LIMIT_EVENT,
  554. CORE_LEVEL);
  555. if (this_cpu_has(X86_FEATURE_PTS)) {
  556. rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
  557. /* check violations of package thermal thresholds */
  558. notify_package_thresholds(msr_val);
  559. therm_throt_process(msr_val & PACKAGE_THERM_STATUS_PROCHOT,
  560. THERMAL_THROTTLING_EVENT,
  561. PACKAGE_LEVEL);
  562. if (this_cpu_has(X86_FEATURE_PLN) && int_pln_enable)
  563. therm_throt_process(msr_val &
  564. PACKAGE_THERM_STATUS_POWER_LIMIT,
  565. POWER_LIMIT_EVENT,
  566. PACKAGE_LEVEL);
  567. if (this_cpu_has(X86_FEATURE_HFI))
  568. intel_hfi_process_event(msr_val &
  569. PACKAGE_THERM_STATUS_HFI_UPDATED);
  570. }
  571. }
  572. /* Thermal monitoring depends on APIC, ACPI and clock modulation */
  573. static int intel_thermal_supported(struct cpuinfo_x86 *c)
  574. {
  575. if (!boot_cpu_has(X86_FEATURE_APIC))
  576. return 0;
  577. if (!cpu_has(c, X86_FEATURE_ACPI) || !cpu_has(c, X86_FEATURE_ACC))
  578. return 0;
  579. return 1;
  580. }
  581. bool x86_thermal_enabled(void)
  582. {
  583. return atomic_read(&therm_throt_en);
  584. }
  585. void __init therm_lvt_init(void)
  586. {
  587. /*
  588. * This function is only called on boot CPU. Save the init thermal
  589. * LVT value on BSP and use that value to restore APs' thermal LVT
  590. * entry BIOS programmed later
  591. */
  592. if (intel_thermal_supported(&boot_cpu_data))
  593. lvtthmr_init = apic_read(APIC_LVTTHMR);
  594. }
  595. void intel_init_thermal(struct cpuinfo_x86 *c)
  596. {
  597. unsigned int cpu = smp_processor_id();
  598. int tm2 = 0;
  599. u32 l, h;
  600. if (!intel_thermal_supported(c))
  601. return;
  602. /*
  603. * First check if its enabled already, in which case there might
  604. * be some SMM goo which handles it, so we can't even put a handler
  605. * since it might be delivered via SMI already:
  606. */
  607. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  608. h = lvtthmr_init;
  609. /*
  610. * The initial value of thermal LVT entries on all APs always reads
  611. * 0x10000 because APs are woken up by BSP issuing INIT-SIPI-SIPI
  612. * sequence to them and LVT registers are reset to 0s except for
  613. * the mask bits which are set to 1s when APs receive INIT IPI.
  614. * If BIOS takes over the thermal interrupt and sets its interrupt
  615. * delivery mode to SMI (not fixed), it restores the value that the
  616. * BIOS has programmed on AP based on BSP's info we saved since BIOS
  617. * is always setting the same value for all threads/cores.
  618. */
  619. if ((h & APIC_DM_FIXED_MASK) != APIC_DM_FIXED)
  620. apic_write(APIC_LVTTHMR, lvtthmr_init);
  621. if ((l & MSR_IA32_MISC_ENABLE_TM1) && (h & APIC_DM_SMI)) {
  622. if (system_state == SYSTEM_BOOTING)
  623. pr_debug("CPU%d: Thermal monitoring handled by SMI\n", cpu);
  624. return;
  625. }
  626. /* early Pentium M models use different method for enabling TM2 */
  627. if (cpu_has(c, X86_FEATURE_TM2)) {
  628. if (c->x86 == 6 && (c->x86_model == 9 || c->x86_model == 13)) {
  629. rdmsr(MSR_THERM2_CTL, l, h);
  630. if (l & MSR_THERM2_CTL_TM_SELECT)
  631. tm2 = 1;
  632. } else if (l & MSR_IA32_MISC_ENABLE_TM2)
  633. tm2 = 1;
  634. }
  635. /* We'll mask the thermal vector in the lapic till we're ready: */
  636. h = THERMAL_APIC_VECTOR | APIC_DM_FIXED | APIC_LVT_MASKED;
  637. apic_write(APIC_LVTTHMR, h);
  638. thermal_intr_init_core_clear_mask();
  639. thermal_intr_init_pkg_clear_mask();
  640. rdmsr(MSR_IA32_THERM_INTERRUPT, l, h);
  641. if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
  642. wrmsr(MSR_IA32_THERM_INTERRUPT,
  643. (l | (THERM_INT_LOW_ENABLE
  644. | THERM_INT_HIGH_ENABLE)) & ~THERM_INT_PLN_ENABLE, h);
  645. else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  646. wrmsr(MSR_IA32_THERM_INTERRUPT,
  647. l | (THERM_INT_LOW_ENABLE
  648. | THERM_INT_HIGH_ENABLE | THERM_INT_PLN_ENABLE), h);
  649. else
  650. wrmsr(MSR_IA32_THERM_INTERRUPT,
  651. l | (THERM_INT_LOW_ENABLE | THERM_INT_HIGH_ENABLE), h);
  652. if (cpu_has(c, X86_FEATURE_PTS)) {
  653. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  654. if (cpu_has(c, X86_FEATURE_PLN) && !int_pln_enable)
  655. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  656. (l | (PACKAGE_THERM_INT_LOW_ENABLE
  657. | PACKAGE_THERM_INT_HIGH_ENABLE))
  658. & ~PACKAGE_THERM_INT_PLN_ENABLE, h);
  659. else if (cpu_has(c, X86_FEATURE_PLN) && int_pln_enable)
  660. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  661. l | (PACKAGE_THERM_INT_LOW_ENABLE
  662. | PACKAGE_THERM_INT_HIGH_ENABLE
  663. | PACKAGE_THERM_INT_PLN_ENABLE), h);
  664. else
  665. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  666. l | (PACKAGE_THERM_INT_LOW_ENABLE
  667. | PACKAGE_THERM_INT_HIGH_ENABLE), h);
  668. if (cpu_has(c, X86_FEATURE_HFI)) {
  669. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  670. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  671. l | PACKAGE_THERM_INT_HFI_ENABLE, h);
  672. }
  673. }
  674. rdmsr(MSR_IA32_MISC_ENABLE, l, h);
  675. wrmsr(MSR_IA32_MISC_ENABLE, l | MSR_IA32_MISC_ENABLE_TM1, h);
  676. pr_info_once("CPU0: Thermal monitoring enabled (%s)\n",
  677. tm2 ? "TM2" : "TM1");
  678. /* enable thermal throttle processing */
  679. atomic_set(&therm_throt_en, 1);
  680. }