coresight-etm4x.c 29 KB

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