1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
6 #include <linux/kernel.h>
7 #include <linux/moduleparam.h>
8 #include <linux/init.h>
9 #include <linux/types.h>
10 #include <linux/device.h>
12 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <linux/smp.h>
17 #include <linux/sysfs.h>
18 #include <linux/stat.h>
19 #include <linux/clk.h>
20 #include <linux/cpu.h>
21 #include <linux/coresight.h>
22 #include <linux/coresight-pmu.h>
23 #include <linux/pm_wakeup.h>
24 #include <linux/amba/bus.h>
25 #include <linux/seq_file.h>
26 #include <linux/uaccess.h>
27 #include <linux/perf_event.h>
28 #include <linux/pm_runtime.h>
29 #include <asm/sections.h>
30 #include <asm/local.h>
33 #include "coresight-etm4x.h"
34 #include "coresight-etm-perf.h"
36 static int boot_enable;
37 module_param_named(boot_enable, boot_enable, int, S_IRUGO);
39 /* The number of ETMv4 currently registered */
40 static int etm4_count;
41 static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
42 static void etm4_set_default_config(struct etmv4_config *config);
43 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
44 struct perf_event *event);
46 static enum cpuhp_state hp_online;
48 static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
50 /* Writing any value to ETMOSLAR unlocks the trace registers */
51 writel_relaxed(0x0, drvdata->base + TRCOSLAR);
52 drvdata->os_unlock = true;
56 static bool etm4_arch_supported(u8 arch)
58 /* Mask out the minor version number */
59 switch (arch & 0xf0) {
68 static int etm4_cpu_id(struct coresight_device *csdev)
70 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
75 static int etm4_trace_id(struct coresight_device *csdev)
77 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
79 return drvdata->trcid;
82 static void etm4_enable_hw(void *info)
85 struct etmv4_drvdata *drvdata = info;
86 struct etmv4_config *config = &drvdata->config;
88 CS_UNLOCK(drvdata->base);
90 etm4_os_unlock(drvdata);
92 /* Disable the trace unit before programming trace registers */
93 writel_relaxed(0, drvdata->base + TRCPRGCTLR);
95 /* wait for TRCSTATR.IDLE to go up */
96 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
98 "timeout while waiting for Idle Trace Status\n");
100 writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
101 writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
102 /* nothing specific implemented */
103 writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
104 writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
105 writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
106 writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
107 writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
108 writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
109 writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
110 writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
111 writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
112 writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
113 writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
114 writel_relaxed(config->vissctlr,
115 drvdata->base + TRCVISSCTLR);
116 writel_relaxed(config->vipcssctlr,
117 drvdata->base + TRCVIPCSSCTLR);
118 for (i = 0; i < drvdata->nrseqstate - 1; i++)
119 writel_relaxed(config->seq_ctrl[i],
120 drvdata->base + TRCSEQEVRn(i));
121 writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
122 writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
123 writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
124 for (i = 0; i < drvdata->nr_cntr; i++) {
125 writel_relaxed(config->cntrldvr[i],
126 drvdata->base + TRCCNTRLDVRn(i));
127 writel_relaxed(config->cntr_ctrl[i],
128 drvdata->base + TRCCNTCTLRn(i));
129 writel_relaxed(config->cntr_val[i],
130 drvdata->base + TRCCNTVRn(i));
133 /* Resource selector pair 0 is always implemented and reserved */
134 for (i = 0; i < drvdata->nr_resource * 2; i++)
135 writel_relaxed(config->res_ctrl[i],
136 drvdata->base + TRCRSCTLRn(i));
138 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
139 writel_relaxed(config->ss_ctrl[i],
140 drvdata->base + TRCSSCCRn(i));
141 writel_relaxed(config->ss_status[i],
142 drvdata->base + TRCSSCSRn(i));
143 writel_relaxed(config->ss_pe_cmp[i],
144 drvdata->base + TRCSSPCICRn(i));
146 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
147 writeq_relaxed(config->addr_val[i],
148 drvdata->base + TRCACVRn(i));
149 writeq_relaxed(config->addr_acc[i],
150 drvdata->base + TRCACATRn(i));
152 for (i = 0; i < drvdata->numcidc; i++)
153 writeq_relaxed(config->ctxid_pid[i],
154 drvdata->base + TRCCIDCVRn(i));
155 writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
156 writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
158 for (i = 0; i < drvdata->numvmidc; i++)
159 writeq_relaxed(config->vmid_val[i],
160 drvdata->base + TRCVMIDCVRn(i));
161 writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
162 writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
165 * Request to keep the trace unit powered and also
166 * emulation of powerdown
168 writel_relaxed(readl_relaxed(drvdata->base + TRCPDCR) | TRCPDCR_PU,
169 drvdata->base + TRCPDCR);
171 /* Enable the trace unit */
172 writel_relaxed(1, drvdata->base + TRCPRGCTLR);
174 /* wait for TRCSTATR.IDLE to go back down to '0' */
175 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
176 dev_err(drvdata->dev,
177 "timeout while waiting for Idle Trace Status\n");
179 * As recommended by section 4.3.7 ("Synchronization when using the
180 * memory-mapped interface") of ARM IHI 0064D
185 CS_LOCK(drvdata->base);
187 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
190 static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
191 struct perf_event *event)
194 struct etmv4_config *config = &drvdata->config;
195 struct perf_event_attr *attr = &event->attr;
202 /* Clear configuration from previous run */
203 memset(config, 0, sizeof(struct etmv4_config));
205 if (attr->exclude_kernel)
206 config->mode = ETM_MODE_EXCL_KERN;
208 if (attr->exclude_user)
209 config->mode = ETM_MODE_EXCL_USER;
211 /* Always start from the default config */
212 etm4_set_default_config(config);
214 /* Configure filters specified on the perf cmd line, if any. */
215 ret = etm4_set_event_filters(drvdata, event);
219 /* Go from generic option to ETMv4 specifics */
220 if (attr->config & BIT(ETM_OPT_CYCACC)) {
221 config->cfg |= BIT(4);
222 /* TRM: Must program this for cycacc to work */
223 config->ccctlr = ETM_CYC_THRESHOLD_DEFAULT;
225 if (attr->config & BIT(ETM_OPT_TS))
226 /* bit[11], Global timestamp tracing bit */
227 config->cfg |= BIT(11);
228 /* return stack - enable if selected and supported */
229 if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack)
230 /* bit[12], Return stack enable bit */
231 config->cfg |= BIT(12);
237 static int etm4_enable_perf(struct coresight_device *csdev,
238 struct perf_event *event)
241 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
243 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) {
248 /* Configure the tracer based on the session's specifics */
249 ret = etm4_parse_event_config(drvdata, event);
253 etm4_enable_hw(drvdata);
259 static int etm4_enable_sysfs(struct coresight_device *csdev)
261 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
264 spin_lock(&drvdata->spinlock);
267 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
268 * ensures that register writes occur when cpu is powered.
270 ret = smp_call_function_single(drvdata->cpu,
271 etm4_enable_hw, drvdata, 1);
275 drvdata->sticky_enable = true;
276 spin_unlock(&drvdata->spinlock);
278 dev_info(drvdata->dev, "ETM tracing enabled\n");
282 spin_unlock(&drvdata->spinlock);
286 static int etm4_enable(struct coresight_device *csdev,
287 struct perf_event *event, u32 mode)
291 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
293 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
295 /* Someone is already using the tracer */
301 ret = etm4_enable_sysfs(csdev);
304 ret = etm4_enable_perf(csdev, event);
310 /* The tracer didn't start */
312 local_set(&drvdata->mode, CS_MODE_DISABLED);
317 static void etm4_disable_hw(void *info)
320 struct etmv4_drvdata *drvdata = info;
322 CS_UNLOCK(drvdata->base);
324 /* power can be removed from the trace unit now */
325 control = readl_relaxed(drvdata->base + TRCPDCR);
326 control &= ~TRCPDCR_PU;
327 writel_relaxed(control, drvdata->base + TRCPDCR);
329 control = readl_relaxed(drvdata->base + TRCPRGCTLR);
331 /* EN, bit[0] Trace unit enable bit */
335 * Make sure everything completes before disabling, as recommended
336 * by section 7.3.77 ("TRCVICTLR, ViewInst Main Control Register,
337 * SSTATUS") of ARM IHI 0064D
341 writel_relaxed(control, drvdata->base + TRCPRGCTLR);
343 CS_LOCK(drvdata->base);
345 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
348 static int etm4_disable_perf(struct coresight_device *csdev,
349 struct perf_event *event)
352 struct etm_filters *filters = event->hw.addr_filters;
353 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
355 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
358 etm4_disable_hw(drvdata);
361 * Check if the start/stop logic was active when the unit was stopped.
362 * That way we can re-enable the start/stop logic when the process is
363 * scheduled again. Configuration of the start/stop logic happens in
364 * function etm4_set_event_filters().
366 control = readl_relaxed(drvdata->base + TRCVICTLR);
367 /* TRCVICTLR::SSSTATUS, bit[9] */
368 filters->ssstatus = (control & BIT(9));
373 static void etm4_disable_sysfs(struct coresight_device *csdev)
375 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
378 * Taking hotplug lock here protects from clocks getting disabled
379 * with tracing being left on (crash scenario) if user disable occurs
380 * after cpu online mask indicates the cpu is offline but before the
381 * DYING hotplug callback is serviced by the ETM driver.
384 spin_lock(&drvdata->spinlock);
387 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
388 * ensures that register writes occur when cpu is powered.
390 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
392 spin_unlock(&drvdata->spinlock);
395 dev_info(drvdata->dev, "ETM tracing disabled\n");
398 static void etm4_disable(struct coresight_device *csdev,
399 struct perf_event *event)
402 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
405 * For as long as the tracer isn't disabled another entity can't
406 * change its status. As such we can read the status here without
407 * fearing it will change under us.
409 mode = local_read(&drvdata->mode);
412 case CS_MODE_DISABLED:
415 etm4_disable_sysfs(csdev);
418 etm4_disable_perf(csdev, event);
423 local_set(&drvdata->mode, CS_MODE_DISABLED);
426 static const struct coresight_ops_source etm4_source_ops = {
427 .cpu_id = etm4_cpu_id,
428 .trace_id = etm4_trace_id,
429 .enable = etm4_enable,
430 .disable = etm4_disable,
433 static const struct coresight_ops etm4_cs_ops = {
434 .source_ops = &etm4_source_ops,
437 static void etm4_init_arch_data(void *info)
445 struct etmv4_drvdata *drvdata = info;
447 /* Make sure all registers are accessible */
448 etm4_os_unlock(drvdata);
450 CS_UNLOCK(drvdata->base);
452 /* find all capabilities of the tracing unit */
453 etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
455 /* INSTP0, bits[2:1] P0 tracing support field */
456 if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
457 drvdata->instrp0 = true;
459 drvdata->instrp0 = false;
461 /* TRCBB, bit[5] Branch broadcast tracing support bit */
462 if (BMVAL(etmidr0, 5, 5))
463 drvdata->trcbb = true;
465 drvdata->trcbb = false;
467 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
468 if (BMVAL(etmidr0, 6, 6))
469 drvdata->trccond = true;
471 drvdata->trccond = false;
473 /* TRCCCI, bit[7] Cycle counting instruction bit */
474 if (BMVAL(etmidr0, 7, 7))
475 drvdata->trccci = true;
477 drvdata->trccci = false;
479 /* RETSTACK, bit[9] Return stack bit */
480 if (BMVAL(etmidr0, 9, 9))
481 drvdata->retstack = true;
483 drvdata->retstack = false;
485 /* NUMEVENT, bits[11:10] Number of events field */
486 drvdata->nr_event = BMVAL(etmidr0, 10, 11);
487 /* QSUPP, bits[16:15] Q element support field */
488 drvdata->q_support = BMVAL(etmidr0, 15, 16);
489 /* TSSIZE, bits[28:24] Global timestamp size field */
490 drvdata->ts_size = BMVAL(etmidr0, 24, 28);
492 /* base architecture of trace unit */
493 etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
495 * TRCARCHMIN, bits[7:4] architecture the minor version number
496 * TRCARCHMAJ, bits[11:8] architecture major versin number
498 drvdata->arch = BMVAL(etmidr1, 4, 11);
500 /* maximum size of resources */
501 etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
502 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
503 drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
504 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
505 drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
506 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
507 drvdata->ccsize = BMVAL(etmidr2, 25, 28);
509 etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
510 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
511 drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
512 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
513 drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
514 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
515 drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
518 * TRCERR, bit[24] whether a trace unit can trace a
519 * system error exception.
521 if (BMVAL(etmidr3, 24, 24))
522 drvdata->trc_error = true;
524 drvdata->trc_error = false;
526 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
527 if (BMVAL(etmidr3, 25, 25))
528 drvdata->syncpr = true;
530 drvdata->syncpr = false;
532 /* STALLCTL, bit[26] is stall control implemented? */
533 if (BMVAL(etmidr3, 26, 26))
534 drvdata->stallctl = true;
536 drvdata->stallctl = false;
538 /* SYSSTALL, bit[27] implementation can support stall control? */
539 if (BMVAL(etmidr3, 27, 27))
540 drvdata->sysstall = true;
542 drvdata->sysstall = false;
544 /* NUMPROC, bits[30:28] the number of PEs available for tracing */
545 drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
547 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
548 if (BMVAL(etmidr3, 31, 31))
549 drvdata->nooverflow = true;
551 drvdata->nooverflow = false;
553 /* number of resources trace unit supports */
554 etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
555 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
556 drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
557 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
558 drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
560 * NUMRSPAIR, bits[19:16]
561 * The number of resource pairs conveyed by the HW starts at 0, i.e a
562 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
563 * As such add 1 to the value of NUMRSPAIR for a better representation.
565 drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
567 * NUMSSCC, bits[23:20] the number of single-shot
568 * comparator control for tracing
570 drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
571 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
572 drvdata->numcidc = BMVAL(etmidr4, 24, 27);
573 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
574 drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
576 etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
577 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
578 drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
579 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
580 drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
581 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
582 if (BMVAL(etmidr5, 22, 22))
583 drvdata->atbtrig = true;
585 drvdata->atbtrig = false;
587 * LPOVERRIDE, bit[23] implementation supports
588 * low-power state override
590 if (BMVAL(etmidr5, 23, 23))
591 drvdata->lpoverride = true;
593 drvdata->lpoverride = false;
594 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
595 drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
596 /* NUMCNTR, bits[30:28] number of counters available for tracing */
597 drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
598 CS_LOCK(drvdata->base);
601 static void etm4_set_default_config(struct etmv4_config *config)
603 /* disable all events tracing */
604 config->eventctrl0 = 0x0;
605 config->eventctrl1 = 0x0;
607 /* disable stalling */
608 config->stall_ctrl = 0x0;
610 /* enable trace synchronization every 4096 bytes, if available */
611 config->syncfreq = 0xC;
613 /* disable timestamp event */
614 config->ts_ctrl = 0x0;
616 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
617 config->vinst_ctrl |= BIT(0);
620 static u64 etm4_get_ns_access_type(struct etmv4_config *config)
625 * EXLEVEL_NS, bits[15:12]
626 * The Exception levels are:
627 * Bit[12] Exception level 0 - Application
628 * Bit[13] Exception level 1 - OS
629 * Bit[14] Exception level 2 - Hypervisor
630 * Bit[15] Never implemented
632 if (!is_kernel_in_hyp_mode()) {
633 /* Stay away from hypervisor mode for non-VHE */
634 access_type = ETM_EXLEVEL_NS_HYP;
635 if (config->mode & ETM_MODE_EXCL_KERN)
636 access_type |= ETM_EXLEVEL_NS_OS;
637 } else if (config->mode & ETM_MODE_EXCL_KERN) {
638 access_type = ETM_EXLEVEL_NS_HYP;
641 if (config->mode & ETM_MODE_EXCL_USER)
642 access_type |= ETM_EXLEVEL_NS_APP;
647 static u64 etm4_get_access_type(struct etmv4_config *config)
649 u64 access_type = etm4_get_ns_access_type(config);
652 * EXLEVEL_S, bits[11:8], don't trace anything happening
655 access_type |= (ETM_EXLEVEL_S_APP |
662 static void etm4_set_comparator_filter(struct etmv4_config *config,
663 u64 start, u64 stop, int comparator)
665 u64 access_type = etm4_get_access_type(config);
667 /* First half of default address comparator */
668 config->addr_val[comparator] = start;
669 config->addr_acc[comparator] = access_type;
670 config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
672 /* Second half of default address comparator */
673 config->addr_val[comparator + 1] = stop;
674 config->addr_acc[comparator + 1] = access_type;
675 config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
678 * Configure the ViewInst function to include this address range
681 * @comparator is divided by two since it is the index in the
682 * etmv4_config::addr_val array but register TRCVIIECTLR deals with
683 * address range comparator _pairs_.
686 * index 0 -> compatator pair 0
687 * index 2 -> comparator pair 1
688 * index 4 -> comparator pair 2
690 * index 14 -> comparator pair 7
692 config->viiectlr |= BIT(comparator / 2);
695 static void etm4_set_start_stop_filter(struct etmv4_config *config,
696 u64 address, int comparator,
697 enum etm_addr_type type)
700 u64 access_type = etm4_get_access_type(config);
702 /* Configure the comparator */
703 config->addr_val[comparator] = address;
704 config->addr_acc[comparator] = access_type;
705 config->addr_type[comparator] = type;
708 * Configure ViewInst Start-Stop control register.
709 * Addresses configured to start tracing go from bit 0 to n-1,
710 * while those configured to stop tracing from 16 to 16 + n-1.
712 shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
713 config->vissctlr |= BIT(shift + comparator);
716 static void etm4_set_default_filter(struct etmv4_config *config)
721 * Configure address range comparator '0' to encompass all
722 * possible addresses.
727 etm4_set_comparator_filter(config, start, stop,
728 ETM_DEFAULT_ADDR_COMP);
731 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
732 * in the started state
734 config->vinst_ctrl |= BIT(9);
736 /* No start-stop filtering for ViewInst */
737 config->vissctlr = 0x0;
740 static void etm4_set_default(struct etmv4_config *config)
742 if (WARN_ON_ONCE(!config))
746 * Make default initialisation trace everything
748 * Select the "always true" resource selector on the
749 * "Enablign Event" line and configure address range comparator
750 * '0' to trace all the possible address range. From there
751 * configure the "include/exclude" engine to include address
752 * range comparator '0'.
754 etm4_set_default_config(config);
755 etm4_set_default_filter(config);
758 static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
760 int nr_comparator, index = 0;
761 struct etmv4_config *config = &drvdata->config;
764 * nr_addr_cmp holds the number of comparator _pair_, so time 2
765 * for the total number of comparators.
767 nr_comparator = drvdata->nr_addr_cmp * 2;
769 /* Go through the tally of comparators looking for a free one. */
770 while (index < nr_comparator) {
772 case ETM_ADDR_TYPE_RANGE:
773 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
774 config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
777 /* Address range comparators go in pairs */
780 case ETM_ADDR_TYPE_START:
781 case ETM_ADDR_TYPE_STOP:
782 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
785 /* Start/stop address can have odd indexes */
793 /* If we are here all the comparators have been used. */
797 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
798 struct perf_event *event)
800 int i, comparator, ret = 0;
802 struct etmv4_config *config = &drvdata->config;
803 struct etm_filters *filters = event->hw.addr_filters;
808 /* Sync events with what Perf got */
809 perf_event_addr_filters_sync(event);
812 * If there are no filters to deal with simply go ahead with
813 * the default filter, i.e the entire address range.
815 if (!filters->nr_filters)
818 for (i = 0; i < filters->nr_filters; i++) {
819 struct etm_filter *filter = &filters->etm_filter[i];
820 enum etm_addr_type type = filter->type;
822 /* See if a comparator is free. */
823 comparator = etm4_get_next_comparator(drvdata, type);
824 if (comparator < 0) {
830 case ETM_ADDR_TYPE_RANGE:
831 etm4_set_comparator_filter(config,
836 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
837 * in the started state
839 config->vinst_ctrl |= BIT(9);
841 /* No start-stop filtering for ViewInst */
842 config->vissctlr = 0x0;
844 case ETM_ADDR_TYPE_START:
845 case ETM_ADDR_TYPE_STOP:
846 /* Get the right start or stop address */
847 address = (type == ETM_ADDR_TYPE_START ?
851 /* Configure comparator */
852 etm4_set_start_stop_filter(config, address,
856 * If filters::ssstatus == 1, trace acquisition was
857 * started but the process was yanked away before the
858 * the stop address was hit. As such the start/stop
859 * logic needs to be re-started so that tracing can
860 * resume where it left.
862 * The start/stop logic status when a process is
863 * scheduled out is checked in function
864 * etm4_disable_perf().
866 if (filters->ssstatus)
867 config->vinst_ctrl |= BIT(9);
869 /* No include/exclude filtering for ViewInst */
870 config->viiectlr = 0x0;
882 etm4_set_default_filter(config);
888 void etm4_config_trace_mode(struct etmv4_config *config)
893 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
895 /* excluding kernel AND user space doesn't make sense */
896 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
898 /* nothing to do if neither flags are set */
899 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
902 addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
903 /* clear default config */
904 addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS |
907 addr_acc |= etm4_get_ns_access_type(config);
909 config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
910 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
913 static int etm4_online_cpu(unsigned int cpu)
915 if (!etmdrvdata[cpu])
918 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
919 coresight_enable(etmdrvdata[cpu]->csdev);
923 static int etm4_starting_cpu(unsigned int cpu)
925 if (!etmdrvdata[cpu])
928 spin_lock(&etmdrvdata[cpu]->spinlock);
929 if (!etmdrvdata[cpu]->os_unlock) {
930 etm4_os_unlock(etmdrvdata[cpu]);
931 etmdrvdata[cpu]->os_unlock = true;
934 if (local_read(&etmdrvdata[cpu]->mode))
935 etm4_enable_hw(etmdrvdata[cpu]);
936 spin_unlock(&etmdrvdata[cpu]->spinlock);
940 static int etm4_dying_cpu(unsigned int cpu)
942 if (!etmdrvdata[cpu])
945 spin_lock(&etmdrvdata[cpu]->spinlock);
946 if (local_read(&etmdrvdata[cpu]->mode))
947 etm4_disable_hw(etmdrvdata[cpu]);
948 spin_unlock(&etmdrvdata[cpu]->spinlock);
952 static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
954 drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
957 static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
961 struct device *dev = &adev->dev;
962 struct coresight_platform_data *pdata = NULL;
963 struct etmv4_drvdata *drvdata;
964 struct resource *res = &adev->res;
965 struct coresight_desc desc = { 0 };
966 struct device_node *np = adev->dev.of_node;
968 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
973 pdata = of_get_coresight_platform_data(dev, np);
975 return PTR_ERR(pdata);
976 adev->dev.platform_data = pdata;
979 drvdata->dev = &adev->dev;
980 dev_set_drvdata(dev, drvdata);
982 /* Validity for the resource is already checked by the AMBA core */
983 base = devm_ioremap_resource(dev, res);
985 return PTR_ERR(base);
987 drvdata->base = base;
989 spin_lock_init(&drvdata->spinlock);
991 drvdata->cpu = pdata ? pdata->cpu : 0;
994 etmdrvdata[drvdata->cpu] = drvdata;
996 if (smp_call_function_single(drvdata->cpu,
997 etm4_init_arch_data, drvdata, 1))
998 dev_err(dev, "ETM arch init failed\n");
1000 if (!etm4_count++) {
1001 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ARM_CORESIGHT_STARTING,
1002 "arm/coresight4:starting",
1003 etm4_starting_cpu, etm4_dying_cpu);
1004 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
1005 "arm/coresight4:online",
1006 etm4_online_cpu, NULL);
1008 goto err_arch_supported;
1014 if (etm4_arch_supported(drvdata->arch) == false) {
1016 goto err_arch_supported;
1019 etm4_init_trace_id(drvdata);
1020 etm4_set_default(&drvdata->config);
1022 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
1023 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
1024 desc.ops = &etm4_cs_ops;
1027 desc.groups = coresight_etmv4_groups;
1028 drvdata->csdev = coresight_register(&desc);
1029 if (IS_ERR(drvdata->csdev)) {
1030 ret = PTR_ERR(drvdata->csdev);
1031 goto err_arch_supported;
1034 ret = etm_perf_symlink(drvdata->csdev, true);
1036 coresight_unregister(drvdata->csdev);
1037 goto err_arch_supported;
1040 pm_runtime_put(&adev->dev);
1041 dev_info(dev, "CPU%d: ETM v%d.%d initialized\n",
1042 drvdata->cpu, drvdata->arch >> 4, drvdata->arch & 0xf);
1045 coresight_enable(drvdata->csdev);
1046 drvdata->boot_enable = true;
1052 if (--etm4_count == 0) {
1053 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
1055 cpuhp_remove_state_nocalls(hp_online);
1060 #define ETM4x_AMBA_ID(pid) \
1063 .mask = 0x000fffff, \
1066 static const struct amba_id etm4_ids[] = {
1067 ETM4x_AMBA_ID(0x000bb95d), /* Cortex-A53 */
1068 ETM4x_AMBA_ID(0x000bb95e), /* Cortex-A57 */
1069 ETM4x_AMBA_ID(0x000bb95a), /* Cortex-A72 */
1070 ETM4x_AMBA_ID(0x000bb959), /* Cortex-A73 */
1071 ETM4x_AMBA_ID(0x000bb9da), /* Cortex-A35 */
1075 static struct amba_driver etm4x_driver = {
1077 .name = "coresight-etm4x",
1078 .suppress_bind_attrs = true,
1080 .probe = etm4_probe,
1081 .id_table = etm4_ids,
1083 builtin_amba_driver(etm4x_driver);