blob: 04462fffe8e4191ff45b37ba2de8be56f2b892b0 [file] [log] [blame]
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001/* 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>
Mathieu Poirierfc208ab2016-04-05 11:53:45 -060029#include <linux/coresight-pmu.h>
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060030#include <linux/pm_wakeup.h>
31#include <linux/amba/bus.h>
32#include <linux/seq_file.h>
33#include <linux/uaccess.h>
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -060034#include <linux/perf_event.h>
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060035#include <linux/pm_runtime.h>
36#include <asm/sections.h>
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -060037#include <asm/local.h>
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060038
39#include "coresight-etm4x.h"
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -060040#include "coresight-etm-perf.h"
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060041
42static int boot_enable;
43module_param_named(boot_enable, boot_enable, int, S_IRUGO);
44
45/* The number of ETMv4 currently registered */
46static int etm4_count;
47static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -060048static void etm4_set_default(struct etmv4_config *config);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060049
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +000050static enum cpuhp_state hp_online;
51
Mathieu Poirier66bbbb72016-04-05 11:53:46 -060052static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060053{
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060054 /* Writing any value to ETMOSLAR unlocks the trace registers */
55 writel_relaxed(0x0, drvdata->base + TRCOSLAR);
Mathieu Poirier66bbbb72016-04-05 11:53:46 -060056 drvdata->os_unlock = true;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060057 isb();
58}
59
60static bool etm4_arch_supported(u8 arch)
61{
62 switch (arch) {
63 case ETM_ARCH_V4:
64 break;
65 default:
66 return false;
67 }
68 return true;
69}
70
Mathieu Poirier52210c82016-02-02 14:14:01 -070071static int etm4_cpu_id(struct coresight_device *csdev)
72{
73 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
74
75 return drvdata->cpu;
76}
77
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060078static int etm4_trace_id(struct coresight_device *csdev)
79{
80 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060081
Sudeep Hollab1149ad2016-08-25 15:18:53 -060082 return drvdata->trcid;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060083}
84
85static void etm4_enable_hw(void *info)
86{
87 int i;
88 struct etmv4_drvdata *drvdata = info;
Mathieu Poirier54ff8922016-04-05 11:53:44 -060089 struct etmv4_config *config = &drvdata->config;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060090
91 CS_UNLOCK(drvdata->base);
92
93 etm4_os_unlock(drvdata);
94
95 /* Disable the trace unit before programming trace registers */
96 writel_relaxed(0, drvdata->base + TRCPRGCTLR);
97
98 /* wait for TRCSTATR.IDLE to go up */
99 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
100 dev_err(drvdata->dev,
Suzuki K Poulose67337e82016-08-25 15:19:00 -0600101 "timeout while waiting for Idle Trace Status\n");
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600102
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600103 writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
104 writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600105 /* nothing specific implemented */
106 writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600107 writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
108 writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
109 writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
110 writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
111 writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
112 writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
113 writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600114 writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600115 writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
116 writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
117 writel_relaxed(config->vissctlr,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600118 drvdata->base + TRCVISSCTLR);
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600119 writel_relaxed(config->vipcssctlr,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600120 drvdata->base + TRCVIPCSSCTLR);
121 for (i = 0; i < drvdata->nrseqstate - 1; i++)
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600122 writel_relaxed(config->seq_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600123 drvdata->base + TRCSEQEVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600124 writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
125 writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
126 writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600127 for (i = 0; i < drvdata->nr_cntr; i++) {
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600128 writel_relaxed(config->cntrldvr[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600129 drvdata->base + TRCCNTRLDVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600130 writel_relaxed(config->cntr_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600131 drvdata->base + TRCCNTCTLRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600132 writel_relaxed(config->cntr_val[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600133 drvdata->base + TRCCNTVRn(i));
134 }
Chunyan Zhang497b5952015-10-07 09:26:38 -0600135
136 /* Resource selector pair 0 is always implemented and reserved */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600137 for (i = 0; i < drvdata->nr_resource * 2; i++)
138 writel_relaxed(config->res_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600139 drvdata->base + TRCRSCTLRn(i));
140
141 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600142 writel_relaxed(config->ss_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600143 drvdata->base + TRCSSCCRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600144 writel_relaxed(config->ss_status[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600145 drvdata->base + TRCSSCSRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600146 writel_relaxed(config->ss_pe_cmp[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600147 drvdata->base + TRCSSPCICRn(i));
148 }
149 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600150 writeq_relaxed(config->addr_val[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600151 drvdata->base + TRCACVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600152 writeq_relaxed(config->addr_acc[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600153 drvdata->base + TRCACATRn(i));
154 }
155 for (i = 0; i < drvdata->numcidc; i++)
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600156 writeq_relaxed(config->ctxid_pid[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600157 drvdata->base + TRCCIDCVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600158 writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
159 writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600160
161 for (i = 0; i < drvdata->numvmidc; i++)
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600162 writeq_relaxed(config->vmid_val[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600163 drvdata->base + TRCVMIDCVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600164 writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
165 writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600166
Sudeep Holla46a3d5c2016-08-25 15:19:08 -0600167 /*
168 * Request to keep the trace unit powered and also
169 * emulation of powerdown
170 */
171 writel_relaxed(readl_relaxed(drvdata->base + TRCPDCR) | TRCPDCR_PU,
172 drvdata->base + TRCPDCR);
173
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600174 /* Enable the trace unit */
175 writel_relaxed(1, drvdata->base + TRCPRGCTLR);
176
177 /* wait for TRCSTATR.IDLE to go back down to '0' */
178 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
179 dev_err(drvdata->dev,
Suzuki K Poulose67337e82016-08-25 15:19:00 -0600180 "timeout while waiting for Idle Trace Status\n");
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600181
182 CS_LOCK(drvdata->base);
183
184 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
185}
186
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600187static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
Mathieu Poirier68905d72016-08-25 15:19:10 -0600188 struct perf_event *event)
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600189{
190 struct etmv4_config *config = &drvdata->config;
Mathieu Poirier68905d72016-08-25 15:19:10 -0600191 struct perf_event_attr *attr = &event->attr;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600192
193 if (!attr)
194 return -EINVAL;
195
196 /* Clear configuration from previous run */
197 memset(config, 0, sizeof(struct etmv4_config));
198
199 if (attr->exclude_kernel)
200 config->mode = ETM_MODE_EXCL_KERN;
201
202 if (attr->exclude_user)
203 config->mode = ETM_MODE_EXCL_USER;
204
205 /* Always start from the default config */
206 etm4_set_default(config);
207
208 /*
209 * By default the tracers are configured to trace the whole address
210 * range. Narrow the field only if requested by user space.
211 */
212 if (config->mode)
213 etm4_config_trace_mode(config);
214
215 /* Go from generic option to ETMv4 specifics */
216 if (attr->config & BIT(ETM_OPT_CYCACC))
217 config->cfg |= ETMv4_MODE_CYCACC;
218 if (attr->config & BIT(ETM_OPT_TS))
219 config->cfg |= ETMv4_MODE_TIMESTAMP;
220
221 return 0;
222}
223
224static int etm4_enable_perf(struct coresight_device *csdev,
Mathieu Poirier68905d72016-08-25 15:19:10 -0600225 struct perf_event *event)
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600226{
227 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
228
229 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
230 return -EINVAL;
231
232 /* Configure the tracer based on the session's specifics */
Mathieu Poirier68905d72016-08-25 15:19:10 -0600233 etm4_parse_event_config(drvdata, event);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600234 /* And enable it */
235 etm4_enable_hw(drvdata);
236
237 return 0;
238}
239
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600240static int etm4_enable_sysfs(struct coresight_device *csdev)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600241{
242 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
243 int ret;
244
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600245 spin_lock(&drvdata->spinlock);
246
247 /*
248 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
249 * ensures that register writes occur when cpu is powered.
250 */
251 ret = smp_call_function_single(drvdata->cpu,
252 etm4_enable_hw, drvdata, 1);
253 if (ret)
254 goto err;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600255
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600256 drvdata->sticky_enable = true;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600257 spin_unlock(&drvdata->spinlock);
258
259 dev_info(drvdata->dev, "ETM tracing enabled\n");
260 return 0;
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600261
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600262err:
263 spin_unlock(&drvdata->spinlock);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600264 return ret;
265}
266
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600267static int etm4_enable(struct coresight_device *csdev,
Mathieu Poirier68905d72016-08-25 15:19:10 -0600268 struct perf_event *event, u32 mode)
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600269{
270 int ret;
271 u32 val;
272 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
273
274 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
275
276 /* Someone is already using the tracer */
277 if (val)
278 return -EBUSY;
279
280 switch (mode) {
281 case CS_MODE_SYSFS:
282 ret = etm4_enable_sysfs(csdev);
283 break;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600284 case CS_MODE_PERF:
Mathieu Poirier68905d72016-08-25 15:19:10 -0600285 ret = etm4_enable_perf(csdev, event);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600286 break;
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600287 default:
288 ret = -EINVAL;
289 }
290
291 /* The tracer didn't start */
292 if (ret)
293 local_set(&drvdata->mode, CS_MODE_DISABLED);
294
295 return ret;
296}
297
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600298static void etm4_disable_hw(void *info)
299{
300 u32 control;
301 struct etmv4_drvdata *drvdata = info;
302
303 CS_UNLOCK(drvdata->base);
304
Sudeep Holla46a3d5c2016-08-25 15:19:08 -0600305 /* power can be removed from the trace unit now */
306 control = readl_relaxed(drvdata->base + TRCPDCR);
307 control &= ~TRCPDCR_PU;
308 writel_relaxed(control, drvdata->base + TRCPDCR);
309
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600310 control = readl_relaxed(drvdata->base + TRCPRGCTLR);
311
312 /* EN, bit[0] Trace unit enable bit */
313 control &= ~0x1;
314
315 /* make sure everything completes before disabling */
316 mb();
317 isb();
318 writel_relaxed(control, drvdata->base + TRCPRGCTLR);
319
320 CS_LOCK(drvdata->base);
321
322 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
323}
324
Mathieu Poirier68905d72016-08-25 15:19:10 -0600325static int etm4_disable_perf(struct coresight_device *csdev,
326 struct perf_event *event)
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600327{
328 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
329
330 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
331 return -EINVAL;
332
333 etm4_disable_hw(drvdata);
334 return 0;
335}
336
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600337static void etm4_disable_sysfs(struct coresight_device *csdev)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600338{
339 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
340
341 /*
342 * Taking hotplug lock here protects from clocks getting disabled
343 * with tracing being left on (crash scenario) if user disable occurs
344 * after cpu online mask indicates the cpu is offline but before the
345 * DYING hotplug callback is serviced by the ETM driver.
346 */
347 get_online_cpus();
348 spin_lock(&drvdata->spinlock);
349
350 /*
351 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
352 * ensures that register writes occur when cpu is powered.
353 */
354 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600355
356 spin_unlock(&drvdata->spinlock);
357 put_online_cpus();
358
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600359 dev_info(drvdata->dev, "ETM tracing disabled\n");
360}
361
Mathieu Poirier68905d72016-08-25 15:19:10 -0600362static void etm4_disable(struct coresight_device *csdev,
363 struct perf_event *event)
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600364{
365 u32 mode;
366 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
367
368 /*
369 * For as long as the tracer isn't disabled another entity can't
370 * change its status. As such we can read the status here without
371 * fearing it will change under us.
372 */
373 mode = local_read(&drvdata->mode);
374
375 switch (mode) {
376 case CS_MODE_DISABLED:
377 break;
378 case CS_MODE_SYSFS:
379 etm4_disable_sysfs(csdev);
380 break;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600381 case CS_MODE_PERF:
Mathieu Poirier68905d72016-08-25 15:19:10 -0600382 etm4_disable_perf(csdev, event);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600383 break;
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600384 }
385
386 if (mode)
387 local_set(&drvdata->mode, CS_MODE_DISABLED);
388}
389
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600390static const struct coresight_ops_source etm4_source_ops = {
Mathieu Poirier52210c82016-02-02 14:14:01 -0700391 .cpu_id = etm4_cpu_id,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600392 .trace_id = etm4_trace_id,
393 .enable = etm4_enable,
394 .disable = etm4_disable,
395};
396
397static const struct coresight_ops etm4_cs_ops = {
398 .source_ops = &etm4_source_ops,
399};
400
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600401static void etm4_init_arch_data(void *info)
402{
403 u32 etmidr0;
404 u32 etmidr1;
405 u32 etmidr2;
406 u32 etmidr3;
407 u32 etmidr4;
408 u32 etmidr5;
409 struct etmv4_drvdata *drvdata = info;
410
Mathieu Poirier66bbbb72016-04-05 11:53:46 -0600411 /* Make sure all registers are accessible */
412 etm4_os_unlock(drvdata);
413
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600414 CS_UNLOCK(drvdata->base);
415
416 /* find all capabilities of the tracing unit */
417 etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
418
419 /* INSTP0, bits[2:1] P0 tracing support field */
420 if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
421 drvdata->instrp0 = true;
422 else
423 drvdata->instrp0 = false;
424
425 /* TRCBB, bit[5] Branch broadcast tracing support bit */
426 if (BMVAL(etmidr0, 5, 5))
427 drvdata->trcbb = true;
428 else
429 drvdata->trcbb = false;
430
431 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
432 if (BMVAL(etmidr0, 6, 6))
433 drvdata->trccond = true;
434 else
435 drvdata->trccond = false;
436
437 /* TRCCCI, bit[7] Cycle counting instruction bit */
438 if (BMVAL(etmidr0, 7, 7))
439 drvdata->trccci = true;
440 else
441 drvdata->trccci = false;
442
443 /* RETSTACK, bit[9] Return stack bit */
444 if (BMVAL(etmidr0, 9, 9))
445 drvdata->retstack = true;
446 else
447 drvdata->retstack = false;
448
449 /* NUMEVENT, bits[11:10] Number of events field */
450 drvdata->nr_event = BMVAL(etmidr0, 10, 11);
451 /* QSUPP, bits[16:15] Q element support field */
452 drvdata->q_support = BMVAL(etmidr0, 15, 16);
453 /* TSSIZE, bits[28:24] Global timestamp size field */
454 drvdata->ts_size = BMVAL(etmidr0, 24, 28);
455
456 /* base architecture of trace unit */
457 etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
458 /*
459 * TRCARCHMIN, bits[7:4] architecture the minor version number
460 * TRCARCHMAJ, bits[11:8] architecture major versin number
461 */
462 drvdata->arch = BMVAL(etmidr1, 4, 11);
463
464 /* maximum size of resources */
465 etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
466 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
467 drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
468 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
469 drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
470 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
471 drvdata->ccsize = BMVAL(etmidr2, 25, 28);
472
473 etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
474 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
475 drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
476 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
477 drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
478 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
479 drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
480
481 /*
482 * TRCERR, bit[24] whether a trace unit can trace a
483 * system error exception.
484 */
485 if (BMVAL(etmidr3, 24, 24))
486 drvdata->trc_error = true;
487 else
488 drvdata->trc_error = false;
489
490 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
491 if (BMVAL(etmidr3, 25, 25))
492 drvdata->syncpr = true;
493 else
494 drvdata->syncpr = false;
495
496 /* STALLCTL, bit[26] is stall control implemented? */
497 if (BMVAL(etmidr3, 26, 26))
498 drvdata->stallctl = true;
499 else
500 drvdata->stallctl = false;
501
502 /* SYSSTALL, bit[27] implementation can support stall control? */
503 if (BMVAL(etmidr3, 27, 27))
504 drvdata->sysstall = true;
505 else
506 drvdata->sysstall = false;
507
508 /* NUMPROC, bits[30:28] the number of PEs available for tracing */
509 drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
510
511 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
512 if (BMVAL(etmidr3, 31, 31))
513 drvdata->nooverflow = true;
514 else
515 drvdata->nooverflow = false;
516
517 /* number of resources trace unit supports */
518 etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
519 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
520 drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
521 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
522 drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
Chunyan Zhang497b5952015-10-07 09:26:38 -0600523 /*
524 * NUMRSPAIR, bits[19:16]
525 * The number of resource pairs conveyed by the HW starts at 0, i.e a
526 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
527 * As such add 1 to the value of NUMRSPAIR for a better representation.
528 */
529 drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600530 /*
531 * NUMSSCC, bits[23:20] the number of single-shot
532 * comparator control for tracing
533 */
534 drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
535 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
536 drvdata->numcidc = BMVAL(etmidr4, 24, 27);
537 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
538 drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
539
540 etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
541 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
542 drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
543 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
544 drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
545 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
546 if (BMVAL(etmidr5, 22, 22))
547 drvdata->atbtrig = true;
548 else
549 drvdata->atbtrig = false;
550 /*
551 * LPOVERRIDE, bit[23] implementation supports
552 * low-power state override
553 */
554 if (BMVAL(etmidr5, 23, 23))
555 drvdata->lpoverride = true;
556 else
557 drvdata->lpoverride = false;
558 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
559 drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
560 /* NUMCNTR, bits[30:28] number of counters available for tracing */
561 drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
562 CS_LOCK(drvdata->base);
563}
564
Mathieu Poirier2a5695a52016-08-25 15:19:13 -0600565static void etm4_set_default_config(struct etmv4_config *config)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600566{
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600567 /* disable all events tracing */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600568 config->eventctrl0 = 0x0;
569 config->eventctrl1 = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600570
571 /* disable stalling */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600572 config->stall_ctrl = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600573
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600574 /* enable trace synchronization every 4096 bytes, if available */
575 config->syncfreq = 0xC;
576
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600577 /* disable timestamp event */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600578 config->ts_ctrl = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600579
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600580 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
581 config->vinst_ctrl |= BIT(0);
Mathieu Poirier2a5695a52016-08-25 15:19:13 -0600582}
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600583
Mathieu Poirier2a5695a52016-08-25 15:19:13 -0600584static void etm4_set_default_filter(struct etmv4_config *config)
585{
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600586 u64 start, stop, access_type = 0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600587
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600588 /*
589 * Configure address range comparator '0' to encompass all
590 * possible addresses.
591 */
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600592 start = 0x0;
593 stop = ~0x0;
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600594
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600595 /* EXLEVEL_NS, bits[12:15], always stay away from hypervisor mode. */
596 access_type = ETM_EXLEVEL_NS_HYP;
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600597
598 /*
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600599 * EXLEVEL_S, bits[11:8], don't trace anything happening
600 * in secure state.
601 */
602 access_type |= (ETM_EXLEVEL_S_APP |
603 ETM_EXLEVEL_S_OS |
604 ETM_EXLEVEL_S_HYP);
605
606 /* First half of default address comparator */
607 config->addr_val[ETM_DEFAULT_ADDR_COMP] = start;
608 config->addr_acc[ETM_DEFAULT_ADDR_COMP] = access_type;
609 config->addr_type[ETM_DEFAULT_ADDR_COMP] = ETM_ADDR_TYPE_RANGE;
610
611 /* Second half of default address comparator */
612 config->addr_val[ETM_DEFAULT_ADDR_COMP + 1] = stop;
613 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = access_type;
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600614 config->addr_type[ETM_DEFAULT_ADDR_COMP + 1] = ETM_ADDR_TYPE_RANGE;
615
616 /*
617 * Configure the ViewInst function to filter on address range
618 * comparator '0'.
619 */
620 config->viiectlr = BIT(0);
621
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600622 /*
623 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
624 * in the started state
625 */
626 config->vinst_ctrl |= BIT(9);
627
628 /* No start-stop filtering for ViewInst */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600629 config->vissctlr = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600630}
631
Mathieu Poirier2a5695a52016-08-25 15:19:13 -0600632static void etm4_set_default(struct etmv4_config *config)
633{
634 if (WARN_ON_ONCE(!config))
635 return;
636
637 /*
638 * Make default initialisation trace everything
639 *
640 * Select the "always true" resource selector on the
641 * "Enablign Event" line and configure address range comparator
642 * '0' to trace all the possible address range. From there
643 * configure the "include/exclude" engine to include address
644 * range comparator '0'.
645 */
646 etm4_set_default_config(config);
647 etm4_set_default_filter(config);
648}
649
Mathieu Poirier4f6fce52016-04-05 11:53:48 -0600650void etm4_config_trace_mode(struct etmv4_config *config)
651{
652 u32 addr_acc, mode;
653
654 mode = config->mode;
655 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
656
657 /* excluding kernel AND user space doesn't make sense */
658 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
659
660 /* nothing to do if neither flags are set */
661 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
662 return;
663
664 addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
665 /* clear default config */
666 addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS);
667
668 /*
669 * EXLEVEL_NS, bits[15:12]
670 * The Exception levels are:
671 * Bit[12] Exception level 0 - Application
672 * Bit[13] Exception level 1 - OS
673 * Bit[14] Exception level 2 - Hypervisor
674 * Bit[15] Never implemented
675 */
676 if (mode & ETM_MODE_EXCL_KERN)
677 addr_acc |= ETM_EXLEVEL_NS_OS;
678 else
679 addr_acc |= ETM_EXLEVEL_NS_APP;
680
681 config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
682 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
683}
684
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000685static int etm4_online_cpu(unsigned int cpu)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600686{
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600687 if (!etmdrvdata[cpu])
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000688 return 0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600689
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000690 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
691 coresight_enable(etmdrvdata[cpu]->csdev);
692 return 0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600693}
694
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000695static int etm4_starting_cpu(unsigned int cpu)
696{
697 if (!etmdrvdata[cpu])
698 return 0;
699
700 spin_lock(&etmdrvdata[cpu]->spinlock);
701 if (!etmdrvdata[cpu]->os_unlock) {
702 etm4_os_unlock(etmdrvdata[cpu]);
703 etmdrvdata[cpu]->os_unlock = true;
704 }
705
706 if (local_read(&etmdrvdata[cpu]->mode))
707 etm4_enable_hw(etmdrvdata[cpu]);
708 spin_unlock(&etmdrvdata[cpu]->spinlock);
709 return 0;
710}
711
712static int etm4_dying_cpu(unsigned int cpu)
713{
714 if (!etmdrvdata[cpu])
715 return 0;
716
717 spin_lock(&etmdrvdata[cpu]->spinlock);
718 if (local_read(&etmdrvdata[cpu]->mode))
719 etm4_disable_hw(etmdrvdata[cpu]);
720 spin_unlock(&etmdrvdata[cpu]->spinlock);
721 return 0;
722}
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600723
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600724static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
725{
726 drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
727}
728
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600729static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
730{
731 int ret;
732 void __iomem *base;
733 struct device *dev = &adev->dev;
734 struct coresight_platform_data *pdata = NULL;
735 struct etmv4_drvdata *drvdata;
736 struct resource *res = &adev->res;
Suzuki K Poulose94862952016-08-25 15:19:05 -0600737 struct coresight_desc desc = { 0 };
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600738 struct device_node *np = adev->dev.of_node;
739
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600740 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
741 if (!drvdata)
742 return -ENOMEM;
743
744 if (np) {
745 pdata = of_get_coresight_platform_data(dev, np);
746 if (IS_ERR(pdata))
747 return PTR_ERR(pdata);
748 adev->dev.platform_data = pdata;
749 }
750
751 drvdata->dev = &adev->dev;
752 dev_set_drvdata(dev, drvdata);
753
754 /* Validity for the resource is already checked by the AMBA core */
755 base = devm_ioremap_resource(dev, res);
756 if (IS_ERR(base))
757 return PTR_ERR(base);
758
759 drvdata->base = base;
760
761 spin_lock_init(&drvdata->spinlock);
762
763 drvdata->cpu = pdata ? pdata->cpu : 0;
764
765 get_online_cpus();
766 etmdrvdata[drvdata->cpu] = drvdata;
767
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600768 if (smp_call_function_single(drvdata->cpu,
769 etm4_init_arch_data, drvdata, 1))
770 dev_err(dev, "ETM arch init failed\n");
771
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000772 if (!etm4_count++) {
773 cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING,
774 "AP_ARM_CORESIGHT4_STARTING",
775 etm4_starting_cpu, etm4_dying_cpu);
776 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
777 "AP_ARM_CORESIGHT4_ONLINE",
778 etm4_online_cpu, NULL);
779 if (ret < 0)
780 goto err_arch_supported;
781 hp_online = ret;
782 }
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600783
784 put_online_cpus();
785
786 if (etm4_arch_supported(drvdata->arch) == false) {
787 ret = -EINVAL;
788 goto err_arch_supported;
789 }
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600790
791 etm4_init_trace_id(drvdata);
792 etm4_set_default(&drvdata->config);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600793
Suzuki K Poulose94862952016-08-25 15:19:05 -0600794 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
795 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
796 desc.ops = &etm4_cs_ops;
797 desc.pdata = pdata;
798 desc.dev = dev;
799 desc.groups = coresight_etmv4_groups;
800 drvdata->csdev = coresight_register(&desc);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600801 if (IS_ERR(drvdata->csdev)) {
802 ret = PTR_ERR(drvdata->csdev);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600803 goto err_arch_supported;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600804 }
805
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600806 ret = etm_perf_symlink(drvdata->csdev, true);
807 if (ret) {
808 coresight_unregister(drvdata->csdev);
809 goto err_arch_supported;
810 }
811
812 pm_runtime_put(&adev->dev);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600813 dev_info(dev, "%s initialized\n", (char *)id->data);
814
815 if (boot_enable) {
816 coresight_enable(drvdata->csdev);
817 drvdata->boot_enable = true;
818 }
819
820 return 0;
821
822err_arch_supported:
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000823 if (--etm4_count == 0) {
824 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING);
825 if (hp_online)
826 cpuhp_remove_state_nocalls(hp_online);
827 }
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600828 return ret;
829}
830
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600831static struct amba_id etm4_ids[] = {
Suzuki K Poulose78247e22016-08-25 15:18:58 -0600832 { /* ETM 4.0 - Cortex-A53 */
833 .id = 0x000bb95d,
834 .mask = 0x000fffff,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600835 .data = "ETM 4.0",
836 },
Suzuki K Poulose78247e22016-08-25 15:18:58 -0600837 { /* ETM 4.0 - Cortex-A57 */
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600838 .id = 0x000bb95e,
839 .mask = 0x000fffff,
840 .data = "ETM 4.0",
841 },
Li Pengcheng960e3092016-05-03 11:33:42 -0600842 { /* ETM 4.0 - A72, Maia, HiSilicon */
843 .id = 0x000bb95a,
844 .mask = 0x000fffff,
845 .data = "ETM 4.0",
846 },
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600847 { 0, 0},
848};
849
850static struct amba_driver etm4x_driver = {
851 .drv = {
852 .name = "coresight-etm4x",
Mathieu Poirierb15f0fb2016-02-02 14:14:00 -0700853 .suppress_bind_attrs = true,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600854 },
855 .probe = etm4_probe,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600856 .id_table = etm4_ids,
857};
Paul Gortmaker941943c2016-02-17 17:52:03 -0700858builtin_amba_driver(etm4x_driver);