2 * linux/arch/arm/kernel/etm.c
4 * Driver for ARM's Embedded Trace Macrocell and Embedded Trace Buffer.
6 * Copyright (C) 2009 Nokia Corporation.
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.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
18 #include <linux/sysrq.h>
19 #include <linux/device.h>
20 #include <linux/clk.h>
21 #include <linux/amba/bus.h>
23 #include <linux/uaccess.h>
24 #include <linux/miscdevice.h>
25 #include <linux/vmalloc.h>
26 #include <linux/mutex.h>
27 #include <asm/hardware/coresight.h>
28 #include <asm/sections.h>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Alexander Shishkin");
33 static struct tracectx tracer;
35 static inline bool trace_isrunning(struct tracectx *t)
37 return !!(t->flags & TRACER_RUNNING);
40 static int etm_setup_address_range(struct tracectx *t, int n,
41 unsigned long start, unsigned long end, int exclude, int data)
43 u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_NSONLY | \
46 if (n < 1 || n > t->ncmppairs)
49 /* comparators and ranges are numbered starting with 1 as opposed
50 * to bits in a word */
54 flags |= ETMAAT_DLOADSTORE;
56 flags |= ETMAAT_IEXEC;
58 /* first comparator for the range */
59 etm_writel(t, flags, ETMR_COMP_ACC_TYPE(n * 2));
60 etm_writel(t, start, ETMR_COMP_VAL(n * 2));
62 /* second comparator is right next to it */
63 etm_writel(t, flags, ETMR_COMP_ACC_TYPE(n * 2 + 1));
64 etm_writel(t, end, ETMR_COMP_VAL(n * 2 + 1));
66 flags = exclude ? ETMTE_INCLEXCL : 0;
67 etm_writel(t, flags | (1 << n), ETMR_TRACEENCTRL);
72 static int trace_start(struct tracectx *t)
75 unsigned long timeout = TRACER_TIMEOUT;
79 etb_writel(t, 0, ETBR_FORMATTERCTRL);
80 etb_writel(t, 1, ETBR_CTRL);
85 v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
87 if (t->flags & TRACER_CYCLE_ACC)
88 v |= ETMCTRL_CYCLEACCURATE;
92 etm_writel(t, v, ETMR_CTRL);
94 while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
97 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
102 etm_setup_address_range(t, 1, (unsigned long)_stext,
103 (unsigned long)_etext, 0, 0);
104 etm_writel(t, 0, ETMR_TRACEENCTRL2);
105 etm_writel(t, 0, ETMR_TRACESSCTRL);
106 etm_writel(t, 0x6f, ETMR_TRACEENEVT);
108 v &= ~ETMCTRL_PROGRAM;
109 v |= ETMCTRL_PORTSEL;
111 etm_writel(t, v, ETMR_CTRL);
113 timeout = TRACER_TIMEOUT;
114 while (etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
117 dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
124 t->flags |= TRACER_RUNNING;
129 static int trace_stop(struct tracectx *t)
131 unsigned long timeout = TRACER_TIMEOUT;
135 etm_writel(t, 0x440, ETMR_CTRL);
136 while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
139 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
147 etb_writel(t, ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
149 timeout = TRACER_TIMEOUT;
150 while (etb_readl(t, ETBR_FORMATTERCTRL) &
151 ETBFF_MANUAL_FLUSH && --timeout)
154 dev_dbg(t->dev, "Waiting for formatter flush to commence "
160 etb_writel(t, 0, ETBR_CTRL);
164 t->flags &= ~TRACER_RUNNING;
169 static int etb_getdatalen(struct tracectx *t)
174 v = etb_readl(t, ETBR_STATUS);
179 rp = etb_readl(t, ETBR_READADDR);
180 wp = etb_readl(t, ETBR_WRITEADDR);
183 etb_writel(t, 0, ETBR_READADDR);
184 etb_writel(t, 0, ETBR_WRITEADDR);
192 /* sysrq+v will always stop the running trace and leave it at that */
193 static void etm_dump(void)
195 struct tracectx *t = &tracer;
200 printk(KERN_INFO "No tracing hardware found\n");
204 if (trace_isrunning(t))
209 length = etb_getdatalen(t);
211 if (length == t->etb_bufsz)
212 first = etb_readl(t, ETBR_WRITEADDR);
214 etb_writel(t, first, ETBR_READADDR);
216 printk(KERN_INFO "Trace buffer contents length: %d\n", length);
217 printk(KERN_INFO "--- ETB buffer begin ---\n");
218 for (; length; length--)
219 printk("%08x", cpu_to_be32(etb_readl(t, ETBR_READMEM)));
220 printk(KERN_INFO "\n--- ETB buffer end ---\n");
222 /* deassert the overflow bit */
223 etb_writel(t, 1, ETBR_CTRL);
224 etb_writel(t, 0, ETBR_CTRL);
226 etb_writel(t, 0, ETBR_TRIGGERCOUNT);
227 etb_writel(t, 0, ETBR_READADDR);
228 etb_writel(t, 0, ETBR_WRITEADDR);
233 static void sysrq_etm_dump(int key, struct tty_struct *tty)
235 dev_dbg(tracer.dev, "Dumping ETB buffer\n");
239 static struct sysrq_key_op sysrq_etm_op = {
240 .handler = sysrq_etm_dump,
241 .help_msg = "ETM buffer dump",
245 static int etb_open(struct inode *inode, struct file *file)
247 if (!tracer.etb_regs)
250 file->private_data = &tracer;
252 return nonseekable_open(inode, file);
255 static ssize_t etb_read(struct file *file, char __user *data,
256 size_t len, loff_t *ppos)
260 struct tracectx *t = file->private_data;
264 mutex_lock(&t->mutex);
266 if (trace_isrunning(t)) {
273 total = etb_getdatalen(t);
274 if (total == t->etb_bufsz)
275 first = etb_readl(t, ETBR_WRITEADDR);
277 etb_writel(t, first, ETBR_READADDR);
279 length = min(total * 4, (int)len);
280 buf = vmalloc(length);
282 dev_dbg(t->dev, "ETB buffer length: %d\n", total);
283 dev_dbg(t->dev, "ETB status reg: %x\n", etb_readl(t, ETBR_STATUS));
284 for (i = 0; i < length / 4; i++)
285 buf[i] = etb_readl(t, ETBR_READMEM);
287 /* the only way to deassert overflow bit in ETB status is this */
288 etb_writel(t, 1, ETBR_CTRL);
289 etb_writel(t, 0, ETBR_CTRL);
291 etb_writel(t, 0, ETBR_WRITEADDR);
292 etb_writel(t, 0, ETBR_READADDR);
293 etb_writel(t, 0, ETBR_TRIGGERCOUNT);
297 length -= copy_to_user(data, buf, length);
301 mutex_unlock(&t->mutex);
306 static int etb_release(struct inode *inode, struct file *file)
308 /* there's nothing to do here, actually */
312 static const struct file_operations etb_fops = {
313 .owner = THIS_MODULE,
316 .release = etb_release,
319 static struct miscdevice etb_miscdev = {
325 static int __init etb_probe(struct amba_device *dev, struct amba_id *id)
327 struct tracectx *t = &tracer;
330 ret = amba_request_regions(dev, NULL);
334 t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
340 amba_set_drvdata(dev, t);
342 etb_miscdev.parent = &dev->dev;
344 ret = misc_register(&etb_miscdev);
348 t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
349 if (IS_ERR(t->emu_clk)) {
350 dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
354 clk_enable(t->emu_clk);
357 t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
358 dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
360 /* make sure trace capture is disabled */
361 etb_writel(t, 0, ETBR_CTRL);
362 etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
365 dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
371 amba_set_drvdata(dev, NULL);
372 iounmap(t->etb_regs);
375 amba_release_regions(dev);
380 static int etb_remove(struct amba_device *dev)
382 struct tracectx *t = amba_get_drvdata(dev);
384 amba_set_drvdata(dev, NULL);
386 iounmap(t->etb_regs);
389 clk_disable(t->emu_clk);
392 amba_release_regions(dev);
397 static struct amba_id etb_ids[] = {
405 static struct amba_driver etb_driver = {
408 .owner = THIS_MODULE,
411 .remove = etb_remove,
415 /* use a sysfs file "trace_running" to start/stop tracing */
416 static ssize_t trace_running_show(struct kobject *kobj,
417 struct kobj_attribute *attr,
420 return sprintf(buf, "%x\n", trace_isrunning(&tracer));
423 static ssize_t trace_running_store(struct kobject *kobj,
424 struct kobj_attribute *attr,
425 const char *buf, size_t n)
430 if (sscanf(buf, "%u", &value) != 1)
433 mutex_lock(&tracer.mutex);
434 ret = value ? trace_start(&tracer) : trace_stop(&tracer);
435 mutex_unlock(&tracer.mutex);
440 static struct kobj_attribute trace_running_attr =
441 __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
443 static ssize_t trace_info_show(struct kobject *kobj,
444 struct kobj_attribute *attr,
447 u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
451 datalen = etb_getdatalen(&tracer);
452 etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
453 etb_ra = etb_readl(&tracer, ETBR_READADDR);
454 etb_st = etb_readl(&tracer, ETBR_STATUS);
455 etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
459 etm_ctrl = etm_readl(&tracer, ETMR_CTRL);
460 etm_st = etm_readl(&tracer, ETMR_STATUS);
463 return sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
464 "ETBR_WRITEADDR:\t%08x\n"
465 "ETBR_READADDR:\t%08x\n"
466 "ETBR_STATUS:\t%08x\n"
467 "ETBR_FORMATTERCTRL:\t%08x\n"
469 "ETMR_STATUS:\t%08x\n",
481 static struct kobj_attribute trace_info_attr =
482 __ATTR(trace_info, 0444, trace_info_show, NULL);
484 static ssize_t trace_mode_show(struct kobject *kobj,
485 struct kobj_attribute *attr,
488 return sprintf(buf, "%d %d\n",
489 !!(tracer.flags & TRACER_CYCLE_ACC),
493 static ssize_t trace_mode_store(struct kobject *kobj,
494 struct kobj_attribute *attr,
495 const char *buf, size_t n)
497 unsigned int cycacc, portsz;
499 if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
502 mutex_lock(&tracer.mutex);
504 tracer.flags |= TRACER_CYCLE_ACC;
506 tracer.flags &= ~TRACER_CYCLE_ACC;
508 tracer.etm_portsz = portsz & 0x0f;
509 mutex_unlock(&tracer.mutex);
514 static struct kobj_attribute trace_mode_attr =
515 __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
517 static int __init etm_probe(struct amba_device *dev, struct amba_id *id)
519 struct tracectx *t = &tracer;
523 dev_dbg(&dev->dev, "ETM already initialized\n");
528 ret = amba_request_regions(dev, NULL);
532 t->etm_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
538 amba_set_drvdata(dev, t);
540 mutex_init(&t->mutex);
542 t->flags = TRACER_CYCLE_ACC;
546 ret = etm_readl(t, CSCR_PRSR);
548 t->ncmppairs = etm_readl(t, ETMR_CONFCODE) & 0xf;
549 etm_writel(t, 0x440, ETMR_CTRL);
552 ret = sysfs_create_file(&dev->dev.kobj,
553 &trace_running_attr.attr);
557 /* failing to create any of these two is not fatal */
558 ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
560 dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
562 ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
564 dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
566 dev_dbg(t->dev, "ETM AMBA driver initialized.\n");
572 amba_set_drvdata(dev, NULL);
573 iounmap(t->etm_regs);
576 amba_release_regions(dev);
581 static int etm_remove(struct amba_device *dev)
583 struct tracectx *t = amba_get_drvdata(dev);
585 amba_set_drvdata(dev, NULL);
587 iounmap(t->etm_regs);
590 amba_release_regions(dev);
592 sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
593 sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
594 sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
599 static struct amba_id etm_ids[] = {
607 static struct amba_driver etm_driver = {
610 .owner = THIS_MODULE,
613 .remove = etm_remove,
617 static int __init etm_init(void)
621 retval = amba_driver_register(&etb_driver);
623 printk(KERN_ERR "Failed to register etb\n");
627 retval = amba_driver_register(&etm_driver);
629 amba_driver_unregister(&etb_driver);
630 printk(KERN_ERR "Failed to probe etm\n");
634 /* not being able to install this handler is not fatal */
635 (void)register_sysrq_key('v', &sysrq_etm_op);
640 device_initcall(etm_init);