x86_pkg_temp_thermal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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, unsigned long *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 %ld\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, unsigned long *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 %ld\n", *temp);
  185. return 0;
  186. }
  187. static int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip,
  188. unsigned long 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. enable_pkg_thres_interrupt();
  286. rdmsrl(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
  287. if (msr_val & THERM_LOG_THRESHOLD0) {
  288. wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS,
  289. msr_val & ~THERM_LOG_THRESHOLD0);
  290. notify = true;
  291. }
  292. if (msr_val & THERM_LOG_THRESHOLD1) {
  293. wrmsrl(MSR_IA32_PACKAGE_THERM_STATUS,
  294. msr_val & ~THERM_LOG_THRESHOLD1);
  295. notify = true;
  296. }
  297. if (notify) {
  298. pr_debug("thermal_zone_device_update\n");
  299. thermal_zone_device_update(phdev->tzone);
  300. }
  301. }
  302. static int pkg_temp_thermal_platform_thermal_notify(__u64 msr_val)
  303. {
  304. unsigned long flags;
  305. int cpu = smp_processor_id();
  306. int phy_id = topology_physical_package_id(cpu);
  307. /*
  308. * When a package is in interrupted state, all CPU's in that package
  309. * are in the same interrupt state. So scheduling on any one CPU in
  310. * the package is enough and simply return for others.
  311. */
  312. spin_lock_irqsave(&pkg_work_lock, flags);
  313. ++pkg_interrupt_cnt;
  314. if (unlikely(phy_id > max_phy_id) || unlikely(!pkg_work_scheduled) ||
  315. pkg_work_scheduled[phy_id]) {
  316. disable_pkg_thres_interrupt();
  317. spin_unlock_irqrestore(&pkg_work_lock, flags);
  318. return -EINVAL;
  319. }
  320. pkg_work_scheduled[phy_id] = 1;
  321. spin_unlock_irqrestore(&pkg_work_lock, flags);
  322. disable_pkg_thres_interrupt();
  323. schedule_delayed_work_on(cpu,
  324. &per_cpu(pkg_temp_thermal_threshold_work, cpu),
  325. msecs_to_jiffies(notify_delay_ms));
  326. return 0;
  327. }
  328. static int find_siblings_cpu(int cpu)
  329. {
  330. int i;
  331. int id = topology_physical_package_id(cpu);
  332. for_each_online_cpu(i)
  333. if (i != cpu && topology_physical_package_id(i) == id)
  334. return i;
  335. return 0;
  336. }
  337. static int pkg_temp_thermal_device_add(unsigned int cpu)
  338. {
  339. int err;
  340. u32 tj_max;
  341. struct phy_dev_entry *phy_dev_entry;
  342. int thres_count;
  343. u32 eax, ebx, ecx, edx;
  344. u8 *temp;
  345. unsigned long flags;
  346. cpuid(6, &eax, &ebx, &ecx, &edx);
  347. thres_count = ebx & 0x07;
  348. if (!thres_count)
  349. return -ENODEV;
  350. if (topology_physical_package_id(cpu) > MAX_PKG_TEMP_ZONE_IDS)
  351. return -ENODEV;
  352. thres_count = clamp_val(thres_count, 0, MAX_NUMBER_OF_TRIPS);
  353. err = get_tj_max(cpu, &tj_max);
  354. if (err)
  355. goto err_ret;
  356. mutex_lock(&phy_dev_list_mutex);
  357. phy_dev_entry = kzalloc(sizeof(*phy_dev_entry), GFP_KERNEL);
  358. if (!phy_dev_entry) {
  359. err = -ENOMEM;
  360. goto err_ret_unlock;
  361. }
  362. spin_lock_irqsave(&pkg_work_lock, flags);
  363. if (topology_physical_package_id(cpu) > max_phy_id)
  364. max_phy_id = topology_physical_package_id(cpu);
  365. temp = krealloc(pkg_work_scheduled,
  366. (max_phy_id+1) * sizeof(u8), GFP_ATOMIC);
  367. if (!temp) {
  368. spin_unlock_irqrestore(&pkg_work_lock, flags);
  369. err = -ENOMEM;
  370. goto err_ret_free;
  371. }
  372. pkg_work_scheduled = temp;
  373. pkg_work_scheduled[topology_physical_package_id(cpu)] = 0;
  374. spin_unlock_irqrestore(&pkg_work_lock, flags);
  375. phy_dev_entry->phys_proc_id = topology_physical_package_id(cpu);
  376. phy_dev_entry->first_cpu = cpu;
  377. phy_dev_entry->tj_max = tj_max;
  378. phy_dev_entry->ref_cnt = 1;
  379. phy_dev_entry->tzone = thermal_zone_device_register("x86_pkg_temp",
  380. thres_count,
  381. (thres_count == MAX_NUMBER_OF_TRIPS) ?
  382. 0x03 : 0x01,
  383. phy_dev_entry, &tzone_ops, &pkg_temp_tz_params, 0, 0);
  384. if (IS_ERR(phy_dev_entry->tzone)) {
  385. err = PTR_ERR(phy_dev_entry->tzone);
  386. goto err_ret_free;
  387. }
  388. /* Store MSR value for package thermal interrupt, to restore at exit */
  389. rdmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT,
  390. &phy_dev_entry->start_pkg_therm_low,
  391. &phy_dev_entry->start_pkg_therm_high);
  392. list_add_tail(&phy_dev_entry->list, &phy_dev_list);
  393. pr_debug("pkg_temp_thermal_device_add :phy_id %d cpu %d\n",
  394. phy_dev_entry->phys_proc_id, cpu);
  395. mutex_unlock(&phy_dev_list_mutex);
  396. return 0;
  397. err_ret_free:
  398. kfree(phy_dev_entry);
  399. err_ret_unlock:
  400. mutex_unlock(&phy_dev_list_mutex);
  401. err_ret:
  402. return err;
  403. }
  404. static int pkg_temp_thermal_device_remove(unsigned int cpu)
  405. {
  406. struct phy_dev_entry *n;
  407. u16 phys_proc_id = topology_physical_package_id(cpu);
  408. struct phy_dev_entry *phdev =
  409. pkg_temp_thermal_get_phy_entry(cpu);
  410. if (!phdev)
  411. return -ENODEV;
  412. mutex_lock(&phy_dev_list_mutex);
  413. /* If we are loosing the first cpu for this package, we need change */
  414. if (phdev->first_cpu == cpu) {
  415. phdev->first_cpu = find_siblings_cpu(cpu);
  416. pr_debug("thermal_device_remove: first cpu switched %d\n",
  417. phdev->first_cpu);
  418. }
  419. /*
  420. * It is possible that no siblings left as this was the last cpu
  421. * going offline. We don't need to worry about this assignment
  422. * as the phydev entry will be removed in this case and
  423. * thermal zone is removed.
  424. */
  425. --phdev->ref_cnt;
  426. pr_debug("thermal_device_remove: pkg: %d cpu %d ref_cnt %d\n",
  427. phys_proc_id, cpu, phdev->ref_cnt);
  428. if (!phdev->ref_cnt)
  429. list_for_each_entry_safe(phdev, n, &phy_dev_list, list) {
  430. if (phdev->phys_proc_id == phys_proc_id) {
  431. thermal_zone_device_unregister(phdev->tzone);
  432. list_del(&phdev->list);
  433. kfree(phdev);
  434. break;
  435. }
  436. }
  437. mutex_unlock(&phy_dev_list_mutex);
  438. return 0;
  439. }
  440. static int get_core_online(unsigned int cpu)
  441. {
  442. struct cpuinfo_x86 *c = &cpu_data(cpu);
  443. struct phy_dev_entry *phdev = pkg_temp_thermal_get_phy_entry(cpu);
  444. /* Check if there is already an instance for this package */
  445. if (!phdev) {
  446. if (!cpu_has(c, X86_FEATURE_DTHERM) ||
  447. !cpu_has(c, X86_FEATURE_PTS))
  448. return -ENODEV;
  449. if (pkg_temp_thermal_device_add(cpu))
  450. return -ENODEV;
  451. } else {
  452. mutex_lock(&phy_dev_list_mutex);
  453. ++phdev->ref_cnt;
  454. pr_debug("get_core_online: cpu %d ref_cnt %d\n",
  455. cpu, phdev->ref_cnt);
  456. mutex_unlock(&phy_dev_list_mutex);
  457. }
  458. INIT_DELAYED_WORK(&per_cpu(pkg_temp_thermal_threshold_work, cpu),
  459. pkg_temp_thermal_threshold_work_fn);
  460. pr_debug("get_core_online: cpu %d successful\n", cpu);
  461. return 0;
  462. }
  463. static void put_core_offline(unsigned int cpu)
  464. {
  465. if (!pkg_temp_thermal_device_remove(cpu))
  466. cancel_delayed_work_sync(
  467. &per_cpu(pkg_temp_thermal_threshold_work, cpu));
  468. pr_debug("put_core_offline: cpu %d\n", cpu);
  469. }
  470. static int pkg_temp_thermal_cpu_callback(struct notifier_block *nfb,
  471. unsigned long action, void *hcpu)
  472. {
  473. unsigned int cpu = (unsigned long) hcpu;
  474. switch (action) {
  475. case CPU_ONLINE:
  476. case CPU_DOWN_FAILED:
  477. get_core_online(cpu);
  478. break;
  479. case CPU_DOWN_PREPARE:
  480. put_core_offline(cpu);
  481. break;
  482. }
  483. return NOTIFY_OK;
  484. }
  485. static struct notifier_block pkg_temp_thermal_notifier __refdata = {
  486. .notifier_call = pkg_temp_thermal_cpu_callback,
  487. };
  488. static const struct x86_cpu_id __initconst pkg_temp_thermal_ids[] = {
  489. { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_PTS },
  490. {}
  491. };
  492. MODULE_DEVICE_TABLE(x86cpu, pkg_temp_thermal_ids);
  493. static int __init pkg_temp_thermal_init(void)
  494. {
  495. int i;
  496. if (!x86_match_cpu(pkg_temp_thermal_ids))
  497. return -ENODEV;
  498. spin_lock_init(&pkg_work_lock);
  499. platform_thermal_package_notify =
  500. pkg_temp_thermal_platform_thermal_notify;
  501. platform_thermal_package_rate_control =
  502. pkg_temp_thermal_platform_thermal_rate_control;
  503. cpu_notifier_register_begin();
  504. for_each_online_cpu(i)
  505. if (get_core_online(i))
  506. goto err_ret;
  507. __register_hotcpu_notifier(&pkg_temp_thermal_notifier);
  508. cpu_notifier_register_done();
  509. pkg_temp_debugfs_init(); /* Don't care if fails */
  510. return 0;
  511. err_ret:
  512. for_each_online_cpu(i)
  513. put_core_offline(i);
  514. cpu_notifier_register_done();
  515. kfree(pkg_work_scheduled);
  516. platform_thermal_package_notify = NULL;
  517. platform_thermal_package_rate_control = NULL;
  518. return -ENODEV;
  519. }
  520. static void __exit pkg_temp_thermal_exit(void)
  521. {
  522. struct phy_dev_entry *phdev, *n;
  523. int i;
  524. cpu_notifier_register_begin();
  525. __unregister_hotcpu_notifier(&pkg_temp_thermal_notifier);
  526. mutex_lock(&phy_dev_list_mutex);
  527. list_for_each_entry_safe(phdev, n, &phy_dev_list, list) {
  528. /* Retore old MSR value for package thermal interrupt */
  529. wrmsr_on_cpu(phdev->first_cpu,
  530. MSR_IA32_PACKAGE_THERM_INTERRUPT,
  531. phdev->start_pkg_therm_low,
  532. phdev->start_pkg_therm_high);
  533. thermal_zone_device_unregister(phdev->tzone);
  534. list_del(&phdev->list);
  535. kfree(phdev);
  536. }
  537. mutex_unlock(&phy_dev_list_mutex);
  538. platform_thermal_package_notify = NULL;
  539. platform_thermal_package_rate_control = NULL;
  540. for_each_online_cpu(i)
  541. cancel_delayed_work_sync(
  542. &per_cpu(pkg_temp_thermal_threshold_work, i));
  543. cpu_notifier_register_done();
  544. kfree(pkg_work_scheduled);
  545. debugfs_remove_recursive(debugfs);
  546. }
  547. module_init(pkg_temp_thermal_init)
  548. module_exit(pkg_temp_thermal_exit)
  549. MODULE_DESCRIPTION("X86 PKG TEMP Thermal Driver");
  550. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  551. MODULE_LICENSE("GPL v2");