Merge tag 'v4.20' into for-linus
[linux-2.6-microblaze.git] / drivers / platform / x86 / intel_telemetry_debugfs.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel SOC Telemetry debugfs Driver: Currently supports APL
4  * Copyright (c) 2015, Intel Corporation.
5  * All Rights Reserved.
6  *
7  * This file provides the debugfs interfaces for telemetry.
8  * /sys/kernel/debug/telemetry/pss_info: Shows Primary Control Sub-Sys Counters
9  * /sys/kernel/debug/telemetry/ioss_info: Shows IO Sub-System Counters
10  * /sys/kernel/debug/telemetry/soc_states: Shows SoC State
11  * /sys/kernel/debug/telemetry/pss_trace_verbosity: Read and Change Tracing
12  *                              Verbosity via firmware
13  * /sys/kernel/debug/telemetry/ioss_race_verbosity: Write and Change Tracing
14  *                              Verbosity via firmware
15  */
16 #include <linux/debugfs.h>
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 #include <linux/seq_file.h>
21 #include <linux/suspend.h>
22
23 #include <asm/cpu_device_id.h>
24 #include <asm/intel-family.h>
25 #include <asm/intel_pmc_ipc.h>
26 #include <asm/intel_telemetry.h>
27
28 #define DRIVER_NAME                     "telemetry_soc_debugfs"
29 #define DRIVER_VERSION                  "1.0.0"
30
31 /* ApolloLake SoC Event-IDs */
32 #define TELEM_APL_PSS_PSTATES_ID        0x2802
33 #define TELEM_APL_PSS_IDLE_ID           0x2806
34 #define TELEM_APL_PCS_IDLE_BLOCKED_ID   0x2C00
35 #define TELEM_APL_PCS_S0IX_BLOCKED_ID   0x2C01
36 #define TELEM_APL_PSS_WAKEUP_ID         0x2C02
37 #define TELEM_APL_PSS_LTR_BLOCKING_ID   0x2C03
38
39 #define TELEM_APL_S0IX_TOTAL_OCC_ID     0x4000
40 #define TELEM_APL_S0IX_SHLW_OCC_ID      0x4001
41 #define TELEM_APL_S0IX_DEEP_OCC_ID      0x4002
42 #define TELEM_APL_S0IX_TOTAL_RES_ID     0x4800
43 #define TELEM_APL_S0IX_SHLW_RES_ID      0x4801
44 #define TELEM_APL_S0IX_DEEP_RES_ID      0x4802
45 #define TELEM_APL_D0IX_ID               0x581A
46 #define TELEM_APL_D3_ID                 0x5819
47 #define TELEM_APL_PG_ID                 0x5818
48
49 #define TELEM_INFO_SRAMEVTS_MASK        0xFF00
50 #define TELEM_INFO_SRAMEVTS_SHIFT       0x8
51 #define TELEM_SSRAM_READ_TIMEOUT        10
52
53 #define TELEM_MASK_BIT                  1
54 #define TELEM_MASK_BYTE                 0xFF
55 #define BYTES_PER_LONG                  8
56 #define TELEM_APL_MASK_PCS_STATE        0xF
57
58 /* Max events in bitmap to check for */
59 #define TELEM_PSS_IDLE_EVTS             25
60 #define TELEM_PSS_IDLE_BLOCKED_EVTS     20
61 #define TELEM_PSS_S0IX_BLOCKED_EVTS     20
62 #define TELEM_PSS_S0IX_WAKEUP_EVTS      20
63 #define TELEM_PSS_LTR_BLOCKING_EVTS     20
64 #define TELEM_IOSS_DX_D0IX_EVTS         25
65 #define TELEM_IOSS_PG_EVTS              30
66
67 #define TELEM_CHECK_AND_PARSE_EVTS(EVTID, EVTNUM, BUF, EVTLOG, EVTDAT, MASK) { \
68         if (evtlog[index].telem_evtid == (EVTID)) { \
69                 for (idx = 0; idx < (EVTNUM); idx++) \
70                         (BUF)[idx] = ((EVTLOG) >> (EVTDAT)[idx].bit_pos) & \
71                                      (MASK); \
72         continue; \
73         } \
74 }
75
76 #define TELEM_CHECK_AND_PARSE_CTRS(EVTID, CTR) { \
77         if (evtlog[index].telem_evtid == (EVTID)) { \
78                 (CTR) = evtlog[index].telem_evtlog; \
79                 continue; \
80         } \
81 }
82
83 static u8 suspend_prep_ok;
84 static u32 suspend_shlw_ctr_temp, suspend_deep_ctr_temp;
85 static u64 suspend_shlw_res_temp, suspend_deep_res_temp;
86
87 struct telemetry_susp_stats {
88         u32 shlw_ctr;
89         u32 deep_ctr;
90         u64 shlw_res;
91         u64 deep_res;
92 };
93
94 /* Bitmap definitions for default counters in APL */
95 struct telem_pss_idle_stateinfo {
96         const char *name;
97         u32 bit_pos;
98 };
99
100 static struct telem_pss_idle_stateinfo telem_apl_pss_idle_data[] = {
101         {"IA_CORE0_C1E",                0},
102         {"IA_CORE1_C1E",                1},
103         {"IA_CORE2_C1E",                2},
104         {"IA_CORE3_C1E",                3},
105         {"IA_CORE0_C6",                 16},
106         {"IA_CORE1_C6",                 17},
107         {"IA_CORE2_C6",                 18},
108         {"IA_CORE3_C6",                 19},
109         {"IA_MODULE0_C7",               32},
110         {"IA_MODULE1_C7",               33},
111         {"GT_RC6",                      40},
112         {"IUNIT_PROCESSING_IDLE",       41},
113         {"FAR_MEM_IDLE",                43},
114         {"DISPLAY_IDLE",                44},
115         {"IUNIT_INPUT_SYSTEM_IDLE",     45},
116         {"PCS_STATUS",                  60},
117 };
118
119 struct telem_pcs_blkd_info {
120         const char *name;
121         u32 bit_pos;
122 };
123
124 static struct telem_pcs_blkd_info telem_apl_pcs_idle_blkd_data[] = {
125         {"COMPUTE",                     0},
126         {"MISC",                        8},
127         {"MODULE_ACTIONS_PENDING",      16},
128         {"LTR",                         24},
129         {"DISPLAY_WAKE",                32},
130         {"ISP_WAKE",                    40},
131         {"PSF0_ACTIVE",                 48},
132 };
133
134 static struct telem_pcs_blkd_info telem_apl_pcs_s0ix_blkd_data[] = {
135         {"LTR",                         0},
136         {"IRTL",                        8},
137         {"WAKE_DEADLINE_PENDING",       16},
138         {"DISPLAY",                     24},
139         {"ISP",                         32},
140         {"CORE",                        40},
141         {"PMC",                         48},
142         {"MISC",                        56},
143 };
144
145 struct telem_pss_ltr_info {
146         const char *name;
147         u32 bit_pos;
148 };
149
150 static struct telem_pss_ltr_info telem_apl_pss_ltr_data[] = {
151         {"CORE_ACTIVE",         0},
152         {"MEM_UP",              8},
153         {"DFX",                 16},
154         {"DFX_FORCE_LTR",       24},
155         {"DISPLAY",             32},
156         {"ISP",                 40},
157         {"SOUTH",               48},
158 };
159
160 struct telem_pss_wakeup_info {
161         const char *name;
162         u32 bit_pos;
163 };
164
165 static struct telem_pss_wakeup_info telem_apl_pss_wakeup[] = {
166         {"IP_IDLE",                     0},
167         {"DISPLAY_WAKE",                8},
168         {"VOLTAGE_REG_INT",             16},
169         {"DROWSY_TIMER (HOTPLUG)",      24},
170         {"CORE_WAKE",                   32},
171         {"MISC_S0IX",                   40},
172         {"MISC_ABORT",                  56},
173 };
174
175 struct telem_ioss_d0ix_stateinfo {
176         const char *name;
177         u32 bit_pos;
178 };
179
180 static struct telem_ioss_d0ix_stateinfo telem_apl_ioss_d0ix_data[] = {
181         {"CSE",         0},
182         {"SCC2",        1},
183         {"GMM",         2},
184         {"XDCI",        3},
185         {"XHCI",        4},
186         {"ISH",         5},
187         {"AVS",         6},
188         {"PCIE0P1",     7},
189         {"PECI0P0",     8},
190         {"LPSS",        9},
191         {"SCC",         10},
192         {"PWM",         11},
193         {"PCIE1_P3",    12},
194         {"PCIE1_P2",    13},
195         {"PCIE1_P1",    14},
196         {"PCIE1_P0",    15},
197         {"CNV",         16},
198         {"SATA",        17},
199         {"PRTC",        18},
200 };
201
202 struct telem_ioss_pg_info {
203         const char *name;
204         u32 bit_pos;
205 };
206
207 static struct telem_ioss_pg_info telem_apl_ioss_pg_data[] = {
208         {"LPSS",        0},
209         {"SCC",         1},
210         {"P2SB",        2},
211         {"SCC2",        3},
212         {"GMM",         4},
213         {"PCIE0",       5},
214         {"XDCI",        6},
215         {"xHCI",        7},
216         {"CSE",         8},
217         {"SPI",         9},
218         {"AVSPGD4",     10},
219         {"AVSPGD3",     11},
220         {"AVSPGD2",     12},
221         {"AVSPGD1",     13},
222         {"ISH",         14},
223         {"EXI",         15},
224         {"NPKVRC",      16},
225         {"NPKVNN",      17},
226         {"CUNIT",       18},
227         {"FUSE_CTRL",   19},
228         {"PCIE1",       20},
229         {"CNV",         21},
230         {"LPC",         22},
231         {"SATA",        23},
232         {"SMB",         24},
233         {"PRTC",        25},
234 };
235
236 struct telemetry_debugfs_conf {
237         struct telemetry_susp_stats suspend_stats;
238         struct dentry *telemetry_dbg_dir;
239
240         /* Bitmap Data */
241         struct telem_ioss_d0ix_stateinfo *ioss_d0ix_data;
242         struct telem_pss_idle_stateinfo *pss_idle_data;
243         struct telem_pcs_blkd_info *pcs_idle_blkd_data;
244         struct telem_pcs_blkd_info *pcs_s0ix_blkd_data;
245         struct telem_pss_wakeup_info *pss_wakeup;
246         struct telem_pss_ltr_info *pss_ltr_data;
247         struct telem_ioss_pg_info *ioss_pg_data;
248         u8 pcs_idle_blkd_evts;
249         u8 pcs_s0ix_blkd_evts;
250         u8 pss_wakeup_evts;
251         u8 pss_idle_evts;
252         u8 pss_ltr_evts;
253         u8 ioss_d0ix_evts;
254         u8 ioss_pg_evts;
255
256         /* IDs */
257         u16  pss_ltr_blocking_id;
258         u16  pcs_idle_blkd_id;
259         u16  pcs_s0ix_blkd_id;
260         u16  s0ix_total_occ_id;
261         u16  s0ix_shlw_occ_id;
262         u16  s0ix_deep_occ_id;
263         u16  s0ix_total_res_id;
264         u16  s0ix_shlw_res_id;
265         u16  s0ix_deep_res_id;
266         u16  pss_wakeup_id;
267         u16  ioss_d0ix_id;
268         u16  pstates_id;
269         u16  pss_idle_id;
270         u16  ioss_d3_id;
271         u16  ioss_pg_id;
272 };
273
274 static struct telemetry_debugfs_conf *debugfs_conf;
275
276 static struct telemetry_debugfs_conf telem_apl_debugfs_conf = {
277         .pss_idle_data = telem_apl_pss_idle_data,
278         .pcs_idle_blkd_data = telem_apl_pcs_idle_blkd_data,
279         .pcs_s0ix_blkd_data = telem_apl_pcs_s0ix_blkd_data,
280         .pss_ltr_data = telem_apl_pss_ltr_data,
281         .pss_wakeup = telem_apl_pss_wakeup,
282         .ioss_d0ix_data = telem_apl_ioss_d0ix_data,
283         .ioss_pg_data = telem_apl_ioss_pg_data,
284
285         .pss_idle_evts = ARRAY_SIZE(telem_apl_pss_idle_data),
286         .pcs_idle_blkd_evts = ARRAY_SIZE(telem_apl_pcs_idle_blkd_data),
287         .pcs_s0ix_blkd_evts = ARRAY_SIZE(telem_apl_pcs_s0ix_blkd_data),
288         .pss_ltr_evts = ARRAY_SIZE(telem_apl_pss_ltr_data),
289         .pss_wakeup_evts = ARRAY_SIZE(telem_apl_pss_wakeup),
290         .ioss_d0ix_evts = ARRAY_SIZE(telem_apl_ioss_d0ix_data),
291         .ioss_pg_evts = ARRAY_SIZE(telem_apl_ioss_pg_data),
292
293         .pstates_id = TELEM_APL_PSS_PSTATES_ID,
294         .pss_idle_id = TELEM_APL_PSS_IDLE_ID,
295         .pcs_idle_blkd_id = TELEM_APL_PCS_IDLE_BLOCKED_ID,
296         .pcs_s0ix_blkd_id = TELEM_APL_PCS_S0IX_BLOCKED_ID,
297         .pss_wakeup_id = TELEM_APL_PSS_WAKEUP_ID,
298         .pss_ltr_blocking_id = TELEM_APL_PSS_LTR_BLOCKING_ID,
299         .s0ix_total_occ_id = TELEM_APL_S0IX_TOTAL_OCC_ID,
300         .s0ix_shlw_occ_id = TELEM_APL_S0IX_SHLW_OCC_ID,
301         .s0ix_deep_occ_id = TELEM_APL_S0IX_DEEP_OCC_ID,
302         .s0ix_total_res_id = TELEM_APL_S0IX_TOTAL_RES_ID,
303         .s0ix_shlw_res_id = TELEM_APL_S0IX_SHLW_RES_ID,
304         .s0ix_deep_res_id = TELEM_APL_S0IX_DEEP_RES_ID,
305         .ioss_d0ix_id = TELEM_APL_D0IX_ID,
306         .ioss_d3_id = TELEM_APL_D3_ID,
307         .ioss_pg_id = TELEM_APL_PG_ID,
308 };
309
310 static const struct x86_cpu_id telemetry_debugfs_cpu_ids[] = {
311         INTEL_CPU_FAM6(ATOM_GOLDMONT, telem_apl_debugfs_conf),
312         INTEL_CPU_FAM6(ATOM_GOLDMONT_PLUS, telem_apl_debugfs_conf),
313         {}
314 };
315
316 MODULE_DEVICE_TABLE(x86cpu, telemetry_debugfs_cpu_ids);
317
318 static int telemetry_debugfs_check_evts(void)
319 {
320         if ((debugfs_conf->pss_idle_evts > TELEM_PSS_IDLE_EVTS) ||
321             (debugfs_conf->pcs_idle_blkd_evts > TELEM_PSS_IDLE_BLOCKED_EVTS) ||
322             (debugfs_conf->pcs_s0ix_blkd_evts > TELEM_PSS_S0IX_BLOCKED_EVTS) ||
323             (debugfs_conf->pss_ltr_evts > TELEM_PSS_LTR_BLOCKING_EVTS) ||
324             (debugfs_conf->pss_wakeup_evts > TELEM_PSS_S0IX_WAKEUP_EVTS) ||
325             (debugfs_conf->ioss_d0ix_evts > TELEM_IOSS_DX_D0IX_EVTS) ||
326             (debugfs_conf->ioss_pg_evts > TELEM_IOSS_PG_EVTS))
327                 return -EINVAL;
328
329         return 0;
330 }
331
332 static int telem_pss_states_show(struct seq_file *s, void *unused)
333 {
334         struct telemetry_evtlog evtlog[TELEM_MAX_OS_ALLOCATED_EVENTS];
335         struct telemetry_debugfs_conf *conf = debugfs_conf;
336         const char *name[TELEM_MAX_OS_ALLOCATED_EVENTS];
337         u32 pcs_idle_blkd[TELEM_PSS_IDLE_BLOCKED_EVTS],
338             pcs_s0ix_blkd[TELEM_PSS_S0IX_BLOCKED_EVTS],
339             pss_s0ix_wakeup[TELEM_PSS_S0IX_WAKEUP_EVTS],
340             pss_ltr_blkd[TELEM_PSS_LTR_BLOCKING_EVTS],
341             pss_idle[TELEM_PSS_IDLE_EVTS];
342         int index, idx, ret, err = 0;
343         u64 pstates = 0;
344
345         ret = telemetry_read_eventlog(TELEM_PSS, evtlog,
346                                       TELEM_MAX_OS_ALLOCATED_EVENTS);
347         if (ret < 0)
348                 return ret;
349
350         err = telemetry_get_evtname(TELEM_PSS, name,
351                                     TELEM_MAX_OS_ALLOCATED_EVENTS);
352         if (err < 0)
353                 return err;
354
355         seq_puts(s, "\n----------------------------------------------------\n");
356         seq_puts(s, "\tPSS TELEM EVENTLOG (Residency = field/19.2 us\n");
357         seq_puts(s, "----------------------------------------------------\n");
358         for (index = 0; index < ret; index++) {
359                 seq_printf(s, "%-32s %llu\n",
360                            name[index], evtlog[index].telem_evtlog);
361
362                 /* Fetch PSS IDLE State */
363                 if (evtlog[index].telem_evtid == conf->pss_idle_id) {
364                         pss_idle[conf->pss_idle_evts - 1] =
365                         (evtlog[index].telem_evtlog >>
366                         conf->pss_idle_data[conf->pss_idle_evts - 1].bit_pos) &
367                         TELEM_APL_MASK_PCS_STATE;
368                 }
369
370                 TELEM_CHECK_AND_PARSE_EVTS(conf->pss_idle_id,
371                                            conf->pss_idle_evts - 1,
372                                            pss_idle, evtlog[index].telem_evtlog,
373                                            conf->pss_idle_data, TELEM_MASK_BIT);
374
375                 TELEM_CHECK_AND_PARSE_EVTS(conf->pcs_idle_blkd_id,
376                                            conf->pcs_idle_blkd_evts,
377                                            pcs_idle_blkd,
378                                            evtlog[index].telem_evtlog,
379                                            conf->pcs_idle_blkd_data,
380                                            TELEM_MASK_BYTE);
381
382                 TELEM_CHECK_AND_PARSE_EVTS(conf->pcs_s0ix_blkd_id,
383                                            conf->pcs_s0ix_blkd_evts,
384                                            pcs_s0ix_blkd,
385                                            evtlog[index].telem_evtlog,
386                                            conf->pcs_s0ix_blkd_data,
387                                            TELEM_MASK_BYTE);
388
389                 TELEM_CHECK_AND_PARSE_EVTS(conf->pss_wakeup_id,
390                                            conf->pss_wakeup_evts,
391                                            pss_s0ix_wakeup,
392                                            evtlog[index].telem_evtlog,
393                                            conf->pss_wakeup, TELEM_MASK_BYTE);
394
395                 TELEM_CHECK_AND_PARSE_EVTS(conf->pss_ltr_blocking_id,
396                                            conf->pss_ltr_evts, pss_ltr_blkd,
397                                            evtlog[index].telem_evtlog,
398                                            conf->pss_ltr_data, TELEM_MASK_BYTE);
399
400                 if (evtlog[index].telem_evtid == debugfs_conf->pstates_id)
401                         pstates = evtlog[index].telem_evtlog;
402         }
403
404         seq_puts(s, "\n--------------------------------------\n");
405         seq_puts(s, "PStates\n");
406         seq_puts(s, "--------------------------------------\n");
407         seq_puts(s, "Domain\t\t\t\tFreq(Mhz)\n");
408         seq_printf(s, " IA\t\t\t\t %llu\n GT\t\t\t\t %llu\n",
409                    (pstates & TELEM_MASK_BYTE)*100,
410                    ((pstates >> 8) & TELEM_MASK_BYTE)*50/3);
411
412         seq_printf(s, " IUNIT\t\t\t\t %llu\n SA\t\t\t\t %llu\n",
413                    ((pstates >> 16) & TELEM_MASK_BYTE)*25,
414                    ((pstates >> 24) & TELEM_MASK_BYTE)*50/3);
415
416         seq_puts(s, "\n--------------------------------------\n");
417         seq_puts(s, "PSS IDLE Status\n");
418         seq_puts(s, "--------------------------------------\n");
419         seq_puts(s, "Device\t\t\t\t\tIDLE\n");
420         for (index = 0; index < debugfs_conf->pss_idle_evts; index++) {
421                 seq_printf(s, "%-32s\t%u\n",
422                            debugfs_conf->pss_idle_data[index].name,
423                            pss_idle[index]);
424         }
425
426         seq_puts(s, "\n--------------------------------------\n");
427         seq_puts(s, "PSS Idle blkd Status (~1ms saturating bucket)\n");
428         seq_puts(s, "--------------------------------------\n");
429         seq_puts(s, "Blocker\t\t\t\t\tCount\n");
430         for (index = 0; index < debugfs_conf->pcs_idle_blkd_evts; index++) {
431                 seq_printf(s, "%-32s\t%u\n",
432                            debugfs_conf->pcs_idle_blkd_data[index].name,
433                            pcs_idle_blkd[index]);
434         }
435
436         seq_puts(s, "\n--------------------------------------\n");
437         seq_puts(s, "PSS S0ix blkd Status (~1ms saturating bucket)\n");
438         seq_puts(s, "--------------------------------------\n");
439         seq_puts(s, "Blocker\t\t\t\t\tCount\n");
440         for (index = 0; index < debugfs_conf->pcs_s0ix_blkd_evts; index++) {
441                 seq_printf(s, "%-32s\t%u\n",
442                            debugfs_conf->pcs_s0ix_blkd_data[index].name,
443                            pcs_s0ix_blkd[index]);
444         }
445
446         seq_puts(s, "\n--------------------------------------\n");
447         seq_puts(s, "LTR Blocking Status (~1ms saturating bucket)\n");
448         seq_puts(s, "--------------------------------------\n");
449         seq_puts(s, "Blocker\t\t\t\t\tCount\n");
450         for (index = 0; index < debugfs_conf->pss_ltr_evts; index++) {
451                 seq_printf(s, "%-32s\t%u\n",
452                            debugfs_conf->pss_ltr_data[index].name,
453                            pss_s0ix_wakeup[index]);
454         }
455
456         seq_puts(s, "\n--------------------------------------\n");
457         seq_puts(s, "Wakes Status (~1ms saturating bucket)\n");
458         seq_puts(s, "--------------------------------------\n");
459         seq_puts(s, "Wakes\t\t\t\t\tCount\n");
460         for (index = 0; index < debugfs_conf->pss_wakeup_evts; index++) {
461                 seq_printf(s, "%-32s\t%u\n",
462                            debugfs_conf->pss_wakeup[index].name,
463                            pss_ltr_blkd[index]);
464         }
465
466         return 0;
467 }
468
469 static int telem_pss_state_open(struct inode *inode, struct file *file)
470 {
471         return single_open(file, telem_pss_states_show, inode->i_private);
472 }
473
474 static const struct file_operations telem_pss_ops = {
475         .open           = telem_pss_state_open,
476         .read           = seq_read,
477         .llseek         = seq_lseek,
478         .release        = single_release,
479 };
480
481 static int telem_ioss_states_show(struct seq_file *s, void *unused)
482 {
483         struct telemetry_evtlog evtlog[TELEM_MAX_OS_ALLOCATED_EVENTS];
484         const char *name[TELEM_MAX_OS_ALLOCATED_EVENTS];
485         int index, ret, err;
486
487         ret = telemetry_read_eventlog(TELEM_IOSS, evtlog,
488                                       TELEM_MAX_OS_ALLOCATED_EVENTS);
489         if (ret < 0)
490                 return ret;
491
492         err = telemetry_get_evtname(TELEM_IOSS, name,
493                                     TELEM_MAX_OS_ALLOCATED_EVENTS);
494         if (err < 0)
495                 return err;
496
497         seq_puts(s, "--------------------------------------\n");
498         seq_puts(s, "\tI0SS TELEMETRY EVENTLOG\n");
499         seq_puts(s, "--------------------------------------\n");
500         for (index = 0; index < ret; index++) {
501                 seq_printf(s, "%-32s 0x%llx\n",
502                            name[index], evtlog[index].telem_evtlog);
503         }
504
505         return 0;
506 }
507
508 static int telem_ioss_state_open(struct inode *inode, struct file *file)
509 {
510         return single_open(file, telem_ioss_states_show, inode->i_private);
511 }
512
513 static const struct file_operations telem_ioss_ops = {
514         .open           = telem_ioss_state_open,
515         .read           = seq_read,
516         .llseek         = seq_lseek,
517         .release        = single_release,
518 };
519
520 static int telem_soc_states_show(struct seq_file *s, void *unused)
521 {
522         u32 d3_sts[TELEM_IOSS_DX_D0IX_EVTS], d0ix_sts[TELEM_IOSS_DX_D0IX_EVTS];
523         u32 pg_sts[TELEM_IOSS_PG_EVTS], pss_idle[TELEM_PSS_IDLE_EVTS];
524         struct telemetry_evtlog evtlog[TELEM_MAX_OS_ALLOCATED_EVENTS];
525         u32 s0ix_total_ctr = 0, s0ix_shlw_ctr = 0, s0ix_deep_ctr = 0;
526         u64 s0ix_total_res = 0, s0ix_shlw_res = 0, s0ix_deep_res = 0;
527         struct telemetry_debugfs_conf *conf = debugfs_conf;
528         struct pci_dev *dev = NULL;
529         int index, idx, ret;
530         u32 d3_state;
531         u16 pmcsr;
532
533         ret = telemetry_read_eventlog(TELEM_IOSS, evtlog,
534                                       TELEM_MAX_OS_ALLOCATED_EVENTS);
535         if (ret < 0)
536                 return ret;
537
538         for (index = 0; index < ret; index++) {
539                 TELEM_CHECK_AND_PARSE_EVTS(conf->ioss_d3_id,
540                                            conf->ioss_d0ix_evts,
541                                            d3_sts, evtlog[index].telem_evtlog,
542                                            conf->ioss_d0ix_data,
543                                            TELEM_MASK_BIT);
544
545                 TELEM_CHECK_AND_PARSE_EVTS(conf->ioss_pg_id, conf->ioss_pg_evts,
546                                            pg_sts, evtlog[index].telem_evtlog,
547                                            conf->ioss_pg_data, TELEM_MASK_BIT);
548
549                 TELEM_CHECK_AND_PARSE_EVTS(conf->ioss_d0ix_id,
550                                            conf->ioss_d0ix_evts,
551                                            d0ix_sts, evtlog[index].telem_evtlog,
552                                            conf->ioss_d0ix_data,
553                                            TELEM_MASK_BIT);
554
555                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_total_occ_id,
556                                            s0ix_total_ctr);
557
558                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_shlw_occ_id,
559                                            s0ix_shlw_ctr);
560
561                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_deep_occ_id,
562                                            s0ix_deep_ctr);
563
564                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_total_res_id,
565                                            s0ix_total_res);
566
567                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_shlw_res_id,
568                                            s0ix_shlw_res);
569
570                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_deep_res_id,
571                                            s0ix_deep_res);
572         }
573
574         seq_puts(s, "\n---------------------------------------------------\n");
575         seq_puts(s, "S0IX Type\t\t\t Occurrence\t\t Residency(us)\n");
576         seq_puts(s, "---------------------------------------------------\n");
577
578         seq_printf(s, "S0IX Shallow\t\t\t %10u\t %10llu\n",
579                    s0ix_shlw_ctr -
580                    conf->suspend_stats.shlw_ctr,
581                    (u64)((s0ix_shlw_res -
582                    conf->suspend_stats.shlw_res)*10/192));
583
584         seq_printf(s, "S0IX Deep\t\t\t %10u\t %10llu\n",
585                    s0ix_deep_ctr -
586                    conf->suspend_stats.deep_ctr,
587                    (u64)((s0ix_deep_res -
588                    conf->suspend_stats.deep_res)*10/192));
589
590         seq_printf(s, "Suspend(With S0ixShallow)\t %10u\t %10llu\n",
591                    conf->suspend_stats.shlw_ctr,
592                    (u64)(conf->suspend_stats.shlw_res*10)/192);
593
594         seq_printf(s, "Suspend(With S0ixDeep)\t\t %10u\t %10llu\n",
595                    conf->suspend_stats.deep_ctr,
596                    (u64)(conf->suspend_stats.deep_res*10)/192);
597
598         seq_printf(s, "TOTAL S0IX\t\t\t %10u\t %10llu\n", s0ix_total_ctr,
599                    (u64)(s0ix_total_res*10/192));
600         seq_puts(s, "\n-------------------------------------------------\n");
601         seq_puts(s, "\t\tDEVICE STATES\n");
602         seq_puts(s, "-------------------------------------------------\n");
603
604         for_each_pci_dev(dev) {
605                 pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
606                 d3_state = ((pmcsr & PCI_PM_CTRL_STATE_MASK) ==
607                             (__force int)PCI_D3hot) ? 1 : 0;
608
609                 seq_printf(s, "pci %04x %04X %s %20.20s: ",
610                            dev->vendor, dev->device, dev_name(&dev->dev),
611                            dev_driver_string(&dev->dev));
612                 seq_printf(s, " d3:%x\n", d3_state);
613         }
614
615         seq_puts(s, "\n--------------------------------------\n");
616         seq_puts(s, "D3/D0i3 Status\n");
617         seq_puts(s, "--------------------------------------\n");
618         seq_puts(s, "Block\t\t D3\t D0i3\n");
619         for (index = 0; index < conf->ioss_d0ix_evts; index++) {
620                 seq_printf(s, "%-10s\t %u\t %u\n",
621                            conf->ioss_d0ix_data[index].name,
622                            d3_sts[index], d0ix_sts[index]);
623         }
624
625         seq_puts(s, "\n--------------------------------------\n");
626         seq_puts(s, "South Complex PowerGate Status\n");
627         seq_puts(s, "--------------------------------------\n");
628         seq_puts(s, "Device\t\t PG\n");
629         for (index = 0; index < conf->ioss_pg_evts; index++) {
630                 seq_printf(s, "%-10s\t %u\n",
631                            conf->ioss_pg_data[index].name,
632                            pg_sts[index]);
633         }
634
635         evtlog->telem_evtid = conf->pss_idle_id;
636         ret = telemetry_read_events(TELEM_PSS, evtlog, 1);
637         if (ret < 0)
638                 return ret;
639
640         seq_puts(s, "\n-----------------------------------------\n");
641         seq_puts(s, "North Idle Status\n");
642         seq_puts(s, "-----------------------------------------\n");
643         for (idx = 0; idx < conf->pss_idle_evts - 1; idx++) {
644                 pss_idle[idx] = (evtlog->telem_evtlog >>
645                                 conf->pss_idle_data[idx].bit_pos) &
646                                 TELEM_MASK_BIT;
647         }
648
649         pss_idle[idx] = (evtlog->telem_evtlog >>
650                         conf->pss_idle_data[idx].bit_pos) &
651                         TELEM_APL_MASK_PCS_STATE;
652
653         for (index = 0; index < conf->pss_idle_evts; index++) {
654                 seq_printf(s, "%-30s %u\n",
655                            conf->pss_idle_data[index].name,
656                            pss_idle[index]);
657         }
658
659         seq_puts(s, "\nPCS_STATUS Code\n");
660         seq_puts(s, "0:C0 1:C1 2:C1_DN_WT_DEV 3:C2 4:C2_WT_DE_MEM_UP\n");
661         seq_puts(s, "5:C2_WT_DE_MEM_DOWN 6:C2_UP_WT_DEV 7:C2_DN 8:C2_VOA\n");
662         seq_puts(s, "9:C2_VOA_UP 10:S0IX_PRE 11:S0IX\n");
663
664         return 0;
665 }
666
667 static int telem_soc_state_open(struct inode *inode, struct file *file)
668 {
669         return single_open(file, telem_soc_states_show, inode->i_private);
670 }
671
672 static const struct file_operations telem_socstate_ops = {
673         .open           = telem_soc_state_open,
674         .read           = seq_read,
675         .llseek         = seq_lseek,
676         .release        = single_release,
677 };
678
679 static int telem_s0ix_res_get(void *data, u64 *val)
680 {
681         u64 s0ix_total_res;
682         int ret;
683
684         ret = intel_pmc_s0ix_counter_read(&s0ix_total_res);
685         if (ret) {
686                 pr_err("Failed to read S0ix residency");
687                 return ret;
688         }
689
690         *val = s0ix_total_res;
691
692         return 0;
693 }
694
695 DEFINE_DEBUGFS_ATTRIBUTE(telem_s0ix_fops, telem_s0ix_res_get, NULL, "%llu\n");
696
697 static int telem_pss_trc_verb_show(struct seq_file *s, void *unused)
698 {
699         u32 verbosity;
700         int err;
701
702         err = telemetry_get_trace_verbosity(TELEM_PSS, &verbosity);
703         if (err) {
704                 pr_err("Get PSS Trace Verbosity Failed with Error %d\n", err);
705                 return -EFAULT;
706         }
707
708         seq_printf(s, "PSS Trace Verbosity %u\n", verbosity);
709         return 0;
710 }
711
712 static ssize_t telem_pss_trc_verb_write(struct file *file,
713                                         const char __user *userbuf,
714                                         size_t count, loff_t *ppos)
715 {
716         u32 verbosity;
717         int err;
718
719         if (kstrtou32_from_user(userbuf, count, 0, &verbosity))
720                 return -EFAULT;
721
722         err = telemetry_set_trace_verbosity(TELEM_PSS, verbosity);
723         if (err) {
724                 pr_err("Changing PSS Trace Verbosity Failed. Error %d\n", err);
725                 count = err;
726         }
727
728         return count;
729 }
730
731 static int telem_pss_trc_verb_open(struct inode *inode, struct file *file)
732 {
733         return single_open(file, telem_pss_trc_verb_show, inode->i_private);
734 }
735
736 static const struct file_operations telem_pss_trc_verb_ops = {
737         .open           = telem_pss_trc_verb_open,
738         .read           = seq_read,
739         .write          = telem_pss_trc_verb_write,
740         .llseek         = seq_lseek,
741         .release        = single_release,
742 };
743
744 static int telem_ioss_trc_verb_show(struct seq_file *s, void *unused)
745 {
746         u32 verbosity;
747         int err;
748
749         err = telemetry_get_trace_verbosity(TELEM_IOSS, &verbosity);
750         if (err) {
751                 pr_err("Get IOSS Trace Verbosity Failed with Error %d\n", err);
752                 return -EFAULT;
753         }
754
755         seq_printf(s, "IOSS Trace Verbosity %u\n", verbosity);
756         return 0;
757 }
758
759 static ssize_t telem_ioss_trc_verb_write(struct file *file,
760                                          const char __user *userbuf,
761                                          size_t count, loff_t *ppos)
762 {
763         u32 verbosity;
764         int err;
765
766         if (kstrtou32_from_user(userbuf, count, 0, &verbosity))
767                 return -EFAULT;
768
769         err = telemetry_set_trace_verbosity(TELEM_IOSS, verbosity);
770         if (err) {
771                 pr_err("Changing IOSS Trace Verbosity Failed. Error %d\n", err);
772                 count = err;
773         }
774
775         return count;
776 }
777
778 static int telem_ioss_trc_verb_open(struct inode *inode, struct file *file)
779 {
780         return single_open(file, telem_ioss_trc_verb_show, inode->i_private);
781 }
782
783 static const struct file_operations telem_ioss_trc_verb_ops = {
784         .open           = telem_ioss_trc_verb_open,
785         .read           = seq_read,
786         .write          = telem_ioss_trc_verb_write,
787         .llseek         = seq_lseek,
788         .release        = single_release,
789 };
790
791 static int pm_suspend_prep_cb(void)
792 {
793         struct telemetry_evtlog evtlog[TELEM_MAX_OS_ALLOCATED_EVENTS];
794         struct telemetry_debugfs_conf *conf = debugfs_conf;
795         int ret, index;
796
797         ret = telemetry_raw_read_eventlog(TELEM_IOSS, evtlog,
798                         TELEM_MAX_OS_ALLOCATED_EVENTS);
799         if (ret < 0) {
800                 suspend_prep_ok = 0;
801                 goto out;
802         }
803
804         for (index = 0; index < ret; index++) {
805
806                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_shlw_occ_id,
807                                            suspend_shlw_ctr_temp);
808
809                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_deep_occ_id,
810                                            suspend_deep_ctr_temp);
811
812                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_shlw_res_id,
813                                            suspend_shlw_res_temp);
814
815                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_deep_res_id,
816                                            suspend_deep_res_temp);
817         }
818         suspend_prep_ok = 1;
819 out:
820         return NOTIFY_OK;
821 }
822
823 static int pm_suspend_exit_cb(void)
824 {
825         struct telemetry_evtlog evtlog[TELEM_MAX_OS_ALLOCATED_EVENTS];
826         static u32 suspend_shlw_ctr_exit, suspend_deep_ctr_exit;
827         static u64 suspend_shlw_res_exit, suspend_deep_res_exit;
828         struct telemetry_debugfs_conf *conf = debugfs_conf;
829         int ret, index;
830
831         if (!suspend_prep_ok)
832                 goto out;
833
834         ret = telemetry_raw_read_eventlog(TELEM_IOSS, evtlog,
835                                           TELEM_MAX_OS_ALLOCATED_EVENTS);
836         if (ret < 0)
837                 goto out;
838
839         for (index = 0; index < ret; index++) {
840                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_shlw_occ_id,
841                                            suspend_shlw_ctr_exit);
842
843                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_deep_occ_id,
844                                            suspend_deep_ctr_exit);
845
846                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_shlw_res_id,
847                                            suspend_shlw_res_exit);
848
849                 TELEM_CHECK_AND_PARSE_CTRS(conf->s0ix_deep_res_id,
850                                            suspend_deep_res_exit);
851         }
852
853         if ((suspend_shlw_ctr_exit < suspend_shlw_ctr_temp) ||
854             (suspend_deep_ctr_exit < suspend_deep_ctr_temp) ||
855             (suspend_shlw_res_exit < suspend_shlw_res_temp) ||
856             (suspend_deep_res_exit < suspend_deep_res_temp)) {
857                 pr_err("Wrong s0ix counters detected\n");
858                 goto out;
859         }
860
861         /*
862          * Due to some design limitations in the firmware, sometimes the
863          * counters do not get updated by the time we reach here. As a
864          * workaround, we try to see if this was a genuine case of sleep
865          * failure or not by cross-checking from PMC GCR registers directly.
866          */
867         if (suspend_shlw_ctr_exit == suspend_shlw_ctr_temp &&
868             suspend_deep_ctr_exit == suspend_deep_ctr_temp) {
869                 ret = intel_pmc_gcr_read64(PMC_GCR_TELEM_SHLW_S0IX_REG,
870                                           &suspend_shlw_res_exit);
871                 if (ret < 0)
872                         goto out;
873
874                 ret = intel_pmc_gcr_read64(PMC_GCR_TELEM_DEEP_S0IX_REG,
875                                           &suspend_deep_res_exit);
876                 if (ret < 0)
877                         goto out;
878
879                 if (suspend_shlw_res_exit > suspend_shlw_res_temp)
880                         suspend_shlw_ctr_exit++;
881
882                 if (suspend_deep_res_exit > suspend_deep_res_temp)
883                         suspend_deep_ctr_exit++;
884         }
885
886         suspend_shlw_ctr_exit -= suspend_shlw_ctr_temp;
887         suspend_deep_ctr_exit -= suspend_deep_ctr_temp;
888         suspend_shlw_res_exit -= suspend_shlw_res_temp;
889         suspend_deep_res_exit -= suspend_deep_res_temp;
890
891         if (suspend_shlw_ctr_exit != 0) {
892                 conf->suspend_stats.shlw_ctr +=
893                 suspend_shlw_ctr_exit;
894
895                 conf->suspend_stats.shlw_res +=
896                 suspend_shlw_res_exit;
897         }
898
899         if (suspend_deep_ctr_exit != 0) {
900                 conf->suspend_stats.deep_ctr +=
901                 suspend_deep_ctr_exit;
902
903                 conf->suspend_stats.deep_res +=
904                 suspend_deep_res_exit;
905         }
906
907 out:
908         suspend_prep_ok = 0;
909         return NOTIFY_OK;
910 }
911
912 static int pm_notification(struct notifier_block *this,
913                            unsigned long event, void *ptr)
914 {
915         switch (event) {
916         case PM_SUSPEND_PREPARE:
917                 return pm_suspend_prep_cb();
918         case PM_POST_SUSPEND:
919                 return pm_suspend_exit_cb();
920         }
921
922         return NOTIFY_DONE;
923 }
924
925 static struct notifier_block pm_notifier = {
926         .notifier_call = pm_notification,
927 };
928
929 static int __init telemetry_debugfs_init(void)
930 {
931         const struct x86_cpu_id *id;
932         int err;
933         struct dentry *f;
934
935         /* Only APL supported for now */
936         id = x86_match_cpu(telemetry_debugfs_cpu_ids);
937         if (!id)
938                 return -ENODEV;
939
940         debugfs_conf = (struct telemetry_debugfs_conf *)id->driver_data;
941
942         err = telemetry_pltconfig_valid();
943         if (err < 0) {
944                 pr_info("Invalid pltconfig, ensure IPC1 device is enabled in BIOS\n");
945                 return -ENODEV;
946         }
947
948         err = telemetry_debugfs_check_evts();
949         if (err < 0) {
950                 pr_info("telemetry_debugfs_check_evts failed\n");
951                 return -EINVAL;
952         }
953
954         register_pm_notifier(&pm_notifier);
955
956         err = -ENOMEM;
957         debugfs_conf->telemetry_dbg_dir = debugfs_create_dir("telemetry", NULL);
958         if (!debugfs_conf->telemetry_dbg_dir)
959                 goto out_pm;
960
961         f = debugfs_create_file("pss_info", S_IFREG | S_IRUGO,
962                                 debugfs_conf->telemetry_dbg_dir, NULL,
963                                 &telem_pss_ops);
964         if (!f) {
965                 pr_err("pss_sample_info debugfs register failed\n");
966                 goto out;
967         }
968
969         f = debugfs_create_file("ioss_info", S_IFREG | S_IRUGO,
970                                 debugfs_conf->telemetry_dbg_dir, NULL,
971                                 &telem_ioss_ops);
972         if (!f) {
973                 pr_err("ioss_sample_info debugfs register failed\n");
974                 goto out;
975         }
976
977         f = debugfs_create_file("soc_states", S_IFREG | S_IRUGO,
978                                 debugfs_conf->telemetry_dbg_dir,
979                                 NULL, &telem_socstate_ops);
980         if (!f) {
981                 pr_err("ioss_sample_info debugfs register failed\n");
982                 goto out;
983         }
984
985         f = debugfs_create_file("s0ix_residency_usec", S_IFREG | S_IRUGO,
986                                 debugfs_conf->telemetry_dbg_dir,
987                                 NULL, &telem_s0ix_fops);
988         if (!f) {
989                 pr_err("s0ix_residency_usec debugfs register failed\n");
990                 goto out;
991         }
992
993         f = debugfs_create_file("pss_trace_verbosity", S_IFREG | S_IRUGO,
994                                 debugfs_conf->telemetry_dbg_dir, NULL,
995                                 &telem_pss_trc_verb_ops);
996         if (!f) {
997                 pr_err("pss_trace_verbosity debugfs register failed\n");
998                 goto out;
999         }
1000
1001         f = debugfs_create_file("ioss_trace_verbosity", S_IFREG | S_IRUGO,
1002                                 debugfs_conf->telemetry_dbg_dir, NULL,
1003                                 &telem_ioss_trc_verb_ops);
1004         if (!f) {
1005                 pr_err("ioss_trace_verbosity debugfs register failed\n");
1006                 goto out;
1007         }
1008
1009         return 0;
1010
1011 out:
1012         debugfs_remove_recursive(debugfs_conf->telemetry_dbg_dir);
1013         debugfs_conf->telemetry_dbg_dir = NULL;
1014 out_pm:
1015         unregister_pm_notifier(&pm_notifier);
1016
1017         return err;
1018 }
1019
1020 static void __exit telemetry_debugfs_exit(void)
1021 {
1022         debugfs_remove_recursive(debugfs_conf->telemetry_dbg_dir);
1023         debugfs_conf->telemetry_dbg_dir = NULL;
1024         unregister_pm_notifier(&pm_notifier);
1025 }
1026
1027 late_initcall(telemetry_debugfs_init);
1028 module_exit(telemetry_debugfs_exit);
1029
1030 MODULE_AUTHOR("Souvik Kumar Chakravarty <souvik.k.chakravarty@intel.com>");
1031 MODULE_DESCRIPTION("Intel SoC Telemetry debugfs Interface");
1032 MODULE_VERSION(DRIVER_VERSION);
1033 MODULE_LICENSE("GPL v2");