coresight-etm4x.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/init.h>
  8. #include <linux/types.h>
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/err.h>
  12. #include <linux/fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/smp.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/stat.h>
  18. #include <linux/clk.h>
  19. #include <linux/cpu.h>
  20. #include <linux/coresight.h>
  21. #include <linux/coresight-pmu.h>
  22. #include <linux/pm_wakeup.h>
  23. #include <linux/amba/bus.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/perf_event.h>
  27. #include <linux/pm_runtime.h>
  28. #include <asm/sections.h>
  29. #include <asm/local.h>
  30. #include "coresight-etm4x.h"
  31. #include "coresight-etm-perf.h"
  32. static int boot_enable;
  33. module_param_named(boot_enable, boot_enable, int, S_IRUGO);
  34. /* The number of ETMv4 currently registered */
  35. static int etm4_count;
  36. static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
  37. static void etm4_set_default_config(struct etmv4_config *config);
  38. static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
  39. struct perf_event *event);
  40. static enum cpuhp_state hp_online;
  41. static void etm4_os_unlock(struct etmv4_drvdata *drvdata)
  42. {
  43. /* Writing any value to ETMOSLAR unlocks the trace registers */
  44. writel_relaxed(0x0, drvdata->base + TRCOSLAR);
  45. drvdata->os_unlock = true;
  46. isb();
  47. }
  48. static bool etm4_arch_supported(u8 arch)
  49. {
  50. switch (arch) {
  51. case ETM_ARCH_V4:
  52. break;
  53. default:
  54. return false;
  55. }
  56. return true;
  57. }
  58. static int etm4_cpu_id(struct coresight_device *csdev)
  59. {
  60. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  61. return drvdata->cpu;
  62. }
  63. static int etm4_trace_id(struct coresight_device *csdev)
  64. {
  65. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  66. return drvdata->trcid;
  67. }
  68. static void etm4_enable_hw(void *info)
  69. {
  70. int i;
  71. struct etmv4_drvdata *drvdata = info;
  72. struct etmv4_config *config = &drvdata->config;
  73. CS_UNLOCK(drvdata->base);
  74. etm4_os_unlock(drvdata);
  75. /* Disable the trace unit before programming trace registers */
  76. writel_relaxed(0, drvdata->base + TRCPRGCTLR);
  77. /* wait for TRCSTATR.IDLE to go up */
  78. if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
  79. dev_err(drvdata->dev,
  80. "timeout while waiting for Idle Trace Status\n");
  81. writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
  82. writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
  83. /* nothing specific implemented */
  84. writel_relaxed(0x0, drvdata->base + TRCAUXCTLR);
  85. writel_relaxed(config->eventctrl0, drvdata->base + TRCEVENTCTL0R);
  86. writel_relaxed(config->eventctrl1, drvdata->base + TRCEVENTCTL1R);
  87. writel_relaxed(config->stall_ctrl, drvdata->base + TRCSTALLCTLR);
  88. writel_relaxed(config->ts_ctrl, drvdata->base + TRCTSCTLR);
  89. writel_relaxed(config->syncfreq, drvdata->base + TRCSYNCPR);
  90. writel_relaxed(config->ccctlr, drvdata->base + TRCCCCTLR);
  91. writel_relaxed(config->bb_ctrl, drvdata->base + TRCBBCTLR);
  92. writel_relaxed(drvdata->trcid, drvdata->base + TRCTRACEIDR);
  93. writel_relaxed(config->vinst_ctrl, drvdata->base + TRCVICTLR);
  94. writel_relaxed(config->viiectlr, drvdata->base + TRCVIIECTLR);
  95. writel_relaxed(config->vissctlr,
  96. drvdata->base + TRCVISSCTLR);
  97. writel_relaxed(config->vipcssctlr,
  98. drvdata->base + TRCVIPCSSCTLR);
  99. for (i = 0; i < drvdata->nrseqstate - 1; i++)
  100. writel_relaxed(config->seq_ctrl[i],
  101. drvdata->base + TRCSEQEVRn(i));
  102. writel_relaxed(config->seq_rst, drvdata->base + TRCSEQRSTEVR);
  103. writel_relaxed(config->seq_state, drvdata->base + TRCSEQSTR);
  104. writel_relaxed(config->ext_inp, drvdata->base + TRCEXTINSELR);
  105. for (i = 0; i < drvdata->nr_cntr; i++) {
  106. writel_relaxed(config->cntrldvr[i],
  107. drvdata->base + TRCCNTRLDVRn(i));
  108. writel_relaxed(config->cntr_ctrl[i],
  109. drvdata->base + TRCCNTCTLRn(i));
  110. writel_relaxed(config->cntr_val[i],
  111. drvdata->base + TRCCNTVRn(i));
  112. }
  113. /* Resource selector pair 0 is always implemented and reserved */
  114. for (i = 0; i < drvdata->nr_resource * 2; i++)
  115. writel_relaxed(config->res_ctrl[i],
  116. drvdata->base + TRCRSCTLRn(i));
  117. for (i = 0; i < drvdata->nr_ss_cmp; i++) {
  118. writel_relaxed(config->ss_ctrl[i],
  119. drvdata->base + TRCSSCCRn(i));
  120. writel_relaxed(config->ss_status[i],
  121. drvdata->base + TRCSSCSRn(i));
  122. writel_relaxed(config->ss_pe_cmp[i],
  123. drvdata->base + TRCSSPCICRn(i));
  124. }
  125. for (i = 0; i < drvdata->nr_addr_cmp; i++) {
  126. writeq_relaxed(config->addr_val[i],
  127. drvdata->base + TRCACVRn(i));
  128. writeq_relaxed(config->addr_acc[i],
  129. drvdata->base + TRCACATRn(i));
  130. }
  131. for (i = 0; i < drvdata->numcidc; i++)
  132. writeq_relaxed(config->ctxid_pid[i],
  133. drvdata->base + TRCCIDCVRn(i));
  134. writel_relaxed(config->ctxid_mask0, drvdata->base + TRCCIDCCTLR0);
  135. writel_relaxed(config->ctxid_mask1, drvdata->base + TRCCIDCCTLR1);
  136. for (i = 0; i < drvdata->numvmidc; i++)
  137. writeq_relaxed(config->vmid_val[i],
  138. drvdata->base + TRCVMIDCVRn(i));
  139. writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
  140. writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
  141. /*
  142. * Request to keep the trace unit powered and also
  143. * emulation of powerdown
  144. */
  145. writel_relaxed(readl_relaxed(drvdata->base + TRCPDCR) | TRCPDCR_PU,
  146. drvdata->base + TRCPDCR);
  147. /* Enable the trace unit */
  148. writel_relaxed(1, drvdata->base + TRCPRGCTLR);
  149. /* wait for TRCSTATR.IDLE to go back down to '0' */
  150. if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
  151. dev_err(drvdata->dev,
  152. "timeout while waiting for Idle Trace Status\n");
  153. CS_LOCK(drvdata->base);
  154. dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
  155. }
  156. static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
  157. struct perf_event *event)
  158. {
  159. int ret = 0;
  160. struct etmv4_config *config = &drvdata->config;
  161. struct perf_event_attr *attr = &event->attr;
  162. if (!attr) {
  163. ret = -EINVAL;
  164. goto out;
  165. }
  166. /* Clear configuration from previous run */
  167. memset(config, 0, sizeof(struct etmv4_config));
  168. if (attr->exclude_kernel)
  169. config->mode = ETM_MODE_EXCL_KERN;
  170. if (attr->exclude_user)
  171. config->mode = ETM_MODE_EXCL_USER;
  172. /* Always start from the default config */
  173. etm4_set_default_config(config);
  174. /* Configure filters specified on the perf cmd line, if any. */
  175. ret = etm4_set_event_filters(drvdata, event);
  176. if (ret)
  177. goto out;
  178. /* Go from generic option to ETMv4 specifics */
  179. if (attr->config & BIT(ETM_OPT_CYCACC)) {
  180. config->cfg |= BIT(4);
  181. /* TRM: Must program this for cycacc to work */
  182. config->ccctlr = ETM_CYC_THRESHOLD_DEFAULT;
  183. }
  184. if (attr->config & BIT(ETM_OPT_TS))
  185. /* bit[11], Global timestamp tracing bit */
  186. config->cfg |= BIT(11);
  187. /* return stack - enable if selected and supported */
  188. if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack)
  189. /* bit[12], Return stack enable bit */
  190. config->cfg |= BIT(12);
  191. out:
  192. return ret;
  193. }
  194. static int etm4_enable_perf(struct coresight_device *csdev,
  195. struct perf_event *event)
  196. {
  197. int ret = 0;
  198. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  199. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) {
  200. ret = -EINVAL;
  201. goto out;
  202. }
  203. /* Configure the tracer based on the session's specifics */
  204. ret = etm4_parse_event_config(drvdata, event);
  205. if (ret)
  206. goto out;
  207. /* And enable it */
  208. etm4_enable_hw(drvdata);
  209. out:
  210. return ret;
  211. }
  212. static int etm4_enable_sysfs(struct coresight_device *csdev)
  213. {
  214. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  215. int ret;
  216. spin_lock(&drvdata->spinlock);
  217. /*
  218. * Executing etm4_enable_hw on the cpu whose ETM is being enabled
  219. * ensures that register writes occur when cpu is powered.
  220. */
  221. ret = smp_call_function_single(drvdata->cpu,
  222. etm4_enable_hw, drvdata, 1);
  223. if (ret)
  224. goto err;
  225. drvdata->sticky_enable = true;
  226. spin_unlock(&drvdata->spinlock);
  227. dev_info(drvdata->dev, "ETM tracing enabled\n");
  228. return 0;
  229. err:
  230. spin_unlock(&drvdata->spinlock);
  231. return ret;
  232. }
  233. static int etm4_enable(struct coresight_device *csdev,
  234. struct perf_event *event, u32 mode)
  235. {
  236. int ret;
  237. u32 val;
  238. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  239. val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
  240. /* Someone is already using the tracer */
  241. if (val)
  242. return -EBUSY;
  243. switch (mode) {
  244. case CS_MODE_SYSFS:
  245. ret = etm4_enable_sysfs(csdev);
  246. break;
  247. case CS_MODE_PERF:
  248. ret = etm4_enable_perf(csdev, event);
  249. break;
  250. default:
  251. ret = -EINVAL;
  252. }
  253. /* The tracer didn't start */
  254. if (ret)
  255. local_set(&drvdata->mode, CS_MODE_DISABLED);
  256. return ret;
  257. }
  258. static void etm4_disable_hw(void *info)
  259. {
  260. u32 control;
  261. struct etmv4_drvdata *drvdata = info;
  262. CS_UNLOCK(drvdata->base);
  263. /* power can be removed from the trace unit now */
  264. control = readl_relaxed(drvdata->base + TRCPDCR);
  265. control &= ~TRCPDCR_PU;
  266. writel_relaxed(control, drvdata->base + TRCPDCR);
  267. control = readl_relaxed(drvdata->base + TRCPRGCTLR);
  268. /* EN, bit[0] Trace unit enable bit */
  269. control &= ~0x1;
  270. /* make sure everything completes before disabling */
  271. mb();
  272. isb();
  273. writel_relaxed(control, drvdata->base + TRCPRGCTLR);
  274. CS_LOCK(drvdata->base);
  275. dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
  276. }
  277. static int etm4_disable_perf(struct coresight_device *csdev,
  278. struct perf_event *event)
  279. {
  280. u32 control;
  281. struct etm_filters *filters = event->hw.addr_filters;
  282. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  283. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  284. return -EINVAL;
  285. etm4_disable_hw(drvdata);
  286. /*
  287. * Check if the start/stop logic was active when the unit was stopped.
  288. * That way we can re-enable the start/stop logic when the process is
  289. * scheduled again. Configuration of the start/stop logic happens in
  290. * function etm4_set_event_filters().
  291. */
  292. control = readl_relaxed(drvdata->base + TRCVICTLR);
  293. /* TRCVICTLR::SSSTATUS, bit[9] */
  294. filters->ssstatus = (control & BIT(9));
  295. return 0;
  296. }
  297. static void etm4_disable_sysfs(struct coresight_device *csdev)
  298. {
  299. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  300. /*
  301. * Taking hotplug lock here protects from clocks getting disabled
  302. * with tracing being left on (crash scenario) if user disable occurs
  303. * after cpu online mask indicates the cpu is offline but before the
  304. * DYING hotplug callback is serviced by the ETM driver.
  305. */
  306. cpus_read_lock();
  307. spin_lock(&drvdata->spinlock);
  308. /*
  309. * Executing etm4_disable_hw on the cpu whose ETM is being disabled
  310. * ensures that register writes occur when cpu is powered.
  311. */
  312. smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1);
  313. spin_unlock(&drvdata->spinlock);
  314. cpus_read_unlock();
  315. dev_info(drvdata->dev, "ETM tracing disabled\n");
  316. }
  317. static void etm4_disable(struct coresight_device *csdev,
  318. struct perf_event *event)
  319. {
  320. u32 mode;
  321. struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  322. /*
  323. * For as long as the tracer isn't disabled another entity can't
  324. * change its status. As such we can read the status here without
  325. * fearing it will change under us.
  326. */
  327. mode = local_read(&drvdata->mode);
  328. switch (mode) {
  329. case CS_MODE_DISABLED:
  330. break;
  331. case CS_MODE_SYSFS:
  332. etm4_disable_sysfs(csdev);
  333. break;
  334. case CS_MODE_PERF:
  335. etm4_disable_perf(csdev, event);
  336. break;
  337. }
  338. if (mode)
  339. local_set(&drvdata->mode, CS_MODE_DISABLED);
  340. }
  341. static const struct coresight_ops_source etm4_source_ops = {
  342. .cpu_id = etm4_cpu_id,
  343. .trace_id = etm4_trace_id,
  344. .enable = etm4_enable,
  345. .disable = etm4_disable,
  346. };
  347. static const struct coresight_ops etm4_cs_ops = {
  348. .source_ops = &etm4_source_ops,
  349. };
  350. static void etm4_init_arch_data(void *info)
  351. {
  352. u32 etmidr0;
  353. u32 etmidr1;
  354. u32 etmidr2;
  355. u32 etmidr3;
  356. u32 etmidr4;
  357. u32 etmidr5;
  358. struct etmv4_drvdata *drvdata = info;
  359. /* Make sure all registers are accessible */
  360. etm4_os_unlock(drvdata);
  361. CS_UNLOCK(drvdata->base);
  362. /* find all capabilities of the tracing unit */
  363. etmidr0 = readl_relaxed(drvdata->base + TRCIDR0);
  364. /* INSTP0, bits[2:1] P0 tracing support field */
  365. if (BMVAL(etmidr0, 1, 1) && BMVAL(etmidr0, 2, 2))
  366. drvdata->instrp0 = true;
  367. else
  368. drvdata->instrp0 = false;
  369. /* TRCBB, bit[5] Branch broadcast tracing support bit */
  370. if (BMVAL(etmidr0, 5, 5))
  371. drvdata->trcbb = true;
  372. else
  373. drvdata->trcbb = false;
  374. /* TRCCOND, bit[6] Conditional instruction tracing support bit */
  375. if (BMVAL(etmidr0, 6, 6))
  376. drvdata->trccond = true;
  377. else
  378. drvdata->trccond = false;
  379. /* TRCCCI, bit[7] Cycle counting instruction bit */
  380. if (BMVAL(etmidr0, 7, 7))
  381. drvdata->trccci = true;
  382. else
  383. drvdata->trccci = false;
  384. /* RETSTACK, bit[9] Return stack bit */
  385. if (BMVAL(etmidr0, 9, 9))
  386. drvdata->retstack = true;
  387. else
  388. drvdata->retstack = false;
  389. /* NUMEVENT, bits[11:10] Number of events field */
  390. drvdata->nr_event = BMVAL(etmidr0, 10, 11);
  391. /* QSUPP, bits[16:15] Q element support field */
  392. drvdata->q_support = BMVAL(etmidr0, 15, 16);
  393. /* TSSIZE, bits[28:24] Global timestamp size field */
  394. drvdata->ts_size = BMVAL(etmidr0, 24, 28);
  395. /* base architecture of trace unit */
  396. etmidr1 = readl_relaxed(drvdata->base + TRCIDR1);
  397. /*
  398. * TRCARCHMIN, bits[7:4] architecture the minor version number
  399. * TRCARCHMAJ, bits[11:8] architecture major versin number
  400. */
  401. drvdata->arch = BMVAL(etmidr1, 4, 11);
  402. /* maximum size of resources */
  403. etmidr2 = readl_relaxed(drvdata->base + TRCIDR2);
  404. /* CIDSIZE, bits[9:5] Indicates the Context ID size */
  405. drvdata->ctxid_size = BMVAL(etmidr2, 5, 9);
  406. /* VMIDSIZE, bits[14:10] Indicates the VMID size */
  407. drvdata->vmid_size = BMVAL(etmidr2, 10, 14);
  408. /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */
  409. drvdata->ccsize = BMVAL(etmidr2, 25, 28);
  410. etmidr3 = readl_relaxed(drvdata->base + TRCIDR3);
  411. /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */
  412. drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
  413. /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
  414. drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
  415. /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
  416. drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
  417. /*
  418. * TRCERR, bit[24] whether a trace unit can trace a
  419. * system error exception.
  420. */
  421. if (BMVAL(etmidr3, 24, 24))
  422. drvdata->trc_error = true;
  423. else
  424. drvdata->trc_error = false;
  425. /* SYNCPR, bit[25] implementation has a fixed synchronization period? */
  426. if (BMVAL(etmidr3, 25, 25))
  427. drvdata->syncpr = true;
  428. else
  429. drvdata->syncpr = false;
  430. /* STALLCTL, bit[26] is stall control implemented? */
  431. if (BMVAL(etmidr3, 26, 26))
  432. drvdata->stallctl = true;
  433. else
  434. drvdata->stallctl = false;
  435. /* SYSSTALL, bit[27] implementation can support stall control? */
  436. if (BMVAL(etmidr3, 27, 27))
  437. drvdata->sysstall = true;
  438. else
  439. drvdata->sysstall = false;
  440. /* NUMPROC, bits[30:28] the number of PEs available for tracing */
  441. drvdata->nr_pe = BMVAL(etmidr3, 28, 30);
  442. /* NOOVERFLOW, bit[31] is trace overflow prevention supported */
  443. if (BMVAL(etmidr3, 31, 31))
  444. drvdata->nooverflow = true;
  445. else
  446. drvdata->nooverflow = false;
  447. /* number of resources trace unit supports */
  448. etmidr4 = readl_relaxed(drvdata->base + TRCIDR4);
  449. /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */
  450. drvdata->nr_addr_cmp = BMVAL(etmidr4, 0, 3);
  451. /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */
  452. drvdata->nr_pe_cmp = BMVAL(etmidr4, 12, 15);
  453. /*
  454. * NUMRSPAIR, bits[19:16]
  455. * The number of resource pairs conveyed by the HW starts at 0, i.e a
  456. * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on.
  457. * As such add 1 to the value of NUMRSPAIR for a better representation.
  458. */
  459. drvdata->nr_resource = BMVAL(etmidr4, 16, 19) + 1;
  460. /*
  461. * NUMSSCC, bits[23:20] the number of single-shot
  462. * comparator control for tracing
  463. */
  464. drvdata->nr_ss_cmp = BMVAL(etmidr4, 20, 23);
  465. /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */
  466. drvdata->numcidc = BMVAL(etmidr4, 24, 27);
  467. /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */
  468. drvdata->numvmidc = BMVAL(etmidr4, 28, 31);
  469. etmidr5 = readl_relaxed(drvdata->base + TRCIDR5);
  470. /* NUMEXTIN, bits[8:0] number of external inputs implemented */
  471. drvdata->nr_ext_inp = BMVAL(etmidr5, 0, 8);
  472. /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */
  473. drvdata->trcid_size = BMVAL(etmidr5, 16, 21);
  474. /* ATBTRIG, bit[22] implementation can support ATB triggers? */
  475. if (BMVAL(etmidr5, 22, 22))
  476. drvdata->atbtrig = true;
  477. else
  478. drvdata->atbtrig = false;
  479. /*
  480. * LPOVERRIDE, bit[23] implementation supports
  481. * low-power state override
  482. */
  483. if (BMVAL(etmidr5, 23, 23))
  484. drvdata->lpoverride = true;
  485. else
  486. drvdata->lpoverride = false;
  487. /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */
  488. drvdata->nrseqstate = BMVAL(etmidr5, 25, 27);
  489. /* NUMCNTR, bits[30:28] number of counters available for tracing */
  490. drvdata->nr_cntr = BMVAL(etmidr5, 28, 30);
  491. CS_LOCK(drvdata->base);
  492. }
  493. static void etm4_set_default_config(struct etmv4_config *config)
  494. {
  495. /* disable all events tracing */
  496. config->eventctrl0 = 0x0;
  497. config->eventctrl1 = 0x0;
  498. /* disable stalling */
  499. config->stall_ctrl = 0x0;
  500. /* enable trace synchronization every 4096 bytes, if available */
  501. config->syncfreq = 0xC;
  502. /* disable timestamp event */
  503. config->ts_ctrl = 0x0;
  504. /* TRCVICTLR::EVENT = 0x01, select the always on logic */
  505. config->vinst_ctrl |= BIT(0);
  506. }
  507. static u64 etm4_get_access_type(struct etmv4_config *config)
  508. {
  509. u64 access_type = 0;
  510. /*
  511. * EXLEVEL_NS, bits[15:12]
  512. * The Exception levels are:
  513. * Bit[12] Exception level 0 - Application
  514. * Bit[13] Exception level 1 - OS
  515. * Bit[14] Exception level 2 - Hypervisor
  516. * Bit[15] Never implemented
  517. *
  518. * Always stay away from hypervisor mode.
  519. */
  520. access_type = ETM_EXLEVEL_NS_HYP;
  521. if (config->mode & ETM_MODE_EXCL_KERN)
  522. access_type |= ETM_EXLEVEL_NS_OS;
  523. if (config->mode & ETM_MODE_EXCL_USER)
  524. access_type |= ETM_EXLEVEL_NS_APP;
  525. /*
  526. * EXLEVEL_S, bits[11:8], don't trace anything happening
  527. * in secure state.
  528. */
  529. access_type |= (ETM_EXLEVEL_S_APP |
  530. ETM_EXLEVEL_S_OS |
  531. ETM_EXLEVEL_S_HYP);
  532. return access_type;
  533. }
  534. static void etm4_set_comparator_filter(struct etmv4_config *config,
  535. u64 start, u64 stop, int comparator)
  536. {
  537. u64 access_type = etm4_get_access_type(config);
  538. /* First half of default address comparator */
  539. config->addr_val[comparator] = start;
  540. config->addr_acc[comparator] = access_type;
  541. config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
  542. /* Second half of default address comparator */
  543. config->addr_val[comparator + 1] = stop;
  544. config->addr_acc[comparator + 1] = access_type;
  545. config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
  546. /*
  547. * Configure the ViewInst function to include this address range
  548. * comparator.
  549. *
  550. * @comparator is divided by two since it is the index in the
  551. * etmv4_config::addr_val array but register TRCVIIECTLR deals with
  552. * address range comparator _pairs_.
  553. *
  554. * Therefore:
  555. * index 0 -> compatator pair 0
  556. * index 2 -> comparator pair 1
  557. * index 4 -> comparator pair 2
  558. * ...
  559. * index 14 -> comparator pair 7
  560. */
  561. config->viiectlr |= BIT(comparator / 2);
  562. }
  563. static void etm4_set_start_stop_filter(struct etmv4_config *config,
  564. u64 address, int comparator,
  565. enum etm_addr_type type)
  566. {
  567. int shift;
  568. u64 access_type = etm4_get_access_type(config);
  569. /* Configure the comparator */
  570. config->addr_val[comparator] = address;
  571. config->addr_acc[comparator] = access_type;
  572. config->addr_type[comparator] = type;
  573. /*
  574. * Configure ViewInst Start-Stop control register.
  575. * Addresses configured to start tracing go from bit 0 to n-1,
  576. * while those configured to stop tracing from 16 to 16 + n-1.
  577. */
  578. shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
  579. config->vissctlr |= BIT(shift + comparator);
  580. }
  581. static void etm4_set_default_filter(struct etmv4_config *config)
  582. {
  583. u64 start, stop;
  584. /*
  585. * Configure address range comparator '0' to encompass all
  586. * possible addresses.
  587. */
  588. start = 0x0;
  589. stop = ~0x0;
  590. etm4_set_comparator_filter(config, start, stop,
  591. ETM_DEFAULT_ADDR_COMP);
  592. /*
  593. * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
  594. * in the started state
  595. */
  596. config->vinst_ctrl |= BIT(9);
  597. /* No start-stop filtering for ViewInst */
  598. config->vissctlr = 0x0;
  599. }
  600. static void etm4_set_default(struct etmv4_config *config)
  601. {
  602. if (WARN_ON_ONCE(!config))
  603. return;
  604. /*
  605. * Make default initialisation trace everything
  606. *
  607. * Select the "always true" resource selector on the
  608. * "Enablign Event" line and configure address range comparator
  609. * '0' to trace all the possible address range. From there
  610. * configure the "include/exclude" engine to include address
  611. * range comparator '0'.
  612. */
  613. etm4_set_default_config(config);
  614. etm4_set_default_filter(config);
  615. }
  616. static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
  617. {
  618. int nr_comparator, index = 0;
  619. struct etmv4_config *config = &drvdata->config;
  620. /*
  621. * nr_addr_cmp holds the number of comparator _pair_, so time 2
  622. * for the total number of comparators.
  623. */
  624. nr_comparator = drvdata->nr_addr_cmp * 2;
  625. /* Go through the tally of comparators looking for a free one. */
  626. while (index < nr_comparator) {
  627. switch (type) {
  628. case ETM_ADDR_TYPE_RANGE:
  629. if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
  630. config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
  631. return index;
  632. /* Address range comparators go in pairs */
  633. index += 2;
  634. break;
  635. case ETM_ADDR_TYPE_START:
  636. case ETM_ADDR_TYPE_STOP:
  637. if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
  638. return index;
  639. /* Start/stop address can have odd indexes */
  640. index += 1;
  641. break;
  642. default:
  643. return -EINVAL;
  644. }
  645. }
  646. /* If we are here all the comparators have been used. */
  647. return -ENOSPC;
  648. }
  649. static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
  650. struct perf_event *event)
  651. {
  652. int i, comparator, ret = 0;
  653. u64 address;
  654. struct etmv4_config *config = &drvdata->config;
  655. struct etm_filters *filters = event->hw.addr_filters;
  656. if (!filters)
  657. goto default_filter;
  658. /* Sync events with what Perf got */
  659. perf_event_addr_filters_sync(event);
  660. /*
  661. * If there are no filters to deal with simply go ahead with
  662. * the default filter, i.e the entire address range.
  663. */
  664. if (!filters->nr_filters)
  665. goto default_filter;
  666. for (i = 0; i < filters->nr_filters; i++) {
  667. struct etm_filter *filter = &filters->etm_filter[i];
  668. enum etm_addr_type type = filter->type;
  669. /* See if a comparator is free. */
  670. comparator = etm4_get_next_comparator(drvdata, type);
  671. if (comparator < 0) {
  672. ret = comparator;
  673. goto out;
  674. }
  675. switch (type) {
  676. case ETM_ADDR_TYPE_RANGE:
  677. etm4_set_comparator_filter(config,
  678. filter->start_addr,
  679. filter->stop_addr,
  680. comparator);
  681. /*
  682. * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
  683. * in the started state
  684. */
  685. config->vinst_ctrl |= BIT(9);
  686. /* No start-stop filtering for ViewInst */
  687. config->vissctlr = 0x0;
  688. break;
  689. case ETM_ADDR_TYPE_START:
  690. case ETM_ADDR_TYPE_STOP:
  691. /* Get the right start or stop address */
  692. address = (type == ETM_ADDR_TYPE_START ?
  693. filter->start_addr :
  694. filter->stop_addr);
  695. /* Configure comparator */
  696. etm4_set_start_stop_filter(config, address,
  697. comparator, type);
  698. /*
  699. * If filters::ssstatus == 1, trace acquisition was
  700. * started but the process was yanked away before the
  701. * the stop address was hit. As such the start/stop
  702. * logic needs to be re-started so that tracing can
  703. * resume where it left.
  704. *
  705. * The start/stop logic status when a process is
  706. * scheduled out is checked in function
  707. * etm4_disable_perf().
  708. */
  709. if (filters->ssstatus)
  710. config->vinst_ctrl |= BIT(9);
  711. /* No include/exclude filtering for ViewInst */
  712. config->viiectlr = 0x0;
  713. break;
  714. default:
  715. ret = -EINVAL;
  716. goto out;
  717. }
  718. }
  719. goto out;
  720. default_filter:
  721. etm4_set_default_filter(config);
  722. out:
  723. return ret;
  724. }
  725. void etm4_config_trace_mode(struct etmv4_config *config)
  726. {
  727. u32 addr_acc, mode;
  728. mode = config->mode;
  729. mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
  730. /* excluding kernel AND user space doesn't make sense */
  731. WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER));
  732. /* nothing to do if neither flags are set */
  733. if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
  734. return;
  735. addr_acc = config->addr_acc[ETM_DEFAULT_ADDR_COMP];
  736. /* clear default config */
  737. addr_acc &= ~(ETM_EXLEVEL_NS_APP | ETM_EXLEVEL_NS_OS);
  738. /*
  739. * EXLEVEL_NS, bits[15:12]
  740. * The Exception levels are:
  741. * Bit[12] Exception level 0 - Application
  742. * Bit[13] Exception level 1 - OS
  743. * Bit[14] Exception level 2 - Hypervisor
  744. * Bit[15] Never implemented
  745. */
  746. if (mode & ETM_MODE_EXCL_KERN)
  747. addr_acc |= ETM_EXLEVEL_NS_OS;
  748. else
  749. addr_acc |= ETM_EXLEVEL_NS_APP;
  750. config->addr_acc[ETM_DEFAULT_ADDR_COMP] = addr_acc;
  751. config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] = addr_acc;
  752. }
  753. static int etm4_online_cpu(unsigned int cpu)
  754. {
  755. if (!etmdrvdata[cpu])
  756. return 0;
  757. if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
  758. coresight_enable(etmdrvdata[cpu]->csdev);
  759. return 0;
  760. }
  761. static int etm4_starting_cpu(unsigned int cpu)
  762. {
  763. if (!etmdrvdata[cpu])
  764. return 0;
  765. spin_lock(&etmdrvdata[cpu]->spinlock);
  766. if (!etmdrvdata[cpu]->os_unlock) {
  767. etm4_os_unlock(etmdrvdata[cpu]);
  768. etmdrvdata[cpu]->os_unlock = true;
  769. }
  770. if (local_read(&etmdrvdata[cpu]->mode))
  771. etm4_enable_hw(etmdrvdata[cpu]);
  772. spin_unlock(&etmdrvdata[cpu]->spinlock);
  773. return 0;
  774. }
  775. static int etm4_dying_cpu(unsigned int cpu)
  776. {
  777. if (!etmdrvdata[cpu])
  778. return 0;
  779. spin_lock(&etmdrvdata[cpu]->spinlock);
  780. if (local_read(&etmdrvdata[cpu]->mode))
  781. etm4_disable_hw(etmdrvdata[cpu]);
  782. spin_unlock(&etmdrvdata[cpu]->spinlock);
  783. return 0;
  784. }
  785. static void etm4_init_trace_id(struct etmv4_drvdata *drvdata)
  786. {
  787. drvdata->trcid = coresight_get_trace_id(drvdata->cpu);
  788. }
  789. static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
  790. {
  791. int ret;
  792. void __iomem *base;
  793. struct device *dev = &adev->dev;
  794. struct coresight_platform_data *pdata = NULL;
  795. struct etmv4_drvdata *drvdata;
  796. struct resource *res = &adev->res;
  797. struct coresight_desc desc = { 0 };
  798. struct device_node *np = adev->dev.of_node;
  799. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  800. if (!drvdata)
  801. return -ENOMEM;
  802. if (np) {
  803. pdata = of_get_coresight_platform_data(dev, np);
  804. if (IS_ERR(pdata))
  805. return PTR_ERR(pdata);
  806. adev->dev.platform_data = pdata;
  807. }
  808. drvdata->dev = &adev->dev;
  809. dev_set_drvdata(dev, drvdata);
  810. /* Validity for the resource is already checked by the AMBA core */
  811. base = devm_ioremap_resource(dev, res);
  812. if (IS_ERR(base))
  813. return PTR_ERR(base);
  814. drvdata->base = base;
  815. spin_lock_init(&drvdata->spinlock);
  816. drvdata->cpu = pdata ? pdata->cpu : 0;
  817. cpus_read_lock();
  818. etmdrvdata[drvdata->cpu] = drvdata;
  819. if (smp_call_function_single(drvdata->cpu,
  820. etm4_init_arch_data, drvdata, 1))
  821. dev_err(dev, "ETM arch init failed\n");
  822. if (!etm4_count++) {
  823. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ARM_CORESIGHT_STARTING,
  824. "arm/coresight4:starting",
  825. etm4_starting_cpu, etm4_dying_cpu);
  826. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
  827. "arm/coresight4:online",
  828. etm4_online_cpu, NULL);
  829. if (ret < 0)
  830. goto err_arch_supported;
  831. hp_online = ret;
  832. }
  833. cpus_read_unlock();
  834. if (etm4_arch_supported(drvdata->arch) == false) {
  835. ret = -EINVAL;
  836. goto err_arch_supported;
  837. }
  838. etm4_init_trace_id(drvdata);
  839. etm4_set_default(&drvdata->config);
  840. desc.type = CORESIGHT_DEV_TYPE_SOURCE;
  841. desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
  842. desc.ops = &etm4_cs_ops;
  843. desc.pdata = pdata;
  844. desc.dev = dev;
  845. desc.groups = coresight_etmv4_groups;
  846. drvdata->csdev = coresight_register(&desc);
  847. if (IS_ERR(drvdata->csdev)) {
  848. ret = PTR_ERR(drvdata->csdev);
  849. goto err_arch_supported;
  850. }
  851. ret = etm_perf_symlink(drvdata->csdev, true);
  852. if (ret) {
  853. coresight_unregister(drvdata->csdev);
  854. goto err_arch_supported;
  855. }
  856. pm_runtime_put(&adev->dev);
  857. dev_info(dev, "%s initialized\n", (char *)id->data);
  858. if (boot_enable) {
  859. coresight_enable(drvdata->csdev);
  860. drvdata->boot_enable = true;
  861. }
  862. return 0;
  863. err_arch_supported:
  864. if (--etm4_count == 0) {
  865. cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
  866. if (hp_online)
  867. cpuhp_remove_state_nocalls(hp_online);
  868. }
  869. return ret;
  870. }
  871. static const struct amba_id etm4_ids[] = {
  872. { /* ETM 4.0 - Cortex-A53 */
  873. .id = 0x000bb95d,
  874. .mask = 0x000fffff,
  875. .data = "ETM 4.0",
  876. },
  877. { /* ETM 4.0 - Cortex-A57 */
  878. .id = 0x000bb95e,
  879. .mask = 0x000fffff,
  880. .data = "ETM 4.0",
  881. },
  882. { /* ETM 4.0 - A72, Maia, HiSilicon */
  883. .id = 0x000bb95a,
  884. .mask = 0x000fffff,
  885. .data = "ETM 4.0",
  886. },
  887. { 0, 0},
  888. };
  889. static struct amba_driver etm4x_driver = {
  890. .drv = {
  891. .name = "coresight-etm4x",
  892. .suppress_bind_attrs = true,
  893. },
  894. .probe = etm4_probe,
  895. .id_table = etm4_ids,
  896. };
  897. builtin_amba_driver(etm4x_driver);