Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 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> |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 18 | #include <linux/slab.h> |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 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> |
Paul Gortmaker | 73017a5 | 2011-07-31 16:14:14 -0400 | [diff] [blame] | 28 | #include <linux/module.h> |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 29 | #include <asm/hardware/coresight.h> |
| 30 | #include <asm/sections.h> |
| 31 | |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | MODULE_AUTHOR("Alexander Shishkin"); |
| 34 | |
Alexander Shishkin | 8234eae | 2010-08-04 11:22:43 +0100 | [diff] [blame] | 35 | /* |
| 36 | * ETM tracer state |
| 37 | */ |
| 38 | struct tracectx { |
| 39 | unsigned int etb_bufsz; |
| 40 | void __iomem *etb_regs; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 41 | void __iomem **etm_regs; |
| 42 | int etm_regs_count; |
Alexander Shishkin | 8234eae | 2010-08-04 11:22:43 +0100 | [diff] [blame] | 43 | unsigned long flags; |
| 44 | int ncmppairs; |
| 45 | int etm_portsz; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 46 | u32 etb_fc; |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 47 | unsigned long range_start; |
| 48 | unsigned long range_end; |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 49 | unsigned long data_range_start; |
| 50 | unsigned long data_range_end; |
Arve Hjønnevåg | 3a91f3e | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 51 | bool dump_initial_etb; |
Alexander Shishkin | 8234eae | 2010-08-04 11:22:43 +0100 | [diff] [blame] | 52 | struct device *dev; |
| 53 | struct clk *emu_clk; |
| 54 | struct mutex mutex; |
| 55 | }; |
| 56 | |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 57 | static struct tracectx tracer = { |
| 58 | .range_start = (unsigned long)_stext, |
| 59 | .range_end = (unsigned long)_etext, |
| 60 | }; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 61 | |
| 62 | static inline bool trace_isrunning(struct tracectx *t) |
| 63 | { |
| 64 | return !!(t->flags & TRACER_RUNNING); |
| 65 | } |
| 66 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 67 | static int etm_setup_address_range(struct tracectx *t, int id, int n, |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 68 | unsigned long start, unsigned long end, int exclude, int data) |
| 69 | { |
Arve Hjønnevåg | 8511b5b | 2011-01-28 23:33:11 -0800 | [diff] [blame] | 70 | u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_IGNSECURITY | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 71 | ETMAAT_NOVALCMP; |
| 72 | |
| 73 | if (n < 1 || n > t->ncmppairs) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | /* comparators and ranges are numbered starting with 1 as opposed |
| 77 | * to bits in a word */ |
| 78 | n--; |
| 79 | |
| 80 | if (data) |
| 81 | flags |= ETMAAT_DLOADSTORE; |
| 82 | else |
| 83 | flags |= ETMAAT_IEXEC; |
| 84 | |
| 85 | /* first comparator for the range */ |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 86 | etm_writel(t, id, flags, ETMR_COMP_ACC_TYPE(n * 2)); |
| 87 | etm_writel(t, id, start, ETMR_COMP_VAL(n * 2)); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 88 | |
| 89 | /* second comparator is right next to it */ |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 90 | etm_writel(t, id, flags, ETMR_COMP_ACC_TYPE(n * 2 + 1)); |
| 91 | etm_writel(t, id, end, ETMR_COMP_VAL(n * 2 + 1)); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 92 | |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 93 | if (data) { |
| 94 | flags = exclude ? ETMVDC3_EXCLONLY : 0; |
| 95 | if (exclude) |
| 96 | n += 8; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 97 | etm_writel(t, id, flags | BIT(n), ETMR_VIEWDATACTRL3); |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 98 | } else { |
| 99 | flags = exclude ? ETMTE_INCLEXCL : 0; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 100 | etm_writel(t, id, flags | (1 << n), ETMR_TRACEENCTRL); |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 101 | } |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 106 | static int trace_start_etm(struct tracectx *t, int id) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 107 | { |
| 108 | u32 v; |
| 109 | unsigned long timeout = TRACER_TIMEOUT; |
| 110 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 111 | v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz); |
| 112 | |
| 113 | if (t->flags & TRACER_CYCLE_ACC) |
| 114 | v |= ETMCTRL_CYCLEACCURATE; |
| 115 | |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 116 | if (t->flags & TRACER_TRACE_DATA) |
| 117 | v |= ETMCTRL_DATA_DO_ADDR; |
| 118 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 119 | etm_unlock(t, id); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 120 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 121 | etm_writel(t, id, v, ETMR_CTRL); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 122 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 123 | while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 124 | ; |
| 125 | if (!timeout) { |
| 126 | dev_dbg(t->dev, "Waiting for progbit to assert timed out\n"); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 127 | etm_lock(t, id); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 128 | return -EFAULT; |
| 129 | } |
| 130 | |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 131 | if (t->range_start || t->range_end) |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 132 | etm_setup_address_range(t, id, 1, |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 133 | t->range_start, t->range_end, 0, 0); |
| 134 | else |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 135 | etm_writel(t, id, ETMTE_INCLEXCL, ETMR_TRACEENCTRL); |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 136 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 137 | etm_writel(t, id, 0, ETMR_TRACEENCTRL2); |
| 138 | etm_writel(t, id, 0, ETMR_TRACESSCTRL); |
| 139 | etm_writel(t, id, 0x6f, ETMR_TRACEENEVT); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 140 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 141 | etm_writel(t, id, 0, ETMR_VIEWDATACTRL1); |
| 142 | etm_writel(t, id, 0, ETMR_VIEWDATACTRL2); |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 143 | |
| 144 | if (t->data_range_start || t->data_range_end) |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 145 | etm_setup_address_range(t, id, 2, t->data_range_start, |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 146 | t->data_range_end, 0, 1); |
| 147 | else |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 148 | etm_writel(t, id, ETMVDC3_EXCLONLY, ETMR_VIEWDATACTRL3); |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 149 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 150 | etm_writel(t, id, 0x6f, ETMR_VIEWDATAEVT); |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 151 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 152 | v &= ~ETMCTRL_PROGRAM; |
| 153 | v |= ETMCTRL_PORTSEL; |
| 154 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 155 | etm_writel(t, id, v, ETMR_CTRL); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 156 | |
| 157 | timeout = TRACER_TIMEOUT; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 158 | while (etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 159 | ; |
| 160 | if (!timeout) { |
| 161 | dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n"); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 162 | etm_lock(t, id); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 163 | return -EFAULT; |
| 164 | } |
| 165 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 166 | etm_lock(t, id); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int trace_start(struct tracectx *t) |
| 171 | { |
| 172 | int ret; |
| 173 | int id; |
| 174 | u32 etb_fc = t->etb_fc; |
| 175 | |
| 176 | etb_unlock(t); |
| 177 | |
| 178 | t->dump_initial_etb = false; |
| 179 | etb_writel(t, 0, ETBR_WRITEADDR); |
| 180 | etb_writel(t, etb_fc, ETBR_FORMATTERCTRL); |
| 181 | etb_writel(t, 1, ETBR_CTRL); |
| 182 | |
| 183 | etb_lock(t); |
| 184 | |
| 185 | /* configure etm(s) */ |
| 186 | for (id = 0; id < t->etm_regs_count; id++) { |
| 187 | ret = trace_start_etm(t, id); |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | } |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 191 | |
| 192 | t->flags |= TRACER_RUNNING; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 197 | static int trace_stop_etm(struct tracectx *t, int id) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 198 | { |
| 199 | unsigned long timeout = TRACER_TIMEOUT; |
| 200 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 201 | etm_unlock(t, id); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 202 | |
Arve Hjønnevåg | 4246983 | 2011-02-23 16:51:58 -0800 | [diff] [blame^] | 203 | etm_writel(t, id, 0x441, ETMR_CTRL); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 204 | while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 205 | ; |
| 206 | if (!timeout) { |
| 207 | dev_dbg(t->dev, "Waiting for progbit to assert timed out\n"); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 208 | etm_lock(t, id); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 209 | return -EFAULT; |
| 210 | } |
| 211 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 212 | etm_lock(t, id); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int trace_stop(struct tracectx *t) |
| 217 | { |
| 218 | int id; |
| 219 | int ret; |
| 220 | unsigned long timeout = TRACER_TIMEOUT; |
| 221 | u32 etb_fc = t->etb_fc; |
| 222 | |
| 223 | for (id = 0; id < t->etm_regs_count; id++) { |
| 224 | ret = trace_stop_etm(t, id); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | } |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 228 | |
| 229 | etb_unlock(t); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 230 | if (etb_fc) { |
| 231 | etb_fc |= ETBFF_STOPFL; |
| 232 | etb_writel(t, t->etb_fc, ETBR_FORMATTERCTRL); |
| 233 | } |
| 234 | etb_writel(t, etb_fc | ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 235 | |
| 236 | timeout = TRACER_TIMEOUT; |
| 237 | while (etb_readl(t, ETBR_FORMATTERCTRL) & |
| 238 | ETBFF_MANUAL_FLUSH && --timeout) |
| 239 | ; |
| 240 | if (!timeout) { |
| 241 | dev_dbg(t->dev, "Waiting for formatter flush to commence " |
| 242 | "timed out\n"); |
| 243 | etb_lock(t); |
| 244 | return -EFAULT; |
| 245 | } |
| 246 | |
| 247 | etb_writel(t, 0, ETBR_CTRL); |
| 248 | |
| 249 | etb_lock(t); |
| 250 | |
| 251 | t->flags &= ~TRACER_RUNNING; |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int etb_getdatalen(struct tracectx *t) |
| 257 | { |
| 258 | u32 v; |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 259 | int wp; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 260 | |
| 261 | v = etb_readl(t, ETBR_STATUS); |
| 262 | |
| 263 | if (v & 1) |
| 264 | return t->etb_bufsz; |
| 265 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 266 | wp = etb_readl(t, ETBR_WRITEADDR); |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 267 | return wp; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* sysrq+v will always stop the running trace and leave it at that */ |
| 271 | static void etm_dump(void) |
| 272 | { |
| 273 | struct tracectx *t = &tracer; |
| 274 | u32 first = 0; |
| 275 | int length; |
| 276 | |
| 277 | if (!t->etb_regs) { |
| 278 | printk(KERN_INFO "No tracing hardware found\n"); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if (trace_isrunning(t)) |
| 283 | trace_stop(t); |
| 284 | |
| 285 | etb_unlock(t); |
| 286 | |
| 287 | length = etb_getdatalen(t); |
| 288 | |
| 289 | if (length == t->etb_bufsz) |
| 290 | first = etb_readl(t, ETBR_WRITEADDR); |
| 291 | |
| 292 | etb_writel(t, first, ETBR_READADDR); |
| 293 | |
| 294 | printk(KERN_INFO "Trace buffer contents length: %d\n", length); |
| 295 | printk(KERN_INFO "--- ETB buffer begin ---\n"); |
| 296 | for (; length; length--) |
| 297 | printk("%08x", cpu_to_be32(etb_readl(t, ETBR_READMEM))); |
| 298 | printk(KERN_INFO "\n--- ETB buffer end ---\n"); |
| 299 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 300 | etb_lock(t); |
| 301 | } |
| 302 | |
Dmitry Torokhov | 1495cc9 | 2010-08-17 21:15:46 -0700 | [diff] [blame] | 303 | static void sysrq_etm_dump(int key) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 304 | { |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 305 | if (!mutex_trylock(&tracer.mutex)) { |
| 306 | printk(KERN_INFO "Tracing hardware busy\n"); |
| 307 | return; |
| 308 | } |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 309 | dev_dbg(tracer.dev, "Dumping ETB buffer\n"); |
| 310 | etm_dump(); |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 311 | mutex_unlock(&tracer.mutex); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static struct sysrq_key_op sysrq_etm_op = { |
| 315 | .handler = sysrq_etm_dump, |
| 316 | .help_msg = "ETM buffer dump", |
| 317 | .action_msg = "etm", |
| 318 | }; |
| 319 | |
| 320 | static int etb_open(struct inode *inode, struct file *file) |
| 321 | { |
| 322 | if (!tracer.etb_regs) |
| 323 | return -ENODEV; |
| 324 | |
| 325 | file->private_data = &tracer; |
| 326 | |
| 327 | return nonseekable_open(inode, file); |
| 328 | } |
| 329 | |
| 330 | static ssize_t etb_read(struct file *file, char __user *data, |
| 331 | size_t len, loff_t *ppos) |
| 332 | { |
| 333 | int total, i; |
| 334 | long length; |
| 335 | struct tracectx *t = file->private_data; |
| 336 | u32 first = 0; |
| 337 | u32 *buf; |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 338 | int wpos; |
| 339 | int skip; |
| 340 | long wlength; |
| 341 | loff_t pos = *ppos; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 342 | |
| 343 | mutex_lock(&t->mutex); |
| 344 | |
| 345 | if (trace_isrunning(t)) { |
| 346 | length = 0; |
| 347 | goto out; |
| 348 | } |
| 349 | |
| 350 | etb_unlock(t); |
| 351 | |
| 352 | total = etb_getdatalen(t); |
Arve Hjønnevåg | 3a91f3e | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 353 | if (total == 0 && t->dump_initial_etb) |
| 354 | total = t->etb_bufsz; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 355 | if (total == t->etb_bufsz) |
| 356 | first = etb_readl(t, ETBR_WRITEADDR); |
| 357 | |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 358 | if (pos > total * 4) { |
| 359 | skip = 0; |
| 360 | wpos = total; |
| 361 | } else { |
| 362 | skip = (int)pos % 4; |
| 363 | wpos = (int)pos / 4; |
| 364 | } |
| 365 | total -= wpos; |
| 366 | first = (first + wpos) % t->etb_bufsz; |
| 367 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 368 | etb_writel(t, first, ETBR_READADDR); |
| 369 | |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 370 | wlength = min(total, DIV_ROUND_UP(skip + (int)len, 4)); |
| 371 | length = min(total * 4 - skip, (int)len); |
| 372 | buf = vmalloc(wlength * 4); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 373 | |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 374 | dev_dbg(t->dev, "ETB read %ld bytes to %lld from %ld words at %d\n", |
| 375 | length, pos, wlength, first); |
| 376 | dev_dbg(t->dev, "ETB buffer length: %d\n", total + wpos); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 377 | dev_dbg(t->dev, "ETB status reg: %x\n", etb_readl(t, ETBR_STATUS)); |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 378 | for (i = 0; i < wlength; i++) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 379 | buf[i] = etb_readl(t, ETBR_READMEM); |
| 380 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 381 | etb_lock(t); |
| 382 | |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 383 | length -= copy_to_user(data, (u8 *)buf + skip, length); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 384 | vfree(buf); |
Arve Hjønnevåg | f13ae47 | 2011-01-31 21:34:47 -0800 | [diff] [blame] | 385 | *ppos = pos + length; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 386 | |
| 387 | out: |
| 388 | mutex_unlock(&t->mutex); |
| 389 | |
| 390 | return length; |
| 391 | } |
| 392 | |
| 393 | static int etb_release(struct inode *inode, struct file *file) |
| 394 | { |
| 395 | /* there's nothing to do here, actually */ |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static const struct file_operations etb_fops = { |
| 400 | .owner = THIS_MODULE, |
| 401 | .read = etb_read, |
| 402 | .open = etb_open, |
| 403 | .release = etb_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 404 | .llseek = no_llseek, |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | static struct miscdevice etb_miscdev = { |
| 408 | .name = "tracebuf", |
| 409 | .minor = 0, |
| 410 | .fops = &etb_fops, |
| 411 | }; |
| 412 | |
Ming Lei | 8e88069 | 2011-03-28 06:10:25 +0100 | [diff] [blame] | 413 | static int __devinit etb_probe(struct amba_device *dev, const struct amba_id *id) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 414 | { |
| 415 | struct tracectx *t = &tracer; |
| 416 | int ret = 0; |
| 417 | |
| 418 | ret = amba_request_regions(dev, NULL); |
| 419 | if (ret) |
| 420 | goto out; |
| 421 | |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 422 | mutex_lock(&t->mutex); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 423 | t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res)); |
| 424 | if (!t->etb_regs) { |
| 425 | ret = -ENOMEM; |
| 426 | goto out_release; |
| 427 | } |
| 428 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 429 | t->dev = &dev->dev; |
Arve Hjønnevåg | 3a91f3e | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 430 | t->dump_initial_etb = true; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 431 | amba_set_drvdata(dev, t); |
| 432 | |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 433 | etb_unlock(t); |
| 434 | t->etb_bufsz = etb_readl(t, ETBR_DEPTH); |
| 435 | dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz); |
| 436 | |
| 437 | /* make sure trace capture is disabled */ |
| 438 | etb_writel(t, 0, ETBR_CTRL); |
| 439 | etb_writel(t, 0x1000, ETBR_FORMATTERCTRL); |
| 440 | etb_lock(t); |
| 441 | mutex_unlock(&t->mutex); |
| 442 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 443 | etb_miscdev.parent = &dev->dev; |
| 444 | |
| 445 | ret = misc_register(&etb_miscdev); |
| 446 | if (ret) |
| 447 | goto out_unmap; |
| 448 | |
Arve Hjønnevåg | d387659 | 2011-01-28 23:12:32 -0800 | [diff] [blame] | 449 | /* Get optional clock. Currently used to select clock source on omap3 */ |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 450 | t->emu_clk = clk_get(&dev->dev, "emu_src_ck"); |
Arve Hjønnevåg | d387659 | 2011-01-28 23:12:32 -0800 | [diff] [blame] | 451 | if (IS_ERR(t->emu_clk)) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 452 | dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n"); |
Arve Hjønnevåg | d387659 | 2011-01-28 23:12:32 -0800 | [diff] [blame] | 453 | else |
| 454 | clk_enable(t->emu_clk); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 455 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 456 | dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n"); |
| 457 | |
| 458 | out: |
| 459 | return ret; |
| 460 | |
| 461 | out_unmap: |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 462 | mutex_lock(&t->mutex); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 463 | amba_set_drvdata(dev, NULL); |
| 464 | iounmap(t->etb_regs); |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 465 | t->etb_regs = NULL; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 466 | |
| 467 | out_release: |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 468 | mutex_unlock(&t->mutex); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 469 | amba_release_regions(dev); |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | static int etb_remove(struct amba_device *dev) |
| 475 | { |
| 476 | struct tracectx *t = amba_get_drvdata(dev); |
| 477 | |
| 478 | amba_set_drvdata(dev, NULL); |
| 479 | |
| 480 | iounmap(t->etb_regs); |
| 481 | t->etb_regs = NULL; |
| 482 | |
Arve Hjønnevåg | d387659 | 2011-01-28 23:12:32 -0800 | [diff] [blame] | 483 | if (!IS_ERR(t->emu_clk)) { |
| 484 | clk_disable(t->emu_clk); |
| 485 | clk_put(t->emu_clk); |
| 486 | } |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 487 | |
| 488 | amba_release_regions(dev); |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static struct amba_id etb_ids[] = { |
| 494 | { |
| 495 | .id = 0x0003b907, |
| 496 | .mask = 0x0007ffff, |
| 497 | }, |
| 498 | { 0, 0 }, |
| 499 | }; |
| 500 | |
| 501 | static struct amba_driver etb_driver = { |
| 502 | .drv = { |
| 503 | .name = "etb", |
| 504 | .owner = THIS_MODULE, |
| 505 | }, |
| 506 | .probe = etb_probe, |
| 507 | .remove = etb_remove, |
| 508 | .id_table = etb_ids, |
| 509 | }; |
| 510 | |
| 511 | /* use a sysfs file "trace_running" to start/stop tracing */ |
| 512 | static ssize_t trace_running_show(struct kobject *kobj, |
| 513 | struct kobj_attribute *attr, |
| 514 | char *buf) |
| 515 | { |
| 516 | return sprintf(buf, "%x\n", trace_isrunning(&tracer)); |
| 517 | } |
| 518 | |
| 519 | static ssize_t trace_running_store(struct kobject *kobj, |
| 520 | struct kobj_attribute *attr, |
| 521 | const char *buf, size_t n) |
| 522 | { |
| 523 | unsigned int value; |
| 524 | int ret; |
| 525 | |
| 526 | if (sscanf(buf, "%u", &value) != 1) |
| 527 | return -EINVAL; |
| 528 | |
| 529 | mutex_lock(&tracer.mutex); |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 530 | if (!tracer.etb_regs) |
| 531 | ret = -ENODEV; |
| 532 | else |
| 533 | ret = value ? trace_start(&tracer) : trace_stop(&tracer); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 534 | mutex_unlock(&tracer.mutex); |
| 535 | |
| 536 | return ret ? : n; |
| 537 | } |
| 538 | |
| 539 | static struct kobj_attribute trace_running_attr = |
| 540 | __ATTR(trace_running, 0644, trace_running_show, trace_running_store); |
| 541 | |
| 542 | static ssize_t trace_info_show(struct kobject *kobj, |
| 543 | struct kobj_attribute *attr, |
| 544 | char *buf) |
| 545 | { |
| 546 | u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st; |
| 547 | int datalen; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 548 | int id; |
| 549 | int ret; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 550 | |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 551 | mutex_lock(&tracer.mutex); |
| 552 | if (tracer.etb_regs) { |
| 553 | etb_unlock(&tracer); |
| 554 | datalen = etb_getdatalen(&tracer); |
| 555 | etb_wa = etb_readl(&tracer, ETBR_WRITEADDR); |
| 556 | etb_ra = etb_readl(&tracer, ETBR_READADDR); |
| 557 | etb_st = etb_readl(&tracer, ETBR_STATUS); |
| 558 | etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL); |
| 559 | etb_lock(&tracer); |
| 560 | } else { |
| 561 | etb_wa = etb_ra = etb_st = etb_fc = ~0; |
| 562 | datalen = -1; |
| 563 | } |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 564 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 565 | ret = sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n" |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 566 | "ETBR_WRITEADDR:\t%08x\n" |
| 567 | "ETBR_READADDR:\t%08x\n" |
| 568 | "ETBR_STATUS:\t%08x\n" |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 569 | "ETBR_FORMATTERCTRL:\t%08x\n", |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 570 | datalen, |
| 571 | tracer.ncmppairs, |
| 572 | etb_wa, |
| 573 | etb_ra, |
| 574 | etb_st, |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 575 | etb_fc |
| 576 | ); |
| 577 | |
| 578 | for (id = 0; id < tracer.etm_regs_count; id++) { |
| 579 | etm_unlock(&tracer, id); |
| 580 | etm_ctrl = etm_readl(&tracer, id, ETMR_CTRL); |
| 581 | etm_st = etm_readl(&tracer, id, ETMR_STATUS); |
| 582 | etm_lock(&tracer, id); |
| 583 | ret += sprintf(buf + ret, "ETMR_CTRL:\t%08x\n" |
| 584 | "ETMR_STATUS:\t%08x\n", |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 585 | etm_ctrl, |
| 586 | etm_st |
| 587 | ); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 588 | } |
| 589 | mutex_unlock(&tracer.mutex); |
| 590 | |
| 591 | return ret; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static struct kobj_attribute trace_info_attr = |
| 595 | __ATTR(trace_info, 0444, trace_info_show, NULL); |
| 596 | |
| 597 | static ssize_t trace_mode_show(struct kobject *kobj, |
| 598 | struct kobj_attribute *attr, |
| 599 | char *buf) |
| 600 | { |
| 601 | return sprintf(buf, "%d %d\n", |
| 602 | !!(tracer.flags & TRACER_CYCLE_ACC), |
| 603 | tracer.etm_portsz); |
| 604 | } |
| 605 | |
| 606 | static ssize_t trace_mode_store(struct kobject *kobj, |
| 607 | struct kobj_attribute *attr, |
| 608 | const char *buf, size_t n) |
| 609 | { |
| 610 | unsigned int cycacc, portsz; |
| 611 | |
| 612 | if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | mutex_lock(&tracer.mutex); |
| 616 | if (cycacc) |
| 617 | tracer.flags |= TRACER_CYCLE_ACC; |
| 618 | else |
| 619 | tracer.flags &= ~TRACER_CYCLE_ACC; |
| 620 | |
| 621 | tracer.etm_portsz = portsz & 0x0f; |
| 622 | mutex_unlock(&tracer.mutex); |
| 623 | |
| 624 | return n; |
| 625 | } |
| 626 | |
| 627 | static struct kobj_attribute trace_mode_attr = |
| 628 | __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store); |
| 629 | |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 630 | static ssize_t trace_range_show(struct kobject *kobj, |
| 631 | struct kobj_attribute *attr, |
| 632 | char *buf) |
| 633 | { |
| 634 | return sprintf(buf, "%08lx %08lx\n", |
| 635 | tracer.range_start, tracer.range_end); |
| 636 | } |
| 637 | |
| 638 | static ssize_t trace_range_store(struct kobject *kobj, |
| 639 | struct kobj_attribute *attr, |
| 640 | const char *buf, size_t n) |
| 641 | { |
| 642 | unsigned long range_start, range_end; |
| 643 | |
| 644 | if (sscanf(buf, "%lx %lx", &range_start, &range_end) != 2) |
| 645 | return -EINVAL; |
| 646 | |
| 647 | mutex_lock(&tracer.mutex); |
| 648 | tracer.range_start = range_start; |
| 649 | tracer.range_end = range_end; |
| 650 | mutex_unlock(&tracer.mutex); |
| 651 | |
| 652 | return n; |
| 653 | } |
| 654 | |
| 655 | |
| 656 | static struct kobj_attribute trace_range_attr = |
| 657 | __ATTR(trace_range, 0644, trace_range_show, trace_range_store); |
| 658 | |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 659 | static ssize_t trace_data_range_show(struct kobject *kobj, |
| 660 | struct kobj_attribute *attr, |
| 661 | char *buf) |
| 662 | { |
| 663 | unsigned long range_start; |
| 664 | u64 range_end; |
| 665 | mutex_lock(&tracer.mutex); |
| 666 | range_start = tracer.data_range_start; |
| 667 | range_end = tracer.data_range_end; |
| 668 | if (!range_end && (tracer.flags & TRACER_TRACE_DATA)) |
| 669 | range_end = 0x100000000ULL; |
| 670 | mutex_unlock(&tracer.mutex); |
| 671 | return sprintf(buf, "%08lx %08llx\n", range_start, range_end); |
| 672 | } |
| 673 | |
| 674 | static ssize_t trace_data_range_store(struct kobject *kobj, |
| 675 | struct kobj_attribute *attr, |
| 676 | const char *buf, size_t n) |
| 677 | { |
| 678 | unsigned long range_start; |
| 679 | u64 range_end; |
| 680 | |
| 681 | if (sscanf(buf, "%lx %llx", &range_start, &range_end) != 2) |
| 682 | return -EINVAL; |
| 683 | |
| 684 | mutex_lock(&tracer.mutex); |
| 685 | tracer.data_range_start = range_start; |
| 686 | tracer.data_range_end = (unsigned long)range_end; |
| 687 | if (range_end) |
| 688 | tracer.flags |= TRACER_TRACE_DATA; |
| 689 | else |
| 690 | tracer.flags &= ~TRACER_TRACE_DATA; |
| 691 | mutex_unlock(&tracer.mutex); |
| 692 | |
| 693 | return n; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | static struct kobj_attribute trace_data_range_attr = |
| 698 | __ATTR(trace_data_range, 0644, |
| 699 | trace_data_range_show, trace_data_range_store); |
| 700 | |
Ming Lei | 8e88069 | 2011-03-28 06:10:25 +0100 | [diff] [blame] | 701 | static int __devinit etm_probe(struct amba_device *dev, const struct amba_id *id) |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 702 | { |
| 703 | struct tracectx *t = &tracer; |
| 704 | int ret = 0; |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 705 | void __iomem **new_regs; |
| 706 | int new_count; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 707 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 708 | mutex_lock(&t->mutex); |
| 709 | new_count = t->etm_regs_count + 1; |
| 710 | new_regs = krealloc(t->etm_regs, |
| 711 | sizeof(t->etm_regs[0]) * new_count, GFP_KERNEL); |
| 712 | |
| 713 | if (!new_regs) { |
| 714 | dev_dbg(&dev->dev, "Failed to allocate ETM register array\n"); |
| 715 | ret = -ENOMEM; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 716 | goto out; |
| 717 | } |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 718 | t->etm_regs = new_regs; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 719 | |
| 720 | ret = amba_request_regions(dev, NULL); |
| 721 | if (ret) |
| 722 | goto out; |
| 723 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 724 | t->etm_regs[t->etm_regs_count] = |
| 725 | ioremap_nocache(dev->res.start, resource_size(&dev->res)); |
| 726 | if (!t->etm_regs[t->etm_regs_count]) { |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 727 | ret = -ENOMEM; |
| 728 | goto out_release; |
| 729 | } |
| 730 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 731 | amba_set_drvdata(dev, t->etm_regs[t->etm_regs_count]); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 732 | |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 733 | t->flags = TRACER_CYCLE_ACC | TRACER_TRACE_DATA; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 734 | t->etm_portsz = 1; |
| 735 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 736 | etm_unlock(t, t->etm_regs_count); |
| 737 | (void)etm_readl(t, t->etm_regs_count, ETMMR_PDSR); |
Alexander Shishkin | 988257c | 2010-08-04 11:27:33 +0100 | [diff] [blame] | 738 | /* dummy first read */ |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 739 | (void)etm_readl(&tracer, t->etm_regs_count, ETMMR_OSSRR); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 740 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 741 | t->ncmppairs = etm_readl(t, t->etm_regs_count, ETMR_CONFCODE) & 0xf; |
Arve Hjønnevåg | 4246983 | 2011-02-23 16:51:58 -0800 | [diff] [blame^] | 742 | etm_writel(t, t->etm_regs_count, 0x441, ETMR_CTRL); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 743 | etm_writel(t, t->etm_regs_count, new_count, ETMR_TRACEIDR); |
| 744 | etm_lock(t, t->etm_regs_count); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 745 | |
| 746 | ret = sysfs_create_file(&dev->dev.kobj, |
| 747 | &trace_running_attr.attr); |
| 748 | if (ret) |
| 749 | goto out_unmap; |
| 750 | |
| 751 | /* failing to create any of these two is not fatal */ |
| 752 | ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr); |
| 753 | if (ret) |
| 754 | dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n"); |
| 755 | |
| 756 | ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr); |
| 757 | if (ret) |
| 758 | dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n"); |
| 759 | |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 760 | ret = sysfs_create_file(&dev->dev.kobj, &trace_range_attr.attr); |
| 761 | if (ret) |
| 762 | dev_dbg(&dev->dev, "Failed to create trace_range in sysfs\n"); |
| 763 | |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 764 | ret = sysfs_create_file(&dev->dev.kobj, &trace_data_range_attr.attr); |
| 765 | if (ret) |
| 766 | dev_dbg(&dev->dev, |
| 767 | "Failed to create trace_data_range in sysfs\n"); |
| 768 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 769 | dev_dbg(&dev->dev, "ETM AMBA driver initialized.\n"); |
| 770 | |
| 771 | /* Enable formatter if there are multiple trace sources */ |
| 772 | if (new_count > 1) |
| 773 | t->etb_fc = ETBFF_ENFCONT | ETBFF_ENFTC; |
| 774 | |
| 775 | t->etm_regs_count = new_count; |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 776 | |
| 777 | out: |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 778 | mutex_unlock(&t->mutex); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 779 | return ret; |
| 780 | |
| 781 | out_unmap: |
| 782 | amba_set_drvdata(dev, NULL); |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 783 | iounmap(t->etm_regs[t->etm_regs_count]); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 784 | |
| 785 | out_release: |
| 786 | amba_release_regions(dev); |
| 787 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 788 | mutex_unlock(&t->mutex); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 789 | return ret; |
| 790 | } |
| 791 | |
| 792 | static int etm_remove(struct amba_device *dev) |
| 793 | { |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 794 | int i; |
| 795 | struct tracectx *t = &tracer; |
| 796 | void __iomem *etm_regs = amba_get_drvdata(dev); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 797 | |
| 798 | sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr); |
| 799 | sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr); |
| 800 | sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr); |
Arve Hjønnevåg | d71c695 | 2011-01-28 23:44:43 -0800 | [diff] [blame] | 801 | sysfs_remove_file(&dev->dev.kobj, &trace_range_attr.attr); |
Arve Hjønnevåg | 0c3da53 | 2011-01-31 18:33:55 -0800 | [diff] [blame] | 802 | sysfs_remove_file(&dev->dev.kobj, &trace_data_range_attr.attr); |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 803 | |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 804 | amba_set_drvdata(dev, NULL); |
| 805 | |
| 806 | mutex_lock(&t->mutex); |
| 807 | for (i = 0; i < t->etm_regs_count; i++) |
| 808 | if (t->etm_regs[i] == etm_regs) |
| 809 | break; |
| 810 | for (; i < t->etm_regs_count - 1; i++) |
| 811 | t->etm_regs[i] = t->etm_regs[i + 1]; |
| 812 | t->etm_regs_count--; |
| 813 | if (!t->etm_regs_count) { |
| 814 | kfree(t->etm_regs); |
| 815 | t->etm_regs = NULL; |
| 816 | } |
| 817 | mutex_unlock(&t->mutex); |
| 818 | |
| 819 | iounmap(etm_regs); |
| 820 | amba_release_regions(dev); |
| 821 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static struct amba_id etm_ids[] = { |
| 826 | { |
| 827 | .id = 0x0003b921, |
| 828 | .mask = 0x0007ffff, |
| 829 | }, |
Arve Hjønnevåg | 992cd59 | 2011-02-04 22:38:14 -0800 | [diff] [blame] | 830 | { |
| 831 | .id = 0x0003b950, |
| 832 | .mask = 0x0007ffff, |
| 833 | }, |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 834 | { 0, 0 }, |
| 835 | }; |
| 836 | |
| 837 | static struct amba_driver etm_driver = { |
| 838 | .drv = { |
| 839 | .name = "etm", |
| 840 | .owner = THIS_MODULE, |
| 841 | }, |
| 842 | .probe = etm_probe, |
| 843 | .remove = etm_remove, |
| 844 | .id_table = etm_ids, |
| 845 | }; |
| 846 | |
| 847 | static int __init etm_init(void) |
| 848 | { |
| 849 | int retval; |
| 850 | |
Arve Hjønnevåg | fe05bda | 2011-02-14 21:09:51 -0800 | [diff] [blame] | 851 | mutex_init(&tracer.mutex); |
| 852 | |
Alexander Shishkin | c5d6c77 | 2009-12-01 14:00:51 +0100 | [diff] [blame] | 853 | retval = amba_driver_register(&etb_driver); |
| 854 | if (retval) { |
| 855 | printk(KERN_ERR "Failed to register etb\n"); |
| 856 | return retval; |
| 857 | } |
| 858 | |
| 859 | retval = amba_driver_register(&etm_driver); |
| 860 | if (retval) { |
| 861 | amba_driver_unregister(&etb_driver); |
| 862 | printk(KERN_ERR "Failed to probe etm\n"); |
| 863 | return retval; |
| 864 | } |
| 865 | |
| 866 | /* not being able to install this handler is not fatal */ |
| 867 | (void)register_sysrq_key('v', &sysrq_etm_op); |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | device_initcall(etm_init); |
| 873 | |