Linux Audio

Check our new training course

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