etm.c 13 KB

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