Linux Audio

Check our new training course

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