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