coresight-etm4x.c 28 KB

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