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)
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,
320 static struct miscdevice etb_miscdev = {
326 static int __init etb_probe(struct amba_device *dev, struct amba_id *id)
328 struct tracectx *t = &tracer;
331 ret = amba_request_regions(dev, NULL);
335 t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
341 amba_set_drvdata(dev, t);
343 etb_miscdev.parent = &dev->dev;
345 ret = misc_register(&etb_miscdev);
349 t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
350 if (IS_ERR(t->emu_clk)) {
351 dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
355 clk_enable(t->emu_clk);
358 t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
359 dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
361 /* make sure trace capture is disabled */
362 etb_writel(t, 0, ETBR_CTRL);
363 etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
366 dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
372 amba_set_drvdata(dev, NULL);
373 iounmap(t->etb_regs);
376 amba_release_regions(dev);
381 static int etb_remove(struct amba_device *dev)
383 struct tracectx *t = amba_get_drvdata(dev);
385 amba_set_drvdata(dev, NULL);
387 iounmap(t->etb_regs);
390 clk_disable(t->emu_clk);
393 amba_release_regions(dev);
398 static struct amba_id etb_ids[] = {
406 static struct amba_driver etb_driver = {
409 .owner = THIS_MODULE,
412 .remove = etb_remove,
416 /* use a sysfs file "trace_running" to start/stop tracing */
417 static ssize_t trace_running_show(struct kobject *kobj,
418 struct kobj_attribute *attr,
421 return sprintf(buf, "%x\n", trace_isrunning(&tracer));
424 static ssize_t trace_running_store(struct kobject *kobj,
425 struct kobj_attribute *attr,
426 const char *buf, size_t n)
431 if (sscanf(buf, "%u", &value) != 1)
434 mutex_lock(&tracer.mutex);
435 ret = value ? trace_start(&tracer) : trace_stop(&tracer);
436 mutex_unlock(&tracer.mutex);
441 static struct kobj_attribute trace_running_attr =
442 __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
444 static ssize_t trace_info_show(struct kobject *kobj,
445 struct kobj_attribute *attr,
448 u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
452 datalen = etb_getdatalen(&tracer);
453 etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
454 etb_ra = etb_readl(&tracer, ETBR_READADDR);
455 etb_st = etb_readl(&tracer, ETBR_STATUS);
456 etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
460 etm_ctrl = etm_readl(&tracer, ETMR_CTRL);
461 etm_st = etm_readl(&tracer, ETMR_STATUS);
464 return sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
465 "ETBR_WRITEADDR:\t%08x\n"
466 "ETBR_READADDR:\t%08x\n"
467 "ETBR_STATUS:\t%08x\n"
468 "ETBR_FORMATTERCTRL:\t%08x\n"
470 "ETMR_STATUS:\t%08x\n",
482 static struct kobj_attribute trace_info_attr =
483 __ATTR(trace_info, 0444, trace_info_show, NULL);
485 static ssize_t trace_mode_show(struct kobject *kobj,
486 struct kobj_attribute *attr,
489 return sprintf(buf, "%d %d\n",
490 !!(tracer.flags & TRACER_CYCLE_ACC),
494 static ssize_t trace_mode_store(struct kobject *kobj,
495 struct kobj_attribute *attr,
496 const char *buf, size_t n)
498 unsigned int cycacc, portsz;
500 if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
503 mutex_lock(&tracer.mutex);
505 tracer.flags |= TRACER_CYCLE_ACC;
507 tracer.flags &= ~TRACER_CYCLE_ACC;
509 tracer.etm_portsz = portsz & 0x0f;
510 mutex_unlock(&tracer.mutex);
515 static struct kobj_attribute trace_mode_attr =
516 __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
518 static int __init etm_probe(struct amba_device *dev, struct amba_id *id)
520 struct tracectx *t = &tracer;
524 dev_dbg(&dev->dev, "ETM already initialized\n");
529 ret = amba_request_regions(dev, NULL);
533 t->etm_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
539 amba_set_drvdata(dev, t);
541 mutex_init(&t->mutex);
543 t->flags = TRACER_CYCLE_ACC;
547 (void)etm_readl(t, ETMMR_PDSR);
548 /* dummy first read */
549 (void)etm_readl(&tracer, ETMMR_OSSRR);
551 t->ncmppairs = etm_readl(t, ETMR_CONFCODE) & 0xf;
552 etm_writel(t, 0x440, ETMR_CTRL);
555 ret = sysfs_create_file(&dev->dev.kobj,
556 &trace_running_attr.attr);
560 /* failing to create any of these two is not fatal */
561 ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
563 dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
565 ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
567 dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
569 dev_dbg(t->dev, "ETM AMBA driver initialized.\n");
575 amba_set_drvdata(dev, NULL);
576 iounmap(t->etm_regs);
579 amba_release_regions(dev);
584 static int etm_remove(struct amba_device *dev)
586 struct tracectx *t = amba_get_drvdata(dev);
588 amba_set_drvdata(dev, NULL);
590 iounmap(t->etm_regs);
593 amba_release_regions(dev);
595 sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
596 sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
597 sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
602 static struct amba_id etm_ids[] = {
610 static struct amba_driver etm_driver = {
613 .owner = THIS_MODULE,
616 .remove = etm_remove,
620 static int __init etm_init(void)
624 retval = amba_driver_register(&etb_driver);
626 printk(KERN_ERR "Failed to register etb\n");
630 retval = amba_driver_register(&etm_driver);
632 amba_driver_unregister(&etb_driver);
633 printk(KERN_ERR "Failed to probe etm\n");
637 /* not being able to install this handler is not fatal */
638 (void)register_sysrq_key('v', &sysrq_etm_op);
643 device_initcall(etm_init);