coresight-etm4x.c 29 KB

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