1 /*
2 * linux/arch/arm/kernel/etm.c
3 *
4 * Driver for ARM's Embedded Trace Macrocell and Embedded Trace Buffer.
5 *
6 * Copyright (C) 2009 Nokia Corporation.
7 * Alexander Shishkin
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <linux/sysrq.h>
20 #include <linux/device.h>
21 #include <linux/clk.h>
22 #include <linux/amba/bus.h>
23 #include <linux/fs.h>
24 #include <linux/uaccess.h>
25 #include <linux/miscdevice.h>
26 #include <linux/vmalloc.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <asm/hardware/coresight.h>
30 #include <asm/sections.h>
31
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Alexander Shishkin");
34
35 /*
36 * ETM tracer state
37 */
38 struct tracectx {
39 unsigned int etb_bufsz;
40 void __iomem *etb_regs;
41 void __iomem **etm_regs;
42 int etm_regs_count;
43 unsigned long flags;
44 int ncmppairs;
45 int etm_portsz;
46 int etm_contextid_size;
47 u32 etb_fc;
48 unsigned long range_start;
49 unsigned long range_end;
50 unsigned long data_range_start;
51 unsigned long data_range_end;
52 bool dump_initial_etb;
53 struct device *dev;
54 struct clk *emu_clk;
55 struct mutex mutex;
56 };
57
58 static struct tracectx tracer = {
59 .range_start = (unsigned long)_stext,
60 .range_end = (unsigned long)_etext,
61 };
62
trace_isrunning(struct tracectx * t)63 static inline bool trace_isrunning(struct tracectx *t)
64 {
65 return !!(t->flags & TRACER_RUNNING);
66 }
67
etm_setup_address_range(struct tracectx * t,int id,int n,unsigned long start,unsigned long end,int exclude,int data)68 static int etm_setup_address_range(struct tracectx *t, int id, int n,
69 unsigned long start, unsigned long end, int exclude, int data)
70 {
71 u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_IGNSECURITY |
72 ETMAAT_NOVALCMP;
73
74 if (n < 1 || n > t->ncmppairs)
75 return -EINVAL;
76
77 /* comparators and ranges are numbered starting with 1 as opposed
78 * to bits in a word */
79 n--;
80
81 if (data)
82 flags |= ETMAAT_DLOADSTORE;
83 else
84 flags |= ETMAAT_IEXEC;
85
86 /* first comparator for the range */
87 etm_writel(t, id, flags, ETMR_COMP_ACC_TYPE(n * 2));
88 etm_writel(t, id, start, ETMR_COMP_VAL(n * 2));
89
90 /* second comparator is right next to it */
91 etm_writel(t, id, flags, ETMR_COMP_ACC_TYPE(n * 2 + 1));
92 etm_writel(t, id, end, ETMR_COMP_VAL(n * 2 + 1));
93
94 if (data) {
95 flags = exclude ? ETMVDC3_EXCLONLY : 0;
96 if (exclude)
97 n += 8;
98 etm_writel(t, id, flags | BIT(n), ETMR_VIEWDATACTRL3);
99 } else {
100 flags = exclude ? ETMTE_INCLEXCL : 0;
101 etm_writel(t, id, flags | (1 << n), ETMR_TRACEENCTRL);
102 }
103
104 return 0;
105 }
106
trace_start_etm(struct tracectx * t,int id)107 static int trace_start_etm(struct tracectx *t, int id)
108 {
109 u32 v;
110 unsigned long timeout = TRACER_TIMEOUT;
111
112 v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
113 v |= ETMCTRL_CONTEXTIDSIZE(t->etm_contextid_size);
114
115 if (t->flags & TRACER_CYCLE_ACC)
116 v |= ETMCTRL_CYCLEACCURATE;
117
118 if (t->flags & TRACER_BRANCHOUTPUT)
119 v |= ETMCTRL_BRANCH_OUTPUT;
120
121 if (t->flags & TRACER_TRACE_DATA)
122 v |= ETMCTRL_DATA_DO_ADDR;
123
124 if (t->flags & TRACER_TIMESTAMP)
125 v |= ETMCTRL_TIMESTAMP_EN;
126
127 if (t->flags & TRACER_RETURN_STACK)
128 v |= ETMCTRL_RETURN_STACK_EN;
129
130 etm_unlock(t, id);
131
132 etm_writel(t, id, v, ETMR_CTRL);
133
134 while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
135 ;
136 if (!timeout) {
137 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
138 etm_lock(t, id);
139 return -EFAULT;
140 }
141
142 if (t->range_start || t->range_end)
143 etm_setup_address_range(t, id, 1,
144 t->range_start, t->range_end, 0, 0);
145 else
146 etm_writel(t, id, ETMTE_INCLEXCL, ETMR_TRACEENCTRL);
147
148 etm_writel(t, id, 0, ETMR_TRACEENCTRL2);
149 etm_writel(t, id, 0, ETMR_TRACESSCTRL);
150 etm_writel(t, id, 0x6f, ETMR_TRACEENEVT);
151
152 etm_writel(t, id, 0, ETMR_VIEWDATACTRL1);
153 etm_writel(t, id, 0, ETMR_VIEWDATACTRL2);
154
155 if (t->data_range_start || t->data_range_end)
156 etm_setup_address_range(t, id, 2, t->data_range_start,
157 t->data_range_end, 0, 1);
158 else
159 etm_writel(t, id, ETMVDC3_EXCLONLY, ETMR_VIEWDATACTRL3);
160
161 etm_writel(t, id, 0x6f, ETMR_VIEWDATAEVT);
162
163 v &= ~ETMCTRL_PROGRAM;
164 v |= ETMCTRL_PORTSEL;
165
166 etm_writel(t, id, v, ETMR_CTRL);
167
168 timeout = TRACER_TIMEOUT;
169 while (etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
170 ;
171 if (!timeout) {
172 dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
173 etm_lock(t, id);
174 return -EFAULT;
175 }
176
177 etm_lock(t, id);
178 return 0;
179 }
180
trace_start(struct tracectx * t)181 static int trace_start(struct tracectx *t)
182 {
183 int ret;
184 int id;
185 u32 etb_fc = t->etb_fc;
186
187 etb_unlock(t);
188
189 t->dump_initial_etb = false;
190 etb_writel(t, 0, ETBR_WRITEADDR);
191 etb_writel(t, etb_fc, ETBR_FORMATTERCTRL);
192 etb_writel(t, 1, ETBR_CTRL);
193
194 etb_lock(t);
195
196 /* configure etm(s) */
197 for (id = 0; id < t->etm_regs_count; id++) {
198 ret = trace_start_etm(t, id);
199 if (ret)
200 return ret;
201 }
202
203 t->flags |= TRACER_RUNNING;
204
205 return 0;
206 }
207
trace_stop_etm(struct tracectx * t,int id)208 static int trace_stop_etm(struct tracectx *t, int id)
209 {
210 unsigned long timeout = TRACER_TIMEOUT;
211
212 etm_unlock(t, id);
213
214 etm_writel(t, id, 0x440, ETMR_CTRL);
215 while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
216 ;
217 if (!timeout) {
218 dev_err(t->dev,
219 "etm%d: Waiting for progbit to assert timed out\n",
220 id);
221 etm_lock(t, id);
222 return -EFAULT;
223 }
224
225 etm_lock(t, id);
226 return 0;
227 }
228
trace_power_down_etm(struct tracectx * t,int id)229 static int trace_power_down_etm(struct tracectx *t, int id)
230 {
231 unsigned long timeout = TRACER_TIMEOUT;
232 etm_unlock(t, id);
233 while (!(etm_readl(t, id, ETMR_STATUS) & ETMST_PROGBIT) && --timeout)
234 ;
235 if (!timeout) {
236 dev_err(t->dev, "etm%d: Waiting for status progbit to assert timed out\n",
237 id);
238 etm_lock(t, id);
239 return -EFAULT;
240 }
241
242 etm_writel(t, id, 0x441, ETMR_CTRL);
243
244 etm_lock(t, id);
245 return 0;
246 }
247
trace_stop(struct tracectx * t)248 static int trace_stop(struct tracectx *t)
249 {
250 int id;
251 unsigned long timeout = TRACER_TIMEOUT;
252 u32 etb_fc = t->etb_fc;
253
254 for (id = 0; id < t->etm_regs_count; id++)
255 trace_stop_etm(t, id);
256
257 for (id = 0; id < t->etm_regs_count; id++)
258 trace_power_down_etm(t, id);
259
260 etb_unlock(t);
261 if (etb_fc) {
262 etb_fc |= ETBFF_STOPFL;
263 etb_writel(t, t->etb_fc, ETBR_FORMATTERCTRL);
264 }
265 etb_writel(t, etb_fc | ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
266
267 timeout = TRACER_TIMEOUT;
268 while (etb_readl(t, ETBR_FORMATTERCTRL) &
269 ETBFF_MANUAL_FLUSH && --timeout)
270 ;
271 if (!timeout) {
272 dev_dbg(t->dev, "Waiting for formatter flush to commence "
273 "timed out\n");
274 etb_lock(t);
275 return -EFAULT;
276 }
277
278 etb_writel(t, 0, ETBR_CTRL);
279
280 etb_lock(t);
281
282 t->flags &= ~TRACER_RUNNING;
283
284 return 0;
285 }
286
etb_getdatalen(struct tracectx * t)287 static int etb_getdatalen(struct tracectx *t)
288 {
289 u32 v;
290 int wp;
291
292 v = etb_readl(t, ETBR_STATUS);
293
294 if (v & 1)
295 return t->etb_bufsz;
296
297 wp = etb_readl(t, ETBR_WRITEADDR);
298 return wp;
299 }
300
301 /* sysrq+v will always stop the running trace and leave it at that */
etm_dump(void)302 static void etm_dump(void)
303 {
304 struct tracectx *t = &tracer;
305 u32 first = 0;
306 int length;
307
308 if (!t->etb_regs) {
309 printk(KERN_INFO "No tracing hardware found\n");
310 return;
311 }
312
313 if (trace_isrunning(t))
314 trace_stop(t);
315
316 etb_unlock(t);
317
318 length = etb_getdatalen(t);
319
320 if (length == t->etb_bufsz)
321 first = etb_readl(t, ETBR_WRITEADDR);
322
323 etb_writel(t, first, ETBR_READADDR);
324
325 printk(KERN_INFO "Trace buffer contents length: %d\n", length);
326 printk(KERN_INFO "--- ETB buffer begin ---\n");
327 for (; length; length--)
328 printk("%08x", cpu_to_be32(etb_readl(t, ETBR_READMEM)));
329 printk(KERN_INFO "\n--- ETB buffer end ---\n");
330
331 etb_lock(t);
332 }
333
sysrq_etm_dump(int key)334 static void sysrq_etm_dump(int key)
335 {
336 if (!mutex_trylock(&tracer.mutex)) {
337 printk(KERN_INFO "Tracing hardware busy\n");
338 return;
339 }
340 dev_dbg(tracer.dev, "Dumping ETB buffer\n");
341 etm_dump();
342 mutex_unlock(&tracer.mutex);
343 }
344
345 static struct sysrq_key_op sysrq_etm_op = {
346 .handler = sysrq_etm_dump,
347 .help_msg = "etm-buffer-dump(v)",
348 .action_msg = "etm",
349 };
350
etb_open(struct inode * inode,struct file * file)351 static int etb_open(struct inode *inode, struct file *file)
352 {
353 if (!tracer.etb_regs)
354 return -ENODEV;
355
356 file->private_data = &tracer;
357
358 return nonseekable_open(inode, file);
359 }
360
etb_read(struct file * file,char __user * data,size_t len,loff_t * ppos)361 static ssize_t etb_read(struct file *file, char __user *data,
362 size_t len, loff_t *ppos)
363 {
364 int total, i;
365 long length;
366 struct tracectx *t = file->private_data;
367 u32 first = 0;
368 u32 *buf;
369 int wpos;
370 int skip;
371 long wlength;
372 loff_t pos = *ppos;
373
374 mutex_lock(&t->mutex);
375
376 if (trace_isrunning(t)) {
377 length = 0;
378 goto out;
379 }
380
381 etb_unlock(t);
382
383 total = etb_getdatalen(t);
384 if (total == 0 && t->dump_initial_etb)
385 total = t->etb_bufsz;
386 if (total == t->etb_bufsz)
387 first = etb_readl(t, ETBR_WRITEADDR);
388
389 if (pos > total * 4) {
390 skip = 0;
391 wpos = total;
392 } else {
393 skip = (int)pos % 4;
394 wpos = (int)pos / 4;
395 }
396 total -= wpos;
397 first = (first + wpos) % t->etb_bufsz;
398
399 etb_writel(t, first, ETBR_READADDR);
400
401 wlength = min(total, DIV_ROUND_UP(skip + (int)len, 4));
402 length = min(total * 4 - skip, (int)len);
403 buf = vmalloc(wlength * 4);
404
405 dev_dbg(t->dev, "ETB read %ld bytes to %lld from %ld words at %d\n",
406 length, pos, wlength, first);
407 dev_dbg(t->dev, "ETB buffer length: %d\n", total + wpos);
408 dev_dbg(t->dev, "ETB status reg: %x\n", etb_readl(t, ETBR_STATUS));
409 for (i = 0; i < wlength; i++)
410 buf[i] = etb_readl(t, ETBR_READMEM);
411
412 etb_lock(t);
413
414 length -= copy_to_user(data, (u8 *)buf + skip, length);
415 vfree(buf);
416 *ppos = pos + length;
417
418 out:
419 mutex_unlock(&t->mutex);
420
421 return length;
422 }
423
etb_release(struct inode * inode,struct file * file)424 static int etb_release(struct inode *inode, struct file *file)
425 {
426 /* there's nothing to do here, actually */
427 return 0;
428 }
429
430 static const struct file_operations etb_fops = {
431 .owner = THIS_MODULE,
432 .read = etb_read,
433 .open = etb_open,
434 .release = etb_release,
435 .llseek = no_llseek,
436 };
437
438 static struct miscdevice etb_miscdev = {
439 .name = "tracebuf",
440 .minor = 0,
441 .fops = &etb_fops,
442 };
443
etb_probe(struct amba_device * dev,const struct amba_id * id)444 static int etb_probe(struct amba_device *dev, const struct amba_id *id)
445 {
446 struct tracectx *t = &tracer;
447 int ret = 0;
448
449 ret = amba_request_regions(dev, NULL);
450 if (ret)
451 goto out;
452
453 mutex_lock(&t->mutex);
454 t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
455 if (!t->etb_regs) {
456 ret = -ENOMEM;
457 goto out_release;
458 }
459
460 t->dev = &dev->dev;
461 t->dump_initial_etb = true;
462 amba_set_drvdata(dev, t);
463
464 etb_unlock(t);
465 t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
466 dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
467
468 /* make sure trace capture is disabled */
469 etb_writel(t, 0, ETBR_CTRL);
470 etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
471 etb_lock(t);
472 mutex_unlock(&t->mutex);
473
474 etb_miscdev.parent = &dev->dev;
475
476 ret = misc_register(&etb_miscdev);
477 if (ret)
478 goto out_unmap;
479
480 /* Get optional clock. Currently used to select clock source on omap3 */
481 t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
482 if (IS_ERR(t->emu_clk))
483 dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
484 else
485 clk_enable(t->emu_clk);
486
487 dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
488
489 out:
490 return ret;
491
492 out_unmap:
493 mutex_lock(&t->mutex);
494 iounmap(t->etb_regs);
495 t->etb_regs = NULL;
496
497 out_release:
498 mutex_unlock(&t->mutex);
499 amba_release_regions(dev);
500
501 return ret;
502 }
503
etb_remove(struct amba_device * dev)504 static int etb_remove(struct amba_device *dev)
505 {
506 struct tracectx *t = amba_get_drvdata(dev);
507
508 iounmap(t->etb_regs);
509 t->etb_regs = NULL;
510
511 if (!IS_ERR(t->emu_clk)) {
512 clk_disable(t->emu_clk);
513 clk_put(t->emu_clk);
514 }
515
516 amba_release_regions(dev);
517
518 return 0;
519 }
520
521 static struct amba_id etb_ids[] = {
522 {
523 .id = 0x0003b907,
524 .mask = 0x0007ffff,
525 },
526 { 0, 0 },
527 };
528
529 static struct amba_driver etb_driver = {
530 .drv = {
531 .name = "etb",
532 .owner = THIS_MODULE,
533 },
534 .probe = etb_probe,
535 .remove = etb_remove,
536 .id_table = etb_ids,
537 };
538
539 /* use a sysfs file "trace_running" to start/stop tracing */
trace_running_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)540 static ssize_t trace_running_show(struct kobject *kobj,
541 struct kobj_attribute *attr,
542 char *buf)
543 {
544 return sprintf(buf, "%x\n", trace_isrunning(&tracer));
545 }
546
trace_running_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)547 static ssize_t trace_running_store(struct kobject *kobj,
548 struct kobj_attribute *attr,
549 const char *buf, size_t n)
550 {
551 unsigned int value;
552 int ret;
553
554 if (sscanf(buf, "%u", &value) != 1)
555 return -EINVAL;
556
557 mutex_lock(&tracer.mutex);
558 if (!tracer.etb_regs)
559 ret = -ENODEV;
560 else
561 ret = value ? trace_start(&tracer) : trace_stop(&tracer);
562 mutex_unlock(&tracer.mutex);
563
564 return ret ? : n;
565 }
566
567 static struct kobj_attribute trace_running_attr =
568 __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
569
trace_info_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)570 static ssize_t trace_info_show(struct kobject *kobj,
571 struct kobj_attribute *attr,
572 char *buf)
573 {
574 u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
575 int datalen;
576 int id;
577 int ret;
578
579 mutex_lock(&tracer.mutex);
580 if (tracer.etb_regs) {
581 etb_unlock(&tracer);
582 datalen = etb_getdatalen(&tracer);
583 etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
584 etb_ra = etb_readl(&tracer, ETBR_READADDR);
585 etb_st = etb_readl(&tracer, ETBR_STATUS);
586 etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
587 etb_lock(&tracer);
588 } else {
589 etb_wa = etb_ra = etb_st = etb_fc = ~0;
590 datalen = -1;
591 }
592
593 ret = sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
594 "ETBR_WRITEADDR:\t%08x\n"
595 "ETBR_READADDR:\t%08x\n"
596 "ETBR_STATUS:\t%08x\n"
597 "ETBR_FORMATTERCTRL:\t%08x\n",
598 datalen,
599 tracer.ncmppairs,
600 etb_wa,
601 etb_ra,
602 etb_st,
603 etb_fc
604 );
605
606 for (id = 0; id < tracer.etm_regs_count; id++) {
607 etm_unlock(&tracer, id);
608 etm_ctrl = etm_readl(&tracer, id, ETMR_CTRL);
609 etm_st = etm_readl(&tracer, id, ETMR_STATUS);
610 etm_lock(&tracer, id);
611 ret += sprintf(buf + ret, "ETMR_CTRL:\t%08x\n"
612 "ETMR_STATUS:\t%08x\n",
613 etm_ctrl,
614 etm_st
615 );
616 }
617 mutex_unlock(&tracer.mutex);
618
619 return ret;
620 }
621
622 static struct kobj_attribute trace_info_attr =
623 __ATTR(trace_info, 0444, trace_info_show, NULL);
624
trace_mode_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)625 static ssize_t trace_mode_show(struct kobject *kobj,
626 struct kobj_attribute *attr,
627 char *buf)
628 {
629 return sprintf(buf, "%d %d\n",
630 !!(tracer.flags & TRACER_CYCLE_ACC),
631 tracer.etm_portsz);
632 }
633
trace_mode_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)634 static ssize_t trace_mode_store(struct kobject *kobj,
635 struct kobj_attribute *attr,
636 const char *buf, size_t n)
637 {
638 unsigned int cycacc, portsz;
639
640 if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
641 return -EINVAL;
642
643 mutex_lock(&tracer.mutex);
644 if (cycacc)
645 tracer.flags |= TRACER_CYCLE_ACC;
646 else
647 tracer.flags &= ~TRACER_CYCLE_ACC;
648
649 tracer.etm_portsz = portsz & 0x0f;
650 mutex_unlock(&tracer.mutex);
651
652 return n;
653 }
654
655 static struct kobj_attribute trace_mode_attr =
656 __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
657
trace_contextid_size_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)658 static ssize_t trace_contextid_size_show(struct kobject *kobj,
659 struct kobj_attribute *attr,
660 char *buf)
661 {
662 /* 0: No context id tracing, 1: One byte, 2: Two bytes, 3: Four bytes */
663 return sprintf(buf, "%d\n", (1 << tracer.etm_contextid_size) >> 1);
664 }
665
trace_contextid_size_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)666 static ssize_t trace_contextid_size_store(struct kobject *kobj,
667 struct kobj_attribute *attr,
668 const char *buf, size_t n)
669 {
670 unsigned int contextid_size;
671
672 if (sscanf(buf, "%u", &contextid_size) != 1)
673 return -EINVAL;
674
675 if (contextid_size == 3 || contextid_size > 4)
676 return -EINVAL;
677
678 mutex_lock(&tracer.mutex);
679 tracer.etm_contextid_size = fls(contextid_size);
680 mutex_unlock(&tracer.mutex);
681
682 return n;
683 }
684
685 static struct kobj_attribute trace_contextid_size_attr =
686 __ATTR(trace_contextid_size, 0644,
687 trace_contextid_size_show, trace_contextid_size_store);
688
trace_branch_output_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)689 static ssize_t trace_branch_output_show(struct kobject *kobj,
690 struct kobj_attribute *attr,
691 char *buf)
692 {
693 return sprintf(buf, "%d\n", !!(tracer.flags & TRACER_BRANCHOUTPUT));
694 }
695
trace_branch_output_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)696 static ssize_t trace_branch_output_store(struct kobject *kobj,
697 struct kobj_attribute *attr,
698 const char *buf, size_t n)
699 {
700 unsigned int branch_output;
701
702 if (sscanf(buf, "%u", &branch_output) != 1)
703 return -EINVAL;
704
705 mutex_lock(&tracer.mutex);
706 if (branch_output) {
707 tracer.flags |= TRACER_BRANCHOUTPUT;
708 /* Branch broadcasting is incompatible with the return stack */
709 tracer.flags &= ~TRACER_RETURN_STACK;
710 } else {
711 tracer.flags &= ~TRACER_BRANCHOUTPUT;
712 }
713 mutex_unlock(&tracer.mutex);
714
715 return n;
716 }
717
718 static struct kobj_attribute trace_branch_output_attr =
719 __ATTR(trace_branch_output, 0644,
720 trace_branch_output_show, trace_branch_output_store);
721
trace_return_stack_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)722 static ssize_t trace_return_stack_show(struct kobject *kobj,
723 struct kobj_attribute *attr,
724 char *buf)
725 {
726 return sprintf(buf, "%d\n", !!(tracer.flags & TRACER_RETURN_STACK));
727 }
728
trace_return_stack_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)729 static ssize_t trace_return_stack_store(struct kobject *kobj,
730 struct kobj_attribute *attr,
731 const char *buf, size_t n)
732 {
733 unsigned int return_stack;
734
735 if (sscanf(buf, "%u", &return_stack) != 1)
736 return -EINVAL;
737
738 mutex_lock(&tracer.mutex);
739 if (return_stack) {
740 tracer.flags |= TRACER_RETURN_STACK;
741 /* Return stack is incompatible with branch broadcasting */
742 tracer.flags &= ~TRACER_BRANCHOUTPUT;
743 } else {
744 tracer.flags &= ~TRACER_RETURN_STACK;
745 }
746 mutex_unlock(&tracer.mutex);
747
748 return n;
749 }
750
751 static struct kobj_attribute trace_return_stack_attr =
752 __ATTR(trace_return_stack, 0644,
753 trace_return_stack_show, trace_return_stack_store);
754
trace_timestamp_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)755 static ssize_t trace_timestamp_show(struct kobject *kobj,
756 struct kobj_attribute *attr,
757 char *buf)
758 {
759 return sprintf(buf, "%d\n", !!(tracer.flags & TRACER_TIMESTAMP));
760 }
761
trace_timestamp_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)762 static ssize_t trace_timestamp_store(struct kobject *kobj,
763 struct kobj_attribute *attr,
764 const char *buf, size_t n)
765 {
766 unsigned int timestamp;
767
768 if (sscanf(buf, "%u", ×tamp) != 1)
769 return -EINVAL;
770
771 mutex_lock(&tracer.mutex);
772 if (timestamp)
773 tracer.flags |= TRACER_TIMESTAMP;
774 else
775 tracer.flags &= ~TRACER_TIMESTAMP;
776 mutex_unlock(&tracer.mutex);
777
778 return n;
779 }
780
781 static struct kobj_attribute trace_timestamp_attr =
782 __ATTR(trace_timestamp, 0644,
783 trace_timestamp_show, trace_timestamp_store);
784
trace_range_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)785 static ssize_t trace_range_show(struct kobject *kobj,
786 struct kobj_attribute *attr,
787 char *buf)
788 {
789 return sprintf(buf, "%08lx %08lx\n",
790 tracer.range_start, tracer.range_end);
791 }
792
trace_range_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)793 static ssize_t trace_range_store(struct kobject *kobj,
794 struct kobj_attribute *attr,
795 const char *buf, size_t n)
796 {
797 unsigned long range_start, range_end;
798
799 if (sscanf(buf, "%lx %lx", &range_start, &range_end) != 2)
800 return -EINVAL;
801
802 mutex_lock(&tracer.mutex);
803 tracer.range_start = range_start;
804 tracer.range_end = range_end;
805 mutex_unlock(&tracer.mutex);
806
807 return n;
808 }
809
810
811 static struct kobj_attribute trace_range_attr =
812 __ATTR(trace_range, 0644, trace_range_show, trace_range_store);
813
trace_data_range_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)814 static ssize_t trace_data_range_show(struct kobject *kobj,
815 struct kobj_attribute *attr,
816 char *buf)
817 {
818 unsigned long range_start;
819 u64 range_end;
820 mutex_lock(&tracer.mutex);
821 range_start = tracer.data_range_start;
822 range_end = tracer.data_range_end;
823 if (!range_end && (tracer.flags & TRACER_TRACE_DATA))
824 range_end = 0x100000000ULL;
825 mutex_unlock(&tracer.mutex);
826 return sprintf(buf, "%08lx %08llx\n", range_start, range_end);
827 }
828
trace_data_range_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)829 static ssize_t trace_data_range_store(struct kobject *kobj,
830 struct kobj_attribute *attr,
831 const char *buf, size_t n)
832 {
833 unsigned long range_start;
834 u64 range_end;
835
836 if (sscanf(buf, "%lx %llx", &range_start, &range_end) != 2)
837 return -EINVAL;
838
839 mutex_lock(&tracer.mutex);
840 tracer.data_range_start = range_start;
841 tracer.data_range_end = (unsigned long)range_end;
842 if (range_end)
843 tracer.flags |= TRACER_TRACE_DATA;
844 else
845 tracer.flags &= ~TRACER_TRACE_DATA;
846 mutex_unlock(&tracer.mutex);
847
848 return n;
849 }
850
851
852 static struct kobj_attribute trace_data_range_attr =
853 __ATTR(trace_data_range, 0644,
854 trace_data_range_show, trace_data_range_store);
855
etm_probe(struct amba_device * dev,const struct amba_id * id)856 static int etm_probe(struct amba_device *dev, const struct amba_id *id)
857 {
858 struct tracectx *t = &tracer;
859 int ret = 0;
860 void __iomem **new_regs;
861 int new_count;
862 u32 etmccr;
863 u32 etmidr;
864 u32 etmccer = 0;
865 u8 etm_version = 0;
866
867 mutex_lock(&t->mutex);
868 new_count = t->etm_regs_count + 1;
869 new_regs = krealloc(t->etm_regs,
870 sizeof(t->etm_regs[0]) * new_count, GFP_KERNEL);
871
872 if (!new_regs) {
873 dev_dbg(&dev->dev, "Failed to allocate ETM register array\n");
874 ret = -ENOMEM;
875 goto out;
876 }
877 t->etm_regs = new_regs;
878
879 ret = amba_request_regions(dev, NULL);
880 if (ret)
881 goto out;
882
883 t->etm_regs[t->etm_regs_count] =
884 ioremap_nocache(dev->res.start, resource_size(&dev->res));
885 if (!t->etm_regs[t->etm_regs_count]) {
886 ret = -ENOMEM;
887 goto out_release;
888 }
889
890 amba_set_drvdata(dev, t->etm_regs[t->etm_regs_count]);
891
892 t->flags = TRACER_CYCLE_ACC | TRACER_TRACE_DATA | TRACER_BRANCHOUTPUT;
893 t->etm_portsz = 1;
894 t->etm_contextid_size = 3;
895
896 etm_unlock(t, t->etm_regs_count);
897 (void)etm_readl(t, t->etm_regs_count, ETMMR_PDSR);
898 /* dummy first read */
899 (void)etm_readl(&tracer, t->etm_regs_count, ETMMR_OSSRR);
900
901 etmccr = etm_readl(t, t->etm_regs_count, ETMR_CONFCODE);
902 t->ncmppairs = etmccr & 0xf;
903 if (etmccr & ETMCCR_ETMIDR_PRESENT) {
904 etmidr = etm_readl(t, t->etm_regs_count, ETMR_ID);
905 etm_version = ETMIDR_VERSION(etmidr);
906 if (etm_version >= ETMIDR_VERSION_3_1)
907 etmccer = etm_readl(t, t->etm_regs_count, ETMR_CCE);
908 }
909 etm_writel(t, t->etm_regs_count, 0x441, ETMR_CTRL);
910 etm_writel(t, t->etm_regs_count, new_count, ETMR_TRACEIDR);
911 etm_lock(t, t->etm_regs_count);
912
913 ret = sysfs_create_file(&dev->dev.kobj,
914 &trace_running_attr.attr);
915 if (ret)
916 goto out_unmap;
917
918 /* failing to create any of these two is not fatal */
919 ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
920 if (ret)
921 dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
922
923 ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
924 if (ret)
925 dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
926
927 ret = sysfs_create_file(&dev->dev.kobj,
928 &trace_contextid_size_attr.attr);
929 if (ret)
930 dev_dbg(&dev->dev,
931 "Failed to create trace_contextid_size in sysfs\n");
932
933 ret = sysfs_create_file(&dev->dev.kobj,
934 &trace_branch_output_attr.attr);
935 if (ret)
936 dev_dbg(&dev->dev,
937 "Failed to create trace_branch_output in sysfs\n");
938
939 if (etmccer & ETMCCER_RETURN_STACK_IMPLEMENTED) {
940 ret = sysfs_create_file(&dev->dev.kobj,
941 &trace_return_stack_attr.attr);
942 if (ret)
943 dev_dbg(&dev->dev,
944 "Failed to create trace_return_stack in sysfs\n");
945 }
946
947 if (etmccer & ETMCCER_TIMESTAMPING_IMPLEMENTED) {
948 ret = sysfs_create_file(&dev->dev.kobj,
949 &trace_timestamp_attr.attr);
950 if (ret)
951 dev_dbg(&dev->dev,
952 "Failed to create trace_timestamp in sysfs\n");
953 }
954
955 ret = sysfs_create_file(&dev->dev.kobj, &trace_range_attr.attr);
956 if (ret)
957 dev_dbg(&dev->dev, "Failed to create trace_range in sysfs\n");
958
959 if (etm_version < ETMIDR_VERSION_PFT_1_0) {
960 ret = sysfs_create_file(&dev->dev.kobj,
961 &trace_data_range_attr.attr);
962 if (ret)
963 dev_dbg(&dev->dev,
964 "Failed to create trace_data_range in sysfs\n");
965 } else {
966 tracer.flags &= ~TRACER_TRACE_DATA;
967 }
968
969 dev_dbg(&dev->dev, "ETM AMBA driver initialized.\n");
970
971 /* Enable formatter if there are multiple trace sources */
972 if (new_count > 1)
973 t->etb_fc = ETBFF_ENFCONT | ETBFF_ENFTC;
974
975 t->etm_regs_count = new_count;
976
977 out:
978 mutex_unlock(&t->mutex);
979 return ret;
980
981 out_unmap:
982 iounmap(t->etm_regs[t->etm_regs_count]);
983
984 out_release:
985 amba_release_regions(dev);
986
987 mutex_unlock(&t->mutex);
988 return ret;
989 }
990
etm_remove(struct amba_device * dev)991 static int etm_remove(struct amba_device *dev)
992 {
993 int i;
994 struct tracectx *t = &tracer;
995 void __iomem *etm_regs = amba_get_drvdata(dev);
996
997 sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
998 sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
999 sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
1000 sysfs_remove_file(&dev->dev.kobj, &trace_range_attr.attr);
1001 sysfs_remove_file(&dev->dev.kobj, &trace_data_range_attr.attr);
1002
1003 mutex_lock(&t->mutex);
1004 for (i = 0; i < t->etm_regs_count; i++)
1005 if (t->etm_regs[i] == etm_regs)
1006 break;
1007 for (; i < t->etm_regs_count - 1; i++)
1008 t->etm_regs[i] = t->etm_regs[i + 1];
1009 t->etm_regs_count--;
1010 if (!t->etm_regs_count) {
1011 kfree(t->etm_regs);
1012 t->etm_regs = NULL;
1013 }
1014 mutex_unlock(&t->mutex);
1015
1016 iounmap(etm_regs);
1017 amba_release_regions(dev);
1018
1019 return 0;
1020 }
1021
1022 static struct amba_id etm_ids[] = {
1023 {
1024 .id = 0x0003b921,
1025 .mask = 0x0007ffff,
1026 },
1027 {
1028 .id = 0x0003b950,
1029 .mask = 0x0007ffff,
1030 },
1031 { 0, 0 },
1032 };
1033
1034 static struct amba_driver etm_driver = {
1035 .drv = {
1036 .name = "etm",
1037 .owner = THIS_MODULE,
1038 },
1039 .probe = etm_probe,
1040 .remove = etm_remove,
1041 .id_table = etm_ids,
1042 };
1043
etm_init(void)1044 static int __init etm_init(void)
1045 {
1046 int retval;
1047
1048 mutex_init(&tracer.mutex);
1049
1050 retval = amba_driver_register(&etb_driver);
1051 if (retval) {
1052 printk(KERN_ERR "Failed to register etb\n");
1053 return retval;
1054 }
1055
1056 retval = amba_driver_register(&etm_driver);
1057 if (retval) {
1058 amba_driver_unregister(&etb_driver);
1059 printk(KERN_ERR "Failed to probe etm\n");
1060 return retval;
1061 }
1062
1063 /* not being able to install this handler is not fatal */
1064 (void)register_sysrq_key('v', &sysrq_etm_op);
1065
1066 return 0;
1067 }
1068
1069 device_initcall(etm_init);
1070
1071