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");
37 unsigned int etb_bufsz;
38 void __iomem *etb_regs;
39 void __iomem *etm_regs;
48 static struct tracectx tracer;
50 static inline bool trace_isrunning(struct tracectx *t)
52 return !!(t->flags & TRACER_RUNNING);
55 static int etm_setup_address_range(struct tracectx *t, int n,
56 unsigned long start, unsigned long end, int exclude, int data)
58 u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_NSONLY | \
61 if (n < 1 || n > t->ncmppairs)
64 /* comparators and ranges are numbered starting with 1 as opposed
65 * to bits in a word */
69 flags |= ETMAAT_DLOADSTORE;
71 flags |= ETMAAT_IEXEC;
73 /* first comparator for the range */
74 etm_writel(t, flags, ETMR_COMP_ACC_TYPE(n * 2));
75 etm_writel(t, start, ETMR_COMP_VAL(n * 2));
77 /* second comparator is right next to it */
78 etm_writel(t, flags, ETMR_COMP_ACC_TYPE(n * 2 + 1));
79 etm_writel(t, end, ETMR_COMP_VAL(n * 2 + 1));
81 flags = exclude ? ETMTE_INCLEXCL : 0;
82 etm_writel(t, flags | (1 << n), ETMR_TRACEENCTRL);
87 static int trace_start(struct tracectx *t)
90 unsigned long timeout = TRACER_TIMEOUT;
94 etb_writel(t, 0, ETBR_FORMATTERCTRL);
95 etb_writel(t, 1, ETBR_CTRL);
100 v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
102 if (t->flags & TRACER_CYCLE_ACC)
103 v |= ETMCTRL_CYCLEACCURATE;
107 etm_writel(t, v, ETMR_CTRL);
109 while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
112 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
117 etm_setup_address_range(t, 1, (unsigned long)_stext,
118 (unsigned long)_etext, 0, 0);
119 etm_writel(t, 0, ETMR_TRACEENCTRL2);
120 etm_writel(t, 0, ETMR_TRACESSCTRL);
121 etm_writel(t, 0x6f, ETMR_TRACEENEVT);
123 v &= ~ETMCTRL_PROGRAM;
124 v |= ETMCTRL_PORTSEL;
126 etm_writel(t, v, ETMR_CTRL);
128 timeout = TRACER_TIMEOUT;
129 while (etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
132 dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
139 t->flags |= TRACER_RUNNING;
144 static int trace_stop(struct tracectx *t)
146 unsigned long timeout = TRACER_TIMEOUT;
150 etm_writel(t, 0x440, ETMR_CTRL);
151 while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
154 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
162 etb_writel(t, ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
164 timeout = TRACER_TIMEOUT;
165 while (etb_readl(t, ETBR_FORMATTERCTRL) &
166 ETBFF_MANUAL_FLUSH && --timeout)
169 dev_dbg(t->dev, "Waiting for formatter flush to commence "
175 etb_writel(t, 0, ETBR_CTRL);
179 t->flags &= ~TRACER_RUNNING;
184 static int etb_getdatalen(struct tracectx *t)
189 v = etb_readl(t, ETBR_STATUS);
194 rp = etb_readl(t, ETBR_READADDR);
195 wp = etb_readl(t, ETBR_WRITEADDR);
198 etb_writel(t, 0, ETBR_READADDR);
199 etb_writel(t, 0, ETBR_WRITEADDR);
207 /* sysrq+v will always stop the running trace and leave it at that */
208 static void etm_dump(void)
210 struct tracectx *t = &tracer;
215 printk(KERN_INFO "No tracing hardware found\n");
219 if (trace_isrunning(t))
224 length = etb_getdatalen(t);
226 if (length == t->etb_bufsz)
227 first = etb_readl(t, ETBR_WRITEADDR);
229 etb_writel(t, first, ETBR_READADDR);
231 printk(KERN_INFO "Trace buffer contents length: %d\n", length);
232 printk(KERN_INFO "--- ETB buffer begin ---\n");
233 for (; length; length--)
234 printk("%08x", cpu_to_be32(etb_readl(t, ETBR_READMEM)));
235 printk(KERN_INFO "\n--- ETB buffer end ---\n");
237 /* deassert the overflow bit */
238 etb_writel(t, 1, ETBR_CTRL);
239 etb_writel(t, 0, ETBR_CTRL);
241 etb_writel(t, 0, ETBR_TRIGGERCOUNT);
242 etb_writel(t, 0, ETBR_READADDR);
243 etb_writel(t, 0, ETBR_WRITEADDR);
248 static void sysrq_etm_dump(int key)
250 dev_dbg(tracer.dev, "Dumping ETB buffer\n");
254 static struct sysrq_key_op sysrq_etm_op = {
255 .handler = sysrq_etm_dump,
256 .help_msg = "ETM buffer dump",
260 static int etb_open(struct inode *inode, struct file *file)
262 if (!tracer.etb_regs)
265 file->private_data = &tracer;
267 return nonseekable_open(inode, file);
270 static ssize_t etb_read(struct file *file, char __user *data,
271 size_t len, loff_t *ppos)
275 struct tracectx *t = file->private_data;
279 mutex_lock(&t->mutex);
281 if (trace_isrunning(t)) {
288 total = etb_getdatalen(t);
289 if (total == t->etb_bufsz)
290 first = etb_readl(t, ETBR_WRITEADDR);
292 etb_writel(t, first, ETBR_READADDR);
294 length = min(total * 4, (int)len);
295 buf = vmalloc(length);
297 dev_dbg(t->dev, "ETB buffer length: %d\n", total);
298 dev_dbg(t->dev, "ETB status reg: %x\n", etb_readl(t, ETBR_STATUS));
299 for (i = 0; i < length / 4; i++)
300 buf[i] = etb_readl(t, ETBR_READMEM);
302 /* the only way to deassert overflow bit in ETB status is this */
303 etb_writel(t, 1, ETBR_CTRL);
304 etb_writel(t, 0, ETBR_CTRL);
306 etb_writel(t, 0, ETBR_WRITEADDR);
307 etb_writel(t, 0, ETBR_READADDR);
308 etb_writel(t, 0, ETBR_TRIGGERCOUNT);
312 length -= copy_to_user(data, buf, length);
316 mutex_unlock(&t->mutex);
321 static int etb_release(struct inode *inode, struct file *file)
323 /* there's nothing to do here, actually */
327 static const struct file_operations etb_fops = {
328 .owner = THIS_MODULE,
331 .release = etb_release,
334 static struct miscdevice etb_miscdev = {
340 static int __init etb_probe(struct amba_device *dev, struct amba_id *id)
342 struct tracectx *t = &tracer;
345 ret = amba_request_regions(dev, NULL);
349 t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
355 amba_set_drvdata(dev, t);
357 etb_miscdev.parent = &dev->dev;
359 ret = misc_register(&etb_miscdev);
363 t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
364 if (IS_ERR(t->emu_clk)) {
365 dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
369 clk_enable(t->emu_clk);
372 t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
373 dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
375 /* make sure trace capture is disabled */
376 etb_writel(t, 0, ETBR_CTRL);
377 etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
380 dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
386 amba_set_drvdata(dev, NULL);
387 iounmap(t->etb_regs);
390 amba_release_regions(dev);
395 static int etb_remove(struct amba_device *dev)
397 struct tracectx *t = amba_get_drvdata(dev);
399 amba_set_drvdata(dev, NULL);
401 iounmap(t->etb_regs);
404 clk_disable(t->emu_clk);
407 amba_release_regions(dev);
412 static struct amba_id etb_ids[] = {
420 static struct amba_driver etb_driver = {
423 .owner = THIS_MODULE,
426 .remove = etb_remove,
430 /* use a sysfs file "trace_running" to start/stop tracing */
431 static ssize_t trace_running_show(struct kobject *kobj,
432 struct kobj_attribute *attr,
435 return sprintf(buf, "%x\n", trace_isrunning(&tracer));
438 static ssize_t trace_running_store(struct kobject *kobj,
439 struct kobj_attribute *attr,
440 const char *buf, size_t n)
445 if (sscanf(buf, "%u", &value) != 1)
448 mutex_lock(&tracer.mutex);
449 ret = value ? trace_start(&tracer) : trace_stop(&tracer);
450 mutex_unlock(&tracer.mutex);
455 static struct kobj_attribute trace_running_attr =
456 __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
458 static ssize_t trace_info_show(struct kobject *kobj,
459 struct kobj_attribute *attr,
462 u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
466 datalen = etb_getdatalen(&tracer);
467 etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
468 etb_ra = etb_readl(&tracer, ETBR_READADDR);
469 etb_st = etb_readl(&tracer, ETBR_STATUS);
470 etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
474 etm_ctrl = etm_readl(&tracer, ETMR_CTRL);
475 etm_st = etm_readl(&tracer, ETMR_STATUS);
478 return sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
479 "ETBR_WRITEADDR:\t%08x\n"
480 "ETBR_READADDR:\t%08x\n"
481 "ETBR_STATUS:\t%08x\n"
482 "ETBR_FORMATTERCTRL:\t%08x\n"
484 "ETMR_STATUS:\t%08x\n",
496 static struct kobj_attribute trace_info_attr =
497 __ATTR(trace_info, 0444, trace_info_show, NULL);
499 static ssize_t trace_mode_show(struct kobject *kobj,
500 struct kobj_attribute *attr,
503 return sprintf(buf, "%d %d\n",
504 !!(tracer.flags & TRACER_CYCLE_ACC),
508 static ssize_t trace_mode_store(struct kobject *kobj,
509 struct kobj_attribute *attr,
510 const char *buf, size_t n)
512 unsigned int cycacc, portsz;
514 if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
517 mutex_lock(&tracer.mutex);
519 tracer.flags |= TRACER_CYCLE_ACC;
521 tracer.flags &= ~TRACER_CYCLE_ACC;
523 tracer.etm_portsz = portsz & 0x0f;
524 mutex_unlock(&tracer.mutex);
529 static struct kobj_attribute trace_mode_attr =
530 __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
532 static int __init etm_probe(struct amba_device *dev, struct amba_id *id)
534 struct tracectx *t = &tracer;
538 dev_dbg(&dev->dev, "ETM already initialized\n");
543 ret = amba_request_regions(dev, NULL);
547 t->etm_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
553 amba_set_drvdata(dev, t);
555 mutex_init(&t->mutex);
557 t->flags = TRACER_CYCLE_ACC;
561 (void)etm_readl(t, ETMMR_PDSR);
562 /* dummy first read */
563 (void)etm_readl(&tracer, ETMMR_OSSRR);
565 t->ncmppairs = etm_readl(t, ETMR_CONFCODE) & 0xf;
566 etm_writel(t, 0x440, ETMR_CTRL);
569 ret = sysfs_create_file(&dev->dev.kobj,
570 &trace_running_attr.attr);
574 /* failing to create any of these two is not fatal */
575 ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
577 dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
579 ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
581 dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
583 dev_dbg(t->dev, "ETM AMBA driver initialized.\n");
589 amba_set_drvdata(dev, NULL);
590 iounmap(t->etm_regs);
593 amba_release_regions(dev);
598 static int etm_remove(struct amba_device *dev)
600 struct tracectx *t = amba_get_drvdata(dev);
602 amba_set_drvdata(dev, NULL);
604 iounmap(t->etm_regs);
607 amba_release_regions(dev);
609 sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
610 sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
611 sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
616 static struct amba_id etm_ids[] = {
624 static struct amba_driver etm_driver = {
627 .owner = THIS_MODULE,
630 .remove = etm_remove,
634 static int __init etm_init(void)
638 retval = amba_driver_register(&etb_driver);
640 printk(KERN_ERR "Failed to register etb\n");
644 retval = amba_driver_register(&etm_driver);
646 amba_driver_unregister(&etb_driver);
647 printk(KERN_ERR "Failed to probe etm\n");
651 /* not being able to install this handler is not fatal */
652 (void)register_sysrq_key('v', &sysrq_etm_op);
657 device_initcall(etm_init);