coresight-etm4x.c 29 KB

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