coresight-etm4x.c 29 KB

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