1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4 */
5
6 #include <linux/acpi.h>
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
9 #include <linux/kernel.h>
10 #include <linux/kvm_host.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/device.h>
15 #include <linux/io.h>
16 #include <linux/err.h>
17 #include <linux/fs.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/smp.h>
21 #include <linux/sysfs.h>
22 #include <linux/stat.h>
23 #include <linux/clk.h>
24 #include <linux/cpu.h>
25 #include <linux/cpu_pm.h>
26 #include <linux/coresight.h>
27 #include <linux/coresight-pmu.h>
28 #include <linux/amba/bus.h>
29 #include <linux/seq_file.h>
30 #include <linux/uaccess.h>
31 #include <linux/perf_event.h>
32 #include <linux/perf/arm_pmu.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/property.h>
36 #include <linux/clk/clk-conf.h>
37
38 #include <asm/barrier.h>
39 #include <asm/sections.h>
40 #include <asm/sysreg.h>
41 #include <asm/local.h>
42 #include <asm/virt.h>
43
44 #include "coresight-etm4x.h"
45 #include "coresight-etm-perf.h"
46 #include "coresight-etm4x-cfg.h"
47 #include "coresight-self-hosted-trace.h"
48 #include "coresight-syscfg.h"
49 #include "coresight-trace-id.h"
50
51 static int boot_enable;
52 module_param(boot_enable, int, 0444);
53 MODULE_PARM_DESC(boot_enable, "Enable tracing on boot");
54
55 #define PARAM_PM_SAVE_FIRMWARE 0 /* save self-hosted state as per firmware */
56 #define PARAM_PM_SAVE_NEVER 1 /* never save any state */
57 #define PARAM_PM_SAVE_SELF_HOSTED 2 /* save self-hosted state only */
58
59 static int pm_save_enable = PARAM_PM_SAVE_FIRMWARE;
60 module_param(pm_save_enable, int, 0444);
61 MODULE_PARM_DESC(pm_save_enable,
62 "Save/restore state on power down: 1 = never, 2 = self-hosted");
63
64 static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
65 static void etm4_set_default_config(struct etmv4_config *config);
66 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
67 struct perf_event *event);
68 static u64 etm4_get_access_type(struct etmv4_config *config);
69
70 static enum cpuhp_state hp_online;
71
72 struct etm4_init_arg {
73 struct device *dev;
74 struct csdev_access *csa;
75 };
76
77 static DEFINE_PER_CPU(struct etm4_init_arg *, delayed_probe);
78 static int etm4_probe_cpu(unsigned int cpu);
79
80 /*
81 * Check if TRCSSPCICRn(i) is implemented for a given instance.
82 *
83 * TRCSSPCICRn is implemented only if :
84 * TRCSSPCICR<n> is present only if all of the following are true:
85 * TRCIDR4.NUMSSCC > n.
86 * TRCIDR4.NUMPC > 0b0000 .
87 * TRCSSCSR<n>.PC == 0b1
88 */
etm4x_sspcicrn_present(struct etmv4_drvdata * drvdata,int n)89 static bool etm4x_sspcicrn_present(struct etmv4_drvdata *drvdata, int n)
90 {
91 return (n < drvdata->nr_ss_cmp) &&
92 drvdata->nr_pe &&
93 (drvdata->config.ss_status[n] & TRCSSCSRn_PC);
94 }
95
etm4x_sysreg_read(u32 offset,bool _relaxed,bool _64bit)96 u64 etm4x_sysreg_read(u32 offset, bool _relaxed, bool _64bit)
97 {
98 u64 res = 0;
99
100 switch (offset) {
101 ETM4x_READ_SYSREG_CASES(res)
102 default :
103 pr_warn_ratelimited("etm4x: trying to read unsupported register @%x\n",
104 offset);
105 }
106
107 if (!_relaxed)
108 __io_ar(res); /* Imitate the !relaxed I/O helpers */
109
110 return res;
111 }
112
etm4x_sysreg_write(u64 val,u32 offset,bool _relaxed,bool _64bit)113 void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)
114 {
115 if (!_relaxed)
116 __io_bw(); /* Imitate the !relaxed I/O helpers */
117 if (!_64bit)
118 val &= GENMASK(31, 0);
119
120 switch (offset) {
121 ETM4x_WRITE_SYSREG_CASES(val)
122 default :
123 pr_warn_ratelimited("etm4x: trying to write to unsupported register @%x\n",
124 offset);
125 }
126 }
127
ete_sysreg_read(u32 offset,bool _relaxed,bool _64bit)128 static u64 ete_sysreg_read(u32 offset, bool _relaxed, bool _64bit)
129 {
130 u64 res = 0;
131
132 switch (offset) {
133 ETE_READ_CASES(res)
134 default :
135 pr_warn_ratelimited("ete: trying to read unsupported register @%x\n",
136 offset);
137 }
138
139 if (!_relaxed)
140 __io_ar(res); /* Imitate the !relaxed I/O helpers */
141
142 return res;
143 }
144
ete_sysreg_write(u64 val,u32 offset,bool _relaxed,bool _64bit)145 static void ete_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit)
146 {
147 if (!_relaxed)
148 __io_bw(); /* Imitate the !relaxed I/O helpers */
149 if (!_64bit)
150 val &= GENMASK(31, 0);
151
152 switch (offset) {
153 ETE_WRITE_CASES(val)
154 default :
155 pr_warn_ratelimited("ete: trying to write to unsupported register @%x\n",
156 offset);
157 }
158 }
159
etm_detect_os_lock(struct etmv4_drvdata * drvdata,struct csdev_access * csa)160 static void etm_detect_os_lock(struct etmv4_drvdata *drvdata,
161 struct csdev_access *csa)
162 {
163 u32 oslsr = etm4x_relaxed_read32(csa, TRCOSLSR);
164
165 drvdata->os_lock_model = ETM_OSLSR_OSLM(oslsr);
166 }
167
etm_write_os_lock(struct etmv4_drvdata * drvdata,struct csdev_access * csa,u32 val)168 static void etm_write_os_lock(struct etmv4_drvdata *drvdata,
169 struct csdev_access *csa, u32 val)
170 {
171 val = !!val;
172
173 switch (drvdata->os_lock_model) {
174 case ETM_OSLOCK_PRESENT:
175 etm4x_relaxed_write32(csa, val, TRCOSLAR);
176 break;
177 case ETM_OSLOCK_PE:
178 write_sysreg_s(val, SYS_OSLAR_EL1);
179 break;
180 default:
181 pr_warn_once("CPU%d: Unsupported Trace OSLock model: %x\n",
182 smp_processor_id(), drvdata->os_lock_model);
183 fallthrough;
184 case ETM_OSLOCK_NI:
185 return;
186 }
187 isb();
188 }
189
etm4_os_unlock_csa(struct etmv4_drvdata * drvdata,struct csdev_access * csa)190 static void etm4_os_unlock_csa(struct etmv4_drvdata *drvdata,
191 struct csdev_access *csa)
192 {
193 WARN_ON(drvdata->cpu != smp_processor_id());
194
195 /* Writing 0 to OS Lock unlocks the trace unit registers */
196 etm_write_os_lock(drvdata, csa, 0x0);
197 drvdata->os_unlock = true;
198 }
199
etm4_os_unlock(struct etmv4_drvdata * drvdata)200 static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
201 {
202 if (!WARN_ON(!drvdata->csdev))
203 etm4_os_unlock_csa(drvdata, &drvdata->csdev->access);
204 }
205
etm4_os_lock(struct etmv4_drvdata * drvdata)206 static void etm4_os_lock(struct etmv4_drvdata *drvdata)
207 {
208 if (WARN_ON(!drvdata->csdev))
209 return;
210 /* Writing 0x1 to OS Lock locks the trace registers */
211 etm_write_os_lock(drvdata, &drvdata->csdev->access, 0x1);
212 drvdata->os_unlock = false;
213 }
214
etm4_cs_lock(struct etmv4_drvdata * drvdata,struct csdev_access * csa)215 static void etm4_cs_lock(struct etmv4_drvdata *drvdata,
216 struct csdev_access *csa)
217 {
218 /* Software Lock is only accessible via memory mapped interface */
219 if (csa->io_mem)
220 CS_LOCK(csa->base);
221 }
222
etm4_cs_unlock(struct etmv4_drvdata * drvdata,struct csdev_access * csa)223 static void etm4_cs_unlock(struct etmv4_drvdata *drvdata,
224 struct csdev_access *csa)
225 {
226 if (csa->io_mem)
227 CS_UNLOCK(csa->base);
228 }
229
etm4_cpu_id(struct coresight_device * csdev)230 static int etm4_cpu_id(struct coresight_device *csdev)
231 {
232 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
233
234 return drvdata->cpu;
235 }
236
etm4_release_trace_id(struct etmv4_drvdata * drvdata)237 void etm4_release_trace_id(struct etmv4_drvdata *drvdata)
238 {
239 coresight_trace_id_put_cpu_id(drvdata->cpu);
240 }
241
242 struct etm4_enable_arg {
243 struct etmv4_drvdata *drvdata;
244 int rc;
245 };
246
247 /*
248 * etm4x_prohibit_trace - Prohibit the CPU from tracing at all ELs.
249 * When the CPU supports FEAT_TRF, we could move the ETM to a trace
250 * prohibited state by filtering the Exception levels via TRFCR_EL1.
251 */
etm4x_prohibit_trace(struct etmv4_drvdata * drvdata)252 static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata)
253 {
254 u64 trfcr;
255
256 /* If the CPU doesn't support FEAT_TRF, nothing to do */
257 if (!drvdata->trfcr)
258 return;
259
260 trfcr = drvdata->trfcr & ~(TRFCR_EL1_ExTRE | TRFCR_EL1_E0TRE);
261
262 write_trfcr(trfcr);
263 kvm_tracing_set_el1_configuration(trfcr);
264 }
265
etm4x_get_kern_user_filter(struct etmv4_drvdata * drvdata)266 static u64 etm4x_get_kern_user_filter(struct etmv4_drvdata *drvdata)
267 {
268 u64 trfcr = drvdata->trfcr;
269
270 if (drvdata->config.mode & ETM_MODE_EXCL_KERN)
271 trfcr &= ~TRFCR_EL1_ExTRE;
272 if (drvdata->config.mode & ETM_MODE_EXCL_USER)
273 trfcr &= ~TRFCR_EL1_E0TRE;
274
275 return trfcr;
276 }
277
278 /*
279 * etm4x_allow_trace - Allow CPU tracing in the respective ELs,
280 * as configured by the drvdata->config.mode for the current
281 * session. Even though we have TRCVICTLR bits to filter the
282 * trace in the ELs, it doesn't prevent the ETM from generating
283 * a packet (e.g, TraceInfo) that might contain the addresses from
284 * the excluded levels. Thus we use the additional controls provided
285 * via the Trace Filtering controls (FEAT_TRF) to make sure no trace
286 * is generated for the excluded ELs.
287 */
etm4x_allow_trace(struct etmv4_drvdata * drvdata)288 static void etm4x_allow_trace(struct etmv4_drvdata *drvdata)
289 {
290 u64 trfcr, guest_trfcr;
291
292 /* If the CPU doesn't support FEAT_TRF, nothing to do */
293 if (!drvdata->trfcr)
294 return;
295
296 if (drvdata->config.mode & ETM_MODE_EXCL_HOST)
297 trfcr = drvdata->trfcr & ~(TRFCR_EL1_ExTRE | TRFCR_EL1_E0TRE);
298 else
299 trfcr = etm4x_get_kern_user_filter(drvdata);
300
301 write_trfcr(trfcr);
302
303 /* Set filters for guests and pass to KVM */
304 if (drvdata->config.mode & ETM_MODE_EXCL_GUEST)
305 guest_trfcr = drvdata->trfcr & ~(TRFCR_EL1_ExTRE | TRFCR_EL1_E0TRE);
306 else
307 guest_trfcr = etm4x_get_kern_user_filter(drvdata);
308
309 /* TRFCR_EL1 doesn't have CX so mask it out. */
310 guest_trfcr &= ~TRFCR_EL2_CX;
311 kvm_tracing_set_el1_configuration(guest_trfcr);
312 }
313
314 #ifdef CONFIG_ETM4X_IMPDEF_FEATURE
315
316 #define HISI_HIP08_AMBA_ID 0x000b6d01
317 #define ETM4_AMBA_MASK 0xfffff
318 #define HISI_HIP08_CORE_COMMIT_MASK 0x3000
319 #define HISI_HIP08_CORE_COMMIT_SHIFT 12
320 #define HISI_HIP08_CORE_COMMIT_FULL 0b00
321 #define HISI_HIP08_CORE_COMMIT_LVL_1 0b01
322 #define HISI_HIP08_CORE_COMMIT_REG sys_reg(3, 1, 15, 2, 5)
323
324 struct etm4_arch_features {
325 void (*arch_callback)(bool enable);
326 };
327
etm4_hisi_match_pid(unsigned int id)328 static bool etm4_hisi_match_pid(unsigned int id)
329 {
330 return (id & ETM4_AMBA_MASK) == HISI_HIP08_AMBA_ID;
331 }
332
etm4_hisi_config_core_commit(bool enable)333 static void etm4_hisi_config_core_commit(bool enable)
334 {
335 u8 commit = enable ? HISI_HIP08_CORE_COMMIT_LVL_1 :
336 HISI_HIP08_CORE_COMMIT_FULL;
337 u64 val;
338
339 /*
340 * bit 12 and 13 of HISI_HIP08_CORE_COMMIT_REG are used together
341 * to set core-commit, 2'b00 means cpu is at full speed, 2'b01,
342 * 2'b10, 2'b11 mean reduce pipeline speed, and 2'b01 means level-1
343 * speed(minimun value). So bit 12 and 13 should be cleared together.
344 */
345 val = read_sysreg_s(HISI_HIP08_CORE_COMMIT_REG);
346 val &= ~HISI_HIP08_CORE_COMMIT_MASK;
347 val |= commit << HISI_HIP08_CORE_COMMIT_SHIFT;
348 write_sysreg_s(val, HISI_HIP08_CORE_COMMIT_REG);
349 }
350
351 static struct etm4_arch_features etm4_features[] = {
352 [ETM4_IMPDEF_HISI_CORE_COMMIT] = {
353 .arch_callback = etm4_hisi_config_core_commit,
354 },
355 {},
356 };
357
etm4_enable_arch_specific(struct etmv4_drvdata * drvdata)358 static void etm4_enable_arch_specific(struct etmv4_drvdata *drvdata)
359 {
360 struct etm4_arch_features *ftr;
361 int bit;
362
363 for_each_set_bit(bit, drvdata->arch_features, ETM4_IMPDEF_FEATURE_MAX) {
364 ftr = &etm4_features[bit];
365
366 if (ftr->arch_callback)
367 ftr->arch_callback(true);
368 }
369 }
370
etm4_disable_arch_specific(struct etmv4_drvdata * drvdata)371 static void etm4_disable_arch_specific(struct etmv4_drvdata *drvdata)
372 {
373 struct etm4_arch_features *ftr;
374 int bit;
375
376 for_each_set_bit(bit, drvdata->arch_features, ETM4_IMPDEF_FEATURE_MAX) {
377 ftr = &etm4_features[bit];
378
379 if (ftr->arch_callback)
380 ftr->arch_callback(false);
381 }
382 }
383
etm4_check_arch_features(struct etmv4_drvdata * drvdata,struct csdev_access * csa)384 static void etm4_check_arch_features(struct etmv4_drvdata *drvdata,
385 struct csdev_access *csa)
386 {
387 /*
388 * TRCPIDR* registers are not required for ETMs with system
389 * instructions. They must be identified by the MIDR+REVIDRs.
390 * Skip the TRCPID checks for now.
391 */
392 if (!csa->io_mem)
393 return;
394
395 if (etm4_hisi_match_pid(coresight_get_pid(csa)))
396 set_bit(ETM4_IMPDEF_HISI_CORE_COMMIT, drvdata->arch_features);
397 }
398 #else
etm4_enable_arch_specific(struct etmv4_drvdata * drvdata)399 static void etm4_enable_arch_specific(struct etmv4_drvdata *drvdata)
400 {
401 }
402
etm4_disable_arch_specific(struct etmv4_drvdata * drvdata)403 static void etm4_disable_arch_specific(struct etmv4_drvdata *drvdata)
404 {
405 }
406
etm4_check_arch_features(struct etmv4_drvdata * drvdata,struct csdev_access * csa)407 static void etm4_check_arch_features(struct etmv4_drvdata *drvdata,
408 struct csdev_access *csa)
409 {
410 }
411 #endif /* CONFIG_ETM4X_IMPDEF_FEATURE */
412
etm4x_sys_ins_barrier(struct csdev_access * csa,u32 offset,int pos,int val)413 static void etm4x_sys_ins_barrier(struct csdev_access *csa, u32 offset, int pos, int val)
414 {
415 if (!csa->io_mem)
416 isb();
417 }
418
419 /*
420 * etm4x_wait_status: Poll for TRCSTATR.<pos> == <val>. While using system
421 * instruction to access the trace unit, each access must be separated by a
422 * synchronization barrier. See ARM IHI0064H.b section "4.3.7 Synchronization of
423 * register updates", for system instructions section, in "Notes":
424 *
425 * "In particular, whenever disabling or enabling the trace unit, a poll of
426 * TRCSTATR needs explicit synchronization between each read of TRCSTATR"
427 */
etm4x_wait_status(struct csdev_access * csa,int pos,int val)428 static int etm4x_wait_status(struct csdev_access *csa, int pos, int val)
429 {
430 if (!csa->io_mem)
431 return coresight_timeout_action(csa, TRCSTATR, pos, val,
432 etm4x_sys_ins_barrier);
433 return coresight_timeout(csa, TRCSTATR, pos, val);
434 }
435
etm4_enable_trace_unit(struct etmv4_drvdata * drvdata)436 static int etm4_enable_trace_unit(struct etmv4_drvdata *drvdata)
437 {
438 struct coresight_device *csdev = drvdata->csdev;
439 struct device *etm_dev = &csdev->dev;
440 struct csdev_access *csa = &csdev->access;
441
442 /*
443 * ETE mandates that the TRCRSR is written to before
444 * enabling it.
445 */
446 if (etm4x_is_ete(drvdata))
447 etm4x_relaxed_write32(csa, TRCRSR_TA, TRCRSR);
448
449 etm4x_allow_trace(drvdata);
450
451 /*
452 * According to software usage PKLXF in Arm ARM (ARM DDI 0487 L.a),
453 * execute a Context synchronization event to guarantee the trace unit
454 * will observe the new values of the System registers.
455 */
456 if (!csa->io_mem)
457 isb();
458
459 /* Enable the trace unit */
460 etm4x_relaxed_write32(csa, 1, TRCPRGCTLR);
461
462 /*
463 * As recommended by section 4.3.7 ("Synchronization when using system
464 * instructions to progrom the trace unit") of ARM IHI 0064H.b, the
465 * self-hosted trace analyzer must perform a Context synchronization
466 * event between writing to the TRCPRGCTLR and reading the TRCSTATR.
467 */
468 if (!csa->io_mem)
469 isb();
470
471 /* wait for TRCSTATR.IDLE to go back down to '0' */
472 if (etm4x_wait_status(csa, TRCSTATR_IDLE_BIT, 0)) {
473 dev_err(etm_dev,
474 "timeout while waiting for Idle Trace Status\n");
475 return -ETIME;
476 }
477
478 /*
479 * As recommended in section 4.3.7 (Synchronization of register updates)
480 * of ARM IHI 0064H.b, the self-hosted trace analyzer always executes an
481 * ISB instruction after programming the trace unit registers.
482 *
483 * For the memory-mapped interface, the registers are mapped as Device
484 * type (Device-nGnRE). Reading back the value of any register in the
485 * trace unit ensures that all writes have completed. Therefore, polling
486 * on TRCSTATR guarantees that the writing TRCPRGCTLR is complete, and
487 * no explicit dsb() is required at here.
488 */
489 isb();
490
491 return 0;
492 }
493
etm4_enable_hw(struct etmv4_drvdata * drvdata)494 static int etm4_enable_hw(struct etmv4_drvdata *drvdata)
495 {
496 int i, rc;
497 struct etmv4_config *config = &drvdata->config;
498 struct coresight_device *csdev = drvdata->csdev;
499 struct device *etm_dev = &csdev->dev;
500 struct csdev_access *csa = &csdev->access;
501
502
503 etm4_cs_unlock(drvdata, csa);
504 etm4_enable_arch_specific(drvdata);
505
506 etm4_os_unlock(drvdata);
507
508 rc = coresight_claim_device_unlocked(csdev);
509 if (rc)
510 goto done;
511
512 /* Disable the trace unit before programming trace registers */
513 etm4x_relaxed_write32(csa, 0, TRCPRGCTLR);
514
515 /*
516 * If we use system instructions, we need to synchronize the
517 * write to the TRCPRGCTLR, before accessing the TRCSTATR.
518 * See ARM IHI0064F, section
519 * "4.3.7 Synchronization of register updates"
520 */
521 if (!csa->io_mem)
522 isb();
523
524 /* wait for TRCSTATR.IDLE to go up */
525 if (etm4x_wait_status(csa, TRCSTATR_IDLE_BIT, 1))
526 dev_err(etm_dev,
527 "timeout while waiting for Idle Trace Status\n");
528 if (drvdata->nr_pe)
529 etm4x_relaxed_write32(csa, config->pe_sel, TRCPROCSELR);
530 etm4x_relaxed_write32(csa, config->cfg, TRCCONFIGR);
531 /* nothing specific implemented */
532 etm4x_relaxed_write32(csa, 0x0, TRCAUXCTLR);
533 etm4x_relaxed_write32(csa, config->eventctrl0, TRCEVENTCTL0R);
534 etm4x_relaxed_write32(csa, config->eventctrl1, TRCEVENTCTL1R);
535 if (drvdata->stallctl)
536 etm4x_relaxed_write32(csa, config->stall_ctrl, TRCSTALLCTLR);
537 etm4x_relaxed_write32(csa, config->ts_ctrl, TRCTSCTLR);
538 etm4x_relaxed_write32(csa, config->syncfreq, TRCSYNCPR);
539 etm4x_relaxed_write32(csa, config->ccctlr, TRCCCCTLR);
540 etm4x_relaxed_write32(csa, config->bb_ctrl, TRCBBCTLR);
541 etm4x_relaxed_write32(csa, drvdata->trcid, TRCTRACEIDR);
542 etm4x_relaxed_write32(csa, config->vinst_ctrl, TRCVICTLR);
543 etm4x_relaxed_write32(csa, config->viiectlr, TRCVIIECTLR);
544 etm4x_relaxed_write32(csa, config->vissctlr, TRCVISSCTLR);
545 if (drvdata->nr_pe_cmp)
546 etm4x_relaxed_write32(csa, config->vipcssctlr, TRCVIPCSSCTLR);
547 for (i = 0; i < drvdata->nrseqstate - 1; i++)
548 etm4x_relaxed_write32(csa, config->seq_ctrl[i], TRCSEQEVRn(i));
549 if (drvdata->nrseqstate) {
550 etm4x_relaxed_write32(csa, config->seq_rst, TRCSEQRSTEVR);
551 etm4x_relaxed_write32(csa, config->seq_state, TRCSEQSTR);
552 }
553 if (drvdata->numextinsel)
554 etm4x_relaxed_write32(csa, config->ext_inp, TRCEXTINSELR);
555 for (i = 0; i < drvdata->nr_cntr; i++) {
556 etm4x_relaxed_write32(csa, config->cntrldvr[i], TRCCNTRLDVRn(i));
557 etm4x_relaxed_write32(csa, config->cntr_ctrl[i], TRCCNTCTLRn(i));
558 etm4x_relaxed_write32(csa, config->cntr_val[i], TRCCNTVRn(i));
559 }
560
561 /*
562 * Resource selector pair 0 is always implemented and reserved. As
563 * such start at 2.
564 */
565 for (i = 2; i < drvdata->nr_resource * 2; i++)
566 etm4x_relaxed_write32(csa, config->res_ctrl[i], TRCRSCTLRn(i));
567
568 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
569 /* always clear status bit on restart if using single-shot */
570 if (config->ss_ctrl[i] || config->ss_pe_cmp[i])
571 config->ss_status[i] &= ~TRCSSCSRn_STATUS;
572 etm4x_relaxed_write32(csa, config->ss_ctrl[i], TRCSSCCRn(i));
573 etm4x_relaxed_write32(csa, config->ss_status[i], TRCSSCSRn(i));
574 if (etm4x_sspcicrn_present(drvdata, i))
575 etm4x_relaxed_write32(csa, config->ss_pe_cmp[i], TRCSSPCICRn(i));
576 }
577 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) {
578 etm4x_relaxed_write64(csa, config->addr_val[i], TRCACVRn(i));
579 etm4x_relaxed_write64(csa, config->addr_acc[i], TRCACATRn(i));
580 }
581 for (i = 0; i < drvdata->numcidc; i++)
582 etm4x_relaxed_write64(csa, config->ctxid_pid[i], TRCCIDCVRn(i));
583 etm4x_relaxed_write32(csa, config->ctxid_mask0, TRCCIDCCTLR0);
584 if (drvdata->numcidc > 4)
585 etm4x_relaxed_write32(csa, config->ctxid_mask1, TRCCIDCCTLR1);
586
587 for (i = 0; i < drvdata->numvmidc; i++)
588 etm4x_relaxed_write64(csa, config->vmid_val[i], TRCVMIDCVRn(i));
589 etm4x_relaxed_write32(csa, config->vmid_mask0, TRCVMIDCCTLR0);
590 if (drvdata->numvmidc > 4)
591 etm4x_relaxed_write32(csa, config->vmid_mask1, TRCVMIDCCTLR1);
592
593 if (!drvdata->skip_power_up) {
594 u32 trcpdcr = etm4x_relaxed_read32(csa, TRCPDCR);
595
596 /*
597 * Request to keep the trace unit powered and also
598 * emulation of powerdown
599 */
600 etm4x_relaxed_write32(csa, trcpdcr | TRCPDCR_PU, TRCPDCR);
601 }
602
603 if (!drvdata->paused)
604 rc = etm4_enable_trace_unit(drvdata);
605 done:
606 etm4_cs_lock(drvdata, csa);
607
608 dev_dbg(etm_dev, "cpu: %d enable smp call done: %d\n",
609 drvdata->cpu, rc);
610 return rc;
611 }
612
etm4_enable_sysfs_smp_call(void * info)613 static void etm4_enable_sysfs_smp_call(void *info)
614 {
615 struct etm4_enable_arg *arg = info;
616 struct coresight_device *csdev;
617
618 if (WARN_ON(!arg))
619 return;
620
621 csdev = arg->drvdata->csdev;
622 if (!coresight_take_mode(csdev, CS_MODE_SYSFS)) {
623 /* Someone is already using the tracer */
624 arg->rc = -EBUSY;
625 return;
626 }
627
628 arg->rc = etm4_enable_hw(arg->drvdata);
629
630 /* The tracer didn't start */
631 if (arg->rc)
632 coresight_set_mode(csdev, CS_MODE_DISABLED);
633 }
634
635 /*
636 * The goal of function etm4_config_timestamp_event() is to configure a
637 * counter that will tell the tracer to emit a timestamp packet when it
638 * reaches zero. This is done in order to get a more fine grained idea
639 * of when instructions are executed so that they can be correlated
640 * with execution on other CPUs.
641 *
642 * To do this the counter itself is configured to self reload and
643 * TRCRSCTLR1 (always true) used to get the counter to decrement. From
644 * there a resource selector is configured with the counter and the
645 * timestamp control register to use the resource selector to trigger the
646 * event that will insert a timestamp packet in the stream:
647 *
648 * +--------------+
649 * | Resource 1 | fixed "always-true" resource
650 * +--------------+
651 * |
652 * +------v-------+
653 * | Counter x | (reload to 2 ^ (ts_level - 1) on underflow)
654 * +--------------+
655 * |
656 * +------v--------------+
657 * | Resource Selector y | (trigger on counter x == 0)
658 * +---------------------+
659 * |
660 * +------v---------------+
661 * | Timestamp Generator | (timestamp on resource y)
662 * +----------------------+
663 */
etm4_config_timestamp_event(struct etmv4_drvdata * drvdata,u8 ts_level)664 static int etm4_config_timestamp_event(struct etmv4_drvdata *drvdata,
665 u8 ts_level)
666 {
667 int ctridx;
668 int rselector;
669 struct etmv4_config *config = &drvdata->config;
670
671 /* No point in trying if we don't have at least one counter */
672 if (!drvdata->nr_cntr)
673 return -EINVAL;
674
675 /* Find a counter that hasn't been initialised */
676 for (ctridx = 0; ctridx < drvdata->nr_cntr; ctridx++)
677 if (config->cntr_val[ctridx] == 0)
678 break;
679
680 /* All the counters have been configured already, bail out */
681 if (ctridx == drvdata->nr_cntr) {
682 pr_debug("%s: no available counter found\n", __func__);
683 return -ENOSPC;
684 }
685
686 /*
687 * Searching for an available resource selector to use, starting at '2'
688 * since resource 0 is the fixed 'always returns false' resource and 1
689 * is the fixed 'always returns true' resource. See IHI0064H_b '7.3.64
690 * TRCRSCTLRn, Resource Selection Control Registers, n=2-31'. If there
691 * are no resources, there would also be no counters so wouldn't get
692 * here.
693 *
694 * ETMIDR4 gives the number of resource selector _pairs_, hence multiply
695 * by 2.
696 */
697 for (rselector = 2; rselector < drvdata->nr_resource * 2; rselector++)
698 if (!config->res_ctrl[rselector])
699 break;
700
701 if (rselector == drvdata->nr_resource * 2) {
702 pr_debug("%s: no available resource selector found\n",
703 __func__);
704 return -ENOSPC;
705 }
706
707 /* Initialise original and reload counter value. */
708 config->cntr_val[ctridx] = config->cntrldvr[ctridx] = 1 << (ts_level - 1);
709
710 /*
711 * Trace Counter Control Register TRCCNTCTLRn
712 *
713 * CNTCHAIN = 0, don't reload on the previous counter
714 * RLDSELF = true, reload counter automatically on underflow
715 * RLDEVENT = RES_SEL_FALSE (0), reload on single false resource (never reload)
716 * CNTEVENT = RES_SEL_TRUE (1), count single fixed 'always true' resource (always decrement)
717 */
718 config->cntr_ctrl[ctridx] = TRCCNTCTLRn_RLDSELF |
719 FIELD_PREP(TRCCNTCTLRn_RLDEVENT_MASK,
720 etm4_res_sel_single(ETM4_RES_SEL_FALSE)) |
721 FIELD_PREP(TRCCNTCTLRn_CNTEVENT_MASK,
722 etm4_res_sel_single(ETM4_RES_SEL_TRUE));
723
724 /*
725 * Resource Selection Control Register TRCRSCTLRn
726 *
727 * PAIRINV = 0, INV = 0, don't invert
728 * GROUP = 2, SELECT = ctridx, trigger when counter 'ctridx' reaches 0
729 *
730 * Multiple counters can be selected, and each bit signifies a counter,
731 * so set bit 'ctridx' to select our counter.
732 */
733 config->res_ctrl[rselector] = FIELD_PREP(TRCRSCTLRn_GROUP_MASK, 2) |
734 FIELD_PREP(TRCRSCTLRn_SELECT_MASK, 1 << ctridx);
735
736 /*
737 * Global Timestamp Control Register TRCTSCTLR
738 *
739 * EVENT = generate timestamp on single resource 'rselector'
740 */
741 config->ts_ctrl = FIELD_PREP(TRCTSCTLR_EVENT_MASK,
742 etm4_res_sel_single(rselector));
743
744 return 0;
745 }
746
etm4_parse_event_config(struct coresight_device * csdev,struct perf_event * event)747 static int etm4_parse_event_config(struct coresight_device *csdev,
748 struct perf_event *event)
749 {
750 int ret = 0;
751 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
752 struct etmv4_config *config = &drvdata->config;
753 struct perf_event_attr max_timestamp = {
754 .ATTR_CFG_FLD_timestamp_CFG = U64_MAX,
755 };
756 struct perf_event_attr *attr = &event->attr;
757 unsigned long cfg_hash;
758 int preset, cc_threshold;
759 u8 ts_level;
760
761 /* Clear configuration from previous run */
762 memset(config, 0, sizeof(struct etmv4_config));
763
764 if (attr->exclude_kernel)
765 config->mode = ETM_MODE_EXCL_KERN;
766
767 if (attr->exclude_user)
768 config->mode = ETM_MODE_EXCL_USER;
769
770 if (attr->exclude_host)
771 config->mode |= ETM_MODE_EXCL_HOST;
772
773 if (attr->exclude_guest)
774 config->mode |= ETM_MODE_EXCL_GUEST;
775
776 /* Always start from the default config */
777 etm4_set_default_config(config);
778
779 /* Configure filters specified on the perf cmd line, if any. */
780 ret = etm4_set_event_filters(drvdata, event);
781 if (ret)
782 goto out;
783
784 /* Go from generic option to ETMv4 specifics */
785 if (ATTR_CFG_GET_FLD(attr, cycacc)) {
786 config->cfg |= TRCCONFIGR_CCI;
787 /* TRM: Must program this for cycacc to work */
788 cc_threshold = ATTR_CFG_GET_FLD(attr, cc_threshold);
789 if (!cc_threshold)
790 cc_threshold = ETM_CYC_THRESHOLD_DEFAULT;
791 if (cc_threshold < drvdata->ccitmin)
792 cc_threshold = drvdata->ccitmin;
793 config->ccctlr = cc_threshold;
794 }
795
796 ts_level = max(ATTR_CFG_GET_FLD(attr, timestamp),
797 ATTR_CFG_GET_FLD(attr, deprecated_timestamp));
798 if (ts_level) {
799 /*
800 * Don't do counter generated timestamps when ts_level == MAX.
801 * Leave only SYNC timestamps from TRCCONFIGR_TS.
802 */
803 if (ts_level != ATTR_CFG_GET_FLD(&max_timestamp, timestamp)) {
804 ret = etm4_config_timestamp_event(drvdata, ts_level);
805
806 /*
807 * Error if user asked for timestamps but there was no
808 * free counter.
809 */
810 if (ret)
811 goto out;
812 }
813
814 /* bit[11], Global timestamp tracing bit */
815 config->cfg |= TRCCONFIGR_TS;
816 }
817
818 /* Only trace contextID when runs in root PID namespace */
819 if (ATTR_CFG_GET_FLD(attr, contextid1) &&
820 task_is_in_init_pid_ns(current))
821 /* bit[6], Context ID tracing bit */
822 config->cfg |= TRCCONFIGR_CID;
823
824 /*
825 * If set bit contextid2 in perf config, this asks to trace VMID for
826 * recording CONTEXTIDR_EL2. Do not enable VMID tracing if the kernel
827 * is not running in EL2.
828 */
829 if (ATTR_CFG_GET_FLD(attr, contextid2)) {
830 if (!is_kernel_in_hyp_mode()) {
831 ret = -EINVAL;
832 goto out;
833 }
834 /* Only trace virtual contextID when runs in root PID namespace */
835 if (task_is_in_init_pid_ns(current))
836 config->cfg |= TRCCONFIGR_VMID | TRCCONFIGR_VMIDOPT;
837 }
838
839 /* return stack - enable if selected and supported */
840 if (ATTR_CFG_GET_FLD(attr, retstack) && drvdata->retstack)
841 /* bit[12], Return stack enable bit */
842 config->cfg |= TRCCONFIGR_RS;
843
844 /*
845 * Set any selected configuration and preset. A zero configid means no
846 * configuration active, preset = 0 means no preset selected.
847 */
848 cfg_hash = ATTR_CFG_GET_FLD(attr, configid);
849 if (cfg_hash) {
850 preset = ATTR_CFG_GET_FLD(attr, preset);
851 ret = cscfg_csdev_enable_active_config(csdev, cfg_hash, preset);
852 }
853
854 /* branch broadcast - enable if selected and supported */
855 if (ATTR_CFG_GET_FLD(attr, branch_broadcast)) {
856 if (!drvdata->trcbb) {
857 /*
858 * Missing BB support could cause silent decode errors
859 * so fail to open if it's not supported.
860 */
861 ret = -EINVAL;
862 goto out;
863 } else {
864 config->cfg |= TRCCONFIGR_BB;
865 }
866 }
867
868 out:
869 return ret;
870 }
871
etm4_enable_perf(struct coresight_device * csdev,struct perf_event * event,struct coresight_path * path)872 static int etm4_enable_perf(struct coresight_device *csdev,
873 struct perf_event *event,
874 struct coresight_path *path)
875 {
876 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
877 int ret;
878
879 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
880 return -EINVAL;
881
882 if (!coresight_take_mode(csdev, CS_MODE_PERF))
883 return -EBUSY;
884
885 /* Configure the tracer based on the session's specifics */
886 ret = etm4_parse_event_config(csdev, event);
887 if (ret)
888 goto out;
889
890 drvdata->trcid = path->trace_id;
891
892 /* Populate pause state */
893 drvdata->paused = !!READ_ONCE(event->hw.aux_paused);
894
895 /* And enable it */
896 ret = etm4_enable_hw(drvdata);
897
898 out:
899 /* Failed to start tracer; roll back to DISABLED mode */
900 if (ret)
901 coresight_set_mode(csdev, CS_MODE_DISABLED);
902 return ret;
903 }
904
etm4_enable_sysfs(struct coresight_device * csdev,struct coresight_path * path)905 static int etm4_enable_sysfs(struct coresight_device *csdev, struct coresight_path *path)
906 {
907 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
908 struct etm4_enable_arg arg = { };
909 unsigned long cfg_hash;
910 int ret, preset;
911
912 /* enable any config activated by configfs */
913 cscfg_config_sysfs_get_active_cfg(&cfg_hash, &preset);
914 if (cfg_hash) {
915 ret = cscfg_csdev_enable_active_config(csdev, cfg_hash, preset);
916 if (ret)
917 return ret;
918 }
919
920 raw_spin_lock(&drvdata->spinlock);
921
922 drvdata->trcid = path->trace_id;
923
924 /* Tracer will never be paused in sysfs mode */
925 drvdata->paused = false;
926
927 /*
928 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
929 * ensures that register writes occur when cpu is powered.
930 */
931 arg.drvdata = drvdata;
932 ret = smp_call_function_single(drvdata->cpu,
933 etm4_enable_sysfs_smp_call, &arg, 1);
934 if (!ret)
935 ret = arg.rc;
936 if (!ret)
937 drvdata->sticky_enable = true;
938
939 if (ret)
940 etm4_release_trace_id(drvdata);
941
942 raw_spin_unlock(&drvdata->spinlock);
943
944 if (!ret)
945 dev_dbg(&csdev->dev, "ETM tracing enabled\n");
946 return ret;
947 }
948
etm4_enable(struct coresight_device * csdev,struct perf_event * event,enum cs_mode mode,struct coresight_path * path)949 static int etm4_enable(struct coresight_device *csdev, struct perf_event *event,
950 enum cs_mode mode, struct coresight_path *path)
951 {
952 int ret;
953
954 switch (mode) {
955 case CS_MODE_SYSFS:
956 ret = etm4_enable_sysfs(csdev, path);
957 break;
958 case CS_MODE_PERF:
959 ret = etm4_enable_perf(csdev, event, path);
960 break;
961 default:
962 ret = -EINVAL;
963 }
964
965 return ret;
966 }
967
etm4_disable_trace_unit(struct etmv4_drvdata * drvdata)968 static void etm4_disable_trace_unit(struct etmv4_drvdata *drvdata)
969 {
970 u32 control;
971 struct coresight_device *csdev = drvdata->csdev;
972 struct device *etm_dev = &csdev->dev;
973 struct csdev_access *csa = &csdev->access;
974
975 control = etm4x_relaxed_read32(csa, TRCPRGCTLR);
976
977 /* EN, bit[0] Trace unit enable bit */
978 control &= ~0x1;
979
980 /*
981 * If the CPU supports v8.4 Trace filter Control,
982 * set the ETM to trace prohibited region.
983 */
984 etm4x_prohibit_trace(drvdata);
985 /*
986 * Prevent being speculative at the point of disabling the trace unit,
987 * as recommended by section 7.3.77 ("TRCVICTLR, ViewInst Main Control
988 * Register, SSTATUS") of ARM IHI 0064D
989 */
990 dsb(sy);
991 /*
992 * According to software usage VKHHY in Arm ARM (ARM DDI 0487 L.a),
993 * execute a Context synchronization event to guarantee no new
994 * program-flow trace is generated.
995 */
996 isb();
997 /* Trace synchronization barrier, is a nop if not supported */
998 tsb_csync();
999 etm4x_relaxed_write32(csa, control, TRCPRGCTLR);
1000
1001 /*
1002 * As recommended by section 4.3.7 ("Synchronization when using system
1003 * instructions to progrom the trace unit") of ARM IHI 0064H.b, the
1004 * self-hosted trace analyzer must perform a Context synchronization
1005 * event between writing to the TRCPRGCTLR and reading the TRCSTATR.
1006 */
1007 if (!csa->io_mem)
1008 isb();
1009
1010 /* wait for TRCSTATR.PMSTABLE to go to '1' */
1011 if (etm4x_wait_status(csa, TRCSTATR_PMSTABLE_BIT, 1))
1012 dev_err(etm_dev,
1013 "timeout while waiting for PM stable Trace Status\n");
1014 /*
1015 * As recommended in section 4.3.7 (Synchronization of register updates)
1016 * of ARM IHI 0064H.b, the self-hosted trace analyzer always executes an
1017 * ISB instruction after programming the trace unit registers.
1018 *
1019 * For the memory-mapped interface, the registers are mapped as Device
1020 * type (Device-nGnRE). Reading back the value of any register in the
1021 * trace unit ensures that all writes have completed. Therefore, polling
1022 * on TRCSTATR guarantees that the writing TRCPRGCTLR is complete, and
1023 * no explicit dsb() is required at here.
1024 */
1025 isb();
1026 }
1027
etm4_disable_hw(struct etmv4_drvdata * drvdata)1028 static void etm4_disable_hw(struct etmv4_drvdata *drvdata)
1029 {
1030 u32 control;
1031 struct etmv4_config *config = &drvdata->config;
1032 struct coresight_device *csdev = drvdata->csdev;
1033 struct csdev_access *csa = &csdev->access;
1034 int i;
1035
1036 etm4_cs_unlock(drvdata, csa);
1037 etm4_disable_arch_specific(drvdata);
1038
1039 if (!drvdata->skip_power_up) {
1040 /* power can be removed from the trace unit now */
1041 control = etm4x_relaxed_read32(csa, TRCPDCR);
1042 control &= ~TRCPDCR_PU;
1043 etm4x_relaxed_write32(csa, control, TRCPDCR);
1044 }
1045
1046 etm4_disable_trace_unit(drvdata);
1047
1048 /* read the status of the single shot comparators */
1049 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
1050 config->ss_status[i] =
1051 etm4x_relaxed_read32(csa, TRCSSCSRn(i));
1052 }
1053
1054 /* read back the current counter values */
1055 for (i = 0; i < drvdata->nr_cntr; i++) {
1056 config->cntr_val[i] =
1057 etm4x_relaxed_read32(csa, TRCCNTVRn(i));
1058 }
1059
1060 coresight_disclaim_device_unlocked(csdev);
1061 etm4_cs_lock(drvdata, csa);
1062
1063 dev_dbg(&drvdata->csdev->dev,
1064 "cpu: %d disable smp call done\n", drvdata->cpu);
1065 }
1066
etm4_disable_sysfs_smp_call(void * info)1067 static void etm4_disable_sysfs_smp_call(void *info)
1068 {
1069 struct etmv4_drvdata *drvdata = info;
1070
1071 etm4_disable_hw(drvdata);
1072
1073 coresight_set_mode(drvdata->csdev, CS_MODE_DISABLED);
1074 }
1075
etm4_disable_perf(struct coresight_device * csdev,struct perf_event * event)1076 static int etm4_disable_perf(struct coresight_device *csdev,
1077 struct perf_event *event)
1078 {
1079 u32 control;
1080 struct etm_filters *filters = event->hw.addr_filters;
1081 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1082 struct perf_event_attr *attr = &event->attr;
1083
1084 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
1085 return -EINVAL;
1086
1087 etm4_disable_hw(drvdata);
1088 /* If configid is non-zero then we will have enabled a config. */
1089 if (ATTR_CFG_GET_FLD(attr, configid))
1090 cscfg_csdev_disable_active_config(csdev);
1091
1092 /*
1093 * Check if the start/stop logic was active when the unit was stopped.
1094 * That way we can re-enable the start/stop logic when the process is
1095 * scheduled again. Configuration of the start/stop logic happens in
1096 * function etm4_set_event_filters().
1097 */
1098 control = etm4x_relaxed_read32(&csdev->access, TRCVICTLR);
1099 /* TRCVICTLR::SSSTATUS, bit[9] */
1100 filters->ssstatus = (control & BIT(9));
1101
1102 coresight_set_mode(drvdata->csdev, CS_MODE_DISABLED);
1103
1104 /*
1105 * perf will release trace ids when _free_aux() is
1106 * called at the end of the session.
1107 */
1108
1109 return 0;
1110 }
1111
etm4_disable_sysfs(struct coresight_device * csdev)1112 static void etm4_disable_sysfs(struct coresight_device *csdev)
1113 {
1114 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1115
1116 /*
1117 * Taking hotplug lock here protects from clocks getting disabled
1118 * with tracing being left on (crash scenario) if user disable occurs
1119 * after cpu online mask indicates the cpu is offline but before the
1120 * DYING hotplug callback is serviced by the ETM driver.
1121 */
1122 cpus_read_lock();
1123 raw_spin_lock(&drvdata->spinlock);
1124
1125 /*
1126 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
1127 * ensures that register writes occur when cpu is powered.
1128 */
1129 smp_call_function_single(drvdata->cpu, etm4_disable_sysfs_smp_call,
1130 drvdata, 1);
1131
1132 raw_spin_unlock(&drvdata->spinlock);
1133
1134 cscfg_csdev_disable_active_config(csdev);
1135
1136 cpus_read_unlock();
1137
1138 /*
1139 * we only release trace IDs when resetting sysfs.
1140 * This permits sysfs users to read the trace ID after the trace
1141 * session has completed. This maintains operational behaviour with
1142 * prior trace id allocation method
1143 */
1144
1145 dev_dbg(&csdev->dev, "ETM tracing disabled\n");
1146 }
1147
etm4_disable(struct coresight_device * csdev,struct perf_event * event)1148 static void etm4_disable(struct coresight_device *csdev,
1149 struct perf_event *event)
1150 {
1151 enum cs_mode mode;
1152
1153 /*
1154 * For as long as the tracer isn't disabled another entity can't
1155 * change its status. As such we can read the status here without
1156 * fearing it will change under us.
1157 */
1158 mode = coresight_get_mode(csdev);
1159
1160 switch (mode) {
1161 case CS_MODE_DISABLED:
1162 break;
1163 case CS_MODE_SYSFS:
1164 etm4_disable_sysfs(csdev);
1165 break;
1166 case CS_MODE_PERF:
1167 etm4_disable_perf(csdev, event);
1168 break;
1169 }
1170 }
1171
etm4_resume_perf(struct coresight_device * csdev)1172 static int etm4_resume_perf(struct coresight_device *csdev)
1173 {
1174 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1175 struct csdev_access *csa = &csdev->access;
1176
1177 if (coresight_get_mode(csdev) != CS_MODE_PERF)
1178 return -EINVAL;
1179
1180 etm4_cs_unlock(drvdata, csa);
1181 etm4_enable_trace_unit(drvdata);
1182 etm4_cs_lock(drvdata, csa);
1183
1184 drvdata->paused = false;
1185 return 0;
1186 }
1187
etm4_pause_perf(struct coresight_device * csdev)1188 static void etm4_pause_perf(struct coresight_device *csdev)
1189 {
1190 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
1191 struct csdev_access *csa = &csdev->access;
1192
1193 if (coresight_get_mode(csdev) != CS_MODE_PERF)
1194 return;
1195
1196 etm4_cs_unlock(drvdata, csa);
1197 etm4_disable_trace_unit(drvdata);
1198 etm4_cs_lock(drvdata, csa);
1199
1200 drvdata->paused = true;
1201 }
1202
1203 static const struct coresight_ops_source etm4_source_ops = {
1204 .cpu_id = etm4_cpu_id,
1205 .enable = etm4_enable,
1206 .disable = etm4_disable,
1207 .resume_perf = etm4_resume_perf,
1208 .pause_perf = etm4_pause_perf,
1209 };
1210
1211 static const struct coresight_ops etm4_cs_ops = {
1212 .trace_id = coresight_etm_get_trace_id,
1213 .source_ops = &etm4_source_ops,
1214 };
1215
cpu_supports_sysreg_trace(void)1216 static bool cpu_supports_sysreg_trace(void)
1217 {
1218 u64 dfr0 = read_sysreg_s(SYS_ID_AA64DFR0_EL1);
1219
1220 return ((dfr0 >> ID_AA64DFR0_EL1_TraceVer_SHIFT) & 0xfUL) > 0;
1221 }
1222
etm4_init_sysreg_access(struct etmv4_drvdata * drvdata,struct csdev_access * csa)1223 static bool etm4_init_sysreg_access(struct etmv4_drvdata *drvdata,
1224 struct csdev_access *csa)
1225 {
1226 u32 devarch;
1227
1228 if (!cpu_supports_sysreg_trace())
1229 return false;
1230
1231 /*
1232 * ETMs implementing sysreg access must implement TRCDEVARCH.
1233 */
1234 devarch = read_etm4x_sysreg_const_offset(TRCDEVARCH);
1235 switch (devarch & ETM_DEVARCH_ID_MASK) {
1236 case ETM_DEVARCH_ETMv4x_ARCH:
1237 *csa = (struct csdev_access) {
1238 .io_mem = false,
1239 .read = etm4x_sysreg_read,
1240 .write = etm4x_sysreg_write,
1241 };
1242 break;
1243 case ETM_DEVARCH_ETE_ARCH:
1244 *csa = (struct csdev_access) {
1245 .io_mem = false,
1246 .read = ete_sysreg_read,
1247 .write = ete_sysreg_write,
1248 };
1249 break;
1250 default:
1251 return false;
1252 }
1253
1254 drvdata->arch = etm_devarch_to_arch(devarch);
1255 return true;
1256 }
1257
is_devtype_cpu_trace(void __iomem * base)1258 static bool is_devtype_cpu_trace(void __iomem *base)
1259 {
1260 u32 devtype = readl(base + TRCDEVTYPE);
1261
1262 return (devtype == CS_DEVTYPE_PE_TRACE);
1263 }
1264
etm4_init_iomem_access(struct etmv4_drvdata * drvdata,struct csdev_access * csa)1265 static bool etm4_init_iomem_access(struct etmv4_drvdata *drvdata,
1266 struct csdev_access *csa)
1267 {
1268 u32 devarch = readl_relaxed(drvdata->base + TRCDEVARCH);
1269
1270 if (!is_coresight_device(drvdata->base) || !is_devtype_cpu_trace(drvdata->base))
1271 return false;
1272
1273 /*
1274 * All ETMs must implement TRCDEVARCH to indicate that
1275 * the component is an ETMv4. Even though TRCIDR1 also
1276 * contains the information, it is part of the "Trace"
1277 * register and must be accessed with the OSLK cleared,
1278 * with MMIO. But we cannot touch the OSLK until we are
1279 * sure this is an ETM. So rely only on the TRCDEVARCH.
1280 */
1281 if ((devarch & ETM_DEVARCH_ID_MASK) != ETM_DEVARCH_ETMv4x_ARCH) {
1282 pr_warn_once("TRCDEVARCH doesn't match ETMv4 architecture\n");
1283 return false;
1284 }
1285
1286 drvdata->arch = etm_devarch_to_arch(devarch);
1287 *csa = CSDEV_ACCESS_IOMEM(drvdata->base);
1288 return true;
1289 }
1290
etm4_init_csdev_access(struct etmv4_drvdata * drvdata,struct csdev_access * csa)1291 static bool etm4_init_csdev_access(struct etmv4_drvdata *drvdata,
1292 struct csdev_access *csa)
1293 {
1294 /*
1295 * Always choose the memory mapped io, if there is
1296 * a memory map to prevent sysreg access on broken
1297 * systems.
1298 */
1299 if (drvdata->base)
1300 return etm4_init_iomem_access(drvdata, csa);
1301
1302 if (etm4_init_sysreg_access(drvdata, csa))
1303 return true;
1304
1305 return false;
1306 }
1307
cpu_detect_trace_filtering(struct etmv4_drvdata * drvdata)1308 static void cpu_detect_trace_filtering(struct etmv4_drvdata *drvdata)
1309 {
1310 u64 dfr0 = read_sysreg(id_aa64dfr0_el1);
1311 u64 trfcr;
1312
1313 drvdata->trfcr = 0;
1314 if (!cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT))
1315 return;
1316
1317 /*
1318 * If the CPU supports v8.4 SelfHosted Tracing, enable
1319 * tracing at the kernel EL and EL0, forcing to use the
1320 * virtual time as the timestamp.
1321 */
1322 trfcr = (FIELD_PREP(TRFCR_EL1_TS_MASK, TRFCR_EL1_TS_VIRTUAL) |
1323 TRFCR_EL1_ExTRE |
1324 TRFCR_EL1_E0TRE);
1325
1326 /* If we are running at EL2, allow tracing the CONTEXTIDR_EL2. */
1327 if (is_kernel_in_hyp_mode())
1328 trfcr |= TRFCR_EL2_CX;
1329
1330 drvdata->trfcr = trfcr;
1331 }
1332
1333 /*
1334 * The following errata on applicable cpu ranges, affect the CCITMIN filed
1335 * in TCRIDR3 register. Software read for the field returns 0x100 limiting
1336 * the cycle threshold granularity, whereas the right value should have
1337 * been 0x4, which is well supported in the hardware.
1338 */
1339 static struct midr_range etm_wrong_ccitmin_cpus[] = {
1340 /* Erratum #1490853 - Cortex-A76 */
1341 MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 4, 0),
1342 /* Erratum #1490853 - Neoverse-N1 */
1343 MIDR_RANGE(MIDR_NEOVERSE_N1, 0, 0, 4, 0),
1344 /* Erratum #1491015 - Cortex-A77 */
1345 MIDR_RANGE(MIDR_CORTEX_A77, 0, 0, 1, 0),
1346 /* Erratum #1502854 - Cortex-X1 */
1347 MIDR_REV(MIDR_CORTEX_X1, 0, 0),
1348 /* Erratum #1619801 - Neoverse-V1 */
1349 MIDR_REV(MIDR_NEOVERSE_V1, 0, 0),
1350 {},
1351 };
1352
etm4_fixup_wrong_ccitmin(struct etmv4_drvdata * drvdata)1353 static void etm4_fixup_wrong_ccitmin(struct etmv4_drvdata *drvdata)
1354 {
1355 /*
1356 * Erratum affected cpus will read 256 as the minimum
1357 * instruction trace cycle counting threshold whereas
1358 * the correct value should be 4 instead. Override the
1359 * recorded value for 'drvdata->ccitmin' to workaround
1360 * this problem.
1361 */
1362 if (is_midr_in_range_list(etm_wrong_ccitmin_cpus)) {
1363 if (drvdata->ccitmin == 256)
1364 drvdata->ccitmin = 4;
1365 }
1366 }
1367
etm4_init_arch_data(void * info)1368 static void etm4_init_arch_data(void *info)
1369 {
1370 u32 etmidr0;
1371 u32 etmidr2;
1372 u32 etmidr3;
1373 u32 etmidr4;
1374 u32 etmidr5;
1375 struct etm4_init_arg *init_arg = info;
1376 struct etmv4_drvdata *drvdata;
1377 struct csdev_access *csa;
1378 struct device *dev = init_arg->dev;
1379 int i;
1380
1381 drvdata = dev_get_drvdata(init_arg->dev);
1382 csa = init_arg->csa;
1383
1384 /*
1385 * If we are unable to detect the access mechanism,
1386 * or unable to detect the trace unit type, fail
1387 * early.
1388 */
1389 if (!etm4_init_csdev_access(drvdata, csa))
1390 return;
1391
1392 if (!csa->io_mem ||
1393 fwnode_property_present(dev_fwnode(dev), "qcom,skip-power-up"))
1394 drvdata->skip_power_up = true;
1395
1396 /* Detect the support for OS Lock before we actually use it */
1397 etm_detect_os_lock(drvdata, csa);
1398
1399 /* Make sure all registers are accessible */
1400 etm4_os_unlock_csa(drvdata, csa);
1401 etm4_cs_unlock(drvdata, csa);
1402
1403 etm4_check_arch_features(drvdata, csa);
1404
1405 /* find all capabilities of the tracing unit */
1406 etmidr0 = etm4x_relaxed_read32(csa, TRCIDR0);
1407
1408 /* INSTP0, bits[2:1] P0 tracing support field */
1409 drvdata->instrp0 = !!(FIELD_GET(TRCIDR0_INSTP0_MASK, etmidr0) == 0b11);
1410 /* TRCBB, bit[5] Branch broadcast tracing support bit */
1411 drvdata->trcbb = !!(etmidr0 & TRCIDR0_TRCBB);
1412 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
1413 drvdata->trccond = !!(etmidr0 & TRCIDR0_TRCCOND);
1414 /* TRCCCI, bit[7] Cycle counting instruction bit */
1415 drvdata->trccci = !!(etmidr0 & TRCIDR0_TRCCCI);
1416 /* RETSTACK, bit[9] Return stack bit */
1417 drvdata->retstack = !!(etmidr0 & TRCIDR0_RETSTACK);
1418 /* NUMEVENT, bits[11:10] Number of events field */
1419 drvdata->nr_event = FIELD_GET(TRCIDR0_NUMEVENT_MASK, etmidr0);
1420 /* QSUPP, bits[16:15] Q element support field */
1421 drvdata->q_support = FIELD_GET(TRCIDR0_QSUPP_MASK, etmidr0);
1422 if (drvdata->q_support)
1423 drvdata->q_filt = !!(etmidr0 & TRCIDR0_QFILT);
1424 /* TSSIZE, bits[28:24] Global timestamp size field */
1425 drvdata->ts_size = FIELD_GET(TRCIDR0_TSSIZE_MASK, etmidr0);
1426
1427 /* maximum size of resources */
1428 etmidr2 = etm4x_relaxed_read32(csa, TRCIDR2);
1429 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
1430 drvdata->ctxid_size = FIELD_GET(TRCIDR2_CIDSIZE_MASK, etmidr2);
1431 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
1432 drvdata->vmid_size = FIELD_GET(TRCIDR2_VMIDSIZE_MASK, etmidr2);
1433 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
1434 drvdata->ccsize = FIELD_GET(TRCIDR2_CCSIZE_MASK, etmidr2);
1435
1436 etmidr3 = etm4x_relaxed_read32(csa, TRCIDR3);
1437 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
1438 drvdata->ccitmin = FIELD_GET(TRCIDR3_CCITMIN_MASK, etmidr3);
1439 etm4_fixup_wrong_ccitmin(drvdata);
1440
1441 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
1442 drvdata->s_ex_level = FIELD_GET(TRCIDR3_EXLEVEL_S_MASK, etmidr3);
1443 drvdata->config.s_ex_level = drvdata->s_ex_level;
1444 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
1445 drvdata->ns_ex_level = FIELD_GET(TRCIDR3_EXLEVEL_NS_MASK, etmidr3);
1446 /*
1447 * TRCERR, bit[24] whether a trace unit can trace a
1448 * system error exception.
1449 */
1450 drvdata->trc_error = !!(etmidr3 & TRCIDR3_TRCERR);
1451 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
1452 drvdata->syncpr = !!(etmidr3 & TRCIDR3_SYNCPR);
1453 /* STALLCTL, bit[26] is stall control implemented? */
1454 drvdata->stallctl = !!(etmidr3 & TRCIDR3_STALLCTL);
1455 /* SYSSTALL, bit[27] implementation can support stall control? */
1456 drvdata->sysstall = !!(etmidr3 & TRCIDR3_SYSSTALL);
1457 /*
1458 * NUMPROC - the number of PEs available for tracing, 5bits
1459 * = TRCIDR3.bits[13:12]bits[30:28]
1460 * bits[4:3] = TRCIDR3.bits[13:12] (since etm-v4.2, otherwise RES0)
1461 * bits[3:0] = TRCIDR3.bits[30:28]
1462 */
1463 drvdata->nr_pe = (FIELD_GET(TRCIDR3_NUMPROC_HI_MASK, etmidr3) << 3) |
1464 FIELD_GET(TRCIDR3_NUMPROC_LO_MASK, etmidr3);
1465 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
1466 drvdata->nooverflow = !!(etmidr3 & TRCIDR3_NOOVERFLOW);
1467
1468 /* number of resources trace unit supports */
1469 etmidr4 = etm4x_relaxed_read32(csa, TRCIDR4);
1470 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
1471 drvdata->nr_addr_cmp = FIELD_GET(TRCIDR4_NUMACPAIRS_MASK, etmidr4);
1472 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
1473 drvdata->nr_pe_cmp = FIELD_GET(TRCIDR4_NUMPC_MASK, etmidr4);
1474 /*
1475 * NUMRSPAIR, bits[19:16]
1476 * The number of resource pairs conveyed by the HW starts at 0, i.e a
1477 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
1478 * As such add 1 to the value of NUMRSPAIR for a better representation.
1479 *
1480 * For ETM v4.3 and later, 0x0 means 0, and no pairs are available -
1481 * the default TRUE and FALSE resource selectors are omitted.
1482 * Otherwise for values 0x1 and above the number is N + 1 as per v4.2.
1483 */
1484 drvdata->nr_resource = FIELD_GET(TRCIDR4_NUMRSPAIR_MASK, etmidr4);
1485 if ((drvdata->arch < ETM_ARCH_V4_3) || (drvdata->nr_resource > 0))
1486 drvdata->nr_resource += 1;
1487 /*
1488 * NUMSSCC, bits[23:20] the number of single-shot
1489 * comparator control for tracing. Read any status regs as these
1490 * also contain RO capability data.
1491 */
1492 drvdata->nr_ss_cmp = FIELD_GET(TRCIDR4_NUMSSCC_MASK, etmidr4);
1493 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
1494 drvdata->config.ss_status[i] =
1495 etm4x_relaxed_read32(csa, TRCSSCSRn(i));
1496 }
1497 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
1498 drvdata->numcidc = FIELD_GET(TRCIDR4_NUMCIDC_MASK, etmidr4);
1499 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
1500 drvdata->numvmidc = FIELD_GET(TRCIDR4_NUMVMIDC_MASK, etmidr4);
1501
1502 etmidr5 = etm4x_relaxed_read32(csa, TRCIDR5);
1503 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
1504 drvdata->nr_ext_inp = FIELD_GET(TRCIDR5_NUMEXTIN_MASK, etmidr5);
1505 drvdata->numextinsel = FIELD_GET(TRCIDR5_NUMEXTINSEL_MASK, etmidr5);
1506 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
1507 drvdata->trcid_size = FIELD_GET(TRCIDR5_TRACEIDSIZE_MASK, etmidr5);
1508 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
1509 drvdata->atbtrig = !!(etmidr5 & TRCIDR5_ATBTRIG);
1510 /*
1511 * LPOVERRIDE, bit[23] implementation supports
1512 * low-power state override
1513 */
1514 drvdata->lpoverride = (etmidr5 & TRCIDR5_LPOVERRIDE) && (!drvdata->skip_power_up);
1515 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
1516 drvdata->nrseqstate = FIELD_GET(TRCIDR5_NUMSEQSTATE_MASK, etmidr5);
1517 /* NUMCNTR, bits[30:28] number of counters available for tracing */
1518 drvdata->nr_cntr = FIELD_GET(TRCIDR5_NUMCNTR_MASK, etmidr5);
1519
1520 coresight_clear_self_claim_tag_unlocked(csa);
1521 etm4_cs_lock(drvdata, csa);
1522 cpu_detect_trace_filtering(drvdata);
1523 }
1524
etm4_get_victlr_access_type(struct etmv4_config * config)1525 static u32 etm4_get_victlr_access_type(struct etmv4_config *config)
1526 {
1527 return etm4_get_access_type(config) << __bf_shf(TRCVICTLR_EXLEVEL_MASK);
1528 }
1529
1530 /* Set ELx trace filter access in the TRCVICTLR register */
etm4_set_victlr_access(struct etmv4_config * config)1531 static void etm4_set_victlr_access(struct etmv4_config *config)
1532 {
1533 config->vinst_ctrl &= ~TRCVICTLR_EXLEVEL_MASK;
1534 config->vinst_ctrl |= etm4_get_victlr_access_type(config);
1535 }
1536
etm4_set_default_config(struct etmv4_config * config)1537 static void etm4_set_default_config(struct etmv4_config *config)
1538 {
1539 /* disable all events tracing */
1540 config->eventctrl0 = 0x0;
1541 config->eventctrl1 = 0x0;
1542
1543 /* disable stalling */
1544 config->stall_ctrl = 0x0;
1545
1546 /* enable trace synchronization every 4096 bytes, if available */
1547 config->syncfreq = 0xC;
1548
1549 /* disable timestamp event */
1550 config->ts_ctrl = 0x0;
1551
1552 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
1553 config->vinst_ctrl = FIELD_PREP(TRCVICTLR_EVENT_MASK, 0x01);
1554
1555 /* TRCVICTLR::EXLEVEL_NS:EXLEVELS: Set kernel / user filtering */
1556 etm4_set_victlr_access(config);
1557 }
1558
etm4_get_ns_access_type(struct etmv4_config * config)1559 static u64 etm4_get_ns_access_type(struct etmv4_config *config)
1560 {
1561 u64 access_type = 0;
1562
1563 /*
1564 * EXLEVEL_NS, for NonSecure Exception levels.
1565 * The mask here is a generic value and must be
1566 * shifted to the corresponding field for the registers
1567 */
1568 if (!is_kernel_in_hyp_mode()) {
1569 /* Stay away from hypervisor mode for non-VHE */
1570 access_type = ETM_EXLEVEL_NS_HYP;
1571 if (config->mode & ETM_MODE_EXCL_KERN)
1572 access_type |= ETM_EXLEVEL_NS_OS;
1573 } else if (config->mode & ETM_MODE_EXCL_KERN) {
1574 access_type = ETM_EXLEVEL_NS_HYP;
1575 }
1576
1577 if (config->mode & ETM_MODE_EXCL_USER)
1578 access_type |= ETM_EXLEVEL_NS_APP;
1579
1580 return access_type;
1581 }
1582
1583 /*
1584 * Construct the exception level masks for a given config.
1585 * This must be shifted to the corresponding register field
1586 * for usage.
1587 */
etm4_get_access_type(struct etmv4_config * config)1588 static u64 etm4_get_access_type(struct etmv4_config *config)
1589 {
1590 /* All Secure exception levels are excluded from the trace */
1591 return etm4_get_ns_access_type(config) | (u64)config->s_ex_level;
1592 }
1593
etm4_get_comparator_access_type(struct etmv4_config * config)1594 static u64 etm4_get_comparator_access_type(struct etmv4_config *config)
1595 {
1596 return etm4_get_access_type(config) << TRCACATR_EXLEVEL_SHIFT;
1597 }
1598
etm4_set_comparator_filter(struct etmv4_config * config,u64 start,u64 stop,int comparator)1599 static void etm4_set_comparator_filter(struct etmv4_config *config,
1600 u64 start, u64 stop, int comparator)
1601 {
1602 u64 access_type = etm4_get_comparator_access_type(config);
1603
1604 /* First half of default address comparator */
1605 config->addr_val[comparator] = start;
1606 config->addr_acc[comparator] = access_type;
1607 config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
1608
1609 /* Second half of default address comparator */
1610 config->addr_val[comparator + 1] = stop;
1611 config->addr_acc[comparator + 1] = access_type;
1612 config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
1613
1614 /*
1615 * Configure the ViewInst function to include this address range
1616 * comparator.
1617 *
1618 * @comparator is divided by two since it is the index in the
1619 * etmv4_config::addr_val array but register TRCVIIECTLR deals with
1620 * address range comparator _pairs_.
1621 *
1622 * Therefore:
1623 * index 0 -> compatator pair 0
1624 * index 2 -> comparator pair 1
1625 * index 4 -> comparator pair 2
1626 * ...
1627 * index 14 -> comparator pair 7
1628 */
1629 config->viiectlr |= BIT(comparator / 2);
1630 }
1631
etm4_set_start_stop_filter(struct etmv4_config * config,u64 address,int comparator,enum etm_addr_type type)1632 static void etm4_set_start_stop_filter(struct etmv4_config *config,
1633 u64 address, int comparator,
1634 enum etm_addr_type type)
1635 {
1636 int shift;
1637 u64 access_type = etm4_get_comparator_access_type(config);
1638
1639 /* Configure the comparator */
1640 config->addr_val[comparator] = address;
1641 config->addr_acc[comparator] = access_type;
1642 config->addr_type[comparator] = type;
1643
1644 /*
1645 * Configure ViewInst Start-Stop control register.
1646 * Addresses configured to start tracing go from bit 0 to n-1,
1647 * while those configured to stop tracing from 16 to 16 + n-1.
1648 */
1649 shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
1650 config->vissctlr |= BIT(shift + comparator);
1651 }
1652
etm4_set_default_filter(struct etmv4_config * config)1653 static void etm4_set_default_filter(struct etmv4_config *config)
1654 {
1655 /* Trace everything 'default' filter achieved by no filtering */
1656 config->viiectlr = 0x0;
1657
1658 /*
1659 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
1660 * in the started state
1661 */
1662 config->vinst_ctrl |= TRCVICTLR_SSSTATUS;
1663 config->mode |= ETM_MODE_VIEWINST_STARTSTOP;
1664
1665 /* No start-stop filtering for ViewInst */
1666 config->vissctlr = 0x0;
1667 }
1668
etm4_set_default(struct etmv4_config * config)1669 static void etm4_set_default(struct etmv4_config *config)
1670 {
1671 if (WARN_ON_ONCE(!config))
1672 return;
1673
1674 /*
1675 * Make default initialisation trace everything
1676 *
1677 * This is done by a minimum default config sufficient to enable
1678 * full instruction trace - with a default filter for trace all
1679 * achieved by having no filtering.
1680 */
1681 etm4_set_default_config(config);
1682 etm4_set_default_filter(config);
1683 }
1684
etm4_get_next_comparator(struct etmv4_drvdata * drvdata,u32 type)1685 static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
1686 {
1687 int nr_comparator, index = 0;
1688 struct etmv4_config *config = &drvdata->config;
1689
1690 /*
1691 * nr_addr_cmp holds the number of comparator _pair_, so time 2
1692 * for the total number of comparators.
1693 */
1694 nr_comparator = drvdata->nr_addr_cmp * 2;
1695
1696 /* Go through the tally of comparators looking for a free one. */
1697 while (index < nr_comparator) {
1698 switch (type) {
1699 case ETM_ADDR_TYPE_RANGE:
1700 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
1701 config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
1702 return index;
1703
1704 /* Address range comparators go in pairs */
1705 index += 2;
1706 break;
1707 case ETM_ADDR_TYPE_START:
1708 case ETM_ADDR_TYPE_STOP:
1709 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
1710 return index;
1711
1712 /* Start/stop address can have odd indexes */
1713 index += 1;
1714 break;
1715 default:
1716 return -EINVAL;
1717 }
1718 }
1719
1720 /* If we are here all the comparators have been used. */
1721 return -ENOSPC;
1722 }
1723
etm4_set_event_filters(struct etmv4_drvdata * drvdata,struct perf_event * event)1724 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
1725 struct perf_event *event)
1726 {
1727 int i, comparator, ret = 0;
1728 u64 address;
1729 struct etmv4_config *config = &drvdata->config;
1730 struct etm_filters *filters = event->hw.addr_filters;
1731
1732 if (!filters)
1733 goto default_filter;
1734
1735 /* Sync events with what Perf got */
1736 perf_event_addr_filters_sync(event);
1737
1738 /*
1739 * If there are no filters to deal with simply go ahead with
1740 * the default filter, i.e the entire address range.
1741 */
1742 if (!filters->nr_filters)
1743 goto default_filter;
1744
1745 for (i = 0; i < filters->nr_filters; i++) {
1746 struct etm_filter *filter = &filters->etm_filter[i];
1747 enum etm_addr_type type = filter->type;
1748
1749 /* See if a comparator is free. */
1750 comparator = etm4_get_next_comparator(drvdata, type);
1751 if (comparator < 0) {
1752 ret = comparator;
1753 goto out;
1754 }
1755
1756 switch (type) {
1757 case ETM_ADDR_TYPE_RANGE:
1758 etm4_set_comparator_filter(config,
1759 filter->start_addr,
1760 filter->stop_addr,
1761 comparator);
1762 /*
1763 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
1764 * in the started state
1765 */
1766 config->vinst_ctrl |= TRCVICTLR_SSSTATUS;
1767
1768 /* No start-stop filtering for ViewInst */
1769 config->vissctlr = 0x0;
1770 break;
1771 case ETM_ADDR_TYPE_START:
1772 case ETM_ADDR_TYPE_STOP:
1773 /* Get the right start or stop address */
1774 address = (type == ETM_ADDR_TYPE_START ?
1775 filter->start_addr :
1776 filter->stop_addr);
1777
1778 /* Configure comparator */
1779 etm4_set_start_stop_filter(config, address,
1780 comparator, type);
1781
1782 /*
1783 * If filters::ssstatus == 1, trace acquisition was
1784 * started but the process was yanked away before the
1785 * stop address was hit. As such the start/stop
1786 * logic needs to be re-started so that tracing can
1787 * resume where it left.
1788 *
1789 * The start/stop logic status when a process is
1790 * scheduled out is checked in function
1791 * etm4_disable_perf().
1792 */
1793 if (filters->ssstatus)
1794 config->vinst_ctrl |= TRCVICTLR_SSSTATUS;
1795
1796 /* No include/exclude filtering for ViewInst */
1797 config->viiectlr = 0x0;
1798 break;
1799 default:
1800 ret = -EINVAL;
1801 goto out;
1802 }
1803 }
1804
1805 goto out;
1806
1807
1808 default_filter:
1809 etm4_set_default_filter(config);
1810
1811 out:
1812 return ret;
1813 }
1814
etm4_config_trace_mode(struct etmv4_config * config)1815 void etm4_config_trace_mode(struct etmv4_config *config)
1816 {
1817 u32 mode;
1818
1819 mode = config->mode;
1820 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
1821
1822 /* excluding kernel AND user space doesn't make sense */
1823 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
1824
1825 /* nothing to do if neither flags are set */
1826 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
1827 return;
1828
1829 etm4_set_victlr_access(config);
1830 }
1831
etm4_online_cpu(unsigned int cpu)1832 static int etm4_online_cpu(unsigned int cpu)
1833 {
1834 if (!etmdrvdata[cpu])
1835 return etm4_probe_cpu(cpu);
1836
1837 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
1838 coresight_enable_sysfs(etmdrvdata[cpu]->csdev);
1839 return 0;
1840 }
1841
etm4_starting_cpu(unsigned int cpu)1842 static int etm4_starting_cpu(unsigned int cpu)
1843 {
1844 if (!etmdrvdata[cpu])
1845 return 0;
1846
1847 raw_spin_lock(&etmdrvdata[cpu]->spinlock);
1848 if (!etmdrvdata[cpu]->os_unlock)
1849 etm4_os_unlock(etmdrvdata[cpu]);
1850
1851 if (coresight_get_mode(etmdrvdata[cpu]->csdev))
1852 etm4_enable_hw(etmdrvdata[cpu]);
1853 raw_spin_unlock(&etmdrvdata[cpu]->spinlock);
1854 return 0;
1855 }
1856
etm4_dying_cpu(unsigned int cpu)1857 static int etm4_dying_cpu(unsigned int cpu)
1858 {
1859 if (!etmdrvdata[cpu])
1860 return 0;
1861
1862 raw_spin_lock(&etmdrvdata[cpu]->spinlock);
1863 if (coresight_get_mode(etmdrvdata[cpu]->csdev))
1864 etm4_disable_hw(etmdrvdata[cpu]);
1865 raw_spin_unlock(&etmdrvdata[cpu]->spinlock);
1866 return 0;
1867 }
1868
__etm4_cpu_save(struct etmv4_drvdata * drvdata)1869 static int __etm4_cpu_save(struct etmv4_drvdata *drvdata)
1870 {
1871 int i, ret = 0;
1872 struct etmv4_save_state *state;
1873 struct coresight_device *csdev = drvdata->csdev;
1874 struct csdev_access *csa;
1875 struct device *etm_dev;
1876
1877 if (WARN_ON(!csdev))
1878 return -ENODEV;
1879
1880 etm_dev = &csdev->dev;
1881 csa = &csdev->access;
1882
1883 /*
1884 * As recommended by 3.4.1 ("The procedure when powering down the PE")
1885 * of ARM IHI 0064D
1886 */
1887 dsb(sy);
1888 isb();
1889
1890 etm4_cs_unlock(drvdata, csa);
1891 /* Lock the OS lock to disable trace and external debugger access */
1892 etm4_os_lock(drvdata);
1893
1894 /* wait for TRCSTATR.PMSTABLE to go up */
1895 if (etm4x_wait_status(csa, TRCSTATR_PMSTABLE_BIT, 1)) {
1896 dev_err(etm_dev,
1897 "timeout while waiting for PM Stable Status\n");
1898 etm4_os_unlock(drvdata);
1899 ret = -EBUSY;
1900 goto out;
1901 }
1902
1903 if (!drvdata->paused)
1904 etm4_disable_trace_unit(drvdata);
1905
1906 state = drvdata->save_state;
1907
1908 if (drvdata->nr_pe)
1909 state->trcprocselr = etm4x_read32(csa, TRCPROCSELR);
1910 state->trcconfigr = etm4x_read32(csa, TRCCONFIGR);
1911 state->trcauxctlr = etm4x_read32(csa, TRCAUXCTLR);
1912 state->trceventctl0r = etm4x_read32(csa, TRCEVENTCTL0R);
1913 state->trceventctl1r = etm4x_read32(csa, TRCEVENTCTL1R);
1914 if (drvdata->stallctl)
1915 state->trcstallctlr = etm4x_read32(csa, TRCSTALLCTLR);
1916 state->trctsctlr = etm4x_read32(csa, TRCTSCTLR);
1917 state->trcsyncpr = etm4x_read32(csa, TRCSYNCPR);
1918 state->trcccctlr = etm4x_read32(csa, TRCCCCTLR);
1919 state->trcbbctlr = etm4x_read32(csa, TRCBBCTLR);
1920 state->trctraceidr = etm4x_read32(csa, TRCTRACEIDR);
1921 if (drvdata->q_filt)
1922 state->trcqctlr = etm4x_read32(csa, TRCQCTLR);
1923
1924 state->trcvictlr = etm4x_read32(csa, TRCVICTLR);
1925 state->trcviiectlr = etm4x_read32(csa, TRCVIIECTLR);
1926 state->trcvissctlr = etm4x_read32(csa, TRCVISSCTLR);
1927 if (drvdata->nr_pe_cmp)
1928 state->trcvipcssctlr = etm4x_read32(csa, TRCVIPCSSCTLR);
1929
1930 for (i = 0; i < drvdata->nrseqstate - 1; i++)
1931 state->trcseqevr[i] = etm4x_read32(csa, TRCSEQEVRn(i));
1932
1933 if (drvdata->nrseqstate) {
1934 state->trcseqrstevr = etm4x_read32(csa, TRCSEQRSTEVR);
1935 state->trcseqstr = etm4x_read32(csa, TRCSEQSTR);
1936 }
1937
1938 if (drvdata->numextinsel)
1939 state->trcextinselr = etm4x_read32(csa, TRCEXTINSELR);
1940
1941 for (i = 0; i < drvdata->nr_cntr; i++) {
1942 state->trccntrldvr[i] = etm4x_read32(csa, TRCCNTRLDVRn(i));
1943 state->trccntctlr[i] = etm4x_read32(csa, TRCCNTCTLRn(i));
1944 state->trccntvr[i] = etm4x_read32(csa, TRCCNTVRn(i));
1945 }
1946
1947 /* Resource selector pair 0 is reserved */
1948 for (i = 2; i < drvdata->nr_resource * 2; i++)
1949 state->trcrsctlr[i] = etm4x_read32(csa, TRCRSCTLRn(i));
1950
1951 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
1952 state->trcssccr[i] = etm4x_read32(csa, TRCSSCCRn(i));
1953 state->trcsscsr[i] = etm4x_read32(csa, TRCSSCSRn(i));
1954 if (etm4x_sspcicrn_present(drvdata, i))
1955 state->trcsspcicr[i] = etm4x_read32(csa, TRCSSPCICRn(i));
1956 }
1957
1958 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) {
1959 state->trcacvr[i] = etm4x_read64(csa, TRCACVRn(i));
1960 state->trcacatr[i] = etm4x_read64(csa, TRCACATRn(i));
1961 }
1962
1963 /*
1964 * Data trace stream is architecturally prohibited for A profile cores
1965 * so we don't save (or later restore) trcdvcvr and trcdvcmr - As per
1966 * section 1.3.4 ("Possible functional configurations of an ETMv4 trace
1967 * unit") of ARM IHI 0064D.
1968 */
1969
1970 for (i = 0; i < drvdata->numcidc; i++)
1971 state->trccidcvr[i] = etm4x_read64(csa, TRCCIDCVRn(i));
1972
1973 for (i = 0; i < drvdata->numvmidc; i++)
1974 state->trcvmidcvr[i] = etm4x_read64(csa, TRCVMIDCVRn(i));
1975
1976 state->trccidcctlr0 = etm4x_read32(csa, TRCCIDCCTLR0);
1977 if (drvdata->numcidc > 4)
1978 state->trccidcctlr1 = etm4x_read32(csa, TRCCIDCCTLR1);
1979
1980 state->trcvmidcctlr0 = etm4x_read32(csa, TRCVMIDCCTLR0);
1981 if (drvdata->numvmidc > 4)
1982 state->trcvmidcctlr0 = etm4x_read32(csa, TRCVMIDCCTLR1);
1983
1984 state->trcclaimset = etm4x_read32(csa, TRCCLAIMCLR);
1985
1986 if (!drvdata->skip_power_up)
1987 state->trcpdcr = etm4x_read32(csa, TRCPDCR);
1988
1989 /* wait for TRCSTATR.IDLE to go up */
1990 if (etm4x_wait_status(csa, TRCSTATR_IDLE_BIT, 1)) {
1991 dev_err(etm_dev,
1992 "timeout while waiting for Idle Trace Status\n");
1993 etm4_os_unlock(drvdata);
1994 ret = -EBUSY;
1995 goto out;
1996 }
1997
1998 /*
1999 * Power can be removed from the trace unit now. We do this to
2000 * potentially save power on systems that respect the TRCPDCR_PU
2001 * despite requesting software to save/restore state.
2002 */
2003 if (!drvdata->skip_power_up)
2004 etm4x_relaxed_write32(csa, (state->trcpdcr & ~TRCPDCR_PU),
2005 TRCPDCR);
2006 out:
2007 etm4_cs_lock(drvdata, csa);
2008 return ret;
2009 }
2010
etm4_cpu_save(struct etmv4_drvdata * drvdata)2011 static int etm4_cpu_save(struct etmv4_drvdata *drvdata)
2012 {
2013 int ret = 0;
2014
2015 if (pm_save_enable != PARAM_PM_SAVE_SELF_HOSTED)
2016 return 0;
2017
2018 /*
2019 * Save and restore the ETM Trace registers only if
2020 * the ETM is active.
2021 */
2022 if (coresight_get_mode(drvdata->csdev))
2023 ret = __etm4_cpu_save(drvdata);
2024 return ret;
2025 }
2026
__etm4_cpu_restore(struct etmv4_drvdata * drvdata)2027 static void __etm4_cpu_restore(struct etmv4_drvdata *drvdata)
2028 {
2029 int i;
2030 struct etmv4_save_state *state = drvdata->save_state;
2031 struct csdev_access *csa = &drvdata->csdev->access;
2032
2033 if (WARN_ON(!drvdata->csdev))
2034 return;
2035
2036 etm4_cs_unlock(drvdata, csa);
2037 etm4x_relaxed_write32(csa, state->trcclaimset, TRCCLAIMSET);
2038
2039 if (drvdata->nr_pe)
2040 etm4x_relaxed_write32(csa, state->trcprocselr, TRCPROCSELR);
2041 etm4x_relaxed_write32(csa, state->trcconfigr, TRCCONFIGR);
2042 etm4x_relaxed_write32(csa, state->trcauxctlr, TRCAUXCTLR);
2043 etm4x_relaxed_write32(csa, state->trceventctl0r, TRCEVENTCTL0R);
2044 etm4x_relaxed_write32(csa, state->trceventctl1r, TRCEVENTCTL1R);
2045 if (drvdata->stallctl)
2046 etm4x_relaxed_write32(csa, state->trcstallctlr, TRCSTALLCTLR);
2047 etm4x_relaxed_write32(csa, state->trctsctlr, TRCTSCTLR);
2048 etm4x_relaxed_write32(csa, state->trcsyncpr, TRCSYNCPR);
2049 etm4x_relaxed_write32(csa, state->trcccctlr, TRCCCCTLR);
2050 etm4x_relaxed_write32(csa, state->trcbbctlr, TRCBBCTLR);
2051 etm4x_relaxed_write32(csa, state->trctraceidr, TRCTRACEIDR);
2052 if (drvdata->q_filt)
2053 etm4x_relaxed_write32(csa, state->trcqctlr, TRCQCTLR);
2054
2055 etm4x_relaxed_write32(csa, state->trcvictlr, TRCVICTLR);
2056 etm4x_relaxed_write32(csa, state->trcviiectlr, TRCVIIECTLR);
2057 etm4x_relaxed_write32(csa, state->trcvissctlr, TRCVISSCTLR);
2058 if (drvdata->nr_pe_cmp)
2059 etm4x_relaxed_write32(csa, state->trcvipcssctlr, TRCVIPCSSCTLR);
2060
2061 for (i = 0; i < drvdata->nrseqstate - 1; i++)
2062 etm4x_relaxed_write32(csa, state->trcseqevr[i], TRCSEQEVRn(i));
2063
2064 if (drvdata->nrseqstate) {
2065 etm4x_relaxed_write32(csa, state->trcseqrstevr, TRCSEQRSTEVR);
2066 etm4x_relaxed_write32(csa, state->trcseqstr, TRCSEQSTR);
2067 }
2068 if (drvdata->numextinsel)
2069 etm4x_relaxed_write32(csa, state->trcextinselr, TRCEXTINSELR);
2070
2071 for (i = 0; i < drvdata->nr_cntr; i++) {
2072 etm4x_relaxed_write32(csa, state->trccntrldvr[i], TRCCNTRLDVRn(i));
2073 etm4x_relaxed_write32(csa, state->trccntctlr[i], TRCCNTCTLRn(i));
2074 etm4x_relaxed_write32(csa, state->trccntvr[i], TRCCNTVRn(i));
2075 }
2076
2077 /* Resource selector pair 0 is reserved */
2078 for (i = 2; i < drvdata->nr_resource * 2; i++)
2079 etm4x_relaxed_write32(csa, state->trcrsctlr[i], TRCRSCTLRn(i));
2080
2081 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
2082 etm4x_relaxed_write32(csa, state->trcssccr[i], TRCSSCCRn(i));
2083 etm4x_relaxed_write32(csa, state->trcsscsr[i], TRCSSCSRn(i));
2084 if (etm4x_sspcicrn_present(drvdata, i))
2085 etm4x_relaxed_write32(csa, state->trcsspcicr[i], TRCSSPCICRn(i));
2086 }
2087
2088 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) {
2089 etm4x_relaxed_write64(csa, state->trcacvr[i], TRCACVRn(i));
2090 etm4x_relaxed_write64(csa, state->trcacatr[i], TRCACATRn(i));
2091 }
2092
2093 for (i = 0; i < drvdata->numcidc; i++)
2094 etm4x_relaxed_write64(csa, state->trccidcvr[i], TRCCIDCVRn(i));
2095
2096 for (i = 0; i < drvdata->numvmidc; i++)
2097 etm4x_relaxed_write64(csa, state->trcvmidcvr[i], TRCVMIDCVRn(i));
2098
2099 etm4x_relaxed_write32(csa, state->trccidcctlr0, TRCCIDCCTLR0);
2100 if (drvdata->numcidc > 4)
2101 etm4x_relaxed_write32(csa, state->trccidcctlr1, TRCCIDCCTLR1);
2102
2103 etm4x_relaxed_write32(csa, state->trcvmidcctlr0, TRCVMIDCCTLR0);
2104 if (drvdata->numvmidc > 4)
2105 etm4x_relaxed_write32(csa, state->trcvmidcctlr0, TRCVMIDCCTLR1);
2106
2107 etm4x_relaxed_write32(csa, state->trcclaimset, TRCCLAIMSET);
2108
2109 if (!drvdata->skip_power_up)
2110 etm4x_relaxed_write32(csa, state->trcpdcr, TRCPDCR);
2111
2112 /*
2113 * As recommended by section 4.3.7 ("Synchronization when using the
2114 * memory-mapped interface") of ARM IHI 0064D
2115 */
2116 dsb(sy);
2117 isb();
2118
2119 /* Unlock the OS lock to re-enable trace and external debug access */
2120 etm4_os_unlock(drvdata);
2121
2122 if (!drvdata->paused)
2123 etm4_enable_trace_unit(drvdata);
2124
2125 etm4_cs_lock(drvdata, csa);
2126 }
2127
etm4_cpu_restore(struct etmv4_drvdata * drvdata)2128 static void etm4_cpu_restore(struct etmv4_drvdata *drvdata)
2129 {
2130 if (pm_save_enable != PARAM_PM_SAVE_SELF_HOSTED)
2131 return;
2132
2133 if (coresight_get_mode(drvdata->csdev))
2134 __etm4_cpu_restore(drvdata);
2135 }
2136
etm4_cpu_pm_notify(struct notifier_block * nb,unsigned long cmd,void * v)2137 static int etm4_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd,
2138 void *v)
2139 {
2140 struct etmv4_drvdata *drvdata;
2141 unsigned int cpu = smp_processor_id();
2142
2143 if (!etmdrvdata[cpu])
2144 return NOTIFY_OK;
2145
2146 drvdata = etmdrvdata[cpu];
2147
2148 if (WARN_ON_ONCE(drvdata->cpu != cpu))
2149 return NOTIFY_BAD;
2150
2151 switch (cmd) {
2152 case CPU_PM_ENTER:
2153 if (etm4_cpu_save(drvdata))
2154 return NOTIFY_BAD;
2155 break;
2156 case CPU_PM_EXIT:
2157 case CPU_PM_ENTER_FAILED:
2158 etm4_cpu_restore(drvdata);
2159 break;
2160 default:
2161 return NOTIFY_DONE;
2162 }
2163
2164 return NOTIFY_OK;
2165 }
2166
2167 static struct notifier_block etm4_cpu_pm_nb = {
2168 .notifier_call = etm4_cpu_pm_notify,
2169 };
2170
2171 /* Setup PM. Deals with error conditions and counts */
etm4_pm_setup(void)2172 static int __init etm4_pm_setup(void)
2173 {
2174 int ret;
2175
2176 ret = cpu_pm_register_notifier(&etm4_cpu_pm_nb);
2177 if (ret)
2178 return ret;
2179
2180 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING,
2181 "arm/coresight4:starting",
2182 etm4_starting_cpu, etm4_dying_cpu);
2183
2184 if (ret)
2185 goto unregister_notifier;
2186
2187 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
2188 "arm/coresight4:online",
2189 etm4_online_cpu, NULL);
2190
2191 /* HP dyn state ID returned in ret on success */
2192 if (ret > 0) {
2193 hp_online = ret;
2194 return 0;
2195 }
2196
2197 /* failed dyn state - remove others */
2198 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
2199
2200 unregister_notifier:
2201 cpu_pm_unregister_notifier(&etm4_cpu_pm_nb);
2202 return ret;
2203 }
2204
etm4_pm_clear(void)2205 static void etm4_pm_clear(void)
2206 {
2207 cpu_pm_unregister_notifier(&etm4_cpu_pm_nb);
2208 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
2209 if (hp_online) {
2210 cpuhp_remove_state_nocalls(hp_online);
2211 hp_online = 0;
2212 }
2213 }
2214
etm4_add_coresight_dev(struct etm4_init_arg * init_arg)2215 static int etm4_add_coresight_dev(struct etm4_init_arg *init_arg)
2216 {
2217 int ret;
2218 struct coresight_platform_data *pdata = NULL;
2219 struct device *dev = init_arg->dev;
2220 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev);
2221 struct coresight_desc desc = { 0 };
2222 u8 major, minor;
2223 char *type_name;
2224
2225 if (!drvdata)
2226 return -EINVAL;
2227
2228 desc.access = *init_arg->csa;
2229
2230 if (!drvdata->arch)
2231 return -EINVAL;
2232
2233 major = ETM_ARCH_MAJOR_VERSION(drvdata->arch);
2234 minor = ETM_ARCH_MINOR_VERSION(drvdata->arch);
2235
2236 if (etm4x_is_ete(drvdata)) {
2237 type_name = "ete";
2238 /* ETE v1 has major version == 0b101. Adjust this for logging.*/
2239 major -= 4;
2240 } else {
2241 type_name = "etm";
2242 }
2243
2244 desc.name = devm_kasprintf(dev, GFP_KERNEL,
2245 "%s%d", type_name, drvdata->cpu);
2246 if (!desc.name)
2247 return -ENOMEM;
2248
2249 etm4_set_default(&drvdata->config);
2250
2251 pdata = coresight_get_platform_data(dev);
2252 if (IS_ERR(pdata))
2253 return PTR_ERR(pdata);
2254
2255 dev->platform_data = pdata;
2256
2257 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
2258 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
2259 desc.ops = &etm4_cs_ops;
2260 desc.pdata = pdata;
2261 desc.dev = dev;
2262 desc.groups = coresight_etmv4_groups;
2263 drvdata->csdev = coresight_register(&desc);
2264 if (IS_ERR(drvdata->csdev))
2265 return PTR_ERR(drvdata->csdev);
2266
2267 ret = etm_perf_symlink(drvdata->csdev, true);
2268 if (ret) {
2269 coresight_unregister(drvdata->csdev);
2270 return ret;
2271 }
2272
2273 /* register with config infrastructure & load any current features */
2274 ret = etm4_cscfg_register(drvdata->csdev);
2275 if (ret) {
2276 coresight_unregister(drvdata->csdev);
2277 return ret;
2278 }
2279
2280 etmdrvdata[drvdata->cpu] = drvdata;
2281
2282 dev_info(&drvdata->csdev->dev, "CPU%d: %s v%d.%d initialized\n",
2283 drvdata->cpu, type_name, major, minor);
2284
2285 if (boot_enable) {
2286 coresight_enable_sysfs(drvdata->csdev);
2287 drvdata->boot_enable = true;
2288 }
2289
2290 return 0;
2291 }
2292
etm4_probe(struct device * dev)2293 static int etm4_probe(struct device *dev)
2294 {
2295 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev);
2296 struct csdev_access access = { 0 };
2297 struct etm4_init_arg init_arg = { 0 };
2298 struct etm4_init_arg *delayed;
2299 int ret;
2300
2301 if (WARN_ON(!drvdata))
2302 return -ENOMEM;
2303
2304 ret = coresight_get_enable_clocks(dev, &drvdata->pclk, &drvdata->atclk);
2305 if (ret)
2306 return ret;
2307
2308 if (pm_save_enable == PARAM_PM_SAVE_FIRMWARE)
2309 pm_save_enable = coresight_loses_context_with_cpu(dev) ?
2310 PARAM_PM_SAVE_SELF_HOSTED : PARAM_PM_SAVE_NEVER;
2311
2312 if (pm_save_enable != PARAM_PM_SAVE_NEVER) {
2313 drvdata->save_state = devm_kmalloc(dev,
2314 sizeof(struct etmv4_save_state), GFP_KERNEL);
2315 if (!drvdata->save_state)
2316 return -ENOMEM;
2317 }
2318
2319 raw_spin_lock_init(&drvdata->spinlock);
2320
2321 drvdata->cpu = coresight_get_cpu(dev);
2322 if (drvdata->cpu < 0)
2323 return drvdata->cpu;
2324
2325 init_arg.dev = dev;
2326 init_arg.csa = &access;
2327
2328 /*
2329 * Serialize against CPUHP callbacks to avoid race condition
2330 * between the smp call and saving the delayed probe.
2331 */
2332 cpus_read_lock();
2333 if (smp_call_function_single(drvdata->cpu,
2334 etm4_init_arch_data, &init_arg, 1)) {
2335 /* The CPU was offline, try again once it comes online. */
2336 delayed = devm_kmalloc(dev, sizeof(*delayed), GFP_KERNEL);
2337 if (!delayed) {
2338 cpus_read_unlock();
2339 return -ENOMEM;
2340 }
2341
2342 *delayed = init_arg;
2343
2344 per_cpu(delayed_probe, drvdata->cpu) = delayed;
2345
2346 cpus_read_unlock();
2347 return 0;
2348 }
2349 cpus_read_unlock();
2350
2351 return etm4_add_coresight_dev(&init_arg);
2352 }
2353
etm4_probe_amba(struct amba_device * adev,const struct amba_id * id)2354 static int etm4_probe_amba(struct amba_device *adev, const struct amba_id *id)
2355 {
2356 struct etmv4_drvdata *drvdata;
2357 void __iomem *base;
2358 struct device *dev = &adev->dev;
2359 struct resource *res = &adev->res;
2360 int ret;
2361
2362 /* Validity for the resource is already checked by the AMBA core */
2363 base = devm_ioremap_resource(dev, res);
2364 if (IS_ERR(base))
2365 return PTR_ERR(base);
2366
2367 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
2368 if (!drvdata)
2369 return -ENOMEM;
2370
2371 drvdata->base = base;
2372 dev_set_drvdata(dev, drvdata);
2373 ret = etm4_probe(dev);
2374 if (!ret)
2375 pm_runtime_put(&adev->dev);
2376
2377 return ret;
2378 }
2379
etm4_probe_platform_dev(struct platform_device * pdev)2380 static int etm4_probe_platform_dev(struct platform_device *pdev)
2381 {
2382 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2383 struct etmv4_drvdata *drvdata;
2384 int ret;
2385
2386 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL);
2387 if (!drvdata)
2388 return -ENOMEM;
2389
2390 if (res) {
2391 drvdata->base = devm_ioremap_resource(&pdev->dev, res);
2392 if (IS_ERR(drvdata->base))
2393 return PTR_ERR(drvdata->base);
2394 }
2395
2396 dev_set_drvdata(&pdev->dev, drvdata);
2397 pm_runtime_get_noresume(&pdev->dev);
2398 pm_runtime_set_active(&pdev->dev);
2399 pm_runtime_enable(&pdev->dev);
2400
2401 ret = etm4_probe(&pdev->dev);
2402
2403 pm_runtime_put(&pdev->dev);
2404 if (ret)
2405 pm_runtime_disable(&pdev->dev);
2406
2407 return ret;
2408 }
2409
etm4_probe_cpu(unsigned int cpu)2410 static int etm4_probe_cpu(unsigned int cpu)
2411 {
2412 int ret;
2413 struct etm4_init_arg init_arg;
2414 struct csdev_access access = { 0 };
2415 struct etm4_init_arg *iap = *this_cpu_ptr(&delayed_probe);
2416
2417 if (!iap)
2418 return 0;
2419
2420 init_arg = *iap;
2421 devm_kfree(init_arg.dev, iap);
2422 *this_cpu_ptr(&delayed_probe) = NULL;
2423
2424 ret = pm_runtime_resume_and_get(init_arg.dev);
2425 if (ret < 0) {
2426 dev_err(init_arg.dev, "Failed to get PM runtime!\n");
2427 return 0;
2428 }
2429
2430 init_arg.csa = &access;
2431 etm4_init_arch_data(&init_arg);
2432
2433 etm4_add_coresight_dev(&init_arg);
2434
2435 pm_runtime_put(init_arg.dev);
2436 return 0;
2437 }
2438
2439 static struct amba_cs_uci_id uci_id_etm4[] = {
2440 {
2441 /* ETMv4 UCI data */
2442 .devarch = ETM_DEVARCH_ETMv4x_ARCH,
2443 .devarch_mask = ETM_DEVARCH_ID_MASK,
2444 .devtype = CS_DEVTYPE_PE_TRACE,
2445 }
2446 };
2447
clear_etmdrvdata(void * info)2448 static void clear_etmdrvdata(void *info)
2449 {
2450 int cpu = *(int *)info;
2451
2452 etmdrvdata[cpu] = NULL;
2453 per_cpu(delayed_probe, cpu) = NULL;
2454 }
2455
etm4_remove_dev(struct etmv4_drvdata * drvdata)2456 static void etm4_remove_dev(struct etmv4_drvdata *drvdata)
2457 {
2458 bool had_delayed_probe;
2459 /*
2460 * Taking hotplug lock here to avoid racing between etm4_remove_dev()
2461 * and CPU hotplug call backs.
2462 */
2463 cpus_read_lock();
2464
2465 had_delayed_probe = per_cpu(delayed_probe, drvdata->cpu);
2466
2467 /*
2468 * The readers for etmdrvdata[] are CPU hotplug call backs
2469 * and PM notification call backs. Change etmdrvdata[i] on
2470 * CPU i ensures these call backs has consistent view
2471 * inside one call back function.
2472 */
2473 if (smp_call_function_single(drvdata->cpu, clear_etmdrvdata, &drvdata->cpu, 1))
2474 clear_etmdrvdata(&drvdata->cpu);
2475
2476 cpus_read_unlock();
2477
2478 if (!had_delayed_probe) {
2479 etm_perf_symlink(drvdata->csdev, false);
2480 cscfg_unregister_csdev(drvdata->csdev);
2481 coresight_unregister(drvdata->csdev);
2482 }
2483 }
2484
etm4_remove_amba(struct amba_device * adev)2485 static void etm4_remove_amba(struct amba_device *adev)
2486 {
2487 struct etmv4_drvdata *drvdata = dev_get_drvdata(&adev->dev);
2488
2489 if (drvdata)
2490 etm4_remove_dev(drvdata);
2491 }
2492
etm4_remove_platform_dev(struct platform_device * pdev)2493 static void etm4_remove_platform_dev(struct platform_device *pdev)
2494 {
2495 struct etmv4_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
2496
2497 if (drvdata)
2498 etm4_remove_dev(drvdata);
2499 pm_runtime_disable(&pdev->dev);
2500 }
2501
2502 static const struct amba_id etm4_ids[] = {
2503 CS_AMBA_ID(0x000bb95d), /* Cortex-A53 */
2504 CS_AMBA_ID(0x000bb95e), /* Cortex-A57 */
2505 CS_AMBA_ID(0x000bb95a), /* Cortex-A72 */
2506 CS_AMBA_ID(0x000bb959), /* Cortex-A73 */
2507 CS_AMBA_UCI_ID(0x000bb9da, uci_id_etm4),/* Cortex-A35 */
2508 CS_AMBA_UCI_ID(0x000bbd05, uci_id_etm4),/* Cortex-A55 */
2509 CS_AMBA_UCI_ID(0x000bbd0a, uci_id_etm4),/* Cortex-A75 */
2510 CS_AMBA_UCI_ID(0x000bbd0c, uci_id_etm4),/* Neoverse N1 */
2511 CS_AMBA_UCI_ID(0x000bbd41, uci_id_etm4),/* Cortex-A78 */
2512 CS_AMBA_UCI_ID(0x000f0205, uci_id_etm4),/* Qualcomm Kryo */
2513 CS_AMBA_UCI_ID(0x000f0211, uci_id_etm4),/* Qualcomm Kryo */
2514 CS_AMBA_UCI_ID(0x000bb802, uci_id_etm4),/* Qualcomm Kryo 385 Cortex-A55 */
2515 CS_AMBA_UCI_ID(0x000bb803, uci_id_etm4),/* Qualcomm Kryo 385 Cortex-A75 */
2516 CS_AMBA_UCI_ID(0x000bb805, uci_id_etm4),/* Qualcomm Kryo 4XX Cortex-A55 */
2517 CS_AMBA_UCI_ID(0x000bb804, uci_id_etm4),/* Qualcomm Kryo 4XX Cortex-A76 */
2518 CS_AMBA_UCI_ID(0x000bbd0d, uci_id_etm4),/* Qualcomm Kryo 5XX Cortex-A77 */
2519 CS_AMBA_UCI_ID(0x000cc0af, uci_id_etm4),/* Marvell ThunderX2 */
2520 CS_AMBA_UCI_ID(0x000b6d01, uci_id_etm4),/* HiSilicon-Hip08 */
2521 CS_AMBA_UCI_ID(0x000b6d02, uci_id_etm4),/* HiSilicon-Hip09 */
2522 /*
2523 * Match all PIDs with ETM4 DEVARCH. No need for adding any of the new
2524 * CPUs to the list here.
2525 */
2526 CS_AMBA_MATCH_ALL_UCI(uci_id_etm4),
2527 {},
2528 };
2529
2530 MODULE_DEVICE_TABLE(amba, etm4_ids);
2531
2532 static struct amba_driver etm4x_amba_driver = {
2533 .drv = {
2534 .name = "coresight-etm4x",
2535 .suppress_bind_attrs = true,
2536 },
2537 .probe = etm4_probe_amba,
2538 .remove = etm4_remove_amba,
2539 .id_table = etm4_ids,
2540 };
2541
2542 #ifdef CONFIG_PM
etm4_runtime_suspend(struct device * dev)2543 static int etm4_runtime_suspend(struct device *dev)
2544 {
2545 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev);
2546
2547 clk_disable_unprepare(drvdata->atclk);
2548 clk_disable_unprepare(drvdata->pclk);
2549
2550 return 0;
2551 }
2552
etm4_runtime_resume(struct device * dev)2553 static int etm4_runtime_resume(struct device *dev)
2554 {
2555 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev);
2556 int ret;
2557
2558 ret = clk_prepare_enable(drvdata->pclk);
2559 if (ret)
2560 return ret;
2561
2562 ret = clk_prepare_enable(drvdata->atclk);
2563 if (ret)
2564 clk_disable_unprepare(drvdata->pclk);
2565
2566 return ret;
2567 }
2568 #endif
2569
2570 static const struct dev_pm_ops etm4_dev_pm_ops = {
2571 SET_RUNTIME_PM_OPS(etm4_runtime_suspend, etm4_runtime_resume, NULL)
2572 };
2573
2574 static const struct of_device_id etm4_sysreg_match[] = {
2575 { .compatible = "arm,coresight-etm4x-sysreg" },
2576 { .compatible = "arm,embedded-trace-extension" },
2577 {}
2578 };
2579
2580 #ifdef CONFIG_ACPI
2581 static const struct acpi_device_id etm4x_acpi_ids[] = {
2582 {"ARMHC500", 0, 0, 0}, /* ARM CoreSight ETM4x */
2583 {}
2584 };
2585 MODULE_DEVICE_TABLE(acpi, etm4x_acpi_ids);
2586 #endif
2587
2588 static struct platform_driver etm4_platform_driver = {
2589 .probe = etm4_probe_platform_dev,
2590 .remove = etm4_remove_platform_dev,
2591 .driver = {
2592 .name = "coresight-etm4x",
2593 .of_match_table = etm4_sysreg_match,
2594 .acpi_match_table = ACPI_PTR(etm4x_acpi_ids),
2595 .suppress_bind_attrs = true,
2596 .pm = &etm4_dev_pm_ops,
2597 },
2598 };
2599
etm4x_init(void)2600 static int __init etm4x_init(void)
2601 {
2602 int ret;
2603
2604 ret = etm4_pm_setup();
2605
2606 /* etm4_pm_setup() does its own cleanup - exit on error */
2607 if (ret)
2608 return ret;
2609
2610 ret = amba_driver_register(&etm4x_amba_driver);
2611 if (ret) {
2612 pr_err("Error registering etm4x AMBA driver\n");
2613 goto clear_pm;
2614 }
2615
2616 ret = platform_driver_register(&etm4_platform_driver);
2617 if (!ret)
2618 return 0;
2619
2620 pr_err("Error registering etm4x platform driver\n");
2621 amba_driver_unregister(&etm4x_amba_driver);
2622
2623 clear_pm:
2624 etm4_pm_clear();
2625 return ret;
2626 }
2627
etm4x_exit(void)2628 static void __exit etm4x_exit(void)
2629 {
2630 amba_driver_unregister(&etm4x_amba_driver);
2631 platform_driver_unregister(&etm4_platform_driver);
2632 etm4_pm_clear();
2633 }
2634
2635 module_init(etm4x_init);
2636 module_exit(etm4x_exit);
2637
2638 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>");
2639 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>");
2640 MODULE_DESCRIPTION("Arm CoreSight Program Flow Trace v4.x driver");
2641 MODULE_LICENSE("GPL v2");
2642