etm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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/slab.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. 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. int etm_regs_count;
  39. unsigned long flags;
  40. int ncmppairs;
  41. int etm_portsz;
  42. u32 etb_fc;
  43. unsigned long range_start;
  44. unsigned long range_end;
  45. unsigned long data_range_start;
  46. unsigned long data_range_end;
  47. bool dump_initial_etb;
  48. struct device *dev;
  49. struct clk *emu_clk;
  50. struct mutex mutex;
  51. };
  52. static struct tracectx tracer = {
  53. .range_start = (unsigned long)_stext,
  54. .range_end = (unsigned long)_etext,
  55. };
  56. static inline bool trace_isrunning(struct tracectx *t)
  57. {
  58. return !!(t->flags & TRACER_RUNNING);
  59. }
  60. static int etm_setup_address_range(struct tracectx *t, int id, int n,
  61. unsigned long start, unsigned long end, int exclude, int data)
  62. {
  63. u32 flags = ETMAAT_ARM | ETMAAT_IGNCONTEXTID | ETMAAT_IGNSECURITY |
  64. ETMAAT_NOVALCMP;
  65. if (n < 1 || n > t->ncmppairs)
  66. return -EINVAL;
  67. /* comparators and ranges are numbered starting with 1 as opposed
  68. * to bits in a word */
  69. n--;
  70. if (data)
  71. flags |= ETMAAT_DLOADSTORE;
  72. else
  73. flags |= ETMAAT_IEXEC;
  74. /* first comparator for the range */
  75. etm_writel(t, id, flags, ETMR_COMP_ACC_TYPE(n * 2));
  76. etm_writel(t, id, start, ETMR_COMP_VAL(n * 2));
  77. /* second comparator is right next to it */
  78. etm_writel(t, id, flags, ETMR_COMP_ACC_TYPE(n * 2 + 1));
  79. etm_writel(t, id, end, ETMR_COMP_VAL(n * 2 + 1));
  80. if (data) {
  81. flags = exclude ? ETMVDC3_EXCLONLY : 0;
  82. if (exclude)
  83. n += 8;
  84. etm_writel(t, id, flags | BIT(n), ETMR_VIEWDATACTRL3);
  85. } else {
  86. flags = exclude ? ETMTE_INCLEXCL : 0;
  87. etm_writel(t, id, flags | (1 << n), ETMR_TRACEENCTRL);
  88. }
  89. return 0;
  90. }
  91. static int trace_start_etm(struct tracectx *t, int id)
  92. {
  93. u32 v;
  94. unsigned long timeout = TRACER_TIMEOUT;
  95. v = ETMCTRL_OPTS | ETMCTRL_PROGRAM | ETMCTRL_PORTSIZE(t->etm_portsz);
  96. if (t->flags & TRACER_CYCLE_ACC)
  97. v |= ETMCTRL_CYCLEACCURATE;
  98. if (t->flags & TRACER_TRACE_DATA)
  99. v |= ETMCTRL_DATA_DO_ADDR;
  100. etm_unlock(t, id);
  101. etm_writel(t, id, v, ETMR_CTRL);
  102. while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
  103. ;
  104. if (!timeout) {
  105. dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
  106. etm_lock(t, id);
  107. return -EFAULT;
  108. }
  109. if (t->range_start || t->range_end)
  110. etm_setup_address_range(t, id, 1,
  111. t->range_start, t->range_end, 0, 0);
  112. else
  113. etm_writel(t, id, ETMTE_INCLEXCL, ETMR_TRACEENCTRL);
  114. etm_writel(t, id, 0, ETMR_TRACEENCTRL2);
  115. etm_writel(t, id, 0, ETMR_TRACESSCTRL);
  116. etm_writel(t, id, 0x6f, ETMR_TRACEENEVT);
  117. etm_writel(t, id, 0, ETMR_VIEWDATACTRL1);
  118. etm_writel(t, id, 0, ETMR_VIEWDATACTRL2);
  119. if (t->data_range_start || t->data_range_end)
  120. etm_setup_address_range(t, id, 2, t->data_range_start,
  121. t->data_range_end, 0, 1);
  122. else
  123. etm_writel(t, id, ETMVDC3_EXCLONLY, ETMR_VIEWDATACTRL3);
  124. etm_writel(t, id, 0x6f, ETMR_VIEWDATAEVT);
  125. v &= ~ETMCTRL_PROGRAM;
  126. v |= ETMCTRL_PORTSEL;
  127. etm_writel(t, id, v, ETMR_CTRL);
  128. timeout = TRACER_TIMEOUT;
  129. while (etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM && --timeout)
  130. ;
  131. if (!timeout) {
  132. dev_dbg(t->dev, "Waiting for progbit to deassert timed out\n");
  133. etm_lock(t, id);
  134. return -EFAULT;
  135. }
  136. etm_lock(t, id);
  137. return 0;
  138. }
  139. static int trace_start(struct tracectx *t)
  140. {
  141. int ret;
  142. int id;
  143. u32 etb_fc = t->etb_fc;
  144. etb_unlock(t);
  145. t->dump_initial_etb = false;
  146. etb_writel(t, 0, ETBR_WRITEADDR);
  147. etb_writel(t, etb_fc, ETBR_FORMATTERCTRL);
  148. etb_writel(t, 1, ETBR_CTRL);
  149. etb_lock(t);
  150. /* configure etm(s) */
  151. for (id = 0; id < t->etm_regs_count; id++) {
  152. ret = trace_start_etm(t, id);
  153. if (ret)
  154. return ret;
  155. }
  156. t->flags |= TRACER_RUNNING;
  157. return 0;
  158. }
  159. static int trace_stop_etm(struct tracectx *t, int id)
  160. {
  161. unsigned long timeout = TRACER_TIMEOUT;
  162. etm_unlock(t, id);
  163. etm_writel(t, id, 0x441, ETMR_CTRL);
  164. while (!(etm_readl(t, id, ETMR_CTRL) & ETMCTRL_PROGRAM) && --timeout)
  165. ;
  166. if (!timeout) {
  167. dev_dbg(t->dev, "Waiting for progbit to assert timed out\n");
  168. etm_lock(t, id);
  169. return -EFAULT;
  170. }
  171. etm_lock(t, id);
  172. return 0;
  173. }
  174. static int trace_stop(struct tracectx *t)
  175. {
  176. int id;
  177. int ret;
  178. unsigned long timeout = TRACER_TIMEOUT;
  179. u32 etb_fc = t->etb_fc;
  180. for (id = 0; id < t->etm_regs_count; id++) {
  181. ret = trace_stop_etm(t, id);
  182. if (ret)
  183. return ret;
  184. }
  185. etb_unlock(t);
  186. if (etb_fc) {
  187. etb_fc |= ETBFF_STOPFL;
  188. etb_writel(t, t->etb_fc, ETBR_FORMATTERCTRL);
  189. }
  190. etb_writel(t, etb_fc | ETBFF_MANUAL_FLUSH, ETBR_FORMATTERCTRL);
  191. timeout = TRACER_TIMEOUT;
  192. while (etb_readl(t, ETBR_FORMATTERCTRL) &
  193. ETBFF_MANUAL_FLUSH && --timeout)
  194. ;
  195. if (!timeout) {
  196. dev_dbg(t->dev, "Waiting for formatter flush to commence "
  197. "timed out\n");
  198. etb_lock(t);
  199. return -EFAULT;
  200. }
  201. etb_writel(t, 0, ETBR_CTRL);
  202. etb_lock(t);
  203. t->flags &= ~TRACER_RUNNING;
  204. return 0;
  205. }
  206. static int etb_getdatalen(struct tracectx *t)
  207. {
  208. u32 v;
  209. int wp;
  210. v = etb_readl(t, ETBR_STATUS);
  211. if (v & 1)
  212. return t->etb_bufsz;
  213. wp = etb_readl(t, ETBR_WRITEADDR);
  214. return wp;
  215. }
  216. /* sysrq+v will always stop the running trace and leave it at that */
  217. static void etm_dump(void)
  218. {
  219. struct tracectx *t = &tracer;
  220. u32 first = 0;
  221. int length;
  222. if (!t->etb_regs) {
  223. printk(KERN_INFO "No tracing hardware found\n");
  224. return;
  225. }
  226. if (trace_isrunning(t))
  227. trace_stop(t);
  228. etb_unlock(t);
  229. length = etb_getdatalen(t);
  230. if (length == t->etb_bufsz)
  231. first = etb_readl(t, ETBR_WRITEADDR);
  232. etb_writel(t, first, ETBR_READADDR);
  233. printk(KERN_INFO "Trace buffer contents length: %d\n", length);
  234. printk(KERN_INFO "--- ETB buffer begin ---\n");
  235. for (; length; length--)
  236. printk("%08x", cpu_to_be32(etb_readl(t, ETBR_READMEM)));
  237. printk(KERN_INFO "\n--- ETB buffer end ---\n");
  238. etb_lock(t);
  239. }
  240. static void sysrq_etm_dump(int key)
  241. {
  242. if (!mutex_trylock(&tracer.mutex)) {
  243. printk(KERN_INFO "Tracing hardware busy\n");
  244. return;
  245. }
  246. dev_dbg(tracer.dev, "Dumping ETB buffer\n");
  247. etm_dump();
  248. mutex_unlock(&tracer.mutex);
  249. }
  250. static struct sysrq_key_op sysrq_etm_op = {
  251. .handler = sysrq_etm_dump,
  252. .help_msg = "ETM buffer dump",
  253. .action_msg = "etm",
  254. };
  255. static int etb_open(struct inode *inode, struct file *file)
  256. {
  257. if (!tracer.etb_regs)
  258. return -ENODEV;
  259. file->private_data = &tracer;
  260. return nonseekable_open(inode, file);
  261. }
  262. static ssize_t etb_read(struct file *file, char __user *data,
  263. size_t len, loff_t *ppos)
  264. {
  265. int total, i;
  266. long length;
  267. struct tracectx *t = file->private_data;
  268. u32 first = 0;
  269. u32 *buf;
  270. int wpos;
  271. int skip;
  272. long wlength;
  273. loff_t pos = *ppos;
  274. mutex_lock(&t->mutex);
  275. if (trace_isrunning(t)) {
  276. length = 0;
  277. goto out;
  278. }
  279. etb_unlock(t);
  280. total = etb_getdatalen(t);
  281. if (total == 0 && t->dump_initial_etb)
  282. total = t->etb_bufsz;
  283. if (total == t->etb_bufsz)
  284. first = etb_readl(t, ETBR_WRITEADDR);
  285. if (pos > total * 4) {
  286. skip = 0;
  287. wpos = total;
  288. } else {
  289. skip = (int)pos % 4;
  290. wpos = (int)pos / 4;
  291. }
  292. total -= wpos;
  293. first = (first + wpos) % t->etb_bufsz;
  294. etb_writel(t, first, ETBR_READADDR);
  295. wlength = min(total, DIV_ROUND_UP(skip + (int)len, 4));
  296. length = min(total * 4 - skip, (int)len);
  297. buf = vmalloc(wlength * 4);
  298. dev_dbg(t->dev, "ETB read %ld bytes to %lld from %ld words at %d\n",
  299. length, pos, wlength, first);
  300. dev_dbg(t->dev, "ETB buffer length: %d\n", total + wpos);
  301. dev_dbg(t->dev, "ETB status reg: %x\n", etb_readl(t, ETBR_STATUS));
  302. for (i = 0; i < wlength; i++)
  303. buf[i] = etb_readl(t, ETBR_READMEM);
  304. etb_lock(t);
  305. length -= copy_to_user(data, (u8 *)buf + skip, length);
  306. vfree(buf);
  307. *ppos = pos + length;
  308. out:
  309. mutex_unlock(&t->mutex);
  310. return length;
  311. }
  312. static int etb_release(struct inode *inode, struct file *file)
  313. {
  314. /* there's nothing to do here, actually */
  315. return 0;
  316. }
  317. static const struct file_operations etb_fops = {
  318. .owner = THIS_MODULE,
  319. .read = etb_read,
  320. .open = etb_open,
  321. .release = etb_release,
  322. .llseek = no_llseek,
  323. };
  324. static struct miscdevice etb_miscdev = {
  325. .name = "tracebuf",
  326. .minor = 0,
  327. .fops = &etb_fops,
  328. };
  329. static int __devinit etb_probe(struct amba_device *dev, const struct amba_id *id)
  330. {
  331. struct tracectx *t = &tracer;
  332. int ret = 0;
  333. ret = amba_request_regions(dev, NULL);
  334. if (ret)
  335. goto out;
  336. mutex_lock(&t->mutex);
  337. t->etb_regs = ioremap_nocache(dev->res.start, resource_size(&dev->res));
  338. if (!t->etb_regs) {
  339. ret = -ENOMEM;
  340. goto out_release;
  341. }
  342. t->dev = &dev->dev;
  343. t->dump_initial_etb = true;
  344. amba_set_drvdata(dev, t);
  345. etb_unlock(t);
  346. t->etb_bufsz = etb_readl(t, ETBR_DEPTH);
  347. dev_dbg(&dev->dev, "Size: %x\n", t->etb_bufsz);
  348. /* make sure trace capture is disabled */
  349. etb_writel(t, 0, ETBR_CTRL);
  350. etb_writel(t, 0x1000, ETBR_FORMATTERCTRL);
  351. etb_lock(t);
  352. mutex_unlock(&t->mutex);
  353. etb_miscdev.parent = &dev->dev;
  354. ret = misc_register(&etb_miscdev);
  355. if (ret)
  356. goto out_unmap;
  357. /* Get optional clock. Currently used to select clock source on omap3 */
  358. t->emu_clk = clk_get(&dev->dev, "emu_src_ck");
  359. if (IS_ERR(t->emu_clk))
  360. dev_dbg(&dev->dev, "Failed to obtain emu_src_ck.\n");
  361. else
  362. clk_enable(t->emu_clk);
  363. dev_dbg(&dev->dev, "ETB AMBA driver initialized.\n");
  364. out:
  365. return ret;
  366. out_unmap:
  367. mutex_lock(&t->mutex);
  368. amba_set_drvdata(dev, NULL);
  369. iounmap(t->etb_regs);
  370. t->etb_regs = NULL;
  371. out_release:
  372. mutex_unlock(&t->mutex);
  373. amba_release_regions(dev);
  374. return ret;
  375. }
  376. static int etb_remove(struct amba_device *dev)
  377. {
  378. struct tracectx *t = amba_get_drvdata(dev);
  379. amba_set_drvdata(dev, NULL);
  380. iounmap(t->etb_regs);
  381. t->etb_regs = NULL;
  382. if (!IS_ERR(t->emu_clk)) {
  383. clk_disable(t->emu_clk);
  384. clk_put(t->emu_clk);
  385. }
  386. amba_release_regions(dev);
  387. return 0;
  388. }
  389. static struct amba_id etb_ids[] = {
  390. {
  391. .id = 0x0003b907,
  392. .mask = 0x0007ffff,
  393. },
  394. { 0, 0 },
  395. };
  396. static struct amba_driver etb_driver = {
  397. .drv = {
  398. .name = "etb",
  399. .owner = THIS_MODULE,
  400. },
  401. .probe = etb_probe,
  402. .remove = etb_remove,
  403. .id_table = etb_ids,
  404. };
  405. /* use a sysfs file "trace_running" to start/stop tracing */
  406. static ssize_t trace_running_show(struct kobject *kobj,
  407. struct kobj_attribute *attr,
  408. char *buf)
  409. {
  410. return sprintf(buf, "%x\n", trace_isrunning(&tracer));
  411. }
  412. static ssize_t trace_running_store(struct kobject *kobj,
  413. struct kobj_attribute *attr,
  414. const char *buf, size_t n)
  415. {
  416. unsigned int value;
  417. int ret;
  418. if (sscanf(buf, "%u", &value) != 1)
  419. return -EINVAL;
  420. mutex_lock(&tracer.mutex);
  421. if (!tracer.etb_regs)
  422. ret = -ENODEV;
  423. else
  424. ret = value ? trace_start(&tracer) : trace_stop(&tracer);
  425. mutex_unlock(&tracer.mutex);
  426. return ret ? : n;
  427. }
  428. static struct kobj_attribute trace_running_attr =
  429. __ATTR(trace_running, 0644, trace_running_show, trace_running_store);
  430. static ssize_t trace_info_show(struct kobject *kobj,
  431. struct kobj_attribute *attr,
  432. char *buf)
  433. {
  434. u32 etb_wa, etb_ra, etb_st, etb_fc, etm_ctrl, etm_st;
  435. int datalen;
  436. int id;
  437. int ret;
  438. mutex_lock(&tracer.mutex);
  439. if (tracer.etb_regs) {
  440. etb_unlock(&tracer);
  441. datalen = etb_getdatalen(&tracer);
  442. etb_wa = etb_readl(&tracer, ETBR_WRITEADDR);
  443. etb_ra = etb_readl(&tracer, ETBR_READADDR);
  444. etb_st = etb_readl(&tracer, ETBR_STATUS);
  445. etb_fc = etb_readl(&tracer, ETBR_FORMATTERCTRL);
  446. etb_lock(&tracer);
  447. } else {
  448. etb_wa = etb_ra = etb_st = etb_fc = ~0;
  449. datalen = -1;
  450. }
  451. ret = sprintf(buf, "Trace buffer len: %d\nComparator pairs: %d\n"
  452. "ETBR_WRITEADDR:\t%08x\n"
  453. "ETBR_READADDR:\t%08x\n"
  454. "ETBR_STATUS:\t%08x\n"
  455. "ETBR_FORMATTERCTRL:\t%08x\n",
  456. datalen,
  457. tracer.ncmppairs,
  458. etb_wa,
  459. etb_ra,
  460. etb_st,
  461. etb_fc
  462. );
  463. for (id = 0; id < tracer.etm_regs_count; id++) {
  464. etm_unlock(&tracer, id);
  465. etm_ctrl = etm_readl(&tracer, id, ETMR_CTRL);
  466. etm_st = etm_readl(&tracer, id, ETMR_STATUS);
  467. etm_lock(&tracer, id);
  468. ret += sprintf(buf + ret, "ETMR_CTRL:\t%08x\n"
  469. "ETMR_STATUS:\t%08x\n",
  470. etm_ctrl,
  471. etm_st
  472. );
  473. }
  474. mutex_unlock(&tracer.mutex);
  475. return ret;
  476. }
  477. static struct kobj_attribute trace_info_attr =
  478. __ATTR(trace_info, 0444, trace_info_show, NULL);
  479. static ssize_t trace_mode_show(struct kobject *kobj,
  480. struct kobj_attribute *attr,
  481. char *buf)
  482. {
  483. return sprintf(buf, "%d %d\n",
  484. !!(tracer.flags & TRACER_CYCLE_ACC),
  485. tracer.etm_portsz);
  486. }
  487. static ssize_t trace_mode_store(struct kobject *kobj,
  488. struct kobj_attribute *attr,
  489. const char *buf, size_t n)
  490. {
  491. unsigned int cycacc, portsz;
  492. if (sscanf(buf, "%u %u", &cycacc, &portsz) != 2)
  493. return -EINVAL;
  494. mutex_lock(&tracer.mutex);
  495. if (cycacc)
  496. tracer.flags |= TRACER_CYCLE_ACC;
  497. else
  498. tracer.flags &= ~TRACER_CYCLE_ACC;
  499. tracer.etm_portsz = portsz & 0x0f;
  500. mutex_unlock(&tracer.mutex);
  501. return n;
  502. }
  503. static struct kobj_attribute trace_mode_attr =
  504. __ATTR(trace_mode, 0644, trace_mode_show, trace_mode_store);
  505. static ssize_t trace_range_show(struct kobject *kobj,
  506. struct kobj_attribute *attr,
  507. char *buf)
  508. {
  509. return sprintf(buf, "%08lx %08lx\n",
  510. tracer.range_start, tracer.range_end);
  511. }
  512. static ssize_t trace_range_store(struct kobject *kobj,
  513. struct kobj_attribute *attr,
  514. const char *buf, size_t n)
  515. {
  516. unsigned long range_start, range_end;
  517. if (sscanf(buf, "%lx %lx", &range_start, &range_end) != 2)
  518. return -EINVAL;
  519. mutex_lock(&tracer.mutex);
  520. tracer.range_start = range_start;
  521. tracer.range_end = range_end;
  522. mutex_unlock(&tracer.mutex);
  523. return n;
  524. }
  525. static struct kobj_attribute trace_range_attr =
  526. __ATTR(trace_range, 0644, trace_range_show, trace_range_store);
  527. static ssize_t trace_data_range_show(struct kobject *kobj,
  528. struct kobj_attribute *attr,
  529. char *buf)
  530. {
  531. unsigned long range_start;
  532. u64 range_end;
  533. mutex_lock(&tracer.mutex);
  534. range_start = tracer.data_range_start;
  535. range_end = tracer.data_range_end;
  536. if (!range_end && (tracer.flags & TRACER_TRACE_DATA))
  537. range_end = 0x100000000ULL;
  538. mutex_unlock(&tracer.mutex);
  539. return sprintf(buf, "%08lx %08llx\n", range_start, range_end);
  540. }
  541. static ssize_t trace_data_range_store(struct kobject *kobj,
  542. struct kobj_attribute *attr,
  543. const char *buf, size_t n)
  544. {
  545. unsigned long range_start;
  546. u64 range_end;
  547. if (sscanf(buf, "%lx %llx", &range_start, &range_end) != 2)
  548. return -EINVAL;
  549. mutex_lock(&tracer.mutex);
  550. tracer.data_range_start = range_start;
  551. tracer.data_range_end = (unsigned long)range_end;
  552. if (range_end)
  553. tracer.flags |= TRACER_TRACE_DATA;
  554. else
  555. tracer.flags &= ~TRACER_TRACE_DATA;
  556. mutex_unlock(&tracer.mutex);
  557. return n;
  558. }
  559. static struct kobj_attribute trace_data_range_attr =
  560. __ATTR(trace_data_range, 0644,
  561. trace_data_range_show, trace_data_range_store);
  562. static int __devinit etm_probe(struct amba_device *dev, const struct amba_id *id)
  563. {
  564. struct tracectx *t = &tracer;
  565. int ret = 0;
  566. void __iomem **new_regs;
  567. int new_count;
  568. mutex_lock(&t->mutex);
  569. new_count = t->etm_regs_count + 1;
  570. new_regs = krealloc(t->etm_regs,
  571. sizeof(t->etm_regs[0]) * new_count, GFP_KERNEL);
  572. if (!new_regs) {
  573. dev_dbg(&dev->dev, "Failed to allocate ETM register array\n");
  574. ret = -ENOMEM;
  575. goto out;
  576. }
  577. t->etm_regs = new_regs;
  578. ret = amba_request_regions(dev, NULL);
  579. if (ret)
  580. goto out;
  581. t->etm_regs[t->etm_regs_count] =
  582. ioremap_nocache(dev->res.start, resource_size(&dev->res));
  583. if (!t->etm_regs[t->etm_regs_count]) {
  584. ret = -ENOMEM;
  585. goto out_release;
  586. }
  587. amba_set_drvdata(dev, t->etm_regs[t->etm_regs_count]);
  588. t->flags = TRACER_CYCLE_ACC | TRACER_TRACE_DATA;
  589. t->etm_portsz = 1;
  590. etm_unlock(t, t->etm_regs_count);
  591. (void)etm_readl(t, t->etm_regs_count, ETMMR_PDSR);
  592. /* dummy first read */
  593. (void)etm_readl(&tracer, t->etm_regs_count, ETMMR_OSSRR);
  594. t->ncmppairs = etm_readl(t, t->etm_regs_count, ETMR_CONFCODE) & 0xf;
  595. etm_writel(t, t->etm_regs_count, 0x441, ETMR_CTRL);
  596. etm_writel(t, t->etm_regs_count, new_count, ETMR_TRACEIDR);
  597. etm_lock(t, t->etm_regs_count);
  598. ret = sysfs_create_file(&dev->dev.kobj,
  599. &trace_running_attr.attr);
  600. if (ret)
  601. goto out_unmap;
  602. /* failing to create any of these two is not fatal */
  603. ret = sysfs_create_file(&dev->dev.kobj, &trace_info_attr.attr);
  604. if (ret)
  605. dev_dbg(&dev->dev, "Failed to create trace_info in sysfs\n");
  606. ret = sysfs_create_file(&dev->dev.kobj, &trace_mode_attr.attr);
  607. if (ret)
  608. dev_dbg(&dev->dev, "Failed to create trace_mode in sysfs\n");
  609. ret = sysfs_create_file(&dev->dev.kobj, &trace_range_attr.attr);
  610. if (ret)
  611. dev_dbg(&dev->dev, "Failed to create trace_range in sysfs\n");
  612. ret = sysfs_create_file(&dev->dev.kobj, &trace_data_range_attr.attr);
  613. if (ret)
  614. dev_dbg(&dev->dev,
  615. "Failed to create trace_data_range in sysfs\n");
  616. dev_dbg(&dev->dev, "ETM AMBA driver initialized.\n");
  617. /* Enable formatter if there are multiple trace sources */
  618. if (new_count > 1)
  619. t->etb_fc = ETBFF_ENFCONT | ETBFF_ENFTC;
  620. t->etm_regs_count = new_count;
  621. out:
  622. mutex_unlock(&t->mutex);
  623. return ret;
  624. out_unmap:
  625. amba_set_drvdata(dev, NULL);
  626. iounmap(t->etm_regs[t->etm_regs_count]);
  627. out_release:
  628. amba_release_regions(dev);
  629. mutex_unlock(&t->mutex);
  630. return ret;
  631. }
  632. static int etm_remove(struct amba_device *dev)
  633. {
  634. int i;
  635. struct tracectx *t = &tracer;
  636. void __iomem *etm_regs = amba_get_drvdata(dev);
  637. sysfs_remove_file(&dev->dev.kobj, &trace_running_attr.attr);
  638. sysfs_remove_file(&dev->dev.kobj, &trace_info_attr.attr);
  639. sysfs_remove_file(&dev->dev.kobj, &trace_mode_attr.attr);
  640. sysfs_remove_file(&dev->dev.kobj, &trace_range_attr.attr);
  641. sysfs_remove_file(&dev->dev.kobj, &trace_data_range_attr.attr);
  642. amba_set_drvdata(dev, NULL);
  643. mutex_lock(&t->mutex);
  644. for (i = 0; i < t->etm_regs_count; i++)
  645. if (t->etm_regs[i] == etm_regs)
  646. break;
  647. for (; i < t->etm_regs_count - 1; i++)
  648. t->etm_regs[i] = t->etm_regs[i + 1];
  649. t->etm_regs_count--;
  650. if (!t->etm_regs_count) {
  651. kfree(t->etm_regs);
  652. t->etm_regs = NULL;
  653. }
  654. mutex_unlock(&t->mutex);
  655. iounmap(etm_regs);
  656. amba_release_regions(dev);
  657. return 0;
  658. }
  659. static struct amba_id etm_ids[] = {
  660. {
  661. .id = 0x0003b921,
  662. .mask = 0x0007ffff,
  663. },
  664. {
  665. .id = 0x0003b950,
  666. .mask = 0x0007ffff,
  667. },
  668. { 0, 0 },
  669. };
  670. static struct amba_driver etm_driver = {
  671. .drv = {
  672. .name = "etm",
  673. .owner = THIS_MODULE,
  674. },
  675. .probe = etm_probe,
  676. .remove = etm_remove,
  677. .id_table = etm_ids,
  678. };
  679. static int __init etm_init(void)
  680. {
  681. int retval;
  682. mutex_init(&tracer.mutex);
  683. retval = amba_driver_register(&etb_driver);
  684. if (retval) {
  685. printk(KERN_ERR "Failed to register etb\n");
  686. return retval;
  687. }
  688. retval = amba_driver_register(&etm_driver);
  689. if (retval) {
  690. amba_driver_unregister(&etb_driver);
  691. printk(KERN_ERR "Failed to probe etm\n");
  692. return retval;
  693. }
  694. /* not being able to install this handler is not fatal */
  695. (void)register_sysrq_key('v', &sysrq_etm_op);
  696. return 0;
  697. }
  698. device_initcall(etm_init);