1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright(C) 2015 Linaro Limited. All rights reserved.
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)
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)
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)
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)
71 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
72 struct etm_config *config = &drvdata->config;
74 ret = kstrtoul(buf, 16, &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 etm_release_trace_id(drvdata);
89 spin_unlock(&drvdata->spinlock);
94 static DEVICE_ATTR_WO(reset);
96 static ssize_t mode_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
100 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
101 struct etm_config *config = &drvdata->config;
104 return sprintf(buf, "%#lx\n", val);
107 static ssize_t mode_store(struct device *dev,
108 struct device_attribute *attr,
109 const char *buf, size_t size)
113 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
114 struct etm_config *config = &drvdata->config;
116 ret = kstrtoul(buf, 16, &val);
120 spin_lock(&drvdata->spinlock);
121 config->mode = val & ETM_MODE_ALL;
123 if (config->mode & ETM_MODE_EXCLUDE)
124 config->enable_ctrl1 |= ETMTECR1_INC_EXC;
126 config->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
128 if (config->mode & ETM_MODE_CYCACC)
129 config->ctrl |= ETMCR_CYC_ACC;
131 config->ctrl &= ~ETMCR_CYC_ACC;
133 if (config->mode & ETM_MODE_STALL) {
134 if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
135 dev_warn(dev, "stall mode not supported\n");
139 config->ctrl |= ETMCR_STALL_MODE;
141 config->ctrl &= ~ETMCR_STALL_MODE;
143 if (config->mode & ETM_MODE_TIMESTAMP) {
144 if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
145 dev_warn(dev, "timestamp not supported\n");
149 config->ctrl |= ETMCR_TIMESTAMP_EN;
151 config->ctrl &= ~ETMCR_TIMESTAMP_EN;
153 if (config->mode & ETM_MODE_CTXID)
154 config->ctrl |= ETMCR_CTXID_SIZE;
156 config->ctrl &= ~ETMCR_CTXID_SIZE;
158 if (config->mode & ETM_MODE_BBROAD)
159 config->ctrl |= ETMCR_BRANCH_BROADCAST;
161 config->ctrl &= ~ETMCR_BRANCH_BROADCAST;
163 if (config->mode & ETM_MODE_RET_STACK)
164 config->ctrl |= ETMCR_RETURN_STACK;
166 config->ctrl &= ~ETMCR_RETURN_STACK;
168 if (config->mode & (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
169 etm_config_trace_mode(config);
171 spin_unlock(&drvdata->spinlock);
176 spin_unlock(&drvdata->spinlock);
179 static DEVICE_ATTR_RW(mode);
181 static ssize_t trigger_event_show(struct device *dev,
182 struct device_attribute *attr, char *buf)
185 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
186 struct etm_config *config = &drvdata->config;
188 val = config->trigger_event;
189 return sprintf(buf, "%#lx\n", val);
192 static ssize_t trigger_event_store(struct device *dev,
193 struct device_attribute *attr,
194 const char *buf, size_t size)
198 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
199 struct etm_config *config = &drvdata->config;
201 ret = kstrtoul(buf, 16, &val);
205 config->trigger_event = val & ETM_EVENT_MASK;
209 static DEVICE_ATTR_RW(trigger_event);
211 static ssize_t enable_event_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
215 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
216 struct etm_config *config = &drvdata->config;
218 val = config->enable_event;
219 return sprintf(buf, "%#lx\n", val);
222 static ssize_t enable_event_store(struct device *dev,
223 struct device_attribute *attr,
224 const char *buf, size_t size)
228 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
229 struct etm_config *config = &drvdata->config;
231 ret = kstrtoul(buf, 16, &val);
235 config->enable_event = val & ETM_EVENT_MASK;
239 static DEVICE_ATTR_RW(enable_event);
241 static ssize_t fifofull_level_show(struct device *dev,
242 struct device_attribute *attr, char *buf)
245 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
246 struct etm_config *config = &drvdata->config;
248 val = config->fifofull_level;
249 return sprintf(buf, "%#lx\n", val);
252 static ssize_t fifofull_level_store(struct device *dev,
253 struct device_attribute *attr,
254 const char *buf, size_t size)
258 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
259 struct etm_config *config = &drvdata->config;
261 ret = kstrtoul(buf, 16, &val);
265 config->fifofull_level = val;
269 static DEVICE_ATTR_RW(fifofull_level);
271 static ssize_t addr_idx_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
275 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
276 struct etm_config *config = &drvdata->config;
278 val = config->addr_idx;
279 return sprintf(buf, "%#lx\n", val);
282 static ssize_t addr_idx_store(struct device *dev,
283 struct device_attribute *attr,
284 const char *buf, size_t size)
288 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
289 struct etm_config *config = &drvdata->config;
291 ret = kstrtoul(buf, 16, &val);
295 if (val >= drvdata->nr_addr_cmp)
299 * Use spinlock to ensure index doesn't change while it gets
300 * dereferenced multiple times within a spinlock block elsewhere.
302 spin_lock(&drvdata->spinlock);
303 config->addr_idx = val;
304 spin_unlock(&drvdata->spinlock);
308 static DEVICE_ATTR_RW(addr_idx);
310 static ssize_t addr_single_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
315 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
316 struct etm_config *config = &drvdata->config;
318 spin_lock(&drvdata->spinlock);
319 idx = config->addr_idx;
320 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
321 config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
322 spin_unlock(&drvdata->spinlock);
326 val = config->addr_val[idx];
327 spin_unlock(&drvdata->spinlock);
329 return sprintf(buf, "%#lx\n", val);
332 static ssize_t addr_single_store(struct device *dev,
333 struct device_attribute *attr,
334 const char *buf, size_t size)
339 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
340 struct etm_config *config = &drvdata->config;
342 ret = kstrtoul(buf, 16, &val);
346 spin_lock(&drvdata->spinlock);
347 idx = config->addr_idx;
348 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
349 config->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
350 spin_unlock(&drvdata->spinlock);
354 config->addr_val[idx] = val;
355 config->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
356 spin_unlock(&drvdata->spinlock);
360 static DEVICE_ATTR_RW(addr_single);
362 static ssize_t addr_range_show(struct device *dev,
363 struct device_attribute *attr, char *buf)
366 unsigned long val1, val2;
367 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
368 struct etm_config *config = &drvdata->config;
370 spin_lock(&drvdata->spinlock);
371 idx = config->addr_idx;
373 spin_unlock(&drvdata->spinlock);
376 if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
377 config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
378 (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
379 config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
380 spin_unlock(&drvdata->spinlock);
384 val1 = config->addr_val[idx];
385 val2 = config->addr_val[idx + 1];
386 spin_unlock(&drvdata->spinlock);
388 return sprintf(buf, "%#lx %#lx\n", val1, val2);
391 static ssize_t addr_range_store(struct device *dev,
392 struct device_attribute *attr,
393 const char *buf, size_t size)
396 unsigned long val1, val2;
397 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
398 struct etm_config *config = &drvdata->config;
400 if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
402 /* Lower address comparator cannot have a higher address value */
406 spin_lock(&drvdata->spinlock);
407 idx = config->addr_idx;
409 spin_unlock(&drvdata->spinlock);
412 if (!((config->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
413 config->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
414 (config->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
415 config->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
416 spin_unlock(&drvdata->spinlock);
420 config->addr_val[idx] = val1;
421 config->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
422 config->addr_val[idx + 1] = val2;
423 config->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
424 config->enable_ctrl1 |= (1 << (idx/2));
425 spin_unlock(&drvdata->spinlock);
429 static DEVICE_ATTR_RW(addr_range);
431 static ssize_t addr_start_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
436 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
437 struct etm_config *config = &drvdata->config;
439 spin_lock(&drvdata->spinlock);
440 idx = config->addr_idx;
441 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
442 config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
443 spin_unlock(&drvdata->spinlock);
447 val = config->addr_val[idx];
448 spin_unlock(&drvdata->spinlock);
450 return sprintf(buf, "%#lx\n", val);
453 static ssize_t addr_start_store(struct device *dev,
454 struct device_attribute *attr,
455 const char *buf, size_t size)
460 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
461 struct etm_config *config = &drvdata->config;
463 ret = kstrtoul(buf, 16, &val);
467 spin_lock(&drvdata->spinlock);
468 idx = config->addr_idx;
469 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
470 config->addr_type[idx] == ETM_ADDR_TYPE_START)) {
471 spin_unlock(&drvdata->spinlock);
475 config->addr_val[idx] = val;
476 config->addr_type[idx] = ETM_ADDR_TYPE_START;
477 config->startstop_ctrl |= (1 << idx);
478 config->enable_ctrl1 |= ETMTECR1_START_STOP;
479 spin_unlock(&drvdata->spinlock);
483 static DEVICE_ATTR_RW(addr_start);
485 static ssize_t addr_stop_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
490 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
491 struct etm_config *config = &drvdata->config;
493 spin_lock(&drvdata->spinlock);
494 idx = config->addr_idx;
495 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
496 config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
497 spin_unlock(&drvdata->spinlock);
501 val = config->addr_val[idx];
502 spin_unlock(&drvdata->spinlock);
504 return sprintf(buf, "%#lx\n", val);
507 static ssize_t addr_stop_store(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t size)
514 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
515 struct etm_config *config = &drvdata->config;
517 ret = kstrtoul(buf, 16, &val);
521 spin_lock(&drvdata->spinlock);
522 idx = config->addr_idx;
523 if (!(config->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
524 config->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
525 spin_unlock(&drvdata->spinlock);
529 config->addr_val[idx] = val;
530 config->addr_type[idx] = ETM_ADDR_TYPE_STOP;
531 config->startstop_ctrl |= (1 << (idx + 16));
532 config->enable_ctrl1 |= ETMTECR1_START_STOP;
533 spin_unlock(&drvdata->spinlock);
537 static DEVICE_ATTR_RW(addr_stop);
539 static ssize_t addr_acctype_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
543 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
544 struct etm_config *config = &drvdata->config;
546 spin_lock(&drvdata->spinlock);
547 val = config->addr_acctype[config->addr_idx];
548 spin_unlock(&drvdata->spinlock);
550 return sprintf(buf, "%#lx\n", val);
553 static ssize_t addr_acctype_store(struct device *dev,
554 struct device_attribute *attr,
555 const char *buf, size_t size)
559 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
560 struct etm_config *config = &drvdata->config;
562 ret = kstrtoul(buf, 16, &val);
566 spin_lock(&drvdata->spinlock);
567 config->addr_acctype[config->addr_idx] = val;
568 spin_unlock(&drvdata->spinlock);
572 static DEVICE_ATTR_RW(addr_acctype);
574 static ssize_t cntr_idx_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
578 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
579 struct etm_config *config = &drvdata->config;
581 val = config->cntr_idx;
582 return sprintf(buf, "%#lx\n", val);
585 static ssize_t cntr_idx_store(struct device *dev,
586 struct device_attribute *attr,
587 const char *buf, size_t size)
591 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
592 struct etm_config *config = &drvdata->config;
594 ret = kstrtoul(buf, 16, &val);
598 if (val >= drvdata->nr_cntr)
601 * Use spinlock to ensure index doesn't change while it gets
602 * dereferenced multiple times within a spinlock block elsewhere.
604 spin_lock(&drvdata->spinlock);
605 config->cntr_idx = val;
606 spin_unlock(&drvdata->spinlock);
610 static DEVICE_ATTR_RW(cntr_idx);
612 static ssize_t cntr_rld_val_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
616 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
617 struct etm_config *config = &drvdata->config;
619 spin_lock(&drvdata->spinlock);
620 val = config->cntr_rld_val[config->cntr_idx];
621 spin_unlock(&drvdata->spinlock);
623 return sprintf(buf, "%#lx\n", val);
626 static ssize_t cntr_rld_val_store(struct device *dev,
627 struct device_attribute *attr,
628 const char *buf, size_t size)
632 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
633 struct etm_config *config = &drvdata->config;
635 ret = kstrtoul(buf, 16, &val);
639 spin_lock(&drvdata->spinlock);
640 config->cntr_rld_val[config->cntr_idx] = val;
641 spin_unlock(&drvdata->spinlock);
645 static DEVICE_ATTR_RW(cntr_rld_val);
647 static ssize_t cntr_event_show(struct device *dev,
648 struct device_attribute *attr, char *buf)
651 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
652 struct etm_config *config = &drvdata->config;
654 spin_lock(&drvdata->spinlock);
655 val = config->cntr_event[config->cntr_idx];
656 spin_unlock(&drvdata->spinlock);
658 return sprintf(buf, "%#lx\n", val);
661 static ssize_t cntr_event_store(struct device *dev,
662 struct device_attribute *attr,
663 const char *buf, size_t size)
667 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
668 struct etm_config *config = &drvdata->config;
670 ret = kstrtoul(buf, 16, &val);
674 spin_lock(&drvdata->spinlock);
675 config->cntr_event[config->cntr_idx] = val & ETM_EVENT_MASK;
676 spin_unlock(&drvdata->spinlock);
680 static DEVICE_ATTR_RW(cntr_event);
682 static ssize_t cntr_rld_event_show(struct device *dev,
683 struct device_attribute *attr, char *buf)
686 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
687 struct etm_config *config = &drvdata->config;
689 spin_lock(&drvdata->spinlock);
690 val = config->cntr_rld_event[config->cntr_idx];
691 spin_unlock(&drvdata->spinlock);
693 return sprintf(buf, "%#lx\n", val);
696 static ssize_t cntr_rld_event_store(struct device *dev,
697 struct device_attribute *attr,
698 const char *buf, size_t size)
702 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
703 struct etm_config *config = &drvdata->config;
705 ret = kstrtoul(buf, 16, &val);
709 spin_lock(&drvdata->spinlock);
710 config->cntr_rld_event[config->cntr_idx] = val & ETM_EVENT_MASK;
711 spin_unlock(&drvdata->spinlock);
715 static DEVICE_ATTR_RW(cntr_rld_event);
717 static ssize_t cntr_val_show(struct device *dev,
718 struct device_attribute *attr, char *buf)
722 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
723 struct etm_config *config = &drvdata->config;
725 if (!local_read(&drvdata->mode)) {
726 spin_lock(&drvdata->spinlock);
727 for (i = 0; i < drvdata->nr_cntr; i++)
728 ret += sprintf(buf, "counter %d: %x\n",
729 i, config->cntr_val[i]);
730 spin_unlock(&drvdata->spinlock);
734 for (i = 0; i < drvdata->nr_cntr; i++) {
735 val = etm_readl(drvdata, ETMCNTVRn(i));
736 ret += sprintf(buf, "counter %d: %x\n", i, val);
742 static ssize_t cntr_val_store(struct device *dev,
743 struct device_attribute *attr,
744 const char *buf, size_t size)
748 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
749 struct etm_config *config = &drvdata->config;
751 ret = kstrtoul(buf, 16, &val);
755 spin_lock(&drvdata->spinlock);
756 config->cntr_val[config->cntr_idx] = val;
757 spin_unlock(&drvdata->spinlock);
761 static DEVICE_ATTR_RW(cntr_val);
763 static ssize_t seq_12_event_show(struct device *dev,
764 struct device_attribute *attr, char *buf)
767 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
768 struct etm_config *config = &drvdata->config;
770 val = config->seq_12_event;
771 return sprintf(buf, "%#lx\n", val);
774 static ssize_t seq_12_event_store(struct device *dev,
775 struct device_attribute *attr,
776 const char *buf, size_t size)
780 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
781 struct etm_config *config = &drvdata->config;
783 ret = kstrtoul(buf, 16, &val);
787 config->seq_12_event = val & ETM_EVENT_MASK;
790 static DEVICE_ATTR_RW(seq_12_event);
792 static ssize_t seq_21_event_show(struct device *dev,
793 struct device_attribute *attr, char *buf)
796 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
797 struct etm_config *config = &drvdata->config;
799 val = config->seq_21_event;
800 return sprintf(buf, "%#lx\n", val);
803 static ssize_t seq_21_event_store(struct device *dev,
804 struct device_attribute *attr,
805 const char *buf, size_t size)
809 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
810 struct etm_config *config = &drvdata->config;
812 ret = kstrtoul(buf, 16, &val);
816 config->seq_21_event = val & ETM_EVENT_MASK;
819 static DEVICE_ATTR_RW(seq_21_event);
821 static ssize_t seq_23_event_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
825 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
826 struct etm_config *config = &drvdata->config;
828 val = config->seq_23_event;
829 return sprintf(buf, "%#lx\n", val);
832 static ssize_t seq_23_event_store(struct device *dev,
833 struct device_attribute *attr,
834 const char *buf, size_t size)
838 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
839 struct etm_config *config = &drvdata->config;
841 ret = kstrtoul(buf, 16, &val);
845 config->seq_23_event = val & ETM_EVENT_MASK;
848 static DEVICE_ATTR_RW(seq_23_event);
850 static ssize_t seq_31_event_show(struct device *dev,
851 struct device_attribute *attr, char *buf)
854 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
855 struct etm_config *config = &drvdata->config;
857 val = config->seq_31_event;
858 return sprintf(buf, "%#lx\n", val);
861 static ssize_t seq_31_event_store(struct device *dev,
862 struct device_attribute *attr,
863 const char *buf, size_t size)
867 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
868 struct etm_config *config = &drvdata->config;
870 ret = kstrtoul(buf, 16, &val);
874 config->seq_31_event = val & ETM_EVENT_MASK;
877 static DEVICE_ATTR_RW(seq_31_event);
879 static ssize_t seq_32_event_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
883 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
884 struct etm_config *config = &drvdata->config;
886 val = config->seq_32_event;
887 return sprintf(buf, "%#lx\n", val);
890 static ssize_t seq_32_event_store(struct device *dev,
891 struct device_attribute *attr,
892 const char *buf, size_t size)
896 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
897 struct etm_config *config = &drvdata->config;
899 ret = kstrtoul(buf, 16, &val);
903 config->seq_32_event = val & ETM_EVENT_MASK;
906 static DEVICE_ATTR_RW(seq_32_event);
908 static ssize_t seq_13_event_show(struct device *dev,
909 struct device_attribute *attr, char *buf)
912 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
913 struct etm_config *config = &drvdata->config;
915 val = config->seq_13_event;
916 return sprintf(buf, "%#lx\n", val);
919 static ssize_t seq_13_event_store(struct device *dev,
920 struct device_attribute *attr,
921 const char *buf, size_t size)
925 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
926 struct etm_config *config = &drvdata->config;
928 ret = kstrtoul(buf, 16, &val);
932 config->seq_13_event = val & ETM_EVENT_MASK;
935 static DEVICE_ATTR_RW(seq_13_event);
937 static ssize_t seq_curr_state_show(struct device *dev,
938 struct device_attribute *attr, char *buf)
940 unsigned long val, flags;
941 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
942 struct etm_config *config = &drvdata->config;
944 if (!local_read(&drvdata->mode)) {
945 val = config->seq_curr_state;
949 pm_runtime_get_sync(dev->parent);
950 spin_lock_irqsave(&drvdata->spinlock, flags);
952 CS_UNLOCK(drvdata->base);
953 val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
954 CS_LOCK(drvdata->base);
956 spin_unlock_irqrestore(&drvdata->spinlock, flags);
957 pm_runtime_put(dev->parent);
959 return sprintf(buf, "%#lx\n", val);
962 static ssize_t seq_curr_state_store(struct device *dev,
963 struct device_attribute *attr,
964 const char *buf, size_t size)
968 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
969 struct etm_config *config = &drvdata->config;
971 ret = kstrtoul(buf, 16, &val);
975 if (val > ETM_SEQ_STATE_MAX_VAL)
978 config->seq_curr_state = val;
982 static DEVICE_ATTR_RW(seq_curr_state);
984 static ssize_t ctxid_idx_show(struct device *dev,
985 struct device_attribute *attr, char *buf)
988 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
989 struct etm_config *config = &drvdata->config;
991 val = config->ctxid_idx;
992 return sprintf(buf, "%#lx\n", val);
995 static ssize_t ctxid_idx_store(struct device *dev,
996 struct device_attribute *attr,
997 const char *buf, size_t size)
1001 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1002 struct etm_config *config = &drvdata->config;
1004 ret = kstrtoul(buf, 16, &val);
1008 if (val >= drvdata->nr_ctxid_cmp)
1012 * Use spinlock to ensure index doesn't change while it gets
1013 * dereferenced multiple times within a spinlock block elsewhere.
1015 spin_lock(&drvdata->spinlock);
1016 config->ctxid_idx = val;
1017 spin_unlock(&drvdata->spinlock);
1021 static DEVICE_ATTR_RW(ctxid_idx);
1023 static ssize_t ctxid_pid_show(struct device *dev,
1024 struct device_attribute *attr, char *buf)
1027 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1028 struct etm_config *config = &drvdata->config;
1031 * Don't use contextID tracing if coming from a PID namespace. See
1032 * comment in ctxid_pid_store().
1034 if (task_active_pid_ns(current) != &init_pid_ns)
1037 spin_lock(&drvdata->spinlock);
1038 val = config->ctxid_pid[config->ctxid_idx];
1039 spin_unlock(&drvdata->spinlock);
1041 return sprintf(buf, "%#lx\n", val);
1044 static ssize_t ctxid_pid_store(struct device *dev,
1045 struct device_attribute *attr,
1046 const char *buf, size_t size)
1050 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1051 struct etm_config *config = &drvdata->config;
1054 * When contextID tracing is enabled the tracers will insert the
1055 * value found in the contextID register in the trace stream. But if
1056 * a process is in a namespace the PID of that process as seen from the
1057 * namespace won't be what the kernel sees, something that makes the
1058 * feature confusing and can potentially leak kernel only information.
1059 * As such refuse to use the feature if @current is not in the initial
1062 if (task_active_pid_ns(current) != &init_pid_ns)
1065 ret = kstrtoul(buf, 16, &pid);
1069 spin_lock(&drvdata->spinlock);
1070 config->ctxid_pid[config->ctxid_idx] = pid;
1071 spin_unlock(&drvdata->spinlock);
1075 static DEVICE_ATTR_RW(ctxid_pid);
1077 static ssize_t ctxid_mask_show(struct device *dev,
1078 struct device_attribute *attr, char *buf)
1081 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1082 struct etm_config *config = &drvdata->config;
1085 * Don't use contextID tracing if coming from a PID namespace. See
1086 * comment in ctxid_pid_store().
1088 if (task_active_pid_ns(current) != &init_pid_ns)
1091 val = config->ctxid_mask;
1092 return sprintf(buf, "%#lx\n", val);
1095 static ssize_t ctxid_mask_store(struct device *dev,
1096 struct device_attribute *attr,
1097 const char *buf, size_t size)
1101 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1102 struct etm_config *config = &drvdata->config;
1105 * Don't use contextID tracing if coming from a PID namespace. See
1106 * comment in ctxid_pid_store().
1108 if (task_active_pid_ns(current) != &init_pid_ns)
1111 ret = kstrtoul(buf, 16, &val);
1115 config->ctxid_mask = val;
1118 static DEVICE_ATTR_RW(ctxid_mask);
1120 static ssize_t sync_freq_show(struct device *dev,
1121 struct device_attribute *attr, char *buf)
1124 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1125 struct etm_config *config = &drvdata->config;
1127 val = config->sync_freq;
1128 return sprintf(buf, "%#lx\n", val);
1131 static ssize_t sync_freq_store(struct device *dev,
1132 struct device_attribute *attr,
1133 const char *buf, size_t size)
1137 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1138 struct etm_config *config = &drvdata->config;
1140 ret = kstrtoul(buf, 16, &val);
1144 config->sync_freq = val & ETM_SYNC_MASK;
1147 static DEVICE_ATTR_RW(sync_freq);
1149 static ssize_t timestamp_event_show(struct device *dev,
1150 struct device_attribute *attr, char *buf)
1153 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1154 struct etm_config *config = &drvdata->config;
1156 val = config->timestamp_event;
1157 return sprintf(buf, "%#lx\n", val);
1160 static ssize_t timestamp_event_store(struct device *dev,
1161 struct device_attribute *attr,
1162 const char *buf, size_t size)
1166 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1167 struct etm_config *config = &drvdata->config;
1169 ret = kstrtoul(buf, 16, &val);
1173 config->timestamp_event = val & ETM_EVENT_MASK;
1176 static DEVICE_ATTR_RW(timestamp_event);
1178 static ssize_t cpu_show(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1182 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1185 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1188 static DEVICE_ATTR_RO(cpu);
1190 static ssize_t traceid_show(struct device *dev,
1191 struct device_attribute *attr, char *buf)
1194 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1196 trace_id = etm_read_alloc_trace_id(drvdata);
1200 return sysfs_emit(buf, "%#x\n", trace_id);
1202 static DEVICE_ATTR_RO(traceid);
1204 static struct attribute *coresight_etm_attrs[] = {
1205 &dev_attr_nr_addr_cmp.attr,
1206 &dev_attr_nr_cntr.attr,
1207 &dev_attr_nr_ctxid_cmp.attr,
1208 &dev_attr_etmsr.attr,
1209 &dev_attr_reset.attr,
1210 &dev_attr_mode.attr,
1211 &dev_attr_trigger_event.attr,
1212 &dev_attr_enable_event.attr,
1213 &dev_attr_fifofull_level.attr,
1214 &dev_attr_addr_idx.attr,
1215 &dev_attr_addr_single.attr,
1216 &dev_attr_addr_range.attr,
1217 &dev_attr_addr_start.attr,
1218 &dev_attr_addr_stop.attr,
1219 &dev_attr_addr_acctype.attr,
1220 &dev_attr_cntr_idx.attr,
1221 &dev_attr_cntr_rld_val.attr,
1222 &dev_attr_cntr_event.attr,
1223 &dev_attr_cntr_rld_event.attr,
1224 &dev_attr_cntr_val.attr,
1225 &dev_attr_seq_12_event.attr,
1226 &dev_attr_seq_21_event.attr,
1227 &dev_attr_seq_23_event.attr,
1228 &dev_attr_seq_31_event.attr,
1229 &dev_attr_seq_32_event.attr,
1230 &dev_attr_seq_13_event.attr,
1231 &dev_attr_seq_curr_state.attr,
1232 &dev_attr_ctxid_idx.attr,
1233 &dev_attr_ctxid_pid.attr,
1234 &dev_attr_ctxid_mask.attr,
1235 &dev_attr_sync_freq.attr,
1236 &dev_attr_timestamp_event.attr,
1237 &dev_attr_traceid.attr,
1242 static struct attribute *coresight_etm_mgmt_attrs[] = {
1243 coresight_simple_reg32(etmccr, ETMCCR),
1244 coresight_simple_reg32(etmccer, ETMCCER),
1245 coresight_simple_reg32(etmscr, ETMSCR),
1246 coresight_simple_reg32(etmidr, ETMIDR),
1247 coresight_simple_reg32(etmcr, ETMCR),
1248 coresight_simple_reg32(etmtraceidr, ETMTRACEIDR),
1249 coresight_simple_reg32(etmteevr, ETMTEEVR),
1250 coresight_simple_reg32(etmtssvr, ETMTSSCR),
1251 coresight_simple_reg32(etmtecr1, ETMTECR1),
1252 coresight_simple_reg32(etmtecr2, ETMTECR2),
1256 static const struct attribute_group coresight_etm_group = {
1257 .attrs = coresight_etm_attrs,
1260 static const struct attribute_group coresight_etm_mgmt_group = {
1261 .attrs = coresight_etm_mgmt_attrs,
1265 const struct attribute_group *coresight_etm_groups[] = {
1266 &coresight_etm_group,
1267 &coresight_etm_mgmt_group,