1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
6 #include <linux/bitops.h>
7 #include <linux/kernel.h>
8 #include <linux/moduleparam.h>
9 #include <linux/init.h>
10 #include <linux/types.h>
11 #include <linux/device.h>
13 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/smp.h>
18 #include <linux/sysfs.h>
19 #include <linux/stat.h>
20 #include <linux/clk.h>
21 #include <linux/cpu.h>
22 #include <linux/cpu_pm.h>
23 #include <linux/coresight.h>
24 #include <linux/coresight-pmu.h>
25 #include <linux/pm_wakeup.h>
26 #include <linux/amba/bus.h>
27 #include <linux/seq_file.h>
28 #include <linux/uaccess.h>
29 #include <linux/perf_event.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/property.h>
33 #include <asm/sections.h>
34 #include <asm/sysreg.h>
35 #include <asm/local.h>
38 #include "coresight-etm4x.h"
39 #include "coresight-etm-perf.h"
41 static int boot_enable;
42 module_param(boot_enable, int, 0444);
43 MODULE_PARM_DESC(boot_enable, "Enable tracing on boot");
45 #define PARAM_PM_SAVE_FIRMWARE 0 /* save self-hosted state as per firmware */
46 #define PARAM_PM_SAVE_NEVER 1 /* never save any state */
47 #define PARAM_PM_SAVE_SELF_HOSTED 2 /* save self-hosted state only */
49 static int pm_save_enable = PARAM_PM_SAVE_FIRMWARE;
50 module_param(pm_save_enable, int, 0444);
51 MODULE_PARM_DESC(pm_save_enable,
52 "Save/restore state on power down: 1 = never, 2 = self-hosted");
54 static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
55 static void etm4_set_default_config(struct etmv4_config *config);
56 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
57 struct perf_event *event);
58 static u64 etm4_get_access_type(struct etmv4_config *config);
60 static enum cpuhp_state hp_online;
62 struct etm4_init_arg {
64 struct etmv4_drvdata *drvdata;
65 struct csdev_access *csa;
69 * Check if TRCSSPCICRn(i) is implemented for a given instance.
71 * TRCSSPCICRn is implemented only if :
72 * TRCSSPCICR<n> is present only if all of the following are true:
73 * TRCIDR4.NUMSSCC > n.
74 * TRCIDR4.NUMPC > 0b0000 .
75 * TRCSSCSR<n>.PC == 0b1
77 static inline bool etm4x_sspcicrn_present(struct etmv4_drvdata *drvdata, int n)
79 return (n < drvdata->nr_ss_cmp) &&
81 (drvdata->config.ss_status[n] & TRCSSCSRn_PC);
84 u64 etm4x_sysreg_read(u32 offset, bool _relaxed, bool _64bit)
89 ETM4x_READ_SYSREG_CASES(res)
91 pr_warn_ratelimited("etm4x: trying to read unsupported register @%x\n",
96 __iormb(res); /* Imitate the !relaxed I/O helpers */
101 void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)
104 __iowmb(); /* Imitate the !relaxed I/O helpers */
106 val &= GENMASK(31, 0);
109 ETM4x_WRITE_SYSREG_CASES(val)
111 pr_warn_ratelimited("etm4x: trying to write to unsupported register @%x\n",
116 static void etm4_os_unlock_csa(struct etmv4_drvdata *drvdata, struct csdev_access *csa)
118 /* Writing 0 to TRCOSLAR unlocks the trace registers */
119 etm4x_relaxed_write32(csa, 0x0, TRCOSLAR);
120 drvdata->os_unlock = true;
124 static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
126 if (!WARN_ON(!drvdata->csdev))
127 etm4_os_unlock_csa(drvdata, &drvdata->csdev->access);
131 static void etm4_os_lock(struct etmv4_drvdata *drvdata)
133 if (WARN_ON(!drvdata->csdev))
136 /* Writing 0x1 to TRCOSLAR locks the trace registers */
137 etm4x_relaxed_write32(&drvdata->csdev->access, 0x1, TRCOSLAR);
138 drvdata->os_unlock = false;
142 static void etm4_cs_lock(struct etmv4_drvdata *drvdata,
143 struct csdev_access *csa)
145 /* Software Lock is only accessible via memory mapped interface */
150 static void etm4_cs_unlock(struct etmv4_drvdata *drvdata,
151 struct csdev_access *csa)
154 CS_UNLOCK(csa->base);
157 static int etm4_cpu_id(struct coresight_device *csdev)
159 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
164 static int etm4_trace_id(struct coresight_device *csdev)
166 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
168 return drvdata->trcid;
171 struct etm4_enable_arg {
172 struct etmv4_drvdata *drvdata;
176 #ifdef CONFIG_ETM4X_IMPDEF_FEATURE
178 #define HISI_HIP08_AMBA_ID 0x000b6d01
179 #define ETM4_AMBA_MASK 0xfffff
180 #define HISI_HIP08_CORE_COMMIT_MASK 0x3000
181 #define HISI_HIP08_CORE_COMMIT_SHIFT 12
182 #define HISI_HIP08_CORE_COMMIT_FULL 0b00
183 #define HISI_HIP08_CORE_COMMIT_LVL_1 0b01
184 #define HISI_HIP08_CORE_COMMIT_REG sys_reg(3, 1, 15, 2, 5)
186 struct etm4_arch_features {
187 void (*arch_callback)(bool enable);
190 static bool etm4_hisi_match_pid(unsigned int id)
192 return (id & ETM4_AMBA_MASK) == HISI_HIP08_AMBA_ID;
195 static void etm4_hisi_config_core_commit(bool enable)
197 u8 commit = enable ? HISI_HIP08_CORE_COMMIT_LVL_1 :
198 HISI_HIP08_CORE_COMMIT_FULL;
202 * bit 12 and 13 of HISI_HIP08_CORE_COMMIT_REG are used together
203 * to set core-commit, 2'b00 means cpu is at full speed, 2'b01,
204 * 2'b10, 2'b11 mean reduce pipeline speed, and 2'b01 means level-1
205 * speed(minimun value). So bit 12 and 13 should be cleared together.
207 val = read_sysreg_s(HISI_HIP08_CORE_COMMIT_REG);
208 val &= ~HISI_HIP08_CORE_COMMIT_MASK;
209 val |= commit << HISI_HIP08_CORE_COMMIT_SHIFT;
210 write_sysreg_s(val, HISI_HIP08_CORE_COMMIT_REG);
213 static struct etm4_arch_features etm4_features[] = {
214 [ETM4_IMPDEF_HISI_CORE_COMMIT] = {
215 .arch_callback = etm4_hisi_config_core_commit,
220 static void etm4_enable_arch_specific(struct etmv4_drvdata *drvdata)
222 struct etm4_arch_features *ftr;
225 for_each_set_bit(bit, drvdata->arch_features, ETM4_IMPDEF_FEATURE_MAX) {
226 ftr = &etm4_features[bit];
228 if (ftr->arch_callback)
229 ftr->arch_callback(true);
233 static void etm4_disable_arch_specific(struct etmv4_drvdata *drvdata)
235 struct etm4_arch_features *ftr;
238 for_each_set_bit(bit, drvdata->arch_features, ETM4_IMPDEF_FEATURE_MAX) {
239 ftr = &etm4_features[bit];
241 if (ftr->arch_callback)
242 ftr->arch_callback(false);
246 static void etm4_check_arch_features(struct etmv4_drvdata *drvdata,
249 if (etm4_hisi_match_pid(id))
250 set_bit(ETM4_IMPDEF_HISI_CORE_COMMIT, drvdata->arch_features);
253 static void etm4_enable_arch_specific(struct etmv4_drvdata *drvdata)
257 static void etm4_disable_arch_specific(struct etmv4_drvdata *drvdata)
261 static void etm4_check_arch_features(struct etmv4_drvdata *drvdata,
265 #endif /* CONFIG_ETM4X_IMPDEF_FEATURE */
267 static int etm4_enable_hw(struct etmv4_drvdata *drvdata)
270 struct etmv4_config *config = &drvdata->config;
271 struct coresight_device *csdev = drvdata->csdev;
272 struct device *etm_dev = &csdev->dev;
273 struct csdev_access *csa = &csdev->access;
276 etm4_cs_unlock(drvdata, csa);
277 etm4_enable_arch_specific(drvdata);
279 etm4_os_unlock(drvdata);
281 rc = coresight_claim_device_unlocked(csdev);
285 /* Disable the trace unit before programming trace registers */
286 etm4x_relaxed_write32(csa, 0, TRCPRGCTLR);
289 * If we use system instructions, we need to synchronize the
290 * write to the TRCPRGCTLR, before accessing the TRCSTATR.
291 * See ARM IHI0064F, section
292 * "4.3.7 Synchronization of register updates"
297 /* wait for TRCSTATR.IDLE to go up */
298 if (coresight_timeout(csa, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
300 "timeout while waiting for Idle Trace Status\n");
302 etm4x_relaxed_write32(csa, config->pe_sel, TRCPROCSELR);
303 etm4x_relaxed_write32(csa, config->cfg, TRCCONFIGR);
304 /* nothing specific implemented */
305 etm4x_relaxed_write32(csa, 0x0, TRCAUXCTLR);
306 etm4x_relaxed_write32(csa, config->eventctrl0, TRCEVENTCTL0R);
307 etm4x_relaxed_write32(csa, config->eventctrl1, TRCEVENTCTL1R);
308 etm4x_relaxed_write32(csa, config->stall_ctrl, TRCSTALLCTLR);
309 etm4x_relaxed_write32(csa, config->ts_ctrl, TRCTSCTLR);
310 etm4x_relaxed_write32(csa, config->syncfreq, TRCSYNCPR);
311 etm4x_relaxed_write32(csa, config->ccctlr, TRCCCCTLR);
312 etm4x_relaxed_write32(csa, config->bb_ctrl, TRCBBCTLR);
313 etm4x_relaxed_write32(csa, drvdata->trcid, TRCTRACEIDR);
314 etm4x_relaxed_write32(csa, config->vinst_ctrl, TRCVICTLR);
315 etm4x_relaxed_write32(csa, config->viiectlr, TRCVIIECTLR);
316 etm4x_relaxed_write32(csa, config->vissctlr, TRCVISSCTLR);
317 if (drvdata->nr_pe_cmp)
318 etm4x_relaxed_write32(csa, config->vipcssctlr, TRCVIPCSSCTLR);
319 for (i = 0; i < drvdata->nrseqstate - 1; i++)
320 etm4x_relaxed_write32(csa, config->seq_ctrl[i], TRCSEQEVRn(i));
321 etm4x_relaxed_write32(csa, config->seq_rst, TRCSEQRSTEVR);
322 etm4x_relaxed_write32(csa, config->seq_state, TRCSEQSTR);
323 etm4x_relaxed_write32(csa, config->ext_inp, TRCEXTINSELR);
324 for (i = 0; i < drvdata->nr_cntr; i++) {
325 etm4x_relaxed_write32(csa, config->cntrldvr[i], TRCCNTRLDVRn(i));
326 etm4x_relaxed_write32(csa, config->cntr_ctrl[i], TRCCNTCTLRn(i));
327 etm4x_relaxed_write32(csa, config->cntr_val[i], TRCCNTVRn(i));
331 * Resource selector pair 0 is always implemented and reserved. As
334 for (i = 2; i < drvdata->nr_resource * 2; i++)
335 etm4x_relaxed_write32(csa, config->res_ctrl[i], TRCRSCTLRn(i));
337 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
338 /* always clear status bit on restart if using single-shot */
339 if (config->ss_ctrl[i] || config->ss_pe_cmp[i])
340 config->ss_status[i] &= ~BIT(31);
341 etm4x_relaxed_write32(csa, config->ss_ctrl[i], TRCSSCCRn(i));
342 etm4x_relaxed_write32(csa, config->ss_status[i], TRCSSCSRn(i));
343 if (etm4x_sspcicrn_present(drvdata, i))
344 etm4x_relaxed_write32(csa, config->ss_pe_cmp[i], TRCSSPCICRn(i));
346 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
347 etm4x_relaxed_write64(csa, config->addr_val[i], TRCACVRn(i));
348 etm4x_relaxed_write64(csa, config->addr_acc[i], TRCACATRn(i));
350 for (i = 0; i < drvdata->numcidc; i++)
351 etm4x_relaxed_write64(csa, config->ctxid_pid[i], TRCCIDCVRn(i));
352 etm4x_relaxed_write32(csa, config->ctxid_mask0, TRCCIDCCTLR0);
353 if (drvdata->numcidc > 4)
354 etm4x_relaxed_write32(csa, config->ctxid_mask1, TRCCIDCCTLR1);
356 for (i = 0; i < drvdata->numvmidc; i++)
357 etm4x_relaxed_write64(csa, config->vmid_val[i], TRCVMIDCVRn(i));
358 etm4x_relaxed_write32(csa, config->vmid_mask0, TRCVMIDCCTLR0);
359 if (drvdata->numvmidc > 4)
360 etm4x_relaxed_write32(csa, config->vmid_mask1, TRCVMIDCCTLR1);
362 if (!drvdata->skip_power_up) {
363 u32 trcpdcr = etm4x_relaxed_read32(csa, TRCPDCR);
366 * Request to keep the trace unit powered and also
367 * emulation of powerdown
369 etm4x_relaxed_write32(csa, trcpdcr | TRCPDCR_PU, TRCPDCR);
372 /* Enable the trace unit */
373 etm4x_relaxed_write32(csa, 1, TRCPRGCTLR);
375 /* Synchronize the register updates for sysreg access */
379 /* wait for TRCSTATR.IDLE to go back down to '0' */
380 if (coresight_timeout(csa, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
382 "timeout while waiting for Idle Trace Status\n");
385 * As recommended by section 4.3.7 ("Synchronization when using the
386 * memory-mapped interface") of ARM IHI 0064D
392 etm4_cs_lock(drvdata, csa);
394 dev_dbg(etm_dev, "cpu: %d enable smp call done: %d\n",
399 static void etm4_enable_hw_smp_call(void *info)
401 struct etm4_enable_arg *arg = info;
405 arg->rc = etm4_enable_hw(arg->drvdata);
409 * The goal of function etm4_config_timestamp_event() is to configure a
410 * counter that will tell the tracer to emit a timestamp packet when it
411 * reaches zero. This is done in order to get a more fine grained idea
412 * of when instructions are executed so that they can be correlated
413 * with execution on other CPUs.
415 * To do this the counter itself is configured to self reload and
416 * TRCRSCTLR1 (always true) used to get the counter to decrement. From
417 * there a resource selector is configured with the counter and the
418 * timestamp control register to use the resource selector to trigger the
419 * event that will insert a timestamp packet in the stream.
421 static int etm4_config_timestamp_event(struct etmv4_drvdata *drvdata)
423 int ctridx, ret = -EINVAL;
424 int counter, rselector;
426 struct etmv4_config *config = &drvdata->config;
428 /* No point in trying if we don't have at least one counter */
429 if (!drvdata->nr_cntr)
432 /* Find a counter that hasn't been initialised */
433 for (ctridx = 0; ctridx < drvdata->nr_cntr; ctridx++)
434 if (config->cntr_val[ctridx] == 0)
437 /* All the counters have been configured already, bail out */
438 if (ctridx == drvdata->nr_cntr) {
439 pr_debug("%s: no available counter found\n", __func__);
445 * Searching for an available resource selector to use, starting at
446 * '2' since every implementation has at least 2 resource selector.
447 * ETMIDR4 gives the number of resource selector _pairs_,
448 * hence multiply by 2.
450 for (rselector = 2; rselector < drvdata->nr_resource * 2; rselector++)
451 if (!config->res_ctrl[rselector])
454 if (rselector == drvdata->nr_resource * 2) {
455 pr_debug("%s: no available resource selector found\n",
461 /* Remember what counter we used */
462 counter = 1 << ctridx;
465 * Initialise original and reload counter value to the smallest
466 * possible value in order to get as much precision as we can.
468 config->cntr_val[ctridx] = 1;
469 config->cntrldvr[ctridx] = 1;
471 /* Set the trace counter control register */
472 val = 0x1 << 16 | /* Bit 16, reload counter automatically */
473 0x0 << 7 | /* Select single resource selector */
474 0x1; /* Resource selector 1, i.e always true */
476 config->cntr_ctrl[ctridx] = val;
478 val = 0x2 << 16 | /* Group 0b0010 - Counter and sequencers */
479 counter << 0; /* Counter to use */
481 config->res_ctrl[rselector] = val;
483 val = 0x0 << 7 | /* Select single resource selector */
484 rselector; /* Resource selector */
486 config->ts_ctrl = val;
493 static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
494 struct perf_event *event)
497 struct etmv4_config *config = &drvdata->config;
498 struct perf_event_attr *attr = &event->attr;
505 /* Clear configuration from previous run */
506 memset(config, 0, sizeof(struct etmv4_config));
508 if (attr->exclude_kernel)
509 config->mode = ETM_MODE_EXCL_KERN;
511 if (attr->exclude_user)
512 config->mode = ETM_MODE_EXCL_USER;
514 /* Always start from the default config */
515 etm4_set_default_config(config);
517 /* Configure filters specified on the perf cmd line, if any. */
518 ret = etm4_set_event_filters(drvdata, event);
522 /* Go from generic option to ETMv4 specifics */
523 if (attr->config & BIT(ETM_OPT_CYCACC)) {
524 config->cfg |= BIT(4);
525 /* TRM: Must program this for cycacc to work */
526 config->ccctlr = ETM_CYC_THRESHOLD_DEFAULT;
528 if (attr->config & BIT(ETM_OPT_TS)) {
530 * Configure timestamps to be emitted at regular intervals in
531 * order to correlate instructions executed on different CPUs
532 * (CPU-wide trace scenarios).
534 ret = etm4_config_timestamp_event(drvdata);
537 * No need to go further if timestamp intervals can't
543 /* bit[11], Global timestamp tracing bit */
544 config->cfg |= BIT(11);
547 if (attr->config & BIT(ETM_OPT_CTXTID))
548 /* bit[6], Context ID tracing bit */
549 config->cfg |= BIT(ETM4_CFG_BIT_CTXTID);
551 /* return stack - enable if selected and supported */
552 if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack)
553 /* bit[12], Return stack enable bit */
554 config->cfg |= BIT(12);
560 static int etm4_enable_perf(struct coresight_device *csdev,
561 struct perf_event *event)
564 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
566 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) {
571 /* Configure the tracer based on the session's specifics */
572 ret = etm4_parse_event_config(drvdata, event);
576 ret = etm4_enable_hw(drvdata);
582 static int etm4_enable_sysfs(struct coresight_device *csdev)
584 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
585 struct etm4_enable_arg arg = { };
588 spin_lock(&drvdata->spinlock);
591 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
592 * ensures that register writes occur when cpu is powered.
594 arg.drvdata = drvdata;
595 ret = smp_call_function_single(drvdata->cpu,
596 etm4_enable_hw_smp_call, &arg, 1);
600 drvdata->sticky_enable = true;
601 spin_unlock(&drvdata->spinlock);
604 dev_dbg(&csdev->dev, "ETM tracing enabled\n");
608 static int etm4_enable(struct coresight_device *csdev,
609 struct perf_event *event, u32 mode)
613 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
615 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
617 /* Someone is already using the tracer */
623 ret = etm4_enable_sysfs(csdev);
626 ret = etm4_enable_perf(csdev, event);
632 /* The tracer didn't start */
634 local_set(&drvdata->mode, CS_MODE_DISABLED);
639 static void etm4_disable_hw(void *info)
642 struct etmv4_drvdata *drvdata = info;
643 struct etmv4_config *config = &drvdata->config;
644 struct coresight_device *csdev = drvdata->csdev;
645 struct device *etm_dev = &csdev->dev;
646 struct csdev_access *csa = &csdev->access;
649 etm4_cs_unlock(drvdata, csa);
650 etm4_disable_arch_specific(drvdata);
652 if (!drvdata->skip_power_up) {
653 /* power can be removed from the trace unit now */
654 control = etm4x_relaxed_read32(csa, TRCPDCR);
655 control &= ~TRCPDCR_PU;
656 etm4x_relaxed_write32(csa, control, TRCPDCR);
659 control = etm4x_relaxed_read32(csa, TRCPRGCTLR);
661 /* EN, bit[0] Trace unit enable bit */
665 * Make sure everything completes before disabling, as recommended
666 * by section 7.3.77 ("TRCVICTLR, ViewInst Main Control Register,
667 * SSTATUS") of ARM IHI 0064D
671 etm4x_relaxed_write32(csa, control, TRCPRGCTLR);
673 /* wait for TRCSTATR.PMSTABLE to go to '1' */
674 if (coresight_timeout(csa, TRCSTATR, TRCSTATR_PMSTABLE_BIT, 1))
676 "timeout while waiting for PM stable Trace Status\n");
678 /* read the status of the single shot comparators */
679 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
680 config->ss_status[i] =
681 etm4x_relaxed_read32(csa, TRCSSCSRn(i));
684 /* read back the current counter values */
685 for (i = 0; i < drvdata->nr_cntr; i++) {
686 config->cntr_val[i] =
687 etm4x_relaxed_read32(csa, TRCCNTVRn(i));
690 coresight_disclaim_device_unlocked(csdev);
691 etm4_cs_lock(drvdata, csa);
693 dev_dbg(&drvdata->csdev->dev,
694 "cpu: %d disable smp call done\n", drvdata->cpu);
697 static int etm4_disable_perf(struct coresight_device *csdev,
698 struct perf_event *event)
701 struct etm_filters *filters = event->hw.addr_filters;
702 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
704 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
707 etm4_disable_hw(drvdata);
710 * Check if the start/stop logic was active when the unit was stopped.
711 * That way we can re-enable the start/stop logic when the process is
712 * scheduled again. Configuration of the start/stop logic happens in
713 * function etm4_set_event_filters().
715 control = etm4x_relaxed_read32(&csdev->access, TRCVICTLR);
716 /* TRCVICTLR::SSSTATUS, bit[9] */
717 filters->ssstatus = (control & BIT(9));
722 static void etm4_disable_sysfs(struct coresight_device *csdev)
724 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
727 * Taking hotplug lock here protects from clocks getting disabled
728 * with tracing being left on (crash scenario) if user disable occurs
729 * after cpu online mask indicates the cpu is offline but before the
730 * DYING hotplug callback is serviced by the ETM driver.
733 spin_lock(&drvdata->spinlock);
736 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
737 * ensures that register writes occur when cpu is powered.
739 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
741 spin_unlock(&drvdata->spinlock);
744 dev_dbg(&csdev->dev, "ETM tracing disabled\n");
747 static void etm4_disable(struct coresight_device *csdev,
748 struct perf_event *event)
751 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
754 * For as long as the tracer isn't disabled another entity can't
755 * change its status. As such we can read the status here without
756 * fearing it will change under us.
758 mode = local_read(&drvdata->mode);
761 case CS_MODE_DISABLED:
764 etm4_disable_sysfs(csdev);
767 etm4_disable_perf(csdev, event);
772 local_set(&drvdata->mode, CS_MODE_DISABLED);
775 static const struct coresight_ops_source etm4_source_ops = {
776 .cpu_id = etm4_cpu_id,
777 .trace_id = etm4_trace_id,
778 .enable = etm4_enable,
779 .disable = etm4_disable,
782 static const struct coresight_ops etm4_cs_ops = {
783 .source_ops = &etm4_source_ops,
786 static inline bool cpu_supports_sysreg_trace(void)
788 u64 dfr0 = read_sysreg_s(SYS_ID_AA64DFR0_EL1);
790 return ((dfr0 >> ID_AA64DFR0_TRACEVER_SHIFT) & 0xfUL) > 0;
793 static bool etm4_init_sysreg_access(struct etmv4_drvdata *drvdata,
794 struct csdev_access *csa)
798 if (!cpu_supports_sysreg_trace())
802 * ETMs implementing sysreg access must implement TRCDEVARCH.
804 devarch = read_etm4x_sysreg_const_offset(TRCDEVARCH);
805 if ((devarch & ETM_DEVARCH_ID_MASK) != ETM_DEVARCH_ETMv4x_ARCH)
807 *csa = (struct csdev_access) {
809 .read = etm4x_sysreg_read,
810 .write = etm4x_sysreg_write,
813 drvdata->arch = etm_devarch_to_arch(devarch);
817 static bool etm4_init_iomem_access(struct etmv4_drvdata *drvdata,
818 struct csdev_access *csa)
820 u32 devarch = readl_relaxed(drvdata->base + TRCDEVARCH);
821 u32 idr1 = readl_relaxed(drvdata->base + TRCIDR1);
824 * All ETMs must implement TRCDEVARCH to indicate that
825 * the component is an ETMv4. To support any broken
826 * implementations we fall back to TRCIDR1 check, which
827 * is not really reliable.
829 if ((devarch & ETM_DEVARCH_ID_MASK) == ETM_DEVARCH_ETMv4x_ARCH) {
830 drvdata->arch = etm_devarch_to_arch(devarch);
832 pr_warn("CPU%d: ETM4x incompatible TRCDEVARCH: %x, falling back to TRCIDR1\n",
833 smp_processor_id(), devarch);
835 if (ETM_TRCIDR1_ARCH_MAJOR(idr1) != ETM_TRCIDR1_ARCH_ETMv4)
837 drvdata->arch = etm_trcidr_to_arch(idr1);
840 *csa = CSDEV_ACCESS_IOMEM(drvdata->base);
844 static bool etm4_init_csdev_access(struct etmv4_drvdata *drvdata,
845 struct csdev_access *csa)
848 * Always choose the memory mapped io, if there is
849 * a memory map to prevent sysreg access on broken
853 return etm4_init_iomem_access(drvdata, csa);
855 if (etm4_init_sysreg_access(drvdata, csa))
861 static void etm4_init_arch_data(void *info)
868 struct etm4_init_arg *init_arg = info;
869 struct etmv4_drvdata *drvdata;
870 struct csdev_access *csa;
873 drvdata = init_arg->drvdata;
877 * If we are unable to detect the access mechanism,
878 * or unable to detect the trace unit type, fail
881 if (!etm4_init_csdev_access(drvdata, csa))
884 /* Make sure all registers are accessible */
885 etm4_os_unlock_csa(drvdata, csa);
886 etm4_cs_unlock(drvdata, csa);
888 etm4_check_arch_features(drvdata, init_arg->pid);
890 /* find all capabilities of the tracing unit */
891 etmidr0 = etm4x_relaxed_read32(csa, TRCIDR0);
893 /* INSTP0, bits[2:1] P0 tracing support field */
894 if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
895 drvdata->instrp0 = true;
897 drvdata->instrp0 = false;
899 /* TRCBB, bit[5] Branch broadcast tracing support bit */
900 if (BMVAL(etmidr0, 5, 5))
901 drvdata->trcbb = true;
903 drvdata->trcbb = false;
905 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
906 if (BMVAL(etmidr0, 6, 6))
907 drvdata->trccond = true;
909 drvdata->trccond = false;
911 /* TRCCCI, bit[7] Cycle counting instruction bit */
912 if (BMVAL(etmidr0, 7, 7))
913 drvdata->trccci = true;
915 drvdata->trccci = false;
917 /* RETSTACK, bit[9] Return stack bit */
918 if (BMVAL(etmidr0, 9, 9))
919 drvdata->retstack = true;
921 drvdata->retstack = false;
923 /* NUMEVENT, bits[11:10] Number of events field */
924 drvdata->nr_event = BMVAL(etmidr0, 10, 11);
925 /* QSUPP, bits[16:15] Q element support field */
926 drvdata->q_support = BMVAL(etmidr0, 15, 16);
927 /* TSSIZE, bits[28:24] Global timestamp size field */
928 drvdata->ts_size = BMVAL(etmidr0, 24, 28);
930 /* maximum size of resources */
931 etmidr2 = etm4x_relaxed_read32(csa, TRCIDR2);
932 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
933 drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
934 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
935 drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
936 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
937 drvdata->ccsize = BMVAL(etmidr2, 25, 28);
939 etmidr3 = etm4x_relaxed_read32(csa, TRCIDR3);
940 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
941 drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
942 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
943 drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
944 drvdata->config.s_ex_level = drvdata->s_ex_level;
945 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
946 drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
949 * TRCERR, bit[24] whether a trace unit can trace a
950 * system error exception.
952 if (BMVAL(etmidr3, 24, 24))
953 drvdata->trc_error = true;
955 drvdata->trc_error = false;
957 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
958 if (BMVAL(etmidr3, 25, 25))
959 drvdata->syncpr = true;
961 drvdata->syncpr = false;
963 /* STALLCTL, bit[26] is stall control implemented? */
964 if (BMVAL(etmidr3, 26, 26))
965 drvdata->stallctl = true;
967 drvdata->stallctl = false;
969 /* SYSSTALL, bit[27] implementation can support stall control? */
970 if (BMVAL(etmidr3, 27, 27))
971 drvdata->sysstall = true;
973 drvdata->sysstall = false;
976 * NUMPROC - the number of PEs available for tracing, 5bits
977 * = TRCIDR3.bits[13:12]bits[30:28]
978 * bits[4:3] = TRCIDR3.bits[13:12] (since etm-v4.2, otherwise RES0)
979 * bits[3:0] = TRCIDR3.bits[30:28]
981 drvdata->nr_pe = (BMVAL(etmidr3, 12, 13) << 3) | BMVAL(etmidr3, 28, 30);
983 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
984 if (BMVAL(etmidr3, 31, 31))
985 drvdata->nooverflow = true;
987 drvdata->nooverflow = false;
989 /* number of resources trace unit supports */
990 etmidr4 = etm4x_relaxed_read32(csa, TRCIDR4);
991 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
992 drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
993 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
994 drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
996 * NUMRSPAIR, bits[19:16]
997 * The number of resource pairs conveyed by the HW starts at 0, i.e a
998 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
999 * As such add 1 to the value of NUMRSPAIR for a better representation.
1001 * For ETM v4.3 and later, 0x0 means 0, and no pairs are available -
1002 * the default TRUE and FALSE resource selectors are omitted.
1003 * Otherwise for values 0x1 and above the number is N + 1 as per v4.2.
1005 drvdata->nr_resource = BMVAL(etmidr4, 16, 19);
1006 if ((drvdata->arch < ETM_ARCH_V4_3) || (drvdata->nr_resource > 0))
1007 drvdata->nr_resource += 1;
1009 * NUMSSCC, bits[23:20] the number of single-shot
1010 * comparator control for tracing. Read any status regs as these
1011 * also contain RO capability data.
1013 drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
1014 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
1015 drvdata->config.ss_status[i] =
1016 etm4x_relaxed_read32(csa, TRCSSCSRn(i));
1018 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
1019 drvdata->numcidc = BMVAL(etmidr4, 24, 27);
1020 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
1021 drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
1023 etmidr5 = etm4x_relaxed_read32(csa, TRCIDR5);
1024 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
1025 drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
1026 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
1027 drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
1028 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
1029 if (BMVAL(etmidr5, 22, 22))
1030 drvdata->atbtrig = true;
1032 drvdata->atbtrig = false;
1034 * LPOVERRIDE, bit[23] implementation supports
1035 * low-power state override
1037 if (BMVAL(etmidr5, 23, 23) && (!drvdata->skip_power_up))
1038 drvdata->lpoverride = true;
1040 drvdata->lpoverride = false;
1041 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
1042 drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
1043 /* NUMCNTR, bits[30:28] number of counters available for tracing */
1044 drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
1045 etm4_cs_lock(drvdata, csa);
1048 static inline u32 etm4_get_victlr_access_type(struct etmv4_config *config)
1050 return etm4_get_access_type(config) << TRCVICTLR_EXLEVEL_SHIFT;
1053 /* Set ELx trace filter access in the TRCVICTLR register */
1054 static void etm4_set_victlr_access(struct etmv4_config *config)
1056 config->vinst_ctrl &= ~TRCVICTLR_EXLEVEL_MASK;
1057 config->vinst_ctrl |= etm4_get_victlr_access_type(config);
1060 static void etm4_set_default_config(struct etmv4_config *config)
1062 /* disable all events tracing */
1063 config->eventctrl0 = 0x0;
1064 config->eventctrl1 = 0x0;
1066 /* disable stalling */
1067 config->stall_ctrl = 0x0;
1069 /* enable trace synchronization every 4096 bytes, if available */
1070 config->syncfreq = 0xC;
1072 /* disable timestamp event */
1073 config->ts_ctrl = 0x0;
1075 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
1076 config->vinst_ctrl = BIT(0);
1078 /* TRCVICTLR::EXLEVEL_NS:EXLEVELS: Set kernel / user filtering */
1079 etm4_set_victlr_access(config);
1082 static u64 etm4_get_ns_access_type(struct etmv4_config *config)
1084 u64 access_type = 0;
1087 * EXLEVEL_NS, for NonSecure Exception levels.
1088 * The mask here is a generic value and must be
1089 * shifted to the corresponding field for the registers
1091 if (!is_kernel_in_hyp_mode()) {
1092 /* Stay away from hypervisor mode for non-VHE */
1093 access_type = ETM_EXLEVEL_NS_HYP;
1094 if (config->mode & ETM_MODE_EXCL_KERN)
1095 access_type |= ETM_EXLEVEL_NS_OS;
1096 } else if (config->mode & ETM_MODE_EXCL_KERN) {
1097 access_type = ETM_EXLEVEL_NS_HYP;
1100 if (config->mode & ETM_MODE_EXCL_USER)
1101 access_type |= ETM_EXLEVEL_NS_APP;
1107 * Construct the exception level masks for a given config.
1108 * This must be shifted to the corresponding register field
1111 static u64 etm4_get_access_type(struct etmv4_config *config)
1113 /* All Secure exception levels are excluded from the trace */
1114 return etm4_get_ns_access_type(config) | (u64)config->s_ex_level;
1117 static u64 etm4_get_comparator_access_type(struct etmv4_config *config)
1119 return etm4_get_access_type(config) << TRCACATR_EXLEVEL_SHIFT;
1122 static void etm4_set_comparator_filter(struct etmv4_config *config,
1123 u64 start, u64 stop, int comparator)
1125 u64 access_type = etm4_get_comparator_access_type(config);
1127 /* First half of default address comparator */
1128 config->addr_val[comparator] = start;
1129 config->addr_acc[comparator] = access_type;
1130 config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
1132 /* Second half of default address comparator */
1133 config->addr_val[comparator + 1] = stop;
1134 config->addr_acc[comparator + 1] = access_type;
1135 config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
1138 * Configure the ViewInst function to include this address range
1141 * @comparator is divided by two since it is the index in the
1142 * etmv4_config::addr_val array but register TRCVIIECTLR deals with
1143 * address range comparator _pairs_.
1146 * index 0 -> compatator pair 0
1147 * index 2 -> comparator pair 1
1148 * index 4 -> comparator pair 2
1150 * index 14 -> comparator pair 7
1152 config->viiectlr |= BIT(comparator / 2);
1155 static void etm4_set_start_stop_filter(struct etmv4_config *config,
1156 u64 address, int comparator,
1157 enum etm_addr_type type)
1160 u64 access_type = etm4_get_comparator_access_type(config);
1162 /* Configure the comparator */
1163 config->addr_val[comparator] = address;
1164 config->addr_acc[comparator] = access_type;
1165 config->addr_type[comparator] = type;
1168 * Configure ViewInst Start-Stop control register.
1169 * Addresses configured to start tracing go from bit 0 to n-1,
1170 * while those configured to stop tracing from 16 to 16 + n-1.
1172 shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
1173 config->vissctlr |= BIT(shift + comparator);
1176 static void etm4_set_default_filter(struct etmv4_config *config)
1178 /* Trace everything 'default' filter achieved by no filtering */
1179 config->viiectlr = 0x0;
1182 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
1183 * in the started state
1185 config->vinst_ctrl |= BIT(9);
1186 config->mode |= ETM_MODE_VIEWINST_STARTSTOP;
1188 /* No start-stop filtering for ViewInst */
1189 config->vissctlr = 0x0;
1192 static void etm4_set_default(struct etmv4_config *config)
1194 if (WARN_ON_ONCE(!config))
1198 * Make default initialisation trace everything
1200 * This is done by a minimum default config sufficient to enable
1201 * full instruction trace - with a default filter for trace all
1202 * achieved by having no filtering.
1204 etm4_set_default_config(config);
1205 etm4_set_default_filter(config);
1208 static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
1210 int nr_comparator, index = 0;
1211 struct etmv4_config *config = &drvdata->config;
1214 * nr_addr_cmp holds the number of comparator _pair_, so time 2
1215 * for the total number of comparators.
1217 nr_comparator = drvdata->nr_addr_cmp * 2;
1219 /* Go through the tally of comparators looking for a free one. */
1220 while (index < nr_comparator) {
1222 case ETM_ADDR_TYPE_RANGE:
1223 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
1224 config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
1227 /* Address range comparators go in pairs */
1230 case ETM_ADDR_TYPE_START:
1231 case ETM_ADDR_TYPE_STOP:
1232 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
1235 /* Start/stop address can have odd indexes */
1243 /* If we are here all the comparators have been used. */
1247 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
1248 struct perf_event *event)
1250 int i, comparator, ret = 0;
1252 struct etmv4_config *config = &drvdata->config;
1253 struct etm_filters *filters = event->hw.addr_filters;
1256 goto default_filter;
1258 /* Sync events with what Perf got */
1259 perf_event_addr_filters_sync(event);
1262 * If there are no filters to deal with simply go ahead with
1263 * the default filter, i.e the entire address range.
1265 if (!filters->nr_filters)
1266 goto default_filter;
1268 for (i = 0; i < filters->nr_filters; i++) {
1269 struct etm_filter *filter = &filters->etm_filter[i];
1270 enum etm_addr_type type = filter->type;
1272 /* See if a comparator is free. */
1273 comparator = etm4_get_next_comparator(drvdata, type);
1274 if (comparator < 0) {
1280 case ETM_ADDR_TYPE_RANGE:
1281 etm4_set_comparator_filter(config,
1286 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
1287 * in the started state
1289 config->vinst_ctrl |= BIT(9);
1291 /* No start-stop filtering for ViewInst */
1292 config->vissctlr = 0x0;
1294 case ETM_ADDR_TYPE_START:
1295 case ETM_ADDR_TYPE_STOP:
1296 /* Get the right start or stop address */
1297 address = (type == ETM_ADDR_TYPE_START ?
1298 filter->start_addr :
1301 /* Configure comparator */
1302 etm4_set_start_stop_filter(config, address,
1306 * If filters::ssstatus == 1, trace acquisition was
1307 * started but the process was yanked away before the
1308 * the stop address was hit. As such the start/stop
1309 * logic needs to be re-started so that tracing can
1310 * resume where it left.
1312 * The start/stop logic status when a process is
1313 * scheduled out is checked in function
1314 * etm4_disable_perf().
1316 if (filters->ssstatus)
1317 config->vinst_ctrl |= BIT(9);
1319 /* No include/exclude filtering for ViewInst */
1320 config->viiectlr = 0x0;
1332 etm4_set_default_filter(config);
1338 void etm4_config_trace_mode(struct etmv4_config *config)
1342 mode = config->mode;
1343 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
1345 /* excluding kernel AND user space doesn't make sense */
1346 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
1348 /* nothing to do if neither flags are set */
1349 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
1352 etm4_set_victlr_access(config);
1355 static int etm4_online_cpu(unsigned int cpu)
1357 if (!etmdrvdata[cpu])
1360 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
1361 coresight_enable(etmdrvdata[cpu]->csdev);
1365 static int etm4_starting_cpu(unsigned int cpu)
1367 if (!etmdrvdata[cpu])
1370 spin_lock(&etmdrvdata[cpu]->spinlock);
1371 if (!etmdrvdata[cpu]->os_unlock)
1372 etm4_os_unlock(etmdrvdata[cpu]);
1374 if (local_read(&etmdrvdata[cpu]->mode))
1375 etm4_enable_hw(etmdrvdata[cpu]);
1376 spin_unlock(&etmdrvdata[cpu]->spinlock);
1380 static int etm4_dying_cpu(unsigned int cpu)
1382 if (!etmdrvdata[cpu])
1385 spin_lock(&etmdrvdata[cpu]->spinlock);
1386 if (local_read(&etmdrvdata[cpu]->mode))
1387 etm4_disable_hw(etmdrvdata[cpu]);
1388 spin_unlock(&etmdrvdata[cpu]->spinlock);
1392 static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
1394 drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
1397 static int etm4_cpu_save(struct etmv4_drvdata *drvdata)
1400 struct etmv4_save_state *state;
1401 struct coresight_device *csdev = drvdata->csdev;
1402 struct csdev_access *csa;
1403 struct device *etm_dev;
1405 if (WARN_ON(!csdev))
1408 etm_dev = &csdev->dev;
1409 csa = &csdev->access;
1412 * As recommended by 3.4.1 ("The procedure when powering down the PE")
1418 etm4_cs_unlock(drvdata, csa);
1419 /* Lock the OS lock to disable trace and external debugger access */
1420 etm4_os_lock(drvdata);
1422 /* wait for TRCSTATR.PMSTABLE to go up */
1423 if (coresight_timeout(csa, TRCSTATR, TRCSTATR_PMSTABLE_BIT, 1)) {
1425 "timeout while waiting for PM Stable Status\n");
1426 etm4_os_unlock(drvdata);
1431 state = drvdata->save_state;
1433 state->trcprgctlr = etm4x_read32(csa, TRCPRGCTLR);
1435 state->trcprocselr = etm4x_read32(csa, TRCPROCSELR);
1436 state->trcconfigr = etm4x_read32(csa, TRCCONFIGR);
1437 state->trcauxctlr = etm4x_read32(csa, TRCAUXCTLR);
1438 state->trceventctl0r = etm4x_read32(csa, TRCEVENTCTL0R);
1439 state->trceventctl1r = etm4x_read32(csa, TRCEVENTCTL1R);
1440 state->trcstallctlr = etm4x_read32(csa, TRCSTALLCTLR);
1441 state->trctsctlr = etm4x_read32(csa, TRCTSCTLR);
1442 state->trcsyncpr = etm4x_read32(csa, TRCSYNCPR);
1443 state->trcccctlr = etm4x_read32(csa, TRCCCCTLR);
1444 state->trcbbctlr = etm4x_read32(csa, TRCBBCTLR);
1445 state->trctraceidr = etm4x_read32(csa, TRCTRACEIDR);
1446 state->trcqctlr = etm4x_read32(csa, TRCQCTLR);
1448 state->trcvictlr = etm4x_read32(csa, TRCVICTLR);
1449 state->trcviiectlr = etm4x_read32(csa, TRCVIIECTLR);
1450 state->trcvissctlr = etm4x_read32(csa, TRCVISSCTLR);
1451 if (drvdata->nr_pe_cmp)
1452 state->trcvipcssctlr = etm4x_read32(csa, TRCVIPCSSCTLR);
1453 state->trcvdctlr = etm4x_read32(csa, TRCVDCTLR);
1454 state->trcvdsacctlr = etm4x_read32(csa, TRCVDSACCTLR);
1455 state->trcvdarcctlr = etm4x_read32(csa, TRCVDARCCTLR);
1457 for (i = 0; i < drvdata->nrseqstate - 1; i++)
1458 state->trcseqevr[i] = etm4x_read32(csa, TRCSEQEVRn(i));
1460 state->trcseqrstevr = etm4x_read32(csa, TRCSEQRSTEVR);
1461 state->trcseqstr = etm4x_read32(csa, TRCSEQSTR);
1462 state->trcextinselr = etm4x_read32(csa, TRCEXTINSELR);
1464 for (i = 0; i < drvdata->nr_cntr; i++) {
1465 state->trccntrldvr[i] = etm4x_read32(csa, TRCCNTRLDVRn(i));
1466 state->trccntctlr[i] = etm4x_read32(csa, TRCCNTCTLRn(i));
1467 state->trccntvr[i] = etm4x_read32(csa, TRCCNTVRn(i));
1470 for (i = 0; i < drvdata->nr_resource * 2; i++)
1471 state->trcrsctlr[i] = etm4x_read32(csa, TRCRSCTLRn(i));
1473 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
1474 state->trcssccr[i] = etm4x_read32(csa, TRCSSCCRn(i));
1475 state->trcsscsr[i] = etm4x_read32(csa, TRCSSCSRn(i));
1476 if (etm4x_sspcicrn_present(drvdata, i))
1477 state->trcsspcicr[i] = etm4x_read32(csa, TRCSSPCICRn(i));
1480 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) {
1481 state->trcacvr[i] = etm4x_read64(csa, TRCACVRn(i));
1482 state->trcacatr[i] = etm4x_read64(csa, TRCACATRn(i));
1486 * Data trace stream is architecturally prohibited for A profile cores
1487 * so we don't save (or later restore) trcdvcvr and trcdvcmr - As per
1488 * section 1.3.4 ("Possible functional configurations of an ETMv4 trace
1489 * unit") of ARM IHI 0064D.
1492 for (i = 0; i < drvdata->numcidc; i++)
1493 state->trccidcvr[i] = etm4x_read64(csa, TRCCIDCVRn(i));
1495 for (i = 0; i < drvdata->numvmidc; i++)
1496 state->trcvmidcvr[i] = etm4x_read64(csa, TRCVMIDCVRn(i));
1498 state->trccidcctlr0 = etm4x_read32(csa, TRCCIDCCTLR0);
1499 if (drvdata->numcidc > 4)
1500 state->trccidcctlr1 = etm4x_read32(csa, TRCCIDCCTLR1);
1502 state->trcvmidcctlr0 = etm4x_read32(csa, TRCVMIDCCTLR0);
1503 if (drvdata->numvmidc > 4)
1504 state->trcvmidcctlr0 = etm4x_read32(csa, TRCVMIDCCTLR1);
1506 state->trcclaimset = etm4x_read32(csa, TRCCLAIMCLR);
1508 if (!drvdata->skip_power_up)
1509 state->trcpdcr = etm4x_read32(csa, TRCPDCR);
1511 /* wait for TRCSTATR.IDLE to go up */
1512 if (coresight_timeout(csa, TRCSTATR, TRCSTATR_IDLE_BIT, 1)) {
1514 "timeout while waiting for Idle Trace Status\n");
1515 etm4_os_unlock(drvdata);
1520 drvdata->state_needs_restore = true;
1523 * Power can be removed from the trace unit now. We do this to
1524 * potentially save power on systems that respect the TRCPDCR_PU
1525 * despite requesting software to save/restore state.
1527 if (!drvdata->skip_power_up)
1528 etm4x_relaxed_write32(csa, (state->trcpdcr & ~TRCPDCR_PU),
1531 etm4_cs_lock(drvdata, csa);
1535 static void etm4_cpu_restore(struct etmv4_drvdata *drvdata)
1538 struct etmv4_save_state *state = drvdata->save_state;
1539 struct csdev_access tmp_csa = CSDEV_ACCESS_IOMEM(drvdata->base);
1540 struct csdev_access *csa = &tmp_csa;
1542 etm4_cs_unlock(drvdata, csa);
1543 etm4x_relaxed_write32(csa, state->trcclaimset, TRCCLAIMSET);
1545 etm4x_relaxed_write32(csa, state->trcprgctlr, TRCPRGCTLR);
1547 etm4x_relaxed_write32(csa, state->trcprocselr, TRCPROCSELR);
1548 etm4x_relaxed_write32(csa, state->trcconfigr, TRCCONFIGR);
1549 etm4x_relaxed_write32(csa, state->trcauxctlr, TRCAUXCTLR);
1550 etm4x_relaxed_write32(csa, state->trceventctl0r, TRCEVENTCTL0R);
1551 etm4x_relaxed_write32(csa, state->trceventctl1r, TRCEVENTCTL1R);
1552 etm4x_relaxed_write32(csa, state->trcstallctlr, TRCSTALLCTLR);
1553 etm4x_relaxed_write32(csa, state->trctsctlr, TRCTSCTLR);
1554 etm4x_relaxed_write32(csa, state->trcsyncpr, TRCSYNCPR);
1555 etm4x_relaxed_write32(csa, state->trcccctlr, TRCCCCTLR);
1556 etm4x_relaxed_write32(csa, state->trcbbctlr, TRCBBCTLR);
1557 etm4x_relaxed_write32(csa, state->trctraceidr, TRCTRACEIDR);
1558 etm4x_relaxed_write32(csa, state->trcqctlr, TRCQCTLR);
1560 etm4x_relaxed_write32(csa, state->trcvictlr, TRCVICTLR);
1561 etm4x_relaxed_write32(csa, state->trcviiectlr, TRCVIIECTLR);
1562 etm4x_relaxed_write32(csa, state->trcvissctlr, TRCVISSCTLR);
1563 if (drvdata->nr_pe_cmp)
1564 etm4x_relaxed_write32(csa, state->trcvipcssctlr, TRCVIPCSSCTLR);
1565 etm4x_relaxed_write32(csa, state->trcvdctlr, TRCVDCTLR);
1566 etm4x_relaxed_write32(csa, state->trcvdsacctlr, TRCVDSACCTLR);
1567 etm4x_relaxed_write32(csa, state->trcvdarcctlr, TRCVDARCCTLR);
1569 for (i = 0; i < drvdata->nrseqstate - 1; i++)
1570 etm4x_relaxed_write32(csa, state->trcseqevr[i], TRCSEQEVRn(i));
1572 etm4x_relaxed_write32(csa, state->trcseqrstevr, TRCSEQRSTEVR);
1573 etm4x_relaxed_write32(csa, state->trcseqstr, TRCSEQSTR);
1574 etm4x_relaxed_write32(csa, state->trcextinselr, TRCEXTINSELR);
1576 for (i = 0; i < drvdata->nr_cntr; i++) {
1577 etm4x_relaxed_write32(csa, state->trccntrldvr[i], TRCCNTRLDVRn(i));
1578 etm4x_relaxed_write32(csa, state->trccntctlr[i], TRCCNTCTLRn(i));
1579 etm4x_relaxed_write32(csa, state->trccntvr[i], TRCCNTVRn(i));
1582 for (i = 0; i < drvdata->nr_resource * 2; i++)
1583 etm4x_relaxed_write32(csa, state->trcrsctlr[i], TRCRSCTLRn(i));
1585 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
1586 etm4x_relaxed_write32(csa, state->trcssccr[i], TRCSSCCRn(i));
1587 etm4x_relaxed_write32(csa, state->trcsscsr[i], TRCSSCSRn(i));
1588 if (etm4x_sspcicrn_present(drvdata, i))
1589 etm4x_relaxed_write32(csa, state->trcsspcicr[i], TRCSSPCICRn(i));
1592 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) {
1593 etm4x_relaxed_write64(csa, state->trcacvr[i], TRCACVRn(i));
1594 etm4x_relaxed_write64(csa, state->trcacatr[i], TRCACATRn(i));
1597 for (i = 0; i < drvdata->numcidc; i++)
1598 etm4x_relaxed_write64(csa, state->trccidcvr[i], TRCCIDCVRn(i));
1600 for (i = 0; i < drvdata->numvmidc; i++)
1601 etm4x_relaxed_write64(csa, state->trcvmidcvr[i], TRCVMIDCVRn(i));
1603 etm4x_relaxed_write32(csa, state->trccidcctlr0, TRCCIDCCTLR0);
1604 if (drvdata->numcidc > 4)
1605 etm4x_relaxed_write32(csa, state->trccidcctlr1, TRCCIDCCTLR1);
1607 etm4x_relaxed_write32(csa, state->trcvmidcctlr0, TRCVMIDCCTLR0);
1608 if (drvdata->numvmidc > 4)
1609 etm4x_relaxed_write32(csa, state->trcvmidcctlr0, TRCVMIDCCTLR1);
1611 etm4x_relaxed_write32(csa, state->trcclaimset, TRCCLAIMSET);
1613 if (!drvdata->skip_power_up)
1614 etm4x_relaxed_write32(csa, state->trcpdcr, TRCPDCR);
1616 drvdata->state_needs_restore = false;
1619 * As recommended by section 4.3.7 ("Synchronization when using the
1620 * memory-mapped interface") of ARM IHI 0064D
1625 /* Unlock the OS lock to re-enable trace and external debug access */
1626 etm4_os_unlock(drvdata);
1627 etm4_cs_lock(drvdata, csa);
1630 static int etm4_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd,
1633 struct etmv4_drvdata *drvdata;
1634 unsigned int cpu = smp_processor_id();
1636 if (!etmdrvdata[cpu])
1639 drvdata = etmdrvdata[cpu];
1641 if (!drvdata->save_state)
1644 if (WARN_ON_ONCE(drvdata->cpu != cpu))
1649 /* save the state if self-hosted coresight is in use */
1650 if (local_read(&drvdata->mode))
1651 if (etm4_cpu_save(drvdata))
1655 case CPU_PM_ENTER_FAILED:
1656 if (drvdata->state_needs_restore)
1657 etm4_cpu_restore(drvdata);
1666 static struct notifier_block etm4_cpu_pm_nb = {
1667 .notifier_call = etm4_cpu_pm_notify,
1670 /* Setup PM. Deals with error conditions and counts */
1671 static int __init etm4_pm_setup(void)
1675 ret = cpu_pm_register_notifier(&etm4_cpu_pm_nb);
1679 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING,
1680 "arm/coresight4:starting",
1681 etm4_starting_cpu, etm4_dying_cpu);
1684 goto unregister_notifier;
1686 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
1687 "arm/coresight4:online",
1688 etm4_online_cpu, NULL);
1690 /* HP dyn state ID returned in ret on success */
1696 /* failed dyn state - remove others */
1697 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
1699 unregister_notifier:
1700 cpu_pm_unregister_notifier(&etm4_cpu_pm_nb);
1704 static void etm4_pm_clear(void)
1706 cpu_pm_unregister_notifier(&etm4_cpu_pm_nb);
1707 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
1709 cpuhp_remove_state_nocalls(hp_online);
1714 static int etm4_probe(struct device *dev, void __iomem *base, u32 etm_pid)
1717 struct coresight_platform_data *pdata = NULL;
1718 struct etmv4_drvdata *drvdata;
1719 struct coresight_desc desc = { 0 };
1720 struct etm4_init_arg init_arg = { 0 };
1722 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1726 dev_set_drvdata(dev, drvdata);
1728 if (pm_save_enable == PARAM_PM_SAVE_FIRMWARE)
1729 pm_save_enable = coresight_loses_context_with_cpu(dev) ?
1730 PARAM_PM_SAVE_SELF_HOSTED : PARAM_PM_SAVE_NEVER;
1732 if (pm_save_enable != PARAM_PM_SAVE_NEVER) {
1733 drvdata->save_state = devm_kmalloc(dev,
1734 sizeof(struct etmv4_save_state), GFP_KERNEL);
1735 if (!drvdata->save_state)
1739 if (fwnode_property_present(dev_fwnode(dev), "qcom,skip-power-up"))
1740 drvdata->skip_power_up = true;
1742 drvdata->base = base;
1744 spin_lock_init(&drvdata->spinlock);
1746 drvdata->cpu = coresight_get_cpu(dev);
1747 if (drvdata->cpu < 0)
1748 return drvdata->cpu;
1750 desc.name = devm_kasprintf(dev, GFP_KERNEL, "etm%d", drvdata->cpu);
1754 init_arg.drvdata = drvdata;
1755 init_arg.csa = &desc.access;
1756 init_arg.pid = etm_pid;
1758 if (smp_call_function_single(drvdata->cpu,
1759 etm4_init_arch_data, &init_arg, 1))
1760 dev_err(dev, "ETM arch init failed\n");
1765 etm4_init_trace_id(drvdata);
1766 etm4_set_default(&drvdata->config);
1768 pdata = coresight_get_platform_data(dev);
1770 return PTR_ERR(pdata);
1772 dev->platform_data = pdata;
1774 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
1775 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
1776 desc.ops = &etm4_cs_ops;
1779 desc.groups = coresight_etmv4_groups;
1780 drvdata->csdev = coresight_register(&desc);
1781 if (IS_ERR(drvdata->csdev))
1782 return PTR_ERR(drvdata->csdev);
1784 ret = etm_perf_symlink(drvdata->csdev, true);
1786 coresight_unregister(drvdata->csdev);
1790 etmdrvdata[drvdata->cpu] = drvdata;
1792 dev_info(&drvdata->csdev->dev, "CPU%d: ETM v%d.%d initialized\n",
1793 drvdata->cpu, ETM_ARCH_MAJOR_VERSION(drvdata->arch),
1794 ETM_ARCH_MINOR_VERSION(drvdata->arch));
1797 coresight_enable(drvdata->csdev);
1798 drvdata->boot_enable = true;
1804 static int etm4_probe_amba(struct amba_device *adev, const struct amba_id *id)
1807 struct device *dev = &adev->dev;
1808 struct resource *res = &adev->res;
1811 /* Validity for the resource is already checked by the AMBA core */
1812 base = devm_ioremap_resource(dev, res);
1814 return PTR_ERR(base);
1816 ret = etm4_probe(dev, base, id->id);
1818 pm_runtime_put(&adev->dev);
1823 static struct amba_cs_uci_id uci_id_etm4[] = {
1825 /* ETMv4 UCI data */
1826 .devarch = ETM_DEVARCH_ETMv4x_ARCH,
1827 .devarch_mask = ETM_DEVARCH_ID_MASK,
1828 .devtype = 0x00000013,
1832 static void clear_etmdrvdata(void *info)
1834 int cpu = *(int *)info;
1836 etmdrvdata[cpu] = NULL;
1839 static int __exit etm4_remove_dev(struct etmv4_drvdata *drvdata)
1841 etm_perf_symlink(drvdata->csdev, false);
1843 * Taking hotplug lock here to avoid racing between etm4_remove_dev()
1844 * and CPU hotplug call backs.
1848 * The readers for etmdrvdata[] are CPU hotplug call backs
1849 * and PM notification call backs. Change etmdrvdata[i] on
1850 * CPU i ensures these call backs has consistent view
1851 * inside one call back function.
1853 if (smp_call_function_single(drvdata->cpu, clear_etmdrvdata, &drvdata->cpu, 1))
1854 etmdrvdata[drvdata->cpu] = NULL;
1858 coresight_unregister(drvdata->csdev);
1863 static int __exit etm4_remove_amba(struct amba_device *adev)
1865 struct etmv4_drvdata *drvdata = dev_get_drvdata(&adev->dev);
1868 return etm4_remove_dev(drvdata);
1872 static const struct amba_id etm4_ids[] = {
1873 CS_AMBA_ID(0x000bb95d), /* Cortex-A53 */
1874 CS_AMBA_ID(0x000bb95e), /* Cortex-A57 */
1875 CS_AMBA_ID(0x000bb95a), /* Cortex-A72 */
1876 CS_AMBA_ID(0x000bb959), /* Cortex-A73 */
1877 CS_AMBA_UCI_ID(0x000bb9da, uci_id_etm4),/* Cortex-A35 */
1878 CS_AMBA_UCI_ID(0x000bbd05, uci_id_etm4),/* Cortex-A55 */
1879 CS_AMBA_UCI_ID(0x000bbd0a, uci_id_etm4),/* Cortex-A75 */
1880 CS_AMBA_UCI_ID(0x000bbd0c, uci_id_etm4),/* Neoverse N1 */
1881 CS_AMBA_UCI_ID(0x000f0205, uci_id_etm4),/* Qualcomm Kryo */
1882 CS_AMBA_UCI_ID(0x000f0211, uci_id_etm4),/* Qualcomm Kryo */
1883 CS_AMBA_UCI_ID(0x000bb802, uci_id_etm4),/* Qualcomm Kryo 385 Cortex-A55 */
1884 CS_AMBA_UCI_ID(0x000bb803, uci_id_etm4),/* Qualcomm Kryo 385 Cortex-A75 */
1885 CS_AMBA_UCI_ID(0x000bb805, uci_id_etm4),/* Qualcomm Kryo 4XX Cortex-A55 */
1886 CS_AMBA_UCI_ID(0x000bb804, uci_id_etm4),/* Qualcomm Kryo 4XX Cortex-A76 */
1887 CS_AMBA_UCI_ID(0x000cc0af, uci_id_etm4),/* Marvell ThunderX2 */
1888 CS_AMBA_UCI_ID(0x000b6d01, uci_id_etm4),/* HiSilicon-Hip08 */
1889 CS_AMBA_UCI_ID(0x000b6d02, uci_id_etm4),/* HiSilicon-Hip09 */
1893 MODULE_DEVICE_TABLE(amba, etm4_ids);
1895 static struct amba_driver etm4x_amba_driver = {
1897 .name = "coresight-etm4x",
1898 .owner = THIS_MODULE,
1899 .suppress_bind_attrs = true,
1901 .probe = etm4_probe_amba,
1902 .remove = etm4_remove_amba,
1903 .id_table = etm4_ids,
1906 static int __init etm4x_init(void)
1910 ret = etm4_pm_setup();
1912 /* etm4_pm_setup() does its own cleanup - exit on error */
1916 ret = amba_driver_register(&etm4x_amba_driver);
1918 pr_err("Error registering etm4x driver\n");
1925 static void __exit etm4x_exit(void)
1927 amba_driver_unregister(&etm4x_amba_driver);
1931 module_init(etm4x_init);
1932 module_exit(etm4x_exit);
1934 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
1935 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
1936 MODULE_DESCRIPTION("Arm CoreSight Program Flow Trace v4.x driver");
1937 MODULE_LICENSE("GPL v2");