2 * Intel SOC Telemetry Platform Driver: Currently supports APL
3 * Copyright (c) 2015, Intel Corporation.
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.
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
15 * This file provides the platform specific telemetry implementation for APL.
16 * It used the PUNIT and PMC IPC interfaces for configuring the counters.
17 * The accumulated results are fetched from SRAM.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/debugfs.h>
23 #include <linux/seq_file.h>
25 #include <linux/uaccess.h>
26 #include <linux/pci.h>
27 #include <linux/suspend.h>
28 #include <linux/platform_device.h>
30 #include <asm/cpu_device_id.h>
31 #include <asm/intel-family.h>
32 #include <asm/intel_pmc_ipc.h>
33 #include <asm/intel_punit_ipc.h>
34 #include <asm/intel_telemetry.h>
36 #define DRIVER_NAME "intel_telemetry"
37 #define DRIVER_VERSION "1.0.0"
39 #define TELEM_TRC_VERBOSITY_MASK 0x3
41 #define TELEM_MIN_PERIOD(x) ((x) & 0x7F0000)
42 #define TELEM_MAX_PERIOD(x) ((x) & 0x7F000000)
43 #define TELEM_SAMPLE_PERIOD_INVALID(x) ((x) & (BIT(7)))
44 #define TELEM_CLEAR_SAMPLE_PERIOD(x) ((x) &= ~0x7F)
46 #define TELEM_SAMPLING_DEFAULT_PERIOD 0xD
48 #define TELEM_MAX_EVENTS_SRAM 28
49 #define TELEM_SSRAM_STARTTIME_OFFSET 8
50 #define TELEM_SSRAM_EVTLOG_OFFSET 16
52 #define IOSS_TELEM_EVENT_READ 0x0
53 #define IOSS_TELEM_EVENT_WRITE 0x1
54 #define IOSS_TELEM_INFO_READ 0x2
55 #define IOSS_TELEM_TRACE_CTL_READ 0x5
56 #define IOSS_TELEM_TRACE_CTL_WRITE 0x6
57 #define IOSS_TELEM_EVENT_CTL_READ 0x7
58 #define IOSS_TELEM_EVENT_CTL_WRITE 0x8
59 #define IOSS_TELEM_EVT_CTRL_WRITE_SIZE 0x4
60 #define IOSS_TELEM_READ_WORD 0x1
61 #define IOSS_TELEM_WRITE_FOURBYTES 0x4
62 #define IOSS_TELEM_EVT_WRITE_SIZE 0x3
64 #define TELEM_INFO_SRAMEVTS_MASK 0xFF00
65 #define TELEM_INFO_SRAMEVTS_SHIFT 0x8
66 #define TELEM_SSRAM_READ_TIMEOUT 10
68 #define TELEM_INFO_NENABLES_MASK 0xFF
69 #define TELEM_EVENT_ENABLE 0x8000
71 #define TELEM_MASK_BIT 1
72 #define TELEM_MASK_BYTE 0xFF
73 #define BYTES_PER_LONG 8
74 #define TELEM_MASK_PCS_STATE 0xF
76 #define TELEM_DISABLE(x) ((x) &= ~(BIT(31)))
77 #define TELEM_CLEAR_EVENTS(x) ((x) |= (BIT(30)))
78 #define TELEM_ENABLE_SRAM_EVT_TRACE(x) ((x) &= ~(BIT(30) | BIT(24)))
79 #define TELEM_ENABLE_PERIODIC(x) ((x) |= (BIT(23) | BIT(31) | BIT(7)))
80 #define TELEM_EXTRACT_VERBOSITY(x, y) ((y) = (((x) >> 27) & 0x3))
81 #define TELEM_CLEAR_VERBOSITY_BITS(x) ((x) &= ~(BIT(27) | BIT(28)))
82 #define TELEM_SET_VERBOSITY_BITS(x, y) ((x) |= ((y) << 27))
84 #define TELEM_CPU(model, data) \
85 { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&data }
87 enum telemetry_action {
94 struct telem_ssram_region {
97 u64 events[TELEM_MAX_EVENTS_SRAM];
100 static struct telemetry_plt_config *telm_conf;
103 * The following counters are programmed by default during setup.
104 * Only 20 allocated to kernel driver
106 static struct telemetry_evtmap
107 telemetry_apl_ioss_default_events[TELEM_MAX_OS_ALLOCATED_EVENTS] = {
108 {"SOC_S0IX_TOTAL_RES", 0x4800},
109 {"SOC_S0IX_TOTAL_OCC", 0x4000},
110 {"SOC_S0IX_SHALLOW_RES", 0x4801},
111 {"SOC_S0IX_SHALLOW_OCC", 0x4001},
112 {"SOC_S0IX_DEEP_RES", 0x4802},
113 {"SOC_S0IX_DEEP_OCC", 0x4002},
114 {"PMC_POWER_GATE", 0x5818},
115 {"PMC_D3_STATES", 0x5819},
116 {"PMC_D0I3_STATES", 0x581A},
117 {"PMC_S0IX_WAKE_REASON_GPIO", 0x6000},
118 {"PMC_S0IX_WAKE_REASON_TIMER", 0x6001},
119 {"PMC_S0IX_WAKE_REASON_VNNREQ", 0x6002},
120 {"PMC_S0IX_WAKE_REASON_LOWPOWER", 0x6003},
121 {"PMC_S0IX_WAKE_REASON_EXTERNAL", 0x6004},
122 {"PMC_S0IX_WAKE_REASON_MISC", 0x6005},
123 {"PMC_S0IX_BLOCKING_IPS_D3_D0I3", 0x6006},
124 {"PMC_S0IX_BLOCKING_IPS_PG", 0x6007},
125 {"PMC_S0IX_BLOCKING_MISC_IPS_PG", 0x6008},
126 {"PMC_S0IX_BLOCK_IPS_VNN_REQ", 0x6009},
127 {"PMC_S0IX_BLOCK_IPS_CLOCKS", 0x600B},
131 static struct telemetry_evtmap
132 telemetry_apl_pss_default_events[TELEM_MAX_OS_ALLOCATED_EVENTS] = {
133 {"IA_CORE0_C6_RES", 0x0400},
134 {"IA_CORE0_C6_CTR", 0x0000},
135 {"IA_MODULE0_C7_RES", 0x0410},
136 {"IA_MODULE0_C7_CTR", 0x000E},
137 {"IA_C0_RES", 0x0805},
140 {"SOC_S0I3_RES", 0x0409},
141 {"SOC_S0I3_CTR", 0x000A},
142 {"PCS_S0I3_CTR", 0x0009},
143 {"PCS_C1E_RES", 0x041A},
144 {"PCS_IDLE_STATUS", 0x2806},
145 {"IA_PERF_LIMITS", 0x280B},
146 {"GT_PERF_LIMITS", 0x280C},
147 {"PCS_WAKEUP_S0IX_CTR", 0x0030},
148 {"PCS_IDLE_BLOCKED", 0x2C00},
149 {"PCS_S0IX_BLOCKED", 0x2C01},
150 {"PCS_S0IX_WAKE_REASONS", 0x2C02},
151 {"PCS_LTR_BLOCKING", 0x2C03},
152 {"PC2_AND_MEM_SHALLOW_IDLE_RES", 0x1D40},
155 static struct telemetry_evtmap
156 telemetry_glk_pss_default_events[TELEM_MAX_OS_ALLOCATED_EVENTS] = {
157 {"IA_CORE0_C6_RES", 0x0400},
158 {"IA_CORE0_C6_CTR", 0x0000},
159 {"IA_MODULE0_C7_RES", 0x0410},
160 {"IA_MODULE0_C7_CTR", 0x000C},
161 {"IA_C0_RES", 0x0805},
164 {"SOC_S0I3_RES", 0x0407},
165 {"SOC_S0I3_CTR", 0x0008},
166 {"PCS_S0I3_CTR", 0x0007},
167 {"PCS_C1E_RES", 0x0414},
168 {"PCS_IDLE_STATUS", 0x2806},
169 {"IA_PERF_LIMITS", 0x280B},
170 {"GT_PERF_LIMITS", 0x280C},
171 {"PCS_WAKEUP_S0IX_CTR", 0x0025},
172 {"PCS_IDLE_BLOCKED", 0x2C00},
173 {"PCS_S0IX_BLOCKED", 0x2C01},
174 {"PCS_S0IX_WAKE_REASONS", 0x2C02},
175 {"PCS_LTR_BLOCKING", 0x2C03},
176 {"PC2_AND_MEM_SHALLOW_IDLE_RES", 0x1D40},
179 /* APL specific Data */
180 static struct telemetry_plt_config telem_apl_config = {
182 .telem_evts = telemetry_apl_pss_default_events,
185 .telem_evts = telemetry_apl_ioss_default_events,
189 /* GLK specific Data */
190 static struct telemetry_plt_config telem_glk_config = {
192 .telem_evts = telemetry_glk_pss_default_events,
195 .telem_evts = telemetry_apl_ioss_default_events,
199 static const struct x86_cpu_id telemetry_cpu_ids[] = {
200 TELEM_CPU(INTEL_FAM6_ATOM_GOLDMONT, telem_apl_config),
201 TELEM_CPU(INTEL_FAM6_ATOM_GOLDMONT_PLUS, telem_glk_config),
205 MODULE_DEVICE_TABLE(x86cpu, telemetry_cpu_ids);
207 static inline int telem_get_unitconfig(enum telemetry_unit telem_unit,
208 struct telemetry_unit_config **unit_config)
210 if (telem_unit == TELEM_PSS)
211 *unit_config = &(telm_conf->pss_config);
212 else if (telem_unit == TELEM_IOSS)
213 *unit_config = &(telm_conf->ioss_config);
221 static int telemetry_check_evtid(enum telemetry_unit telem_unit,
223 enum telemetry_action action)
225 struct telemetry_unit_config *unit_config;
228 ret = telem_get_unitconfig(telem_unit, &unit_config);
234 if (len > TELEM_MAX_EVENTS_SRAM)
240 if (len > TELEM_MAX_EVENTS_SRAM)
243 if ((len > 0) && (evtmap == NULL))
249 if ((len + unit_config->ssram_evts_used) >
250 TELEM_MAX_EVENTS_SRAM)
253 if ((len > 0) && (evtmap == NULL))
259 pr_err("Unknown Telemetry action Specified %d\n", action);
267 static inline int telemetry_plt_config_ioss_event(u32 evt_id, int index)
272 write_buf = evt_id | TELEM_EVENT_ENABLE;
273 write_buf <<= BITS_PER_BYTE;
276 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
277 IOSS_TELEM_EVENT_WRITE, (u8 *)&write_buf,
278 IOSS_TELEM_EVT_WRITE_SIZE, NULL, 0);
283 static inline int telemetry_plt_config_pss_event(u32 evt_id, int index)
288 write_buf = evt_id | TELEM_EVENT_ENABLE;
289 ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT,
290 index, 0, &write_buf, NULL);
295 static int telemetry_setup_iossevtconfig(struct telemetry_evtconfig evtconfig,
296 enum telemetry_action action)
298 u8 num_ioss_evts, ioss_period;
303 num_ioss_evts = evtconfig.num_evts;
304 ioss_period = evtconfig.period;
305 ioss_evtmap = evtconfig.evtmap;
307 /* Get telemetry EVENT CTL */
308 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
309 IOSS_TELEM_EVENT_CTL_READ, NULL, 0,
310 &telem_ctrl, IOSS_TELEM_READ_WORD);
312 pr_err("IOSS TELEM_CTRL Read Failed\n");
316 /* Disable Telemetry */
317 TELEM_DISABLE(telem_ctrl);
319 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
320 IOSS_TELEM_EVENT_CTL_WRITE,
322 IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
325 pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
330 /* Reset Everything */
331 if (action == TELEM_RESET) {
332 /* Clear All Events */
333 TELEM_CLEAR_EVENTS(telem_ctrl);
335 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
336 IOSS_TELEM_EVENT_CTL_WRITE,
338 IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
341 pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
344 telm_conf->ioss_config.ssram_evts_used = 0;
346 /* Configure Events */
347 for (idx = 0; idx < num_ioss_evts; idx++) {
348 if (telemetry_plt_config_ioss_event(
349 telm_conf->ioss_config.telem_evts[idx].evt_id,
351 pr_err("IOSS TELEM_RESET Fail for data: %x\n",
352 telm_conf->ioss_config.telem_evts[idx].evt_id);
355 telm_conf->ioss_config.ssram_evts_used++;
359 /* Re-Configure Everything */
360 if (action == TELEM_UPDATE) {
361 /* Clear All Events */
362 TELEM_CLEAR_EVENTS(telem_ctrl);
364 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
365 IOSS_TELEM_EVENT_CTL_WRITE,
367 IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
370 pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
373 telm_conf->ioss_config.ssram_evts_used = 0;
375 /* Configure Events */
376 for (index = 0; index < num_ioss_evts; index++) {
377 telm_conf->ioss_config.telem_evts[index].evt_id =
380 if (telemetry_plt_config_ioss_event(
381 telm_conf->ioss_config.telem_evts[index].evt_id,
383 pr_err("IOSS TELEM_UPDATE Fail for Evt%x\n",
387 telm_conf->ioss_config.ssram_evts_used++;
391 /* Add some Events */
392 if (action == TELEM_ADD) {
393 /* Configure Events */
394 for (index = telm_conf->ioss_config.ssram_evts_used, idx = 0;
395 idx < num_ioss_evts; index++, idx++) {
396 telm_conf->ioss_config.telem_evts[index].evt_id =
399 if (telemetry_plt_config_ioss_event(
400 telm_conf->ioss_config.telem_evts[index].evt_id,
402 pr_err("IOSS TELEM_ADD Fail for Event %x\n",
406 telm_conf->ioss_config.ssram_evts_used++;
410 /* Enable Periodic Telemetry Events and enable SRAM trace */
411 TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
412 TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
413 TELEM_ENABLE_PERIODIC(telem_ctrl);
414 telem_ctrl |= ioss_period;
416 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
417 IOSS_TELEM_EVENT_CTL_WRITE,
419 IOSS_TELEM_EVT_CTRL_WRITE_SIZE, NULL, 0);
421 pr_err("IOSS TELEM_CTRL Event Enable Write Failed\n");
425 telm_conf->ioss_config.curr_period = ioss_period;
431 static int telemetry_setup_pssevtconfig(struct telemetry_evtconfig evtconfig,
432 enum telemetry_action action)
434 u8 num_pss_evts, pss_period;
439 num_pss_evts = evtconfig.num_evts;
440 pss_period = evtconfig.period;
441 pss_evtmap = evtconfig.evtmap;
444 /* Get telemetry EVENT CTL */
445 ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_READ_TELE_EVENT_CTRL,
446 0, 0, NULL, &telem_ctrl);
448 pr_err("PSS TELEM_CTRL Read Failed\n");
452 /* Disable Telemetry */
453 TELEM_DISABLE(telem_ctrl);
454 ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
455 0, 0, &telem_ctrl, NULL);
457 pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
461 /* Reset Everything */
462 if (action == TELEM_RESET) {
463 /* Clear All Events */
464 TELEM_CLEAR_EVENTS(telem_ctrl);
466 ret = intel_punit_ipc_command(
467 IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
468 0, 0, &telem_ctrl, NULL);
470 pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
473 telm_conf->pss_config.ssram_evts_used = 0;
474 /* Configure Events */
475 for (idx = 0; idx < num_pss_evts; idx++) {
476 if (telemetry_plt_config_pss_event(
477 telm_conf->pss_config.telem_evts[idx].evt_id,
479 pr_err("PSS TELEM_RESET Fail for Event %x\n",
480 telm_conf->pss_config.telem_evts[idx].evt_id);
483 telm_conf->pss_config.ssram_evts_used++;
487 /* Re-Configure Everything */
488 if (action == TELEM_UPDATE) {
489 /* Clear All Events */
490 TELEM_CLEAR_EVENTS(telem_ctrl);
492 ret = intel_punit_ipc_command(
493 IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
494 0, 0, &telem_ctrl, NULL);
496 pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
499 telm_conf->pss_config.ssram_evts_used = 0;
501 /* Configure Events */
502 for (index = 0; index < num_pss_evts; index++) {
503 telm_conf->pss_config.telem_evts[index].evt_id =
506 if (telemetry_plt_config_pss_event(
507 telm_conf->pss_config.telem_evts[index].evt_id,
509 pr_err("PSS TELEM_UPDATE Fail for Event %x\n",
513 telm_conf->pss_config.ssram_evts_used++;
517 /* Add some Events */
518 if (action == TELEM_ADD) {
519 /* Configure Events */
520 for (index = telm_conf->pss_config.ssram_evts_used, idx = 0;
521 idx < num_pss_evts; index++, idx++) {
523 telm_conf->pss_config.telem_evts[index].evt_id =
526 if (telemetry_plt_config_pss_event(
527 telm_conf->pss_config.telem_evts[index].evt_id,
529 pr_err("PSS TELEM_ADD Fail for Event %x\n",
533 telm_conf->pss_config.ssram_evts_used++;
537 /* Enable Periodic Telemetry Events and enable SRAM trace */
538 TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
539 TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
540 TELEM_ENABLE_PERIODIC(telem_ctrl);
541 telem_ctrl |= pss_period;
543 ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
544 0, 0, &telem_ctrl, NULL);
546 pr_err("PSS TELEM_CTRL Event Enable Write Failed\n");
550 telm_conf->pss_config.curr_period = pss_period;
555 static int telemetry_setup_evtconfig(struct telemetry_evtconfig pss_evtconfig,
556 struct telemetry_evtconfig ioss_evtconfig,
557 enum telemetry_action action)
561 mutex_lock(&(telm_conf->telem_lock));
563 if ((action == TELEM_UPDATE) && (telm_conf->telem_in_use)) {
568 ret = telemetry_check_evtid(TELEM_PSS, pss_evtconfig.evtmap,
569 pss_evtconfig.num_evts, action);
573 ret = telemetry_check_evtid(TELEM_IOSS, ioss_evtconfig.evtmap,
574 ioss_evtconfig.num_evts, action);
578 if (ioss_evtconfig.num_evts) {
579 ret = telemetry_setup_iossevtconfig(ioss_evtconfig, action);
584 if (pss_evtconfig.num_evts) {
585 ret = telemetry_setup_pssevtconfig(pss_evtconfig, action);
590 if ((action == TELEM_UPDATE) || (action == TELEM_ADD))
591 telm_conf->telem_in_use = true;
593 telm_conf->telem_in_use = false;
596 mutex_unlock(&(telm_conf->telem_lock));
600 static int telemetry_setup(struct platform_device *pdev)
602 struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
603 u32 read_buf, events, event_regs;
606 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY, IOSS_TELEM_INFO_READ,
607 NULL, 0, &read_buf, IOSS_TELEM_READ_WORD);
609 dev_err(&pdev->dev, "IOSS TELEM_INFO Read Failed\n");
613 /* Get telemetry Info */
614 events = (read_buf & TELEM_INFO_SRAMEVTS_MASK) >>
615 TELEM_INFO_SRAMEVTS_SHIFT;
616 event_regs = read_buf & TELEM_INFO_NENABLES_MASK;
617 if ((events < TELEM_MAX_EVENTS_SRAM) ||
618 (event_regs < TELEM_MAX_EVENTS_SRAM)) {
619 dev_err(&pdev->dev, "IOSS:Insufficient Space for SRAM Trace\n");
620 dev_err(&pdev->dev, "SRAM Events %d; Event Regs %d\n",
625 telm_conf->ioss_config.min_period = TELEM_MIN_PERIOD(read_buf);
626 telm_conf->ioss_config.max_period = TELEM_MAX_PERIOD(read_buf);
628 /* PUNIT Mailbox Setup */
629 ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_READ_TELE_INFO, 0, 0,
632 dev_err(&pdev->dev, "PSS TELEM_INFO Read Failed\n");
636 /* Get telemetry Info */
637 events = (read_buf & TELEM_INFO_SRAMEVTS_MASK) >>
638 TELEM_INFO_SRAMEVTS_SHIFT;
639 event_regs = read_buf & TELEM_INFO_SRAMEVTS_MASK;
640 if ((events < TELEM_MAX_EVENTS_SRAM) ||
641 (event_regs < TELEM_MAX_EVENTS_SRAM)) {
642 dev_err(&pdev->dev, "PSS:Insufficient Space for SRAM Trace\n");
643 dev_err(&pdev->dev, "SRAM Events %d; Event Regs %d\n",
648 telm_conf->pss_config.min_period = TELEM_MIN_PERIOD(read_buf);
649 telm_conf->pss_config.max_period = TELEM_MAX_PERIOD(read_buf);
651 pss_evtconfig.evtmap = NULL;
652 pss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
653 pss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
655 ioss_evtconfig.evtmap = NULL;
656 ioss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
657 ioss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
659 ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
662 dev_err(&pdev->dev, "TELEMTRY Setup Failed\n");
668 static int telemetry_plt_update_events(struct telemetry_evtconfig pss_evtconfig,
669 struct telemetry_evtconfig ioss_evtconfig)
673 if ((pss_evtconfig.num_evts > 0) &&
674 (TELEM_SAMPLE_PERIOD_INVALID(pss_evtconfig.period))) {
675 pr_err("PSS Sampling Period Out of Range\n");
679 if ((ioss_evtconfig.num_evts > 0) &&
680 (TELEM_SAMPLE_PERIOD_INVALID(ioss_evtconfig.period))) {
681 pr_err("IOSS Sampling Period Out of Range\n");
685 ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
688 pr_err("TELEMTRY Config Failed\n");
694 static int telemetry_plt_set_sampling_period(u8 pss_period, u8 ioss_period)
699 mutex_lock(&(telm_conf->telem_lock));
701 if (TELEM_SAMPLE_PERIOD_INVALID(ioss_period)) {
702 pr_err("IOSS Sampling Period Out of Range\n");
707 /* Get telemetry EVENT CTL */
708 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
709 IOSS_TELEM_EVENT_CTL_READ, NULL, 0,
710 &telem_ctrl, IOSS_TELEM_READ_WORD);
712 pr_err("IOSS TELEM_CTRL Read Failed\n");
716 /* Disable Telemetry */
717 TELEM_DISABLE(telem_ctrl);
719 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
720 IOSS_TELEM_EVENT_CTL_WRITE,
722 IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
725 pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
729 /* Enable Periodic Telemetry Events and enable SRAM trace */
730 TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
731 TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
732 TELEM_ENABLE_PERIODIC(telem_ctrl);
733 telem_ctrl |= ioss_period;
735 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
736 IOSS_TELEM_EVENT_CTL_WRITE,
738 IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
741 pr_err("IOSS TELEM_CTRL Event Enable Write Failed\n");
744 telm_conf->ioss_config.curr_period = ioss_period;
748 if (TELEM_SAMPLE_PERIOD_INVALID(pss_period)) {
749 pr_err("PSS Sampling Period Out of Range\n");
754 /* Get telemetry EVENT CTL */
755 ret = intel_punit_ipc_command(
756 IPC_PUNIT_BIOS_READ_TELE_EVENT_CTRL,
757 0, 0, NULL, &telem_ctrl);
759 pr_err("PSS TELEM_CTRL Read Failed\n");
763 /* Disable Telemetry */
764 TELEM_DISABLE(telem_ctrl);
765 ret = intel_punit_ipc_command(
766 IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
767 0, 0, &telem_ctrl, NULL);
769 pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
773 /* Enable Periodic Telemetry Events and enable SRAM trace */
774 TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
775 TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
776 TELEM_ENABLE_PERIODIC(telem_ctrl);
777 telem_ctrl |= pss_period;
779 ret = intel_punit_ipc_command(
780 IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
781 0, 0, &telem_ctrl, NULL);
783 pr_err("PSS TELEM_CTRL Event Enable Write Failed\n");
786 telm_conf->pss_config.curr_period = pss_period;
790 mutex_unlock(&(telm_conf->telem_lock));
795 static int telemetry_plt_get_sampling_period(u8 *pss_min_period,
800 *pss_min_period = telm_conf->pss_config.min_period;
801 *pss_max_period = telm_conf->pss_config.max_period;
802 *ioss_min_period = telm_conf->ioss_config.min_period;
803 *ioss_max_period = telm_conf->ioss_config.max_period;
809 static int telemetry_plt_reset_events(void)
811 struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
814 pss_evtconfig.evtmap = NULL;
815 pss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
816 pss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
818 ioss_evtconfig.evtmap = NULL;
819 ioss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
820 ioss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
822 ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
825 pr_err("TELEMTRY Reset Failed\n");
831 static int telemetry_plt_get_eventconfig(struct telemetry_evtconfig *pss_config,
832 struct telemetry_evtconfig *ioss_config,
833 int pss_len, int ioss_len)
835 u32 *pss_evtmap, *ioss_evtmap;
838 pss_evtmap = pss_config->evtmap;
839 ioss_evtmap = ioss_config->evtmap;
841 mutex_lock(&(telm_conf->telem_lock));
842 pss_config->num_evts = telm_conf->pss_config.ssram_evts_used;
843 ioss_config->num_evts = telm_conf->ioss_config.ssram_evts_used;
845 pss_config->period = telm_conf->pss_config.curr_period;
846 ioss_config->period = telm_conf->ioss_config.curr_period;
848 if ((pss_len < telm_conf->pss_config.ssram_evts_used) ||
849 (ioss_len < telm_conf->ioss_config.ssram_evts_used)) {
850 mutex_unlock(&(telm_conf->telem_lock));
854 for (index = 0; index < telm_conf->pss_config.ssram_evts_used;
857 telm_conf->pss_config.telem_evts[index].evt_id;
860 for (index = 0; index < telm_conf->ioss_config.ssram_evts_used;
863 telm_conf->ioss_config.telem_evts[index].evt_id;
866 mutex_unlock(&(telm_conf->telem_lock));
871 static int telemetry_plt_add_events(u8 num_pss_evts, u8 num_ioss_evts,
872 u32 *pss_evtmap, u32 *ioss_evtmap)
874 struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
877 pss_evtconfig.evtmap = pss_evtmap;
878 pss_evtconfig.num_evts = num_pss_evts;
879 pss_evtconfig.period = telm_conf->pss_config.curr_period;
881 ioss_evtconfig.evtmap = ioss_evtmap;
882 ioss_evtconfig.num_evts = num_ioss_evts;
883 ioss_evtconfig.period = telm_conf->ioss_config.curr_period;
885 ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
888 pr_err("TELEMTRY ADD Failed\n");
893 static int telem_evtlog_read(enum telemetry_unit telem_unit,
894 struct telem_ssram_region *ssram_region, u8 len)
896 struct telemetry_unit_config *unit_config;
897 u64 timestamp_prev, timestamp_next;
898 int ret, index, timeout = 0;
900 ret = telem_get_unitconfig(telem_unit, &unit_config);
904 if (len > unit_config->ssram_evts_used)
905 len = unit_config->ssram_evts_used;
908 timestamp_prev = readq(unit_config->regmap);
909 if (!timestamp_prev) {
910 pr_err("Ssram under update. Please Try Later\n");
914 ssram_region->start_time = readq(unit_config->regmap +
915 TELEM_SSRAM_STARTTIME_OFFSET);
917 for (index = 0; index < len; index++) {
918 ssram_region->events[index] =
919 readq(unit_config->regmap + TELEM_SSRAM_EVTLOG_OFFSET +
920 BYTES_PER_LONG*index);
923 timestamp_next = readq(unit_config->regmap);
924 if (!timestamp_next) {
925 pr_err("Ssram under update. Please Try Later\n");
929 if (timeout++ > TELEM_SSRAM_READ_TIMEOUT) {
930 pr_err("Timeout while reading Events\n");
934 } while (timestamp_prev != timestamp_next);
936 ssram_region->timestamp = timestamp_next;
941 static int telemetry_plt_raw_read_eventlog(enum telemetry_unit telem_unit,
942 struct telemetry_evtlog *evtlog,
943 int len, int log_all_evts)
945 int index, idx1, ret, readlen = len;
946 struct telem_ssram_region ssram_region;
947 struct telemetry_evtmap *evtmap;
949 switch (telem_unit) {
951 evtmap = telm_conf->pss_config.telem_evts;
955 evtmap = telm_conf->ioss_config.telem_evts;
959 pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
964 readlen = TELEM_MAX_EVENTS_SRAM;
966 ret = telem_evtlog_read(telem_unit, &ssram_region, readlen);
970 /* Invalid evt-id array specified via length mismatch */
971 if ((!log_all_evts) && (len > ret))
975 for (index = 0; index < ret; index++) {
976 evtlog[index].telem_evtlog = ssram_region.events[index];
977 evtlog[index].telem_evtid = evtmap[index].evt_id;
980 for (index = 0, readlen = 0; (index < ret) && (readlen < len);
982 for (idx1 = 0; idx1 < len; idx1++) {
983 /* Elements matched */
984 if (evtmap[index].evt_id ==
985 evtlog[idx1].telem_evtid) {
986 evtlog[idx1].telem_evtlog =
987 ssram_region.events[index];
998 static int telemetry_plt_read_eventlog(enum telemetry_unit telem_unit,
999 struct telemetry_evtlog *evtlog, int len, int log_all_evts)
1003 mutex_lock(&(telm_conf->telem_lock));
1004 ret = telemetry_plt_raw_read_eventlog(telem_unit, evtlog,
1006 mutex_unlock(&(telm_conf->telem_lock));
1011 static int telemetry_plt_get_trace_verbosity(enum telemetry_unit telem_unit,
1017 if (verbosity == NULL)
1020 mutex_lock(&(telm_conf->telem_trace_lock));
1021 switch (telem_unit) {
1023 ret = intel_punit_ipc_command(
1024 IPC_PUNIT_BIOS_READ_TELE_TRACE_CTRL,
1027 pr_err("PSS TRACE_CTRL Read Failed\n");
1034 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1035 IOSS_TELEM_TRACE_CTL_READ, NULL, 0, &temp,
1036 IOSS_TELEM_READ_WORD);
1038 pr_err("IOSS TRACE_CTL Read Failed\n");
1045 pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
1049 TELEM_EXTRACT_VERBOSITY(temp, *verbosity);
1052 mutex_unlock(&(telm_conf->telem_trace_lock));
1056 static int telemetry_plt_set_trace_verbosity(enum telemetry_unit telem_unit,
1062 verbosity &= TELEM_TRC_VERBOSITY_MASK;
1064 mutex_lock(&(telm_conf->telem_trace_lock));
1065 switch (telem_unit) {
1067 ret = intel_punit_ipc_command(
1068 IPC_PUNIT_BIOS_READ_TELE_TRACE_CTRL,
1071 pr_err("PSS TRACE_CTRL Read Failed\n");
1075 TELEM_CLEAR_VERBOSITY_BITS(temp);
1076 TELEM_SET_VERBOSITY_BITS(temp, verbosity);
1078 ret = intel_punit_ipc_command(
1079 IPC_PUNIT_BIOS_WRITE_TELE_TRACE_CTRL,
1082 pr_err("PSS TRACE_CTRL Verbosity Set Failed\n");
1088 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1089 IOSS_TELEM_TRACE_CTL_READ, NULL, 0, &temp,
1090 IOSS_TELEM_READ_WORD);
1092 pr_err("IOSS TRACE_CTL Read Failed\n");
1096 TELEM_CLEAR_VERBOSITY_BITS(temp);
1097 TELEM_SET_VERBOSITY_BITS(temp, verbosity);
1099 ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1100 IOSS_TELEM_TRACE_CTL_WRITE, (u8 *)&temp,
1101 IOSS_TELEM_WRITE_FOURBYTES, NULL, 0);
1103 pr_err("IOSS TRACE_CTL Verbosity Set Failed\n");
1109 pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
1115 mutex_unlock(&(telm_conf->telem_trace_lock));
1119 static const struct telemetry_core_ops telm_pltops = {
1120 .get_trace_verbosity = telemetry_plt_get_trace_verbosity,
1121 .set_trace_verbosity = telemetry_plt_set_trace_verbosity,
1122 .set_sampling_period = telemetry_plt_set_sampling_period,
1123 .get_sampling_period = telemetry_plt_get_sampling_period,
1124 .raw_read_eventlog = telemetry_plt_raw_read_eventlog,
1125 .get_eventconfig = telemetry_plt_get_eventconfig,
1126 .update_events = telemetry_plt_update_events,
1127 .read_eventlog = telemetry_plt_read_eventlog,
1128 .reset_events = telemetry_plt_reset_events,
1129 .add_events = telemetry_plt_add_events,
1132 static int telemetry_pltdrv_probe(struct platform_device *pdev)
1134 struct resource *res0 = NULL, *res1 = NULL;
1135 const struct x86_cpu_id *id;
1136 int size, ret = -ENOMEM;
1138 id = x86_match_cpu(telemetry_cpu_ids);
1142 telm_conf = (struct telemetry_plt_config *)id->driver_data;
1144 res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1149 size = resource_size(res0);
1150 if (!devm_request_mem_region(&pdev->dev, res0->start, size,
1155 telm_conf->pss_config.ssram_base_addr = res0->start;
1156 telm_conf->pss_config.ssram_size = size;
1158 res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1163 size = resource_size(res1);
1164 if (!devm_request_mem_region(&pdev->dev, res1->start, size,
1170 telm_conf->ioss_config.ssram_base_addr = res1->start;
1171 telm_conf->ioss_config.ssram_size = size;
1173 telm_conf->pss_config.regmap = ioremap_nocache(
1174 telm_conf->pss_config.ssram_base_addr,
1175 telm_conf->pss_config.ssram_size);
1176 if (!telm_conf->pss_config.regmap) {
1181 telm_conf->ioss_config.regmap = ioremap_nocache(
1182 telm_conf->ioss_config.ssram_base_addr,
1183 telm_conf->ioss_config.ssram_size);
1184 if (!telm_conf->ioss_config.regmap) {
1189 mutex_init(&telm_conf->telem_lock);
1190 mutex_init(&telm_conf->telem_trace_lock);
1192 ret = telemetry_setup(pdev);
1196 ret = telemetry_set_pltdata(&telm_pltops, telm_conf);
1198 dev_err(&pdev->dev, "TELEMTRY Set Pltops Failed.\n");
1206 release_mem_region(res0->start, resource_size(res0));
1208 release_mem_region(res1->start, resource_size(res1));
1209 if (telm_conf->pss_config.regmap)
1210 iounmap(telm_conf->pss_config.regmap);
1211 if (telm_conf->ioss_config.regmap)
1212 iounmap(telm_conf->ioss_config.regmap);
1213 dev_err(&pdev->dev, "TELEMTRY Setup Failed.\n");
1218 static int telemetry_pltdrv_remove(struct platform_device *pdev)
1220 telemetry_clear_pltdata();
1221 iounmap(telm_conf->pss_config.regmap);
1222 iounmap(telm_conf->ioss_config.regmap);
1227 static struct platform_driver telemetry_soc_driver = {
1228 .probe = telemetry_pltdrv_probe,
1229 .remove = telemetry_pltdrv_remove,
1231 .name = DRIVER_NAME,
1235 static int __init telemetry_module_init(void)
1237 pr_info(DRIVER_NAME ": version %s loaded\n", DRIVER_VERSION);
1238 return platform_driver_register(&telemetry_soc_driver);
1241 static void __exit telemetry_module_exit(void)
1243 platform_driver_unregister(&telemetry_soc_driver);
1246 device_initcall(telemetry_module_init);
1247 module_exit(telemetry_module_exit);
1249 MODULE_AUTHOR("Souvik Kumar Chakravarty <souvik.k.chakravarty@intel.com>");
1250 MODULE_DESCRIPTION("Intel SoC Telemetry Platform Driver");
1251 MODULE_VERSION(DRIVER_VERSION);
1252 MODULE_LICENSE("GPL");