Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 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/module.h> |
Pratik Patel | cf41862 | 2011-09-22 11:15:11 -0700 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
| 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <linux/smp.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 27 | #include <linux/wakelock.h> |
| 28 | #include <linux/pm_qos_params.h> |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 29 | #include <asm/atomic.h> |
| 30 | |
| 31 | #include "qdss.h" |
| 32 | |
| 33 | #define ptm_writel(ptm, cpu, val, off) \ |
| 34 | __raw_writel((val), ptm.base + (SZ_4K * cpu) + off) |
| 35 | #define ptm_readl(ptm, cpu, off) \ |
| 36 | __raw_readl(ptm.base + (SZ_4K * cpu) + off) |
| 37 | |
| 38 | /* |
| 39 | * Device registers: |
| 40 | * 0x000 - 0x2FC: Trace registers |
| 41 | * 0x300 - 0x314: Management registers |
| 42 | * 0x318 - 0xEFC: Trace registers |
| 43 | * |
| 44 | * Coresight registers |
| 45 | * 0xF00 - 0xF9C: Management registers |
| 46 | * 0xFA0 - 0xFA4: Management registers in PFTv1.0 |
| 47 | * Trace registers in PFTv1.1 |
| 48 | * 0xFA8 - 0xFFC: Management registers |
| 49 | */ |
| 50 | |
| 51 | /* Trace registers (0x000-0x2FC) */ |
| 52 | #define ETMCR (0x000) |
| 53 | #define ETMCCR (0x004) |
| 54 | #define ETMTRIGGER (0x008) |
| 55 | #define ETMSR (0x010) |
| 56 | #define ETMSCR (0x014) |
| 57 | #define ETMTSSCR (0x018) |
| 58 | #define ETMTEEVR (0x020) |
| 59 | #define ETMTECR1 (0x024) |
| 60 | #define ETMFFLR (0x02C) |
| 61 | #define ETMACVRn(n) (0x040 + (n * 4)) |
| 62 | #define ETMACTRn(n) (0x080 + (n * 4)) |
| 63 | #define ETMCNTRLDVRn(n) (0x140 + (n * 4)) |
| 64 | #define ETMCNTENRn(n) (0x150 + (n * 4)) |
| 65 | #define ETMCNTRLDEVRn(n) (0x160 + (n * 4)) |
| 66 | #define ETMCNTVRn(n) (0x170 + (n * 4)) |
| 67 | #define ETMSQ12EVR (0x180) |
| 68 | #define ETMSQ21EVR (0x184) |
| 69 | #define ETMSQ23EVR (0x188) |
Pratik Patel | d5bbc76 | 2012-01-29 14:13:21 -0800 | [diff] [blame^] | 70 | #define ETMSQ31EVR (0x18C) |
| 71 | #define ETMSQ32EVR (0x190) |
| 72 | #define ETMSQ13EVR (0x194) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 73 | #define ETMSQR (0x19C) |
| 74 | #define ETMEXTOUTEVRn(n) (0x1A0 + (n * 4)) |
| 75 | #define ETMCIDCVRn(n) (0x1B0 + (n * 4)) |
| 76 | #define ETMCIDCMR (0x1BC) |
| 77 | #define ETMIMPSPEC0 (0x1C0) |
| 78 | #define ETMIMPSPEC1 (0x1C4) |
| 79 | #define ETMIMPSPEC2 (0x1C8) |
| 80 | #define ETMIMPSPEC3 (0x1CC) |
| 81 | #define ETMIMPSPEC4 (0x1D0) |
| 82 | #define ETMIMPSPEC5 (0x1D4) |
| 83 | #define ETMIMPSPEC6 (0x1D8) |
| 84 | #define ETMIMPSPEC7 (0x1DC) |
| 85 | #define ETMSYNCFR (0x1E0) |
| 86 | #define ETMIDR (0x1E4) |
| 87 | #define ETMCCER (0x1E8) |
| 88 | #define ETMEXTINSELR (0x1EC) |
| 89 | #define ETMTESSEICR (0x1F0) |
| 90 | #define ETMEIBCR (0x1F4) |
| 91 | #define ETMTSEVR (0x1F8) |
| 92 | #define ETMAUXCR (0x1FC) |
| 93 | #define ETMTRACEIDR (0x200) |
Pratik Patel | d5bbc76 | 2012-01-29 14:13:21 -0800 | [diff] [blame^] | 94 | #define ETMVMIDCVR (0x240) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 95 | /* Management registers (0x300-0x314) */ |
| 96 | #define ETMOSLAR (0x300) |
| 97 | #define ETMOSLSR (0x304) |
| 98 | #define ETMOSSRR (0x308) |
| 99 | #define ETMPDCR (0x310) |
| 100 | #define ETMPDSR (0x314) |
| 101 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 102 | #define PTM_LOCK(cpu) \ |
| 103 | do { \ |
| 104 | mb(); \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 105 | ptm_writel(ptm, cpu, 0x0, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 106 | } while (0) |
| 107 | #define PTM_UNLOCK(cpu) \ |
| 108 | do { \ |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 109 | ptm_writel(ptm, cpu, CS_UNLOCK_MAGIC, CS_LAR); \ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 110 | mb(); \ |
| 111 | } while (0) |
| 112 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 113 | |
| 114 | /* Forward declarations */ |
| 115 | static void ptm_cfg_rw_init(void); |
| 116 | |
Pratik Patel | 29cba15 | 2012-01-03 11:40:26 -0800 | [diff] [blame] | 117 | #ifdef CONFIG_MSM_QDSS_ETM_DEFAULT_ENABLE |
| 118 | static int trace_on_boot = 1; |
| 119 | #else |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 120 | static int trace_on_boot; |
Pratik Patel | 29cba15 | 2012-01-03 11:40:26 -0800 | [diff] [blame] | 121 | #endif |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 122 | module_param_named( |
| 123 | trace_on_boot, trace_on_boot, int, S_IRUGO |
| 124 | ); |
| 125 | |
| 126 | struct ptm_config { |
| 127 | /* read only config registers */ |
| 128 | uint32_t config_code; |
| 129 | /* derived values */ |
| 130 | uint8_t nr_addr_comp; |
| 131 | uint8_t nr_cntr; |
| 132 | uint8_t nr_ext_input; |
| 133 | uint8_t nr_ext_output; |
| 134 | uint8_t nr_context_id_comp; |
| 135 | |
| 136 | uint32_t config_code_extn; |
| 137 | /* derived values */ |
| 138 | uint8_t nr_extnd_ext_input_sel; |
| 139 | uint8_t nr_instr_resources; |
| 140 | |
| 141 | uint32_t system_config; |
| 142 | /* derived values */ |
| 143 | uint8_t fifofull_supported; |
| 144 | uint8_t nr_procs_supported; |
| 145 | |
| 146 | /* read-write registers */ |
| 147 | uint32_t main_control; |
| 148 | uint32_t trigger_event; |
| 149 | uint32_t te_start_stop_control; |
| 150 | uint32_t te_event; |
| 151 | uint32_t te_control; |
| 152 | uint32_t fifofull_level; |
| 153 | uint32_t addr_comp_value[16]; |
| 154 | uint32_t addr_comp_access_type[16]; |
| 155 | uint32_t cntr_reload_value[4]; |
| 156 | uint32_t cntr_enable_event[4]; |
| 157 | uint32_t cntr_reload_event[4]; |
| 158 | uint32_t cntr_value[4]; |
| 159 | uint32_t seq_state_12_event; |
| 160 | uint32_t seq_state_21_event; |
| 161 | uint32_t seq_state_23_event; |
| 162 | uint32_t seq_state_32_event; |
| 163 | uint32_t seq_state_13_event; |
| 164 | uint32_t seq_state_31_event; |
| 165 | uint32_t current_seq_state; |
| 166 | uint32_t ext_output_event[4]; |
| 167 | uint32_t context_id_comp_value[3]; |
| 168 | uint32_t context_id_comp_mask; |
| 169 | uint32_t sync_freq; |
| 170 | uint32_t extnd_ext_input_sel; |
| 171 | uint32_t ts_event; |
| 172 | uint32_t aux_control; |
| 173 | uint32_t coresight_trace_id; |
| 174 | uint32_t vmid_comp_value; |
| 175 | }; |
| 176 | |
| 177 | struct ptm_ctx { |
| 178 | struct ptm_config cfg; |
| 179 | void __iomem *base; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 180 | bool trace_enabled; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 181 | struct wake_lock wake_lock; |
| 182 | struct pm_qos_request_list qos_req; |
| 183 | atomic_t in_use; |
| 184 | struct device *dev; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | static struct ptm_ctx ptm; |
| 188 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 189 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 190 | /* ETM clock is derived from the processor clock and gets enabled on a |
| 191 | * logical OR of below items on Krait (pass2 onwards): |
| 192 | * 1.CPMR[ETMCLKEN] is 1 |
| 193 | * 2.ETMCR[PD] is 0 |
| 194 | * 3.ETMPDCR[PU] is 1 |
| 195 | * 4.Reset is asserted (core or debug) |
| 196 | * 5.APB memory mapped requests (eg. EDAP access) |
| 197 | * |
| 198 | * 1., 2. and 3. above are permanent enables whereas 4. and 5. are temporary |
| 199 | * enables |
| 200 | * |
| 201 | * We rely on 5. to be able to access ETMCR and then use 2. above for ETM |
| 202 | * clock vote in the driver and the save-restore code uses 1. above |
| 203 | * for its vote |
| 204 | */ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 205 | static void ptm_set_powerdown(int cpu) |
| 206 | { |
| 207 | uint32_t etmcr; |
| 208 | |
| 209 | etmcr = ptm_readl(ptm, cpu, ETMCR); |
| 210 | etmcr |= BIT(0); |
| 211 | ptm_writel(ptm, cpu, etmcr, ETMCR); |
| 212 | } |
| 213 | |
| 214 | static void ptm_clear_powerdown(int cpu) |
| 215 | { |
| 216 | uint32_t etmcr; |
| 217 | |
| 218 | etmcr = ptm_readl(ptm, cpu, ETMCR); |
| 219 | etmcr &= ~BIT(0); |
| 220 | ptm_writel(ptm, cpu, etmcr, ETMCR); |
| 221 | } |
| 222 | |
| 223 | static void ptm_set_prog(int cpu) |
| 224 | { |
| 225 | uint32_t etmcr; |
| 226 | int count; |
| 227 | |
| 228 | etmcr = ptm_readl(ptm, cpu, ETMCR); |
| 229 | etmcr |= BIT(10); |
| 230 | ptm_writel(ptm, cpu, etmcr, ETMCR); |
| 231 | |
| 232 | for (count = TIMEOUT_US; BVAL(ptm_readl(ptm, cpu, ETMSR), 1) != 1 |
| 233 | && count > 0; count--) |
| 234 | udelay(1); |
| 235 | WARN(count == 0, "timeout while setting prog bit\n"); |
| 236 | } |
| 237 | |
| 238 | static void ptm_clear_prog(int cpu) |
| 239 | { |
| 240 | uint32_t etmcr; |
| 241 | int count; |
| 242 | |
| 243 | etmcr = ptm_readl(ptm, cpu, ETMCR); |
| 244 | etmcr &= ~BIT(10); |
| 245 | ptm_writel(ptm, cpu, etmcr, ETMCR); |
| 246 | |
| 247 | for (count = TIMEOUT_US; BVAL(ptm_readl(ptm, cpu, ETMSR), 1) != 0 |
| 248 | && count > 0; count--) |
| 249 | udelay(1); |
| 250 | WARN(count == 0, "timeout while clearing prog bit\n"); |
| 251 | } |
| 252 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 253 | static void __ptm_trace_enable(int cpu) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 254 | { |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 255 | int i; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 256 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 257 | PTM_UNLOCK(cpu); |
| 258 | /* Vote for ETM power/clock enable */ |
| 259 | ptm_clear_powerdown(cpu); |
| 260 | ptm_set_prog(cpu); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 261 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 262 | ptm_writel(ptm, cpu, ptm.cfg.main_control | BIT(10), ETMCR); |
| 263 | ptm_writel(ptm, cpu, ptm.cfg.trigger_event, ETMTRIGGER); |
| 264 | ptm_writel(ptm, cpu, ptm.cfg.te_start_stop_control, ETMTSSCR); |
| 265 | ptm_writel(ptm, cpu, ptm.cfg.te_event, ETMTEEVR); |
| 266 | ptm_writel(ptm, cpu, ptm.cfg.te_control, ETMTECR1); |
| 267 | ptm_writel(ptm, cpu, ptm.cfg.fifofull_level, ETMFFLR); |
| 268 | for (i = 0; i < ptm.cfg.nr_addr_comp; i++) { |
| 269 | ptm_writel(ptm, cpu, ptm.cfg.addr_comp_value[i], ETMACVRn(i)); |
| 270 | ptm_writel(ptm, cpu, ptm.cfg.addr_comp_access_type[i], |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 271 | ETMACTRn(i)); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 272 | } |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 273 | for (i = 0; i < ptm.cfg.nr_cntr; i++) { |
| 274 | ptm_writel(ptm, cpu, ptm.cfg.cntr_reload_value[i], |
| 275 | ETMCNTRLDVRn(i)); |
| 276 | ptm_writel(ptm, cpu, ptm.cfg.cntr_enable_event[i], |
| 277 | ETMCNTENRn(i)); |
| 278 | ptm_writel(ptm, cpu, ptm.cfg.cntr_reload_event[i], |
| 279 | ETMCNTRLDEVRn(i)); |
| 280 | ptm_writel(ptm, cpu, ptm.cfg.cntr_value[i], ETMCNTVRn(i)); |
| 281 | } |
| 282 | ptm_writel(ptm, cpu, ptm.cfg.seq_state_12_event, ETMSQ12EVR); |
| 283 | ptm_writel(ptm, cpu, ptm.cfg.seq_state_21_event, ETMSQ21EVR); |
| 284 | ptm_writel(ptm, cpu, ptm.cfg.seq_state_23_event, ETMSQ23EVR); |
| 285 | ptm_writel(ptm, cpu, ptm.cfg.seq_state_32_event, ETMSQ32EVR); |
| 286 | ptm_writel(ptm, cpu, ptm.cfg.seq_state_13_event, ETMSQ13EVR); |
| 287 | ptm_writel(ptm, cpu, ptm.cfg.seq_state_31_event, ETMSQ31EVR); |
| 288 | ptm_writel(ptm, cpu, ptm.cfg.current_seq_state, ETMSQR); |
| 289 | for (i = 0; i < ptm.cfg.nr_ext_output; i++) |
| 290 | ptm_writel(ptm, cpu, ptm.cfg.ext_output_event[i], |
| 291 | ETMEXTOUTEVRn(i)); |
| 292 | for (i = 0; i < ptm.cfg.nr_context_id_comp; i++) |
| 293 | ptm_writel(ptm, cpu, ptm.cfg.context_id_comp_value[i], |
| 294 | ETMCIDCVRn(i)); |
| 295 | ptm_writel(ptm, cpu, ptm.cfg.context_id_comp_mask, ETMCIDCMR); |
| 296 | ptm_writel(ptm, cpu, ptm.cfg.sync_freq, ETMSYNCFR); |
| 297 | ptm_writel(ptm, cpu, ptm.cfg.extnd_ext_input_sel, ETMEXTINSELR); |
| 298 | ptm_writel(ptm, cpu, ptm.cfg.ts_event, ETMTSEVR); |
| 299 | ptm_writel(ptm, cpu, ptm.cfg.aux_control, ETMAUXCR); |
| 300 | ptm_writel(ptm, cpu, cpu+1, ETMTRACEIDR); |
| 301 | ptm_writel(ptm, cpu, ptm.cfg.vmid_comp_value, ETMVMIDCVR); |
| 302 | |
| 303 | ptm_clear_prog(cpu); |
| 304 | PTM_LOCK(cpu); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int ptm_trace_enable(void) |
| 308 | { |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 309 | int ret, cpu; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 310 | |
Pratik Patel | cc320a4 | 2011-12-22 10:48:14 -0800 | [diff] [blame] | 311 | ret = qdss_clk_enable(); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 312 | if (ret) |
| 313 | return ret; |
| 314 | |
| 315 | wake_lock(&ptm.wake_lock); |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 316 | /* 1. causes all online cpus to come out of idle PC |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 317 | * 2. prevents idle PC until save restore flag is enabled atomically |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 318 | * |
| 319 | * we rely on the user to prevent hotplug on/off racing with this |
| 320 | * operation and to ensure cores where trace is expected to be turned |
| 321 | * on are already hotplugged on |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 322 | */ |
| 323 | pm_qos_update_request(&ptm.qos_req, 0); |
| 324 | |
| 325 | etb_disable(); |
| 326 | tpiu_disable(); |
| 327 | /* enable ETB first to avoid loosing any trace data */ |
| 328 | etb_enable(); |
| 329 | funnel_enable(0x0, 0x3); |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 330 | for_each_online_cpu(cpu) |
| 331 | __ptm_trace_enable(cpu); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 332 | |
| 333 | ptm.trace_enabled = true; |
| 334 | |
| 335 | pm_qos_update_request(&ptm.qos_req, PM_QOS_DEFAULT_VALUE); |
| 336 | wake_unlock(&ptm.wake_lock); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 341 | static void __ptm_trace_disable(int cpu) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 342 | { |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 343 | PTM_UNLOCK(cpu); |
| 344 | ptm_set_prog(cpu); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 345 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 346 | /* program trace enable to low by using always false event */ |
| 347 | ptm_writel(ptm, cpu, 0x6F | BIT(14), ETMTEEVR); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 348 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 349 | /* Vote for ETM power/clock disable */ |
| 350 | ptm_set_powerdown(cpu); |
| 351 | PTM_LOCK(cpu); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void ptm_trace_disable(void) |
| 355 | { |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 356 | int cpu; |
| 357 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 358 | wake_lock(&ptm.wake_lock); |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 359 | /* 1. causes all online cpus to come out of idle PC |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 360 | * 2. prevents idle PC until save restore flag is disabled atomically |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 361 | * |
| 362 | * we rely on the user to prevent hotplug on/off racing with this |
| 363 | * operation and to ensure cores where trace is expected to be turned |
| 364 | * off are already hotplugged on |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 365 | */ |
| 366 | pm_qos_update_request(&ptm.qos_req, 0); |
| 367 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 368 | for_each_online_cpu(cpu) |
| 369 | __ptm_trace_disable(cpu); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 370 | etb_dump(); |
| 371 | etb_disable(); |
| 372 | funnel_disable(0x0, 0x3); |
| 373 | |
| 374 | ptm.trace_enabled = false; |
| 375 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 376 | pm_qos_update_request(&ptm.qos_req, PM_QOS_DEFAULT_VALUE); |
| 377 | wake_unlock(&ptm.wake_lock); |
| 378 | |
Pratik Patel | cc320a4 | 2011-12-22 10:48:14 -0800 | [diff] [blame] | 379 | qdss_clk_disable(); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static int ptm_open(struct inode *inode, struct file *file) |
| 383 | { |
| 384 | if (atomic_cmpxchg(&ptm.in_use, 0, 1)) |
| 385 | return -EBUSY; |
| 386 | |
| 387 | dev_dbg(ptm.dev, "%s: successfully opened\n", __func__); |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static void ptm_range_filter(char range, uint32_t reg1, |
| 392 | uint32_t addr1, uint32_t reg2, uint32_t addr2) |
| 393 | { |
| 394 | ptm.cfg.addr_comp_value[reg1] = addr1; |
| 395 | ptm.cfg.addr_comp_value[reg2] = addr2; |
| 396 | |
| 397 | ptm.cfg.te_control |= (1 << (reg1/2)); |
| 398 | if (range == 'i') |
| 399 | ptm.cfg.te_control &= ~BIT(24); |
| 400 | else if (range == 'e') |
| 401 | ptm.cfg.te_control |= BIT(24); |
| 402 | } |
| 403 | |
| 404 | static void ptm_start_stop_filter(char start_stop, |
| 405 | uint32_t reg, uint32_t addr) |
| 406 | { |
| 407 | ptm.cfg.addr_comp_value[reg] = addr; |
| 408 | |
| 409 | if (start_stop == 's') |
| 410 | ptm.cfg.te_start_stop_control |= (1 << reg); |
| 411 | else if (start_stop == 't') |
| 412 | ptm.cfg.te_start_stop_control |= (1 << (reg + 16)); |
| 413 | |
| 414 | ptm.cfg.te_control |= BIT(25); |
| 415 | } |
| 416 | |
| 417 | #define MAX_COMMAND_STRLEN 40 |
| 418 | static ssize_t ptm_write(struct file *file, const char __user *data, |
| 419 | size_t len, loff_t *ppos) |
| 420 | { |
| 421 | char command[MAX_COMMAND_STRLEN]; |
| 422 | int str_len; |
| 423 | unsigned long reg1, reg2; |
| 424 | unsigned long addr1, addr2; |
| 425 | |
| 426 | str_len = strnlen_user(data, MAX_COMMAND_STRLEN); |
| 427 | dev_dbg(ptm.dev, "string length: %d", str_len); |
| 428 | if (str_len == 0 || str_len == (MAX_COMMAND_STRLEN+1)) { |
| 429 | dev_err(ptm.dev, "error in str_len: %d", str_len); |
| 430 | return -EFAULT; |
| 431 | } |
| 432 | /* includes the null character */ |
| 433 | if (copy_from_user(command, data, str_len)) { |
| 434 | dev_err(ptm.dev, "error in copy_from_user: %d", str_len); |
| 435 | return -EFAULT; |
| 436 | } |
| 437 | |
| 438 | dev_dbg(ptm.dev, "input = %s", command); |
| 439 | |
| 440 | switch (command[0]) { |
| 441 | case '0': |
| 442 | if (ptm.trace_enabled) { |
| 443 | ptm_trace_disable(); |
| 444 | dev_info(ptm.dev, "tracing disabled\n"); |
| 445 | } else |
| 446 | dev_err(ptm.dev, "trace already disabled\n"); |
| 447 | |
| 448 | break; |
| 449 | case '1': |
| 450 | if (!ptm.trace_enabled) { |
| 451 | if (!ptm_trace_enable()) |
| 452 | dev_info(ptm.dev, "tracing enabled\n"); |
| 453 | else |
| 454 | dev_err(ptm.dev, "error enabling trace\n"); |
| 455 | } else |
| 456 | dev_err(ptm.dev, "trace already enabled\n"); |
| 457 | break; |
| 458 | case 'f': |
| 459 | switch (command[2]) { |
| 460 | case 'i': |
| 461 | switch (command[4]) { |
| 462 | case 'i': |
| 463 | if (sscanf(&command[6], "%lx:%lx:%lx:%lx\\0", |
| 464 | ®1, &addr1, ®2, &addr2) != 4) |
| 465 | goto err_out; |
| 466 | if (reg1 > 7 || reg2 > 7 || (reg1 % 2)) |
| 467 | goto err_out; |
| 468 | ptm_range_filter('i', |
| 469 | reg1, addr1, reg2, addr2); |
| 470 | break; |
| 471 | case 'e': |
| 472 | if (sscanf(&command[6], "%lx:%lx:%lx:%lx\\0", |
| 473 | ®1, &addr1, ®2, &addr2) != 4) |
| 474 | goto err_out; |
| 475 | if (reg1 > 7 || reg2 > 7 || (reg1 % 2) |
| 476 | || command[2] == 'd') |
| 477 | goto err_out; |
| 478 | ptm_range_filter('e', |
| 479 | reg1, addr1, reg2, addr2); |
| 480 | break; |
| 481 | case 's': |
| 482 | if (sscanf(&command[6], "%lx:%lx\\0", |
| 483 | ®1, &addr1) != 2) |
| 484 | goto err_out; |
| 485 | if (reg1 > 7) |
| 486 | goto err_out; |
| 487 | ptm_start_stop_filter('s', reg1, addr1); |
| 488 | break; |
| 489 | case 't': |
| 490 | if (sscanf(&command[6], "%lx:%lx\\0", |
| 491 | ®1, &addr1) != 2) |
| 492 | goto err_out; |
| 493 | if (reg1 > 7) |
| 494 | goto err_out; |
| 495 | ptm_start_stop_filter('t', reg1, addr1); |
| 496 | break; |
| 497 | default: |
| 498 | goto err_out; |
| 499 | } |
| 500 | break; |
| 501 | case 'r': |
| 502 | ptm_cfg_rw_init(); |
| 503 | break; |
| 504 | default: |
| 505 | goto err_out; |
| 506 | } |
| 507 | break; |
| 508 | default: |
| 509 | goto err_out; |
| 510 | } |
| 511 | |
| 512 | return len; |
| 513 | |
| 514 | err_out: |
| 515 | return -EFAULT; |
| 516 | } |
| 517 | |
| 518 | static int ptm_release(struct inode *inode, struct file *file) |
| 519 | { |
| 520 | atomic_set(&ptm.in_use, 0); |
| 521 | dev_dbg(ptm.dev, "%s: released\n", __func__); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static const struct file_operations ptm_fops = { |
| 526 | .owner = THIS_MODULE, |
| 527 | .open = ptm_open, |
| 528 | .write = ptm_write, |
| 529 | .release = ptm_release, |
| 530 | }; |
| 531 | |
| 532 | static struct miscdevice ptm_misc = { |
| 533 | .name = "msm_ptm", |
| 534 | .minor = MISC_DYNAMIC_MINOR, |
| 535 | .fops = &ptm_fops, |
| 536 | }; |
| 537 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 538 | static void ptm_cfg_rw_init(void) |
| 539 | { |
| 540 | int i; |
| 541 | |
| 542 | ptm.cfg.main_control = 0x00001000; |
| 543 | ptm.cfg.trigger_event = 0x0000406F; |
| 544 | ptm.cfg.te_start_stop_control = 0x00000000; |
| 545 | ptm.cfg.te_event = 0x0000006F; |
| 546 | ptm.cfg.te_control = 0x01000000; |
| 547 | ptm.cfg.fifofull_level = 0x00000028; |
| 548 | for (i = 0; i < ptm.cfg.nr_addr_comp; i++) { |
| 549 | ptm.cfg.addr_comp_value[i] = 0x00000000; |
| 550 | ptm.cfg.addr_comp_access_type[i] = 0x00000000; |
| 551 | } |
| 552 | for (i = 0; i < ptm.cfg.nr_cntr; i++) { |
| 553 | ptm.cfg.cntr_reload_value[i] = 0x00000000; |
| 554 | ptm.cfg.cntr_enable_event[i] = 0x0000406F; |
| 555 | ptm.cfg.cntr_reload_event[i] = 0x0000406F; |
| 556 | ptm.cfg.cntr_value[i] = 0x00000000; |
| 557 | } |
| 558 | ptm.cfg.seq_state_12_event = 0x0000406F; |
| 559 | ptm.cfg.seq_state_21_event = 0x0000406F; |
| 560 | ptm.cfg.seq_state_23_event = 0x0000406F; |
| 561 | ptm.cfg.seq_state_32_event = 0x0000406F; |
| 562 | ptm.cfg.seq_state_13_event = 0x0000406F; |
| 563 | ptm.cfg.seq_state_31_event = 0x0000406F; |
| 564 | ptm.cfg.current_seq_state = 0x00000000; |
| 565 | for (i = 0; i < ptm.cfg.nr_ext_output; i++) |
| 566 | ptm.cfg.ext_output_event[i] = 0x0000406F; |
| 567 | for (i = 0; i < ptm.cfg.nr_context_id_comp; i++) |
| 568 | ptm.cfg.context_id_comp_value[i] = 0x00000000; |
| 569 | ptm.cfg.context_id_comp_mask = 0x00000000; |
| 570 | ptm.cfg.sync_freq = 0x00000080; |
| 571 | ptm.cfg.extnd_ext_input_sel = 0x00000000; |
| 572 | ptm.cfg.ts_event = 0x0000406F; |
| 573 | ptm.cfg.aux_control = 0x00000000; |
| 574 | ptm.cfg.vmid_comp_value = 0x00000000; |
| 575 | } |
| 576 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 577 | /* Memory mapped writes to clear os lock not supported */ |
| 578 | static void ptm_os_unlock(void *unused) |
| 579 | { |
| 580 | unsigned long value = 0x0; |
| 581 | |
| 582 | asm("mcr p14, 1, %0, c1, c0, 4\n\t" : : "r" (value)); |
| 583 | asm("isb\n\t"); |
| 584 | } |
| 585 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 586 | static void ptm_cfg_ro_init(void) |
| 587 | { |
| 588 | /* use cpu 0 for setup */ |
| 589 | int cpu = 0; |
| 590 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 591 | /* Unlock OS lock first to allow memory mapped reads and writes */ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 592 | ptm_os_unlock(NULL); |
| 593 | smp_call_function(ptm_os_unlock, NULL, 1); |
| 594 | PTM_UNLOCK(cpu); |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 595 | /* Vote for ETM power/clock enable */ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 596 | ptm_clear_powerdown(cpu); |
| 597 | ptm_set_prog(cpu); |
| 598 | |
| 599 | /* find all capabilities */ |
| 600 | ptm.cfg.config_code = ptm_readl(ptm, cpu, ETMCCR); |
| 601 | ptm.cfg.nr_addr_comp = BMVAL(ptm.cfg.config_code, 0, 3) * 2; |
| 602 | ptm.cfg.nr_cntr = BMVAL(ptm.cfg.config_code, 13, 15); |
| 603 | ptm.cfg.nr_ext_input = BMVAL(ptm.cfg.config_code, 17, 19); |
| 604 | ptm.cfg.nr_ext_output = BMVAL(ptm.cfg.config_code, 20, 22); |
| 605 | ptm.cfg.nr_context_id_comp = BMVAL(ptm.cfg.config_code, 24, 25); |
| 606 | |
| 607 | ptm.cfg.config_code_extn = ptm_readl(ptm, cpu, ETMCCER); |
| 608 | ptm.cfg.nr_extnd_ext_input_sel = |
| 609 | BMVAL(ptm.cfg.config_code_extn, 0, 2); |
| 610 | ptm.cfg.nr_instr_resources = BMVAL(ptm.cfg.config_code_extn, 13, 15); |
| 611 | |
| 612 | ptm.cfg.system_config = ptm_readl(ptm, cpu, ETMSCR); |
| 613 | ptm.cfg.fifofull_supported = BVAL(ptm.cfg.system_config, 8); |
| 614 | ptm.cfg.nr_procs_supported = BMVAL(ptm.cfg.system_config, 12, 14); |
| 615 | |
Pratik Patel | 17f3b82 | 2011-11-21 12:41:47 -0800 | [diff] [blame] | 616 | /* Vote for ETM power/clock disable */ |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 617 | ptm_set_powerdown(cpu); |
| 618 | PTM_LOCK(cpu); |
| 619 | } |
| 620 | |
| 621 | static int __devinit ptm_probe(struct platform_device *pdev) |
| 622 | { |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 623 | int ret; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 624 | struct resource *res; |
| 625 | |
| 626 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 627 | if (!res) { |
| 628 | ret = -EINVAL; |
| 629 | goto err_res; |
| 630 | } |
| 631 | |
| 632 | ptm.base = ioremap_nocache(res->start, resource_size(res)); |
| 633 | if (!ptm.base) { |
| 634 | ret = -EINVAL; |
| 635 | goto err_ioremap; |
| 636 | } |
| 637 | |
| 638 | ptm.dev = &pdev->dev; |
| 639 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 640 | ret = misc_register(&ptm_misc); |
| 641 | if (ret) |
| 642 | goto err_misc; |
| 643 | |
Pratik Patel | cc320a4 | 2011-12-22 10:48:14 -0800 | [diff] [blame] | 644 | ret = qdss_clk_enable(); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 645 | if (ret) |
Pratik Patel | cc320a4 | 2011-12-22 10:48:14 -0800 | [diff] [blame] | 646 | goto err_clk; |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 647 | |
| 648 | ptm_cfg_ro_init(); |
| 649 | ptm_cfg_rw_init(); |
| 650 | |
| 651 | ptm.trace_enabled = false; |
| 652 | |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 653 | wake_lock_init(&ptm.wake_lock, WAKE_LOCK_SUSPEND, "msm_ptm"); |
| 654 | pm_qos_add_request(&ptm.qos_req, PM_QOS_CPU_DMA_LATENCY, |
| 655 | PM_QOS_DEFAULT_VALUE); |
| 656 | atomic_set(&ptm.in_use, 0); |
| 657 | |
Pratik Patel | cc320a4 | 2011-12-22 10:48:14 -0800 | [diff] [blame] | 658 | qdss_clk_disable(); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 659 | |
| 660 | dev_info(ptm.dev, "PTM intialized.\n"); |
| 661 | |
| 662 | if (trace_on_boot) { |
| 663 | if (!ptm_trace_enable()) |
| 664 | dev_info(ptm.dev, "tracing enabled\n"); |
| 665 | else |
| 666 | dev_err(ptm.dev, "error enabling trace\n"); |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | |
Pratik Patel | cc320a4 | 2011-12-22 10:48:14 -0800 | [diff] [blame] | 671 | err_clk: |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 672 | misc_deregister(&ptm_misc); |
| 673 | err_misc: |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 674 | iounmap(ptm.base); |
| 675 | err_ioremap: |
| 676 | err_res: |
| 677 | return ret; |
| 678 | } |
| 679 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 680 | static int ptm_remove(struct platform_device *pdev) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 681 | { |
| 682 | if (ptm.trace_enabled) |
| 683 | ptm_trace_disable(); |
| 684 | pm_qos_remove_request(&ptm.qos_req); |
| 685 | wake_lock_destroy(&ptm.wake_lock); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 686 | misc_deregister(&ptm_misc); |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 687 | iounmap(ptm.base); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static struct platform_driver ptm_driver = { |
| 693 | .probe = ptm_probe, |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 694 | .remove = ptm_remove, |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 695 | .driver = { |
| 696 | .name = "msm_ptm", |
| 697 | }, |
| 698 | }; |
| 699 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 700 | int __init ptm_init(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 701 | { |
| 702 | return platform_driver_register(&ptm_driver); |
| 703 | } |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 704 | |
Pratik Patel | 59e2994 | 2011-12-27 10:31:33 -0800 | [diff] [blame] | 705 | void ptm_exit(void) |
Pratik Patel | 7831c08 | 2011-06-08 21:44:37 -0700 | [diff] [blame] | 706 | { |
| 707 | platform_driver_unregister(&ptm_driver); |
| 708 | } |