gfs2: Initialize atime of I_NEW inodes
[cascardo/linux.git] / drivers / hwtracing / coresight / coresight-etm3x.c
1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
2  *
3  * Description: CoreSight Program Flow Trace driver
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/types.h>
19 #include <linux/device.h>
20 #include <linux/io.h>
21 #include <linux/err.h>
22 #include <linux/fs.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/smp.h>
26 #include <linux/sysfs.h>
27 #include <linux/stat.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/cpu.h>
30 #include <linux/of.h>
31 #include <linux/coresight.h>
32 #include <linux/coresight-pmu.h>
33 #include <linux/amba/bus.h>
34 #include <linux/seq_file.h>
35 #include <linux/uaccess.h>
36 #include <linux/clk.h>
37 #include <linux/perf_event.h>
38 #include <asm/sections.h>
39
40 #include "coresight-etm.h"
41 #include "coresight-etm-perf.h"
42
43 /*
44  * Not really modular but using module_param is the easiest way to
45  * remain consistent with existing use cases for now.
46  */
47 static int boot_enable;
48 module_param_named(boot_enable, boot_enable, int, S_IRUGO);
49
50 /* The number of ETM/PTM currently registered */
51 static int etm_count;
52 static struct etm_drvdata *etmdrvdata[NR_CPUS];
53
54 static enum cpuhp_state hp_online;
55
56 /*
57  * Memory mapped writes to clear os lock are not supported on some processors
58  * and OS lock must be unlocked before any memory mapped access on such
59  * processors, otherwise memory mapped reads/writes will be invalid.
60  */
61 static void etm_os_unlock(struct etm_drvdata *drvdata)
62 {
63         /* Writing any value to ETMOSLAR unlocks the trace registers */
64         etm_writel(drvdata, 0x0, ETMOSLAR);
65         drvdata->os_unlock = true;
66         isb();
67 }
68
69 static void etm_set_pwrdwn(struct etm_drvdata *drvdata)
70 {
71         u32 etmcr;
72
73         /* Ensure pending cp14 accesses complete before setting pwrdwn */
74         mb();
75         isb();
76         etmcr = etm_readl(drvdata, ETMCR);
77         etmcr |= ETMCR_PWD_DWN;
78         etm_writel(drvdata, etmcr, ETMCR);
79 }
80
81 static void etm_clr_pwrdwn(struct etm_drvdata *drvdata)
82 {
83         u32 etmcr;
84
85         etmcr = etm_readl(drvdata, ETMCR);
86         etmcr &= ~ETMCR_PWD_DWN;
87         etm_writel(drvdata, etmcr, ETMCR);
88         /* Ensure pwrup completes before subsequent cp14 accesses */
89         mb();
90         isb();
91 }
92
93 static void etm_set_pwrup(struct etm_drvdata *drvdata)
94 {
95         u32 etmpdcr;
96
97         etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
98         etmpdcr |= ETMPDCR_PWD_UP;
99         writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
100         /* Ensure pwrup completes before subsequent cp14 accesses */
101         mb();
102         isb();
103 }
104
105 static void etm_clr_pwrup(struct etm_drvdata *drvdata)
106 {
107         u32 etmpdcr;
108
109         /* Ensure pending cp14 accesses complete before clearing pwrup */
110         mb();
111         isb();
112         etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
113         etmpdcr &= ~ETMPDCR_PWD_UP;
114         writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
115 }
116
117 /**
118  * coresight_timeout_etm - loop until a bit has changed to a specific state.
119  * @drvdata: etm's private data structure.
120  * @offset: address of a register, starting from @addr.
121  * @position: the position of the bit of interest.
122  * @value: the value the bit should have.
123  *
124  * Basically the same as @coresight_timeout except for the register access
125  * method where we have to account for CP14 configurations.
126
127  * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
128  * TIMEOUT_US has elapsed, which ever happens first.
129  */
130
131 static int coresight_timeout_etm(struct etm_drvdata *drvdata, u32 offset,
132                                   int position, int value)
133 {
134         int i;
135         u32 val;
136
137         for (i = TIMEOUT_US; i > 0; i--) {
138                 val = etm_readl(drvdata, offset);
139                 /* Waiting on the bit to go from 0 to 1 */
140                 if (value) {
141                         if (val & BIT(position))
142                                 return 0;
143                 /* Waiting on the bit to go from 1 to 0 */
144                 } else {
145                         if (!(val & BIT(position)))
146                                 return 0;
147                 }
148
149                 /*
150                  * Delay is arbitrary - the specification doesn't say how long
151                  * we are expected to wait.  Extra check required to make sure
152                  * we don't wait needlessly on the last iteration.
153                  */
154                 if (i - 1)
155                         udelay(1);
156         }
157
158         return -EAGAIN;
159 }
160
161
162 static void etm_set_prog(struct etm_drvdata *drvdata)
163 {
164         u32 etmcr;
165
166         etmcr = etm_readl(drvdata, ETMCR);
167         etmcr |= ETMCR_ETM_PRG;
168         etm_writel(drvdata, etmcr, ETMCR);
169         /*
170          * Recommended by spec for cp14 accesses to ensure etmcr write is
171          * complete before polling etmsr
172          */
173         isb();
174         if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 1)) {
175                 dev_err(drvdata->dev,
176                         "%s: timeout observed when probing at offset %#x\n",
177                         __func__, ETMSR);
178         }
179 }
180
181 static void etm_clr_prog(struct etm_drvdata *drvdata)
182 {
183         u32 etmcr;
184
185         etmcr = etm_readl(drvdata, ETMCR);
186         etmcr &= ~ETMCR_ETM_PRG;
187         etm_writel(drvdata, etmcr, ETMCR);
188         /*
189          * Recommended by spec for cp14 accesses to ensure etmcr write is
190          * complete before polling etmsr
191          */
192         isb();
193         if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 0)) {
194                 dev_err(drvdata->dev,
195                         "%s: timeout observed when probing at offset %#x\n",
196                         __func__, ETMSR);
197         }
198 }
199
200 void etm_set_default(struct etm_config *config)
201 {
202         int i;
203
204         if (WARN_ON_ONCE(!config))
205                 return;
206
207         /*
208          * Taken verbatim from the TRM:
209          *
210          * To trace all memory:
211          *  set bit [24] in register 0x009, the ETMTECR1, to 1
212          *  set all other bits in register 0x009, the ETMTECR1, to 0
213          *  set all bits in register 0x007, the ETMTECR2, to 0
214          *  set register 0x008, the ETMTEEVR, to 0x6F (TRUE).
215          */
216         config->enable_ctrl1 = BIT(24);
217         config->enable_ctrl2 = 0x0;
218         config->enable_event = ETM_HARD_WIRE_RES_A;
219
220         config->trigger_event = ETM_DEFAULT_EVENT_VAL;
221         config->enable_event = ETM_HARD_WIRE_RES_A;
222
223         config->seq_12_event = ETM_DEFAULT_EVENT_VAL;
224         config->seq_21_event = ETM_DEFAULT_EVENT_VAL;
225         config->seq_23_event = ETM_DEFAULT_EVENT_VAL;
226         config->seq_31_event = ETM_DEFAULT_EVENT_VAL;
227         config->seq_32_event = ETM_DEFAULT_EVENT_VAL;
228         config->seq_13_event = ETM_DEFAULT_EVENT_VAL;
229         config->timestamp_event = ETM_DEFAULT_EVENT_VAL;
230
231         for (i = 0; i < ETM_MAX_CNTR; i++) {
232                 config->cntr_rld_val[i] = 0x0;
233                 config->cntr_event[i] = ETM_DEFAULT_EVENT_VAL;
234                 config->cntr_rld_event[i] = ETM_DEFAULT_EVENT_VAL;
235                 config->cntr_val[i] = 0x0;
236         }
237
238         config->seq_curr_state = 0x0;
239         config->ctxid_idx = 0x0;
240         for (i = 0; i < ETM_MAX_CTXID_CMP; i++) {
241                 config->ctxid_pid[i] = 0x0;
242                 config->ctxid_vpid[i] = 0x0;
243         }
244
245         config->ctxid_mask = 0x0;
246 }
247
248 void etm_config_trace_mode(struct etm_config *config)
249 {
250         u32 flags, mode;
251
252         mode = config->mode;
253
254         mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
255
256         /* excluding kernel AND user space doesn't make sense */
257         if (mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
258                 return;
259
260         /* nothing to do if neither flags are set */
261         if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
262                 return;
263
264         flags = (1 << 0 |       /* instruction execute */
265                  3 << 3 |       /* ARM instruction */
266                  0 << 5 |       /* No data value comparison */
267                  0 << 7 |       /* No exact mach */
268                  0 << 8);       /* Ignore context ID */
269
270         /* No need to worry about single address comparators. */
271         config->enable_ctrl2 = 0x0;
272
273         /* Bit 0 is address range comparator 1 */
274         config->enable_ctrl1 = ETMTECR1_ADDR_COMP_1;
275
276         /*
277          * On ETMv3.5:
278          * ETMACTRn[13,11] == Non-secure state comparison control
279          * ETMACTRn[12,10] == Secure state comparison control
280          *
281          * b00 == Match in all modes in this state
282          * b01 == Do not match in any more in this state
283          * b10 == Match in all modes excepts user mode in this state
284          * b11 == Match only in user mode in this state
285          */
286
287         /* Tracing in secure mode is not supported at this time */
288         flags |= (0 << 12 | 1 << 10);
289
290         if (mode & ETM_MODE_EXCL_USER) {
291                 /* exclude user, match all modes except user mode */
292                 flags |= (1 << 13 | 0 << 11);
293         } else {
294                 /* exclude kernel, match only in user mode */
295                 flags |= (1 << 13 | 1 << 11);
296         }
297
298         /*
299          * The ETMEEVR register is already set to "hard wire A".  As such
300          * all there is to do is setup an address comparator that spans
301          * the entire address range and configure the state and mode bits.
302          */
303         config->addr_val[0] = (u32) 0x0;
304         config->addr_val[1] = (u32) ~0x0;
305         config->addr_acctype[0] = flags;
306         config->addr_acctype[1] = flags;
307         config->addr_type[0] = ETM_ADDR_TYPE_RANGE;
308         config->addr_type[1] = ETM_ADDR_TYPE_RANGE;
309 }
310
311 #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | ETMCR_TIMESTAMP_EN)
312
313 static int etm_parse_event_config(struct etm_drvdata *drvdata,
314                                   struct perf_event_attr *attr)
315 {
316         struct etm_config *config = &drvdata->config;
317
318         if (!attr)
319                 return -EINVAL;
320
321         /* Clear configuration from previous run */
322         memset(config, 0, sizeof(struct etm_config));
323
324         if (attr->exclude_kernel)
325                 config->mode = ETM_MODE_EXCL_KERN;
326
327         if (attr->exclude_user)
328                 config->mode = ETM_MODE_EXCL_USER;
329
330         /* Always start from the default config */
331         etm_set_default(config);
332
333         /*
334          * By default the tracers are configured to trace the whole address
335          * range.  Narrow the field only if requested by user space.
336          */
337         if (config->mode)
338                 etm_config_trace_mode(config);
339
340         /*
341          * At this time only cycle accurate and timestamp options are
342          * available.
343          */
344         if (attr->config & ~ETM3X_SUPPORTED_OPTIONS)
345                 return -EINVAL;
346
347         config->ctrl = attr->config;
348
349         return 0;
350 }
351
352 static void etm_enable_hw(void *info)
353 {
354         int i;
355         u32 etmcr;
356         struct etm_drvdata *drvdata = info;
357         struct etm_config *config = &drvdata->config;
358
359         CS_UNLOCK(drvdata->base);
360
361         /* Turn engine on */
362         etm_clr_pwrdwn(drvdata);
363         /* Apply power to trace registers */
364         etm_set_pwrup(drvdata);
365         /* Make sure all registers are accessible */
366         etm_os_unlock(drvdata);
367
368         etm_set_prog(drvdata);
369
370         etmcr = etm_readl(drvdata, ETMCR);
371         /* Clear setting from a previous run if need be */
372         etmcr &= ~ETM3X_SUPPORTED_OPTIONS;
373         etmcr |= drvdata->port_size;
374         etmcr |= ETMCR_ETM_EN;
375         etm_writel(drvdata, config->ctrl | etmcr, ETMCR);
376         etm_writel(drvdata, config->trigger_event, ETMTRIGGER);
377         etm_writel(drvdata, config->startstop_ctrl, ETMTSSCR);
378         etm_writel(drvdata, config->enable_event, ETMTEEVR);
379         etm_writel(drvdata, config->enable_ctrl1, ETMTECR1);
380         etm_writel(drvdata, config->fifofull_level, ETMFFLR);
381         for (i = 0; i < drvdata->nr_addr_cmp; i++) {
382                 etm_writel(drvdata, config->addr_val[i], ETMACVRn(i));
383                 etm_writel(drvdata, config->addr_acctype[i], ETMACTRn(i));
384         }
385         for (i = 0; i < drvdata->nr_cntr; i++) {
386                 etm_writel(drvdata, config->cntr_rld_val[i], ETMCNTRLDVRn(i));
387                 etm_writel(drvdata, config->cntr_event[i], ETMCNTENRn(i));
388                 etm_writel(drvdata, config->cntr_rld_event[i],
389                            ETMCNTRLDEVRn(i));
390                 etm_writel(drvdata, config->cntr_val[i], ETMCNTVRn(i));
391         }
392         etm_writel(drvdata, config->seq_12_event, ETMSQ12EVR);
393         etm_writel(drvdata, config->seq_21_event, ETMSQ21EVR);
394         etm_writel(drvdata, config->seq_23_event, ETMSQ23EVR);
395         etm_writel(drvdata, config->seq_31_event, ETMSQ31EVR);
396         etm_writel(drvdata, config->seq_32_event, ETMSQ32EVR);
397         etm_writel(drvdata, config->seq_13_event, ETMSQ13EVR);
398         etm_writel(drvdata, config->seq_curr_state, ETMSQR);
399         for (i = 0; i < drvdata->nr_ext_out; i++)
400                 etm_writel(drvdata, ETM_DEFAULT_EVENT_VAL, ETMEXTOUTEVRn(i));
401         for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
402                 etm_writel(drvdata, config->ctxid_pid[i], ETMCIDCVRn(i));
403         etm_writel(drvdata, config->ctxid_mask, ETMCIDCMR);
404         etm_writel(drvdata, config->sync_freq, ETMSYNCFR);
405         /* No external input selected */
406         etm_writel(drvdata, 0x0, ETMEXTINSELR);
407         etm_writel(drvdata, config->timestamp_event, ETMTSEVR);
408         /* No auxiliary control selected */
409         etm_writel(drvdata, 0x0, ETMAUXCR);
410         etm_writel(drvdata, drvdata->traceid, ETMTRACEIDR);
411         /* No VMID comparator value selected */
412         etm_writel(drvdata, 0x0, ETMVMIDCVR);
413
414         etm_clr_prog(drvdata);
415         CS_LOCK(drvdata->base);
416
417         dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
418 }
419
420 static int etm_cpu_id(struct coresight_device *csdev)
421 {
422         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
423
424         return drvdata->cpu;
425 }
426
427 int etm_get_trace_id(struct etm_drvdata *drvdata)
428 {
429         unsigned long flags;
430         int trace_id = -1;
431
432         if (!drvdata)
433                 goto out;
434
435         if (!local_read(&drvdata->mode))
436                 return drvdata->traceid;
437
438         pm_runtime_get_sync(drvdata->dev);
439
440         spin_lock_irqsave(&drvdata->spinlock, flags);
441
442         CS_UNLOCK(drvdata->base);
443         trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
444         CS_LOCK(drvdata->base);
445
446         spin_unlock_irqrestore(&drvdata->spinlock, flags);
447         pm_runtime_put(drvdata->dev);
448
449 out:
450         return trace_id;
451
452 }
453
454 static int etm_trace_id(struct coresight_device *csdev)
455 {
456         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
457
458         return etm_get_trace_id(drvdata);
459 }
460
461 static int etm_enable_perf(struct coresight_device *csdev,
462                            struct perf_event_attr *attr)
463 {
464         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
465
466         if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
467                 return -EINVAL;
468
469         /* Configure the tracer based on the session's specifics */
470         etm_parse_event_config(drvdata, attr);
471         /* And enable it */
472         etm_enable_hw(drvdata);
473
474         return 0;
475 }
476
477 static int etm_enable_sysfs(struct coresight_device *csdev)
478 {
479         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
480         int ret;
481
482         spin_lock(&drvdata->spinlock);
483
484         /*
485          * Configure the ETM only if the CPU is online.  If it isn't online
486          * hw configuration will take place on the local CPU during bring up.
487          */
488         if (cpu_online(drvdata->cpu)) {
489                 ret = smp_call_function_single(drvdata->cpu,
490                                                etm_enable_hw, drvdata, 1);
491                 if (ret)
492                         goto err;
493         }
494
495         drvdata->sticky_enable = true;
496         spin_unlock(&drvdata->spinlock);
497
498         dev_info(drvdata->dev, "ETM tracing enabled\n");
499         return 0;
500
501 err:
502         spin_unlock(&drvdata->spinlock);
503         return ret;
504 }
505
506 static int etm_enable(struct coresight_device *csdev,
507                       struct perf_event_attr *attr, u32 mode)
508 {
509         int ret;
510         u32 val;
511         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
512
513         val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
514
515         /* Someone is already using the tracer */
516         if (val)
517                 return -EBUSY;
518
519         switch (mode) {
520         case CS_MODE_SYSFS:
521                 ret = etm_enable_sysfs(csdev);
522                 break;
523         case CS_MODE_PERF:
524                 ret = etm_enable_perf(csdev, attr);
525                 break;
526         default:
527                 ret = -EINVAL;
528         }
529
530         /* The tracer didn't start */
531         if (ret)
532                 local_set(&drvdata->mode, CS_MODE_DISABLED);
533
534         return ret;
535 }
536
537 static void etm_disable_hw(void *info)
538 {
539         int i;
540         struct etm_drvdata *drvdata = info;
541         struct etm_config *config = &drvdata->config;
542
543         CS_UNLOCK(drvdata->base);
544         etm_set_prog(drvdata);
545
546         /* Read back sequencer and counters for post trace analysis */
547         config->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
548
549         for (i = 0; i < drvdata->nr_cntr; i++)
550                 config->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
551
552         etm_set_pwrdwn(drvdata);
553         CS_LOCK(drvdata->base);
554
555         dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
556 }
557
558 static void etm_disable_perf(struct coresight_device *csdev)
559 {
560         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
561
562         if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
563                 return;
564
565         CS_UNLOCK(drvdata->base);
566
567         /* Setting the prog bit disables tracing immediately */
568         etm_set_prog(drvdata);
569
570         /*
571          * There is no way to know when the tracer will be used again so
572          * power down the tracer.
573          */
574         etm_set_pwrdwn(drvdata);
575
576         CS_LOCK(drvdata->base);
577 }
578
579 static void etm_disable_sysfs(struct coresight_device *csdev)
580 {
581         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
582
583         /*
584          * Taking hotplug lock here protects from clocks getting disabled
585          * with tracing being left on (crash scenario) if user disable occurs
586          * after cpu online mask indicates the cpu is offline but before the
587          * DYING hotplug callback is serviced by the ETM driver.
588          */
589         get_online_cpus();
590         spin_lock(&drvdata->spinlock);
591
592         /*
593          * Executing etm_disable_hw on the cpu whose ETM is being disabled
594          * ensures that register writes occur when cpu is powered.
595          */
596         smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
597
598         spin_unlock(&drvdata->spinlock);
599         put_online_cpus();
600
601         dev_info(drvdata->dev, "ETM tracing disabled\n");
602 }
603
604 static void etm_disable(struct coresight_device *csdev)
605 {
606         u32 mode;
607         struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
608
609         /*
610          * For as long as the tracer isn't disabled another entity can't
611          * change its status.  As such we can read the status here without
612          * fearing it will change under us.
613          */
614         mode = local_read(&drvdata->mode);
615
616         switch (mode) {
617         case CS_MODE_DISABLED:
618                 break;
619         case CS_MODE_SYSFS:
620                 etm_disable_sysfs(csdev);
621                 break;
622         case CS_MODE_PERF:
623                 etm_disable_perf(csdev);
624                 break;
625         default:
626                 WARN_ON_ONCE(mode);
627                 return;
628         }
629
630         if (mode)
631                 local_set(&drvdata->mode, CS_MODE_DISABLED);
632 }
633
634 static const struct coresight_ops_source etm_source_ops = {
635         .cpu_id         = etm_cpu_id,
636         .trace_id       = etm_trace_id,
637         .enable         = etm_enable,
638         .disable        = etm_disable,
639 };
640
641 static const struct coresight_ops etm_cs_ops = {
642         .source_ops     = &etm_source_ops,
643 };
644
645 static int etm_online_cpu(unsigned int cpu)
646 {
647         if (!etmdrvdata[cpu])
648                 return 0;
649
650         if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
651                 coresight_enable(etmdrvdata[cpu]->csdev);
652         return 0;
653 }
654
655 static int etm_starting_cpu(unsigned int cpu)
656 {
657         if (!etmdrvdata[cpu])
658                 return 0;
659
660         spin_lock(&etmdrvdata[cpu]->spinlock);
661         if (!etmdrvdata[cpu]->os_unlock) {
662                 etm_os_unlock(etmdrvdata[cpu]);
663                 etmdrvdata[cpu]->os_unlock = true;
664         }
665
666         if (local_read(&etmdrvdata[cpu]->mode))
667                 etm_enable_hw(etmdrvdata[cpu]);
668         spin_unlock(&etmdrvdata[cpu]->spinlock);
669         return 0;
670 }
671
672 static int etm_dying_cpu(unsigned int cpu)
673 {
674         if (!etmdrvdata[cpu])
675                 return 0;
676
677         spin_lock(&etmdrvdata[cpu]->spinlock);
678         if (local_read(&etmdrvdata[cpu]->mode))
679                 etm_disable_hw(etmdrvdata[cpu]);
680         spin_unlock(&etmdrvdata[cpu]->spinlock);
681         return 0;
682 }
683
684 static bool etm_arch_supported(u8 arch)
685 {
686         switch (arch) {
687         case ETM_ARCH_V3_3:
688                 break;
689         case ETM_ARCH_V3_5:
690                 break;
691         case PFT_ARCH_V1_0:
692                 break;
693         case PFT_ARCH_V1_1:
694                 break;
695         default:
696                 return false;
697         }
698         return true;
699 }
700
701 static void etm_init_arch_data(void *info)
702 {
703         u32 etmidr;
704         u32 etmccr;
705         struct etm_drvdata *drvdata = info;
706
707         /* Make sure all registers are accessible */
708         etm_os_unlock(drvdata);
709
710         CS_UNLOCK(drvdata->base);
711
712         /* First dummy read */
713         (void)etm_readl(drvdata, ETMPDSR);
714         /* Provide power to ETM: ETMPDCR[3] == 1 */
715         etm_set_pwrup(drvdata);
716         /*
717          * Clear power down bit since when this bit is set writes to
718          * certain registers might be ignored.
719          */
720         etm_clr_pwrdwn(drvdata);
721         /*
722          * Set prog bit. It will be set from reset but this is included to
723          * ensure it is set
724          */
725         etm_set_prog(drvdata);
726
727         /* Find all capabilities */
728         etmidr = etm_readl(drvdata, ETMIDR);
729         drvdata->arch = BMVAL(etmidr, 4, 11);
730         drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
731
732         drvdata->etmccer = etm_readl(drvdata, ETMCCER);
733         etmccr = etm_readl(drvdata, ETMCCR);
734         drvdata->etmccr = etmccr;
735         drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
736         drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
737         drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
738         drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
739         drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
740
741         etm_set_pwrdwn(drvdata);
742         etm_clr_pwrup(drvdata);
743         CS_LOCK(drvdata->base);
744 }
745
746 static void etm_init_trace_id(struct etm_drvdata *drvdata)
747 {
748         drvdata->traceid = coresight_get_trace_id(drvdata->cpu);
749 }
750
751 static int etm_probe(struct amba_device *adev, const struct amba_id *id)
752 {
753         int ret;
754         void __iomem *base;
755         struct device *dev = &adev->dev;
756         struct coresight_platform_data *pdata = NULL;
757         struct etm_drvdata *drvdata;
758         struct resource *res = &adev->res;
759         struct coresight_desc *desc;
760         struct device_node *np = adev->dev.of_node;
761
762         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
763         if (!desc)
764                 return -ENOMEM;
765
766         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
767         if (!drvdata)
768                 return -ENOMEM;
769
770         if (np) {
771                 pdata = of_get_coresight_platform_data(dev, np);
772                 if (IS_ERR(pdata))
773                         return PTR_ERR(pdata);
774
775                 adev->dev.platform_data = pdata;
776                 drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
777         }
778
779         drvdata->dev = &adev->dev;
780         dev_set_drvdata(dev, drvdata);
781
782         /* Validity for the resource is already checked by the AMBA core */
783         base = devm_ioremap_resource(dev, res);
784         if (IS_ERR(base))
785                 return PTR_ERR(base);
786
787         drvdata->base = base;
788
789         spin_lock_init(&drvdata->spinlock);
790
791         drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
792         if (!IS_ERR(drvdata->atclk)) {
793                 ret = clk_prepare_enable(drvdata->atclk);
794                 if (ret)
795                         return ret;
796         }
797
798         drvdata->cpu = pdata ? pdata->cpu : 0;
799
800         get_online_cpus();
801         etmdrvdata[drvdata->cpu] = drvdata;
802
803         if (smp_call_function_single(drvdata->cpu,
804                                      etm_init_arch_data,  drvdata, 1))
805                 dev_err(dev, "ETM arch init failed\n");
806
807         if (!etm_count++) {
808                 cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING,
809                                           "AP_ARM_CORESIGHT_STARTING",
810                                           etm_starting_cpu, etm_dying_cpu);
811                 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
812                                                 "AP_ARM_CORESIGHT_ONLINE",
813                                                 etm_online_cpu, NULL);
814                 if (ret < 0)
815                         goto err_arch_supported;
816                 hp_online = ret;
817         }
818         put_online_cpus();
819
820         if (etm_arch_supported(drvdata->arch) == false) {
821                 ret = -EINVAL;
822                 goto err_arch_supported;
823         }
824
825         etm_init_trace_id(drvdata);
826         etm_set_default(&drvdata->config);
827
828         desc->type = CORESIGHT_DEV_TYPE_SOURCE;
829         desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
830         desc->ops = &etm_cs_ops;
831         desc->pdata = pdata;
832         desc->dev = dev;
833         desc->groups = coresight_etm_groups;
834         drvdata->csdev = coresight_register(desc);
835         if (IS_ERR(drvdata->csdev)) {
836                 ret = PTR_ERR(drvdata->csdev);
837                 goto err_arch_supported;
838         }
839
840         ret = etm_perf_symlink(drvdata->csdev, true);
841         if (ret) {
842                 coresight_unregister(drvdata->csdev);
843                 goto err_arch_supported;
844         }
845
846         pm_runtime_put(&adev->dev);
847         dev_info(dev, "%s initialized\n", (char *)id->data);
848         if (boot_enable) {
849                 coresight_enable(drvdata->csdev);
850                 drvdata->boot_enable = true;
851         }
852
853         return 0;
854
855 err_arch_supported:
856         if (--etm_count == 0) {
857                 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
858                 if (hp_online)
859                         cpuhp_remove_state_nocalls(hp_online);
860         }
861         return ret;
862 }
863
864 #ifdef CONFIG_PM
865 static int etm_runtime_suspend(struct device *dev)
866 {
867         struct etm_drvdata *drvdata = dev_get_drvdata(dev);
868
869         if (drvdata && !IS_ERR(drvdata->atclk))
870                 clk_disable_unprepare(drvdata->atclk);
871
872         return 0;
873 }
874
875 static int etm_runtime_resume(struct device *dev)
876 {
877         struct etm_drvdata *drvdata = dev_get_drvdata(dev);
878
879         if (drvdata && !IS_ERR(drvdata->atclk))
880                 clk_prepare_enable(drvdata->atclk);
881
882         return 0;
883 }
884 #endif
885
886 static const struct dev_pm_ops etm_dev_pm_ops = {
887         SET_RUNTIME_PM_OPS(etm_runtime_suspend, etm_runtime_resume, NULL)
888 };
889
890 static struct amba_id etm_ids[] = {
891         {       /* ETM 3.3 */
892                 .id     = 0x0003b921,
893                 .mask   = 0x0003ffff,
894                 .data   = "ETM 3.3",
895         },
896         {       /* ETM 3.5 */
897                 .id     = 0x0003b956,
898                 .mask   = 0x0003ffff,
899                 .data   = "ETM 3.5",
900         },
901         {       /* PTM 1.0 */
902                 .id     = 0x0003b950,
903                 .mask   = 0x0003ffff,
904                 .data   = "PTM 1.0",
905         },
906         {       /* PTM 1.1 */
907                 .id     = 0x0003b95f,
908                 .mask   = 0x0003ffff,
909                 .data   = "PTM 1.1",
910         },
911         {       /* PTM 1.1 Qualcomm */
912                 .id     = 0x0003006f,
913                 .mask   = 0x0003ffff,
914                 .data   = "PTM 1.1",
915         },
916         { 0, 0},
917 };
918
919 static struct amba_driver etm_driver = {
920         .drv = {
921                 .name   = "coresight-etm3x",
922                 .owner  = THIS_MODULE,
923                 .pm     = &etm_dev_pm_ops,
924                 .suppress_bind_attrs = true,
925         },
926         .probe          = etm_probe,
927         .id_table       = etm_ids,
928 };
929 builtin_amba_driver(etm_driver);