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