intel_ips.c 44 KB

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