intel_ips.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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. /* Average CPU core temps (all averages in .01 degrees C for precision) */
  279. u16 ctv1_avg_temp;
  280. u16 ctv2_avg_temp;
  281. /* GMCH average */
  282. u16 mch_avg_temp;
  283. /* Average for the CPU (both cores?) */
  284. u16 mcp_avg_temp;
  285. /* Average power consumption (in mW) */
  286. u32 cpu_avg_power;
  287. u32 mch_avg_power;
  288. /* Offset values */
  289. u16 cta_val;
  290. u16 pta_val;
  291. u16 mgta_val;
  292. /* Maximums & prefs, protected by turbo status lock */
  293. spinlock_t turbo_status_lock;
  294. u16 mcp_temp_limit;
  295. u16 mcp_power_limit;
  296. u16 core_power_limit;
  297. u16 mch_power_limit;
  298. bool cpu_turbo_enabled;
  299. bool __cpu_turbo_on;
  300. bool gpu_turbo_enabled;
  301. bool __gpu_turbo_on;
  302. bool gpu_preferred;
  303. bool poll_turbo_status;
  304. bool second_cpu;
  305. bool turbo_toggle_allowed;
  306. struct ips_mcp_limits *limits;
  307. /* Optional MCH interfaces for if i915 is in use */
  308. unsigned long (*read_mch_val)(void);
  309. bool (*gpu_raise)(void);
  310. bool (*gpu_lower)(void);
  311. bool (*gpu_busy)(void);
  312. bool (*gpu_turbo_disable)(void);
  313. /* For restoration at unload */
  314. u64 orig_turbo_limit;
  315. u64 orig_turbo_ratios;
  316. };
  317. static bool
  318. ips_gpu_turbo_enabled(struct ips_driver *ips);
  319. /**
  320. * ips_cpu_busy - is CPU busy?
  321. * @ips: IPS driver struct
  322. *
  323. * Check CPU for load to see whether we should increase its thermal budget.
  324. *
  325. * RETURNS:
  326. * True if the CPU could use more power, false otherwise.
  327. */
  328. static bool ips_cpu_busy(struct ips_driver *ips)
  329. {
  330. if ((avenrun[0] >> FSHIFT) > 1)
  331. return true;
  332. return false;
  333. }
  334. /**
  335. * ips_cpu_raise - raise CPU power clamp
  336. * @ips: IPS driver struct
  337. *
  338. * Raise the CPU power clamp by %IPS_CPU_STEP, in accordance with TDP for
  339. * this platform.
  340. *
  341. * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR upwards (as
  342. * long as we haven't hit the TDP limit for the SKU).
  343. */
  344. static void ips_cpu_raise(struct ips_driver *ips)
  345. {
  346. u64 turbo_override;
  347. u16 cur_tdp_limit, new_tdp_limit;
  348. if (!ips->cpu_turbo_enabled)
  349. return;
  350. rdmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  351. cur_tdp_limit = turbo_override & TURBO_TDP_MASK;
  352. new_tdp_limit = cur_tdp_limit + 8; /* 1W increase */
  353. /* Clamp to SKU TDP limit */
  354. if (((new_tdp_limit * 10) / 8) > ips->core_power_limit)
  355. new_tdp_limit = cur_tdp_limit;
  356. thm_writew(THM_MPCPC, (new_tdp_limit * 10) / 8);
  357. turbo_override |= TURBO_TDC_OVR_EN | TURBO_TDP_OVR_EN;
  358. wrmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  359. turbo_override &= ~TURBO_TDP_MASK;
  360. turbo_override |= new_tdp_limit;
  361. wrmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  362. }
  363. /**
  364. * ips_cpu_lower - lower CPU power clamp
  365. * @ips: IPS driver struct
  366. *
  367. * Lower CPU power clamp b %IPS_CPU_STEP if possible.
  368. *
  369. * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR down, going
  370. * as low as the platform limits will allow (though we could go lower there
  371. * wouldn't be much point).
  372. */
  373. static void ips_cpu_lower(struct ips_driver *ips)
  374. {
  375. u64 turbo_override;
  376. u16 cur_limit, new_limit;
  377. rdmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  378. cur_limit = turbo_override & TURBO_TDP_MASK;
  379. new_limit = cur_limit - 8; /* 1W decrease */
  380. /* Clamp to SKU TDP limit */
  381. if (new_limit < (ips->orig_turbo_limit & TURBO_TDP_MASK))
  382. new_limit = ips->orig_turbo_limit & TURBO_TDP_MASK;
  383. thm_writew(THM_MPCPC, (new_limit * 10) / 8);
  384. turbo_override |= TURBO_TDC_OVR_EN | TURBO_TDP_OVR_EN;
  385. wrmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  386. turbo_override &= ~TURBO_TDP_MASK;
  387. turbo_override |= new_limit;
  388. wrmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  389. }
  390. /**
  391. * do_enable_cpu_turbo - internal turbo enable function
  392. * @data: unused
  393. *
  394. * Internal function for actually updating MSRs. When we enable/disable
  395. * turbo, we need to do it on each CPU; this function is the one called
  396. * by on_each_cpu() when needed.
  397. */
  398. static void do_enable_cpu_turbo(void *data)
  399. {
  400. u64 perf_ctl;
  401. rdmsrl(IA32_PERF_CTL, perf_ctl);
  402. if (perf_ctl & IA32_PERF_TURBO_DIS) {
  403. perf_ctl &= ~IA32_PERF_TURBO_DIS;
  404. wrmsrl(IA32_PERF_CTL, perf_ctl);
  405. }
  406. }
  407. /**
  408. * ips_enable_cpu_turbo - enable turbo mode on all CPUs
  409. * @ips: IPS driver struct
  410. *
  411. * Enable turbo mode by clearing the disable bit in IA32_PERF_CTL on
  412. * all logical threads.
  413. */
  414. static void ips_enable_cpu_turbo(struct ips_driver *ips)
  415. {
  416. /* Already on, no need to mess with MSRs */
  417. if (ips->__cpu_turbo_on)
  418. return;
  419. if (ips->turbo_toggle_allowed)
  420. on_each_cpu(do_enable_cpu_turbo, ips, 1);
  421. ips->__cpu_turbo_on = true;
  422. }
  423. /**
  424. * do_disable_cpu_turbo - internal turbo disable function
  425. * @data: unused
  426. *
  427. * Internal function for actually updating MSRs. When we enable/disable
  428. * turbo, we need to do it on each CPU; this function is the one called
  429. * by on_each_cpu() when needed.
  430. */
  431. static void do_disable_cpu_turbo(void *data)
  432. {
  433. u64 perf_ctl;
  434. rdmsrl(IA32_PERF_CTL, perf_ctl);
  435. if (!(perf_ctl & IA32_PERF_TURBO_DIS)) {
  436. perf_ctl |= IA32_PERF_TURBO_DIS;
  437. wrmsrl(IA32_PERF_CTL, perf_ctl);
  438. }
  439. }
  440. /**
  441. * ips_disable_cpu_turbo - disable turbo mode on all CPUs
  442. * @ips: IPS driver struct
  443. *
  444. * Disable turbo mode by setting the disable bit in IA32_PERF_CTL on
  445. * all logical threads.
  446. */
  447. static void ips_disable_cpu_turbo(struct ips_driver *ips)
  448. {
  449. /* Already off, leave it */
  450. if (!ips->__cpu_turbo_on)
  451. return;
  452. if (ips->turbo_toggle_allowed)
  453. on_each_cpu(do_disable_cpu_turbo, ips, 1);
  454. ips->__cpu_turbo_on = false;
  455. }
  456. /**
  457. * ips_gpu_busy - is GPU busy?
  458. * @ips: IPS driver struct
  459. *
  460. * Check GPU for load to see whether we should increase its thermal budget.
  461. * We need to call into the i915 driver in this case.
  462. *
  463. * RETURNS:
  464. * True if the GPU could use more power, false otherwise.
  465. */
  466. static bool ips_gpu_busy(struct ips_driver *ips)
  467. {
  468. if (!ips_gpu_turbo_enabled(ips))
  469. return false;
  470. return ips->gpu_busy();
  471. }
  472. /**
  473. * ips_gpu_raise - raise GPU power clamp
  474. * @ips: IPS driver struct
  475. *
  476. * Raise the GPU frequency/power if possible. We need to call into the
  477. * i915 driver in this case.
  478. */
  479. static void ips_gpu_raise(struct ips_driver *ips)
  480. {
  481. if (!ips_gpu_turbo_enabled(ips))
  482. return;
  483. if (!ips->gpu_raise())
  484. ips->gpu_turbo_enabled = false;
  485. return;
  486. }
  487. /**
  488. * ips_gpu_lower - lower GPU power clamp
  489. * @ips: IPS driver struct
  490. *
  491. * Lower GPU frequency/power if possible. Need to call i915.
  492. */
  493. static void ips_gpu_lower(struct ips_driver *ips)
  494. {
  495. if (!ips_gpu_turbo_enabled(ips))
  496. return;
  497. if (!ips->gpu_lower())
  498. ips->gpu_turbo_enabled = false;
  499. return;
  500. }
  501. /**
  502. * ips_enable_gpu_turbo - notify the gfx driver turbo is available
  503. * @ips: IPS driver struct
  504. *
  505. * Call into the graphics driver indicating that it can safely use
  506. * turbo mode.
  507. */
  508. static void ips_enable_gpu_turbo(struct ips_driver *ips)
  509. {
  510. if (ips->__gpu_turbo_on)
  511. return;
  512. ips->__gpu_turbo_on = true;
  513. }
  514. /**
  515. * ips_disable_gpu_turbo - notify the gfx driver to disable turbo mode
  516. * @ips: IPS driver struct
  517. *
  518. * Request that the graphics driver disable turbo mode.
  519. */
  520. static void ips_disable_gpu_turbo(struct ips_driver *ips)
  521. {
  522. /* Avoid calling i915 if turbo is already disabled */
  523. if (!ips->__gpu_turbo_on)
  524. return;
  525. if (!ips->gpu_turbo_disable())
  526. dev_err(ips->dev, "failed to disable graphics turbo\n");
  527. else
  528. ips->__gpu_turbo_on = false;
  529. }
  530. /**
  531. * mcp_exceeded - check whether we're outside our thermal & power limits
  532. * @ips: IPS driver struct
  533. *
  534. * Check whether the MCP is over its thermal or power budget.
  535. */
  536. static bool mcp_exceeded(struct ips_driver *ips)
  537. {
  538. unsigned long flags;
  539. bool ret = false;
  540. u32 temp_limit;
  541. u32 avg_power;
  542. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  543. temp_limit = ips->mcp_temp_limit * 100;
  544. if (ips->mcp_avg_temp > temp_limit)
  545. ret = true;
  546. avg_power = ips->cpu_avg_power + ips->mch_avg_power;
  547. if (avg_power > ips->mcp_power_limit)
  548. ret = true;
  549. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  550. return ret;
  551. }
  552. /**
  553. * cpu_exceeded - check whether a CPU core is outside its limits
  554. * @ips: IPS driver struct
  555. * @cpu: CPU number to check
  556. *
  557. * Check a given CPU's average temp or power is over its limit.
  558. */
  559. static bool cpu_exceeded(struct ips_driver *ips, int cpu)
  560. {
  561. unsigned long flags;
  562. int avg;
  563. bool ret = false;
  564. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  565. avg = cpu ? ips->ctv2_avg_temp : ips->ctv1_avg_temp;
  566. if (avg > (ips->limits->core_temp_limit * 100))
  567. ret = true;
  568. if (ips->cpu_avg_power > ips->core_power_limit * 100)
  569. ret = true;
  570. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  571. if (ret)
  572. dev_info(ips->dev, "CPU power or thermal limit exceeded\n");
  573. return ret;
  574. }
  575. /**
  576. * mch_exceeded - check whether the GPU is over budget
  577. * @ips: IPS driver struct
  578. *
  579. * Check the MCH temp & power against their maximums.
  580. */
  581. static bool mch_exceeded(struct ips_driver *ips)
  582. {
  583. unsigned long flags;
  584. bool ret = false;
  585. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  586. if (ips->mch_avg_temp > (ips->limits->mch_temp_limit * 100))
  587. ret = true;
  588. if (ips->mch_avg_power > ips->mch_power_limit)
  589. ret = true;
  590. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  591. return ret;
  592. }
  593. /**
  594. * verify_limits - verify BIOS provided limits
  595. * @ips: IPS structure
  596. *
  597. * BIOS can optionally provide non-default limits for power and temp. Check
  598. * them here and use the defaults if the BIOS values are not provided or
  599. * are otherwise unusable.
  600. */
  601. static void verify_limits(struct ips_driver *ips)
  602. {
  603. if (ips->mcp_power_limit < ips->limits->mcp_power_limit ||
  604. ips->mcp_power_limit > 35000)
  605. ips->mcp_power_limit = ips->limits->mcp_power_limit;
  606. if (ips->mcp_temp_limit < ips->limits->core_temp_limit ||
  607. ips->mcp_temp_limit < ips->limits->mch_temp_limit ||
  608. ips->mcp_temp_limit > 150)
  609. ips->mcp_temp_limit = min(ips->limits->core_temp_limit,
  610. ips->limits->mch_temp_limit);
  611. }
  612. /**
  613. * update_turbo_limits - get various limits & settings from regs
  614. * @ips: IPS driver struct
  615. *
  616. * Update the IPS power & temp limits, along with turbo enable flags,
  617. * based on latest register contents.
  618. *
  619. * Used at init time and for runtime BIOS support, which requires polling
  620. * the regs for updates (as a result of AC->DC transition for example).
  621. *
  622. * LOCKING:
  623. * Caller must hold turbo_status_lock (outside of init)
  624. */
  625. static void update_turbo_limits(struct ips_driver *ips)
  626. {
  627. u32 hts = thm_readl(THM_HTS);
  628. ips->cpu_turbo_enabled = !(hts & HTS_PCTD_DIS);
  629. /*
  630. * Disable turbo for now, until we can figure out why the power figures
  631. * are wrong
  632. */
  633. ips->cpu_turbo_enabled = false;
  634. if (ips->gpu_busy)
  635. ips->gpu_turbo_enabled = !(hts & HTS_GTD_DIS);
  636. ips->core_power_limit = thm_readw(THM_MPCPC);
  637. ips->mch_power_limit = thm_readw(THM_MMGPC);
  638. ips->mcp_temp_limit = thm_readw(THM_PTL);
  639. ips->mcp_power_limit = thm_readw(THM_MPPC);
  640. verify_limits(ips);
  641. /* Ignore BIOS CPU vs GPU pref */
  642. }
  643. /**
  644. * ips_adjust - adjust power clamp based on thermal state
  645. * @data: ips driver structure
  646. *
  647. * Wake up every 5s or so and check whether we should adjust the power clamp.
  648. * Check CPU and GPU load to determine which needs adjustment. There are
  649. * several things to consider here:
  650. * - do we need to adjust up or down?
  651. * - is CPU busy?
  652. * - is GPU busy?
  653. * - is CPU in turbo?
  654. * - is GPU in turbo?
  655. * - is CPU or GPU preferred? (CPU is default)
  656. *
  657. * So, given the above, we do the following:
  658. * - up (TDP available)
  659. * - CPU not busy, GPU not busy - nothing
  660. * - CPU busy, GPU not busy - adjust CPU up
  661. * - CPU not busy, GPU busy - adjust GPU up
  662. * - CPU busy, GPU busy - adjust preferred unit up, taking headroom from
  663. * non-preferred unit if necessary
  664. * - down (at TDP limit)
  665. * - adjust both CPU and GPU down if possible
  666. *
  667. cpu+ gpu+ cpu+gpu- cpu-gpu+ cpu-gpu-
  668. cpu < gpu < cpu+gpu+ cpu+ gpu+ nothing
  669. cpu < gpu >= cpu+gpu-(mcp<) cpu+gpu-(mcp<) gpu- gpu-
  670. cpu >= gpu < cpu-gpu+(mcp<) cpu- cpu-gpu+(mcp<) cpu-
  671. cpu >= gpu >= cpu-gpu- cpu-gpu- cpu-gpu- cpu-gpu-
  672. *
  673. */
  674. static int ips_adjust(void *data)
  675. {
  676. struct ips_driver *ips = data;
  677. unsigned long flags;
  678. dev_dbg(ips->dev, "starting ips-adjust thread\n");
  679. /*
  680. * Adjust CPU and GPU clamps every 5s if needed. Doing it more
  681. * often isn't recommended due to ME interaction.
  682. */
  683. do {
  684. bool cpu_busy = ips_cpu_busy(ips);
  685. bool gpu_busy = ips_gpu_busy(ips);
  686. spin_lock_irqsave(&ips->turbo_status_lock, flags);
  687. if (ips->poll_turbo_status)
  688. update_turbo_limits(ips);
  689. spin_unlock_irqrestore(&ips->turbo_status_lock, flags);
  690. /* Update turbo status if necessary */
  691. if (ips->cpu_turbo_enabled)
  692. ips_enable_cpu_turbo(ips);
  693. else
  694. ips_disable_cpu_turbo(ips);
  695. if (ips->gpu_turbo_enabled)
  696. ips_enable_gpu_turbo(ips);
  697. else
  698. ips_disable_gpu_turbo(ips);
  699. /* We're outside our comfort zone, crank them down */
  700. if (mcp_exceeded(ips)) {
  701. ips_cpu_lower(ips);
  702. ips_gpu_lower(ips);
  703. goto sleep;
  704. }
  705. if (!cpu_exceeded(ips, 0) && cpu_busy)
  706. ips_cpu_raise(ips);
  707. else
  708. ips_cpu_lower(ips);
  709. if (!mch_exceeded(ips) && gpu_busy)
  710. ips_gpu_raise(ips);
  711. else
  712. ips_gpu_lower(ips);
  713. sleep:
  714. schedule_timeout_interruptible(msecs_to_jiffies(IPS_ADJUST_PERIOD));
  715. } while (!kthread_should_stop());
  716. dev_dbg(ips->dev, "ips-adjust thread stopped\n");
  717. return 0;
  718. }
  719. /*
  720. * Helpers for reading out temp/power values and calculating their
  721. * averages for the decision making and monitoring functions.
  722. */
  723. static u16 calc_avg_temp(struct ips_driver *ips, u16 *array)
  724. {
  725. u64 total = 0;
  726. int i;
  727. u16 avg;
  728. for (i = 0; i < IPS_SAMPLE_COUNT; i++)
  729. total += (u64)(array[i] * 100);
  730. do_div(total, IPS_SAMPLE_COUNT);
  731. avg = (u16)total;
  732. return avg;
  733. }
  734. static u16 read_mgtv(struct ips_driver *ips)
  735. {
  736. u16 ret;
  737. u64 slope, offset;
  738. u64 val;
  739. val = thm_readq(THM_MGTV);
  740. val = (val & TV_MASK) >> TV_SHIFT;
  741. slope = offset = thm_readw(THM_MGTA);
  742. slope = (slope & MGTA_SLOPE_MASK) >> MGTA_SLOPE_SHIFT;
  743. offset = offset & MGTA_OFFSET_MASK;
  744. ret = ((val * slope + 0x40) >> 7) + offset;
  745. return 0; /* MCH temp reporting buggy */
  746. }
  747. static u16 read_ptv(struct ips_driver *ips)
  748. {
  749. u16 val, slope, offset;
  750. slope = (ips->pta_val & PTA_SLOPE_MASK) >> PTA_SLOPE_SHIFT;
  751. offset = ips->pta_val & PTA_OFFSET_MASK;
  752. val = thm_readw(THM_PTV) & PTV_MASK;
  753. return val;
  754. }
  755. static u16 read_ctv(struct ips_driver *ips, int cpu)
  756. {
  757. int reg = cpu ? THM_CTV2 : THM_CTV1;
  758. u16 val;
  759. val = thm_readw(reg);
  760. if (!(val & CTV_TEMP_ERROR))
  761. val = (val) >> 6; /* discard fractional component */
  762. else
  763. val = 0;
  764. return val;
  765. }
  766. static u32 get_cpu_power(struct ips_driver *ips, u32 *last, int period)
  767. {
  768. u32 val;
  769. u32 ret;
  770. /*
  771. * CEC is in joules/65535. Take difference over time to
  772. * get watts.
  773. */
  774. val = thm_readl(THM_CEC);
  775. /* period is in ms and we want mW */
  776. ret = (((val - *last) * 1000) / period);
  777. ret = (ret * 1000) / 65535;
  778. *last = val;
  779. return 0;
  780. }
  781. static const u16 temp_decay_factor = 2;
  782. static u16 update_average_temp(u16 avg, u16 val)
  783. {
  784. u16 ret;
  785. /* Multiply by 100 for extra precision */
  786. ret = (val * 100 / temp_decay_factor) +
  787. (((temp_decay_factor - 1) * avg) / temp_decay_factor);
  788. return ret;
  789. }
  790. static const u16 power_decay_factor = 2;
  791. static u16 update_average_power(u32 avg, u32 val)
  792. {
  793. u32 ret;
  794. ret = (val / power_decay_factor) +
  795. (((power_decay_factor - 1) * avg) / power_decay_factor);
  796. return ret;
  797. }
  798. static u32 calc_avg_power(struct ips_driver *ips, u32 *array)
  799. {
  800. u64 total = 0;
  801. u32 avg;
  802. int i;
  803. for (i = 0; i < IPS_SAMPLE_COUNT; i++)
  804. total += array[i];
  805. do_div(total, IPS_SAMPLE_COUNT);
  806. avg = (u32)total;
  807. return avg;
  808. }
  809. static void monitor_timeout(unsigned long arg)
  810. {
  811. wake_up_process((struct task_struct *)arg);
  812. }
  813. /**
  814. * ips_monitor - temp/power monitoring thread
  815. * @data: ips driver structure
  816. *
  817. * This is the main function for the IPS driver. It monitors power and
  818. * tempurature in the MCP and adjusts CPU and GPU power clams accordingly.
  819. *
  820. * We keep a 5s moving average of power consumption and tempurature. Using
  821. * that data, along with CPU vs GPU preference, we adjust the power clamps
  822. * up or down.
  823. */
  824. static int ips_monitor(void *data)
  825. {
  826. struct ips_driver *ips = data;
  827. struct timer_list timer;
  828. unsigned long seqno_timestamp, expire, last_msecs, last_sample_period;
  829. int i;
  830. u32 *cpu_samples, *mchp_samples, old_cpu_power;
  831. u16 *mcp_samples, *ctv1_samples, *ctv2_samples, *mch_samples;
  832. u8 cur_seqno, last_seqno;
  833. mcp_samples = kzalloc(sizeof(u16) * IPS_SAMPLE_COUNT, GFP_KERNEL);
  834. ctv1_samples = kzalloc(sizeof(u16) * IPS_SAMPLE_COUNT, GFP_KERNEL);
  835. ctv2_samples = kzalloc(sizeof(u16) * IPS_SAMPLE_COUNT, GFP_KERNEL);
  836. mch_samples = kzalloc(sizeof(u16) * IPS_SAMPLE_COUNT, GFP_KERNEL);
  837. cpu_samples = kzalloc(sizeof(u32) * IPS_SAMPLE_COUNT, GFP_KERNEL);
  838. mchp_samples = kzalloc(sizeof(u32) * IPS_SAMPLE_COUNT, GFP_KERNEL);
  839. if (!mcp_samples || !ctv1_samples || !ctv2_samples || !mch_samples ||
  840. !cpu_samples || !mchp_samples) {
  841. dev_err(ips->dev,
  842. "failed to allocate sample array, ips disabled\n");
  843. kfree(mcp_samples);
  844. kfree(ctv1_samples);
  845. kfree(ctv2_samples);
  846. kfree(mch_samples);
  847. kfree(cpu_samples);
  848. kfree(mchp_samples);
  849. return -ENOMEM;
  850. }
  851. last_seqno = (thm_readl(THM_ITV) & ITV_ME_SEQNO_MASK) >>
  852. ITV_ME_SEQNO_SHIFT;
  853. seqno_timestamp = get_jiffies_64();
  854. old_cpu_power = thm_readl(THM_CEC);
  855. schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
  856. /* Collect an initial average */
  857. for (i = 0; i < IPS_SAMPLE_COUNT; i++) {
  858. u32 mchp, cpu_power;
  859. u16 val;
  860. mcp_samples[i] = read_ptv(ips);
  861. val = read_ctv(ips, 0);
  862. ctv1_samples[i] = val;
  863. val = read_ctv(ips, 1);
  864. ctv2_samples[i] = val;
  865. val = read_mgtv(ips);
  866. mch_samples[i] = val;
  867. cpu_power = get_cpu_power(ips, &old_cpu_power,
  868. IPS_SAMPLE_PERIOD);
  869. cpu_samples[i] = cpu_power;
  870. if (ips->read_mch_val) {
  871. mchp = ips->read_mch_val();
  872. mchp_samples[i] = mchp;
  873. }
  874. schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
  875. if (kthread_should_stop())
  876. break;
  877. }
  878. ips->mcp_avg_temp = calc_avg_temp(ips, mcp_samples);
  879. ips->ctv1_avg_temp = calc_avg_temp(ips, ctv1_samples);
  880. ips->ctv2_avg_temp = calc_avg_temp(ips, ctv2_samples);
  881. ips->mch_avg_temp = calc_avg_temp(ips, mch_samples);
  882. ips->cpu_avg_power = calc_avg_power(ips, cpu_samples);
  883. ips->mch_avg_power = calc_avg_power(ips, mchp_samples);
  884. kfree(mcp_samples);
  885. kfree(ctv1_samples);
  886. kfree(ctv2_samples);
  887. kfree(mch_samples);
  888. kfree(cpu_samples);
  889. kfree(mchp_samples);
  890. /* Start the adjustment thread now that we have data */
  891. wake_up_process(ips->adjust);
  892. /*
  893. * Ok, now we have an initial avg. From here on out, we track the
  894. * running avg using a decaying average calculation. This allows
  895. * us to reduce the sample frequency if the CPU and GPU are idle.
  896. */
  897. old_cpu_power = thm_readl(THM_CEC);
  898. schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD));
  899. last_sample_period = IPS_SAMPLE_PERIOD;
  900. setup_deferrable_timer_on_stack(&timer, monitor_timeout,
  901. (unsigned long)current);
  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(&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(&timer);
  960. destroy_timer_on_stack(&timer);
  961. dev_dbg(ips->dev, "ips-monitor thread stopped\n");
  962. return 0;
  963. }
  964. #if 0
  965. #define THM_DUMPW(reg) \
  966. { \
  967. u16 val = thm_readw(reg); \
  968. dev_dbg(ips->dev, #reg ": 0x%04x\n", val); \
  969. }
  970. #define THM_DUMPL(reg) \
  971. { \
  972. u32 val = thm_readl(reg); \
  973. dev_dbg(ips->dev, #reg ": 0x%08x\n", val); \
  974. }
  975. #define THM_DUMPQ(reg) \
  976. { \
  977. u64 val = thm_readq(reg); \
  978. dev_dbg(ips->dev, #reg ": 0x%016x\n", val); \
  979. }
  980. static void dump_thermal_info(struct ips_driver *ips)
  981. {
  982. u16 ptl;
  983. ptl = thm_readw(THM_PTL);
  984. dev_dbg(ips->dev, "Processor temp limit: %d\n", ptl);
  985. THM_DUMPW(THM_CTA);
  986. THM_DUMPW(THM_TRC);
  987. THM_DUMPW(THM_CTV1);
  988. THM_DUMPL(THM_STS);
  989. THM_DUMPW(THM_PTV);
  990. THM_DUMPQ(THM_MGTV);
  991. }
  992. #endif
  993. /**
  994. * ips_irq_handler - handle temperature triggers and other IPS events
  995. * @irq: irq number
  996. * @arg: unused
  997. *
  998. * Handle temperature limit trigger events, generally by lowering the clamps.
  999. * If we're at a critical limit, we clamp back to the lowest possible value
  1000. * to prevent emergency shutdown.
  1001. */
  1002. static irqreturn_t ips_irq_handler(int irq, void *arg)
  1003. {
  1004. struct ips_driver *ips = arg;
  1005. u8 tses = thm_readb(THM_TSES);
  1006. u8 tes = thm_readb(THM_TES);
  1007. if (!tses && !tes)
  1008. return IRQ_NONE;
  1009. dev_info(ips->dev, "TSES: 0x%02x\n", tses);
  1010. dev_info(ips->dev, "TES: 0x%02x\n", tes);
  1011. /* STS update from EC? */
  1012. if (tes & 1) {
  1013. u32 sts, tc1;
  1014. sts = thm_readl(THM_STS);
  1015. tc1 = thm_readl(THM_TC1);
  1016. if (sts & STS_NVV) {
  1017. spin_lock(&ips->turbo_status_lock);
  1018. ips->core_power_limit = (sts & STS_PCPL_MASK) >>
  1019. STS_PCPL_SHIFT;
  1020. ips->mch_power_limit = (sts & STS_GPL_MASK) >>
  1021. STS_GPL_SHIFT;
  1022. /* ignore EC CPU vs GPU pref */
  1023. ips->cpu_turbo_enabled = !(sts & STS_PCTD_DIS);
  1024. /*
  1025. * Disable turbo for now, until we can figure
  1026. * out why the power figures are wrong
  1027. */
  1028. ips->cpu_turbo_enabled = false;
  1029. if (ips->gpu_busy)
  1030. ips->gpu_turbo_enabled = !(sts & STS_GTD_DIS);
  1031. ips->mcp_temp_limit = (sts & STS_PTL_MASK) >>
  1032. STS_PTL_SHIFT;
  1033. ips->mcp_power_limit = (tc1 & STS_PPL_MASK) >>
  1034. STS_PPL_SHIFT;
  1035. verify_limits(ips);
  1036. spin_unlock(&ips->turbo_status_lock);
  1037. thm_writeb(THM_SEC, SEC_ACK);
  1038. }
  1039. thm_writeb(THM_TES, tes);
  1040. }
  1041. /* Thermal trip */
  1042. if (tses) {
  1043. dev_warn(ips->dev, "thermal trip occurred, tses: 0x%04x\n",
  1044. tses);
  1045. thm_writeb(THM_TSES, tses);
  1046. }
  1047. return IRQ_HANDLED;
  1048. }
  1049. #ifndef CONFIG_DEBUG_FS
  1050. static void ips_debugfs_init(struct ips_driver *ips) { return; }
  1051. static void ips_debugfs_cleanup(struct ips_driver *ips) { return; }
  1052. #else
  1053. /* Expose current state and limits in debugfs if possible */
  1054. struct ips_debugfs_node {
  1055. struct ips_driver *ips;
  1056. char *name;
  1057. int (*show)(struct seq_file *m, void *data);
  1058. };
  1059. static int show_cpu_temp(struct seq_file *m, void *data)
  1060. {
  1061. struct ips_driver *ips = m->private;
  1062. seq_printf(m, "%d.%02d\n", ips->ctv1_avg_temp / 100,
  1063. ips->ctv1_avg_temp % 100);
  1064. return 0;
  1065. }
  1066. static int show_cpu_power(struct seq_file *m, void *data)
  1067. {
  1068. struct ips_driver *ips = m->private;
  1069. seq_printf(m, "%dmW\n", ips->cpu_avg_power);
  1070. return 0;
  1071. }
  1072. static int show_cpu_clamp(struct seq_file *m, void *data)
  1073. {
  1074. u64 turbo_override;
  1075. int tdp, tdc;
  1076. rdmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  1077. tdp = (int)(turbo_override & TURBO_TDP_MASK);
  1078. tdc = (int)((turbo_override & TURBO_TDC_MASK) >> TURBO_TDC_SHIFT);
  1079. /* Convert to .1W/A units */
  1080. tdp = tdp * 10 / 8;
  1081. tdc = tdc * 10 / 8;
  1082. /* Watts Amperes */
  1083. seq_printf(m, "%d.%dW %d.%dA\n", tdp / 10, tdp % 10,
  1084. tdc / 10, tdc % 10);
  1085. return 0;
  1086. }
  1087. static int show_mch_temp(struct seq_file *m, void *data)
  1088. {
  1089. struct ips_driver *ips = m->private;
  1090. seq_printf(m, "%d.%02d\n", ips->mch_avg_temp / 100,
  1091. ips->mch_avg_temp % 100);
  1092. return 0;
  1093. }
  1094. static int show_mch_power(struct seq_file *m, void *data)
  1095. {
  1096. struct ips_driver *ips = m->private;
  1097. seq_printf(m, "%dmW\n", ips->mch_avg_power);
  1098. return 0;
  1099. }
  1100. static struct ips_debugfs_node ips_debug_files[] = {
  1101. { NULL, "cpu_temp", show_cpu_temp },
  1102. { NULL, "cpu_power", show_cpu_power },
  1103. { NULL, "cpu_clamp", show_cpu_clamp },
  1104. { NULL, "mch_temp", show_mch_temp },
  1105. { NULL, "mch_power", show_mch_power },
  1106. };
  1107. static int ips_debugfs_open(struct inode *inode, struct file *file)
  1108. {
  1109. struct ips_debugfs_node *node = inode->i_private;
  1110. return single_open(file, node->show, node->ips);
  1111. }
  1112. static const struct file_operations ips_debugfs_ops = {
  1113. .owner = THIS_MODULE,
  1114. .open = ips_debugfs_open,
  1115. .read = seq_read,
  1116. .llseek = seq_lseek,
  1117. .release = single_release,
  1118. };
  1119. static void ips_debugfs_cleanup(struct ips_driver *ips)
  1120. {
  1121. if (ips->debug_root)
  1122. debugfs_remove_recursive(ips->debug_root);
  1123. return;
  1124. }
  1125. static void ips_debugfs_init(struct ips_driver *ips)
  1126. {
  1127. int i;
  1128. ips->debug_root = debugfs_create_dir("ips", NULL);
  1129. if (!ips->debug_root) {
  1130. dev_err(ips->dev, "failed to create debugfs entries: %ld\n",
  1131. PTR_ERR(ips->debug_root));
  1132. return;
  1133. }
  1134. for (i = 0; i < ARRAY_SIZE(ips_debug_files); i++) {
  1135. struct dentry *ent;
  1136. struct ips_debugfs_node *node = &ips_debug_files[i];
  1137. node->ips = ips;
  1138. ent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
  1139. ips->debug_root, node,
  1140. &ips_debugfs_ops);
  1141. if (!ent) {
  1142. dev_err(ips->dev, "failed to create debug file: %ld\n",
  1143. PTR_ERR(ent));
  1144. goto err_cleanup;
  1145. }
  1146. }
  1147. return;
  1148. err_cleanup:
  1149. ips_debugfs_cleanup(ips);
  1150. return;
  1151. }
  1152. #endif /* CONFIG_DEBUG_FS */
  1153. /**
  1154. * ips_detect_cpu - detect whether CPU supports IPS
  1155. *
  1156. * Walk our list and see if we're on a supported CPU. If we find one,
  1157. * return the limits for it.
  1158. */
  1159. static struct ips_mcp_limits *ips_detect_cpu(struct ips_driver *ips)
  1160. {
  1161. u64 turbo_power, misc_en;
  1162. struct ips_mcp_limits *limits = NULL;
  1163. u16 tdp;
  1164. if (!(boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model == 37)) {
  1165. dev_info(ips->dev, "Non-IPS CPU detected.\n");
  1166. return NULL;
  1167. }
  1168. rdmsrl(IA32_MISC_ENABLE, misc_en);
  1169. /*
  1170. * If the turbo enable bit isn't set, we shouldn't try to enable/disable
  1171. * turbo manually or we'll get an illegal MSR access, even though
  1172. * turbo will still be available.
  1173. */
  1174. if (misc_en & IA32_MISC_TURBO_EN)
  1175. ips->turbo_toggle_allowed = true;
  1176. else
  1177. ips->turbo_toggle_allowed = false;
  1178. if (strstr(boot_cpu_data.x86_model_id, "CPU M"))
  1179. limits = &ips_sv_limits;
  1180. else if (strstr(boot_cpu_data.x86_model_id, "CPU L"))
  1181. limits = &ips_lv_limits;
  1182. else if (strstr(boot_cpu_data.x86_model_id, "CPU U"))
  1183. limits = &ips_ulv_limits;
  1184. else {
  1185. dev_info(ips->dev, "No CPUID match found.\n");
  1186. return NULL;
  1187. }
  1188. rdmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_power);
  1189. tdp = turbo_power & TURBO_TDP_MASK;
  1190. /* Sanity check TDP against CPU */
  1191. if (limits->core_power_limit != (tdp / 8) * 1000) {
  1192. dev_info(ips->dev,
  1193. "CPU TDP doesn't match expected value (found %d, expected %d)\n",
  1194. tdp / 8, limits->core_power_limit / 1000);
  1195. limits->core_power_limit = (tdp / 8) * 1000;
  1196. }
  1197. return limits;
  1198. }
  1199. /**
  1200. * ips_get_i915_syms - try to get GPU control methods from i915 driver
  1201. * @ips: IPS driver
  1202. *
  1203. * The i915 driver exports several interfaces to allow the IPS driver to
  1204. * monitor and control graphics turbo mode. If we can find them, we can
  1205. * enable graphics turbo, otherwise we must disable it to avoid exceeding
  1206. * thermal and power limits in the MCP.
  1207. */
  1208. static bool ips_get_i915_syms(struct ips_driver *ips)
  1209. {
  1210. ips->read_mch_val = symbol_get(i915_read_mch_val);
  1211. if (!ips->read_mch_val)
  1212. goto out_err;
  1213. ips->gpu_raise = symbol_get(i915_gpu_raise);
  1214. if (!ips->gpu_raise)
  1215. goto out_put_mch;
  1216. ips->gpu_lower = symbol_get(i915_gpu_lower);
  1217. if (!ips->gpu_lower)
  1218. goto out_put_raise;
  1219. ips->gpu_busy = symbol_get(i915_gpu_busy);
  1220. if (!ips->gpu_busy)
  1221. goto out_put_lower;
  1222. ips->gpu_turbo_disable = symbol_get(i915_gpu_turbo_disable);
  1223. if (!ips->gpu_turbo_disable)
  1224. goto out_put_busy;
  1225. return true;
  1226. out_put_busy:
  1227. symbol_put(i915_gpu_busy);
  1228. out_put_lower:
  1229. symbol_put(i915_gpu_lower);
  1230. out_put_raise:
  1231. symbol_put(i915_gpu_raise);
  1232. out_put_mch:
  1233. symbol_put(i915_read_mch_val);
  1234. out_err:
  1235. return false;
  1236. }
  1237. static bool
  1238. ips_gpu_turbo_enabled(struct ips_driver *ips)
  1239. {
  1240. if (!ips->gpu_busy && late_i915_load) {
  1241. if (ips_get_i915_syms(ips)) {
  1242. dev_info(ips->dev,
  1243. "i915 driver attached, reenabling gpu turbo\n");
  1244. ips->gpu_turbo_enabled = !(thm_readl(THM_HTS) & HTS_GTD_DIS);
  1245. }
  1246. }
  1247. return ips->gpu_turbo_enabled;
  1248. }
  1249. void
  1250. ips_link_to_i915_driver(void)
  1251. {
  1252. /* We can't cleanly get at the various ips_driver structs from
  1253. * this caller (the i915 driver), so just set a flag saying
  1254. * that it's time to try getting the symbols again.
  1255. */
  1256. late_i915_load = true;
  1257. }
  1258. EXPORT_SYMBOL_GPL(ips_link_to_i915_driver);
  1259. static const struct pci_device_id ips_id_table[] = {
  1260. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_THERMAL_SENSOR), },
  1261. { 0, }
  1262. };
  1263. MODULE_DEVICE_TABLE(pci, ips_id_table);
  1264. static int ips_blacklist_callback(const struct dmi_system_id *id)
  1265. {
  1266. pr_info("Blacklisted intel_ips for %s\n", id->ident);
  1267. return 1;
  1268. }
  1269. static const struct dmi_system_id ips_blacklist[] = {
  1270. {
  1271. .callback = ips_blacklist_callback,
  1272. .ident = "HP ProBook",
  1273. .matches = {
  1274. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  1275. DMI_MATCH(DMI_PRODUCT_NAME, "HP ProBook"),
  1276. },
  1277. },
  1278. { } /* terminating entry */
  1279. };
  1280. static int ips_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1281. {
  1282. u64 platform_info;
  1283. struct ips_driver *ips;
  1284. u32 hts;
  1285. int ret = 0;
  1286. u16 htshi, trc, trc_required_mask;
  1287. u8 tse;
  1288. if (dmi_check_system(ips_blacklist))
  1289. return -ENODEV;
  1290. ips = devm_kzalloc(&dev->dev, sizeof(*ips), GFP_KERNEL);
  1291. if (!ips)
  1292. return -ENOMEM;
  1293. spin_lock_init(&ips->turbo_status_lock);
  1294. ips->dev = &dev->dev;
  1295. ips->limits = ips_detect_cpu(ips);
  1296. if (!ips->limits) {
  1297. dev_info(&dev->dev, "IPS not supported on this CPU\n");
  1298. return -ENXIO;
  1299. }
  1300. ret = pcim_enable_device(dev);
  1301. if (ret) {
  1302. dev_err(&dev->dev, "can't enable PCI device, aborting\n");
  1303. return ret;
  1304. }
  1305. ret = pcim_iomap_regions(dev, 1 << 0, pci_name(dev));
  1306. if (ret) {
  1307. dev_err(&dev->dev, "failed to map thermal regs, aborting\n");
  1308. return ret;
  1309. }
  1310. ips->regmap = pcim_iomap_table(dev)[0];
  1311. pci_set_drvdata(dev, ips);
  1312. tse = thm_readb(THM_TSE);
  1313. if (tse != TSE_EN) {
  1314. dev_err(&dev->dev, "thermal device not enabled (0x%02x), aborting\n", tse);
  1315. return -ENXIO;
  1316. }
  1317. trc = thm_readw(THM_TRC);
  1318. trc_required_mask = TRC_CORE1_EN | TRC_CORE_PWR | TRC_MCH_EN;
  1319. if ((trc & trc_required_mask) != trc_required_mask) {
  1320. dev_err(&dev->dev, "thermal reporting for required devices not enabled, aborting\n");
  1321. return -ENXIO;
  1322. }
  1323. if (trc & TRC_CORE2_EN)
  1324. ips->second_cpu = true;
  1325. update_turbo_limits(ips);
  1326. dev_dbg(&dev->dev, "max cpu power clamp: %dW\n",
  1327. ips->mcp_power_limit / 10);
  1328. dev_dbg(&dev->dev, "max core power clamp: %dW\n",
  1329. ips->core_power_limit / 10);
  1330. /* BIOS may update limits at runtime */
  1331. if (thm_readl(THM_PSC) & PSP_PBRT)
  1332. ips->poll_turbo_status = true;
  1333. if (!ips_get_i915_syms(ips)) {
  1334. dev_info(&dev->dev, "failed to get i915 symbols, graphics turbo disabled until i915 loads\n");
  1335. ips->gpu_turbo_enabled = false;
  1336. } else {
  1337. dev_dbg(&dev->dev, "graphics turbo enabled\n");
  1338. ips->gpu_turbo_enabled = true;
  1339. }
  1340. /*
  1341. * Check PLATFORM_INFO MSR to make sure this chip is
  1342. * turbo capable.
  1343. */
  1344. rdmsrl(PLATFORM_INFO, platform_info);
  1345. if (!(platform_info & PLATFORM_TDP)) {
  1346. dev_err(&dev->dev, "platform indicates TDP override unavailable, aborting\n");
  1347. return -ENODEV;
  1348. }
  1349. /*
  1350. * IRQ handler for ME interaction
  1351. * Note: don't use MSI here as the PCH has bugs.
  1352. */
  1353. ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
  1354. if (ret < 0)
  1355. return ret;
  1356. ips->irq = pci_irq_vector(dev, 0);
  1357. ret = request_irq(ips->irq, ips_irq_handler, IRQF_SHARED, "ips", ips);
  1358. if (ret) {
  1359. dev_err(&dev->dev, "request irq failed, aborting\n");
  1360. return ret;
  1361. }
  1362. /* Enable aux, hot & critical interrupts */
  1363. thm_writeb(THM_TSPIEN, TSPIEN_AUX2_LOHI | TSPIEN_CRIT_LOHI |
  1364. TSPIEN_HOT_LOHI | TSPIEN_AUX_LOHI);
  1365. thm_writeb(THM_TEN, TEN_UPDATE_EN);
  1366. /* Collect adjustment values */
  1367. ips->cta_val = thm_readw(THM_CTA);
  1368. ips->pta_val = thm_readw(THM_PTA);
  1369. ips->mgta_val = thm_readw(THM_MGTA);
  1370. /* Save turbo limits & ratios */
  1371. rdmsrl(TURBO_POWER_CURRENT_LIMIT, ips->orig_turbo_limit);
  1372. ips_disable_cpu_turbo(ips);
  1373. ips->cpu_turbo_enabled = false;
  1374. /* Create thermal adjust thread */
  1375. ips->adjust = kthread_create(ips_adjust, ips, "ips-adjust");
  1376. if (IS_ERR(ips->adjust)) {
  1377. dev_err(&dev->dev,
  1378. "failed to create thermal adjust thread, aborting\n");
  1379. ret = -ENOMEM;
  1380. goto error_free_irq;
  1381. }
  1382. /*
  1383. * Set up the work queue and monitor thread. The monitor thread
  1384. * will wake up ips_adjust thread.
  1385. */
  1386. ips->monitor = kthread_run(ips_monitor, ips, "ips-monitor");
  1387. if (IS_ERR(ips->monitor)) {
  1388. dev_err(&dev->dev,
  1389. "failed to create thermal monitor thread, aborting\n");
  1390. ret = -ENOMEM;
  1391. goto error_thread_cleanup;
  1392. }
  1393. hts = (ips->core_power_limit << HTS_PCPL_SHIFT) |
  1394. (ips->mcp_temp_limit << HTS_PTL_SHIFT) | HTS_NVV;
  1395. htshi = HTS2_PRST_RUNNING << HTS2_PRST_SHIFT;
  1396. thm_writew(THM_HTSHI, htshi);
  1397. thm_writel(THM_HTS, hts);
  1398. ips_debugfs_init(ips);
  1399. dev_info(&dev->dev, "IPS driver initialized, MCP temp limit %d\n",
  1400. ips->mcp_temp_limit);
  1401. return ret;
  1402. error_thread_cleanup:
  1403. kthread_stop(ips->adjust);
  1404. error_free_irq:
  1405. free_irq(ips->irq, ips);
  1406. pci_free_irq_vectors(dev);
  1407. return ret;
  1408. }
  1409. static void ips_remove(struct pci_dev *dev)
  1410. {
  1411. struct ips_driver *ips = pci_get_drvdata(dev);
  1412. u64 turbo_override;
  1413. if (!ips)
  1414. return;
  1415. ips_debugfs_cleanup(ips);
  1416. /* Release i915 driver */
  1417. if (ips->read_mch_val)
  1418. symbol_put(i915_read_mch_val);
  1419. if (ips->gpu_raise)
  1420. symbol_put(i915_gpu_raise);
  1421. if (ips->gpu_lower)
  1422. symbol_put(i915_gpu_lower);
  1423. if (ips->gpu_busy)
  1424. symbol_put(i915_gpu_busy);
  1425. if (ips->gpu_turbo_disable)
  1426. symbol_put(i915_gpu_turbo_disable);
  1427. rdmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  1428. turbo_override &= ~(TURBO_TDC_OVR_EN | TURBO_TDP_OVR_EN);
  1429. wrmsrl(TURBO_POWER_CURRENT_LIMIT, turbo_override);
  1430. wrmsrl(TURBO_POWER_CURRENT_LIMIT, ips->orig_turbo_limit);
  1431. free_irq(ips->irq, ips);
  1432. pci_free_irq_vectors(dev);
  1433. if (ips->adjust)
  1434. kthread_stop(ips->adjust);
  1435. if (ips->monitor)
  1436. kthread_stop(ips->monitor);
  1437. dev_dbg(&dev->dev, "IPS driver removed\n");
  1438. }
  1439. static struct pci_driver ips_pci_driver = {
  1440. .name = "intel ips",
  1441. .id_table = ips_id_table,
  1442. .probe = ips_probe,
  1443. .remove = ips_remove,
  1444. };
  1445. module_pci_driver(ips_pci_driver);
  1446. MODULE_LICENSE("GPL");
  1447. MODULE_AUTHOR("Jesse Barnes <jbarnes@virtuousgeek.org>");
  1448. MODULE_DESCRIPTION("Intelligent Power Sharing Driver");