intel_ips.c 44 KB

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