coresight-etm4x.c 29 KB

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