]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/arm/kernel/etm.c
llseek: automatically add .llseek fop
[mirror_ubuntu-artful-kernel.git] / arch / arm / kernel / etm.c
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>
18 #include <linux/sysrq.h>
19 #include <linux/device.h>
20 #include <linux/clk.h>
21 #include <linux/amba/bus.h>
22 #include <linux/fs.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>
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Alexander Shishkin");
32
33 static struct tracectx tracer;
34
35 static inline bool trace_isrunning(struct tracectx *t)
36 {
37 return !!(t->flags & TRACER_RUNNING);
38 }
39
40 static int etm_setup_address_range(struct tracectx *t, int n,
41 unsigned long start, unsigned long end, int exclude, int data)
42 {
43 u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_NSONLY | \
44 ETMAAT_NOVALCMP;
45
46 if (n < 1 || n > t->ncmppairs)
47 return -EINVAL;
48
49 /* comparators and ranges are numbered starting with 1 as opposed
50 * to bits in a word */
51 n--;
52
53 if (data)
54 flags |= ETMAAT_DLOADSTORE;
55 else
56 flags |= ETMAAT_IEXEC;
57
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));
61
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));
65
66 flags = exclude ? ETMTE_INCLEXCL : 0;
67 etm_writel(t, flags | (1 << n), ETMR_TRACEENCTRL);
68
69 return 0;
70 }
71
72 static int trace_start(struct tracectx *t)
73 {
74 u32 v;
75 unsigned long timeout = TRACER_TIMEOUT;
76
77 etb_unlock(t);
78
79 etb_writel(t, 0, ETBR_FORMATTERCTRL);
80 etb_writel(t, 1, ETBR_CTRL);
81
82 etb_lock(t);
83
84 /* configure etm */
85 v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
86
87 if (t->flags & TRACER_CYCLE_ACC)
88 v |= ETMCTRL_CYCLEACCURATE;
89
90 etm_unlock(t);
91
92 etm_writel(t, v, ETMR_CTRL);
93
94 while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
95 ;
96 if (!timeout) {
97 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
98 etm_lock(t);
99 return -EFAULT;
100 }
101
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);
107
108 v &= ~ETMCTRL_PROGRAM;
109 v |= ETMCTRL_PORTSEL;
110
111 etm_writel(t, v, ETMR_CTRL);
112
113 timeout = TRACER_TIMEOUT;
114 while (etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
115 ;
116 if (!timeout) {
117 dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
118 etm_lock(t);
119 return -EFAULT;
120 }
121
122 etm_lock(t);
123
124 t->flags |= TRACER_RUNNING;
125
126 return 0;
127 }
128
129 static int trace_stop(struct tracectx *t)
130 {
131 unsigned long timeout = TRACER_TIMEOUT;
132
133 etm_unlock(t);
134
135 etm_writel(t, 0x440, ETMR_CTRL);
136 while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
137 ;
138 if (!timeout) {
139 dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
140 etm_lock(t);
141 return -EFAULT;
142 }
143
144 etm_lock(t);
145
146 etb_unlock(t);
147 etb_writel(t, ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
148
149 timeout = TRACER_TIMEOUT;
150 while (etb_readl(t, ETBR_FORMATTERCTRL) &
151 ETBFF_MANUAL_FLUSH && --timeout)
152 ;
153 if (!timeout) {
154 dev_dbg(t->dev, "Waiting for formatter flush to commence "
155 "timed out\n");
156 etb_lock(t);
157 return -EFAULT;
158 }
159
160 etb_writel(t, 0, ETBR_CTRL);
161
162 etb_lock(t);
163
164 t->flags &= ~TRACER_RUNNING;
165
166 return 0;
167 }
168
169 static int etb_getdatalen(struct tracectx *t)
170 {
171 u32 v;
172 int rp, wp;
173
174 v = etb_readl(t, ETBR_STATUS);
175
176 if (v & 1)
177 return t->etb_bufsz;
178
179 rp = etb_readl(t, ETBR_READADDR);
180 wp = etb_readl(t, ETBR_WRITEADDR);
181
182 if (rp > wp) {
183 etb_writel(t, 0, ETBR_READADDR);
184 etb_writel(t, 0, ETBR_WRITEADDR);
185
186 return 0;
187 }
188
189 return wp - rp;
190 }
191
192 /* sysrq+v will always stop the running trace and leave it at that */
193 static void etm_dump(void)
194 {
195 struct tracectx *t = &tracer;
196 u32 first = 0;
197 int length;
198
199 if (!t->etb_regs) {
200 printk(KERN_INFO "No tracing hardware found\n");
201 return;
202 }
203
204 if (trace_isrunning(t))
205 trace_stop(t);
206
207 etb_unlock(t);
208
209 length = etb_getdatalen(t);
210
211 if (length == t->etb_bufsz)
212 first = etb_readl(t, ETBR_WRITEADDR);
213
214 etb_writel(t, first, ETBR_READADDR);
215
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");
221
222 /* deassert the overflow bit */
223 etb_writel(t, 1, ETBR_CTRL);
224 etb_writel(t, 0, ETBR_CTRL);
225
226 etb_writel(t, 0, ETBR_TRIGGERCOUNT);
227 etb_writel(t, 0, ETBR_READADDR);
228 etb_writel(t, 0, ETBR_WRITEADDR);
229
230 etb_lock(t);
231 }
232
233 static void sysrq_etm_dump(int key)
234 {
235 dev_dbg(tracer.dev, "Dumping ETB buffer\n");
236 etm_dump();
237 }
238
239 static struct sysrq_key_op sysrq_etm_op = {
240 .handler = sysrq_etm_dump,
241 .help_msg = "ETM buffer dump",
242 .action_msg = "etm",
243 };
244
245 static int etb_open(struct inode *inode, struct file *file)
246 {
247 if (!tracer.etb_regs)
248 return -ENODEV;
249
250 file->private_data = &tracer;
251
252 return nonseekable_open(inode, file);
253 }
254
255 static ssize_t etb_read(struct file *file, char __user *data,
256 size_t len, loff_t *ppos)
257 {
258 int total, i;
259 long length;
260 struct tracectx *t = file->private_data;
261 u32 first = 0;
262 u32 *buf;
263
264 mutex_lock(&t->mutex);
265
266 if (trace_isrunning(t)) {
267 length = 0;
268 goto out;
269 }
270
271 etb_unlock(t);
272
273 total = etb_getdatalen(t);
274 if (total == t->etb_bufsz)
275 first = etb_readl(t, ETBR_WRITEADDR);
276
277 etb_writel(t, first, ETBR_READADDR);
278
279 length = min(total * 4, (int)len);
280 buf = vmalloc(length);
281
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);
286
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);
290
291 etb_writel(t, 0, ETBR_WRITEADDR);
292 etb_writel(t, 0, ETBR_READADDR);
293 etb_writel(t, 0, ETBR_TRIGGERCOUNT);
294
295 etb_lock(t);
296
297 length -= copy_to_user(data, buf, length);
298 vfree(buf);
299
300 out:
301 mutex_unlock(&t->mutex);
302
303 return length;
304 }
305
306 static int etb_release(struct inode *inode, struct file *file)
307 {
308 /* there's nothing to do here, actually */
309 return 0;
310 }
311
312 static const struct file_operations etb_fops = {
313 .owner = THIS_MODULE,
314 .read = etb_read,
315 .open = etb_open,
316 .release = etb_release,
317 .llseek = no_llseek,
318 };
319
320 static struct miscdevice etb_miscdev = {
321 .name = "tracebuf",
322 .minor = 0,
323 .fops = &etb_fops,
324 };
325
326 static int __init etb_probe(struct amba_device *dev, struct amba_id *id)
327 {
328 struct tracectx *t = &tracer;
329 int ret = 0;
330
331 ret = amba_request_regions(dev, NULL);
332 if (ret)
333 goto out;
334
335 t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
336 if (!t->etb_regs) {
337 ret = -ENOMEM;
338 goto out_release;
339 }
340
341 amba_set_drvdata(dev, t);
342
343 etb_miscdev.parent = &dev->dev;
344
345 ret = misc_register(&etb_miscdev);
346 if (ret)
347 goto out_unmap;
348
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");
352 return -EFAULT;
353 }
354
355 clk_enable(t->emu_clk);
356
357 etb_unlock(t);
358 t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
359 dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
360
361 /* make sure trace capture is disabled */
362 etb_writel(t, 0, ETBR_CTRL);
363 etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
364 etb_lock(t);
365
366 dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
367
368 out:
369 return ret;
370
371 out_unmap:
372 amba_set_drvdata(dev, NULL);
373 iounmap(t->etb_regs);
374
375 out_release:
376 amba_release_regions(dev);
377
378 return ret;
379 }
380
381 static int etb_remove(struct amba_device *dev)
382 {
383 struct tracectx *t = amba_get_drvdata(dev);
384
385 amba_set_drvdata(dev, NULL);
386
387 iounmap(t->etb_regs);
388 t->etb_regs = NULL;
389
390 clk_disable(t->emu_clk);
391 clk_put(t->emu_clk);
392
393 amba_release_regions(dev);
394
395 return 0;
396 }
397
398 static struct amba_id etb_ids[] = {
399 {
400 .id = 0x0003b907,
401 .mask = 0x0007ffff,
402 },
403 { 0, 0 },
404 };
405
406 static struct amba_driver etb_driver = {
407 .drv = {
408 .name = "etb",
409 .owner = THIS_MODULE,
410 },
411 .probe = etb_probe,
412 .remove = etb_remove,
413 .id_table = etb_ids,
414 };
415
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,
419 char *buf)
420 {
421 return sprintf(buf, "%x\n", trace_isrunning(&tracer));
422 }
423
424 static ssize_t trace_running_store(struct kobject *kobj,
425 struct kobj_attribute *attr,
426 const char *buf, size_t n)
427 {
428 unsigned int value;
429 int ret;
430
431 if (sscanf(buf, "%u", &value) != 1)
432 return -EINVAL;
433
434 mutex_lock(&tracer.mutex);
435 ret = value ? trace_start(&tracer) : trace_stop(&tracer);
436 mutex_unlock(&tracer.mutex);
437
438 return ret ? : n;
439 }
440
441 static struct kobj_attribute trace_running_attr =
442 __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
443
444 static ssize_t trace_info_show(struct kobject *kobj,
445 struct kobj_attribute *attr,
446 char *buf)
447 {
448 u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
449 int datalen;
450
451 etb_unlock(&tracer);
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);
457 etb_lock(&tracer);
458
459 etm_unlock(&tracer);
460 etm_ctrl = etm_readl(&tracer, ETMR_CTRL);
461 etm_st = etm_readl(&tracer, ETMR_STATUS);
462 etm_lock(&tracer);
463
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"
469 "ETMR_CTRL:\t%08x\n"
470 "ETMR_STATUS:\t%08x\n",
471 datalen,
472 tracer.ncmppairs,
473 etb_wa,
474 etb_ra,
475 etb_st,
476 etb_fc,
477 etm_ctrl,
478 etm_st
479 );
480 }
481
482 static struct kobj_attribute trace_info_attr =
483 __ATTR(trace_info, 0444, trace_info_show, NULL);
484
485 static ssize_t trace_mode_show(struct kobject *kobj,
486 struct kobj_attribute *attr,
487 char *buf)
488 {
489 return sprintf(buf, "%d %d\n",
490 !!(tracer.flags & TRACER_CYCLE_ACC),
491 tracer.etm_portsz);
492 }
493
494 static ssize_t trace_mode_store(struct kobject *kobj,
495 struct kobj_attribute *attr,
496 const char *buf, size_t n)
497 {
498 unsigned int cycacc, portsz;
499
500 if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
501 return -EINVAL;
502
503 mutex_lock(&tracer.mutex);
504 if (cycacc)
505 tracer.flags |= TRACER_CYCLE_ACC;
506 else
507 tracer.flags &= ~TRACER_CYCLE_ACC;
508
509 tracer.etm_portsz = portsz & 0x0f;
510 mutex_unlock(&tracer.mutex);
511
512 return n;
513 }
514
515 static struct kobj_attribute trace_mode_attr =
516 __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
517
518 static int __init etm_probe(struct amba_device *dev, struct amba_id *id)
519 {
520 struct tracectx *t = &tracer;
521 int ret = 0;
522
523 if (t->etm_regs) {
524 dev_dbg(&dev->dev, "ETM already initialized\n");
525 ret = -EBUSY;
526 goto out;
527 }
528
529 ret = amba_request_regions(dev, NULL);
530 if (ret)
531 goto out;
532
533 t->etm_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
534 if (!t->etm_regs) {
535 ret = -ENOMEM;
536 goto out_release;
537 }
538
539 amba_set_drvdata(dev, t);
540
541 mutex_init(&t->mutex);
542 t->dev = &dev->dev;
543 t->flags = TRACER_CYCLE_ACC;
544 t->etm_portsz = 1;
545
546 etm_unlock(t);
547 (void)etm_readl(t, ETMMR_PDSR);
548 /* dummy first read */
549 (void)etm_readl(&tracer, ETMMR_OSSRR);
550
551 t->ncmppairs = etm_readl(t, ETMR_CONFCODE) & 0xf;
552 etm_writel(t, 0x440, ETMR_CTRL);
553 etm_lock(t);
554
555 ret = sysfs_create_file(&dev->dev.kobj,
556 &trace_running_attr.attr);
557 if (ret)
558 goto out_unmap;
559
560 /* failing to create any of these two is not fatal */
561 ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
562 if (ret)
563 dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
564
565 ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
566 if (ret)
567 dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
568
569 dev_dbg(t->dev, "ETM AMBA driver initialized.\n");
570
571 out:
572 return ret;
573
574 out_unmap:
575 amba_set_drvdata(dev, NULL);
576 iounmap(t->etm_regs);
577
578 out_release:
579 amba_release_regions(dev);
580
581 return ret;
582 }
583
584 static int etm_remove(struct amba_device *dev)
585 {
586 struct tracectx *t = amba_get_drvdata(dev);
587
588 amba_set_drvdata(dev, NULL);
589
590 iounmap(t->etm_regs);
591 t->etm_regs = NULL;
592
593 amba_release_regions(dev);
594
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);
598
599 return 0;
600 }
601
602 static struct amba_id etm_ids[] = {
603 {
604 .id = 0x0003b921,
605 .mask = 0x0007ffff,
606 },
607 { 0, 0 },
608 };
609
610 static struct amba_driver etm_driver = {
611 .drv = {
612 .name = "etm",
613 .owner = THIS_MODULE,
614 },
615 .probe = etm_probe,
616 .remove = etm_remove,
617 .id_table = etm_ids,
618 };
619
620 static int __init etm_init(void)
621 {
622 int retval;
623
624 retval = amba_driver_register(&etb_driver);
625 if (retval) {
626 printk(KERN_ERR "Failed to register etb\n");
627 return retval;
628 }
629
630 retval = amba_driver_register(&etm_driver);
631 if (retval) {
632 amba_driver_unregister(&etb_driver);
633 printk(KERN_ERR "Failed to probe etm\n");
634 return retval;
635 }
636
637 /* not being able to install this handler is not fatal */
638 (void)register_sysrq_key('v', &sysrq_etm_op);
639
640 return 0;
641 }
642
643 device_initcall(etm_init);
644