1 /* Copyright (c) 2011-2012, 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/module.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/pm_runtime.h>
27 #include <linux/cpu.h>
29 #include <linux/coresight.h>
30 #include <linux/amba/bus.h>
31 #include <linux/seq_file.h>
32 #include <linux/uaccess.h>
33 #include <linux/clk.h>
34 #include <asm/sections.h>
36 #include "coresight-etm.h"
38 static int boot_enable;
39 module_param_named(boot_enable, boot_enable, int, S_IRUGO);
41 /* The number of ETM/PTM currently registered */
43 static struct etm_drvdata *etmdrvdata[NR_CPUS];
46 * Memory mapped writes to clear os lock are not supported on some processors
47 * and OS lock must be unlocked before any memory mapped access on such
48 * processors, otherwise memory mapped reads/writes will be invalid.
50 static void etm_os_unlock(struct etm_drvdata *drvdata)
52 /* Writing any value to ETMOSLAR unlocks the trace registers */
53 etm_writel(drvdata, 0x0, ETMOSLAR);
54 drvdata->os_unlock = true;
58 static void etm_set_pwrdwn(struct etm_drvdata *drvdata)
62 /* Ensure pending cp14 accesses complete before setting pwrdwn */
65 etmcr = etm_readl(drvdata, ETMCR);
66 etmcr |= ETMCR_PWD_DWN;
67 etm_writel(drvdata, etmcr, ETMCR);
70 static void etm_clr_pwrdwn(struct etm_drvdata *drvdata)
74 etmcr = etm_readl(drvdata, ETMCR);
75 etmcr &= ~ETMCR_PWD_DWN;
76 etm_writel(drvdata, etmcr, ETMCR);
77 /* Ensure pwrup completes before subsequent cp14 accesses */
82 static void etm_set_pwrup(struct etm_drvdata *drvdata)
86 etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
87 etmpdcr |= ETMPDCR_PWD_UP;
88 writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
89 /* Ensure pwrup completes before subsequent cp14 accesses */
94 static void etm_clr_pwrup(struct etm_drvdata *drvdata)
98 /* Ensure pending cp14 accesses complete before clearing pwrup */
101 etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
102 etmpdcr &= ~ETMPDCR_PWD_UP;
103 writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
107 * coresight_timeout_etm - loop until a bit has changed to a specific state.
108 * @drvdata: etm's private data structure.
109 * @offset: address of a register, starting from @addr.
110 * @position: the position of the bit of interest.
111 * @value: the value the bit should have.
113 * Basically the same as @coresight_timeout except for the register access
114 * method where we have to account for CP14 configurations.
116 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
117 * TIMEOUT_US has elapsed, which ever happens first.
120 static int coresight_timeout_etm(struct etm_drvdata *drvdata, u32 offset,
121 int position, int value)
126 for (i = TIMEOUT_US; i > 0; i--) {
127 val = etm_readl(drvdata, offset);
128 /* Waiting on the bit to go from 0 to 1 */
130 if (val & BIT(position))
132 /* Waiting on the bit to go from 1 to 0 */
134 if (!(val & BIT(position)))
139 * Delay is arbitrary - the specification doesn't say how long
140 * we are expected to wait. Extra check required to make sure
141 * we don't wait needlessly on the last iteration.
151 static void etm_set_prog(struct etm_drvdata *drvdata)
155 etmcr = etm_readl(drvdata, ETMCR);
156 etmcr |= ETMCR_ETM_PRG;
157 etm_writel(drvdata, etmcr, ETMCR);
159 * Recommended by spec for cp14 accesses to ensure etmcr write is
160 * complete before polling etmsr
163 if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 1)) {
164 dev_err(drvdata->dev,
165 "%s: timeout observed when probing at offset %#x\n",
170 static void etm_clr_prog(struct etm_drvdata *drvdata)
174 etmcr = etm_readl(drvdata, ETMCR);
175 etmcr &= ~ETMCR_ETM_PRG;
176 etm_writel(drvdata, etmcr, ETMCR);
178 * Recommended by spec for cp14 accesses to ensure etmcr write is
179 * complete before polling etmsr
182 if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 0)) {
183 dev_err(drvdata->dev,
184 "%s: timeout observed when probing at offset %#x\n",
189 void etm_set_default(struct etm_config *config)
193 if (WARN_ON_ONCE(!config))
197 * Taken verbatim from the TRM:
199 * To trace all memory:
200 * set bit [24] in register 0x009, the ETMTECR1, to 1
201 * set all other bits in register 0x009, the ETMTECR1, to 0
202 * set all bits in register 0x007, the ETMTECR2, to 0
203 * set register 0x008, the ETMTEEVR, to 0x6F (TRUE).
205 config->enable_ctrl1 = BIT(24);
206 config->enable_ctrl2 = 0x0;
207 config->enable_event = ETM_HARD_WIRE_RES_A;
209 config->trigger_event = ETM_DEFAULT_EVENT_VAL;
210 config->enable_event = ETM_HARD_WIRE_RES_A;
212 config->seq_12_event = ETM_DEFAULT_EVENT_VAL;
213 config->seq_21_event = ETM_DEFAULT_EVENT_VAL;
214 config->seq_23_event = ETM_DEFAULT_EVENT_VAL;
215 config->seq_31_event = ETM_DEFAULT_EVENT_VAL;
216 config->seq_32_event = ETM_DEFAULT_EVENT_VAL;
217 config->seq_13_event = ETM_DEFAULT_EVENT_VAL;
218 config->timestamp_event = ETM_DEFAULT_EVENT_VAL;
220 for (i = 0; i < ETM_MAX_CNTR; i++) {
221 config->cntr_rld_val[i] = 0x0;
222 config->cntr_event[i] = ETM_DEFAULT_EVENT_VAL;
223 config->cntr_rld_event[i] = ETM_DEFAULT_EVENT_VAL;
224 config->cntr_val[i] = 0x0;
227 config->seq_curr_state = 0x0;
228 config->ctxid_idx = 0x0;
229 for (i = 0; i < ETM_MAX_CTXID_CMP; i++) {
230 config->ctxid_pid[i] = 0x0;
231 config->ctxid_vpid[i] = 0x0;
234 config->ctxid_mask = 0x0;
237 void etm_config_trace_mode(struct etm_config *config)
243 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
245 /* excluding kernel AND user space doesn't make sense */
246 if (mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
249 /* nothing to do if neither flags are set */
250 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
253 flags = (1 << 0 | /* instruction execute */
254 3 << 3 | /* ARM instruction */
255 0 << 5 | /* No data value comparison */
256 0 << 7 | /* No exact mach */
257 0 << 8); /* Ignore context ID */
259 /* No need to worry about single address comparators. */
260 config->enable_ctrl2 = 0x0;
262 /* Bit 0 is address range comparator 1 */
263 config->enable_ctrl1 = ETMTECR1_ADDR_COMP_1;
267 * ETMACTRn[13,11] == Non-secure state comparison control
268 * ETMACTRn[12,10] == Secure state comparison control
270 * b00 == Match in all modes in this state
271 * b01 == Do not match in any more in this state
272 * b10 == Match in all modes excepts user mode in this state
273 * b11 == Match only in user mode in this state
276 /* Tracing in secure mode is not supported at this time */
277 flags |= (0 << 12 | 1 << 10);
279 if (mode & ETM_MODE_EXCL_USER) {
280 /* exclude user, match all modes except user mode */
281 flags |= (1 << 13 | 0 << 11);
283 /* exclude kernel, match only in user mode */
284 flags |= (1 << 13 | 1 << 11);
288 * The ETMEEVR register is already set to "hard wire A". As such
289 * all there is to do is setup an address comparator that spans
290 * the entire address range and configure the state and mode bits.
292 config->addr_val[0] = (u32) 0x0;
293 config->addr_val[1] = (u32) ~0x0;
294 config->addr_acctype[0] = flags;
295 config->addr_acctype[1] = flags;
296 config->addr_type[0] = ETM_ADDR_TYPE_RANGE;
297 config->addr_type[1] = ETM_ADDR_TYPE_RANGE;
300 static void etm_enable_hw(void *info)
304 struct etm_drvdata *drvdata = info;
305 struct etm_config *config = &drvdata->config;
307 CS_UNLOCK(drvdata->base);
310 etm_clr_pwrdwn(drvdata);
311 /* Apply power to trace registers */
312 etm_set_pwrup(drvdata);
313 /* Make sure all registers are accessible */
314 etm_os_unlock(drvdata);
316 etm_set_prog(drvdata);
318 etmcr = etm_readl(drvdata, ETMCR);
319 etmcr &= (ETMCR_PWD_DWN | ETMCR_ETM_PRG);
320 etmcr |= drvdata->port_size;
321 etm_writel(drvdata, config->ctrl | etmcr, ETMCR);
322 etm_writel(drvdata, config->trigger_event, ETMTRIGGER);
323 etm_writel(drvdata, config->startstop_ctrl, ETMTSSCR);
324 etm_writel(drvdata, config->enable_event, ETMTEEVR);
325 etm_writel(drvdata, config->enable_ctrl1, ETMTECR1);
326 etm_writel(drvdata, config->fifofull_level, ETMFFLR);
327 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
328 etm_writel(drvdata, config->addr_val[i], ETMACVRn(i));
329 etm_writel(drvdata, config->addr_acctype[i], ETMACTRn(i));
331 for (i = 0; i < drvdata->nr_cntr; i++) {
332 etm_writel(drvdata, config->cntr_rld_val[i], ETMCNTRLDVRn(i));
333 etm_writel(drvdata, config->cntr_event[i], ETMCNTENRn(i));
334 etm_writel(drvdata, config->cntr_rld_event[i],
336 etm_writel(drvdata, config->cntr_val[i], ETMCNTVRn(i));
338 etm_writel(drvdata, config->seq_12_event, ETMSQ12EVR);
339 etm_writel(drvdata, config->seq_21_event, ETMSQ21EVR);
340 etm_writel(drvdata, config->seq_23_event, ETMSQ23EVR);
341 etm_writel(drvdata, config->seq_31_event, ETMSQ31EVR);
342 etm_writel(drvdata, config->seq_32_event, ETMSQ32EVR);
343 etm_writel(drvdata, config->seq_13_event, ETMSQ13EVR);
344 etm_writel(drvdata, config->seq_curr_state, ETMSQR);
345 for (i = 0; i < drvdata->nr_ext_out; i++)
346 etm_writel(drvdata, ETM_DEFAULT_EVENT_VAL, ETMEXTOUTEVRn(i));
347 for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
348 etm_writel(drvdata, config->ctxid_pid[i], ETMCIDCVRn(i));
349 etm_writel(drvdata, config->ctxid_mask, ETMCIDCMR);
350 etm_writel(drvdata, config->sync_freq, ETMSYNCFR);
351 /* No external input selected */
352 etm_writel(drvdata, 0x0, ETMEXTINSELR);
353 etm_writel(drvdata, config->timestamp_event, ETMTSEVR);
354 /* No auxiliary control selected */
355 etm_writel(drvdata, 0x0, ETMAUXCR);
356 etm_writel(drvdata, drvdata->traceid, ETMTRACEIDR);
357 /* No VMID comparator value selected */
358 etm_writel(drvdata, 0x0, ETMVMIDCVR);
360 /* Ensures trace output is enabled from this ETM */
361 etm_writel(drvdata, config->ctrl | ETMCR_ETM_EN | etmcr, ETMCR);
363 etm_clr_prog(drvdata);
364 CS_LOCK(drvdata->base);
366 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
369 static int etm_cpu_id(struct coresight_device *csdev)
371 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
376 int etm_get_trace_id(struct etm_drvdata *drvdata)
384 if (!local_read(&drvdata->mode))
385 return drvdata->traceid;
387 pm_runtime_get_sync(drvdata->dev);
389 spin_lock_irqsave(&drvdata->spinlock, flags);
391 CS_UNLOCK(drvdata->base);
392 trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
393 CS_LOCK(drvdata->base);
395 spin_unlock_irqrestore(&drvdata->spinlock, flags);
396 pm_runtime_put(drvdata->dev);
403 static int etm_trace_id(struct coresight_device *csdev)
405 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
407 return etm_get_trace_id(drvdata);
410 static int etm_enable_sysfs(struct coresight_device *csdev)
412 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
415 spin_lock(&drvdata->spinlock);
418 * Configure the ETM only if the CPU is online. If it isn't online
419 * hw configuration will take place when 'CPU_STARTING' is received
420 * in @etm_cpu_callback.
422 if (cpu_online(drvdata->cpu)) {
423 ret = smp_call_function_single(drvdata->cpu,
424 etm_enable_hw, drvdata, 1);
429 drvdata->sticky_enable = true;
430 spin_unlock(&drvdata->spinlock);
432 dev_info(drvdata->dev, "ETM tracing enabled\n");
436 spin_unlock(&drvdata->spinlock);
440 static int etm_enable(struct coresight_device *csdev, u32 mode)
444 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
446 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
448 /* Someone is already using the tracer */
454 ret = etm_enable_sysfs(csdev);
460 /* The tracer didn't start */
462 local_set(&drvdata->mode, CS_MODE_DISABLED);
467 static void etm_disable_hw(void *info)
470 struct etm_drvdata *drvdata = info;
471 struct etm_config *config = &drvdata->config;
473 CS_UNLOCK(drvdata->base);
474 etm_set_prog(drvdata);
476 /* Read back sequencer and counters for post trace analysis */
477 config->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
479 for (i = 0; i < drvdata->nr_cntr; i++)
480 config->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
482 etm_set_pwrdwn(drvdata);
483 CS_LOCK(drvdata->base);
485 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
488 static void etm_disable_sysfs(struct coresight_device *csdev)
490 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
493 * Taking hotplug lock here protects from clocks getting disabled
494 * with tracing being left on (crash scenario) if user disable occurs
495 * after cpu online mask indicates the cpu is offline but before the
496 * DYING hotplug callback is serviced by the ETM driver.
499 spin_lock(&drvdata->spinlock);
502 * Executing etm_disable_hw on the cpu whose ETM is being disabled
503 * ensures that register writes occur when cpu is powered.
505 smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
507 spin_unlock(&drvdata->spinlock);
510 dev_info(drvdata->dev, "ETM tracing disabled\n");
513 static void etm_disable(struct coresight_device *csdev)
516 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
519 * For as long as the tracer isn't disabled another entity can't
520 * change its status. As such we can read the status here without
521 * fearing it will change under us.
523 mode = local_read(&drvdata->mode);
526 case CS_MODE_DISABLED:
529 etm_disable_sysfs(csdev);
537 local_set(&drvdata->mode, CS_MODE_DISABLED);
540 static const struct coresight_ops_source etm_source_ops = {
541 .cpu_id = etm_cpu_id,
542 .trace_id = etm_trace_id,
543 .enable = etm_enable,
544 .disable = etm_disable,
547 static const struct coresight_ops etm_cs_ops = {
548 .source_ops = &etm_source_ops,
551 static int etm_cpu_callback(struct notifier_block *nfb, unsigned long action,
554 unsigned int cpu = (unsigned long)hcpu;
556 if (!etmdrvdata[cpu])
559 switch (action & (~CPU_TASKS_FROZEN)) {
561 spin_lock(&etmdrvdata[cpu]->spinlock);
562 if (!etmdrvdata[cpu]->os_unlock) {
563 etm_os_unlock(etmdrvdata[cpu]);
564 etmdrvdata[cpu]->os_unlock = true;
567 if (local_read(&etmdrvdata[cpu]->mode))
568 etm_enable_hw(etmdrvdata[cpu]);
569 spin_unlock(&etmdrvdata[cpu]->spinlock);
573 if (etmdrvdata[cpu]->boot_enable &&
574 !etmdrvdata[cpu]->sticky_enable)
575 coresight_enable(etmdrvdata[cpu]->csdev);
579 spin_lock(&etmdrvdata[cpu]->spinlock);
580 if (local_read(&etmdrvdata[cpu]->mode))
581 etm_disable_hw(etmdrvdata[cpu]);
582 spin_unlock(&etmdrvdata[cpu]->spinlock);
589 static struct notifier_block etm_cpu_notifier = {
590 .notifier_call = etm_cpu_callback,
593 static bool etm_arch_supported(u8 arch)
610 static void etm_init_arch_data(void *info)
614 struct etm_drvdata *drvdata = info;
616 /* Make sure all registers are accessible */
617 etm_os_unlock(drvdata);
619 CS_UNLOCK(drvdata->base);
621 /* First dummy read */
622 (void)etm_readl(drvdata, ETMPDSR);
623 /* Provide power to ETM: ETMPDCR[3] == 1 */
624 etm_set_pwrup(drvdata);
626 * Clear power down bit since when this bit is set writes to
627 * certain registers might be ignored.
629 etm_clr_pwrdwn(drvdata);
631 * Set prog bit. It will be set from reset but this is included to
634 etm_set_prog(drvdata);
636 /* Find all capabilities */
637 etmidr = etm_readl(drvdata, ETMIDR);
638 drvdata->arch = BMVAL(etmidr, 4, 11);
639 drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
641 drvdata->etmccer = etm_readl(drvdata, ETMCCER);
642 etmccr = etm_readl(drvdata, ETMCCR);
643 drvdata->etmccr = etmccr;
644 drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
645 drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
646 drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
647 drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
648 drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
650 etm_set_pwrdwn(drvdata);
651 etm_clr_pwrup(drvdata);
652 CS_LOCK(drvdata->base);
655 static void etm_init_trace_id(struct etm_drvdata *drvdata)
658 * A trace ID of value 0 is invalid, so let's start at some
659 * random value that fits in 7 bits and go from there.
661 drvdata->traceid = 0x10 + drvdata->cpu;
664 static int etm_probe(struct amba_device *adev, const struct amba_id *id)
668 struct device *dev = &adev->dev;
669 struct coresight_platform_data *pdata = NULL;
670 struct etm_drvdata *drvdata;
671 struct resource *res = &adev->res;
672 struct coresight_desc *desc;
673 struct device_node *np = adev->dev.of_node;
675 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
679 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
684 pdata = of_get_coresight_platform_data(dev, np);
686 return PTR_ERR(pdata);
688 adev->dev.platform_data = pdata;
689 drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
692 drvdata->dev = &adev->dev;
693 dev_set_drvdata(dev, drvdata);
695 /* Validity for the resource is already checked by the AMBA core */
696 base = devm_ioremap_resource(dev, res);
698 return PTR_ERR(base);
700 drvdata->base = base;
702 spin_lock_init(&drvdata->spinlock);
704 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
705 if (!IS_ERR(drvdata->atclk)) {
706 ret = clk_prepare_enable(drvdata->atclk);
711 drvdata->cpu = pdata ? pdata->cpu : 0;
714 etmdrvdata[drvdata->cpu] = drvdata;
716 if (smp_call_function_single(drvdata->cpu,
717 etm_init_arch_data, drvdata, 1))
718 dev_err(dev, "ETM arch init failed\n");
721 register_hotcpu_notifier(&etm_cpu_notifier);
725 if (etm_arch_supported(drvdata->arch) == false) {
727 goto err_arch_supported;
730 etm_init_trace_id(drvdata);
731 etm_set_default(&drvdata->config);
733 desc->type = CORESIGHT_DEV_TYPE_SOURCE;
734 desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
735 desc->ops = &etm_cs_ops;
738 desc->groups = coresight_etm_groups;
739 drvdata->csdev = coresight_register(desc);
740 if (IS_ERR(drvdata->csdev)) {
741 ret = PTR_ERR(drvdata->csdev);
742 goto err_arch_supported;
745 pm_runtime_put(&adev->dev);
746 dev_info(dev, "%s initialized\n", (char *)id->data);
749 coresight_enable(drvdata->csdev);
750 drvdata->boot_enable = true;
756 if (--etm_count == 0)
757 unregister_hotcpu_notifier(&etm_cpu_notifier);
762 static int etm_runtime_suspend(struct device *dev)
764 struct etm_drvdata *drvdata = dev_get_drvdata(dev);
766 if (drvdata && !IS_ERR(drvdata->atclk))
767 clk_disable_unprepare(drvdata->atclk);
772 static int etm_runtime_resume(struct device *dev)
774 struct etm_drvdata *drvdata = dev_get_drvdata(dev);
776 if (drvdata && !IS_ERR(drvdata->atclk))
777 clk_prepare_enable(drvdata->atclk);
783 static const struct dev_pm_ops etm_dev_pm_ops = {
784 SET_RUNTIME_PM_OPS(etm_runtime_suspend, etm_runtime_resume, NULL)
787 static struct amba_id etm_ids[] = {
808 { /* PTM 1.1 Qualcomm */
816 static struct amba_driver etm_driver = {
818 .name = "coresight-etm3x",
819 .owner = THIS_MODULE,
820 .pm = &etm_dev_pm_ops,
821 .suppress_bind_attrs = true,
827 module_amba_driver(etm_driver);
829 MODULE_LICENSE("GPL v2");
830 MODULE_DESCRIPTION("CoreSight Program Flow Trace driver");