etm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/io.h>
  17. #include <linux/sysrq.h>
  18. #include <linux/device.h>
  19. #include <linux/clk.h>
  20. #include <linux/amba/bus.h>
  21. #include <linux/fs.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mutex.h>
  26. #include <asm/hardware/coresight.h>
  27. #include <asm/sections.h>
  28. MODULE_LICENSE("GPL");
  29. MODULE_AUTHOR("Alexander Shishkin");
  30. /*
  31. * ETM tracer state
  32. */
  33. struct tracectx {
  34. unsigned int etb_bufsz;
  35. void __iomem *etb_regs;
  36. void __iomem *etm_regs;
  37. unsigned long flags;
  38. int ncmppairs;
  39. int etm_portsz;
  40. struct device *dev;
  41. struct clk *emu_clk;
  42. struct mutex mutex;
  43. };
  44. static struct tracectx tracer;
  45. static inline bool trace_isrunning(struct tracectx *t)
  46. {
  47. return !!(t->flags & TRACER_RUNNING);
  48. }
  49. static int etm_setup_address_range(struct tracectx *t, int n,
  50. unsigned long start, unsigned long end, int exclude, int data)
  51. {
  52. u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_NSONLY | \
  53. ETMAAT_NOVALCMP;
  54. if (n < 1 || n > t->ncmppairs)
  55. return -EINVAL;
  56. /* comparators and ranges are numbered starting with 1 as opposed
  57. * to bits in a word */
  58. n--;
  59. if (data)
  60. flags |= ETMAAT_DLOADSTORE;
  61. else
  62. flags |= ETMAAT_IEXEC;
  63. /* first comparator for the range */
  64. etm_writel(t, flags, ETMR_COMP_ACC_TYPE(n * 2));
  65. etm_writel(t, start, ETMR_COMP_VAL(n * 2));
  66. /* second comparator is right next to it */
  67. etm_writel(t, flags, ETMR_COMP_ACC_TYPE(n * 2 + 1));
  68. etm_writel(t, end, ETMR_COMP_VAL(n * 2 + 1));
  69. flags = exclude ? ETMTE_INCLEXCL : 0;
  70. etm_writel(t, flags | (1 << n), ETMR_TRACEENCTRL);
  71. return 0;
  72. }
  73. static int trace_start(struct tracectx *t)
  74. {
  75. u32 v;
  76. unsigned long timeout = TRACER_TIMEOUT;
  77. etb_unlock(t);
  78. etb_writel(t, 0, ETBR_FORMATTERCTRL);
  79. etb_writel(t, 1, ETBR_CTRL);
  80. etb_lock(t);
  81. /* configure etm */
  82. v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
  83. if (t->flags & TRACER_CYCLE_ACC)
  84. v |= ETMCTRL_CYCLEACCURATE;
  85. etm_unlock(t);
  86. etm_writel(t, v, ETMR_CTRL);
  87. while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
  88. ;
  89. if (!timeout) {
  90. dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
  91. etm_lock(t);
  92. return -EFAULT;
  93. }
  94. etm_setup_address_range(t, 1, (unsigned long)_stext,
  95. (unsigned long)_etext, 0, 0);
  96. etm_writel(t, 0, ETMR_TRACEENCTRL2);
  97. etm_writel(t, 0, ETMR_TRACESSCTRL);
  98. etm_writel(t, 0x6f, ETMR_TRACEENEVT);
  99. v &= ~ETMCTRL_PROGRAM;
  100. v |= ETMCTRL_PORTSEL;
  101. etm_writel(t, v, ETMR_CTRL);
  102. timeout = TRACER_TIMEOUT;
  103. while (etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
  104. ;
  105. if (!timeout) {
  106. dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
  107. etm_lock(t);
  108. return -EFAULT;
  109. }
  110. etm_lock(t);
  111. t->flags |= TRACER_RUNNING;
  112. return 0;
  113. }
  114. static int trace_stop(struct tracectx *t)
  115. {
  116. unsigned long timeout = TRACER_TIMEOUT;
  117. etm_unlock(t);
  118. etm_writel(t, 0x440, ETMR_CTRL);
  119. while (!(etm_readl(t, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
  120. ;
  121. if (!timeout) {
  122. dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
  123. etm_lock(t);
  124. return -EFAULT;
  125. }
  126. etm_lock(t);
  127. etb_unlock(t);
  128. etb_writel(t, ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
  129. timeout = TRACER_TIMEOUT;
  130. while (etb_readl(t, ETBR_FORMATTERCTRL) &
  131. ETBFF_MANUAL_FLUSH && --timeout)
  132. ;
  133. if (!timeout) {
  134. dev_dbg(t->dev, "Waiting for formatter flush to commence "
  135. "timed out\n");
  136. etb_lock(t);
  137. return -EFAULT;
  138. }
  139. etb_writel(t, 0, ETBR_CTRL);
  140. etb_lock(t);
  141. t->flags &= ~TRACER_RUNNING;
  142. return 0;
  143. }
  144. static int etb_getdatalen(struct tracectx *t)
  145. {
  146. u32 v;
  147. int rp, wp;
  148. v = etb_readl(t, ETBR_STATUS);
  149. if (v & 1)
  150. return t->etb_bufsz;
  151. rp = etb_readl(t, ETBR_READADDR);
  152. wp = etb_readl(t, ETBR_WRITEADDR);
  153. if (rp > wp) {
  154. etb_writel(t, 0, ETBR_READADDR);
  155. etb_writel(t, 0, ETBR_WRITEADDR);
  156. return 0;
  157. }
  158. return wp - rp;
  159. }
  160. /* sysrq+v will always stop the running trace and leave it at that */
  161. static void etm_dump(void)
  162. {
  163. struct tracectx *t = &tracer;
  164. u32 first = 0;
  165. int length;
  166. if (!t->etb_regs) {
  167. printk(KERN_INFO "No tracing hardware found\n");
  168. return;
  169. }
  170. if (trace_isrunning(t))
  171. trace_stop(t);
  172. etb_unlock(t);
  173. length = etb_getdatalen(t);
  174. if (length == t->etb_bufsz)
  175. first = etb_readl(t, ETBR_WRITEADDR);
  176. etb_writel(t, first, ETBR_READADDR);
  177. printk(KERN_INFO "Trace buffer contents length: %d\n", length);
  178. printk(KERN_INFO "--- ETB buffer begin ---\n");
  179. for (; length; length--)
  180. printk("%08x", cpu_to_be32(etb_readl(t, ETBR_READMEM)));
  181. printk(KERN_INFO "\n--- ETB buffer end ---\n");
  182. /* deassert the overflow bit */
  183. etb_writel(t, 1, ETBR_CTRL);
  184. etb_writel(t, 0, ETBR_CTRL);
  185. etb_writel(t, 0, ETBR_TRIGGERCOUNT);
  186. etb_writel(t, 0, ETBR_READADDR);
  187. etb_writel(t, 0, ETBR_WRITEADDR);
  188. etb_lock(t);
  189. }
  190. static void sysrq_etm_dump(int key)
  191. {
  192. dev_dbg(tracer.dev, "Dumping ETB buffer\n");
  193. etm_dump();
  194. }
  195. static struct sysrq_key_op sysrq_etm_op = {
  196. .handler = sysrq_etm_dump,
  197. .help_msg = "ETM buffer dump",
  198. .action_msg = "etm",
  199. };
  200. static int etb_open(struct inode *inode, struct file *file)
  201. {
  202. if (!tracer.etb_regs)
  203. return -ENODEV;
  204. file->private_data = &tracer;
  205. return nonseekable_open(inode, file);
  206. }
  207. static ssize_t etb_read(struct file *file, char __user *data,
  208. size_t len, loff_t *ppos)
  209. {
  210. int total, i;
  211. long length;
  212. struct tracectx *t = file->private_data;
  213. u32 first = 0;
  214. u32 *buf;
  215. mutex_lock(&t->mutex);
  216. if (trace_isrunning(t)) {
  217. length = 0;
  218. goto out;
  219. }
  220. etb_unlock(t);
  221. total = etb_getdatalen(t);
  222. if (total == t->etb_bufsz)
  223. first = etb_readl(t, ETBR_WRITEADDR);
  224. etb_writel(t, first, ETBR_READADDR);
  225. length = min(total * 4, (int)len);
  226. buf = vmalloc(length);
  227. dev_dbg(t->dev, "ETB buffer length: %d\n", total);
  228. dev_dbg(t->dev, "ETB status reg: %x\n", etb_readl(t, ETBR_STATUS));
  229. for (i = 0; i < length / 4; i++)
  230. buf[i] = etb_readl(t, ETBR_READMEM);
  231. /* the only way to deassert overflow bit in ETB status is this */
  232. etb_writel(t, 1, ETBR_CTRL);
  233. etb_writel(t, 0, ETBR_CTRL);
  234. etb_writel(t, 0, ETBR_WRITEADDR);
  235. etb_writel(t, 0, ETBR_READADDR);
  236. etb_writel(t, 0, ETBR_TRIGGERCOUNT);
  237. etb_lock(t);
  238. length -= copy_to_user(data, buf, length);
  239. vfree(buf);
  240. out:
  241. mutex_unlock(&t->mutex);
  242. return length;
  243. }
  244. static int etb_release(struct inode *inode, struct file *file)
  245. {
  246. /* there's nothing to do here, actually */
  247. return 0;
  248. }
  249. static const struct file_operations etb_fops = {
  250. .owner = THIS_MODULE,
  251. .read = etb_read,
  252. .open = etb_open,
  253. .release = etb_release,
  254. .llseek = no_llseek,
  255. };
  256. static struct miscdevice etb_miscdev = {
  257. .name = "tracebuf",
  258. .minor = 0,
  259. .fops = &etb_fops,
  260. };
  261. static int __devinit etb_probe(struct amba_device *dev, const struct amba_id *id)
  262. {
  263. struct tracectx *t = &tracer;
  264. int ret = 0;
  265. ret = amba_request_regions(dev, NULL);
  266. if (ret)
  267. goto out;
  268. t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
  269. if (!t->etb_regs) {
  270. ret = -ENOMEM;
  271. goto out_release;
  272. }
  273. amba_set_drvdata(dev, t);
  274. etb_miscdev.parent = &dev->dev;
  275. ret = misc_register(&etb_miscdev);
  276. if (ret)
  277. goto out_unmap;
  278. t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
  279. if (IS_ERR(t->emu_clk)) {
  280. dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
  281. return -EFAULT;
  282. }
  283. clk_enable(t->emu_clk);
  284. etb_unlock(t);
  285. t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
  286. dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
  287. /* make sure trace capture is disabled */
  288. etb_writel(t, 0, ETBR_CTRL);
  289. etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
  290. etb_lock(t);
  291. dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
  292. out:
  293. return ret;
  294. out_unmap:
  295. amba_set_drvdata(dev, NULL);
  296. iounmap(t->etb_regs);
  297. out_release:
  298. amba_release_regions(dev);
  299. return ret;
  300. }
  301. static int etb_remove(struct amba_device *dev)
  302. {
  303. struct tracectx *t = amba_get_drvdata(dev);
  304. amba_set_drvdata(dev, NULL);
  305. iounmap(t->etb_regs);
  306. t->etb_regs = NULL;
  307. clk_disable(t->emu_clk);
  308. clk_put(t->emu_clk);
  309. amba_release_regions(dev);
  310. return 0;
  311. }
  312. static struct amba_id etb_ids[] = {
  313. {
  314. .id = 0x0003b907,
  315. .mask = 0x0007ffff,
  316. },
  317. { 0, 0 },
  318. };
  319. static struct amba_driver etb_driver = {
  320. .drv = {
  321. .name = "etb",
  322. .owner = THIS_MODULE,
  323. },
  324. .probe = etb_probe,
  325. .remove = etb_remove,
  326. .id_table = etb_ids,
  327. };
  328. /* use a sysfs file "trace_running" to start/stop tracing */
  329. static ssize_t trace_running_show(struct kobject *kobj,
  330. struct kobj_attribute *attr,
  331. char *buf)
  332. {
  333. return sprintf(buf, "%x\n", trace_isrunning(&tracer));
  334. }
  335. static ssize_t trace_running_store(struct kobject *kobj,
  336. struct kobj_attribute *attr,
  337. const char *buf, size_t n)
  338. {
  339. unsigned int value;
  340. int ret;
  341. if (sscanf(buf, "%u", &value) != 1)
  342. return -EINVAL;
  343. mutex_lock(&tracer.mutex);
  344. ret = value ? trace_start(&tracer) : trace_stop(&tracer);
  345. mutex_unlock(&tracer.mutex);
  346. return ret ? : n;
  347. }
  348. static struct kobj_attribute trace_running_attr =
  349. __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
  350. static ssize_t trace_info_show(struct kobject *kobj,
  351. struct kobj_attribute *attr,
  352. char *buf)
  353. {
  354. u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
  355. int datalen;
  356. etb_unlock(&tracer);
  357. datalen = etb_getdatalen(&tracer);
  358. etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
  359. etb_ra = etb_readl(&tracer, ETBR_READADDR);
  360. etb_st = etb_readl(&tracer, ETBR_STATUS);
  361. etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
  362. etb_lock(&tracer);
  363. etm_unlock(&tracer);
  364. etm_ctrl = etm_readl(&tracer, ETMR_CTRL);
  365. etm_st = etm_readl(&tracer, ETMR_STATUS);
  366. etm_lock(&tracer);
  367. return sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
  368. "ETBR_WRITEADDR:\t%08x\n"
  369. "ETBR_READADDR:\t%08x\n"
  370. "ETBR_STATUS:\t%08x\n"
  371. "ETBR_FORMATTERCTRL:\t%08x\n"
  372. "ETMR_CTRL:\t%08x\n"
  373. "ETMR_STATUS:\t%08x\n",
  374. datalen,
  375. tracer.ncmppairs,
  376. etb_wa,
  377. etb_ra,
  378. etb_st,
  379. etb_fc,
  380. etm_ctrl,
  381. etm_st
  382. );
  383. }
  384. static struct kobj_attribute trace_info_attr =
  385. __ATTR(trace_info, 0444, trace_info_show, NULL);
  386. static ssize_t trace_mode_show(struct kobject *kobj,
  387. struct kobj_attribute *attr,
  388. char *buf)
  389. {
  390. return sprintf(buf, "%d %d\n",
  391. !!(tracer.flags & TRACER_CYCLE_ACC),
  392. tracer.etm_portsz);
  393. }
  394. static ssize_t trace_mode_store(struct kobject *kobj,
  395. struct kobj_attribute *attr,
  396. const char *buf, size_t n)
  397. {
  398. unsigned int cycacc, portsz;
  399. if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
  400. return -EINVAL;
  401. mutex_lock(&tracer.mutex);
  402. if (cycacc)
  403. tracer.flags |= TRACER_CYCLE_ACC;
  404. else
  405. tracer.flags &= ~TRACER_CYCLE_ACC;
  406. tracer.etm_portsz = portsz & 0x0f;
  407. mutex_unlock(&tracer.mutex);
  408. return n;
  409. }
  410. static struct kobj_attribute trace_mode_attr =
  411. __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
  412. static int __devinit etm_probe(struct amba_device *dev, const struct amba_id *id)
  413. {
  414. struct tracectx *t = &tracer;
  415. int ret = 0;
  416. if (t->etm_regs) {
  417. dev_dbg(&dev->dev, "ETM already initialized\n");
  418. ret = -EBUSY;
  419. goto out;
  420. }
  421. ret = amba_request_regions(dev, NULL);
  422. if (ret)
  423. goto out;
  424. t->etm_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
  425. if (!t->etm_regs) {
  426. ret = -ENOMEM;
  427. goto out_release;
  428. }
  429. amba_set_drvdata(dev, t);
  430. mutex_init(&t->mutex);
  431. t->dev = &dev->dev;
  432. t->flags = TRACER_CYCLE_ACC;
  433. t->etm_portsz = 1;
  434. etm_unlock(t);
  435. (void)etm_readl(t, ETMMR_PDSR);
  436. /* dummy first read */
  437. (void)etm_readl(&tracer, ETMMR_OSSRR);
  438. t->ncmppairs = etm_readl(t, ETMR_CONFCODE) & 0xf;
  439. etm_writel(t, 0x440, ETMR_CTRL);
  440. etm_lock(t);
  441. ret = sysfs_create_file(&dev->dev.kobj,
  442. &trace_running_attr.attr);
  443. if (ret)
  444. goto out_unmap;
  445. /* failing to create any of these two is not fatal */
  446. ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
  447. if (ret)
  448. dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
  449. ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
  450. if (ret)
  451. dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
  452. dev_dbg(t->dev, "ETM AMBA driver initialized.\n");
  453. out:
  454. return ret;
  455. out_unmap:
  456. amba_set_drvdata(dev, NULL);
  457. iounmap(t->etm_regs);
  458. out_release:
  459. amba_release_regions(dev);
  460. return ret;
  461. }
  462. static int etm_remove(struct amba_device *dev)
  463. {
  464. struct tracectx *t = amba_get_drvdata(dev);
  465. amba_set_drvdata(dev, NULL);
  466. iounmap(t->etm_regs);
  467. t->etm_regs = NULL;
  468. amba_release_regions(dev);
  469. sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
  470. sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
  471. sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
  472. return 0;
  473. }
  474. static struct amba_id etm_ids[] = {
  475. {
  476. .id = 0x0003b921,
  477. .mask = 0x0007ffff,
  478. },
  479. { 0, 0 },
  480. };
  481. static struct amba_driver etm_driver = {
  482. .drv = {
  483. .name = "etm",
  484. .owner = THIS_MODULE,
  485. },
  486. .probe = etm_probe,
  487. .remove = etm_remove,
  488. .id_table = etm_ids,
  489. };
  490. static int __init etm_init(void)
  491. {
  492. int retval;
  493. retval = amba_driver_register(&etb_driver);
  494. if (retval) {
  495. printk(KERN_ERR "Failed to register etb\n");
  496. return retval;
  497. }
  498. retval = amba_driver_register(&etm_driver);
  499. if (retval) {
  500. amba_driver_unregister(&etb_driver);
  501. printk(KERN_ERR "Failed to probe etm\n");
  502. return retval;
  503. }
  504. /* not being able to install this handler is not fatal */
  505. (void)register_sysrq_key('v', &sysrq_etm_op);
  506. return 0;
  507. }
  508. device_initcall(etm_init);