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