coresight-etm3x-sysfs.c 31.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/*
 * Copyright(C) 2015 Linaro Limited. All rights reserved.
 * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/pm_runtime.h>
#include <linux/sysfs.h>
#include "coresight-etm.h"

static ssize_t nr_addr_cmp_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	val = drvdata->nr_addr_cmp;
	return sprintf(buf, "%#lx\n", val);
}
static DEVICE_ATTR_RO(nr_addr_cmp);

static ssize_t nr_cntr_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	val = drvdata->nr_cntr;
	return sprintf(buf, "%#lx\n", val);
}
static DEVICE_ATTR_RO(nr_cntr);

static ssize_t nr_ctxid_cmp_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	val = drvdata->nr_ctxid_cmp;
	return sprintf(buf, "%#lx\n", val);
}
static DEVICE_ATTR_RO(nr_ctxid_cmp);

static ssize_t etmsr_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	unsigned long flags, val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	pm_runtime_get_sync(drvdata->dev);
	spin_lock_irqsave(&drvdata->spinlock, flags);
	CS_UNLOCK(drvdata->base);

	val = etm_readl(drvdata, ETMSR);

	CS_LOCK(drvdata->base);
	spin_unlock_irqrestore(&drvdata->spinlock, flags);
	pm_runtime_put(drvdata->dev);

	return sprintf(buf, "%#lx\n", val);
}
static DEVICE_ATTR_RO(etmsr);

static ssize_t reset_store(struct device *dev,
			   struct device_attribute *attr,
			   const char *buf, size_t size)
{
	int i, ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
81
	struct etm_config *config = &drvdata->config;
82 83 84 85 86 87 88

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	if (val) {
		spin_lock(&drvdata->spinlock);
89 90 91
		memset(config, 0, sizeof(struct etm_config));
		config->mode = ETM_MODE_EXCLUDE;
		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 98 99 100 101 102 103 104 105 106 107 108
		spin_unlock(&drvdata->spinlock);
	}

	return size;
}
static DEVICE_ATTR_WO(reset);

static ssize_t mode_show(struct device *dev,
			 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
109
	struct etm_config *config = &drvdata->config;
110

111
	val = config->mode;
112 113 114 115 116 117 118 119 120 121
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t mode_store(struct device *dev,
			  struct device_attribute *attr,
			  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
122
	struct etm_config *config = &drvdata->config;
123 124 125 126 127 128

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
129
	config->mode = val & ETM_MODE_ALL;
130

131 132
	if (config->mode & ETM_MODE_EXCLUDE)
		config->enable_ctrl1 |= ETMTECR1_INC_EXC;
133
	else
134
		config->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
135

136 137
	if (config->mode & ETM_MODE_CYCACC)
		config->ctrl |= ETMCR_CYC_ACC;
138
	else
139
		config->ctrl &= ~ETMCR_CYC_ACC;
140

141
	if (config->mode & ETM_MODE_STALL) {
142 143 144 145 146
		if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
			dev_warn(drvdata->dev, "stall mode not supported\n");
			ret = -EINVAL;
			goto err_unlock;
		}
147
		config->ctrl |= ETMCR_STALL_MODE;
148
	 } else
149
		config->ctrl &= ~ETMCR_STALL_MODE;
150

151
	if (config->mode & ETM_MODE_TIMESTAMP) {
152 153 154 155 156
		if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
			dev_warn(drvdata->dev, "timestamp not supported\n");
			ret = -EINVAL;
			goto err_unlock;
		}
157
		config->ctrl |= ETMCR_TIMESTAMP_EN;
158
	} else
159
		config->ctrl &= ~ETMCR_TIMESTAMP_EN;
160

161 162
	if (config->mode & ETM_MODE_CTXID)
		config->ctrl |= ETMCR_CTXID_SIZE;
163
	else
164
		config->ctrl &= ~ETMCR_CTXID_SIZE;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	spin_unlock(&drvdata->spinlock);

	return size;

err_unlock:
	spin_unlock(&drvdata->spinlock);
	return ret;
}
static DEVICE_ATTR_RW(mode);

static ssize_t trigger_event_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
180
	struct etm_config *config = &drvdata->config;
181

182
	val = config->trigger_event;
183 184 185 186 187 188 189 190 191 192
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t trigger_event_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
193
	struct etm_config *config = &drvdata->config;
194 195 196 197 198

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

199
	config->trigger_event = val & ETM_EVENT_MASK;
200 201 202 203 204 205 206 207 208 209

	return size;
}
static DEVICE_ATTR_RW(trigger_event);

static ssize_t enable_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
210
	struct etm_config *config = &drvdata->config;
211

212
	val = config->enable_event;
213 214 215 216 217 218 219 220 221 222
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t enable_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
223
	struct etm_config *config = &drvdata->config;
224 225 226 227 228

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

229
	config->enable_event = val & ETM_EVENT_MASK;
230 231 232 233 234 235 236 237 238 239

	return size;
}
static DEVICE_ATTR_RW(enable_event);

static ssize_t fifofull_level_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
240
	struct etm_config *config = &drvdata->config;
241

242
	val = config->fifofull_level;
243 244 245 246 247 248 249 250 251 252
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t fifofull_level_store(struct device *dev,
				    struct device_attribute *attr,
				    const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
253
	struct etm_config *config = &drvdata->config;
254 255 256 257 258

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

259
	config->fifofull_level = val;
260 261 262 263 264 265 266 267 268 269

	return size;
}
static DEVICE_ATTR_RW(fifofull_level);

static ssize_t addr_idx_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
270
	struct etm_config *config = &drvdata->config;
271

272
	val = config->addr_idx;
273 274 275 276 277 278 279 280 281 282
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t addr_idx_store(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
283
	struct etm_config *config = &drvdata->config;
284 285 286 287 288 289 290 291 292 293 294 295 296

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	if (val >= drvdata->nr_addr_cmp)
		return -EINVAL;

	/*
	 * Use spinlock to ensure index doesn't change while it gets
	 * dereferenced multiple times within a spinlock block elsewhere.
	 */
	spin_lock(&drvdata->spinlock);
297
	config->addr_idx = val;
298 299 300 301 302 303 304 305 306 307 308 309
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(addr_idx);

static ssize_t addr_single_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	u8 idx;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
310
	struct etm_config *config = &drvdata->config;
311 312

	spin_lock(&drvdata->spinlock);
313 314 315
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
316 317 318 319
		spin_unlock(&drvdata->spinlock);
		return -EINVAL;
	}

320
	val = config->addr_val[idx];
321 322 323 324 325 326 327 328 329 330 331 332 333
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t addr_single_store(struct device *dev,
				 struct device_attribute *attr,
				 const char *buf, size_t size)
{
	u8 idx;
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
334
	struct etm_config *config = &drvdata->config;
335 336 337 338 339 340

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
341 342 343
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
344 345 346 347
		spin_unlock(&drvdata->spinlock);
		return -EINVAL;
	}

348 349
	config->addr_val[idx] = val;
	config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
350 351 352 353 354 355 356 357 358 359 360 361
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(addr_single);

static ssize_t addr_range_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	u8 idx;
	unsigned long val1, val2;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
362
	struct etm_config *config = &drvdata->config;
363 364

	spin_lock(&drvdata->spinlock);
365
	idx = config->addr_idx;
366 367 368 369
	if (idx % 2 != 0) {
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}
370 371 372 373
	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
374 375 376 377
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

378 379
	val1 = config->addr_val[idx];
	val2 = config->addr_val[idx + 1];
380 381 382 383 384 385 386 387 388 389 390 391
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx %#lx\n", val1, val2);
}

static ssize_t addr_range_store(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t size)
{
	u8 idx;
	unsigned long val1, val2;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
392
	struct etm_config *config = &drvdata->config;
393 394 395 396 397 398 399 400

	if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
		return -EINVAL;
	/* Lower address comparator cannot have a higher address value */
	if (val1 > val2)
		return -EINVAL;

	spin_lock(&drvdata->spinlock);
401
	idx = config->addr_idx;
402 403 404 405
	if (idx % 2 != 0) {
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}
406 407 408 409
	if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
	      (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
	       config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
410 411 412 413
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

414 415 416 417 418
	config->addr_val[idx] = val1;
	config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
	config->addr_val[idx + 1] = val2;
	config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
	config->enable_ctrl1 |= (1 << (idx/2));
419 420 421 422 423 424 425 426 427 428 429 430
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(addr_range);

static ssize_t addr_start_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	u8 idx;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
431
	struct etm_config *config = &drvdata->config;
432 433

	spin_lock(&drvdata->spinlock);
434 435 436
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
437 438 439 440
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

441
	val = config->addr_val[idx];
442 443 444 445 446 447 448 449 450 451 452 453 454
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t addr_start_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t size)
{
	u8 idx;
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
455
	struct etm_config *config = &drvdata->config;
456 457 458 459 460 461

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
462 463 464
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
465 466 467 468
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

469 470 471 472
	config->addr_val[idx] = val;
	config->addr_type[idx] = ETM_ADDR_TYPE_START;
	config->startstop_ctrl |= (1 << idx);
	config->enable_ctrl1 |= BIT(25);
473 474 475 476 477 478 479 480 481 482 483 484
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(addr_start);

static ssize_t addr_stop_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	u8 idx;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
485
	struct etm_config *config = &drvdata->config;
486 487

	spin_lock(&drvdata->spinlock);
488 489 490
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
491 492 493 494
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

495
	val = config->addr_val[idx];
496 497 498 499 500 501 502 503 504 505 506 507 508
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t addr_stop_store(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t size)
{
	u8 idx;
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
509
	struct etm_config *config = &drvdata->config;
510 511 512 513 514 515

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
516 517 518
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
519 520 521 522
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

523 524 525 526
	config->addr_val[idx] = val;
	config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
	config->startstop_ctrl |= (1 << (idx + 16));
	config->enable_ctrl1 |= ETMTECR1_START_STOP;
527 528 529 530 531 532 533 534 535 536 537
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(addr_stop);

static ssize_t addr_acctype_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
538
	struct etm_config *config = &drvdata->config;
539 540

	spin_lock(&drvdata->spinlock);
541
	val = config->addr_acctype[config->addr_idx];
542 543 544 545 546 547 548 549 550 551 552 553
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t addr_acctype_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
554
	struct etm_config *config = &drvdata->config;
555 556 557 558 559 560

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
561
	config->addr_acctype[config->addr_idx] = val;
562 563 564 565 566 567 568 569 570 571 572
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(addr_acctype);

static ssize_t cntr_idx_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
573
	struct etm_config *config = &drvdata->config;
574

575
	val = config->cntr_idx;
576 577 578 579 580 581 582 583 584 585
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t cntr_idx_store(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
586
	struct etm_config *config = &drvdata->config;
587 588 589 590 591 592 593 594 595 596 597 598

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	if (val >= drvdata->nr_cntr)
		return -EINVAL;
	/*
	 * Use spinlock to ensure index doesn't change while it gets
	 * dereferenced multiple times within a spinlock block elsewhere.
	 */
	spin_lock(&drvdata->spinlock);
599
	config->cntr_idx = val;
600 601 602 603 604 605 606 607 608 609 610
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(cntr_idx);

static ssize_t cntr_rld_val_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
611
	struct etm_config *config = &drvdata->config;
612 613

	spin_lock(&drvdata->spinlock);
614
	val = config->cntr_rld_val[config->cntr_idx];
615 616 617 618 619 620 621 622 623 624 625 626
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t cntr_rld_val_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
627
	struct etm_config *config = &drvdata->config;
628 629 630 631 632 633

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
634
	config->cntr_rld_val[config->cntr_idx] = val;
635 636 637 638 639 640 641 642 643 644 645
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(cntr_rld_val);

static ssize_t cntr_event_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
646
	struct etm_config *config = &drvdata->config;
647 648

	spin_lock(&drvdata->spinlock);
649
	val = config->cntr_event[config->cntr_idx];
650 651 652 653 654 655 656 657 658 659 660 661
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t cntr_event_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
662
	struct etm_config *config = &drvdata->config;
663 664 665 666 667 668

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
669
	config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
670 671 672 673 674 675 676 677 678 679 680
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(cntr_event);

static ssize_t cntr_rld_event_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
681
	struct etm_config *config = &drvdata->config;
682 683

	spin_lock(&drvdata->spinlock);
684
	val = config->cntr_rld_event[config->cntr_idx];
685 686 687 688 689 690 691 692 693 694 695 696
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t cntr_rld_event_store(struct device *dev,
				    struct device_attribute *attr,
				    const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
697
	struct etm_config *config = &drvdata->config;
698 699 700 701 702 703

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
704
	config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
705 706 707 708 709 710 711 712 713 714 715 716
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(cntr_rld_event);

static ssize_t cntr_val_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	int i, ret = 0;
	u32 val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
717
	struct etm_config *config = &drvdata->config;
718 719 720 721 722

	if (!drvdata->enable) {
		spin_lock(&drvdata->spinlock);
		for (i = 0; i < drvdata->nr_cntr; i++)
			ret += sprintf(buf, "counter %d: %x\n",
723
				       i, config->cntr_val[i]);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		spin_unlock(&drvdata->spinlock);
		return ret;
	}

	for (i = 0; i < drvdata->nr_cntr; i++) {
		val = etm_readl(drvdata, ETMCNTVRn(i));
		ret += sprintf(buf, "counter %d: %x\n", i, val);
	}

	return ret;
}

static ssize_t cntr_val_store(struct device *dev,
			      struct device_attribute *attr,
			      const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
743
	struct etm_config *config = &drvdata->config;
744 745 746 747 748 749

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	spin_lock(&drvdata->spinlock);
750
	config->cntr_val[config->cntr_idx] = val;
751 752 753 754 755 756 757 758 759 760 761
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(cntr_val);

static ssize_t seq_12_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
762
	struct etm_config *config = &drvdata->config;
763

764
	val = config->seq_12_event;
765 766 767 768 769 770 771 772 773 774
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_12_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
775
	struct etm_config *config = &drvdata->config;
776 777 778 779 780

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

781
	config->seq_12_event = val & ETM_EVENT_MASK;
782 783 784 785 786 787 788 789 790
	return size;
}
static DEVICE_ATTR_RW(seq_12_event);

static ssize_t seq_21_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
791
	struct etm_config *config = &drvdata->config;
792

793
	val = config->seq_21_event;
794 795 796 797 798 799 800 801 802 803
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_21_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
804
	struct etm_config *config = &drvdata->config;
805 806 807 808 809

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

810
	config->seq_21_event = val & ETM_EVENT_MASK;
811 812 813 814 815 816 817 818 819
	return size;
}
static DEVICE_ATTR_RW(seq_21_event);

static ssize_t seq_23_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
820
	struct etm_config *config = &drvdata->config;
821

822
	val = config->seq_23_event;
823 824 825 826 827 828 829 830 831 832
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_23_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
833
	struct etm_config *config = &drvdata->config;
834 835 836 837 838

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

839
	config->seq_23_event = val & ETM_EVENT_MASK;
840 841 842 843 844 845 846 847 848
	return size;
}
static DEVICE_ATTR_RW(seq_23_event);

static ssize_t seq_31_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
849
	struct etm_config *config = &drvdata->config;
850

851
	val = config->seq_31_event;
852 853 854 855 856 857 858 859 860 861
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_31_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
862
	struct etm_config *config = &drvdata->config;
863 864 865 866 867

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

868
	config->seq_31_event = val & ETM_EVENT_MASK;
869 870 871 872 873 874 875 876 877
	return size;
}
static DEVICE_ATTR_RW(seq_31_event);

static ssize_t seq_32_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
878
	struct etm_config *config = &drvdata->config;
879

880
	val = config->seq_32_event;
881 882 883 884 885 886 887 888 889 890
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_32_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
891
	struct etm_config *config = &drvdata->config;
892 893 894 895 896

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

897
	config->seq_32_event = val & ETM_EVENT_MASK;
898 899 900 901 902 903 904 905 906
	return size;
}
static DEVICE_ATTR_RW(seq_32_event);

static ssize_t seq_13_event_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
907
	struct etm_config *config = &drvdata->config;
908

909
	val = config->seq_13_event;
910 911 912 913 914 915 916 917 918 919
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_13_event_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
920
	struct etm_config *config = &drvdata->config;
921 922 923 924 925

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

926
	config->seq_13_event = val & ETM_EVENT_MASK;
927 928 929 930 931 932 933 934 935
	return size;
}
static DEVICE_ATTR_RW(seq_13_event);

static ssize_t seq_curr_state_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	unsigned long val, flags;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
936
	struct etm_config *config = &drvdata->config;
937 938

	if (!drvdata->enable) {
939
		val = config->seq_curr_state;
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
		goto out;
	}

	pm_runtime_get_sync(drvdata->dev);
	spin_lock_irqsave(&drvdata->spinlock, flags);

	CS_UNLOCK(drvdata->base);
	val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
	CS_LOCK(drvdata->base);

	spin_unlock_irqrestore(&drvdata->spinlock, flags);
	pm_runtime_put(drvdata->dev);
out:
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t seq_curr_state_store(struct device *dev,
				    struct device_attribute *attr,
				    const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
963
	struct etm_config *config = &drvdata->config;
964 965 966 967 968 969 970 971

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	if (val > ETM_SEQ_STATE_MAX_VAL)
		return -EINVAL;

972
	config->seq_curr_state = val;
973 974 975 976 977 978 979 980 981 982

	return size;
}
static DEVICE_ATTR_RW(seq_curr_state);

static ssize_t ctxid_idx_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
983
	struct etm_config *config = &drvdata->config;
984

985
	val = config->ctxid_idx;
986 987 988 989 990 991 992 993 994 995
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t ctxid_idx_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
996
	struct etm_config *config = &drvdata->config;
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	if (val >= drvdata->nr_ctxid_cmp)
		return -EINVAL;

	/*
	 * Use spinlock to ensure index doesn't change while it gets
	 * dereferenced multiple times within a spinlock block elsewhere.
	 */
	spin_lock(&drvdata->spinlock);
1010
	config->ctxid_idx = val;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(ctxid_idx);

static ssize_t ctxid_pid_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1022
	struct etm_config *config = &drvdata->config;
1023 1024

	spin_lock(&drvdata->spinlock);
1025
	val = config->ctxid_vpid[config->ctxid_idx];
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	spin_unlock(&drvdata->spinlock);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t ctxid_pid_store(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t size)
{
	int ret;
	unsigned long vpid, pid;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1038
	struct etm_config *config = &drvdata->config;
1039 1040 1041 1042 1043 1044 1045 1046

	ret = kstrtoul(buf, 16, &vpid);
	if (ret)
		return ret;

	pid = coresight_vpid_to_pid(vpid);

	spin_lock(&drvdata->spinlock);
1047 1048
	config->ctxid_pid[config->ctxid_idx] = pid;
	config->ctxid_vpid[config->ctxid_idx] = vpid;
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	spin_unlock(&drvdata->spinlock);

	return size;
}
static DEVICE_ATTR_RW(ctxid_pid);

static ssize_t ctxid_mask_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1060
	struct etm_config *config = &drvdata->config;
1061

1062
	val = config->ctxid_mask;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t ctxid_mask_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1073
	struct etm_config *config = &drvdata->config;
1074 1075 1076 1077 1078

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

1079
	config->ctxid_mask = val;
1080 1081 1082 1083 1084 1085 1086 1087 1088
	return size;
}
static DEVICE_ATTR_RW(ctxid_mask);

static ssize_t sync_freq_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1089
	struct etm_config *config = &drvdata->config;
1090

1091
	val = config->sync_freq;
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t sync_freq_store(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1102
	struct etm_config *config = &drvdata->config;
1103 1104 1105 1106 1107

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

1108
	config->sync_freq = val & ETM_SYNC_MASK;
1109 1110 1111 1112 1113 1114 1115 1116 1117
	return size;
}
static DEVICE_ATTR_RW(sync_freq);

static ssize_t timestamp_event_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1118
	struct etm_config *config = &drvdata->config;
1119

1120
	val = config->timestamp_event;
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	return sprintf(buf, "%#lx\n", val);
}

static ssize_t timestamp_event_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1131
	struct etm_config *config = &drvdata->config;
1132 1133 1134 1135 1136

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

1137
	config->timestamp_event = val & ETM_EVENT_MASK;
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
	return size;
}
static DEVICE_ATTR_RW(timestamp_event);

static ssize_t cpu_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	int val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	val = drvdata->cpu;
	return scnprintf(buf, PAGE_SIZE, "%d\n", val);

}
static DEVICE_ATTR_RO(cpu);

static ssize_t traceid_show(struct device *dev,
			    struct device_attribute *attr, char *buf)
{
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	val = etm_get_trace_id(drvdata);

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t traceid_store(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	drvdata->traceid = val & ETM_TRACEID_MASK;
	return size;
}
static DEVICE_ATTR_RW(traceid);

static struct attribute *coresight_etm_attrs[] = {
	&dev_attr_nr_addr_cmp.attr,
	&dev_attr_nr_cntr.attr,
	&dev_attr_nr_ctxid_cmp.attr,
	&dev_attr_etmsr.attr,
	&dev_attr_reset.attr,
	&dev_attr_mode.attr,
	&dev_attr_trigger_event.attr,
	&dev_attr_enable_event.attr,
	&dev_attr_fifofull_level.attr,
	&dev_attr_addr_idx.attr,
	&dev_attr_addr_single.attr,
	&dev_attr_addr_range.attr,
	&dev_attr_addr_start.attr,
	&dev_attr_addr_stop.attr,
	&dev_attr_addr_acctype.attr,
	&dev_attr_cntr_idx.attr,
	&dev_attr_cntr_rld_val.attr,
	&dev_attr_cntr_event.attr,
	&dev_attr_cntr_rld_event.attr,
	&dev_attr_cntr_val.attr,
	&dev_attr_seq_12_event.attr,
	&dev_attr_seq_21_event.attr,
	&dev_attr_seq_23_event.attr,
	&dev_attr_seq_31_event.attr,
	&dev_attr_seq_32_event.attr,
	&dev_attr_seq_13_event.attr,
	&dev_attr_seq_curr_state.attr,
	&dev_attr_ctxid_idx.attr,
	&dev_attr_ctxid_pid.attr,
	&dev_attr_ctxid_mask.attr,
	&dev_attr_sync_freq.attr,
	&dev_attr_timestamp_event.attr,
	&dev_attr_traceid.attr,
	&dev_attr_cpu.attr,
	NULL,
};

#define coresight_simple_func(name, offset)                             \
static ssize_t name##_show(struct device *_dev,                         \
			   struct device_attribute *attr, char *buf)    \
{                                                                       \
	struct etm_drvdata *drvdata = dev_get_drvdata(_dev->parent);    \
	return scnprintf(buf, PAGE_SIZE, "0x%x\n",                      \
			 readl_relaxed(drvdata->base + offset));        \
}                                                                       \
DEVICE_ATTR_RO(name)

coresight_simple_func(etmccr, ETMCCR);
coresight_simple_func(etmccer, ETMCCER);
coresight_simple_func(etmscr, ETMSCR);
coresight_simple_func(etmidr, ETMIDR);
coresight_simple_func(etmcr, ETMCR);
coresight_simple_func(etmtraceidr, ETMTRACEIDR);
coresight_simple_func(etmteevr, ETMTEEVR);
coresight_simple_func(etmtssvr, ETMTSSCR);
coresight_simple_func(etmtecr1, ETMTECR1);
coresight_simple_func(etmtecr2, ETMTECR2);

static struct attribute *coresight_etm_mgmt_attrs[] = {
	&dev_attr_etmccr.attr,
	&dev_attr_etmccer.attr,
	&dev_attr_etmscr.attr,
	&dev_attr_etmidr.attr,
	&dev_attr_etmcr.attr,
	&dev_attr_etmtraceidr.attr,
	&dev_attr_etmteevr.attr,
	&dev_attr_etmtssvr.attr,
	&dev_attr_etmtecr1.attr,
	&dev_attr_etmtecr2.attr,
	NULL,
};

static const struct attribute_group coresight_etm_group = {
	.attrs = coresight_etm_attrs,
};

static const struct attribute_group coresight_etm_mgmt_group = {
	.attrs = coresight_etm_mgmt_attrs,
	.name = "mgmt",
};

const struct attribute_group *coresight_etm_groups[] = {
	&coresight_etm_group,
	&coresight_etm_mgmt_group,
	NULL,
};