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