coresight-etm4x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* Copyright (c) 2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/device.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. #include <linux/fs.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/smp.h>
  23. #include <linux/sysfs.h>
  24. #include <linux/stat.h>
  25. #include <linux/clk.h>
  26. #include <linux/cpu.h>
  27. #include <linux/coresight.h>
  28. #include <linux/coresight-pmu.h>
  29. #include <linux/pm_wakeup.h>
  30. #include <linux/amba/bus.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/perf_event.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/perf_event.h>
  36. #include <asm/sections.h>
  37. #include <asm/local.h>
  38. #include "coresight-etm4x.h"
  39. #include "coresight-etm-perf.h"
  40. static int boot_enable;
  41. module_param_named(boot_enable, boot_enable, int, S_IRUGO);
  42. /* The number of ETMv4 currently registered */
  43. static int etm4_count;
  44. static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
  45. static void etm4_set_default(struct etmv4_config *config);
  46. static enum cpuhp_state hp_online;
  47. static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
  48. {
  49. /* Writing any value to ETMOSLAR unlocks the trace registers */
  50. writel_relaxed(0x0, drvdata->base + TRCOSLAR);
  51. drvdata->os_unlock = true;
  52. isb();
  53. }
  54. static bool etm4_arch_supported(u8 arch)
  55. {
  56. switch (arch) {
  57. case ETM_ARCH_V4:
  58. break;
  59. default:
  60. return false;
  61. }
  62. return true;
  63. }
  64. static int etm4_cpu_id(struct coresight_device *csdev)
  65. {
  66. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  67. return drvdata->cpu;
  68. }
  69. static int etm4_trace_id(struct coresight_device *csdev)
  70. {
  71. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  72. unsigned long flags;
  73. int trace_id = -1;
  74. if (!local_read(&drvdata->mode))
  75. return drvdata->trcid;
  76. spin_lock_irqsave(&drvdata->spinlock, flags);
  77. CS_UNLOCK(drvdata->base);
  78. trace_id = readl_relaxed(drvdata->base + TRCTRACEIDR);
  79. trace_id &= ETM_TRACEID_MASK;
  80. CS_LOCK(drvdata->base);
  81. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  82. return trace_id;
  83. }
  84. static void etm4_enable_hw(void *info)
  85. {
  86. int i;
  87. struct etmv4_drvdata *drvdata = info;
  88. struct etmv4_config *config = &drvdata->config;
  89. CS_UNLOCK(drvdata->base);
  90. etm4_os_unlock(drvdata);
  91. /* Disable the trace unit before programming trace registers */
  92. writel_relaxed(0, drvdata->base + TRCPRGCTLR);
  93. /* wait for TRCSTATR.IDLE to go up */
  94. if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
  95. dev_err(drvdata->dev,
  96. "timeout observed when probing at offset %#x\n",
  97. TRCSTATR);
  98. writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
  99. writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
  100. /* nothing specific implemented */
  101. writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
  102. writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
  103. writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
  104. writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
  105. writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
  106. writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
  107. writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
  108. writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
  109. writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
  110. writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
  111. writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
  112. writel_relaxed(config->vissctlr,
  113. drvdata->base + TRCVISSCTLR);
  114. writel_relaxed(config->vipcssctlr,
  115. drvdata->base + TRCVIPCSSCTLR);
  116. for (i = 0; i < drvdata->nrseqstate - 1; i++)
  117. writel_relaxed(config->seq_ctrl[i],
  118. drvdata->base + TRCSEQEVRn(i));
  119. writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
  120. writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
  121. writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
  122. for (i = 0; i < drvdata->nr_cntr; i++) {
  123. writel_relaxed(config->cntrldvr[i],
  124. drvdata->base + TRCCNTRLDVRn(i));
  125. writel_relaxed(config->cntr_ctrl[i],
  126. drvdata->base + TRCCNTCTLRn(i));
  127. writel_relaxed(config->cntr_val[i],
  128. drvdata->base + TRCCNTVRn(i));
  129. }
  130. /* Resource selector pair 0 is always implemented and reserved */
  131. for (i = 0; i < drvdata->nr_resource * 2; i++)
  132. writel_relaxed(config->res_ctrl[i],
  133. drvdata->base + TRCRSCTLRn(i));
  134. for (i = 0; i < drvdata->nr_ss_cmp; i++) {
  135. writel_relaxed(config->ss_ctrl[i],
  136. drvdata->base + TRCSSCCRn(i));
  137. writel_relaxed(config->ss_status[i],
  138. drvdata->base + TRCSSCSRn(i));
  139. writel_relaxed(config->ss_pe_cmp[i],
  140. drvdata->base + TRCSSPCICRn(i));
  141. }
  142. for (i = 0; i < drvdata->nr_addr_cmp; i++) {
  143. writeq_relaxed(config->addr_val[i],
  144. drvdata->base + TRCACVRn(i));
  145. writeq_relaxed(config->addr_acc[i],
  146. drvdata->base + TRCACATRn(i));
  147. }
  148. for (i = 0; i < drvdata->numcidc; i++)
  149. writeq_relaxed(config->ctxid_pid[i],
  150. drvdata->base + TRCCIDCVRn(i));
  151. writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
  152. writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
  153. for (i = 0; i < drvdata->numvmidc; i++)
  154. writeq_relaxed(config->vmid_val[i],
  155. drvdata->base + TRCVMIDCVRn(i));
  156. writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
  157. writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
  158. /* Enable the trace unit */
  159. writel_relaxed(1, drvdata->base + TRCPRGCTLR);
  160. /* wait for TRCSTATR.IDLE to go back down to '0' */
  161. if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
  162. dev_err(drvdata->dev,
  163. "timeout observed when probing at offset %#x\n",
  164. TRCSTATR);
  165. CS_LOCK(drvdata->base);
  166. dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
  167. }
  168. static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
  169. struct perf_event_attr *attr)
  170. {
  171. struct etmv4_config *config = &drvdata->config;
  172. if (!attr)
  173. return -EINVAL;
  174. /* Clear configuration from previous run */
  175. memset(config, 0, sizeof(struct etmv4_config));
  176. if (attr->exclude_kernel)
  177. config->mode = ETM_MODE_EXCL_KERN;
  178. if (attr->exclude_user)
  179. config->mode = ETM_MODE_EXCL_USER;
  180. /* Always start from the default config */
  181. etm4_set_default(config);
  182. /*
  183. * By default the tracers are configured to trace the whole address
  184. * range. Narrow the field only if requested by user space.
  185. */
  186. if (config->mode)
  187. etm4_config_trace_mode(config);
  188. /* Go from generic option to ETMv4 specifics */
  189. if (attr->config & BIT(ETM_OPT_CYCACC))
  190. config->cfg |= ETMv4_MODE_CYCACC;
  191. if (attr->config & BIT(ETM_OPT_TS))
  192. config->cfg |= ETMv4_MODE_TIMESTAMP;
  193. return 0;
  194. }
  195. static int etm4_enable_perf(struct coresight_device *csdev,
  196. struct perf_event_attr *attr)
  197. {
  198. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  199. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  200. return -EINVAL;
  201. /* Configure the tracer based on the session's specifics */
  202. etm4_parse_event_config(drvdata, attr);
  203. /* And enable it */
  204. etm4_enable_hw(drvdata);
  205. return 0;
  206. }
  207. static int etm4_enable_sysfs(struct coresight_device *csdev)
  208. {
  209. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  210. int ret;
  211. spin_lock(&drvdata->spinlock);
  212. /*
  213. * Executing etm4_enable_hw on the cpu whose ETM is being enabled
  214. * ensures that register writes occur when cpu is powered.
  215. */
  216. ret = smp_call_function_single(drvdata->cpu,
  217. etm4_enable_hw, drvdata, 1);
  218. if (ret)
  219. goto err;
  220. drvdata->sticky_enable = true;
  221. spin_unlock(&drvdata->spinlock);
  222. dev_info(drvdata->dev, "ETM tracing enabled\n");
  223. return 0;
  224. err:
  225. spin_unlock(&drvdata->spinlock);
  226. return ret;
  227. }
  228. static int etm4_enable(struct coresight_device *csdev,
  229. struct perf_event_attr *attr, u32 mode)
  230. {
  231. int ret;
  232. u32 val;
  233. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  234. val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
  235. /* Someone is already using the tracer */
  236. if (val)
  237. return -EBUSY;
  238. switch (mode) {
  239. case CS_MODE_SYSFS:
  240. ret = etm4_enable_sysfs(csdev);
  241. break;
  242. case CS_MODE_PERF:
  243. ret = etm4_enable_perf(csdev, attr);
  244. break;
  245. default:
  246. ret = -EINVAL;
  247. }
  248. /* The tracer didn't start */
  249. if (ret)
  250. local_set(&drvdata->mode, CS_MODE_DISABLED);
  251. return ret;
  252. }
  253. static void etm4_disable_hw(void *info)
  254. {
  255. u32 control;
  256. struct etmv4_drvdata *drvdata = info;
  257. CS_UNLOCK(drvdata->base);
  258. control = readl_relaxed(drvdata->base + TRCPRGCTLR);
  259. /* EN, bit[0] Trace unit enable bit */
  260. control &= ~0x1;
  261. /* make sure everything completes before disabling */
  262. mb();
  263. isb();
  264. writel_relaxed(control, drvdata->base + TRCPRGCTLR);
  265. CS_LOCK(drvdata->base);
  266. dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
  267. }
  268. static int etm4_disable_perf(struct coresight_device *csdev)
  269. {
  270. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  271. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  272. return -EINVAL;
  273. etm4_disable_hw(drvdata);
  274. return 0;
  275. }
  276. static void etm4_disable_sysfs(struct coresight_device *csdev)
  277. {
  278. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  279. /*
  280. * Taking hotplug lock here protects from clocks getting disabled
  281. * with tracing being left on (crash scenario) if user disable occurs
  282. * after cpu online mask indicates the cpu is offline but before the
  283. * DYING hotplug callback is serviced by the ETM driver.
  284. */
  285. get_online_cpus();
  286. spin_lock(&drvdata->spinlock);
  287. /*
  288. * Executing etm4_disable_hw on the cpu whose ETM is being disabled
  289. * ensures that register writes occur when cpu is powered.
  290. */
  291. smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
  292. spin_unlock(&drvdata->spinlock);
  293. put_online_cpus();
  294. dev_info(drvdata->dev, "ETM tracing disabled\n");
  295. }
  296. static void etm4_disable(struct coresight_device *csdev)
  297. {
  298. u32 mode;
  299. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  300. /*
  301. * For as long as the tracer isn't disabled another entity can't
  302. * change its status. As such we can read the status here without
  303. * fearing it will change under us.
  304. */
  305. mode = local_read(&drvdata->mode);
  306. switch (mode) {
  307. case CS_MODE_DISABLED:
  308. break;
  309. case CS_MODE_SYSFS:
  310. etm4_disable_sysfs(csdev);
  311. break;
  312. case CS_MODE_PERF:
  313. etm4_disable_perf(csdev);
  314. break;
  315. }
  316. if (mode)
  317. local_set(&drvdata->mode, CS_MODE_DISABLED);
  318. }
  319. static const struct coresight_ops_source etm4_source_ops = {
  320. .cpu_id = etm4_cpu_id,
  321. .trace_id = etm4_trace_id,
  322. .enable = etm4_enable,
  323. .disable = etm4_disable,
  324. };
  325. static const struct coresight_ops etm4_cs_ops = {
  326. .source_ops = &etm4_source_ops,
  327. };
  328. static void etm4_init_arch_data(void *info)
  329. {
  330. u32 etmidr0;
  331. u32 etmidr1;
  332. u32 etmidr2;
  333. u32 etmidr3;
  334. u32 etmidr4;
  335. u32 etmidr5;
  336. struct etmv4_drvdata *drvdata = info;
  337. /* Make sure all registers are accessible */
  338. etm4_os_unlock(drvdata);
  339. CS_UNLOCK(drvdata->base);
  340. /* find all capabilities of the tracing unit */
  341. etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
  342. /* INSTP0, bits[2:1] P0 tracing support field */
  343. if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
  344. drvdata->instrp0 = true;
  345. else
  346. drvdata->instrp0 = false;
  347. /* TRCBB, bit[5] Branch broadcast tracing support bit */
  348. if (BMVAL(etmidr0, 5, 5))
  349. drvdata->trcbb = true;
  350. else
  351. drvdata->trcbb = false;
  352. /* TRCCOND, bit[6] Conditional instruction tracing support bit */
  353. if (BMVAL(etmidr0, 6, 6))
  354. drvdata->trccond = true;
  355. else
  356. drvdata->trccond = false;
  357. /* TRCCCI, bit[7] Cycle counting instruction bit */
  358. if (BMVAL(etmidr0, 7, 7))
  359. drvdata->trccci = true;
  360. else
  361. drvdata->trccci = false;
  362. /* RETSTACK, bit[9] Return stack bit */
  363. if (BMVAL(etmidr0, 9, 9))
  364. drvdata->retstack = true;
  365. else
  366. drvdata->retstack = false;
  367. /* NUMEVENT, bits[11:10] Number of events field */
  368. drvdata->nr_event = BMVAL(etmidr0, 10, 11);
  369. /* QSUPP, bits[16:15] Q element support field */
  370. drvdata->q_support = BMVAL(etmidr0, 15, 16);
  371. /* TSSIZE, bits[28:24] Global timestamp size field */
  372. drvdata->ts_size = BMVAL(etmidr0, 24, 28);
  373. /* base architecture of trace unit */
  374. etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
  375. /*
  376. * TRCARCHMIN, bits[7:4] architecture the minor version number
  377. * TRCARCHMAJ, bits[11:8] architecture major versin number
  378. */
  379. drvdata->arch = BMVAL(etmidr1, 4, 11);
  380. /* maximum size of resources */
  381. etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
  382. /* CIDSIZE, bits[9:5] Indicates the Context ID size */
  383. drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
  384. /* VMIDSIZE, bits[14:10] Indicates the VMID size */
  385. drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
  386. /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
  387. drvdata->ccsize = BMVAL(etmidr2, 25, 28);
  388. etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
  389. /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
  390. drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
  391. /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
  392. drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
  393. /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
  394. drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
  395. /*
  396. * TRCERR, bit[24] whether a trace unit can trace a
  397. * system error exception.
  398. */
  399. if (BMVAL(etmidr3, 24, 24))
  400. drvdata->trc_error = true;
  401. else
  402. drvdata->trc_error = false;
  403. /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
  404. if (BMVAL(etmidr3, 25, 25))
  405. drvdata->syncpr = true;
  406. else
  407. drvdata->syncpr = false;
  408. /* STALLCTL, bit[26] is stall control implemented? */
  409. if (BMVAL(etmidr3, 26, 26))
  410. drvdata->stallctl = true;
  411. else
  412. drvdata->stallctl = false;
  413. /* SYSSTALL, bit[27] implementation can support stall control? */
  414. if (BMVAL(etmidr3, 27, 27))
  415. drvdata->sysstall = true;
  416. else
  417. drvdata->sysstall = false;
  418. /* NUMPROC, bits[30:28] the number of PEs available for tracing */
  419. drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
  420. /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
  421. if (BMVAL(etmidr3, 31, 31))
  422. drvdata->nooverflow = true;
  423. else
  424. drvdata->nooverflow = false;
  425. /* number of resources trace unit supports */
  426. etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
  427. /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
  428. drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
  429. /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
  430. drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
  431. /*
  432. * NUMRSPAIR, bits[19:16]
  433. * The number of resource pairs conveyed by the HW starts at 0, i.e a
  434. * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
  435. * As such add 1 to the value of NUMRSPAIR for a better representation.
  436. */
  437. drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
  438. /*
  439. * NUMSSCC, bits[23:20] the number of single-shot
  440. * comparator control for tracing
  441. */
  442. drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
  443. /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
  444. drvdata->numcidc = BMVAL(etmidr4, 24, 27);
  445. /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
  446. drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
  447. etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
  448. /* NUMEXTIN, bits[8:0] number of external inputs implemented */
  449. drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
  450. /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
  451. drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
  452. /* ATBTRIG, bit[22] implementation can support ATB triggers? */
  453. if (BMVAL(etmidr5, 22, 22))
  454. drvdata->atbtrig = true;
  455. else
  456. drvdata->atbtrig = false;
  457. /*
  458. * LPOVERRIDE, bit[23] implementation supports
  459. * low-power state override
  460. */
  461. if (BMVAL(etmidr5, 23, 23))
  462. drvdata->lpoverride = true;
  463. else
  464. drvdata->lpoverride = false;
  465. /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
  466. drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
  467. /* NUMCNTR, bits[30:28] number of counters available for tracing */
  468. drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
  469. CS_LOCK(drvdata->base);
  470. }
  471. static void etm4_set_default(struct etmv4_config *config)
  472. {
  473. if (WARN_ON_ONCE(!config))
  474. return;
  475. /*
  476. * Make default initialisation trace everything
  477. *
  478. * Select the "always true" resource selector on the
  479. * "Enablign Event" line and configure address range comparator
  480. * '0' to trace all the possible address range. From there
  481. * configure the "include/exclude" engine to include address
  482. * range comparator '0'.
  483. */
  484. /* disable all events tracing */
  485. config->eventctrl0 = 0x0;
  486. config->eventctrl1 = 0x0;
  487. /* disable stalling */
  488. config->stall_ctrl = 0x0;
  489. /* enable trace synchronization every 4096 bytes, if available */
  490. config->syncfreq = 0xC;
  491. /* disable timestamp event */
  492. config->ts_ctrl = 0x0;
  493. /* TRCVICTLR::EVENT = 0x01, select the always on logic */
  494. config->vinst_ctrl |= BIT(0);
  495. /*
  496. * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
  497. * in the started state
  498. */
  499. config->vinst_ctrl |= BIT(9);
  500. /*
  501. * Configure address range comparator '0' to encompass all
  502. * possible addresses.
  503. */
  504. /* First half of default address comparator: start at address 0 */
  505. config->addr_val[ETM_DEFAULT_ADDR_COMP] = 0x0;
  506. /* trace instruction addresses */
  507. config->addr_acc[ETM_DEFAULT_ADDR_COMP] &= ~(BIT(0) | BIT(1));
  508. /* EXLEVEL_NS, bits[12:15], only trace application and kernel space */
  509. config->addr_acc[ETM_DEFAULT_ADDR_COMP] |= ETM_EXLEVEL_NS_HYP;
  510. /* EXLEVEL_S, bits[11:8], don't trace anything in secure state */
  511. config->addr_acc[ETM_DEFAULT_ADDR_COMP] |= (ETM_EXLEVEL_S_APP |
  512. ETM_EXLEVEL_S_OS |
  513. ETM_EXLEVEL_S_HYP);
  514. config->addr_type[ETM_DEFAULT_ADDR_COMP] = ETM_ADDR_TYPE_RANGE;
  515. /*
  516. * Second half of default address comparator: go all
  517. * the way to the top.
  518. */
  519. config->addr_val[ETM_DEFAULT_ADDR_COMP + 1] = ~0x0;
  520. /* trace instruction addresses */
  521. config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] &= ~(BIT(0) | BIT(1));
  522. /* Address comparator type must be equal for both halves */
  523. config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] =
  524. config->addr_acc[ETM_DEFAULT_ADDR_COMP];
  525. config->addr_type[ETM_DEFAULT_ADDR_COMP + 1] = ETM_ADDR_TYPE_RANGE;
  526. /*
  527. * Configure the ViewInst function to filter on address range
  528. * comparator '0'.
  529. */
  530. config->viiectlr = BIT(0);
  531. /* no start-stop filtering for ViewInst */
  532. config->vissctlr = 0x0;
  533. }
  534. void etm4_config_trace_mode(struct etmv4_config *config)
  535. {
  536. u32 addr_acc, mode;
  537. mode = config->mode;
  538. mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
  539. /* excluding kernel AND user space doesn't make sense */
  540. WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
  541. /* nothing to do if neither flags are set */
  542. if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
  543. return;
  544. addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
  545. /* clear default config */
  546. addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS);
  547. /*
  548. * EXLEVEL_NS, bits[15:12]
  549. * The Exception levels are:
  550. * Bit[12] Exception level 0 - Application
  551. * Bit[13] Exception level 1 - OS
  552. * Bit[14] Exception level 2 - Hypervisor
  553. * Bit[15] Never implemented
  554. */
  555. if (mode & ETM_MODE_EXCL_KERN)
  556. addr_acc |= ETM_EXLEVEL_NS_OS;
  557. else
  558. addr_acc |= ETM_EXLEVEL_NS_APP;
  559. config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
  560. config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
  561. }
  562. static int etm4_online_cpu(unsigned int cpu)
  563. {
  564. if (!etmdrvdata[cpu])
  565. return 0;
  566. if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
  567. coresight_enable(etmdrvdata[cpu]->csdev);
  568. return 0;
  569. }
  570. static int etm4_starting_cpu(unsigned int cpu)
  571. {
  572. if (!etmdrvdata[cpu])
  573. return 0;
  574. spin_lock(&etmdrvdata[cpu]->spinlock);
  575. if (!etmdrvdata[cpu]->os_unlock) {
  576. etm4_os_unlock(etmdrvdata[cpu]);
  577. etmdrvdata[cpu]->os_unlock = true;
  578. }
  579. if (local_read(&etmdrvdata[cpu]->mode))
  580. etm4_enable_hw(etmdrvdata[cpu]);
  581. spin_unlock(&etmdrvdata[cpu]->spinlock);
  582. return 0;
  583. }
  584. static int etm4_dying_cpu(unsigned int cpu)
  585. {
  586. if (!etmdrvdata[cpu])
  587. return 0;
  588. spin_lock(&etmdrvdata[cpu]->spinlock);
  589. if (local_read(&etmdrvdata[cpu]->mode))
  590. etm4_disable_hw(etmdrvdata[cpu]);
  591. spin_unlock(&etmdrvdata[cpu]->spinlock);
  592. return 0;
  593. }
  594. static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
  595. {
  596. drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
  597. }
  598. static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
  599. {
  600. int ret;
  601. void __iomem *base;
  602. struct device *dev = &adev->dev;
  603. struct coresight_platform_data *pdata = NULL;
  604. struct etmv4_drvdata *drvdata;
  605. struct resource *res = &adev->res;
  606. struct coresight_desc *desc;
  607. struct device_node *np = adev->dev.of_node;
  608. desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
  609. if (!desc)
  610. return -ENOMEM;
  611. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  612. if (!drvdata)
  613. return -ENOMEM;
  614. if (np) {
  615. pdata = of_get_coresight_platform_data(dev, np);
  616. if (IS_ERR(pdata))
  617. return PTR_ERR(pdata);
  618. adev->dev.platform_data = pdata;
  619. }
  620. drvdata->dev = &adev->dev;
  621. dev_set_drvdata(dev, drvdata);
  622. /* Validity for the resource is already checked by the AMBA core */
  623. base = devm_ioremap_resource(dev, res);
  624. if (IS_ERR(base))
  625. return PTR_ERR(base);
  626. drvdata->base = base;
  627. spin_lock_init(&drvdata->spinlock);
  628. drvdata->cpu = pdata ? pdata->cpu : 0;
  629. get_online_cpus();
  630. etmdrvdata[drvdata->cpu] = drvdata;
  631. if (smp_call_function_single(drvdata->cpu,
  632. etm4_init_arch_data, drvdata, 1))
  633. dev_err(dev, "ETM arch init failed\n");
  634. if (!etm4_count++) {
  635. cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING,
  636. "AP_ARM_CORESIGHT4_STARTING",
  637. etm4_starting_cpu, etm4_dying_cpu);
  638. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  639. "AP_ARM_CORESIGHT4_ONLINE",
  640. etm4_online_cpu, NULL);
  641. if (ret < 0)
  642. goto err_arch_supported;
  643. hp_online = ret;
  644. }
  645. put_online_cpus();
  646. if (etm4_arch_supported(drvdata->arch) == false) {
  647. ret = -EINVAL;
  648. goto err_arch_supported;
  649. }
  650. etm4_init_trace_id(drvdata);
  651. etm4_set_default(&drvdata->config);
  652. desc->type = CORESIGHT_DEV_TYPE_SOURCE;
  653. desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
  654. desc->ops = &etm4_cs_ops;
  655. desc->pdata = pdata;
  656. desc->dev = dev;
  657. desc->groups = coresight_etmv4_groups;
  658. drvdata->csdev = coresight_register(desc);
  659. if (IS_ERR(drvdata->csdev)) {
  660. ret = PTR_ERR(drvdata->csdev);
  661. goto err_arch_supported;
  662. }
  663. ret = etm_perf_symlink(drvdata->csdev, true);
  664. if (ret) {
  665. coresight_unregister(drvdata->csdev);
  666. goto err_arch_supported;
  667. }
  668. pm_runtime_put(&adev->dev);
  669. dev_info(dev, "%s initialized\n", (char *)id->data);
  670. if (boot_enable) {
  671. coresight_enable(drvdata->csdev);
  672. drvdata->boot_enable = true;
  673. }
  674. return 0;
  675. err_arch_supported:
  676. if (--etm4_count == 0) {
  677. cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT4_STARTING);
  678. if (hp_online)
  679. cpuhp_remove_state_nocalls(hp_online);
  680. }
  681. return ret;
  682. }
  683. static struct amba_id etm4_ids[] = {
  684. { /* ETM 4.0 - Qualcomm */
  685. .id = 0x0003b95d,
  686. .mask = 0x0003ffff,
  687. .data = "ETM 4.0",
  688. },
  689. { /* ETM 4.0 - Juno board */
  690. .id = 0x000bb95e,
  691. .mask = 0x000fffff,
  692. .data = "ETM 4.0",
  693. },
  694. { /* ETM 4.0 - A72, Maia, HiSilicon */
  695. .id = 0x000bb95a,
  696. .mask = 0x000fffff,
  697. .data = "ETM 4.0",
  698. },
  699. { 0, 0},
  700. };
  701. static struct amba_driver etm4x_driver = {
  702. .drv = {
  703. .name = "coresight-etm4x",
  704. .suppress_bind_attrs = true,
  705. },
  706. .probe = etm4_probe,
  707. .id_table = etm4_ids,
  708. };
  709. builtin_amba_driver(etm4x_driver);