blob: b970786567adf0f9dc87f96c564f105931e56269 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. 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/module.h>
14#include <linux/kernel.h>
15#include <linux/fs.h>
16#include <linux/slab.h>
17#include <linux/miscdevice.h>
18#include <linux/cpu.h>
19#include <linux/smp.h>
20#include <linux/percpu.h>
21#include <linux/io.h>
22#include <linux/uaccess.h>
23#include <linux/wakelock.h>
24#include <linux/pm_qos_params.h>
25
26#include <asm/atomic.h>
27
28#include "cp14.h"
29
30#define LOG_BUF_LEN 32768
31#define ETM_NUM_REGS 128
32#define ETB_NUM_REGS 9
33/* each slot is 4 bytes, 8kb total */
34#define ETB_RAM_SLOTS 2048
35
36#define DATALOG_SYNC 0xB5C7
37#define ETM_DUMP_MSG_ID 0x000A6960
38#define ETB_DUMP_MSG_ID 0x000A6961
39
40/* ETM Registers */
41#define ETM_REG_CONTROL 0x00
42#define ETM_REG_STATUS 0x04
43#define ETB_REG_CONTROL 0x71
44#define ETB_REG_STATUS 0x72
45#define ETB_REG_COUNT 0x73
46#define ETB_REG_ADDRESS 0x74
47#define ETB_REG_DATA 0x75
48
49/* Bitmasks for the ETM control register */
50#define ETM_CONTROL_POWERDOWN 0x00000001
51#define ETM_CONTROL_PROGRAM 0x00000400
52
53/* Bitmasks for the ETM status register */
54#define ETM_STATUS_PROGRAMMING 0x00000002
55
56/* ETB Status Register bit definitions */
57#define OV 0x00200000
58
59/* ETB Control Register bit definitions */
60#define AIR 0x00000008
61#define AIW 0x00000004
62#define CPTM 0x00000002
63#define CPTEN 0x00000001
64
65/* Bitmasks for the swconfig field of ETM_CONFIG
66 * ETM trigger propagated to ETM instances on all cores
67 */
68#define TRIGGER_ALL 0x00000002
69
70#define PROG_TIMEOUT_MS 500
71
72static int trace_enabled;
73static int *cpu_restore;
74static int cpu_to_dump;
75static int next_cpu_to_dump;
76static struct wake_lock etm_wake_lock;
77static struct pm_qos_request_list etm_qos_req;
78static int trace_on_boot;
79module_param_named(
80 trace_on_boot, trace_on_boot, int, S_IRUGO
81);
82
83struct b {
84 uint8_t etm_log_buf[LOG_BUF_LEN];
85 uint32_t log_end;
86};
87
88static struct b buf[NR_CPUS];
89static struct b __percpu * *alloc_b;
90static atomic_t etm_dev_in_use;
91
92/* These default settings will be used to configure the ETM/ETB
93 * when the driver loads. */
94struct etm_config_struct {
95 uint32_t etm_00_control;
96 uint32_t etm_02_trigger_event;
97 uint32_t etm_06_te_start_stop;
98 uint32_t etm_07_te_single_addr_comp;
99 uint32_t etm_08_te_event;
100 uint32_t etm_09_te_control;
101 uint32_t etm_0a_fifofull_region;
102 uint32_t etm_0b_fifofull_level;
103 uint32_t etm_0c_vd_event;
104 uint32_t etm_0d_vd_single_addr_comp;
105 uint32_t etm_0e_vd_mmd;
106 uint32_t etm_0f_vd_control;
107 uint32_t etm_addr_comp_value[8]; /* 10 to 17 */
108 uint32_t etm_addr_access_type[8]; /* 20 to 27 */
109 uint32_t etm_data_comp_value[2]; /* 30 and 32 */
110 uint32_t etm_data_comp_mask[2]; /* 40 and 42 */
111 uint32_t etm_counter_reload_value[2]; /* 50 to 51 */
112 uint32_t etm_counter_enable[2]; /* 54 to 55 */
113 uint32_t etm_counter_reload_event[2]; /* 58 to 59 */
114 uint32_t etm_60_seq_event_1_to_2;
115 uint32_t etm_61_seq_event_2_to_1;
116 uint32_t etm_62_seq_event_2_to_3;
117 uint32_t etm_63_seq_event_3_to_1;
118 uint32_t etm_64_seq_event_3_to_2;
119 uint32_t etm_65_seq_event_1_to_3;
120 uint32_t etm_6c_cid_comp_value_1;
121 uint32_t etm_6f_cid_comp_mask;
122 uint32_t etm_78_sync_freq;
123 uint32_t swconfig;
124 uint32_t etb_trig_cnt;
125 uint32_t etb_init_ptr;
126};
127
128static struct etm_config_struct etm_config = {
129 /* etm_00_control 0x0000D84E: 32-bit CID, cycle-accurate,
130 * monitorCPRT */
131 .etm_00_control = 0x0000D84E,
132 /* etm_02_trigger_event 0x00000000: address comparator 0 matches */
133 .etm_02_trigger_event = 0x00000000,
134 .etm_06_te_start_stop = 0x00000000,
135 .etm_07_te_single_addr_comp = 0x00000000,
136 /* etm_08_te_event 0x0000006F: always true */
137 .etm_08_te_event = 0x0000006F,
138 /* etm_09_te_control 0x01000000: exclude none */
139 .etm_09_te_control = 0x01000000,
140 .etm_0a_fifofull_region = 0x00000000,
141 .etm_0b_fifofull_level = 0x00000000,
142 /* etm_0c_vd_event 0x0000006F: always true */
143 .etm_0c_vd_event = 0x0000006F,
144 .etm_0d_vd_single_addr_comp = 0x00000000,
145 .etm_0e_vd_mmd = 0x00000000,
146 /* etm_0f_vd_control 0x00010000: exclude none */
147 .etm_0f_vd_control = 0x00010000,
148 .etm_addr_comp_value[0] = 0x00000000,
149 .etm_addr_comp_value[1] = 0x00000000,
150 .etm_addr_comp_value[2] = 0x00000000,
151 .etm_addr_comp_value[3] = 0x00000000,
152 .etm_addr_comp_value[4] = 0x00000000,
153 .etm_addr_comp_value[5] = 0x00000000,
154 .etm_addr_comp_value[6] = 0x00000000,
155 .etm_addr_comp_value[7] = 0x00000000,
156 .etm_addr_access_type[0] = 0x00000000,
157 .etm_addr_access_type[1] = 0x00000000,
158 .etm_addr_access_type[2] = 0x00000000,
159 .etm_addr_access_type[3] = 0x00000000,
160 .etm_addr_access_type[4] = 0x00000000,
161 .etm_addr_access_type[5] = 0x00000000,
162 .etm_addr_access_type[6] = 0x00000000,
163 .etm_addr_access_type[7] = 0x00000000,
164 .etm_data_comp_value[0] = 0x00000000,
165 .etm_data_comp_value[1] = 0x00000000,
166 .etm_data_comp_mask[0] = 0x00000000,
167 .etm_data_comp_mask[1] = 0x00000000,
168 .etm_counter_reload_value[0] = 0x00000000,
169 .etm_counter_reload_value[1] = 0x00000000,
170 .etm_counter_enable[0] = 0x0002406F,
171 .etm_counter_enable[1] = 0x0002406F,
172 .etm_counter_reload_event[0] = 0x0000406F,
173 .etm_counter_reload_event[1] = 0x0000406F,
174 .etm_60_seq_event_1_to_2 = 0x0000406F,
175 .etm_61_seq_event_2_to_1 = 0x0000406F,
176 .etm_62_seq_event_2_to_3 = 0x0000406F,
177 .etm_63_seq_event_3_to_1 = 0x0000406F,
178 .etm_64_seq_event_3_to_2 = 0x0000406F,
179 .etm_65_seq_event_1_to_3 = 0x0000406F,
180 .etm_6c_cid_comp_value_1 = 0x00000000,
181 .etm_6f_cid_comp_mask = 0x00000000,
182 .etm_78_sync_freq = 0x00000400,
183 .swconfig = 0x00000002,
184 /* etb_trig_cnt 0x00000000: ignore trigger */
185 .etb_trig_cnt = 0x00000000,
186 /* etb_init_ptr 0x00000010: 16 marker bytes */
187 .etb_init_ptr = 0x00000010,
188};
189
190static void emit_log_char(uint8_t c)
191{
192 int this_cpu = get_cpu();
193 struct b *mybuf = *per_cpu_ptr(alloc_b, this_cpu);
194 char *log_buf = mybuf->etm_log_buf;
195 int index = (mybuf->log_end)++ & (LOG_BUF_LEN - 1);
196 log_buf[index] = c;
197 put_cpu();
198}
199
200static void emit_log_word(uint32_t word)
201{
202 emit_log_char(word >> 24);
203 emit_log_char(word >> 16);
204 emit_log_char(word >> 8);
205 emit_log_char(word >> 0);
206}
207
208static void __cpu_enable_etb(void)
209{
210 uint32_t etb_control;
211 uint32_t i;
212
213 /* enable auto-increment on reads and writes */
214 etb_control = AIR | AIW;
215 etm_write_reg(ETB_REG_CONTROL, etb_control);
216
217 /* write tags to the slots before the write pointer so we can
218 * detect overflow */
219 etm_write_reg(ETB_REG_ADDRESS, 0x00000000);
220 for (i = 0; i < (etm_config.etb_init_ptr >> 2); i++)
221 etm_write_reg(ETB_REG_DATA, 0xDEADBEEF);
222
223 etm_write_reg(ETB_REG_STATUS, 0x00000000);
224
225 /* initialize write pointer */
226 etm_write_reg(ETB_REG_ADDRESS, etm_config.etb_init_ptr);
227
228 /* multiple of 16 */
229 etm_write_reg(ETB_REG_COUNT, etm_config.etb_trig_cnt & 0xFFFFFFF0);
230
231 /* Enable ETB and enable the trigger counter as appropriate. A
232 * trigger count of 0 will be used to signify that the user wants to
233 * ignore the trigger (just keep writing to the ETB and overwriting
234 * the oldest data). For "trace before trigger" captures the user
235 * should set the trigger count to a small number. */
236
237 etb_control |= CPTEN;
238 if (etm_config.etb_trig_cnt)
239 etb_control |= CPTM;
240 etm_write_reg(ETB_REG_CONTROL, etb_control);
241}
242
243static void __cpu_disable_etb(void)
244{
245 uint32_t etb_control;
246 etb_control = etm_read_reg(ETB_REG_CONTROL);
247 etb_control &= ~CPTEN;
248 etm_write_reg(ETB_REG_CONTROL, etb_control);
249}
250
251static void __cpu_enable_etm(void)
252{
253 uint32_t etm_control;
254 unsigned long timeout = jiffies + msecs_to_jiffies(PROG_TIMEOUT_MS);
255
256 etm_control = etm_read_reg(ETM_REG_CONTROL);
257 etm_control &= ~ETM_CONTROL_PROGRAM;
258 etm_write_reg(ETM_REG_CONTROL, etm_control);
259
260 while ((etm_read_reg(ETM_REG_STATUS) & ETM_STATUS_PROGRAMMING) == 1) {
261 cpu_relax();
262 if (time_after(jiffies, timeout)) {
263 pr_err("etm: timeout while clearing prog bit\n");
264 break;
265 }
266 }
267}
268
269static void __cpu_disable_etm(void)
270{
271 uint32_t etm_control;
272 unsigned long timeout = jiffies + msecs_to_jiffies(PROG_TIMEOUT_MS);
273
274 etm_control = etm_read_reg(ETM_REG_CONTROL);
275 etm_control |= ETM_CONTROL_PROGRAM;
276 etm_write_reg(ETM_REG_CONTROL, etm_control);
277
278 while ((etm_read_reg(ETM_REG_STATUS) & ETM_STATUS_PROGRAMMING) == 0) {
279 cpu_relax();
280 if (time_after(jiffies, timeout)) {
281 pr_err("etm: timeout while setting prog bit\n");
282 break;
283 }
284 }
285}
286
287static void __cpu_enable_trace(void *unused)
288{
289 uint32_t etm_control;
290 uint32_t etm_trigger;
291 uint32_t etm_external_output;
292
293 get_cpu();
294
295 etm_read_reg(0xC5); /* clear sticky bit in PDSR */
296
297 __cpu_disable_etb();
298 __cpu_disable_etm();
299
300 etm_control = (etm_config.etm_00_control & ~ETM_CONTROL_POWERDOWN)
301 | ETM_CONTROL_PROGRAM;
302 etm_write_reg(0x00, etm_control);
303
304 etm_trigger = etm_config.etm_02_trigger_event;
305 etm_external_output = 0x406F; /* always FALSE */
306
307 if (etm_config.swconfig & TRIGGER_ALL) {
308 uint32_t function = 0x5; /* A OR B */
309 uint32_t resource_b = 0x60; /* external input 1 */
310
311 etm_trigger &= 0x7F; /* keep resource A, clear function and
312 * resource B */
313 etm_trigger |= (function << 14);
314 etm_trigger |= (resource_b << 7);
315 etm_external_output = etm_trigger;
316 }
317
318 etm_write_reg(0x02, etm_trigger);
319 etm_write_reg(0x06, etm_config.etm_06_te_start_stop);
320 etm_write_reg(0x07, etm_config.etm_07_te_single_addr_comp);
321 etm_write_reg(0x08, etm_config.etm_08_te_event);
322 etm_write_reg(0x09, etm_config.etm_09_te_control);
323 etm_write_reg(0x0a, etm_config.etm_0a_fifofull_region);
324 etm_write_reg(0x0b, etm_config.etm_0b_fifofull_level);
325 etm_write_reg(0x0c, etm_config.etm_0c_vd_event);
326 etm_write_reg(0x0d, etm_config.etm_0d_vd_single_addr_comp);
327 etm_write_reg(0x0e, etm_config.etm_0e_vd_mmd);
328 etm_write_reg(0x0f, etm_config.etm_0f_vd_control);
329 etm_write_reg(0x10, etm_config.etm_addr_comp_value[0]);
330 etm_write_reg(0x11, etm_config.etm_addr_comp_value[1]);
331 etm_write_reg(0x12, etm_config.etm_addr_comp_value[2]);
332 etm_write_reg(0x13, etm_config.etm_addr_comp_value[3]);
333 etm_write_reg(0x14, etm_config.etm_addr_comp_value[4]);
334 etm_write_reg(0x15, etm_config.etm_addr_comp_value[5]);
335 etm_write_reg(0x16, etm_config.etm_addr_comp_value[6]);
336 etm_write_reg(0x17, etm_config.etm_addr_comp_value[7]);
337 etm_write_reg(0x20, etm_config.etm_addr_access_type[0]);
338 etm_write_reg(0x21, etm_config.etm_addr_access_type[1]);
339 etm_write_reg(0x22, etm_config.etm_addr_access_type[2]);
340 etm_write_reg(0x23, etm_config.etm_addr_access_type[3]);
341 etm_write_reg(0x24, etm_config.etm_addr_access_type[4]);
342 etm_write_reg(0x25, etm_config.etm_addr_access_type[5]);
343 etm_write_reg(0x26, etm_config.etm_addr_access_type[6]);
344 etm_write_reg(0x27, etm_config.etm_addr_access_type[7]);
345 etm_write_reg(0x30, etm_config.etm_data_comp_value[0]);
346 etm_write_reg(0x32, etm_config.etm_data_comp_value[1]);
347 etm_write_reg(0x40, etm_config.etm_data_comp_mask[0]);
348 etm_write_reg(0x42, etm_config.etm_data_comp_mask[1]);
349 etm_write_reg(0x50, etm_config.etm_counter_reload_value[0]);
350 etm_write_reg(0x51, etm_config.etm_counter_reload_value[1]);
351 etm_write_reg(0x54, etm_config.etm_counter_enable[0]);
352 etm_write_reg(0x55, etm_config.etm_counter_enable[1]);
353 etm_write_reg(0x58, etm_config.etm_counter_reload_event[0]);
354 etm_write_reg(0x59, etm_config.etm_counter_reload_event[1]);
355 etm_write_reg(0x60, etm_config.etm_60_seq_event_1_to_2);
356 etm_write_reg(0x61, etm_config.etm_61_seq_event_2_to_1);
357 etm_write_reg(0x62, etm_config.etm_62_seq_event_2_to_3);
358 etm_write_reg(0x63, etm_config.etm_63_seq_event_3_to_1);
359 etm_write_reg(0x64, etm_config.etm_64_seq_event_3_to_2);
360 etm_write_reg(0x65, etm_config.etm_65_seq_event_1_to_3);
361 etm_write_reg(0x68, etm_external_output);
362 etm_write_reg(0x6c, etm_config.etm_6c_cid_comp_value_1);
363 etm_write_reg(0x6f, etm_config.etm_6f_cid_comp_mask);
364 etm_write_reg(0x78, etm_config.etm_78_sync_freq);
365
366 /* Note that we must enable the ETB before we enable the ETM if we
367 * want to capture the "always true" trigger event. */
368
369 __cpu_enable_etb();
370 __cpu_enable_etm();
371
372 put_cpu();
373}
374
375static void __cpu_disable_trace(void *unused)
376{
377 uint32_t etm_control;
378
379 get_cpu();
380 etm_read_reg(0xC5); /* clear sticky bit in PDSR */
381
382 __cpu_disable_etm();
383
384 /* program trace enable to be low by using always false event */
385 etm_write_reg(0x08, 0x6F | BIT(14));
386
387 /* set the powerdown bit */
388 etm_control = etm_read_reg(ETM_REG_CONTROL);
389 etm_control |= ETM_CONTROL_POWERDOWN;
390 etm_write_reg(ETM_REG_CONTROL, etm_control);
391
392 __cpu_enable_etm();
393 __cpu_disable_etb();
394
395 put_cpu();
396}
397
398static void enable_trace(void)
399{
400 wake_lock(&etm_wake_lock);
401 pm_qos_update_request(&etm_qos_req, 0);
402
403 if (etm_config.swconfig & TRIGGER_ALL) {
404 /* This register is accessible from either core.
405 * CPU1_extout[0] -> CPU0_extin[0]
406 * CPU_extout[0] -> CPU1_extin[0] */
407 l2tevselr0_write(0x00000001);
408 }
409
410 get_cpu();
411 __cpu_enable_trace(NULL);
412 smp_call_function(__cpu_enable_trace, NULL, 1);
413 put_cpu();
414
415 /* When the smp_call returns, we are guaranteed that all online
416 * cpus are out of wfi/power_collapse and won't be allowed to enter
417 * again due to the pm_qos latency request above.
418 */
419 trace_enabled = 1;
420
421 pm_qos_update_request(&etm_qos_req, PM_QOS_DEFAULT_VALUE);
422 wake_unlock(&etm_wake_lock);
423}
424
425static void disable_trace(void)
426{
427 int cpu;
428
429 wake_lock(&etm_wake_lock);
430 pm_qos_update_request(&etm_qos_req, 0);
431
432 get_cpu();
433 __cpu_disable_trace(NULL);
434 smp_call_function(__cpu_disable_trace, NULL, 1);
435 put_cpu();
436
437 /* When the smp_call returns, we are guaranteed that all online
438 * cpus are out of wfi/power_collapse and won't be allowed to enter
439 * again due to the pm_qos latency request above.
440 */
441 trace_enabled = 0;
442
443 for_each_possible_cpu(cpu)
444 *per_cpu_ptr(cpu_restore, cpu) = 0;
445
446 cpu_to_dump = next_cpu_to_dump = 0;
447
448 pm_qos_update_request(&etm_qos_req, PM_QOS_DEFAULT_VALUE);
449 wake_unlock(&etm_wake_lock);
450}
451
452static void generate_etb_dump(void)
453{
454 uint32_t i;
455 uint32_t full_slots;
456 uint32_t etb_control;
457 uint32_t prim_len;
458 uint32_t uptime = 0;
459
460 etb_control = etm_read_reg(ETB_REG_CONTROL);
461 etb_control |= AIR;
462 etm_write_reg(ETB_REG_CONTROL, etb_control);
463
464 if (etm_read_reg(ETB_REG_STATUS) & OV)
465 full_slots = ETB_RAM_SLOTS;
466 else
467 full_slots = etm_read_reg(ETB_REG_ADDRESS) >> 2;
468
469 prim_len = 28 + (full_slots * 4);
470
471 emit_log_char((DATALOG_SYNC >> 8) & 0xFF);
472 emit_log_char((DATALOG_SYNC >> 0) & 0xFF);
473 emit_log_char((prim_len >> 8) & 0xFF);
474 emit_log_char((prim_len >> 0) & 0xFF);
475 emit_log_word(uptime);
476 emit_log_word(ETB_DUMP_MSG_ID);
477 emit_log_word(etm_read_reg(ETM_REG_CONTROL));
478 emit_log_word(etm_config.etb_init_ptr >> 2);
479 emit_log_word(etm_read_reg(ETB_REG_ADDRESS) >> 2);
480 emit_log_word((etm_read_reg(ETB_REG_STATUS) & OV) >> 21);
481
482 etm_write_reg(ETB_REG_ADDRESS, 0x00000000);
483 for (i = 0; i < full_slots; i++)
484 emit_log_word(etm_read_reg(ETB_REG_DATA));
485}
486
487static void generate_etm_dump(void)
488{
489 uint32_t i;
490 uint32_t prim_len;
491 uint32_t uptime = 0;
492
493 prim_len = 12 + (4 * ETM_NUM_REGS);
494
495 emit_log_char((DATALOG_SYNC >> 8) & 0xFF);
496 emit_log_char((DATALOG_SYNC >> 0) & 0xFF);
497 emit_log_char((prim_len >> 8) & 0xFF);
498 emit_log_char((prim_len >> 0) & 0xFF);
499 emit_log_word(uptime);
500 emit_log_word(ETM_DUMP_MSG_ID);
501
502 /* do not disturb ETB_REG_ADDRESS by reading ETB_REG_DATA */
503 for (i = 0; i < ETM_NUM_REGS; i++)
504 if (i == ETB_REG_DATA)
505 emit_log_word(0);
506 else
507 emit_log_word(etm_read_reg(i));
508}
509
510static void dump_all(void *unused)
511{
512 get_cpu();
513 etm_read_reg(0xC5); /* clear sticky bit in PDSR in case
514 * trace hasn't been enabled yet. */
515 __cpu_disable_etb();
516 generate_etm_dump();
517 generate_etb_dump();
518 if (trace_enabled)
519 __cpu_enable_etb();
520 put_cpu();
521}
522
523static void dump_trace(void)
524{
525 get_cpu();
526 dump_all(NULL);
527 smp_call_function(dump_all, NULL, 1);
528 put_cpu();
529}
530
531static int bytes_to_dump;
532static uint8_t *etm_buf_ptr;
533
534static int etm_dev_open(struct inode *inode, struct file *file)
535{
536 if (atomic_cmpxchg(&etm_dev_in_use, 0, 1))
537 return -EBUSY;
538
539 pr_debug("%s: successfully opened\n", __func__);
540 return 0;
541}
542
543static ssize_t etm_dev_read(struct file *file, char __user *data,
544 size_t len, loff_t *ppos)
545{
546 if (cpu_to_dump == next_cpu_to_dump) {
547 if (cpu_to_dump == 0)
548 dump_trace();
549 bytes_to_dump = buf[cpu_to_dump].log_end;
550 buf[cpu_to_dump].log_end = 0;
551 etm_buf_ptr = buf[cpu_to_dump].etm_log_buf;
552 next_cpu_to_dump++;
553 if (next_cpu_to_dump >= num_possible_cpus())
554 next_cpu_to_dump = 0;
555 }
556
557 if (len > bytes_to_dump)
558 len = bytes_to_dump;
559
560 if (copy_to_user(data, etm_buf_ptr, len)) {
561 pr_debug("%s: copy_to_user failed\n", __func__);
562 return -EFAULT;
563 }
564
565 bytes_to_dump -= len;
566 etm_buf_ptr += len;
567
568 pr_debug("%s: %d bytes copied, %d bytes left (cpu %d)\n",
569 __func__, len, bytes_to_dump, next_cpu_to_dump);
570 return len;
571}
572
573static void setup_range_filter(char addr_type, char range, uint32_t reg1,
574 uint32_t addr1, uint32_t reg2, uint32_t addr2)
575{
576 etm_config.etm_addr_comp_value[reg1] = addr1;
577 etm_config.etm_addr_comp_value[reg2] = addr2;
578
579 etm_config.etm_07_te_single_addr_comp |= (1 << reg1);
580 etm_config.etm_07_te_single_addr_comp |= (1 << reg2);
581
582 etm_config.etm_09_te_control |= (1 << (reg1/2));
583 if (range == 'i')
584 etm_config.etm_09_te_control &= ~(1 << 24);
585 else if (range == 'e')
586 etm_config.etm_09_te_control |= (1 << 24);
587
588 if (addr_type == 'i') {
589 etm_config.etm_addr_access_type[reg1] = 0x99;
590 etm_config.etm_addr_access_type[reg2] = 0x99;
591 } else if (addr_type == 'd') {
592 etm_config.etm_addr_access_type[reg1] = 0x9C;
593 etm_config.etm_addr_access_type[reg2] = 0x9C;
594 }
595}
596
597static void setup_start_stop_filter(char addr_type, char start_stop,
598 uint32_t reg, uint32_t addr)
599{
600 etm_config.etm_addr_comp_value[reg] = addr;
601
602 if (start_stop == 's')
603 etm_config.etm_06_te_start_stop |= (1 << reg);
604 else if (start_stop == 't')
605 etm_config.etm_06_te_start_stop |= (1 << (reg + 16));
606
607 etm_config.etm_09_te_control |= (1 << 25);
608
609 if (addr_type == 'i')
610 etm_config.etm_addr_access_type[reg] = 0x99;
611 else if (addr_type == 'd')
612 etm_config.etm_addr_access_type[reg] = 0x9C;
613}
614
615static void setup_viewdata_range_filter(char range, uint32_t reg1,
616 uint32_t addr1, uint32_t reg2, uint32_t addr2)
617{
618 etm_config.etm_addr_comp_value[reg1] = addr1;
619 etm_config.etm_addr_comp_value[reg2] = addr2;
620
621 if (range == 'i') {
622 etm_config.etm_0d_vd_single_addr_comp |= (1 << reg1);
623 etm_config.etm_0d_vd_single_addr_comp |= (1 << reg2);
624 etm_config.etm_0f_vd_control |= (1 << (reg1/2));
625 } else if (range == 'e') {
626 etm_config.etm_0d_vd_single_addr_comp |= (1 << (reg1 + 16));
627 etm_config.etm_0d_vd_single_addr_comp |= (1 << (reg2 + 16));
628 etm_config.etm_0f_vd_control |= (1 << ((reg1/2) + 8));
629 }
630 etm_config.etm_0f_vd_control &= ~(1 << 16);
631
632 etm_config.etm_addr_access_type[reg1] = 0x9C;
633 etm_config.etm_addr_access_type[reg2] = 0x9C;
634}
635
636static void setup_viewdata_start_stop_filter(char start_stop, uint32_t reg,
637 uint32_t addr)
638{
639 etm_config.etm_addr_comp_value[reg] = addr;
640
641 if (start_stop == 's')
642 etm_config.etm_06_te_start_stop |= (1 << reg);
643 else if (start_stop == 't')
644 etm_config.etm_06_te_start_stop |= (1 << (reg + 16));
645
646 etm_config.etm_addr_access_type[reg] = 0x9C;
647}
648
649static void setup_access_type(uint32_t reg, uint32_t value)
650{
651 etm_config.etm_addr_access_type[reg] &= 0xFFFFFFF8;
652 value &= 0x7;
653 etm_config.etm_addr_access_type[reg] |= value;
654}
655
656static void reset_filter(void)
657{
658 etm_config.etm_00_control = 0x0000D84E;
659 /* etm_02_trigger_event 0x00000000: address comparator 0 matches */
660 etm_config.etm_02_trigger_event = 0x00000000;
661 etm_config.etm_06_te_start_stop = 0x00000000;
662 etm_config.etm_07_te_single_addr_comp = 0x00000000;
663 /* etm_08_te_event 0x0000006F: always true */
664 etm_config.etm_08_te_event = 0x0000006F;
665 /* etm_09_te_control 0x01000000: exclude none */
666 etm_config.etm_09_te_control = 0x01000000;
667 etm_config.etm_0a_fifofull_region = 0x00000000;
668 etm_config.etm_0b_fifofull_level = 0x00000000;
669 /* etm_0c_vd_event 0x0000006F: always true */
670 etm_config.etm_0c_vd_event = 0x0000006F;
671 etm_config.etm_0d_vd_single_addr_comp = 0x00000000;
672 etm_config.etm_0e_vd_mmd = 0x00000000;
673 /* etm_0f_vd_control 0x00010000: exclude none */
674 etm_config.etm_0f_vd_control = 0x00010000;
675 etm_config.etm_addr_comp_value[0] = 0x00000000;
676 etm_config.etm_addr_comp_value[1] = 0x00000000;
677 etm_config.etm_addr_comp_value[2] = 0x00000000;
678 etm_config.etm_addr_comp_value[3] = 0x00000000;
679 etm_config.etm_addr_comp_value[4] = 0x00000000;
680 etm_config.etm_addr_comp_value[5] = 0x00000000;
681 etm_config.etm_addr_comp_value[6] = 0x00000000;
682 etm_config.etm_addr_comp_value[7] = 0x00000000;
683 etm_config.etm_addr_access_type[0] = 0x00000000;
684 etm_config.etm_addr_access_type[1] = 0x00000000;
685 etm_config.etm_addr_access_type[2] = 0x00000000;
686 etm_config.etm_addr_access_type[3] = 0x00000000;
687 etm_config.etm_addr_access_type[4] = 0x00000000;
688 etm_config.etm_addr_access_type[5] = 0x00000000;
689 etm_config.etm_addr_access_type[6] = 0x00000000;
690 etm_config.etm_addr_access_type[7] = 0x00000000;
691 etm_config.etm_data_comp_value[0] = 0x00000000;
692 etm_config.etm_data_comp_value[1] = 0x00000000;
693 etm_config.etm_data_comp_mask[0] = 0x00000000;
694 etm_config.etm_data_comp_mask[1] = 0x00000000;
695 etm_config.etm_counter_reload_value[0] = 0x00000000;
696 etm_config.etm_counter_reload_value[1] = 0x00000000;
697 etm_config.etm_counter_enable[0] = 0x0002406F;
698 etm_config.etm_counter_enable[1] = 0x0002406F;
699 etm_config.etm_counter_reload_event[0] = 0x0000406F;
700 etm_config.etm_counter_reload_event[1] = 0x0000406F;
701 etm_config.etm_60_seq_event_1_to_2 = 0x0000406F;
702 etm_config.etm_61_seq_event_2_to_1 = 0x0000406F;
703 etm_config.etm_62_seq_event_2_to_3 = 0x0000406F;
704 etm_config.etm_63_seq_event_3_to_1 = 0x0000406F;
705 etm_config.etm_64_seq_event_3_to_2 = 0x0000406F;
706 etm_config.etm_65_seq_event_1_to_3 = 0x0000406F;
707 etm_config.etm_6c_cid_comp_value_1 = 0x00000000;
708 etm_config.etm_6f_cid_comp_mask = 0x00000000;
709 etm_config.etm_78_sync_freq = 0x00000400;
710 etm_config.swconfig = 0x00000002;
711 /* etb_trig_cnt 0x00000020: ignore trigger */
712 etm_config.etb_trig_cnt = 0x00000000;
713 /* etb_init_ptr 0x00000010: 16 marker bytes */
714 etm_config.etb_init_ptr = 0x00000010;
715}
716
717#define MAX_COMMAND_STRLEN 40
718static ssize_t etm_dev_write(struct file *file, const char __user *data,
719 size_t len, loff_t *ppos)
720{
721 char command[MAX_COMMAND_STRLEN];
722 int strlen;
723 unsigned long value;
724 unsigned long reg1, reg2;
725 unsigned long addr1, addr2;
726
727 strlen = strnlen_user(data, MAX_COMMAND_STRLEN);
728 pr_debug("etm: string length: %d", strlen);
729 if (strlen == 0 || strlen == (MAX_COMMAND_STRLEN+1)) {
730 pr_err("etm: error in strlen: %d", strlen);
731 return -EFAULT;
732 }
733 /* includes the null character */
734 if (copy_from_user(command, data, strlen)) {
735 pr_err("etm: error in copy_from_user: %d", strlen);
736 return -EFAULT;
737 }
738
739 pr_debug("etm: input = %s", command);
740
741 switch (command[0]) {
742 case '0':
743 if (trace_enabled) {
744 disable_trace();
745 pr_info("etm: tracing disabled\n");
746 }
747 break;
748 case '1':
749 if (!trace_enabled) {
750 enable_trace();
751 pr_info("etm: tracing enabled\n");
752 }
753 break;
754 case 'f':
755 switch (command[2]) {
756 case 'i':
757 case 'd':
758 switch (command[4]) {
759 case 'i':
760 if (sscanf(&command[6], "%lx:%lx:%lx:%lx\\0",
761 &reg1, &addr1, &reg2, &addr2) != 4)
762 goto err_out;
763 if (reg1 > 7 || reg2 > 7 || (reg1 % 2))
764 goto err_out;
765 setup_range_filter(command[2], 'i',
766 reg1, addr1, reg2, addr2);
767 break;
768 case 'e':
769 if (sscanf(&command[6], "%lx:%lx:%lx:%lx\\0",
770 &reg1, &addr1, &reg2, &addr2) != 4)
771 goto err_out;
772 if (reg1 > 7 || reg2 > 7 || (reg1 % 2)
773 || command[2] == 'd')
774 goto err_out;
775 setup_range_filter(command[2], 'e',
776 reg1, addr1, reg2, addr2);
777 break;
778 case 's':
779 if (sscanf(&command[6], "%lx:%lx\\0",
780 &reg1, &addr1) != 2)
781 goto err_out;
782 if (reg1 > 7)
783 goto err_out;
784 setup_start_stop_filter(command[2], 's',
785 reg1, addr1);
786 break;
787 case 't':
788 if (sscanf(&command[6], "%lx:%lx\\0",
789 &reg1, &addr1) != 2)
790 goto err_out;
791 if (reg1 > 7)
792 goto err_out;
793 setup_start_stop_filter(command[2], 't',
794 reg1, addr1);
795 break;
796 default:
797 goto err_out;
798 }
799 break;
800 case 'r':
801 reset_filter();
802 break;
803 default:
804 goto err_out;
805 }
806 break;
807 case 'v':
808 switch (command[2]) {
809 case 'd':
810 switch (command[4]) {
811 case 'i':
812 if (sscanf(&command[6], "%lx:%lx:%lx:%lx\\0",
813 &reg1, &addr1, &reg2, &addr2) != 4)
814 goto err_out;
815 if (reg1 > 7 || reg2 > 7 || (reg1 % 2))
816 goto err_out;
817 setup_viewdata_range_filter('i',
818 reg1, addr1, reg2, addr2);
819 break;
820 case 'e':
821 if (sscanf(&command[6], "%lx:%lx:%lx:%lx\\0",
822 &reg1, &addr1, &reg2, &addr2) != 4)
823 goto err_out;
824 if (reg1 > 7 || reg2 > 7 || (reg1 % 2))
825 goto err_out;
826 setup_viewdata_range_filter('e',
827 reg1, addr1, reg2, addr2);
828 break;
829 case 's':
830 if (sscanf(&command[6], "%lx:%lx\\0",
831 &reg1, &addr1) != 2)
832 goto err_out;
833 if (reg1 > 7)
834 goto err_out;
835 setup_viewdata_start_stop_filter('s',
836 reg1, addr1);
837 break;
838 case 't':
839 if (sscanf(&command[6], "%lx:%lx\\0",
840 &reg1, &addr1) != 2)
841 goto err_out;
842 if (reg1 > 7)
843 goto err_out;
844 setup_viewdata_start_stop_filter('t',
845 reg1, addr1);
846 break;
847 default:
848 goto err_out;
849 }
850 break;
851 default:
852 goto err_out;
853 }
854 break;
855 case 'a':
856 switch (command[2]) {
857 case 't':
858 if (sscanf(&command[4], "%lx:%lx\\0",
859 &reg1, &value) != 2)
860 goto err_out;
861 if (reg1 > 7 || value > 6)
862 goto err_out;
863 setup_access_type(reg1, value);
864 break;
865 default:
866 goto err_out;
867 }
868 break;
869 default:
870 goto err_out;
871 }
872
873 return len;
874
875err_out:
876 return -EFAULT;
877}
878
879static int etm_dev_release(struct inode *inode, struct file *file)
880{
881 if (cpu_to_dump == next_cpu_to_dump)
882 next_cpu_to_dump = 0;
883 cpu_to_dump = next_cpu_to_dump;
884
885 atomic_set(&etm_dev_in_use, 0);
886 pr_debug("%s: released\n", __func__);
887 return 0;
888}
889
890static const struct file_operations etm_dev_fops = {
891 .owner = THIS_MODULE,
892 .open = etm_dev_open,
893 .read = etm_dev_read,
894 .write = etm_dev_write,
895 .release = etm_dev_release,
896};
897
898static struct miscdevice etm_dev = {
899 .name = "msm_etm",
900 .minor = MISC_DYNAMIC_MINOR,
901 .fops = &etm_dev_fops,
902};
903
904/* etm_save_reg_check and etm_restore_reg_check should be fast
905 *
906 * These functions will be called either from:
907 * 1. per_cpu idle thread context for idle wfi and power collapses.
908 * 2. per_cpu idle thread context for hotplug/suspend power collapse for
909 * nonboot cpus.
910 * 3. suspend thread context for core0.
911 *
912 * In all cases we are guaranteed to be running on the same cpu for the
913 * entire duration.
914 *
915 * Another assumption is that etm registers won't change after trace_enabled
916 * is set. Current usage model guarantees this doesn't happen.
917 */
918void etm_save_reg_check(void)
919{
920 if (trace_enabled) {
921 int cpu = smp_processor_id();
922
923 /* Don't save the registers if we just got called from per_cpu
924 * idle thread context of a nonboot cpu after hotplug/suspend
925 * power collapse. This is to prevent corruption due to saving
926 * twice since nonboot cpus start out fresh without the
927 * corresponding restore.
928 */
929 if (!(*per_cpu_ptr(cpu_restore, cpu))) {
930 etm_save_reg();
931 *per_cpu_ptr(cpu_restore, cpu) = 1;
932 }
933 }
934}
935
936void etm_restore_reg_check(void)
937{
938 if (trace_enabled) {
939 int cpu = smp_processor_id();
940
941 etm_restore_reg();
942 *per_cpu_ptr(cpu_restore, cpu) = 0;
943 }
944}
945
946static int __init etm_init(void)
947{
948 int ret, cpu;
949
950 ret = misc_register(&etm_dev);
951 if (ret)
952 return -ENODEV;
953
954 alloc_b = alloc_percpu(typeof(*alloc_b));
955 if (!alloc_b)
956 goto err1;
957
958 for_each_possible_cpu(cpu)
959 *per_cpu_ptr(alloc_b, cpu) = &buf[cpu];
960
961 cpu_restore = alloc_percpu(int);
962 if (!cpu_restore)
963 goto err2;
964
965 for_each_possible_cpu(cpu)
966 *per_cpu_ptr(cpu_restore, cpu) = 0;
967
968 wake_lock_init(&etm_wake_lock, WAKE_LOCK_SUSPEND, "msm_etm");
969 pm_qos_add_request(&etm_qos_req, PM_QOS_CPU_DMA_LATENCY,
970 PM_QOS_DEFAULT_VALUE);
971
972 cpu_to_dump = next_cpu_to_dump = 0;
973
974 pr_info("ETM/ETB intialized.\n");
975
976 if (trace_on_boot)
977 enable_trace();
978
979 return 0;
980
981err2:
982 free_percpu(alloc_b);
983err1:
984 misc_deregister(&etm_dev);
985 return -ENOMEM;
986}
987
988static void __exit etm_exit(void)
989{
990 disable_trace();
991 pm_qos_remove_request(&etm_qos_req);
992 wake_lock_destroy(&etm_wake_lock);
993 free_percpu(cpu_restore);
994 free_percpu(alloc_b);
995 misc_deregister(&etm_dev);
996}
997
998module_init(etm_init);
999module_exit(etm_exit);
1000MODULE_LICENSE("GPL v2");
1001MODULE_DESCRIPTION("embedded trace driver");