1 /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/device.h>
19 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/smp.h>
24 #include <linux/sysfs.h>
25 #include <linux/stat.h>
26 #include <linux/clk.h>
27 #include <linux/cpu.h>
28 #include <linux/coresight.h>
29 #include <linux/coresight-pmu.h>
30 #include <linux/pm_wakeup.h>
31 #include <linux/amba/bus.h>
32 #include <linux/seq_file.h>
33 #include <linux/uaccess.h>
34 #include <linux/perf_event.h>
35 #include <linux/pm_runtime.h>
36 #include <asm/sections.h>
37 #include <asm/local.h>
39 #include "coresight-etm4x.h"
40 #include "coresight-etm-perf.h"
42 static int boot_enable
;
43 module_param_named(boot_enable
, boot_enable
, int, S_IRUGO
);
45 /* The number of ETMv4 currently registered */
46 static int etm4_count
;
47 static struct etmv4_drvdata
*etmdrvdata
[NR_CPUS
];
48 static void etm4_set_default(struct etmv4_config
*config
);
50 static enum cpuhp_state hp_online
;
52 static void etm4_os_unlock(struct etmv4_drvdata
*drvdata
)
54 /* Writing any value to ETMOSLAR unlocks the trace registers */
55 writel_relaxed(0x0, drvdata
->base
+ TRCOSLAR
);
56 drvdata
->os_unlock
= true;
60 static bool etm4_arch_supported(u8 arch
)
71 static int etm4_cpu_id(struct coresight_device
*csdev
)
73 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
78 static int etm4_trace_id(struct coresight_device
*csdev
)
80 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
82 return drvdata
->trcid
;
85 static void etm4_enable_hw(void *info
)
88 struct etmv4_drvdata
*drvdata
= info
;
89 struct etmv4_config
*config
= &drvdata
->config
;
91 CS_UNLOCK(drvdata
->base
);
93 etm4_os_unlock(drvdata
);
95 /* Disable the trace unit before programming trace registers */
96 writel_relaxed(0, drvdata
->base
+ TRCPRGCTLR
);
98 /* wait for TRCSTATR.IDLE to go up */
99 if (coresight_timeout(drvdata
->base
, TRCSTATR
, TRCSTATR_IDLE_BIT
, 1))
100 dev_err(drvdata
->dev
,
101 "timeout while waiting for Idle Trace Status\n");
103 writel_relaxed(config
->pe_sel
, drvdata
->base
+ TRCPROCSELR
);
104 writel_relaxed(config
->cfg
, drvdata
->base
+ TRCCONFIGR
);
105 /* nothing specific implemented */
106 writel_relaxed(0x0, drvdata
->base
+ TRCAUXCTLR
);
107 writel_relaxed(config
->eventctrl0
, drvdata
->base
+ TRCEVENTCTL0R
);
108 writel_relaxed(config
->eventctrl1
, drvdata
->base
+ TRCEVENTCTL1R
);
109 writel_relaxed(config
->stall_ctrl
, drvdata
->base
+ TRCSTALLCTLR
);
110 writel_relaxed(config
->ts_ctrl
, drvdata
->base
+ TRCTSCTLR
);
111 writel_relaxed(config
->syncfreq
, drvdata
->base
+ TRCSYNCPR
);
112 writel_relaxed(config
->ccctlr
, drvdata
->base
+ TRCCCCTLR
);
113 writel_relaxed(config
->bb_ctrl
, drvdata
->base
+ TRCBBCTLR
);
114 writel_relaxed(drvdata
->trcid
, drvdata
->base
+ TRCTRACEIDR
);
115 writel_relaxed(config
->vinst_ctrl
, drvdata
->base
+ TRCVICTLR
);
116 writel_relaxed(config
->viiectlr
, drvdata
->base
+ TRCVIIECTLR
);
117 writel_relaxed(config
->vissctlr
,
118 drvdata
->base
+ TRCVISSCTLR
);
119 writel_relaxed(config
->vipcssctlr
,
120 drvdata
->base
+ TRCVIPCSSCTLR
);
121 for (i
= 0; i
< drvdata
->nrseqstate
- 1; i
++)
122 writel_relaxed(config
->seq_ctrl
[i
],
123 drvdata
->base
+ TRCSEQEVRn(i
));
124 writel_relaxed(config
->seq_rst
, drvdata
->base
+ TRCSEQRSTEVR
);
125 writel_relaxed(config
->seq_state
, drvdata
->base
+ TRCSEQSTR
);
126 writel_relaxed(config
->ext_inp
, drvdata
->base
+ TRCEXTINSELR
);
127 for (i
= 0; i
< drvdata
->nr_cntr
; i
++) {
128 writel_relaxed(config
->cntrldvr
[i
],
129 drvdata
->base
+ TRCCNTRLDVRn(i
));
130 writel_relaxed(config
->cntr_ctrl
[i
],
131 drvdata
->base
+ TRCCNTCTLRn(i
));
132 writel_relaxed(config
->cntr_val
[i
],
133 drvdata
->base
+ TRCCNTVRn(i
));
136 /* Resource selector pair 0 is always implemented and reserved */
137 for (i
= 0; i
< drvdata
->nr_resource
* 2; i
++)
138 writel_relaxed(config
->res_ctrl
[i
],
139 drvdata
->base
+ TRCRSCTLRn(i
));
141 for (i
= 0; i
< drvdata
->nr_ss_cmp
; i
++) {
142 writel_relaxed(config
->ss_ctrl
[i
],
143 drvdata
->base
+ TRCSSCCRn(i
));
144 writel_relaxed(config
->ss_status
[i
],
145 drvdata
->base
+ TRCSSCSRn(i
));
146 writel_relaxed(config
->ss_pe_cmp
[i
],
147 drvdata
->base
+ TRCSSPCICRn(i
));
149 for (i
= 0; i
< drvdata
->nr_addr_cmp
; i
++) {
150 writeq_relaxed(config
->addr_val
[i
],
151 drvdata
->base
+ TRCACVRn(i
));
152 writeq_relaxed(config
->addr_acc
[i
],
153 drvdata
->base
+ TRCACATRn(i
));
155 for (i
= 0; i
< drvdata
->numcidc
; i
++)
156 writeq_relaxed(config
->ctxid_pid
[i
],
157 drvdata
->base
+ TRCCIDCVRn(i
));
158 writel_relaxed(config
->ctxid_mask0
, drvdata
->base
+ TRCCIDCCTLR0
);
159 writel_relaxed(config
->ctxid_mask1
, drvdata
->base
+ TRCCIDCCTLR1
);
161 for (i
= 0; i
< drvdata
->numvmidc
; i
++)
162 writeq_relaxed(config
->vmid_val
[i
],
163 drvdata
->base
+ TRCVMIDCVRn(i
));
164 writel_relaxed(config
->vmid_mask0
, drvdata
->base
+ TRCVMIDCCTLR0
);
165 writel_relaxed(config
->vmid_mask1
, drvdata
->base
+ TRCVMIDCCTLR1
);
168 * Request to keep the trace unit powered and also
169 * emulation of powerdown
171 writel_relaxed(readl_relaxed(drvdata
->base
+ TRCPDCR
) | TRCPDCR_PU
,
172 drvdata
->base
+ TRCPDCR
);
174 /* Enable the trace unit */
175 writel_relaxed(1, drvdata
->base
+ TRCPRGCTLR
);
177 /* wait for TRCSTATR.IDLE to go back down to '0' */
178 if (coresight_timeout(drvdata
->base
, TRCSTATR
, TRCSTATR_IDLE_BIT
, 0))
179 dev_err(drvdata
->dev
,
180 "timeout while waiting for Idle Trace Status\n");
182 CS_LOCK(drvdata
->base
);
184 dev_dbg(drvdata
->dev
, "cpu: %d enable smp call done\n", drvdata
->cpu
);
187 static int etm4_parse_event_config(struct etmv4_drvdata
*drvdata
,
188 struct perf_event_attr
*attr
)
190 struct etmv4_config
*config
= &drvdata
->config
;
195 /* Clear configuration from previous run */
196 memset(config
, 0, sizeof(struct etmv4_config
));
198 if (attr
->exclude_kernel
)
199 config
->mode
= ETM_MODE_EXCL_KERN
;
201 if (attr
->exclude_user
)
202 config
->mode
= ETM_MODE_EXCL_USER
;
204 /* Always start from the default config */
205 etm4_set_default(config
);
208 * By default the tracers are configured to trace the whole address
209 * range. Narrow the field only if requested by user space.
212 etm4_config_trace_mode(config
);
214 /* Go from generic option to ETMv4 specifics */
215 if (attr
->config
& BIT(ETM_OPT_CYCACC
))
216 config
->cfg
|= ETMv4_MODE_CYCACC
;
217 if (attr
->config
& BIT(ETM_OPT_TS
))
218 config
->cfg
|= ETMv4_MODE_TIMESTAMP
;
223 static int etm4_enable_perf(struct coresight_device
*csdev
,
224 struct perf_event_attr
*attr
)
226 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
228 if (WARN_ON_ONCE(drvdata
->cpu
!= smp_processor_id()))
231 /* Configure the tracer based on the session's specifics */
232 etm4_parse_event_config(drvdata
, attr
);
234 etm4_enable_hw(drvdata
);
239 static int etm4_enable_sysfs(struct coresight_device
*csdev
)
241 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
244 spin_lock(&drvdata
->spinlock
);
247 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
248 * ensures that register writes occur when cpu is powered.
250 ret
= smp_call_function_single(drvdata
->cpu
,
251 etm4_enable_hw
, drvdata
, 1);
255 drvdata
->sticky_enable
= true;
256 spin_unlock(&drvdata
->spinlock
);
258 dev_info(drvdata
->dev
, "ETM tracing enabled\n");
262 spin_unlock(&drvdata
->spinlock
);
266 static int etm4_enable(struct coresight_device
*csdev
,
267 struct perf_event_attr
*attr
, u32 mode
)
271 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
273 val
= local_cmpxchg(&drvdata
->mode
, CS_MODE_DISABLED
, mode
);
275 /* Someone is already using the tracer */
281 ret
= etm4_enable_sysfs(csdev
);
284 ret
= etm4_enable_perf(csdev
, attr
);
290 /* The tracer didn't start */
292 local_set(&drvdata
->mode
, CS_MODE_DISABLED
);
297 static void etm4_disable_hw(void *info
)
300 struct etmv4_drvdata
*drvdata
= info
;
302 CS_UNLOCK(drvdata
->base
);
304 /* power can be removed from the trace unit now */
305 control
= readl_relaxed(drvdata
->base
+ TRCPDCR
);
306 control
&= ~TRCPDCR_PU
;
307 writel_relaxed(control
, drvdata
->base
+ TRCPDCR
);
309 control
= readl_relaxed(drvdata
->base
+ TRCPRGCTLR
);
311 /* EN, bit[0] Trace unit enable bit */
314 /* make sure everything completes before disabling */
317 writel_relaxed(control
, drvdata
->base
+ TRCPRGCTLR
);
319 CS_LOCK(drvdata
->base
);
321 dev_dbg(drvdata
->dev
, "cpu: %d disable smp call done\n", drvdata
->cpu
);
324 static int etm4_disable_perf(struct coresight_device
*csdev
)
326 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
328 if (WARN_ON_ONCE(drvdata
->cpu
!= smp_processor_id()))
331 etm4_disable_hw(drvdata
);
335 static void etm4_disable_sysfs(struct coresight_device
*csdev
)
337 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
340 * Taking hotplug lock here protects from clocks getting disabled
341 * with tracing being left on (crash scenario) if user disable occurs
342 * after cpu online mask indicates the cpu is offline but before the
343 * DYING hotplug callback is serviced by the ETM driver.
346 spin_lock(&drvdata
->spinlock
);
349 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
350 * ensures that register writes occur when cpu is powered.
352 smp_call_function_single(drvdata
->cpu
, etm4_disable_hw
, drvdata
, 1);
354 spin_unlock(&drvdata
->spinlock
);
357 dev_info(drvdata
->dev
, "ETM tracing disabled\n");
360 static void etm4_disable(struct coresight_device
*csdev
)
363 struct etmv4_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
366 * For as long as the tracer isn't disabled another entity can't
367 * change its status. As such we can read the status here without
368 * fearing it will change under us.
370 mode
= local_read(&drvdata
->mode
);
373 case CS_MODE_DISABLED
:
376 etm4_disable_sysfs(csdev
);
379 etm4_disable_perf(csdev
);
384 local_set(&drvdata
->mode
, CS_MODE_DISABLED
);
387 static const struct coresight_ops_source etm4_source_ops
= {
388 .cpu_id
= etm4_cpu_id
,
389 .trace_id
= etm4_trace_id
,
390 .enable
= etm4_enable
,
391 .disable
= etm4_disable
,
394 static const struct coresight_ops etm4_cs_ops
= {
395 .source_ops
= &etm4_source_ops
,
398 static void etm4_init_arch_data(void *info
)
406 struct etmv4_drvdata
*drvdata
= info
;
408 /* Make sure all registers are accessible */
409 etm4_os_unlock(drvdata
);
411 CS_UNLOCK(drvdata
->base
);
413 /* find all capabilities of the tracing unit */
414 etmidr0
= readl_relaxed(drvdata
->base
+ TRCIDR0
);
416 /* INSTP0, bits[2:1] P0 tracing support field */
417 if (BMVAL(etmidr0
, 1, 1) && BMVAL(etmidr0
, 2, 2))
418 drvdata
->instrp0
= true;
420 drvdata
->instrp0
= false;
422 /* TRCBB, bit[5] Branch broadcast tracing support bit */
423 if (BMVAL(etmidr0
, 5, 5))
424 drvdata
->trcbb
= true;
426 drvdata
->trcbb
= false;
428 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
429 if (BMVAL(etmidr0
, 6, 6))
430 drvdata
->trccond
= true;
432 drvdata
->trccond
= false;
434 /* TRCCCI, bit[7] Cycle counting instruction bit */
435 if (BMVAL(etmidr0
, 7, 7))
436 drvdata
->trccci
= true;
438 drvdata
->trccci
= false;
440 /* RETSTACK, bit[9] Return stack bit */
441 if (BMVAL(etmidr0
, 9, 9))
442 drvdata
->retstack
= true;
444 drvdata
->retstack
= false;
446 /* NUMEVENT, bits[11:10] Number of events field */
447 drvdata
->nr_event
= BMVAL(etmidr0
, 10, 11);
448 /* QSUPP, bits[16:15] Q element support field */
449 drvdata
->q_support
= BMVAL(etmidr0
, 15, 16);
450 /* TSSIZE, bits[28:24] Global timestamp size field */
451 drvdata
->ts_size
= BMVAL(etmidr0
, 24, 28);
453 /* base architecture of trace unit */
454 etmidr1
= readl_relaxed(drvdata
->base
+ TRCIDR1
);
456 * TRCARCHMIN, bits[7:4] architecture the minor version number
457 * TRCARCHMAJ, bits[11:8] architecture major versin number
459 drvdata
->arch
= BMVAL(etmidr1
, 4, 11);
461 /* maximum size of resources */
462 etmidr2
= readl_relaxed(drvdata
->base
+ TRCIDR2
);
463 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
464 drvdata
->ctxid_size
= BMVAL(etmidr2
, 5, 9);
465 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
466 drvdata
->vmid_size
= BMVAL(etmidr2
, 10, 14);
467 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
468 drvdata
->ccsize
= BMVAL(etmidr2
, 25, 28);
470 etmidr3
= readl_relaxed(drvdata
->base
+ TRCIDR3
);
471 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
472 drvdata
->ccitmin
= BMVAL(etmidr3
, 0, 11);
473 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
474 drvdata
->s_ex_level
= BMVAL(etmidr3
, 16, 19);
475 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
476 drvdata
->ns_ex_level
= BMVAL(etmidr3
, 20, 23);
479 * TRCERR, bit[24] whether a trace unit can trace a
480 * system error exception.
482 if (BMVAL(etmidr3
, 24, 24))
483 drvdata
->trc_error
= true;
485 drvdata
->trc_error
= false;
487 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
488 if (BMVAL(etmidr3
, 25, 25))
489 drvdata
->syncpr
= true;
491 drvdata
->syncpr
= false;
493 /* STALLCTL, bit[26] is stall control implemented? */
494 if (BMVAL(etmidr3
, 26, 26))
495 drvdata
->stallctl
= true;
497 drvdata
->stallctl
= false;
499 /* SYSSTALL, bit[27] implementation can support stall control? */
500 if (BMVAL(etmidr3
, 27, 27))
501 drvdata
->sysstall
= true;
503 drvdata
->sysstall
= false;
505 /* NUMPROC, bits[30:28] the number of PEs available for tracing */
506 drvdata
->nr_pe
= BMVAL(etmidr3
, 28, 30);
508 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
509 if (BMVAL(etmidr3
, 31, 31))
510 drvdata
->nooverflow
= true;
512 drvdata
->nooverflow
= false;
514 /* number of resources trace unit supports */
515 etmidr4
= readl_relaxed(drvdata
->base
+ TRCIDR4
);
516 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
517 drvdata
->nr_addr_cmp
= BMVAL(etmidr4
, 0, 3);
518 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
519 drvdata
->nr_pe_cmp
= BMVAL(etmidr4
, 12, 15);
521 * NUMRSPAIR, bits[19:16]
522 * The number of resource pairs conveyed by the HW starts at 0, i.e a
523 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
524 * As such add 1 to the value of NUMRSPAIR for a better representation.
526 drvdata
->nr_resource
= BMVAL(etmidr4
, 16, 19) + 1;
528 * NUMSSCC, bits[23:20] the number of single-shot
529 * comparator control for tracing
531 drvdata
->nr_ss_cmp
= BMVAL(etmidr4
, 20, 23);
532 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
533 drvdata
->numcidc
= BMVAL(etmidr4
, 24, 27);
534 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
535 drvdata
->numvmidc
= BMVAL(etmidr4
, 28, 31);
537 etmidr5
= readl_relaxed(drvdata
->base
+ TRCIDR5
);
538 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
539 drvdata
->nr_ext_inp
= BMVAL(etmidr5
, 0, 8);
540 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
541 drvdata
->trcid_size
= BMVAL(etmidr5
, 16, 21);
542 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
543 if (BMVAL(etmidr5
, 22, 22))
544 drvdata
->atbtrig
= true;
546 drvdata
->atbtrig
= false;
548 * LPOVERRIDE, bit[23] implementation supports
549 * low-power state override
551 if (BMVAL(etmidr5
, 23, 23))
552 drvdata
->lpoverride
= true;
554 drvdata
->lpoverride
= false;
555 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
556 drvdata
->nrseqstate
= BMVAL(etmidr5
, 25, 27);
557 /* NUMCNTR, bits[30:28] number of counters available for tracing */
558 drvdata
->nr_cntr
= BMVAL(etmidr5
, 28, 30);
559 CS_LOCK(drvdata
->base
);
562 static void etm4_set_default(struct etmv4_config
*config
)
564 if (WARN_ON_ONCE(!config
))
568 * Make default initialisation trace everything
570 * Select the "always true" resource selector on the
571 * "Enablign Event" line and configure address range comparator
572 * '0' to trace all the possible address range. From there
573 * configure the "include/exclude" engine to include address
574 * range comparator '0'.
577 /* disable all events tracing */
578 config
->eventctrl0
= 0x0;
579 config
->eventctrl1
= 0x0;
581 /* disable stalling */
582 config
->stall_ctrl
= 0x0;
584 /* enable trace synchronization every 4096 bytes, if available */
585 config
->syncfreq
= 0xC;
587 /* disable timestamp event */
588 config
->ts_ctrl
= 0x0;
590 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
591 config
->vinst_ctrl
|= BIT(0);
594 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
595 * in the started state
597 config
->vinst_ctrl
|= BIT(9);
600 * Configure address range comparator '0' to encompass all
601 * possible addresses.
604 /* First half of default address comparator: start at address 0 */
605 config
->addr_val
[ETM_DEFAULT_ADDR_COMP
] = 0x0;
606 /* trace instruction addresses */
607 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
] &= ~(BIT(0) | BIT(1));
608 /* EXLEVEL_NS, bits[12:15], only trace application and kernel space */
609 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
] |= ETM_EXLEVEL_NS_HYP
;
610 /* EXLEVEL_S, bits[11:8], don't trace anything in secure state */
611 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
] |= (ETM_EXLEVEL_S_APP
|
614 config
->addr_type
[ETM_DEFAULT_ADDR_COMP
] = ETM_ADDR_TYPE_RANGE
;
617 * Second half of default address comparator: go all
618 * the way to the top.
620 config
->addr_val
[ETM_DEFAULT_ADDR_COMP
+ 1] = ~0x0;
621 /* trace instruction addresses */
622 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
+ 1] &= ~(BIT(0) | BIT(1));
623 /* Address comparator type must be equal for both halves */
624 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
+ 1] =
625 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
];
626 config
->addr_type
[ETM_DEFAULT_ADDR_COMP
+ 1] = ETM_ADDR_TYPE_RANGE
;
629 * Configure the ViewInst function to filter on address range
632 config
->viiectlr
= BIT(0);
634 /* no start-stop filtering for ViewInst */
635 config
->vissctlr
= 0x0;
638 void etm4_config_trace_mode(struct etmv4_config
*config
)
643 mode
&= (ETM_MODE_EXCL_KERN
| ETM_MODE_EXCL_USER
);
645 /* excluding kernel AND user space doesn't make sense */
646 WARN_ON_ONCE(mode
== (ETM_MODE_EXCL_KERN
| ETM_MODE_EXCL_USER
));
648 /* nothing to do if neither flags are set */
649 if (!(mode
& ETM_MODE_EXCL_KERN
) && !(mode
& ETM_MODE_EXCL_USER
))
652 addr_acc
= config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
];
653 /* clear default config */
654 addr_acc
&= ~(ETM_EXLEVEL_NS_APP
| ETM_EXLEVEL_NS_OS
);
657 * EXLEVEL_NS, bits[15:12]
658 * The Exception levels are:
659 * Bit[12] Exception level 0 - Application
660 * Bit[13] Exception level 1 - OS
661 * Bit[14] Exception level 2 - Hypervisor
662 * Bit[15] Never implemented
664 if (mode
& ETM_MODE_EXCL_KERN
)
665 addr_acc
|= ETM_EXLEVEL_NS_OS
;
667 addr_acc
|= ETM_EXLEVEL_NS_APP
;
669 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
] = addr_acc
;
670 config
->addr_acc
[ETM_DEFAULT_ADDR_COMP
+ 1] = addr_acc
;
673 static int etm4_online_cpu(unsigned int cpu
)
675 if (!etmdrvdata
[cpu
])
678 if (etmdrvdata
[cpu
]->boot_enable
&& !etmdrvdata
[cpu
]->sticky_enable
)
679 coresight_enable(etmdrvdata
[cpu
]->csdev
);
683 static int etm4_starting_cpu(unsigned int cpu
)
685 if (!etmdrvdata
[cpu
])
688 spin_lock(&etmdrvdata
[cpu
]->spinlock
);
689 if (!etmdrvdata
[cpu
]->os_unlock
) {
690 etm4_os_unlock(etmdrvdata
[cpu
]);
691 etmdrvdata
[cpu
]->os_unlock
= true;
694 if (local_read(&etmdrvdata
[cpu
]->mode
))
695 etm4_enable_hw(etmdrvdata
[cpu
]);
696 spin_unlock(&etmdrvdata
[cpu
]->spinlock
);
700 static int etm4_dying_cpu(unsigned int cpu
)
702 if (!etmdrvdata
[cpu
])
705 spin_lock(&etmdrvdata
[cpu
]->spinlock
);
706 if (local_read(&etmdrvdata
[cpu
]->mode
))
707 etm4_disable_hw(etmdrvdata
[cpu
]);
708 spin_unlock(&etmdrvdata
[cpu
]->spinlock
);
712 static void etm4_init_trace_id(struct etmv4_drvdata
*drvdata
)
714 drvdata
->trcid
= coresight_get_trace_id(drvdata
->cpu
);
717 static int etm4_probe(struct amba_device
*adev
, const struct amba_id
*id
)
721 struct device
*dev
= &adev
->dev
;
722 struct coresight_platform_data
*pdata
= NULL
;
723 struct etmv4_drvdata
*drvdata
;
724 struct resource
*res
= &adev
->res
;
725 struct coresight_desc desc
= { 0 };
726 struct device_node
*np
= adev
->dev
.of_node
;
728 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
733 pdata
= of_get_coresight_platform_data(dev
, np
);
735 return PTR_ERR(pdata
);
736 adev
->dev
.platform_data
= pdata
;
739 drvdata
->dev
= &adev
->dev
;
740 dev_set_drvdata(dev
, drvdata
);
742 /* Validity for the resource is already checked by the AMBA core */
743 base
= devm_ioremap_resource(dev
, res
);
745 return PTR_ERR(base
);
747 drvdata
->base
= base
;
749 spin_lock_init(&drvdata
->spinlock
);
751 drvdata
->cpu
= pdata
? pdata
->cpu
: 0;
754 etmdrvdata
[drvdata
->cpu
] = drvdata
;
756 if (smp_call_function_single(drvdata
->cpu
,
757 etm4_init_arch_data
, drvdata
, 1))
758 dev_err(dev
, "ETM arch init failed\n");
761 cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING
,
762 "AP_ARM_CORESIGHT4_STARTING",
763 etm4_starting_cpu
, etm4_dying_cpu
);
764 ret
= cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN
,
765 "AP_ARM_CORESIGHT4_ONLINE",
766 etm4_online_cpu
, NULL
);
768 goto err_arch_supported
;
774 if (etm4_arch_supported(drvdata
->arch
) == false) {
776 goto err_arch_supported
;
779 etm4_init_trace_id(drvdata
);
780 etm4_set_default(&drvdata
->config
);
782 desc
.type
= CORESIGHT_DEV_TYPE_SOURCE
;
783 desc
.subtype
.source_subtype
= CORESIGHT_DEV_SUBTYPE_SOURCE_PROC
;
784 desc
.ops
= &etm4_cs_ops
;
787 desc
.groups
= coresight_etmv4_groups
;
788 drvdata
->csdev
= coresight_register(&desc
);
789 if (IS_ERR(drvdata
->csdev
)) {
790 ret
= PTR_ERR(drvdata
->csdev
);
791 goto err_arch_supported
;
794 ret
= etm_perf_symlink(drvdata
->csdev
, true);
796 coresight_unregister(drvdata
->csdev
);
797 goto err_arch_supported
;
800 pm_runtime_put(&adev
->dev
);
801 dev_info(dev
, "%s initialized\n", (char *)id
->data
);
804 coresight_enable(drvdata
->csdev
);
805 drvdata
->boot_enable
= true;
811 if (--etm4_count
== 0) {
812 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING
);
814 cpuhp_remove_state_nocalls(hp_online
);
819 static struct amba_id etm4_ids
[] = {
820 { /* ETM 4.0 - Cortex-A53 */
825 { /* ETM 4.0 - Cortex-A57 */
830 { /* ETM 4.0 - A72, Maia, HiSilicon */
838 static struct amba_driver etm4x_driver
= {
840 .name
= "coresight-etm4x",
841 .suppress_bind_attrs
= true,
844 .id_table
= etm4_ids
,
846 builtin_amba_driver(etm4x_driver
);