Debian Codegraph @

drivers/hwtracing/coresight/coresight-etm3x-core.c

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