x86_pkg_temp_thermal.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * x86_pkg_temp_thermal driver
  4. * Copyright (c) 2013, Intel Corporation.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/intel_tcc.h>
  10. #include <linux/err.h>
  11. #include <linux/param.h>
  12. #include <linux/device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/cpu.h>
  15. #include <linux/smp.h>
  16. #include <linux/slab.h>
  17. #include <linux/pm.h>
  18. #include <linux/thermal.h>
  19. #include <linux/debugfs.h>
  20. #include <asm/cpu_device_id.h>
  21. #include "thermal_interrupt.h"
  22. /*
  23. * Rate control delay: Idea is to introduce denounce effect
  24. * This should be long enough to avoid reduce events, when
  25. * threshold is set to a temperature, which is constantly
  26. * violated, but at the short enough to take any action.
  27. * The action can be remove threshold or change it to next
  28. * interesting setting. Based on experiments, in around
  29. * every 5 seconds under load will give us a significant
  30. * temperature change.
  31. */
  32. #define PKG_TEMP_THERMAL_NOTIFY_DELAY 5000
  33. static int notify_delay_ms = PKG_TEMP_THERMAL_NOTIFY_DELAY;
  34. module_param(notify_delay_ms, int, 0644);
  35. MODULE_PARM_DESC(notify_delay_ms,
  36. "User space notification delay in milli seconds.");
  37. /* Number of trip points in thermal zone. Currently it can't
  38. * be more than 2. MSR can allow setting and getting notifications
  39. * for only 2 thresholds. This define enforces this, if there
  40. * is some wrong values returned by cpuid for number of thresholds.
  41. */
  42. #define MAX_NUMBER_OF_TRIPS 2
  43. struct zone_device {
  44. int cpu;
  45. bool work_scheduled;
  46. u32 msr_pkg_therm_low;
  47. u32 msr_pkg_therm_high;
  48. struct delayed_work work;
  49. struct thermal_zone_device *tzone;
  50. struct cpumask cpumask;
  51. };
  52. static struct thermal_zone_params pkg_temp_tz_params = {
  53. .no_hwmon = true,
  54. };
  55. /* Keep track of how many zone pointers we allocated in init() */
  56. static int max_id __read_mostly;
  57. /* Array of zone pointers */
  58. static struct zone_device **zones;
  59. /* Serializes interrupt notification, work and hotplug */
  60. static DEFINE_RAW_SPINLOCK(pkg_temp_lock);
  61. /* Protects zone operation in the work function against hotplug removal */
  62. static DEFINE_MUTEX(thermal_zone_mutex);
  63. /* The dynamically assigned cpu hotplug state for module_exit() */
  64. static enum cpuhp_state pkg_thermal_hp_state __read_mostly;
  65. /* Debug counters to show using debugfs */
  66. static struct dentry *debugfs;
  67. static unsigned int pkg_interrupt_cnt;
  68. static unsigned int pkg_work_cnt;
  69. static void pkg_temp_debugfs_init(void)
  70. {
  71. debugfs = debugfs_create_dir("pkg_temp_thermal", NULL);
  72. debugfs_create_u32("pkg_thres_interrupt", S_IRUGO, debugfs,
  73. &pkg_interrupt_cnt);
  74. debugfs_create_u32("pkg_thres_work", S_IRUGO, debugfs,
  75. &pkg_work_cnt);
  76. }
  77. /*
  78. * Protection:
  79. *
  80. * - cpu hotplug: Read serialized by cpu hotplug lock
  81. * Write must hold pkg_temp_lock
  82. *
  83. * - Other callsites: Must hold pkg_temp_lock
  84. */
  85. static struct zone_device *pkg_temp_thermal_get_dev(unsigned int cpu)
  86. {
  87. int id = topology_logical_die_id(cpu);
  88. if (id >= 0 && id < max_id)
  89. return zones[id];
  90. return NULL;
  91. }
  92. static int sys_get_curr_temp(struct thermal_zone_device *tzd, int *temp)
  93. {
  94. struct zone_device *zonedev = thermal_zone_device_priv(tzd);
  95. int val, ret;
  96. ret = intel_tcc_get_temp(zonedev->cpu, &val, true);
  97. if (ret < 0)
  98. return ret;
  99. *temp = val * 1000;
  100. pr_debug("sys_get_curr_temp %d\n", *temp);
  101. return 0;
  102. }
  103. static int
  104. sys_set_trip_temp(struct thermal_zone_device *tzd,
  105. const struct thermal_trip *trip, int temp)
  106. {
  107. struct zone_device *zonedev = thermal_zone_device_priv(tzd);
  108. unsigned int trip_index = THERMAL_TRIP_PRIV_TO_INT(trip->priv);
  109. u32 l, h, mask, shift, intr;
  110. int tj_max, val, ret;
  111. tj_max = intel_tcc_get_tjmax(zonedev->cpu);
  112. if (tj_max < 0)
  113. return tj_max;
  114. tj_max *= 1000;
  115. val = (tj_max - temp)/1000;
  116. if (trip_index >= MAX_NUMBER_OF_TRIPS || val < 0 || val > 0x7f)
  117. return -EINVAL;
  118. ret = rdmsr_on_cpu(zonedev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
  119. &l, &h);
  120. if (ret < 0)
  121. return ret;
  122. if (trip_index) {
  123. mask = THERM_MASK_THRESHOLD1;
  124. shift = THERM_SHIFT_THRESHOLD1;
  125. intr = THERM_INT_THRESHOLD1_ENABLE;
  126. } else {
  127. mask = THERM_MASK_THRESHOLD0;
  128. shift = THERM_SHIFT_THRESHOLD0;
  129. intr = THERM_INT_THRESHOLD0_ENABLE;
  130. }
  131. l &= ~mask;
  132. /*
  133. * When users space sets a trip temperature == 0, which is indication
  134. * that, it is no longer interested in receiving notifications.
  135. */
  136. if (!temp) {
  137. l &= ~intr;
  138. } else {
  139. l |= val << shift;
  140. l |= intr;
  141. }
  142. return wrmsr_on_cpu(zonedev->cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
  143. l, h);
  144. }
  145. /* Thermal zone callback registry */
  146. static const struct thermal_zone_device_ops tzone_ops = {
  147. .get_temp = sys_get_curr_temp,
  148. .set_trip_temp = sys_set_trip_temp,
  149. };
  150. static bool pkg_thermal_rate_control(void)
  151. {
  152. return true;
  153. }
  154. /* Enable threshold interrupt on local package/cpu */
  155. static inline void enable_pkg_thres_interrupt(void)
  156. {
  157. u8 thres_0, thres_1;
  158. u32 l, h;
  159. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  160. /* only enable/disable if it had valid threshold value */
  161. thres_0 = (l & THERM_MASK_THRESHOLD0) >> THERM_SHIFT_THRESHOLD0;
  162. thres_1 = (l & THERM_MASK_THRESHOLD1) >> THERM_SHIFT_THRESHOLD1;
  163. if (thres_0)
  164. l |= THERM_INT_THRESHOLD0_ENABLE;
  165. if (thres_1)
  166. l |= THERM_INT_THRESHOLD1_ENABLE;
  167. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  168. }
  169. /* Disable threshold interrupt on local package/cpu */
  170. static inline void disable_pkg_thres_interrupt(void)
  171. {
  172. u32 l, h;
  173. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  174. l &= ~(THERM_INT_THRESHOLD0_ENABLE | THERM_INT_THRESHOLD1_ENABLE);
  175. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  176. }
  177. static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
  178. {
  179. struct thermal_zone_device *tzone = NULL;
  180. int cpu = smp_processor_id();
  181. struct zone_device *zonedev;
  182. mutex_lock(&thermal_zone_mutex);
  183. raw_spin_lock_irq(&pkg_temp_lock);
  184. ++pkg_work_cnt;
  185. zonedev = pkg_temp_thermal_get_dev(cpu);
  186. if (!zonedev) {
  187. raw_spin_unlock_irq(&pkg_temp_lock);
  188. mutex_unlock(&thermal_zone_mutex);
  189. return;
  190. }
  191. zonedev->work_scheduled = false;
  192. thermal_clear_package_intr_status(PACKAGE_LEVEL, THERM_LOG_THRESHOLD0 | THERM_LOG_THRESHOLD1);
  193. tzone = zonedev->tzone;
  194. enable_pkg_thres_interrupt();
  195. raw_spin_unlock_irq(&pkg_temp_lock);
  196. /*
  197. * If tzone is not NULL, then thermal_zone_mutex will prevent the
  198. * concurrent removal in the cpu offline callback.
  199. */
  200. if (tzone)
  201. thermal_zone_device_update(tzone, THERMAL_EVENT_UNSPECIFIED);
  202. mutex_unlock(&thermal_zone_mutex);
  203. }
  204. static void pkg_thermal_schedule_work(int cpu, struct delayed_work *work)
  205. {
  206. unsigned long ms = msecs_to_jiffies(notify_delay_ms);
  207. schedule_delayed_work_on(cpu, work, ms);
  208. }
  209. static int pkg_thermal_notify(u64 msr_val)
  210. {
  211. int cpu = smp_processor_id();
  212. struct zone_device *zonedev;
  213. unsigned long flags;
  214. raw_spin_lock_irqsave(&pkg_temp_lock, flags);
  215. ++pkg_interrupt_cnt;
  216. disable_pkg_thres_interrupt();
  217. /* Work is per package, so scheduling it once is enough. */
  218. zonedev = pkg_temp_thermal_get_dev(cpu);
  219. if (zonedev && !zonedev->work_scheduled) {
  220. zonedev->work_scheduled = true;
  221. pkg_thermal_schedule_work(zonedev->cpu, &zonedev->work);
  222. }
  223. raw_spin_unlock_irqrestore(&pkg_temp_lock, flags);
  224. return 0;
  225. }
  226. static int pkg_temp_thermal_trips_init(int cpu, int tj_max,
  227. struct thermal_trip *trips, int num_trips)
  228. {
  229. unsigned long thres_reg_value;
  230. u32 mask, shift, eax, edx;
  231. int ret, i;
  232. for (i = 0; i < num_trips; i++) {
  233. if (i) {
  234. mask = THERM_MASK_THRESHOLD1;
  235. shift = THERM_SHIFT_THRESHOLD1;
  236. } else {
  237. mask = THERM_MASK_THRESHOLD0;
  238. shift = THERM_SHIFT_THRESHOLD0;
  239. }
  240. ret = rdmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
  241. &eax, &edx);
  242. if (ret < 0)
  243. return ret;
  244. thres_reg_value = (eax & mask) >> shift;
  245. trips[i].temperature = thres_reg_value ?
  246. tj_max - thres_reg_value * 1000 : THERMAL_TEMP_INVALID;
  247. trips[i].type = THERMAL_TRIP_PASSIVE;
  248. trips[i].flags |= THERMAL_TRIP_FLAG_RW_TEMP;
  249. trips[i].priv = THERMAL_INT_TO_TRIP_PRIV(i);
  250. pr_debug("%s: cpu=%d, trip=%d, temp=%d\n",
  251. __func__, cpu, i, trips[i].temperature);
  252. }
  253. return 0;
  254. }
  255. static int pkg_temp_thermal_device_add(unsigned int cpu)
  256. {
  257. struct thermal_trip trips[MAX_NUMBER_OF_TRIPS] = { 0 };
  258. int id = topology_logical_die_id(cpu);
  259. u32 eax, ebx, ecx, edx;
  260. struct zone_device *zonedev;
  261. int thres_count, err;
  262. int tj_max;
  263. if (id >= max_id)
  264. return -ENOMEM;
  265. cpuid(6, &eax, &ebx, &ecx, &edx);
  266. thres_count = ebx & 0x07;
  267. if (!thres_count)
  268. return -ENODEV;
  269. thres_count = clamp_val(thres_count, 0, MAX_NUMBER_OF_TRIPS);
  270. tj_max = intel_tcc_get_tjmax(cpu);
  271. if (tj_max < 0)
  272. return tj_max;
  273. tj_max *= 1000;
  274. zonedev = kzalloc(sizeof(*zonedev), GFP_KERNEL);
  275. if (!zonedev)
  276. return -ENOMEM;
  277. err = pkg_temp_thermal_trips_init(cpu, tj_max, trips, thres_count);
  278. if (err)
  279. goto out_kfree_zonedev;
  280. INIT_DELAYED_WORK(&zonedev->work, pkg_temp_thermal_threshold_work_fn);
  281. zonedev->cpu = cpu;
  282. zonedev->tzone = thermal_zone_device_register_with_trips("x86_pkg_temp",
  283. trips, thres_count,
  284. zonedev, &tzone_ops, &pkg_temp_tz_params, 0, 0);
  285. if (IS_ERR(zonedev->tzone)) {
  286. err = PTR_ERR(zonedev->tzone);
  287. goto out_kfree_zonedev;
  288. }
  289. err = thermal_zone_device_enable(zonedev->tzone);
  290. if (err)
  291. goto out_unregister_tz;
  292. /* Store MSR value for package thermal interrupt, to restore at exit */
  293. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, zonedev->msr_pkg_therm_low,
  294. zonedev->msr_pkg_therm_high);
  295. cpumask_set_cpu(cpu, &zonedev->cpumask);
  296. raw_spin_lock_irq(&pkg_temp_lock);
  297. zones[id] = zonedev;
  298. raw_spin_unlock_irq(&pkg_temp_lock);
  299. return 0;
  300. out_unregister_tz:
  301. thermal_zone_device_unregister(zonedev->tzone);
  302. out_kfree_zonedev:
  303. kfree(zonedev);
  304. return err;
  305. }
  306. static int pkg_thermal_cpu_offline(unsigned int cpu)
  307. {
  308. struct zone_device *zonedev = pkg_temp_thermal_get_dev(cpu);
  309. bool lastcpu, was_target;
  310. int target;
  311. if (!zonedev)
  312. return 0;
  313. target = cpumask_any_but(&zonedev->cpumask, cpu);
  314. cpumask_clear_cpu(cpu, &zonedev->cpumask);
  315. lastcpu = target >= nr_cpu_ids;
  316. /*
  317. * Remove the sysfs files, if this is the last cpu in the package
  318. * before doing further cleanups.
  319. */
  320. if (lastcpu) {
  321. struct thermal_zone_device *tzone = zonedev->tzone;
  322. /*
  323. * We must protect against a work function calling
  324. * thermal_zone_update, after/while unregister. We null out
  325. * the pointer under the zone mutex, so the worker function
  326. * won't try to call.
  327. */
  328. mutex_lock(&thermal_zone_mutex);
  329. zonedev->tzone = NULL;
  330. mutex_unlock(&thermal_zone_mutex);
  331. thermal_zone_device_unregister(tzone);
  332. }
  333. /* Protect against work and interrupts */
  334. raw_spin_lock_irq(&pkg_temp_lock);
  335. /*
  336. * Check whether this cpu was the current target and store the new
  337. * one. When we drop the lock, then the interrupt notify function
  338. * will see the new target.
  339. */
  340. was_target = zonedev->cpu == cpu;
  341. zonedev->cpu = target;
  342. /*
  343. * If this is the last CPU in the package remove the package
  344. * reference from the array and restore the interrupt MSR. When we
  345. * drop the lock neither the interrupt notify function nor the
  346. * worker will see the package anymore.
  347. */
  348. if (lastcpu) {
  349. zones[topology_logical_die_id(cpu)] = NULL;
  350. /* After this point nothing touches the MSR anymore. */
  351. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  352. zonedev->msr_pkg_therm_low, zonedev->msr_pkg_therm_high);
  353. }
  354. /*
  355. * Check whether there is work scheduled and whether the work is
  356. * targeted at the outgoing CPU.
  357. */
  358. if (zonedev->work_scheduled && was_target) {
  359. /*
  360. * To cancel the work we need to drop the lock, otherwise
  361. * we might deadlock if the work needs to be flushed.
  362. */
  363. raw_spin_unlock_irq(&pkg_temp_lock);
  364. cancel_delayed_work_sync(&zonedev->work);
  365. raw_spin_lock_irq(&pkg_temp_lock);
  366. /*
  367. * If this is not the last cpu in the package and the work
  368. * did not run after we dropped the lock above, then we
  369. * need to reschedule the work, otherwise the interrupt
  370. * stays disabled forever.
  371. */
  372. if (!lastcpu && zonedev->work_scheduled)
  373. pkg_thermal_schedule_work(target, &zonedev->work);
  374. }
  375. raw_spin_unlock_irq(&pkg_temp_lock);
  376. /* Final cleanup if this is the last cpu */
  377. if (lastcpu)
  378. kfree(zonedev);
  379. return 0;
  380. }
  381. static int pkg_thermal_cpu_online(unsigned int cpu)
  382. {
  383. struct zone_device *zonedev = pkg_temp_thermal_get_dev(cpu);
  384. struct cpuinfo_x86 *c = &cpu_data(cpu);
  385. /* Paranoia check */
  386. if (!cpu_has(c, X86_FEATURE_DTHERM) || !cpu_has(c, X86_FEATURE_PTS))
  387. return -ENODEV;
  388. /* If the package exists, nothing to do */
  389. if (zonedev) {
  390. cpumask_set_cpu(cpu, &zonedev->cpumask);
  391. return 0;
  392. }
  393. return pkg_temp_thermal_device_add(cpu);
  394. }
  395. static const struct x86_cpu_id __initconst pkg_temp_thermal_ids[] = {
  396. X86_MATCH_VENDOR_FEATURE(INTEL, X86_FEATURE_PTS, NULL),
  397. {}
  398. };
  399. MODULE_DEVICE_TABLE(x86cpu, pkg_temp_thermal_ids);
  400. static int __init pkg_temp_thermal_init(void)
  401. {
  402. int ret;
  403. if (!x86_match_cpu(pkg_temp_thermal_ids))
  404. return -ENODEV;
  405. max_id = topology_max_packages() * topology_max_dies_per_package();
  406. zones = kcalloc(max_id, sizeof(struct zone_device *),
  407. GFP_KERNEL);
  408. if (!zones)
  409. return -ENOMEM;
  410. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "thermal/x86_pkg:online",
  411. pkg_thermal_cpu_online, pkg_thermal_cpu_offline);
  412. if (ret < 0)
  413. goto err;
  414. /* Store the state for module exit */
  415. pkg_thermal_hp_state = ret;
  416. platform_thermal_package_notify = pkg_thermal_notify;
  417. platform_thermal_package_rate_control = pkg_thermal_rate_control;
  418. /* Don't care if it fails */
  419. pkg_temp_debugfs_init();
  420. return 0;
  421. err:
  422. kfree(zones);
  423. return ret;
  424. }
  425. module_init(pkg_temp_thermal_init)
  426. static void __exit pkg_temp_thermal_exit(void)
  427. {
  428. platform_thermal_package_notify = NULL;
  429. platform_thermal_package_rate_control = NULL;
  430. cpuhp_remove_state(pkg_thermal_hp_state);
  431. debugfs_remove_recursive(debugfs);
  432. kfree(zones);
  433. }
  434. module_exit(pkg_temp_thermal_exit)
  435. MODULE_IMPORT_NS(INTEL_TCC);
  436. MODULE_DESCRIPTION("X86 PKG TEMP Thermal Driver");
  437. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  438. MODULE_LICENSE("GPL v2");