2 * Copyright (c) 2009-2010 Intel Corporation
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.
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
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
17 * Jesse Barnes <jbarnes@virtuousgeek.org>
21 * Some Intel Ibex Peak based platforms support so-called "intelligent
22 * power sharing", which allows the CPU and GPU to cooperate to maximize
23 * performance within a given TDP (thermal design point). This driver
24 * performs the coordination between the CPU and GPU, monitors thermal and
25 * power statistics in the platform, and initializes power monitoring
26 * hardware. It also provides a few tunables to control behavior. Its
27 * primary purpose is to safely allow CPU and GPU turbo modes to be enabled
28 * by tracking power and thermal budget; secondarily it can boost turbo
29 * performance by allocating more power or thermal budget to the CPU or GPU
30 * based on available headroom and activity.
32 * The basic algorithm is driven by a 5s moving average of temperature. If
33 * thermal headroom is available, the CPU and/or GPU power clamps may be
34 * adjusted upwards. If we hit the thermal ceiling or a thermal trigger,
35 * we scale back the clamp. Aside from trigger events (when we're critically
36 * close or over our TDP) we don't adjust the clamps more than once every
39 * The thermal device (device 31, function 6) has a set of registers that
40 * are updated by the ME firmware. The ME should also take the clamp values
41 * written to those registers and write them to the CPU, but we currently
42 * bypass that functionality and write the CPU MSR directly.
48 * - handle CPU hotplug
49 * - provide turbo enable/disable api
52 * - CDI 403777, 403778 - Auburndale EDS vol 1 & 2
53 * - CDI 401376 - Ibex Peak EDS
54 * - ref 26037, 26641 - IPS BIOS spec
55 * - ref 26489 - Nehalem BIOS writer's guide
56 * - ref 26921 - Ibex Peak BIOS Specification
59 #include <linux/debugfs.h>
60 #include <linux/delay.h>
61 #include <linux/interrupt.h>
62 #include <linux/kernel.h>
63 #include <linux/kthread.h>
64 #include <linux/module.h>
65 #include <linux/pci.h>
66 #include <linux/sched.h>
67 #include <linux/sched/loadavg.h>
68 #include <linux/seq_file.h>
69 #include <linux/string.h>
70 #include <linux/tick.h>
71 #include <linux/timer.h>
72 #include <linux/dmi.h>
73 #include <drm/i915_drm.h>
75 #include <asm/processor.h>
76 #include "intel_ips.h"
78 #include <linux/io-64-nonatomic-lo-hi.h>
80 #define PCI_DEVICE_ID_INTEL_THERMAL_SENSOR 0x3b32
83 * Package level MSRs for monitor/control
85 #define PLATFORM_INFO 0xce
86 #define PLATFORM_TDP (1<<29)
87 #define PLATFORM_RATIO (1<<28)
89 #define IA32_MISC_ENABLE 0x1a0
90 #define IA32_MISC_TURBO_EN (1ULL<<38)
92 #define TURBO_POWER_CURRENT_LIMIT 0x1ac
93 #define TURBO_TDC_OVR_EN (1UL<<31)
94 #define TURBO_TDC_MASK (0x000000007fff0000UL)
95 #define TURBO_TDC_SHIFT (16)
96 #define TURBO_TDP_OVR_EN (1UL<<15)
97 #define TURBO_TDP_MASK (0x0000000000003fffUL)
100 * Core/thread MSRs for monitoring
102 #define IA32_PERF_CTL 0x199
103 #define IA32_PERF_TURBO_DIS (1ULL<<32)
106 * Thermal PCI device regs
108 #define THM_CFG_TBAR 0x10
109 #define THM_CFG_TBAR_HI 0x14
111 #define THM_TSIU 0x00
115 #define THM_TSTR 0x03
116 #define THM_TSTTP 0x04
117 #define THM_TSCO 0x08
118 #define THM_TSES 0x0c
119 #define THM_TSGPEN 0x0d
120 #define TSGPEN_HOT_LOHI (1<<1)
121 #define TSGPEN_CRIT_LOHI (1<<2)
122 #define THM_TSPC 0x0e
123 #define THM_PPEC 0x10
126 #define PTA_SLOPE_MASK (0xff00)
127 #define PTA_SLOPE_SHIFT 8
128 #define PTA_OFFSET_MASK (0x00ff)
129 #define THM_MGTA 0x16
130 #define MGTA_SLOPE_MASK (0xff00)
131 #define MGTA_SLOPE_SHIFT 8
132 #define MGTA_OFFSET_MASK (0x00ff)
134 #define TRC_CORE2_EN (1<<15)
135 #define TRC_THM_EN (1<<12)
136 #define TRC_C6_WAR (1<<8)
137 #define TRC_CORE1_EN (1<<7)
138 #define TRC_CORE_PWR (1<<6)
139 #define TRC_PCH_EN (1<<5)
140 #define TRC_MCH_EN (1<<4)
141 #define TRC_DIMM4 (1<<3)
142 #define TRC_DIMM3 (1<<2)
143 #define TRC_DIMM2 (1<<1)
144 #define TRC_DIMM1 (1<<0)
147 #define TEN_UPDATE_EN 1
149 #define PSC_NTG (1<<0) /* No GFX turbo support */
150 #define PSC_NTPC (1<<1) /* No CPU turbo support */
151 #define PSC_PP_DEF (0<<2) /* Perf policy up to driver */
152 #define PSP_PP_PC (1<<2) /* BIOS prefers CPU perf */
153 #define PSP_PP_BAL (2<<2) /* BIOS wants balanced perf */
154 #define PSP_PP_GFX (3<<2) /* BIOS prefers GFX perf */
155 #define PSP_PBRT (1<<4) /* BIOS run time support */
156 #define THM_CTV1 0x30
157 #define CTV_TEMP_ERROR (1<<15)
158 #define CTV_TEMP_MASK 0x3f
160 #define THM_CTV2 0x32
161 #define THM_CEC 0x34 /* undocumented power accumulator in joules */
163 #define THM_HTS 0x50 /* 32 bits */
164 #define HTS_PCPL_MASK (0x7fe00000)
165 #define HTS_PCPL_SHIFT 21
166 #define HTS_GPL_MASK (0x001ff000)
167 #define HTS_GPL_SHIFT 12
168 #define HTS_PP_MASK (0x00000c00)
169 #define HTS_PP_SHIFT 10
171 #define HTS_PP_PROC 1
174 #define HTS_PCTD_DIS (1<<9)
175 #define HTS_GTD_DIS (1<<8)
176 #define HTS_PTL_MASK (0x000000fe)
177 #define HTS_PTL_SHIFT 1
178 #define HTS_NVV (1<<0)
179 #define THM_HTSHI 0x54 /* 16 bits */
180 #define HTS2_PPL_MASK (0x03ff)
181 #define HTS2_PRST_MASK (0x3c00)
182 #define HTS2_PRST_SHIFT 10
183 #define HTS2_PRST_UNLOADED 0
184 #define HTS2_PRST_RUNNING 1
185 #define HTS2_PRST_TDISOP 2 /* turbo disabled due to power */
186 #define HTS2_PRST_TDISHT 3 /* turbo disabled due to high temp */
187 #define HTS2_PRST_TDISUSR 4 /* user disabled turbo */
188 #define HTS2_PRST_TDISPLAT 5 /* platform disabled turbo */
189 #define HTS2_PRST_TDISPM 6 /* power management disabled turbo */
190 #define HTS2_PRST_TDISERR 7 /* some kind of error disabled turbo */
192 #define THM_MGTV 0x58
193 #define TV_MASK 0x000000000000ff00
196 #define PTV_MASK 0x00ff
197 #define THM_MMGPC 0x64
198 #define THM_MPPC 0x66
199 #define THM_MPCPC 0x68
200 #define THM_TSPIEN 0x82
201 #define TSPIEN_AUX_LOHI (1<<0)
202 #define TSPIEN_HOT_LOHI (1<<1)
203 #define TSPIEN_CRIT_LOHI (1<<2)
204 #define TSPIEN_AUX2_LOHI (1<<3)
205 #define THM_TSLOCK 0x83
209 #define STS_PCPL_MASK (0x7fe00000)
210 #define STS_PCPL_SHIFT 21
211 #define STS_GPL_MASK (0x001ff000)
212 #define STS_GPL_SHIFT 12
213 #define STS_PP_MASK (0x00000c00)
214 #define STS_PP_SHIFT 10
216 #define STS_PP_PROC 1
219 #define STS_PCTD_DIS (1<<9)
220 #define STS_GTD_DIS (1<<8)
221 #define STS_PTL_MASK (0x000000fe)
222 #define STS_PTL_SHIFT 1
223 #define STS_NVV (1<<0)
225 #define SEC_ACK (1<<0)
228 #define STS_PPL_MASK (0x0003ff00)
229 #define STS_PPL_SHIFT 16
233 #define ITV_ME_SEQNO_MASK 0x00ff0000 /* ME should update every ~200ms */
234 #define ITV_ME_SEQNO_SHIFT (16)
235 #define ITV_MCH_TEMP_MASK 0x0000ff00
236 #define ITV_MCH_TEMP_SHIFT (8)
237 #define ITV_PCH_TEMP_MASK 0x000000ff
239 #define thm_readb(off) readb(ips->regmap + (off))
240 #define thm_readw(off) readw(ips->regmap + (off))
241 #define thm_readl(off) readl(ips->regmap + (off))
242 #define thm_readq(off) readq(ips->regmap + (off))
244 #define thm_writeb(off, val) writeb((val), ips->regmap + (off))
245 #define thm_writew(off, val) writew((val), ips->regmap + (off))
246 #define thm_writel(off, val) writel((val), ips->regmap + (off))
248 static const int IPS_ADJUST_PERIOD
= 5000; /* ms */
249 static bool late_i915_load
= false;
251 /* For initial average collection */
252 static const int IPS_SAMPLE_PERIOD
= 200; /* ms */
253 static const int IPS_SAMPLE_WINDOW
= 5000; /* 5s moving window of samples */
254 #define IPS_SAMPLE_COUNT (IPS_SAMPLE_WINDOW / IPS_SAMPLE_PERIOD)
257 struct ips_mcp_limits
{
258 int mcp_power_limit
; /* mW units */
259 int core_power_limit
;
261 int core_temp_limit
; /* degrees C */
265 /* Max temps are -10 degrees C to avoid PROCHOT# */
267 static struct ips_mcp_limits ips_sv_limits
= {
268 .mcp_power_limit
= 35000,
269 .core_power_limit
= 29000,
270 .mch_power_limit
= 20000,
271 .core_temp_limit
= 95,
275 static struct ips_mcp_limits ips_lv_limits
= {
276 .mcp_power_limit
= 25000,
277 .core_power_limit
= 21000,
278 .mch_power_limit
= 13000,
279 .core_temp_limit
= 95,
283 static struct ips_mcp_limits ips_ulv_limits
= {
284 .mcp_power_limit
= 18000,
285 .core_power_limit
= 14000,
286 .mch_power_limit
= 11000,
287 .core_temp_limit
= 95,
293 void __iomem
*regmap
;
296 struct task_struct
*monitor
;
297 struct task_struct
*adjust
;
298 struct dentry
*debug_root
;
299 struct timer_list timer
;
301 /* Average CPU core temps (all averages in .01 degrees C for precision) */
306 /* Average for the CPU (both cores?) */
308 /* Average power consumption (in mW) */
317 /* Maximums & prefs, protected by turbo status lock */
318 spinlock_t turbo_status_lock
;
321 u16 core_power_limit
;
323 bool cpu_turbo_enabled
;
325 bool gpu_turbo_enabled
;
328 bool poll_turbo_status
;
330 bool turbo_toggle_allowed
;
331 struct ips_mcp_limits
*limits
;
333 /* Optional MCH interfaces for if i915 is in use */
334 unsigned long (*read_mch_val
)(void);
335 bool (*gpu_raise
)(void);
336 bool (*gpu_lower
)(void);
337 bool (*gpu_busy
)(void);
338 bool (*gpu_turbo_disable
)(void);
340 /* For restoration at unload */
341 u64 orig_turbo_limit
;
342 u64 orig_turbo_ratios
;
346 ips_gpu_turbo_enabled(struct ips_driver
*ips
);
349 * ips_cpu_busy - is CPU busy?
350 * @ips: IPS driver struct
352 * Check CPU for load to see whether we should increase its thermal budget.
355 * True if the CPU could use more power, false otherwise.
357 static bool ips_cpu_busy(struct ips_driver
*ips
)
359 if ((avenrun
[0] >> FSHIFT
) > 1)
366 * ips_cpu_raise - raise CPU power clamp
367 * @ips: IPS driver struct
369 * Raise the CPU power clamp by %IPS_CPU_STEP, in accordance with TDP for
372 * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR upwards (as
373 * long as we haven't hit the TDP limit for the SKU).
375 static void ips_cpu_raise(struct ips_driver
*ips
)
378 u16 cur_tdp_limit
, new_tdp_limit
;
380 if (!ips
->cpu_turbo_enabled
)
383 rdmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
385 cur_tdp_limit
= turbo_override
& TURBO_TDP_MASK
;
386 new_tdp_limit
= cur_tdp_limit
+ 8; /* 1W increase */
388 /* Clamp to SKU TDP limit */
389 if (((new_tdp_limit
* 10) / 8) > ips
->core_power_limit
)
390 new_tdp_limit
= cur_tdp_limit
;
392 thm_writew(THM_MPCPC
, (new_tdp_limit
* 10) / 8);
394 turbo_override
|= TURBO_TDC_OVR_EN
| TURBO_TDP_OVR_EN
;
395 wrmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
397 turbo_override
&= ~TURBO_TDP_MASK
;
398 turbo_override
|= new_tdp_limit
;
400 wrmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
404 * ips_cpu_lower - lower CPU power clamp
405 * @ips: IPS driver struct
407 * Lower CPU power clamp b %IPS_CPU_STEP if possible.
409 * We do this by adjusting the TURBO_POWER_CURRENT_LIMIT MSR down, going
410 * as low as the platform limits will allow (though we could go lower there
411 * wouldn't be much point).
413 static void ips_cpu_lower(struct ips_driver
*ips
)
416 u16 cur_limit
, new_limit
;
418 rdmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
420 cur_limit
= turbo_override
& TURBO_TDP_MASK
;
421 new_limit
= cur_limit
- 8; /* 1W decrease */
423 /* Clamp to SKU TDP limit */
424 if (new_limit
< (ips
->orig_turbo_limit
& TURBO_TDP_MASK
))
425 new_limit
= ips
->orig_turbo_limit
& TURBO_TDP_MASK
;
427 thm_writew(THM_MPCPC
, (new_limit
* 10) / 8);
429 turbo_override
|= TURBO_TDC_OVR_EN
| TURBO_TDP_OVR_EN
;
430 wrmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
432 turbo_override
&= ~TURBO_TDP_MASK
;
433 turbo_override
|= new_limit
;
435 wrmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
439 * do_enable_cpu_turbo - internal turbo enable function
442 * Internal function for actually updating MSRs. When we enable/disable
443 * turbo, we need to do it on each CPU; this function is the one called
444 * by on_each_cpu() when needed.
446 static void do_enable_cpu_turbo(void *data
)
450 rdmsrl(IA32_PERF_CTL
, perf_ctl
);
451 if (perf_ctl
& IA32_PERF_TURBO_DIS
) {
452 perf_ctl
&= ~IA32_PERF_TURBO_DIS
;
453 wrmsrl(IA32_PERF_CTL
, perf_ctl
);
458 * ips_enable_cpu_turbo - enable turbo mode on all CPUs
459 * @ips: IPS driver struct
461 * Enable turbo mode by clearing the disable bit in IA32_PERF_CTL on
462 * all logical threads.
464 static void ips_enable_cpu_turbo(struct ips_driver
*ips
)
466 /* Already on, no need to mess with MSRs */
467 if (ips
->__cpu_turbo_on
)
470 if (ips
->turbo_toggle_allowed
)
471 on_each_cpu(do_enable_cpu_turbo
, ips
, 1);
473 ips
->__cpu_turbo_on
= true;
477 * do_disable_cpu_turbo - internal turbo disable function
480 * Internal function for actually updating MSRs. When we enable/disable
481 * turbo, we need to do it on each CPU; this function is the one called
482 * by on_each_cpu() when needed.
484 static void do_disable_cpu_turbo(void *data
)
488 rdmsrl(IA32_PERF_CTL
, perf_ctl
);
489 if (!(perf_ctl
& IA32_PERF_TURBO_DIS
)) {
490 perf_ctl
|= IA32_PERF_TURBO_DIS
;
491 wrmsrl(IA32_PERF_CTL
, perf_ctl
);
496 * ips_disable_cpu_turbo - disable turbo mode on all CPUs
497 * @ips: IPS driver struct
499 * Disable turbo mode by setting the disable bit in IA32_PERF_CTL on
500 * all logical threads.
502 static void ips_disable_cpu_turbo(struct ips_driver
*ips
)
504 /* Already off, leave it */
505 if (!ips
->__cpu_turbo_on
)
508 if (ips
->turbo_toggle_allowed
)
509 on_each_cpu(do_disable_cpu_turbo
, ips
, 1);
511 ips
->__cpu_turbo_on
= false;
515 * ips_gpu_busy - is GPU busy?
516 * @ips: IPS driver struct
518 * Check GPU for load to see whether we should increase its thermal budget.
519 * We need to call into the i915 driver in this case.
522 * True if the GPU could use more power, false otherwise.
524 static bool ips_gpu_busy(struct ips_driver
*ips
)
526 if (!ips_gpu_turbo_enabled(ips
))
529 return ips
->gpu_busy();
533 * ips_gpu_raise - raise GPU power clamp
534 * @ips: IPS driver struct
536 * Raise the GPU frequency/power if possible. We need to call into the
537 * i915 driver in this case.
539 static void ips_gpu_raise(struct ips_driver
*ips
)
541 if (!ips_gpu_turbo_enabled(ips
))
544 if (!ips
->gpu_raise())
545 ips
->gpu_turbo_enabled
= false;
551 * ips_gpu_lower - lower GPU power clamp
552 * @ips: IPS driver struct
554 * Lower GPU frequency/power if possible. Need to call i915.
556 static void ips_gpu_lower(struct ips_driver
*ips
)
558 if (!ips_gpu_turbo_enabled(ips
))
561 if (!ips
->gpu_lower())
562 ips
->gpu_turbo_enabled
= false;
568 * ips_enable_gpu_turbo - notify the gfx driver turbo is available
569 * @ips: IPS driver struct
571 * Call into the graphics driver indicating that it can safely use
574 static void ips_enable_gpu_turbo(struct ips_driver
*ips
)
576 if (ips
->__gpu_turbo_on
)
578 ips
->__gpu_turbo_on
= true;
582 * ips_disable_gpu_turbo - notify the gfx driver to disable turbo mode
583 * @ips: IPS driver struct
585 * Request that the graphics driver disable turbo mode.
587 static void ips_disable_gpu_turbo(struct ips_driver
*ips
)
589 /* Avoid calling i915 if turbo is already disabled */
590 if (!ips
->__gpu_turbo_on
)
593 if (!ips
->gpu_turbo_disable())
594 dev_err(ips
->dev
, "failed to disable graphics turbo\n");
596 ips
->__gpu_turbo_on
= false;
600 * mcp_exceeded - check whether we're outside our thermal & power limits
601 * @ips: IPS driver struct
603 * Check whether the MCP is over its thermal or power budget.
605 static bool mcp_exceeded(struct ips_driver
*ips
)
612 spin_lock_irqsave(&ips
->turbo_status_lock
, flags
);
614 temp_limit
= ips
->mcp_temp_limit
* 100;
615 if (ips
->mcp_avg_temp
> temp_limit
)
618 avg_power
= ips
->cpu_avg_power
+ ips
->mch_avg_power
;
619 if (avg_power
> ips
->mcp_power_limit
)
622 spin_unlock_irqrestore(&ips
->turbo_status_lock
, flags
);
628 * cpu_exceeded - check whether a CPU core is outside its limits
629 * @ips: IPS driver struct
630 * @cpu: CPU number to check
632 * Check a given CPU's average temp or power is over its limit.
634 static bool cpu_exceeded(struct ips_driver
*ips
, int cpu
)
640 spin_lock_irqsave(&ips
->turbo_status_lock
, flags
);
641 avg
= cpu
? ips
->ctv2_avg_temp
: ips
->ctv1_avg_temp
;
642 if (avg
> (ips
->limits
->core_temp_limit
* 100))
644 if (ips
->cpu_avg_power
> ips
->core_power_limit
* 100)
646 spin_unlock_irqrestore(&ips
->turbo_status_lock
, flags
);
649 dev_info(ips
->dev
, "CPU power or thermal limit exceeded\n");
655 * mch_exceeded - check whether the GPU is over budget
656 * @ips: IPS driver struct
658 * Check the MCH temp & power against their maximums.
660 static bool mch_exceeded(struct ips_driver
*ips
)
665 spin_lock_irqsave(&ips
->turbo_status_lock
, flags
);
666 if (ips
->mch_avg_temp
> (ips
->limits
->mch_temp_limit
* 100))
668 if (ips
->mch_avg_power
> ips
->mch_power_limit
)
670 spin_unlock_irqrestore(&ips
->turbo_status_lock
, flags
);
676 * verify_limits - verify BIOS provided limits
677 * @ips: IPS structure
679 * BIOS can optionally provide non-default limits for power and temp. Check
680 * them here and use the defaults if the BIOS values are not provided or
681 * are otherwise unusable.
683 static void verify_limits(struct ips_driver
*ips
)
685 if (ips
->mcp_power_limit
< ips
->limits
->mcp_power_limit
||
686 ips
->mcp_power_limit
> 35000)
687 ips
->mcp_power_limit
= ips
->limits
->mcp_power_limit
;
689 if (ips
->mcp_temp_limit
< ips
->limits
->core_temp_limit
||
690 ips
->mcp_temp_limit
< ips
->limits
->mch_temp_limit
||
691 ips
->mcp_temp_limit
> 150)
692 ips
->mcp_temp_limit
= min(ips
->limits
->core_temp_limit
,
693 ips
->limits
->mch_temp_limit
);
697 * update_turbo_limits - get various limits & settings from regs
698 * @ips: IPS driver struct
700 * Update the IPS power & temp limits, along with turbo enable flags,
701 * based on latest register contents.
703 * Used at init time and for runtime BIOS support, which requires polling
704 * the regs for updates (as a result of AC->DC transition for example).
707 * Caller must hold turbo_status_lock (outside of init)
709 static void update_turbo_limits(struct ips_driver
*ips
)
711 u32 hts
= thm_readl(THM_HTS
);
713 ips
->cpu_turbo_enabled
= !(hts
& HTS_PCTD_DIS
);
715 * Disable turbo for now, until we can figure out why the power figures
718 ips
->cpu_turbo_enabled
= false;
721 ips
->gpu_turbo_enabled
= !(hts
& HTS_GTD_DIS
);
723 ips
->core_power_limit
= thm_readw(THM_MPCPC
);
724 ips
->mch_power_limit
= thm_readw(THM_MMGPC
);
725 ips
->mcp_temp_limit
= thm_readw(THM_PTL
);
726 ips
->mcp_power_limit
= thm_readw(THM_MPPC
);
729 /* Ignore BIOS CPU vs GPU pref */
733 * ips_adjust - adjust power clamp based on thermal state
734 * @data: ips driver structure
736 * Wake up every 5s or so and check whether we should adjust the power clamp.
737 * Check CPU and GPU load to determine which needs adjustment. There are
738 * several things to consider here:
739 * - do we need to adjust up or down?
744 * - is CPU or GPU preferred? (CPU is default)
746 * So, given the above, we do the following:
747 * - up (TDP available)
748 * - CPU not busy, GPU not busy - nothing
749 * - CPU busy, GPU not busy - adjust CPU up
750 * - CPU not busy, GPU busy - adjust GPU up
751 * - CPU busy, GPU busy - adjust preferred unit up, taking headroom from
752 * non-preferred unit if necessary
753 * - down (at TDP limit)
754 * - adjust both CPU and GPU down if possible
756 cpu+ gpu+ cpu+gpu- cpu-gpu+ cpu-gpu-
757 cpu < gpu < cpu+gpu+ cpu+ gpu+ nothing
758 cpu < gpu >= cpu+gpu-(mcp<) cpu+gpu-(mcp<) gpu- gpu-
759 cpu >= gpu < cpu-gpu+(mcp<) cpu- cpu-gpu+(mcp<) cpu-
760 cpu >= gpu >= cpu-gpu- cpu-gpu- cpu-gpu- cpu-gpu-
763 static int ips_adjust(void *data
)
765 struct ips_driver
*ips
= data
;
768 dev_dbg(ips
->dev
, "starting ips-adjust thread\n");
771 * Adjust CPU and GPU clamps every 5s if needed. Doing it more
772 * often isn't recommended due to ME interaction.
775 bool cpu_busy
= ips_cpu_busy(ips
);
776 bool gpu_busy
= ips_gpu_busy(ips
);
778 spin_lock_irqsave(&ips
->turbo_status_lock
, flags
);
779 if (ips
->poll_turbo_status
)
780 update_turbo_limits(ips
);
781 spin_unlock_irqrestore(&ips
->turbo_status_lock
, flags
);
783 /* Update turbo status if necessary */
784 if (ips
->cpu_turbo_enabled
)
785 ips_enable_cpu_turbo(ips
);
787 ips_disable_cpu_turbo(ips
);
789 if (ips
->gpu_turbo_enabled
)
790 ips_enable_gpu_turbo(ips
);
792 ips_disable_gpu_turbo(ips
);
794 /* We're outside our comfort zone, crank them down */
795 if (mcp_exceeded(ips
)) {
801 if (!cpu_exceeded(ips
, 0) && cpu_busy
)
806 if (!mch_exceeded(ips
) && gpu_busy
)
812 schedule_timeout_interruptible(msecs_to_jiffies(IPS_ADJUST_PERIOD
));
813 } while (!kthread_should_stop());
815 dev_dbg(ips
->dev
, "ips-adjust thread stopped\n");
821 * Helpers for reading out temp/power values and calculating their
822 * averages for the decision making and monitoring functions.
825 static u16
calc_avg_temp(struct ips_driver
*ips
, u16
*array
)
831 for (i
= 0; i
< IPS_SAMPLE_COUNT
; i
++)
832 total
+= (u64
)(array
[i
] * 100);
834 do_div(total
, IPS_SAMPLE_COUNT
);
841 static u16
read_mgtv(struct ips_driver
*ips
)
847 val
= thm_readq(THM_MGTV
);
848 val
= (val
& TV_MASK
) >> TV_SHIFT
;
850 slope
= offset
= thm_readw(THM_MGTA
);
851 slope
= (slope
& MGTA_SLOPE_MASK
) >> MGTA_SLOPE_SHIFT
;
852 offset
= offset
& MGTA_OFFSET_MASK
;
854 ret
= ((val
* slope
+ 0x40) >> 7) + offset
;
856 return 0; /* MCH temp reporting buggy */
859 static u16
read_ptv(struct ips_driver
*ips
)
863 val
= thm_readw(THM_PTV
) & PTV_MASK
;
868 static u16
read_ctv(struct ips_driver
*ips
, int cpu
)
870 int reg
= cpu
? THM_CTV2
: THM_CTV1
;
873 val
= thm_readw(reg
);
874 if (!(val
& CTV_TEMP_ERROR
))
875 val
= (val
) >> 6; /* discard fractional component */
882 static u32
get_cpu_power(struct ips_driver
*ips
, u32
*last
, int period
)
888 * CEC is in joules/65535. Take difference over time to
891 val
= thm_readl(THM_CEC
);
893 /* period is in ms and we want mW */
894 ret
= (((val
- *last
) * 1000) / period
);
895 ret
= (ret
* 1000) / 65535;
901 static const u16 temp_decay_factor
= 2;
902 static u16
update_average_temp(u16 avg
, u16 val
)
906 /* Multiply by 100 for extra precision */
907 ret
= (val
* 100 / temp_decay_factor
) +
908 (((temp_decay_factor
- 1) * avg
) / temp_decay_factor
);
912 static const u16 power_decay_factor
= 2;
913 static u16
update_average_power(u32 avg
, u32 val
)
917 ret
= (val
/ power_decay_factor
) +
918 (((power_decay_factor
- 1) * avg
) / power_decay_factor
);
923 static u32
calc_avg_power(struct ips_driver
*ips
, u32
*array
)
929 for (i
= 0; i
< IPS_SAMPLE_COUNT
; i
++)
932 do_div(total
, IPS_SAMPLE_COUNT
);
938 static void monitor_timeout(struct timer_list
*t
)
940 struct ips_driver
*ips
= from_timer(ips
, t
, timer
);
941 wake_up_process(ips
->monitor
);
945 * ips_monitor - temp/power monitoring thread
946 * @data: ips driver structure
948 * This is the main function for the IPS driver. It monitors power and
949 * tempurature in the MCP and adjusts CPU and GPU power clams accordingly.
951 * We keep a 5s moving average of power consumption and tempurature. Using
952 * that data, along with CPU vs GPU preference, we adjust the power clamps
955 static int ips_monitor(void *data
)
957 struct ips_driver
*ips
= data
;
958 unsigned long seqno_timestamp
, expire
, last_msecs
, last_sample_period
;
960 u32
*cpu_samples
, *mchp_samples
, old_cpu_power
;
961 u16
*mcp_samples
, *ctv1_samples
, *ctv2_samples
, *mch_samples
;
962 u8 cur_seqno
, last_seqno
;
964 mcp_samples
= kcalloc(IPS_SAMPLE_COUNT
, sizeof(u16
), GFP_KERNEL
);
965 ctv1_samples
= kcalloc(IPS_SAMPLE_COUNT
, sizeof(u16
), GFP_KERNEL
);
966 ctv2_samples
= kcalloc(IPS_SAMPLE_COUNT
, sizeof(u16
), GFP_KERNEL
);
967 mch_samples
= kcalloc(IPS_SAMPLE_COUNT
, sizeof(u16
), GFP_KERNEL
);
968 cpu_samples
= kcalloc(IPS_SAMPLE_COUNT
, sizeof(u32
), GFP_KERNEL
);
969 mchp_samples
= kcalloc(IPS_SAMPLE_COUNT
, sizeof(u32
), GFP_KERNEL
);
970 if (!mcp_samples
|| !ctv1_samples
|| !ctv2_samples
|| !mch_samples
||
971 !cpu_samples
|| !mchp_samples
) {
973 "failed to allocate sample array, ips disabled\n");
983 last_seqno
= (thm_readl(THM_ITV
) & ITV_ME_SEQNO_MASK
) >>
985 seqno_timestamp
= get_jiffies_64();
987 old_cpu_power
= thm_readl(THM_CEC
);
988 schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD
));
990 /* Collect an initial average */
991 for (i
= 0; i
< IPS_SAMPLE_COUNT
; i
++) {
995 mcp_samples
[i
] = read_ptv(ips
);
997 val
= read_ctv(ips
, 0);
998 ctv1_samples
[i
] = val
;
1000 val
= read_ctv(ips
, 1);
1001 ctv2_samples
[i
] = val
;
1003 val
= read_mgtv(ips
);
1004 mch_samples
[i
] = val
;
1006 cpu_power
= get_cpu_power(ips
, &old_cpu_power
,
1008 cpu_samples
[i
] = cpu_power
;
1010 if (ips
->read_mch_val
) {
1011 mchp
= ips
->read_mch_val();
1012 mchp_samples
[i
] = mchp
;
1015 schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD
));
1016 if (kthread_should_stop())
1020 ips
->mcp_avg_temp
= calc_avg_temp(ips
, mcp_samples
);
1021 ips
->ctv1_avg_temp
= calc_avg_temp(ips
, ctv1_samples
);
1022 ips
->ctv2_avg_temp
= calc_avg_temp(ips
, ctv2_samples
);
1023 ips
->mch_avg_temp
= calc_avg_temp(ips
, mch_samples
);
1024 ips
->cpu_avg_power
= calc_avg_power(ips
, cpu_samples
);
1025 ips
->mch_avg_power
= calc_avg_power(ips
, mchp_samples
);
1027 kfree(ctv1_samples
);
1028 kfree(ctv2_samples
);
1031 kfree(mchp_samples
);
1033 /* Start the adjustment thread now that we have data */
1034 wake_up_process(ips
->adjust
);
1037 * Ok, now we have an initial avg. From here on out, we track the
1038 * running avg using a decaying average calculation. This allows
1039 * us to reduce the sample frequency if the CPU and GPU are idle.
1041 old_cpu_power
= thm_readl(THM_CEC
);
1042 schedule_timeout_interruptible(msecs_to_jiffies(IPS_SAMPLE_PERIOD
));
1043 last_sample_period
= IPS_SAMPLE_PERIOD
;
1045 timer_setup(&ips
->timer
, monitor_timeout
, TIMER_DEFERRABLE
);
1047 u32 cpu_val
, mch_val
;
1051 val
= read_ptv(ips
);
1052 ips
->mcp_avg_temp
= update_average_temp(ips
->mcp_avg_temp
, val
);
1055 val
= read_ctv(ips
, 0);
1056 ips
->ctv1_avg_temp
=
1057 update_average_temp(ips
->ctv1_avg_temp
, val
);
1059 cpu_val
= get_cpu_power(ips
, &old_cpu_power
,
1060 last_sample_period
);
1061 ips
->cpu_avg_power
=
1062 update_average_power(ips
->cpu_avg_power
, cpu_val
);
1064 if (ips
->second_cpu
) {
1066 val
= read_ctv(ips
, 1);
1067 ips
->ctv2_avg_temp
=
1068 update_average_temp(ips
->ctv2_avg_temp
, val
);
1072 val
= read_mgtv(ips
);
1073 ips
->mch_avg_temp
= update_average_temp(ips
->mch_avg_temp
, val
);
1075 if (ips
->read_mch_val
) {
1076 mch_val
= ips
->read_mch_val();
1077 ips
->mch_avg_power
=
1078 update_average_power(ips
->mch_avg_power
,
1083 * Make sure ME is updating thermal regs.
1085 * If it's been more than a second since the last update,
1086 * the ME is probably hung.
1088 cur_seqno
= (thm_readl(THM_ITV
) & ITV_ME_SEQNO_MASK
) >>
1090 if (cur_seqno
== last_seqno
&&
1091 time_after(jiffies
, seqno_timestamp
+ HZ
)) {
1093 "ME failed to update for more than 1s, likely hung\n");
1095 seqno_timestamp
= get_jiffies_64();
1096 last_seqno
= cur_seqno
;
1099 last_msecs
= jiffies_to_msecs(jiffies
);
1100 expire
= jiffies
+ msecs_to_jiffies(IPS_SAMPLE_PERIOD
);
1102 __set_current_state(TASK_INTERRUPTIBLE
);
1103 mod_timer(&ips
->timer
, expire
);
1106 /* Calculate actual sample period for power averaging */
1107 last_sample_period
= jiffies_to_msecs(jiffies
) - last_msecs
;
1108 if (!last_sample_period
)
1109 last_sample_period
= 1;
1110 } while (!kthread_should_stop());
1112 del_timer_sync(&ips
->timer
);
1114 dev_dbg(ips
->dev
, "ips-monitor thread stopped\n");
1120 #define THM_DUMPW(reg) \
1122 u16 val = thm_readw(reg); \
1123 dev_dbg(ips->dev, #reg ": 0x%04x\n", val); \
1125 #define THM_DUMPL(reg) \
1127 u32 val = thm_readl(reg); \
1128 dev_dbg(ips->dev, #reg ": 0x%08x\n", val); \
1130 #define THM_DUMPQ(reg) \
1132 u64 val = thm_readq(reg); \
1133 dev_dbg(ips->dev, #reg ": 0x%016x\n", val); \
1136 static void dump_thermal_info(struct ips_driver
*ips
)
1140 ptl
= thm_readw(THM_PTL
);
1141 dev_dbg(ips
->dev
, "Processor temp limit: %d\n", ptl
);
1145 THM_DUMPW(THM_CTV1
);
1148 THM_DUMPQ(THM_MGTV
);
1153 * ips_irq_handler - handle temperature triggers and other IPS events
1157 * Handle temperature limit trigger events, generally by lowering the clamps.
1158 * If we're at a critical limit, we clamp back to the lowest possible value
1159 * to prevent emergency shutdown.
1161 static irqreturn_t
ips_irq_handler(int irq
, void *arg
)
1163 struct ips_driver
*ips
= arg
;
1164 u8 tses
= thm_readb(THM_TSES
);
1165 u8 tes
= thm_readb(THM_TES
);
1170 dev_info(ips
->dev
, "TSES: 0x%02x\n", tses
);
1171 dev_info(ips
->dev
, "TES: 0x%02x\n", tes
);
1173 /* STS update from EC? */
1177 sts
= thm_readl(THM_STS
);
1178 tc1
= thm_readl(THM_TC1
);
1180 if (sts
& STS_NVV
) {
1181 spin_lock(&ips
->turbo_status_lock
);
1182 ips
->core_power_limit
= (sts
& STS_PCPL_MASK
) >>
1184 ips
->mch_power_limit
= (sts
& STS_GPL_MASK
) >>
1186 /* ignore EC CPU vs GPU pref */
1187 ips
->cpu_turbo_enabled
= !(sts
& STS_PCTD_DIS
);
1189 * Disable turbo for now, until we can figure
1190 * out why the power figures are wrong
1192 ips
->cpu_turbo_enabled
= false;
1194 ips
->gpu_turbo_enabled
= !(sts
& STS_GTD_DIS
);
1195 ips
->mcp_temp_limit
= (sts
& STS_PTL_MASK
) >>
1197 ips
->mcp_power_limit
= (tc1
& STS_PPL_MASK
) >>
1200 spin_unlock(&ips
->turbo_status_lock
);
1202 thm_writeb(THM_SEC
, SEC_ACK
);
1204 thm_writeb(THM_TES
, tes
);
1209 dev_warn(ips
->dev
, "thermal trip occurred, tses: 0x%04x\n",
1211 thm_writeb(THM_TSES
, tses
);
1217 #ifndef CONFIG_DEBUG_FS
1218 static void ips_debugfs_init(struct ips_driver
*ips
) { return; }
1219 static void ips_debugfs_cleanup(struct ips_driver
*ips
) { return; }
1222 /* Expose current state and limits in debugfs if possible */
1224 struct ips_debugfs_node
{
1225 struct ips_driver
*ips
;
1227 int (*show
)(struct seq_file
*m
, void *data
);
1230 static int show_cpu_temp(struct seq_file
*m
, void *data
)
1232 struct ips_driver
*ips
= m
->private;
1234 seq_printf(m
, "%d.%02d\n", ips
->ctv1_avg_temp
/ 100,
1235 ips
->ctv1_avg_temp
% 100);
1240 static int show_cpu_power(struct seq_file
*m
, void *data
)
1242 struct ips_driver
*ips
= m
->private;
1244 seq_printf(m
, "%dmW\n", ips
->cpu_avg_power
);
1249 static int show_cpu_clamp(struct seq_file
*m
, void *data
)
1254 rdmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
1256 tdp
= (int)(turbo_override
& TURBO_TDP_MASK
);
1257 tdc
= (int)((turbo_override
& TURBO_TDC_MASK
) >> TURBO_TDC_SHIFT
);
1259 /* Convert to .1W/A units */
1264 seq_printf(m
, "%d.%dW %d.%dA\n", tdp
/ 10, tdp
% 10,
1265 tdc
/ 10, tdc
% 10);
1270 static int show_mch_temp(struct seq_file
*m
, void *data
)
1272 struct ips_driver
*ips
= m
->private;
1274 seq_printf(m
, "%d.%02d\n", ips
->mch_avg_temp
/ 100,
1275 ips
->mch_avg_temp
% 100);
1280 static int show_mch_power(struct seq_file
*m
, void *data
)
1282 struct ips_driver
*ips
= m
->private;
1284 seq_printf(m
, "%dmW\n", ips
->mch_avg_power
);
1289 static struct ips_debugfs_node ips_debug_files
[] = {
1290 { NULL
, "cpu_temp", show_cpu_temp
},
1291 { NULL
, "cpu_power", show_cpu_power
},
1292 { NULL
, "cpu_clamp", show_cpu_clamp
},
1293 { NULL
, "mch_temp", show_mch_temp
},
1294 { NULL
, "mch_power", show_mch_power
},
1297 static int ips_debugfs_open(struct inode
*inode
, struct file
*file
)
1299 struct ips_debugfs_node
*node
= inode
->i_private
;
1301 return single_open(file
, node
->show
, node
->ips
);
1304 static const struct file_operations ips_debugfs_ops
= {
1305 .owner
= THIS_MODULE
,
1306 .open
= ips_debugfs_open
,
1308 .llseek
= seq_lseek
,
1309 .release
= single_release
,
1312 static void ips_debugfs_cleanup(struct ips_driver
*ips
)
1314 if (ips
->debug_root
)
1315 debugfs_remove_recursive(ips
->debug_root
);
1319 static void ips_debugfs_init(struct ips_driver
*ips
)
1323 ips
->debug_root
= debugfs_create_dir("ips", NULL
);
1324 if (!ips
->debug_root
) {
1325 dev_err(ips
->dev
, "failed to create debugfs entries: %ld\n",
1326 PTR_ERR(ips
->debug_root
));
1330 for (i
= 0; i
< ARRAY_SIZE(ips_debug_files
); i
++) {
1332 struct ips_debugfs_node
*node
= &ips_debug_files
[i
];
1335 ent
= debugfs_create_file(node
->name
, S_IFREG
| S_IRUGO
,
1336 ips
->debug_root
, node
,
1339 dev_err(ips
->dev
, "failed to create debug file: %ld\n",
1348 ips_debugfs_cleanup(ips
);
1351 #endif /* CONFIG_DEBUG_FS */
1354 * ips_detect_cpu - detect whether CPU supports IPS
1356 * Walk our list and see if we're on a supported CPU. If we find one,
1357 * return the limits for it.
1359 static struct ips_mcp_limits
*ips_detect_cpu(struct ips_driver
*ips
)
1361 u64 turbo_power
, misc_en
;
1362 struct ips_mcp_limits
*limits
= NULL
;
1365 if (!(boot_cpu_data
.x86
== 6 && boot_cpu_data
.x86_model
== 37)) {
1366 dev_info(ips
->dev
, "Non-IPS CPU detected.\n");
1370 rdmsrl(IA32_MISC_ENABLE
, misc_en
);
1372 * If the turbo enable bit isn't set, we shouldn't try to enable/disable
1373 * turbo manually or we'll get an illegal MSR access, even though
1374 * turbo will still be available.
1376 if (misc_en
& IA32_MISC_TURBO_EN
)
1377 ips
->turbo_toggle_allowed
= true;
1379 ips
->turbo_toggle_allowed
= false;
1381 if (strstr(boot_cpu_data
.x86_model_id
, "CPU M"))
1382 limits
= &ips_sv_limits
;
1383 else if (strstr(boot_cpu_data
.x86_model_id
, "CPU L"))
1384 limits
= &ips_lv_limits
;
1385 else if (strstr(boot_cpu_data
.x86_model_id
, "CPU U"))
1386 limits
= &ips_ulv_limits
;
1388 dev_info(ips
->dev
, "No CPUID match found.\n");
1392 rdmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_power
);
1393 tdp
= turbo_power
& TURBO_TDP_MASK
;
1395 /* Sanity check TDP against CPU */
1396 if (limits
->core_power_limit
!= (tdp
/ 8) * 1000) {
1398 "CPU TDP doesn't match expected value (found %d, expected %d)\n",
1399 tdp
/ 8, limits
->core_power_limit
/ 1000);
1400 limits
->core_power_limit
= (tdp
/ 8) * 1000;
1407 * ips_get_i915_syms - try to get GPU control methods from i915 driver
1410 * The i915 driver exports several interfaces to allow the IPS driver to
1411 * monitor and control graphics turbo mode. If we can find them, we can
1412 * enable graphics turbo, otherwise we must disable it to avoid exceeding
1413 * thermal and power limits in the MCP.
1415 static bool ips_get_i915_syms(struct ips_driver
*ips
)
1417 ips
->read_mch_val
= symbol_get(i915_read_mch_val
);
1418 if (!ips
->read_mch_val
)
1420 ips
->gpu_raise
= symbol_get(i915_gpu_raise
);
1421 if (!ips
->gpu_raise
)
1423 ips
->gpu_lower
= symbol_get(i915_gpu_lower
);
1424 if (!ips
->gpu_lower
)
1426 ips
->gpu_busy
= symbol_get(i915_gpu_busy
);
1429 ips
->gpu_turbo_disable
= symbol_get(i915_gpu_turbo_disable
);
1430 if (!ips
->gpu_turbo_disable
)
1436 symbol_put(i915_gpu_busy
);
1438 symbol_put(i915_gpu_lower
);
1440 symbol_put(i915_gpu_raise
);
1442 symbol_put(i915_read_mch_val
);
1448 ips_gpu_turbo_enabled(struct ips_driver
*ips
)
1450 if (!ips
->gpu_busy
&& late_i915_load
) {
1451 if (ips_get_i915_syms(ips
)) {
1453 "i915 driver attached, reenabling gpu turbo\n");
1454 ips
->gpu_turbo_enabled
= !(thm_readl(THM_HTS
) & HTS_GTD_DIS
);
1458 return ips
->gpu_turbo_enabled
;
1462 ips_link_to_i915_driver(void)
1464 /* We can't cleanly get at the various ips_driver structs from
1465 * this caller (the i915 driver), so just set a flag saying
1466 * that it's time to try getting the symbols again.
1468 late_i915_load
= true;
1470 EXPORT_SYMBOL_GPL(ips_link_to_i915_driver
);
1472 static const struct pci_device_id ips_id_table
[] = {
1473 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_THERMAL_SENSOR
), },
1477 MODULE_DEVICE_TABLE(pci
, ips_id_table
);
1479 static int ips_blacklist_callback(const struct dmi_system_id
*id
)
1481 pr_info("Blacklisted intel_ips for %s\n", id
->ident
);
1485 static const struct dmi_system_id ips_blacklist
[] = {
1487 .callback
= ips_blacklist_callback
,
1488 .ident
= "HP ProBook",
1490 DMI_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
1491 DMI_MATCH(DMI_PRODUCT_NAME
, "HP ProBook"),
1494 { } /* terminating entry */
1497 static int ips_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
1500 struct ips_driver
*ips
;
1503 u16 htshi
, trc
, trc_required_mask
;
1506 if (dmi_check_system(ips_blacklist
))
1509 ips
= devm_kzalloc(&dev
->dev
, sizeof(*ips
), GFP_KERNEL
);
1513 spin_lock_init(&ips
->turbo_status_lock
);
1514 ips
->dev
= &dev
->dev
;
1516 ips
->limits
= ips_detect_cpu(ips
);
1518 dev_info(&dev
->dev
, "IPS not supported on this CPU\n");
1522 ret
= pcim_enable_device(dev
);
1524 dev_err(&dev
->dev
, "can't enable PCI device, aborting\n");
1528 ret
= pcim_iomap_regions(dev
, 1 << 0, pci_name(dev
));
1530 dev_err(&dev
->dev
, "failed to map thermal regs, aborting\n");
1533 ips
->regmap
= pcim_iomap_table(dev
)[0];
1535 pci_set_drvdata(dev
, ips
);
1537 tse
= thm_readb(THM_TSE
);
1538 if (tse
!= TSE_EN
) {
1539 dev_err(&dev
->dev
, "thermal device not enabled (0x%02x), aborting\n", tse
);
1543 trc
= thm_readw(THM_TRC
);
1544 trc_required_mask
= TRC_CORE1_EN
| TRC_CORE_PWR
| TRC_MCH_EN
;
1545 if ((trc
& trc_required_mask
) != trc_required_mask
) {
1546 dev_err(&dev
->dev
, "thermal reporting for required devices not enabled, aborting\n");
1550 if (trc
& TRC_CORE2_EN
)
1551 ips
->second_cpu
= true;
1553 update_turbo_limits(ips
);
1554 dev_dbg(&dev
->dev
, "max cpu power clamp: %dW\n",
1555 ips
->mcp_power_limit
/ 10);
1556 dev_dbg(&dev
->dev
, "max core power clamp: %dW\n",
1557 ips
->core_power_limit
/ 10);
1558 /* BIOS may update limits at runtime */
1559 if (thm_readl(THM_PSC
) & PSP_PBRT
)
1560 ips
->poll_turbo_status
= true;
1562 if (!ips_get_i915_syms(ips
)) {
1563 dev_info(&dev
->dev
, "failed to get i915 symbols, graphics turbo disabled until i915 loads\n");
1564 ips
->gpu_turbo_enabled
= false;
1566 dev_dbg(&dev
->dev
, "graphics turbo enabled\n");
1567 ips
->gpu_turbo_enabled
= true;
1571 * Check PLATFORM_INFO MSR to make sure this chip is
1574 rdmsrl(PLATFORM_INFO
, platform_info
);
1575 if (!(platform_info
& PLATFORM_TDP
)) {
1576 dev_err(&dev
->dev
, "platform indicates TDP override unavailable, aborting\n");
1581 * IRQ handler for ME interaction
1582 * Note: don't use MSI here as the PCH has bugs.
1584 ret
= pci_alloc_irq_vectors(dev
, 1, 1, PCI_IRQ_LEGACY
);
1588 ips
->irq
= pci_irq_vector(dev
, 0);
1590 ret
= request_irq(ips
->irq
, ips_irq_handler
, IRQF_SHARED
, "ips", ips
);
1592 dev_err(&dev
->dev
, "request irq failed, aborting\n");
1596 /* Enable aux, hot & critical interrupts */
1597 thm_writeb(THM_TSPIEN
, TSPIEN_AUX2_LOHI
| TSPIEN_CRIT_LOHI
|
1598 TSPIEN_HOT_LOHI
| TSPIEN_AUX_LOHI
);
1599 thm_writeb(THM_TEN
, TEN_UPDATE_EN
);
1601 /* Collect adjustment values */
1602 ips
->cta_val
= thm_readw(THM_CTA
);
1603 ips
->pta_val
= thm_readw(THM_PTA
);
1604 ips
->mgta_val
= thm_readw(THM_MGTA
);
1606 /* Save turbo limits & ratios */
1607 rdmsrl(TURBO_POWER_CURRENT_LIMIT
, ips
->orig_turbo_limit
);
1609 ips_disable_cpu_turbo(ips
);
1610 ips
->cpu_turbo_enabled
= false;
1612 /* Create thermal adjust thread */
1613 ips
->adjust
= kthread_create(ips_adjust
, ips
, "ips-adjust");
1614 if (IS_ERR(ips
->adjust
)) {
1616 "failed to create thermal adjust thread, aborting\n");
1618 goto error_free_irq
;
1623 * Set up the work queue and monitor thread. The monitor thread
1624 * will wake up ips_adjust thread.
1626 ips
->monitor
= kthread_run(ips_monitor
, ips
, "ips-monitor");
1627 if (IS_ERR(ips
->monitor
)) {
1629 "failed to create thermal monitor thread, aborting\n");
1631 goto error_thread_cleanup
;
1634 hts
= (ips
->core_power_limit
<< HTS_PCPL_SHIFT
) |
1635 (ips
->mcp_temp_limit
<< HTS_PTL_SHIFT
) | HTS_NVV
;
1636 htshi
= HTS2_PRST_RUNNING
<< HTS2_PRST_SHIFT
;
1638 thm_writew(THM_HTSHI
, htshi
);
1639 thm_writel(THM_HTS
, hts
);
1641 ips_debugfs_init(ips
);
1643 dev_info(&dev
->dev
, "IPS driver initialized, MCP temp limit %d\n",
1644 ips
->mcp_temp_limit
);
1647 error_thread_cleanup
:
1648 kthread_stop(ips
->adjust
);
1650 free_irq(ips
->irq
, ips
);
1651 pci_free_irq_vectors(dev
);
1655 static void ips_remove(struct pci_dev
*dev
)
1657 struct ips_driver
*ips
= pci_get_drvdata(dev
);
1663 ips_debugfs_cleanup(ips
);
1665 /* Release i915 driver */
1666 if (ips
->read_mch_val
)
1667 symbol_put(i915_read_mch_val
);
1669 symbol_put(i915_gpu_raise
);
1671 symbol_put(i915_gpu_lower
);
1673 symbol_put(i915_gpu_busy
);
1674 if (ips
->gpu_turbo_disable
)
1675 symbol_put(i915_gpu_turbo_disable
);
1677 rdmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
1678 turbo_override
&= ~(TURBO_TDC_OVR_EN
| TURBO_TDP_OVR_EN
);
1679 wrmsrl(TURBO_POWER_CURRENT_LIMIT
, turbo_override
);
1680 wrmsrl(TURBO_POWER_CURRENT_LIMIT
, ips
->orig_turbo_limit
);
1682 free_irq(ips
->irq
, ips
);
1683 pci_free_irq_vectors(dev
);
1685 kthread_stop(ips
->adjust
);
1687 kthread_stop(ips
->monitor
);
1688 dev_dbg(&dev
->dev
, "IPS driver removed\n");
1691 static struct pci_driver ips_pci_driver
= {
1692 .name
= "intel ips",
1693 .id_table
= ips_id_table
,
1695 .remove
= ips_remove
,
1698 module_pci_driver(ips_pci_driver
);
1700 MODULE_LICENSE("GPL");
1701 MODULE_AUTHOR("Jesse Barnes <jbarnes@virtuousgeek.org>");
1702 MODULE_DESCRIPTION("Intelligent Power Sharing Driver");