intel_ips.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2009-2010 Intel Corporation
  4. *
  5. * Authors:
  6. * Jesse Barnes <jbarnes@virtuousgeek.org>
  7. */
  8. /*
  9. * Some Intel Ibex Peak based platforms support so-called "intelligent
  10. * power sharing", which allows the CPU and GPU to cooperate to maximize
  11. * performance within a given TDP (thermal design point). This driver
  12. * performs the coordination between the CPU and GPU, monitors thermal and
  13. * power statistics in the platform, and initializes power monitoring
  14. * hardware. It also provides a few tunables to control behavior. Its
  15. * primary purpose is to safely allow CPU and GPU turbo modes to be enabled
  16. * by tracking power and thermal budget; secondarily it can boost turbo
  17. * performance by allocating more power or thermal budget to the CPU or GPU
  18. * based on available headroom and activity.
  19. *
  20. * The basic algorithm is driven by a 5s moving average of temperature. If
  21. * thermal headroom is available, the CPU and/or GPU power clamps may be
  22. * adjusted upwards. If we hit the thermal ceiling or a thermal trigger,
  23. * we scale back the clamp. Aside from trigger events (when we're critically
  24. * close or over our TDP) we don't adjust the clamps more than once every
  25. * five seconds.
  26. *
  27. * The thermal device (device 31, function 6) has a set of registers that
  28. * are updated by the ME firmware. The ME should also take the clamp values
  29. * written to those registers and write them to the CPU, but we currently
  30. * bypass that functionality and write the CPU MSR directly.
  31. *
  32. * UNSUPPORTED:
  33. * - dual MCP configs
  34. *
  35. * TODO:
  36. * - handle CPU hotplug
  37. * - provide turbo enable/disable api
  38. *
  39. * Related documents:
  40. * - CDI 403777, 403778 - Auburndale EDS vol 1 & 2
  41. * - CDI 401376 - Ibex Peak EDS
  42. * - ref 26037, 26641 - IPS BIOS spec
  43. * - ref 26489 - Nehalem BIOS writer's guide
  44. * - ref 26921 - Ibex Peak BIOS Specification
  45. */
  46. #include <linux/debugfs.h>
  47. #include <linux/delay.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/kernel.h>
  50. #include <linux/kthread.h>
  51. #include <linux/module.h>
  52. #include <linux/pci.h>
  53. #include <linux/sched.h>
  54. #include <linux/sched/loadavg.h>
  55. #include <linux/seq_file.h>
  56. #include <linux/string.h>
  57. #include <linux/tick.h>
  58. #include <linux/timer.h>
  59. #include <linux/dmi.h>
  60. #include <drm/intel/i915_drm.h>
  61. #include <asm/msr.h>
  62. #include <asm/processor.h>
  63. #include <asm/cpu_device_id.h>
  64. #include "intel_ips.h"
  65. #include <linux/io-64-nonatomic-lo-hi.h>
  66. #define PCI_DEVICE_ID_INTEL_THERMAL_SENSOR 0x3b32
  67. /*
  68. * Package level MSRs for monitor/control
  69. */
  70. #define PLATFORM_INFO 0xce
  71. #define PLATFORM_TDP (1<<29)
  72. #define PLATFORM_RATIO (1<<28)
  73. #define IA32_MISC_ENABLE 0x1a0
  74. #define IA32_MISC_TURBO_EN (1ULL<<38)
  75. #define TURBO_POWER_CURRENT_LIMIT 0x1ac
  76. #define TURBO_TDC_OVR_EN (1UL<<31)
  77. #define TURBO_TDC_MASK (0x000000007fff0000UL)
  78. #define TURBO_TDC_SHIFT (16)
  79. #define TURBO_TDP_OVR_EN (1UL<<15)
  80. #define TURBO_TDP_MASK (0x0000000000003fffUL)
  81. /*
  82. * Core/thread MSRs for monitoring
  83. */
  84. #define IA32_PERF_CTL 0x199
  85. #define IA32_PERF_TURBO_DIS (1ULL<<32)
  86. /*
  87. * Thermal PCI device regs
  88. */
  89. #define THM_CFG_TBAR 0x10
  90. #define THM_CFG_TBAR_HI 0x14
  91. #define THM_TSIU 0x00
  92. #define THM_TSE 0x01
  93. #define TSE_EN 0xb8
  94. #define THM_TSS 0x02
  95. #define THM_TSTR 0x03
  96. #define THM_TSTTP 0x04
  97. #define THM_TSCO 0x08
  98. #define THM_TSES 0x0c
  99. #define THM_TSGPEN 0x0d
  100. #define TSGPEN_HOT_LOHI (1<<1)
  101. #define TSGPEN_CRIT_LOHI (1<<2)
  102. #define THM_TSPC 0x0e
  103. #define THM_PPEC 0x10
  104. #define THM_CTA 0x12
  105. #define THM_PTA 0x14
  106. #define PTA_SLOPE_MASK (0xff00)
  107. #define PTA_SLOPE_SHIFT 8
  108. #define PTA_OFFSET_MASK (0x00ff)
  109. #define THM_MGTA 0x16
  110. #define MGTA_SLOPE_MASK (0xff00)
  111. #define MGTA_SLOPE_SHIFT 8
  112. #define MGTA_OFFSET_MASK (0x00ff)
  113. #define THM_TRC 0x1a
  114. #define TRC_CORE2_EN (1<<15)
  115. #define TRC_THM_EN (1<<12)
  116. #define TRC_C6_WAR (1<<8)
  117. #define TRC_CORE1_EN (1<<7)
  118. #define TRC_CORE_PWR (1<<6)
  119. #define TRC_PCH_EN (1<<5)
  120. #define TRC_MCH_EN (1<<4)
  121. #define TRC_DIMM4 (1<<3)
  122. #define TRC_DIMM3 (1<<2)
  123. #define TRC_DIMM2 (1<<1)
  124. #define TRC_DIMM1 (1<<0)
  125. #define THM_TES 0x20
  126. #define THM_TEN 0x21
  127. #define TEN_UPDATE_EN 1
  128. #define THM_PSC 0x24
  129. #define PSC_NTG (1<<0) /* No GFX turbo support */
  130. #define PSC_NTPC (1<<1) /* No CPU turbo support */
  131. #define PSC_PP_DEF (0<<2) /* Perf policy up to driver */
  132. #define PSP_PP_PC (1<<2) /* BIOS prefers CPU perf */
  133. #define PSP_PP_BAL (2<<2) /* BIOS wants balanced perf */
  134. #define PSP_PP_GFX (3<<2) /* BIOS prefers GFX perf */
  135. #define PSP_PBRT (1<<4) /* BIOS run time support */
  136. #define THM_CTV1 0x30
  137. #define CTV_TEMP_ERROR (1<<15)
  138. #define CTV_TEMP_MASK 0x3f
  139. #define CTV_
  140. #define THM_CTV2 0x32
  141. #define THM_CEC 0x34 /* undocumented power accumulator in joules */
  142. #define THM_AE 0x3f
  143. #define THM_HTS 0x50 /* 32 bits */
  144. #define HTS_PCPL_MASK (0x7fe00000)
  145. #define HTS_PCPL_SHIFT 21
  146. #define HTS_GPL_MASK (0x001ff000)
  147. #define HTS_GPL_SHIFT 12
  148. #define HTS_PP_MASK (0x00000c00)
  149. #define HTS_PP_SHIFT 10
  150. #define HTS_PP_DEF 0
  151. #define HTS_PP_PROC 1
  152. #define HTS_PP_BAL 2
  153. #define HTS_PP_GFX 3
  154. #define HTS_PCTD_DIS (1<<9)
  155. #define HTS_GTD_DIS (1<<8)
  156. #define HTS_PTL_MASK (0x000000fe)
  157. #define HTS_PTL_SHIFT 1
  158. #define HTS_NVV (1<<0)
  159. #define THM_HTSHI 0x54 /* 16 bits */
  160. #define HTS2_PPL_MASK (0x03ff)
  161. #define HTS2_PRST_MASK (0x3c00)
  162. #define HTS2_PRST_SHIFT 10
  163. #define HTS2_PRST_UNLOADED 0
  164. #define HTS2_PRST_RUNNING 1
  165. #define HTS2_PRST_TDISOP 2 /* turbo disabled due to power */
  166. #define HTS2_PRST_TDISHT 3 /* turbo disabled due to high temp */
  167. #define HTS2_PRST_TDISUSR 4 /* user disabled turbo */
  168. #define HTS2_PRST_TDISPLAT 5 /* platform disabled turbo */
  169. #define HTS2_PRST_TDISPM 6 /* power management disabled turbo */
  170. #define HTS2_PRST_TDISERR 7 /* some kind of error disabled turbo */
  171. #define THM_PTL 0x56
  172. #define THM_MGTV 0x58
  173. #define TV_MASK 0x000000000000ff00
  174. #define TV_SHIFT 8
  175. #define THM_PTV 0x60
  176. #define PTV_MASK 0x00ff
  177. #define THM_MMGPC 0x64
  178. #define THM_MPPC 0x66
  179. #define THM_MPCPC 0x68
  180. #define THM_TSPIEN 0x82
  181. #define TSPIEN_AUX_LOHI (1<<0)
  182. #define TSPIEN_HOT_LOHI (1<<1)
  183. #define TSPIEN_CRIT_LOHI (1<<2)
  184. #define TSPIEN_AUX2_LOHI (1<<3)
  185. #define THM_TSLOCK 0x83
  186. #define THM_ATR 0x84
  187. #define THM_TOF 0x87
  188. #define THM_STS 0x98
  189. #define STS_PCPL_MASK (0x7fe00000)
  190. #define STS_PCPL_SHIFT 21
  191. #define STS_GPL_MASK (0x001ff000)
  192. #define STS_GPL_SHIFT 12
  193. #define STS_PP_MASK (0x00000c00)
  194. #define STS_PP_SHIFT 10
  195. #define STS_PP_DEF 0
  196. #define STS_PP_PROC 1
  197. #define STS_PP_BAL 2
  198. #define STS_PP_GFX 3
  199. #define STS_PCTD_DIS (1<<9)
  200. #define STS_GTD_DIS (1<<8)
  201. #define STS_PTL_MASK (0x000000fe)
  202. #define STS_PTL_SHIFT 1
  203. #define STS_NVV (1<<0)
  204. #define THM_SEC 0x9c
  205. #define SEC_ACK (1<<0)
  206. #define THM_TC3 0xa4
  207. #define THM_TC1 0xa8
  208. #define STS_PPL_MASK (0x0003ff00)
  209. #define STS_PPL_SHIFT 16
  210. #define THM_TC2 0xac
  211. #define THM_DTV 0xb0
  212. #define THM_ITV 0xd8
  213. #define ITV_ME_SEQNO_MASK 0x00ff0000 /* ME should update every ~200ms */
  214. #define ITV_ME_SEQNO_SHIFT (16)
  215. #define ITV_MCH_TEMP_MASK 0x0000ff00
  216. #define ITV_MCH_TEMP_SHIFT (8)
  217. #define ITV_PCH_TEMP_MASK 0x000000ff
  218. #define thm_readb(off) readb(ips->regmap + (off))
  219. #define thm_readw(off) readw(ips->regmap + (off))
  220. #define thm_readl(off) readl(ips->regmap + (off))
  221. #define thm_readq(off) readq(ips->regmap + (off))
  222. #define thm_writeb(off, val) writeb((val), ips->regmap + (off))
  223. #define thm_writew(off, val) writew((val), ips->regmap + (off))
  224. #define thm_writel(off, val) writel((val), ips->regmap + (off))
  225. static const int IPS_ADJUST_PERIOD = 5000; /* ms */
  226. static bool late_i915_load = false;
  227. /* For initial average collection */
  228. static const int IPS_SAMPLE_PERIOD = 200; /* ms */
  229. static const int IPS_SAMPLE_WINDOW = 5000; /* 5s moving window of samples */
  230. #define IPS_SAMPLE_COUNT (IPS_SAMPLE_WINDOW / IPS_SAMPLE_PERIOD)
  231. /* Per-SKU limits */
  232. struct ips_mcp_limits {
  233. int mcp_power_limit; /* mW units */
  234. int core_power_limit;
  235. int mch_power_limit;
  236. int core_temp_limit; /* degrees C */
  237. int mch_temp_limit;
  238. };
  239. /* Max temps are -10 degrees C to avoid PROCHOT# */
  240. static struct ips_mcp_limits ips_sv_limits = {
  241. .mcp_power_limit = 35000,
  242. .core_power_limit = 29000,
  243. .mch_power_limit = 20000,
  244. .core_temp_limit = 95,
  245. .mch_temp_limit = 90
  246. };
  247. static struct ips_mcp_limits ips_lv_limits = {
  248. .mcp_power_limit = 25000,
  249. .core_power_limit = 21000,
  250. .mch_power_limit = 13000,
  251. .core_temp_limit = 95,
  252. .mch_temp_limit = 90
  253. };
  254. static struct ips_mcp_limits ips_ulv_limits = {
  255. .mcp_power_limit = 18000,
  256. .core_power_limit = 14000,
  257. .mch_power_limit = 11000,
  258. .core_temp_limit = 95,
  259. .mch_temp_limit = 90
  260. };
  261. struct ips_driver {
  262. struct device *dev;
  263. void __iomem *regmap;
  264. int irq;
  265. struct task_struct *monitor;
  266. struct task_struct *adjust;
  267. struct dentry *debug_root;
  268. struct timer_list timer;
  269. /* Average CPU core temps (all averages in .01 degrees C for precision) */
  270. u16 ctv1_avg_temp;
  271. u16 ctv2_avg_temp;
  272. /* GMCH average */
  273. u16 mch_avg_temp;
  274. /* Average for the CPU (both cores?) */
  275. u16 mcp_avg_temp;
  276. /* Average power consumption (in mW) */
  277. u32 cpu_avg_power;
  278. u32 mch_avg_power;
  279. /* Offset values */
  280. u16 cta_val;
  281. u16 pta_val;
  282. u16 mgta_val;
  283. /* Maximums & prefs, protected by turbo status lock */
  284. spinlock_t turbo_status_lock;
  285. u16 mcp_temp_limit;
  286. u16 mcp_power_limit;
  287. u16 core_power_limit;
  288. u16 mch_power_limit;
  289. bool cpu_turbo_enabled;
  290. bool __cpu_turbo_on;
  291. bool gpu_turbo_enabled;
  292. bool __gpu_turbo_on;
  293. bool gpu_preferred;
  294. bool poll_turbo_status;
  295. bool second_cpu;
  296. bool turbo_toggle_allowed;
  297. struct ips_mcp_limits *limits;
  298. /* Optional MCH interfaces for if i915 is in use */
  299. unsigned long (*read_mch_val)(void);
  300. bool (*gpu_raise)(void);
  301. bool (*gpu_lower)(void);
  302. bool (*gpu_busy)(void);
  303. bool (*gpu_turbo_disable)(void);
  304. /* For restoration at unload */
  305. u64 orig_turbo_limit;
  306. u64 orig_turbo_ratios;
  307. };
  308. static bool
  309. ips_gpu_turbo_enabled(struct ips_driver *ips);
  310. /**
  311. * ips_cpu_busy - is CPU busy?
  312. * @ips: IPS driver struct
  313. *
  314. * Check CPU for load to see whether we should increase its thermal budget.
  315. *
  316. * RETURNS:
  317. * True if the CPU could use more power, false otherwise.
  318. */
  319. static bool ips_cpu_busy(struct ips_driver *ips)
  320. {
  321. if ((avenrun[0] >> FSHIFT) > 1)
  322. return true;
  323. return false;
  324. }
  325. /**
  326. * ips_cpu_raise - raise CPU power clamp
  327. * @ips: IPS driver struct
  328. *
  329. * Raise the CPU power clamp by %IPS_CPU_STEP, in accordance with TDP for
  330. * this platform.
  331. *
  332. * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR upwards (as
  333. * long as we haven't hit the TDP limit for the SKU).
  334. */
  335. static void ips_cpu_raise(struct ips_driver *ips)
  336. {
  337. u64 turbo_override;
  338. u16 cur_tdp_limit, new_tdp_limit;
  339. if (!ips->cpu_turbo_enabled)
  340. return;
  341. rdmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  342. cur_tdp_limit = turbo_override & TURBO_TDP_MASK;
  343. new_tdp_limit = cur_tdp_limit + 8; /* 1W increase */
  344. /* Clamp to SKU TDP limit */
  345. if (((new_tdp_limit * 10) / 8) > ips->core_power_limit)
  346. new_tdp_limit = cur_tdp_limit;
  347. thm_writew(THM_MPCPC, (new_tdp_limit * 10) / 8);
  348. turbo_override |= TURBO_TDC_OVR_EN | TURBO_TDP_OVR_EN;
  349. wrmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  350. turbo_override &= ~TURBO_TDP_MASK;
  351. turbo_override |= new_tdp_limit;
  352. wrmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  353. }
  354. /**
  355. * ips_cpu_lower - lower CPU power clamp
  356. * @ips: IPS driver struct
  357. *
  358. * Lower CPU power clamp b %IPS_CPU_STEP if possible.
  359. *
  360. * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR down, going
  361. * as low as the platform limits will allow (though we could go lower there
  362. * wouldn't be much point).
  363. */
  364. static void ips_cpu_lower(struct ips_driver *ips)
  365. {
  366. u64 turbo_override;
  367. u16 cur_limit, new_limit;
  368. rdmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  369. cur_limit = turbo_override & TURBO_TDP_MASK;
  370. new_limit = cur_limit - 8; /* 1W decrease */
  371. /* Clamp to SKU TDP limit */
  372. if (new_limit < (ips->orig_turbo_limit & TURBO_TDP_MASK))
  373. new_limit = ips->orig_turbo_limit & TURBO_TDP_MASK;
  374. thm_writew(THM_MPCPC, (new_limit * 10) / 8);
  375. turbo_override |= TURBO_TDC_OVR_EN | TURBO_TDP_OVR_EN;
  376. wrmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  377. turbo_override &= ~TURBO_TDP_MASK;
  378. turbo_override |= new_limit;
  379. wrmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  380. }
  381. /**
  382. * do_enable_cpu_turbo - internal turbo enable function
  383. * @data: unused
  384. *
  385. * Internal function for actually updating MSRs. When we enable/disable
  386. * turbo, we need to do it on each CPU; this function is the one called
  387. * by on_each_cpu() when needed.
  388. */
  389. static void do_enable_cpu_turbo(void *data)
  390. {
  391. u64 perf_ctl;
  392. rdmsrq(IA32_PERF_CTL, perf_ctl);
  393. if (perf_ctl & IA32_PERF_TURBO_DIS) {
  394. perf_ctl &= ~IA32_PERF_TURBO_DIS;
  395. wrmsrq(IA32_PERF_CTL, perf_ctl);
  396. }
  397. }
  398. /**
  399. * ips_enable_cpu_turbo - enable turbo mode on all CPUs
  400. * @ips: IPS driver struct
  401. *
  402. * Enable turbo mode by clearing the disable bit in IA32_PERF_CTL on
  403. * all logical threads.
  404. */
  405. static void ips_enable_cpu_turbo(struct ips_driver *ips)
  406. {
  407. /* Already on, no need to mess with MSRs */
  408. if (ips->__cpu_turbo_on)
  409. return;
  410. if (ips->turbo_toggle_allowed)
  411. on_each_cpu(do_enable_cpu_turbo, ips, 1);
  412. ips->__cpu_turbo_on = true;
  413. }
  414. /**
  415. * do_disable_cpu_turbo - internal turbo disable function
  416. * @data: unused
  417. *
  418. * Internal function for actually updating MSRs. When we enable/disable
  419. * turbo, we need to do it on each CPU; this function is the one called
  420. * by on_each_cpu() when needed.
  421. */
  422. static void do_disable_cpu_turbo(void *data)
  423. {
  424. u64 perf_ctl;
  425. rdmsrq(IA32_PERF_CTL, perf_ctl);
  426. if (!(perf_ctl & IA32_PERF_TURBO_DIS)) {
  427. perf_ctl |= IA32_PERF_TURBO_DIS;
  428. wrmsrq(IA32_PERF_CTL, perf_ctl);
  429. }
  430. }
  431. /**
  432. * ips_disable_cpu_turbo - disable turbo mode on all CPUs
  433. * @ips: IPS driver struct
  434. *
  435. * Disable turbo mode by setting the disable bit in IA32_PERF_CTL on
  436. * all logical threads.
  437. */
  438. static void ips_disable_cpu_turbo(struct ips_driver *ips)
  439. {
  440. /* Already off, leave it */
  441. if (!ips->__cpu_turbo_on)
  442. return;
  443. if (ips->turbo_toggle_allowed)
  444. on_each_cpu(do_disable_cpu_turbo, ips, 1);
  445. ips->__cpu_turbo_on = false;
  446. }
  447. /**
  448. * ips_gpu_busy - is GPU busy?
  449. * @ips: IPS driver struct
  450. *
  451. * Check GPU for load to see whether we should increase its thermal budget.
  452. * We need to call into the i915 driver in this case.
  453. *
  454. * RETURNS:
  455. * True if the GPU could use more power, false otherwise.
  456. */
  457. static bool ips_gpu_busy(struct ips_driver *ips)
  458. {
  459. if (!ips_gpu_turbo_enabled(ips))
  460. return false;
  461. return ips->gpu_busy();
  462. }
  463. /**
  464. * ips_gpu_raise - raise GPU power clamp
  465. * @ips: IPS driver struct
  466. *
  467. * Raise the GPU frequency/power if possible. We need to call into the
  468. * i915 driver in this case.
  469. */
  470. static void ips_gpu_raise(struct ips_driver *ips)
  471. {
  472. if (!ips_gpu_turbo_enabled(ips))
  473. return;
  474. if (!ips->gpu_raise())
  475. ips->gpu_turbo_enabled = false;
  476. return;
  477. }
  478. /**
  479. * ips_gpu_lower - lower GPU power clamp
  480. * @ips: IPS driver struct
  481. *
  482. * Lower GPU frequency/power if possible. Need to call i915.
  483. */
  484. static void ips_gpu_lower(struct ips_driver *ips)
  485. {
  486. if (!ips_gpu_turbo_enabled(ips))
  487. return;
  488. if (!ips->gpu_lower())
  489. ips->gpu_turbo_enabled = false;
  490. return;
  491. }
  492. /**
  493. * ips_enable_gpu_turbo - notify the gfx driver turbo is available
  494. * @ips: IPS driver struct
  495. *
  496. * Call into the graphics driver indicating that it can safely use
  497. * turbo mode.
  498. */
  499. static void ips_enable_gpu_turbo(struct ips_driver *ips)
  500. {
  501. if (ips->__gpu_turbo_on)
  502. return;
  503. ips->__gpu_turbo_on = true;
  504. }
  505. /**
  506. * ips_disable_gpu_turbo - notify the gfx driver to disable turbo mode
  507. * @ips: IPS driver struct
  508. *
  509. * Request that the graphics driver disable turbo mode.
  510. */
  511. static void ips_disable_gpu_turbo(struct ips_driver *ips)
  512. {
  513. /* Avoid calling i915 if turbo is already disabled */
  514. if (!ips->__gpu_turbo_on)
  515. return;
  516. if (!ips->gpu_turbo_disable())
  517. dev_err(ips->dev, "failed to disable graphics turbo\n");
  518. else
  519. ips->__gpu_turbo_on = false;
  520. }
  521. /**
  522. * mcp_exceeded - check whether we're outside our thermal & power limits
  523. * @ips: IPS driver struct
  524. *
  525. * Check whether the MCP is over its thermal or power budget.
  526. *
  527. * Returns: %true if the temp or power has exceeded its maximum, else %false
  528. */
  529. static bool mcp_exceeded(struct ips_driver *ips)
  530. {
  531. unsigned long flags;
  532. bool ret = false;
  533. u32 temp_limit;
  534. u32 avg_power;
  535. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  536. temp_limit = ips->mcp_temp_limit * 100;
  537. if (ips->mcp_avg_temp > temp_limit)
  538. ret = true;
  539. avg_power = ips->cpu_avg_power + ips->mch_avg_power;
  540. if (avg_power > ips->mcp_power_limit)
  541. ret = true;
  542. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  543. return ret;
  544. }
  545. /**
  546. * cpu_exceeded - check whether a CPU core is outside its limits
  547. * @ips: IPS driver struct
  548. * @cpu: CPU number to check
  549. *
  550. * Check a given CPU's average temp or power is over its limit.
  551. *
  552. * Returns: %true if the temp or power has exceeded its maximum, else %false
  553. */
  554. static bool cpu_exceeded(struct ips_driver *ips, int cpu)
  555. {
  556. unsigned long flags;
  557. int avg;
  558. bool ret = false;
  559. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  560. avg = cpu ? ips->ctv2_avg_temp : ips->ctv1_avg_temp;
  561. if (avg > (ips->limits->core_temp_limit * 100))
  562. ret = true;
  563. if (ips->cpu_avg_power > ips->core_power_limit * 100)
  564. ret = true;
  565. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  566. if (ret)
  567. dev_info(ips->dev, "CPU power or thermal limit exceeded\n");
  568. return ret;
  569. }
  570. /**
  571. * mch_exceeded - check whether the GPU is over budget
  572. * @ips: IPS driver struct
  573. *
  574. * Check the MCH temp & power against their maximums.
  575. *
  576. * Returns: %true if the temp or power has exceeded its maximum, else %false
  577. */
  578. static bool mch_exceeded(struct ips_driver *ips)
  579. {
  580. unsigned long flags;
  581. bool ret = false;
  582. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  583. if (ips->mch_avg_temp > (ips->limits->mch_temp_limit * 100))
  584. ret = true;
  585. if (ips->mch_avg_power > ips->mch_power_limit)
  586. ret = true;
  587. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  588. return ret;
  589. }
  590. /**
  591. * verify_limits - verify BIOS provided limits
  592. * @ips: IPS structure
  593. *
  594. * BIOS can optionally provide non-default limits for power and temp. Check
  595. * them here and use the defaults if the BIOS values are not provided or
  596. * are otherwise unusable.
  597. */
  598. static void verify_limits(struct ips_driver *ips)
  599. {
  600. if (ips->mcp_power_limit < ips->limits->mcp_power_limit ||
  601. ips->mcp_power_limit > 35000)
  602. ips->mcp_power_limit = ips->limits->mcp_power_limit;
  603. if (ips->mcp_temp_limit < ips->limits->core_temp_limit ||
  604. ips->mcp_temp_limit < ips->limits->mch_temp_limit ||
  605. ips->mcp_temp_limit > 150)
  606. ips->mcp_temp_limit = min(ips->limits->core_temp_limit,
  607. ips->limits->mch_temp_limit);
  608. }
  609. /**
  610. * update_turbo_limits - get various limits & settings from regs
  611. * @ips: IPS driver struct
  612. *
  613. * Update the IPS power & temp limits, along with turbo enable flags,
  614. * based on latest register contents.
  615. *
  616. * Used at init time and for runtime BIOS support, which requires polling
  617. * the regs for updates (as a result of AC->DC transition for example).
  618. *
  619. * LOCKING:
  620. * Caller must hold turbo_status_lock (outside of init)
  621. */
  622. static void update_turbo_limits(struct ips_driver *ips)
  623. {
  624. u32 hts = thm_readl(THM_HTS);
  625. ips->cpu_turbo_enabled = !(hts & HTS_PCTD_DIS);
  626. /*
  627. * Disable turbo for now, until we can figure out why the power figures
  628. * are wrong
  629. */
  630. ips->cpu_turbo_enabled = false;
  631. if (ips->gpu_busy)
  632. ips->gpu_turbo_enabled = !(hts & HTS_GTD_DIS);
  633. ips->core_power_limit = thm_readw(THM_MPCPC);
  634. ips->mch_power_limit = thm_readw(THM_MMGPC);
  635. ips->mcp_temp_limit = thm_readw(THM_PTL);
  636. ips->mcp_power_limit = thm_readw(THM_MPPC);
  637. verify_limits(ips);
  638. /* Ignore BIOS CPU vs GPU pref */
  639. }
  640. /**
  641. * ips_adjust - adjust power clamp based on thermal state
  642. * @data: ips driver structure
  643. *
  644. * Wake up every 5s or so and check whether we should adjust the power clamp.
  645. * Check CPU and GPU load to determine which needs adjustment. There are
  646. * several things to consider here:
  647. * - do we need to adjust up or down?
  648. * - is CPU busy?
  649. * - is GPU busy?
  650. * - is CPU in turbo?
  651. * - is GPU in turbo?
  652. * - is CPU or GPU preferred? (CPU is default)
  653. *
  654. * So, given the above, we do the following:
  655. * - up (TDP available)
  656. * - CPU not busy, GPU not busy - nothing
  657. * - CPU busy, GPU not busy - adjust CPU up
  658. * - CPU not busy, GPU busy - adjust GPU up
  659. * - CPU busy, GPU busy - adjust preferred unit up, taking headroom from
  660. * non-preferred unit if necessary
  661. * - down (at TDP limit)
  662. * - adjust both CPU and GPU down if possible
  663. *
  664. * |cpu+ gpu+ cpu+gpu- cpu-gpu+ cpu-gpu-
  665. * cpu < gpu < |cpu+gpu+ cpu+ gpu+ nothing
  666. * cpu < gpu >= |cpu+gpu-(mcp<) cpu+gpu-(mcp<) gpu- gpu-
  667. * cpu >= gpu < |cpu-gpu+(mcp<) cpu- cpu-gpu+(mcp<) cpu-
  668. * cpu >= gpu >=|cpu-gpu- cpu-gpu- cpu-gpu- cpu-gpu-
  669. *
  670. * Returns: %0
  671. */
  672. static int ips_adjust(void *data)
  673. {
  674. struct ips_driver *ips = data;
  675. unsigned long flags;
  676. dev_dbg(ips->dev, "starting ips-adjust thread\n");
  677. /*
  678. * Adjust CPU and GPU clamps every 5s if needed. Doing it more
  679. * often isn't recommended due to ME interaction.
  680. */
  681. do {
  682. bool cpu_busy = ips_cpu_busy(ips);
  683. bool gpu_busy = ips_gpu_busy(ips);
  684. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  685. if (ips->poll_turbo_status)
  686. update_turbo_limits(ips);
  687. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  688. /* Update turbo status if necessary */
  689. if (ips->cpu_turbo_enabled)
  690. ips_enable_cpu_turbo(ips);
  691. else
  692. ips_disable_cpu_turbo(ips);
  693. if (ips->gpu_turbo_enabled)
  694. ips_enable_gpu_turbo(ips);
  695. else
  696. ips_disable_gpu_turbo(ips);
  697. /* We're outside our comfort zone, crank them down */
  698. if (mcp_exceeded(ips)) {
  699. ips_cpu_lower(ips);
  700. ips_gpu_lower(ips);
  701. goto sleep;
  702. }
  703. if (!cpu_exceeded(ips, 0) && cpu_busy)
  704. ips_cpu_raise(ips);
  705. else
  706. ips_cpu_lower(ips);
  707. if (!mch_exceeded(ips) && gpu_busy)
  708. ips_gpu_raise(ips);
  709. else
  710. ips_gpu_lower(ips);
  711. sleep:
  712. schedule_timeout_interruptible(msecs_to_jiffies(IPS_ADJUST_PERIOD));
  713. } while (!kthread_should_stop());
  714. dev_dbg(ips->dev, "ips-adjust thread stopped\n");
  715. return 0;
  716. }
  717. /*
  718. * Helpers for reading out temp/power values and calculating their
  719. * averages for the decision making and monitoring functions.
  720. */
  721. static u16 calc_avg_temp(struct ips_driver *ips, u16 *array)
  722. {
  723. u64 total = 0;
  724. int i;
  725. u16 avg;
  726. for (i = 0; i < IPS_SAMPLE_COUNT; i++)
  727. total += (u64)(array[i] * 100);
  728. do_div(total, IPS_SAMPLE_COUNT);
  729. avg = (u16)total;
  730. return avg;
  731. }
  732. static u16 read_mgtv(struct ips_driver *ips)
  733. {
  734. u16 __maybe_unused ret;
  735. u64 slope, offset;
  736. u64 val;
  737. val = thm_readq(THM_MGTV);
  738. val = (val & TV_MASK) >> TV_SHIFT;
  739. slope = offset = thm_readw(THM_MGTA);
  740. slope = (slope & MGTA_SLOPE_MASK) >> MGTA_SLOPE_SHIFT;
  741. offset = offset & MGTA_OFFSET_MASK;
  742. ret = ((val * slope + 0x40) >> 7) + offset;
  743. return 0; /* MCH temp reporting buggy */
  744. }
  745. static u16 read_ptv(struct ips_driver *ips)
  746. {
  747. u16 val;
  748. val = thm_readw(THM_PTV) & PTV_MASK;
  749. return val;
  750. }
  751. static u16 read_ctv(struct ips_driver *ips, int cpu)
  752. {
  753. int reg = cpu ? THM_CTV2 : THM_CTV1;
  754. u16 val;
  755. val = thm_readw(reg);
  756. if (!(val & CTV_TEMP_ERROR))
  757. val = (val) >> 6; /* discard fractional component */
  758. else
  759. val = 0;
  760. return val;
  761. }
  762. static u32 get_cpu_power(struct ips_driver *ips, u32 *last, int period)
  763. {
  764. u32 val;
  765. u32 ret;
  766. /*
  767. * CEC is in joules/65535. Take difference over time to
  768. * get watts.
  769. */
  770. val = thm_readl(THM_CEC);
  771. /* period is in ms and we want mW */
  772. ret = (((val - *last) * 1000) / period);
  773. ret = (ret * 1000) / 65535;
  774. *last = val;
  775. return 0;
  776. }
  777. static const u16 temp_decay_factor = 2;
  778. static u16 update_average_temp(u16 avg, u16 val)
  779. {
  780. u16 ret;
  781. /* Multiply by 100 for extra precision */
  782. ret = (val * 100 / temp_decay_factor) +
  783. (((temp_decay_factor - 1) * avg) / temp_decay_factor);
  784. return ret;
  785. }
  786. static const u16 power_decay_factor = 2;
  787. static u16 update_average_power(u32 avg, u32 val)
  788. {
  789. u32 ret;
  790. ret = (val / power_decay_factor) +
  791. (((power_decay_factor - 1) * avg) / power_decay_factor);
  792. return ret;
  793. }
  794. static u32 calc_avg_power(struct ips_driver *ips, u32 *array)
  795. {
  796. u64 total = 0;
  797. u32 avg;
  798. int i;
  799. for (i = 0; i < IPS_SAMPLE_COUNT; i++)
  800. total += array[i];
  801. do_div(total, IPS_SAMPLE_COUNT);
  802. avg = (u32)total;
  803. return avg;
  804. }
  805. static void monitor_timeout(struct timer_list *t)
  806. {
  807. struct ips_driver *ips = timer_container_of(ips, t, timer);
  808. wake_up_process(ips->monitor);
  809. }
  810. /**
  811. * ips_monitor - temp/power monitoring thread
  812. * @data: ips driver structure
  813. *
  814. * This is the main function for the IPS driver. It monitors power and
  815. * temperature in the MCP and adjusts CPU and GPU power clamps accordingly.
  816. *
  817. * We keep a 5s moving average of power consumption and temperature. Using
  818. * that data, along with CPU vs GPU preference, we adjust the power clamps
  819. * up or down.
  820. *
  821. * Returns: %0 on success or -errno on error
  822. */
  823. static int ips_monitor(void *data)
  824. {
  825. struct ips_driver *ips = data;
  826. unsigned long seqno_timestamp, expire, last_msecs, last_sample_period;
  827. int i;
  828. u32 *cpu_samples, *mchp_samples, old_cpu_power;
  829. u16 *mcp_samples, *ctv1_samples, *ctv2_samples, *mch_samples;
  830. u8 cur_seqno, last_seqno;
  831. mcp_samples = kcalloc(IPS_SAMPLE_COUNT, sizeof(u16), GFP_KERNEL);
  832. ctv1_samples = kcalloc(IPS_SAMPLE_COUNT, sizeof(u16), GFP_KERNEL);
  833. ctv2_samples = kcalloc(IPS_SAMPLE_COUNT, sizeof(u16), GFP_KERNEL);
  834. mch_samples = kcalloc(IPS_SAMPLE_COUNT, sizeof(u16), GFP_KERNEL);
  835. cpu_samples = kcalloc(IPS_SAMPLE_COUNT, sizeof(u32), GFP_KERNEL);
  836. mchp_samples = kcalloc(IPS_SAMPLE_COUNT, sizeof(u32), GFP_KERNEL);
  837. if (!mcp_samples || !ctv1_samples || !ctv2_samples || !mch_samples ||
  838. !cpu_samples || !mchp_samples) {
  839. dev_err(ips->dev,
  840. "failed to allocate sample array, ips disabled\n");
  841. kfree(mcp_samples);
  842. kfree(ctv1_samples);
  843. kfree(ctv2_samples);
  844. kfree(mch_samples);
  845. kfree(cpu_samples);
  846. kfree(mchp_samples);
  847. return -ENOMEM;
  848. }
  849. last_seqno = (thm_readl(THM_ITV) & ITV_ME_SEQNO_MASK) >>
  850. ITV_ME_SEQNO_SHIFT;
  851. seqno_timestamp = get_jiffies_64();
  852. old_cpu_power = thm_readl(THM_CEC);
  853. schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
  854. /* Collect an initial average */
  855. for (i = 0; i < IPS_SAMPLE_COUNT; i++) {
  856. u32 mchp, cpu_power;
  857. u16 val;
  858. mcp_samples[i] = read_ptv(ips);
  859. val = read_ctv(ips, 0);
  860. ctv1_samples[i] = val;
  861. val = read_ctv(ips, 1);
  862. ctv2_samples[i] = val;
  863. val = read_mgtv(ips);
  864. mch_samples[i] = val;
  865. cpu_power = get_cpu_power(ips, &old_cpu_power,
  866. IPS_SAMPLE_PERIOD);
  867. cpu_samples[i] = cpu_power;
  868. if (ips->read_mch_val) {
  869. mchp = ips->read_mch_val();
  870. mchp_samples[i] = mchp;
  871. }
  872. schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
  873. if (kthread_should_stop())
  874. break;
  875. }
  876. ips->mcp_avg_temp = calc_avg_temp(ips, mcp_samples);
  877. ips->ctv1_avg_temp = calc_avg_temp(ips, ctv1_samples);
  878. ips->ctv2_avg_temp = calc_avg_temp(ips, ctv2_samples);
  879. ips->mch_avg_temp = calc_avg_temp(ips, mch_samples);
  880. ips->cpu_avg_power = calc_avg_power(ips, cpu_samples);
  881. ips->mch_avg_power = calc_avg_power(ips, mchp_samples);
  882. kfree(mcp_samples);
  883. kfree(ctv1_samples);
  884. kfree(ctv2_samples);
  885. kfree(mch_samples);
  886. kfree(cpu_samples);
  887. kfree(mchp_samples);
  888. /* Start the adjustment thread now that we have data */
  889. wake_up_process(ips->adjust);
  890. /*
  891. * Ok, now we have an initial avg. From here on out, we track the
  892. * running avg using a decaying average calculation. This allows
  893. * us to reduce the sample frequency if the CPU and GPU are idle.
  894. */
  895. old_cpu_power = thm_readl(THM_CEC);
  896. schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
  897. last_sample_period = IPS_SAMPLE_PERIOD;
  898. timer_setup(&ips->timer, monitor_timeout, TIMER_DEFERRABLE);
  899. do {
  900. u32 cpu_val, mch_val;
  901. u16 val;
  902. /* MCP itself */
  903. val = read_ptv(ips);
  904. ips->mcp_avg_temp = update_average_temp(ips->mcp_avg_temp, val);
  905. /* Processor 0 */
  906. val = read_ctv(ips, 0);
  907. ips->ctv1_avg_temp =
  908. update_average_temp(ips->ctv1_avg_temp, val);
  909. /* Power */
  910. cpu_val = get_cpu_power(ips, &old_cpu_power,
  911. last_sample_period);
  912. ips->cpu_avg_power =
  913. update_average_power(ips->cpu_avg_power, cpu_val);
  914. if (ips->second_cpu) {
  915. /* Processor 1 */
  916. val = read_ctv(ips, 1);
  917. ips->ctv2_avg_temp =
  918. update_average_temp(ips->ctv2_avg_temp, val);
  919. }
  920. /* MCH */
  921. val = read_mgtv(ips);
  922. ips->mch_avg_temp = update_average_temp(ips->mch_avg_temp, val);
  923. /* Power */
  924. if (ips->read_mch_val) {
  925. mch_val = ips->read_mch_val();
  926. ips->mch_avg_power =
  927. update_average_power(ips->mch_avg_power,
  928. mch_val);
  929. }
  930. /*
  931. * Make sure ME is updating thermal regs.
  932. * Note:
  933. * If it's been more than a second since the last update,
  934. * the ME is probably hung.
  935. */
  936. cur_seqno = (thm_readl(THM_ITV) & ITV_ME_SEQNO_MASK) >>
  937. ITV_ME_SEQNO_SHIFT;
  938. if (cur_seqno == last_seqno &&
  939. time_after(jiffies, seqno_timestamp + HZ)) {
  940. dev_warn(ips->dev,
  941. "ME failed to update for more than 1s, likely hung\n");
  942. } else {
  943. seqno_timestamp = get_jiffies_64();
  944. last_seqno = cur_seqno;
  945. }
  946. last_msecs = jiffies_to_msecs(jiffies);
  947. expire = jiffies + msecs_to_jiffies(IPS_SAMPLE_PERIOD);
  948. __set_current_state(TASK_INTERRUPTIBLE);
  949. mod_timer(&ips->timer, expire);
  950. schedule();
  951. /* Calculate actual sample period for power averaging */
  952. last_sample_period = jiffies_to_msecs(jiffies) - last_msecs;
  953. if (!last_sample_period)
  954. last_sample_period = 1;
  955. } while (!kthread_should_stop());
  956. timer_delete_sync(&ips->timer);
  957. dev_dbg(ips->dev, "ips-monitor thread stopped\n");
  958. return 0;
  959. }
  960. /**
  961. * ips_irq_handler - handle temperature triggers and other IPS events
  962. * @irq: irq number
  963. * @arg: unused
  964. *
  965. * Handle temperature limit trigger events, generally by lowering the clamps.
  966. * If we're at a critical limit, we clamp back to the lowest possible value
  967. * to prevent emergency shutdown.
  968. *
  969. * Returns: IRQ_NONE or IRQ_HANDLED
  970. */
  971. static irqreturn_t ips_irq_handler(int irq, void *arg)
  972. {
  973. struct ips_driver *ips = arg;
  974. u8 tses = thm_readb(THM_TSES);
  975. u8 tes = thm_readb(THM_TES);
  976. if (!tses && !tes)
  977. return IRQ_NONE;
  978. dev_info(ips->dev, "TSES: 0x%02x\n", tses);
  979. dev_info(ips->dev, "TES: 0x%02x\n", tes);
  980. /* STS update from EC? */
  981. if (tes & 1) {
  982. u32 sts, tc1;
  983. sts = thm_readl(THM_STS);
  984. tc1 = thm_readl(THM_TC1);
  985. if (sts & STS_NVV) {
  986. spin_lock(&ips->turbo_status_lock);
  987. ips->core_power_limit = (sts & STS_PCPL_MASK) >>
  988. STS_PCPL_SHIFT;
  989. ips->mch_power_limit = (sts & STS_GPL_MASK) >>
  990. STS_GPL_SHIFT;
  991. /* ignore EC CPU vs GPU pref */
  992. ips->cpu_turbo_enabled = !(sts & STS_PCTD_DIS);
  993. /*
  994. * Disable turbo for now, until we can figure
  995. * out why the power figures are wrong
  996. */
  997. ips->cpu_turbo_enabled = false;
  998. if (ips->gpu_busy)
  999. ips->gpu_turbo_enabled = !(sts & STS_GTD_DIS);
  1000. ips->mcp_temp_limit = (sts & STS_PTL_MASK) >>
  1001. STS_PTL_SHIFT;
  1002. ips->mcp_power_limit = (tc1 & STS_PPL_MASK) >>
  1003. STS_PPL_SHIFT;
  1004. verify_limits(ips);
  1005. spin_unlock(&ips->turbo_status_lock);
  1006. thm_writeb(THM_SEC, SEC_ACK);
  1007. }
  1008. thm_writeb(THM_TES, tes);
  1009. }
  1010. /* Thermal trip */
  1011. if (tses) {
  1012. dev_warn(ips->dev, "thermal trip occurred, tses: 0x%04x\n",
  1013. tses);
  1014. thm_writeb(THM_TSES, tses);
  1015. }
  1016. return IRQ_HANDLED;
  1017. }
  1018. #ifndef CONFIG_DEBUG_FS
  1019. static void ips_debugfs_init(struct ips_driver *ips) { return; }
  1020. static void ips_debugfs_cleanup(struct ips_driver *ips) { return; }
  1021. #else
  1022. /* Expose current state and limits in debugfs if possible */
  1023. static int cpu_temp_show(struct seq_file *m, void *data)
  1024. {
  1025. struct ips_driver *ips = m->private;
  1026. seq_printf(m, "%d.%02d\n", ips->ctv1_avg_temp / 100,
  1027. ips->ctv1_avg_temp % 100);
  1028. return 0;
  1029. }
  1030. DEFINE_SHOW_ATTRIBUTE(cpu_temp);
  1031. static int cpu_power_show(struct seq_file *m, void *data)
  1032. {
  1033. struct ips_driver *ips = m->private;
  1034. seq_printf(m, "%dmW\n", ips->cpu_avg_power);
  1035. return 0;
  1036. }
  1037. DEFINE_SHOW_ATTRIBUTE(cpu_power);
  1038. static int cpu_clamp_show(struct seq_file *m, void *data)
  1039. {
  1040. u64 turbo_override;
  1041. int tdp, tdc;
  1042. rdmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  1043. tdp = (int)(turbo_override & TURBO_TDP_MASK);
  1044. tdc = (int)((turbo_override & TURBO_TDC_MASK) >> TURBO_TDC_SHIFT);
  1045. /* Convert to .1W/A units */
  1046. tdp = tdp * 10 / 8;
  1047. tdc = tdc * 10 / 8;
  1048. /* Watts Amperes */
  1049. seq_printf(m, "%d.%dW %d.%dA\n", tdp / 10, tdp % 10,
  1050. tdc / 10, tdc % 10);
  1051. return 0;
  1052. }
  1053. DEFINE_SHOW_ATTRIBUTE(cpu_clamp);
  1054. static int mch_temp_show(struct seq_file *m, void *data)
  1055. {
  1056. struct ips_driver *ips = m->private;
  1057. seq_printf(m, "%d.%02d\n", ips->mch_avg_temp / 100,
  1058. ips->mch_avg_temp % 100);
  1059. return 0;
  1060. }
  1061. DEFINE_SHOW_ATTRIBUTE(mch_temp);
  1062. static int mch_power_show(struct seq_file *m, void *data)
  1063. {
  1064. struct ips_driver *ips = m->private;
  1065. seq_printf(m, "%dmW\n", ips->mch_avg_power);
  1066. return 0;
  1067. }
  1068. DEFINE_SHOW_ATTRIBUTE(mch_power);
  1069. static void ips_debugfs_cleanup(struct ips_driver *ips)
  1070. {
  1071. debugfs_remove_recursive(ips->debug_root);
  1072. }
  1073. static void ips_debugfs_init(struct ips_driver *ips)
  1074. {
  1075. ips->debug_root = debugfs_create_dir("ips", NULL);
  1076. debugfs_create_file("cpu_temp", 0444, ips->debug_root, ips, &cpu_temp_fops);
  1077. debugfs_create_file("cpu_power", 0444, ips->debug_root, ips, &cpu_power_fops);
  1078. debugfs_create_file("cpu_clamp", 0444, ips->debug_root, ips, &cpu_clamp_fops);
  1079. debugfs_create_file("mch_temp", 0444, ips->debug_root, ips, &mch_temp_fops);
  1080. debugfs_create_file("mch_power", 0444, ips->debug_root, ips, &mch_power_fops);
  1081. }
  1082. #endif /* CONFIG_DEBUG_FS */
  1083. /**
  1084. * ips_detect_cpu - detect whether CPU supports IPS
  1085. * @ips: IPS driver struct
  1086. *
  1087. * Walk our list and see if we're on a supported CPU. If we find one,
  1088. * return the limits for it.
  1089. *
  1090. * Returns: the &ips_mcp_limits struct that matches the boot CPU or %NULL
  1091. */
  1092. static struct ips_mcp_limits *ips_detect_cpu(struct ips_driver *ips)
  1093. {
  1094. u64 turbo_power, misc_en;
  1095. struct ips_mcp_limits *limits = NULL;
  1096. u16 tdp;
  1097. if (!(boot_cpu_data.x86_vfm == INTEL_WESTMERE)) {
  1098. dev_info(ips->dev, "Non-IPS CPU detected.\n");
  1099. return NULL;
  1100. }
  1101. rdmsrq(IA32_MISC_ENABLE, misc_en);
  1102. /*
  1103. * If the turbo enable bit isn't set, we shouldn't try to enable/disable
  1104. * turbo manually or we'll get an illegal MSR access, even though
  1105. * turbo will still be available.
  1106. */
  1107. if (misc_en & IA32_MISC_TURBO_EN)
  1108. ips->turbo_toggle_allowed = true;
  1109. else
  1110. ips->turbo_toggle_allowed = false;
  1111. if (strstr(boot_cpu_data.x86_model_id, "CPU M"))
  1112. limits = &ips_sv_limits;
  1113. else if (strstr(boot_cpu_data.x86_model_id, "CPU L"))
  1114. limits = &ips_lv_limits;
  1115. else if (strstr(boot_cpu_data.x86_model_id, "CPU U"))
  1116. limits = &ips_ulv_limits;
  1117. else {
  1118. dev_info(ips->dev, "No CPUID match found.\n");
  1119. return NULL;
  1120. }
  1121. rdmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_power);
  1122. tdp = turbo_power & TURBO_TDP_MASK;
  1123. /* Sanity check TDP against CPU */
  1124. if (limits->core_power_limit != (tdp / 8) * 1000) {
  1125. dev_info(ips->dev,
  1126. "CPU TDP doesn't match expected value (found %d, expected %d)\n",
  1127. tdp / 8, limits->core_power_limit / 1000);
  1128. limits->core_power_limit = (tdp / 8) * 1000;
  1129. }
  1130. return limits;
  1131. }
  1132. /**
  1133. * ips_get_i915_syms - try to get GPU control methods from i915 driver
  1134. * @ips: IPS driver
  1135. *
  1136. * The i915 driver exports several interfaces to allow the IPS driver to
  1137. * monitor and control graphics turbo mode. If we can find them, we can
  1138. * enable graphics turbo, otherwise we must disable it to avoid exceeding
  1139. * thermal and power limits in the MCP.
  1140. *
  1141. * Returns: %true if the required symbols are found, else %false
  1142. */
  1143. static bool ips_get_i915_syms(struct ips_driver *ips)
  1144. {
  1145. ips->read_mch_val = symbol_get(i915_read_mch_val);
  1146. if (!ips->read_mch_val)
  1147. goto out_err;
  1148. ips->gpu_raise = symbol_get(i915_gpu_raise);
  1149. if (!ips->gpu_raise)
  1150. goto out_put_mch;
  1151. ips->gpu_lower = symbol_get(i915_gpu_lower);
  1152. if (!ips->gpu_lower)
  1153. goto out_put_raise;
  1154. ips->gpu_busy = symbol_get(i915_gpu_busy);
  1155. if (!ips->gpu_busy)
  1156. goto out_put_lower;
  1157. ips->gpu_turbo_disable = symbol_get(i915_gpu_turbo_disable);
  1158. if (!ips->gpu_turbo_disable)
  1159. goto out_put_busy;
  1160. return true;
  1161. out_put_busy:
  1162. symbol_put(i915_gpu_busy);
  1163. out_put_lower:
  1164. symbol_put(i915_gpu_lower);
  1165. out_put_raise:
  1166. symbol_put(i915_gpu_raise);
  1167. out_put_mch:
  1168. symbol_put(i915_read_mch_val);
  1169. out_err:
  1170. return false;
  1171. }
  1172. static bool
  1173. ips_gpu_turbo_enabled(struct ips_driver *ips)
  1174. {
  1175. if (!ips->gpu_busy && late_i915_load) {
  1176. if (ips_get_i915_syms(ips)) {
  1177. dev_info(ips->dev,
  1178. "i915 driver attached, reenabling gpu turbo\n");
  1179. ips->gpu_turbo_enabled = !(thm_readl(THM_HTS) & HTS_GTD_DIS);
  1180. }
  1181. }
  1182. return ips->gpu_turbo_enabled;
  1183. }
  1184. void
  1185. ips_link_to_i915_driver(void)
  1186. {
  1187. /* We can't cleanly get at the various ips_driver structs from
  1188. * this caller (the i915 driver), so just set a flag saying
  1189. * that it's time to try getting the symbols again.
  1190. */
  1191. late_i915_load = true;
  1192. }
  1193. EXPORT_SYMBOL_GPL(ips_link_to_i915_driver);
  1194. static const struct pci_device_id ips_id_table[] = {
  1195. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_THERMAL_SENSOR), },
  1196. { 0, }
  1197. };
  1198. MODULE_DEVICE_TABLE(pci, ips_id_table);
  1199. static int ips_blacklist_callback(const struct dmi_system_id *id)
  1200. {
  1201. pr_info("Blacklisted intel_ips for %s\n", id->ident);
  1202. return 1;
  1203. }
  1204. static const struct dmi_system_id ips_blacklist[] = {
  1205. {
  1206. .callback = ips_blacklist_callback,
  1207. .ident = "HP ProBook",
  1208. .matches = {
  1209. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  1210. DMI_MATCH(DMI_PRODUCT_NAME, "HP ProBook"),
  1211. },
  1212. },
  1213. { } /* terminating entry */
  1214. };
  1215. static int ips_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1216. {
  1217. u64 platform_info;
  1218. struct ips_driver *ips;
  1219. u32 hts;
  1220. int ret = 0;
  1221. u16 htshi, trc, trc_required_mask;
  1222. u8 tse;
  1223. if (dmi_check_system(ips_blacklist))
  1224. return -ENODEV;
  1225. ips = devm_kzalloc(&dev->dev, sizeof(*ips), GFP_KERNEL);
  1226. if (!ips)
  1227. return -ENOMEM;
  1228. spin_lock_init(&ips->turbo_status_lock);
  1229. ips->dev = &dev->dev;
  1230. ips->limits = ips_detect_cpu(ips);
  1231. if (!ips->limits) {
  1232. dev_info(&dev->dev, "IPS not supported on this CPU\n");
  1233. return -ENXIO;
  1234. }
  1235. ret = pcim_enable_device(dev);
  1236. if (ret) {
  1237. dev_err(&dev->dev, "can't enable PCI device, aborting\n");
  1238. return ret;
  1239. }
  1240. ret = pcim_iomap_regions(dev, 1 << 0, pci_name(dev));
  1241. if (ret) {
  1242. dev_err(&dev->dev, "failed to map thermal regs, aborting\n");
  1243. return ret;
  1244. }
  1245. ips->regmap = pcim_iomap_table(dev)[0];
  1246. pci_set_drvdata(dev, ips);
  1247. tse = thm_readb(THM_TSE);
  1248. if (tse != TSE_EN) {
  1249. dev_err(&dev->dev, "thermal device not enabled (0x%02x), aborting\n", tse);
  1250. return -ENXIO;
  1251. }
  1252. trc = thm_readw(THM_TRC);
  1253. trc_required_mask = TRC_CORE1_EN | TRC_CORE_PWR | TRC_MCH_EN;
  1254. if ((trc & trc_required_mask) != trc_required_mask) {
  1255. dev_err(&dev->dev, "thermal reporting for required devices not enabled, aborting\n");
  1256. return -ENXIO;
  1257. }
  1258. if (trc & TRC_CORE2_EN)
  1259. ips->second_cpu = true;
  1260. update_turbo_limits(ips);
  1261. dev_dbg(&dev->dev, "max cpu power clamp: %dW\n",
  1262. ips->mcp_power_limit / 10);
  1263. dev_dbg(&dev->dev, "max core power clamp: %dW\n",
  1264. ips->core_power_limit / 10);
  1265. /* BIOS may update limits at runtime */
  1266. if (thm_readl(THM_PSC) & PSP_PBRT)
  1267. ips->poll_turbo_status = true;
  1268. if (!ips_get_i915_syms(ips)) {
  1269. dev_info(&dev->dev, "failed to get i915 symbols, graphics turbo disabled until i915 loads\n");
  1270. ips->gpu_turbo_enabled = false;
  1271. } else {
  1272. dev_dbg(&dev->dev, "graphics turbo enabled\n");
  1273. ips->gpu_turbo_enabled = true;
  1274. }
  1275. /*
  1276. * Check PLATFORM_INFO MSR to make sure this chip is
  1277. * turbo capable.
  1278. */
  1279. rdmsrq(PLATFORM_INFO, platform_info);
  1280. if (!(platform_info & PLATFORM_TDP)) {
  1281. dev_err(&dev->dev, "platform indicates TDP override unavailable, aborting\n");
  1282. return -ENODEV;
  1283. }
  1284. /*
  1285. * IRQ handler for ME interaction
  1286. * Note: don't use MSI here as the PCH has bugs.
  1287. */
  1288. ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_INTX);
  1289. if (ret < 0)
  1290. return ret;
  1291. ips->irq = pci_irq_vector(dev, 0);
  1292. ret = request_irq(ips->irq, ips_irq_handler, IRQF_SHARED, "ips", ips);
  1293. if (ret) {
  1294. dev_err(&dev->dev, "request irq failed, aborting\n");
  1295. return ret;
  1296. }
  1297. /* Enable aux, hot & critical interrupts */
  1298. thm_writeb(THM_TSPIEN, TSPIEN_AUX2_LOHI | TSPIEN_CRIT_LOHI |
  1299. TSPIEN_HOT_LOHI | TSPIEN_AUX_LOHI);
  1300. thm_writeb(THM_TEN, TEN_UPDATE_EN);
  1301. /* Collect adjustment values */
  1302. ips->cta_val = thm_readw(THM_CTA);
  1303. ips->pta_val = thm_readw(THM_PTA);
  1304. ips->mgta_val = thm_readw(THM_MGTA);
  1305. /* Save turbo limits & ratios */
  1306. rdmsrq(TURBO_POWER_CURRENT_LIMIT, ips->orig_turbo_limit);
  1307. ips_disable_cpu_turbo(ips);
  1308. ips->cpu_turbo_enabled = false;
  1309. /* Create thermal adjust thread */
  1310. ips->adjust = kthread_create(ips_adjust, ips, "ips-adjust");
  1311. if (IS_ERR(ips->adjust)) {
  1312. dev_err(&dev->dev,
  1313. "failed to create thermal adjust thread, aborting\n");
  1314. ret = -ENOMEM;
  1315. goto error_free_irq;
  1316. }
  1317. /*
  1318. * Set up the work queue and monitor thread. The monitor thread
  1319. * will wake up ips_adjust thread.
  1320. */
  1321. ips->monitor = kthread_run(ips_monitor, ips, "ips-monitor");
  1322. if (IS_ERR(ips->monitor)) {
  1323. dev_err(&dev->dev,
  1324. "failed to create thermal monitor thread, aborting\n");
  1325. ret = -ENOMEM;
  1326. goto error_thread_cleanup;
  1327. }
  1328. hts = (ips->core_power_limit << HTS_PCPL_SHIFT) |
  1329. (ips->mcp_temp_limit << HTS_PTL_SHIFT) | HTS_NVV;
  1330. htshi = HTS2_PRST_RUNNING << HTS2_PRST_SHIFT;
  1331. thm_writew(THM_HTSHI, htshi);
  1332. thm_writel(THM_HTS, hts);
  1333. ips_debugfs_init(ips);
  1334. dev_info(&dev->dev, "IPS driver initialized, MCP temp limit %d\n",
  1335. ips->mcp_temp_limit);
  1336. return ret;
  1337. error_thread_cleanup:
  1338. kthread_stop(ips->adjust);
  1339. error_free_irq:
  1340. free_irq(ips->irq, ips);
  1341. pci_free_irq_vectors(dev);
  1342. return ret;
  1343. }
  1344. static void ips_remove(struct pci_dev *dev)
  1345. {
  1346. struct ips_driver *ips = pci_get_drvdata(dev);
  1347. u64 turbo_override;
  1348. ips_debugfs_cleanup(ips);
  1349. /* Release i915 driver */
  1350. if (ips->read_mch_val)
  1351. symbol_put(i915_read_mch_val);
  1352. if (ips->gpu_raise)
  1353. symbol_put(i915_gpu_raise);
  1354. if (ips->gpu_lower)
  1355. symbol_put(i915_gpu_lower);
  1356. if (ips->gpu_busy)
  1357. symbol_put(i915_gpu_busy);
  1358. if (ips->gpu_turbo_disable)
  1359. symbol_put(i915_gpu_turbo_disable);
  1360. rdmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  1361. turbo_override &= ~(TURBO_TDC_OVR_EN | TURBO_TDP_OVR_EN);
  1362. wrmsrq(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  1363. wrmsrq(TURBO_POWER_CURRENT_LIMIT, ips->orig_turbo_limit);
  1364. free_irq(ips->irq, ips);
  1365. pci_free_irq_vectors(dev);
  1366. if (ips->adjust)
  1367. kthread_stop(ips->adjust);
  1368. if (ips->monitor)
  1369. kthread_stop(ips->monitor);
  1370. dev_dbg(&dev->dev, "IPS driver removed\n");
  1371. }
  1372. static struct pci_driver ips_pci_driver = {
  1373. .name = "intel ips",
  1374. .id_table = ips_id_table,
  1375. .probe = ips_probe,
  1376. .remove = ips_remove,
  1377. };
  1378. module_pci_driver(ips_pci_driver);
  1379. MODULE_LICENSE("GPL v2");
  1380. MODULE_AUTHOR("Jesse Barnes <jbarnes@virtuousgeek.org>");
  1381. MODULE_DESCRIPTION("Intelligent Power Sharing Driver");