intel_ips.c 42 KB

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