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/slab.h>
19 #include <linux/sysrq.h>
20 #include <linux/device.h>
21 #include <linux/clk.h>
22 #include <linux/amba/bus.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>
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Alexander Shishkin");
39 unsigned int etb_bufsz;
40 void __iomem *etb_regs;
41 void __iomem **etm_regs;
46 int etm_contextid_size;
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;
58 static struct tracectx tracer = {
59 .range_start = (unsigned long)_stext,
60 .range_end = (unsigned long)_etext,
63 static inline bool trace_isrunning(struct tracectx *t)
65 return !!(t->flags & TRACER_RUNNING);
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)
71 u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_IGNSECURITY |
74 if (n < 1 || n > t->ncmppairs)
77 /* comparators and ranges are numbered starting with 1 as opposed
78 * to bits in a word */
82 flags |= ETMAAT_DLOADSTORE;
84 flags |= ETMAAT_IEXEC;
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));
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));
95 flags = exclude ? ETMVDC3_EXCLONLY : 0;
98 etm_writel(t, id, flags | BIT(n), ETMR_VIEWDATACTRL3);
100 flags = exclude ? ETMTE_INCLEXCL : 0;
101 etm_writel(t, id, flags | (1 << n), ETMR_TRACEENCTRL);
107 static int trace_start_etm(struct tracectx *t, int id)
110 unsigned long timeout = TRACER_TIMEOUT;
112 v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
113 v |= ETMCTRL_CONTEXTIDSIZE(t->etm_contextid_size);
115 if (t->flags & TRACER_CYCLE_ACC)
116 v |= ETMCTRL_CYCLEACCURATE;
118 if (t->flags & TRACER_BRANCHOUTPUT)
119 v |= ETMCTRL_BRANCH_OUTPUT;
121 if (t->flags & TRACER_TRACE_DATA)
122 v |= ETMCTRL_DATA_DO_ADDR;
124 if (t->flags & TRACER_TIMESTAMP)
125 v |= ETMCTRL_TIMESTAMP_EN;
127 if (t->flags & TRACER_RETURN_STACK)
128 v |= ETMCTRL_RETURN_STACK_EN;
132 etm_writel(t, id, v, ETMR_CTRL);
134 while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
137 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
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);
146 etm_writel(t, id, ETMTE_INCLEXCL, ETMR_TRACEENCTRL);
148 etm_writel(t, id, 0, ETMR_TRACEENCTRL2);
149 etm_writel(t, id, 0, ETMR_TRACESSCTRL);
150 etm_writel(t, id, 0x6f, ETMR_TRACEENEVT);
152 etm_writel(t, id, 0, ETMR_VIEWDATACTRL1);
153 etm_writel(t, id, 0, ETMR_VIEWDATACTRL2);
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);
159 etm_writel(t, id, ETMVDC3_EXCLONLY, ETMR_VIEWDATACTRL3);
161 etm_writel(t, id, 0x6f, ETMR_VIEWDATAEVT);
163 v &= ~ETMCTRL_PROGRAM;
164 v |= ETMCTRL_PORTSEL;
166 etm_writel(t, id, v, ETMR_CTRL);
168 timeout = TRACER_TIMEOUT;
169 while (etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
172 dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
181 static int trace_start(struct tracectx *t)
185 u32 etb_fc = t->etb_fc;
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);
196 /* configure etm(s) */
197 for (id = 0; id < t->etm_regs_count; id++) {
198 ret = trace_start_etm(t, id);
203 t->flags |= TRACER_RUNNING;
208 static int trace_stop_etm(struct tracectx *t, int id)
210 unsigned long timeout = TRACER_TIMEOUT;
214 etm_writel(t, id, 0x440, ETMR_CTRL);
215 while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
219 "etm%d: Waiting for progbit to assert timed out\n",
229 static int trace_power_down_etm(struct tracectx *t, int id)
231 unsigned long timeout = TRACER_TIMEOUT;
233 while (!(etm_readl(t, id, ETMR_STATUS) & ETMST_PROGBIT) && --timeout)
236 dev_err(t->dev, "etm%d: Waiting for status progbit to assert timed out\n",
242 etm_writel(t, id, 0x441, ETMR_CTRL);
248 static int trace_stop(struct tracectx *t)
251 unsigned long timeout = TRACER_TIMEOUT;
252 u32 etb_fc = t->etb_fc;
254 for (id = 0; id < t->etm_regs_count; id++)
255 trace_stop_etm(t, id);
257 for (id = 0; id < t->etm_regs_count; id++)
258 trace_power_down_etm(t, id);
262 etb_fc |= ETBFF_STOPFL;
263 etb_writel(t, t->etb_fc, ETBR_FORMATTERCTRL);
265 etb_writel(t, etb_fc | ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
267 timeout = TRACER_TIMEOUT;
268 while (etb_readl(t, ETBR_FORMATTERCTRL) &
269 ETBFF_MANUAL_FLUSH && --timeout)
272 dev_dbg(t->dev, "Waiting for formatter flush to commence "
278 etb_writel(t, 0, ETBR_CTRL);
282 t->flags &= ~TRACER_RUNNING;
287 static int etb_getdatalen(struct tracectx *t)
292 v = etb_readl(t, ETBR_STATUS);
297 wp = etb_readl(t, ETBR_WRITEADDR);
301 /* sysrq+v will always stop the running trace and leave it at that */
302 static void etm_dump(void)
304 struct tracectx *t = &tracer;
309 printk(KERN_INFO "No tracing hardware found\n");
313 if (trace_isrunning(t))
318 length = etb_getdatalen(t);
320 if (length == t->etb_bufsz)
321 first = etb_readl(t, ETBR_WRITEADDR);
323 etb_writel(t, first, ETBR_READADDR);
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");
334 static void sysrq_etm_dump(int key)
336 if (!mutex_trylock(&tracer.mutex)) {
337 printk(KERN_INFO "Tracing hardware busy\n");
340 dev_dbg(tracer.dev, "Dumping ETB buffer\n");
342 mutex_unlock(&tracer.mutex);
345 static struct sysrq_key_op sysrq_etm_op = {
346 .handler = sysrq_etm_dump,
347 .help_msg = "etm-buffer-dump(v)",
351 static int etb_open(struct inode *inode, struct file *file)
353 if (!tracer.etb_regs)
356 file->private_data = &tracer;
358 return nonseekable_open(inode, file);
361 static ssize_t etb_read(struct file *file, char __user *data,
362 size_t len, loff_t *ppos)
366 struct tracectx *t = file->private_data;
374 mutex_lock(&t->mutex);
376 if (trace_isrunning(t)) {
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);
389 if (pos > total * 4) {
397 first = (first + wpos) % t->etb_bufsz;
399 etb_writel(t, first, ETBR_READADDR);
401 wlength = min(total, DIV_ROUND_UP(skip + (int)len, 4));
402 length = min(total * 4 - skip, (int)len);
403 buf = vmalloc(wlength * 4);
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);
414 length -= copy_to_user(data, (u8 *)buf + skip, length);
416 *ppos = pos + length;
419 mutex_unlock(&t->mutex);
424 static int etb_release(struct inode *inode, struct file *file)
426 /* there's nothing to do here, actually */
430 static const struct file_operations etb_fops = {
431 .owner = THIS_MODULE,
434 .release = etb_release,
438 static struct miscdevice etb_miscdev = {
444 static int etb_probe(struct amba_device *dev, const struct amba_id *id)
446 struct tracectx *t = &tracer;
449 ret = amba_request_regions(dev, NULL);
453 mutex_lock(&t->mutex);
454 t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
461 t->dump_initial_etb = true;
462 amba_set_drvdata(dev, t);
465 t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
466 dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
468 /* make sure trace capture is disabled */
469 etb_writel(t, 0, ETBR_CTRL);
470 etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
472 mutex_unlock(&t->mutex);
474 etb_miscdev.parent = &dev->dev;
476 ret = misc_register(&etb_miscdev);
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");
485 clk_enable(t->emu_clk);
487 dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
493 mutex_lock(&t->mutex);
494 amba_set_drvdata(dev, NULL);
495 iounmap(t->etb_regs);
499 mutex_unlock(&t->mutex);
500 amba_release_regions(dev);
505 static int etb_remove(struct amba_device *dev)
507 struct tracectx *t = amba_get_drvdata(dev);
509 amba_set_drvdata(dev, NULL);
511 iounmap(t->etb_regs);
514 if (!IS_ERR(t->emu_clk)) {
515 clk_disable(t->emu_clk);
519 amba_release_regions(dev);
524 static struct amba_id etb_ids[] = {
532 static struct amba_driver etb_driver = {
535 .owner = THIS_MODULE,
538 .remove = etb_remove,
542 /* use a sysfs file "trace_running" to start/stop tracing */
543 static ssize_t trace_running_show(struct kobject *kobj,
544 struct kobj_attribute *attr,
547 return sprintf(buf, "%x\n", trace_isrunning(&tracer));
550 static ssize_t trace_running_store(struct kobject *kobj,
551 struct kobj_attribute *attr,
552 const char *buf, size_t n)
557 if (sscanf(buf, "%u", &value) != 1)
560 mutex_lock(&tracer.mutex);
561 if (!tracer.etb_regs)
564 ret = value ? trace_start(&tracer) : trace_stop(&tracer);
565 mutex_unlock(&tracer.mutex);
570 static struct kobj_attribute trace_running_attr =
571 __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
573 static ssize_t trace_info_show(struct kobject *kobj,
574 struct kobj_attribute *attr,
577 u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
582 mutex_lock(&tracer.mutex);
583 if (tracer.etb_regs) {
585 datalen = etb_getdatalen(&tracer);
586 etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
587 etb_ra = etb_readl(&tracer, ETBR_READADDR);
588 etb_st = etb_readl(&tracer, ETBR_STATUS);
589 etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
592 etb_wa = etb_ra = etb_st = etb_fc = ~0;
596 ret = sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
597 "ETBR_WRITEADDR:\t%08x\n"
598 "ETBR_READADDR:\t%08x\n"
599 "ETBR_STATUS:\t%08x\n"
600 "ETBR_FORMATTERCTRL:\t%08x\n",
609 for (id = 0; id < tracer.etm_regs_count; id++) {
610 etm_unlock(&tracer, id);
611 etm_ctrl = etm_readl(&tracer, id, ETMR_CTRL);
612 etm_st = etm_readl(&tracer, id, ETMR_STATUS);
613 etm_lock(&tracer, id);
614 ret += sprintf(buf + ret, "ETMR_CTRL:\t%08x\n"
615 "ETMR_STATUS:\t%08x\n",
620 mutex_unlock(&tracer.mutex);
625 static struct kobj_attribute trace_info_attr =
626 __ATTR(trace_info, 0444, trace_info_show, NULL);
628 static ssize_t trace_mode_show(struct kobject *kobj,
629 struct kobj_attribute *attr,
632 return sprintf(buf, "%d %d\n",
633 !!(tracer.flags & TRACER_CYCLE_ACC),
637 static ssize_t trace_mode_store(struct kobject *kobj,
638 struct kobj_attribute *attr,
639 const char *buf, size_t n)
641 unsigned int cycacc, portsz;
643 if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
646 mutex_lock(&tracer.mutex);
648 tracer.flags |= TRACER_CYCLE_ACC;
650 tracer.flags &= ~TRACER_CYCLE_ACC;
652 tracer.etm_portsz = portsz & 0x0f;
653 mutex_unlock(&tracer.mutex);
658 static struct kobj_attribute trace_mode_attr =
659 __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
661 static ssize_t trace_contextid_size_show(struct kobject *kobj,
662 struct kobj_attribute *attr,
665 /* 0: No context id tracing, 1: One byte, 2: Two bytes, 3: Four bytes */
666 return sprintf(buf, "%d\n", (1 << tracer.etm_contextid_size) >> 1);
669 static ssize_t trace_contextid_size_store(struct kobject *kobj,
670 struct kobj_attribute *attr,
671 const char *buf, size_t n)
673 unsigned int contextid_size;
675 if (sscanf(buf, "%u", &contextid_size) != 1)
678 if (contextid_size == 3 || contextid_size > 4)
681 mutex_lock(&tracer.mutex);
682 tracer.etm_contextid_size = fls(contextid_size);
683 mutex_unlock(&tracer.mutex);
688 static struct kobj_attribute trace_contextid_size_attr =
689 __ATTR(trace_contextid_size, 0644,
690 trace_contextid_size_show, trace_contextid_size_store);
692 static ssize_t trace_branch_output_show(struct kobject *kobj,
693 struct kobj_attribute *attr,
696 return sprintf(buf, "%d\n", !!(tracer.flags & TRACER_BRANCHOUTPUT));
699 static ssize_t trace_branch_output_store(struct kobject *kobj,
700 struct kobj_attribute *attr,
701 const char *buf, size_t n)
703 unsigned int branch_output;
705 if (sscanf(buf, "%u", &branch_output) != 1)
708 mutex_lock(&tracer.mutex);
710 tracer.flags |= TRACER_BRANCHOUTPUT;
711 /* Branch broadcasting is incompatible with the return stack */
712 tracer.flags &= ~TRACER_RETURN_STACK;
714 tracer.flags &= ~TRACER_BRANCHOUTPUT;
716 mutex_unlock(&tracer.mutex);
721 static struct kobj_attribute trace_branch_output_attr =
722 __ATTR(trace_branch_output, 0644,
723 trace_branch_output_show, trace_branch_output_store);
725 static ssize_t trace_return_stack_show(struct kobject *kobj,
726 struct kobj_attribute *attr,
729 return sprintf(buf, "%d\n", !!(tracer.flags & TRACER_RETURN_STACK));
732 static ssize_t trace_return_stack_store(struct kobject *kobj,
733 struct kobj_attribute *attr,
734 const char *buf, size_t n)
736 unsigned int return_stack;
738 if (sscanf(buf, "%u", &return_stack) != 1)
741 mutex_lock(&tracer.mutex);
743 tracer.flags |= TRACER_RETURN_STACK;
744 /* Return stack is incompatible with branch broadcasting */
745 tracer.flags &= ~TRACER_BRANCHOUTPUT;
747 tracer.flags &= ~TRACER_RETURN_STACK;
749 mutex_unlock(&tracer.mutex);
754 static struct kobj_attribute trace_return_stack_attr =
755 __ATTR(trace_return_stack, 0644,
756 trace_return_stack_show, trace_return_stack_store);
758 static ssize_t trace_timestamp_show(struct kobject *kobj,
759 struct kobj_attribute *attr,
762 return sprintf(buf, "%d\n", !!(tracer.flags & TRACER_TIMESTAMP));
765 static ssize_t trace_timestamp_store(struct kobject *kobj,
766 struct kobj_attribute *attr,
767 const char *buf, size_t n)
769 unsigned int timestamp;
771 if (sscanf(buf, "%u", ×tamp) != 1)
774 mutex_lock(&tracer.mutex);
776 tracer.flags |= TRACER_TIMESTAMP;
778 tracer.flags &= ~TRACER_TIMESTAMP;
779 mutex_unlock(&tracer.mutex);
784 static struct kobj_attribute trace_timestamp_attr =
785 __ATTR(trace_timestamp, 0644,
786 trace_timestamp_show, trace_timestamp_store);
788 static ssize_t trace_range_show(struct kobject *kobj,
789 struct kobj_attribute *attr,
792 return sprintf(buf, "%08lx %08lx\n",
793 tracer.range_start, tracer.range_end);
796 static ssize_t trace_range_store(struct kobject *kobj,
797 struct kobj_attribute *attr,
798 const char *buf, size_t n)
800 unsigned long range_start, range_end;
802 if (sscanf(buf, "%lx %lx", &range_start, &range_end) != 2)
805 mutex_lock(&tracer.mutex);
806 tracer.range_start = range_start;
807 tracer.range_end = range_end;
808 mutex_unlock(&tracer.mutex);
814 static struct kobj_attribute trace_range_attr =
815 __ATTR(trace_range, 0644, trace_range_show, trace_range_store);
817 static ssize_t trace_data_range_show(struct kobject *kobj,
818 struct kobj_attribute *attr,
821 unsigned long range_start;
823 mutex_lock(&tracer.mutex);
824 range_start = tracer.data_range_start;
825 range_end = tracer.data_range_end;
826 if (!range_end && (tracer.flags & TRACER_TRACE_DATA))
827 range_end = 0x100000000ULL;
828 mutex_unlock(&tracer.mutex);
829 return sprintf(buf, "%08lx %08llx\n", range_start, range_end);
832 static ssize_t trace_data_range_store(struct kobject *kobj,
833 struct kobj_attribute *attr,
834 const char *buf, size_t n)
836 unsigned long range_start;
839 if (sscanf(buf, "%lx %llx", &range_start, &range_end) != 2)
842 mutex_lock(&tracer.mutex);
843 tracer.data_range_start = range_start;
844 tracer.data_range_end = (unsigned long)range_end;
846 tracer.flags |= TRACER_TRACE_DATA;
848 tracer.flags &= ~TRACER_TRACE_DATA;
849 mutex_unlock(&tracer.mutex);
855 static struct kobj_attribute trace_data_range_attr =
856 __ATTR(trace_data_range, 0644,
857 trace_data_range_show, trace_data_range_store);
859 static int etm_probe(struct amba_device *dev, const struct amba_id *id)
861 struct tracectx *t = &tracer;
863 void __iomem **new_regs;
870 mutex_lock(&t->mutex);
871 new_count = t->etm_regs_count + 1;
872 new_regs = krealloc(t->etm_regs,
873 sizeof(t->etm_regs[0]) * new_count, GFP_KERNEL);
876 dev_dbg(&dev->dev, "Failed to allocate ETM register array\n");
880 t->etm_regs = new_regs;
882 ret = amba_request_regions(dev, NULL);
886 t->etm_regs[t->etm_regs_count] =
887 ioremap_nocache(dev->res.start, resource_size(&dev->res));
888 if (!t->etm_regs[t->etm_regs_count]) {
893 amba_set_drvdata(dev, t->etm_regs[t->etm_regs_count]);
895 t->flags = TRACER_CYCLE_ACC | TRACER_TRACE_DATA | TRACER_BRANCHOUTPUT;
897 t->etm_contextid_size = 3;
899 etm_unlock(t, t->etm_regs_count);
900 (void)etm_readl(t, t->etm_regs_count, ETMMR_PDSR);
901 /* dummy first read */
902 (void)etm_readl(&tracer, t->etm_regs_count, ETMMR_OSSRR);
904 etmccr = etm_readl(t, t->etm_regs_count, ETMR_CONFCODE);
905 t->ncmppairs = etmccr & 0xf;
906 if (etmccr & ETMCCR_ETMIDR_PRESENT) {
907 etmidr = etm_readl(t, t->etm_regs_count, ETMR_ID);
908 etm_version = ETMIDR_VERSION(etmidr);
909 if (etm_version >= ETMIDR_VERSION_3_1)
910 etmccer = etm_readl(t, t->etm_regs_count, ETMR_CCE);
912 etm_writel(t, t->etm_regs_count, 0x441, ETMR_CTRL);
913 etm_writel(t, t->etm_regs_count, new_count, ETMR_TRACEIDR);
914 etm_lock(t, t->etm_regs_count);
916 ret = sysfs_create_file(&dev->dev.kobj,
917 &trace_running_attr.attr);
921 /* failing to create any of these two is not fatal */
922 ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
924 dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
926 ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
928 dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
930 ret = sysfs_create_file(&dev->dev.kobj,
931 &trace_contextid_size_attr.attr);
934 "Failed to create trace_contextid_size in sysfs\n");
936 ret = sysfs_create_file(&dev->dev.kobj,
937 &trace_branch_output_attr.attr);
940 "Failed to create trace_branch_output in sysfs\n");
942 if (etmccer & ETMCCER_RETURN_STACK_IMPLEMENTED) {
943 ret = sysfs_create_file(&dev->dev.kobj,
944 &trace_return_stack_attr.attr);
947 "Failed to create trace_return_stack in sysfs\n");
950 if (etmccer & ETMCCER_TIMESTAMPING_IMPLEMENTED) {
951 ret = sysfs_create_file(&dev->dev.kobj,
952 &trace_timestamp_attr.attr);
955 "Failed to create trace_timestamp in sysfs\n");
958 ret = sysfs_create_file(&dev->dev.kobj, &trace_range_attr.attr);
960 dev_dbg(&dev->dev, "Failed to create trace_range in sysfs\n");
962 if (etm_version < ETMIDR_VERSION_PFT_1_0) {
963 ret = sysfs_create_file(&dev->dev.kobj,
964 &trace_data_range_attr.attr);
967 "Failed to create trace_data_range in sysfs\n");
969 tracer.flags &= ~TRACER_TRACE_DATA;
972 dev_dbg(&dev->dev, "ETM AMBA driver initialized.\n");
974 /* Enable formatter if there are multiple trace sources */
976 t->etb_fc = ETBFF_ENFCONT | ETBFF_ENFTC;
978 t->etm_regs_count = new_count;
981 mutex_unlock(&t->mutex);
985 amba_set_drvdata(dev, NULL);
986 iounmap(t->etm_regs[t->etm_regs_count]);
989 amba_release_regions(dev);
991 mutex_unlock(&t->mutex);
995 static int etm_remove(struct amba_device *dev)
998 struct tracectx *t = &tracer;
999 void __iomem *etm_regs = amba_get_drvdata(dev);
1001 sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
1002 sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
1003 sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
1004 sysfs_remove_file(&dev->dev.kobj, &trace_range_attr.attr);
1005 sysfs_remove_file(&dev->dev.kobj, &trace_data_range_attr.attr);
1007 amba_set_drvdata(dev, NULL);
1009 mutex_lock(&t->mutex);
1010 for (i = 0; i < t->etm_regs_count; i++)
1011 if (t->etm_regs[i] == etm_regs)
1013 for (; i < t->etm_regs_count - 1; i++)
1014 t->etm_regs[i] = t->etm_regs[i + 1];
1015 t->etm_regs_count--;
1016 if (!t->etm_regs_count) {
1020 mutex_unlock(&t->mutex);
1023 amba_release_regions(dev);
1028 static struct amba_id etm_ids[] = {
1040 static struct amba_driver etm_driver = {
1043 .owner = THIS_MODULE,
1046 .remove = etm_remove,
1047 .id_table = etm_ids,
1050 static int __init etm_init(void)
1054 mutex_init(&tracer.mutex);
1056 retval = amba_driver_register(&etb_driver);
1058 printk(KERN_ERR "Failed to register etb\n");
1062 retval = amba_driver_register(&etm_driver);
1064 amba_driver_unregister(&etb_driver);
1065 printk(KERN_ERR "Failed to probe etm\n");
1069 /* not being able to install this handler is not fatal */
1070 (void)register_sysrq_key('v', &sysrq_etm_op);
1075 device_initcall(etm_init);