Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight-etm3x-sysfs.c
blobe8c7649f123e735dcd17219535a148c52922bc60
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 */
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"
13 static ssize_t nr_addr_cmp_show(struct device *dev,
14 struct device_attribute *attr, char *buf)
16 unsigned long val;
17 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
19 val = drvdata->nr_addr_cmp;
20 return sprintf(buf, "%#lx\n", val);
22 static DEVICE_ATTR_RO(nr_addr_cmp);
24 static 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);
29 val = drvdata->nr_cntr;
30 return sprintf(buf, "%#lx\n", val);
32 static DEVICE_ATTR_RO(nr_cntr);
34 static ssize_t nr_ctxid_cmp_show(struct device *dev,
35 struct device_attribute *attr, char *buf)
37 unsigned long val;
38 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
40 val = drvdata->nr_ctxid_cmp;
41 return sprintf(buf, "%#lx\n", val);
43 static DEVICE_ATTR_RO(nr_ctxid_cmp);
45 static ssize_t etmsr_show(struct device *dev,
46 struct device_attribute *attr, char *buf)
48 unsigned long flags, val;
49 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
51 pm_runtime_get_sync(dev->parent);
52 spin_lock_irqsave(&drvdata->spinlock, flags);
53 CS_UNLOCK(drvdata->base);
55 val = etm_readl(drvdata, ETMSR);
57 CS_LOCK(drvdata->base);
58 spin_unlock_irqrestore(&drvdata->spinlock, flags);
59 pm_runtime_put(dev->parent);
61 return sprintf(buf, "%#lx\n", val);
63 static DEVICE_ATTR_RO(etmsr);
65 static ssize_t reset_store(struct device *dev,
66 struct device_attribute *attr,
67 const char *buf, size_t size)
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;
74 ret = kstrtoul(buf, 16, &val);
75 if (ret)
76 return ret;
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;
87 etm_set_default(config);
88 spin_unlock(&drvdata->spinlock);
91 return size;
93 static DEVICE_ATTR_WO(reset);
95 static ssize_t mode_show(struct device *dev,
96 struct device_attribute *attr, char *buf)
98 unsigned long val;
99 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
100 struct etm_config *config = &drvdata->config;
102 val = config->mode;
103 return sprintf(buf, "%#lx\n", val);
106 static ssize_t mode_store(struct device *dev,
107 struct device_attribute *attr,
108 const char *buf, size_t size)
110 int ret;
111 unsigned long val;
112 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
113 struct etm_config *config = &drvdata->config;
115 ret = kstrtoul(buf, 16, &val);
116 if (ret)
117 return ret;
119 spin_lock(&drvdata->spinlock);
120 config->mode = val & ETM_MODE_ALL;
122 if (config->mode & ETM_MODE_EXCLUDE)
123 config->enable_ctrl1 |= ETMTECR1_INC_EXC;
124 else
125 config->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
127 if (config->mode & ETM_MODE_CYCACC)
128 config->ctrl |= ETMCR_CYC_ACC;
129 else
130 config->ctrl &= ~ETMCR_CYC_ACC;
132 if (config->mode & ETM_MODE_STALL) {
133 if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
134 dev_warn(dev, "stall mode not supported\n");
135 ret = -EINVAL;
136 goto err_unlock;
138 config->ctrl |= ETMCR_STALL_MODE;
139 } else
140 config->ctrl &= ~ETMCR_STALL_MODE;
142 if (config->mode & ETM_MODE_TIMESTAMP) {
143 if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
144 dev_warn(dev, "timestamp not supported\n");
145 ret = -EINVAL;
146 goto err_unlock;
148 config->ctrl |= ETMCR_TIMESTAMP_EN;
149 } else
150 config->ctrl &= ~ETMCR_TIMESTAMP_EN;
152 if (config->mode & ETM_MODE_CTXID)
153 config->ctrl |= ETMCR_CTXID_SIZE;
154 else
155 config->ctrl &= ~ETMCR_CTXID_SIZE;
157 if (config->mode & ETM_MODE_BBROAD)
158 config->ctrl |= ETMCR_BRANCH_BROADCAST;
159 else
160 config->ctrl &= ~ETMCR_BRANCH_BROADCAST;
162 if (config->mode & ETM_MODE_RET_STACK)
163 config->ctrl |= ETMCR_RETURN_STACK;
164 else
165 config->ctrl &= ~ETMCR_RETURN_STACK;
167 if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
168 etm_config_trace_mode(config);
170 spin_unlock(&drvdata->spinlock);
172 return size;
174 err_unlock:
175 spin_unlock(&drvdata->spinlock);
176 return ret;
178 static DEVICE_ATTR_RW(mode);
180 static ssize_t trigger_event_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
183 unsigned long val;
184 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
185 struct etm_config *config = &drvdata->config;
187 val = config->trigger_event;
188 return sprintf(buf, "%#lx\n", val);
191 static ssize_t trigger_event_store(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf, size_t size)
195 int ret;
196 unsigned long val;
197 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
198 struct etm_config *config = &drvdata->config;
200 ret = kstrtoul(buf, 16, &val);
201 if (ret)
202 return ret;
204 config->trigger_event = val & ETM_EVENT_MASK;
206 return size;
208 static DEVICE_ATTR_RW(trigger_event);
210 static ssize_t enable_event_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
213 unsigned long val;
214 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
215 struct etm_config *config = &drvdata->config;
217 val = config->enable_event;
218 return sprintf(buf, "%#lx\n", val);
221 static ssize_t enable_event_store(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf, size_t size)
225 int ret;
226 unsigned long val;
227 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
228 struct etm_config *config = &drvdata->config;
230 ret = kstrtoul(buf, 16, &val);
231 if (ret)
232 return ret;
234 config->enable_event = val & ETM_EVENT_MASK;
236 return size;
238 static DEVICE_ATTR_RW(enable_event);
240 static ssize_t fifofull_level_show(struct device *dev,
241 struct device_attribute *attr, char *buf)
243 unsigned long val;
244 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
245 struct etm_config *config = &drvdata->config;
247 val = config->fifofull_level;
248 return sprintf(buf, "%#lx\n", val);
251 static ssize_t fifofull_level_store(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t size)
255 int ret;
256 unsigned long val;
257 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
258 struct etm_config *config = &drvdata->config;
260 ret = kstrtoul(buf, 16, &val);
261 if (ret)
262 return ret;
264 config->fifofull_level = val;
266 return size;
268 static DEVICE_ATTR_RW(fifofull_level);
270 static ssize_t addr_idx_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 unsigned long val;
274 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
275 struct etm_config *config = &drvdata->config;
277 val = config->addr_idx;
278 return sprintf(buf, "%#lx\n", val);
281 static ssize_t addr_idx_store(struct device *dev,
282 struct device_attribute *attr,
283 const char *buf, size_t size)
285 int ret;
286 unsigned long val;
287 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
288 struct etm_config *config = &drvdata->config;
290 ret = kstrtoul(buf, 16, &val);
291 if (ret)
292 return ret;
294 if (val >= drvdata->nr_addr_cmp)
295 return -EINVAL;
298 * Use spinlock to ensure index doesn't change while it gets
299 * dereferenced multiple times within a spinlock block elsewhere.
301 spin_lock(&drvdata->spinlock);
302 config->addr_idx = val;
303 spin_unlock(&drvdata->spinlock);
305 return size;
307 static DEVICE_ATTR_RW(addr_idx);
309 static ssize_t addr_single_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 u8 idx;
313 unsigned long val;
314 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
315 struct etm_config *config = &drvdata->config;
317 spin_lock(&drvdata->spinlock);
318 idx = config->addr_idx;
319 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
320 config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
321 spin_unlock(&drvdata->spinlock);
322 return -EINVAL;
325 val = config->addr_val[idx];
326 spin_unlock(&drvdata->spinlock);
328 return sprintf(buf, "%#lx\n", val);
331 static ssize_t addr_single_store(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf, size_t size)
335 u8 idx;
336 int ret;
337 unsigned long val;
338 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
339 struct etm_config *config = &drvdata->config;
341 ret = kstrtoul(buf, 16, &val);
342 if (ret)
343 return ret;
345 spin_lock(&drvdata->spinlock);
346 idx = config->addr_idx;
347 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
348 config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
349 spin_unlock(&drvdata->spinlock);
350 return -EINVAL;
353 config->addr_val[idx] = val;
354 config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
355 spin_unlock(&drvdata->spinlock);
357 return size;
359 static DEVICE_ATTR_RW(addr_single);
361 static ssize_t addr_range_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
364 u8 idx;
365 unsigned long val1, val2;
366 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
367 struct etm_config *config = &drvdata->config;
369 spin_lock(&drvdata->spinlock);
370 idx = config->addr_idx;
371 if (idx % 2 != 0) {
372 spin_unlock(&drvdata->spinlock);
373 return -EPERM;
375 if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
376 config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
377 (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
378 config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
379 spin_unlock(&drvdata->spinlock);
380 return -EPERM;
383 val1 = config->addr_val[idx];
384 val2 = config->addr_val[idx + 1];
385 spin_unlock(&drvdata->spinlock);
387 return sprintf(buf, "%#lx %#lx\n", val1, val2);
390 static ssize_t addr_range_store(struct device *dev,
391 struct device_attribute *attr,
392 const char *buf, size_t size)
394 u8 idx;
395 unsigned long val1, val2;
396 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
397 struct etm_config *config = &drvdata->config;
399 if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
400 return -EINVAL;
401 /* Lower address comparator cannot have a higher address value */
402 if (val1 > val2)
403 return -EINVAL;
405 spin_lock(&drvdata->spinlock);
406 idx = config->addr_idx;
407 if (idx % 2 != 0) {
408 spin_unlock(&drvdata->spinlock);
409 return -EPERM;
411 if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
412 config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
413 (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
414 config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
415 spin_unlock(&drvdata->spinlock);
416 return -EPERM;
419 config->addr_val[idx] = val1;
420 config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
421 config->addr_val[idx + 1] = val2;
422 config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
423 config->enable_ctrl1 |= (1 << (idx/2));
424 spin_unlock(&drvdata->spinlock);
426 return size;
428 static DEVICE_ATTR_RW(addr_range);
430 static ssize_t addr_start_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
433 u8 idx;
434 unsigned long val;
435 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
436 struct etm_config *config = &drvdata->config;
438 spin_lock(&drvdata->spinlock);
439 idx = config->addr_idx;
440 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
441 config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
442 spin_unlock(&drvdata->spinlock);
443 return -EPERM;
446 val = config->addr_val[idx];
447 spin_unlock(&drvdata->spinlock);
449 return sprintf(buf, "%#lx\n", val);
452 static ssize_t addr_start_store(struct device *dev,
453 struct device_attribute *attr,
454 const char *buf, size_t size)
456 u8 idx;
457 int ret;
458 unsigned long val;
459 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
460 struct etm_config *config = &drvdata->config;
462 ret = kstrtoul(buf, 16, &val);
463 if (ret)
464 return ret;
466 spin_lock(&drvdata->spinlock);
467 idx = config->addr_idx;
468 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
469 config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
470 spin_unlock(&drvdata->spinlock);
471 return -EPERM;
474 config->addr_val[idx] = val;
475 config->addr_type[idx] = ETM_ADDR_TYPE_START;
476 config->startstop_ctrl |= (1 << idx);
477 config->enable_ctrl1 |= BIT(25);
478 spin_unlock(&drvdata->spinlock);
480 return size;
482 static DEVICE_ATTR_RW(addr_start);
484 static ssize_t addr_stop_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
487 u8 idx;
488 unsigned long val;
489 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
490 struct etm_config *config = &drvdata->config;
492 spin_lock(&drvdata->spinlock);
493 idx = config->addr_idx;
494 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
495 config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
496 spin_unlock(&drvdata->spinlock);
497 return -EPERM;
500 val = config->addr_val[idx];
501 spin_unlock(&drvdata->spinlock);
503 return sprintf(buf, "%#lx\n", val);
506 static ssize_t addr_stop_store(struct device *dev,
507 struct device_attribute *attr,
508 const char *buf, size_t size)
510 u8 idx;
511 int ret;
512 unsigned long val;
513 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
514 struct etm_config *config = &drvdata->config;
516 ret = kstrtoul(buf, 16, &val);
517 if (ret)
518 return ret;
520 spin_lock(&drvdata->spinlock);
521 idx = config->addr_idx;
522 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
523 config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
524 spin_unlock(&drvdata->spinlock);
525 return -EPERM;
528 config->addr_val[idx] = val;
529 config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
530 config->startstop_ctrl |= (1 << (idx + 16));
531 config->enable_ctrl1 |= ETMTECR1_START_STOP;
532 spin_unlock(&drvdata->spinlock);
534 return size;
536 static DEVICE_ATTR_RW(addr_stop);
538 static ssize_t addr_acctype_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
541 unsigned long val;
542 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
543 struct etm_config *config = &drvdata->config;
545 spin_lock(&drvdata->spinlock);
546 val = config->addr_acctype[config->addr_idx];
547 spin_unlock(&drvdata->spinlock);
549 return sprintf(buf, "%#lx\n", val);
552 static ssize_t addr_acctype_store(struct device *dev,
553 struct device_attribute *attr,
554 const char *buf, size_t size)
556 int ret;
557 unsigned long val;
558 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
559 struct etm_config *config = &drvdata->config;
561 ret = kstrtoul(buf, 16, &val);
562 if (ret)
563 return ret;
565 spin_lock(&drvdata->spinlock);
566 config->addr_acctype[config->addr_idx] = val;
567 spin_unlock(&drvdata->spinlock);
569 return size;
571 static DEVICE_ATTR_RW(addr_acctype);
573 static ssize_t cntr_idx_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
576 unsigned long val;
577 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
578 struct etm_config *config = &drvdata->config;
580 val = config->cntr_idx;
581 return sprintf(buf, "%#lx\n", val);
584 static ssize_t cntr_idx_store(struct device *dev,
585 struct device_attribute *attr,
586 const char *buf, size_t size)
588 int ret;
589 unsigned long val;
590 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
591 struct etm_config *config = &drvdata->config;
593 ret = kstrtoul(buf, 16, &val);
594 if (ret)
595 return ret;
597 if (val >= drvdata->nr_cntr)
598 return -EINVAL;
600 * Use spinlock to ensure index doesn't change while it gets
601 * dereferenced multiple times within a spinlock block elsewhere.
603 spin_lock(&drvdata->spinlock);
604 config->cntr_idx = val;
605 spin_unlock(&drvdata->spinlock);
607 return size;
609 static DEVICE_ATTR_RW(cntr_idx);
611 static ssize_t cntr_rld_val_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
614 unsigned long val;
615 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
616 struct etm_config *config = &drvdata->config;
618 spin_lock(&drvdata->spinlock);
619 val = config->cntr_rld_val[config->cntr_idx];
620 spin_unlock(&drvdata->spinlock);
622 return sprintf(buf, "%#lx\n", val);
625 static ssize_t cntr_rld_val_store(struct device *dev,
626 struct device_attribute *attr,
627 const char *buf, size_t size)
629 int ret;
630 unsigned long val;
631 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
632 struct etm_config *config = &drvdata->config;
634 ret = kstrtoul(buf, 16, &val);
635 if (ret)
636 return ret;
638 spin_lock(&drvdata->spinlock);
639 config->cntr_rld_val[config->cntr_idx] = val;
640 spin_unlock(&drvdata->spinlock);
642 return size;
644 static DEVICE_ATTR_RW(cntr_rld_val);
646 static ssize_t cntr_event_show(struct device *dev,
647 struct device_attribute *attr, char *buf)
649 unsigned long val;
650 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
651 struct etm_config *config = &drvdata->config;
653 spin_lock(&drvdata->spinlock);
654 val = config->cntr_event[config->cntr_idx];
655 spin_unlock(&drvdata->spinlock);
657 return sprintf(buf, "%#lx\n", val);
660 static ssize_t cntr_event_store(struct device *dev,
661 struct device_attribute *attr,
662 const char *buf, size_t size)
664 int ret;
665 unsigned long val;
666 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
667 struct etm_config *config = &drvdata->config;
669 ret = kstrtoul(buf, 16, &val);
670 if (ret)
671 return ret;
673 spin_lock(&drvdata->spinlock);
674 config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
675 spin_unlock(&drvdata->spinlock);
677 return size;
679 static DEVICE_ATTR_RW(cntr_event);
681 static ssize_t cntr_rld_event_show(struct device *dev,
682 struct device_attribute *attr, char *buf)
684 unsigned long val;
685 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
686 struct etm_config *config = &drvdata->config;
688 spin_lock(&drvdata->spinlock);
689 val = config->cntr_rld_event[config->cntr_idx];
690 spin_unlock(&drvdata->spinlock);
692 return sprintf(buf, "%#lx\n", val);
695 static ssize_t cntr_rld_event_store(struct device *dev,
696 struct device_attribute *attr,
697 const char *buf, size_t size)
699 int ret;
700 unsigned long val;
701 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
702 struct etm_config *config = &drvdata->config;
704 ret = kstrtoul(buf, 16, &val);
705 if (ret)
706 return ret;
708 spin_lock(&drvdata->spinlock);
709 config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
710 spin_unlock(&drvdata->spinlock);
712 return size;
714 static DEVICE_ATTR_RW(cntr_rld_event);
716 static ssize_t cntr_val_show(struct device *dev,
717 struct device_attribute *attr, char *buf)
719 int i, ret = 0;
720 u32 val;
721 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
722 struct etm_config *config = &drvdata->config;
724 if (!local_read(&drvdata->mode)) {
725 spin_lock(&drvdata->spinlock);
726 for (i = 0; i < drvdata->nr_cntr; i++)
727 ret += sprintf(buf, "counter %d: %x\n",
728 i, config->cntr_val[i]);
729 spin_unlock(&drvdata->spinlock);
730 return ret;
733 for (i = 0; i < drvdata->nr_cntr; i++) {
734 val = etm_readl(drvdata, ETMCNTVRn(i));
735 ret += sprintf(buf, "counter %d: %x\n", i, val);
738 return ret;
741 static ssize_t cntr_val_store(struct device *dev,
742 struct device_attribute *attr,
743 const char *buf, size_t size)
745 int ret;
746 unsigned long val;
747 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
748 struct etm_config *config = &drvdata->config;
750 ret = kstrtoul(buf, 16, &val);
751 if (ret)
752 return ret;
754 spin_lock(&drvdata->spinlock);
755 config->cntr_val[config->cntr_idx] = val;
756 spin_unlock(&drvdata->spinlock);
758 return size;
760 static DEVICE_ATTR_RW(cntr_val);
762 static ssize_t seq_12_event_show(struct device *dev,
763 struct device_attribute *attr, char *buf)
765 unsigned long val;
766 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
767 struct etm_config *config = &drvdata->config;
769 val = config->seq_12_event;
770 return sprintf(buf, "%#lx\n", val);
773 static ssize_t seq_12_event_store(struct device *dev,
774 struct device_attribute *attr,
775 const char *buf, size_t size)
777 int ret;
778 unsigned long val;
779 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
780 struct etm_config *config = &drvdata->config;
782 ret = kstrtoul(buf, 16, &val);
783 if (ret)
784 return ret;
786 config->seq_12_event = val & ETM_EVENT_MASK;
787 return size;
789 static DEVICE_ATTR_RW(seq_12_event);
791 static ssize_t seq_21_event_show(struct device *dev,
792 struct device_attribute *attr, char *buf)
794 unsigned long val;
795 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
796 struct etm_config *config = &drvdata->config;
798 val = config->seq_21_event;
799 return sprintf(buf, "%#lx\n", val);
802 static ssize_t seq_21_event_store(struct device *dev,
803 struct device_attribute *attr,
804 const char *buf, size_t size)
806 int ret;
807 unsigned long val;
808 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
809 struct etm_config *config = &drvdata->config;
811 ret = kstrtoul(buf, 16, &val);
812 if (ret)
813 return ret;
815 config->seq_21_event = val & ETM_EVENT_MASK;
816 return size;
818 static DEVICE_ATTR_RW(seq_21_event);
820 static ssize_t seq_23_event_show(struct device *dev,
821 struct device_attribute *attr, char *buf)
823 unsigned long val;
824 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
825 struct etm_config *config = &drvdata->config;
827 val = config->seq_23_event;
828 return sprintf(buf, "%#lx\n", val);
831 static ssize_t seq_23_event_store(struct device *dev,
832 struct device_attribute *attr,
833 const char *buf, size_t size)
835 int ret;
836 unsigned long val;
837 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
838 struct etm_config *config = &drvdata->config;
840 ret = kstrtoul(buf, 16, &val);
841 if (ret)
842 return ret;
844 config->seq_23_event = val & ETM_EVENT_MASK;
845 return size;
847 static DEVICE_ATTR_RW(seq_23_event);
849 static ssize_t seq_31_event_show(struct device *dev,
850 struct device_attribute *attr, char *buf)
852 unsigned long val;
853 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
854 struct etm_config *config = &drvdata->config;
856 val = config->seq_31_event;
857 return sprintf(buf, "%#lx\n", val);
860 static ssize_t seq_31_event_store(struct device *dev,
861 struct device_attribute *attr,
862 const char *buf, size_t size)
864 int ret;
865 unsigned long val;
866 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
867 struct etm_config *config = &drvdata->config;
869 ret = kstrtoul(buf, 16, &val);
870 if (ret)
871 return ret;
873 config->seq_31_event = val & ETM_EVENT_MASK;
874 return size;
876 static DEVICE_ATTR_RW(seq_31_event);
878 static ssize_t seq_32_event_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
881 unsigned long val;
882 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
883 struct etm_config *config = &drvdata->config;
885 val = config->seq_32_event;
886 return sprintf(buf, "%#lx\n", val);
889 static ssize_t seq_32_event_store(struct device *dev,
890 struct device_attribute *attr,
891 const char *buf, size_t size)
893 int ret;
894 unsigned long val;
895 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
896 struct etm_config *config = &drvdata->config;
898 ret = kstrtoul(buf, 16, &val);
899 if (ret)
900 return ret;
902 config->seq_32_event = val & ETM_EVENT_MASK;
903 return size;
905 static DEVICE_ATTR_RW(seq_32_event);
907 static ssize_t seq_13_event_show(struct device *dev,
908 struct device_attribute *attr, char *buf)
910 unsigned long val;
911 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
912 struct etm_config *config = &drvdata->config;
914 val = config->seq_13_event;
915 return sprintf(buf, "%#lx\n", val);
918 static ssize_t seq_13_event_store(struct device *dev,
919 struct device_attribute *attr,
920 const char *buf, size_t size)
922 int ret;
923 unsigned long val;
924 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
925 struct etm_config *config = &drvdata->config;
927 ret = kstrtoul(buf, 16, &val);
928 if (ret)
929 return ret;
931 config->seq_13_event = val & ETM_EVENT_MASK;
932 return size;
934 static DEVICE_ATTR_RW(seq_13_event);
936 static ssize_t seq_curr_state_show(struct device *dev,
937 struct device_attribute *attr, char *buf)
939 unsigned long val, flags;
940 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
941 struct etm_config *config = &drvdata->config;
943 if (!local_read(&drvdata->mode)) {
944 val = config->seq_curr_state;
945 goto out;
948 pm_runtime_get_sync(dev->parent);
949 spin_lock_irqsave(&drvdata->spinlock, flags);
951 CS_UNLOCK(drvdata->base);
952 val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
953 CS_LOCK(drvdata->base);
955 spin_unlock_irqrestore(&drvdata->spinlock, flags);
956 pm_runtime_put(dev->parent);
957 out:
958 return sprintf(buf, "%#lx\n", val);
961 static ssize_t seq_curr_state_store(struct device *dev,
962 struct device_attribute *attr,
963 const char *buf, size_t size)
965 int ret;
966 unsigned long val;
967 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
968 struct etm_config *config = &drvdata->config;
970 ret = kstrtoul(buf, 16, &val);
971 if (ret)
972 return ret;
974 if (val > ETM_SEQ_STATE_MAX_VAL)
975 return -EINVAL;
977 config->seq_curr_state = val;
979 return size;
981 static DEVICE_ATTR_RW(seq_curr_state);
983 static ssize_t ctxid_idx_show(struct device *dev,
984 struct device_attribute *attr, char *buf)
986 unsigned long val;
987 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
988 struct etm_config *config = &drvdata->config;
990 val = config->ctxid_idx;
991 return sprintf(buf, "%#lx\n", val);
994 static ssize_t ctxid_idx_store(struct device *dev,
995 struct device_attribute *attr,
996 const char *buf, size_t size)
998 int ret;
999 unsigned long val;
1000 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1001 struct etm_config *config = &drvdata->config;
1003 ret = kstrtoul(buf, 16, &val);
1004 if (ret)
1005 return ret;
1007 if (val >= drvdata->nr_ctxid_cmp)
1008 return -EINVAL;
1011 * Use spinlock to ensure index doesn't change while it gets
1012 * dereferenced multiple times within a spinlock block elsewhere.
1014 spin_lock(&drvdata->spinlock);
1015 config->ctxid_idx = val;
1016 spin_unlock(&drvdata->spinlock);
1018 return size;
1020 static DEVICE_ATTR_RW(ctxid_idx);
1022 static ssize_t ctxid_pid_show(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1025 unsigned long val;
1026 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1027 struct etm_config *config = &drvdata->config;
1030 * Don't use contextID tracing if coming from a PID namespace. See
1031 * comment in ctxid_pid_store().
1033 if (task_active_pid_ns(current) != &init_pid_ns)
1034 return -EINVAL;
1036 spin_lock(&drvdata->spinlock);
1037 val = config->ctxid_pid[config->ctxid_idx];
1038 spin_unlock(&drvdata->spinlock);
1040 return sprintf(buf, "%#lx\n", val);
1043 static ssize_t ctxid_pid_store(struct device *dev,
1044 struct device_attribute *attr,
1045 const char *buf, size_t size)
1047 int ret;
1048 unsigned long pid;
1049 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1050 struct etm_config *config = &drvdata->config;
1053 * When contextID tracing is enabled the tracers will insert the
1054 * value found in the contextID register in the trace stream. But if
1055 * a process is in a namespace the PID of that process as seen from the
1056 * namespace won't be what the kernel sees, something that makes the
1057 * feature confusing and can potentially leak kernel only information.
1058 * As such refuse to use the feature if @current is not in the initial
1059 * PID namespace.
1061 if (task_active_pid_ns(current) != &init_pid_ns)
1062 return -EINVAL;
1064 ret = kstrtoul(buf, 16, &pid);
1065 if (ret)
1066 return ret;
1068 spin_lock(&drvdata->spinlock);
1069 config->ctxid_pid[config->ctxid_idx] = pid;
1070 spin_unlock(&drvdata->spinlock);
1072 return size;
1074 static DEVICE_ATTR_RW(ctxid_pid);
1076 static ssize_t ctxid_mask_show(struct device *dev,
1077 struct device_attribute *attr, char *buf)
1079 unsigned long val;
1080 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1081 struct etm_config *config = &drvdata->config;
1084 * Don't use contextID tracing if coming from a PID namespace. See
1085 * comment in ctxid_pid_store().
1087 if (task_active_pid_ns(current) != &init_pid_ns)
1088 return -EINVAL;
1090 val = config->ctxid_mask;
1091 return sprintf(buf, "%#lx\n", val);
1094 static ssize_t ctxid_mask_store(struct device *dev,
1095 struct device_attribute *attr,
1096 const char *buf, size_t size)
1098 int ret;
1099 unsigned long val;
1100 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1101 struct etm_config *config = &drvdata->config;
1104 * Don't use contextID tracing if coming from a PID namespace. See
1105 * comment in ctxid_pid_store().
1107 if (task_active_pid_ns(current) != &init_pid_ns)
1108 return -EINVAL;
1110 ret = kstrtoul(buf, 16, &val);
1111 if (ret)
1112 return ret;
1114 config->ctxid_mask = val;
1115 return size;
1117 static DEVICE_ATTR_RW(ctxid_mask);
1119 static ssize_t sync_freq_show(struct device *dev,
1120 struct device_attribute *attr, char *buf)
1122 unsigned long val;
1123 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1124 struct etm_config *config = &drvdata->config;
1126 val = config->sync_freq;
1127 return sprintf(buf, "%#lx\n", val);
1130 static ssize_t sync_freq_store(struct device *dev,
1131 struct device_attribute *attr,
1132 const char *buf, size_t size)
1134 int ret;
1135 unsigned long val;
1136 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1137 struct etm_config *config = &drvdata->config;
1139 ret = kstrtoul(buf, 16, &val);
1140 if (ret)
1141 return ret;
1143 config->sync_freq = val & ETM_SYNC_MASK;
1144 return size;
1146 static DEVICE_ATTR_RW(sync_freq);
1148 static ssize_t timestamp_event_show(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1151 unsigned long val;
1152 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1153 struct etm_config *config = &drvdata->config;
1155 val = config->timestamp_event;
1156 return sprintf(buf, "%#lx\n", val);
1159 static ssize_t timestamp_event_store(struct device *dev,
1160 struct device_attribute *attr,
1161 const char *buf, size_t size)
1163 int ret;
1164 unsigned long val;
1165 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1166 struct etm_config *config = &drvdata->config;
1168 ret = kstrtoul(buf, 16, &val);
1169 if (ret)
1170 return ret;
1172 config->timestamp_event = val & ETM_EVENT_MASK;
1173 return size;
1175 static DEVICE_ATTR_RW(timestamp_event);
1177 static ssize_t cpu_show(struct device *dev,
1178 struct device_attribute *attr, char *buf)
1180 int val;
1181 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1183 val = drvdata->cpu;
1184 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1187 static DEVICE_ATTR_RO(cpu);
1189 static ssize_t traceid_show(struct device *dev,
1190 struct device_attribute *attr, char *buf)
1192 unsigned long val;
1193 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1195 val = etm_get_trace_id(drvdata);
1197 return sprintf(buf, "%#lx\n", val);
1200 static ssize_t traceid_store(struct device *dev,
1201 struct device_attribute *attr,
1202 const char *buf, size_t size)
1204 int ret;
1205 unsigned long val;
1206 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1208 ret = kstrtoul(buf, 16, &val);
1209 if (ret)
1210 return ret;
1212 drvdata->traceid = val & ETM_TRACEID_MASK;
1213 return size;
1215 static DEVICE_ATTR_RW(traceid);
1217 static struct attribute *coresight_etm_attrs[] = {
1218 &dev_attr_nr_addr_cmp.attr,
1219 &dev_attr_nr_cntr.attr,
1220 &dev_attr_nr_ctxid_cmp.attr,
1221 &dev_attr_etmsr.attr,
1222 &dev_attr_reset.attr,
1223 &dev_attr_mode.attr,
1224 &dev_attr_trigger_event.attr,
1225 &dev_attr_enable_event.attr,
1226 &dev_attr_fifofull_level.attr,
1227 &dev_attr_addr_idx.attr,
1228 &dev_attr_addr_single.attr,
1229 &dev_attr_addr_range.attr,
1230 &dev_attr_addr_start.attr,
1231 &dev_attr_addr_stop.attr,
1232 &dev_attr_addr_acctype.attr,
1233 &dev_attr_cntr_idx.attr,
1234 &dev_attr_cntr_rld_val.attr,
1235 &dev_attr_cntr_event.attr,
1236 &dev_attr_cntr_rld_event.attr,
1237 &dev_attr_cntr_val.attr,
1238 &dev_attr_seq_12_event.attr,
1239 &dev_attr_seq_21_event.attr,
1240 &dev_attr_seq_23_event.attr,
1241 &dev_attr_seq_31_event.attr,
1242 &dev_attr_seq_32_event.attr,
1243 &dev_attr_seq_13_event.attr,
1244 &dev_attr_seq_curr_state.attr,
1245 &dev_attr_ctxid_idx.attr,
1246 &dev_attr_ctxid_pid.attr,
1247 &dev_attr_ctxid_mask.attr,
1248 &dev_attr_sync_freq.attr,
1249 &dev_attr_timestamp_event.attr,
1250 &dev_attr_traceid.attr,
1251 &dev_attr_cpu.attr,
1252 NULL,
1255 #define coresight_etm3x_reg(name, offset) \
1256 coresight_simple_reg32(struct etm_drvdata, name, offset)
1258 coresight_etm3x_reg(etmccr, ETMCCR);
1259 coresight_etm3x_reg(etmccer, ETMCCER);
1260 coresight_etm3x_reg(etmscr, ETMSCR);
1261 coresight_etm3x_reg(etmidr, ETMIDR);
1262 coresight_etm3x_reg(etmcr, ETMCR);
1263 coresight_etm3x_reg(etmtraceidr, ETMTRACEIDR);
1264 coresight_etm3x_reg(etmteevr, ETMTEEVR);
1265 coresight_etm3x_reg(etmtssvr, ETMTSSCR);
1266 coresight_etm3x_reg(etmtecr1, ETMTECR1);
1267 coresight_etm3x_reg(etmtecr2, ETMTECR2);
1269 static struct attribute *coresight_etm_mgmt_attrs[] = {
1270 &dev_attr_etmccr.attr,
1271 &dev_attr_etmccer.attr,
1272 &dev_attr_etmscr.attr,
1273 &dev_attr_etmidr.attr,
1274 &dev_attr_etmcr.attr,
1275 &dev_attr_etmtraceidr.attr,
1276 &dev_attr_etmteevr.attr,
1277 &dev_attr_etmtssvr.attr,
1278 &dev_attr_etmtecr1.attr,
1279 &dev_attr_etmtecr2.attr,
1280 NULL,
1283 static const struct attribute_group coresight_etm_group = {
1284 .attrs = coresight_etm_attrs,
1287 static const struct attribute_group coresight_etm_mgmt_group = {
1288 .attrs = coresight_etm_mgmt_attrs,
1289 .name = "mgmt",
1292 const struct attribute_group *coresight_etm_groups[] = {
1293 &coresight_etm_group,
1294 &coresight_etm_mgmt_group,
1295 NULL,