]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/hwtracing/coresight/coresight-etm4x.c
coresight: etmv4: Fix ETMv4x peripheral ID table
[mirror_ubuntu-zesty-kernel.git] / drivers / hwtracing / coresight / coresight-etm4x.c
1 /* Copyright (c) 2014, 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/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/device.h>
18 #include <linux/io.h>
19 #include <linux/err.h>
20 #include <linux/fs.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/smp.h>
24 #include <linux/sysfs.h>
25 #include <linux/stat.h>
26 #include <linux/clk.h>
27 #include <linux/cpu.h>
28 #include <linux/coresight.h>
29 #include <linux/coresight-pmu.h>
30 #include <linux/pm_wakeup.h>
31 #include <linux/amba/bus.h>
32 #include <linux/seq_file.h>
33 #include <linux/uaccess.h>
34 #include <linux/perf_event.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/perf_event.h>
37 #include <asm/sections.h>
38 #include <asm/local.h>
39
40 #include "coresight-etm4x.h"
41 #include "coresight-etm-perf.h"
42
43 static int boot_enable;
44 module_param_named(boot_enable, boot_enable, int, S_IRUGO);
45
46 /* The number of ETMv4 currently registered */
47 static int etm4_count;
48 static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
49 static void etm4_set_default(struct etmv4_config *config);
50
51 static enum cpuhp_state hp_online;
52
53 static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
54 {
55 /* Writing any value to ETMOSLAR unlocks the trace registers */
56 writel_relaxed(0x0, drvdata->base + TRCOSLAR);
57 drvdata->os_unlock = true;
58 isb();
59 }
60
61 static bool etm4_arch_supported(u8 arch)
62 {
63 switch (arch) {
64 case ETM_ARCH_V4:
65 break;
66 default:
67 return false;
68 }
69 return true;
70 }
71
72 static int etm4_cpu_id(struct coresight_device *csdev)
73 {
74 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
75
76 return drvdata->cpu;
77 }
78
79 static int etm4_trace_id(struct coresight_device *csdev)
80 {
81 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
82
83 return drvdata->trcid;
84 }
85
86 static void etm4_enable_hw(void *info)
87 {
88 int i;
89 struct etmv4_drvdata *drvdata = info;
90 struct etmv4_config *config = &drvdata->config;
91
92 CS_UNLOCK(drvdata->base);
93
94 etm4_os_unlock(drvdata);
95
96 /* Disable the trace unit before programming trace registers */
97 writel_relaxed(0, drvdata->base + TRCPRGCTLR);
98
99 /* wait for TRCSTATR.IDLE to go up */
100 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
101 dev_err(drvdata->dev,
102 "timeout observed when probing at offset %#x\n",
103 TRCSTATR);
104
105 writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
106 writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
107 /* nothing specific implemented */
108 writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
109 writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
110 writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
111 writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
112 writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
113 writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
114 writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
115 writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
116 writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
117 writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
118 writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
119 writel_relaxed(config->vissctlr,
120 drvdata->base + TRCVISSCTLR);
121 writel_relaxed(config->vipcssctlr,
122 drvdata->base + TRCVIPCSSCTLR);
123 for (i = 0; i < drvdata->nrseqstate - 1; i++)
124 writel_relaxed(config->seq_ctrl[i],
125 drvdata->base + TRCSEQEVRn(i));
126 writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
127 writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
128 writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
129 for (i = 0; i < drvdata->nr_cntr; i++) {
130 writel_relaxed(config->cntrldvr[i],
131 drvdata->base + TRCCNTRLDVRn(i));
132 writel_relaxed(config->cntr_ctrl[i],
133 drvdata->base + TRCCNTCTLRn(i));
134 writel_relaxed(config->cntr_val[i],
135 drvdata->base + TRCCNTVRn(i));
136 }
137
138 /* Resource selector pair 0 is always implemented and reserved */
139 for (i = 0; i < drvdata->nr_resource * 2; i++)
140 writel_relaxed(config->res_ctrl[i],
141 drvdata->base + TRCRSCTLRn(i));
142
143 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
144 writel_relaxed(config->ss_ctrl[i],
145 drvdata->base + TRCSSCCRn(i));
146 writel_relaxed(config->ss_status[i],
147 drvdata->base + TRCSSCSRn(i));
148 writel_relaxed(config->ss_pe_cmp[i],
149 drvdata->base + TRCSSPCICRn(i));
150 }
151 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
152 writeq_relaxed(config->addr_val[i],
153 drvdata->base + TRCACVRn(i));
154 writeq_relaxed(config->addr_acc[i],
155 drvdata->base + TRCACATRn(i));
156 }
157 for (i = 0; i < drvdata->numcidc; i++)
158 writeq_relaxed(config->ctxid_pid[i],
159 drvdata->base + TRCCIDCVRn(i));
160 writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
161 writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
162
163 for (i = 0; i < drvdata->numvmidc; i++)
164 writeq_relaxed(config->vmid_val[i],
165 drvdata->base + TRCVMIDCVRn(i));
166 writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
167 writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
168
169 /* Enable the trace unit */
170 writel_relaxed(1, drvdata->base + TRCPRGCTLR);
171
172 /* wait for TRCSTATR.IDLE to go back down to '0' */
173 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
174 dev_err(drvdata->dev,
175 "timeout observed when probing at offset %#x\n",
176 TRCSTATR);
177
178 CS_LOCK(drvdata->base);
179
180 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
181 }
182
183 static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
184 struct perf_event_attr *attr)
185 {
186 struct etmv4_config *config = &drvdata->config;
187
188 if (!attr)
189 return -EINVAL;
190
191 /* Clear configuration from previous run */
192 memset(config, 0, sizeof(struct etmv4_config));
193
194 if (attr->exclude_kernel)
195 config->mode = ETM_MODE_EXCL_KERN;
196
197 if (attr->exclude_user)
198 config->mode = ETM_MODE_EXCL_USER;
199
200 /* Always start from the default config */
201 etm4_set_default(config);
202
203 /*
204 * By default the tracers are configured to trace the whole address
205 * range. Narrow the field only if requested by user space.
206 */
207 if (config->mode)
208 etm4_config_trace_mode(config);
209
210 /* Go from generic option to ETMv4 specifics */
211 if (attr->config & BIT(ETM_OPT_CYCACC))
212 config->cfg |= ETMv4_MODE_CYCACC;
213 if (attr->config & BIT(ETM_OPT_TS))
214 config->cfg |= ETMv4_MODE_TIMESTAMP;
215
216 return 0;
217 }
218
219 static int etm4_enable_perf(struct coresight_device *csdev,
220 struct perf_event_attr *attr)
221 {
222 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
223
224 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
225 return -EINVAL;
226
227 /* Configure the tracer based on the session's specifics */
228 etm4_parse_event_config(drvdata, attr);
229 /* And enable it */
230 etm4_enable_hw(drvdata);
231
232 return 0;
233 }
234
235 static int etm4_enable_sysfs(struct coresight_device *csdev)
236 {
237 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
238 int ret;
239
240 spin_lock(&drvdata->spinlock);
241
242 /*
243 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
244 * ensures that register writes occur when cpu is powered.
245 */
246 ret = smp_call_function_single(drvdata->cpu,
247 etm4_enable_hw, drvdata, 1);
248 if (ret)
249 goto err;
250
251 drvdata->sticky_enable = true;
252 spin_unlock(&drvdata->spinlock);
253
254 dev_info(drvdata->dev, "ETM tracing enabled\n");
255 return 0;
256
257 err:
258 spin_unlock(&drvdata->spinlock);
259 return ret;
260 }
261
262 static int etm4_enable(struct coresight_device *csdev,
263 struct perf_event_attr *attr, u32 mode)
264 {
265 int ret;
266 u32 val;
267 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
268
269 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
270
271 /* Someone is already using the tracer */
272 if (val)
273 return -EBUSY;
274
275 switch (mode) {
276 case CS_MODE_SYSFS:
277 ret = etm4_enable_sysfs(csdev);
278 break;
279 case CS_MODE_PERF:
280 ret = etm4_enable_perf(csdev, attr);
281 break;
282 default:
283 ret = -EINVAL;
284 }
285
286 /* The tracer didn't start */
287 if (ret)
288 local_set(&drvdata->mode, CS_MODE_DISABLED);
289
290 return ret;
291 }
292
293 static void etm4_disable_hw(void *info)
294 {
295 u32 control;
296 struct etmv4_drvdata *drvdata = info;
297
298 CS_UNLOCK(drvdata->base);
299
300 control = readl_relaxed(drvdata->base + TRCPRGCTLR);
301
302 /* EN, bit[0] Trace unit enable bit */
303 control &= ~0x1;
304
305 /* make sure everything completes before disabling */
306 mb();
307 isb();
308 writel_relaxed(control, drvdata->base + TRCPRGCTLR);
309
310 CS_LOCK(drvdata->base);
311
312 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
313 }
314
315 static int etm4_disable_perf(struct coresight_device *csdev)
316 {
317 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
318
319 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
320 return -EINVAL;
321
322 etm4_disable_hw(drvdata);
323 return 0;
324 }
325
326 static void etm4_disable_sysfs(struct coresight_device *csdev)
327 {
328 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
329
330 /*
331 * Taking hotplug lock here protects from clocks getting disabled
332 * with tracing being left on (crash scenario) if user disable occurs
333 * after cpu online mask indicates the cpu is offline but before the
334 * DYING hotplug callback is serviced by the ETM driver.
335 */
336 get_online_cpus();
337 spin_lock(&drvdata->spinlock);
338
339 /*
340 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
341 * ensures that register writes occur when cpu is powered.
342 */
343 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
344
345 spin_unlock(&drvdata->spinlock);
346 put_online_cpus();
347
348 dev_info(drvdata->dev, "ETM tracing disabled\n");
349 }
350
351 static void etm4_disable(struct coresight_device *csdev)
352 {
353 u32 mode;
354 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
355
356 /*
357 * For as long as the tracer isn't disabled another entity can't
358 * change its status. As such we can read the status here without
359 * fearing it will change under us.
360 */
361 mode = local_read(&drvdata->mode);
362
363 switch (mode) {
364 case CS_MODE_DISABLED:
365 break;
366 case CS_MODE_SYSFS:
367 etm4_disable_sysfs(csdev);
368 break;
369 case CS_MODE_PERF:
370 etm4_disable_perf(csdev);
371 break;
372 }
373
374 if (mode)
375 local_set(&drvdata->mode, CS_MODE_DISABLED);
376 }
377
378 static const struct coresight_ops_source etm4_source_ops = {
379 .cpu_id = etm4_cpu_id,
380 .trace_id = etm4_trace_id,
381 .enable = etm4_enable,
382 .disable = etm4_disable,
383 };
384
385 static const struct coresight_ops etm4_cs_ops = {
386 .source_ops = &etm4_source_ops,
387 };
388
389 static void etm4_init_arch_data(void *info)
390 {
391 u32 etmidr0;
392 u32 etmidr1;
393 u32 etmidr2;
394 u32 etmidr3;
395 u32 etmidr4;
396 u32 etmidr5;
397 struct etmv4_drvdata *drvdata = info;
398
399 /* Make sure all registers are accessible */
400 etm4_os_unlock(drvdata);
401
402 CS_UNLOCK(drvdata->base);
403
404 /* find all capabilities of the tracing unit */
405 etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
406
407 /* INSTP0, bits[2:1] P0 tracing support field */
408 if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
409 drvdata->instrp0 = true;
410 else
411 drvdata->instrp0 = false;
412
413 /* TRCBB, bit[5] Branch broadcast tracing support bit */
414 if (BMVAL(etmidr0, 5, 5))
415 drvdata->trcbb = true;
416 else
417 drvdata->trcbb = false;
418
419 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
420 if (BMVAL(etmidr0, 6, 6))
421 drvdata->trccond = true;
422 else
423 drvdata->trccond = false;
424
425 /* TRCCCI, bit[7] Cycle counting instruction bit */
426 if (BMVAL(etmidr0, 7, 7))
427 drvdata->trccci = true;
428 else
429 drvdata->trccci = false;
430
431 /* RETSTACK, bit[9] Return stack bit */
432 if (BMVAL(etmidr0, 9, 9))
433 drvdata->retstack = true;
434 else
435 drvdata->retstack = false;
436
437 /* NUMEVENT, bits[11:10] Number of events field */
438 drvdata->nr_event = BMVAL(etmidr0, 10, 11);
439 /* QSUPP, bits[16:15] Q element support field */
440 drvdata->q_support = BMVAL(etmidr0, 15, 16);
441 /* TSSIZE, bits[28:24] Global timestamp size field */
442 drvdata->ts_size = BMVAL(etmidr0, 24, 28);
443
444 /* base architecture of trace unit */
445 etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
446 /*
447 * TRCARCHMIN, bits[7:4] architecture the minor version number
448 * TRCARCHMAJ, bits[11:8] architecture major versin number
449 */
450 drvdata->arch = BMVAL(etmidr1, 4, 11);
451
452 /* maximum size of resources */
453 etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
454 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
455 drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
456 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
457 drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
458 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
459 drvdata->ccsize = BMVAL(etmidr2, 25, 28);
460
461 etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
462 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
463 drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
464 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
465 drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
466 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
467 drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
468
469 /*
470 * TRCERR, bit[24] whether a trace unit can trace a
471 * system error exception.
472 */
473 if (BMVAL(etmidr3, 24, 24))
474 drvdata->trc_error = true;
475 else
476 drvdata->trc_error = false;
477
478 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
479 if (BMVAL(etmidr3, 25, 25))
480 drvdata->syncpr = true;
481 else
482 drvdata->syncpr = false;
483
484 /* STALLCTL, bit[26] is stall control implemented? */
485 if (BMVAL(etmidr3, 26, 26))
486 drvdata->stallctl = true;
487 else
488 drvdata->stallctl = false;
489
490 /* SYSSTALL, bit[27] implementation can support stall control? */
491 if (BMVAL(etmidr3, 27, 27))
492 drvdata->sysstall = true;
493 else
494 drvdata->sysstall = false;
495
496 /* NUMPROC, bits[30:28] the number of PEs available for tracing */
497 drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
498
499 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
500 if (BMVAL(etmidr3, 31, 31))
501 drvdata->nooverflow = true;
502 else
503 drvdata->nooverflow = false;
504
505 /* number of resources trace unit supports */
506 etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
507 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
508 drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
509 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
510 drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
511 /*
512 * NUMRSPAIR, bits[19:16]
513 * The number of resource pairs conveyed by the HW starts at 0, i.e a
514 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
515 * As such add 1 to the value of NUMRSPAIR for a better representation.
516 */
517 drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
518 /*
519 * NUMSSCC, bits[23:20] the number of single-shot
520 * comparator control for tracing
521 */
522 drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
523 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
524 drvdata->numcidc = BMVAL(etmidr4, 24, 27);
525 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
526 drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
527
528 etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
529 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
530 drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
531 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
532 drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
533 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
534 if (BMVAL(etmidr5, 22, 22))
535 drvdata->atbtrig = true;
536 else
537 drvdata->atbtrig = false;
538 /*
539 * LPOVERRIDE, bit[23] implementation supports
540 * low-power state override
541 */
542 if (BMVAL(etmidr5, 23, 23))
543 drvdata->lpoverride = true;
544 else
545 drvdata->lpoverride = false;
546 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
547 drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
548 /* NUMCNTR, bits[30:28] number of counters available for tracing */
549 drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
550 CS_LOCK(drvdata->base);
551 }
552
553 static void etm4_set_default(struct etmv4_config *config)
554 {
555 if (WARN_ON_ONCE(!config))
556 return;
557
558 /*
559 * Make default initialisation trace everything
560 *
561 * Select the "always true" resource selector on the
562 * "Enablign Event" line and configure address range comparator
563 * '0' to trace all the possible address range. From there
564 * configure the "include/exclude" engine to include address
565 * range comparator '0'.
566 */
567
568 /* disable all events tracing */
569 config->eventctrl0 = 0x0;
570 config->eventctrl1 = 0x0;
571
572 /* disable stalling */
573 config->stall_ctrl = 0x0;
574
575 /* enable trace synchronization every 4096 bytes, if available */
576 config->syncfreq = 0xC;
577
578 /* disable timestamp event */
579 config->ts_ctrl = 0x0;
580
581 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
582 config->vinst_ctrl |= BIT(0);
583
584 /*
585 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
586 * in the started state
587 */
588 config->vinst_ctrl |= BIT(9);
589
590 /*
591 * Configure address range comparator '0' to encompass all
592 * possible addresses.
593 */
594
595 /* First half of default address comparator: start at address 0 */
596 config->addr_val[ETM_DEFAULT_ADDR_COMP] = 0x0;
597 /* trace instruction addresses */
598 config->addr_acc[ETM_DEFAULT_ADDR_COMP] &= ~(BIT(0) | BIT(1));
599 /* EXLEVEL_NS, bits[12:15], only trace application and kernel space */
600 config->addr_acc[ETM_DEFAULT_ADDR_COMP] |= ETM_EXLEVEL_NS_HYP;
601 /* EXLEVEL_S, bits[11:8], don't trace anything in secure state */
602 config->addr_acc[ETM_DEFAULT_ADDR_COMP] |= (ETM_EXLEVEL_S_APP |
603 ETM_EXLEVEL_S_OS |
604 ETM_EXLEVEL_S_HYP);
605 config->addr_type[ETM_DEFAULT_ADDR_COMP] = ETM_ADDR_TYPE_RANGE;
606
607 /*
608 * Second half of default address comparator: go all
609 * the way to the top.
610 */
611 config->addr_val[ETM_DEFAULT_ADDR_COMP + 1] = ~0x0;
612 /* trace instruction addresses */
613 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] &= ~(BIT(0) | BIT(1));
614 /* Address comparator type must be equal for both halves */
615 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] =
616 config->addr_acc[ETM_DEFAULT_ADDR_COMP];
617 config->addr_type[ETM_DEFAULT_ADDR_COMP + 1] = ETM_ADDR_TYPE_RANGE;
618
619 /*
620 * Configure the ViewInst function to filter on address range
621 * comparator '0'.
622 */
623 config->viiectlr = BIT(0);
624
625 /* no start-stop filtering for ViewInst */
626 config->vissctlr = 0x0;
627 }
628
629 void etm4_config_trace_mode(struct etmv4_config *config)
630 {
631 u32 addr_acc, mode;
632
633 mode = config->mode;
634 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
635
636 /* excluding kernel AND user space doesn't make sense */
637 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
638
639 /* nothing to do if neither flags are set */
640 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
641 return;
642
643 addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
644 /* clear default config */
645 addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS);
646
647 /*
648 * EXLEVEL_NS, bits[15:12]
649 * The Exception levels are:
650 * Bit[12] Exception level 0 - Application
651 * Bit[13] Exception level 1 - OS
652 * Bit[14] Exception level 2 - Hypervisor
653 * Bit[15] Never implemented
654 */
655 if (mode & ETM_MODE_EXCL_KERN)
656 addr_acc |= ETM_EXLEVEL_NS_OS;
657 else
658 addr_acc |= ETM_EXLEVEL_NS_APP;
659
660 config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
661 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
662 }
663
664 static int etm4_online_cpu(unsigned int cpu)
665 {
666 if (!etmdrvdata[cpu])
667 return 0;
668
669 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
670 coresight_enable(etmdrvdata[cpu]->csdev);
671 return 0;
672 }
673
674 static int etm4_starting_cpu(unsigned int cpu)
675 {
676 if (!etmdrvdata[cpu])
677 return 0;
678
679 spin_lock(&etmdrvdata[cpu]->spinlock);
680 if (!etmdrvdata[cpu]->os_unlock) {
681 etm4_os_unlock(etmdrvdata[cpu]);
682 etmdrvdata[cpu]->os_unlock = true;
683 }
684
685 if (local_read(&etmdrvdata[cpu]->mode))
686 etm4_enable_hw(etmdrvdata[cpu]);
687 spin_unlock(&etmdrvdata[cpu]->spinlock);
688 return 0;
689 }
690
691 static int etm4_dying_cpu(unsigned int cpu)
692 {
693 if (!etmdrvdata[cpu])
694 return 0;
695
696 spin_lock(&etmdrvdata[cpu]->spinlock);
697 if (local_read(&etmdrvdata[cpu]->mode))
698 etm4_disable_hw(etmdrvdata[cpu]);
699 spin_unlock(&etmdrvdata[cpu]->spinlock);
700 return 0;
701 }
702
703 static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
704 {
705 drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
706 }
707
708 static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
709 {
710 int ret;
711 void __iomem *base;
712 struct device *dev = &adev->dev;
713 struct coresight_platform_data *pdata = NULL;
714 struct etmv4_drvdata *drvdata;
715 struct resource *res = &adev->res;
716 struct coresight_desc *desc;
717 struct device_node *np = adev->dev.of_node;
718
719 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
720 if (!desc)
721 return -ENOMEM;
722
723 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
724 if (!drvdata)
725 return -ENOMEM;
726
727 if (np) {
728 pdata = of_get_coresight_platform_data(dev, np);
729 if (IS_ERR(pdata))
730 return PTR_ERR(pdata);
731 adev->dev.platform_data = pdata;
732 }
733
734 drvdata->dev = &adev->dev;
735 dev_set_drvdata(dev, drvdata);
736
737 /* Validity for the resource is already checked by the AMBA core */
738 base = devm_ioremap_resource(dev, res);
739 if (IS_ERR(base))
740 return PTR_ERR(base);
741
742 drvdata->base = base;
743
744 spin_lock_init(&drvdata->spinlock);
745
746 drvdata->cpu = pdata ? pdata->cpu : 0;
747
748 get_online_cpus();
749 etmdrvdata[drvdata->cpu] = drvdata;
750
751 if (smp_call_function_single(drvdata->cpu,
752 etm4_init_arch_data, drvdata, 1))
753 dev_err(dev, "ETM arch init failed\n");
754
755 if (!etm4_count++) {
756 cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING,
757 "AP_ARM_CORESIGHT4_STARTING",
758 etm4_starting_cpu, etm4_dying_cpu);
759 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
760 "AP_ARM_CORESIGHT4_ONLINE",
761 etm4_online_cpu, NULL);
762 if (ret < 0)
763 goto err_arch_supported;
764 hp_online = ret;
765 }
766
767 put_online_cpus();
768
769 if (etm4_arch_supported(drvdata->arch) == false) {
770 ret = -EINVAL;
771 goto err_arch_supported;
772 }
773
774 etm4_init_trace_id(drvdata);
775 etm4_set_default(&drvdata->config);
776
777 desc->type = CORESIGHT_DEV_TYPE_SOURCE;
778 desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
779 desc->ops = &etm4_cs_ops;
780 desc->pdata = pdata;
781 desc->dev = dev;
782 desc->groups = coresight_etmv4_groups;
783 drvdata->csdev = coresight_register(desc);
784 if (IS_ERR(drvdata->csdev)) {
785 ret = PTR_ERR(drvdata->csdev);
786 goto err_arch_supported;
787 }
788
789 ret = etm_perf_symlink(drvdata->csdev, true);
790 if (ret) {
791 coresight_unregister(drvdata->csdev);
792 goto err_arch_supported;
793 }
794
795 pm_runtime_put(&adev->dev);
796 dev_info(dev, "%s initialized\n", (char *)id->data);
797
798 if (boot_enable) {
799 coresight_enable(drvdata->csdev);
800 drvdata->boot_enable = true;
801 }
802
803 return 0;
804
805 err_arch_supported:
806 if (--etm4_count == 0) {
807 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING);
808 if (hp_online)
809 cpuhp_remove_state_nocalls(hp_online);
810 }
811 return ret;
812 }
813
814 static struct amba_id etm4_ids[] = {
815 { /* ETM 4.0 - Cortex-A53 */
816 .id = 0x000bb95d,
817 .mask = 0x000fffff,
818 .data = "ETM 4.0",
819 },
820 { /* ETM 4.0 - Cortex-A57 */
821 .id = 0x000bb95e,
822 .mask = 0x000fffff,
823 .data = "ETM 4.0",
824 },
825 { /* ETM 4.0 - A72, Maia, HiSilicon */
826 .id = 0x000bb95a,
827 .mask = 0x000fffff,
828 .data = "ETM 4.0",
829 },
830 { 0, 0},
831 };
832
833 static struct amba_driver etm4x_driver = {
834 .drv = {
835 .name = "coresight-etm4x",
836 .suppress_bind_attrs = true,
837 },
838 .probe = etm4_probe,
839 .id_table = etm4_ids,
840 };
841 builtin_amba_driver(etm4x_driver);