Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright(C) 2015 Linaro Limited. All rights reserved.
   4 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
 
 
 
 
 
 
 
 
 
 
 
 
   5 */
   6
   7#include <linux/pid_namespace.h>
   8#include <linux/pm_runtime.h>
   9#include <linux/sysfs.h>
  10#include "coresight-etm.h"
  11#include "coresight-priv.h"
  12
  13static ssize_t nr_addr_cmp_show(struct device *dev,
  14				struct device_attribute *attr, char *buf)
  15{
  16	unsigned long val;
  17	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  18
  19	val = drvdata->nr_addr_cmp;
  20	return sprintf(buf, "%#lx\n", val);
  21}
  22static DEVICE_ATTR_RO(nr_addr_cmp);
  23
  24static ssize_t nr_cntr_show(struct device *dev,
  25			    struct device_attribute *attr, char *buf)
  26{	unsigned long val;
  27	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  28
  29	val = drvdata->nr_cntr;
  30	return sprintf(buf, "%#lx\n", val);
  31}
  32static DEVICE_ATTR_RO(nr_cntr);
  33
  34static ssize_t nr_ctxid_cmp_show(struct device *dev,
  35				 struct device_attribute *attr, char *buf)
  36{
  37	unsigned long val;
  38	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  39
  40	val = drvdata->nr_ctxid_cmp;
  41	return sprintf(buf, "%#lx\n", val);
  42}
  43static DEVICE_ATTR_RO(nr_ctxid_cmp);
  44
  45static ssize_t etmsr_show(struct device *dev,
  46			  struct device_attribute *attr, char *buf)
  47{
  48	unsigned long flags, val;
  49	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  50
  51	pm_runtime_get_sync(dev->parent);
  52	spin_lock_irqsave(&drvdata->spinlock, flags);
  53	CS_UNLOCK(drvdata->base);
  54
  55	val = etm_readl(drvdata, ETMSR);
  56
  57	CS_LOCK(drvdata->base);
  58	spin_unlock_irqrestore(&drvdata->spinlock, flags);
  59	pm_runtime_put(dev->parent);
  60
  61	return sprintf(buf, "%#lx\n", val);
  62}
  63static DEVICE_ATTR_RO(etmsr);
  64
  65static ssize_t reset_store(struct device *dev,
  66			   struct device_attribute *attr,
  67			   const char *buf, size_t size)
  68{
  69	int i, ret;
  70	unsigned long val;
  71	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  72	struct etm_config *config = &drvdata->config;
  73
  74	ret = kstrtoul(buf, 16, &val);
  75	if (ret)
  76		return ret;
  77
  78	if (val) {
  79		spin_lock(&drvdata->spinlock);
  80		memset(config, 0, sizeof(struct etm_config));
  81		config->mode = ETM_MODE_EXCLUDE;
  82		config->trigger_event = ETM_DEFAULT_EVENT_VAL;
  83		for (i = 0; i < drvdata->nr_addr_cmp; i++) {
  84			config->addr_type[i] = ETM_ADDR_TYPE_NONE;
  85		}
  86
  87		etm_set_default(config);
  88		spin_unlock(&drvdata->spinlock);
  89	}
  90
  91	return size;
  92}
  93static DEVICE_ATTR_WO(reset);
  94
  95static ssize_t mode_show(struct device *dev,
  96			 struct device_attribute *attr, char *buf)
  97{
  98	unsigned long val;
  99	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 100	struct etm_config *config = &drvdata->config;
 101
 102	val = config->mode;
 103	return sprintf(buf, "%#lx\n", val);
 104}
 105
 106static ssize_t mode_store(struct device *dev,
 107			  struct device_attribute *attr,
 108			  const char *buf, size_t size)
 109{
 110	int ret;
 111	unsigned long val;
 112	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 113	struct etm_config *config = &drvdata->config;
 114
 115	ret = kstrtoul(buf, 16, &val);
 116	if (ret)
 117		return ret;
 118
 119	spin_lock(&drvdata->spinlock);
 120	config->mode = val & ETM_MODE_ALL;
 121
 122	if (config->mode & ETM_MODE_EXCLUDE)
 123		config->enable_ctrl1 |= ETMTECR1_INC_EXC;
 124	else
 125		config->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
 126
 127	if (config->mode & ETM_MODE_CYCACC)
 128		config->ctrl |= ETMCR_CYC_ACC;
 129	else
 130		config->ctrl &= ~ETMCR_CYC_ACC;
 131
 132	if (config->mode & ETM_MODE_STALL) {
 133		if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
 134			dev_warn(dev, "stall mode not supported\n");
 135			ret = -EINVAL;
 136			goto err_unlock;
 137		}
 138		config->ctrl |= ETMCR_STALL_MODE;
 139	} else
 140		config->ctrl &= ~ETMCR_STALL_MODE;
 141
 142	if (config->mode & ETM_MODE_TIMESTAMP) {
 143		if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
 144			dev_warn(dev, "timestamp not supported\n");
 145			ret = -EINVAL;
 146			goto err_unlock;
 147		}
 148		config->ctrl |= ETMCR_TIMESTAMP_EN;
 149	} else
 150		config->ctrl &= ~ETMCR_TIMESTAMP_EN;
 151
 152	if (config->mode & ETM_MODE_CTXID)
 153		config->ctrl |= ETMCR_CTXID_SIZE;
 154	else
 155		config->ctrl &= ~ETMCR_CTXID_SIZE;
 156
 157	if (config->mode & ETM_MODE_BBROAD)
 158		config->ctrl |= ETMCR_BRANCH_BROADCAST;
 159	else
 160		config->ctrl &= ~ETMCR_BRANCH_BROADCAST;
 161
 162	if (config->mode & ETM_MODE_RET_STACK)
 163		config->ctrl |= ETMCR_RETURN_STACK;
 164	else
 165		config->ctrl &= ~ETMCR_RETURN_STACK;
 166
 167	if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
 168		etm_config_trace_mode(config);
 169
 170	spin_unlock(&drvdata->spinlock);
 171
 172	return size;
 173
 174err_unlock:
 175	spin_unlock(&drvdata->spinlock);
 176	return ret;
 177}
 178static DEVICE_ATTR_RW(mode);
 179
 180static ssize_t trigger_event_show(struct device *dev,
 181				  struct device_attribute *attr, char *buf)
 182{
 183	unsigned long val;
 184	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 185	struct etm_config *config = &drvdata->config;
 186
 187	val = config->trigger_event;
 188	return sprintf(buf, "%#lx\n", val);
 189}
 190
 191static ssize_t trigger_event_store(struct device *dev,
 192				   struct device_attribute *attr,
 193				   const char *buf, size_t size)
 194{
 195	int ret;
 196	unsigned long val;
 197	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 198	struct etm_config *config = &drvdata->config;
 199
 200	ret = kstrtoul(buf, 16, &val);
 201	if (ret)
 202		return ret;
 203
 204	config->trigger_event = val & ETM_EVENT_MASK;
 205
 206	return size;
 207}
 208static DEVICE_ATTR_RW(trigger_event);
 209
 210static ssize_t enable_event_show(struct device *dev,
 211				 struct device_attribute *attr, char *buf)
 212{
 213	unsigned long val;
 214	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 215	struct etm_config *config = &drvdata->config;
 216
 217	val = config->enable_event;
 218	return sprintf(buf, "%#lx\n", val);
 219}
 220
 221static ssize_t enable_event_store(struct device *dev,
 222				  struct device_attribute *attr,
 223				  const char *buf, size_t size)
 224{
 225	int ret;
 226	unsigned long val;
 227	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 228	struct etm_config *config = &drvdata->config;
 229
 230	ret = kstrtoul(buf, 16, &val);
 231	if (ret)
 232		return ret;
 233
 234	config->enable_event = val & ETM_EVENT_MASK;
 235
 236	return size;
 237}
 238static DEVICE_ATTR_RW(enable_event);
 239
 240static ssize_t fifofull_level_show(struct device *dev,
 241				   struct device_attribute *attr, char *buf)
 242{
 243	unsigned long val;
 244	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 245	struct etm_config *config = &drvdata->config;
 246
 247	val = config->fifofull_level;
 248	return sprintf(buf, "%#lx\n", val);
 249}
 250
 251static ssize_t fifofull_level_store(struct device *dev,
 252				    struct device_attribute *attr,
 253				    const char *buf, size_t size)
 254{
 255	int ret;
 256	unsigned long val;
 257	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 258	struct etm_config *config = &drvdata->config;
 259
 260	ret = kstrtoul(buf, 16, &val);
 261	if (ret)
 262		return ret;
 263
 264	config->fifofull_level = val;
 265
 266	return size;
 267}
 268static DEVICE_ATTR_RW(fifofull_level);
 269
 270static ssize_t addr_idx_show(struct device *dev,
 271			     struct device_attribute *attr, char *buf)
 272{
 273	unsigned long val;
 274	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 275	struct etm_config *config = &drvdata->config;
 276
 277	val = config->addr_idx;
 278	return sprintf(buf, "%#lx\n", val);
 279}
 280
 281static ssize_t addr_idx_store(struct device *dev,
 282			      struct device_attribute *attr,
 283			      const char *buf, size_t size)
 284{
 285	int ret;
 286	unsigned long val;
 287	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 288	struct etm_config *config = &drvdata->config;
 289
 290	ret = kstrtoul(buf, 16, &val);
 291	if (ret)
 292		return ret;
 293
 294	if (val >= drvdata->nr_addr_cmp)
 295		return -EINVAL;
 296
 297	/*
 298	 * Use spinlock to ensure index doesn't change while it gets
 299	 * dereferenced multiple times within a spinlock block elsewhere.
 300	 */
 301	spin_lock(&drvdata->spinlock);
 302	config->addr_idx = val;
 303	spin_unlock(&drvdata->spinlock);
 304
 305	return size;
 306}
 307static DEVICE_ATTR_RW(addr_idx);
 308
 309static ssize_t addr_single_show(struct device *dev,
 310				struct device_attribute *attr, char *buf)
 311{
 312	u8 idx;
 313	unsigned long val;
 314	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 315	struct etm_config *config = &drvdata->config;
 316
 317	spin_lock(&drvdata->spinlock);
 318	idx = config->addr_idx;
 319	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 320	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
 321		spin_unlock(&drvdata->spinlock);
 322		return -EINVAL;
 323	}
 324
 325	val = config->addr_val[idx];
 326	spin_unlock(&drvdata->spinlock);
 327
 328	return sprintf(buf, "%#lx\n", val);
 329}
 330
 331static ssize_t addr_single_store(struct device *dev,
 332				 struct device_attribute *attr,
 333				 const char *buf, size_t size)
 334{
 335	u8 idx;
 336	int ret;
 337	unsigned long val;
 338	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 339	struct etm_config *config = &drvdata->config;
 340
 341	ret = kstrtoul(buf, 16, &val);
 342	if (ret)
 343		return ret;
 344
 345	spin_lock(&drvdata->spinlock);
 346	idx = config->addr_idx;
 347	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 348	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
 349		spin_unlock(&drvdata->spinlock);
 350		return -EINVAL;
 351	}
 352
 353	config->addr_val[idx] = val;
 354	config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
 355	spin_unlock(&drvdata->spinlock);
 356
 357	return size;
 358}
 359static DEVICE_ATTR_RW(addr_single);
 360
 361static ssize_t addr_range_show(struct device *dev,
 362			       struct device_attribute *attr, char *buf)
 363{
 364	u8 idx;
 365	unsigned long val1, val2;
 366	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 367	struct etm_config *config = &drvdata->config;
 368
 369	spin_lock(&drvdata->spinlock);
 370	idx = config->addr_idx;
 371	if (idx % 2 != 0) {
 372		spin_unlock(&drvdata->spinlock);
 373		return -EPERM;
 374	}
 375	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
 376	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
 377	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
 378	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
 379		spin_unlock(&drvdata->spinlock);
 380		return -EPERM;
 381	}
 382
 383	val1 = config->addr_val[idx];
 384	val2 = config->addr_val[idx + 1];
 385	spin_unlock(&drvdata->spinlock);
 386
 387	return sprintf(buf, "%#lx %#lx\n", val1, val2);
 388}
 389
 390static ssize_t addr_range_store(struct device *dev,
 391			      struct device_attribute *attr,
 392			      const char *buf, size_t size)
 393{
 394	u8 idx;
 395	unsigned long val1, val2;
 396	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 397	struct etm_config *config = &drvdata->config;
 398
 399	if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
 400		return -EINVAL;
 401	/* Lower address comparator cannot have a higher address value */
 402	if (val1 > val2)
 403		return -EINVAL;
 404
 405	spin_lock(&drvdata->spinlock);
 406	idx = config->addr_idx;
 407	if (idx % 2 != 0) {
 408		spin_unlock(&drvdata->spinlock);
 409		return -EPERM;
 410	}
 411	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
 412	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
 413	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
 414	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
 415		spin_unlock(&drvdata->spinlock);
 416		return -EPERM;
 417	}
 418
 419	config->addr_val[idx] = val1;
 420	config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
 421	config->addr_val[idx + 1] = val2;
 422	config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
 423	config->enable_ctrl1 |= (1 << (idx/2));
 424	spin_unlock(&drvdata->spinlock);
 425
 426	return size;
 427}
 428static DEVICE_ATTR_RW(addr_range);
 429
 430static ssize_t addr_start_show(struct device *dev,
 431			       struct device_attribute *attr, char *buf)
 432{
 433	u8 idx;
 434	unsigned long val;
 435	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 436	struct etm_config *config = &drvdata->config;
 437
 438	spin_lock(&drvdata->spinlock);
 439	idx = config->addr_idx;
 440	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 441	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
 442		spin_unlock(&drvdata->spinlock);
 443		return -EPERM;
 444	}
 445
 446	val = config->addr_val[idx];
 447	spin_unlock(&drvdata->spinlock);
 448
 449	return sprintf(buf, "%#lx\n", val);
 450}
 451
 452static ssize_t addr_start_store(struct device *dev,
 453				struct device_attribute *attr,
 454				const char *buf, size_t size)
 455{
 456	u8 idx;
 457	int ret;
 458	unsigned long val;
 459	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 460	struct etm_config *config = &drvdata->config;
 461
 462	ret = kstrtoul(buf, 16, &val);
 463	if (ret)
 464		return ret;
 465
 466	spin_lock(&drvdata->spinlock);
 467	idx = config->addr_idx;
 468	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 469	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
 470		spin_unlock(&drvdata->spinlock);
 471		return -EPERM;
 472	}
 473
 474	config->addr_val[idx] = val;
 475	config->addr_type[idx] = ETM_ADDR_TYPE_START;
 476	config->startstop_ctrl |= (1 << idx);
 477	config->enable_ctrl1 |= ETMTECR1_START_STOP;
 478	spin_unlock(&drvdata->spinlock);
 479
 480	return size;
 481}
 482static DEVICE_ATTR_RW(addr_start);
 483
 484static ssize_t addr_stop_show(struct device *dev,
 485			      struct device_attribute *attr, char *buf)
 486{
 487	u8 idx;
 488	unsigned long val;
 489	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 490	struct etm_config *config = &drvdata->config;
 491
 492	spin_lock(&drvdata->spinlock);
 493	idx = config->addr_idx;
 494	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 495	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
 496		spin_unlock(&drvdata->spinlock);
 497		return -EPERM;
 498	}
 499
 500	val = config->addr_val[idx];
 501	spin_unlock(&drvdata->spinlock);
 502
 503	return sprintf(buf, "%#lx\n", val);
 504}
 505
 506static ssize_t addr_stop_store(struct device *dev,
 507			       struct device_attribute *attr,
 508			       const char *buf, size_t size)
 509{
 510	u8 idx;
 511	int ret;
 512	unsigned long val;
 513	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 514	struct etm_config *config = &drvdata->config;
 515
 516	ret = kstrtoul(buf, 16, &val);
 517	if (ret)
 518		return ret;
 519
 520	spin_lock(&drvdata->spinlock);
 521	idx = config->addr_idx;
 522	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 523	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
 524		spin_unlock(&drvdata->spinlock);
 525		return -EPERM;
 526	}
 527
 528	config->addr_val[idx] = val;
 529	config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
 530	config->startstop_ctrl |= (1 << (idx + 16));
 531	config->enable_ctrl1 |= ETMTECR1_START_STOP;
 532	spin_unlock(&drvdata->spinlock);
 533
 534	return size;
 535}
 536static DEVICE_ATTR_RW(addr_stop);
 537
 538static ssize_t addr_acctype_show(struct device *dev,
 539				 struct device_attribute *attr, char *buf)
 540{
 541	unsigned long val;
 542	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 543	struct etm_config *config = &drvdata->config;
 544
 545	spin_lock(&drvdata->spinlock);
 546	val = config->addr_acctype[config->addr_idx];
 547	spin_unlock(&drvdata->spinlock);
 548
 549	return sprintf(buf, "%#lx\n", val);
 550}
 551
 552static ssize_t addr_acctype_store(struct device *dev,
 553				  struct device_attribute *attr,
 554				  const char *buf, size_t size)
 555{
 556	int ret;
 557	unsigned long val;
 558	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 559	struct etm_config *config = &drvdata->config;
 560
 561	ret = kstrtoul(buf, 16, &val);
 562	if (ret)
 563		return ret;
 564
 565	spin_lock(&drvdata->spinlock);
 566	config->addr_acctype[config->addr_idx] = val;
 567	spin_unlock(&drvdata->spinlock);
 568
 569	return size;
 570}
 571static DEVICE_ATTR_RW(addr_acctype);
 572
 573static ssize_t cntr_idx_show(struct device *dev,
 574			     struct device_attribute *attr, char *buf)
 575{
 576	unsigned long val;
 577	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 578	struct etm_config *config = &drvdata->config;
 579
 580	val = config->cntr_idx;
 581	return sprintf(buf, "%#lx\n", val);
 582}
 583
 584static ssize_t cntr_idx_store(struct device *dev,
 585			      struct device_attribute *attr,
 586			      const char *buf, size_t size)
 587{
 588	int ret;
 589	unsigned long val;
 590	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 591	struct etm_config *config = &drvdata->config;
 592
 593	ret = kstrtoul(buf, 16, &val);
 594	if (ret)
 595		return ret;
 596
 597	if (val >= drvdata->nr_cntr)
 598		return -EINVAL;
 599	/*
 600	 * Use spinlock to ensure index doesn't change while it gets
 601	 * dereferenced multiple times within a spinlock block elsewhere.
 602	 */
 603	spin_lock(&drvdata->spinlock);
 604	config->cntr_idx = val;
 605	spin_unlock(&drvdata->spinlock);
 606
 607	return size;
 608}
 609static DEVICE_ATTR_RW(cntr_idx);
 610
 611static ssize_t cntr_rld_val_show(struct device *dev,
 612				 struct device_attribute *attr, char *buf)
 613{
 614	unsigned long val;
 615	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 616	struct etm_config *config = &drvdata->config;
 617
 618	spin_lock(&drvdata->spinlock);
 619	val = config->cntr_rld_val[config->cntr_idx];
 620	spin_unlock(&drvdata->spinlock);
 621
 622	return sprintf(buf, "%#lx\n", val);
 623}
 624
 625static ssize_t cntr_rld_val_store(struct device *dev,
 626				  struct device_attribute *attr,
 627				  const char *buf, size_t size)
 628{
 629	int ret;
 630	unsigned long val;
 631	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 632	struct etm_config *config = &drvdata->config;
 633
 634	ret = kstrtoul(buf, 16, &val);
 635	if (ret)
 636		return ret;
 637
 638	spin_lock(&drvdata->spinlock);
 639	config->cntr_rld_val[config->cntr_idx] = val;
 640	spin_unlock(&drvdata->spinlock);
 641
 642	return size;
 643}
 644static DEVICE_ATTR_RW(cntr_rld_val);
 645
 646static ssize_t cntr_event_show(struct device *dev,
 647			       struct device_attribute *attr, char *buf)
 648{
 649	unsigned long val;
 650	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 651	struct etm_config *config = &drvdata->config;
 652
 653	spin_lock(&drvdata->spinlock);
 654	val = config->cntr_event[config->cntr_idx];
 655	spin_unlock(&drvdata->spinlock);
 656
 657	return sprintf(buf, "%#lx\n", val);
 658}
 659
 660static ssize_t cntr_event_store(struct device *dev,
 661				struct device_attribute *attr,
 662				const char *buf, size_t size)
 663{
 664	int ret;
 665	unsigned long val;
 666	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 667	struct etm_config *config = &drvdata->config;
 668
 669	ret = kstrtoul(buf, 16, &val);
 670	if (ret)
 671		return ret;
 672
 673	spin_lock(&drvdata->spinlock);
 674	config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
 675	spin_unlock(&drvdata->spinlock);
 676
 677	return size;
 678}
 679static DEVICE_ATTR_RW(cntr_event);
 680
 681static ssize_t cntr_rld_event_show(struct device *dev,
 682				   struct device_attribute *attr, char *buf)
 683{
 684	unsigned long val;
 685	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 686	struct etm_config *config = &drvdata->config;
 687
 688	spin_lock(&drvdata->spinlock);
 689	val = config->cntr_rld_event[config->cntr_idx];
 690	spin_unlock(&drvdata->spinlock);
 691
 692	return sprintf(buf, "%#lx\n", val);
 693}
 694
 695static ssize_t cntr_rld_event_store(struct device *dev,
 696				    struct device_attribute *attr,
 697				    const char *buf, size_t size)
 698{
 699	int ret;
 700	unsigned long val;
 701	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 702	struct etm_config *config = &drvdata->config;
 703
 704	ret = kstrtoul(buf, 16, &val);
 705	if (ret)
 706		return ret;
 707
 708	spin_lock(&drvdata->spinlock);
 709	config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
 710	spin_unlock(&drvdata->spinlock);
 711
 712	return size;
 713}
 714static DEVICE_ATTR_RW(cntr_rld_event);
 715
 716static ssize_t cntr_val_show(struct device *dev,
 717			     struct device_attribute *attr, char *buf)
 718{
 719	int i, ret = 0;
 720	u32 val;
 721	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 722	struct etm_config *config = &drvdata->config;
 723
 724	if (!local_read(&drvdata->mode)) {
 725		spin_lock(&drvdata->spinlock);
 726		for (i = 0; i < drvdata->nr_cntr; i++)
 727			ret += sprintf(buf, "counter %d: %x\n",
 728				       i, config->cntr_val[i]);
 729		spin_unlock(&drvdata->spinlock);
 730		return ret;
 731	}
 732
 733	for (i = 0; i < drvdata->nr_cntr; i++) {
 734		val = etm_readl(drvdata, ETMCNTVRn(i));
 735		ret += sprintf(buf, "counter %d: %x\n", i, val);
 736	}
 737
 738	return ret;
 739}
 740
 741static ssize_t cntr_val_store(struct device *dev,
 742			      struct device_attribute *attr,
 743			      const char *buf, size_t size)
 744{
 745	int ret;
 746	unsigned long val;
 747	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 748	struct etm_config *config = &drvdata->config;
 749
 750	ret = kstrtoul(buf, 16, &val);
 751	if (ret)
 752		return ret;
 753
 754	spin_lock(&drvdata->spinlock);
 755	config->cntr_val[config->cntr_idx] = val;
 756	spin_unlock(&drvdata->spinlock);
 757
 758	return size;
 759}
 760static DEVICE_ATTR_RW(cntr_val);
 761
 762static ssize_t seq_12_event_show(struct device *dev,
 763				 struct device_attribute *attr, char *buf)
 764{
 765	unsigned long val;
 766	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 767	struct etm_config *config = &drvdata->config;
 768
 769	val = config->seq_12_event;
 770	return sprintf(buf, "%#lx\n", val);
 771}
 772
 773static ssize_t seq_12_event_store(struct device *dev,
 774				  struct device_attribute *attr,
 775				  const char *buf, size_t size)
 776{
 777	int ret;
 778	unsigned long val;
 779	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 780	struct etm_config *config = &drvdata->config;
 781
 782	ret = kstrtoul(buf, 16, &val);
 783	if (ret)
 784		return ret;
 785
 786	config->seq_12_event = val & ETM_EVENT_MASK;
 787	return size;
 788}
 789static DEVICE_ATTR_RW(seq_12_event);
 790
 791static ssize_t seq_21_event_show(struct device *dev,
 792				 struct device_attribute *attr, char *buf)
 793{
 794	unsigned long val;
 795	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 796	struct etm_config *config = &drvdata->config;
 797
 798	val = config->seq_21_event;
 799	return sprintf(buf, "%#lx\n", val);
 800}
 801
 802static ssize_t seq_21_event_store(struct device *dev,
 803				  struct device_attribute *attr,
 804				  const char *buf, size_t size)
 805{
 806	int ret;
 807	unsigned long val;
 808	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 809	struct etm_config *config = &drvdata->config;
 810
 811	ret = kstrtoul(buf, 16, &val);
 812	if (ret)
 813		return ret;
 814
 815	config->seq_21_event = val & ETM_EVENT_MASK;
 816	return size;
 817}
 818static DEVICE_ATTR_RW(seq_21_event);
 819
 820static ssize_t seq_23_event_show(struct device *dev,
 821				 struct device_attribute *attr, char *buf)
 822{
 823	unsigned long val;
 824	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 825	struct etm_config *config = &drvdata->config;
 826
 827	val = config->seq_23_event;
 828	return sprintf(buf, "%#lx\n", val);
 829}
 830
 831static ssize_t seq_23_event_store(struct device *dev,
 832				  struct device_attribute *attr,
 833				  const char *buf, size_t size)
 834{
 835	int ret;
 836	unsigned long val;
 837	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 838	struct etm_config *config = &drvdata->config;
 839
 840	ret = kstrtoul(buf, 16, &val);
 841	if (ret)
 842		return ret;
 843
 844	config->seq_23_event = val & ETM_EVENT_MASK;
 845	return size;
 846}
 847static DEVICE_ATTR_RW(seq_23_event);
 848
 849static ssize_t seq_31_event_show(struct device *dev,
 850				 struct device_attribute *attr, char *buf)
 851{
 852	unsigned long val;
 853	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 854	struct etm_config *config = &drvdata->config;
 855
 856	val = config->seq_31_event;
 857	return sprintf(buf, "%#lx\n", val);
 858}
 859
 860static ssize_t seq_31_event_store(struct device *dev,
 861				  struct device_attribute *attr,
 862				  const char *buf, size_t size)
 863{
 864	int ret;
 865	unsigned long val;
 866	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 867	struct etm_config *config = &drvdata->config;
 868
 869	ret = kstrtoul(buf, 16, &val);
 870	if (ret)
 871		return ret;
 872
 873	config->seq_31_event = val & ETM_EVENT_MASK;
 874	return size;
 875}
 876static DEVICE_ATTR_RW(seq_31_event);
 877
 878static ssize_t seq_32_event_show(struct device *dev,
 879				 struct device_attribute *attr, char *buf)
 880{
 881	unsigned long val;
 882	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 883	struct etm_config *config = &drvdata->config;
 884
 885	val = config->seq_32_event;
 886	return sprintf(buf, "%#lx\n", val);
 887}
 888
 889static ssize_t seq_32_event_store(struct device *dev,
 890				  struct device_attribute *attr,
 891				  const char *buf, size_t size)
 892{
 893	int ret;
 894	unsigned long val;
 895	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 896	struct etm_config *config = &drvdata->config;
 897
 898	ret = kstrtoul(buf, 16, &val);
 899	if (ret)
 900		return ret;
 901
 902	config->seq_32_event = val & ETM_EVENT_MASK;
 903	return size;
 904}
 905static DEVICE_ATTR_RW(seq_32_event);
 906
 907static ssize_t seq_13_event_show(struct device *dev,
 908				 struct device_attribute *attr, char *buf)
 909{
 910	unsigned long val;
 911	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 912	struct etm_config *config = &drvdata->config;
 913
 914	val = config->seq_13_event;
 915	return sprintf(buf, "%#lx\n", val);
 916}
 917
 918static ssize_t seq_13_event_store(struct device *dev,
 919				  struct device_attribute *attr,
 920				  const char *buf, size_t size)
 921{
 922	int ret;
 923	unsigned long val;
 924	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 925	struct etm_config *config = &drvdata->config;
 926
 927	ret = kstrtoul(buf, 16, &val);
 928	if (ret)
 929		return ret;
 930
 931	config->seq_13_event = val & ETM_EVENT_MASK;
 932	return size;
 933}
 934static DEVICE_ATTR_RW(seq_13_event);
 935
 936static ssize_t seq_curr_state_show(struct device *dev,
 937				   struct device_attribute *attr, char *buf)
 938{
 939	unsigned long val, flags;
 940	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 941	struct etm_config *config = &drvdata->config;
 942
 943	if (!local_read(&drvdata->mode)) {
 944		val = config->seq_curr_state;
 945		goto out;
 946	}
 947
 948	pm_runtime_get_sync(dev->parent);
 949	spin_lock_irqsave(&drvdata->spinlock, flags);
 950
 951	CS_UNLOCK(drvdata->base);
 952	val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
 953	CS_LOCK(drvdata->base);
 954
 955	spin_unlock_irqrestore(&drvdata->spinlock, flags);
 956	pm_runtime_put(dev->parent);
 957out:
 958	return sprintf(buf, "%#lx\n", val);
 959}
 960
 961static ssize_t seq_curr_state_store(struct device *dev,
 962				    struct device_attribute *attr,
 963				    const char *buf, size_t size)
 964{
 965	int ret;
 966	unsigned long val;
 967	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 968	struct etm_config *config = &drvdata->config;
 969
 970	ret = kstrtoul(buf, 16, &val);
 971	if (ret)
 972		return ret;
 973
 974	if (val > ETM_SEQ_STATE_MAX_VAL)
 975		return -EINVAL;
 976
 977	config->seq_curr_state = val;
 978
 979	return size;
 980}
 981static DEVICE_ATTR_RW(seq_curr_state);
 982
 983static ssize_t ctxid_idx_show(struct device *dev,
 984			      struct device_attribute *attr, char *buf)
 985{
 986	unsigned long val;
 987	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 988	struct etm_config *config = &drvdata->config;
 989
 990	val = config->ctxid_idx;
 991	return sprintf(buf, "%#lx\n", val);
 992}
 993
 994static ssize_t ctxid_idx_store(struct device *dev,
 995				struct device_attribute *attr,
 996				const char *buf, size_t size)
 997{
 998	int ret;
 999	unsigned long val;
1000	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1001	struct etm_config *config = &drvdata->config;
1002
1003	ret = kstrtoul(buf, 16, &val);
1004	if (ret)
1005		return ret;
1006
1007	if (val >= drvdata->nr_ctxid_cmp)
1008		return -EINVAL;
1009
1010	/*
1011	 * Use spinlock to ensure index doesn't change while it gets
1012	 * dereferenced multiple times within a spinlock block elsewhere.
1013	 */
1014	spin_lock(&drvdata->spinlock);
1015	config->ctxid_idx = val;
1016	spin_unlock(&drvdata->spinlock);
1017
1018	return size;
1019}
1020static DEVICE_ATTR_RW(ctxid_idx);
1021
1022static ssize_t ctxid_pid_show(struct device *dev,
1023			      struct device_attribute *attr, char *buf)
1024{
1025	unsigned long val;
1026	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1027	struct etm_config *config = &drvdata->config;
1028
1029	/*
1030	 * Don't use contextID tracing if coming from a PID namespace.  See
1031	 * comment in ctxid_pid_store().
1032	 */
1033	if (task_active_pid_ns(current) != &init_pid_ns)
1034		return -EINVAL;
1035
1036	spin_lock(&drvdata->spinlock);
1037	val = config->ctxid_pid[config->ctxid_idx];
1038	spin_unlock(&drvdata->spinlock);
1039
1040	return sprintf(buf, "%#lx\n", val);
1041}
1042
1043static ssize_t ctxid_pid_store(struct device *dev,
1044			       struct device_attribute *attr,
1045			       const char *buf, size_t size)
1046{
1047	int ret;
1048	unsigned long pid;
1049	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1050	struct etm_config *config = &drvdata->config;
1051
1052	/*
1053	 * When contextID tracing is enabled the tracers will insert the
1054	 * value found in the contextID register in the trace stream.  But if
1055	 * a process is in a namespace the PID of that process as seen from the
1056	 * namespace won't be what the kernel sees, something that makes the
1057	 * feature confusing and can potentially leak kernel only information.
1058	 * As such refuse to use the feature if @current is not in the initial
1059	 * PID namespace.
1060	 */
1061	if (task_active_pid_ns(current) != &init_pid_ns)
1062		return -EINVAL;
1063
1064	ret = kstrtoul(buf, 16, &pid);
1065	if (ret)
1066		return ret;
1067
 
 
1068	spin_lock(&drvdata->spinlock);
1069	config->ctxid_pid[config->ctxid_idx] = pid;
 
1070	spin_unlock(&drvdata->spinlock);
1071
1072	return size;
1073}
1074static DEVICE_ATTR_RW(ctxid_pid);
1075
1076static ssize_t ctxid_mask_show(struct device *dev,
1077			       struct device_attribute *attr, char *buf)
1078{
1079	unsigned long val;
1080	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1081	struct etm_config *config = &drvdata->config;
1082
1083	/*
1084	 * Don't use contextID tracing if coming from a PID namespace.  See
1085	 * comment in ctxid_pid_store().
1086	 */
1087	if (task_active_pid_ns(current) != &init_pid_ns)
1088		return -EINVAL;
1089
1090	val = config->ctxid_mask;
1091	return sprintf(buf, "%#lx\n", val);
1092}
1093
1094static ssize_t ctxid_mask_store(struct device *dev,
1095				struct device_attribute *attr,
1096				const char *buf, size_t size)
1097{
1098	int ret;
1099	unsigned long val;
1100	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1101	struct etm_config *config = &drvdata->config;
1102
1103	/*
1104	 * Don't use contextID tracing if coming from a PID namespace.  See
1105	 * comment in ctxid_pid_store().
1106	 */
1107	if (task_active_pid_ns(current) != &init_pid_ns)
1108		return -EINVAL;
1109
1110	ret = kstrtoul(buf, 16, &val);
1111	if (ret)
1112		return ret;
1113
1114	config->ctxid_mask = val;
1115	return size;
1116}
1117static DEVICE_ATTR_RW(ctxid_mask);
1118
1119static ssize_t sync_freq_show(struct device *dev,
1120			      struct device_attribute *attr, char *buf)
1121{
1122	unsigned long val;
1123	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1124	struct etm_config *config = &drvdata->config;
1125
1126	val = config->sync_freq;
1127	return sprintf(buf, "%#lx\n", val);
1128}
1129
1130static ssize_t sync_freq_store(struct device *dev,
1131			       struct device_attribute *attr,
1132			       const char *buf, size_t size)
1133{
1134	int ret;
1135	unsigned long val;
1136	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1137	struct etm_config *config = &drvdata->config;
1138
1139	ret = kstrtoul(buf, 16, &val);
1140	if (ret)
1141		return ret;
1142
1143	config->sync_freq = val & ETM_SYNC_MASK;
1144	return size;
1145}
1146static DEVICE_ATTR_RW(sync_freq);
1147
1148static ssize_t timestamp_event_show(struct device *dev,
1149				    struct device_attribute *attr, char *buf)
1150{
1151	unsigned long val;
1152	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1153	struct etm_config *config = &drvdata->config;
1154
1155	val = config->timestamp_event;
1156	return sprintf(buf, "%#lx\n", val);
1157}
1158
1159static ssize_t timestamp_event_store(struct device *dev,
1160				     struct device_attribute *attr,
1161				     const char *buf, size_t size)
1162{
1163	int ret;
1164	unsigned long val;
1165	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1166	struct etm_config *config = &drvdata->config;
1167
1168	ret = kstrtoul(buf, 16, &val);
1169	if (ret)
1170		return ret;
1171
1172	config->timestamp_event = val & ETM_EVENT_MASK;
1173	return size;
1174}
1175static DEVICE_ATTR_RW(timestamp_event);
1176
1177static ssize_t cpu_show(struct device *dev,
1178			struct device_attribute *attr, char *buf)
1179{
1180	int val;
1181	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1182
1183	val = drvdata->cpu;
1184	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1185
1186}
1187static DEVICE_ATTR_RO(cpu);
1188
1189static ssize_t traceid_show(struct device *dev,
1190			    struct device_attribute *attr, char *buf)
1191{
1192	unsigned long val;
1193	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1194
1195	val = etm_get_trace_id(drvdata);
1196
1197	return sprintf(buf, "%#lx\n", val);
1198}
1199
1200static ssize_t traceid_store(struct device *dev,
1201			     struct device_attribute *attr,
1202			     const char *buf, size_t size)
1203{
1204	int ret;
1205	unsigned long val;
1206	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1207
1208	ret = kstrtoul(buf, 16, &val);
1209	if (ret)
1210		return ret;
1211
1212	drvdata->traceid = val & ETM_TRACEID_MASK;
1213	return size;
1214}
1215static DEVICE_ATTR_RW(traceid);
1216
1217static struct attribute *coresight_etm_attrs[] = {
1218	&dev_attr_nr_addr_cmp.attr,
1219	&dev_attr_nr_cntr.attr,
1220	&dev_attr_nr_ctxid_cmp.attr,
1221	&dev_attr_etmsr.attr,
1222	&dev_attr_reset.attr,
1223	&dev_attr_mode.attr,
1224	&dev_attr_trigger_event.attr,
1225	&dev_attr_enable_event.attr,
1226	&dev_attr_fifofull_level.attr,
1227	&dev_attr_addr_idx.attr,
1228	&dev_attr_addr_single.attr,
1229	&dev_attr_addr_range.attr,
1230	&dev_attr_addr_start.attr,
1231	&dev_attr_addr_stop.attr,
1232	&dev_attr_addr_acctype.attr,
1233	&dev_attr_cntr_idx.attr,
1234	&dev_attr_cntr_rld_val.attr,
1235	&dev_attr_cntr_event.attr,
1236	&dev_attr_cntr_rld_event.attr,
1237	&dev_attr_cntr_val.attr,
1238	&dev_attr_seq_12_event.attr,
1239	&dev_attr_seq_21_event.attr,
1240	&dev_attr_seq_23_event.attr,
1241	&dev_attr_seq_31_event.attr,
1242	&dev_attr_seq_32_event.attr,
1243	&dev_attr_seq_13_event.attr,
1244	&dev_attr_seq_curr_state.attr,
1245	&dev_attr_ctxid_idx.attr,
1246	&dev_attr_ctxid_pid.attr,
1247	&dev_attr_ctxid_mask.attr,
1248	&dev_attr_sync_freq.attr,
1249	&dev_attr_timestamp_event.attr,
1250	&dev_attr_traceid.attr,
1251	&dev_attr_cpu.attr,
1252	NULL,
1253};
1254
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1255static struct attribute *coresight_etm_mgmt_attrs[] = {
1256	coresight_simple_reg32(etmccr, ETMCCR),
1257	coresight_simple_reg32(etmccer, ETMCCER),
1258	coresight_simple_reg32(etmscr, ETMSCR),
1259	coresight_simple_reg32(etmidr, ETMIDR),
1260	coresight_simple_reg32(etmcr, ETMCR),
1261	coresight_simple_reg32(etmtraceidr, ETMTRACEIDR),
1262	coresight_simple_reg32(etmteevr, ETMTEEVR),
1263	coresight_simple_reg32(etmtssvr, ETMTSSCR),
1264	coresight_simple_reg32(etmtecr1, ETMTECR1),
1265	coresight_simple_reg32(etmtecr2, ETMTECR2),
1266	NULL,
1267};
1268
1269static const struct attribute_group coresight_etm_group = {
1270	.attrs = coresight_etm_attrs,
1271};
1272
1273static const struct attribute_group coresight_etm_mgmt_group = {
1274	.attrs = coresight_etm_mgmt_attrs,
1275	.name = "mgmt",
1276};
1277
1278const struct attribute_group *coresight_etm_groups[] = {
1279	&coresight_etm_group,
1280	&coresight_etm_mgmt_group,
1281	NULL,
1282};
v4.17
 
   1/*
   2 * Copyright(C) 2015 Linaro Limited. All rights reserved.
   3 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 as published by
   7 * the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 * You should have received a copy of the GNU General Public License along with
  15 * this program.  If not, see <http://www.gnu.org/licenses/>.
  16 */
  17
 
  18#include <linux/pm_runtime.h>
  19#include <linux/sysfs.h>
  20#include "coresight-etm.h"
  21#include "coresight-priv.h"
  22
  23static ssize_t nr_addr_cmp_show(struct device *dev,
  24				struct device_attribute *attr, char *buf)
  25{
  26	unsigned long val;
  27	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  28
  29	val = drvdata->nr_addr_cmp;
  30	return sprintf(buf, "%#lx\n", val);
  31}
  32static DEVICE_ATTR_RO(nr_addr_cmp);
  33
  34static ssize_t nr_cntr_show(struct device *dev,
  35			    struct device_attribute *attr, char *buf)
  36{	unsigned long val;
  37	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  38
  39	val = drvdata->nr_cntr;
  40	return sprintf(buf, "%#lx\n", val);
  41}
  42static DEVICE_ATTR_RO(nr_cntr);
  43
  44static ssize_t nr_ctxid_cmp_show(struct device *dev,
  45				 struct device_attribute *attr, char *buf)
  46{
  47	unsigned long val;
  48	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  49
  50	val = drvdata->nr_ctxid_cmp;
  51	return sprintf(buf, "%#lx\n", val);
  52}
  53static DEVICE_ATTR_RO(nr_ctxid_cmp);
  54
  55static ssize_t etmsr_show(struct device *dev,
  56			  struct device_attribute *attr, char *buf)
  57{
  58	unsigned long flags, val;
  59	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  60
  61	pm_runtime_get_sync(drvdata->dev);
  62	spin_lock_irqsave(&drvdata->spinlock, flags);
  63	CS_UNLOCK(drvdata->base);
  64
  65	val = etm_readl(drvdata, ETMSR);
  66
  67	CS_LOCK(drvdata->base);
  68	spin_unlock_irqrestore(&drvdata->spinlock, flags);
  69	pm_runtime_put(drvdata->dev);
  70
  71	return sprintf(buf, "%#lx\n", val);
  72}
  73static DEVICE_ATTR_RO(etmsr);
  74
  75static ssize_t reset_store(struct device *dev,
  76			   struct device_attribute *attr,
  77			   const char *buf, size_t size)
  78{
  79	int i, ret;
  80	unsigned long val;
  81	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
  82	struct etm_config *config = &drvdata->config;
  83
  84	ret = kstrtoul(buf, 16, &val);
  85	if (ret)
  86		return ret;
  87
  88	if (val) {
  89		spin_lock(&drvdata->spinlock);
  90		memset(config, 0, sizeof(struct etm_config));
  91		config->mode = ETM_MODE_EXCLUDE;
  92		config->trigger_event = ETM_DEFAULT_EVENT_VAL;
  93		for (i = 0; i < drvdata->nr_addr_cmp; i++) {
  94			config->addr_type[i] = ETM_ADDR_TYPE_NONE;
  95		}
  96
  97		etm_set_default(config);
  98		spin_unlock(&drvdata->spinlock);
  99	}
 100
 101	return size;
 102}
 103static DEVICE_ATTR_WO(reset);
 104
 105static ssize_t mode_show(struct device *dev,
 106			 struct device_attribute *attr, char *buf)
 107{
 108	unsigned long val;
 109	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 110	struct etm_config *config = &drvdata->config;
 111
 112	val = config->mode;
 113	return sprintf(buf, "%#lx\n", val);
 114}
 115
 116static ssize_t mode_store(struct device *dev,
 117			  struct device_attribute *attr,
 118			  const char *buf, size_t size)
 119{
 120	int ret;
 121	unsigned long val;
 122	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 123	struct etm_config *config = &drvdata->config;
 124
 125	ret = kstrtoul(buf, 16, &val);
 126	if (ret)
 127		return ret;
 128
 129	spin_lock(&drvdata->spinlock);
 130	config->mode = val & ETM_MODE_ALL;
 131
 132	if (config->mode & ETM_MODE_EXCLUDE)
 133		config->enable_ctrl1 |= ETMTECR1_INC_EXC;
 134	else
 135		config->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
 136
 137	if (config->mode & ETM_MODE_CYCACC)
 138		config->ctrl |= ETMCR_CYC_ACC;
 139	else
 140		config->ctrl &= ~ETMCR_CYC_ACC;
 141
 142	if (config->mode & ETM_MODE_STALL) {
 143		if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
 144			dev_warn(drvdata->dev, "stall mode not supported\n");
 145			ret = -EINVAL;
 146			goto err_unlock;
 147		}
 148		config->ctrl |= ETMCR_STALL_MODE;
 149	} else
 150		config->ctrl &= ~ETMCR_STALL_MODE;
 151
 152	if (config->mode & ETM_MODE_TIMESTAMP) {
 153		if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
 154			dev_warn(drvdata->dev, "timestamp not supported\n");
 155			ret = -EINVAL;
 156			goto err_unlock;
 157		}
 158		config->ctrl |= ETMCR_TIMESTAMP_EN;
 159	} else
 160		config->ctrl &= ~ETMCR_TIMESTAMP_EN;
 161
 162	if (config->mode & ETM_MODE_CTXID)
 163		config->ctrl |= ETMCR_CTXID_SIZE;
 164	else
 165		config->ctrl &= ~ETMCR_CTXID_SIZE;
 166
 167	if (config->mode & ETM_MODE_BBROAD)
 168		config->ctrl |= ETMCR_BRANCH_BROADCAST;
 169	else
 170		config->ctrl &= ~ETMCR_BRANCH_BROADCAST;
 171
 172	if (config->mode & ETM_MODE_RET_STACK)
 173		config->ctrl |= ETMCR_RETURN_STACK;
 174	else
 175		config->ctrl &= ~ETMCR_RETURN_STACK;
 176
 177	if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
 178		etm_config_trace_mode(config);
 179
 180	spin_unlock(&drvdata->spinlock);
 181
 182	return size;
 183
 184err_unlock:
 185	spin_unlock(&drvdata->spinlock);
 186	return ret;
 187}
 188static DEVICE_ATTR_RW(mode);
 189
 190static ssize_t trigger_event_show(struct device *dev,
 191				  struct device_attribute *attr, char *buf)
 192{
 193	unsigned long val;
 194	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 195	struct etm_config *config = &drvdata->config;
 196
 197	val = config->trigger_event;
 198	return sprintf(buf, "%#lx\n", val);
 199}
 200
 201static ssize_t trigger_event_store(struct device *dev,
 202				   struct device_attribute *attr,
 203				   const char *buf, size_t size)
 204{
 205	int ret;
 206	unsigned long val;
 207	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 208	struct etm_config *config = &drvdata->config;
 209
 210	ret = kstrtoul(buf, 16, &val);
 211	if (ret)
 212		return ret;
 213
 214	config->trigger_event = val & ETM_EVENT_MASK;
 215
 216	return size;
 217}
 218static DEVICE_ATTR_RW(trigger_event);
 219
 220static ssize_t enable_event_show(struct device *dev,
 221				 struct device_attribute *attr, char *buf)
 222{
 223	unsigned long val;
 224	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 225	struct etm_config *config = &drvdata->config;
 226
 227	val = config->enable_event;
 228	return sprintf(buf, "%#lx\n", val);
 229}
 230
 231static ssize_t enable_event_store(struct device *dev,
 232				  struct device_attribute *attr,
 233				  const char *buf, size_t size)
 234{
 235	int ret;
 236	unsigned long val;
 237	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 238	struct etm_config *config = &drvdata->config;
 239
 240	ret = kstrtoul(buf, 16, &val);
 241	if (ret)
 242		return ret;
 243
 244	config->enable_event = val & ETM_EVENT_MASK;
 245
 246	return size;
 247}
 248static DEVICE_ATTR_RW(enable_event);
 249
 250static ssize_t fifofull_level_show(struct device *dev,
 251				   struct device_attribute *attr, char *buf)
 252{
 253	unsigned long val;
 254	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 255	struct etm_config *config = &drvdata->config;
 256
 257	val = config->fifofull_level;
 258	return sprintf(buf, "%#lx\n", val);
 259}
 260
 261static ssize_t fifofull_level_store(struct device *dev,
 262				    struct device_attribute *attr,
 263				    const char *buf, size_t size)
 264{
 265	int ret;
 266	unsigned long val;
 267	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 268	struct etm_config *config = &drvdata->config;
 269
 270	ret = kstrtoul(buf, 16, &val);
 271	if (ret)
 272		return ret;
 273
 274	config->fifofull_level = val;
 275
 276	return size;
 277}
 278static DEVICE_ATTR_RW(fifofull_level);
 279
 280static ssize_t addr_idx_show(struct device *dev,
 281			     struct device_attribute *attr, char *buf)
 282{
 283	unsigned long val;
 284	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 285	struct etm_config *config = &drvdata->config;
 286
 287	val = config->addr_idx;
 288	return sprintf(buf, "%#lx\n", val);
 289}
 290
 291static ssize_t addr_idx_store(struct device *dev,
 292			      struct device_attribute *attr,
 293			      const char *buf, size_t size)
 294{
 295	int ret;
 296	unsigned long val;
 297	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 298	struct etm_config *config = &drvdata->config;
 299
 300	ret = kstrtoul(buf, 16, &val);
 301	if (ret)
 302		return ret;
 303
 304	if (val >= drvdata->nr_addr_cmp)
 305		return -EINVAL;
 306
 307	/*
 308	 * Use spinlock to ensure index doesn't change while it gets
 309	 * dereferenced multiple times within a spinlock block elsewhere.
 310	 */
 311	spin_lock(&drvdata->spinlock);
 312	config->addr_idx = val;
 313	spin_unlock(&drvdata->spinlock);
 314
 315	return size;
 316}
 317static DEVICE_ATTR_RW(addr_idx);
 318
 319static ssize_t addr_single_show(struct device *dev,
 320				struct device_attribute *attr, char *buf)
 321{
 322	u8 idx;
 323	unsigned long val;
 324	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 325	struct etm_config *config = &drvdata->config;
 326
 327	spin_lock(&drvdata->spinlock);
 328	idx = config->addr_idx;
 329	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 330	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
 331		spin_unlock(&drvdata->spinlock);
 332		return -EINVAL;
 333	}
 334
 335	val = config->addr_val[idx];
 336	spin_unlock(&drvdata->spinlock);
 337
 338	return sprintf(buf, "%#lx\n", val);
 339}
 340
 341static ssize_t addr_single_store(struct device *dev,
 342				 struct device_attribute *attr,
 343				 const char *buf, size_t size)
 344{
 345	u8 idx;
 346	int ret;
 347	unsigned long val;
 348	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 349	struct etm_config *config = &drvdata->config;
 350
 351	ret = kstrtoul(buf, 16, &val);
 352	if (ret)
 353		return ret;
 354
 355	spin_lock(&drvdata->spinlock);
 356	idx = config->addr_idx;
 357	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 358	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
 359		spin_unlock(&drvdata->spinlock);
 360		return -EINVAL;
 361	}
 362
 363	config->addr_val[idx] = val;
 364	config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
 365	spin_unlock(&drvdata->spinlock);
 366
 367	return size;
 368}
 369static DEVICE_ATTR_RW(addr_single);
 370
 371static ssize_t addr_range_show(struct device *dev,
 372			       struct device_attribute *attr, char *buf)
 373{
 374	u8 idx;
 375	unsigned long val1, val2;
 376	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 377	struct etm_config *config = &drvdata->config;
 378
 379	spin_lock(&drvdata->spinlock);
 380	idx = config->addr_idx;
 381	if (idx % 2 != 0) {
 382		spin_unlock(&drvdata->spinlock);
 383		return -EPERM;
 384	}
 385	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
 386	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
 387	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
 388	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
 389		spin_unlock(&drvdata->spinlock);
 390		return -EPERM;
 391	}
 392
 393	val1 = config->addr_val[idx];
 394	val2 = config->addr_val[idx + 1];
 395	spin_unlock(&drvdata->spinlock);
 396
 397	return sprintf(buf, "%#lx %#lx\n", val1, val2);
 398}
 399
 400static ssize_t addr_range_store(struct device *dev,
 401			      struct device_attribute *attr,
 402			      const char *buf, size_t size)
 403{
 404	u8 idx;
 405	unsigned long val1, val2;
 406	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 407	struct etm_config *config = &drvdata->config;
 408
 409	if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
 410		return -EINVAL;
 411	/* Lower address comparator cannot have a higher address value */
 412	if (val1 > val2)
 413		return -EINVAL;
 414
 415	spin_lock(&drvdata->spinlock);
 416	idx = config->addr_idx;
 417	if (idx % 2 != 0) {
 418		spin_unlock(&drvdata->spinlock);
 419		return -EPERM;
 420	}
 421	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
 422	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
 423	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
 424	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
 425		spin_unlock(&drvdata->spinlock);
 426		return -EPERM;
 427	}
 428
 429	config->addr_val[idx] = val1;
 430	config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
 431	config->addr_val[idx + 1] = val2;
 432	config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
 433	config->enable_ctrl1 |= (1 << (idx/2));
 434	spin_unlock(&drvdata->spinlock);
 435
 436	return size;
 437}
 438static DEVICE_ATTR_RW(addr_range);
 439
 440static ssize_t addr_start_show(struct device *dev,
 441			       struct device_attribute *attr, char *buf)
 442{
 443	u8 idx;
 444	unsigned long val;
 445	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 446	struct etm_config *config = &drvdata->config;
 447
 448	spin_lock(&drvdata->spinlock);
 449	idx = config->addr_idx;
 450	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 451	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
 452		spin_unlock(&drvdata->spinlock);
 453		return -EPERM;
 454	}
 455
 456	val = config->addr_val[idx];
 457	spin_unlock(&drvdata->spinlock);
 458
 459	return sprintf(buf, "%#lx\n", val);
 460}
 461
 462static ssize_t addr_start_store(struct device *dev,
 463				struct device_attribute *attr,
 464				const char *buf, size_t size)
 465{
 466	u8 idx;
 467	int ret;
 468	unsigned long val;
 469	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 470	struct etm_config *config = &drvdata->config;
 471
 472	ret = kstrtoul(buf, 16, &val);
 473	if (ret)
 474		return ret;
 475
 476	spin_lock(&drvdata->spinlock);
 477	idx = config->addr_idx;
 478	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 479	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
 480		spin_unlock(&drvdata->spinlock);
 481		return -EPERM;
 482	}
 483
 484	config->addr_val[idx] = val;
 485	config->addr_type[idx] = ETM_ADDR_TYPE_START;
 486	config->startstop_ctrl |= (1 << idx);
 487	config->enable_ctrl1 |= BIT(25);
 488	spin_unlock(&drvdata->spinlock);
 489
 490	return size;
 491}
 492static DEVICE_ATTR_RW(addr_start);
 493
 494static ssize_t addr_stop_show(struct device *dev,
 495			      struct device_attribute *attr, char *buf)
 496{
 497	u8 idx;
 498	unsigned long val;
 499	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 500	struct etm_config *config = &drvdata->config;
 501
 502	spin_lock(&drvdata->spinlock);
 503	idx = config->addr_idx;
 504	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 505	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
 506		spin_unlock(&drvdata->spinlock);
 507		return -EPERM;
 508	}
 509
 510	val = config->addr_val[idx];
 511	spin_unlock(&drvdata->spinlock);
 512
 513	return sprintf(buf, "%#lx\n", val);
 514}
 515
 516static ssize_t addr_stop_store(struct device *dev,
 517			       struct device_attribute *attr,
 518			       const char *buf, size_t size)
 519{
 520	u8 idx;
 521	int ret;
 522	unsigned long val;
 523	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 524	struct etm_config *config = &drvdata->config;
 525
 526	ret = kstrtoul(buf, 16, &val);
 527	if (ret)
 528		return ret;
 529
 530	spin_lock(&drvdata->spinlock);
 531	idx = config->addr_idx;
 532	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
 533	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
 534		spin_unlock(&drvdata->spinlock);
 535		return -EPERM;
 536	}
 537
 538	config->addr_val[idx] = val;
 539	config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
 540	config->startstop_ctrl |= (1 << (idx + 16));
 541	config->enable_ctrl1 |= ETMTECR1_START_STOP;
 542	spin_unlock(&drvdata->spinlock);
 543
 544	return size;
 545}
 546static DEVICE_ATTR_RW(addr_stop);
 547
 548static ssize_t addr_acctype_show(struct device *dev,
 549				 struct device_attribute *attr, char *buf)
 550{
 551	unsigned long val;
 552	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 553	struct etm_config *config = &drvdata->config;
 554
 555	spin_lock(&drvdata->spinlock);
 556	val = config->addr_acctype[config->addr_idx];
 557	spin_unlock(&drvdata->spinlock);
 558
 559	return sprintf(buf, "%#lx\n", val);
 560}
 561
 562static ssize_t addr_acctype_store(struct device *dev,
 563				  struct device_attribute *attr,
 564				  const char *buf, size_t size)
 565{
 566	int ret;
 567	unsigned long val;
 568	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 569	struct etm_config *config = &drvdata->config;
 570
 571	ret = kstrtoul(buf, 16, &val);
 572	if (ret)
 573		return ret;
 574
 575	spin_lock(&drvdata->spinlock);
 576	config->addr_acctype[config->addr_idx] = val;
 577	spin_unlock(&drvdata->spinlock);
 578
 579	return size;
 580}
 581static DEVICE_ATTR_RW(addr_acctype);
 582
 583static ssize_t cntr_idx_show(struct device *dev,
 584			     struct device_attribute *attr, char *buf)
 585{
 586	unsigned long val;
 587	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 588	struct etm_config *config = &drvdata->config;
 589
 590	val = config->cntr_idx;
 591	return sprintf(buf, "%#lx\n", val);
 592}
 593
 594static ssize_t cntr_idx_store(struct device *dev,
 595			      struct device_attribute *attr,
 596			      const char *buf, size_t size)
 597{
 598	int ret;
 599	unsigned long val;
 600	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 601	struct etm_config *config = &drvdata->config;
 602
 603	ret = kstrtoul(buf, 16, &val);
 604	if (ret)
 605		return ret;
 606
 607	if (val >= drvdata->nr_cntr)
 608		return -EINVAL;
 609	/*
 610	 * Use spinlock to ensure index doesn't change while it gets
 611	 * dereferenced multiple times within a spinlock block elsewhere.
 612	 */
 613	spin_lock(&drvdata->spinlock);
 614	config->cntr_idx = val;
 615	spin_unlock(&drvdata->spinlock);
 616
 617	return size;
 618}
 619static DEVICE_ATTR_RW(cntr_idx);
 620
 621static ssize_t cntr_rld_val_show(struct device *dev,
 622				 struct device_attribute *attr, char *buf)
 623{
 624	unsigned long val;
 625	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 626	struct etm_config *config = &drvdata->config;
 627
 628	spin_lock(&drvdata->spinlock);
 629	val = config->cntr_rld_val[config->cntr_idx];
 630	spin_unlock(&drvdata->spinlock);
 631
 632	return sprintf(buf, "%#lx\n", val);
 633}
 634
 635static ssize_t cntr_rld_val_store(struct device *dev,
 636				  struct device_attribute *attr,
 637				  const char *buf, size_t size)
 638{
 639	int ret;
 640	unsigned long val;
 641	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 642	struct etm_config *config = &drvdata->config;
 643
 644	ret = kstrtoul(buf, 16, &val);
 645	if (ret)
 646		return ret;
 647
 648	spin_lock(&drvdata->spinlock);
 649	config->cntr_rld_val[config->cntr_idx] = val;
 650	spin_unlock(&drvdata->spinlock);
 651
 652	return size;
 653}
 654static DEVICE_ATTR_RW(cntr_rld_val);
 655
 656static ssize_t cntr_event_show(struct device *dev,
 657			       struct device_attribute *attr, char *buf)
 658{
 659	unsigned long val;
 660	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 661	struct etm_config *config = &drvdata->config;
 662
 663	spin_lock(&drvdata->spinlock);
 664	val = config->cntr_event[config->cntr_idx];
 665	spin_unlock(&drvdata->spinlock);
 666
 667	return sprintf(buf, "%#lx\n", val);
 668}
 669
 670static ssize_t cntr_event_store(struct device *dev,
 671				struct device_attribute *attr,
 672				const char *buf, size_t size)
 673{
 674	int ret;
 675	unsigned long val;
 676	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 677	struct etm_config *config = &drvdata->config;
 678
 679	ret = kstrtoul(buf, 16, &val);
 680	if (ret)
 681		return ret;
 682
 683	spin_lock(&drvdata->spinlock);
 684	config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
 685	spin_unlock(&drvdata->spinlock);
 686
 687	return size;
 688}
 689static DEVICE_ATTR_RW(cntr_event);
 690
 691static ssize_t cntr_rld_event_show(struct device *dev,
 692				   struct device_attribute *attr, char *buf)
 693{
 694	unsigned long val;
 695	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 696	struct etm_config *config = &drvdata->config;
 697
 698	spin_lock(&drvdata->spinlock);
 699	val = config->cntr_rld_event[config->cntr_idx];
 700	spin_unlock(&drvdata->spinlock);
 701
 702	return sprintf(buf, "%#lx\n", val);
 703}
 704
 705static ssize_t cntr_rld_event_store(struct device *dev,
 706				    struct device_attribute *attr,
 707				    const char *buf, size_t size)
 708{
 709	int ret;
 710	unsigned long val;
 711	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 712	struct etm_config *config = &drvdata->config;
 713
 714	ret = kstrtoul(buf, 16, &val);
 715	if (ret)
 716		return ret;
 717
 718	spin_lock(&drvdata->spinlock);
 719	config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
 720	spin_unlock(&drvdata->spinlock);
 721
 722	return size;
 723}
 724static DEVICE_ATTR_RW(cntr_rld_event);
 725
 726static ssize_t cntr_val_show(struct device *dev,
 727			     struct device_attribute *attr, char *buf)
 728{
 729	int i, ret = 0;
 730	u32 val;
 731	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 732	struct etm_config *config = &drvdata->config;
 733
 734	if (!local_read(&drvdata->mode)) {
 735		spin_lock(&drvdata->spinlock);
 736		for (i = 0; i < drvdata->nr_cntr; i++)
 737			ret += sprintf(buf, "counter %d: %x\n",
 738				       i, config->cntr_val[i]);
 739		spin_unlock(&drvdata->spinlock);
 740		return ret;
 741	}
 742
 743	for (i = 0; i < drvdata->nr_cntr; i++) {
 744		val = etm_readl(drvdata, ETMCNTVRn(i));
 745		ret += sprintf(buf, "counter %d: %x\n", i, val);
 746	}
 747
 748	return ret;
 749}
 750
 751static ssize_t cntr_val_store(struct device *dev,
 752			      struct device_attribute *attr,
 753			      const char *buf, size_t size)
 754{
 755	int ret;
 756	unsigned long val;
 757	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 758	struct etm_config *config = &drvdata->config;
 759
 760	ret = kstrtoul(buf, 16, &val);
 761	if (ret)
 762		return ret;
 763
 764	spin_lock(&drvdata->spinlock);
 765	config->cntr_val[config->cntr_idx] = val;
 766	spin_unlock(&drvdata->spinlock);
 767
 768	return size;
 769}
 770static DEVICE_ATTR_RW(cntr_val);
 771
 772static ssize_t seq_12_event_show(struct device *dev,
 773				 struct device_attribute *attr, char *buf)
 774{
 775	unsigned long val;
 776	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 777	struct etm_config *config = &drvdata->config;
 778
 779	val = config->seq_12_event;
 780	return sprintf(buf, "%#lx\n", val);
 781}
 782
 783static ssize_t seq_12_event_store(struct device *dev,
 784				  struct device_attribute *attr,
 785				  const char *buf, size_t size)
 786{
 787	int ret;
 788	unsigned long val;
 789	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 790	struct etm_config *config = &drvdata->config;
 791
 792	ret = kstrtoul(buf, 16, &val);
 793	if (ret)
 794		return ret;
 795
 796	config->seq_12_event = val & ETM_EVENT_MASK;
 797	return size;
 798}
 799static DEVICE_ATTR_RW(seq_12_event);
 800
 801static ssize_t seq_21_event_show(struct device *dev,
 802				 struct device_attribute *attr, char *buf)
 803{
 804	unsigned long val;
 805	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 806	struct etm_config *config = &drvdata->config;
 807
 808	val = config->seq_21_event;
 809	return sprintf(buf, "%#lx\n", val);
 810}
 811
 812static ssize_t seq_21_event_store(struct device *dev,
 813				  struct device_attribute *attr,
 814				  const char *buf, size_t size)
 815{
 816	int ret;
 817	unsigned long val;
 818	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 819	struct etm_config *config = &drvdata->config;
 820
 821	ret = kstrtoul(buf, 16, &val);
 822	if (ret)
 823		return ret;
 824
 825	config->seq_21_event = val & ETM_EVENT_MASK;
 826	return size;
 827}
 828static DEVICE_ATTR_RW(seq_21_event);
 829
 830static ssize_t seq_23_event_show(struct device *dev,
 831				 struct device_attribute *attr, char *buf)
 832{
 833	unsigned long val;
 834	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 835	struct etm_config *config = &drvdata->config;
 836
 837	val = config->seq_23_event;
 838	return sprintf(buf, "%#lx\n", val);
 839}
 840
 841static ssize_t seq_23_event_store(struct device *dev,
 842				  struct device_attribute *attr,
 843				  const char *buf, size_t size)
 844{
 845	int ret;
 846	unsigned long val;
 847	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 848	struct etm_config *config = &drvdata->config;
 849
 850	ret = kstrtoul(buf, 16, &val);
 851	if (ret)
 852		return ret;
 853
 854	config->seq_23_event = val & ETM_EVENT_MASK;
 855	return size;
 856}
 857static DEVICE_ATTR_RW(seq_23_event);
 858
 859static ssize_t seq_31_event_show(struct device *dev,
 860				 struct device_attribute *attr, char *buf)
 861{
 862	unsigned long val;
 863	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 864	struct etm_config *config = &drvdata->config;
 865
 866	val = config->seq_31_event;
 867	return sprintf(buf, "%#lx\n", val);
 868}
 869
 870static ssize_t seq_31_event_store(struct device *dev,
 871				  struct device_attribute *attr,
 872				  const char *buf, size_t size)
 873{
 874	int ret;
 875	unsigned long val;
 876	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 877	struct etm_config *config = &drvdata->config;
 878
 879	ret = kstrtoul(buf, 16, &val);
 880	if (ret)
 881		return ret;
 882
 883	config->seq_31_event = val & ETM_EVENT_MASK;
 884	return size;
 885}
 886static DEVICE_ATTR_RW(seq_31_event);
 887
 888static ssize_t seq_32_event_show(struct device *dev,
 889				 struct device_attribute *attr, char *buf)
 890{
 891	unsigned long val;
 892	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 893	struct etm_config *config = &drvdata->config;
 894
 895	val = config->seq_32_event;
 896	return sprintf(buf, "%#lx\n", val);
 897}
 898
 899static ssize_t seq_32_event_store(struct device *dev,
 900				  struct device_attribute *attr,
 901				  const char *buf, size_t size)
 902{
 903	int ret;
 904	unsigned long val;
 905	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 906	struct etm_config *config = &drvdata->config;
 907
 908	ret = kstrtoul(buf, 16, &val);
 909	if (ret)
 910		return ret;
 911
 912	config->seq_32_event = val & ETM_EVENT_MASK;
 913	return size;
 914}
 915static DEVICE_ATTR_RW(seq_32_event);
 916
 917static ssize_t seq_13_event_show(struct device *dev,
 918				 struct device_attribute *attr, char *buf)
 919{
 920	unsigned long val;
 921	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 922	struct etm_config *config = &drvdata->config;
 923
 924	val = config->seq_13_event;
 925	return sprintf(buf, "%#lx\n", val);
 926}
 927
 928static ssize_t seq_13_event_store(struct device *dev,
 929				  struct device_attribute *attr,
 930				  const char *buf, size_t size)
 931{
 932	int ret;
 933	unsigned long val;
 934	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 935	struct etm_config *config = &drvdata->config;
 936
 937	ret = kstrtoul(buf, 16, &val);
 938	if (ret)
 939		return ret;
 940
 941	config->seq_13_event = val & ETM_EVENT_MASK;
 942	return size;
 943}
 944static DEVICE_ATTR_RW(seq_13_event);
 945
 946static ssize_t seq_curr_state_show(struct device *dev,
 947				   struct device_attribute *attr, char *buf)
 948{
 949	unsigned long val, flags;
 950	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 951	struct etm_config *config = &drvdata->config;
 952
 953	if (!local_read(&drvdata->mode)) {
 954		val = config->seq_curr_state;
 955		goto out;
 956	}
 957
 958	pm_runtime_get_sync(drvdata->dev);
 959	spin_lock_irqsave(&drvdata->spinlock, flags);
 960
 961	CS_UNLOCK(drvdata->base);
 962	val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
 963	CS_LOCK(drvdata->base);
 964
 965	spin_unlock_irqrestore(&drvdata->spinlock, flags);
 966	pm_runtime_put(drvdata->dev);
 967out:
 968	return sprintf(buf, "%#lx\n", val);
 969}
 970
 971static ssize_t seq_curr_state_store(struct device *dev,
 972				    struct device_attribute *attr,
 973				    const char *buf, size_t size)
 974{
 975	int ret;
 976	unsigned long val;
 977	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 978	struct etm_config *config = &drvdata->config;
 979
 980	ret = kstrtoul(buf, 16, &val);
 981	if (ret)
 982		return ret;
 983
 984	if (val > ETM_SEQ_STATE_MAX_VAL)
 985		return -EINVAL;
 986
 987	config->seq_curr_state = val;
 988
 989	return size;
 990}
 991static DEVICE_ATTR_RW(seq_curr_state);
 992
 993static ssize_t ctxid_idx_show(struct device *dev,
 994			      struct device_attribute *attr, char *buf)
 995{
 996	unsigned long val;
 997	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
 998	struct etm_config *config = &drvdata->config;
 999
1000	val = config->ctxid_idx;
1001	return sprintf(buf, "%#lx\n", val);
1002}
1003
1004static ssize_t ctxid_idx_store(struct device *dev,
1005				struct device_attribute *attr,
1006				const char *buf, size_t size)
1007{
1008	int ret;
1009	unsigned long val;
1010	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1011	struct etm_config *config = &drvdata->config;
1012
1013	ret = kstrtoul(buf, 16, &val);
1014	if (ret)
1015		return ret;
1016
1017	if (val >= drvdata->nr_ctxid_cmp)
1018		return -EINVAL;
1019
1020	/*
1021	 * Use spinlock to ensure index doesn't change while it gets
1022	 * dereferenced multiple times within a spinlock block elsewhere.
1023	 */
1024	spin_lock(&drvdata->spinlock);
1025	config->ctxid_idx = val;
1026	spin_unlock(&drvdata->spinlock);
1027
1028	return size;
1029}
1030static DEVICE_ATTR_RW(ctxid_idx);
1031
1032static ssize_t ctxid_pid_show(struct device *dev,
1033			      struct device_attribute *attr, char *buf)
1034{
1035	unsigned long val;
1036	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1037	struct etm_config *config = &drvdata->config;
1038
 
 
 
 
 
 
 
1039	spin_lock(&drvdata->spinlock);
1040	val = config->ctxid_vpid[config->ctxid_idx];
1041	spin_unlock(&drvdata->spinlock);
1042
1043	return sprintf(buf, "%#lx\n", val);
1044}
1045
1046static ssize_t ctxid_pid_store(struct device *dev,
1047			       struct device_attribute *attr,
1048			       const char *buf, size_t size)
1049{
1050	int ret;
1051	unsigned long vpid, pid;
1052	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1053	struct etm_config *config = &drvdata->config;
1054
1055	ret = kstrtoul(buf, 16, &vpid);
 
 
 
 
 
 
 
 
 
 
 
 
1056	if (ret)
1057		return ret;
1058
1059	pid = coresight_vpid_to_pid(vpid);
1060
1061	spin_lock(&drvdata->spinlock);
1062	config->ctxid_pid[config->ctxid_idx] = pid;
1063	config->ctxid_vpid[config->ctxid_idx] = vpid;
1064	spin_unlock(&drvdata->spinlock);
1065
1066	return size;
1067}
1068static DEVICE_ATTR_RW(ctxid_pid);
1069
1070static ssize_t ctxid_mask_show(struct device *dev,
1071			       struct device_attribute *attr, char *buf)
1072{
1073	unsigned long val;
1074	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1075	struct etm_config *config = &drvdata->config;
1076
 
 
 
 
 
 
 
1077	val = config->ctxid_mask;
1078	return sprintf(buf, "%#lx\n", val);
1079}
1080
1081static ssize_t ctxid_mask_store(struct device *dev,
1082				struct device_attribute *attr,
1083				const char *buf, size_t size)
1084{
1085	int ret;
1086	unsigned long val;
1087	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1088	struct etm_config *config = &drvdata->config;
1089
 
 
 
 
 
 
 
1090	ret = kstrtoul(buf, 16, &val);
1091	if (ret)
1092		return ret;
1093
1094	config->ctxid_mask = val;
1095	return size;
1096}
1097static DEVICE_ATTR_RW(ctxid_mask);
1098
1099static ssize_t sync_freq_show(struct device *dev,
1100			      struct device_attribute *attr, char *buf)
1101{
1102	unsigned long val;
1103	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1104	struct etm_config *config = &drvdata->config;
1105
1106	val = config->sync_freq;
1107	return sprintf(buf, "%#lx\n", val);
1108}
1109
1110static ssize_t sync_freq_store(struct device *dev,
1111			       struct device_attribute *attr,
1112			       const char *buf, size_t size)
1113{
1114	int ret;
1115	unsigned long val;
1116	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1117	struct etm_config *config = &drvdata->config;
1118
1119	ret = kstrtoul(buf, 16, &val);
1120	if (ret)
1121		return ret;
1122
1123	config->sync_freq = val & ETM_SYNC_MASK;
1124	return size;
1125}
1126static DEVICE_ATTR_RW(sync_freq);
1127
1128static ssize_t timestamp_event_show(struct device *dev,
1129				    struct device_attribute *attr, char *buf)
1130{
1131	unsigned long val;
1132	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1133	struct etm_config *config = &drvdata->config;
1134
1135	val = config->timestamp_event;
1136	return sprintf(buf, "%#lx\n", val);
1137}
1138
1139static ssize_t timestamp_event_store(struct device *dev,
1140				     struct device_attribute *attr,
1141				     const char *buf, size_t size)
1142{
1143	int ret;
1144	unsigned long val;
1145	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1146	struct etm_config *config = &drvdata->config;
1147
1148	ret = kstrtoul(buf, 16, &val);
1149	if (ret)
1150		return ret;
1151
1152	config->timestamp_event = val & ETM_EVENT_MASK;
1153	return size;
1154}
1155static DEVICE_ATTR_RW(timestamp_event);
1156
1157static ssize_t cpu_show(struct device *dev,
1158			struct device_attribute *attr, char *buf)
1159{
1160	int val;
1161	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1162
1163	val = drvdata->cpu;
1164	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1165
1166}
1167static DEVICE_ATTR_RO(cpu);
1168
1169static ssize_t traceid_show(struct device *dev,
1170			    struct device_attribute *attr, char *buf)
1171{
1172	unsigned long val;
1173	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1174
1175	val = etm_get_trace_id(drvdata);
1176
1177	return sprintf(buf, "%#lx\n", val);
1178}
1179
1180static ssize_t traceid_store(struct device *dev,
1181			     struct device_attribute *attr,
1182			     const char *buf, size_t size)
1183{
1184	int ret;
1185	unsigned long val;
1186	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1187
1188	ret = kstrtoul(buf, 16, &val);
1189	if (ret)
1190		return ret;
1191
1192	drvdata->traceid = val & ETM_TRACEID_MASK;
1193	return size;
1194}
1195static DEVICE_ATTR_RW(traceid);
1196
1197static struct attribute *coresight_etm_attrs[] = {
1198	&dev_attr_nr_addr_cmp.attr,
1199	&dev_attr_nr_cntr.attr,
1200	&dev_attr_nr_ctxid_cmp.attr,
1201	&dev_attr_etmsr.attr,
1202	&dev_attr_reset.attr,
1203	&dev_attr_mode.attr,
1204	&dev_attr_trigger_event.attr,
1205	&dev_attr_enable_event.attr,
1206	&dev_attr_fifofull_level.attr,
1207	&dev_attr_addr_idx.attr,
1208	&dev_attr_addr_single.attr,
1209	&dev_attr_addr_range.attr,
1210	&dev_attr_addr_start.attr,
1211	&dev_attr_addr_stop.attr,
1212	&dev_attr_addr_acctype.attr,
1213	&dev_attr_cntr_idx.attr,
1214	&dev_attr_cntr_rld_val.attr,
1215	&dev_attr_cntr_event.attr,
1216	&dev_attr_cntr_rld_event.attr,
1217	&dev_attr_cntr_val.attr,
1218	&dev_attr_seq_12_event.attr,
1219	&dev_attr_seq_21_event.attr,
1220	&dev_attr_seq_23_event.attr,
1221	&dev_attr_seq_31_event.attr,
1222	&dev_attr_seq_32_event.attr,
1223	&dev_attr_seq_13_event.attr,
1224	&dev_attr_seq_curr_state.attr,
1225	&dev_attr_ctxid_idx.attr,
1226	&dev_attr_ctxid_pid.attr,
1227	&dev_attr_ctxid_mask.attr,
1228	&dev_attr_sync_freq.attr,
1229	&dev_attr_timestamp_event.attr,
1230	&dev_attr_traceid.attr,
1231	&dev_attr_cpu.attr,
1232	NULL,
1233};
1234
1235#define coresight_etm3x_reg(name, offset)			\
1236	coresight_simple_reg32(struct etm_drvdata, name, offset)
1237
1238coresight_etm3x_reg(etmccr, ETMCCR);
1239coresight_etm3x_reg(etmccer, ETMCCER);
1240coresight_etm3x_reg(etmscr, ETMSCR);
1241coresight_etm3x_reg(etmidr, ETMIDR);
1242coresight_etm3x_reg(etmcr, ETMCR);
1243coresight_etm3x_reg(etmtraceidr, ETMTRACEIDR);
1244coresight_etm3x_reg(etmteevr, ETMTEEVR);
1245coresight_etm3x_reg(etmtssvr, ETMTSSCR);
1246coresight_etm3x_reg(etmtecr1, ETMTECR1);
1247coresight_etm3x_reg(etmtecr2, ETMTECR2);
1248
1249static struct attribute *coresight_etm_mgmt_attrs[] = {
1250	&dev_attr_etmccr.attr,
1251	&dev_attr_etmccer.attr,
1252	&dev_attr_etmscr.attr,
1253	&dev_attr_etmidr.attr,
1254	&dev_attr_etmcr.attr,
1255	&dev_attr_etmtraceidr.attr,
1256	&dev_attr_etmteevr.attr,
1257	&dev_attr_etmtssvr.attr,
1258	&dev_attr_etmtecr1.attr,
1259	&dev_attr_etmtecr2.attr,
1260	NULL,
1261};
1262
1263static const struct attribute_group coresight_etm_group = {
1264	.attrs = coresight_etm_attrs,
1265};
1266
1267static const struct attribute_group coresight_etm_mgmt_group = {
1268	.attrs = coresight_etm_mgmt_attrs,
1269	.name = "mgmt",
1270};
1271
1272const struct attribute_group *coresight_etm_groups[] = {
1273	&coresight_etm_group,
1274	&coresight_etm_mgmt_group,
1275	NULL,
1276};