etm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. };
  255. static struct miscdevice etb_miscdev = {
  256. .name = "tracebuf",
  257. .minor = 0,
  258. .fops = &etb_fops,
  259. };
  260. static int __init etb_probe(struct amba_device *dev, struct amba_id *id)
  261. {
  262. struct tracectx *t = &tracer;
  263. int ret = 0;
  264. ret = amba_request_regions(dev, NULL);
  265. if (ret)
  266. goto out;
  267. t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
  268. if (!t->etb_regs) {
  269. ret = -ENOMEM;
  270. goto out_release;
  271. }
  272. amba_set_drvdata(dev, t);
  273. etb_miscdev.parent = &dev->dev;
  274. ret = misc_register(&etb_miscdev);
  275. if (ret)
  276. goto out_unmap;
  277. t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
  278. if (IS_ERR(t->emu_clk)) {
  279. dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
  280. return -EFAULT;
  281. }
  282. clk_enable(t->emu_clk);
  283. etb_unlock(t);
  284. t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
  285. dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
  286. /* make sure trace capture is disabled */
  287. etb_writel(t, 0, ETBR_CTRL);
  288. etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
  289. etb_lock(t);
  290. dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
  291. out:
  292. return ret;
  293. out_unmap:
  294. amba_set_drvdata(dev, NULL);
  295. iounmap(t->etb_regs);
  296. out_release:
  297. amba_release_regions(dev);
  298. return ret;
  299. }
  300. static int etb_remove(struct amba_device *dev)
  301. {
  302. struct tracectx *t = amba_get_drvdata(dev);
  303. amba_set_drvdata(dev, NULL);
  304. iounmap(t->etb_regs);
  305. t->etb_regs = NULL;
  306. clk_disable(t->emu_clk);
  307. clk_put(t->emu_clk);
  308. amba_release_regions(dev);
  309. return 0;
  310. }
  311. static struct amba_id etb_ids[] = {
  312. {
  313. .id = 0x0003b907,
  314. .mask = 0x0007ffff,
  315. },
  316. { 0, 0 },
  317. };
  318. static struct amba_driver etb_driver = {
  319. .drv = {
  320. .name = "etb",
  321. .owner = THIS_MODULE,
  322. },
  323. .probe = etb_probe,
  324. .remove = etb_remove,
  325. .id_table = etb_ids,
  326. };
  327. /* use a sysfs file "trace_running" to start/stop tracing */
  328. static ssize_t trace_running_show(struct kobject *kobj,
  329. struct kobj_attribute *attr,
  330. char *buf)
  331. {
  332. return sprintf(buf, "%x\n", trace_isrunning(&tracer));
  333. }
  334. static ssize_t trace_running_store(struct kobject *kobj,
  335. struct kobj_attribute *attr,
  336. const char *buf, size_t n)
  337. {
  338. unsigned int value;
  339. int ret;
  340. if (sscanf(buf, "%u", &value) != 1)
  341. return -EINVAL;
  342. mutex_lock(&tracer.mutex);
  343. ret = value ? trace_start(&tracer) : trace_stop(&tracer);
  344. mutex_unlock(&tracer.mutex);
  345. return ret ? : n;
  346. }
  347. static struct kobj_attribute trace_running_attr =
  348. __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
  349. static ssize_t trace_info_show(struct kobject *kobj,
  350. struct kobj_attribute *attr,
  351. char *buf)
  352. {
  353. u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
  354. int datalen;
  355. etb_unlock(&tracer);
  356. datalen = etb_getdatalen(&tracer);
  357. etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
  358. etb_ra = etb_readl(&tracer, ETBR_READADDR);
  359. etb_st = etb_readl(&tracer, ETBR_STATUS);
  360. etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
  361. etb_lock(&tracer);
  362. etm_unlock(&tracer);
  363. etm_ctrl = etm_readl(&tracer, ETMR_CTRL);
  364. etm_st = etm_readl(&tracer, ETMR_STATUS);
  365. etm_lock(&tracer);
  366. return sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
  367. "ETBR_WRITEADDR:\t%08x\n"
  368. "ETBR_READADDR:\t%08x\n"
  369. "ETBR_STATUS:\t%08x\n"
  370. "ETBR_FORMATTERCTRL:\t%08x\n"
  371. "ETMR_CTRL:\t%08x\n"
  372. "ETMR_STATUS:\t%08x\n",
  373. datalen,
  374. tracer.ncmppairs,
  375. etb_wa,
  376. etb_ra,
  377. etb_st,
  378. etb_fc,
  379. etm_ctrl,
  380. etm_st
  381. );
  382. }
  383. static struct kobj_attribute trace_info_attr =
  384. __ATTR(trace_info, 0444, trace_info_show, NULL);
  385. static ssize_t trace_mode_show(struct kobject *kobj,
  386. struct kobj_attribute *attr,
  387. char *buf)
  388. {
  389. return sprintf(buf, "%d %d\n",
  390. !!(tracer.flags & TRACER_CYCLE_ACC),
  391. tracer.etm_portsz);
  392. }
  393. static ssize_t trace_mode_store(struct kobject *kobj,
  394. struct kobj_attribute *attr,
  395. const char *buf, size_t n)
  396. {
  397. unsigned int cycacc, portsz;
  398. if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
  399. return -EINVAL;
  400. mutex_lock(&tracer.mutex);
  401. if (cycacc)
  402. tracer.flags |= TRACER_CYCLE_ACC;
  403. else
  404. tracer.flags &= ~TRACER_CYCLE_ACC;
  405. tracer.etm_portsz = portsz & 0x0f;
  406. mutex_unlock(&tracer.mutex);
  407. return n;
  408. }
  409. static struct kobj_attribute trace_mode_attr =
  410. __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
  411. static int __init etm_probe(struct amba_device *dev, struct amba_id *id)
  412. {
  413. struct tracectx *t = &tracer;
  414. int ret = 0;
  415. if (t->etm_regs) {
  416. dev_dbg(&dev->dev, "ETM already initialized\n");
  417. ret = -EBUSY;
  418. goto out;
  419. }
  420. ret = amba_request_regions(dev, NULL);
  421. if (ret)
  422. goto out;
  423. t->etm_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
  424. if (!t->etm_regs) {
  425. ret = -ENOMEM;
  426. goto out_release;
  427. }
  428. amba_set_drvdata(dev, t);
  429. mutex_init(&t->mutex);
  430. t->dev = &dev->dev;
  431. t->flags = TRACER_CYCLE_ACC;
  432. t->etm_portsz = 1;
  433. etm_unlock(t);
  434. (void)etm_readl(t, ETMMR_PDSR);
  435. /* dummy first read */
  436. (void)etm_readl(&tracer, ETMMR_OSSRR);
  437. t->ncmppairs = etm_readl(t, ETMR_CONFCODE) & 0xf;
  438. etm_writel(t, 0x440, ETMR_CTRL);
  439. etm_lock(t);
  440. ret = sysfs_create_file(&dev->dev.kobj,
  441. &trace_running_attr.attr);
  442. if (ret)
  443. goto out_unmap;
  444. /* failing to create any of these two is not fatal */
  445. ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
  446. if (ret)
  447. dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
  448. ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
  449. if (ret)
  450. dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
  451. dev_dbg(t->dev, "ETM AMBA driver initialized.\n");
  452. out:
  453. return ret;
  454. out_unmap:
  455. amba_set_drvdata(dev, NULL);
  456. iounmap(t->etm_regs);
  457. out_release:
  458. amba_release_regions(dev);
  459. return ret;
  460. }
  461. static int etm_remove(struct amba_device *dev)
  462. {
  463. struct tracectx *t = amba_get_drvdata(dev);
  464. amba_set_drvdata(dev, NULL);
  465. iounmap(t->etm_regs);
  466. t->etm_regs = NULL;
  467. amba_release_regions(dev);
  468. sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
  469. sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
  470. sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
  471. return 0;
  472. }
  473. static struct amba_id etm_ids[] = {
  474. {
  475. .id = 0x0003b921,
  476. .mask = 0x0007ffff,
  477. },
  478. { 0, 0 },
  479. };
  480. static struct amba_driver etm_driver = {
  481. .drv = {
  482. .name = "etm",
  483. .owner = THIS_MODULE,
  484. },
  485. .probe = etm_probe,
  486. .remove = etm_remove,
  487. .id_table = etm_ids,
  488. };
  489. static int __init etm_init(void)
  490. {
  491. int retval;
  492. retval = amba_driver_register(&etb_driver);
  493. if (retval) {
  494. printk(KERN_ERR "Failed to register etb\n");
  495. return retval;
  496. }
  497. retval = amba_driver_register(&etm_driver);
  498. if (retval) {
  499. amba_driver_unregister(&etb_driver);
  500. printk(KERN_ERR "Failed to probe etm\n");
  501. return retval;
  502. }
  503. /* not being able to install this handler is not fatal */
  504. (void)register_sysrq_key('v', &sysrq_etm_op);
  505. return 0;
  506. }
  507. device_initcall(etm_init);