coresight-etm3x-sysfs.c 31.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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"
21
#include "coresight-priv.h"
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 81

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);
82
	struct etm_config *config = &drvdata->config;
83 84 85 86 87 88 89

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

	if (val) {
		spin_lock(&drvdata->spinlock);
90 91 92
		memset(config, 0, sizeof(struct etm_config));
		config->mode = ETM_MODE_EXCLUDE;
		config->trigger_event = ETM_DEFAULT_EVENT_VAL;
93
		for (i = 0; i < drvdata->nr_addr_cmp; i++) {
94
			config->addr_type[i] = ETM_ADDR_TYPE_NONE;
95 96
		}

97
		etm_set_default(config);
98 99 100 101 102 103 104 105 106 107 108 109
		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);
110
	struct etm_config *config = &drvdata->config;
111

112
	val = config->mode;
113 114 115 116 117 118 119 120 121 122
	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);
123
	struct etm_config *config = &drvdata->config;
124 125 126 127 128 129

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

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

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

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

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

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

162 163
	if (config->mode & ETM_MODE_CTXID)
		config->ctrl |= ETMCR_CTXID_SIZE;
164
	else
165
		config->ctrl &= ~ETMCR_CTXID_SIZE;
166

167 168 169 170 171 172 173 174 175 176
	if (config->mode & ETM_MODE_BBROAD)
		config->ctrl |= ETMCR_BRANCH_BROADCAST;
	else
		config->ctrl &= ~ETMCR_BRANCH_BROADCAST;

	if (config->mode & ETM_MODE_RET_STACK)
		config->ctrl |= ETMCR_RETURN_STACK;
	else
		config->ctrl &= ~ETMCR_RETURN_STACK;

177 178 179
	if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
		etm_config_trace_mode(config);

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	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);
195
	struct etm_config *config = &drvdata->config;
196

197
	val = config->trigger_event;
198 199 200 201 202 203 204 205 206 207
	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);
208
	struct etm_config *config = &drvdata->config;
209 210 211 212 213

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

214
	config->trigger_event = val & ETM_EVENT_MASK;
215 216 217 218 219 220 221 222 223 224

	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);
225
	struct etm_config *config = &drvdata->config;
226

227
	val = config->enable_event;
228 229 230 231 232 233 234 235 236 237
	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);
238
	struct etm_config *config = &drvdata->config;
239 240 241 242 243

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

244
	config->enable_event = val & ETM_EVENT_MASK;
245 246 247 248 249 250 251 252 253 254

	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);
255
	struct etm_config *config = &drvdata->config;
256

257
	val = config->fifofull_level;
258 259 260 261 262 263 264 265 266 267
	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);
268
	struct etm_config *config = &drvdata->config;
269 270 271 272 273

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

274
	config->fifofull_level = val;
275 276 277 278 279 280 281 282 283 284

	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);
285
	struct etm_config *config = &drvdata->config;
286

287
	val = config->addr_idx;
288 289 290 291 292 293 294 295 296 297
	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);
298
	struct etm_config *config = &drvdata->config;
299 300 301 302 303 304 305 306 307 308 309 310 311

	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);
312
	config->addr_idx = val;
313 314 315 316 317 318 319 320 321 322 323 324
	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);
325
	struct etm_config *config = &drvdata->config;
326 327

	spin_lock(&drvdata->spinlock);
328 329 330
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
331 332 333 334
		spin_unlock(&drvdata->spinlock);
		return -EINVAL;
	}

335
	val = config->addr_val[idx];
336 337 338 339 340 341 342 343 344 345 346 347 348
	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);
349
	struct etm_config *config = &drvdata->config;
350 351 352 353 354 355

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

	spin_lock(&drvdata->spinlock);
356 357 358
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
359 360 361 362
		spin_unlock(&drvdata->spinlock);
		return -EINVAL;
	}

363 364
	config->addr_val[idx] = val;
	config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
365 366 367 368 369 370 371 372 373 374 375 376
	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);
377
	struct etm_config *config = &drvdata->config;
378 379

	spin_lock(&drvdata->spinlock);
380
	idx = config->addr_idx;
381 382 383 384
	if (idx % 2 != 0) {
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}
385 386 387 388
	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))) {
389 390 391 392
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

393 394
	val1 = config->addr_val[idx];
	val2 = config->addr_val[idx + 1];
395 396 397 398 399 400 401 402 403 404 405 406
	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);
407
	struct etm_config *config = &drvdata->config;
408 409 410 411 412 413 414 415

	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);
416
	idx = config->addr_idx;
417 418 419 420
	if (idx % 2 != 0) {
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}
421 422 423 424
	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))) {
425 426 427 428
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

429 430 431 432 433
	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));
434 435 436 437 438 439 440 441 442 443 444 445
	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);
446
	struct etm_config *config = &drvdata->config;
447 448

	spin_lock(&drvdata->spinlock);
449 450 451
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
452 453 454 455
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

456
	val = config->addr_val[idx];
457 458 459 460 461 462 463 464 465 466 467 468 469
	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);
470
	struct etm_config *config = &drvdata->config;
471 472 473 474 475 476

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

	spin_lock(&drvdata->spinlock);
477 478 479
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
480 481 482 483
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

484 485 486 487
	config->addr_val[idx] = val;
	config->addr_type[idx] = ETM_ADDR_TYPE_START;
	config->startstop_ctrl |= (1 << idx);
	config->enable_ctrl1 |= BIT(25);
488 489 490 491 492 493 494 495 496 497 498 499
	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);
500
	struct etm_config *config = &drvdata->config;
501 502

	spin_lock(&drvdata->spinlock);
503 504 505
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
506 507 508 509
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

510
	val = config->addr_val[idx];
511 512 513 514 515 516 517 518 519 520 521 522 523
	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);
524
	struct etm_config *config = &drvdata->config;
525 526 527 528 529 530

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

	spin_lock(&drvdata->spinlock);
531 532 533
	idx = config->addr_idx;
	if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
	      config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
534 535 536 537
		spin_unlock(&drvdata->spinlock);
		return -EPERM;
	}

538 539 540 541
	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;
542 543 544 545 546 547 548 549 550 551 552
	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);
553
	struct etm_config *config = &drvdata->config;
554 555

	spin_lock(&drvdata->spinlock);
556
	val = config->addr_acctype[config->addr_idx];
557 558 559 560 561 562 563 564 565 566 567 568
	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);
569
	struct etm_config *config = &drvdata->config;
570 571 572 573 574 575

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

	spin_lock(&drvdata->spinlock);
576
	config->addr_acctype[config->addr_idx] = val;
577 578 579 580 581 582 583 584 585 586 587
	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);
588
	struct etm_config *config = &drvdata->config;
589

590
	val = config->cntr_idx;
591 592 593 594 595 596 597 598 599 600
	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);
601
	struct etm_config *config = &drvdata->config;
602 603 604 605 606 607 608 609 610 611 612 613

	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);
614
	config->cntr_idx = val;
615 616 617 618 619 620 621 622 623 624 625
	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);
626
	struct etm_config *config = &drvdata->config;
627 628

	spin_lock(&drvdata->spinlock);
629
	val = config->cntr_rld_val[config->cntr_idx];
630 631 632 633 634 635 636 637 638 639 640 641
	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);
642
	struct etm_config *config = &drvdata->config;
643 644 645 646 647 648

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

	spin_lock(&drvdata->spinlock);
649
	config->cntr_rld_val[config->cntr_idx] = val;
650 651 652 653 654 655 656 657 658 659 660
	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);
661
	struct etm_config *config = &drvdata->config;
662 663

	spin_lock(&drvdata->spinlock);
664
	val = config->cntr_event[config->cntr_idx];
665 666 667 668 669 670 671 672 673 674 675 676
	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);
677
	struct etm_config *config = &drvdata->config;
678 679 680 681 682 683

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

	spin_lock(&drvdata->spinlock);
684
	config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
685 686 687 688 689 690 691 692 693 694 695
	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);
696
	struct etm_config *config = &drvdata->config;
697 698

	spin_lock(&drvdata->spinlock);
699
	val = config->cntr_rld_event[config->cntr_idx];
700 701 702 703 704 705 706 707 708 709 710 711
	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);
712
	struct etm_config *config = &drvdata->config;
713 714 715 716 717 718

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

	spin_lock(&drvdata->spinlock);
719
	config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
720 721 722 723 724 725 726 727 728 729 730 731
	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);
732
	struct etm_config *config = &drvdata->config;
733

734
	if (!local_read(&drvdata->mode)) {
735 736 737
		spin_lock(&drvdata->spinlock);
		for (i = 0; i < drvdata->nr_cntr; i++)
			ret += sprintf(buf, "counter %d: %x\n",
738
				       i, config->cntr_val[i]);
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
		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);
758
	struct etm_config *config = &drvdata->config;
759 760 761 762 763 764

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

	spin_lock(&drvdata->spinlock);
765
	config->cntr_val[config->cntr_idx] = val;
766 767 768 769 770 771 772 773 774 775 776
	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);
777
	struct etm_config *config = &drvdata->config;
778

779
	val = config->seq_12_event;
780 781 782 783 784 785 786 787 788 789
	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);
790
	struct etm_config *config = &drvdata->config;
791 792 793 794 795

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

796
	config->seq_12_event = val & ETM_EVENT_MASK;
797 798 799 800 801 802 803 804 805
	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);
806
	struct etm_config *config = &drvdata->config;
807

808
	val = config->seq_21_event;
809 810 811 812 813 814 815 816 817 818
	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);
819
	struct etm_config *config = &drvdata->config;
820 821 822 823 824

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

825
	config->seq_21_event = val & ETM_EVENT_MASK;
826 827 828 829 830 831 832 833 834
	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);
835
	struct etm_config *config = &drvdata->config;
836

837
	val = config->seq_23_event;
838 839 840 841 842 843 844 845 846 847
	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);
848
	struct etm_config *config = &drvdata->config;
849 850 851 852 853

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

854
	config->seq_23_event = val & ETM_EVENT_MASK;
855 856 857 858 859 860 861 862 863
	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);
864
	struct etm_config *config = &drvdata->config;
865

866
	val = config->seq_31_event;
867 868 869 870 871 872 873 874 875 876
	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);
877
	struct etm_config *config = &drvdata->config;
878 879 880 881 882

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

883
	config->seq_31_event = val & ETM_EVENT_MASK;
884 885 886 887 888 889 890 891 892
	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);
893
	struct etm_config *config = &drvdata->config;
894

895
	val = config->seq_32_event;
896 897 898 899 900 901 902 903 904 905
	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);
906
	struct etm_config *config = &drvdata->config;
907 908 909 910 911

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

912
	config->seq_32_event = val & ETM_EVENT_MASK;
913 914 915 916 917 918 919 920 921
	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);
922
	struct etm_config *config = &drvdata->config;
923

924
	val = config->seq_13_event;
925 926 927 928 929 930 931 932 933 934
	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);
935
	struct etm_config *config = &drvdata->config;
936 937 938 939 940

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

941
	config->seq_13_event = val & ETM_EVENT_MASK;
942 943 944 945 946 947 948 949 950
	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);
951
	struct etm_config *config = &drvdata->config;
952

953
	if (!local_read(&drvdata->mode)) {
954
		val = config->seq_curr_state;
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
		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);
978
	struct etm_config *config = &drvdata->config;
979 980 981 982 983 984 985 986

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

	if (val > ETM_SEQ_STATE_MAX_VAL)
		return -EINVAL;

987
	config->seq_curr_state = val;
988 989 990 991 992 993 994 995 996 997

	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);
998
	struct etm_config *config = &drvdata->config;
999

1000
	val = config->ctxid_idx;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	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);
1011
	struct etm_config *config = &drvdata->config;
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

	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);
1025
	config->ctxid_idx = val;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	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);
1037
	struct etm_config *config = &drvdata->config;
1038 1039

	spin_lock(&drvdata->spinlock);
1040
	val = config->ctxid_vpid[config->ctxid_idx];
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	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);
1053
	struct etm_config *config = &drvdata->config;
1054 1055 1056 1057 1058 1059 1060 1061

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

	pid = coresight_vpid_to_pid(vpid);

	spin_lock(&drvdata->spinlock);
1062 1063
	config->ctxid_pid[config->ctxid_idx] = pid;
	config->ctxid_vpid[config->ctxid_idx] = vpid;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	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);
1075
	struct etm_config *config = &drvdata->config;
1076

1077
	val = config->ctxid_mask;
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	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);
1088
	struct etm_config *config = &drvdata->config;
1089 1090 1091 1092 1093

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

1094
	config->ctxid_mask = val;
1095 1096 1097 1098 1099 1100 1101 1102 1103
	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);
1104
	struct etm_config *config = &drvdata->config;
1105

1106
	val = config->sync_freq;
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	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);
1117
	struct etm_config *config = &drvdata->config;
1118 1119 1120 1121 1122

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

1123
	config->sync_freq = val & ETM_SYNC_MASK;
1124 1125 1126 1127 1128 1129 1130 1131 1132
	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);
1133
	struct etm_config *config = &drvdata->config;
1134

1135
	val = config->timestamp_event;
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	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);
1146
	struct etm_config *config = &drvdata->config;
1147 1148 1149 1150 1151

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

1152
	config->timestamp_event = val & ETM_EVENT_MASK;
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
	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,
};

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
#define coresight_etm3x_reg(name, offset)			\
	coresight_simple_reg32(struct etm_drvdata, name, offset)

coresight_etm3x_reg(etmccr, ETMCCR);
coresight_etm3x_reg(etmccer, ETMCCER);
coresight_etm3x_reg(etmscr, ETMSCR);
coresight_etm3x_reg(etmidr, ETMIDR);
coresight_etm3x_reg(etmcr, ETMCR);
coresight_etm3x_reg(etmtraceidr, ETMTRACEIDR);
coresight_etm3x_reg(etmteevr, ETMTEEVR);
coresight_etm3x_reg(etmtssvr, ETMTSSCR);
coresight_etm3x_reg(etmtecr1, ETMTECR1);
coresight_etm3x_reg(etmtecr2, ETMTECR2);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

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,
};