Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2014, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/smp.h> |
| 24 | #include <linux/sysfs.h> |
| 25 | #include <linux/stat.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/cpu.h> |
| 28 | #include <linux/coresight.h> |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 29 | #include <linux/coresight-pmu.h> |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 30 | #include <linux/pm_wakeup.h> |
| 31 | #include <linux/amba/bus.h> |
| 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/uaccess.h> |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 34 | #include <linux/perf_event.h> |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 35 | #include <linux/pm_runtime.h> |
| 36 | #include <asm/sections.h> |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 37 | #include <asm/local.h> |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 38 | |
| 39 | #include "coresight-etm4x.h" |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 40 | #include "coresight-etm-perf.h" |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 41 | |
| 42 | static int boot_enable; |
| 43 | module_param_named(boot_enable, boot_enable, int, S_IRUGO); |
| 44 | |
| 45 | /* The number of ETMv4 currently registered */ |
| 46 | static int etm4_count; |
| 47 | static struct etmv4_drvdata *etmdrvdata[NR_CPUS]; |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 48 | static void etm4_set_default(struct etmv4_config *config); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 49 | |
Sebastian Andrzej Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 50 | static enum cpuhp_state hp_online; |
| 51 | |
Mathieu Poirier | 66bbbb7 | 2016-04-05 11:53:46 -0600 | [diff] [blame] | 52 | static void etm4_os_unlock(struct etmv4_drvdata *drvdata) |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 53 | { |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 54 | /* Writing any value to ETMOSLAR unlocks the trace registers */ |
| 55 | writel_relaxed(0x0, drvdata->base + TRCOSLAR); |
Mathieu Poirier | 66bbbb7 | 2016-04-05 11:53:46 -0600 | [diff] [blame] | 56 | drvdata->os_unlock = true; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 57 | isb(); |
| 58 | } |
| 59 | |
| 60 | static 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 Poirier | 52210c8 | 2016-02-02 14:14:01 -0700 | [diff] [blame] | 71 | static 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 78 | static int etm4_trace_id(struct coresight_device *csdev) |
| 79 | { |
| 80 | struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 81 | |
Sudeep Holla | b1149ad | 2016-08-25 15:18:53 -0600 | [diff] [blame] | 82 | return drvdata->trcid; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void etm4_enable_hw(void *info) |
| 86 | { |
| 87 | int i; |
| 88 | struct etmv4_drvdata *drvdata = info; |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 89 | struct etmv4_config *config = &drvdata->config; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 90 | |
| 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 Poulose | 67337e8 | 2016-08-25 15:19:00 -0600 | [diff] [blame] | 101 | "timeout while waiting for Idle Trace Status\n"); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 102 | |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 103 | writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR); |
| 104 | writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 105 | /* nothing specific implemented */ |
| 106 | writel_relaxed(0x0, drvdata->base + TRCAUXCTLR); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 107 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 114 | writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 115 | writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR); |
| 116 | writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR); |
| 117 | writel_relaxed(config->vissctlr, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 118 | drvdata->base + TRCVISSCTLR); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 119 | writel_relaxed(config->vipcssctlr, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 120 | drvdata->base + TRCVIPCSSCTLR); |
| 121 | for (i = 0; i < drvdata->nrseqstate - 1; i++) |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 122 | writel_relaxed(config->seq_ctrl[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 123 | drvdata->base + TRCSEQEVRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 124 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 127 | for (i = 0; i < drvdata->nr_cntr; i++) { |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 128 | writel_relaxed(config->cntrldvr[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 129 | drvdata->base + TRCCNTRLDVRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 130 | writel_relaxed(config->cntr_ctrl[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 131 | drvdata->base + TRCCNTCTLRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 132 | writel_relaxed(config->cntr_val[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 133 | drvdata->base + TRCCNTVRn(i)); |
| 134 | } |
Chunyan Zhang | 497b595 | 2015-10-07 09:26:38 -0600 | [diff] [blame] | 135 | |
| 136 | /* Resource selector pair 0 is always implemented and reserved */ |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 137 | for (i = 0; i < drvdata->nr_resource * 2; i++) |
| 138 | writel_relaxed(config->res_ctrl[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 139 | drvdata->base + TRCRSCTLRn(i)); |
| 140 | |
| 141 | for (i = 0; i < drvdata->nr_ss_cmp; i++) { |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 142 | writel_relaxed(config->ss_ctrl[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 143 | drvdata->base + TRCSSCCRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 144 | writel_relaxed(config->ss_status[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 145 | drvdata->base + TRCSSCSRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 146 | writel_relaxed(config->ss_pe_cmp[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 147 | drvdata->base + TRCSSPCICRn(i)); |
| 148 | } |
| 149 | for (i = 0; i < drvdata->nr_addr_cmp; i++) { |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 150 | writeq_relaxed(config->addr_val[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 151 | drvdata->base + TRCACVRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 152 | writeq_relaxed(config->addr_acc[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 153 | drvdata->base + TRCACATRn(i)); |
| 154 | } |
| 155 | for (i = 0; i < drvdata->numcidc; i++) |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 156 | writeq_relaxed(config->ctxid_pid[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 157 | drvdata->base + TRCCIDCVRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 158 | writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0); |
| 159 | writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 160 | |
| 161 | for (i = 0; i < drvdata->numvmidc; i++) |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 162 | writeq_relaxed(config->vmid_val[i], |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 163 | drvdata->base + TRCVMIDCVRn(i)); |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 164 | writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0); |
| 165 | writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 166 | |
Sudeep Holla | 46a3d5c | 2016-08-25 15:19:08 -0600 | [diff] [blame] | 167 | /* |
| 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 174 | /* 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 Poulose | 67337e8 | 2016-08-25 15:19:00 -0600 | [diff] [blame] | 180 | "timeout while waiting for Idle Trace Status\n"); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 181 | |
| 182 | CS_LOCK(drvdata->base); |
| 183 | |
| 184 | dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu); |
| 185 | } |
| 186 | |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 187 | static int etm4_parse_event_config(struct etmv4_drvdata *drvdata, |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 188 | struct perf_event *event) |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 189 | { |
| 190 | struct etmv4_config *config = &drvdata->config; |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 191 | struct perf_event_attr *attr = &event->attr; |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 192 | |
| 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 | |
| 224 | static int etm4_enable_perf(struct coresight_device *csdev, |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 225 | struct perf_event *event) |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 226 | { |
| 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 Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 233 | etm4_parse_event_config(drvdata, event); |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 234 | /* And enable it */ |
| 235 | etm4_enable_hw(drvdata); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 240 | static int etm4_enable_sysfs(struct coresight_device *csdev) |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 241 | { |
| 242 | struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); |
| 243 | int ret; |
| 244 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 245 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 255 | |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 256 | drvdata->sticky_enable = true; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 257 | spin_unlock(&drvdata->spinlock); |
| 258 | |
| 259 | dev_info(drvdata->dev, "ETM tracing enabled\n"); |
| 260 | return 0; |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 261 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 262 | err: |
| 263 | spin_unlock(&drvdata->spinlock); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 267 | static int etm4_enable(struct coresight_device *csdev, |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 268 | struct perf_event *event, u32 mode) |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 269 | { |
| 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 Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 284 | case CS_MODE_PERF: |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 285 | ret = etm4_enable_perf(csdev, event); |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 286 | break; |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 287 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 298 | static void etm4_disable_hw(void *info) |
| 299 | { |
| 300 | u32 control; |
| 301 | struct etmv4_drvdata *drvdata = info; |
| 302 | |
| 303 | CS_UNLOCK(drvdata->base); |
| 304 | |
Sudeep Holla | 46a3d5c | 2016-08-25 15:19:08 -0600 | [diff] [blame] | 305 | /* 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 310 | 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 Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 325 | static int etm4_disable_perf(struct coresight_device *csdev, |
| 326 | struct perf_event *event) |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 327 | { |
| 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 Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 337 | static void etm4_disable_sysfs(struct coresight_device *csdev) |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 338 | { |
| 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 355 | |
| 356 | spin_unlock(&drvdata->spinlock); |
| 357 | put_online_cpus(); |
| 358 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 359 | dev_info(drvdata->dev, "ETM tracing disabled\n"); |
| 360 | } |
| 361 | |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 362 | static void etm4_disable(struct coresight_device *csdev, |
| 363 | struct perf_event *event) |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 364 | { |
| 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 Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 381 | case CS_MODE_PERF: |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 382 | etm4_disable_perf(csdev, event); |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 383 | break; |
Mathieu Poirier | c38a9ec | 2016-04-05 11:53:47 -0600 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (mode) |
| 387 | local_set(&drvdata->mode, CS_MODE_DISABLED); |
| 388 | } |
| 389 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 390 | static const struct coresight_ops_source etm4_source_ops = { |
Mathieu Poirier | 52210c8 | 2016-02-02 14:14:01 -0700 | [diff] [blame] | 391 | .cpu_id = etm4_cpu_id, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 392 | .trace_id = etm4_trace_id, |
| 393 | .enable = etm4_enable, |
| 394 | .disable = etm4_disable, |
| 395 | }; |
| 396 | |
| 397 | static const struct coresight_ops etm4_cs_ops = { |
| 398 | .source_ops = &etm4_source_ops, |
| 399 | }; |
| 400 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 401 | static 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 Poirier | 66bbbb7 | 2016-04-05 11:53:46 -0600 | [diff] [blame] | 411 | /* Make sure all registers are accessible */ |
| 412 | etm4_os_unlock(drvdata); |
| 413 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 414 | 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 Zhang | 497b595 | 2015-10-07 09:26:38 -0600 | [diff] [blame] | 523 | /* |
| 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 530 | /* |
| 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 Poirier | 2a5695a5 | 2016-08-25 15:19:13 -0600 | [diff] [blame] | 565 | static void etm4_set_default_config(struct etmv4_config *config) |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 566 | { |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 567 | /* disable all events tracing */ |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 568 | config->eventctrl0 = 0x0; |
| 569 | config->eventctrl1 = 0x0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 570 | |
| 571 | /* disable stalling */ |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 572 | config->stall_ctrl = 0x0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 573 | |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 574 | /* enable trace synchronization every 4096 bytes, if available */ |
| 575 | config->syncfreq = 0xC; |
| 576 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 577 | /* disable timestamp event */ |
Mathieu Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 578 | config->ts_ctrl = 0x0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 579 | |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 580 | /* TRCVICTLR::EVENT = 0x01, select the always on logic */ |
| 581 | config->vinst_ctrl |= BIT(0); |
Mathieu Poirier | 2a5695a5 | 2016-08-25 15:19:13 -0600 | [diff] [blame] | 582 | } |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 583 | |
Mathieu Poirier | 2a5695a5 | 2016-08-25 15:19:13 -0600 | [diff] [blame] | 584 | static void etm4_set_default_filter(struct etmv4_config *config) |
| 585 | { |
Mathieu Poirier | 5edd944 | 2016-08-25 15:19:14 -0600 | [diff] [blame^] | 586 | u64 start, stop, access_type = 0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 587 | |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 588 | /* |
| 589 | * Configure address range comparator '0' to encompass all |
| 590 | * possible addresses. |
| 591 | */ |
Mathieu Poirier | 5edd944 | 2016-08-25 15:19:14 -0600 | [diff] [blame^] | 592 | start = 0x0; |
| 593 | stop = ~0x0; |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 594 | |
Mathieu Poirier | 5edd944 | 2016-08-25 15:19:14 -0600 | [diff] [blame^] | 595 | /* EXLEVEL_NS, bits[12:15], always stay away from hypervisor mode. */ |
| 596 | access_type = ETM_EXLEVEL_NS_HYP; |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 597 | |
| 598 | /* |
Mathieu Poirier | 5edd944 | 2016-08-25 15:19:14 -0600 | [diff] [blame^] | 599 | * 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 Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 614 | 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 Poirier | 5edd944 | 2016-08-25 15:19:14 -0600 | [diff] [blame^] | 622 | /* |
| 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 Poirier | 54ff892 | 2016-04-05 11:53:44 -0600 | [diff] [blame] | 629 | config->vissctlr = 0x0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 630 | } |
| 631 | |
Mathieu Poirier | 2a5695a5 | 2016-08-25 15:19:13 -0600 | [diff] [blame] | 632 | static 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 Poirier | 4f6fce5 | 2016-04-05 11:53:48 -0600 | [diff] [blame] | 650 | void 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 Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 685 | static int etm4_online_cpu(unsigned int cpu) |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 686 | { |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 687 | if (!etmdrvdata[cpu]) |
Sebastian Andrzej Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 688 | return 0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 689 | |
Sebastian Andrzej Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 690 | if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable) |
| 691 | coresight_enable(etmdrvdata[cpu]->csdev); |
| 692 | return 0; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 693 | } |
| 694 | |
Sebastian Andrzej Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 695 | static 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 | |
| 712 | static 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 723 | |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 724 | static void etm4_init_trace_id(struct etmv4_drvdata *drvdata) |
| 725 | { |
| 726 | drvdata->trcid = coresight_get_trace_id(drvdata->cpu); |
| 727 | } |
| 728 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 729 | static 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 Poulose | 9486295 | 2016-08-25 15:19:05 -0600 | [diff] [blame] | 737 | struct coresight_desc desc = { 0 }; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 738 | struct device_node *np = adev->dev.of_node; |
| 739 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 740 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 768 | 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 Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 772 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 783 | |
| 784 | put_online_cpus(); |
| 785 | |
| 786 | if (etm4_arch_supported(drvdata->arch) == false) { |
| 787 | ret = -EINVAL; |
| 788 | goto err_arch_supported; |
| 789 | } |
Mathieu Poirier | fc208ab | 2016-04-05 11:53:45 -0600 | [diff] [blame] | 790 | |
| 791 | etm4_init_trace_id(drvdata); |
| 792 | etm4_set_default(&drvdata->config); |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 793 | |
Suzuki K Poulose | 9486295 | 2016-08-25 15:19:05 -0600 | [diff] [blame] | 794 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 801 | if (IS_ERR(drvdata->csdev)) { |
| 802 | ret = PTR_ERR(drvdata->csdev); |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 803 | goto err_arch_supported; |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 804 | } |
| 805 | |
Mathieu Poirier | 37fbbdb | 2016-04-05 11:53:49 -0600 | [diff] [blame] | 806 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 813 | 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 | |
| 822 | err_arch_supported: |
Sebastian Andrzej Siewior | 58eb457 | 2016-07-13 17:16:55 +0000 | [diff] [blame] | 823 | 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 Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 831 | static struct amba_id etm4_ids[] = { |
Suzuki K Poulose | 78247e2 | 2016-08-25 15:18:58 -0600 | [diff] [blame] | 832 | { /* ETM 4.0 - Cortex-A53 */ |
| 833 | .id = 0x000bb95d, |
| 834 | .mask = 0x000fffff, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 835 | .data = "ETM 4.0", |
| 836 | }, |
Suzuki K Poulose | 78247e2 | 2016-08-25 15:18:58 -0600 | [diff] [blame] | 837 | { /* ETM 4.0 - Cortex-A57 */ |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 838 | .id = 0x000bb95e, |
| 839 | .mask = 0x000fffff, |
| 840 | .data = "ETM 4.0", |
| 841 | }, |
Li Pengcheng | 960e309 | 2016-05-03 11:33:42 -0600 | [diff] [blame] | 842 | { /* ETM 4.0 - A72, Maia, HiSilicon */ |
| 843 | .id = 0x000bb95a, |
| 844 | .mask = 0x000fffff, |
| 845 | .data = "ETM 4.0", |
| 846 | }, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 847 | { 0, 0}, |
| 848 | }; |
| 849 | |
| 850 | static struct amba_driver etm4x_driver = { |
| 851 | .drv = { |
| 852 | .name = "coresight-etm4x", |
Mathieu Poirier | b15f0fb | 2016-02-02 14:14:00 -0700 | [diff] [blame] | 853 | .suppress_bind_attrs = true, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 854 | }, |
| 855 | .probe = etm4_probe, |
Pratik Patel | 2e1cdfe | 2015-05-13 10:34:09 -0600 | [diff] [blame] | 856 | .id_table = etm4_ids, |
| 857 | }; |
Paul Gortmaker | 941943c | 2016-02-17 17:52:03 -0700 | [diff] [blame] | 858 | builtin_amba_driver(etm4x_driver); |