etm.c 13 KB

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