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 struct etm4_enable_arg {
83 struct etmv4_drvdata *drvdata;
87 static int etm4_enable_hw(struct etmv4_drvdata *drvdata)
90 struct etmv4_config *config = &drvdata->config;
91 struct device *etm_dev = &drvdata->csdev->dev;
93 CS_UNLOCK(drvdata->base);
95 etm4_os_unlock(drvdata);
97 rc = coresight_claim_device_unlocked(drvdata->base);
101 /* Disable the trace unit before programming trace registers */
102 writel_relaxed(0, drvdata->base + TRCPRGCTLR);
104 /* wait for TRCSTATR.IDLE to go up */
105 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
107 "timeout while waiting for Idle Trace Status\n");
109 writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
110 writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
111 /* nothing specific implemented */
112 writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
113 writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
114 writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
115 writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
116 writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
117 writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
118 writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
119 writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
120 writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
121 writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
122 writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
123 writel_relaxed(config->vissctlr,
124 drvdata->base + TRCVISSCTLR);
125 writel_relaxed(config->vipcssctlr,
126 drvdata->base + TRCVIPCSSCTLR);
127 for (i = 0; i < drvdata->nrseqstate - 1; i++)
128 writel_relaxed(config->seq_ctrl[i],
129 drvdata->base + TRCSEQEVRn(i));
130 writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
131 writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
132 writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
133 for (i = 0; i < drvdata->nr_cntr; i++) {
134 writel_relaxed(config->cntrldvr[i],
135 drvdata->base + TRCCNTRLDVRn(i));
136 writel_relaxed(config->cntr_ctrl[i],
137 drvdata->base + TRCCNTCTLRn(i));
138 writel_relaxed(config->cntr_val[i],
139 drvdata->base + TRCCNTVRn(i));
143 * Resource selector pair 0 is always implemented and reserved. As
146 for (i = 2; i < drvdata->nr_resource * 2; i++)
147 writel_relaxed(config->res_ctrl[i],
148 drvdata->base + TRCRSCTLRn(i));
150 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
151 writel_relaxed(config->ss_ctrl[i],
152 drvdata->base + TRCSSCCRn(i));
153 writel_relaxed(config->ss_status[i],
154 drvdata->base + TRCSSCSRn(i));
155 writel_relaxed(config->ss_pe_cmp[i],
156 drvdata->base + TRCSSPCICRn(i));
158 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
159 writeq_relaxed(config->addr_val[i],
160 drvdata->base + TRCACVRn(i));
161 writeq_relaxed(config->addr_acc[i],
162 drvdata->base + TRCACATRn(i));
164 for (i = 0; i < drvdata->numcidc; i++)
165 writeq_relaxed(config->ctxid_pid[i],
166 drvdata->base + TRCCIDCVRn(i));
167 writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
168 writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
170 for (i = 0; i < drvdata->numvmidc; i++)
171 writeq_relaxed(config->vmid_val[i],
172 drvdata->base + TRCVMIDCVRn(i));
173 writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
174 writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
177 * Request to keep the trace unit powered and also
178 * emulation of powerdown
180 writel_relaxed(readl_relaxed(drvdata->base + TRCPDCR) | TRCPDCR_PU,
181 drvdata->base + TRCPDCR);
183 /* Enable the trace unit */
184 writel_relaxed(1, drvdata->base + TRCPRGCTLR);
186 /* wait for TRCSTATR.IDLE to go back down to '0' */
187 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
189 "timeout while waiting for Idle Trace Status\n");
192 CS_LOCK(drvdata->base);
194 dev_dbg(etm_dev, "cpu: %d enable smp call done: %d\n",
199 static void etm4_enable_hw_smp_call(void *info)
201 struct etm4_enable_arg *arg = info;
205 arg->rc = etm4_enable_hw(arg->drvdata);
209 * The goal of function etm4_config_timestamp_event() is to configure a
210 * counter that will tell the tracer to emit a timestamp packet when it
211 * reaches zero. This is done in order to get a more fine grained idea
212 * of when instructions are executed so that they can be correlated
213 * with execution on other CPUs.
215 * To do this the counter itself is configured to self reload and
216 * TRCRSCTLR1 (always true) used to get the counter to decrement. From
217 * there a resource selector is configured with the counter and the
218 * timestamp control register to use the resource selector to trigger the
219 * event that will insert a timestamp packet in the stream.
221 static int etm4_config_timestamp_event(struct etmv4_drvdata *drvdata)
223 int ctridx, ret = -EINVAL;
224 int counter, rselector;
226 struct etmv4_config *config = &drvdata->config;
228 /* No point in trying if we don't have at least one counter */
229 if (!drvdata->nr_cntr)
232 /* Find a counter that hasn't been initialised */
233 for (ctridx = 0; ctridx < drvdata->nr_cntr; ctridx++)
234 if (config->cntr_val[ctridx] == 0)
237 /* All the counters have been configured already, bail out */
238 if (ctridx == drvdata->nr_cntr) {
239 pr_debug("%s: no available counter found\n", __func__);
245 * Searching for an available resource selector to use, starting at
246 * '2' since every implementation has at least 2 resource selector.
247 * ETMIDR4 gives the number of resource selector _pairs_,
248 * hence multiply by 2.
250 for (rselector = 2; rselector < drvdata->nr_resource * 2; rselector++)
251 if (!config->res_ctrl[rselector])
254 if (rselector == drvdata->nr_resource * 2) {
255 pr_debug("%s: no available resource selector found\n",
261 /* Remember what counter we used */
262 counter = 1 << ctridx;
265 * Initialise original and reload counter value to the smallest
266 * possible value in order to get as much precision as we can.
268 config->cntr_val[ctridx] = 1;
269 config->cntrldvr[ctridx] = 1;
271 /* Set the trace counter control register */
272 val = 0x1 << 16 | /* Bit 16, reload counter automatically */
273 0x0 << 7 | /* Select single resource selector */
274 0x1; /* Resource selector 1, i.e always true */
276 config->cntr_ctrl[ctridx] = val;
278 val = 0x2 << 16 | /* Group 0b0010 - Counter and sequencers */
279 counter << 0; /* Counter to use */
281 config->res_ctrl[rselector] = val;
283 val = 0x0 << 7 | /* Select single resource selector */
284 rselector; /* Resource selector */
286 config->ts_ctrl = val;
293 static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
294 struct perf_event *event)
297 struct etmv4_config *config = &drvdata->config;
298 struct perf_event_attr *attr = &event->attr;
305 /* Clear configuration from previous run */
306 memset(config, 0, sizeof(struct etmv4_config));
308 if (attr->exclude_kernel)
309 config->mode = ETM_MODE_EXCL_KERN;
311 if (attr->exclude_user)
312 config->mode = ETM_MODE_EXCL_USER;
314 /* Always start from the default config */
315 etm4_set_default_config(config);
317 /* Configure filters specified on the perf cmd line, if any. */
318 ret = etm4_set_event_filters(drvdata, event);
322 /* Go from generic option to ETMv4 specifics */
323 if (attr->config & BIT(ETM_OPT_CYCACC)) {
324 config->cfg |= BIT(4);
325 /* TRM: Must program this for cycacc to work */
326 config->ccctlr = ETM_CYC_THRESHOLD_DEFAULT;
328 if (attr->config & BIT(ETM_OPT_TS)) {
330 * Configure timestamps to be emitted at regular intervals in
331 * order to correlate instructions executed on different CPUs
332 * (CPU-wide trace scenarios).
334 ret = etm4_config_timestamp_event(drvdata);
337 * No need to go further if timestamp intervals can't
343 /* bit[11], Global timestamp tracing bit */
344 config->cfg |= BIT(11);
347 if (attr->config & BIT(ETM_OPT_CTXTID))
348 /* bit[6], Context ID tracing bit */
349 config->cfg |= BIT(ETM4_CFG_BIT_CTXTID);
351 /* return stack - enable if selected and supported */
352 if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack)
353 /* bit[12], Return stack enable bit */
354 config->cfg |= BIT(12);
360 static int etm4_enable_perf(struct coresight_device *csdev,
361 struct perf_event *event)
364 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
366 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) {
371 /* Configure the tracer based on the session's specifics */
372 ret = etm4_parse_event_config(drvdata, event);
376 ret = etm4_enable_hw(drvdata);
382 static int etm4_enable_sysfs(struct coresight_device *csdev)
384 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
385 struct etm4_enable_arg arg = { 0 };
388 spin_lock(&drvdata->spinlock);
391 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
392 * ensures that register writes occur when cpu is powered.
394 arg.drvdata = drvdata;
395 ret = smp_call_function_single(drvdata->cpu,
396 etm4_enable_hw_smp_call, &arg, 1);
400 drvdata->sticky_enable = true;
401 spin_unlock(&drvdata->spinlock);
404 dev_dbg(&csdev->dev, "ETM tracing enabled\n");
408 static int etm4_enable(struct coresight_device *csdev,
409 struct perf_event *event, u32 mode)
413 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
415 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
417 /* Someone is already using the tracer */
423 ret = etm4_enable_sysfs(csdev);
426 ret = etm4_enable_perf(csdev, event);
432 /* The tracer didn't start */
434 local_set(&drvdata->mode, CS_MODE_DISABLED);
439 static void etm4_disable_hw(void *info)
442 struct etmv4_drvdata *drvdata = info;
444 CS_UNLOCK(drvdata->base);
446 /* power can be removed from the trace unit now */
447 control = readl_relaxed(drvdata->base + TRCPDCR);
448 control &= ~TRCPDCR_PU;
449 writel_relaxed(control, drvdata->base + TRCPDCR);
451 control = readl_relaxed(drvdata->base + TRCPRGCTLR);
453 /* EN, bit[0] Trace unit enable bit */
456 /* make sure everything completes before disabling */
459 writel_relaxed(control, drvdata->base + TRCPRGCTLR);
461 coresight_disclaim_device_unlocked(drvdata->base);
463 CS_LOCK(drvdata->base);
465 dev_dbg(&drvdata->csdev->dev,
466 "cpu: %d disable smp call done\n", drvdata->cpu);
469 static int etm4_disable_perf(struct coresight_device *csdev,
470 struct perf_event *event)
473 struct etm_filters *filters = event->hw.addr_filters;
474 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
476 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
479 etm4_disable_hw(drvdata);
482 * Check if the start/stop logic was active when the unit was stopped.
483 * That way we can re-enable the start/stop logic when the process is
484 * scheduled again. Configuration of the start/stop logic happens in
485 * function etm4_set_event_filters().
487 control = readl_relaxed(drvdata->base + TRCVICTLR);
488 /* TRCVICTLR::SSSTATUS, bit[9] */
489 filters->ssstatus = (control & BIT(9));
494 static void etm4_disable_sysfs(struct coresight_device *csdev)
496 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
499 * Taking hotplug lock here protects from clocks getting disabled
500 * with tracing being left on (crash scenario) if user disable occurs
501 * after cpu online mask indicates the cpu is offline but before the
502 * DYING hotplug callback is serviced by the ETM driver.
505 spin_lock(&drvdata->spinlock);
508 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
509 * ensures that register writes occur when cpu is powered.
511 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
513 spin_unlock(&drvdata->spinlock);
516 dev_dbg(&csdev->dev, "ETM tracing disabled\n");
519 static void etm4_disable(struct coresight_device *csdev,
520 struct perf_event *event)
523 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
526 * For as long as the tracer isn't disabled another entity can't
527 * change its status. As such we can read the status here without
528 * fearing it will change under us.
530 mode = local_read(&drvdata->mode);
533 case CS_MODE_DISABLED:
536 etm4_disable_sysfs(csdev);
539 etm4_disable_perf(csdev, event);
544 local_set(&drvdata->mode, CS_MODE_DISABLED);
547 static const struct coresight_ops_source etm4_source_ops = {
548 .cpu_id = etm4_cpu_id,
549 .trace_id = etm4_trace_id,
550 .enable = etm4_enable,
551 .disable = etm4_disable,
554 static const struct coresight_ops etm4_cs_ops = {
555 .source_ops = &etm4_source_ops,
558 static void etm4_init_arch_data(void *info)
566 struct etmv4_drvdata *drvdata = info;
568 /* Make sure all registers are accessible */
569 etm4_os_unlock(drvdata);
571 CS_UNLOCK(drvdata->base);
573 /* find all capabilities of the tracing unit */
574 etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
576 /* INSTP0, bits[2:1] P0 tracing support field */
577 if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
578 drvdata->instrp0 = true;
580 drvdata->instrp0 = false;
582 /* TRCBB, bit[5] Branch broadcast tracing support bit */
583 if (BMVAL(etmidr0, 5, 5))
584 drvdata->trcbb = true;
586 drvdata->trcbb = false;
588 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
589 if (BMVAL(etmidr0, 6, 6))
590 drvdata->trccond = true;
592 drvdata->trccond = false;
594 /* TRCCCI, bit[7] Cycle counting instruction bit */
595 if (BMVAL(etmidr0, 7, 7))
596 drvdata->trccci = true;
598 drvdata->trccci = false;
600 /* RETSTACK, bit[9] Return stack bit */
601 if (BMVAL(etmidr0, 9, 9))
602 drvdata->retstack = true;
604 drvdata->retstack = false;
606 /* NUMEVENT, bits[11:10] Number of events field */
607 drvdata->nr_event = BMVAL(etmidr0, 10, 11);
608 /* QSUPP, bits[16:15] Q element support field */
609 drvdata->q_support = BMVAL(etmidr0, 15, 16);
610 /* TSSIZE, bits[28:24] Global timestamp size field */
611 drvdata->ts_size = BMVAL(etmidr0, 24, 28);
613 /* base architecture of trace unit */
614 etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
616 * TRCARCHMIN, bits[7:4] architecture the minor version number
617 * TRCARCHMAJ, bits[11:8] architecture major versin number
619 drvdata->arch = BMVAL(etmidr1, 4, 11);
621 /* maximum size of resources */
622 etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
623 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
624 drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
625 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
626 drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
627 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
628 drvdata->ccsize = BMVAL(etmidr2, 25, 28);
630 etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
631 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
632 drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
633 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
634 drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
635 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
636 drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
639 * TRCERR, bit[24] whether a trace unit can trace a
640 * system error exception.
642 if (BMVAL(etmidr3, 24, 24))
643 drvdata->trc_error = true;
645 drvdata->trc_error = false;
647 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
648 if (BMVAL(etmidr3, 25, 25))
649 drvdata->syncpr = true;
651 drvdata->syncpr = false;
653 /* STALLCTL, bit[26] is stall control implemented? */
654 if (BMVAL(etmidr3, 26, 26))
655 drvdata->stallctl = true;
657 drvdata->stallctl = false;
659 /* SYSSTALL, bit[27] implementation can support stall control? */
660 if (BMVAL(etmidr3, 27, 27))
661 drvdata->sysstall = true;
663 drvdata->sysstall = false;
665 /* NUMPROC, bits[30:28] the number of PEs available for tracing */
666 drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
668 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
669 if (BMVAL(etmidr3, 31, 31))
670 drvdata->nooverflow = true;
672 drvdata->nooverflow = false;
674 /* number of resources trace unit supports */
675 etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
676 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
677 drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
678 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
679 drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
681 * NUMRSPAIR, bits[19:16]
682 * The number of resource pairs conveyed by the HW starts at 0, i.e a
683 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
684 * As such add 1 to the value of NUMRSPAIR for a better representation.
686 drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
688 * NUMSSCC, bits[23:20] the number of single-shot
689 * comparator control for tracing
691 drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
692 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
693 drvdata->numcidc = BMVAL(etmidr4, 24, 27);
694 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
695 drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
697 etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
698 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
699 drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
700 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
701 drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
702 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
703 if (BMVAL(etmidr5, 22, 22))
704 drvdata->atbtrig = true;
706 drvdata->atbtrig = false;
708 * LPOVERRIDE, bit[23] implementation supports
709 * low-power state override
711 if (BMVAL(etmidr5, 23, 23))
712 drvdata->lpoverride = true;
714 drvdata->lpoverride = false;
715 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
716 drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
717 /* NUMCNTR, bits[30:28] number of counters available for tracing */
718 drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
719 CS_LOCK(drvdata->base);
722 static void etm4_set_default_config(struct etmv4_config *config)
724 /* disable all events tracing */
725 config->eventctrl0 = 0x0;
726 config->eventctrl1 = 0x0;
728 /* disable stalling */
729 config->stall_ctrl = 0x0;
731 /* enable trace synchronization every 4096 bytes, if available */
732 config->syncfreq = 0xC;
734 /* disable timestamp event */
735 config->ts_ctrl = 0x0;
737 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
738 config->vinst_ctrl |= BIT(0);
741 static u64 etm4_get_ns_access_type(struct etmv4_config *config)
746 * EXLEVEL_NS, bits[15:12]
747 * The Exception levels are:
748 * Bit[12] Exception level 0 - Application
749 * Bit[13] Exception level 1 - OS
750 * Bit[14] Exception level 2 - Hypervisor
751 * Bit[15] Never implemented
753 if (!is_kernel_in_hyp_mode()) {
754 /* Stay away from hypervisor mode for non-VHE */
755 access_type = ETM_EXLEVEL_NS_HYP;
756 if (config->mode & ETM_MODE_EXCL_KERN)
757 access_type |= ETM_EXLEVEL_NS_OS;
758 } else if (config->mode & ETM_MODE_EXCL_KERN) {
759 access_type = ETM_EXLEVEL_NS_HYP;
762 if (config->mode & ETM_MODE_EXCL_USER)
763 access_type |= ETM_EXLEVEL_NS_APP;
768 static u64 etm4_get_access_type(struct etmv4_config *config)
770 u64 access_type = etm4_get_ns_access_type(config);
773 * EXLEVEL_S, bits[11:8], don't trace anything happening
776 access_type |= (ETM_EXLEVEL_S_APP |
783 static void etm4_set_comparator_filter(struct etmv4_config *config,
784 u64 start, u64 stop, int comparator)
786 u64 access_type = etm4_get_access_type(config);
788 /* First half of default address comparator */
789 config->addr_val[comparator] = start;
790 config->addr_acc[comparator] = access_type;
791 config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
793 /* Second half of default address comparator */
794 config->addr_val[comparator + 1] = stop;
795 config->addr_acc[comparator + 1] = access_type;
796 config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
799 * Configure the ViewInst function to include this address range
802 * @comparator is divided by two since it is the index in the
803 * etmv4_config::addr_val array but register TRCVIIECTLR deals with
804 * address range comparator _pairs_.
807 * index 0 -> compatator pair 0
808 * index 2 -> comparator pair 1
809 * index 4 -> comparator pair 2
811 * index 14 -> comparator pair 7
813 config->viiectlr |= BIT(comparator / 2);
816 static void etm4_set_start_stop_filter(struct etmv4_config *config,
817 u64 address, int comparator,
818 enum etm_addr_type type)
821 u64 access_type = etm4_get_access_type(config);
823 /* Configure the comparator */
824 config->addr_val[comparator] = address;
825 config->addr_acc[comparator] = access_type;
826 config->addr_type[comparator] = type;
829 * Configure ViewInst Start-Stop control register.
830 * Addresses configured to start tracing go from bit 0 to n-1,
831 * while those configured to stop tracing from 16 to 16 + n-1.
833 shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
834 config->vissctlr |= BIT(shift + comparator);
837 static void etm4_set_default_filter(struct etmv4_config *config)
842 * Configure address range comparator '0' to encompass all
843 * possible addresses.
848 etm4_set_comparator_filter(config, start, stop,
849 ETM_DEFAULT_ADDR_COMP);
852 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
853 * in the started state
855 config->vinst_ctrl |= BIT(9);
857 /* No start-stop filtering for ViewInst */
858 config->vissctlr = 0x0;
861 static void etm4_set_default(struct etmv4_config *config)
863 if (WARN_ON_ONCE(!config))
867 * Make default initialisation trace everything
869 * Select the "always true" resource selector on the
870 * "Enablign Event" line and configure address range comparator
871 * '0' to trace all the possible address range. From there
872 * configure the "include/exclude" engine to include address
873 * range comparator '0'.
875 etm4_set_default_config(config);
876 etm4_set_default_filter(config);
879 static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
881 int nr_comparator, index = 0;
882 struct etmv4_config *config = &drvdata->config;
885 * nr_addr_cmp holds the number of comparator _pair_, so time 2
886 * for the total number of comparators.
888 nr_comparator = drvdata->nr_addr_cmp * 2;
890 /* Go through the tally of comparators looking for a free one. */
891 while (index < nr_comparator) {
893 case ETM_ADDR_TYPE_RANGE:
894 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
895 config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
898 /* Address range comparators go in pairs */
901 case ETM_ADDR_TYPE_START:
902 case ETM_ADDR_TYPE_STOP:
903 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
906 /* Start/stop address can have odd indexes */
914 /* If we are here all the comparators have been used. */
918 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
919 struct perf_event *event)
921 int i, comparator, ret = 0;
923 struct etmv4_config *config = &drvdata->config;
924 struct etm_filters *filters = event->hw.addr_filters;
929 /* Sync events with what Perf got */
930 perf_event_addr_filters_sync(event);
933 * If there are no filters to deal with simply go ahead with
934 * the default filter, i.e the entire address range.
936 if (!filters->nr_filters)
939 for (i = 0; i < filters->nr_filters; i++) {
940 struct etm_filter *filter = &filters->etm_filter[i];
941 enum etm_addr_type type = filter->type;
943 /* See if a comparator is free. */
944 comparator = etm4_get_next_comparator(drvdata, type);
945 if (comparator < 0) {
951 case ETM_ADDR_TYPE_RANGE:
952 etm4_set_comparator_filter(config,
957 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
958 * in the started state
960 config->vinst_ctrl |= BIT(9);
962 /* No start-stop filtering for ViewInst */
963 config->vissctlr = 0x0;
965 case ETM_ADDR_TYPE_START:
966 case ETM_ADDR_TYPE_STOP:
967 /* Get the right start or stop address */
968 address = (type == ETM_ADDR_TYPE_START ?
972 /* Configure comparator */
973 etm4_set_start_stop_filter(config, address,
977 * If filters::ssstatus == 1, trace acquisition was
978 * started but the process was yanked away before the
979 * the stop address was hit. As such the start/stop
980 * logic needs to be re-started so that tracing can
981 * resume where it left.
983 * The start/stop logic status when a process is
984 * scheduled out is checked in function
985 * etm4_disable_perf().
987 if (filters->ssstatus)
988 config->vinst_ctrl |= BIT(9);
990 /* No include/exclude filtering for ViewInst */
991 config->viiectlr = 0x0;
1003 etm4_set_default_filter(config);
1009 void etm4_config_trace_mode(struct etmv4_config *config)
1013 mode = config->mode;
1014 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
1016 /* excluding kernel AND user space doesn't make sense */
1017 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
1019 /* nothing to do if neither flags are set */
1020 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
1023 addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
1024 /* clear default config */
1025 addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS |
1026 ETM_EXLEVEL_NS_HYP);
1028 addr_acc |= etm4_get_ns_access_type(config);
1030 config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
1031 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
1034 static int etm4_online_cpu(unsigned int cpu)
1036 if (!etmdrvdata[cpu])
1039 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
1040 coresight_enable(etmdrvdata[cpu]->csdev);
1044 static int etm4_starting_cpu(unsigned int cpu)
1046 if (!etmdrvdata[cpu])
1049 spin_lock(&etmdrvdata[cpu]->spinlock);
1050 if (!etmdrvdata[cpu]->os_unlock) {
1051 etm4_os_unlock(etmdrvdata[cpu]);
1052 etmdrvdata[cpu]->os_unlock = true;
1055 if (local_read(&etmdrvdata[cpu]->mode))
1056 etm4_enable_hw(etmdrvdata[cpu]);
1057 spin_unlock(&etmdrvdata[cpu]->spinlock);
1061 static int etm4_dying_cpu(unsigned int cpu)
1063 if (!etmdrvdata[cpu])
1066 spin_lock(&etmdrvdata[cpu]->spinlock);
1067 if (local_read(&etmdrvdata[cpu]->mode))
1068 etm4_disable_hw(etmdrvdata[cpu]);
1069 spin_unlock(&etmdrvdata[cpu]->spinlock);
1073 static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
1075 drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
1078 static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
1082 struct device *dev = &adev->dev;
1083 struct coresight_platform_data *pdata = NULL;
1084 struct etmv4_drvdata *drvdata;
1085 struct resource *res = &adev->res;
1086 struct coresight_desc desc = { 0 };
1087 struct device_node *np = adev->dev.of_node;
1089 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1094 pdata = of_get_coresight_platform_data(dev, np);
1096 return PTR_ERR(pdata);
1097 adev->dev.platform_data = pdata;
1100 dev_set_drvdata(dev, drvdata);
1102 /* Validity for the resource is already checked by the AMBA core */
1103 base = devm_ioremap_resource(dev, res);
1105 return PTR_ERR(base);
1107 drvdata->base = base;
1109 spin_lock_init(&drvdata->spinlock);
1111 drvdata->cpu = pdata ? pdata->cpu : 0;
1114 etmdrvdata[drvdata->cpu] = drvdata;
1116 if (smp_call_function_single(drvdata->cpu,
1117 etm4_init_arch_data, drvdata, 1))
1118 dev_err(dev, "ETM arch init failed\n");
1120 if (!etm4_count++) {
1121 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ARM_CORESIGHT_STARTING,
1122 "arm/coresight4:starting",
1123 etm4_starting_cpu, etm4_dying_cpu);
1124 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
1125 "arm/coresight4:online",
1126 etm4_online_cpu, NULL);
1128 goto err_arch_supported;
1134 if (etm4_arch_supported(drvdata->arch) == false) {
1136 goto err_arch_supported;
1139 etm4_init_trace_id(drvdata);
1140 etm4_set_default(&drvdata->config);
1142 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
1143 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
1144 desc.ops = &etm4_cs_ops;
1147 desc.groups = coresight_etmv4_groups;
1148 drvdata->csdev = coresight_register(&desc);
1149 if (IS_ERR(drvdata->csdev)) {
1150 ret = PTR_ERR(drvdata->csdev);
1151 goto err_arch_supported;
1154 ret = etm_perf_symlink(drvdata->csdev, true);
1156 coresight_unregister(drvdata->csdev);
1157 goto err_arch_supported;
1160 pm_runtime_put(&adev->dev);
1161 dev_info(&drvdata->csdev->dev, "CPU%d: ETM v%d.%d initialized\n",
1162 drvdata->cpu, drvdata->arch >> 4, drvdata->arch & 0xf);
1165 coresight_enable(drvdata->csdev);
1166 drvdata->boot_enable = true;
1172 if (--etm4_count == 0) {
1173 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
1175 cpuhp_remove_state_nocalls(hp_online);
1180 static struct amba_cs_uci_id uci_id_etm4[] = {
1182 /* ETMv4 UCI data */
1183 .devarch = 0x47704a13,
1184 .devarch_mask = 0xfff0ffff,
1185 .devtype = 0x00000013,
1189 static const struct amba_id etm4_ids[] = {
1190 CS_AMBA_ID(0x000bb95d), /* Cortex-A53 */
1191 CS_AMBA_ID(0x000bb95e), /* Cortex-A57 */
1192 CS_AMBA_ID(0x000bb95a), /* Cortex-A72 */
1193 CS_AMBA_ID(0x000bb959), /* Cortex-A73 */
1194 CS_AMBA_UCI_ID(0x000bb9da, uci_id_etm4), /* Cortex-A35 */
1198 static struct amba_driver etm4x_driver = {
1200 .name = "coresight-etm4x",
1201 .suppress_bind_attrs = true,
1203 .probe = etm4_probe,
1204 .id_table = etm4_ids,
1206 builtin_amba_driver(etm4x_driver);