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