66 #include <linux/kernel.h>
68 #include <linux/module.h>
69 #include <linux/pci.h>
70 #include <linux/sched.h>
72 #include <linux/string.h>
76 #include <drm/i915_drm.h>
78 #include <asm/processor.h>
83 #define PCI_DEVICE_ID_INTEL_THERMAL_SENSOR 0x3b32
88 #define PLATFORM_INFO 0xce
89 #define PLATFORM_TDP (1<<29)
90 #define PLATFORM_RATIO (1<<28)
92 #define IA32_MISC_ENABLE 0x1a0
93 #define IA32_MISC_TURBO_EN (1ULL<<38)
95 #define TURBO_POWER_CURRENT_LIMIT 0x1ac
96 #define TURBO_TDC_OVR_EN (1UL<<31)
97 #define TURBO_TDC_MASK (0x000000007fff0000UL)
98 #define TURBO_TDC_SHIFT (16)
99 #define TURBO_TDP_OVR_EN (1UL<<15)
100 #define TURBO_TDP_MASK (0x0000000000003fffUL)
105 #define IA32_PERF_CTL 0x199
106 #define IA32_PERF_TURBO_DIS (1ULL<<32)
111 #define THM_CFG_TBAR 0x10
112 #define THM_CFG_TBAR_HI 0x14
114 #define THM_TSIU 0x00
118 #define THM_TSTR 0x03
119 #define THM_TSTTP 0x04
120 #define THM_TSCO 0x08
121 #define THM_TSES 0x0c
122 #define THM_TSGPEN 0x0d
123 #define TSGPEN_HOT_LOHI (1<<1)
124 #define TSGPEN_CRIT_LOHI (1<<2)
125 #define THM_TSPC 0x0e
126 #define THM_PPEC 0x10
129 #define PTA_SLOPE_MASK (0xff00)
130 #define PTA_SLOPE_SHIFT 8
131 #define PTA_OFFSET_MASK (0x00ff)
132 #define THM_MGTA 0x16
133 #define MGTA_SLOPE_MASK (0xff00)
134 #define MGTA_SLOPE_SHIFT 8
135 #define MGTA_OFFSET_MASK (0x00ff)
137 #define TRC_CORE2_EN (1<<15)
138 #define TRC_THM_EN (1<<12)
139 #define TRC_C6_WAR (1<<8)
140 #define TRC_CORE1_EN (1<<7)
141 #define TRC_CORE_PWR (1<<6)
142 #define TRC_PCH_EN (1<<5)
143 #define TRC_MCH_EN (1<<4)
144 #define TRC_DIMM4 (1<<3)
145 #define TRC_DIMM3 (1<<2)
146 #define TRC_DIMM2 (1<<1)
147 #define TRC_DIMM1 (1<<0)
150 #define TEN_UPDATE_EN 1
152 #define PSC_NTG (1<<0)
153 #define PSC_NTPC (1<<1)
154 #define PSC_PP_DEF (0<<2)
155 #define PSP_PP_PC (1<<2)
156 #define PSP_PP_BAL (2<<2)
157 #define PSP_PP_GFX (3<<2)
158 #define PSP_PBRT (1<<4)
159 #define THM_CTV1 0x30
160 #define CTV_TEMP_ERROR (1<<15)
161 #define CTV_TEMP_MASK 0x3f
163 #define THM_CTV2 0x32
167 #define HTS_PCPL_MASK (0x7fe00000)
168 #define HTS_PCPL_SHIFT 21
169 #define HTS_GPL_MASK (0x001ff000)
170 #define HTS_GPL_SHIFT 12
171 #define HTS_PP_MASK (0x00000c00)
172 #define HTS_PP_SHIFT 10
174 #define HTS_PP_PROC 1
177 #define HTS_PCTD_DIS (1<<9)
178 #define HTS_GTD_DIS (1<<8)
179 #define HTS_PTL_MASK (0x000000fe)
180 #define HTS_PTL_SHIFT 1
181 #define HTS_NVV (1<<0)
182 #define THM_HTSHI 0x54
183 #define HTS2_PPL_MASK (0x03ff)
184 #define HTS2_PRST_MASK (0x3c00)
185 #define HTS2_PRST_SHIFT 10
186 #define HTS2_PRST_UNLOADED 0
187 #define HTS2_PRST_RUNNING 1
188 #define HTS2_PRST_TDISOP 2
189 #define HTS2_PRST_TDISHT 3
190 #define HTS2_PRST_TDISUSR 4
191 #define HTS2_PRST_TDISPLAT 5
192 #define HTS2_PRST_TDISPM 6
193 #define HTS2_PRST_TDISERR 7
195 #define THM_MGTV 0x58
196 #define TV_MASK 0x000000000000ff00
199 #define PTV_MASK 0x00ff
200 #define THM_MMGPC 0x64
201 #define THM_MPPC 0x66
202 #define THM_MPCPC 0x68
203 #define THM_TSPIEN 0x82
204 #define TSPIEN_AUX_LOHI (1<<0)
205 #define TSPIEN_HOT_LOHI (1<<1)
206 #define TSPIEN_CRIT_LOHI (1<<2)
207 #define TSPIEN_AUX2_LOHI (1<<3)
208 #define THM_TSLOCK 0x83
212 #define STS_PCPL_MASK (0x7fe00000)
213 #define STS_PCPL_SHIFT 21
214 #define STS_GPL_MASK (0x001ff000)
215 #define STS_GPL_SHIFT 12
216 #define STS_PP_MASK (0x00000c00)
217 #define STS_PP_SHIFT 10
219 #define STS_PP_PROC 1
222 #define STS_PCTD_DIS (1<<9)
223 #define STS_GTD_DIS (1<<8)
224 #define STS_PTL_MASK (0x000000fe)
225 #define STS_PTL_SHIFT 1
226 #define STS_NVV (1<<0)
228 #define SEC_ACK (1<<0)
231 #define STS_PPL_MASK (0x0003ff00)
232 #define STS_PPL_SHIFT 16
236 #define ITV_ME_SEQNO_MASK 0x00ff0000
237 #define ITV_ME_SEQNO_SHIFT (16)
238 #define ITV_MCH_TEMP_MASK 0x0000ff00
239 #define ITV_MCH_TEMP_SHIFT (8)
240 #define ITV_PCH_TEMP_MASK 0x000000ff
242 #define thm_readb(off) readb(ips->regmap + (off))
243 #define thm_readw(off) readw(ips->regmap + (off))
244 #define thm_readl(off) readl(ips->regmap + (off))
245 #define thm_readq(off) readq(ips->regmap + (off))
247 #define thm_writeb(off, val) writeb((val), ips->regmap + (off))
248 #define thm_writew(off, val) writew((val), ips->regmap + (off))
249 #define thm_writel(off, val) writel((val), ips->regmap + (off))
251 static const int IPS_ADJUST_PERIOD = 5000;
252 static bool late_i915_load =
false;
255 static const int IPS_SAMPLE_PERIOD = 200;
256 static const int IPS_SAMPLE_WINDOW = 5000;
257 #define IPS_SAMPLE_COUNT (IPS_SAMPLE_WINDOW / IPS_SAMPLE_PERIOD)
273 .mcp_power_limit = 35000,
274 .core_power_limit = 29000,
275 .mch_power_limit = 20000,
276 .core_temp_limit = 95,
281 .mcp_power_limit = 25000,
282 .core_power_limit = 21000,
283 .mch_power_limit = 13000,
284 .core_temp_limit = 95,
289 .mcp_power_limit = 18000,
290 .core_power_limit = 14000,
291 .mch_power_limit = 11000,
292 .core_temp_limit = 95,
348 ips_gpu_turbo_enabled(
struct ips_driver *ips);
359 static bool ips_cpu_busy(
struct ips_driver *ips)
377 static void ips_cpu_raise(
struct ips_driver *ips)
380 u16 cur_tdp_limit, new_tdp_limit;
388 new_tdp_limit = cur_tdp_limit + 8;
392 new_tdp_limit = cur_tdp_limit;
399 turbo_override &= ~TURBO_TDP_MASK;
400 turbo_override |= new_tdp_limit;
415 static void ips_cpu_lower(
struct ips_driver *ips)
418 u16 cur_limit, new_limit;
423 new_limit = cur_limit - 8;
434 turbo_override &= ~TURBO_TDP_MASK;
435 turbo_override |= new_limit;
448 static void do_enable_cpu_turbo(
void *
data)
454 perf_ctl &= ~IA32_PERF_TURBO_DIS;
466 static void ips_enable_cpu_turbo(
struct ips_driver *ips)
486 static void do_disable_cpu_turbo(
void *data)
504 static void ips_disable_cpu_turbo(
struct ips_driver *ips)
526 static bool ips_gpu_busy(
struct ips_driver *ips)
528 if (!ips_gpu_turbo_enabled(ips))
541 static void ips_gpu_raise(
struct ips_driver *ips)
543 if (!ips_gpu_turbo_enabled(ips))
558 static void ips_gpu_lower(
struct ips_driver *ips)
560 if (!ips_gpu_turbo_enabled(ips))
576 static void ips_enable_gpu_turbo(
struct ips_driver *ips)
589 static void ips_disable_gpu_turbo(
struct ips_driver *ips)
596 dev_err(&ips->
dev->dev,
"failed to disable graphis turbo\n");
607 static bool mcp_exceeded(
struct ips_driver *ips)
644 if (avg > (ips->
limits->core_temp_limit * 100))
652 "CPU power or thermal limit exceeded\n");
663 static bool mch_exceeded(
struct ips_driver *ips)
686 static void verify_limits(
struct ips_driver *ips)
696 ips->
limits->mch_temp_limit);
712 static void update_turbo_limits(
struct ips_driver *ips)
766 static int ips_adjust(
void *data)
771 dev_dbg(&ips->
dev->dev,
"starting ips-adjust thread\n");
778 bool cpu_busy = ips_cpu_busy(ips);
783 update_turbo_limits(ips);
788 ips_enable_cpu_turbo(ips);
790 ips_disable_cpu_turbo(ips);
793 ips_enable_gpu_turbo(ips);
795 ips_disable_gpu_turbo(ips);
798 if (mcp_exceeded(ips)) {
804 if (!cpu_exceeded(ips, 0) && cpu_busy)
809 if (!mch_exceeded(ips) && gpu_busy)
818 dev_dbg(&ips->
dev->dev,
"ips-adjust thread stopped\n");
835 total += (
u64)(array[
i] * 100);
837 do_div(total, IPS_SAMPLE_COUNT);
857 ret = ((val * slope + 0x40) >> 7) +
offset;
900 ret = (((val - *last) * 1000) /
period);
901 ret = (ret * 1000) / 65535;
907 static const u16 temp_decay_factor = 2;
908 static u16 update_average_temp(
u16 avg,
u16 val)
913 ret = (val * 100 / temp_decay_factor) +
914 (((temp_decay_factor - 1) * avg) / temp_decay_factor);
918 static const u16 power_decay_factor = 2;
919 static u16 update_average_power(
u32 avg,
u32 val)
923 ret = (val / power_decay_factor) +
924 (((power_decay_factor - 1) * avg) / power_decay_factor);
938 do_div(total, IPS_SAMPLE_COUNT);
944 static void monitor_timeout(
unsigned long arg)
960 static int ips_monitor(
void *data)
964 unsigned long seqno_timestamp, expire, last_msecs, last_sample_period;
966 u32 *cpu_samples, *mchp_samples, old_cpu_power;
967 u16 *mcp_samples, *ctv1_samples, *ctv2_samples, *mch_samples;
968 u8 cur_seqno, last_seqno;
970 mcp_samples = kzalloc(
sizeof(
u16) * IPS_SAMPLE_COUNT,
GFP_KERNEL);
971 ctv1_samples = kzalloc(
sizeof(
u16) * IPS_SAMPLE_COUNT,
GFP_KERNEL);
972 ctv2_samples = kzalloc(
sizeof(
u16) * IPS_SAMPLE_COUNT,
GFP_KERNEL);
973 mch_samples = kzalloc(
sizeof(
u16) * IPS_SAMPLE_COUNT,
GFP_KERNEL);
974 cpu_samples = kzalloc(
sizeof(
u32) * IPS_SAMPLE_COUNT,
GFP_KERNEL);
975 mchp_samples = kzalloc(
sizeof(
u32) * IPS_SAMPLE_COUNT,
GFP_KERNEL);
976 if (!mcp_samples || !ctv1_samples || !ctv2_samples || !mch_samples ||
977 !cpu_samples || !mchp_samples) {
979 "failed to allocate sample array, ips disabled\n");
1001 mcp_samples[
i] = read_ptv(ips);
1003 val = read_ctv(ips, 0);
1004 ctv1_samples[
i] =
val;
1006 val = read_ctv(ips, 1);
1007 ctv2_samples[
i] =
val;
1009 val = read_mgtv(ips);
1010 mch_samples[
i] =
val;
1012 cpu_power = get_cpu_power(ips, &old_cpu_power,
1014 cpu_samples[
i] = cpu_power;
1018 mchp_samples[
i] = mchp;
1033 kfree(ctv1_samples);
1034 kfree(ctv2_samples);
1037 kfree(mchp_samples);
1049 last_sample_period = IPS_SAMPLE_PERIOD;
1054 u32 cpu_val, mch_val;
1058 val = read_ptv(ips);
1062 val = read_ctv(ips, 0);
1066 cpu_val = get_cpu_power(ips, &old_cpu_power,
1067 last_sample_period);
1073 val = read_ctv(ips, 1);
1079 val = read_mgtv(ips);
1097 if (cur_seqno == last_seqno &&
1099 dev_warn(&ips->
dev->dev,
"ME failed to update for more than 1s, likely hung\n");
1102 last_seqno = cur_seqno;
1114 if (!last_sample_period)
1115 last_sample_period = 1;
1119 destroy_timer_on_stack(&
timer);
1121 dev_dbg(&ips->
dev->dev,
"ips-monitor thread stopped\n");
1127 #define THM_DUMPW(reg) \
1129 u16 val = thm_readw(reg); \
1130 dev_dbg(&ips->dev->dev, #reg ": 0x%04x\n", val); \
1132 #define THM_DUMPL(reg) \
1134 u32 val = thm_readl(reg); \
1135 dev_dbg(&ips->dev->dev, #reg ": 0x%08x\n", val); \
1137 #define THM_DUMPQ(reg) \
1139 u64 val = thm_readq(reg); \
1140 dev_dbg(&ips->dev->dev, #reg ": 0x%016x\n", val); \
1143 static void dump_thermal_info(
struct ips_driver *ips)
1148 dev_dbg(&ips->
dev->dev,
"Processor temp limit: %d\n", ptl);
1168 static irqreturn_t ips_irq_handler(
int irq,
void *arg)
1217 "thermal trip occurred, tses: 0x%04x\n", tses);
1224 #ifndef CONFIG_DEBUG_FS
1225 static void ips_debugfs_init(
struct ips_driver *ips) {
return; }
1226 static void ips_debugfs_cleanup(
struct ips_driver *ips) {
return; }
1231 struct ips_debugfs_node {
1237 static int show_cpu_temp(
struct seq_file *
m,
void *data)
1247 static int show_cpu_power(
struct seq_file *
m,
void *data)
1256 static int show_cpu_clamp(
struct seq_file *
m,
void *data)
1263 tdp = (
int)(turbo_override & TURBO_TDP_MASK);
1271 seq_printf(m,
"%d.%dW %d.%dA\n", tdp / 10, tdp % 10,
1272 tdc / 10, tdc % 10);
1277 static int show_mch_temp(
struct seq_file *m,
void *data)
1287 static int show_mch_power(
struct seq_file *m,
void *data)
1296 static struct ips_debugfs_node ips_debug_files[] = {
1297 {
NULL,
"cpu_temp", show_cpu_temp },
1298 {
NULL,
"cpu_power", show_cpu_power },
1299 {
NULL,
"cpu_clamp", show_cpu_clamp },
1300 {
NULL,
"mch_temp", show_mch_temp },
1301 {
NULL,
"mch_power", show_mch_power },
1313 .open = ips_debugfs_open,
1319 static void ips_debugfs_cleanup(
struct ips_driver *ips)
1326 static void ips_debugfs_init(
struct ips_driver *ips)
1333 "failed to create debugfs entries: %ld\n",
1338 for (i = 0; i <
ARRAY_SIZE(ips_debug_files); i++) {
1340 struct ips_debugfs_node *
node = &ips_debug_files[
i];
1348 "failed to create debug file: %ld\n",
1357 ips_debugfs_cleanup(ips);
1370 u64 turbo_power, misc_en;
1375 dev_info(&ips->
dev->dev,
"Non-IPS CPU detected.\n");
1397 dev_info(&ips->
dev->dev,
"No CPUID match found.\n");
1406 dev_info(&ips->
dev->dev,
"CPU TDP doesn't match expected value (found %d, expected %d)\n",
1424 static bool ips_get_i915_syms(
struct ips_driver *ips)
1457 ips_gpu_turbo_enabled(
struct ips_driver *ips)
1459 if (!ips->
gpu_busy && late_i915_load) {
1460 if (ips_get_i915_syms(ips)) {
1462 "i915 driver attached, reenabling gpu turbo\n");
1477 late_i915_load =
true;
1489 static int ips_blacklist_callback(
const struct dmi_system_id *
id)
1497 .callback = ips_blacklist_callback,
1498 .ident =
"HP ProBook",
1513 u16 htshi,
trc, trc_required_mask;
1523 pci_set_drvdata(dev, ips);
1526 ips->
limits = ips_detect_cpu(ips);
1528 dev_info(&dev->
dev,
"IPS not supported on this CPU\n");
1537 dev_err(&dev->
dev,
"can't enable PCI device, aborting\n");
1542 dev_err(&dev->
dev,
"TBAR not assigned, aborting\n");
1549 dev_err(&dev->
dev,
"thermal resource busy, aborting\n");
1557 dev_err(&dev->
dev,
"failed to map thermal regs, aborting\n");
1564 dev_err(&dev->
dev,
"thermal device not enabled (0x%02x), aborting\n", tse);
1571 if ((trc & trc_required_mask) != trc_required_mask) {
1572 dev_err(&dev->
dev,
"thermal reporting for required devices not enabled, aborting\n");
1580 update_turbo_limits(ips);
1581 dev_dbg(&dev->
dev,
"max cpu power clamp: %dW\n",
1583 dev_dbg(&dev->
dev,
"max core power clamp: %dW\n",
1589 if (!ips_get_i915_syms(ips)) {
1590 dev_info(&dev->
dev,
"failed to get i915 symbols, graphics turbo disabled until i915 loads\n");
1593 dev_dbg(&dev->
dev,
"graphics turbo enabled\n");
1603 dev_err(&dev->
dev,
"platform indicates TDP override unavailable, aborting\n");
1616 dev_err(&dev->
dev,
"request irq failed, aborting\n");
1633 ips_disable_cpu_turbo(ips);
1638 if (IS_ERR(ips->
adjust)) {
1640 "failed to create thermal adjust thread, aborting\n");
1642 goto error_free_irq;
1653 "failed to create thermal monitor thread, aborting\n");
1655 goto error_thread_cleanup;
1665 ips_debugfs_init(ips);
1667 dev_info(&dev->
dev,
"IPS driver initialized, MCP temp limit %d\n",
1671 error_thread_cleanup:
1684 static void ips_remove(
struct pci_dev *dev)
1686 struct ips_driver *ips = pci_get_drvdata(dev);
1692 ips_debugfs_cleanup(ips);
1722 static void ips_shutdown(
struct pci_dev *dev)
1727 .name =
"intel ips",
1728 .id_table = ips_id_table,
1730 .remove = ips_remove,
1731 .shutdown = ips_shutdown,
1734 static int __init ips_init(
void)
1736 return pci_register_driver(&ips_pci_driver);
1740 static void ips_exit(
void)