Linux 4.1.16
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight-etm3x.c
blobc965f5724abdb3cfc5d4693c94b26820a4800f05
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>
18 #include <linux/io.h>
19 #include <linux/err.h>
20 #include <linux/fs.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/of.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 <asm/sections.h>
35 #include "coresight-etm.h"
37 static int boot_enable;
38 module_param_named(boot_enable, boot_enable, int, S_IRUGO);
40 /* The number of ETM/PTM currently registered */
41 static int etm_count;
42 static struct etm_drvdata *etmdrvdata[NR_CPUS];
44 static inline void etm_writel(struct etm_drvdata *drvdata,
45 u32 val, u32 off)
47 if (drvdata->use_cp14) {
48 if (etm_writel_cp14(off, val)) {
49 dev_err(drvdata->dev,
50 "invalid CP14 access to ETM reg: %#x", off);
52 } else {
53 writel_relaxed(val, drvdata->base + off);
57 static inline unsigned int etm_readl(struct etm_drvdata *drvdata, u32 off)
59 u32 val;
61 if (drvdata->use_cp14) {
62 if (etm_readl_cp14(off, &val)) {
63 dev_err(drvdata->dev,
64 "invalid CP14 access to ETM reg: %#x", off);
66 } else {
67 val = readl_relaxed(drvdata->base + off);
70 return val;
74 * Memory mapped writes to clear os lock are not supported on some processors
75 * and OS lock must be unlocked before any memory mapped access on such
76 * processors, otherwise memory mapped reads/writes will be invalid.
78 static void etm_os_unlock(void *info)
80 struct etm_drvdata *drvdata = (struct etm_drvdata *)info;
81 /* Writing any value to ETMOSLAR unlocks the trace registers */
82 etm_writel(drvdata, 0x0, ETMOSLAR);
83 isb();
86 static void etm_set_pwrdwn(struct etm_drvdata *drvdata)
88 u32 etmcr;
90 /* Ensure pending cp14 accesses complete before setting pwrdwn */
91 mb();
92 isb();
93 etmcr = etm_readl(drvdata, ETMCR);
94 etmcr |= ETMCR_PWD_DWN;
95 etm_writel(drvdata, etmcr, ETMCR);
98 static void etm_clr_pwrdwn(struct etm_drvdata *drvdata)
100 u32 etmcr;
102 etmcr = etm_readl(drvdata, ETMCR);
103 etmcr &= ~ETMCR_PWD_DWN;
104 etm_writel(drvdata, etmcr, ETMCR);
105 /* Ensure pwrup completes before subsequent cp14 accesses */
106 mb();
107 isb();
110 static void etm_set_pwrup(struct etm_drvdata *drvdata)
112 u32 etmpdcr;
114 etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
115 etmpdcr |= ETMPDCR_PWD_UP;
116 writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
117 /* Ensure pwrup completes before subsequent cp14 accesses */
118 mb();
119 isb();
122 static void etm_clr_pwrup(struct etm_drvdata *drvdata)
124 u32 etmpdcr;
126 /* Ensure pending cp14 accesses complete before clearing pwrup */
127 mb();
128 isb();
129 etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
130 etmpdcr &= ~ETMPDCR_PWD_UP;
131 writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
135 * coresight_timeout_etm - loop until a bit has changed to a specific state.
136 * @drvdata: etm's private data structure.
137 * @offset: address of a register, starting from @addr.
138 * @position: the position of the bit of interest.
139 * @value: the value the bit should have.
141 * Basically the same as @coresight_timeout except for the register access
142 * method where we have to account for CP14 configurations.
144 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
145 * TIMEOUT_US has elapsed, which ever happens first.
148 static int coresight_timeout_etm(struct etm_drvdata *drvdata, u32 offset,
149 int position, int value)
151 int i;
152 u32 val;
154 for (i = TIMEOUT_US; i > 0; i--) {
155 val = etm_readl(drvdata, offset);
156 /* Waiting on the bit to go from 0 to 1 */
157 if (value) {
158 if (val & BIT(position))
159 return 0;
160 /* Waiting on the bit to go from 1 to 0 */
161 } else {
162 if (!(val & BIT(position)))
163 return 0;
167 * Delay is arbitrary - the specification doesn't say how long
168 * we are expected to wait. Extra check required to make sure
169 * we don't wait needlessly on the last iteration.
171 if (i - 1)
172 udelay(1);
175 return -EAGAIN;
179 static void etm_set_prog(struct etm_drvdata *drvdata)
181 u32 etmcr;
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
190 isb();
191 if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 1)) {
192 dev_err(drvdata->dev,
193 "timeout observed when probing at offset %#x\n", ETMSR);
197 static void etm_clr_prog(struct etm_drvdata *drvdata)
199 u32 etmcr;
201 etmcr = etm_readl(drvdata, ETMCR);
202 etmcr &= ~ETMCR_ETM_PRG;
203 etm_writel(drvdata, etmcr, ETMCR);
205 * Recommended by spec for cp14 accesses to ensure etmcr write is
206 * complete before polling etmsr
208 isb();
209 if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 0)) {
210 dev_err(drvdata->dev,
211 "timeout observed when probing at offset %#x\n", ETMSR);
215 static void etm_set_default(struct etm_drvdata *drvdata)
217 int i;
219 drvdata->trigger_event = ETM_DEFAULT_EVENT_VAL;
220 drvdata->enable_event = ETM_HARD_WIRE_RES_A;
222 drvdata->seq_12_event = ETM_DEFAULT_EVENT_VAL;
223 drvdata->seq_21_event = ETM_DEFAULT_EVENT_VAL;
224 drvdata->seq_23_event = ETM_DEFAULT_EVENT_VAL;
225 drvdata->seq_31_event = ETM_DEFAULT_EVENT_VAL;
226 drvdata->seq_32_event = ETM_DEFAULT_EVENT_VAL;
227 drvdata->seq_13_event = ETM_DEFAULT_EVENT_VAL;
228 drvdata->timestamp_event = ETM_DEFAULT_EVENT_VAL;
230 for (i = 0; i < drvdata->nr_cntr; i++) {
231 drvdata->cntr_rld_val[i] = 0x0;
232 drvdata->cntr_event[i] = ETM_DEFAULT_EVENT_VAL;
233 drvdata->cntr_rld_event[i] = ETM_DEFAULT_EVENT_VAL;
234 drvdata->cntr_val[i] = 0x0;
237 drvdata->seq_curr_state = 0x0;
238 drvdata->ctxid_idx = 0x0;
239 for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
240 drvdata->ctxid_val[i] = 0x0;
241 drvdata->ctxid_mask = 0x0;
244 static void etm_enable_hw(void *info)
246 int i;
247 u32 etmcr;
248 struct etm_drvdata *drvdata = info;
250 CS_UNLOCK(drvdata->base);
252 /* Turn engine on */
253 etm_clr_pwrdwn(drvdata);
254 /* Apply power to trace registers */
255 etm_set_pwrup(drvdata);
256 /* Make sure all registers are accessible */
257 etm_os_unlock(drvdata);
259 etm_set_prog(drvdata);
261 etmcr = etm_readl(drvdata, ETMCR);
262 etmcr &= (ETMCR_PWD_DWN | ETMCR_ETM_PRG);
263 etmcr |= drvdata->port_size;
264 etm_writel(drvdata, drvdata->ctrl | etmcr, ETMCR);
265 etm_writel(drvdata, drvdata->trigger_event, ETMTRIGGER);
266 etm_writel(drvdata, drvdata->startstop_ctrl, ETMTSSCR);
267 etm_writel(drvdata, drvdata->enable_event, ETMTEEVR);
268 etm_writel(drvdata, drvdata->enable_ctrl1, ETMTECR1);
269 etm_writel(drvdata, drvdata->fifofull_level, ETMFFLR);
270 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
271 etm_writel(drvdata, drvdata->addr_val[i], ETMACVRn(i));
272 etm_writel(drvdata, drvdata->addr_acctype[i], ETMACTRn(i));
274 for (i = 0; i < drvdata->nr_cntr; i++) {
275 etm_writel(drvdata, drvdata->cntr_rld_val[i], ETMCNTRLDVRn(i));
276 etm_writel(drvdata, drvdata->cntr_event[i], ETMCNTENRn(i));
277 etm_writel(drvdata, drvdata->cntr_rld_event[i],
278 ETMCNTRLDEVRn(i));
279 etm_writel(drvdata, drvdata->cntr_val[i], ETMCNTVRn(i));
281 etm_writel(drvdata, drvdata->seq_12_event, ETMSQ12EVR);
282 etm_writel(drvdata, drvdata->seq_21_event, ETMSQ21EVR);
283 etm_writel(drvdata, drvdata->seq_23_event, ETMSQ23EVR);
284 etm_writel(drvdata, drvdata->seq_31_event, ETMSQ31EVR);
285 etm_writel(drvdata, drvdata->seq_32_event, ETMSQ32EVR);
286 etm_writel(drvdata, drvdata->seq_13_event, ETMSQ13EVR);
287 etm_writel(drvdata, drvdata->seq_curr_state, ETMSQR);
288 for (i = 0; i < drvdata->nr_ext_out; i++)
289 etm_writel(drvdata, ETM_DEFAULT_EVENT_VAL, ETMEXTOUTEVRn(i));
290 for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
291 etm_writel(drvdata, drvdata->ctxid_val[i], ETMCIDCVRn(i));
292 etm_writel(drvdata, drvdata->ctxid_mask, ETMCIDCMR);
293 etm_writel(drvdata, drvdata->sync_freq, ETMSYNCFR);
294 /* No external input selected */
295 etm_writel(drvdata, 0x0, ETMEXTINSELR);
296 etm_writel(drvdata, drvdata->timestamp_event, ETMTSEVR);
297 /* No auxiliary control selected */
298 etm_writel(drvdata, 0x0, ETMAUXCR);
299 etm_writel(drvdata, drvdata->traceid, ETMTRACEIDR);
300 /* No VMID comparator value selected */
301 etm_writel(drvdata, 0x0, ETMVMIDCVR);
303 /* Ensures trace output is enabled from this ETM */
304 etm_writel(drvdata, drvdata->ctrl | ETMCR_ETM_EN | etmcr, ETMCR);
306 etm_clr_prog(drvdata);
307 CS_LOCK(drvdata->base);
309 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
312 static int etm_trace_id_simple(struct etm_drvdata *drvdata)
314 if (!drvdata->enable)
315 return drvdata->traceid;
317 return (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
320 static int etm_trace_id(struct coresight_device *csdev)
322 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
323 unsigned long flags;
324 int trace_id = -1;
326 if (!drvdata->enable)
327 return drvdata->traceid;
329 if (clk_prepare_enable(drvdata->clk))
330 goto out;
332 spin_lock_irqsave(&drvdata->spinlock, flags);
334 CS_UNLOCK(drvdata->base);
335 trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
336 CS_LOCK(drvdata->base);
338 spin_unlock_irqrestore(&drvdata->spinlock, flags);
339 clk_disable_unprepare(drvdata->clk);
340 out:
341 return trace_id;
344 static int etm_enable(struct coresight_device *csdev)
346 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
347 int ret;
349 ret = clk_prepare_enable(drvdata->clk);
350 if (ret)
351 goto err_clk;
353 spin_lock(&drvdata->spinlock);
356 * Configure the ETM only if the CPU is online. If it isn't online
357 * hw configuration will take place when 'CPU_STARTING' is received
358 * in @etm_cpu_callback.
360 if (cpu_online(drvdata->cpu)) {
361 ret = smp_call_function_single(drvdata->cpu,
362 etm_enable_hw, drvdata, 1);
363 if (ret)
364 goto err;
367 drvdata->enable = true;
368 drvdata->sticky_enable = true;
370 spin_unlock(&drvdata->spinlock);
372 dev_info(drvdata->dev, "ETM tracing enabled\n");
373 return 0;
374 err:
375 spin_unlock(&drvdata->spinlock);
376 clk_disable_unprepare(drvdata->clk);
377 err_clk:
378 return ret;
381 static void etm_disable_hw(void *info)
383 int i;
384 struct etm_drvdata *drvdata = info;
386 CS_UNLOCK(drvdata->base);
387 etm_set_prog(drvdata);
389 /* Program trace enable to low by using always false event */
390 etm_writel(drvdata, ETM_HARD_WIRE_RES_A | ETM_EVENT_NOT_A, ETMTEEVR);
392 /* Read back sequencer and counters for post trace analysis */
393 drvdata->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
395 for (i = 0; i < drvdata->nr_cntr; i++)
396 drvdata->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
398 etm_set_pwrdwn(drvdata);
399 CS_LOCK(drvdata->base);
401 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
404 static void etm_disable(struct coresight_device *csdev)
406 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
409 * Taking hotplug lock here protects from clocks getting disabled
410 * with tracing being left on (crash scenario) if user disable occurs
411 * after cpu online mask indicates the cpu is offline but before the
412 * DYING hotplug callback is serviced by the ETM driver.
414 get_online_cpus();
415 spin_lock(&drvdata->spinlock);
418 * Executing etm_disable_hw on the cpu whose ETM is being disabled
419 * ensures that register writes occur when cpu is powered.
421 smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
422 drvdata->enable = false;
424 spin_unlock(&drvdata->spinlock);
425 put_online_cpus();
427 clk_disable_unprepare(drvdata->clk);
429 dev_info(drvdata->dev, "ETM tracing disabled\n");
432 static const struct coresight_ops_source etm_source_ops = {
433 .trace_id = etm_trace_id,
434 .enable = etm_enable,
435 .disable = etm_disable,
438 static const struct coresight_ops etm_cs_ops = {
439 .source_ops = &etm_source_ops,
442 static ssize_t nr_addr_cmp_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 unsigned long val;
446 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
448 val = drvdata->nr_addr_cmp;
449 return sprintf(buf, "%#lx\n", val);
451 static DEVICE_ATTR_RO(nr_addr_cmp);
453 static ssize_t nr_cntr_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
455 { unsigned long val;
456 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
458 val = drvdata->nr_cntr;
459 return sprintf(buf, "%#lx\n", val);
461 static DEVICE_ATTR_RO(nr_cntr);
463 static ssize_t nr_ctxid_cmp_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
466 unsigned long val;
467 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
469 val = drvdata->nr_ctxid_cmp;
470 return sprintf(buf, "%#lx\n", val);
472 static DEVICE_ATTR_RO(nr_ctxid_cmp);
474 static ssize_t etmsr_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 int ret;
478 unsigned long flags, val;
479 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
481 ret = clk_prepare_enable(drvdata->clk);
482 if (ret)
483 return ret;
485 spin_lock_irqsave(&drvdata->spinlock, flags);
486 CS_UNLOCK(drvdata->base);
488 val = etm_readl(drvdata, ETMSR);
490 CS_LOCK(drvdata->base);
491 spin_unlock_irqrestore(&drvdata->spinlock, flags);
492 clk_disable_unprepare(drvdata->clk);
494 return sprintf(buf, "%#lx\n", val);
496 static DEVICE_ATTR_RO(etmsr);
498 static ssize_t reset_store(struct device *dev,
499 struct device_attribute *attr,
500 const char *buf, size_t size)
502 int i, ret;
503 unsigned long val;
504 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
506 ret = kstrtoul(buf, 16, &val);
507 if (ret)
508 return ret;
510 if (val) {
511 spin_lock(&drvdata->spinlock);
512 drvdata->mode = ETM_MODE_EXCLUDE;
513 drvdata->ctrl = 0x0;
514 drvdata->trigger_event = ETM_DEFAULT_EVENT_VAL;
515 drvdata->startstop_ctrl = 0x0;
516 drvdata->addr_idx = 0x0;
517 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
518 drvdata->addr_val[i] = 0x0;
519 drvdata->addr_acctype[i] = 0x0;
520 drvdata->addr_type[i] = ETM_ADDR_TYPE_NONE;
522 drvdata->cntr_idx = 0x0;
524 etm_set_default(drvdata);
525 spin_unlock(&drvdata->spinlock);
528 return size;
530 static DEVICE_ATTR_WO(reset);
532 static ssize_t mode_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 unsigned long val;
536 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
538 val = drvdata->mode;
539 return sprintf(buf, "%#lx\n", val);
542 static ssize_t mode_store(struct device *dev,
543 struct device_attribute *attr,
544 const char *buf, size_t size)
546 int ret;
547 unsigned long val;
548 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
550 ret = kstrtoul(buf, 16, &val);
551 if (ret)
552 return ret;
554 spin_lock(&drvdata->spinlock);
555 drvdata->mode = val & ETM_MODE_ALL;
557 if (drvdata->mode & ETM_MODE_EXCLUDE)
558 drvdata->enable_ctrl1 |= ETMTECR1_INC_EXC;
559 else
560 drvdata->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
562 if (drvdata->mode & ETM_MODE_CYCACC)
563 drvdata->ctrl |= ETMCR_CYC_ACC;
564 else
565 drvdata->ctrl &= ~ETMCR_CYC_ACC;
567 if (drvdata->mode & ETM_MODE_STALL) {
568 if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
569 dev_warn(drvdata->dev, "stall mode not supported\n");
570 ret = -EINVAL;
571 goto err_unlock;
573 drvdata->ctrl |= ETMCR_STALL_MODE;
574 } else
575 drvdata->ctrl &= ~ETMCR_STALL_MODE;
577 if (drvdata->mode & ETM_MODE_TIMESTAMP) {
578 if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
579 dev_warn(drvdata->dev, "timestamp not supported\n");
580 ret = -EINVAL;
581 goto err_unlock;
583 drvdata->ctrl |= ETMCR_TIMESTAMP_EN;
584 } else
585 drvdata->ctrl &= ~ETMCR_TIMESTAMP_EN;
587 if (drvdata->mode & ETM_MODE_CTXID)
588 drvdata->ctrl |= ETMCR_CTXID_SIZE;
589 else
590 drvdata->ctrl &= ~ETMCR_CTXID_SIZE;
591 spin_unlock(&drvdata->spinlock);
593 return size;
595 err_unlock:
596 spin_unlock(&drvdata->spinlock);
597 return ret;
599 static DEVICE_ATTR_RW(mode);
601 static ssize_t trigger_event_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 unsigned long val;
605 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
607 val = drvdata->trigger_event;
608 return sprintf(buf, "%#lx\n", val);
611 static ssize_t trigger_event_store(struct device *dev,
612 struct device_attribute *attr,
613 const char *buf, size_t size)
615 int ret;
616 unsigned long val;
617 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
619 ret = kstrtoul(buf, 16, &val);
620 if (ret)
621 return ret;
623 drvdata->trigger_event = val & ETM_EVENT_MASK;
625 return size;
627 static DEVICE_ATTR_RW(trigger_event);
629 static ssize_t enable_event_show(struct device *dev,
630 struct device_attribute *attr, char *buf)
632 unsigned long val;
633 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
635 val = drvdata->enable_event;
636 return sprintf(buf, "%#lx\n", val);
639 static ssize_t enable_event_store(struct device *dev,
640 struct device_attribute *attr,
641 const char *buf, size_t size)
643 int ret;
644 unsigned long val;
645 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
647 ret = kstrtoul(buf, 16, &val);
648 if (ret)
649 return ret;
651 drvdata->enable_event = val & ETM_EVENT_MASK;
653 return size;
655 static DEVICE_ATTR_RW(enable_event);
657 static ssize_t fifofull_level_show(struct device *dev,
658 struct device_attribute *attr, char *buf)
660 unsigned long val;
661 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
663 val = drvdata->fifofull_level;
664 return sprintf(buf, "%#lx\n", val);
667 static ssize_t fifofull_level_store(struct device *dev,
668 struct device_attribute *attr,
669 const char *buf, size_t size)
671 int ret;
672 unsigned long val;
673 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
675 ret = kstrtoul(buf, 16, &val);
676 if (ret)
677 return ret;
679 drvdata->fifofull_level = val;
681 return size;
683 static DEVICE_ATTR_RW(fifofull_level);
685 static ssize_t addr_idx_show(struct device *dev,
686 struct device_attribute *attr, char *buf)
688 unsigned long val;
689 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
691 val = drvdata->addr_idx;
692 return sprintf(buf, "%#lx\n", val);
695 static ssize_t addr_idx_store(struct device *dev,
696 struct device_attribute *attr,
697 const char *buf, size_t size)
699 int ret;
700 unsigned long val;
701 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
703 ret = kstrtoul(buf, 16, &val);
704 if (ret)
705 return ret;
707 if (val >= drvdata->nr_addr_cmp)
708 return -EINVAL;
711 * Use spinlock to ensure index doesn't change while it gets
712 * dereferenced multiple times within a spinlock block elsewhere.
714 spin_lock(&drvdata->spinlock);
715 drvdata->addr_idx = val;
716 spin_unlock(&drvdata->spinlock);
718 return size;
720 static DEVICE_ATTR_RW(addr_idx);
722 static ssize_t addr_single_show(struct device *dev,
723 struct device_attribute *attr, char *buf)
725 u8 idx;
726 unsigned long val;
727 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
729 spin_lock(&drvdata->spinlock);
730 idx = drvdata->addr_idx;
731 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
732 drvdata->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
733 spin_unlock(&drvdata->spinlock);
734 return -EINVAL;
737 val = drvdata->addr_val[idx];
738 spin_unlock(&drvdata->spinlock);
740 return sprintf(buf, "%#lx\n", val);
743 static ssize_t addr_single_store(struct device *dev,
744 struct device_attribute *attr,
745 const char *buf, size_t size)
747 u8 idx;
748 int ret;
749 unsigned long val;
750 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
752 ret = kstrtoul(buf, 16, &val);
753 if (ret)
754 return ret;
756 spin_lock(&drvdata->spinlock);
757 idx = drvdata->addr_idx;
758 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
759 drvdata->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
760 spin_unlock(&drvdata->spinlock);
761 return -EINVAL;
764 drvdata->addr_val[idx] = val;
765 drvdata->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
766 spin_unlock(&drvdata->spinlock);
768 return size;
770 static DEVICE_ATTR_RW(addr_single);
772 static ssize_t addr_range_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
775 u8 idx;
776 unsigned long val1, val2;
777 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
779 spin_lock(&drvdata->spinlock);
780 idx = drvdata->addr_idx;
781 if (idx % 2 != 0) {
782 spin_unlock(&drvdata->spinlock);
783 return -EPERM;
785 if (!((drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
786 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
787 (drvdata->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
788 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
789 spin_unlock(&drvdata->spinlock);
790 return -EPERM;
793 val1 = drvdata->addr_val[idx];
794 val2 = drvdata->addr_val[idx + 1];
795 spin_unlock(&drvdata->spinlock);
797 return sprintf(buf, "%#lx %#lx\n", val1, val2);
800 static ssize_t addr_range_store(struct device *dev,
801 struct device_attribute *attr,
802 const char *buf, size_t size)
804 u8 idx;
805 unsigned long val1, val2;
806 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
808 if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
809 return -EINVAL;
810 /* Lower address comparator cannot have a higher address value */
811 if (val1 > val2)
812 return -EINVAL;
814 spin_lock(&drvdata->spinlock);
815 idx = drvdata->addr_idx;
816 if (idx % 2 != 0) {
817 spin_unlock(&drvdata->spinlock);
818 return -EPERM;
820 if (!((drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
821 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
822 (drvdata->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
823 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
824 spin_unlock(&drvdata->spinlock);
825 return -EPERM;
828 drvdata->addr_val[idx] = val1;
829 drvdata->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
830 drvdata->addr_val[idx + 1] = val2;
831 drvdata->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
832 drvdata->enable_ctrl1 |= (1 << (idx/2));
833 spin_unlock(&drvdata->spinlock);
835 return size;
837 static DEVICE_ATTR_RW(addr_range);
839 static ssize_t addr_start_show(struct device *dev,
840 struct device_attribute *attr, char *buf)
842 u8 idx;
843 unsigned long val;
844 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
846 spin_lock(&drvdata->spinlock);
847 idx = drvdata->addr_idx;
848 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
849 drvdata->addr_type[idx] == ETM_ADDR_TYPE_START)) {
850 spin_unlock(&drvdata->spinlock);
851 return -EPERM;
854 val = drvdata->addr_val[idx];
855 spin_unlock(&drvdata->spinlock);
857 return sprintf(buf, "%#lx\n", val);
860 static ssize_t addr_start_store(struct device *dev,
861 struct device_attribute *attr,
862 const char *buf, size_t size)
864 u8 idx;
865 int ret;
866 unsigned long val;
867 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
869 ret = kstrtoul(buf, 16, &val);
870 if (ret)
871 return ret;
873 spin_lock(&drvdata->spinlock);
874 idx = drvdata->addr_idx;
875 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
876 drvdata->addr_type[idx] == ETM_ADDR_TYPE_START)) {
877 spin_unlock(&drvdata->spinlock);
878 return -EPERM;
881 drvdata->addr_val[idx] = val;
882 drvdata->addr_type[idx] = ETM_ADDR_TYPE_START;
883 drvdata->startstop_ctrl |= (1 << idx);
884 drvdata->enable_ctrl1 |= BIT(25);
885 spin_unlock(&drvdata->spinlock);
887 return size;
889 static DEVICE_ATTR_RW(addr_start);
891 static ssize_t addr_stop_show(struct device *dev,
892 struct device_attribute *attr, char *buf)
894 u8 idx;
895 unsigned long val;
896 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
898 spin_lock(&drvdata->spinlock);
899 idx = drvdata->addr_idx;
900 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
901 drvdata->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
902 spin_unlock(&drvdata->spinlock);
903 return -EPERM;
906 val = drvdata->addr_val[idx];
907 spin_unlock(&drvdata->spinlock);
909 return sprintf(buf, "%#lx\n", val);
912 static ssize_t addr_stop_store(struct device *dev,
913 struct device_attribute *attr,
914 const char *buf, size_t size)
916 u8 idx;
917 int ret;
918 unsigned long val;
919 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
921 ret = kstrtoul(buf, 16, &val);
922 if (ret)
923 return ret;
925 spin_lock(&drvdata->spinlock);
926 idx = drvdata->addr_idx;
927 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
928 drvdata->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
929 spin_unlock(&drvdata->spinlock);
930 return -EPERM;
933 drvdata->addr_val[idx] = val;
934 drvdata->addr_type[idx] = ETM_ADDR_TYPE_STOP;
935 drvdata->startstop_ctrl |= (1 << (idx + 16));
936 drvdata->enable_ctrl1 |= ETMTECR1_START_STOP;
937 spin_unlock(&drvdata->spinlock);
939 return size;
941 static DEVICE_ATTR_RW(addr_stop);
943 static ssize_t addr_acctype_show(struct device *dev,
944 struct device_attribute *attr, char *buf)
946 unsigned long val;
947 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
949 spin_lock(&drvdata->spinlock);
950 val = drvdata->addr_acctype[drvdata->addr_idx];
951 spin_unlock(&drvdata->spinlock);
953 return sprintf(buf, "%#lx\n", val);
956 static ssize_t addr_acctype_store(struct device *dev,
957 struct device_attribute *attr,
958 const char *buf, size_t size)
960 int ret;
961 unsigned long val;
962 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
964 ret = kstrtoul(buf, 16, &val);
965 if (ret)
966 return ret;
968 spin_lock(&drvdata->spinlock);
969 drvdata->addr_acctype[drvdata->addr_idx] = val;
970 spin_unlock(&drvdata->spinlock);
972 return size;
974 static DEVICE_ATTR_RW(addr_acctype);
976 static ssize_t cntr_idx_show(struct device *dev,
977 struct device_attribute *attr, char *buf)
979 unsigned long val;
980 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
982 val = drvdata->cntr_idx;
983 return sprintf(buf, "%#lx\n", val);
986 static ssize_t cntr_idx_store(struct device *dev,
987 struct device_attribute *attr,
988 const char *buf, size_t size)
990 int ret;
991 unsigned long val;
992 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
994 ret = kstrtoul(buf, 16, &val);
995 if (ret)
996 return ret;
998 if (val >= drvdata->nr_cntr)
999 return -EINVAL;
1001 * Use spinlock to ensure index doesn't change while it gets
1002 * dereferenced multiple times within a spinlock block elsewhere.
1004 spin_lock(&drvdata->spinlock);
1005 drvdata->cntr_idx = val;
1006 spin_unlock(&drvdata->spinlock);
1008 return size;
1010 static DEVICE_ATTR_RW(cntr_idx);
1012 static ssize_t cntr_rld_val_show(struct device *dev,
1013 struct device_attribute *attr, char *buf)
1015 unsigned long val;
1016 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1018 spin_lock(&drvdata->spinlock);
1019 val = drvdata->cntr_rld_val[drvdata->cntr_idx];
1020 spin_unlock(&drvdata->spinlock);
1022 return sprintf(buf, "%#lx\n", val);
1025 static ssize_t cntr_rld_val_store(struct device *dev,
1026 struct device_attribute *attr,
1027 const char *buf, size_t size)
1029 int ret;
1030 unsigned long val;
1031 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1033 ret = kstrtoul(buf, 16, &val);
1034 if (ret)
1035 return ret;
1037 spin_lock(&drvdata->spinlock);
1038 drvdata->cntr_rld_val[drvdata->cntr_idx] = val;
1039 spin_unlock(&drvdata->spinlock);
1041 return size;
1043 static DEVICE_ATTR_RW(cntr_rld_val);
1045 static ssize_t cntr_event_show(struct device *dev,
1046 struct device_attribute *attr, char *buf)
1048 unsigned long val;
1049 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1051 spin_lock(&drvdata->spinlock);
1052 val = drvdata->cntr_event[drvdata->cntr_idx];
1053 spin_unlock(&drvdata->spinlock);
1055 return sprintf(buf, "%#lx\n", val);
1058 static ssize_t cntr_event_store(struct device *dev,
1059 struct device_attribute *attr,
1060 const char *buf, size_t size)
1062 int ret;
1063 unsigned long val;
1064 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1066 ret = kstrtoul(buf, 16, &val);
1067 if (ret)
1068 return ret;
1070 spin_lock(&drvdata->spinlock);
1071 drvdata->cntr_event[drvdata->cntr_idx] = val & ETM_EVENT_MASK;
1072 spin_unlock(&drvdata->spinlock);
1074 return size;
1076 static DEVICE_ATTR_RW(cntr_event);
1078 static ssize_t cntr_rld_event_show(struct device *dev,
1079 struct device_attribute *attr, char *buf)
1081 unsigned long val;
1082 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1084 spin_lock(&drvdata->spinlock);
1085 val = drvdata->cntr_rld_event[drvdata->cntr_idx];
1086 spin_unlock(&drvdata->spinlock);
1088 return sprintf(buf, "%#lx\n", val);
1091 static ssize_t cntr_rld_event_store(struct device *dev,
1092 struct device_attribute *attr,
1093 const char *buf, size_t size)
1095 int ret;
1096 unsigned long val;
1097 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1099 ret = kstrtoul(buf, 16, &val);
1100 if (ret)
1101 return ret;
1103 spin_lock(&drvdata->spinlock);
1104 drvdata->cntr_rld_event[drvdata->cntr_idx] = val & ETM_EVENT_MASK;
1105 spin_unlock(&drvdata->spinlock);
1107 return size;
1109 static DEVICE_ATTR_RW(cntr_rld_event);
1111 static ssize_t cntr_val_show(struct device *dev,
1112 struct device_attribute *attr, char *buf)
1114 int i, ret = 0;
1115 u32 val;
1116 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1118 if (!drvdata->enable) {
1119 spin_lock(&drvdata->spinlock);
1120 for (i = 0; i < drvdata->nr_cntr; i++)
1121 ret += sprintf(buf, "counter %d: %x\n",
1122 i, drvdata->cntr_val[i]);
1123 spin_unlock(&drvdata->spinlock);
1124 return ret;
1127 for (i = 0; i < drvdata->nr_cntr; i++) {
1128 val = etm_readl(drvdata, ETMCNTVRn(i));
1129 ret += sprintf(buf, "counter %d: %x\n", i, val);
1132 return ret;
1135 static ssize_t cntr_val_store(struct device *dev,
1136 struct device_attribute *attr,
1137 const char *buf, size_t size)
1139 int ret;
1140 unsigned long val;
1141 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1143 ret = kstrtoul(buf, 16, &val);
1144 if (ret)
1145 return ret;
1147 spin_lock(&drvdata->spinlock);
1148 drvdata->cntr_val[drvdata->cntr_idx] = val;
1149 spin_unlock(&drvdata->spinlock);
1151 return size;
1153 static DEVICE_ATTR_RW(cntr_val);
1155 static ssize_t seq_12_event_show(struct device *dev,
1156 struct device_attribute *attr, char *buf)
1158 unsigned long val;
1159 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1161 val = drvdata->seq_12_event;
1162 return sprintf(buf, "%#lx\n", val);
1165 static ssize_t seq_12_event_store(struct device *dev,
1166 struct device_attribute *attr,
1167 const char *buf, size_t size)
1169 int ret;
1170 unsigned long val;
1171 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1173 ret = kstrtoul(buf, 16, &val);
1174 if (ret)
1175 return ret;
1177 drvdata->seq_12_event = val & ETM_EVENT_MASK;
1178 return size;
1180 static DEVICE_ATTR_RW(seq_12_event);
1182 static ssize_t seq_21_event_show(struct device *dev,
1183 struct device_attribute *attr, char *buf)
1185 unsigned long val;
1186 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1188 val = drvdata->seq_21_event;
1189 return sprintf(buf, "%#lx\n", val);
1192 static ssize_t seq_21_event_store(struct device *dev,
1193 struct device_attribute *attr,
1194 const char *buf, size_t size)
1196 int ret;
1197 unsigned long val;
1198 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1200 ret = kstrtoul(buf, 16, &val);
1201 if (ret)
1202 return ret;
1204 drvdata->seq_21_event = val & ETM_EVENT_MASK;
1205 return size;
1207 static DEVICE_ATTR_RW(seq_21_event);
1209 static ssize_t seq_23_event_show(struct device *dev,
1210 struct device_attribute *attr, char *buf)
1212 unsigned long val;
1213 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1215 val = drvdata->seq_23_event;
1216 return sprintf(buf, "%#lx\n", val);
1219 static ssize_t seq_23_event_store(struct device *dev,
1220 struct device_attribute *attr,
1221 const char *buf, size_t size)
1223 int ret;
1224 unsigned long val;
1225 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1227 ret = kstrtoul(buf, 16, &val);
1228 if (ret)
1229 return ret;
1231 drvdata->seq_23_event = val & ETM_EVENT_MASK;
1232 return size;
1234 static DEVICE_ATTR_RW(seq_23_event);
1236 static ssize_t seq_31_event_show(struct device *dev,
1237 struct device_attribute *attr, char *buf)
1239 unsigned long val;
1240 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1242 val = drvdata->seq_31_event;
1243 return sprintf(buf, "%#lx\n", val);
1246 static ssize_t seq_31_event_store(struct device *dev,
1247 struct device_attribute *attr,
1248 const char *buf, size_t size)
1250 int ret;
1251 unsigned long val;
1252 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1254 ret = kstrtoul(buf, 16, &val);
1255 if (ret)
1256 return ret;
1258 drvdata->seq_31_event = val & ETM_EVENT_MASK;
1259 return size;
1261 static DEVICE_ATTR_RW(seq_31_event);
1263 static ssize_t seq_32_event_show(struct device *dev,
1264 struct device_attribute *attr, char *buf)
1266 unsigned long val;
1267 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1269 val = drvdata->seq_32_event;
1270 return sprintf(buf, "%#lx\n", val);
1273 static ssize_t seq_32_event_store(struct device *dev,
1274 struct device_attribute *attr,
1275 const char *buf, size_t size)
1277 int ret;
1278 unsigned long val;
1279 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1281 ret = kstrtoul(buf, 16, &val);
1282 if (ret)
1283 return ret;
1285 drvdata->seq_32_event = val & ETM_EVENT_MASK;
1286 return size;
1288 static DEVICE_ATTR_RW(seq_32_event);
1290 static ssize_t seq_13_event_show(struct device *dev,
1291 struct device_attribute *attr, char *buf)
1293 unsigned long val;
1294 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1296 val = drvdata->seq_13_event;
1297 return sprintf(buf, "%#lx\n", val);
1300 static ssize_t seq_13_event_store(struct device *dev,
1301 struct device_attribute *attr,
1302 const char *buf, size_t size)
1304 int ret;
1305 unsigned long val;
1306 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1308 ret = kstrtoul(buf, 16, &val);
1309 if (ret)
1310 return ret;
1312 drvdata->seq_13_event = val & ETM_EVENT_MASK;
1313 return size;
1315 static DEVICE_ATTR_RW(seq_13_event);
1317 static ssize_t seq_curr_state_show(struct device *dev,
1318 struct device_attribute *attr, char *buf)
1320 int ret;
1321 unsigned long val, flags;
1322 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1324 if (!drvdata->enable) {
1325 val = drvdata->seq_curr_state;
1326 goto out;
1329 ret = clk_prepare_enable(drvdata->clk);
1330 if (ret)
1331 return ret;
1333 spin_lock_irqsave(&drvdata->spinlock, flags);
1335 CS_UNLOCK(drvdata->base);
1336 val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
1337 CS_LOCK(drvdata->base);
1339 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1340 clk_disable_unprepare(drvdata->clk);
1341 out:
1342 return sprintf(buf, "%#lx\n", val);
1345 static ssize_t seq_curr_state_store(struct device *dev,
1346 struct device_attribute *attr,
1347 const char *buf, size_t size)
1349 int ret;
1350 unsigned long val;
1351 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1353 ret = kstrtoul(buf, 16, &val);
1354 if (ret)
1355 return ret;
1357 if (val > ETM_SEQ_STATE_MAX_VAL)
1358 return -EINVAL;
1360 drvdata->seq_curr_state = val;
1362 return size;
1364 static DEVICE_ATTR_RW(seq_curr_state);
1366 static ssize_t ctxid_idx_show(struct device *dev,
1367 struct device_attribute *attr, char *buf)
1369 unsigned long val;
1370 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1372 val = drvdata->ctxid_idx;
1373 return sprintf(buf, "%#lx\n", val);
1376 static ssize_t ctxid_idx_store(struct device *dev,
1377 struct device_attribute *attr,
1378 const char *buf, size_t size)
1380 int ret;
1381 unsigned long val;
1382 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1384 ret = kstrtoul(buf, 16, &val);
1385 if (ret)
1386 return ret;
1388 if (val >= drvdata->nr_ctxid_cmp)
1389 return -EINVAL;
1392 * Use spinlock to ensure index doesn't change while it gets
1393 * dereferenced multiple times within a spinlock block elsewhere.
1395 spin_lock(&drvdata->spinlock);
1396 drvdata->ctxid_idx = val;
1397 spin_unlock(&drvdata->spinlock);
1399 return size;
1401 static DEVICE_ATTR_RW(ctxid_idx);
1403 static ssize_t ctxid_val_show(struct device *dev,
1404 struct device_attribute *attr, char *buf)
1406 unsigned long val;
1407 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1409 spin_lock(&drvdata->spinlock);
1410 val = drvdata->ctxid_val[drvdata->ctxid_idx];
1411 spin_unlock(&drvdata->spinlock);
1413 return sprintf(buf, "%#lx\n", val);
1416 static ssize_t ctxid_val_store(struct device *dev,
1417 struct device_attribute *attr,
1418 const char *buf, size_t size)
1420 int ret;
1421 unsigned long val;
1422 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1424 ret = kstrtoul(buf, 16, &val);
1425 if (ret)
1426 return ret;
1428 spin_lock(&drvdata->spinlock);
1429 drvdata->ctxid_val[drvdata->ctxid_idx] = val;
1430 spin_unlock(&drvdata->spinlock);
1432 return size;
1434 static DEVICE_ATTR_RW(ctxid_val);
1436 static ssize_t ctxid_mask_show(struct device *dev,
1437 struct device_attribute *attr, char *buf)
1439 unsigned long val;
1440 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1442 val = drvdata->ctxid_mask;
1443 return sprintf(buf, "%#lx\n", val);
1446 static ssize_t ctxid_mask_store(struct device *dev,
1447 struct device_attribute *attr,
1448 const char *buf, size_t size)
1450 int ret;
1451 unsigned long val;
1452 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1454 ret = kstrtoul(buf, 16, &val);
1455 if (ret)
1456 return ret;
1458 drvdata->ctxid_mask = val;
1459 return size;
1461 static DEVICE_ATTR_RW(ctxid_mask);
1463 static ssize_t sync_freq_show(struct device *dev,
1464 struct device_attribute *attr, char *buf)
1466 unsigned long val;
1467 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1469 val = drvdata->sync_freq;
1470 return sprintf(buf, "%#lx\n", val);
1473 static ssize_t sync_freq_store(struct device *dev,
1474 struct device_attribute *attr,
1475 const char *buf, size_t size)
1477 int ret;
1478 unsigned long val;
1479 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1481 ret = kstrtoul(buf, 16, &val);
1482 if (ret)
1483 return ret;
1485 drvdata->sync_freq = val & ETM_SYNC_MASK;
1486 return size;
1488 static DEVICE_ATTR_RW(sync_freq);
1490 static ssize_t timestamp_event_show(struct device *dev,
1491 struct device_attribute *attr, char *buf)
1493 unsigned long val;
1494 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1496 val = drvdata->timestamp_event;
1497 return sprintf(buf, "%#lx\n", val);
1500 static ssize_t timestamp_event_store(struct device *dev,
1501 struct device_attribute *attr,
1502 const char *buf, size_t size)
1504 int ret;
1505 unsigned long val;
1506 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1508 ret = kstrtoul(buf, 16, &val);
1509 if (ret)
1510 return ret;
1512 drvdata->timestamp_event = val & ETM_EVENT_MASK;
1513 return size;
1515 static DEVICE_ATTR_RW(timestamp_event);
1517 static ssize_t status_show(struct device *dev,
1518 struct device_attribute *attr, char *buf)
1520 int ret;
1521 unsigned long flags;
1522 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1524 ret = clk_prepare_enable(drvdata->clk);
1525 if (ret)
1526 return ret;
1528 spin_lock_irqsave(&drvdata->spinlock, flags);
1530 CS_UNLOCK(drvdata->base);
1531 ret = sprintf(buf,
1532 "ETMCCR: 0x%08x\n"
1533 "ETMCCER: 0x%08x\n"
1534 "ETMSCR: 0x%08x\n"
1535 "ETMIDR: 0x%08x\n"
1536 "ETMCR: 0x%08x\n"
1537 "ETMTRACEIDR: 0x%08x\n"
1538 "Enable event: 0x%08x\n"
1539 "Enable start/stop: 0x%08x\n"
1540 "Enable control: CR1 0x%08x CR2 0x%08x\n"
1541 "CPU affinity: %d\n",
1542 drvdata->etmccr, drvdata->etmccer,
1543 etm_readl(drvdata, ETMSCR), etm_readl(drvdata, ETMIDR),
1544 etm_readl(drvdata, ETMCR), etm_trace_id_simple(drvdata),
1545 etm_readl(drvdata, ETMTEEVR),
1546 etm_readl(drvdata, ETMTSSCR),
1547 etm_readl(drvdata, ETMTECR1),
1548 etm_readl(drvdata, ETMTECR2),
1549 drvdata->cpu);
1550 CS_LOCK(drvdata->base);
1552 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1553 clk_disable_unprepare(drvdata->clk);
1555 return ret;
1557 static DEVICE_ATTR_RO(status);
1559 static ssize_t traceid_show(struct device *dev,
1560 struct device_attribute *attr, char *buf)
1562 int ret;
1563 unsigned long val, flags;
1564 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1566 if (!drvdata->enable) {
1567 val = drvdata->traceid;
1568 goto out;
1571 ret = clk_prepare_enable(drvdata->clk);
1572 if (ret)
1573 return ret;
1575 spin_lock_irqsave(&drvdata->spinlock, flags);
1576 CS_UNLOCK(drvdata->base);
1578 val = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
1580 CS_LOCK(drvdata->base);
1581 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1582 clk_disable_unprepare(drvdata->clk);
1583 out:
1584 return sprintf(buf, "%#lx\n", val);
1587 static ssize_t traceid_store(struct device *dev,
1588 struct device_attribute *attr,
1589 const char *buf, size_t size)
1591 int ret;
1592 unsigned long val;
1593 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1595 ret = kstrtoul(buf, 16, &val);
1596 if (ret)
1597 return ret;
1599 drvdata->traceid = val & ETM_TRACEID_MASK;
1600 return size;
1602 static DEVICE_ATTR_RW(traceid);
1604 static struct attribute *coresight_etm_attrs[] = {
1605 &dev_attr_nr_addr_cmp.attr,
1606 &dev_attr_nr_cntr.attr,
1607 &dev_attr_nr_ctxid_cmp.attr,
1608 &dev_attr_etmsr.attr,
1609 &dev_attr_reset.attr,
1610 &dev_attr_mode.attr,
1611 &dev_attr_trigger_event.attr,
1612 &dev_attr_enable_event.attr,
1613 &dev_attr_fifofull_level.attr,
1614 &dev_attr_addr_idx.attr,
1615 &dev_attr_addr_single.attr,
1616 &dev_attr_addr_range.attr,
1617 &dev_attr_addr_start.attr,
1618 &dev_attr_addr_stop.attr,
1619 &dev_attr_addr_acctype.attr,
1620 &dev_attr_cntr_idx.attr,
1621 &dev_attr_cntr_rld_val.attr,
1622 &dev_attr_cntr_event.attr,
1623 &dev_attr_cntr_rld_event.attr,
1624 &dev_attr_cntr_val.attr,
1625 &dev_attr_seq_12_event.attr,
1626 &dev_attr_seq_21_event.attr,
1627 &dev_attr_seq_23_event.attr,
1628 &dev_attr_seq_31_event.attr,
1629 &dev_attr_seq_32_event.attr,
1630 &dev_attr_seq_13_event.attr,
1631 &dev_attr_seq_curr_state.attr,
1632 &dev_attr_ctxid_idx.attr,
1633 &dev_attr_ctxid_val.attr,
1634 &dev_attr_ctxid_mask.attr,
1635 &dev_attr_sync_freq.attr,
1636 &dev_attr_timestamp_event.attr,
1637 &dev_attr_status.attr,
1638 &dev_attr_traceid.attr,
1639 NULL,
1641 ATTRIBUTE_GROUPS(coresight_etm);
1643 static int etm_cpu_callback(struct notifier_block *nfb, unsigned long action,
1644 void *hcpu)
1646 unsigned int cpu = (unsigned long)hcpu;
1648 if (!etmdrvdata[cpu])
1649 goto out;
1651 switch (action & (~CPU_TASKS_FROZEN)) {
1652 case CPU_STARTING:
1653 spin_lock(&etmdrvdata[cpu]->spinlock);
1654 if (!etmdrvdata[cpu]->os_unlock) {
1655 etm_os_unlock(etmdrvdata[cpu]);
1656 etmdrvdata[cpu]->os_unlock = true;
1659 if (etmdrvdata[cpu]->enable)
1660 etm_enable_hw(etmdrvdata[cpu]);
1661 spin_unlock(&etmdrvdata[cpu]->spinlock);
1662 break;
1664 case CPU_ONLINE:
1665 if (etmdrvdata[cpu]->boot_enable &&
1666 !etmdrvdata[cpu]->sticky_enable)
1667 coresight_enable(etmdrvdata[cpu]->csdev);
1668 break;
1670 case CPU_DYING:
1671 spin_lock(&etmdrvdata[cpu]->spinlock);
1672 if (etmdrvdata[cpu]->enable)
1673 etm_disable_hw(etmdrvdata[cpu]);
1674 spin_unlock(&etmdrvdata[cpu]->spinlock);
1675 break;
1677 out:
1678 return NOTIFY_OK;
1681 static struct notifier_block etm_cpu_notifier = {
1682 .notifier_call = etm_cpu_callback,
1685 static bool etm_arch_supported(u8 arch)
1687 switch (arch) {
1688 case ETM_ARCH_V3_3:
1689 break;
1690 case ETM_ARCH_V3_5:
1691 break;
1692 case PFT_ARCH_V1_0:
1693 break;
1694 case PFT_ARCH_V1_1:
1695 break;
1696 default:
1697 return false;
1699 return true;
1702 static void etm_init_arch_data(void *info)
1704 u32 etmidr;
1705 u32 etmccr;
1706 struct etm_drvdata *drvdata = info;
1708 CS_UNLOCK(drvdata->base);
1710 /* First dummy read */
1711 (void)etm_readl(drvdata, ETMPDSR);
1712 /* Provide power to ETM: ETMPDCR[3] == 1 */
1713 etm_set_pwrup(drvdata);
1715 * Clear power down bit since when this bit is set writes to
1716 * certain registers might be ignored.
1718 etm_clr_pwrdwn(drvdata);
1720 * Set prog bit. It will be set from reset but this is included to
1721 * ensure it is set
1723 etm_set_prog(drvdata);
1725 /* Find all capabilities */
1726 etmidr = etm_readl(drvdata, ETMIDR);
1727 drvdata->arch = BMVAL(etmidr, 4, 11);
1728 drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
1730 drvdata->etmccer = etm_readl(drvdata, ETMCCER);
1731 etmccr = etm_readl(drvdata, ETMCCR);
1732 drvdata->etmccr = etmccr;
1733 drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
1734 drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
1735 drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
1736 drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
1737 drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
1739 etm_set_pwrdwn(drvdata);
1740 etm_clr_pwrup(drvdata);
1741 CS_LOCK(drvdata->base);
1744 static void etm_init_default_data(struct etm_drvdata *drvdata)
1747 * A trace ID of value 0 is invalid, so let's start at some
1748 * random value that fits in 7 bits and will be just as good.
1750 static int etm3x_traceid = 0x10;
1752 u32 flags = (1 << 0 | /* instruction execute*/
1753 3 << 3 | /* ARM instruction */
1754 0 << 5 | /* No data value comparison */
1755 0 << 7 | /* No exact mach */
1756 0 << 8 | /* Ignore context ID */
1757 0 << 10); /* Security ignored */
1760 * Initial configuration only - guarantees sources handled by
1761 * this driver have a unique ID at startup time but not between
1762 * all other types of sources. For that we lean on the core
1763 * framework.
1765 drvdata->traceid = etm3x_traceid++;
1766 drvdata->ctrl = (ETMCR_CYC_ACC | ETMCR_TIMESTAMP_EN);
1767 drvdata->enable_ctrl1 = ETMTECR1_ADDR_COMP_1;
1768 if (drvdata->nr_addr_cmp >= 2) {
1769 drvdata->addr_val[0] = (u32) _stext;
1770 drvdata->addr_val[1] = (u32) _etext;
1771 drvdata->addr_acctype[0] = flags;
1772 drvdata->addr_acctype[1] = flags;
1773 drvdata->addr_type[0] = ETM_ADDR_TYPE_RANGE;
1774 drvdata->addr_type[1] = ETM_ADDR_TYPE_RANGE;
1777 etm_set_default(drvdata);
1780 static int etm_probe(struct amba_device *adev, const struct amba_id *id)
1782 int ret;
1783 void __iomem *base;
1784 struct device *dev = &adev->dev;
1785 struct coresight_platform_data *pdata = NULL;
1786 struct etm_drvdata *drvdata;
1787 struct resource *res = &adev->res;
1788 struct coresight_desc *desc;
1789 struct device_node *np = adev->dev.of_node;
1791 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1792 if (!desc)
1793 return -ENOMEM;
1795 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1796 if (!drvdata)
1797 return -ENOMEM;
1799 if (np) {
1800 pdata = of_get_coresight_platform_data(dev, np);
1801 if (IS_ERR(pdata))
1802 return PTR_ERR(pdata);
1804 adev->dev.platform_data = pdata;
1805 drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
1808 drvdata->dev = &adev->dev;
1809 dev_set_drvdata(dev, drvdata);
1811 /* Validity for the resource is already checked by the AMBA core */
1812 base = devm_ioremap_resource(dev, res);
1813 if (IS_ERR(base))
1814 return PTR_ERR(base);
1816 drvdata->base = base;
1818 spin_lock_init(&drvdata->spinlock);
1820 drvdata->clk = adev->pclk;
1821 ret = clk_prepare_enable(drvdata->clk);
1822 if (ret)
1823 return ret;
1825 drvdata->cpu = pdata ? pdata->cpu : 0;
1827 get_online_cpus();
1828 etmdrvdata[drvdata->cpu] = drvdata;
1830 if (!smp_call_function_single(drvdata->cpu, etm_os_unlock, drvdata, 1))
1831 drvdata->os_unlock = true;
1833 if (smp_call_function_single(drvdata->cpu,
1834 etm_init_arch_data, drvdata, 1))
1835 dev_err(dev, "ETM arch init failed\n");
1837 if (!etm_count++)
1838 register_hotcpu_notifier(&etm_cpu_notifier);
1840 put_online_cpus();
1842 if (etm_arch_supported(drvdata->arch) == false) {
1843 ret = -EINVAL;
1844 goto err_arch_supported;
1846 etm_init_default_data(drvdata);
1848 clk_disable_unprepare(drvdata->clk);
1850 desc->type = CORESIGHT_DEV_TYPE_SOURCE;
1851 desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
1852 desc->ops = &etm_cs_ops;
1853 desc->pdata = pdata;
1854 desc->dev = dev;
1855 desc->groups = coresight_etm_groups;
1856 drvdata->csdev = coresight_register(desc);
1857 if (IS_ERR(drvdata->csdev)) {
1858 ret = PTR_ERR(drvdata->csdev);
1859 goto err_arch_supported;
1862 dev_info(dev, "ETM initialized\n");
1864 if (boot_enable) {
1865 coresight_enable(drvdata->csdev);
1866 drvdata->boot_enable = true;
1869 return 0;
1871 err_arch_supported:
1872 clk_disable_unprepare(drvdata->clk);
1873 if (--etm_count == 0)
1874 unregister_hotcpu_notifier(&etm_cpu_notifier);
1875 return ret;
1878 static int etm_remove(struct amba_device *adev)
1880 struct etm_drvdata *drvdata = amba_get_drvdata(adev);
1882 coresight_unregister(drvdata->csdev);
1883 if (--etm_count == 0)
1884 unregister_hotcpu_notifier(&etm_cpu_notifier);
1886 return 0;
1889 static struct amba_id etm_ids[] = {
1890 { /* ETM 3.3 */
1891 .id = 0x0003b921,
1892 .mask = 0x0003ffff,
1894 { /* ETM 3.5 */
1895 .id = 0x0003b956,
1896 .mask = 0x0003ffff,
1898 { /* PTM 1.0 */
1899 .id = 0x0003b950,
1900 .mask = 0x0003ffff,
1902 { /* PTM 1.1 */
1903 .id = 0x0003b95f,
1904 .mask = 0x0003ffff,
1906 { 0, 0},
1909 static struct amba_driver etm_driver = {
1910 .drv = {
1911 .name = "coresight-etm3x",
1912 .owner = THIS_MODULE,
1914 .probe = etm_probe,
1915 .remove = etm_remove,
1916 .id_table = etm_ids,
1919 int __init etm_init(void)
1921 return amba_driver_register(&etm_driver);
1923 module_init(etm_init);
1925 void __exit etm_exit(void)
1927 amba_driver_unregister(&etm_driver);
1929 module_exit(etm_exit);
1931 MODULE_LICENSE("GPL v2");
1932 MODULE_DESCRIPTION("CoreSight Program Flow Trace driver");