x86_pkg_temp_thermal.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * x86_pkg_temp_thermal driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.
  16. *
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/err.h>
  22. #include <linux/param.h>
  23. #include <linux/device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/cpu.h>
  26. #include <linux/smp.h>
  27. #include <linux/slab.h>
  28. #include <linux/pm.h>
  29. #include <linux/thermal.h>
  30. #include <linux/debugfs.h>
  31. #include <asm/cpu_device_id.h>
  32. #include <asm/mce.h>
  33. /*
  34. * Rate control delay: Idea is to introduce denounce effect
  35. * This should be long enough to avoid reduce events, when
  36. * threshold is set to a temperature, which is constantly
  37. * violated, but at the short enough to take any action.
  38. * The action can be remove threshold or change it to next
  39. * interesting setting. Based on experiments, in around
  40. * every 5 seconds under load will give us a significant
  41. * temperature change.
  42. */
  43. #define PKG_TEMP_THERMAL_NOTIFY_DELAY 5000
  44. static int notify_delay_ms = PKG_TEMP_THERMAL_NOTIFY_DELAY;
  45. module_param(notify_delay_ms, int, 0644);
  46. MODULE_PARM_DESC(notify_delay_ms,
  47. "User space notification delay in milli seconds.");
  48. /* Number of trip points in thermal zone. Currently it can't
  49. * be more than 2. MSR can allow setting and getting notifications
  50. * for only 2 thresholds. This define enforces this, if there
  51. * is some wrong values returned by cpuid for number of thresholds.
  52. */
  53. #define MAX_NUMBER_OF_TRIPS 2
  54. /* Limit number of package temp zones */
  55. #define MAX_PKG_TEMP_ZONE_IDS 256
  56. struct phy_dev_entry {
  57. struct list_head list;
  58. u16 phys_proc_id;
  59. u16 first_cpu;
  60. u32 tj_max;
  61. int ref_cnt;
  62. u32 start_pkg_therm_low;
  63. u32 start_pkg_therm_high;
  64. struct thermal_zone_device *tzone;
  65. };
  66. static struct thermal_zone_params pkg_temp_tz_params = {
  67. .no_hwmon = true,
  68. };
  69. /* List maintaining number of package instances */
  70. static LIST_HEAD(phy_dev_list);
  71. static DEFINE_MUTEX(phy_dev_list_mutex);
  72. /* Interrupt to work function schedule queue */
  73. static DEFINE_PER_CPU(struct delayed_work, pkg_temp_thermal_threshold_work);
  74. /* To track if the work is already scheduled on a package */
  75. static u8 *pkg_work_scheduled;
  76. /* Spin lock to prevent races with pkg_work_scheduled */
  77. static spinlock_t pkg_work_lock;
  78. static u16 max_phy_id;
  79. /* Debug counters to show using debugfs */
  80. static struct dentry *debugfs;
  81. static unsigned int pkg_interrupt_cnt;
  82. static unsigned int pkg_work_cnt;
  83. static int pkg_temp_debugfs_init(void)
  84. {
  85. struct dentry *d;
  86. debugfs = debugfs_create_dir("pkg_temp_thermal", NULL);
  87. if (!debugfs)
  88. return -ENOENT;
  89. d = debugfs_create_u32("pkg_thres_interrupt", S_IRUGO, debugfs,
  90. (u32 *)&pkg_interrupt_cnt);
  91. if (!d)
  92. goto err_out;
  93. d = debugfs_create_u32("pkg_thres_work", S_IRUGO, debugfs,
  94. (u32 *)&pkg_work_cnt);
  95. if (!d)
  96. goto err_out;
  97. return 0;
  98. err_out:
  99. debugfs_remove_recursive(debugfs);
  100. return -ENOENT;
  101. }
  102. static struct phy_dev_entry
  103. *pkg_temp_thermal_get_phy_entry(unsigned int cpu)
  104. {
  105. u16 phys_proc_id = topology_physical_package_id(cpu);
  106. struct phy_dev_entry *phy_ptr;
  107. mutex_lock(&phy_dev_list_mutex);
  108. list_for_each_entry(phy_ptr, &phy_dev_list, list)
  109. if (phy_ptr->phys_proc_id == phys_proc_id) {
  110. mutex_unlock(&phy_dev_list_mutex);
  111. return phy_ptr;
  112. }
  113. mutex_unlock(&phy_dev_list_mutex);
  114. return NULL;
  115. }
  116. /*
  117. * tj-max is is interesting because threshold is set relative to this
  118. * temperature.
  119. */
  120. static int get_tj_max(int cpu, u32 *tj_max)
  121. {
  122. u32 eax, edx;
  123. u32 val;
  124. int err;
  125. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  126. if (err)
  127. goto err_ret;
  128. else {
  129. val = (eax >> 16) & 0xff;
  130. if (val)
  131. *tj_max = val * 1000;
  132. else {
  133. err = -EINVAL;
  134. goto err_ret;
  135. }
  136. }
  137. return 0;
  138. err_ret:
  139. *tj_max = 0;
  140. return err;
  141. }
  142. static int sys_get_curr_temp(struct thermal_zone_device *tzd, int *temp)
  143. {
  144. u32 eax, edx;
  145. struct phy_dev_entry *phy_dev_entry;
  146. phy_dev_entry = tzd->devdata;
  147. rdmsr_on_cpu(phy_dev_entry->first_cpu, MSR_IA32_PACKAGE_THERM_STATUS,
  148. &eax, &edx);
  149. if (eax & 0x80000000) {
  150. *temp = phy_dev_entry->tj_max -
  151. ((eax >> 16) & 0x7f) * 1000;
  152. pr_debug("sys_get_curr_temp %d\n", *temp);
  153. return 0;
  154. }
  155. return -EINVAL;
  156. }
  157. static int sys_get_trip_temp(struct thermal_zone_device *tzd,
  158. int trip, int *temp)
  159. {
  160. u32 eax, edx;
  161. struct phy_dev_entry *phy_dev_entry;
  162. u32 mask, shift;
  163. unsigned long thres_reg_value;
  164. int ret;
  165. if (trip >= MAX_NUMBER_OF_TRIPS)
  166. return -EINVAL;
  167. phy_dev_entry = tzd->devdata;
  168. if (trip) {
  169. mask = THERM_MASK_THRESHOLD1;
  170. shift = THERM_SHIFT_THRESHOLD1;
  171. } else {
  172. mask = THERM_MASK_THRESHOLD0;
  173. shift = THERM_SHIFT_THRESHOLD0;
  174. }
  175. ret = rdmsr_on_cpu(phy_dev_entry->first_cpu,
  176. MSR_IA32_PACKAGE_THERM_INTERRUPT, &eax, &edx);
  177. if (ret < 0)
  178. return -EINVAL;
  179. thres_reg_value = (eax & mask) >> shift;
  180. if (thres_reg_value)
  181. *temp = phy_dev_entry->tj_max - thres_reg_value * 1000;
  182. else
  183. *temp = 0;
  184. pr_debug("sys_get_trip_temp %d\n", *temp);
  185. return 0;
  186. }
  187. static int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip,
  188. int temp)
  189. {
  190. u32 l, h;
  191. struct phy_dev_entry *phy_dev_entry;
  192. u32 mask, shift, intr;
  193. int ret;
  194. phy_dev_entry = tzd->devdata;
  195. if (trip >= MAX_NUMBER_OF_TRIPS || temp >= phy_dev_entry->tj_max)
  196. return -EINVAL;
  197. ret = rdmsr_on_cpu(phy_dev_entry->first_cpu,
  198. MSR_IA32_PACKAGE_THERM_INTERRUPT,
  199. &l, &h);
  200. if (ret < 0)
  201. return -EINVAL;
  202. if (trip) {
  203. mask = THERM_MASK_THRESHOLD1;
  204. shift = THERM_SHIFT_THRESHOLD1;
  205. intr = THERM_INT_THRESHOLD1_ENABLE;
  206. } else {
  207. mask = THERM_MASK_THRESHOLD0;
  208. shift = THERM_SHIFT_THRESHOLD0;
  209. intr = THERM_INT_THRESHOLD0_ENABLE;
  210. }
  211. l &= ~mask;
  212. /*
  213. * When users space sets a trip temperature == 0, which is indication
  214. * that, it is no longer interested in receiving notifications.
  215. */
  216. if (!temp)
  217. l &= ~intr;
  218. else {
  219. l |= (phy_dev_entry->tj_max - temp)/1000 << shift;
  220. l |= intr;
  221. }
  222. return wrmsr_on_cpu(phy_dev_entry->first_cpu,
  223. MSR_IA32_PACKAGE_THERM_INTERRUPT,
  224. l, h);
  225. }
  226. static int sys_get_trip_type(struct thermal_zone_device *thermal,
  227. int trip, enum thermal_trip_type *type)
  228. {
  229. *type = THERMAL_TRIP_PASSIVE;
  230. return 0;
  231. }
  232. /* Thermal zone callback registry */
  233. static struct thermal_zone_device_ops tzone_ops = {
  234. .get_temp = sys_get_curr_temp,
  235. .get_trip_temp = sys_get_trip_temp,
  236. .get_trip_type = sys_get_trip_type,
  237. .set_trip_temp = sys_set_trip_temp,
  238. };
  239. static bool pkg_temp_thermal_platform_thermal_rate_control(void)
  240. {
  241. return true;
  242. }
  243. /* Enable threshold interrupt on local package/cpu */
  244. static inline void enable_pkg_thres_interrupt(void)
  245. {
  246. u32 l, h;
  247. u8 thres_0, thres_1;
  248. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  249. /* only enable/disable if it had valid threshold value */
  250. thres_0 = (l & THERM_MASK_THRESHOLD0) >> THERM_SHIFT_THRESHOLD0;
  251. thres_1 = (l & THERM_MASK_THRESHOLD1) >> THERM_SHIFT_THRESHOLD1;
  252. if (thres_0)
  253. l |= THERM_INT_THRESHOLD0_ENABLE;
  254. if (thres_1)
  255. l |= THERM_INT_THRESHOLD1_ENABLE;
  256. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  257. }
  258. /* Disable threshold interrupt on local package/cpu */
  259. static inline void disable_pkg_thres_interrupt(void)
  260. {
  261. u32 l, h;
  262. rdmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT, l, h);
  263. wrmsr(MSR_IA32_PACKAGE_THERM_INTERRUPT,
  264. l & (~THERM_INT_THRESHOLD0_ENABLE) &
  265. (~THERM_INT_THRESHOLD1_ENABLE), h);
  266. }
  267. static void pkg_temp_thermal_threshold_work_fn(struct work_struct *work)
  268. {
  269. __u64 msr_val;
  270. int cpu = smp_processor_id();
  271. int phy_id = topology_physical_package_id(cpu);
  272. struct phy_dev_entry *phdev = pkg_temp_thermal_get_phy_entry(cpu);
  273. bool notify = false;
  274. unsigned long flags;
  275. if (!phdev)
  276. return;
  277. spin_lock_irqsave(&pkg_work_lock, flags);
  278. ++pkg_work_cnt;
  279. if (unlikely(phy_id > max_phy_id)) {
  280. spin_unlock_irqrestore(&pkg_work_lock, flags);
  281. return;
  282. }
  283. pkg_work_scheduled[phy_id] = 0;
  284. spin_unlock_irqrestore(&pkg_work_lock, flags);
  285. rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
  286. if (msr_val & THERM_LOG_THRESHOLD0) {
  287. wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS,
  288. msr_val & ~THERM_LOG_THRESHOLD0);
  289. notify = true;
  290. }
  291. if (msr_val & THERM_LOG_THRESHOLD1) {
  292. wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS,
  293. msr_val & ~THERM_LOG_THRESHOLD1);
  294. notify = true;
  295. }
  296. enable_pkg_thres_interrupt();
  297. if (notify) {
  298. pr_debug("thermal_zone_device_update\n");
  299. thermal_zone_device_update(phdev->tzone,
  300. THERMAL_EVENT_UNSPECIFIED);
  301. }
  302. }
  303. static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  304. {
  305. unsigned long flags;
  306. int cpu = smp_processor_id();
  307. int phy_id = topology_physical_package_id(cpu);
  308. /*
  309. * When a package is in interrupted state, all CPU's in that package
  310. * are in the same interrupt state. So scheduling on any one CPU in
  311. * the package is enough and simply return for others.
  312. */
  313. spin_lock_irqsave(&pkg_work_lock, flags);
  314. ++pkg_interrupt_cnt;
  315. if (unlikely(phy_id > max_phy_id) || unlikely(!pkg_work_scheduled) ||
  316. pkg_work_scheduled[phy_id]) {
  317. disable_pkg_thres_interrupt();
  318. spin_unlock_irqrestore(&pkg_work_lock, flags);
  319. return -EINVAL;
  320. }
  321. pkg_work_scheduled[phy_id] = 1;
  322. spin_unlock_irqrestore(&pkg_work_lock, flags);
  323. disable_pkg_thres_interrupt();
  324. schedule_delayed_work_on(cpu,
  325. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  326. msecs_to_jiffies(notify_delay_ms));
  327. return 0;
  328. }
  329. static int find_siblings_cpu(int cpu)
  330. {
  331. int i;
  332. int id = topology_physical_package_id(cpu);
  333. for_each_online_cpu(i)
  334. if (i != cpu && topology_physical_package_id(i) == id)
  335. return i;
  336. return 0;
  337. }
  338. static int pkg_temp_thermal_device_add(unsigned int cpu)
  339. {
  340. int err;
  341. u32 tj_max;
  342. struct phy_dev_entry *phy_dev_entry;
  343. int thres_count;
  344. u32 eax, ebx, ecx, edx;
  345. u8 *temp;
  346. unsigned long flags;
  347. cpuid(6, &eax, &ebx, &ecx, &edx);
  348. thres_count = ebx & 0x07;
  349. if (!thres_count)
  350. return -ENODEV;
  351. if (topology_physical_package_id(cpu) > MAX_PKG_TEMP_ZONE_IDS)
  352. return -ENODEV;
  353. thres_count = clamp_val(thres_count, 0, MAX_NUMBER_OF_TRIPS);
  354. err = get_tj_max(cpu, &tj_max);
  355. if (err)
  356. goto err_ret;
  357. mutex_lock(&phy_dev_list_mutex);
  358. phy_dev_entry = kzalloc(sizeof(*phy_dev_entry), GFP_KERNEL);
  359. if (!phy_dev_entry) {
  360. err = -ENOMEM;
  361. goto err_ret_unlock;
  362. }
  363. spin_lock_irqsave(&pkg_work_lock, flags);
  364. if (topology_physical_package_id(cpu) > max_phy_id)
  365. max_phy_id = topology_physical_package_id(cpu);
  366. temp = krealloc(pkg_work_scheduled,
  367. (max_phy_id+1) * sizeof(u8), GFP_ATOMIC);
  368. if (!temp) {
  369. spin_unlock_irqrestore(&pkg_work_lock, flags);
  370. err = -ENOMEM;
  371. goto err_ret_free;
  372. }
  373. pkg_work_scheduled = temp;
  374. pkg_work_scheduled[topology_physical_package_id(cpu)] = 0;
  375. spin_unlock_irqrestore(&pkg_work_lock, flags);
  376. phy_dev_entry->phys_proc_id = topology_physical_package_id(cpu);
  377. phy_dev_entry->first_cpu = cpu;
  378. phy_dev_entry->tj_max = tj_max;
  379. phy_dev_entry->ref_cnt = 1;
  380. phy_dev_entry->tzone = thermal_zone_device_register("x86_pkg_temp",
  381. thres_count,
  382. (thres_count == MAX_NUMBER_OF_TRIPS) ?
  383. 0x03 : 0x01,
  384. phy_dev_entry, &tzone_ops, &pkg_temp_tz_params, 0, 0);
  385. if (IS_ERR(phy_dev_entry->tzone)) {
  386. err = PTR_ERR(phy_dev_entry->tzone);
  387. goto err_ret_free;
  388. }
  389. /* Store MSR value for package thermal interrupt, to restore at exit */
  390. rdmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
  391. &phy_dev_entry->start_pkg_therm_low,
  392. &phy_dev_entry->start_pkg_therm_high);
  393. list_add_tail(&phy_dev_entry->list, &phy_dev_list);
  394. pr_debug("pkg_temp_thermal_device_add :phy_id %d cpu %d\n",
  395. phy_dev_entry->phys_proc_id, cpu);
  396. mutex_unlock(&phy_dev_list_mutex);
  397. return 0;
  398. err_ret_free:
  399. kfree(phy_dev_entry);
  400. err_ret_unlock:
  401. mutex_unlock(&phy_dev_list_mutex);
  402. err_ret:
  403. return err;
  404. }
  405. static int pkg_temp_thermal_device_remove(unsigned int cpu)
  406. {
  407. struct phy_dev_entry *n;
  408. u16 phys_proc_id = topology_physical_package_id(cpu);
  409. struct phy_dev_entry *phdev =
  410. pkg_temp_thermal_get_phy_entry(cpu);
  411. if (!phdev)
  412. return -ENODEV;
  413. mutex_lock(&phy_dev_list_mutex);
  414. /* If we are loosing the first cpu for this package, we need change */
  415. if (phdev->first_cpu == cpu) {
  416. phdev->first_cpu = find_siblings_cpu(cpu);
  417. pr_debug("thermal_device_remove: first cpu switched %d\n",
  418. phdev->first_cpu);
  419. }
  420. /*
  421. * It is possible that no siblings left as this was the last cpu
  422. * going offline. We don't need to worry about this assignment
  423. * as the phydev entry will be removed in this case and
  424. * thermal zone is removed.
  425. */
  426. --phdev->ref_cnt;
  427. pr_debug("thermal_device_remove: pkg: %d cpu %d ref_cnt %d\n",
  428. phys_proc_id, cpu, phdev->ref_cnt);
  429. if (!phdev->ref_cnt)
  430. list_for_each_entry_safe(phdev, n, &phy_dev_list, list) {
  431. if (phdev->phys_proc_id == phys_proc_id) {
  432. thermal_zone_device_unregister(phdev->tzone);
  433. /*
  434. * Restore original MSR value for package
  435. * thermal interrupt.
  436. */
  437. wrmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
  438. phdev->start_pkg_therm_low,
  439. phdev->start_pkg_therm_high);
  440. list_del(&phdev->list);
  441. kfree(phdev);
  442. break;
  443. }
  444. }
  445. mutex_unlock(&phy_dev_list_mutex);
  446. return 0;
  447. }
  448. static int get_core_online(unsigned int cpu)
  449. {
  450. struct cpuinfo_x86 *c = &cpu_data(cpu);
  451. struct phy_dev_entry *phdev = pkg_temp_thermal_get_phy_entry(cpu);
  452. /* Check if there is already an instance for this package */
  453. if (!phdev) {
  454. if (!cpu_has(c, X86_FEATURE_DTHERM) ||
  455. !cpu_has(c, X86_FEATURE_PTS))
  456. return -ENODEV;
  457. if (pkg_temp_thermal_device_add(cpu))
  458. return -ENODEV;
  459. } else {
  460. mutex_lock(&phy_dev_list_mutex);
  461. ++phdev->ref_cnt;
  462. pr_debug("get_core_online: cpu %d ref_cnt %d\n",
  463. cpu, phdev->ref_cnt);
  464. mutex_unlock(&phy_dev_list_mutex);
  465. }
  466. INIT_DELAYED_WORK(&per_cpu(pkg_temp_thermal_threshold_work, cpu),
  467. pkg_temp_thermal_threshold_work_fn);
  468. pr_debug("get_core_online: cpu %d successful\n", cpu);
  469. return 0;
  470. }
  471. static void put_core_offline(unsigned int cpu)
  472. {
  473. if (!pkg_temp_thermal_device_remove(cpu))
  474. cancel_delayed_work_sync(
  475. &per_cpu(pkg_temp_thermal_threshold_work, cpu));
  476. pr_debug("put_core_offline: cpu %d\n", cpu);
  477. }
  478. static int pkg_temp_thermal_cpu_callback(struct notifier_block *nfb,
  479. unsigned long action, void *hcpu)
  480. {
  481. unsigned int cpu = (unsigned long) hcpu;
  482. switch (action & ~CPU_TASKS_FROZEN) {
  483. case CPU_ONLINE:
  484. case CPU_DOWN_FAILED:
  485. get_core_online(cpu);
  486. break;
  487. case CPU_DOWN_PREPARE:
  488. put_core_offline(cpu);
  489. break;
  490. }
  491. return NOTIFY_OK;
  492. }
  493. static struct notifier_block pkg_temp_thermal_notifier __refdata = {
  494. .notifier_call = pkg_temp_thermal_cpu_callback,
  495. };
  496. static const struct x86_cpu_id __initconst pkg_temp_thermal_ids[] = {
  497. { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_PTS },
  498. {}
  499. };
  500. MODULE_DEVICE_TABLE(x86cpu, pkg_temp_thermal_ids);
  501. static int __init pkg_temp_thermal_init(void)
  502. {
  503. int i;
  504. if (!x86_match_cpu(pkg_temp_thermal_ids))
  505. return -ENODEV;
  506. spin_lock_init(&pkg_work_lock);
  507. platform_thermal_package_notify =
  508. pkg_temp_thermal_platform_thermal_notify;
  509. platform_thermal_package_rate_control =
  510. pkg_temp_thermal_platform_thermal_rate_control;
  511. cpu_notifier_register_begin();
  512. for_each_online_cpu(i)
  513. if (get_core_online(i))
  514. goto err_ret;
  515. __register_hotcpu_notifier(&pkg_temp_thermal_notifier);
  516. cpu_notifier_register_done();
  517. pkg_temp_debugfs_init(); /* Don't care if fails */
  518. return 0;
  519. err_ret:
  520. for_each_online_cpu(i)
  521. put_core_offline(i);
  522. cpu_notifier_register_done();
  523. kfree(pkg_work_scheduled);
  524. platform_thermal_package_notify = NULL;
  525. platform_thermal_package_rate_control = NULL;
  526. return -ENODEV;
  527. }
  528. static void __exit pkg_temp_thermal_exit(void)
  529. {
  530. struct phy_dev_entry *phdev, *n;
  531. int i;
  532. cpu_notifier_register_begin();
  533. __unregister_hotcpu_notifier(&pkg_temp_thermal_notifier);
  534. mutex_lock(&phy_dev_list_mutex);
  535. list_for_each_entry_safe(phdev, n, &phy_dev_list, list) {
  536. /* Retore old MSR value for package thermal interrupt */
  537. wrmsr_on_cpu(phdev->first_cpu,
  538. MSR_IA32_PACKAGE_THERM_INTERRUPT,
  539. phdev->start_pkg_therm_low,
  540. phdev->start_pkg_therm_high);
  541. thermal_zone_device_unregister(phdev->tzone);
  542. list_del(&phdev->list);
  543. kfree(phdev);
  544. }
  545. mutex_unlock(&phy_dev_list_mutex);
  546. platform_thermal_package_notify = NULL;
  547. platform_thermal_package_rate_control = NULL;
  548. for_each_online_cpu(i)
  549. cancel_delayed_work_sync(
  550. &per_cpu(pkg_temp_thermal_threshold_work, i));
  551. cpu_notifier_register_done();
  552. kfree(pkg_work_scheduled);
  553. debugfs_remove_recursive(debugfs);
  554. }
  555. module_init(pkg_temp_thermal_init)
  556. module_exit(pkg_temp_thermal_exit)
  557. MODULE_DESCRIPTION("X86 PKG TEMP Thermal Driver");
  558. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  559. MODULE_LICENSE("GPL v2");