blob: da27f8edba504593f8abdfc59f0c5a792c8f3180 [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 Poirier2703d742016-08-25 15:19:17 -060048static void etm4_set_default_config(struct etmv4_config *config);
49static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
50 struct perf_event *event);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060051
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +000052static enum cpuhp_state hp_online;
53
Mathieu Poirier66bbbb72016-04-05 11:53:46 -060054static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060055{
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060056 /* Writing any value to ETMOSLAR unlocks the trace registers */
57 writel_relaxed(0x0, drvdata->base + TRCOSLAR);
Mathieu Poirier66bbbb72016-04-05 11:53:46 -060058 drvdata->os_unlock = true;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060059 isb();
60}
61
62static bool etm4_arch_supported(u8 arch)
63{
Sai Prakash Ranjandb3ef3e2019-02-25 10:54:01 -070064 /* Mask out the minor version number */
65 switch (arch & 0xf0) {
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060066 case ETM_ARCH_V4:
67 break;
68 default:
69 return false;
70 }
71 return true;
72}
73
Mathieu Poirier52210c82016-02-02 14:14:01 -070074static int etm4_cpu_id(struct coresight_device *csdev)
75{
76 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
77
78 return drvdata->cpu;
79}
80
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060081static int etm4_trace_id(struct coresight_device *csdev)
82{
83 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060084
Sudeep Hollab1149ad2016-08-25 15:18:53 -060085 return drvdata->trcid;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060086}
87
88static void etm4_enable_hw(void *info)
89{
90 int i;
91 struct etmv4_drvdata *drvdata = info;
Mathieu Poirier54ff8922016-04-05 11:53:44 -060092 struct etmv4_config *config = &drvdata->config;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -060093
94 CS_UNLOCK(drvdata->base);
95
96 etm4_os_unlock(drvdata);
97
98 /* Disable the trace unit before programming trace registers */
99 writel_relaxed(0, drvdata->base + TRCPRGCTLR);
100
101 /* wait for TRCSTATR.IDLE to go up */
102 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
103 dev_err(drvdata->dev,
Suzuki K Poulose67337e82016-08-25 15:19:00 -0600104 "timeout while waiting for Idle Trace Status\n");
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600105
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600106 writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
107 writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600108 /* nothing specific implemented */
109 writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600110 writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
111 writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
112 writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
113 writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
114 writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
115 writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
116 writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600117 writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600118 writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
119 writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
120 writel_relaxed(config->vissctlr,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600121 drvdata->base + TRCVISSCTLR);
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600122 writel_relaxed(config->vipcssctlr,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600123 drvdata->base + TRCVIPCSSCTLR);
124 for (i = 0; i < drvdata->nrseqstate - 1; i++)
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600125 writel_relaxed(config->seq_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600126 drvdata->base + TRCSEQEVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600127 writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
128 writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
129 writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600130 for (i = 0; i < drvdata->nr_cntr; i++) {
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600131 writel_relaxed(config->cntrldvr[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600132 drvdata->base + TRCCNTRLDVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600133 writel_relaxed(config->cntr_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600134 drvdata->base + TRCCNTCTLRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600135 writel_relaxed(config->cntr_val[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600136 drvdata->base + TRCCNTVRn(i));
137 }
Chunyan Zhang497b5952015-10-07 09:26:38 -0600138
139 /* Resource selector pair 0 is always implemented and reserved */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600140 for (i = 0; i < drvdata->nr_resource * 2; i++)
141 writel_relaxed(config->res_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600142 drvdata->base + TRCRSCTLRn(i));
143
144 for (i = 0; i < drvdata->nr_ss_cmp; i++) {
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600145 writel_relaxed(config->ss_ctrl[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600146 drvdata->base + TRCSSCCRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600147 writel_relaxed(config->ss_status[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600148 drvdata->base + TRCSSCSRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600149 writel_relaxed(config->ss_pe_cmp[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600150 drvdata->base + TRCSSPCICRn(i));
151 }
152 for (i = 0; i < drvdata->nr_addr_cmp; i++) {
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600153 writeq_relaxed(config->addr_val[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600154 drvdata->base + TRCACVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600155 writeq_relaxed(config->addr_acc[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600156 drvdata->base + TRCACATRn(i));
157 }
158 for (i = 0; i < drvdata->numcidc; i++)
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600159 writeq_relaxed(config->ctxid_pid[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600160 drvdata->base + TRCCIDCVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600161 writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
162 writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600163
164 for (i = 0; i < drvdata->numvmidc; i++)
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600165 writeq_relaxed(config->vmid_val[i],
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600166 drvdata->base + TRCVMIDCVRn(i));
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600167 writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
168 writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600169
Sudeep Holla46a3d5c2016-08-25 15:19:08 -0600170 /*
171 * Request to keep the trace unit powered and also
172 * emulation of powerdown
173 */
174 writel_relaxed(readl_relaxed(drvdata->base + TRCPDCR) | TRCPDCR_PU,
175 drvdata->base + TRCPDCR);
176
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600177 /* Enable the trace unit */
178 writel_relaxed(1, drvdata->base + TRCPRGCTLR);
179
180 /* wait for TRCSTATR.IDLE to go back down to '0' */
181 if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
182 dev_err(drvdata->dev,
Suzuki K Poulose67337e82016-08-25 15:19:00 -0600183 "timeout while waiting for Idle Trace Status\n");
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600184
185 CS_LOCK(drvdata->base);
186
187 dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
188}
189
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600190static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
Mathieu Poirier68905d72016-08-25 15:19:10 -0600191 struct perf_event *event)
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600192{
Mathieu Poirier2703d742016-08-25 15:19:17 -0600193 int ret = 0;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600194 struct etmv4_config *config = &drvdata->config;
Mathieu Poirier68905d72016-08-25 15:19:10 -0600195 struct perf_event_attr *attr = &event->attr;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600196
Mathieu Poirier2703d742016-08-25 15:19:17 -0600197 if (!attr) {
198 ret = -EINVAL;
199 goto out;
200 }
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600201
202 /* Clear configuration from previous run */
203 memset(config, 0, sizeof(struct etmv4_config));
204
205 if (attr->exclude_kernel)
206 config->mode = ETM_MODE_EXCL_KERN;
207
208 if (attr->exclude_user)
209 config->mode = ETM_MODE_EXCL_USER;
210
211 /* Always start from the default config */
Mathieu Poirier2703d742016-08-25 15:19:17 -0600212 etm4_set_default_config(config);
213
214 /* Configure filters specified on the perf cmd line, if any. */
215 ret = etm4_set_event_filters(drvdata, event);
216 if (ret)
217 goto out;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600218
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600219 /* Go from generic option to ETMv4 specifics */
220 if (attr->config & BIT(ETM_OPT_CYCACC))
221 config->cfg |= ETMv4_MODE_CYCACC;
222 if (attr->config & BIT(ETM_OPT_TS))
223 config->cfg |= ETMv4_MODE_TIMESTAMP;
224
Mathieu Poirier2703d742016-08-25 15:19:17 -0600225out:
226 return ret;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600227}
228
229static int etm4_enable_perf(struct coresight_device *csdev,
Mathieu Poirier68905d72016-08-25 15:19:10 -0600230 struct perf_event *event)
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600231{
Mathieu Poirier2703d742016-08-25 15:19:17 -0600232 int ret = 0;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600233 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
234
Mathieu Poirier2703d742016-08-25 15:19:17 -0600235 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) {
236 ret = -EINVAL;
237 goto out;
238 }
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600239
240 /* Configure the tracer based on the session's specifics */
Mathieu Poirier2703d742016-08-25 15:19:17 -0600241 ret = etm4_parse_event_config(drvdata, event);
242 if (ret)
243 goto out;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600244 /* And enable it */
245 etm4_enable_hw(drvdata);
246
Mathieu Poirier2703d742016-08-25 15:19:17 -0600247out:
248 return ret;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600249}
250
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600251static int etm4_enable_sysfs(struct coresight_device *csdev)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600252{
253 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
254 int ret;
255
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600256 spin_lock(&drvdata->spinlock);
257
258 /*
259 * Executing etm4_enable_hw on the cpu whose ETM is being enabled
260 * ensures that register writes occur when cpu is powered.
261 */
262 ret = smp_call_function_single(drvdata->cpu,
263 etm4_enable_hw, drvdata, 1);
264 if (ret)
265 goto err;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600266
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600267 drvdata->sticky_enable = true;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600268 spin_unlock(&drvdata->spinlock);
269
270 dev_info(drvdata->dev, "ETM tracing enabled\n");
271 return 0;
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600272
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600273err:
274 spin_unlock(&drvdata->spinlock);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600275 return ret;
276}
277
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600278static int etm4_enable(struct coresight_device *csdev,
Mathieu Poirier68905d72016-08-25 15:19:10 -0600279 struct perf_event *event, u32 mode)
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600280{
281 int ret;
282 u32 val;
283 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
284
285 val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
286
287 /* Someone is already using the tracer */
288 if (val)
289 return -EBUSY;
290
291 switch (mode) {
292 case CS_MODE_SYSFS:
293 ret = etm4_enable_sysfs(csdev);
294 break;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600295 case CS_MODE_PERF:
Mathieu Poirier68905d72016-08-25 15:19:10 -0600296 ret = etm4_enable_perf(csdev, event);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600297 break;
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600298 default:
299 ret = -EINVAL;
300 }
301
302 /* The tracer didn't start */
303 if (ret)
304 local_set(&drvdata->mode, CS_MODE_DISABLED);
305
306 return ret;
307}
308
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600309static void etm4_disable_hw(void *info)
310{
311 u32 control;
312 struct etmv4_drvdata *drvdata = info;
313
314 CS_UNLOCK(drvdata->base);
315
Sudeep Holla46a3d5c2016-08-25 15:19:08 -0600316 /* power can be removed from the trace unit now */
317 control = readl_relaxed(drvdata->base + TRCPDCR);
318 control &= ~TRCPDCR_PU;
319 writel_relaxed(control, drvdata->base + TRCPDCR);
320
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600321 control = readl_relaxed(drvdata->base + TRCPRGCTLR);
322
323 /* EN, bit[0] Trace unit enable bit */
324 control &= ~0x1;
325
326 /* make sure everything completes before disabling */
327 mb();
328 isb();
329 writel_relaxed(control, drvdata->base + TRCPRGCTLR);
330
331 CS_LOCK(drvdata->base);
332
333 dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
334}
335
Mathieu Poirier68905d72016-08-25 15:19:10 -0600336static int etm4_disable_perf(struct coresight_device *csdev,
337 struct perf_event *event)
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600338{
Mathieu Poiriere97b1c62016-08-25 15:19:18 -0600339 u32 control;
340 struct etm_filters *filters = event->hw.addr_filters;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600341 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
342
343 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
344 return -EINVAL;
345
346 etm4_disable_hw(drvdata);
Mathieu Poiriere97b1c62016-08-25 15:19:18 -0600347
348 /*
349 * Check if the start/stop logic was active when the unit was stopped.
350 * That way we can re-enable the start/stop logic when the process is
351 * scheduled again. Configuration of the start/stop logic happens in
352 * function etm4_set_event_filters().
353 */
354 control = readl_relaxed(drvdata->base + TRCVICTLR);
355 /* TRCVICTLR::SSSTATUS, bit[9] */
356 filters->ssstatus = (control & BIT(9));
357
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600358 return 0;
359}
360
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600361static void etm4_disable_sysfs(struct coresight_device *csdev)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600362{
363 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
364
365 /*
366 * Taking hotplug lock here protects from clocks getting disabled
367 * with tracing being left on (crash scenario) if user disable occurs
368 * after cpu online mask indicates the cpu is offline but before the
369 * DYING hotplug callback is serviced by the ETM driver.
370 */
371 get_online_cpus();
372 spin_lock(&drvdata->spinlock);
373
374 /*
375 * Executing etm4_disable_hw on the cpu whose ETM is being disabled
376 * ensures that register writes occur when cpu is powered.
377 */
378 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600379
380 spin_unlock(&drvdata->spinlock);
381 put_online_cpus();
382
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600383 dev_info(drvdata->dev, "ETM tracing disabled\n");
384}
385
Mathieu Poirier68905d72016-08-25 15:19:10 -0600386static void etm4_disable(struct coresight_device *csdev,
387 struct perf_event *event)
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600388{
389 u32 mode;
390 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
391
392 /*
393 * For as long as the tracer isn't disabled another entity can't
394 * change its status. As such we can read the status here without
395 * fearing it will change under us.
396 */
397 mode = local_read(&drvdata->mode);
398
399 switch (mode) {
400 case CS_MODE_DISABLED:
401 break;
402 case CS_MODE_SYSFS:
403 etm4_disable_sysfs(csdev);
404 break;
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600405 case CS_MODE_PERF:
Mathieu Poirier68905d72016-08-25 15:19:10 -0600406 etm4_disable_perf(csdev, event);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -0600407 break;
Mathieu Poirierc38a9ec2016-04-05 11:53:47 -0600408 }
409
410 if (mode)
411 local_set(&drvdata->mode, CS_MODE_DISABLED);
412}
413
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600414static const struct coresight_ops_source etm4_source_ops = {
Mathieu Poirier52210c82016-02-02 14:14:01 -0700415 .cpu_id = etm4_cpu_id,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600416 .trace_id = etm4_trace_id,
417 .enable = etm4_enable,
418 .disable = etm4_disable,
419};
420
421static const struct coresight_ops etm4_cs_ops = {
422 .source_ops = &etm4_source_ops,
423};
424
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600425static void etm4_init_arch_data(void *info)
426{
427 u32 etmidr0;
428 u32 etmidr1;
429 u32 etmidr2;
430 u32 etmidr3;
431 u32 etmidr4;
432 u32 etmidr5;
433 struct etmv4_drvdata *drvdata = info;
434
Mathieu Poirier66bbbb72016-04-05 11:53:46 -0600435 /* Make sure all registers are accessible */
436 etm4_os_unlock(drvdata);
437
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600438 CS_UNLOCK(drvdata->base);
439
440 /* find all capabilities of the tracing unit */
441 etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
442
443 /* INSTP0, bits[2:1] P0 tracing support field */
444 if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
445 drvdata->instrp0 = true;
446 else
447 drvdata->instrp0 = false;
448
449 /* TRCBB, bit[5] Branch broadcast tracing support bit */
450 if (BMVAL(etmidr0, 5, 5))
451 drvdata->trcbb = true;
452 else
453 drvdata->trcbb = false;
454
455 /* TRCCOND, bit[6] Conditional instruction tracing support bit */
456 if (BMVAL(etmidr0, 6, 6))
457 drvdata->trccond = true;
458 else
459 drvdata->trccond = false;
460
461 /* TRCCCI, bit[7] Cycle counting instruction bit */
462 if (BMVAL(etmidr0, 7, 7))
463 drvdata->trccci = true;
464 else
465 drvdata->trccci = false;
466
467 /* RETSTACK, bit[9] Return stack bit */
468 if (BMVAL(etmidr0, 9, 9))
469 drvdata->retstack = true;
470 else
471 drvdata->retstack = false;
472
473 /* NUMEVENT, bits[11:10] Number of events field */
474 drvdata->nr_event = BMVAL(etmidr0, 10, 11);
475 /* QSUPP, bits[16:15] Q element support field */
476 drvdata->q_support = BMVAL(etmidr0, 15, 16);
477 /* TSSIZE, bits[28:24] Global timestamp size field */
478 drvdata->ts_size = BMVAL(etmidr0, 24, 28);
479
480 /* base architecture of trace unit */
481 etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
482 /*
483 * TRCARCHMIN, bits[7:4] architecture the minor version number
484 * TRCARCHMAJ, bits[11:8] architecture major versin number
485 */
486 drvdata->arch = BMVAL(etmidr1, 4, 11);
487
488 /* maximum size of resources */
489 etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
490 /* CIDSIZE, bits[9:5] Indicates the Context ID size */
491 drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
492 /* VMIDSIZE, bits[14:10] Indicates the VMID size */
493 drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
494 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
495 drvdata->ccsize = BMVAL(etmidr2, 25, 28);
496
497 etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
498 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
499 drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
500 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
501 drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
502 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
503 drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
504
505 /*
506 * TRCERR, bit[24] whether a trace unit can trace a
507 * system error exception.
508 */
509 if (BMVAL(etmidr3, 24, 24))
510 drvdata->trc_error = true;
511 else
512 drvdata->trc_error = false;
513
514 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
515 if (BMVAL(etmidr3, 25, 25))
516 drvdata->syncpr = true;
517 else
518 drvdata->syncpr = false;
519
520 /* STALLCTL, bit[26] is stall control implemented? */
521 if (BMVAL(etmidr3, 26, 26))
522 drvdata->stallctl = true;
523 else
524 drvdata->stallctl = false;
525
526 /* SYSSTALL, bit[27] implementation can support stall control? */
527 if (BMVAL(etmidr3, 27, 27))
528 drvdata->sysstall = true;
529 else
530 drvdata->sysstall = false;
531
532 /* NUMPROC, bits[30:28] the number of PEs available for tracing */
533 drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
534
535 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
536 if (BMVAL(etmidr3, 31, 31))
537 drvdata->nooverflow = true;
538 else
539 drvdata->nooverflow = false;
540
541 /* number of resources trace unit supports */
542 etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
543 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
544 drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
545 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
546 drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
Chunyan Zhang497b5952015-10-07 09:26:38 -0600547 /*
548 * NUMRSPAIR, bits[19:16]
549 * The number of resource pairs conveyed by the HW starts at 0, i.e a
550 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
551 * As such add 1 to the value of NUMRSPAIR for a better representation.
552 */
553 drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600554 /*
555 * NUMSSCC, bits[23:20] the number of single-shot
556 * comparator control for tracing
557 */
558 drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
559 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
560 drvdata->numcidc = BMVAL(etmidr4, 24, 27);
561 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
562 drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
563
564 etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
565 /* NUMEXTIN, bits[8:0] number of external inputs implemented */
566 drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
567 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
568 drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
569 /* ATBTRIG, bit[22] implementation can support ATB triggers? */
570 if (BMVAL(etmidr5, 22, 22))
571 drvdata->atbtrig = true;
572 else
573 drvdata->atbtrig = false;
574 /*
575 * LPOVERRIDE, bit[23] implementation supports
576 * low-power state override
577 */
578 if (BMVAL(etmidr5, 23, 23))
579 drvdata->lpoverride = true;
580 else
581 drvdata->lpoverride = false;
582 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
583 drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
584 /* NUMCNTR, bits[30:28] number of counters available for tracing */
585 drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
586 CS_LOCK(drvdata->base);
587}
588
Mathieu Poirier2a5695a2016-08-25 15:19:13 -0600589static void etm4_set_default_config(struct etmv4_config *config)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600590{
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600591 /* disable all events tracing */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600592 config->eventctrl0 = 0x0;
593 config->eventctrl1 = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600594
595 /* disable stalling */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600596 config->stall_ctrl = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600597
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600598 /* enable trace synchronization every 4096 bytes, if available */
599 config->syncfreq = 0xC;
600
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600601 /* disable timestamp event */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600602 config->ts_ctrl = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600603
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600604 /* TRCVICTLR::EVENT = 0x01, select the always on logic */
605 config->vinst_ctrl |= BIT(0);
Mathieu Poirier2a5695a2016-08-25 15:19:13 -0600606}
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600607
Mathieu Poirierf0d30cc2016-08-25 15:19:16 -0600608static u64 etm4_get_access_type(struct etmv4_config *config)
Mathieu Poirier2a5695a2016-08-25 15:19:13 -0600609{
Mathieu Poirier6cccf662016-08-25 15:19:15 -0600610 u64 access_type = 0;
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600611
Mathieu Poirierf0d30cc2016-08-25 15:19:16 -0600612 /*
613 * EXLEVEL_NS, bits[15:12]
614 * The Exception levels are:
615 * Bit[12] Exception level 0 - Application
616 * Bit[13] Exception level 1 - OS
617 * Bit[14] Exception level 2 - Hypervisor
618 * Bit[15] Never implemented
619 *
620 * Always stay away from hypervisor mode.
621 */
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600622 access_type = ETM_EXLEVEL_NS_HYP;
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600623
Mathieu Poirierf0d30cc2016-08-25 15:19:16 -0600624 if (config->mode & ETM_MODE_EXCL_KERN)
625 access_type |= ETM_EXLEVEL_NS_OS;
626
627 if (config->mode & ETM_MODE_EXCL_USER)
628 access_type |= ETM_EXLEVEL_NS_APP;
629
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600630 /*
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600631 * EXLEVEL_S, bits[11:8], don't trace anything happening
632 * in secure state.
633 */
634 access_type |= (ETM_EXLEVEL_S_APP |
635 ETM_EXLEVEL_S_OS |
636 ETM_EXLEVEL_S_HYP);
637
Mathieu Poirierf0d30cc2016-08-25 15:19:16 -0600638 return access_type;
639}
640
641static void etm4_set_comparator_filter(struct etmv4_config *config,
642 u64 start, u64 stop, int comparator)
643{
644 u64 access_type = etm4_get_access_type(config);
645
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600646 /* First half of default address comparator */
Mathieu Poirier6cccf662016-08-25 15:19:15 -0600647 config->addr_val[comparator] = start;
648 config->addr_acc[comparator] = access_type;
649 config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600650
651 /* Second half of default address comparator */
Mathieu Poirier6cccf662016-08-25 15:19:15 -0600652 config->addr_val[comparator + 1] = stop;
653 config->addr_acc[comparator + 1] = access_type;
654 config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600655
656 /*
Mathieu Poirier6cccf662016-08-25 15:19:15 -0600657 * Configure the ViewInst function to include this address range
658 * comparator.
659 *
660 * @comparator is divided by two since it is the index in the
661 * etmv4_config::addr_val array but register TRCVIIECTLR deals with
662 * address range comparator _pairs_.
663 *
664 * Therefore:
665 * index 0 -> compatator pair 0
666 * index 2 -> comparator pair 1
667 * index 4 -> comparator pair 2
668 * ...
669 * index 14 -> comparator pair 7
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600670 */
Mathieu Poirier6cccf662016-08-25 15:19:15 -0600671 config->viiectlr |= BIT(comparator / 2);
672}
673
Mathieu Poiriere97b1c62016-08-25 15:19:18 -0600674static void etm4_set_start_stop_filter(struct etmv4_config *config,
675 u64 address, int comparator,
676 enum etm_addr_type type)
677{
678 int shift;
679 u64 access_type = etm4_get_access_type(config);
680
681 /* Configure the comparator */
682 config->addr_val[comparator] = address;
683 config->addr_acc[comparator] = access_type;
684 config->addr_type[comparator] = type;
685
686 /*
687 * Configure ViewInst Start-Stop control register.
688 * Addresses configured to start tracing go from bit 0 to n-1,
689 * while those configured to stop tracing from 16 to 16 + n-1.
690 */
691 shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
692 config->vissctlr |= BIT(shift + comparator);
693}
694
Mathieu Poirier6cccf662016-08-25 15:19:15 -0600695static void etm4_set_default_filter(struct etmv4_config *config)
696{
697 u64 start, stop;
698
699 /*
700 * Configure address range comparator '0' to encompass all
701 * possible addresses.
702 */
703 start = 0x0;
704 stop = ~0x0;
705
706 etm4_set_comparator_filter(config, start, stop,
707 ETM_DEFAULT_ADDR_COMP);
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600708
Mathieu Poirier5edd9442016-08-25 15:19:14 -0600709 /*
710 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
711 * in the started state
712 */
713 config->vinst_ctrl |= BIT(9);
714
715 /* No start-stop filtering for ViewInst */
Mathieu Poirier54ff8922016-04-05 11:53:44 -0600716 config->vissctlr = 0x0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600717}
718
Mathieu Poirier2a5695a2016-08-25 15:19:13 -0600719static void etm4_set_default(struct etmv4_config *config)
720{
721 if (WARN_ON_ONCE(!config))
722 return;
723
724 /*
725 * Make default initialisation trace everything
726 *
727 * Select the "always true" resource selector on the
728 * "Enablign Event" line and configure address range comparator
729 * '0' to trace all the possible address range. From there
730 * configure the "include/exclude" engine to include address
731 * range comparator '0'.
732 */
733 etm4_set_default_config(config);
734 etm4_set_default_filter(config);
735}
736
Mathieu Poirier2703d742016-08-25 15:19:17 -0600737static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
738{
739 int nr_comparator, index = 0;
740 struct etmv4_config *config = &drvdata->config;
741
742 /*
743 * nr_addr_cmp holds the number of comparator _pair_, so time 2
744 * for the total number of comparators.
745 */
746 nr_comparator = drvdata->nr_addr_cmp * 2;
747
748 /* Go through the tally of comparators looking for a free one. */
749 while (index < nr_comparator) {
750 switch (type) {
751 case ETM_ADDR_TYPE_RANGE:
752 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
753 config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
754 return index;
755
756 /* Address range comparators go in pairs */
757 index += 2;
758 break;
Mathieu Poiriere97b1c62016-08-25 15:19:18 -0600759 case ETM_ADDR_TYPE_START:
760 case ETM_ADDR_TYPE_STOP:
761 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
762 return index;
763
764 /* Start/stop address can have odd indexes */
765 index += 1;
766 break;
Mathieu Poirier2703d742016-08-25 15:19:17 -0600767 default:
768 return -EINVAL;
769 }
770 }
771
772 /* If we are here all the comparators have been used. */
773 return -ENOSPC;
774}
775
776static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
777 struct perf_event *event)
778{
779 int i, comparator, ret = 0;
Mathieu Poiriere97b1c62016-08-25 15:19:18 -0600780 u64 address;
Mathieu Poirier2703d742016-08-25 15:19:17 -0600781 struct etmv4_config *config = &drvdata->config;
782 struct etm_filters *filters = event->hw.addr_filters;
783
784 if (!filters)
785 goto default_filter;
786
787 /* Sync events with what Perf got */
788 perf_event_addr_filters_sync(event);
789
790 /*
791 * If there are no filters to deal with simply go ahead with
792 * the default filter, i.e the entire address range.
793 */
794 if (!filters->nr_filters)
795 goto default_filter;
796
797 for (i = 0; i < filters->nr_filters; i++) {
798 struct etm_filter *filter = &filters->etm_filter[i];
799 enum etm_addr_type type = filter->type;
800
801 /* See if a comparator is free. */
802 comparator = etm4_get_next_comparator(drvdata, type);
803 if (comparator < 0) {
804 ret = comparator;
805 goto out;
806 }
807
808 switch (type) {
809 case ETM_ADDR_TYPE_RANGE:
810 etm4_set_comparator_filter(config,
811 filter->start_addr,
812 filter->stop_addr,
813 comparator);
814 /*
815 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
816 * in the started state
817 */
818 config->vinst_ctrl |= BIT(9);
819
820 /* No start-stop filtering for ViewInst */
821 config->vissctlr = 0x0;
822 break;
Mathieu Poiriere97b1c62016-08-25 15:19:18 -0600823 case ETM_ADDR_TYPE_START:
824 case ETM_ADDR_TYPE_STOP:
825 /* Get the right start or stop address */
826 address = (type == ETM_ADDR_TYPE_START ?
827 filter->start_addr :
828 filter->stop_addr);
829
830 /* Configure comparator */
831 etm4_set_start_stop_filter(config, address,
832 comparator, type);
833
834 /*
835 * If filters::ssstatus == 1, trace acquisition was
836 * started but the process was yanked away before the
837 * the stop address was hit. As such the start/stop
838 * logic needs to be re-started so that tracing can
839 * resume where it left.
840 *
841 * The start/stop logic status when a process is
842 * scheduled out is checked in function
843 * etm4_disable_perf().
844 */
845 if (filters->ssstatus)
846 config->vinst_ctrl |= BIT(9);
847
848 /* No include/exclude filtering for ViewInst */
849 config->viiectlr = 0x0;
850 break;
Mathieu Poirier2703d742016-08-25 15:19:17 -0600851 default:
852 ret = -EINVAL;
853 goto out;
854 }
855 }
856
857 goto out;
858
859
860default_filter:
861 etm4_set_default_filter(config);
862
863out:
864 return ret;
865}
866
Mathieu Poirier4f6fce52016-04-05 11:53:48 -0600867void etm4_config_trace_mode(struct etmv4_config *config)
868{
869 u32 addr_acc, mode;
870
871 mode = config->mode;
872 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
873
874 /* excluding kernel AND user space doesn't make sense */
875 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
876
877 /* nothing to do if neither flags are set */
878 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
879 return;
880
881 addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
882 /* clear default config */
883 addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS);
884
885 /*
886 * EXLEVEL_NS, bits[15:12]
887 * The Exception levels are:
888 * Bit[12] Exception level 0 - Application
889 * Bit[13] Exception level 1 - OS
890 * Bit[14] Exception level 2 - Hypervisor
891 * Bit[15] Never implemented
892 */
893 if (mode & ETM_MODE_EXCL_KERN)
894 addr_acc |= ETM_EXLEVEL_NS_OS;
895 else
896 addr_acc |= ETM_EXLEVEL_NS_APP;
897
898 config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
899 config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
900}
901
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000902static int etm4_online_cpu(unsigned int cpu)
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600903{
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600904 if (!etmdrvdata[cpu])
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000905 return 0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600906
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000907 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
908 coresight_enable(etmdrvdata[cpu]->csdev);
909 return 0;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600910}
911
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000912static int etm4_starting_cpu(unsigned int cpu)
913{
914 if (!etmdrvdata[cpu])
915 return 0;
916
917 spin_lock(&etmdrvdata[cpu]->spinlock);
918 if (!etmdrvdata[cpu]->os_unlock) {
919 etm4_os_unlock(etmdrvdata[cpu]);
920 etmdrvdata[cpu]->os_unlock = true;
921 }
922
923 if (local_read(&etmdrvdata[cpu]->mode))
924 etm4_enable_hw(etmdrvdata[cpu]);
925 spin_unlock(&etmdrvdata[cpu]->spinlock);
926 return 0;
927}
928
929static int etm4_dying_cpu(unsigned int cpu)
930{
931 if (!etmdrvdata[cpu])
932 return 0;
933
934 spin_lock(&etmdrvdata[cpu]->spinlock);
935 if (local_read(&etmdrvdata[cpu]->mode))
936 etm4_disable_hw(etmdrvdata[cpu]);
937 spin_unlock(&etmdrvdata[cpu]->spinlock);
938 return 0;
939}
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600940
Mathieu Poirierfc208ab2016-04-05 11:53:45 -0600941static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
942{
943 drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
944}
945
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600946static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
947{
948 int ret;
949 void __iomem *base;
950 struct device *dev = &adev->dev;
951 struct coresight_platform_data *pdata = NULL;
952 struct etmv4_drvdata *drvdata;
953 struct resource *res = &adev->res;
Suzuki K Poulose94862952016-08-25 15:19:05 -0600954 struct coresight_desc desc = { 0 };
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600955 struct device_node *np = adev->dev.of_node;
956
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600957 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
958 if (!drvdata)
959 return -ENOMEM;
960
961 if (np) {
962 pdata = of_get_coresight_platform_data(dev, np);
963 if (IS_ERR(pdata))
964 return PTR_ERR(pdata);
965 adev->dev.platform_data = pdata;
966 }
967
968 drvdata->dev = &adev->dev;
969 dev_set_drvdata(dev, drvdata);
970
971 /* Validity for the resource is already checked by the AMBA core */
972 base = devm_ioremap_resource(dev, res);
973 if (IS_ERR(base))
974 return PTR_ERR(base);
975
976 drvdata->base = base;
977
978 spin_lock_init(&drvdata->spinlock);
979
980 drvdata->cpu = pdata ? pdata->cpu : 0;
981
982 get_online_cpus();
983 etmdrvdata[drvdata->cpu] = drvdata;
984
Pratik Patel2e1cdfe2015-05-13 10:34:09 -0600985 if (smp_call_function_single(drvdata->cpu,
986 etm4_init_arch_data, drvdata, 1))
987 dev_err(dev, "ETM arch init failed\n");
988
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +0000989 if (!etm4_count++) {
990 cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING,
991 "AP_ARM_CORESIGHT4_STARTING",
992 etm4_starting_cpu, etm4_dying_cpu);
993 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
994 "AP_ARM_CORESIGHT4_ONLINE",
995 etm4_online_cpu, NULL);
996 if (ret < 0)
997 goto err_arch_supported;
998 hp_online = ret;
999 }
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001000
1001 put_online_cpus();
1002
1003 if (etm4_arch_supported(drvdata->arch) == false) {
1004 ret = -EINVAL;
1005 goto err_arch_supported;
1006 }
Mathieu Poirierfc208ab2016-04-05 11:53:45 -06001007
1008 etm4_init_trace_id(drvdata);
1009 etm4_set_default(&drvdata->config);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001010
Suzuki K Poulose94862952016-08-25 15:19:05 -06001011 desc.type = CORESIGHT_DEV_TYPE_SOURCE;
1012 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
1013 desc.ops = &etm4_cs_ops;
1014 desc.pdata = pdata;
1015 desc.dev = dev;
1016 desc.groups = coresight_etmv4_groups;
1017 drvdata->csdev = coresight_register(&desc);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001018 if (IS_ERR(drvdata->csdev)) {
1019 ret = PTR_ERR(drvdata->csdev);
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -06001020 goto err_arch_supported;
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001021 }
1022
Mathieu Poirier37fbbdb2016-04-05 11:53:49 -06001023 ret = etm_perf_symlink(drvdata->csdev, true);
1024 if (ret) {
1025 coresight_unregister(drvdata->csdev);
1026 goto err_arch_supported;
1027 }
1028
1029 pm_runtime_put(&adev->dev);
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001030 dev_info(dev, "%s initialized\n", (char *)id->data);
1031
1032 if (boot_enable) {
1033 coresight_enable(drvdata->csdev);
1034 drvdata->boot_enable = true;
1035 }
1036
1037 return 0;
1038
1039err_arch_supported:
Sebastian Andrzej Siewior58eb4572016-07-13 17:16:55 +00001040 if (--etm4_count == 0) {
1041 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING);
1042 if (hp_online)
1043 cpuhp_remove_state_nocalls(hp_online);
1044 }
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001045 return ret;
1046}
1047
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001048static struct amba_id etm4_ids[] = {
Suzuki K Poulose78247e22016-08-25 15:18:58 -06001049 { /* ETM 4.0 - Cortex-A53 */
1050 .id = 0x000bb95d,
1051 .mask = 0x000fffff,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001052 .data = "ETM 4.0",
1053 },
Suzuki K Poulose78247e22016-08-25 15:18:58 -06001054 { /* ETM 4.0 - Cortex-A57 */
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001055 .id = 0x000bb95e,
1056 .mask = 0x000fffff,
1057 .data = "ETM 4.0",
1058 },
Li Pengcheng960e3092016-05-03 11:33:42 -06001059 { /* ETM 4.0 - A72, Maia, HiSilicon */
1060 .id = 0x000bb95a,
1061 .mask = 0x000fffff,
1062 .data = "ETM 4.0",
1063 },
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001064 { 0, 0},
1065};
1066
1067static struct amba_driver etm4x_driver = {
1068 .drv = {
1069 .name = "coresight-etm4x",
Mathieu Poirierb15f0fb2016-02-02 14:14:00 -07001070 .suppress_bind_attrs = true,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001071 },
1072 .probe = etm4_probe,
Pratik Patel2e1cdfe2015-05-13 10:34:09 -06001073 .id_table = etm4_ids,
1074};
Paul Gortmaker941943c2016-02-17 17:52:03 -07001075builtin_amba_driver(etm4x_driver);