of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight-etm3x.c
blobd630b7ece73521ccf8cd7b320ebd75ecc92eb1d3
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/pm_runtime.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 <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 */
42 static int etm_count;
43 static struct etm_drvdata *etmdrvdata[NR_CPUS];
45 static inline void etm_writel(struct etm_drvdata *drvdata,
46 u32 val, u32 off)
48 if (drvdata->use_cp14) {
49 if (etm_writel_cp14(off, val)) {
50 dev_err(drvdata->dev,
51 "invalid CP14 access to ETM reg: %#x", off);
53 } else {
54 writel_relaxed(val, drvdata->base + off);
58 static inline unsigned int etm_readl(struct etm_drvdata *drvdata, u32 off)
60 u32 val;
62 if (drvdata->use_cp14) {
63 if (etm_readl_cp14(off, &val)) {
64 dev_err(drvdata->dev,
65 "invalid CP14 access to ETM reg: %#x", off);
67 } else {
68 val = readl_relaxed(drvdata->base + off);
71 return val;
75 * Memory mapped writes to clear os lock are not supported on some processors
76 * and OS lock must be unlocked before any memory mapped access on such
77 * processors, otherwise memory mapped reads/writes will be invalid.
79 static void etm_os_unlock(void *info)
81 struct etm_drvdata *drvdata = (struct etm_drvdata *)info;
82 /* Writing any value to ETMOSLAR unlocks the trace registers */
83 etm_writel(drvdata, 0x0, ETMOSLAR);
84 isb();
87 static void etm_set_pwrdwn(struct etm_drvdata *drvdata)
89 u32 etmcr;
91 /* Ensure pending cp14 accesses complete before setting pwrdwn */
92 mb();
93 isb();
94 etmcr = etm_readl(drvdata, ETMCR);
95 etmcr |= ETMCR_PWD_DWN;
96 etm_writel(drvdata, etmcr, ETMCR);
99 static void etm_clr_pwrdwn(struct etm_drvdata *drvdata)
101 u32 etmcr;
103 etmcr = etm_readl(drvdata, ETMCR);
104 etmcr &= ~ETMCR_PWD_DWN;
105 etm_writel(drvdata, etmcr, ETMCR);
106 /* Ensure pwrup completes before subsequent cp14 accesses */
107 mb();
108 isb();
111 static void etm_set_pwrup(struct etm_drvdata *drvdata)
113 u32 etmpdcr;
115 etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
116 etmpdcr |= ETMPDCR_PWD_UP;
117 writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
118 /* Ensure pwrup completes before subsequent cp14 accesses */
119 mb();
120 isb();
123 static void etm_clr_pwrup(struct etm_drvdata *drvdata)
125 u32 etmpdcr;
127 /* Ensure pending cp14 accesses complete before clearing pwrup */
128 mb();
129 isb();
130 etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
131 etmpdcr &= ~ETMPDCR_PWD_UP;
132 writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
136 * coresight_timeout_etm - loop until a bit has changed to a specific state.
137 * @drvdata: etm's private data structure.
138 * @offset: address of a register, starting from @addr.
139 * @position: the position of the bit of interest.
140 * @value: the value the bit should have.
142 * Basically the same as @coresight_timeout except for the register access
143 * method where we have to account for CP14 configurations.
145 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
146 * TIMEOUT_US has elapsed, which ever happens first.
149 static int coresight_timeout_etm(struct etm_drvdata *drvdata, u32 offset,
150 int position, int value)
152 int i;
153 u32 val;
155 for (i = TIMEOUT_US; i > 0; i--) {
156 val = etm_readl(drvdata, offset);
157 /* Waiting on the bit to go from 0 to 1 */
158 if (value) {
159 if (val & BIT(position))
160 return 0;
161 /* Waiting on the bit to go from 1 to 0 */
162 } else {
163 if (!(val & BIT(position)))
164 return 0;
168 * Delay is arbitrary - the specification doesn't say how long
169 * we are expected to wait. Extra check required to make sure
170 * we don't wait needlessly on the last iteration.
172 if (i - 1)
173 udelay(1);
176 return -EAGAIN;
180 static void etm_set_prog(struct etm_drvdata *drvdata)
182 u32 etmcr;
184 etmcr = etm_readl(drvdata, ETMCR);
185 etmcr |= ETMCR_ETM_PRG;
186 etm_writel(drvdata, etmcr, ETMCR);
188 * Recommended by spec for cp14 accesses to ensure etmcr write is
189 * complete before polling etmsr
191 isb();
192 if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 1)) {
193 dev_err(drvdata->dev,
194 "%s: timeout observed when probing at offset %#x\n",
195 __func__, ETMSR);
199 static void etm_clr_prog(struct etm_drvdata *drvdata)
201 u32 etmcr;
203 etmcr = etm_readl(drvdata, ETMCR);
204 etmcr &= ~ETMCR_ETM_PRG;
205 etm_writel(drvdata, etmcr, ETMCR);
207 * Recommended by spec for cp14 accesses to ensure etmcr write is
208 * complete before polling etmsr
210 isb();
211 if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 0)) {
212 dev_err(drvdata->dev,
213 "%s: timeout observed when probing at offset %#x\n",
214 __func__, ETMSR);
218 static void etm_set_default(struct etm_drvdata *drvdata)
220 int i;
222 drvdata->trigger_event = ETM_DEFAULT_EVENT_VAL;
223 drvdata->enable_event = ETM_HARD_WIRE_RES_A;
225 drvdata->seq_12_event = ETM_DEFAULT_EVENT_VAL;
226 drvdata->seq_21_event = ETM_DEFAULT_EVENT_VAL;
227 drvdata->seq_23_event = ETM_DEFAULT_EVENT_VAL;
228 drvdata->seq_31_event = ETM_DEFAULT_EVENT_VAL;
229 drvdata->seq_32_event = ETM_DEFAULT_EVENT_VAL;
230 drvdata->seq_13_event = ETM_DEFAULT_EVENT_VAL;
231 drvdata->timestamp_event = ETM_DEFAULT_EVENT_VAL;
233 for (i = 0; i < drvdata->nr_cntr; i++) {
234 drvdata->cntr_rld_val[i] = 0x0;
235 drvdata->cntr_event[i] = ETM_DEFAULT_EVENT_VAL;
236 drvdata->cntr_rld_event[i] = ETM_DEFAULT_EVENT_VAL;
237 drvdata->cntr_val[i] = 0x0;
240 drvdata->seq_curr_state = 0x0;
241 drvdata->ctxid_idx = 0x0;
242 for (i = 0; i < drvdata->nr_ctxid_cmp; i++) {
243 drvdata->ctxid_pid[i] = 0x0;
244 drvdata->ctxid_vpid[i] = 0x0;
247 drvdata->ctxid_mask = 0x0;
250 static void etm_enable_hw(void *info)
252 int i;
253 u32 etmcr;
254 struct etm_drvdata *drvdata = info;
256 CS_UNLOCK(drvdata->base);
258 /* Turn engine on */
259 etm_clr_pwrdwn(drvdata);
260 /* Apply power to trace registers */
261 etm_set_pwrup(drvdata);
262 /* Make sure all registers are accessible */
263 etm_os_unlock(drvdata);
265 etm_set_prog(drvdata);
267 etmcr = etm_readl(drvdata, ETMCR);
268 etmcr &= (ETMCR_PWD_DWN | ETMCR_ETM_PRG);
269 etmcr |= drvdata->port_size;
270 etm_writel(drvdata, drvdata->ctrl | etmcr, ETMCR);
271 etm_writel(drvdata, drvdata->trigger_event, ETMTRIGGER);
272 etm_writel(drvdata, drvdata->startstop_ctrl, ETMTSSCR);
273 etm_writel(drvdata, drvdata->enable_event, ETMTEEVR);
274 etm_writel(drvdata, drvdata->enable_ctrl1, ETMTECR1);
275 etm_writel(drvdata, drvdata->fifofull_level, ETMFFLR);
276 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
277 etm_writel(drvdata, drvdata->addr_val[i], ETMACVRn(i));
278 etm_writel(drvdata, drvdata->addr_acctype[i], ETMACTRn(i));
280 for (i = 0; i < drvdata->nr_cntr; i++) {
281 etm_writel(drvdata, drvdata->cntr_rld_val[i], ETMCNTRLDVRn(i));
282 etm_writel(drvdata, drvdata->cntr_event[i], ETMCNTENRn(i));
283 etm_writel(drvdata, drvdata->cntr_rld_event[i],
284 ETMCNTRLDEVRn(i));
285 etm_writel(drvdata, drvdata->cntr_val[i], ETMCNTVRn(i));
287 etm_writel(drvdata, drvdata->seq_12_event, ETMSQ12EVR);
288 etm_writel(drvdata, drvdata->seq_21_event, ETMSQ21EVR);
289 etm_writel(drvdata, drvdata->seq_23_event, ETMSQ23EVR);
290 etm_writel(drvdata, drvdata->seq_31_event, ETMSQ31EVR);
291 etm_writel(drvdata, drvdata->seq_32_event, ETMSQ32EVR);
292 etm_writel(drvdata, drvdata->seq_13_event, ETMSQ13EVR);
293 etm_writel(drvdata, drvdata->seq_curr_state, ETMSQR);
294 for (i = 0; i < drvdata->nr_ext_out; i++)
295 etm_writel(drvdata, ETM_DEFAULT_EVENT_VAL, ETMEXTOUTEVRn(i));
296 for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
297 etm_writel(drvdata, drvdata->ctxid_pid[i], ETMCIDCVRn(i));
298 etm_writel(drvdata, drvdata->ctxid_mask, ETMCIDCMR);
299 etm_writel(drvdata, drvdata->sync_freq, ETMSYNCFR);
300 /* No external input selected */
301 etm_writel(drvdata, 0x0, ETMEXTINSELR);
302 etm_writel(drvdata, drvdata->timestamp_event, ETMTSEVR);
303 /* No auxiliary control selected */
304 etm_writel(drvdata, 0x0, ETMAUXCR);
305 etm_writel(drvdata, drvdata->traceid, ETMTRACEIDR);
306 /* No VMID comparator value selected */
307 etm_writel(drvdata, 0x0, ETMVMIDCVR);
309 /* Ensures trace output is enabled from this ETM */
310 etm_writel(drvdata, drvdata->ctrl | ETMCR_ETM_EN | etmcr, ETMCR);
312 etm_clr_prog(drvdata);
313 CS_LOCK(drvdata->base);
315 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
318 static int etm_trace_id(struct coresight_device *csdev)
320 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
321 unsigned long flags;
322 int trace_id = -1;
324 if (!drvdata->enable)
325 return drvdata->traceid;
326 pm_runtime_get_sync(csdev->dev.parent);
328 spin_lock_irqsave(&drvdata->spinlock, flags);
330 CS_UNLOCK(drvdata->base);
331 trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
332 CS_LOCK(drvdata->base);
334 spin_unlock_irqrestore(&drvdata->spinlock, flags);
335 pm_runtime_put(csdev->dev.parent);
337 return trace_id;
340 static int etm_enable(struct coresight_device *csdev)
342 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
343 int ret;
345 pm_runtime_get_sync(csdev->dev.parent);
346 spin_lock(&drvdata->spinlock);
349 * Configure the ETM only if the CPU is online. If it isn't online
350 * hw configuration will take place when 'CPU_STARTING' is received
351 * in @etm_cpu_callback.
353 if (cpu_online(drvdata->cpu)) {
354 ret = smp_call_function_single(drvdata->cpu,
355 etm_enable_hw, drvdata, 1);
356 if (ret)
357 goto err;
360 drvdata->enable = true;
361 drvdata->sticky_enable = true;
363 spin_unlock(&drvdata->spinlock);
365 dev_info(drvdata->dev, "ETM tracing enabled\n");
366 return 0;
367 err:
368 spin_unlock(&drvdata->spinlock);
369 pm_runtime_put(csdev->dev.parent);
370 return ret;
373 static void etm_disable_hw(void *info)
375 int i;
376 struct etm_drvdata *drvdata = info;
378 CS_UNLOCK(drvdata->base);
379 etm_set_prog(drvdata);
381 /* Program trace enable to low by using always false event */
382 etm_writel(drvdata, ETM_HARD_WIRE_RES_A | ETM_EVENT_NOT_A, ETMTEEVR);
384 /* Read back sequencer and counters for post trace analysis */
385 drvdata->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
387 for (i = 0; i < drvdata->nr_cntr; i++)
388 drvdata->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
390 etm_set_pwrdwn(drvdata);
391 CS_LOCK(drvdata->base);
393 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
396 static void etm_disable(struct coresight_device *csdev)
398 struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
401 * Taking hotplug lock here protects from clocks getting disabled
402 * with tracing being left on (crash scenario) if user disable occurs
403 * after cpu online mask indicates the cpu is offline but before the
404 * DYING hotplug callback is serviced by the ETM driver.
406 get_online_cpus();
407 spin_lock(&drvdata->spinlock);
410 * Executing etm_disable_hw on the cpu whose ETM is being disabled
411 * ensures that register writes occur when cpu is powered.
413 smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
414 drvdata->enable = false;
416 spin_unlock(&drvdata->spinlock);
417 put_online_cpus();
418 pm_runtime_put(csdev->dev.parent);
420 dev_info(drvdata->dev, "ETM tracing disabled\n");
423 static const struct coresight_ops_source etm_source_ops = {
424 .trace_id = etm_trace_id,
425 .enable = etm_enable,
426 .disable = etm_disable,
429 static const struct coresight_ops etm_cs_ops = {
430 .source_ops = &etm_source_ops,
433 static ssize_t nr_addr_cmp_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
436 unsigned long val;
437 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
439 val = drvdata->nr_addr_cmp;
440 return sprintf(buf, "%#lx\n", val);
442 static DEVICE_ATTR_RO(nr_addr_cmp);
444 static ssize_t nr_cntr_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
446 { unsigned long val;
447 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
449 val = drvdata->nr_cntr;
450 return sprintf(buf, "%#lx\n", val);
452 static DEVICE_ATTR_RO(nr_cntr);
454 static ssize_t nr_ctxid_cmp_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 unsigned long val;
458 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
460 val = drvdata->nr_ctxid_cmp;
461 return sprintf(buf, "%#lx\n", val);
463 static DEVICE_ATTR_RO(nr_ctxid_cmp);
465 static ssize_t etmsr_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 unsigned long flags, val;
469 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
471 pm_runtime_get_sync(drvdata->dev);
472 spin_lock_irqsave(&drvdata->spinlock, flags);
473 CS_UNLOCK(drvdata->base);
475 val = etm_readl(drvdata, ETMSR);
477 CS_LOCK(drvdata->base);
478 spin_unlock_irqrestore(&drvdata->spinlock, flags);
479 pm_runtime_put(drvdata->dev);
481 return sprintf(buf, "%#lx\n", val);
483 static DEVICE_ATTR_RO(etmsr);
485 static ssize_t reset_store(struct device *dev,
486 struct device_attribute *attr,
487 const char *buf, size_t size)
489 int i, ret;
490 unsigned long val;
491 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
493 ret = kstrtoul(buf, 16, &val);
494 if (ret)
495 return ret;
497 if (val) {
498 spin_lock(&drvdata->spinlock);
499 drvdata->mode = ETM_MODE_EXCLUDE;
500 drvdata->ctrl = 0x0;
501 drvdata->trigger_event = ETM_DEFAULT_EVENT_VAL;
502 drvdata->startstop_ctrl = 0x0;
503 drvdata->addr_idx = 0x0;
504 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
505 drvdata->addr_val[i] = 0x0;
506 drvdata->addr_acctype[i] = 0x0;
507 drvdata->addr_type[i] = ETM_ADDR_TYPE_NONE;
509 drvdata->cntr_idx = 0x0;
511 etm_set_default(drvdata);
512 spin_unlock(&drvdata->spinlock);
515 return size;
517 static DEVICE_ATTR_WO(reset);
519 static ssize_t mode_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
522 unsigned long val;
523 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
525 val = drvdata->mode;
526 return sprintf(buf, "%#lx\n", val);
529 static ssize_t mode_store(struct device *dev,
530 struct device_attribute *attr,
531 const char *buf, size_t size)
533 int ret;
534 unsigned long val;
535 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
537 ret = kstrtoul(buf, 16, &val);
538 if (ret)
539 return ret;
541 spin_lock(&drvdata->spinlock);
542 drvdata->mode = val & ETM_MODE_ALL;
544 if (drvdata->mode & ETM_MODE_EXCLUDE)
545 drvdata->enable_ctrl1 |= ETMTECR1_INC_EXC;
546 else
547 drvdata->enable_ctrl1 &= ~ETMTECR1_INC_EXC;
549 if (drvdata->mode & ETM_MODE_CYCACC)
550 drvdata->ctrl |= ETMCR_CYC_ACC;
551 else
552 drvdata->ctrl &= ~ETMCR_CYC_ACC;
554 if (drvdata->mode & ETM_MODE_STALL) {
555 if (!(drvdata->etmccr & ETMCCR_FIFOFULL)) {
556 dev_warn(drvdata->dev, "stall mode not supported\n");
557 ret = -EINVAL;
558 goto err_unlock;
560 drvdata->ctrl |= ETMCR_STALL_MODE;
561 } else
562 drvdata->ctrl &= ~ETMCR_STALL_MODE;
564 if (drvdata->mode & ETM_MODE_TIMESTAMP) {
565 if (!(drvdata->etmccer & ETMCCER_TIMESTAMP)) {
566 dev_warn(drvdata->dev, "timestamp not supported\n");
567 ret = -EINVAL;
568 goto err_unlock;
570 drvdata->ctrl |= ETMCR_TIMESTAMP_EN;
571 } else
572 drvdata->ctrl &= ~ETMCR_TIMESTAMP_EN;
574 if (drvdata->mode & ETM_MODE_CTXID)
575 drvdata->ctrl |= ETMCR_CTXID_SIZE;
576 else
577 drvdata->ctrl &= ~ETMCR_CTXID_SIZE;
578 spin_unlock(&drvdata->spinlock);
580 return size;
582 err_unlock:
583 spin_unlock(&drvdata->spinlock);
584 return ret;
586 static DEVICE_ATTR_RW(mode);
588 static ssize_t trigger_event_show(struct device *dev,
589 struct device_attribute *attr, char *buf)
591 unsigned long val;
592 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
594 val = drvdata->trigger_event;
595 return sprintf(buf, "%#lx\n", val);
598 static ssize_t trigger_event_store(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t size)
602 int ret;
603 unsigned long val;
604 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
606 ret = kstrtoul(buf, 16, &val);
607 if (ret)
608 return ret;
610 drvdata->trigger_event = val & ETM_EVENT_MASK;
612 return size;
614 static DEVICE_ATTR_RW(trigger_event);
616 static ssize_t enable_event_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
619 unsigned long val;
620 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
622 val = drvdata->enable_event;
623 return sprintf(buf, "%#lx\n", val);
626 static ssize_t enable_event_store(struct device *dev,
627 struct device_attribute *attr,
628 const char *buf, size_t size)
630 int ret;
631 unsigned long val;
632 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
634 ret = kstrtoul(buf, 16, &val);
635 if (ret)
636 return ret;
638 drvdata->enable_event = val & ETM_EVENT_MASK;
640 return size;
642 static DEVICE_ATTR_RW(enable_event);
644 static ssize_t fifofull_level_show(struct device *dev,
645 struct device_attribute *attr, char *buf)
647 unsigned long val;
648 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
650 val = drvdata->fifofull_level;
651 return sprintf(buf, "%#lx\n", val);
654 static ssize_t fifofull_level_store(struct device *dev,
655 struct device_attribute *attr,
656 const char *buf, size_t size)
658 int ret;
659 unsigned long val;
660 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
662 ret = kstrtoul(buf, 16, &val);
663 if (ret)
664 return ret;
666 drvdata->fifofull_level = val;
668 return size;
670 static DEVICE_ATTR_RW(fifofull_level);
672 static ssize_t addr_idx_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
675 unsigned long val;
676 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
678 val = drvdata->addr_idx;
679 return sprintf(buf, "%#lx\n", val);
682 static ssize_t addr_idx_store(struct device *dev,
683 struct device_attribute *attr,
684 const char *buf, size_t size)
686 int ret;
687 unsigned long val;
688 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
690 ret = kstrtoul(buf, 16, &val);
691 if (ret)
692 return ret;
694 if (val >= drvdata->nr_addr_cmp)
695 return -EINVAL;
698 * Use spinlock to ensure index doesn't change while it gets
699 * dereferenced multiple times within a spinlock block elsewhere.
701 spin_lock(&drvdata->spinlock);
702 drvdata->addr_idx = val;
703 spin_unlock(&drvdata->spinlock);
705 return size;
707 static DEVICE_ATTR_RW(addr_idx);
709 static ssize_t addr_single_show(struct device *dev,
710 struct device_attribute *attr, char *buf)
712 u8 idx;
713 unsigned long val;
714 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
716 spin_lock(&drvdata->spinlock);
717 idx = drvdata->addr_idx;
718 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
719 drvdata->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
720 spin_unlock(&drvdata->spinlock);
721 return -EINVAL;
724 val = drvdata->addr_val[idx];
725 spin_unlock(&drvdata->spinlock);
727 return sprintf(buf, "%#lx\n", val);
730 static ssize_t addr_single_store(struct device *dev,
731 struct device_attribute *attr,
732 const char *buf, size_t size)
734 u8 idx;
735 int ret;
736 unsigned long val;
737 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
739 ret = kstrtoul(buf, 16, &val);
740 if (ret)
741 return ret;
743 spin_lock(&drvdata->spinlock);
744 idx = drvdata->addr_idx;
745 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
746 drvdata->addr_type[idx] == ETM_ADDR_TYPE_SINGLE)) {
747 spin_unlock(&drvdata->spinlock);
748 return -EINVAL;
751 drvdata->addr_val[idx] = val;
752 drvdata->addr_type[idx] = ETM_ADDR_TYPE_SINGLE;
753 spin_unlock(&drvdata->spinlock);
755 return size;
757 static DEVICE_ATTR_RW(addr_single);
759 static ssize_t addr_range_show(struct device *dev,
760 struct device_attribute *attr, char *buf)
762 u8 idx;
763 unsigned long val1, val2;
764 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
766 spin_lock(&drvdata->spinlock);
767 idx = drvdata->addr_idx;
768 if (idx % 2 != 0) {
769 spin_unlock(&drvdata->spinlock);
770 return -EPERM;
772 if (!((drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
773 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
774 (drvdata->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
775 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
776 spin_unlock(&drvdata->spinlock);
777 return -EPERM;
780 val1 = drvdata->addr_val[idx];
781 val2 = drvdata->addr_val[idx + 1];
782 spin_unlock(&drvdata->spinlock);
784 return sprintf(buf, "%#lx %#lx\n", val1, val2);
787 static ssize_t addr_range_store(struct device *dev,
788 struct device_attribute *attr,
789 const char *buf, size_t size)
791 u8 idx;
792 unsigned long val1, val2;
793 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
795 if (sscanf(buf, "%lx %lx", &val1, &val2) != 2)
796 return -EINVAL;
797 /* Lower address comparator cannot have a higher address value */
798 if (val1 > val2)
799 return -EINVAL;
801 spin_lock(&drvdata->spinlock);
802 idx = drvdata->addr_idx;
803 if (idx % 2 != 0) {
804 spin_unlock(&drvdata->spinlock);
805 return -EPERM;
807 if (!((drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE &&
808 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_NONE) ||
809 (drvdata->addr_type[idx] == ETM_ADDR_TYPE_RANGE &&
810 drvdata->addr_type[idx + 1] == ETM_ADDR_TYPE_RANGE))) {
811 spin_unlock(&drvdata->spinlock);
812 return -EPERM;
815 drvdata->addr_val[idx] = val1;
816 drvdata->addr_type[idx] = ETM_ADDR_TYPE_RANGE;
817 drvdata->addr_val[idx + 1] = val2;
818 drvdata->addr_type[idx + 1] = ETM_ADDR_TYPE_RANGE;
819 drvdata->enable_ctrl1 |= (1 << (idx/2));
820 spin_unlock(&drvdata->spinlock);
822 return size;
824 static DEVICE_ATTR_RW(addr_range);
826 static ssize_t addr_start_show(struct device *dev,
827 struct device_attribute *attr, char *buf)
829 u8 idx;
830 unsigned long val;
831 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
833 spin_lock(&drvdata->spinlock);
834 idx = drvdata->addr_idx;
835 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
836 drvdata->addr_type[idx] == ETM_ADDR_TYPE_START)) {
837 spin_unlock(&drvdata->spinlock);
838 return -EPERM;
841 val = drvdata->addr_val[idx];
842 spin_unlock(&drvdata->spinlock);
844 return sprintf(buf, "%#lx\n", val);
847 static ssize_t addr_start_store(struct device *dev,
848 struct device_attribute *attr,
849 const char *buf, size_t size)
851 u8 idx;
852 int ret;
853 unsigned long val;
854 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
856 ret = kstrtoul(buf, 16, &val);
857 if (ret)
858 return ret;
860 spin_lock(&drvdata->spinlock);
861 idx = drvdata->addr_idx;
862 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
863 drvdata->addr_type[idx] == ETM_ADDR_TYPE_START)) {
864 spin_unlock(&drvdata->spinlock);
865 return -EPERM;
868 drvdata->addr_val[idx] = val;
869 drvdata->addr_type[idx] = ETM_ADDR_TYPE_START;
870 drvdata->startstop_ctrl |= (1 << idx);
871 drvdata->enable_ctrl1 |= BIT(25);
872 spin_unlock(&drvdata->spinlock);
874 return size;
876 static DEVICE_ATTR_RW(addr_start);
878 static ssize_t addr_stop_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
881 u8 idx;
882 unsigned long val;
883 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
885 spin_lock(&drvdata->spinlock);
886 idx = drvdata->addr_idx;
887 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
888 drvdata->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
889 spin_unlock(&drvdata->spinlock);
890 return -EPERM;
893 val = drvdata->addr_val[idx];
894 spin_unlock(&drvdata->spinlock);
896 return sprintf(buf, "%#lx\n", val);
899 static ssize_t addr_stop_store(struct device *dev,
900 struct device_attribute *attr,
901 const char *buf, size_t size)
903 u8 idx;
904 int ret;
905 unsigned long val;
906 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
908 ret = kstrtoul(buf, 16, &val);
909 if (ret)
910 return ret;
912 spin_lock(&drvdata->spinlock);
913 idx = drvdata->addr_idx;
914 if (!(drvdata->addr_type[idx] == ETM_ADDR_TYPE_NONE ||
915 drvdata->addr_type[idx] == ETM_ADDR_TYPE_STOP)) {
916 spin_unlock(&drvdata->spinlock);
917 return -EPERM;
920 drvdata->addr_val[idx] = val;
921 drvdata->addr_type[idx] = ETM_ADDR_TYPE_STOP;
922 drvdata->startstop_ctrl |= (1 << (idx + 16));
923 drvdata->enable_ctrl1 |= ETMTECR1_START_STOP;
924 spin_unlock(&drvdata->spinlock);
926 return size;
928 static DEVICE_ATTR_RW(addr_stop);
930 static ssize_t addr_acctype_show(struct device *dev,
931 struct device_attribute *attr, char *buf)
933 unsigned long val;
934 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
936 spin_lock(&drvdata->spinlock);
937 val = drvdata->addr_acctype[drvdata->addr_idx];
938 spin_unlock(&drvdata->spinlock);
940 return sprintf(buf, "%#lx\n", val);
943 static ssize_t addr_acctype_store(struct device *dev,
944 struct device_attribute *attr,
945 const char *buf, size_t size)
947 int ret;
948 unsigned long val;
949 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
951 ret = kstrtoul(buf, 16, &val);
952 if (ret)
953 return ret;
955 spin_lock(&drvdata->spinlock);
956 drvdata->addr_acctype[drvdata->addr_idx] = val;
957 spin_unlock(&drvdata->spinlock);
959 return size;
961 static DEVICE_ATTR_RW(addr_acctype);
963 static ssize_t cntr_idx_show(struct device *dev,
964 struct device_attribute *attr, char *buf)
966 unsigned long val;
967 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
969 val = drvdata->cntr_idx;
970 return sprintf(buf, "%#lx\n", val);
973 static ssize_t cntr_idx_store(struct device *dev,
974 struct device_attribute *attr,
975 const char *buf, size_t size)
977 int ret;
978 unsigned long val;
979 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
981 ret = kstrtoul(buf, 16, &val);
982 if (ret)
983 return ret;
985 if (val >= drvdata->nr_cntr)
986 return -EINVAL;
988 * Use spinlock to ensure index doesn't change while it gets
989 * dereferenced multiple times within a spinlock block elsewhere.
991 spin_lock(&drvdata->spinlock);
992 drvdata->cntr_idx = val;
993 spin_unlock(&drvdata->spinlock);
995 return size;
997 static DEVICE_ATTR_RW(cntr_idx);
999 static ssize_t cntr_rld_val_show(struct device *dev,
1000 struct device_attribute *attr, char *buf)
1002 unsigned long val;
1003 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1005 spin_lock(&drvdata->spinlock);
1006 val = drvdata->cntr_rld_val[drvdata->cntr_idx];
1007 spin_unlock(&drvdata->spinlock);
1009 return sprintf(buf, "%#lx\n", val);
1012 static ssize_t cntr_rld_val_store(struct device *dev,
1013 struct device_attribute *attr,
1014 const char *buf, size_t size)
1016 int ret;
1017 unsigned long val;
1018 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1020 ret = kstrtoul(buf, 16, &val);
1021 if (ret)
1022 return ret;
1024 spin_lock(&drvdata->spinlock);
1025 drvdata->cntr_rld_val[drvdata->cntr_idx] = val;
1026 spin_unlock(&drvdata->spinlock);
1028 return size;
1030 static DEVICE_ATTR_RW(cntr_rld_val);
1032 static ssize_t cntr_event_show(struct device *dev,
1033 struct device_attribute *attr, char *buf)
1035 unsigned long val;
1036 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1038 spin_lock(&drvdata->spinlock);
1039 val = drvdata->cntr_event[drvdata->cntr_idx];
1040 spin_unlock(&drvdata->spinlock);
1042 return sprintf(buf, "%#lx\n", val);
1045 static ssize_t cntr_event_store(struct device *dev,
1046 struct device_attribute *attr,
1047 const char *buf, size_t size)
1049 int ret;
1050 unsigned long val;
1051 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1053 ret = kstrtoul(buf, 16, &val);
1054 if (ret)
1055 return ret;
1057 spin_lock(&drvdata->spinlock);
1058 drvdata->cntr_event[drvdata->cntr_idx] = val & ETM_EVENT_MASK;
1059 spin_unlock(&drvdata->spinlock);
1061 return size;
1063 static DEVICE_ATTR_RW(cntr_event);
1065 static ssize_t cntr_rld_event_show(struct device *dev,
1066 struct device_attribute *attr, char *buf)
1068 unsigned long val;
1069 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1071 spin_lock(&drvdata->spinlock);
1072 val = drvdata->cntr_rld_event[drvdata->cntr_idx];
1073 spin_unlock(&drvdata->spinlock);
1075 return sprintf(buf, "%#lx\n", val);
1078 static ssize_t cntr_rld_event_store(struct device *dev,
1079 struct device_attribute *attr,
1080 const char *buf, size_t size)
1082 int ret;
1083 unsigned long val;
1084 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1086 ret = kstrtoul(buf, 16, &val);
1087 if (ret)
1088 return ret;
1090 spin_lock(&drvdata->spinlock);
1091 drvdata->cntr_rld_event[drvdata->cntr_idx] = val & ETM_EVENT_MASK;
1092 spin_unlock(&drvdata->spinlock);
1094 return size;
1096 static DEVICE_ATTR_RW(cntr_rld_event);
1098 static ssize_t cntr_val_show(struct device *dev,
1099 struct device_attribute *attr, char *buf)
1101 int i, ret = 0;
1102 u32 val;
1103 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1105 if (!drvdata->enable) {
1106 spin_lock(&drvdata->spinlock);
1107 for (i = 0; i < drvdata->nr_cntr; i++)
1108 ret += sprintf(buf, "counter %d: %x\n",
1109 i, drvdata->cntr_val[i]);
1110 spin_unlock(&drvdata->spinlock);
1111 return ret;
1114 for (i = 0; i < drvdata->nr_cntr; i++) {
1115 val = etm_readl(drvdata, ETMCNTVRn(i));
1116 ret += sprintf(buf, "counter %d: %x\n", i, val);
1119 return ret;
1122 static ssize_t cntr_val_store(struct device *dev,
1123 struct device_attribute *attr,
1124 const char *buf, size_t size)
1126 int ret;
1127 unsigned long val;
1128 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1130 ret = kstrtoul(buf, 16, &val);
1131 if (ret)
1132 return ret;
1134 spin_lock(&drvdata->spinlock);
1135 drvdata->cntr_val[drvdata->cntr_idx] = val;
1136 spin_unlock(&drvdata->spinlock);
1138 return size;
1140 static DEVICE_ATTR_RW(cntr_val);
1142 static ssize_t seq_12_event_show(struct device *dev,
1143 struct device_attribute *attr, char *buf)
1145 unsigned long val;
1146 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1148 val = drvdata->seq_12_event;
1149 return sprintf(buf, "%#lx\n", val);
1152 static ssize_t seq_12_event_store(struct device *dev,
1153 struct device_attribute *attr,
1154 const char *buf, size_t size)
1156 int ret;
1157 unsigned long val;
1158 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1160 ret = kstrtoul(buf, 16, &val);
1161 if (ret)
1162 return ret;
1164 drvdata->seq_12_event = val & ETM_EVENT_MASK;
1165 return size;
1167 static DEVICE_ATTR_RW(seq_12_event);
1169 static ssize_t seq_21_event_show(struct device *dev,
1170 struct device_attribute *attr, char *buf)
1172 unsigned long val;
1173 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1175 val = drvdata->seq_21_event;
1176 return sprintf(buf, "%#lx\n", val);
1179 static ssize_t seq_21_event_store(struct device *dev,
1180 struct device_attribute *attr,
1181 const char *buf, size_t size)
1183 int ret;
1184 unsigned long val;
1185 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1187 ret = kstrtoul(buf, 16, &val);
1188 if (ret)
1189 return ret;
1191 drvdata->seq_21_event = val & ETM_EVENT_MASK;
1192 return size;
1194 static DEVICE_ATTR_RW(seq_21_event);
1196 static ssize_t seq_23_event_show(struct device *dev,
1197 struct device_attribute *attr, char *buf)
1199 unsigned long val;
1200 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1202 val = drvdata->seq_23_event;
1203 return sprintf(buf, "%#lx\n", val);
1206 static ssize_t seq_23_event_store(struct device *dev,
1207 struct device_attribute *attr,
1208 const char *buf, size_t size)
1210 int ret;
1211 unsigned long val;
1212 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1214 ret = kstrtoul(buf, 16, &val);
1215 if (ret)
1216 return ret;
1218 drvdata->seq_23_event = val & ETM_EVENT_MASK;
1219 return size;
1221 static DEVICE_ATTR_RW(seq_23_event);
1223 static ssize_t seq_31_event_show(struct device *dev,
1224 struct device_attribute *attr, char *buf)
1226 unsigned long val;
1227 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1229 val = drvdata->seq_31_event;
1230 return sprintf(buf, "%#lx\n", val);
1233 static ssize_t seq_31_event_store(struct device *dev,
1234 struct device_attribute *attr,
1235 const char *buf, size_t size)
1237 int ret;
1238 unsigned long val;
1239 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1241 ret = kstrtoul(buf, 16, &val);
1242 if (ret)
1243 return ret;
1245 drvdata->seq_31_event = val & ETM_EVENT_MASK;
1246 return size;
1248 static DEVICE_ATTR_RW(seq_31_event);
1250 static ssize_t seq_32_event_show(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1253 unsigned long val;
1254 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1256 val = drvdata->seq_32_event;
1257 return sprintf(buf, "%#lx\n", val);
1260 static ssize_t seq_32_event_store(struct device *dev,
1261 struct device_attribute *attr,
1262 const char *buf, size_t size)
1264 int ret;
1265 unsigned long val;
1266 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1268 ret = kstrtoul(buf, 16, &val);
1269 if (ret)
1270 return ret;
1272 drvdata->seq_32_event = val & ETM_EVENT_MASK;
1273 return size;
1275 static DEVICE_ATTR_RW(seq_32_event);
1277 static ssize_t seq_13_event_show(struct device *dev,
1278 struct device_attribute *attr, char *buf)
1280 unsigned long val;
1281 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1283 val = drvdata->seq_13_event;
1284 return sprintf(buf, "%#lx\n", val);
1287 static ssize_t seq_13_event_store(struct device *dev,
1288 struct device_attribute *attr,
1289 const char *buf, size_t size)
1291 int ret;
1292 unsigned long val;
1293 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1295 ret = kstrtoul(buf, 16, &val);
1296 if (ret)
1297 return ret;
1299 drvdata->seq_13_event = val & ETM_EVENT_MASK;
1300 return size;
1302 static DEVICE_ATTR_RW(seq_13_event);
1304 static ssize_t seq_curr_state_show(struct device *dev,
1305 struct device_attribute *attr, char *buf)
1307 unsigned long val, flags;
1308 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1310 if (!drvdata->enable) {
1311 val = drvdata->seq_curr_state;
1312 goto out;
1315 pm_runtime_get_sync(drvdata->dev);
1316 spin_lock_irqsave(&drvdata->spinlock, flags);
1318 CS_UNLOCK(drvdata->base);
1319 val = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
1320 CS_LOCK(drvdata->base);
1322 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1323 pm_runtime_put(drvdata->dev);
1324 out:
1325 return sprintf(buf, "%#lx\n", val);
1328 static ssize_t seq_curr_state_store(struct device *dev,
1329 struct device_attribute *attr,
1330 const char *buf, size_t size)
1332 int ret;
1333 unsigned long val;
1334 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1336 ret = kstrtoul(buf, 16, &val);
1337 if (ret)
1338 return ret;
1340 if (val > ETM_SEQ_STATE_MAX_VAL)
1341 return -EINVAL;
1343 drvdata->seq_curr_state = val;
1345 return size;
1347 static DEVICE_ATTR_RW(seq_curr_state);
1349 static ssize_t ctxid_idx_show(struct device *dev,
1350 struct device_attribute *attr, char *buf)
1352 unsigned long val;
1353 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1355 val = drvdata->ctxid_idx;
1356 return sprintf(buf, "%#lx\n", val);
1359 static ssize_t ctxid_idx_store(struct device *dev,
1360 struct device_attribute *attr,
1361 const char *buf, size_t size)
1363 int ret;
1364 unsigned long val;
1365 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1367 ret = kstrtoul(buf, 16, &val);
1368 if (ret)
1369 return ret;
1371 if (val >= drvdata->nr_ctxid_cmp)
1372 return -EINVAL;
1375 * Use spinlock to ensure index doesn't change while it gets
1376 * dereferenced multiple times within a spinlock block elsewhere.
1378 spin_lock(&drvdata->spinlock);
1379 drvdata->ctxid_idx = val;
1380 spin_unlock(&drvdata->spinlock);
1382 return size;
1384 static DEVICE_ATTR_RW(ctxid_idx);
1386 static ssize_t ctxid_pid_show(struct device *dev,
1387 struct device_attribute *attr, char *buf)
1389 unsigned long val;
1390 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1392 spin_lock(&drvdata->spinlock);
1393 val = drvdata->ctxid_vpid[drvdata->ctxid_idx];
1394 spin_unlock(&drvdata->spinlock);
1396 return sprintf(buf, "%#lx\n", val);
1399 static ssize_t ctxid_pid_store(struct device *dev,
1400 struct device_attribute *attr,
1401 const char *buf, size_t size)
1403 int ret;
1404 unsigned long vpid, pid;
1405 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1407 ret = kstrtoul(buf, 16, &vpid);
1408 if (ret)
1409 return ret;
1411 pid = coresight_vpid_to_pid(vpid);
1413 spin_lock(&drvdata->spinlock);
1414 drvdata->ctxid_pid[drvdata->ctxid_idx] = pid;
1415 drvdata->ctxid_vpid[drvdata->ctxid_idx] = vpid;
1416 spin_unlock(&drvdata->spinlock);
1418 return size;
1420 static DEVICE_ATTR_RW(ctxid_pid);
1422 static ssize_t ctxid_mask_show(struct device *dev,
1423 struct device_attribute *attr, char *buf)
1425 unsigned long val;
1426 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1428 val = drvdata->ctxid_mask;
1429 return sprintf(buf, "%#lx\n", val);
1432 static ssize_t ctxid_mask_store(struct device *dev,
1433 struct device_attribute *attr,
1434 const char *buf, size_t size)
1436 int ret;
1437 unsigned long val;
1438 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1440 ret = kstrtoul(buf, 16, &val);
1441 if (ret)
1442 return ret;
1444 drvdata->ctxid_mask = val;
1445 return size;
1447 static DEVICE_ATTR_RW(ctxid_mask);
1449 static ssize_t sync_freq_show(struct device *dev,
1450 struct device_attribute *attr, char *buf)
1452 unsigned long val;
1453 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1455 val = drvdata->sync_freq;
1456 return sprintf(buf, "%#lx\n", val);
1459 static ssize_t sync_freq_store(struct device *dev,
1460 struct device_attribute *attr,
1461 const char *buf, size_t size)
1463 int ret;
1464 unsigned long val;
1465 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1467 ret = kstrtoul(buf, 16, &val);
1468 if (ret)
1469 return ret;
1471 drvdata->sync_freq = val & ETM_SYNC_MASK;
1472 return size;
1474 static DEVICE_ATTR_RW(sync_freq);
1476 static ssize_t timestamp_event_show(struct device *dev,
1477 struct device_attribute *attr, char *buf)
1479 unsigned long val;
1480 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1482 val = drvdata->timestamp_event;
1483 return sprintf(buf, "%#lx\n", val);
1486 static ssize_t timestamp_event_store(struct device *dev,
1487 struct device_attribute *attr,
1488 const char *buf, size_t size)
1490 int ret;
1491 unsigned long val;
1492 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1494 ret = kstrtoul(buf, 16, &val);
1495 if (ret)
1496 return ret;
1498 drvdata->timestamp_event = val & ETM_EVENT_MASK;
1499 return size;
1501 static DEVICE_ATTR_RW(timestamp_event);
1503 static ssize_t cpu_show(struct device *dev,
1504 struct device_attribute *attr, char *buf)
1506 int val;
1507 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1509 val = drvdata->cpu;
1510 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
1513 static DEVICE_ATTR_RO(cpu);
1515 static ssize_t traceid_show(struct device *dev,
1516 struct device_attribute *attr, char *buf)
1518 unsigned long val, flags;
1519 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1521 if (!drvdata->enable) {
1522 val = drvdata->traceid;
1523 goto out;
1526 pm_runtime_get_sync(drvdata->dev);
1527 spin_lock_irqsave(&drvdata->spinlock, flags);
1528 CS_UNLOCK(drvdata->base);
1530 val = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
1532 CS_LOCK(drvdata->base);
1533 spin_unlock_irqrestore(&drvdata->spinlock, flags);
1534 pm_runtime_put(drvdata->dev);
1535 out:
1536 return sprintf(buf, "%#lx\n", val);
1539 static ssize_t traceid_store(struct device *dev,
1540 struct device_attribute *attr,
1541 const char *buf, size_t size)
1543 int ret;
1544 unsigned long val;
1545 struct etm_drvdata *drvdata = dev_get_drvdata(dev->parent);
1547 ret = kstrtoul(buf, 16, &val);
1548 if (ret)
1549 return ret;
1551 drvdata->traceid = val & ETM_TRACEID_MASK;
1552 return size;
1554 static DEVICE_ATTR_RW(traceid);
1556 static struct attribute *coresight_etm_attrs[] = {
1557 &dev_attr_nr_addr_cmp.attr,
1558 &dev_attr_nr_cntr.attr,
1559 &dev_attr_nr_ctxid_cmp.attr,
1560 &dev_attr_etmsr.attr,
1561 &dev_attr_reset.attr,
1562 &dev_attr_mode.attr,
1563 &dev_attr_trigger_event.attr,
1564 &dev_attr_enable_event.attr,
1565 &dev_attr_fifofull_level.attr,
1566 &dev_attr_addr_idx.attr,
1567 &dev_attr_addr_single.attr,
1568 &dev_attr_addr_range.attr,
1569 &dev_attr_addr_start.attr,
1570 &dev_attr_addr_stop.attr,
1571 &dev_attr_addr_acctype.attr,
1572 &dev_attr_cntr_idx.attr,
1573 &dev_attr_cntr_rld_val.attr,
1574 &dev_attr_cntr_event.attr,
1575 &dev_attr_cntr_rld_event.attr,
1576 &dev_attr_cntr_val.attr,
1577 &dev_attr_seq_12_event.attr,
1578 &dev_attr_seq_21_event.attr,
1579 &dev_attr_seq_23_event.attr,
1580 &dev_attr_seq_31_event.attr,
1581 &dev_attr_seq_32_event.attr,
1582 &dev_attr_seq_13_event.attr,
1583 &dev_attr_seq_curr_state.attr,
1584 &dev_attr_ctxid_idx.attr,
1585 &dev_attr_ctxid_pid.attr,
1586 &dev_attr_ctxid_mask.attr,
1587 &dev_attr_sync_freq.attr,
1588 &dev_attr_timestamp_event.attr,
1589 &dev_attr_traceid.attr,
1590 &dev_attr_cpu.attr,
1591 NULL,
1594 #define coresight_simple_func(name, offset) \
1595 static ssize_t name##_show(struct device *_dev, \
1596 struct device_attribute *attr, char *buf) \
1598 struct etm_drvdata *drvdata = dev_get_drvdata(_dev->parent); \
1599 return scnprintf(buf, PAGE_SIZE, "0x%x\n", \
1600 readl_relaxed(drvdata->base + offset)); \
1602 DEVICE_ATTR_RO(name)
1604 coresight_simple_func(etmccr, ETMCCR);
1605 coresight_simple_func(etmccer, ETMCCER);
1606 coresight_simple_func(etmscr, ETMSCR);
1607 coresight_simple_func(etmidr, ETMIDR);
1608 coresight_simple_func(etmcr, ETMCR);
1609 coresight_simple_func(etmtraceidr, ETMTRACEIDR);
1610 coresight_simple_func(etmteevr, ETMTEEVR);
1611 coresight_simple_func(etmtssvr, ETMTSSCR);
1612 coresight_simple_func(etmtecr1, ETMTECR1);
1613 coresight_simple_func(etmtecr2, ETMTECR2);
1615 static struct attribute *coresight_etm_mgmt_attrs[] = {
1616 &dev_attr_etmccr.attr,
1617 &dev_attr_etmccer.attr,
1618 &dev_attr_etmscr.attr,
1619 &dev_attr_etmidr.attr,
1620 &dev_attr_etmcr.attr,
1621 &dev_attr_etmtraceidr.attr,
1622 &dev_attr_etmteevr.attr,
1623 &dev_attr_etmtssvr.attr,
1624 &dev_attr_etmtecr1.attr,
1625 &dev_attr_etmtecr2.attr,
1626 NULL,
1629 static const struct attribute_group coresight_etm_group = {
1630 .attrs = coresight_etm_attrs,
1634 static const struct attribute_group coresight_etm_mgmt_group = {
1635 .attrs = coresight_etm_mgmt_attrs,
1636 .name = "mgmt",
1639 static const struct attribute_group *coresight_etm_groups[] = {
1640 &coresight_etm_group,
1641 &coresight_etm_mgmt_group,
1642 NULL,
1645 static int etm_cpu_callback(struct notifier_block *nfb, unsigned long action,
1646 void *hcpu)
1648 unsigned int cpu = (unsigned long)hcpu;
1650 if (!etmdrvdata[cpu])
1651 goto out;
1653 switch (action & (~CPU_TASKS_FROZEN)) {
1654 case CPU_STARTING:
1655 spin_lock(&etmdrvdata[cpu]->spinlock);
1656 if (!etmdrvdata[cpu]->os_unlock) {
1657 etm_os_unlock(etmdrvdata[cpu]);
1658 etmdrvdata[cpu]->os_unlock = true;
1661 if (etmdrvdata[cpu]->enable)
1662 etm_enable_hw(etmdrvdata[cpu]);
1663 spin_unlock(&etmdrvdata[cpu]->spinlock);
1664 break;
1666 case CPU_ONLINE:
1667 if (etmdrvdata[cpu]->boot_enable &&
1668 !etmdrvdata[cpu]->sticky_enable)
1669 coresight_enable(etmdrvdata[cpu]->csdev);
1670 break;
1672 case CPU_DYING:
1673 spin_lock(&etmdrvdata[cpu]->spinlock);
1674 if (etmdrvdata[cpu]->enable)
1675 etm_disable_hw(etmdrvdata[cpu]);
1676 spin_unlock(&etmdrvdata[cpu]->spinlock);
1677 break;
1679 out:
1680 return NOTIFY_OK;
1683 static struct notifier_block etm_cpu_notifier = {
1684 .notifier_call = etm_cpu_callback,
1687 static bool etm_arch_supported(u8 arch)
1689 switch (arch) {
1690 case ETM_ARCH_V3_3:
1691 break;
1692 case ETM_ARCH_V3_5:
1693 break;
1694 case PFT_ARCH_V1_0:
1695 break;
1696 case PFT_ARCH_V1_1:
1697 break;
1698 default:
1699 return false;
1701 return true;
1704 static void etm_init_arch_data(void *info)
1706 u32 etmidr;
1707 u32 etmccr;
1708 struct etm_drvdata *drvdata = info;
1710 CS_UNLOCK(drvdata->base);
1712 /* First dummy read */
1713 (void)etm_readl(drvdata, ETMPDSR);
1714 /* Provide power to ETM: ETMPDCR[3] == 1 */
1715 etm_set_pwrup(drvdata);
1717 * Clear power down bit since when this bit is set writes to
1718 * certain registers might be ignored.
1720 etm_clr_pwrdwn(drvdata);
1722 * Set prog bit. It will be set from reset but this is included to
1723 * ensure it is set
1725 etm_set_prog(drvdata);
1727 /* Find all capabilities */
1728 etmidr = etm_readl(drvdata, ETMIDR);
1729 drvdata->arch = BMVAL(etmidr, 4, 11);
1730 drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
1732 drvdata->etmccer = etm_readl(drvdata, ETMCCER);
1733 etmccr = etm_readl(drvdata, ETMCCR);
1734 drvdata->etmccr = etmccr;
1735 drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
1736 drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
1737 drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
1738 drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
1739 drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
1741 etm_set_pwrdwn(drvdata);
1742 etm_clr_pwrup(drvdata);
1743 CS_LOCK(drvdata->base);
1746 static void etm_init_default_data(struct etm_drvdata *drvdata)
1749 * A trace ID of value 0 is invalid, so let's start at some
1750 * random value that fits in 7 bits and will be just as good.
1752 static int etm3x_traceid = 0x10;
1754 u32 flags = (1 << 0 | /* instruction execute*/
1755 3 << 3 | /* ARM instruction */
1756 0 << 5 | /* No data value comparison */
1757 0 << 7 | /* No exact mach */
1758 0 << 8 | /* Ignore context ID */
1759 0 << 10); /* Security ignored */
1762 * Initial configuration only - guarantees sources handled by
1763 * this driver have a unique ID at startup time but not between
1764 * all other types of sources. For that we lean on the core
1765 * framework.
1767 drvdata->traceid = etm3x_traceid++;
1768 drvdata->ctrl = (ETMCR_CYC_ACC | ETMCR_TIMESTAMP_EN);
1769 drvdata->enable_ctrl1 = ETMTECR1_ADDR_COMP_1;
1770 if (drvdata->nr_addr_cmp >= 2) {
1771 drvdata->addr_val[0] = (u32) _stext;
1772 drvdata->addr_val[1] = (u32) _etext;
1773 drvdata->addr_acctype[0] = flags;
1774 drvdata->addr_acctype[1] = flags;
1775 drvdata->addr_type[0] = ETM_ADDR_TYPE_RANGE;
1776 drvdata->addr_type[1] = ETM_ADDR_TYPE_RANGE;
1779 etm_set_default(drvdata);
1782 static int etm_probe(struct amba_device *adev, const struct amba_id *id)
1784 int ret;
1785 void __iomem *base;
1786 struct device *dev = &adev->dev;
1787 struct coresight_platform_data *pdata = NULL;
1788 struct etm_drvdata *drvdata;
1789 struct resource *res = &adev->res;
1790 struct coresight_desc *desc;
1791 struct device_node *np = adev->dev.of_node;
1793 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
1794 if (!desc)
1795 return -ENOMEM;
1797 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
1798 if (!drvdata)
1799 return -ENOMEM;
1801 if (np) {
1802 pdata = of_get_coresight_platform_data(dev, np);
1803 if (IS_ERR(pdata))
1804 return PTR_ERR(pdata);
1806 adev->dev.platform_data = pdata;
1807 drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
1810 drvdata->dev = &adev->dev;
1811 dev_set_drvdata(dev, drvdata);
1813 /* Validity for the resource is already checked by the AMBA core */
1814 base = devm_ioremap_resource(dev, res);
1815 if (IS_ERR(base))
1816 return PTR_ERR(base);
1818 drvdata->base = base;
1820 spin_lock_init(&drvdata->spinlock);
1822 drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
1823 if (!IS_ERR(drvdata->atclk)) {
1824 ret = clk_prepare_enable(drvdata->atclk);
1825 if (ret)
1826 return ret;
1829 drvdata->cpu = pdata ? pdata->cpu : 0;
1831 get_online_cpus();
1832 etmdrvdata[drvdata->cpu] = drvdata;
1834 if (!smp_call_function_single(drvdata->cpu, etm_os_unlock, drvdata, 1))
1835 drvdata->os_unlock = true;
1837 if (smp_call_function_single(drvdata->cpu,
1838 etm_init_arch_data, drvdata, 1))
1839 dev_err(dev, "ETM arch init failed\n");
1841 if (!etm_count++)
1842 register_hotcpu_notifier(&etm_cpu_notifier);
1844 put_online_cpus();
1846 if (etm_arch_supported(drvdata->arch) == false) {
1847 ret = -EINVAL;
1848 goto err_arch_supported;
1850 etm_init_default_data(drvdata);
1852 desc->type = CORESIGHT_DEV_TYPE_SOURCE;
1853 desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
1854 desc->ops = &etm_cs_ops;
1855 desc->pdata = pdata;
1856 desc->dev = dev;
1857 desc->groups = coresight_etm_groups;
1858 drvdata->csdev = coresight_register(desc);
1859 if (IS_ERR(drvdata->csdev)) {
1860 ret = PTR_ERR(drvdata->csdev);
1861 goto err_arch_supported;
1864 pm_runtime_put(&adev->dev);
1865 dev_info(dev, "%s initialized\n", (char *)id->data);
1867 if (boot_enable) {
1868 coresight_enable(drvdata->csdev);
1869 drvdata->boot_enable = true;
1872 return 0;
1874 err_arch_supported:
1875 if (--etm_count == 0)
1876 unregister_hotcpu_notifier(&etm_cpu_notifier);
1877 return ret;
1880 static int etm_remove(struct amba_device *adev)
1882 struct etm_drvdata *drvdata = amba_get_drvdata(adev);
1884 coresight_unregister(drvdata->csdev);
1885 if (--etm_count == 0)
1886 unregister_hotcpu_notifier(&etm_cpu_notifier);
1888 return 0;
1891 #ifdef CONFIG_PM
1892 static int etm_runtime_suspend(struct device *dev)
1894 struct etm_drvdata *drvdata = dev_get_drvdata(dev);
1896 if (drvdata && !IS_ERR(drvdata->atclk))
1897 clk_disable_unprepare(drvdata->atclk);
1899 return 0;
1902 static int etm_runtime_resume(struct device *dev)
1904 struct etm_drvdata *drvdata = dev_get_drvdata(dev);
1906 if (drvdata && !IS_ERR(drvdata->atclk))
1907 clk_prepare_enable(drvdata->atclk);
1909 return 0;
1911 #endif
1913 static const struct dev_pm_ops etm_dev_pm_ops = {
1914 SET_RUNTIME_PM_OPS(etm_runtime_suspend, etm_runtime_resume, NULL)
1917 static struct amba_id etm_ids[] = {
1918 { /* ETM 3.3 */
1919 .id = 0x0003b921,
1920 .mask = 0x0003ffff,
1921 .data = "ETM 3.3",
1923 { /* ETM 3.5 */
1924 .id = 0x0003b956,
1925 .mask = 0x0003ffff,
1926 .data = "ETM 3.5",
1928 { /* PTM 1.0 */
1929 .id = 0x0003b950,
1930 .mask = 0x0003ffff,
1931 .data = "PTM 1.0",
1933 { /* PTM 1.1 */
1934 .id = 0x0003b95f,
1935 .mask = 0x0003ffff,
1936 .data = "PTM 1.1",
1938 { /* PTM 1.1 Qualcomm */
1939 .id = 0x0003006f,
1940 .mask = 0x0003ffff,
1941 .data = "PTM 1.1",
1943 { 0, 0},
1946 static struct amba_driver etm_driver = {
1947 .drv = {
1948 .name = "coresight-etm3x",
1949 .owner = THIS_MODULE,
1950 .pm = &etm_dev_pm_ops,
1952 .probe = etm_probe,
1953 .remove = etm_remove,
1954 .id_table = etm_ids,
1957 module_amba_driver(etm_driver);
1959 MODULE_LICENSE("GPL v2");
1960 MODULE_DESCRIPTION("CoreSight Program Flow Trace driver");