Linux Audio

Check our new training course

Loading...
v4.6
 
   1/*
   2 * Intel SOC Telemetry Platform 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 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.
  18 */
  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>
  24#include <linux/io.h>
  25#include <linux/uaccess.h>
  26#include <linux/pci.h>
  27#include <linux/suspend.h>
  28#include <linux/platform_device.h>
  29
  30#include <asm/cpu_device_id.h>
 
  31#include <asm/intel_pmc_ipc.h>
  32#include <asm/intel_punit_ipc.h>
  33#include <asm/intel_telemetry.h>
  34
  35#define DRIVER_NAME	"intel_telemetry"
  36#define DRIVER_VERSION	"1.0.0"
  37
  38#define TELEM_TRC_VERBOSITY_MASK	0x3
  39
  40#define TELEM_MIN_PERIOD(x)		((x) & 0x7F0000)
  41#define TELEM_MAX_PERIOD(x)		((x) & 0x7F000000)
  42#define TELEM_SAMPLE_PERIOD_INVALID(x)	((x) & (BIT(7)))
  43#define TELEM_CLEAR_SAMPLE_PERIOD(x)	((x) &= ~0x7F)
  44
  45#define TELEM_SAMPLING_DEFAULT_PERIOD	0xD
  46
  47#define TELEM_MAX_EVENTS_SRAM		28
  48#define TELEM_MAX_OS_ALLOCATED_EVENTS	20
  49#define TELEM_SSRAM_STARTTIME_OFFSET	8
  50#define TELEM_SSRAM_EVTLOG_OFFSET	16
  51
  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
  63
  64#define TELEM_INFO_SRAMEVTS_MASK	0xFF00
  65#define TELEM_INFO_SRAMEVTS_SHIFT	0x8
  66#define TELEM_SSRAM_READ_TIMEOUT	10
  67
  68#define TELEM_INFO_NENABLES_MASK	0xFF
  69#define TELEM_EVENT_ENABLE		0x8000
  70
  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
  75
  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))
  83
  84#define TELEM_CPU(model, data) \
  85	{ X86_VENDOR_INTEL, 6, model, X86_FEATURE_MWAIT, (unsigned long)&data }
  86
  87enum telemetry_action {
  88	TELEM_UPDATE = 0,
  89	TELEM_ADD,
  90	TELEM_RESET,
  91	TELEM_ACTION_NONE
  92};
  93
  94struct telem_ssram_region {
  95	u64 timestamp;
  96	u64 start_time;
  97	u64 events[TELEM_MAX_EVENTS_SRAM];
  98};
  99
 100static struct telemetry_plt_config *telm_conf;
 101
 102/*
 103 * The following counters are programmed by default during setup.
 104 * Only 20 allocated to kernel driver
 105 */
 106static 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},
 128};
 129
 130
 131static 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},
 138	{"PCS_LTR",				0x2801},
 139	{"PSTATES",				0x2802},
 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},
 153};
 154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 155/* APL specific Data */
 156static struct telemetry_plt_config telem_apl_config = {
 157	.pss_config = {
 158		.telem_evts = telemetry_apl_pss_default_events,
 159	},
 160	.ioss_config = {
 161		.telem_evts = telemetry_apl_ioss_default_events,
 162	},
 163};
 164
 
 
 
 
 
 
 
 
 
 
 165static const struct x86_cpu_id telemetry_cpu_ids[] = {
 166	TELEM_CPU(0x5c, telem_apl_config),
 
 167	{}
 168};
 169
 170MODULE_DEVICE_TABLE(x86cpu, telemetry_cpu_ids);
 171
 172static inline int telem_get_unitconfig(enum telemetry_unit telem_unit,
 173				     struct telemetry_unit_config **unit_config)
 174{
 175	if (telem_unit == TELEM_PSS)
 176		*unit_config = &(telm_conf->pss_config);
 177	else if (telem_unit == TELEM_IOSS)
 178		*unit_config = &(telm_conf->ioss_config);
 179	else
 180		return -EINVAL;
 181
 182	return 0;
 183
 184}
 185
 186static int telemetry_check_evtid(enum telemetry_unit telem_unit,
 187				 u32 *evtmap, u8 len,
 188				 enum telemetry_action action)
 189{
 190	struct telemetry_unit_config *unit_config;
 191	int ret;
 192
 193	ret = telem_get_unitconfig(telem_unit, &unit_config);
 194	if (ret < 0)
 195		return ret;
 196
 197	switch (action) {
 198	case TELEM_RESET:
 199		if (len > TELEM_MAX_EVENTS_SRAM)
 200			return -EINVAL;
 201
 202		break;
 203
 204	case TELEM_UPDATE:
 205		if (len > TELEM_MAX_EVENTS_SRAM)
 206			return -EINVAL;
 207
 208		if ((len > 0) && (evtmap == NULL))
 209			return -EINVAL;
 210
 211		break;
 212
 213	case TELEM_ADD:
 214		if ((len + unit_config->ssram_evts_used) >
 215		    TELEM_MAX_EVENTS_SRAM)
 216			return -EINVAL;
 217
 218		if ((len > 0) && (evtmap == NULL))
 219			return -EINVAL;
 220
 221		break;
 222
 223	default:
 224		pr_err("Unknown Telemetry action Specified %d\n", action);
 225		return -EINVAL;
 226	}
 227
 228	return 0;
 229}
 230
 231
 232static inline int telemetry_plt_config_ioss_event(u32 evt_id, int index)
 233{
 234	u32 write_buf;
 235	int ret;
 236
 237	write_buf = evt_id | TELEM_EVENT_ENABLE;
 238	write_buf <<= BITS_PER_BYTE;
 239	write_buf |= index;
 240
 241	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 242				    IOSS_TELEM_EVENT_WRITE, (u8 *)&write_buf,
 243				    IOSS_TELEM_EVT_WRITE_SIZE, NULL, 0);
 244
 245	return ret;
 246}
 247
 248static inline int telemetry_plt_config_pss_event(u32 evt_id, int index)
 249{
 250	u32 write_buf;
 251	int ret;
 252
 253	write_buf = evt_id | TELEM_EVENT_ENABLE;
 254	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT,
 255				      index, 0, &write_buf, NULL);
 256
 257	return ret;
 258}
 259
 260static int telemetry_setup_iossevtconfig(struct telemetry_evtconfig evtconfig,
 261					 enum telemetry_action action)
 262{
 263	u8 num_ioss_evts, ioss_period;
 264	int ret, index, idx;
 265	u32 *ioss_evtmap;
 266	u32 telem_ctrl;
 267
 268	num_ioss_evts = evtconfig.num_evts;
 269	ioss_period = evtconfig.period;
 270	ioss_evtmap = evtconfig.evtmap;
 271
 272	/* Get telemetry EVENT CTL */
 273	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 274				    IOSS_TELEM_EVENT_CTL_READ, NULL, 0,
 275				    &telem_ctrl, IOSS_TELEM_READ_WORD);
 276	if (ret) {
 277		pr_err("IOSS TELEM_CTRL Read Failed\n");
 278		return ret;
 279	}
 280
 281	/* Disable Telemetry */
 282	TELEM_DISABLE(telem_ctrl);
 283
 284	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 285				    IOSS_TELEM_EVENT_CTL_WRITE,
 286				    (u8 *)&telem_ctrl,
 287				    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 288				    NULL, 0);
 289	if (ret) {
 290		pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 291		return ret;
 292	}
 293
 294
 295	/* Reset Everything */
 296	if (action == TELEM_RESET) {
 297		/* Clear All Events */
 298		TELEM_CLEAR_EVENTS(telem_ctrl);
 299
 300		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 301					    IOSS_TELEM_EVENT_CTL_WRITE,
 302					    (u8 *)&telem_ctrl,
 303					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 304					    NULL, 0);
 305		if (ret) {
 306			pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 307			return ret;
 308		}
 309		telm_conf->ioss_config.ssram_evts_used = 0;
 310
 311		/* Configure Events */
 312		for (idx = 0; idx < num_ioss_evts; idx++) {
 313			if (telemetry_plt_config_ioss_event(
 314			    telm_conf->ioss_config.telem_evts[idx].evt_id,
 315			    idx)) {
 316				pr_err("IOSS TELEM_RESET Fail for data: %x\n",
 317				telm_conf->ioss_config.telem_evts[idx].evt_id);
 318				continue;
 319			}
 320			telm_conf->ioss_config.ssram_evts_used++;
 321		}
 322	}
 323
 324	/* Re-Configure Everything */
 325	if (action == TELEM_UPDATE) {
 326		/* Clear All Events */
 327		TELEM_CLEAR_EVENTS(telem_ctrl);
 328
 329		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 330					    IOSS_TELEM_EVENT_CTL_WRITE,
 331					    (u8 *)&telem_ctrl,
 332					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 333					    NULL, 0);
 334		if (ret) {
 335			pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 336			return ret;
 337		}
 338		telm_conf->ioss_config.ssram_evts_used = 0;
 339
 340		/* Configure Events */
 341		for (index = 0; index < num_ioss_evts; index++) {
 342			telm_conf->ioss_config.telem_evts[index].evt_id =
 343			ioss_evtmap[index];
 344
 345			if (telemetry_plt_config_ioss_event(
 346			    telm_conf->ioss_config.telem_evts[index].evt_id,
 347			    index)) {
 348				pr_err("IOSS TELEM_UPDATE Fail for Evt%x\n",
 349					ioss_evtmap[index]);
 350				continue;
 351			}
 352			telm_conf->ioss_config.ssram_evts_used++;
 353		}
 354	}
 355
 356	/* Add some Events */
 357	if (action == TELEM_ADD) {
 358		/* Configure Events */
 359		for (index = telm_conf->ioss_config.ssram_evts_used, idx = 0;
 360		     idx < num_ioss_evts; index++, idx++) {
 361			telm_conf->ioss_config.telem_evts[index].evt_id =
 362			ioss_evtmap[idx];
 363
 364			if (telemetry_plt_config_ioss_event(
 365			    telm_conf->ioss_config.telem_evts[index].evt_id,
 366			    index)) {
 367				pr_err("IOSS TELEM_ADD Fail for Event %x\n",
 368					ioss_evtmap[idx]);
 369				continue;
 370			}
 371			telm_conf->ioss_config.ssram_evts_used++;
 372		}
 373	}
 374
 375	/* Enable Periodic Telemetry Events and enable SRAM trace */
 376	TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 377	TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 378	TELEM_ENABLE_PERIODIC(telem_ctrl);
 379	telem_ctrl |= ioss_period;
 380
 381	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 382				    IOSS_TELEM_EVENT_CTL_WRITE,
 383				    (u8 *)&telem_ctrl,
 384				    IOSS_TELEM_EVT_CTRL_WRITE_SIZE, NULL, 0);
 385	if (ret) {
 386		pr_err("IOSS TELEM_CTRL Event Enable Write Failed\n");
 387		return ret;
 388	}
 389
 390	telm_conf->ioss_config.curr_period = ioss_period;
 391
 392	return 0;
 393}
 394
 395
 396static int telemetry_setup_pssevtconfig(struct telemetry_evtconfig evtconfig,
 397					enum telemetry_action action)
 398{
 399	u8 num_pss_evts, pss_period;
 400	int ret, index, idx;
 401	u32 *pss_evtmap;
 402	u32 telem_ctrl;
 403
 404	num_pss_evts = evtconfig.num_evts;
 405	pss_period = evtconfig.period;
 406	pss_evtmap = evtconfig.evtmap;
 407
 408	/* PSS Config */
 409	/* Get telemetry EVENT CTL */
 410	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_READ_TELE_EVENT_CTRL,
 411				      0, 0, NULL, &telem_ctrl);
 412	if (ret) {
 413		pr_err("PSS TELEM_CTRL Read Failed\n");
 414		return ret;
 415	}
 416
 417	/* Disable Telemetry */
 418	TELEM_DISABLE(telem_ctrl);
 419	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 420				      0, 0, &telem_ctrl, NULL);
 421	if (ret) {
 422		pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 423		return ret;
 424	}
 425
 426	/* Reset Everything */
 427	if (action == TELEM_RESET) {
 428		/* Clear All Events */
 429		TELEM_CLEAR_EVENTS(telem_ctrl);
 430
 431		ret = intel_punit_ipc_command(
 432				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 433				0, 0, &telem_ctrl, NULL);
 434		if (ret) {
 435			pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 436			return ret;
 437		}
 438		telm_conf->pss_config.ssram_evts_used = 0;
 439		/* Configure Events */
 440		for (idx = 0; idx < num_pss_evts; idx++) {
 441			if (telemetry_plt_config_pss_event(
 442			    telm_conf->pss_config.telem_evts[idx].evt_id,
 443			    idx)) {
 444				pr_err("PSS TELEM_RESET Fail for Event %x\n",
 445				telm_conf->pss_config.telem_evts[idx].evt_id);
 446				continue;
 447			}
 448			telm_conf->pss_config.ssram_evts_used++;
 449		}
 450	}
 451
 452	/* Re-Configure Everything */
 453	if (action == TELEM_UPDATE) {
 454		/* Clear All Events */
 455		TELEM_CLEAR_EVENTS(telem_ctrl);
 456
 457		ret = intel_punit_ipc_command(
 458				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 459				0, 0, &telem_ctrl, NULL);
 460		if (ret) {
 461			pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 462			return ret;
 463		}
 464		telm_conf->pss_config.ssram_evts_used = 0;
 465
 466		/* Configure Events */
 467		for (index = 0; index < num_pss_evts; index++) {
 468			telm_conf->pss_config.telem_evts[index].evt_id =
 469			pss_evtmap[index];
 470
 471			if (telemetry_plt_config_pss_event(
 472			    telm_conf->pss_config.telem_evts[index].evt_id,
 473			    index)) {
 474				pr_err("PSS TELEM_UPDATE Fail for Event %x\n",
 475					pss_evtmap[index]);
 476				continue;
 477			}
 478			telm_conf->pss_config.ssram_evts_used++;
 479		}
 480	}
 481
 482	/* Add some Events */
 483	if (action == TELEM_ADD) {
 484		/* Configure Events */
 485		for (index = telm_conf->pss_config.ssram_evts_used, idx = 0;
 486		     idx < num_pss_evts; index++, idx++) {
 487
 488			telm_conf->pss_config.telem_evts[index].evt_id =
 489			pss_evtmap[idx];
 490
 491			if (telemetry_plt_config_pss_event(
 492			    telm_conf->pss_config.telem_evts[index].evt_id,
 493			    index)) {
 494				pr_err("PSS TELEM_ADD Fail for Event %x\n",
 495					pss_evtmap[idx]);
 496				continue;
 497			}
 498			telm_conf->pss_config.ssram_evts_used++;
 499		}
 500	}
 501
 502	/* Enable Periodic Telemetry Events and enable SRAM trace */
 503	TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 504	TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 505	TELEM_ENABLE_PERIODIC(telem_ctrl);
 506	telem_ctrl |= pss_period;
 507
 508	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 509				      0, 0, &telem_ctrl, NULL);
 510	if (ret) {
 511		pr_err("PSS TELEM_CTRL Event Enable Write Failed\n");
 512		return ret;
 513	}
 514
 515	telm_conf->pss_config.curr_period = pss_period;
 516
 517	return 0;
 518}
 519
 520static int telemetry_setup_evtconfig(struct telemetry_evtconfig pss_evtconfig,
 521				     struct telemetry_evtconfig ioss_evtconfig,
 522				     enum telemetry_action action)
 523{
 524	int ret;
 525
 526	mutex_lock(&(telm_conf->telem_lock));
 527
 528	if ((action == TELEM_UPDATE) && (telm_conf->telem_in_use)) {
 529		ret = -EBUSY;
 530		goto out;
 531	}
 532
 533	ret = telemetry_check_evtid(TELEM_PSS, pss_evtconfig.evtmap,
 534				    pss_evtconfig.num_evts, action);
 535	if (ret)
 536		goto out;
 537
 538	ret = telemetry_check_evtid(TELEM_IOSS, ioss_evtconfig.evtmap,
 539				    ioss_evtconfig.num_evts, action);
 540	if (ret)
 541		goto out;
 542
 543	if (ioss_evtconfig.num_evts) {
 544		ret = telemetry_setup_iossevtconfig(ioss_evtconfig, action);
 545		if (ret)
 546			goto out;
 547	}
 548
 549	if (pss_evtconfig.num_evts) {
 550		ret = telemetry_setup_pssevtconfig(pss_evtconfig, action);
 551		if (ret)
 552			goto out;
 553	}
 554
 555	if ((action == TELEM_UPDATE) || (action == TELEM_ADD))
 556		telm_conf->telem_in_use = true;
 557	else
 558		telm_conf->telem_in_use = false;
 559
 560out:
 561	mutex_unlock(&(telm_conf->telem_lock));
 562	return ret;
 563}
 564
 565static int telemetry_setup(struct platform_device *pdev)
 566{
 567	struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
 568	u32 read_buf, events, event_regs;
 569	int ret;
 570
 571	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY, IOSS_TELEM_INFO_READ,
 572				    NULL, 0, &read_buf, IOSS_TELEM_READ_WORD);
 573	if (ret) {
 574		dev_err(&pdev->dev, "IOSS TELEM_INFO Read Failed\n");
 575		return ret;
 576	}
 577
 578	/* Get telemetry Info */
 579	events = (read_buf & TELEM_INFO_SRAMEVTS_MASK) >>
 580		  TELEM_INFO_SRAMEVTS_SHIFT;
 581	event_regs = read_buf & TELEM_INFO_NENABLES_MASK;
 582	if ((events < TELEM_MAX_EVENTS_SRAM) ||
 583	    (event_regs < TELEM_MAX_EVENTS_SRAM)) {
 584		dev_err(&pdev->dev, "IOSS:Insufficient Space for SRAM Trace\n");
 585		dev_err(&pdev->dev, "SRAM Events %d; Event Regs %d\n",
 586			events, event_regs);
 587		return -ENOMEM;
 588	}
 589
 590	telm_conf->ioss_config.min_period = TELEM_MIN_PERIOD(read_buf);
 591	telm_conf->ioss_config.max_period = TELEM_MAX_PERIOD(read_buf);
 592
 593	/* PUNIT Mailbox Setup */
 594	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_READ_TELE_INFO, 0, 0,
 595				      NULL, &read_buf);
 596	if (ret) {
 597		dev_err(&pdev->dev, "PSS TELEM_INFO Read Failed\n");
 598		return ret;
 599	}
 600
 601	/* Get telemetry Info */
 602	events = (read_buf & TELEM_INFO_SRAMEVTS_MASK) >>
 603		  TELEM_INFO_SRAMEVTS_SHIFT;
 604	event_regs = read_buf & TELEM_INFO_SRAMEVTS_MASK;
 605	if ((events < TELEM_MAX_EVENTS_SRAM) ||
 606	    (event_regs < TELEM_MAX_EVENTS_SRAM)) {
 607		dev_err(&pdev->dev, "PSS:Insufficient Space for SRAM Trace\n");
 608		dev_err(&pdev->dev, "SRAM Events %d; Event Regs %d\n",
 609			events, event_regs);
 610		return -ENOMEM;
 611	}
 612
 613	telm_conf->pss_config.min_period = TELEM_MIN_PERIOD(read_buf);
 614	telm_conf->pss_config.max_period = TELEM_MAX_PERIOD(read_buf);
 615
 616	pss_evtconfig.evtmap = NULL;
 617	pss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 618	pss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 619
 620	ioss_evtconfig.evtmap = NULL;
 621	ioss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 622	ioss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 623
 624	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 625					TELEM_RESET);
 626	if (ret) {
 627		dev_err(&pdev->dev, "TELEMTRY Setup Failed\n");
 628		return ret;
 629	}
 630	return 0;
 631}
 632
 633static int telemetry_plt_update_events(struct telemetry_evtconfig pss_evtconfig,
 634				struct telemetry_evtconfig ioss_evtconfig)
 635{
 636	int ret;
 637
 638	if ((pss_evtconfig.num_evts > 0) &&
 639	    (TELEM_SAMPLE_PERIOD_INVALID(pss_evtconfig.period))) {
 640		pr_err("PSS Sampling Period Out of Range\n");
 641		return -EINVAL;
 642	}
 643
 644	if ((ioss_evtconfig.num_evts > 0) &&
 645	    (TELEM_SAMPLE_PERIOD_INVALID(ioss_evtconfig.period))) {
 646		pr_err("IOSS Sampling Period Out of Range\n");
 647		return -EINVAL;
 648	}
 649
 650	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 651					TELEM_UPDATE);
 652	if (ret)
 653		pr_err("TELEMTRY Config Failed\n");
 654
 655	return ret;
 656}
 657
 658
 659static int telemetry_plt_set_sampling_period(u8 pss_period, u8 ioss_period)
 660{
 661	u32 telem_ctrl = 0;
 662	int ret = 0;
 663
 664	mutex_lock(&(telm_conf->telem_lock));
 665	if (ioss_period) {
 666		if (TELEM_SAMPLE_PERIOD_INVALID(ioss_period)) {
 667			pr_err("IOSS Sampling Period Out of Range\n");
 668			ret = -EINVAL;
 669			goto out;
 670		}
 671
 672		/* Get telemetry EVENT CTL */
 673		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 674					    IOSS_TELEM_EVENT_CTL_READ, NULL, 0,
 675					    &telem_ctrl, IOSS_TELEM_READ_WORD);
 676		if (ret) {
 677			pr_err("IOSS TELEM_CTRL Read Failed\n");
 678			goto out;
 679		}
 680
 681		/* Disable Telemetry */
 682		TELEM_DISABLE(telem_ctrl);
 683
 684		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 685					    IOSS_TELEM_EVENT_CTL_WRITE,
 686					    (u8 *)&telem_ctrl,
 687					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 688					    NULL, 0);
 689		if (ret) {
 690			pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 691			goto out;
 692		}
 693
 694		/* Enable Periodic Telemetry Events and enable SRAM trace */
 695		TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 696		TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 697		TELEM_ENABLE_PERIODIC(telem_ctrl);
 698		telem_ctrl |= ioss_period;
 699
 700		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 701					    IOSS_TELEM_EVENT_CTL_WRITE,
 702					    (u8 *)&telem_ctrl,
 703					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 704					    NULL, 0);
 705		if (ret) {
 706			pr_err("IOSS TELEM_CTRL Event Enable Write Failed\n");
 707			goto out;
 708		}
 709		telm_conf->ioss_config.curr_period = ioss_period;
 710	}
 711
 712	if (pss_period) {
 713		if (TELEM_SAMPLE_PERIOD_INVALID(pss_period)) {
 714			pr_err("PSS Sampling Period Out of Range\n");
 715			ret = -EINVAL;
 716			goto out;
 717		}
 718
 719		/* Get telemetry EVENT CTL */
 720		ret = intel_punit_ipc_command(
 721				IPC_PUNIT_BIOS_READ_TELE_EVENT_CTRL,
 722				0, 0, NULL, &telem_ctrl);
 723		if (ret) {
 724			pr_err("PSS TELEM_CTRL Read Failed\n");
 725			goto out;
 726		}
 727
 728		/* Disable Telemetry */
 729		TELEM_DISABLE(telem_ctrl);
 730		ret = intel_punit_ipc_command(
 731				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 732				0, 0, &telem_ctrl, NULL);
 733		if (ret) {
 734			pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 735			goto out;
 736		}
 737
 738		/* Enable Periodic Telemetry Events and enable SRAM trace */
 739		TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 740		TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 741		TELEM_ENABLE_PERIODIC(telem_ctrl);
 742		telem_ctrl |= pss_period;
 743
 744		ret = intel_punit_ipc_command(
 745				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 746				0, 0, &telem_ctrl, NULL);
 747		if (ret) {
 748			pr_err("PSS TELEM_CTRL Event Enable Write Failed\n");
 749			goto out;
 750		}
 751		telm_conf->pss_config.curr_period = pss_period;
 752	}
 753
 754out:
 755	mutex_unlock(&(telm_conf->telem_lock));
 756	return ret;
 757}
 758
 759
 760static int telemetry_plt_get_sampling_period(u8 *pss_min_period,
 761					     u8 *pss_max_period,
 762					     u8 *ioss_min_period,
 763					     u8 *ioss_max_period)
 764{
 765	*pss_min_period = telm_conf->pss_config.min_period;
 766	*pss_max_period = telm_conf->pss_config.max_period;
 767	*ioss_min_period = telm_conf->ioss_config.min_period;
 768	*ioss_max_period = telm_conf->ioss_config.max_period;
 769
 770	return 0;
 771}
 772
 773
 774static int telemetry_plt_reset_events(void)
 775{
 776	struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
 777	int ret;
 778
 779	pss_evtconfig.evtmap = NULL;
 780	pss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 781	pss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 782
 783	ioss_evtconfig.evtmap = NULL;
 784	ioss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 785	ioss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 786
 787	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 788					TELEM_RESET);
 789	if (ret)
 790		pr_err("TELEMTRY Reset Failed\n");
 791
 792	return ret;
 793}
 794
 795
 796static int telemetry_plt_get_eventconfig(struct telemetry_evtconfig *pss_config,
 797					struct telemetry_evtconfig *ioss_config,
 798					int pss_len, int ioss_len)
 799{
 800	u32 *pss_evtmap, *ioss_evtmap;
 801	u32 index;
 802
 803	pss_evtmap = pss_config->evtmap;
 804	ioss_evtmap = ioss_config->evtmap;
 805
 806	mutex_lock(&(telm_conf->telem_lock));
 807	pss_config->num_evts = telm_conf->pss_config.ssram_evts_used;
 808	ioss_config->num_evts = telm_conf->ioss_config.ssram_evts_used;
 809
 810	pss_config->period = telm_conf->pss_config.curr_period;
 811	ioss_config->period = telm_conf->ioss_config.curr_period;
 812
 813	if ((pss_len < telm_conf->pss_config.ssram_evts_used) ||
 814	    (ioss_len < telm_conf->ioss_config.ssram_evts_used)) {
 815		mutex_unlock(&(telm_conf->telem_lock));
 816		return -EINVAL;
 817	}
 818
 819	for (index = 0; index < telm_conf->pss_config.ssram_evts_used;
 820	     index++) {
 821		pss_evtmap[index] =
 822		telm_conf->pss_config.telem_evts[index].evt_id;
 823	}
 824
 825	for (index = 0; index < telm_conf->ioss_config.ssram_evts_used;
 826	     index++) {
 827		ioss_evtmap[index] =
 828		telm_conf->ioss_config.telem_evts[index].evt_id;
 829	}
 830
 831	mutex_unlock(&(telm_conf->telem_lock));
 832	return 0;
 833}
 834
 835
 836static int telemetry_plt_add_events(u8 num_pss_evts, u8 num_ioss_evts,
 837				    u32 *pss_evtmap, u32 *ioss_evtmap)
 838{
 839	struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
 840	int ret;
 841
 842	pss_evtconfig.evtmap = pss_evtmap;
 843	pss_evtconfig.num_evts = num_pss_evts;
 844	pss_evtconfig.period = telm_conf->pss_config.curr_period;
 845
 846	ioss_evtconfig.evtmap = ioss_evtmap;
 847	ioss_evtconfig.num_evts = num_ioss_evts;
 848	ioss_evtconfig.period = telm_conf->ioss_config.curr_period;
 849
 850	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 851					TELEM_ADD);
 852	if (ret)
 853		pr_err("TELEMTRY ADD Failed\n");
 854
 855	return ret;
 856}
 857
 858static int telem_evtlog_read(enum telemetry_unit telem_unit,
 859			     struct telem_ssram_region *ssram_region, u8 len)
 860{
 861	struct telemetry_unit_config *unit_config;
 862	u64 timestamp_prev, timestamp_next;
 863	int ret, index, timeout = 0;
 864
 865	ret = telem_get_unitconfig(telem_unit, &unit_config);
 866	if (ret < 0)
 867		return ret;
 868
 869	if (len > unit_config->ssram_evts_used)
 870		len = unit_config->ssram_evts_used;
 871
 872	do {
 873		timestamp_prev = readq(unit_config->regmap);
 874		if (!timestamp_prev) {
 875			pr_err("Ssram under update. Please Try Later\n");
 876			return -EBUSY;
 877		}
 878
 879		ssram_region->start_time = readq(unit_config->regmap +
 880						 TELEM_SSRAM_STARTTIME_OFFSET);
 881
 882		for (index = 0; index < len; index++) {
 883			ssram_region->events[index] =
 884			readq(unit_config->regmap + TELEM_SSRAM_EVTLOG_OFFSET +
 885			      BYTES_PER_LONG*index);
 886		}
 887
 888		timestamp_next = readq(unit_config->regmap);
 889		if (!timestamp_next) {
 890			pr_err("Ssram under update. Please Try Later\n");
 891			return -EBUSY;
 892		}
 893
 894		if (timeout++ > TELEM_SSRAM_READ_TIMEOUT) {
 895			pr_err("Timeout while reading Events\n");
 896			return -EBUSY;
 897		}
 898
 899	} while (timestamp_prev != timestamp_next);
 900
 901	ssram_region->timestamp = timestamp_next;
 902
 903	return len;
 904}
 905
 906static int telemetry_plt_raw_read_eventlog(enum telemetry_unit telem_unit,
 907					   struct telemetry_evtlog *evtlog,
 908					   int len, int log_all_evts)
 909{
 910	int index, idx1, ret, readlen = len;
 911	struct telem_ssram_region ssram_region;
 912	struct telemetry_evtmap *evtmap;
 913
 914	switch (telem_unit)	{
 915	case TELEM_PSS:
 916		evtmap = telm_conf->pss_config.telem_evts;
 917		break;
 918
 919	case TELEM_IOSS:
 920		evtmap = telm_conf->ioss_config.telem_evts;
 921		break;
 922
 923	default:
 924		pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
 925		return -EINVAL;
 926	}
 927
 928	if (!log_all_evts)
 929		readlen = TELEM_MAX_EVENTS_SRAM;
 930
 931	ret = telem_evtlog_read(telem_unit, &ssram_region, readlen);
 932	if (ret < 0)
 933		return ret;
 934
 935	/* Invalid evt-id array specified via length mismatch */
 936	if ((!log_all_evts) && (len > ret))
 937		return -EINVAL;
 938
 939	if (log_all_evts)
 940		for (index = 0; index < ret; index++) {
 941			evtlog[index].telem_evtlog = ssram_region.events[index];
 942			evtlog[index].telem_evtid = evtmap[index].evt_id;
 943		}
 944	else
 945		for (index = 0, readlen = 0; (index < ret) && (readlen < len);
 946		     index++) {
 947			for (idx1 = 0; idx1 < len; idx1++) {
 948				/* Elements matched */
 949				if (evtmap[index].evt_id ==
 950				    evtlog[idx1].telem_evtid) {
 951					evtlog[idx1].telem_evtlog =
 952					ssram_region.events[index];
 953					readlen++;
 954
 955					break;
 956				}
 957			}
 958		}
 959
 960	return readlen;
 961}
 962
 963static int telemetry_plt_read_eventlog(enum telemetry_unit telem_unit,
 964		struct telemetry_evtlog *evtlog, int len, int log_all_evts)
 965{
 966	int ret;
 967
 968	mutex_lock(&(telm_conf->telem_lock));
 969	ret = telemetry_plt_raw_read_eventlog(telem_unit, evtlog,
 970					      len, log_all_evts);
 971	mutex_unlock(&(telm_conf->telem_lock));
 972
 973	return ret;
 974}
 975
 976static int telemetry_plt_get_trace_verbosity(enum telemetry_unit telem_unit,
 977					     u32 *verbosity)
 978{
 979	u32 temp = 0;
 980	int ret;
 981
 982	if (verbosity == NULL)
 983		return -EINVAL;
 984
 985	mutex_lock(&(telm_conf->telem_trace_lock));
 986	switch (telem_unit) {
 987	case TELEM_PSS:
 988		ret = intel_punit_ipc_command(
 989				IPC_PUNIT_BIOS_READ_TELE_TRACE_CTRL,
 990				0, 0, NULL, &temp);
 991		if (ret) {
 992			pr_err("PSS TRACE_CTRL Read Failed\n");
 993			goto out;
 994		}
 995
 996		break;
 997
 998	case TELEM_IOSS:
 999		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1000				IOSS_TELEM_TRACE_CTL_READ, NULL, 0, &temp,
1001				IOSS_TELEM_READ_WORD);
1002		if (ret) {
1003			pr_err("IOSS TRACE_CTL Read Failed\n");
1004			goto out;
1005		}
1006
1007		break;
1008
1009	default:
1010		pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
1011		ret = -EINVAL;
1012		break;
1013	}
1014	TELEM_EXTRACT_VERBOSITY(temp, *verbosity);
1015
1016out:
1017	mutex_unlock(&(telm_conf->telem_trace_lock));
1018	return ret;
1019}
1020
1021static int telemetry_plt_set_trace_verbosity(enum telemetry_unit telem_unit,
1022					     u32 verbosity)
1023{
1024	u32 temp = 0;
1025	int ret;
1026
1027	verbosity &= TELEM_TRC_VERBOSITY_MASK;
1028
1029	mutex_lock(&(telm_conf->telem_trace_lock));
1030	switch (telem_unit) {
1031	case TELEM_PSS:
1032		ret = intel_punit_ipc_command(
1033				IPC_PUNIT_BIOS_READ_TELE_TRACE_CTRL,
1034				0, 0, NULL, &temp);
1035		if (ret) {
1036			pr_err("PSS TRACE_CTRL Read Failed\n");
1037			goto out;
1038		}
1039
1040		TELEM_CLEAR_VERBOSITY_BITS(temp);
1041		TELEM_SET_VERBOSITY_BITS(temp, verbosity);
1042
1043		ret = intel_punit_ipc_command(
1044				IPC_PUNIT_BIOS_WRITE_TELE_TRACE_CTRL,
1045				0, 0, &temp, NULL);
1046		if (ret) {
1047			pr_err("PSS TRACE_CTRL Verbosity Set Failed\n");
1048			goto out;
1049		}
1050		break;
1051
1052	case TELEM_IOSS:
1053		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1054				IOSS_TELEM_TRACE_CTL_READ, NULL, 0, &temp,
1055				IOSS_TELEM_READ_WORD);
1056		if (ret) {
1057			pr_err("IOSS TRACE_CTL Read Failed\n");
1058			goto out;
1059		}
1060
1061		TELEM_CLEAR_VERBOSITY_BITS(temp);
1062		TELEM_SET_VERBOSITY_BITS(temp, verbosity);
1063
1064		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1065				IOSS_TELEM_TRACE_CTL_WRITE, (u8 *)&temp,
1066				IOSS_TELEM_WRITE_FOURBYTES, NULL, 0);
1067		if (ret) {
1068			pr_err("IOSS TRACE_CTL Verbosity Set Failed\n");
1069			goto out;
1070		}
1071		break;
1072
1073	default:
1074		pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
1075		ret = -EINVAL;
1076		break;
1077	}
1078
1079out:
1080	mutex_unlock(&(telm_conf->telem_trace_lock));
1081	return ret;
1082}
1083
1084static struct telemetry_core_ops telm_pltops = {
1085	.get_trace_verbosity = telemetry_plt_get_trace_verbosity,
1086	.set_trace_verbosity = telemetry_plt_set_trace_verbosity,
1087	.set_sampling_period = telemetry_plt_set_sampling_period,
1088	.get_sampling_period = telemetry_plt_get_sampling_period,
1089	.raw_read_eventlog = telemetry_plt_raw_read_eventlog,
1090	.get_eventconfig = telemetry_plt_get_eventconfig,
1091	.update_events = telemetry_plt_update_events,
1092	.read_eventlog = telemetry_plt_read_eventlog,
1093	.reset_events = telemetry_plt_reset_events,
1094	.add_events = telemetry_plt_add_events,
1095};
1096
1097static int telemetry_pltdrv_probe(struct platform_device *pdev)
1098{
1099	struct resource *res0 = NULL, *res1 = NULL;
1100	const struct x86_cpu_id *id;
1101	int size, ret = -ENOMEM;
1102
1103	id = x86_match_cpu(telemetry_cpu_ids);
1104	if (!id)
1105		return -ENODEV;
1106
1107	telm_conf = (struct telemetry_plt_config *)id->driver_data;
1108
1109	res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1110	if (!res0) {
1111		ret = -EINVAL;
1112		goto out;
1113	}
1114	size = resource_size(res0);
1115	if (!devm_request_mem_region(&pdev->dev, res0->start, size,
1116				     pdev->name)) {
1117		ret = -EBUSY;
1118		goto out;
1119	}
1120	telm_conf->pss_config.ssram_base_addr = res0->start;
1121	telm_conf->pss_config.ssram_size = size;
1122
1123	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1124	if (!res1) {
1125		ret = -EINVAL;
1126		goto out;
1127	}
1128	size = resource_size(res1);
1129	if (!devm_request_mem_region(&pdev->dev, res1->start, size,
1130				     pdev->name)) {
1131		ret = -EBUSY;
1132		goto out;
1133	}
1134
1135	telm_conf->ioss_config.ssram_base_addr = res1->start;
1136	telm_conf->ioss_config.ssram_size = size;
1137
1138	telm_conf->pss_config.regmap = ioremap_nocache(
1139					telm_conf->pss_config.ssram_base_addr,
1140					telm_conf->pss_config.ssram_size);
1141	if (!telm_conf->pss_config.regmap) {
1142		ret = -ENOMEM;
1143		goto out;
1144	}
1145
1146	telm_conf->ioss_config.regmap = ioremap_nocache(
1147				telm_conf->ioss_config.ssram_base_addr,
1148				telm_conf->ioss_config.ssram_size);
1149	if (!telm_conf->ioss_config.regmap) {
1150		ret = -ENOMEM;
1151		goto out;
1152	}
1153
1154	mutex_init(&telm_conf->telem_lock);
1155	mutex_init(&telm_conf->telem_trace_lock);
1156
1157	ret = telemetry_setup(pdev);
1158	if (ret)
1159		goto out;
1160
1161	ret = telemetry_set_pltdata(&telm_pltops, telm_conf);
1162	if (ret) {
1163		dev_err(&pdev->dev, "TELEMTRY Set Pltops Failed.\n");
1164		goto out;
1165	}
1166
1167	return 0;
1168
1169out:
1170	if (res0)
1171		release_mem_region(res0->start, resource_size(res0));
1172	if (res1)
1173		release_mem_region(res1->start, resource_size(res1));
1174	if (telm_conf->pss_config.regmap)
1175		iounmap(telm_conf->pss_config.regmap);
1176	if (telm_conf->ioss_config.regmap)
1177		iounmap(telm_conf->ioss_config.regmap);
1178	dev_err(&pdev->dev, "TELEMTRY Setup Failed.\n");
1179
1180	return ret;
1181}
1182
1183static int telemetry_pltdrv_remove(struct platform_device *pdev)
1184{
1185	telemetry_clear_pltdata();
1186	iounmap(telm_conf->pss_config.regmap);
1187	iounmap(telm_conf->ioss_config.regmap);
1188
1189	return 0;
1190}
1191
1192static struct platform_driver telemetry_soc_driver = {
1193	.probe		= telemetry_pltdrv_probe,
1194	.remove		= telemetry_pltdrv_remove,
1195	.driver		= {
1196		.name	= DRIVER_NAME,
1197	},
1198};
1199
1200static int __init telemetry_module_init(void)
1201{
1202	pr_info(DRIVER_NAME ": version %s loaded\n", DRIVER_VERSION);
1203	return platform_driver_register(&telemetry_soc_driver);
1204}
1205
1206static void __exit telemetry_module_exit(void)
1207{
1208	platform_driver_unregister(&telemetry_soc_driver);
1209}
1210
1211device_initcall(telemetry_module_init);
1212module_exit(telemetry_module_exit);
1213
1214MODULE_AUTHOR("Souvik Kumar Chakravarty <souvik.k.chakravarty@intel.com>");
1215MODULE_DESCRIPTION("Intel SoC Telemetry Platform Driver");
1216MODULE_VERSION(DRIVER_VERSION);
1217MODULE_LICENSE("GPL");
v5.4
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Intel SOC Telemetry Platform Driver: Currently supports APL
   4 * Copyright (c) 2015, Intel Corporation.
   5 * All Rights Reserved.
   6 *
 
 
 
 
 
 
 
 
 
   7 * This file provides the platform specific telemetry implementation for APL.
   8 * It used the PUNIT and PMC IPC interfaces for configuring the counters.
   9 * The accumulated results are fetched from SRAM.
  10 */
  11
 
 
 
 
  12#include <linux/io.h>
  13#include <linux/module.h>
 
 
  14#include <linux/platform_device.h>
  15
  16#include <asm/cpu_device_id.h>
  17#include <asm/intel-family.h>
  18#include <asm/intel_pmc_ipc.h>
  19#include <asm/intel_punit_ipc.h>
  20#include <asm/intel_telemetry.h>
  21
  22#define DRIVER_NAME	"intel_telemetry"
  23#define DRIVER_VERSION	"1.0.0"
  24
  25#define TELEM_TRC_VERBOSITY_MASK	0x3
  26
  27#define TELEM_MIN_PERIOD(x)		((x) & 0x7F0000)
  28#define TELEM_MAX_PERIOD(x)		((x) & 0x7F000000)
  29#define TELEM_SAMPLE_PERIOD_INVALID(x)	((x) & (BIT(7)))
  30#define TELEM_CLEAR_SAMPLE_PERIOD(x)	((x) &= ~0x7F)
  31
  32#define TELEM_SAMPLING_DEFAULT_PERIOD	0xD
  33
  34#define TELEM_MAX_EVENTS_SRAM		28
 
  35#define TELEM_SSRAM_STARTTIME_OFFSET	8
  36#define TELEM_SSRAM_EVTLOG_OFFSET	16
  37
  38#define IOSS_TELEM_EVENT_READ		0x0
  39#define IOSS_TELEM_EVENT_WRITE		0x1
  40#define IOSS_TELEM_INFO_READ		0x2
  41#define IOSS_TELEM_TRACE_CTL_READ	0x5
  42#define IOSS_TELEM_TRACE_CTL_WRITE	0x6
  43#define IOSS_TELEM_EVENT_CTL_READ	0x7
  44#define IOSS_TELEM_EVENT_CTL_WRITE	0x8
  45#define IOSS_TELEM_EVT_CTRL_WRITE_SIZE	0x4
  46#define IOSS_TELEM_READ_WORD		0x1
  47#define IOSS_TELEM_WRITE_FOURBYTES	0x4
  48#define IOSS_TELEM_EVT_WRITE_SIZE	0x3
  49
  50#define TELEM_INFO_SRAMEVTS_MASK	0xFF00
  51#define TELEM_INFO_SRAMEVTS_SHIFT	0x8
  52#define TELEM_SSRAM_READ_TIMEOUT	10
  53
  54#define TELEM_INFO_NENABLES_MASK	0xFF
  55#define TELEM_EVENT_ENABLE		0x8000
  56
  57#define TELEM_MASK_BIT			1
  58#define TELEM_MASK_BYTE			0xFF
  59#define BYTES_PER_LONG			8
  60#define TELEM_MASK_PCS_STATE		0xF
  61
  62#define TELEM_DISABLE(x)		((x) &= ~(BIT(31)))
  63#define TELEM_CLEAR_EVENTS(x)		((x) |= (BIT(30)))
  64#define TELEM_ENABLE_SRAM_EVT_TRACE(x)	((x) &= ~(BIT(30) | BIT(24)))
  65#define TELEM_ENABLE_PERIODIC(x)	((x) |= (BIT(23) | BIT(31) | BIT(7)))
  66#define TELEM_EXTRACT_VERBOSITY(x, y)	((y) = (((x) >> 27) & 0x3))
  67#define TELEM_CLEAR_VERBOSITY_BITS(x)	((x) &= ~(BIT(27) | BIT(28)))
  68#define TELEM_SET_VERBOSITY_BITS(x, y)	((x) |= ((y) << 27))
  69
  70#define TELEM_CPU(model, data) \
  71	{ X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&data }
  72
  73enum telemetry_action {
  74	TELEM_UPDATE = 0,
  75	TELEM_ADD,
  76	TELEM_RESET,
  77	TELEM_ACTION_NONE
  78};
  79
  80struct telem_ssram_region {
  81	u64 timestamp;
  82	u64 start_time;
  83	u64 events[TELEM_MAX_EVENTS_SRAM];
  84};
  85
  86static struct telemetry_plt_config *telm_conf;
  87
  88/*
  89 * The following counters are programmed by default during setup.
  90 * Only 20 allocated to kernel driver
  91 */
  92static struct telemetry_evtmap
  93	telemetry_apl_ioss_default_events[TELEM_MAX_OS_ALLOCATED_EVENTS] = {
  94	{"SOC_S0IX_TOTAL_RES",			0x4800},
  95	{"SOC_S0IX_TOTAL_OCC",			0x4000},
  96	{"SOC_S0IX_SHALLOW_RES",		0x4801},
  97	{"SOC_S0IX_SHALLOW_OCC",		0x4001},
  98	{"SOC_S0IX_DEEP_RES",			0x4802},
  99	{"SOC_S0IX_DEEP_OCC",			0x4002},
 100	{"PMC_POWER_GATE",			0x5818},
 101	{"PMC_D3_STATES",			0x5819},
 102	{"PMC_D0I3_STATES",			0x581A},
 103	{"PMC_S0IX_WAKE_REASON_GPIO",		0x6000},
 104	{"PMC_S0IX_WAKE_REASON_TIMER",		0x6001},
 105	{"PMC_S0IX_WAKE_REASON_VNNREQ",         0x6002},
 106	{"PMC_S0IX_WAKE_REASON_LOWPOWER",       0x6003},
 107	{"PMC_S0IX_WAKE_REASON_EXTERNAL",       0x6004},
 108	{"PMC_S0IX_WAKE_REASON_MISC",           0x6005},
 109	{"PMC_S0IX_BLOCKING_IPS_D3_D0I3",       0x6006},
 110	{"PMC_S0IX_BLOCKING_IPS_PG",            0x6007},
 111	{"PMC_S0IX_BLOCKING_MISC_IPS_PG",       0x6008},
 112	{"PMC_S0IX_BLOCK_IPS_VNN_REQ",          0x6009},
 113	{"PMC_S0IX_BLOCK_IPS_CLOCKS",           0x600B},
 114};
 115
 116
 117static struct telemetry_evtmap
 118	telemetry_apl_pss_default_events[TELEM_MAX_OS_ALLOCATED_EVENTS] = {
 119	{"IA_CORE0_C6_RES",			0x0400},
 120	{"IA_CORE0_C6_CTR",			0x0000},
 121	{"IA_MODULE0_C7_RES",			0x0410},
 122	{"IA_MODULE0_C7_CTR",			0x000E},
 123	{"IA_C0_RES",				0x0805},
 124	{"PCS_LTR",				0x2801},
 125	{"PSTATES",				0x2802},
 126	{"SOC_S0I3_RES",			0x0409},
 127	{"SOC_S0I3_CTR",			0x000A},
 128	{"PCS_S0I3_CTR",			0x0009},
 129	{"PCS_C1E_RES",				0x041A},
 130	{"PCS_IDLE_STATUS",			0x2806},
 131	{"IA_PERF_LIMITS",			0x280B},
 132	{"GT_PERF_LIMITS",			0x280C},
 133	{"PCS_WAKEUP_S0IX_CTR",			0x0030},
 134	{"PCS_IDLE_BLOCKED",			0x2C00},
 135	{"PCS_S0IX_BLOCKED",			0x2C01},
 136	{"PCS_S0IX_WAKE_REASONS",		0x2C02},
 137	{"PCS_LTR_BLOCKING",			0x2C03},
 138	{"PC2_AND_MEM_SHALLOW_IDLE_RES",	0x1D40},
 139};
 140
 141static struct telemetry_evtmap
 142	telemetry_glk_pss_default_events[TELEM_MAX_OS_ALLOCATED_EVENTS] = {
 143	{"IA_CORE0_C6_RES",			0x0400},
 144	{"IA_CORE0_C6_CTR",			0x0000},
 145	{"IA_MODULE0_C7_RES",			0x0410},
 146	{"IA_MODULE0_C7_CTR",			0x000C},
 147	{"IA_C0_RES",				0x0805},
 148	{"PCS_LTR",				0x2801},
 149	{"PSTATES",				0x2802},
 150	{"SOC_S0I3_RES",			0x0407},
 151	{"SOC_S0I3_CTR",			0x0008},
 152	{"PCS_S0I3_CTR",			0x0007},
 153	{"PCS_C1E_RES",				0x0414},
 154	{"PCS_IDLE_STATUS",			0x2806},
 155	{"IA_PERF_LIMITS",			0x280B},
 156	{"GT_PERF_LIMITS",			0x280C},
 157	{"PCS_WAKEUP_S0IX_CTR",			0x0025},
 158	{"PCS_IDLE_BLOCKED",			0x2C00},
 159	{"PCS_S0IX_BLOCKED",			0x2C01},
 160	{"PCS_S0IX_WAKE_REASONS",		0x2C02},
 161	{"PCS_LTR_BLOCKING",			0x2C03},
 162	{"PC2_AND_MEM_SHALLOW_IDLE_RES",	0x1D40},
 163};
 164
 165/* APL specific Data */
 166static struct telemetry_plt_config telem_apl_config = {
 167	.pss_config = {
 168		.telem_evts = telemetry_apl_pss_default_events,
 169	},
 170	.ioss_config = {
 171		.telem_evts = telemetry_apl_ioss_default_events,
 172	},
 173};
 174
 175/* GLK specific Data */
 176static struct telemetry_plt_config telem_glk_config = {
 177	.pss_config = {
 178		.telem_evts = telemetry_glk_pss_default_events,
 179	},
 180	.ioss_config = {
 181		.telem_evts = telemetry_apl_ioss_default_events,
 182	},
 183};
 184
 185static const struct x86_cpu_id telemetry_cpu_ids[] = {
 186	TELEM_CPU(INTEL_FAM6_ATOM_GOLDMONT, telem_apl_config),
 187	TELEM_CPU(INTEL_FAM6_ATOM_GOLDMONT_PLUS, telem_glk_config),
 188	{}
 189};
 190
 191MODULE_DEVICE_TABLE(x86cpu, telemetry_cpu_ids);
 192
 193static inline int telem_get_unitconfig(enum telemetry_unit telem_unit,
 194				     struct telemetry_unit_config **unit_config)
 195{
 196	if (telem_unit == TELEM_PSS)
 197		*unit_config = &(telm_conf->pss_config);
 198	else if (telem_unit == TELEM_IOSS)
 199		*unit_config = &(telm_conf->ioss_config);
 200	else
 201		return -EINVAL;
 202
 203	return 0;
 204
 205}
 206
 207static int telemetry_check_evtid(enum telemetry_unit telem_unit,
 208				 u32 *evtmap, u8 len,
 209				 enum telemetry_action action)
 210{
 211	struct telemetry_unit_config *unit_config;
 212	int ret;
 213
 214	ret = telem_get_unitconfig(telem_unit, &unit_config);
 215	if (ret < 0)
 216		return ret;
 217
 218	switch (action) {
 219	case TELEM_RESET:
 220		if (len > TELEM_MAX_EVENTS_SRAM)
 221			return -EINVAL;
 222
 223		break;
 224
 225	case TELEM_UPDATE:
 226		if (len > TELEM_MAX_EVENTS_SRAM)
 227			return -EINVAL;
 228
 229		if ((len > 0) && (evtmap == NULL))
 230			return -EINVAL;
 231
 232		break;
 233
 234	case TELEM_ADD:
 235		if ((len + unit_config->ssram_evts_used) >
 236		    TELEM_MAX_EVENTS_SRAM)
 237			return -EINVAL;
 238
 239		if ((len > 0) && (evtmap == NULL))
 240			return -EINVAL;
 241
 242		break;
 243
 244	default:
 245		pr_err("Unknown Telemetry action specified %d\n", action);
 246		return -EINVAL;
 247	}
 248
 249	return 0;
 250}
 251
 252
 253static inline int telemetry_plt_config_ioss_event(u32 evt_id, int index)
 254{
 255	u32 write_buf;
 256	int ret;
 257
 258	write_buf = evt_id | TELEM_EVENT_ENABLE;
 259	write_buf <<= BITS_PER_BYTE;
 260	write_buf |= index;
 261
 262	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 263				    IOSS_TELEM_EVENT_WRITE, (u8 *)&write_buf,
 264				    IOSS_TELEM_EVT_WRITE_SIZE, NULL, 0);
 265
 266	return ret;
 267}
 268
 269static inline int telemetry_plt_config_pss_event(u32 evt_id, int index)
 270{
 271	u32 write_buf;
 272	int ret;
 273
 274	write_buf = evt_id | TELEM_EVENT_ENABLE;
 275	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT,
 276				      index, 0, &write_buf, NULL);
 277
 278	return ret;
 279}
 280
 281static int telemetry_setup_iossevtconfig(struct telemetry_evtconfig evtconfig,
 282					 enum telemetry_action action)
 283{
 284	u8 num_ioss_evts, ioss_period;
 285	int ret, index, idx;
 286	u32 *ioss_evtmap;
 287	u32 telem_ctrl;
 288
 289	num_ioss_evts = evtconfig.num_evts;
 290	ioss_period = evtconfig.period;
 291	ioss_evtmap = evtconfig.evtmap;
 292
 293	/* Get telemetry EVENT CTL */
 294	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 295				    IOSS_TELEM_EVENT_CTL_READ, NULL, 0,
 296				    &telem_ctrl, IOSS_TELEM_READ_WORD);
 297	if (ret) {
 298		pr_err("IOSS TELEM_CTRL Read Failed\n");
 299		return ret;
 300	}
 301
 302	/* Disable Telemetry */
 303	TELEM_DISABLE(telem_ctrl);
 304
 305	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 306				    IOSS_TELEM_EVENT_CTL_WRITE,
 307				    (u8 *)&telem_ctrl,
 308				    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 309				    NULL, 0);
 310	if (ret) {
 311		pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 312		return ret;
 313	}
 314
 315
 316	/* Reset Everything */
 317	if (action == TELEM_RESET) {
 318		/* Clear All Events */
 319		TELEM_CLEAR_EVENTS(telem_ctrl);
 320
 321		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 322					    IOSS_TELEM_EVENT_CTL_WRITE,
 323					    (u8 *)&telem_ctrl,
 324					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 325					    NULL, 0);
 326		if (ret) {
 327			pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 328			return ret;
 329		}
 330		telm_conf->ioss_config.ssram_evts_used = 0;
 331
 332		/* Configure Events */
 333		for (idx = 0; idx < num_ioss_evts; idx++) {
 334			if (telemetry_plt_config_ioss_event(
 335			    telm_conf->ioss_config.telem_evts[idx].evt_id,
 336			    idx)) {
 337				pr_err("IOSS TELEM_RESET Fail for data: %x\n",
 338				telm_conf->ioss_config.telem_evts[idx].evt_id);
 339				continue;
 340			}
 341			telm_conf->ioss_config.ssram_evts_used++;
 342		}
 343	}
 344
 345	/* Re-Configure Everything */
 346	if (action == TELEM_UPDATE) {
 347		/* Clear All Events */
 348		TELEM_CLEAR_EVENTS(telem_ctrl);
 349
 350		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 351					    IOSS_TELEM_EVENT_CTL_WRITE,
 352					    (u8 *)&telem_ctrl,
 353					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 354					    NULL, 0);
 355		if (ret) {
 356			pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 357			return ret;
 358		}
 359		telm_conf->ioss_config.ssram_evts_used = 0;
 360
 361		/* Configure Events */
 362		for (index = 0; index < num_ioss_evts; index++) {
 363			telm_conf->ioss_config.telem_evts[index].evt_id =
 364			ioss_evtmap[index];
 365
 366			if (telemetry_plt_config_ioss_event(
 367			    telm_conf->ioss_config.telem_evts[index].evt_id,
 368			    index)) {
 369				pr_err("IOSS TELEM_UPDATE Fail for Evt%x\n",
 370					ioss_evtmap[index]);
 371				continue;
 372			}
 373			telm_conf->ioss_config.ssram_evts_used++;
 374		}
 375	}
 376
 377	/* Add some Events */
 378	if (action == TELEM_ADD) {
 379		/* Configure Events */
 380		for (index = telm_conf->ioss_config.ssram_evts_used, idx = 0;
 381		     idx < num_ioss_evts; index++, idx++) {
 382			telm_conf->ioss_config.telem_evts[index].evt_id =
 383			ioss_evtmap[idx];
 384
 385			if (telemetry_plt_config_ioss_event(
 386			    telm_conf->ioss_config.telem_evts[index].evt_id,
 387			    index)) {
 388				pr_err("IOSS TELEM_ADD Fail for Event %x\n",
 389					ioss_evtmap[idx]);
 390				continue;
 391			}
 392			telm_conf->ioss_config.ssram_evts_used++;
 393		}
 394	}
 395
 396	/* Enable Periodic Telemetry Events and enable SRAM trace */
 397	TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 398	TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 399	TELEM_ENABLE_PERIODIC(telem_ctrl);
 400	telem_ctrl |= ioss_period;
 401
 402	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 403				    IOSS_TELEM_EVENT_CTL_WRITE,
 404				    (u8 *)&telem_ctrl,
 405				    IOSS_TELEM_EVT_CTRL_WRITE_SIZE, NULL, 0);
 406	if (ret) {
 407		pr_err("IOSS TELEM_CTRL Event Enable Write Failed\n");
 408		return ret;
 409	}
 410
 411	telm_conf->ioss_config.curr_period = ioss_period;
 412
 413	return 0;
 414}
 415
 416
 417static int telemetry_setup_pssevtconfig(struct telemetry_evtconfig evtconfig,
 418					enum telemetry_action action)
 419{
 420	u8 num_pss_evts, pss_period;
 421	int ret, index, idx;
 422	u32 *pss_evtmap;
 423	u32 telem_ctrl;
 424
 425	num_pss_evts = evtconfig.num_evts;
 426	pss_period = evtconfig.period;
 427	pss_evtmap = evtconfig.evtmap;
 428
 429	/* PSS Config */
 430	/* Get telemetry EVENT CTL */
 431	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_READ_TELE_EVENT_CTRL,
 432				      0, 0, NULL, &telem_ctrl);
 433	if (ret) {
 434		pr_err("PSS TELEM_CTRL Read Failed\n");
 435		return ret;
 436	}
 437
 438	/* Disable Telemetry */
 439	TELEM_DISABLE(telem_ctrl);
 440	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 441				      0, 0, &telem_ctrl, NULL);
 442	if (ret) {
 443		pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 444		return ret;
 445	}
 446
 447	/* Reset Everything */
 448	if (action == TELEM_RESET) {
 449		/* Clear All Events */
 450		TELEM_CLEAR_EVENTS(telem_ctrl);
 451
 452		ret = intel_punit_ipc_command(
 453				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 454				0, 0, &telem_ctrl, NULL);
 455		if (ret) {
 456			pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 457			return ret;
 458		}
 459		telm_conf->pss_config.ssram_evts_used = 0;
 460		/* Configure Events */
 461		for (idx = 0; idx < num_pss_evts; idx++) {
 462			if (telemetry_plt_config_pss_event(
 463			    telm_conf->pss_config.telem_evts[idx].evt_id,
 464			    idx)) {
 465				pr_err("PSS TELEM_RESET Fail for Event %x\n",
 466				telm_conf->pss_config.telem_evts[idx].evt_id);
 467				continue;
 468			}
 469			telm_conf->pss_config.ssram_evts_used++;
 470		}
 471	}
 472
 473	/* Re-Configure Everything */
 474	if (action == TELEM_UPDATE) {
 475		/* Clear All Events */
 476		TELEM_CLEAR_EVENTS(telem_ctrl);
 477
 478		ret = intel_punit_ipc_command(
 479				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 480				0, 0, &telem_ctrl, NULL);
 481		if (ret) {
 482			pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 483			return ret;
 484		}
 485		telm_conf->pss_config.ssram_evts_used = 0;
 486
 487		/* Configure Events */
 488		for (index = 0; index < num_pss_evts; index++) {
 489			telm_conf->pss_config.telem_evts[index].evt_id =
 490			pss_evtmap[index];
 491
 492			if (telemetry_plt_config_pss_event(
 493			    telm_conf->pss_config.telem_evts[index].evt_id,
 494			    index)) {
 495				pr_err("PSS TELEM_UPDATE Fail for Event %x\n",
 496					pss_evtmap[index]);
 497				continue;
 498			}
 499			telm_conf->pss_config.ssram_evts_used++;
 500		}
 501	}
 502
 503	/* Add some Events */
 504	if (action == TELEM_ADD) {
 505		/* Configure Events */
 506		for (index = telm_conf->pss_config.ssram_evts_used, idx = 0;
 507		     idx < num_pss_evts; index++, idx++) {
 508
 509			telm_conf->pss_config.telem_evts[index].evt_id =
 510			pss_evtmap[idx];
 511
 512			if (telemetry_plt_config_pss_event(
 513			    telm_conf->pss_config.telem_evts[index].evt_id,
 514			    index)) {
 515				pr_err("PSS TELEM_ADD Fail for Event %x\n",
 516					pss_evtmap[idx]);
 517				continue;
 518			}
 519			telm_conf->pss_config.ssram_evts_used++;
 520		}
 521	}
 522
 523	/* Enable Periodic Telemetry Events and enable SRAM trace */
 524	TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 525	TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 526	TELEM_ENABLE_PERIODIC(telem_ctrl);
 527	telem_ctrl |= pss_period;
 528
 529	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 530				      0, 0, &telem_ctrl, NULL);
 531	if (ret) {
 532		pr_err("PSS TELEM_CTRL Event Enable Write Failed\n");
 533		return ret;
 534	}
 535
 536	telm_conf->pss_config.curr_period = pss_period;
 537
 538	return 0;
 539}
 540
 541static int telemetry_setup_evtconfig(struct telemetry_evtconfig pss_evtconfig,
 542				     struct telemetry_evtconfig ioss_evtconfig,
 543				     enum telemetry_action action)
 544{
 545	int ret;
 546
 547	mutex_lock(&(telm_conf->telem_lock));
 548
 549	if ((action == TELEM_UPDATE) && (telm_conf->telem_in_use)) {
 550		ret = -EBUSY;
 551		goto out;
 552	}
 553
 554	ret = telemetry_check_evtid(TELEM_PSS, pss_evtconfig.evtmap,
 555				    pss_evtconfig.num_evts, action);
 556	if (ret)
 557		goto out;
 558
 559	ret = telemetry_check_evtid(TELEM_IOSS, ioss_evtconfig.evtmap,
 560				    ioss_evtconfig.num_evts, action);
 561	if (ret)
 562		goto out;
 563
 564	if (ioss_evtconfig.num_evts) {
 565		ret = telemetry_setup_iossevtconfig(ioss_evtconfig, action);
 566		if (ret)
 567			goto out;
 568	}
 569
 570	if (pss_evtconfig.num_evts) {
 571		ret = telemetry_setup_pssevtconfig(pss_evtconfig, action);
 572		if (ret)
 573			goto out;
 574	}
 575
 576	if ((action == TELEM_UPDATE) || (action == TELEM_ADD))
 577		telm_conf->telem_in_use = true;
 578	else
 579		telm_conf->telem_in_use = false;
 580
 581out:
 582	mutex_unlock(&(telm_conf->telem_lock));
 583	return ret;
 584}
 585
 586static int telemetry_setup(struct platform_device *pdev)
 587{
 588	struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
 589	u32 read_buf, events, event_regs;
 590	int ret;
 591
 592	ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY, IOSS_TELEM_INFO_READ,
 593				    NULL, 0, &read_buf, IOSS_TELEM_READ_WORD);
 594	if (ret) {
 595		dev_err(&pdev->dev, "IOSS TELEM_INFO Read Failed\n");
 596		return ret;
 597	}
 598
 599	/* Get telemetry Info */
 600	events = (read_buf & TELEM_INFO_SRAMEVTS_MASK) >>
 601		  TELEM_INFO_SRAMEVTS_SHIFT;
 602	event_regs = read_buf & TELEM_INFO_NENABLES_MASK;
 603	if ((events < TELEM_MAX_EVENTS_SRAM) ||
 604	    (event_regs < TELEM_MAX_EVENTS_SRAM)) {
 605		dev_err(&pdev->dev, "IOSS:Insufficient Space for SRAM Trace\n");
 606		dev_err(&pdev->dev, "SRAM Events %d; Event Regs %d\n",
 607			events, event_regs);
 608		return -ENOMEM;
 609	}
 610
 611	telm_conf->ioss_config.min_period = TELEM_MIN_PERIOD(read_buf);
 612	telm_conf->ioss_config.max_period = TELEM_MAX_PERIOD(read_buf);
 613
 614	/* PUNIT Mailbox Setup */
 615	ret = intel_punit_ipc_command(IPC_PUNIT_BIOS_READ_TELE_INFO, 0, 0,
 616				      NULL, &read_buf);
 617	if (ret) {
 618		dev_err(&pdev->dev, "PSS TELEM_INFO Read Failed\n");
 619		return ret;
 620	}
 621
 622	/* Get telemetry Info */
 623	events = (read_buf & TELEM_INFO_SRAMEVTS_MASK) >>
 624		  TELEM_INFO_SRAMEVTS_SHIFT;
 625	event_regs = read_buf & TELEM_INFO_SRAMEVTS_MASK;
 626	if ((events < TELEM_MAX_EVENTS_SRAM) ||
 627	    (event_regs < TELEM_MAX_EVENTS_SRAM)) {
 628		dev_err(&pdev->dev, "PSS:Insufficient Space for SRAM Trace\n");
 629		dev_err(&pdev->dev, "SRAM Events %d; Event Regs %d\n",
 630			events, event_regs);
 631		return -ENOMEM;
 632	}
 633
 634	telm_conf->pss_config.min_period = TELEM_MIN_PERIOD(read_buf);
 635	telm_conf->pss_config.max_period = TELEM_MAX_PERIOD(read_buf);
 636
 637	pss_evtconfig.evtmap = NULL;
 638	pss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 639	pss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 640
 641	ioss_evtconfig.evtmap = NULL;
 642	ioss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 643	ioss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 644
 645	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 646					TELEM_RESET);
 647	if (ret) {
 648		dev_err(&pdev->dev, "TELEMETRY Setup Failed\n");
 649		return ret;
 650	}
 651	return 0;
 652}
 653
 654static int telemetry_plt_update_events(struct telemetry_evtconfig pss_evtconfig,
 655				struct telemetry_evtconfig ioss_evtconfig)
 656{
 657	int ret;
 658
 659	if ((pss_evtconfig.num_evts > 0) &&
 660	    (TELEM_SAMPLE_PERIOD_INVALID(pss_evtconfig.period))) {
 661		pr_err("PSS Sampling Period Out of Range\n");
 662		return -EINVAL;
 663	}
 664
 665	if ((ioss_evtconfig.num_evts > 0) &&
 666	    (TELEM_SAMPLE_PERIOD_INVALID(ioss_evtconfig.period))) {
 667		pr_err("IOSS Sampling Period Out of Range\n");
 668		return -EINVAL;
 669	}
 670
 671	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 672					TELEM_UPDATE);
 673	if (ret)
 674		pr_err("TELEMETRY Config Failed\n");
 675
 676	return ret;
 677}
 678
 679
 680static int telemetry_plt_set_sampling_period(u8 pss_period, u8 ioss_period)
 681{
 682	u32 telem_ctrl = 0;
 683	int ret = 0;
 684
 685	mutex_lock(&(telm_conf->telem_lock));
 686	if (ioss_period) {
 687		if (TELEM_SAMPLE_PERIOD_INVALID(ioss_period)) {
 688			pr_err("IOSS Sampling Period Out of Range\n");
 689			ret = -EINVAL;
 690			goto out;
 691		}
 692
 693		/* Get telemetry EVENT CTL */
 694		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 695					    IOSS_TELEM_EVENT_CTL_READ, NULL, 0,
 696					    &telem_ctrl, IOSS_TELEM_READ_WORD);
 697		if (ret) {
 698			pr_err("IOSS TELEM_CTRL Read Failed\n");
 699			goto out;
 700		}
 701
 702		/* Disable Telemetry */
 703		TELEM_DISABLE(telem_ctrl);
 704
 705		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 706					    IOSS_TELEM_EVENT_CTL_WRITE,
 707					    (u8 *)&telem_ctrl,
 708					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 709					    NULL, 0);
 710		if (ret) {
 711			pr_err("IOSS TELEM_CTRL Event Disable Write Failed\n");
 712			goto out;
 713		}
 714
 715		/* Enable Periodic Telemetry Events and enable SRAM trace */
 716		TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 717		TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 718		TELEM_ENABLE_PERIODIC(telem_ctrl);
 719		telem_ctrl |= ioss_period;
 720
 721		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
 722					    IOSS_TELEM_EVENT_CTL_WRITE,
 723					    (u8 *)&telem_ctrl,
 724					    IOSS_TELEM_EVT_CTRL_WRITE_SIZE,
 725					    NULL, 0);
 726		if (ret) {
 727			pr_err("IOSS TELEM_CTRL Event Enable Write Failed\n");
 728			goto out;
 729		}
 730		telm_conf->ioss_config.curr_period = ioss_period;
 731	}
 732
 733	if (pss_period) {
 734		if (TELEM_SAMPLE_PERIOD_INVALID(pss_period)) {
 735			pr_err("PSS Sampling Period Out of Range\n");
 736			ret = -EINVAL;
 737			goto out;
 738		}
 739
 740		/* Get telemetry EVENT CTL */
 741		ret = intel_punit_ipc_command(
 742				IPC_PUNIT_BIOS_READ_TELE_EVENT_CTRL,
 743				0, 0, NULL, &telem_ctrl);
 744		if (ret) {
 745			pr_err("PSS TELEM_CTRL Read Failed\n");
 746			goto out;
 747		}
 748
 749		/* Disable Telemetry */
 750		TELEM_DISABLE(telem_ctrl);
 751		ret = intel_punit_ipc_command(
 752				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 753				0, 0, &telem_ctrl, NULL);
 754		if (ret) {
 755			pr_err("PSS TELEM_CTRL Event Disable Write Failed\n");
 756			goto out;
 757		}
 758
 759		/* Enable Periodic Telemetry Events and enable SRAM trace */
 760		TELEM_CLEAR_SAMPLE_PERIOD(telem_ctrl);
 761		TELEM_ENABLE_SRAM_EVT_TRACE(telem_ctrl);
 762		TELEM_ENABLE_PERIODIC(telem_ctrl);
 763		telem_ctrl |= pss_period;
 764
 765		ret = intel_punit_ipc_command(
 766				IPC_PUNIT_BIOS_WRITE_TELE_EVENT_CTRL,
 767				0, 0, &telem_ctrl, NULL);
 768		if (ret) {
 769			pr_err("PSS TELEM_CTRL Event Enable Write Failed\n");
 770			goto out;
 771		}
 772		telm_conf->pss_config.curr_period = pss_period;
 773	}
 774
 775out:
 776	mutex_unlock(&(telm_conf->telem_lock));
 777	return ret;
 778}
 779
 780
 781static int telemetry_plt_get_sampling_period(u8 *pss_min_period,
 782					     u8 *pss_max_period,
 783					     u8 *ioss_min_period,
 784					     u8 *ioss_max_period)
 785{
 786	*pss_min_period = telm_conf->pss_config.min_period;
 787	*pss_max_period = telm_conf->pss_config.max_period;
 788	*ioss_min_period = telm_conf->ioss_config.min_period;
 789	*ioss_max_period = telm_conf->ioss_config.max_period;
 790
 791	return 0;
 792}
 793
 794
 795static int telemetry_plt_reset_events(void)
 796{
 797	struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
 798	int ret;
 799
 800	pss_evtconfig.evtmap = NULL;
 801	pss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 802	pss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 803
 804	ioss_evtconfig.evtmap = NULL;
 805	ioss_evtconfig.num_evts = TELEM_MAX_OS_ALLOCATED_EVENTS;
 806	ioss_evtconfig.period = TELEM_SAMPLING_DEFAULT_PERIOD;
 807
 808	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 809					TELEM_RESET);
 810	if (ret)
 811		pr_err("TELEMETRY Reset Failed\n");
 812
 813	return ret;
 814}
 815
 816
 817static int telemetry_plt_get_eventconfig(struct telemetry_evtconfig *pss_config,
 818					struct telemetry_evtconfig *ioss_config,
 819					int pss_len, int ioss_len)
 820{
 821	u32 *pss_evtmap, *ioss_evtmap;
 822	u32 index;
 823
 824	pss_evtmap = pss_config->evtmap;
 825	ioss_evtmap = ioss_config->evtmap;
 826
 827	mutex_lock(&(telm_conf->telem_lock));
 828	pss_config->num_evts = telm_conf->pss_config.ssram_evts_used;
 829	ioss_config->num_evts = telm_conf->ioss_config.ssram_evts_used;
 830
 831	pss_config->period = telm_conf->pss_config.curr_period;
 832	ioss_config->period = telm_conf->ioss_config.curr_period;
 833
 834	if ((pss_len < telm_conf->pss_config.ssram_evts_used) ||
 835	    (ioss_len < telm_conf->ioss_config.ssram_evts_used)) {
 836		mutex_unlock(&(telm_conf->telem_lock));
 837		return -EINVAL;
 838	}
 839
 840	for (index = 0; index < telm_conf->pss_config.ssram_evts_used;
 841	     index++) {
 842		pss_evtmap[index] =
 843		telm_conf->pss_config.telem_evts[index].evt_id;
 844	}
 845
 846	for (index = 0; index < telm_conf->ioss_config.ssram_evts_used;
 847	     index++) {
 848		ioss_evtmap[index] =
 849		telm_conf->ioss_config.telem_evts[index].evt_id;
 850	}
 851
 852	mutex_unlock(&(telm_conf->telem_lock));
 853	return 0;
 854}
 855
 856
 857static int telemetry_plt_add_events(u8 num_pss_evts, u8 num_ioss_evts,
 858				    u32 *pss_evtmap, u32 *ioss_evtmap)
 859{
 860	struct telemetry_evtconfig pss_evtconfig, ioss_evtconfig;
 861	int ret;
 862
 863	pss_evtconfig.evtmap = pss_evtmap;
 864	pss_evtconfig.num_evts = num_pss_evts;
 865	pss_evtconfig.period = telm_conf->pss_config.curr_period;
 866
 867	ioss_evtconfig.evtmap = ioss_evtmap;
 868	ioss_evtconfig.num_evts = num_ioss_evts;
 869	ioss_evtconfig.period = telm_conf->ioss_config.curr_period;
 870
 871	ret = telemetry_setup_evtconfig(pss_evtconfig, ioss_evtconfig,
 872					TELEM_ADD);
 873	if (ret)
 874		pr_err("TELEMETRY ADD Failed\n");
 875
 876	return ret;
 877}
 878
 879static int telem_evtlog_read(enum telemetry_unit telem_unit,
 880			     struct telem_ssram_region *ssram_region, u8 len)
 881{
 882	struct telemetry_unit_config *unit_config;
 883	u64 timestamp_prev, timestamp_next;
 884	int ret, index, timeout = 0;
 885
 886	ret = telem_get_unitconfig(telem_unit, &unit_config);
 887	if (ret < 0)
 888		return ret;
 889
 890	if (len > unit_config->ssram_evts_used)
 891		len = unit_config->ssram_evts_used;
 892
 893	do {
 894		timestamp_prev = readq(unit_config->regmap);
 895		if (!timestamp_prev) {
 896			pr_err("Ssram under update. Please Try Later\n");
 897			return -EBUSY;
 898		}
 899
 900		ssram_region->start_time = readq(unit_config->regmap +
 901						 TELEM_SSRAM_STARTTIME_OFFSET);
 902
 903		for (index = 0; index < len; index++) {
 904			ssram_region->events[index] =
 905			readq(unit_config->regmap + TELEM_SSRAM_EVTLOG_OFFSET +
 906			      BYTES_PER_LONG*index);
 907		}
 908
 909		timestamp_next = readq(unit_config->regmap);
 910		if (!timestamp_next) {
 911			pr_err("Ssram under update. Please Try Later\n");
 912			return -EBUSY;
 913		}
 914
 915		if (timeout++ > TELEM_SSRAM_READ_TIMEOUT) {
 916			pr_err("Timeout while reading Events\n");
 917			return -EBUSY;
 918		}
 919
 920	} while (timestamp_prev != timestamp_next);
 921
 922	ssram_region->timestamp = timestamp_next;
 923
 924	return len;
 925}
 926
 927static int telemetry_plt_raw_read_eventlog(enum telemetry_unit telem_unit,
 928					   struct telemetry_evtlog *evtlog,
 929					   int len, int log_all_evts)
 930{
 931	int index, idx1, ret, readlen = len;
 932	struct telem_ssram_region ssram_region;
 933	struct telemetry_evtmap *evtmap;
 934
 935	switch (telem_unit)	{
 936	case TELEM_PSS:
 937		evtmap = telm_conf->pss_config.telem_evts;
 938		break;
 939
 940	case TELEM_IOSS:
 941		evtmap = telm_conf->ioss_config.telem_evts;
 942		break;
 943
 944	default:
 945		pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
 946		return -EINVAL;
 947	}
 948
 949	if (!log_all_evts)
 950		readlen = TELEM_MAX_EVENTS_SRAM;
 951
 952	ret = telem_evtlog_read(telem_unit, &ssram_region, readlen);
 953	if (ret < 0)
 954		return ret;
 955
 956	/* Invalid evt-id array specified via length mismatch */
 957	if ((!log_all_evts) && (len > ret))
 958		return -EINVAL;
 959
 960	if (log_all_evts)
 961		for (index = 0; index < ret; index++) {
 962			evtlog[index].telem_evtlog = ssram_region.events[index];
 963			evtlog[index].telem_evtid = evtmap[index].evt_id;
 964		}
 965	else
 966		for (index = 0, readlen = 0; (index < ret) && (readlen < len);
 967		     index++) {
 968			for (idx1 = 0; idx1 < len; idx1++) {
 969				/* Elements matched */
 970				if (evtmap[index].evt_id ==
 971				    evtlog[idx1].telem_evtid) {
 972					evtlog[idx1].telem_evtlog =
 973					ssram_region.events[index];
 974					readlen++;
 975
 976					break;
 977				}
 978			}
 979		}
 980
 981	return readlen;
 982}
 983
 984static int telemetry_plt_read_eventlog(enum telemetry_unit telem_unit,
 985		struct telemetry_evtlog *evtlog, int len, int log_all_evts)
 986{
 987	int ret;
 988
 989	mutex_lock(&(telm_conf->telem_lock));
 990	ret = telemetry_plt_raw_read_eventlog(telem_unit, evtlog,
 991					      len, log_all_evts);
 992	mutex_unlock(&(telm_conf->telem_lock));
 993
 994	return ret;
 995}
 996
 997static int telemetry_plt_get_trace_verbosity(enum telemetry_unit telem_unit,
 998					     u32 *verbosity)
 999{
1000	u32 temp = 0;
1001	int ret;
1002
1003	if (verbosity == NULL)
1004		return -EINVAL;
1005
1006	mutex_lock(&(telm_conf->telem_trace_lock));
1007	switch (telem_unit) {
1008	case TELEM_PSS:
1009		ret = intel_punit_ipc_command(
1010				IPC_PUNIT_BIOS_READ_TELE_TRACE_CTRL,
1011				0, 0, NULL, &temp);
1012		if (ret) {
1013			pr_err("PSS TRACE_CTRL Read Failed\n");
1014			goto out;
1015		}
1016
1017		break;
1018
1019	case TELEM_IOSS:
1020		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1021				IOSS_TELEM_TRACE_CTL_READ, NULL, 0, &temp,
1022				IOSS_TELEM_READ_WORD);
1023		if (ret) {
1024			pr_err("IOSS TRACE_CTL Read Failed\n");
1025			goto out;
1026		}
1027
1028		break;
1029
1030	default:
1031		pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
1032		ret = -EINVAL;
1033		break;
1034	}
1035	TELEM_EXTRACT_VERBOSITY(temp, *verbosity);
1036
1037out:
1038	mutex_unlock(&(telm_conf->telem_trace_lock));
1039	return ret;
1040}
1041
1042static int telemetry_plt_set_trace_verbosity(enum telemetry_unit telem_unit,
1043					     u32 verbosity)
1044{
1045	u32 temp = 0;
1046	int ret;
1047
1048	verbosity &= TELEM_TRC_VERBOSITY_MASK;
1049
1050	mutex_lock(&(telm_conf->telem_trace_lock));
1051	switch (telem_unit) {
1052	case TELEM_PSS:
1053		ret = intel_punit_ipc_command(
1054				IPC_PUNIT_BIOS_READ_TELE_TRACE_CTRL,
1055				0, 0, NULL, &temp);
1056		if (ret) {
1057			pr_err("PSS TRACE_CTRL Read Failed\n");
1058			goto out;
1059		}
1060
1061		TELEM_CLEAR_VERBOSITY_BITS(temp);
1062		TELEM_SET_VERBOSITY_BITS(temp, verbosity);
1063
1064		ret = intel_punit_ipc_command(
1065				IPC_PUNIT_BIOS_WRITE_TELE_TRACE_CTRL,
1066				0, 0, &temp, NULL);
1067		if (ret) {
1068			pr_err("PSS TRACE_CTRL Verbosity Set Failed\n");
1069			goto out;
1070		}
1071		break;
1072
1073	case TELEM_IOSS:
1074		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1075				IOSS_TELEM_TRACE_CTL_READ, NULL, 0, &temp,
1076				IOSS_TELEM_READ_WORD);
1077		if (ret) {
1078			pr_err("IOSS TRACE_CTL Read Failed\n");
1079			goto out;
1080		}
1081
1082		TELEM_CLEAR_VERBOSITY_BITS(temp);
1083		TELEM_SET_VERBOSITY_BITS(temp, verbosity);
1084
1085		ret = intel_pmc_ipc_command(PMC_IPC_PMC_TELEMTRY,
1086				IOSS_TELEM_TRACE_CTL_WRITE, (u8 *)&temp,
1087				IOSS_TELEM_WRITE_FOURBYTES, NULL, 0);
1088		if (ret) {
1089			pr_err("IOSS TRACE_CTL Verbosity Set Failed\n");
1090			goto out;
1091		}
1092		break;
1093
1094	default:
1095		pr_err("Unknown Telemetry Unit Specified %d\n", telem_unit);
1096		ret = -EINVAL;
1097		break;
1098	}
1099
1100out:
1101	mutex_unlock(&(telm_conf->telem_trace_lock));
1102	return ret;
1103}
1104
1105static const struct telemetry_core_ops telm_pltops = {
1106	.get_trace_verbosity = telemetry_plt_get_trace_verbosity,
1107	.set_trace_verbosity = telemetry_plt_set_trace_verbosity,
1108	.set_sampling_period = telemetry_plt_set_sampling_period,
1109	.get_sampling_period = telemetry_plt_get_sampling_period,
1110	.raw_read_eventlog = telemetry_plt_raw_read_eventlog,
1111	.get_eventconfig = telemetry_plt_get_eventconfig,
1112	.update_events = telemetry_plt_update_events,
1113	.read_eventlog = telemetry_plt_read_eventlog,
1114	.reset_events = telemetry_plt_reset_events,
1115	.add_events = telemetry_plt_add_events,
1116};
1117
1118static int telemetry_pltdrv_probe(struct platform_device *pdev)
1119{
1120	struct resource *res0 = NULL, *res1 = NULL;
1121	const struct x86_cpu_id *id;
1122	int size, ret = -ENOMEM;
1123
1124	id = x86_match_cpu(telemetry_cpu_ids);
1125	if (!id)
1126		return -ENODEV;
1127
1128	telm_conf = (struct telemetry_plt_config *)id->driver_data;
1129
1130	res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1131	if (!res0) {
1132		ret = -EINVAL;
1133		goto out;
1134	}
1135	size = resource_size(res0);
1136	if (!devm_request_mem_region(&pdev->dev, res0->start, size,
1137				     pdev->name)) {
1138		ret = -EBUSY;
1139		goto out;
1140	}
1141	telm_conf->pss_config.ssram_base_addr = res0->start;
1142	telm_conf->pss_config.ssram_size = size;
1143
1144	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1145	if (!res1) {
1146		ret = -EINVAL;
1147		goto out;
1148	}
1149	size = resource_size(res1);
1150	if (!devm_request_mem_region(&pdev->dev, res1->start, size,
1151				     pdev->name)) {
1152		ret = -EBUSY;
1153		goto out;
1154	}
1155
1156	telm_conf->ioss_config.ssram_base_addr = res1->start;
1157	telm_conf->ioss_config.ssram_size = size;
1158
1159	telm_conf->pss_config.regmap = ioremap_nocache(
1160					telm_conf->pss_config.ssram_base_addr,
1161					telm_conf->pss_config.ssram_size);
1162	if (!telm_conf->pss_config.regmap) {
1163		ret = -ENOMEM;
1164		goto out;
1165	}
1166
1167	telm_conf->ioss_config.regmap = ioremap_nocache(
1168				telm_conf->ioss_config.ssram_base_addr,
1169				telm_conf->ioss_config.ssram_size);
1170	if (!telm_conf->ioss_config.regmap) {
1171		ret = -ENOMEM;
1172		goto out;
1173	}
1174
1175	mutex_init(&telm_conf->telem_lock);
1176	mutex_init(&telm_conf->telem_trace_lock);
1177
1178	ret = telemetry_setup(pdev);
1179	if (ret)
1180		goto out;
1181
1182	ret = telemetry_set_pltdata(&telm_pltops, telm_conf);
1183	if (ret) {
1184		dev_err(&pdev->dev, "TELEMETRY Set Pltops Failed.\n");
1185		goto out;
1186	}
1187
1188	return 0;
1189
1190out:
1191	if (res0)
1192		release_mem_region(res0->start, resource_size(res0));
1193	if (res1)
1194		release_mem_region(res1->start, resource_size(res1));
1195	if (telm_conf->pss_config.regmap)
1196		iounmap(telm_conf->pss_config.regmap);
1197	if (telm_conf->ioss_config.regmap)
1198		iounmap(telm_conf->ioss_config.regmap);
1199	dev_err(&pdev->dev, "TELEMETRY Setup Failed.\n");
1200
1201	return ret;
1202}
1203
1204static int telemetry_pltdrv_remove(struct platform_device *pdev)
1205{
1206	telemetry_clear_pltdata();
1207	iounmap(telm_conf->pss_config.regmap);
1208	iounmap(telm_conf->ioss_config.regmap);
1209
1210	return 0;
1211}
1212
1213static struct platform_driver telemetry_soc_driver = {
1214	.probe		= telemetry_pltdrv_probe,
1215	.remove		= telemetry_pltdrv_remove,
1216	.driver		= {
1217		.name	= DRIVER_NAME,
1218	},
1219};
1220
1221static int __init telemetry_module_init(void)
1222{
 
1223	return platform_driver_register(&telemetry_soc_driver);
1224}
1225
1226static void __exit telemetry_module_exit(void)
1227{
1228	platform_driver_unregister(&telemetry_soc_driver);
1229}
1230
1231device_initcall(telemetry_module_init);
1232module_exit(telemetry_module_exit);
1233
1234MODULE_AUTHOR("Souvik Kumar Chakravarty <souvik.k.chakravarty@intel.com>");
1235MODULE_DESCRIPTION("Intel SoC Telemetry Platform Driver");
1236MODULE_VERSION(DRIVER_VERSION);
1237MODULE_LICENSE("GPL v2");