intel_soc_dts_thermal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * intel_soc_dts_thermal.c
  3. * Copyright (c) 2014, 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. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/thermal.h>
  20. #include <asm/cpu_device_id.h>
  21. #include <asm/iosf_mbi.h>
  22. #define SOC_DTS_OFFSET_ENABLE 0xB0
  23. #define SOC_DTS_OFFSET_TEMP 0xB1
  24. #define SOC_DTS_OFFSET_PTPS 0xB2
  25. #define SOC_DTS_OFFSET_PTTS 0xB3
  26. #define SOC_DTS_OFFSET_PTTSS 0xB4
  27. #define SOC_DTS_OFFSET_PTMC 0x80
  28. #define SOC_DTS_TE_AUX0 0xB5
  29. #define SOC_DTS_TE_AUX1 0xB6
  30. #define SOC_DTS_AUX0_ENABLE_BIT BIT(0)
  31. #define SOC_DTS_AUX1_ENABLE_BIT BIT(1)
  32. #define SOC_DTS_CPU_MODULE0_ENABLE_BIT BIT(16)
  33. #define SOC_DTS_CPU_MODULE1_ENABLE_BIT BIT(17)
  34. #define SOC_DTS_TE_SCI_ENABLE BIT(9)
  35. #define SOC_DTS_TE_SMI_ENABLE BIT(10)
  36. #define SOC_DTS_TE_MSI_ENABLE BIT(11)
  37. #define SOC_DTS_TE_APICA_ENABLE BIT(14)
  38. #define SOC_DTS_PTMC_APIC_DEASSERT_BIT BIT(4)
  39. /* DTS encoding for TJ MAX temperature */
  40. #define SOC_DTS_TJMAX_ENCODING 0x7F
  41. /* IRQ 86 is a fixed APIC interrupt for BYT DTS Aux threshold notifications */
  42. #define BYT_SOC_DTS_APIC_IRQ 86
  43. /* Only 2 out of 4 is allowed for OSPM */
  44. #define SOC_MAX_DTS_TRIPS 2
  45. /* Mask for two trips in status bits */
  46. #define SOC_DTS_TRIP_MASK 0x03
  47. /* DTS0 and DTS 1 */
  48. #define SOC_MAX_DTS_SENSORS 2
  49. #define CRITICAL_OFFSET_FROM_TJ_MAX 5000
  50. struct soc_sensor_entry {
  51. int id;
  52. u32 tj_max;
  53. u32 temp_mask;
  54. u32 temp_shift;
  55. u32 store_status;
  56. struct thermal_zone_device *tzone;
  57. };
  58. static struct soc_sensor_entry *soc_dts[SOC_MAX_DTS_SENSORS];
  59. static int crit_offset = CRITICAL_OFFSET_FROM_TJ_MAX;
  60. module_param(crit_offset, int, 0644);
  61. MODULE_PARM_DESC(crit_offset,
  62. "Critical Temperature offset from tj max in millidegree Celsius.");
  63. static DEFINE_MUTEX(aux_update_mutex);
  64. static spinlock_t intr_notify_lock;
  65. static int soc_dts_thres_irq;
  66. static int get_tj_max(u32 *tj_max)
  67. {
  68. u32 eax, edx;
  69. u32 val;
  70. int err;
  71. err = rdmsr_safe(MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  72. if (err)
  73. goto err_ret;
  74. else {
  75. val = (eax >> 16) & 0xff;
  76. if (val)
  77. *tj_max = val * 1000;
  78. else {
  79. err = -EINVAL;
  80. goto err_ret;
  81. }
  82. }
  83. return 0;
  84. err_ret:
  85. *tj_max = 0;
  86. return err;
  87. }
  88. static int sys_get_trip_temp(struct thermal_zone_device *tzd,
  89. int trip, unsigned long *temp)
  90. {
  91. int status;
  92. u32 out;
  93. struct soc_sensor_entry *aux_entry;
  94. aux_entry = tzd->devdata;
  95. if (!trip) {
  96. /* Just return the critical temp */
  97. *temp = aux_entry->tj_max - crit_offset;
  98. return 0;
  99. }
  100. mutex_lock(&aux_update_mutex);
  101. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  102. SOC_DTS_OFFSET_PTPS, &out);
  103. mutex_unlock(&aux_update_mutex);
  104. if (status)
  105. return status;
  106. out = (out >> (trip * 8)) & SOC_DTS_TJMAX_ENCODING;
  107. if (!out)
  108. *temp = 0;
  109. else
  110. *temp = aux_entry->tj_max - out * 1000;
  111. return 0;
  112. }
  113. static int update_trip_temp(struct soc_sensor_entry *aux_entry,
  114. int thres_index, unsigned long temp)
  115. {
  116. int status;
  117. u32 temp_out;
  118. u32 out;
  119. u32 store_ptps;
  120. u32 store_ptmc;
  121. u32 store_te_out;
  122. u32 te_out;
  123. u32 int_enable_bit = SOC_DTS_TE_APICA_ENABLE |
  124. SOC_DTS_TE_MSI_ENABLE;
  125. temp_out = (aux_entry->tj_max - temp) / 1000;
  126. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  127. SOC_DTS_OFFSET_PTPS, &store_ptps);
  128. if (status)
  129. return status;
  130. out = (store_ptps & ~(0xFF << (thres_index * 8)));
  131. out |= (temp_out & 0xFF) << (thres_index * 8);
  132. status = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  133. SOC_DTS_OFFSET_PTPS, out);
  134. if (status)
  135. return status;
  136. pr_debug("update_trip_temp PTPS = %x\n", out);
  137. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  138. SOC_DTS_OFFSET_PTMC, &out);
  139. if (status)
  140. goto err_restore_ptps;
  141. store_ptmc = out;
  142. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  143. SOC_DTS_TE_AUX0 + thres_index,
  144. &te_out);
  145. if (status)
  146. goto err_restore_ptmc;
  147. store_te_out = te_out;
  148. /* Enable for CPU module 0 and module 1 */
  149. out |= (SOC_DTS_CPU_MODULE0_ENABLE_BIT |
  150. SOC_DTS_CPU_MODULE1_ENABLE_BIT);
  151. if (temp) {
  152. if (thres_index)
  153. out |= SOC_DTS_AUX1_ENABLE_BIT;
  154. else
  155. out |= SOC_DTS_AUX0_ENABLE_BIT;
  156. te_out |= int_enable_bit;
  157. } else {
  158. if (thres_index)
  159. out &= ~SOC_DTS_AUX1_ENABLE_BIT;
  160. else
  161. out &= ~SOC_DTS_AUX0_ENABLE_BIT;
  162. te_out &= ~int_enable_bit;
  163. }
  164. status = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  165. SOC_DTS_OFFSET_PTMC, out);
  166. if (status)
  167. goto err_restore_te_out;
  168. status = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  169. SOC_DTS_TE_AUX0 + thres_index,
  170. te_out);
  171. if (status)
  172. goto err_restore_te_out;
  173. return 0;
  174. err_restore_te_out:
  175. iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  176. SOC_DTS_OFFSET_PTMC, store_te_out);
  177. err_restore_ptmc:
  178. iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  179. SOC_DTS_OFFSET_PTMC, store_ptmc);
  180. err_restore_ptps:
  181. iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  182. SOC_DTS_OFFSET_PTPS, store_ptps);
  183. /* Nothing we can do if restore fails */
  184. return status;
  185. }
  186. static int sys_set_trip_temp(struct thermal_zone_device *tzd, int trip,
  187. unsigned long temp)
  188. {
  189. struct soc_sensor_entry *aux_entry = tzd->devdata;
  190. int status;
  191. if (temp > (aux_entry->tj_max - crit_offset))
  192. return -EINVAL;
  193. mutex_lock(&aux_update_mutex);
  194. status = update_trip_temp(tzd->devdata, trip, temp);
  195. mutex_unlock(&aux_update_mutex);
  196. return status;
  197. }
  198. static int sys_get_trip_type(struct thermal_zone_device *thermal,
  199. int trip, enum thermal_trip_type *type)
  200. {
  201. if (trip)
  202. *type = THERMAL_TRIP_PASSIVE;
  203. else
  204. *type = THERMAL_TRIP_CRITICAL;
  205. return 0;
  206. }
  207. static int sys_get_curr_temp(struct thermal_zone_device *tzd,
  208. unsigned long *temp)
  209. {
  210. int status;
  211. u32 out;
  212. struct soc_sensor_entry *aux_entry;
  213. aux_entry = tzd->devdata;
  214. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  215. SOC_DTS_OFFSET_TEMP, &out);
  216. if (status)
  217. return status;
  218. out = (out & aux_entry->temp_mask) >> aux_entry->temp_shift;
  219. out -= SOC_DTS_TJMAX_ENCODING;
  220. *temp = aux_entry->tj_max - out * 1000;
  221. return 0;
  222. }
  223. static struct thermal_zone_device_ops tzone_ops = {
  224. .get_temp = sys_get_curr_temp,
  225. .get_trip_temp = sys_get_trip_temp,
  226. .get_trip_type = sys_get_trip_type,
  227. .set_trip_temp = sys_set_trip_temp,
  228. };
  229. static void free_soc_dts(struct soc_sensor_entry *aux_entry)
  230. {
  231. if (aux_entry) {
  232. iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  233. SOC_DTS_OFFSET_ENABLE, aux_entry->store_status);
  234. thermal_zone_device_unregister(aux_entry->tzone);
  235. kfree(aux_entry);
  236. }
  237. }
  238. static int soc_dts_enable(int id)
  239. {
  240. u32 out;
  241. int ret;
  242. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  243. SOC_DTS_OFFSET_ENABLE, &out);
  244. if (ret)
  245. return ret;
  246. if (!(out & BIT(id))) {
  247. out |= BIT(id);
  248. ret = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  249. SOC_DTS_OFFSET_ENABLE, out);
  250. if (ret)
  251. return ret;
  252. }
  253. return ret;
  254. }
  255. static struct soc_sensor_entry *alloc_soc_dts(int id, u32 tj_max)
  256. {
  257. struct soc_sensor_entry *aux_entry;
  258. char name[10];
  259. int err;
  260. aux_entry = kzalloc(sizeof(*aux_entry), GFP_KERNEL);
  261. if (!aux_entry) {
  262. err = -ENOMEM;
  263. return ERR_PTR(-ENOMEM);
  264. }
  265. /* Store status to restor on exit */
  266. err = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  267. SOC_DTS_OFFSET_ENABLE,
  268. &aux_entry->store_status);
  269. if (err)
  270. goto err_ret;
  271. aux_entry->id = id;
  272. aux_entry->tj_max = tj_max;
  273. aux_entry->temp_mask = 0x00FF << (id * 8);
  274. aux_entry->temp_shift = id * 8;
  275. snprintf(name, sizeof(name), "soc_dts%d", id);
  276. aux_entry->tzone = thermal_zone_device_register(name,
  277. SOC_MAX_DTS_TRIPS,
  278. 0x02,
  279. aux_entry, &tzone_ops, NULL, 0, 0);
  280. if (IS_ERR(aux_entry->tzone)) {
  281. err = PTR_ERR(aux_entry->tzone);
  282. goto err_ret;
  283. }
  284. err = soc_dts_enable(id);
  285. if (err)
  286. goto err_aux_status;
  287. return aux_entry;
  288. err_aux_status:
  289. thermal_zone_device_unregister(aux_entry->tzone);
  290. err_ret:
  291. kfree(aux_entry);
  292. return ERR_PTR(err);
  293. }
  294. static void proc_thermal_interrupt(void)
  295. {
  296. u32 sticky_out;
  297. int status;
  298. u32 ptmc_out;
  299. /* Clear APIC interrupt */
  300. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  301. SOC_DTS_OFFSET_PTMC, &ptmc_out);
  302. ptmc_out |= SOC_DTS_PTMC_APIC_DEASSERT_BIT;
  303. status = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  304. SOC_DTS_OFFSET_PTMC, ptmc_out);
  305. /* Read status here */
  306. status = iosf_mbi_read(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_READ,
  307. SOC_DTS_OFFSET_PTTSS, &sticky_out);
  308. pr_debug("status %d PTTSS %x\n", status, sticky_out);
  309. if (sticky_out & SOC_DTS_TRIP_MASK) {
  310. int i;
  311. /* reset sticky bit */
  312. status = iosf_mbi_write(BT_MBI_UNIT_PMC, BT_MBI_BUNIT_WRITE,
  313. SOC_DTS_OFFSET_PTTSS, sticky_out);
  314. for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) {
  315. pr_debug("TZD update for zone %d\n", i);
  316. thermal_zone_device_update(soc_dts[i]->tzone);
  317. }
  318. }
  319. }
  320. static irqreturn_t soc_irq_thread_fn(int irq, void *dev_data)
  321. {
  322. unsigned long flags;
  323. spin_lock_irqsave(&intr_notify_lock, flags);
  324. proc_thermal_interrupt();
  325. spin_unlock_irqrestore(&intr_notify_lock, flags);
  326. pr_debug("proc_thermal_interrupt\n");
  327. return IRQ_HANDLED;
  328. }
  329. static const struct x86_cpu_id soc_thermal_ids[] = {
  330. { X86_VENDOR_INTEL, X86_FAMILY_ANY, 0x37, 0, BYT_SOC_DTS_APIC_IRQ},
  331. {}
  332. };
  333. MODULE_DEVICE_TABLE(x86cpu, soc_thermal_ids);
  334. static int __init intel_soc_thermal_init(void)
  335. {
  336. u32 tj_max;
  337. int err = 0;
  338. int i;
  339. const struct x86_cpu_id *match_cpu;
  340. match_cpu = x86_match_cpu(soc_thermal_ids);
  341. if (!match_cpu)
  342. return -ENODEV;
  343. if (get_tj_max(&tj_max))
  344. return -EINVAL;
  345. for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) {
  346. soc_dts[i] = alloc_soc_dts(i, tj_max);
  347. if (IS_ERR(soc_dts[i])) {
  348. err = PTR_ERR(soc_dts[i]);
  349. goto err_free;
  350. }
  351. }
  352. spin_lock_init(&intr_notify_lock);
  353. soc_dts_thres_irq = (int)match_cpu->driver_data;
  354. err = request_threaded_irq(soc_dts_thres_irq, NULL,
  355. soc_irq_thread_fn,
  356. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  357. "soc_dts", soc_dts);
  358. if (err) {
  359. pr_err("request_threaded_irq ret %d\n", err);
  360. goto err_free;
  361. }
  362. for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i) {
  363. err = update_trip_temp(soc_dts[i], 0, tj_max - crit_offset);
  364. if (err)
  365. goto err_trip_temp;
  366. }
  367. return 0;
  368. err_trip_temp:
  369. i = SOC_MAX_DTS_SENSORS;
  370. free_irq(soc_dts_thres_irq, soc_dts);
  371. err_free:
  372. while (--i >= 0)
  373. free_soc_dts(soc_dts[i]);
  374. return err;
  375. }
  376. static void __exit intel_soc_thermal_exit(void)
  377. {
  378. int i;
  379. for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i)
  380. update_trip_temp(soc_dts[i], 0, 0);
  381. free_irq(soc_dts_thres_irq, soc_dts);
  382. for (i = 0; i < SOC_MAX_DTS_SENSORS; ++i)
  383. free_soc_dts(soc_dts[i]);
  384. }
  385. module_init(intel_soc_thermal_init)
  386. module_exit(intel_soc_thermal_exit)
  387. MODULE_DESCRIPTION("Intel SoC DTS Thermal Driver");
  388. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  389. MODULE_LICENSE("GPL v2");