1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel Core SoC Power Management Controller Driver
4 *
5 * Copyright (c) 2016, Intel Corporation.
6 * All Rights Reserved.
7 *
8 * Authors: Rajneesh Bhardwaj <rajneesh.bhardwaj@intel.com>
9 * Vishwanath Somayaji <vishwanath.somayaji@intel.com>
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 enum header_type {
15 HEADER_STATUS,
16 HEADER_VALUE,
17 };
18
19 #include <linux/bitfield.h>
20 #include <linux/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/dmi.h>
23 #include <linux/io.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/suspend.h>
28 #include <linux/units.h>
29
30 #include <asm/cpuid/api.h>
31 #include <asm/cpu_device_id.h>
32 #include <asm/intel-family.h>
33 #include <asm/msr.h>
34 #include <asm/tsc.h>
35
36 #include "core.h"
37 #include "ssram_telemetry.h"
38 #include "../pmt/telemetry.h"
39
40 /* Maximum number of modes supported by platfoms that has low power mode capability */
41 const char *pmc_lpm_modes[] = {
42 "S0i2.0",
43 "S0i2.1",
44 "S0i2.2",
45 "S0i3.0",
46 "S0i3.1",
47 "S0i3.2",
48 "S0i3.3",
49 "S0i3.4",
50 NULL
51 };
52
53 /* PKGC MSRs are common across Intel Core SoCs */
54 const struct pmc_bit_map msr_map[] = {
55 {"Package C2", MSR_PKG_C2_RESIDENCY},
56 {"Package C3", MSR_PKG_C3_RESIDENCY},
57 {"Package C6", MSR_PKG_C6_RESIDENCY},
58 {"Package C7", MSR_PKG_C7_RESIDENCY},
59 {"Package C8", MSR_PKG_C8_RESIDENCY},
60 {"Package C9", MSR_PKG_C9_RESIDENCY},
61 {"Package C10", MSR_PKG_C10_RESIDENCY},
62 {}
63 };
64
pmc_core_reg_read(struct pmc * pmc,int reg_offset)65 static inline u32 pmc_core_reg_read(struct pmc *pmc, int reg_offset)
66 {
67 return readl(pmc->regbase + reg_offset);
68 }
69
pmc_core_reg_write(struct pmc * pmc,int reg_offset,u32 val)70 static inline void pmc_core_reg_write(struct pmc *pmc, int reg_offset,
71 u32 val)
72 {
73 writel(val, pmc->regbase + reg_offset);
74 }
75
pmc_core_adjust_slp_s0_step(struct pmc * pmc,u32 value)76 static inline u64 pmc_core_adjust_slp_s0_step(struct pmc *pmc, u32 value)
77 {
78 /*
79 * ADL PCH does not have the SLP_S0 counter and LPM Residency counters are
80 * used as a workaround which uses 30.5 usec tick. All other client
81 * programs have the legacy SLP_S0 residency counter that is using the 122
82 * usec tick.
83 */
84 const int lpm_adj_x2 = pmc->map->lpm_res_counter_step_x2;
85
86 if (pmc->map == &adl_reg_map)
87 return (u64)value * GET_X2_COUNTER((u64)lpm_adj_x2);
88 else
89 return (u64)value * pmc->map->slp_s0_res_counter_step;
90 }
91
set_etr3(struct pmc_dev * pmcdev)92 static int set_etr3(struct pmc_dev *pmcdev)
93 {
94 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
95 const struct pmc_reg_map *map = pmc->map;
96 u32 reg;
97
98 if (!map->etr3_offset)
99 return -EOPNOTSUPP;
100
101 guard(mutex)(&pmcdev->lock);
102
103 /* check if CF9 is locked */
104 reg = pmc_core_reg_read(pmc, map->etr3_offset);
105 if (reg & ETR3_CF9LOCK)
106 return -EACCES;
107
108 /* write CF9 global reset bit */
109 reg |= ETR3_CF9GR;
110 pmc_core_reg_write(pmc, map->etr3_offset, reg);
111
112 reg = pmc_core_reg_read(pmc, map->etr3_offset);
113 if (!(reg & ETR3_CF9GR))
114 return -EIO;
115
116 return 0;
117 }
etr3_is_visible(struct kobject * kobj,struct attribute * attr,int idx)118 static umode_t etr3_is_visible(struct kobject *kobj,
119 struct attribute *attr,
120 int idx)
121 {
122 struct device *dev = kobj_to_dev(kobj);
123 struct pmc_dev *pmcdev = dev_get_drvdata(dev);
124 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
125 const struct pmc_reg_map *map = pmc->map;
126 u32 reg;
127
128 scoped_guard(mutex, &pmcdev->lock)
129 reg = pmc_core_reg_read(pmc, map->etr3_offset);
130
131 return reg & ETR3_CF9LOCK ? attr->mode & (SYSFS_PREALLOC | 0444) : attr->mode;
132 }
133
etr3_show(struct device * dev,struct device_attribute * attr,char * buf)134 static ssize_t etr3_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
136 {
137 struct pmc_dev *pmcdev = dev_get_drvdata(dev);
138 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
139 const struct pmc_reg_map *map = pmc->map;
140 u32 reg;
141
142 if (!map->etr3_offset)
143 return -EOPNOTSUPP;
144
145 scoped_guard(mutex, &pmcdev->lock) {
146 reg = pmc_core_reg_read(pmc, map->etr3_offset);
147 reg &= ETR3_CF9GR | ETR3_CF9LOCK;
148 }
149
150 return sysfs_emit(buf, "0x%08x", reg);
151 }
152
etr3_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)153 static ssize_t etr3_store(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t len)
156 {
157 struct pmc_dev *pmcdev = dev_get_drvdata(dev);
158 int err;
159 u32 reg;
160
161 err = kstrtouint(buf, 16, ®);
162 if (err)
163 return err;
164
165 /* allow only CF9 writes */
166 if (reg != ETR3_CF9GR)
167 return -EINVAL;
168
169 err = set_etr3(pmcdev);
170 if (err)
171 return err;
172
173 return len;
174 }
175 static DEVICE_ATTR_RW(etr3);
176
177 static struct attribute *pmc_attrs[] = {
178 &dev_attr_etr3.attr,
179 NULL
180 };
181
182 static const struct attribute_group pmc_attr_group = {
183 .attrs = pmc_attrs,
184 .is_visible = etr3_is_visible,
185 };
186
187 static const struct attribute_group *pmc_dev_groups[] = {
188 &pmc_attr_group,
189 NULL
190 };
191
pmc_core_dev_state_get(void * data,u64 * val)192 static int pmc_core_dev_state_get(void *data, u64 *val)
193 {
194 struct pmc *pmc = data;
195 const struct pmc_reg_map *map = pmc->map;
196 u32 value;
197
198 value = pmc_core_reg_read(pmc, map->slp_s0_offset);
199 *val = pmc_core_adjust_slp_s0_step(pmc, value);
200
201 return 0;
202 }
203
204 DEFINE_DEBUGFS_ATTRIBUTE(pmc_core_dev_state, pmc_core_dev_state_get, NULL, "%llu\n");
205
pmc_core_pson_residency_get(void * data,u64 * val)206 static int pmc_core_pson_residency_get(void *data, u64 *val)
207 {
208 struct pmc *pmc = data;
209 const struct pmc_reg_map *map = pmc->map;
210 u32 value;
211
212 value = pmc_core_reg_read(pmc, map->pson_residency_offset);
213 *val = (u64)value * map->pson_residency_counter_step;
214
215 return 0;
216 }
217
218 DEFINE_DEBUGFS_ATTRIBUTE(pmc_core_pson_residency, pmc_core_pson_residency_get, NULL, "%llu\n");
219
pmc_core_check_read_lock_bit(struct pmc * pmc)220 static int pmc_core_check_read_lock_bit(struct pmc *pmc)
221 {
222 u32 value;
223
224 value = pmc_core_reg_read(pmc, pmc->map->pm_cfg_offset);
225 return value & BIT(pmc->map->pm_read_disable_bit);
226 }
227
pmc_core_slps0_display(struct pmc * pmc,struct device * dev,struct seq_file * s)228 static void pmc_core_slps0_display(struct pmc *pmc, struct device *dev,
229 struct seq_file *s)
230 {
231 const struct pmc_bit_map **maps = pmc->map->slps0_dbg_maps;
232 const struct pmc_bit_map *map;
233 int offset = pmc->map->slps0_dbg_offset;
234 u32 data;
235
236 while (*maps) {
237 map = *maps;
238 data = pmc_core_reg_read(pmc, offset);
239 offset += 4;
240 while (map->name) {
241 if (dev)
242 dev_info(dev, "SLP_S0_DBG: %-32s\tState: %s\n",
243 map->name,
244 data & map->bit_mask ? "Yes" : "No");
245 if (s)
246 seq_printf(s, "SLP_S0_DBG: %-32s\tState: %s\n",
247 map->name,
248 data & map->bit_mask ? "Yes" : "No");
249 ++map;
250 }
251 ++maps;
252 }
253 }
254
pmc_core_lpm_get_arr_size(const struct pmc_bit_map ** maps)255 static unsigned int pmc_core_lpm_get_arr_size(const struct pmc_bit_map **maps)
256 {
257 unsigned int idx;
258
259 for (idx = 0; maps[idx]; idx++)
260 ;/* Nothing */
261
262 return idx;
263 }
264
pmc_core_lpm_display(struct pmc * pmc,struct device * dev,struct seq_file * s,u32 offset,int pmc_index,const char * str,const struct pmc_bit_map ** maps)265 static void pmc_core_lpm_display(struct pmc *pmc, struct device *dev,
266 struct seq_file *s, u32 offset, int pmc_index,
267 const char *str,
268 const struct pmc_bit_map **maps)
269 {
270 unsigned int index, idx, len = 32, arr_size;
271 u32 bit_mask, *lpm_regs;
272
273 arr_size = pmc_core_lpm_get_arr_size(maps);
274 lpm_regs = kmalloc_array(arr_size, sizeof(*lpm_regs), GFP_KERNEL);
275 if (!lpm_regs)
276 return;
277
278 for (index = 0; index < arr_size; index++) {
279 lpm_regs[index] = pmc_core_reg_read(pmc, offset);
280 offset += 4;
281 }
282
283 for (idx = 0; idx < arr_size; idx++) {
284 if (dev)
285 dev_info(dev, "\nPMC%d:LPM_%s_%d:\t0x%x\n", pmc_index, str, idx,
286 lpm_regs[idx]);
287 if (s)
288 seq_printf(s, "\nPMC%d:LPM_%s_%d:\t0x%x\n", pmc_index, str, idx,
289 lpm_regs[idx]);
290 for (index = 0; maps[idx][index].name && index < len; index++) {
291 bit_mask = maps[idx][index].bit_mask;
292 if (dev)
293 dev_info(dev, "PMC%d:%-30s %-30d\n", pmc_index,
294 maps[idx][index].name,
295 lpm_regs[idx] & bit_mask ? 1 : 0);
296 if (s)
297 seq_printf(s, "PMC%d:%-30s %-30d\n", pmc_index,
298 maps[idx][index].name,
299 lpm_regs[idx] & bit_mask ? 1 : 0);
300 }
301 }
302
303 kfree(lpm_regs);
304 }
305
306 static bool slps0_dbg_latch;
307
pmc_core_reg_read_byte(struct pmc * pmc,int offset)308 static inline u8 pmc_core_reg_read_byte(struct pmc *pmc, int offset)
309 {
310 return readb(pmc->regbase + offset);
311 }
312
pmc_core_display_map(struct seq_file * s,int index,int idx,int ip,int pmc_index,u8 pf_reg,const struct pmc_bit_map ** pf_map)313 static void pmc_core_display_map(struct seq_file *s, int index, int idx, int ip,
314 int pmc_index, u8 pf_reg, const struct pmc_bit_map **pf_map)
315 {
316 seq_printf(s, "PMC%d:PCH IP: %-2d - %-32s\tState: %s\n",
317 pmc_index, ip, pf_map[idx][index].name,
318 pf_map[idx][index].bit_mask & pf_reg ? "Off" : "On");
319 }
320
pmc_core_ppfear_show(struct seq_file * s,void * unused)321 static int pmc_core_ppfear_show(struct seq_file *s, void *unused)
322 {
323 struct pmc_dev *pmcdev = s->private;
324 unsigned int i;
325
326 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
327 struct pmc *pmc = pmcdev->pmcs[i];
328 const struct pmc_bit_map **maps;
329 u8 pf_regs[PPFEAR_MAX_NUM_ENTRIES];
330 unsigned int index, iter, idx, ip = 0;
331
332 if (!pmc)
333 continue;
334
335 maps = pmc->map->pfear_sts;
336 iter = pmc->map->ppfear0_offset;
337
338 for (index = 0; index < pmc->map->ppfear_buckets &&
339 index < PPFEAR_MAX_NUM_ENTRIES; index++, iter++)
340 pf_regs[index] = pmc_core_reg_read_byte(pmc, iter);
341
342 for (idx = 0; maps[idx]; idx++) {
343 for (index = 0; maps[idx][index].name &&
344 index < pmc->map->ppfear_buckets * 8; ip++, index++)
345 pmc_core_display_map(s, index, idx, ip, i,
346 pf_regs[index / 8], maps);
347 }
348 }
349
350 return 0;
351 }
352 DEFINE_SHOW_ATTRIBUTE(pmc_core_ppfear);
353
354 /* This function should return link status, 0 means ready */
pmc_core_mtpmc_link_status(struct pmc * pmc)355 static int pmc_core_mtpmc_link_status(struct pmc *pmc)
356 {
357 u32 value;
358
359 value = pmc_core_reg_read(pmc, SPT_PMC_PM_STS_OFFSET);
360 return value & BIT(SPT_PMC_MSG_FULL_STS_BIT);
361 }
362
pmc_core_send_msg(struct pmc * pmc,u32 * addr_xram)363 static int pmc_core_send_msg(struct pmc *pmc, u32 *addr_xram)
364 {
365 u32 dest;
366 int timeout;
367
368 for (timeout = NUM_RETRIES; timeout > 0; timeout--) {
369 if (pmc_core_mtpmc_link_status(pmc) == 0)
370 break;
371 msleep(5);
372 }
373
374 if (timeout <= 0 && pmc_core_mtpmc_link_status(pmc))
375 return -EBUSY;
376
377 dest = (*addr_xram & MTPMC_MASK) | (1U << 1);
378 pmc_core_reg_write(pmc, SPT_PMC_MTPMC_OFFSET, dest);
379 return 0;
380 }
381
pmc_core_mphy_pg_show(struct seq_file * s,void * unused)382 static int pmc_core_mphy_pg_show(struct seq_file *s, void *unused)
383 {
384 struct pmc_dev *pmcdev = s->private;
385 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
386 const struct pmc_bit_map *map = pmc->map->mphy_sts;
387 u32 mphy_core_reg_low, mphy_core_reg_high;
388 u32 val_low, val_high;
389 unsigned int index;
390 int err = 0;
391
392 if (pmcdev->pmc_xram_read_bit) {
393 seq_puts(s, "Access denied: please disable PMC_READ_DISABLE setting in BIOS.");
394 return 0;
395 }
396
397 mphy_core_reg_low = (SPT_PMC_MPHY_CORE_STS_0 << 16);
398 mphy_core_reg_high = (SPT_PMC_MPHY_CORE_STS_1 << 16);
399
400 guard(mutex)(&pmcdev->lock);
401
402 err = pmc_core_send_msg(pmc, &mphy_core_reg_low);
403 if (err)
404 return err;
405
406 msleep(10);
407 val_low = pmc_core_reg_read(pmc, SPT_PMC_MFPMC_OFFSET);
408
409 err = pmc_core_send_msg(pmc, &mphy_core_reg_high);
410 if (err)
411 return err;
412
413 msleep(10);
414 val_high = pmc_core_reg_read(pmc, SPT_PMC_MFPMC_OFFSET);
415
416 for (index = 0; index < 8 && map[index].name; index++) {
417 seq_printf(s, "%-32s\tState: %s\n",
418 map[index].name,
419 map[index].bit_mask & val_low ? "Not power gated" :
420 "Power gated");
421 }
422
423 for (index = 8; map[index].name; index++) {
424 seq_printf(s, "%-32s\tState: %s\n",
425 map[index].name,
426 map[index].bit_mask & val_high ? "Not power gated" :
427 "Power gated");
428 }
429
430 return 0;
431 }
432 DEFINE_SHOW_ATTRIBUTE(pmc_core_mphy_pg);
433
pmc_core_pll_show(struct seq_file * s,void * unused)434 static int pmc_core_pll_show(struct seq_file *s, void *unused)
435 {
436 struct pmc_dev *pmcdev = s->private;
437 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
438 const struct pmc_bit_map *map = pmc->map->pll_sts;
439 u32 mphy_common_reg, val;
440 unsigned int index;
441 int err = 0;
442
443 if (pmcdev->pmc_xram_read_bit) {
444 seq_puts(s, "Access denied: please disable PMC_READ_DISABLE setting in BIOS.");
445 return 0;
446 }
447
448 mphy_common_reg = (SPT_PMC_MPHY_COM_STS_0 << 16);
449 guard(mutex)(&pmcdev->lock);
450
451 err = pmc_core_send_msg(pmc, &mphy_common_reg);
452 if (err)
453 return err;
454
455 /* Observed PMC HW response latency for MTPMC-MFPMC is ~10 ms */
456 msleep(10);
457 val = pmc_core_reg_read(pmc, SPT_PMC_MFPMC_OFFSET);
458
459 for (index = 0; map[index].name ; index++) {
460 seq_printf(s, "%-32s\tState: %s\n",
461 map[index].name,
462 map[index].bit_mask & val ? "Active" : "Idle");
463 }
464
465 return 0;
466 }
467 DEFINE_SHOW_ATTRIBUTE(pmc_core_pll);
468
pmc_core_send_ltr_ignore(struct pmc_dev * pmcdev,u32 value,int ignore)469 int pmc_core_send_ltr_ignore(struct pmc_dev *pmcdev, u32 value, int ignore)
470 {
471 struct pmc *pmc;
472 const struct pmc_reg_map *map;
473 u32 reg;
474 unsigned int pmc_index;
475 int ltr_index;
476
477 ltr_index = value;
478 /* For platforms with multiple pmcs, ltr index value given by user
479 * is based on the contiguous indexes from ltr_show output.
480 * pmc index and ltr index needs to be calculated from it.
481 */
482 for (pmc_index = 0; pmc_index < ARRAY_SIZE(pmcdev->pmcs) && ltr_index >= 0; pmc_index++) {
483 pmc = pmcdev->pmcs[pmc_index];
484
485 if (!pmc)
486 continue;
487
488 map = pmc->map;
489 if (ltr_index <= map->ltr_ignore_max)
490 break;
491
492 /* Along with IP names, ltr_show map includes CURRENT_PLATFORM
493 * and AGGREGATED_SYSTEM values per PMC. Take these two index
494 * values into account in ltr_index calculation. Also, to start
495 * ltr index from zero for next pmc, subtract it by 1.
496 */
497 ltr_index = ltr_index - (map->ltr_ignore_max + 2) - 1;
498 }
499
500 if (pmc_index >= ARRAY_SIZE(pmcdev->pmcs) || ltr_index < 0)
501 return -EINVAL;
502
503 pr_debug("ltr_ignore for pmc%d: ltr_index:%d\n", pmc_index, ltr_index);
504
505 guard(mutex)(&pmcdev->lock);
506
507 reg = pmc_core_reg_read(pmc, map->ltr_ignore_offset);
508 if (ignore)
509 reg |= BIT(ltr_index);
510 else
511 reg &= ~BIT(ltr_index);
512 pmc_core_reg_write(pmc, map->ltr_ignore_offset, reg);
513
514 return 0;
515 }
516
pmc_core_ltr_write(struct pmc_dev * pmcdev,const char __user * userbuf,size_t count,int ignore)517 static ssize_t pmc_core_ltr_write(struct pmc_dev *pmcdev,
518 const char __user *userbuf,
519 size_t count, int ignore)
520 {
521 u32 value;
522 int err;
523
524 err = kstrtou32_from_user(userbuf, count, 10, &value);
525 if (err)
526 return err;
527
528 err = pmc_core_send_ltr_ignore(pmcdev, value, ignore);
529
530 return err ?: count;
531 }
532
pmc_core_ltr_ignore_write(struct file * file,const char __user * userbuf,size_t count,loff_t * ppos)533 static ssize_t pmc_core_ltr_ignore_write(struct file *file,
534 const char __user *userbuf,
535 size_t count, loff_t *ppos)
536 {
537 struct seq_file *s = file->private_data;
538 struct pmc_dev *pmcdev = s->private;
539
540 return pmc_core_ltr_write(pmcdev, userbuf, count, 1);
541 }
542
pmc_core_ltr_ignore_show(struct seq_file * s,void * unused)543 static int pmc_core_ltr_ignore_show(struct seq_file *s, void *unused)
544 {
545 return 0;
546 }
547 DEFINE_SHOW_STORE_ATTRIBUTE(pmc_core_ltr_ignore);
548
pmc_core_ltr_restore_write(struct file * file,const char __user * userbuf,size_t count,loff_t * ppos)549 static ssize_t pmc_core_ltr_restore_write(struct file *file,
550 const char __user *userbuf,
551 size_t count, loff_t *ppos)
552 {
553 struct seq_file *s = file->private_data;
554 struct pmc_dev *pmcdev = s->private;
555
556 return pmc_core_ltr_write(pmcdev, userbuf, count, 0);
557 }
558
pmc_core_ltr_restore_show(struct seq_file * s,void * unused)559 static int pmc_core_ltr_restore_show(struct seq_file *s, void *unused)
560 {
561 return 0;
562 }
563 DEFINE_SHOW_STORE_ATTRIBUTE(pmc_core_ltr_restore);
564
pmc_core_slps0_dbg_latch(struct pmc_dev * pmcdev,bool reset)565 static void pmc_core_slps0_dbg_latch(struct pmc_dev *pmcdev, bool reset)
566 {
567 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
568 const struct pmc_reg_map *map = pmc->map;
569 u32 fd;
570
571 guard(mutex)(&pmcdev->lock);
572
573 if (!reset && !slps0_dbg_latch)
574 return;
575
576 fd = pmc_core_reg_read(pmc, map->slps0_dbg_offset);
577 if (reset)
578 fd &= ~CNP_PMC_LATCH_SLPS0_EVENTS;
579 else
580 fd |= CNP_PMC_LATCH_SLPS0_EVENTS;
581 pmc_core_reg_write(pmc, map->slps0_dbg_offset, fd);
582
583 slps0_dbg_latch = false;
584 }
585
pmc_core_slps0_dbg_show(struct seq_file * s,void * unused)586 static int pmc_core_slps0_dbg_show(struct seq_file *s, void *unused)
587 {
588 struct pmc_dev *pmcdev = s->private;
589
590 pmc_core_slps0_dbg_latch(pmcdev, false);
591 pmc_core_slps0_display(pmcdev->pmcs[PMC_IDX_MAIN], NULL, s);
592 pmc_core_slps0_dbg_latch(pmcdev, true);
593
594 return 0;
595 }
596 DEFINE_SHOW_ATTRIBUTE(pmc_core_slps0_dbg);
597
convert_ltr_scale(u32 val)598 static u32 convert_ltr_scale(u32 val)
599 {
600 /*
601 * As per PCIE specification supporting document
602 * ECN_LatencyTolnReporting_14Aug08.pdf the Latency
603 * Tolerance Reporting data payload is encoded in a
604 * 3 bit scale and 10 bit value fields. Values are
605 * multiplied by the indicated scale to yield an absolute time
606 * value, expressible in a range from 1 nanosecond to
607 * 2^25*(2^10-1) = 34,326,183,936 nanoseconds.
608 *
609 * scale encoding is as follows:
610 *
611 * ----------------------------------------------
612 * |scale factor | Multiplier (ns) |
613 * ----------------------------------------------
614 * | 0 | 1 |
615 * | 1 | 32 |
616 * | 2 | 1024 |
617 * | 3 | 32768 |
618 * | 4 | 1048576 |
619 * | 5 | 33554432 |
620 * | 6 | Invalid |
621 * | 7 | Invalid |
622 * ----------------------------------------------
623 */
624 if (val > 5) {
625 pr_warn("Invalid LTR scale factor.\n");
626 return 0;
627 }
628
629 return 1U << (5 * val);
630 }
631
pmc_core_ltr_show(struct seq_file * s,void * unused)632 static int pmc_core_ltr_show(struct seq_file *s, void *unused)
633 {
634 struct pmc_dev *pmcdev = s->private;
635 u64 decoded_snoop_ltr, decoded_non_snoop_ltr, val;
636 u32 ltr_raw_data, scale;
637 u16 snoop_ltr, nonsnoop_ltr;
638 unsigned int i, index, ltr_index = 0;
639
640 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
641 struct pmc *pmc;
642 const struct pmc_bit_map *map;
643 u32 ltr_ign_reg;
644
645 pmc = pmcdev->pmcs[i];
646 if (!pmc)
647 continue;
648
649 scoped_guard(mutex, &pmcdev->lock)
650 ltr_ign_reg = pmc_core_reg_read(pmc, pmc->map->ltr_ignore_offset);
651
652 map = pmc->map->ltr_show_sts;
653 for (index = 0; map[index].name; index++) {
654 bool ltr_ign_data;
655
656 if (index > pmc->map->ltr_ignore_max)
657 ltr_ign_data = false;
658 else
659 ltr_ign_data = ltr_ign_reg & BIT(index);
660
661 decoded_snoop_ltr = decoded_non_snoop_ltr = 0;
662 ltr_raw_data = pmc_core_reg_read(pmc,
663 map[index].bit_mask);
664 snoop_ltr = ltr_raw_data & ~MTPMC_MASK;
665 nonsnoop_ltr = (ltr_raw_data >> 0x10) & ~MTPMC_MASK;
666
667 if (FIELD_GET(LTR_REQ_NONSNOOP, ltr_raw_data)) {
668 scale = FIELD_GET(LTR_DECODED_SCALE, nonsnoop_ltr);
669 val = FIELD_GET(LTR_DECODED_VAL, nonsnoop_ltr);
670 decoded_non_snoop_ltr = val * convert_ltr_scale(scale);
671 }
672 if (FIELD_GET(LTR_REQ_SNOOP, ltr_raw_data)) {
673 scale = FIELD_GET(LTR_DECODED_SCALE, snoop_ltr);
674 val = FIELD_GET(LTR_DECODED_VAL, snoop_ltr);
675 decoded_snoop_ltr = val * convert_ltr_scale(scale);
676 }
677
678 seq_printf(s, "%d\tPMC%d:%-32s\tLTR: RAW: 0x%-16x\tNon-Snoop(ns): %-16llu\tSnoop(ns): %-16llu\tLTR_IGNORE: %d\n",
679 ltr_index, i, map[index].name, ltr_raw_data,
680 decoded_non_snoop_ltr,
681 decoded_snoop_ltr, ltr_ign_data);
682 ltr_index++;
683 }
684 }
685 return 0;
686 }
687 DEFINE_SHOW_ATTRIBUTE(pmc_core_ltr);
688
pmc_core_s0ix_blocker_show(struct seq_file * s,void * unused)689 static int pmc_core_s0ix_blocker_show(struct seq_file *s, void *unused)
690 {
691 struct pmc_dev *pmcdev = s->private;
692 unsigned int pmcidx;
693
694 for (pmcidx = 0; pmcidx < ARRAY_SIZE(pmcdev->pmcs); pmcidx++) {
695 const struct pmc_bit_map **maps;
696 unsigned int arr_size, r_idx;
697 u32 offset, counter;
698 struct pmc *pmc;
699
700 pmc = pmcdev->pmcs[pmcidx];
701 if (!pmc)
702 continue;
703 maps = pmc->map->s0ix_blocker_maps;
704 offset = pmc->map->s0ix_blocker_offset;
705 arr_size = pmc_core_lpm_get_arr_size(maps);
706
707 for (r_idx = 0; r_idx < arr_size; r_idx++) {
708 const struct pmc_bit_map *map;
709
710 for (map = maps[r_idx]; map->name; map++) {
711 if (!map->blk)
712 continue;
713 counter = pmc_core_reg_read(pmc, offset);
714 seq_printf(s, "PMC%d:%-30s %-30d\n", pmcidx,
715 map->name, counter);
716 offset += map->blk * S0IX_BLK_SIZE;
717 }
718 }
719 }
720 return 0;
721 }
722 DEFINE_SHOW_ATTRIBUTE(pmc_core_s0ix_blocker);
723
pmc_core_ltr_ignore_all(struct pmc_dev * pmcdev)724 static void pmc_core_ltr_ignore_all(struct pmc_dev *pmcdev)
725 {
726 unsigned int i;
727
728 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); i++) {
729 struct pmc *pmc;
730 u32 ltr_ign;
731
732 pmc = pmcdev->pmcs[i];
733 if (!pmc)
734 continue;
735
736 guard(mutex)(&pmcdev->lock);
737 pmc->ltr_ign = pmc_core_reg_read(pmc, pmc->map->ltr_ignore_offset);
738
739 /* ltr_ignore_max is the max index value for LTR ignore register */
740 ltr_ign = pmc->ltr_ign | GENMASK(pmc->map->ltr_ignore_max, 0);
741 pmc_core_reg_write(pmc, pmc->map->ltr_ignore_offset, ltr_ign);
742 }
743
744 /*
745 * Ignoring ME during suspend is blocking platforms with ADL PCH to get to
746 * deeper S0ix substate.
747 */
748 pmc_core_send_ltr_ignore(pmcdev, 6, 0);
749 }
750
pmc_core_ltr_restore_all(struct pmc_dev * pmcdev)751 static void pmc_core_ltr_restore_all(struct pmc_dev *pmcdev)
752 {
753 unsigned int i;
754
755 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); i++) {
756 struct pmc *pmc;
757
758 pmc = pmcdev->pmcs[i];
759 if (!pmc)
760 continue;
761
762 guard(mutex)(&pmcdev->lock);
763 pmc_core_reg_write(pmc, pmc->map->ltr_ignore_offset, pmc->ltr_ign);
764 }
765 }
766
adjust_lpm_residency(struct pmc * pmc,u32 offset,const int lpm_adj_x2)767 static inline u64 adjust_lpm_residency(struct pmc *pmc, u32 offset,
768 const int lpm_adj_x2)
769 {
770 u64 lpm_res = pmc_core_reg_read(pmc, offset);
771
772 return GET_X2_COUNTER((u64)lpm_adj_x2 * lpm_res);
773 }
774
pmc_core_substate_res_show(struct seq_file * s,void * unused)775 static int pmc_core_substate_res_show(struct seq_file *s, void *unused)
776 {
777 struct pmc_dev *pmcdev = s->private;
778 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
779 const int lpm_adj_x2 = pmc->map->lpm_res_counter_step_x2;
780 u32 offset = pmc->map->lpm_residency_offset;
781 int mode;
782
783 seq_printf(s, "%-10s %-15s\n", "Substate", "Residency");
784
785 pmc_for_each_mode(mode, pmcdev) {
786 seq_printf(s, "%-10s %-15llu\n", pmc_lpm_modes[mode],
787 adjust_lpm_residency(pmc, offset + (4 * mode), lpm_adj_x2));
788 }
789
790 return 0;
791 }
792 DEFINE_SHOW_ATTRIBUTE(pmc_core_substate_res);
793
pmc_core_substate_sts_regs_show(struct seq_file * s,void * unused)794 static int pmc_core_substate_sts_regs_show(struct seq_file *s, void *unused)
795 {
796 struct pmc_dev *pmcdev = s->private;
797 unsigned int i;
798
799 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
800 struct pmc *pmc = pmcdev->pmcs[i];
801 const struct pmc_bit_map **maps;
802 u32 offset;
803
804 if (!pmc)
805 continue;
806 maps = pmc->map->lpm_sts;
807 offset = pmc->map->lpm_status_offset;
808 pmc_core_lpm_display(pmc, NULL, s, offset, i, "STATUS", maps);
809 }
810
811 return 0;
812 }
813 DEFINE_SHOW_ATTRIBUTE(pmc_core_substate_sts_regs);
814
pmc_core_substate_l_sts_regs_show(struct seq_file * s,void * unused)815 static int pmc_core_substate_l_sts_regs_show(struct seq_file *s, void *unused)
816 {
817 struct pmc_dev *pmcdev = s->private;
818 unsigned int i;
819
820 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
821 struct pmc *pmc = pmcdev->pmcs[i];
822 const struct pmc_bit_map **maps;
823 u32 offset;
824
825 if (!pmc)
826 continue;
827 maps = pmc->map->lpm_sts;
828 offset = pmc->map->lpm_live_status_offset;
829 pmc_core_lpm_display(pmc, NULL, s, offset, i, "LIVE_STATUS", maps);
830 }
831
832 return 0;
833 }
834 DEFINE_SHOW_ATTRIBUTE(pmc_core_substate_l_sts_regs);
835
pmc_core_substate_req_header_show(struct seq_file * s,int pmc_index,enum header_type type)836 static void pmc_core_substate_req_header_show(struct seq_file *s, int pmc_index,
837 enum header_type type)
838 {
839 struct pmc_dev *pmcdev = s->private;
840 int mode;
841
842 seq_printf(s, "%40s |", "Element");
843 pmc_for_each_mode(mode, pmcdev)
844 seq_printf(s, " %9s |", pmc_lpm_modes[mode]);
845
846 if (type == HEADER_STATUS) {
847 seq_printf(s, " %9s |", "Status");
848 seq_printf(s, " %11s |\n", "Live Status");
849 } else {
850 seq_printf(s, " %9s |\n", "Value");
851 }
852 }
853
pmc_core_substate_blk_req_show(struct seq_file * s,void * unused)854 static int pmc_core_substate_blk_req_show(struct seq_file *s, void *unused)
855 {
856 struct pmc_dev *pmcdev = s->private;
857 unsigned int pmc_idx;
858
859 for (pmc_idx = 0; pmc_idx < ARRAY_SIZE(pmcdev->pmcs); pmc_idx++) {
860 const struct pmc_bit_map **maps;
861 unsigned int arr_size, r_idx;
862 u32 offset, counter;
863 u32 *lpm_req_regs;
864 struct pmc *pmc;
865
866 pmc = pmcdev->pmcs[pmc_idx];
867 if (!pmc || !pmc->lpm_req_regs)
868 continue;
869
870 lpm_req_regs = pmc->lpm_req_regs;
871 maps = pmc->map->s0ix_blocker_maps;
872 offset = pmc->map->s0ix_blocker_offset;
873 arr_size = pmc_core_lpm_get_arr_size(maps);
874
875 /* Display the header */
876 pmc_core_substate_req_header_show(s, pmc_idx, HEADER_VALUE);
877
878 for (r_idx = 0; r_idx < arr_size; r_idx++) {
879 const struct pmc_bit_map *map;
880
881 for (map = maps[r_idx]; map->name; map++) {
882 int mode;
883
884 if (!map->blk)
885 continue;
886
887 counter = pmc_core_reg_read(pmc, offset);
888 seq_printf(s, "pmc%u: %34s |", pmc_idx, map->name);
889 pmc_for_each_mode(mode, pmcdev) {
890 bool required = *lpm_req_regs & BIT(mode);
891
892 seq_printf(s, " %9s |", required ? "Required" : " ");
893 }
894 seq_printf(s, " %9u |\n", counter);
895 offset += map->blk * S0IX_BLK_SIZE;
896 lpm_req_regs++;
897 }
898 }
899 }
900 return 0;
901 }
902
pmc_core_substate_blk_req_open(struct inode * inode,struct file * file)903 static int pmc_core_substate_blk_req_open(struct inode *inode, struct file *file)
904 {
905 return single_open(file, pmc_core_substate_blk_req_show, inode->i_private);
906 }
907
908 const struct file_operations pmc_core_substate_blk_req_fops = {
909 .owner = THIS_MODULE,
910 .open = pmc_core_substate_blk_req_open,
911 .read = seq_read,
912 .llseek = seq_lseek,
913 .release = single_release,
914 };
915
pmc_core_substate_req_regs_show(struct seq_file * s,void * unused)916 static int pmc_core_substate_req_regs_show(struct seq_file *s, void *unused)
917 {
918 struct pmc_dev *pmcdev = s->private;
919 u32 sts_offset;
920 u32 sts_offset_live;
921 u32 *lpm_req_regs;
922 unsigned int mp, pmc_index;
923 int num_maps;
924
925 for (pmc_index = 0; pmc_index < ARRAY_SIZE(pmcdev->pmcs); ++pmc_index) {
926 struct pmc *pmc = pmcdev->pmcs[pmc_index];
927 const struct pmc_bit_map **maps;
928
929 if (!pmc)
930 continue;
931
932 maps = pmc->map->lpm_sts;
933 num_maps = pmc->map->lpm_num_maps;
934 sts_offset = pmc->map->lpm_status_offset;
935 sts_offset_live = pmc->map->lpm_live_status_offset;
936 lpm_req_regs = pmc->lpm_req_regs;
937
938 /*
939 * When there are multiple PMCs, though the PMC may exist, the
940 * requirement register discovery could have failed so check
941 * before accessing.
942 */
943 if (!lpm_req_regs)
944 continue;
945
946 /* Display the header */
947 pmc_core_substate_req_header_show(s, pmc_index, HEADER_STATUS);
948
949 /* Loop over maps */
950 for (mp = 0; mp < num_maps; mp++) {
951 u32 req_mask = 0;
952 u32 lpm_status;
953 u32 lpm_status_live;
954 const struct pmc_bit_map *map;
955 int mode, i, len = 32;
956
957 /*
958 * Capture the requirements and create a mask so that we only
959 * show an element if it's required for at least one of the
960 * enabled low power modes
961 */
962 pmc_for_each_mode(mode, pmcdev)
963 req_mask |= lpm_req_regs[mp + (mode * num_maps)];
964
965 /* Get the last latched status for this map */
966 lpm_status = pmc_core_reg_read(pmc, sts_offset + (mp * 4));
967
968 /* Get the runtime status for this map */
969 lpm_status_live = pmc_core_reg_read(pmc, sts_offset_live + (mp * 4));
970
971 /* Loop over elements in this map */
972 map = maps[mp];
973 for (i = 0; map[i].name && i < len; i++) {
974 u32 bit_mask = map[i].bit_mask;
975
976 if (!(bit_mask & req_mask)) {
977 /*
978 * Not required for any enabled states
979 * so don't display
980 */
981 continue;
982 }
983
984 /* Display the element name in the first column */
985 seq_printf(s, "pmc%d: %34s |", pmc_index, map[i].name);
986
987 /* Loop over the enabled states and display if required */
988 pmc_for_each_mode(mode, pmcdev) {
989 bool required = lpm_req_regs[mp + (mode * num_maps)] &
990 bit_mask;
991 seq_printf(s, " %9s |", required ? "Required" : " ");
992 }
993
994 /* In Status column, show the last captured state of this agent */
995 seq_printf(s, " %9s |", lpm_status & bit_mask ? "Yes" : " ");
996
997 /* In Live status column, show the live state of this agent */
998 seq_printf(s, " %11s |", lpm_status_live & bit_mask ? "Yes" : " ");
999
1000 seq_puts(s, "\n");
1001 }
1002 }
1003 }
1004 return 0;
1005 }
1006
pmc_core_substate_req_regs_open(struct inode * inode,struct file * file)1007 static int pmc_core_substate_req_regs_open(struct inode *inode, struct file *file)
1008 {
1009 return single_open(file, pmc_core_substate_req_regs_show, inode->i_private);
1010 }
1011
1012 const struct file_operations pmc_core_substate_req_regs_fops = {
1013 .owner = THIS_MODULE,
1014 .open = pmc_core_substate_req_regs_open,
1015 .read = seq_read,
1016 .llseek = seq_lseek,
1017 .release = single_release,
1018 };
1019
pmc_core_get_crystal_freq(void)1020 static unsigned int pmc_core_get_crystal_freq(void)
1021 {
1022 unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
1023
1024 if (boot_cpu_data.cpuid_level < CPUID_LEAF_TSC)
1025 return 0;
1026
1027 eax_denominator = ebx_numerator = ecx_hz = edx = 0;
1028
1029 /* TSC/Crystal ratio, plus optionally Crystal Hz */
1030 cpuid(CPUID_LEAF_TSC, &eax_denominator, &ebx_numerator, &ecx_hz, &edx);
1031
1032 if (ebx_numerator == 0 || eax_denominator == 0)
1033 return 0;
1034
1035 return ecx_hz;
1036 }
1037
pmc_core_die_c6_us_show(struct seq_file * s,void * unused)1038 static int pmc_core_die_c6_us_show(struct seq_file *s, void *unused)
1039 {
1040 struct pmc_dev *pmcdev = s->private;
1041 u64 die_c6_res, count;
1042 int ret;
1043
1044 if (!pmcdev->crystal_freq) {
1045 dev_warn_once(&pmcdev->pdev->dev, "Crystal frequency unavailable\n");
1046 return -ENXIO;
1047 }
1048
1049 ret = pmt_telem_read(pmcdev->punit_ep, pmcdev->die_c6_offset,
1050 &count, 1);
1051 if (ret)
1052 return ret;
1053
1054 die_c6_res = div64_u64(count * HZ_PER_MHZ, pmcdev->crystal_freq);
1055 seq_printf(s, "%llu\n", die_c6_res);
1056
1057 return 0;
1058 }
1059 DEFINE_SHOW_ATTRIBUTE(pmc_core_die_c6_us);
1060
pmc_core_lpm_latch_mode_show(struct seq_file * s,void * unused)1061 static int pmc_core_lpm_latch_mode_show(struct seq_file *s, void *unused)
1062 {
1063 struct pmc_dev *pmcdev = s->private;
1064 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1065 bool c10;
1066 u32 reg;
1067 int mode;
1068
1069 reg = pmc_core_reg_read(pmc, pmc->map->lpm_sts_latch_en_offset);
1070 if (reg & LPM_STS_LATCH_MODE) {
1071 seq_puts(s, "c10");
1072 c10 = false;
1073 } else {
1074 seq_puts(s, "[c10]");
1075 c10 = true;
1076 }
1077
1078 pmc_for_each_mode(mode, pmcdev) {
1079 if ((BIT(mode) & reg) && !c10)
1080 seq_printf(s, " [%s]", pmc_lpm_modes[mode]);
1081 else
1082 seq_printf(s, " %s", pmc_lpm_modes[mode]);
1083 }
1084
1085 seq_puts(s, " clear\n");
1086
1087 return 0;
1088 }
1089
pmc_core_lpm_latch_mode_write(struct file * file,const char __user * userbuf,size_t count,loff_t * ppos)1090 static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
1091 const char __user *userbuf,
1092 size_t count, loff_t *ppos)
1093 {
1094 struct seq_file *s = file->private_data;
1095 struct pmc_dev *pmcdev = s->private;
1096 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1097 bool clear = false, c10 = false;
1098 unsigned char buf[8];
1099 int m, mode;
1100 u32 reg;
1101
1102 if (count > sizeof(buf) - 1)
1103 return -EINVAL;
1104 if (copy_from_user(buf, userbuf, count))
1105 return -EFAULT;
1106 buf[count] = '\0';
1107
1108 /*
1109 * Allowed strings are:
1110 * Any enabled substate, e.g. 'S0i2.0'
1111 * 'c10'
1112 * 'clear'
1113 */
1114 mode = sysfs_match_string(pmc_lpm_modes, buf);
1115
1116 /* Check string matches enabled mode */
1117 pmc_for_each_mode(m, pmcdev)
1118 if (mode == m)
1119 break;
1120
1121 if (mode != m || mode < 0) {
1122 if (sysfs_streq(buf, "clear"))
1123 clear = true;
1124 else if (sysfs_streq(buf, "c10"))
1125 c10 = true;
1126 else
1127 return -EINVAL;
1128 }
1129
1130 if (clear) {
1131 guard(mutex)(&pmcdev->lock);
1132
1133 reg = pmc_core_reg_read(pmc, pmc->map->etr3_offset);
1134 reg |= ETR3_CLEAR_LPM_EVENTS;
1135 pmc_core_reg_write(pmc, pmc->map->etr3_offset, reg);
1136
1137 return count;
1138 }
1139
1140 if (c10) {
1141 guard(mutex)(&pmcdev->lock);
1142
1143 reg = pmc_core_reg_read(pmc, pmc->map->lpm_sts_latch_en_offset);
1144 reg &= ~LPM_STS_LATCH_MODE;
1145 pmc_core_reg_write(pmc, pmc->map->lpm_sts_latch_en_offset, reg);
1146
1147 return count;
1148 }
1149
1150 /*
1151 * For LPM mode latching we set the latch enable bit and selected mode
1152 * and clear everything else.
1153 */
1154 reg = LPM_STS_LATCH_MODE | BIT(mode);
1155 guard(mutex)(&pmcdev->lock);
1156 pmc_core_reg_write(pmc, pmc->map->lpm_sts_latch_en_offset, reg);
1157
1158 return count;
1159 }
1160 DEFINE_PMC_CORE_ATTR_WRITE(pmc_core_lpm_latch_mode);
1161
pmc_core_pkgc_show(struct seq_file * s,void * unused)1162 static int pmc_core_pkgc_show(struct seq_file *s, void *unused)
1163 {
1164 struct pmc *pmc = s->private;
1165 const struct pmc_bit_map *map = pmc->map->msr_sts;
1166 u64 pcstate_count;
1167 unsigned int index;
1168
1169 for (index = 0; map[index].name ; index++) {
1170 if (rdmsrq_safe(map[index].bit_mask, &pcstate_count))
1171 continue;
1172
1173 pcstate_count *= 1000;
1174 do_div(pcstate_count, tsc_khz);
1175 seq_printf(s, "%-8s : %llu\n", map[index].name,
1176 pcstate_count);
1177 }
1178
1179 return 0;
1180 }
1181 DEFINE_SHOW_ATTRIBUTE(pmc_core_pkgc);
1182
pmc_core_pri_verify(u32 lpm_pri,u8 * mode_order)1183 static bool pmc_core_pri_verify(u32 lpm_pri, u8 *mode_order)
1184 {
1185 unsigned int i, j;
1186
1187 if (!lpm_pri)
1188 return false;
1189 /*
1190 * Each byte contains the priority level for 2 modes (7:4 and 3:0).
1191 * In a 32 bit register this allows for describing 8 modes. Store the
1192 * levels and look for values out of range.
1193 */
1194 for (i = 0; i < 8; i++) {
1195 int level = lpm_pri & GENMASK(3, 0);
1196
1197 if (level >= LPM_MAX_NUM_MODES)
1198 return false;
1199
1200 mode_order[i] = level;
1201 lpm_pri >>= 4;
1202 }
1203
1204 /* Check that we have unique values */
1205 for (i = 0; i < LPM_MAX_NUM_MODES - 1; i++)
1206 for (j = i + 1; j < LPM_MAX_NUM_MODES; j++)
1207 if (mode_order[i] == mode_order[j])
1208 return false;
1209
1210 return true;
1211 }
1212
pmc_core_get_low_power_modes(struct pmc_dev * pmcdev)1213 void pmc_core_get_low_power_modes(struct pmc_dev *pmcdev)
1214 {
1215 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1216 u8 pri_order[LPM_MAX_NUM_MODES] = LPM_DEFAULT_PRI;
1217 u8 mode_order[LPM_MAX_NUM_MODES];
1218 u32 lpm_pri;
1219 u32 lpm_en;
1220 unsigned int i;
1221 int mode, p;
1222
1223 /* Use LPM Maps to indicate support for substates */
1224 if (!pmc->map->lpm_num_maps)
1225 return;
1226
1227 lpm_en = pmc_core_reg_read(pmc, pmc->map->lpm_en_offset);
1228 /* For MTL, BIT 31 is not an lpm mode but a enable bit.
1229 * Lower byte is enough to cover the number of lpm modes for all
1230 * platforms and hence mask the upper 3 bytes.
1231 */
1232 pmcdev->num_lpm_modes = hweight32(lpm_en & 0xFF);
1233
1234 /* Read 32 bit LPM_PRI register */
1235 lpm_pri = pmc_core_reg_read(pmc, pmc->map->lpm_priority_offset);
1236
1237
1238 /*
1239 * If lpm_pri value passes verification, then override the default
1240 * modes here. Otherwise stick with the default.
1241 */
1242 if (pmc_core_pri_verify(lpm_pri, mode_order))
1243 /* Get list of modes in priority order */
1244 for (mode = 0; mode < LPM_MAX_NUM_MODES; mode++)
1245 pri_order[mode_order[mode]] = mode;
1246 else
1247 dev_dbg(&pmcdev->pdev->dev,
1248 "Assuming a default substate order for this platform\n");
1249
1250 /*
1251 * Loop through all modes from lowest to highest priority,
1252 * and capture all enabled modes in order
1253 */
1254 i = 0;
1255 for (p = LPM_MAX_NUM_MODES - 1; p >= 0; p--) {
1256 int mode = pri_order[p];
1257
1258 if (!(BIT(mode) & lpm_en))
1259 continue;
1260
1261 pmcdev->lpm_en_modes[i++] = mode;
1262 }
1263 }
1264
get_primary_reg_base(struct pmc * pmc)1265 int get_primary_reg_base(struct pmc *pmc)
1266 {
1267 u64 slp_s0_addr;
1268
1269 if (lpit_read_residency_count_address(&slp_s0_addr)) {
1270 pmc->base_addr = PMC_BASE_ADDR_DEFAULT;
1271
1272 if (page_is_ram(PHYS_PFN(pmc->base_addr)))
1273 return -ENODEV;
1274 } else {
1275 pmc->base_addr = slp_s0_addr - pmc->map->slp_s0_offset;
1276 }
1277
1278 pmc->regbase = ioremap(pmc->base_addr, pmc->map->regmap_length);
1279 if (!pmc->regbase)
1280 return -ENOMEM;
1281 return 0;
1282 }
1283
pmc_core_punit_pmt_init(struct pmc_dev * pmcdev,u32 guid)1284 void pmc_core_punit_pmt_init(struct pmc_dev *pmcdev, u32 guid)
1285 {
1286 struct telem_endpoint *ep;
1287 struct pci_dev *pcidev;
1288
1289 pcidev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(10, 0));
1290 if (!pcidev) {
1291 dev_err(&pmcdev->pdev->dev, "PUNIT PMT device not found.");
1292 return;
1293 }
1294
1295 ep = pmt_telem_find_and_register_endpoint(pcidev, guid, 0);
1296 pci_dev_put(pcidev);
1297 if (IS_ERR(ep)) {
1298 dev_err(&pmcdev->pdev->dev,
1299 "pmc_core: couldn't get DMU telem endpoint %ld",
1300 PTR_ERR(ep));
1301 return;
1302 }
1303
1304 pmcdev->punit_ep = ep;
1305
1306 pmcdev->has_die_c6 = true;
1307 pmcdev->die_c6_offset = MTL_PMT_DMU_DIE_C6_OFFSET;
1308 }
1309
pmc_core_set_device_d3(unsigned int device)1310 void pmc_core_set_device_d3(unsigned int device)
1311 {
1312 struct pci_dev *pcidev;
1313
1314 pcidev = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1315 if (pcidev) {
1316 if (!device_trylock(&pcidev->dev)) {
1317 pci_dev_put(pcidev);
1318 return;
1319 }
1320 if (!pcidev->dev.driver) {
1321 dev_info(&pcidev->dev, "Setting to D3hot\n");
1322 pci_set_power_state(pcidev, PCI_D3hot);
1323 }
1324 device_unlock(&pcidev->dev);
1325 pci_dev_put(pcidev);
1326 }
1327 }
1328
pmc_core_is_pson_residency_enabled(struct pmc_dev * pmcdev)1329 static bool pmc_core_is_pson_residency_enabled(struct pmc_dev *pmcdev)
1330 {
1331 struct platform_device *pdev = pmcdev->pdev;
1332 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
1333 u8 val;
1334
1335 if (!adev)
1336 return false;
1337
1338 if (fwnode_property_read_u8(acpi_fwnode_handle(adev),
1339 "intel-cec-pson-switching-enabled-in-s0",
1340 &val))
1341 return false;
1342
1343 return val == 1;
1344 }
1345
pmc_core_dbgfs_unregister(struct pmc_dev * pmcdev)1346 static void pmc_core_dbgfs_unregister(struct pmc_dev *pmcdev)
1347 {
1348 debugfs_remove_recursive(pmcdev->dbgfs_dir);
1349 }
1350
pmc_core_dbgfs_register(struct pmc_dev * pmcdev,struct pmc_dev_info * pmc_dev_info)1351 static void pmc_core_dbgfs_register(struct pmc_dev *pmcdev, struct pmc_dev_info *pmc_dev_info)
1352 {
1353 struct pmc *primary_pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1354 struct dentry *dir;
1355
1356 dir = debugfs_create_dir("pmc_core", NULL);
1357 pmcdev->dbgfs_dir = dir;
1358
1359 debugfs_create_file("slp_s0_residency_usec", 0444, dir, primary_pmc,
1360 &pmc_core_dev_state);
1361
1362 if (primary_pmc->map->pfear_sts)
1363 debugfs_create_file("pch_ip_power_gating_status", 0444, dir,
1364 pmcdev, &pmc_core_ppfear_fops);
1365
1366 debugfs_create_file("ltr_ignore", 0644, dir, pmcdev,
1367 &pmc_core_ltr_ignore_fops);
1368
1369 debugfs_create_file("ltr_restore", 0200, dir, pmcdev, &pmc_core_ltr_restore_fops);
1370
1371 debugfs_create_file("ltr_show", 0444, dir, pmcdev, &pmc_core_ltr_fops);
1372
1373 if (primary_pmc->map->s0ix_blocker_maps)
1374 debugfs_create_file("s0ix_blocker", 0444, dir, pmcdev, &pmc_core_s0ix_blocker_fops);
1375
1376 debugfs_create_file("package_cstate_show", 0444, dir, primary_pmc,
1377 &pmc_core_pkgc_fops);
1378
1379 if (primary_pmc->map->pll_sts)
1380 debugfs_create_file("pll_status", 0444, dir, pmcdev,
1381 &pmc_core_pll_fops);
1382
1383 if (primary_pmc->map->mphy_sts)
1384 debugfs_create_file("mphy_core_lanes_power_gating_status",
1385 0444, dir, pmcdev,
1386 &pmc_core_mphy_pg_fops);
1387
1388 if (primary_pmc->map->slps0_dbg_maps) {
1389 debugfs_create_file("slp_s0_debug_status", 0444,
1390 dir, pmcdev,
1391 &pmc_core_slps0_dbg_fops);
1392
1393 debugfs_create_bool("slp_s0_dbg_latch", 0644,
1394 dir, &slps0_dbg_latch);
1395 }
1396
1397 if (primary_pmc->map->lpm_en_offset) {
1398 debugfs_create_file("substate_residencies", 0444,
1399 pmcdev->dbgfs_dir, pmcdev,
1400 &pmc_core_substate_res_fops);
1401 }
1402
1403 if (primary_pmc->map->lpm_status_offset) {
1404 debugfs_create_file("substate_status_registers", 0444,
1405 pmcdev->dbgfs_dir, pmcdev,
1406 &pmc_core_substate_sts_regs_fops);
1407 debugfs_create_file("substate_live_status_registers", 0444,
1408 pmcdev->dbgfs_dir, pmcdev,
1409 &pmc_core_substate_l_sts_regs_fops);
1410 debugfs_create_file("lpm_latch_mode", 0644,
1411 pmcdev->dbgfs_dir, pmcdev,
1412 &pmc_core_lpm_latch_mode_fops);
1413 }
1414
1415 if (primary_pmc->lpm_req_regs) {
1416 debugfs_create_file("substate_requirements", 0444,
1417 pmcdev->dbgfs_dir, pmcdev,
1418 pmc_dev_info->sub_req_show);
1419 }
1420
1421 if (primary_pmc->map->pson_residency_offset && pmc_core_is_pson_residency_enabled(pmcdev)) {
1422 debugfs_create_file("pson_residency_usec", 0444,
1423 pmcdev->dbgfs_dir, primary_pmc, &pmc_core_pson_residency);
1424 }
1425
1426 if (pmcdev->has_die_c6) {
1427 debugfs_create_file("die_c6_us_show", 0444,
1428 pmcdev->dbgfs_dir, pmcdev,
1429 &pmc_core_die_c6_us_fops);
1430 }
1431 }
1432
pmc_core_find_guid(struct pmc_info * list,const struct pmc_reg_map * map)1433 static u32 pmc_core_find_guid(struct pmc_info *list, const struct pmc_reg_map *map)
1434 {
1435 for (; list->map; ++list)
1436 if (list->map == map)
1437 return list->guid;
1438
1439 return 0;
1440 }
1441
1442 /*
1443 * This function retrieves low power mode requirement data from PMC Low
1444 * Power Mode (LPM) table.
1445 *
1446 * In telemetry space, the LPM table contains a 4 byte header followed
1447 * by 8 consecutive mode blocks (one for each LPM mode). Each block
1448 * has a 4 byte header followed by a set of registers that describe the
1449 * IP state requirements for the given mode. The IP mapping is platform
1450 * specific but the same for each block, making for easy analysis.
1451 * Platforms only use a subset of the space to track the requirements
1452 * for their IPs. Callers provide the requirement registers they use as
1453 * a list of indices. Each requirement register is associated with an
1454 * IP map that's maintained by the caller.
1455 *
1456 * Header
1457 * +----+----------------------------+----------------------------+
1458 * | 0 | REVISION | ENABLED MODES |
1459 * +----+--------------+-------------+-------------+--------------+
1460 *
1461 * Low Power Mode 0 Block
1462 * +----+--------------+-------------+-------------+--------------+
1463 * | 1 | SUB ID | SIZE | MAJOR | MINOR |
1464 * +----+--------------+-------------+-------------+--------------+
1465 * | 2 | LPM0 Requirements 0 |
1466 * +----+---------------------------------------------------------+
1467 * | | ... |
1468 * +----+---------------------------------------------------------+
1469 * | 29 | LPM0 Requirements 27 |
1470 * +----+---------------------------------------------------------+
1471 *
1472 * ...
1473 *
1474 * Low Power Mode 7 Block
1475 * +----+--------------+-------------+-------------+--------------+
1476 * | | SUB ID | SIZE | MAJOR | MINOR |
1477 * +----+--------------+-------------+-------------+--------------+
1478 * | 60 | LPM7 Requirements 0 |
1479 * +----+---------------------------------------------------------+
1480 * | | ... |
1481 * +----+---------------------------------------------------------+
1482 * | 87 | LPM7 Requirements 27 |
1483 * +----+---------------------------------------------------------+
1484 *
1485 */
pmc_core_pmt_get_lpm_req(struct pmc_dev * pmcdev,struct pmc * pmc,struct telem_endpoint * ep)1486 int pmc_core_pmt_get_lpm_req(struct pmc_dev *pmcdev, struct pmc *pmc, struct telem_endpoint *ep)
1487 {
1488 const u8 *lpm_indices;
1489 int num_maps, mode_offset = 0;
1490 int ret, mode;
1491 int lpm_size;
1492
1493 lpm_indices = pmc->map->lpm_reg_index;
1494 num_maps = pmc->map->lpm_num_maps;
1495 lpm_size = LPM_MAX_NUM_MODES * num_maps;
1496
1497 pmc->lpm_req_regs = devm_kzalloc(&pmcdev->pdev->dev,
1498 lpm_size * sizeof(u32),
1499 GFP_KERNEL);
1500 if (!pmc->lpm_req_regs)
1501 return -ENOMEM;
1502
1503 mode_offset = LPM_HEADER_OFFSET + LPM_MODE_OFFSET;
1504 pmc_for_each_mode(mode, pmcdev) {
1505 u32 *req_offset = pmc->lpm_req_regs + (mode * num_maps);
1506 int m;
1507
1508 for (m = 0; m < num_maps; m++) {
1509 u8 sample_id = lpm_indices[m] + mode_offset;
1510
1511 ret = pmt_telem_read32(ep, sample_id, req_offset, 1);
1512 if (ret) {
1513 dev_err(&pmcdev->pdev->dev,
1514 "couldn't read Low Power Mode requirements: %d\n", ret);
1515 return ret;
1516 }
1517 ++req_offset;
1518 }
1519 mode_offset += LPM_REG_COUNT + LPM_MODE_OFFSET;
1520 }
1521 return ret;
1522 }
1523
pmc_core_pmt_get_blk_sub_req(struct pmc_dev * pmcdev,struct pmc * pmc,struct telem_endpoint * ep)1524 int pmc_core_pmt_get_blk_sub_req(struct pmc_dev *pmcdev, struct pmc *pmc,
1525 struct telem_endpoint *ep)
1526 {
1527 u32 num_blocker, sample_offset;
1528 unsigned int index;
1529 u32 *req_offset;
1530 int ret;
1531
1532 num_blocker = pmc->map->num_s0ix_blocker;
1533 sample_offset = pmc->map->blocker_req_offset;
1534
1535 pmc->lpm_req_regs = devm_kcalloc(&pmcdev->pdev->dev, num_blocker,
1536 sizeof(u32), GFP_KERNEL);
1537 if (!pmc->lpm_req_regs)
1538 return -ENOMEM;
1539
1540 req_offset = pmc->lpm_req_regs;
1541 for (index = 0; index < num_blocker; index++, req_offset++) {
1542 ret = pmt_telem_read32(ep, index + sample_offset, req_offset, 1);
1543 if (ret) {
1544 dev_err(&pmcdev->pdev->dev,
1545 "couldn't read Low Power Mode requirements: %d\n", ret);
1546 return ret;
1547 }
1548 }
1549 return 0;
1550 }
1551
pmc_core_get_telem_info(struct pmc_dev * pmcdev,struct pmc_dev_info * pmc_dev_info)1552 static int pmc_core_get_telem_info(struct pmc_dev *pmcdev, struct pmc_dev_info *pmc_dev_info)
1553 {
1554 struct pci_dev *pcidev __free(pci_dev_put) = NULL;
1555 struct telem_endpoint *ep;
1556 unsigned int i;
1557 u32 guid;
1558 int ret;
1559
1560 pcidev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(20, pmc_dev_info->pci_func));
1561 if (!pcidev)
1562 return -ENODEV;
1563
1564 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
1565 struct pmc *pmc;
1566
1567 pmc = pmcdev->pmcs[i];
1568 if (!pmc)
1569 continue;
1570
1571 guid = pmc_core_find_guid(pmcdev->regmap_list, pmc->map);
1572 if (!guid)
1573 return -ENXIO;
1574
1575 ep = pmt_telem_find_and_register_endpoint(pcidev, guid, 0);
1576 if (IS_ERR(ep)) {
1577 dev_dbg(&pmcdev->pdev->dev, "couldn't get telem endpoint %pe", ep);
1578 return -EPROBE_DEFER;
1579 }
1580
1581 ret = pmc_dev_info->sub_req(pmcdev, pmc, ep);
1582 pmt_telem_unregister_endpoint(ep);
1583 if (ret)
1584 return ret;
1585 }
1586
1587 return 0;
1588 }
1589
pmc_core_find_regmap(struct pmc_info * list,u16 devid)1590 static const struct pmc_reg_map *pmc_core_find_regmap(struct pmc_info *list, u16 devid)
1591 {
1592 for (; list->map; ++list)
1593 if (devid == list->devid)
1594 return list->map;
1595
1596 return NULL;
1597 }
1598
pmc_core_pmc_add(struct pmc_dev * pmcdev,unsigned int pmc_index)1599 static int pmc_core_pmc_add(struct pmc_dev *pmcdev, unsigned int pmc_index)
1600
1601 {
1602 struct pmc_ssram_telemetry pmc_ssram_telemetry;
1603 const struct pmc_reg_map *map;
1604 struct pmc *pmc;
1605 int ret;
1606
1607 ret = pmc_ssram_telemetry_get_pmc_info(pmc_index, &pmc_ssram_telemetry);
1608 if (ret)
1609 return ret;
1610
1611 map = pmc_core_find_regmap(pmcdev->regmap_list, pmc_ssram_telemetry.devid);
1612 if (!map)
1613 return -ENODEV;
1614
1615 pmc = pmcdev->pmcs[pmc_index];
1616 /* Memory for primary PMC has been allocated */
1617 if (!pmc) {
1618 pmc = devm_kzalloc(&pmcdev->pdev->dev, sizeof(*pmc), GFP_KERNEL);
1619 if (!pmc)
1620 return -ENOMEM;
1621 }
1622
1623 pmc->map = map;
1624 pmc->base_addr = pmc_ssram_telemetry.base_addr;
1625 pmc->regbase = ioremap(pmc->base_addr, pmc->map->regmap_length);
1626
1627 if (!pmc->regbase) {
1628 devm_kfree(&pmcdev->pdev->dev, pmc);
1629 return -ENOMEM;
1630 }
1631
1632 pmcdev->pmcs[pmc_index] = pmc;
1633
1634 return 0;
1635 }
1636
pmc_core_ssram_get_reg_base(struct pmc_dev * pmcdev)1637 static int pmc_core_ssram_get_reg_base(struct pmc_dev *pmcdev)
1638 {
1639 int ret;
1640
1641 ret = pmc_core_pmc_add(pmcdev, PMC_IDX_MAIN);
1642 if (ret)
1643 return ret;
1644
1645 pmc_core_pmc_add(pmcdev, PMC_IDX_IOE);
1646 pmc_core_pmc_add(pmcdev, PMC_IDX_PCH);
1647
1648 return 0;
1649 }
1650
1651 /*
1652 * When supported, ssram init is used to achieve all available PMCs.
1653 * If ssram init fails, this function uses legacy method to at least get the
1654 * primary PMC.
1655 */
generic_core_init(struct pmc_dev * pmcdev,struct pmc_dev_info * pmc_dev_info)1656 int generic_core_init(struct pmc_dev *pmcdev, struct pmc_dev_info *pmc_dev_info)
1657 {
1658 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1659 bool ssram;
1660 int ret;
1661
1662 pmcdev->suspend = pmc_dev_info->suspend;
1663 pmcdev->resume = pmc_dev_info->resume;
1664
1665 ssram = pmc_dev_info->regmap_list != NULL;
1666 if (ssram) {
1667 pmcdev->regmap_list = pmc_dev_info->regmap_list;
1668 ret = pmc_core_ssram_get_reg_base(pmcdev);
1669 /*
1670 * EAGAIN error code indicates Intel PMC SSRAM Telemetry driver
1671 * has not finished probe and PMC info is not available yet. Try
1672 * again later.
1673 */
1674 if (ret == -EAGAIN)
1675 return -EPROBE_DEFER;
1676
1677 if (ret) {
1678 dev_warn(&pmcdev->pdev->dev,
1679 "Failed to get PMC info from SSRAM, %d, using legacy init\n", ret);
1680 ssram = false;
1681 }
1682 }
1683
1684 if (!ssram) {
1685 pmc->map = pmc_dev_info->map;
1686 ret = get_primary_reg_base(pmc);
1687 if (ret)
1688 return ret;
1689 }
1690
1691 pmc_core_get_low_power_modes(pmcdev);
1692 if (pmc_dev_info->dmu_guid)
1693 pmc_core_punit_pmt_init(pmcdev, pmc_dev_info->dmu_guid);
1694
1695 if (ssram) {
1696 ret = pmc_core_get_telem_info(pmcdev, pmc_dev_info);
1697 if (ret)
1698 goto unmap_regbase;
1699 }
1700
1701 return 0;
1702
1703 unmap_regbase:
1704 for (unsigned int i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
1705 struct pmc *pmc = pmcdev->pmcs[i];
1706
1707 if (pmc && pmc->regbase)
1708 iounmap(pmc->regbase);
1709 }
1710
1711 if (pmcdev->punit_ep)
1712 pmt_telem_unregister_endpoint(pmcdev->punit_ep);
1713
1714 return ret;
1715 }
1716
1717 static const struct x86_cpu_id intel_pmc_core_ids[] = {
1718 X86_MATCH_VFM(INTEL_SKYLAKE_L, &spt_pmc_dev),
1719 X86_MATCH_VFM(INTEL_SKYLAKE, &spt_pmc_dev),
1720 X86_MATCH_VFM(INTEL_KABYLAKE_L, &spt_pmc_dev),
1721 X86_MATCH_VFM(INTEL_KABYLAKE, &spt_pmc_dev),
1722 X86_MATCH_VFM(INTEL_CANNONLAKE_L, &cnp_pmc_dev),
1723 X86_MATCH_VFM(INTEL_ICELAKE_L, &icl_pmc_dev),
1724 X86_MATCH_VFM(INTEL_ICELAKE_NNPI, &icl_pmc_dev),
1725 X86_MATCH_VFM(INTEL_COMETLAKE, &cnp_pmc_dev),
1726 X86_MATCH_VFM(INTEL_COMETLAKE_L, &cnp_pmc_dev),
1727 X86_MATCH_VFM(INTEL_TIGERLAKE_L, &tgl_l_pmc_dev),
1728 X86_MATCH_VFM(INTEL_TIGERLAKE, &tgl_pmc_dev),
1729 X86_MATCH_VFM(INTEL_ATOM_TREMONT, &tgl_l_pmc_dev),
1730 X86_MATCH_VFM(INTEL_ATOM_TREMONT_L, &icl_pmc_dev),
1731 X86_MATCH_VFM(INTEL_ROCKETLAKE, &tgl_pmc_dev),
1732 X86_MATCH_VFM(INTEL_ALDERLAKE_L, &tgl_l_pmc_dev),
1733 X86_MATCH_VFM(INTEL_ATOM_GRACEMONT, &tgl_l_pmc_dev),
1734 X86_MATCH_VFM(INTEL_ALDERLAKE, &adl_pmc_dev),
1735 X86_MATCH_VFM(INTEL_RAPTORLAKE_P, &tgl_l_pmc_dev),
1736 X86_MATCH_VFM(INTEL_RAPTORLAKE, &adl_pmc_dev),
1737 X86_MATCH_VFM(INTEL_RAPTORLAKE_S, &adl_pmc_dev),
1738 X86_MATCH_VFM(INTEL_BARTLETTLAKE, &adl_pmc_dev),
1739 X86_MATCH_VFM(INTEL_METEORLAKE_L, &mtl_pmc_dev),
1740 X86_MATCH_VFM(INTEL_ARROWLAKE, &arl_pmc_dev),
1741 X86_MATCH_VFM(INTEL_ARROWLAKE_H, &arl_h_pmc_dev),
1742 X86_MATCH_VFM(INTEL_ARROWLAKE_U, &arl_h_pmc_dev),
1743 X86_MATCH_VFM(INTEL_LUNARLAKE_M, &lnl_pmc_dev),
1744 X86_MATCH_VFM(INTEL_PANTHERLAKE_L, &ptl_pmc_dev),
1745 X86_MATCH_VFM(INTEL_WILDCATLAKE_L, &wcl_pmc_dev),
1746 {}
1747 };
1748
1749 MODULE_DEVICE_TABLE(x86cpu, intel_pmc_core_ids);
1750
1751 /*
1752 * This quirk can be used on those platforms where
1753 * the platform BIOS enforces 24Mhz crystal to shutdown
1754 * before PMC can assert SLP_S0#.
1755 */
1756 static bool xtal_ignore;
quirk_xtal_ignore(const struct dmi_system_id * id)1757 static int quirk_xtal_ignore(const struct dmi_system_id *id)
1758 {
1759 xtal_ignore = true;
1760 return 0;
1761 }
1762
pmc_core_xtal_ignore(struct pmc * pmc)1763 static void pmc_core_xtal_ignore(struct pmc *pmc)
1764 {
1765 u32 value;
1766
1767 value = pmc_core_reg_read(pmc, pmc->map->pm_vric1_offset);
1768 /* 24MHz Crystal Shutdown Qualification Disable */
1769 value |= SPT_PMC_VRIC1_XTALSDQDIS;
1770 /* Low Voltage Mode Enable */
1771 value &= ~SPT_PMC_VRIC1_SLPS0LVEN;
1772 pmc_core_reg_write(pmc, pmc->map->pm_vric1_offset, value);
1773 }
1774
1775 static const struct dmi_system_id pmc_core_dmi_table[] = {
1776 {
1777 .callback = quirk_xtal_ignore,
1778 .ident = "HP Elite x2 1013 G3",
1779 .matches = {
1780 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
1781 DMI_MATCH(DMI_PRODUCT_NAME, "HP Elite x2 1013 G3"),
1782 },
1783 },
1784 {}
1785 };
1786
pmc_core_do_dmi_quirks(struct pmc * pmc)1787 static void pmc_core_do_dmi_quirks(struct pmc *pmc)
1788 {
1789 dmi_check_system(pmc_core_dmi_table);
1790
1791 if (xtal_ignore)
1792 pmc_core_xtal_ignore(pmc);
1793 }
1794
pmc_core_clean_structure(struct platform_device * pdev)1795 static void pmc_core_clean_structure(struct platform_device *pdev)
1796 {
1797 struct pmc_dev *pmcdev = platform_get_drvdata(pdev);
1798 unsigned int i;
1799
1800 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
1801 struct pmc *pmc = pmcdev->pmcs[i];
1802
1803 if (pmc && pmc->regbase)
1804 iounmap(pmc->regbase);
1805 }
1806
1807 if (pmcdev->punit_ep)
1808 pmt_telem_unregister_endpoint(pmcdev->punit_ep);
1809
1810 platform_set_drvdata(pdev, NULL);
1811 }
1812
pmc_core_probe(struct platform_device * pdev)1813 static int pmc_core_probe(struct platform_device *pdev)
1814 {
1815 static bool device_initialized;
1816 struct pmc_dev *pmcdev;
1817 const struct x86_cpu_id *cpu_id;
1818 struct pmc_dev_info *pmc_dev_info;
1819 struct pmc *primary_pmc;
1820 int ret;
1821
1822 if (device_initialized)
1823 return -ENODEV;
1824
1825 pmcdev = devm_kzalloc(&pdev->dev, sizeof(*pmcdev), GFP_KERNEL);
1826 if (!pmcdev)
1827 return -ENOMEM;
1828
1829 pmcdev->crystal_freq = pmc_core_get_crystal_freq();
1830
1831 platform_set_drvdata(pdev, pmcdev);
1832 pmcdev->pdev = pdev;
1833
1834 cpu_id = x86_match_cpu(intel_pmc_core_ids);
1835 if (!cpu_id)
1836 return -ENODEV;
1837
1838 pmc_dev_info = (struct pmc_dev_info *)cpu_id->driver_data;
1839
1840 /* Primary PMC */
1841 primary_pmc = devm_kzalloc(&pdev->dev, sizeof(*primary_pmc), GFP_KERNEL);
1842 if (!primary_pmc)
1843 return -ENOMEM;
1844 pmcdev->pmcs[PMC_IDX_MAIN] = primary_pmc;
1845
1846 /* The last element in msr_map is empty */
1847 pmcdev->num_of_pkgc = ARRAY_SIZE(msr_map) - 1;
1848 pmcdev->pkgc_res_cnt = devm_kcalloc(&pdev->dev,
1849 pmcdev->num_of_pkgc,
1850 sizeof(*pmcdev->pkgc_res_cnt),
1851 GFP_KERNEL);
1852 if (!pmcdev->pkgc_res_cnt)
1853 return -ENOMEM;
1854
1855 ret = devm_mutex_init(&pdev->dev, &pmcdev->lock);
1856 if (ret)
1857 return ret;
1858
1859 if (pmc_dev_info->init)
1860 ret = pmc_dev_info->init(pmcdev, pmc_dev_info);
1861 else
1862 ret = generic_core_init(pmcdev, pmc_dev_info);
1863
1864 if (ret) {
1865 platform_set_drvdata(pdev, NULL);
1866 return ret;
1867 }
1868
1869 pmcdev->pmc_xram_read_bit = pmc_core_check_read_lock_bit(primary_pmc);
1870 pmc_core_do_dmi_quirks(primary_pmc);
1871
1872 pmc_core_dbgfs_register(pmcdev, pmc_dev_info);
1873 pm_report_max_hw_sleep(FIELD_MAX(SLP_S0_RES_COUNTER_MASK) *
1874 pmc_core_adjust_slp_s0_step(primary_pmc, 1));
1875
1876 device_initialized = true;
1877 dev_info(&pdev->dev, " initialized\n");
1878
1879 return 0;
1880 }
1881
pmc_core_remove(struct platform_device * pdev)1882 static void pmc_core_remove(struct platform_device *pdev)
1883 {
1884 struct pmc_dev *pmcdev = platform_get_drvdata(pdev);
1885 pmc_core_dbgfs_unregister(pmcdev);
1886 pmc_core_clean_structure(pdev);
1887 }
1888
1889 static bool warn_on_s0ix_failures;
1890 module_param(warn_on_s0ix_failures, bool, 0644);
1891 MODULE_PARM_DESC(warn_on_s0ix_failures, "Check and warn for S0ix failures");
1892
1893 static bool ltr_ignore_all_suspend = true;
1894 module_param(ltr_ignore_all_suspend, bool, 0644);
1895 MODULE_PARM_DESC(ltr_ignore_all_suspend, "Ignore all LTRs during suspend");
1896
pmc_core_suspend(struct device * dev)1897 static __maybe_unused int pmc_core_suspend(struct device *dev)
1898 {
1899 struct pmc_dev *pmcdev = dev_get_drvdata(dev);
1900 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1901 unsigned int i;
1902
1903 if (pmcdev->suspend)
1904 pmcdev->suspend(pmcdev);
1905
1906 if (ltr_ignore_all_suspend)
1907 pmc_core_ltr_ignore_all(pmcdev);
1908
1909 /* Check if the syspend will actually use S0ix */
1910 if (pm_suspend_via_firmware())
1911 return 0;
1912
1913 /* Save PKGC residency for checking later */
1914 for (i = 0; i < pmcdev->num_of_pkgc; i++) {
1915 if (rdmsrq_safe(msr_map[i].bit_mask, &pmcdev->pkgc_res_cnt[i]))
1916 return -EIO;
1917 }
1918
1919 /* Save S0ix residency for checking later */
1920 if (pmc_core_dev_state_get(pmc, &pmcdev->s0ix_counter))
1921 return -EIO;
1922
1923 return 0;
1924 }
1925
pmc_core_is_deepest_pkgc_failed(struct pmc_dev * pmcdev)1926 static inline bool pmc_core_is_deepest_pkgc_failed(struct pmc_dev *pmcdev)
1927 {
1928 u32 deepest_pkgc_msr = msr_map[pmcdev->num_of_pkgc - 1].bit_mask;
1929 u64 deepest_pkgc_residency;
1930
1931 if (rdmsrq_safe(deepest_pkgc_msr, &deepest_pkgc_residency))
1932 return false;
1933
1934 if (deepest_pkgc_residency == pmcdev->pkgc_res_cnt[pmcdev->num_of_pkgc - 1])
1935 return true;
1936
1937 return false;
1938 }
1939
pmc_core_is_s0ix_failed(struct pmc_dev * pmcdev)1940 static inline bool pmc_core_is_s0ix_failed(struct pmc_dev *pmcdev)
1941 {
1942 u64 s0ix_counter;
1943
1944 if (pmc_core_dev_state_get(pmcdev->pmcs[PMC_IDX_MAIN], &s0ix_counter))
1945 return false;
1946
1947 pm_report_hw_sleep_time((u32)(s0ix_counter - pmcdev->s0ix_counter));
1948
1949 if (s0ix_counter == pmcdev->s0ix_counter)
1950 return true;
1951
1952 return false;
1953 }
1954
pmc_core_resume_common(struct pmc_dev * pmcdev)1955 int pmc_core_resume_common(struct pmc_dev *pmcdev)
1956 {
1957 struct device *dev = &pmcdev->pdev->dev;
1958 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
1959 const struct pmc_bit_map **maps = pmc->map->lpm_sts;
1960 int offset = pmc->map->lpm_status_offset;
1961 unsigned int i;
1962
1963 /* Check if the syspend used S0ix */
1964 if (pm_suspend_via_firmware())
1965 return 0;
1966
1967 if (!pmc_core_is_s0ix_failed(pmcdev))
1968 return 0;
1969
1970 if (!warn_on_s0ix_failures)
1971 return 0;
1972
1973 if (pmc_core_is_deepest_pkgc_failed(pmcdev)) {
1974 /* S0ix failed because of deepest PKGC entry failure */
1975 dev_info(dev, "CPU did not enter %s!!! (%s cnt=0x%llx)\n",
1976 msr_map[pmcdev->num_of_pkgc - 1].name,
1977 msr_map[pmcdev->num_of_pkgc - 1].name,
1978 pmcdev->pkgc_res_cnt[pmcdev->num_of_pkgc - 1]);
1979
1980 for (i = 0; i < pmcdev->num_of_pkgc; i++) {
1981 u64 pc_cnt;
1982
1983 if (!rdmsrq_safe(msr_map[i].bit_mask, &pc_cnt)) {
1984 dev_info(dev, "Prev %s cnt = 0x%llx, Current %s cnt = 0x%llx\n",
1985 msr_map[i].name, pmcdev->pkgc_res_cnt[i],
1986 msr_map[i].name, pc_cnt);
1987 }
1988 }
1989 return 0;
1990 }
1991
1992 /* The real interesting case - S0ix failed - lets ask PMC why. */
1993 dev_warn(dev, "CPU did not enter SLP_S0!!! (S0ix cnt=%llu)\n",
1994 pmcdev->s0ix_counter);
1995
1996 if (pmc->map->slps0_dbg_maps)
1997 pmc_core_slps0_display(pmc, dev, NULL);
1998
1999 for (i = 0; i < ARRAY_SIZE(pmcdev->pmcs); ++i) {
2000 struct pmc *pmc = pmcdev->pmcs[i];
2001
2002 if (!pmc)
2003 continue;
2004 if (pmc->map->lpm_sts)
2005 pmc_core_lpm_display(pmc, dev, NULL, offset, i, "STATUS", maps);
2006 }
2007
2008 return 0;
2009 }
2010
pmc_core_resume(struct device * dev)2011 static __maybe_unused int pmc_core_resume(struct device *dev)
2012 {
2013 struct pmc_dev *pmcdev = dev_get_drvdata(dev);
2014
2015 if (ltr_ignore_all_suspend)
2016 pmc_core_ltr_restore_all(pmcdev);
2017
2018 if (pmcdev->resume)
2019 return pmcdev->resume(pmcdev);
2020
2021 return pmc_core_resume_common(pmcdev);
2022 }
2023
2024 static const struct dev_pm_ops pmc_core_pm_ops = {
2025 SET_LATE_SYSTEM_SLEEP_PM_OPS(pmc_core_suspend, pmc_core_resume)
2026 };
2027
2028 static const struct acpi_device_id pmc_core_acpi_ids[] = {
2029 {"INT33A1", 0}, /* _HID for Intel Power Engine, _CID PNP0D80*/
2030 { }
2031 };
2032 MODULE_DEVICE_TABLE(acpi, pmc_core_acpi_ids);
2033
2034 static struct platform_driver pmc_core_driver = {
2035 .driver = {
2036 .name = "intel_pmc_core",
2037 .acpi_match_table = ACPI_PTR(pmc_core_acpi_ids),
2038 .pm = &pmc_core_pm_ops,
2039 .dev_groups = pmc_dev_groups,
2040 },
2041 .probe = pmc_core_probe,
2042 .remove = pmc_core_remove,
2043 };
2044
2045 module_platform_driver(pmc_core_driver);
2046
2047 MODULE_IMPORT_NS("INTEL_PMT_TELEMETRY");
2048 MODULE_LICENSE("GPL v2");
2049 MODULE_DESCRIPTION("Intel PMC Core Driver");
2050