1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3 * Description: CoreSight Program Flow Trace driver
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/kernel.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/device.h>
21 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/smp.h>
26 #include <linux/sysfs.h>
27 #include <linux/stat.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/cpu.h>
31 #include <linux/coresight.h>
32 #include <linux/coresight-pmu.h>
33 #include <linux/amba/bus.h>
34 #include <linux/seq_file.h>
35 #include <linux/uaccess.h>
36 #include <linux/clk.h>
37 #include <linux/perf_event.h>
38 #include <asm/sections.h>
40 #include "coresight-etm.h"
41 #include "coresight-etm-perf.h"
44 * Not really modular but using module_param is the easiest way to
45 * remain consistent with existing use cases for now.
47 static int boot_enable
;
48 module_param_named(boot_enable
, boot_enable
, int, S_IRUGO
);
50 /* The number of ETM/PTM currently registered */
52 static struct etm_drvdata
*etmdrvdata
[NR_CPUS
];
55 * Memory mapped writes to clear os lock are not supported on some processors
56 * and OS lock must be unlocked before any memory mapped access on such
57 * processors, otherwise memory mapped reads/writes will be invalid.
59 static void etm_os_unlock(struct etm_drvdata
*drvdata
)
61 /* Writing any value to ETMOSLAR unlocks the trace registers */
62 etm_writel(drvdata
, 0x0, ETMOSLAR
);
63 drvdata
->os_unlock
= true;
67 static void etm_set_pwrdwn(struct etm_drvdata
*drvdata
)
71 /* Ensure pending cp14 accesses complete before setting pwrdwn */
74 etmcr
= etm_readl(drvdata
, ETMCR
);
75 etmcr
|= ETMCR_PWD_DWN
;
76 etm_writel(drvdata
, etmcr
, ETMCR
);
79 static void etm_clr_pwrdwn(struct etm_drvdata
*drvdata
)
83 etmcr
= etm_readl(drvdata
, ETMCR
);
84 etmcr
&= ~ETMCR_PWD_DWN
;
85 etm_writel(drvdata
, etmcr
, ETMCR
);
86 /* Ensure pwrup completes before subsequent cp14 accesses */
91 static void etm_set_pwrup(struct etm_drvdata
*drvdata
)
95 etmpdcr
= readl_relaxed(drvdata
->base
+ ETMPDCR
);
96 etmpdcr
|= ETMPDCR_PWD_UP
;
97 writel_relaxed(etmpdcr
, drvdata
->base
+ ETMPDCR
);
98 /* Ensure pwrup completes before subsequent cp14 accesses */
103 static void etm_clr_pwrup(struct etm_drvdata
*drvdata
)
107 /* Ensure pending cp14 accesses complete before clearing pwrup */
110 etmpdcr
= readl_relaxed(drvdata
->base
+ ETMPDCR
);
111 etmpdcr
&= ~ETMPDCR_PWD_UP
;
112 writel_relaxed(etmpdcr
, drvdata
->base
+ ETMPDCR
);
116 * coresight_timeout_etm - loop until a bit has changed to a specific state.
117 * @drvdata: etm's private data structure.
118 * @offset: address of a register, starting from @addr.
119 * @position: the position of the bit of interest.
120 * @value: the value the bit should have.
122 * Basically the same as @coresight_timeout except for the register access
123 * method where we have to account for CP14 configurations.
125 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
126 * TIMEOUT_US has elapsed, which ever happens first.
129 static int coresight_timeout_etm(struct etm_drvdata
*drvdata
, u32 offset
,
130 int position
, int value
)
135 for (i
= TIMEOUT_US
; i
> 0; i
--) {
136 val
= etm_readl(drvdata
, offset
);
137 /* Waiting on the bit to go from 0 to 1 */
139 if (val
& BIT(position
))
141 /* Waiting on the bit to go from 1 to 0 */
143 if (!(val
& BIT(position
)))
148 * Delay is arbitrary - the specification doesn't say how long
149 * we are expected to wait. Extra check required to make sure
150 * we don't wait needlessly on the last iteration.
160 static void etm_set_prog(struct etm_drvdata
*drvdata
)
164 etmcr
= etm_readl(drvdata
, ETMCR
);
165 etmcr
|= ETMCR_ETM_PRG
;
166 etm_writel(drvdata
, etmcr
, ETMCR
);
168 * Recommended by spec for cp14 accesses to ensure etmcr write is
169 * complete before polling etmsr
172 if (coresight_timeout_etm(drvdata
, ETMSR
, ETMSR_PROG_BIT
, 1)) {
173 dev_err(drvdata
->dev
,
174 "%s: timeout observed when probing at offset %#x\n",
179 static void etm_clr_prog(struct etm_drvdata
*drvdata
)
183 etmcr
= etm_readl(drvdata
, ETMCR
);
184 etmcr
&= ~ETMCR_ETM_PRG
;
185 etm_writel(drvdata
, etmcr
, ETMCR
);
187 * Recommended by spec for cp14 accesses to ensure etmcr write is
188 * complete before polling etmsr
191 if (coresight_timeout_etm(drvdata
, ETMSR
, ETMSR_PROG_BIT
, 0)) {
192 dev_err(drvdata
->dev
,
193 "%s: timeout observed when probing at offset %#x\n",
198 void etm_set_default(struct etm_config
*config
)
202 if (WARN_ON_ONCE(!config
))
206 * Taken verbatim from the TRM:
208 * To trace all memory:
209 * set bit [24] in register 0x009, the ETMTECR1, to 1
210 * set all other bits in register 0x009, the ETMTECR1, to 0
211 * set all bits in register 0x007, the ETMTECR2, to 0
212 * set register 0x008, the ETMTEEVR, to 0x6F (TRUE).
214 config
->enable_ctrl1
= BIT(24);
215 config
->enable_ctrl2
= 0x0;
216 config
->enable_event
= ETM_HARD_WIRE_RES_A
;
218 config
->trigger_event
= ETM_DEFAULT_EVENT_VAL
;
219 config
->enable_event
= ETM_HARD_WIRE_RES_A
;
221 config
->seq_12_event
= ETM_DEFAULT_EVENT_VAL
;
222 config
->seq_21_event
= ETM_DEFAULT_EVENT_VAL
;
223 config
->seq_23_event
= ETM_DEFAULT_EVENT_VAL
;
224 config
->seq_31_event
= ETM_DEFAULT_EVENT_VAL
;
225 config
->seq_32_event
= ETM_DEFAULT_EVENT_VAL
;
226 config
->seq_13_event
= ETM_DEFAULT_EVENT_VAL
;
227 config
->timestamp_event
= ETM_DEFAULT_EVENT_VAL
;
229 for (i
= 0; i
< ETM_MAX_CNTR
; i
++) {
230 config
->cntr_rld_val
[i
] = 0x0;
231 config
->cntr_event
[i
] = ETM_DEFAULT_EVENT_VAL
;
232 config
->cntr_rld_event
[i
] = ETM_DEFAULT_EVENT_VAL
;
233 config
->cntr_val
[i
] = 0x0;
236 config
->seq_curr_state
= 0x0;
237 config
->ctxid_idx
= 0x0;
238 for (i
= 0; i
< ETM_MAX_CTXID_CMP
; i
++) {
239 config
->ctxid_pid
[i
] = 0x0;
240 config
->ctxid_vpid
[i
] = 0x0;
243 config
->ctxid_mask
= 0x0;
246 void etm_config_trace_mode(struct etm_config
*config
)
252 mode
&= (ETM_MODE_EXCL_KERN
| ETM_MODE_EXCL_USER
);
254 /* excluding kernel AND user space doesn't make sense */
255 if (mode
== (ETM_MODE_EXCL_KERN
| ETM_MODE_EXCL_USER
))
258 /* nothing to do if neither flags are set */
259 if (!(mode
& ETM_MODE_EXCL_KERN
) && !(mode
& ETM_MODE_EXCL_USER
))
262 flags
= (1 << 0 | /* instruction execute */
263 3 << 3 | /* ARM instruction */
264 0 << 5 | /* No data value comparison */
265 0 << 7 | /* No exact mach */
266 0 << 8); /* Ignore context ID */
268 /* No need to worry about single address comparators. */
269 config
->enable_ctrl2
= 0x0;
271 /* Bit 0 is address range comparator 1 */
272 config
->enable_ctrl1
= ETMTECR1_ADDR_COMP_1
;
276 * ETMACTRn[13,11] == Non-secure state comparison control
277 * ETMACTRn[12,10] == Secure state comparison control
279 * b00 == Match in all modes in this state
280 * b01 == Do not match in any more in this state
281 * b10 == Match in all modes excepts user mode in this state
282 * b11 == Match only in user mode in this state
285 /* Tracing in secure mode is not supported at this time */
286 flags
|= (0 << 12 | 1 << 10);
288 if (mode
& ETM_MODE_EXCL_USER
) {
289 /* exclude user, match all modes except user mode */
290 flags
|= (1 << 13 | 0 << 11);
292 /* exclude kernel, match only in user mode */
293 flags
|= (1 << 13 | 1 << 11);
297 * The ETMEEVR register is already set to "hard wire A". As such
298 * all there is to do is setup an address comparator that spans
299 * the entire address range and configure the state and mode bits.
301 config
->addr_val
[0] = (u32
) 0x0;
302 config
->addr_val
[1] = (u32
) ~0x0;
303 config
->addr_acctype
[0] = flags
;
304 config
->addr_acctype
[1] = flags
;
305 config
->addr_type
[0] = ETM_ADDR_TYPE_RANGE
;
306 config
->addr_type
[1] = ETM_ADDR_TYPE_RANGE
;
309 #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | ETMCR_TIMESTAMP_EN)
311 static int etm_parse_event_config(struct etm_drvdata
*drvdata
,
312 struct perf_event_attr
*attr
)
314 struct etm_config
*config
= &drvdata
->config
;
319 /* Clear configuration from previous run */
320 memset(config
, 0, sizeof(struct etm_config
));
322 if (attr
->exclude_kernel
)
323 config
->mode
= ETM_MODE_EXCL_KERN
;
325 if (attr
->exclude_user
)
326 config
->mode
= ETM_MODE_EXCL_USER
;
328 /* Always start from the default config */
329 etm_set_default(config
);
332 * By default the tracers are configured to trace the whole address
333 * range. Narrow the field only if requested by user space.
336 etm_config_trace_mode(config
);
339 * At this time only cycle accurate and timestamp options are
342 if (attr
->config
& ~ETM3X_SUPPORTED_OPTIONS
)
345 config
->ctrl
= attr
->config
;
350 static void etm_enable_hw(void *info
)
354 struct etm_drvdata
*drvdata
= info
;
355 struct etm_config
*config
= &drvdata
->config
;
357 CS_UNLOCK(drvdata
->base
);
360 etm_clr_pwrdwn(drvdata
);
361 /* Apply power to trace registers */
362 etm_set_pwrup(drvdata
);
363 /* Make sure all registers are accessible */
364 etm_os_unlock(drvdata
);
366 etm_set_prog(drvdata
);
368 etmcr
= etm_readl(drvdata
, ETMCR
);
369 /* Clear setting from a previous run if need be */
370 etmcr
&= ~ETM3X_SUPPORTED_OPTIONS
;
371 etmcr
|= drvdata
->port_size
;
372 etmcr
|= ETMCR_ETM_EN
;
373 etm_writel(drvdata
, config
->ctrl
| etmcr
, ETMCR
);
374 etm_writel(drvdata
, config
->trigger_event
, ETMTRIGGER
);
375 etm_writel(drvdata
, config
->startstop_ctrl
, ETMTSSCR
);
376 etm_writel(drvdata
, config
->enable_event
, ETMTEEVR
);
377 etm_writel(drvdata
, config
->enable_ctrl1
, ETMTECR1
);
378 etm_writel(drvdata
, config
->fifofull_level
, ETMFFLR
);
379 for (i
= 0; i
< drvdata
->nr_addr_cmp
; i
++) {
380 etm_writel(drvdata
, config
->addr_val
[i
], ETMACVRn(i
));
381 etm_writel(drvdata
, config
->addr_acctype
[i
], ETMACTRn(i
));
383 for (i
= 0; i
< drvdata
->nr_cntr
; i
++) {
384 etm_writel(drvdata
, config
->cntr_rld_val
[i
], ETMCNTRLDVRn(i
));
385 etm_writel(drvdata
, config
->cntr_event
[i
], ETMCNTENRn(i
));
386 etm_writel(drvdata
, config
->cntr_rld_event
[i
],
388 etm_writel(drvdata
, config
->cntr_val
[i
], ETMCNTVRn(i
));
390 etm_writel(drvdata
, config
->seq_12_event
, ETMSQ12EVR
);
391 etm_writel(drvdata
, config
->seq_21_event
, ETMSQ21EVR
);
392 etm_writel(drvdata
, config
->seq_23_event
, ETMSQ23EVR
);
393 etm_writel(drvdata
, config
->seq_31_event
, ETMSQ31EVR
);
394 etm_writel(drvdata
, config
->seq_32_event
, ETMSQ32EVR
);
395 etm_writel(drvdata
, config
->seq_13_event
, ETMSQ13EVR
);
396 etm_writel(drvdata
, config
->seq_curr_state
, ETMSQR
);
397 for (i
= 0; i
< drvdata
->nr_ext_out
; i
++)
398 etm_writel(drvdata
, ETM_DEFAULT_EVENT_VAL
, ETMEXTOUTEVRn(i
));
399 for (i
= 0; i
< drvdata
->nr_ctxid_cmp
; i
++)
400 etm_writel(drvdata
, config
->ctxid_pid
[i
], ETMCIDCVRn(i
));
401 etm_writel(drvdata
, config
->ctxid_mask
, ETMCIDCMR
);
402 etm_writel(drvdata
, config
->sync_freq
, ETMSYNCFR
);
403 /* No external input selected */
404 etm_writel(drvdata
, 0x0, ETMEXTINSELR
);
405 etm_writel(drvdata
, config
->timestamp_event
, ETMTSEVR
);
406 /* No auxiliary control selected */
407 etm_writel(drvdata
, 0x0, ETMAUXCR
);
408 etm_writel(drvdata
, drvdata
->traceid
, ETMTRACEIDR
);
409 /* No VMID comparator value selected */
410 etm_writel(drvdata
, 0x0, ETMVMIDCVR
);
412 etm_clr_prog(drvdata
);
413 CS_LOCK(drvdata
->base
);
415 dev_dbg(drvdata
->dev
, "cpu: %d enable smp call done\n", drvdata
->cpu
);
418 static int etm_cpu_id(struct coresight_device
*csdev
)
420 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
425 int etm_get_trace_id(struct etm_drvdata
*drvdata
)
433 if (!local_read(&drvdata
->mode
))
434 return drvdata
->traceid
;
436 pm_runtime_get_sync(drvdata
->dev
);
438 spin_lock_irqsave(&drvdata
->spinlock
, flags
);
440 CS_UNLOCK(drvdata
->base
);
441 trace_id
= (etm_readl(drvdata
, ETMTRACEIDR
) & ETM_TRACEID_MASK
);
442 CS_LOCK(drvdata
->base
);
444 spin_unlock_irqrestore(&drvdata
->spinlock
, flags
);
445 pm_runtime_put(drvdata
->dev
);
452 static int etm_trace_id(struct coresight_device
*csdev
)
454 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
456 return etm_get_trace_id(drvdata
);
459 static int etm_enable_perf(struct coresight_device
*csdev
,
460 struct perf_event_attr
*attr
)
462 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
464 if (WARN_ON_ONCE(drvdata
->cpu
!= smp_processor_id()))
467 /* Configure the tracer based on the session's specifics */
468 etm_parse_event_config(drvdata
, attr
);
470 etm_enable_hw(drvdata
);
475 static int etm_enable_sysfs(struct coresight_device
*csdev
)
477 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
480 spin_lock(&drvdata
->spinlock
);
483 * Configure the ETM only if the CPU is online. If it isn't online
484 * hw configuration will take place when 'CPU_STARTING' is received
485 * in @etm_cpu_callback.
487 if (cpu_online(drvdata
->cpu
)) {
488 ret
= smp_call_function_single(drvdata
->cpu
,
489 etm_enable_hw
, drvdata
, 1);
494 drvdata
->sticky_enable
= true;
495 spin_unlock(&drvdata
->spinlock
);
497 dev_info(drvdata
->dev
, "ETM tracing enabled\n");
501 spin_unlock(&drvdata
->spinlock
);
505 static int etm_enable(struct coresight_device
*csdev
,
506 struct perf_event_attr
*attr
, u32 mode
)
510 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
512 val
= local_cmpxchg(&drvdata
->mode
, CS_MODE_DISABLED
, mode
);
514 /* Someone is already using the tracer */
520 ret
= etm_enable_sysfs(csdev
);
523 ret
= etm_enable_perf(csdev
, attr
);
529 /* The tracer didn't start */
531 local_set(&drvdata
->mode
, CS_MODE_DISABLED
);
536 static void etm_disable_hw(void *info
)
539 struct etm_drvdata
*drvdata
= info
;
540 struct etm_config
*config
= &drvdata
->config
;
542 CS_UNLOCK(drvdata
->base
);
543 etm_set_prog(drvdata
);
545 /* Read back sequencer and counters for post trace analysis */
546 config
->seq_curr_state
= (etm_readl(drvdata
, ETMSQR
) & ETM_SQR_MASK
);
548 for (i
= 0; i
< drvdata
->nr_cntr
; i
++)
549 config
->cntr_val
[i
] = etm_readl(drvdata
, ETMCNTVRn(i
));
551 etm_set_pwrdwn(drvdata
);
552 CS_LOCK(drvdata
->base
);
554 dev_dbg(drvdata
->dev
, "cpu: %d disable smp call done\n", drvdata
->cpu
);
557 static void etm_disable_perf(struct coresight_device
*csdev
)
559 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
561 if (WARN_ON_ONCE(drvdata
->cpu
!= smp_processor_id()))
564 CS_UNLOCK(drvdata
->base
);
566 /* Setting the prog bit disables tracing immediately */
567 etm_set_prog(drvdata
);
570 * There is no way to know when the tracer will be used again so
571 * power down the tracer.
573 etm_set_pwrdwn(drvdata
);
575 CS_LOCK(drvdata
->base
);
578 static void etm_disable_sysfs(struct coresight_device
*csdev
)
580 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
583 * Taking hotplug lock here protects from clocks getting disabled
584 * with tracing being left on (crash scenario) if user disable occurs
585 * after cpu online mask indicates the cpu is offline but before the
586 * DYING hotplug callback is serviced by the ETM driver.
589 spin_lock(&drvdata
->spinlock
);
592 * Executing etm_disable_hw on the cpu whose ETM is being disabled
593 * ensures that register writes occur when cpu is powered.
595 smp_call_function_single(drvdata
->cpu
, etm_disable_hw
, drvdata
, 1);
597 spin_unlock(&drvdata
->spinlock
);
600 dev_info(drvdata
->dev
, "ETM tracing disabled\n");
603 static void etm_disable(struct coresight_device
*csdev
)
606 struct etm_drvdata
*drvdata
= dev_get_drvdata(csdev
->dev
.parent
);
609 * For as long as the tracer isn't disabled another entity can't
610 * change its status. As such we can read the status here without
611 * fearing it will change under us.
613 mode
= local_read(&drvdata
->mode
);
616 case CS_MODE_DISABLED
:
619 etm_disable_sysfs(csdev
);
622 etm_disable_perf(csdev
);
630 local_set(&drvdata
->mode
, CS_MODE_DISABLED
);
633 static const struct coresight_ops_source etm_source_ops
= {
634 .cpu_id
= etm_cpu_id
,
635 .trace_id
= etm_trace_id
,
636 .enable
= etm_enable
,
637 .disable
= etm_disable
,
640 static const struct coresight_ops etm_cs_ops
= {
641 .source_ops
= &etm_source_ops
,
644 static int etm_cpu_callback(struct notifier_block
*nfb
, unsigned long action
,
647 unsigned int cpu
= (unsigned long)hcpu
;
649 if (!etmdrvdata
[cpu
])
652 switch (action
& (~CPU_TASKS_FROZEN
)) {
654 spin_lock(&etmdrvdata
[cpu
]->spinlock
);
655 if (!etmdrvdata
[cpu
]->os_unlock
) {
656 etm_os_unlock(etmdrvdata
[cpu
]);
657 etmdrvdata
[cpu
]->os_unlock
= true;
660 if (local_read(&etmdrvdata
[cpu
]->mode
))
661 etm_enable_hw(etmdrvdata
[cpu
]);
662 spin_unlock(&etmdrvdata
[cpu
]->spinlock
);
666 if (etmdrvdata
[cpu
]->boot_enable
&&
667 !etmdrvdata
[cpu
]->sticky_enable
)
668 coresight_enable(etmdrvdata
[cpu
]->csdev
);
672 spin_lock(&etmdrvdata
[cpu
]->spinlock
);
673 if (local_read(&etmdrvdata
[cpu
]->mode
))
674 etm_disable_hw(etmdrvdata
[cpu
]);
675 spin_unlock(&etmdrvdata
[cpu
]->spinlock
);
682 static struct notifier_block etm_cpu_notifier
= {
683 .notifier_call
= etm_cpu_callback
,
686 static bool etm_arch_supported(u8 arch
)
703 static void etm_init_arch_data(void *info
)
707 struct etm_drvdata
*drvdata
= info
;
709 /* Make sure all registers are accessible */
710 etm_os_unlock(drvdata
);
712 CS_UNLOCK(drvdata
->base
);
714 /* First dummy read */
715 (void)etm_readl(drvdata
, ETMPDSR
);
716 /* Provide power to ETM: ETMPDCR[3] == 1 */
717 etm_set_pwrup(drvdata
);
719 * Clear power down bit since when this bit is set writes to
720 * certain registers might be ignored.
722 etm_clr_pwrdwn(drvdata
);
724 * Set prog bit. It will be set from reset but this is included to
727 etm_set_prog(drvdata
);
729 /* Find all capabilities */
730 etmidr
= etm_readl(drvdata
, ETMIDR
);
731 drvdata
->arch
= BMVAL(etmidr
, 4, 11);
732 drvdata
->port_size
= etm_readl(drvdata
, ETMCR
) & PORT_SIZE_MASK
;
734 drvdata
->etmccer
= etm_readl(drvdata
, ETMCCER
);
735 etmccr
= etm_readl(drvdata
, ETMCCR
);
736 drvdata
->etmccr
= etmccr
;
737 drvdata
->nr_addr_cmp
= BMVAL(etmccr
, 0, 3) * 2;
738 drvdata
->nr_cntr
= BMVAL(etmccr
, 13, 15);
739 drvdata
->nr_ext_inp
= BMVAL(etmccr
, 17, 19);
740 drvdata
->nr_ext_out
= BMVAL(etmccr
, 20, 22);
741 drvdata
->nr_ctxid_cmp
= BMVAL(etmccr
, 24, 25);
743 etm_set_pwrdwn(drvdata
);
744 etm_clr_pwrup(drvdata
);
745 CS_LOCK(drvdata
->base
);
748 static void etm_init_trace_id(struct etm_drvdata
*drvdata
)
750 drvdata
->traceid
= coresight_get_trace_id(drvdata
->cpu
);
753 static int etm_probe(struct amba_device
*adev
, const struct amba_id
*id
)
757 struct device
*dev
= &adev
->dev
;
758 struct coresight_platform_data
*pdata
= NULL
;
759 struct etm_drvdata
*drvdata
;
760 struct resource
*res
= &adev
->res
;
761 struct coresight_desc
*desc
;
762 struct device_node
*np
= adev
->dev
.of_node
;
764 desc
= devm_kzalloc(dev
, sizeof(*desc
), GFP_KERNEL
);
768 drvdata
= devm_kzalloc(dev
, sizeof(*drvdata
), GFP_KERNEL
);
773 pdata
= of_get_coresight_platform_data(dev
, np
);
775 return PTR_ERR(pdata
);
777 adev
->dev
.platform_data
= pdata
;
778 drvdata
->use_cp14
= of_property_read_bool(np
, "arm,cp14");
781 drvdata
->dev
= &adev
->dev
;
782 dev_set_drvdata(dev
, drvdata
);
784 /* Validity for the resource is already checked by the AMBA core */
785 base
= devm_ioremap_resource(dev
, res
);
787 return PTR_ERR(base
);
789 drvdata
->base
= base
;
791 spin_lock_init(&drvdata
->spinlock
);
793 drvdata
->atclk
= devm_clk_get(&adev
->dev
, "atclk"); /* optional */
794 if (!IS_ERR(drvdata
->atclk
)) {
795 ret
= clk_prepare_enable(drvdata
->atclk
);
800 drvdata
->cpu
= pdata
? pdata
->cpu
: 0;
803 etmdrvdata
[drvdata
->cpu
] = drvdata
;
805 if (smp_call_function_single(drvdata
->cpu
,
806 etm_init_arch_data
, drvdata
, 1))
807 dev_err(dev
, "ETM arch init failed\n");
810 register_hotcpu_notifier(&etm_cpu_notifier
);
814 if (etm_arch_supported(drvdata
->arch
) == false) {
816 goto err_arch_supported
;
819 etm_init_trace_id(drvdata
);
820 etm_set_default(&drvdata
->config
);
822 desc
->type
= CORESIGHT_DEV_TYPE_SOURCE
;
823 desc
->subtype
.source_subtype
= CORESIGHT_DEV_SUBTYPE_SOURCE_PROC
;
824 desc
->ops
= &etm_cs_ops
;
827 desc
->groups
= coresight_etm_groups
;
828 drvdata
->csdev
= coresight_register(desc
);
829 if (IS_ERR(drvdata
->csdev
)) {
830 ret
= PTR_ERR(drvdata
->csdev
);
831 goto err_arch_supported
;
834 ret
= etm_perf_symlink(drvdata
->csdev
, true);
836 coresight_unregister(drvdata
->csdev
);
837 goto err_arch_supported
;
840 pm_runtime_put(&adev
->dev
);
841 dev_info(dev
, "%s initialized\n", (char *)id
->data
);
844 coresight_enable(drvdata
->csdev
);
845 drvdata
->boot_enable
= true;
851 if (--etm_count
== 0)
852 unregister_hotcpu_notifier(&etm_cpu_notifier
);
857 static int etm_runtime_suspend(struct device
*dev
)
859 struct etm_drvdata
*drvdata
= dev_get_drvdata(dev
);
861 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
862 clk_disable_unprepare(drvdata
->atclk
);
867 static int etm_runtime_resume(struct device
*dev
)
869 struct etm_drvdata
*drvdata
= dev_get_drvdata(dev
);
871 if (drvdata
&& !IS_ERR(drvdata
->atclk
))
872 clk_prepare_enable(drvdata
->atclk
);
878 static const struct dev_pm_ops etm_dev_pm_ops
= {
879 SET_RUNTIME_PM_OPS(etm_runtime_suspend
, etm_runtime_resume
, NULL
)
882 static struct amba_id etm_ids
[] = {
903 { /* PTM 1.1 Qualcomm */
911 static struct amba_driver etm_driver
= {
913 .name
= "coresight-etm3x",
914 .owner
= THIS_MODULE
,
915 .pm
= &etm_dev_pm_ops
,
916 .suppress_bind_attrs
= true,
921 builtin_amba_driver(etm_driver
);