intel_ips.c 43 KB

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