coresight-etm3x.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  4. *
  5. * Description: CoreSight Program Flow Trace driver
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/init.h>
  10. #include <linux/types.h>
  11. #include <linux/device.h>
  12. #include <linux/io.h>
  13. #include <linux/err.h>
  14. #include <linux/fs.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/smp.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/stat.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/cpu.h>
  22. #include <linux/of.h>
  23. #include <linux/coresight.h>
  24. #include <linux/coresight-pmu.h>
  25. #include <linux/amba/bus.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/clk.h>
  29. #include <linux/perf_event.h>
  30. #include <asm/sections.h>
  31. #include "coresight-etm.h"
  32. #include "coresight-etm-perf.h"
  33. /*
  34. * Not really modular but using module_param is the easiest way to
  35. * remain consistent with existing use cases for now.
  36. */
  37. static int boot_enable;
  38. module_param_named(boot_enable, boot_enable, int, S_IRUGO);
  39. /* The number of ETM/PTM currently registered */
  40. static int etm_count;
  41. static struct etm_drvdata *etmdrvdata[NR_CPUS];
  42. static enum cpuhp_state hp_online;
  43. /*
  44. * Memory mapped writes to clear os lock are not supported on some processors
  45. * and OS lock must be unlocked before any memory mapped access on such
  46. * processors, otherwise memory mapped reads/writes will be invalid.
  47. */
  48. static void etm_os_unlock(struct etm_drvdata *drvdata)
  49. {
  50. /* Writing any value to ETMOSLAR unlocks the trace registers */
  51. etm_writel(drvdata, 0x0, ETMOSLAR);
  52. drvdata->os_unlock = true;
  53. isb();
  54. }
  55. static void etm_set_pwrdwn(struct etm_drvdata *drvdata)
  56. {
  57. u32 etmcr;
  58. /* Ensure pending cp14 accesses complete before setting pwrdwn */
  59. mb();
  60. isb();
  61. etmcr = etm_readl(drvdata, ETMCR);
  62. etmcr |= ETMCR_PWD_DWN;
  63. etm_writel(drvdata, etmcr, ETMCR);
  64. }
  65. static void etm_clr_pwrdwn(struct etm_drvdata *drvdata)
  66. {
  67. u32 etmcr;
  68. etmcr = etm_readl(drvdata, ETMCR);
  69. etmcr &= ~ETMCR_PWD_DWN;
  70. etm_writel(drvdata, etmcr, ETMCR);
  71. /* Ensure pwrup completes before subsequent cp14 accesses */
  72. mb();
  73. isb();
  74. }
  75. static void etm_set_pwrup(struct etm_drvdata *drvdata)
  76. {
  77. u32 etmpdcr;
  78. etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
  79. etmpdcr |= ETMPDCR_PWD_UP;
  80. writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
  81. /* Ensure pwrup completes before subsequent cp14 accesses */
  82. mb();
  83. isb();
  84. }
  85. static void etm_clr_pwrup(struct etm_drvdata *drvdata)
  86. {
  87. u32 etmpdcr;
  88. /* Ensure pending cp14 accesses complete before clearing pwrup */
  89. mb();
  90. isb();
  91. etmpdcr = readl_relaxed(drvdata->base + ETMPDCR);
  92. etmpdcr &= ~ETMPDCR_PWD_UP;
  93. writel_relaxed(etmpdcr, drvdata->base + ETMPDCR);
  94. }
  95. /**
  96. * coresight_timeout_etm - loop until a bit has changed to a specific state.
  97. * @drvdata: etm's private data structure.
  98. * @offset: address of a register, starting from @addr.
  99. * @position: the position of the bit of interest.
  100. * @value: the value the bit should have.
  101. *
  102. * Basically the same as @coresight_timeout except for the register access
  103. * method where we have to account for CP14 configurations.
  104. * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
  105. * TIMEOUT_US has elapsed, which ever happens first.
  106. */
  107. static int coresight_timeout_etm(struct etm_drvdata *drvdata, u32 offset,
  108. int position, int value)
  109. {
  110. int i;
  111. u32 val;
  112. for (i = TIMEOUT_US; i > 0; i--) {
  113. val = etm_readl(drvdata, offset);
  114. /* Waiting on the bit to go from 0 to 1 */
  115. if (value) {
  116. if (val & BIT(position))
  117. return 0;
  118. /* Waiting on the bit to go from 1 to 0 */
  119. } else {
  120. if (!(val & BIT(position)))
  121. return 0;
  122. }
  123. /*
  124. * Delay is arbitrary - the specification doesn't say how long
  125. * we are expected to wait. Extra check required to make sure
  126. * we don't wait needlessly on the last iteration.
  127. */
  128. if (i - 1)
  129. udelay(1);
  130. }
  131. return -EAGAIN;
  132. }
  133. static void etm_set_prog(struct etm_drvdata *drvdata)
  134. {
  135. u32 etmcr;
  136. etmcr = etm_readl(drvdata, ETMCR);
  137. etmcr |= ETMCR_ETM_PRG;
  138. etm_writel(drvdata, etmcr, ETMCR);
  139. /*
  140. * Recommended by spec for cp14 accesses to ensure etmcr write is
  141. * complete before polling etmsr
  142. */
  143. isb();
  144. if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 1)) {
  145. dev_err(drvdata->dev,
  146. "%s: timeout observed when probing at offset %#x\n",
  147. __func__, ETMSR);
  148. }
  149. }
  150. static void etm_clr_prog(struct etm_drvdata *drvdata)
  151. {
  152. u32 etmcr;
  153. etmcr = etm_readl(drvdata, ETMCR);
  154. etmcr &= ~ETMCR_ETM_PRG;
  155. etm_writel(drvdata, etmcr, ETMCR);
  156. /*
  157. * Recommended by spec for cp14 accesses to ensure etmcr write is
  158. * complete before polling etmsr
  159. */
  160. isb();
  161. if (coresight_timeout_etm(drvdata, ETMSR, ETMSR_PROG_BIT, 0)) {
  162. dev_err(drvdata->dev,
  163. "%s: timeout observed when probing at offset %#x\n",
  164. __func__, ETMSR);
  165. }
  166. }
  167. void etm_set_default(struct etm_config *config)
  168. {
  169. int i;
  170. if (WARN_ON_ONCE(!config))
  171. return;
  172. /*
  173. * Taken verbatim from the TRM:
  174. *
  175. * To trace all memory:
  176. * set bit [24] in register 0x009, the ETMTECR1, to 1
  177. * set all other bits in register 0x009, the ETMTECR1, to 0
  178. * set all bits in register 0x007, the ETMTECR2, to 0
  179. * set register 0x008, the ETMTEEVR, to 0x6F (TRUE).
  180. */
  181. config->enable_ctrl1 = BIT(24);
  182. config->enable_ctrl2 = 0x0;
  183. config->enable_event = ETM_HARD_WIRE_RES_A;
  184. config->trigger_event = ETM_DEFAULT_EVENT_VAL;
  185. config->enable_event = ETM_HARD_WIRE_RES_A;
  186. config->seq_12_event = ETM_DEFAULT_EVENT_VAL;
  187. config->seq_21_event = ETM_DEFAULT_EVENT_VAL;
  188. config->seq_23_event = ETM_DEFAULT_EVENT_VAL;
  189. config->seq_31_event = ETM_DEFAULT_EVENT_VAL;
  190. config->seq_32_event = ETM_DEFAULT_EVENT_VAL;
  191. config->seq_13_event = ETM_DEFAULT_EVENT_VAL;
  192. config->timestamp_event = ETM_DEFAULT_EVENT_VAL;
  193. for (i = 0; i < ETM_MAX_CNTR; i++) {
  194. config->cntr_rld_val[i] = 0x0;
  195. config->cntr_event[i] = ETM_DEFAULT_EVENT_VAL;
  196. config->cntr_rld_event[i] = ETM_DEFAULT_EVENT_VAL;
  197. config->cntr_val[i] = 0x0;
  198. }
  199. config->seq_curr_state = 0x0;
  200. config->ctxid_idx = 0x0;
  201. for (i = 0; i < ETM_MAX_CTXID_CMP; i++)
  202. config->ctxid_pid[i] = 0x0;
  203. config->ctxid_mask = 0x0;
  204. /* Setting default to 1024 as per TRM recommendation */
  205. config->sync_freq = 0x400;
  206. }
  207. void etm_config_trace_mode(struct etm_config *config)
  208. {
  209. u32 flags, mode;
  210. mode = config->mode;
  211. mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
  212. /* excluding kernel AND user space doesn't make sense */
  213. if (mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
  214. return;
  215. /* nothing to do if neither flags are set */
  216. if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
  217. return;
  218. flags = (1 << 0 | /* instruction execute */
  219. 3 << 3 | /* ARM instruction */
  220. 0 << 5 | /* No data value comparison */
  221. 0 << 7 | /* No exact mach */
  222. 0 << 8); /* Ignore context ID */
  223. /* No need to worry about single address comparators. */
  224. config->enable_ctrl2 = 0x0;
  225. /* Bit 0 is address range comparator 1 */
  226. config->enable_ctrl1 = ETMTECR1_ADDR_COMP_1;
  227. /*
  228. * On ETMv3.5:
  229. * ETMACTRn[13,11] == Non-secure state comparison control
  230. * ETMACTRn[12,10] == Secure state comparison control
  231. *
  232. * b00 == Match in all modes in this state
  233. * b01 == Do not match in any more in this state
  234. * b10 == Match in all modes excepts user mode in this state
  235. * b11 == Match only in user mode in this state
  236. */
  237. /* Tracing in secure mode is not supported at this time */
  238. flags |= (0 << 12 | 1 << 10);
  239. if (mode & ETM_MODE_EXCL_USER) {
  240. /* exclude user, match all modes except user mode */
  241. flags |= (1 << 13 | 0 << 11);
  242. } else {
  243. /* exclude kernel, match only in user mode */
  244. flags |= (1 << 13 | 1 << 11);
  245. }
  246. /*
  247. * The ETMEEVR register is already set to "hard wire A". As such
  248. * all there is to do is setup an address comparator that spans
  249. * the entire address range and configure the state and mode bits.
  250. */
  251. config->addr_val[0] = (u32) 0x0;
  252. config->addr_val[1] = (u32) ~0x0;
  253. config->addr_acctype[0] = flags;
  254. config->addr_acctype[1] = flags;
  255. config->addr_type[0] = ETM_ADDR_TYPE_RANGE;
  256. config->addr_type[1] = ETM_ADDR_TYPE_RANGE;
  257. }
  258. #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | \
  259. ETMCR_TIMESTAMP_EN | \
  260. ETMCR_RETURN_STACK)
  261. static int etm_parse_event_config(struct etm_drvdata *drvdata,
  262. struct perf_event *event)
  263. {
  264. struct etm_config *config = &drvdata->config;
  265. struct perf_event_attr *attr = &event->attr;
  266. if (!attr)
  267. return -EINVAL;
  268. /* Clear configuration from previous run */
  269. memset(config, 0, sizeof(struct etm_config));
  270. if (attr->exclude_kernel)
  271. config->mode = ETM_MODE_EXCL_KERN;
  272. if (attr->exclude_user)
  273. config->mode = ETM_MODE_EXCL_USER;
  274. /* Always start from the default config */
  275. etm_set_default(config);
  276. /*
  277. * By default the tracers are configured to trace the whole address
  278. * range. Narrow the field only if requested by user space.
  279. */
  280. if (config->mode)
  281. etm_config_trace_mode(config);
  282. /*
  283. * At this time only cycle accurate, return stack and timestamp
  284. * options are available.
  285. */
  286. if (attr->config & ~ETM3X_SUPPORTED_OPTIONS)
  287. return -EINVAL;
  288. config->ctrl = attr->config;
  289. /*
  290. * Possible to have cores with PTM (supports ret stack) and ETM
  291. * (never has ret stack) on the same SoC. So if we have a request
  292. * for return stack that can't be honoured on this core then
  293. * clear the bit - trace will still continue normally
  294. */
  295. if ((config->ctrl & ETMCR_RETURN_STACK) &&
  296. !(drvdata->etmccer & ETMCCER_RETSTACK))
  297. config->ctrl &= ~ETMCR_RETURN_STACK;
  298. return 0;
  299. }
  300. static int etm_enable_hw(struct etm_drvdata *drvdata)
  301. {
  302. int i, rc;
  303. u32 etmcr;
  304. struct etm_config *config = &drvdata->config;
  305. CS_UNLOCK(drvdata->base);
  306. /* Turn engine on */
  307. etm_clr_pwrdwn(drvdata);
  308. /* Apply power to trace registers */
  309. etm_set_pwrup(drvdata);
  310. /* Make sure all registers are accessible */
  311. etm_os_unlock(drvdata);
  312. rc = coresight_claim_device_unlocked(drvdata->base);
  313. if (rc)
  314. goto done;
  315. etm_set_prog(drvdata);
  316. etmcr = etm_readl(drvdata, ETMCR);
  317. /* Clear setting from a previous run if need be */
  318. etmcr &= ~ETM3X_SUPPORTED_OPTIONS;
  319. etmcr |= drvdata->port_size;
  320. etmcr |= ETMCR_ETM_EN;
  321. etm_writel(drvdata, config->ctrl | etmcr, ETMCR);
  322. etm_writel(drvdata, config->trigger_event, ETMTRIGGER);
  323. etm_writel(drvdata, config->startstop_ctrl, ETMTSSCR);
  324. etm_writel(drvdata, config->enable_event, ETMTEEVR);
  325. etm_writel(drvdata, config->enable_ctrl1, ETMTECR1);
  326. etm_writel(drvdata, config->fifofull_level, ETMFFLR);
  327. for (i = 0; i < drvdata->nr_addr_cmp; i++) {
  328. etm_writel(drvdata, config->addr_val[i], ETMACVRn(i));
  329. etm_writel(drvdata, config->addr_acctype[i], ETMACTRn(i));
  330. }
  331. for (i = 0; i < drvdata->nr_cntr; i++) {
  332. etm_writel(drvdata, config->cntr_rld_val[i], ETMCNTRLDVRn(i));
  333. etm_writel(drvdata, config->cntr_event[i], ETMCNTENRn(i));
  334. etm_writel(drvdata, config->cntr_rld_event[i],
  335. ETMCNTRLDEVRn(i));
  336. etm_writel(drvdata, config->cntr_val[i], ETMCNTVRn(i));
  337. }
  338. etm_writel(drvdata, config->seq_12_event, ETMSQ12EVR);
  339. etm_writel(drvdata, config->seq_21_event, ETMSQ21EVR);
  340. etm_writel(drvdata, config->seq_23_event, ETMSQ23EVR);
  341. etm_writel(drvdata, config->seq_31_event, ETMSQ31EVR);
  342. etm_writel(drvdata, config->seq_32_event, ETMSQ32EVR);
  343. etm_writel(drvdata, config->seq_13_event, ETMSQ13EVR);
  344. etm_writel(drvdata, config->seq_curr_state, ETMSQR);
  345. for (i = 0; i < drvdata->nr_ext_out; i++)
  346. etm_writel(drvdata, ETM_DEFAULT_EVENT_VAL, ETMEXTOUTEVRn(i));
  347. for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
  348. etm_writel(drvdata, config->ctxid_pid[i], ETMCIDCVRn(i));
  349. etm_writel(drvdata, config->ctxid_mask, ETMCIDCMR);
  350. etm_writel(drvdata, config->sync_freq, ETMSYNCFR);
  351. /* No external input selected */
  352. etm_writel(drvdata, 0x0, ETMEXTINSELR);
  353. etm_writel(drvdata, config->timestamp_event, ETMTSEVR);
  354. /* No auxiliary control selected */
  355. etm_writel(drvdata, 0x0, ETMAUXCR);
  356. etm_writel(drvdata, drvdata->traceid, ETMTRACEIDR);
  357. /* No VMID comparator value selected */
  358. etm_writel(drvdata, 0x0, ETMVMIDCVR);
  359. etm_clr_prog(drvdata);
  360. done:
  361. if (rc)
  362. etm_set_pwrdwn(drvdata);
  363. CS_LOCK(drvdata->base);
  364. dev_dbg(drvdata->dev, "cpu: %d enable smp call done: %d\n",
  365. drvdata->cpu, rc);
  366. return rc;
  367. }
  368. struct etm_enable_arg {
  369. struct etm_drvdata *drvdata;
  370. int rc;
  371. };
  372. static void etm_enable_hw_smp_call(void *info)
  373. {
  374. struct etm_enable_arg *arg = info;
  375. if (WARN_ON(!arg))
  376. return;
  377. arg->rc = etm_enable_hw(arg->drvdata);
  378. }
  379. static int etm_cpu_id(struct coresight_device *csdev)
  380. {
  381. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  382. return drvdata->cpu;
  383. }
  384. int etm_get_trace_id(struct etm_drvdata *drvdata)
  385. {
  386. unsigned long flags;
  387. int trace_id = -1;
  388. if (!drvdata)
  389. goto out;
  390. if (!local_read(&drvdata->mode))
  391. return drvdata->traceid;
  392. pm_runtime_get_sync(drvdata->dev);
  393. spin_lock_irqsave(&drvdata->spinlock, flags);
  394. CS_UNLOCK(drvdata->base);
  395. trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
  396. CS_LOCK(drvdata->base);
  397. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  398. pm_runtime_put(drvdata->dev);
  399. out:
  400. return trace_id;
  401. }
  402. static int etm_trace_id(struct coresight_device *csdev)
  403. {
  404. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  405. return etm_get_trace_id(drvdata);
  406. }
  407. static int etm_enable_perf(struct coresight_device *csdev,
  408. struct perf_event *event)
  409. {
  410. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  411. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  412. return -EINVAL;
  413. /* Configure the tracer based on the session's specifics */
  414. etm_parse_event_config(drvdata, event);
  415. /* And enable it */
  416. return etm_enable_hw(drvdata);
  417. }
  418. static int etm_enable_sysfs(struct coresight_device *csdev)
  419. {
  420. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  421. struct etm_enable_arg arg = { 0 };
  422. int ret;
  423. spin_lock(&drvdata->spinlock);
  424. /*
  425. * Configure the ETM only if the CPU is online. If it isn't online
  426. * hw configuration will take place on the local CPU during bring up.
  427. */
  428. if (cpu_online(drvdata->cpu)) {
  429. arg.drvdata = drvdata;
  430. ret = smp_call_function_single(drvdata->cpu,
  431. etm_enable_hw_smp_call, &arg, 1);
  432. if (!ret)
  433. ret = arg.rc;
  434. if (!ret)
  435. drvdata->sticky_enable = true;
  436. } else {
  437. ret = -ENODEV;
  438. }
  439. spin_unlock(&drvdata->spinlock);
  440. if (!ret)
  441. dev_dbg(drvdata->dev, "ETM tracing enabled\n");
  442. return ret;
  443. }
  444. static int etm_enable(struct coresight_device *csdev,
  445. struct perf_event *event, u32 mode)
  446. {
  447. int ret;
  448. u32 val;
  449. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  450. val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
  451. /* Someone is already using the tracer */
  452. if (val)
  453. return -EBUSY;
  454. switch (mode) {
  455. case CS_MODE_SYSFS:
  456. ret = etm_enable_sysfs(csdev);
  457. break;
  458. case CS_MODE_PERF:
  459. ret = etm_enable_perf(csdev, event);
  460. break;
  461. default:
  462. ret = -EINVAL;
  463. }
  464. /* The tracer didn't start */
  465. if (ret)
  466. local_set(&drvdata->mode, CS_MODE_DISABLED);
  467. return ret;
  468. }
  469. static void etm_disable_hw(void *info)
  470. {
  471. int i;
  472. struct etm_drvdata *drvdata = info;
  473. struct etm_config *config = &drvdata->config;
  474. CS_UNLOCK(drvdata->base);
  475. etm_set_prog(drvdata);
  476. /* Read back sequencer and counters for post trace analysis */
  477. config->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
  478. for (i = 0; i < drvdata->nr_cntr; i++)
  479. config->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
  480. coresight_disclaim_device_unlocked(drvdata->base);
  481. etm_set_pwrdwn(drvdata);
  482. CS_LOCK(drvdata->base);
  483. dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
  484. }
  485. static void etm_disable_perf(struct coresight_device *csdev)
  486. {
  487. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  488. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  489. return;
  490. CS_UNLOCK(drvdata->base);
  491. /* Setting the prog bit disables tracing immediately */
  492. etm_set_prog(drvdata);
  493. /*
  494. * There is no way to know when the tracer will be used again so
  495. * power down the tracer.
  496. */
  497. etm_set_pwrdwn(drvdata);
  498. CS_LOCK(drvdata->base);
  499. }
  500. static void etm_disable_sysfs(struct coresight_device *csdev)
  501. {
  502. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  503. /*
  504. * Taking hotplug lock here protects from clocks getting disabled
  505. * with tracing being left on (crash scenario) if user disable occurs
  506. * after cpu online mask indicates the cpu is offline but before the
  507. * DYING hotplug callback is serviced by the ETM driver.
  508. */
  509. cpus_read_lock();
  510. spin_lock(&drvdata->spinlock);
  511. /*
  512. * Executing etm_disable_hw on the cpu whose ETM is being disabled
  513. * ensures that register writes occur when cpu is powered.
  514. */
  515. smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
  516. spin_unlock(&drvdata->spinlock);
  517. cpus_read_unlock();
  518. dev_dbg(drvdata->dev, "ETM tracing disabled\n");
  519. }
  520. static void etm_disable(struct coresight_device *csdev,
  521. struct perf_event *event)
  522. {
  523. u32 mode;
  524. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  525. /*
  526. * For as long as the tracer isn't disabled another entity can't
  527. * change its status. As such we can read the status here without
  528. * fearing it will change under us.
  529. */
  530. mode = local_read(&drvdata->mode);
  531. switch (mode) {
  532. case CS_MODE_DISABLED:
  533. break;
  534. case CS_MODE_SYSFS:
  535. etm_disable_sysfs(csdev);
  536. break;
  537. case CS_MODE_PERF:
  538. etm_disable_perf(csdev);
  539. break;
  540. default:
  541. WARN_ON_ONCE(mode);
  542. return;
  543. }
  544. if (mode)
  545. local_set(&drvdata->mode, CS_MODE_DISABLED);
  546. }
  547. static const struct coresight_ops_source etm_source_ops = {
  548. .cpu_id = etm_cpu_id,
  549. .trace_id = etm_trace_id,
  550. .enable = etm_enable,
  551. .disable = etm_disable,
  552. };
  553. static const struct coresight_ops etm_cs_ops = {
  554. .source_ops = &etm_source_ops,
  555. };
  556. static int etm_online_cpu(unsigned int cpu)
  557. {
  558. if (!etmdrvdata[cpu])
  559. return 0;
  560. if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
  561. coresight_enable(etmdrvdata[cpu]->csdev);
  562. return 0;
  563. }
  564. static int etm_starting_cpu(unsigned int cpu)
  565. {
  566. if (!etmdrvdata[cpu])
  567. return 0;
  568. spin_lock(&etmdrvdata[cpu]->spinlock);
  569. if (!etmdrvdata[cpu]->os_unlock) {
  570. etm_os_unlock(etmdrvdata[cpu]);
  571. etmdrvdata[cpu]->os_unlock = true;
  572. }
  573. if (local_read(&etmdrvdata[cpu]->mode))
  574. etm_enable_hw(etmdrvdata[cpu]);
  575. spin_unlock(&etmdrvdata[cpu]->spinlock);
  576. return 0;
  577. }
  578. static int etm_dying_cpu(unsigned int cpu)
  579. {
  580. if (!etmdrvdata[cpu])
  581. return 0;
  582. spin_lock(&etmdrvdata[cpu]->spinlock);
  583. if (local_read(&etmdrvdata[cpu]->mode))
  584. etm_disable_hw(etmdrvdata[cpu]);
  585. spin_unlock(&etmdrvdata[cpu]->spinlock);
  586. return 0;
  587. }
  588. static bool etm_arch_supported(u8 arch)
  589. {
  590. switch (arch) {
  591. case ETM_ARCH_V3_3:
  592. break;
  593. case ETM_ARCH_V3_5:
  594. break;
  595. case PFT_ARCH_V1_0:
  596. break;
  597. case PFT_ARCH_V1_1:
  598. break;
  599. default:
  600. return false;
  601. }
  602. return true;
  603. }
  604. static void etm_init_arch_data(void *info)
  605. {
  606. u32 etmidr;
  607. u32 etmccr;
  608. struct etm_drvdata *drvdata = info;
  609. /* Make sure all registers are accessible */
  610. etm_os_unlock(drvdata);
  611. CS_UNLOCK(drvdata->base);
  612. /* First dummy read */
  613. (void)etm_readl(drvdata, ETMPDSR);
  614. /* Provide power to ETM: ETMPDCR[3] == 1 */
  615. etm_set_pwrup(drvdata);
  616. /*
  617. * Clear power down bit since when this bit is set writes to
  618. * certain registers might be ignored.
  619. */
  620. etm_clr_pwrdwn(drvdata);
  621. /*
  622. * Set prog bit. It will be set from reset but this is included to
  623. * ensure it is set
  624. */
  625. etm_set_prog(drvdata);
  626. /* Find all capabilities */
  627. etmidr = etm_readl(drvdata, ETMIDR);
  628. drvdata->arch = BMVAL(etmidr, 4, 11);
  629. drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
  630. drvdata->etmccer = etm_readl(drvdata, ETMCCER);
  631. etmccr = etm_readl(drvdata, ETMCCR);
  632. drvdata->etmccr = etmccr;
  633. drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
  634. drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
  635. drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
  636. drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
  637. drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
  638. etm_set_pwrdwn(drvdata);
  639. etm_clr_pwrup(drvdata);
  640. CS_LOCK(drvdata->base);
  641. }
  642. static void etm_init_trace_id(struct etm_drvdata *drvdata)
  643. {
  644. drvdata->traceid = coresight_get_trace_id(drvdata->cpu);
  645. }
  646. static int etm_probe(struct amba_device *adev, const struct amba_id *id)
  647. {
  648. int ret;
  649. void __iomem *base;
  650. struct device *dev = &adev->dev;
  651. struct coresight_platform_data *pdata = NULL;
  652. struct etm_drvdata *drvdata;
  653. struct resource *res = &adev->res;
  654. struct coresight_desc desc = { 0 };
  655. struct device_node *np = adev->dev.of_node;
  656. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  657. if (!drvdata)
  658. return -ENOMEM;
  659. if (np) {
  660. pdata = of_get_coresight_platform_data(dev, np);
  661. if (IS_ERR(pdata))
  662. return PTR_ERR(pdata);
  663. adev->dev.platform_data = pdata;
  664. drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
  665. }
  666. drvdata->dev = &adev->dev;
  667. dev_set_drvdata(dev, drvdata);
  668. /* Validity for the resource is already checked by the AMBA core */
  669. base = devm_ioremap_resource(dev, res);
  670. if (IS_ERR(base))
  671. return PTR_ERR(base);
  672. drvdata->base = base;
  673. spin_lock_init(&drvdata->spinlock);
  674. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  675. if (!IS_ERR(drvdata->atclk)) {
  676. ret = clk_prepare_enable(drvdata->atclk);
  677. if (ret)
  678. return ret;
  679. }
  680. drvdata->cpu = pdata ? pdata->cpu : 0;
  681. cpus_read_lock();
  682. etmdrvdata[drvdata->cpu] = drvdata;
  683. if (smp_call_function_single(drvdata->cpu,
  684. etm_init_arch_data, drvdata, 1))
  685. dev_err(dev, "ETM arch init failed\n");
  686. if (!etm_count++) {
  687. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ARM_CORESIGHT_STARTING,
  688. "arm/coresight:starting",
  689. etm_starting_cpu, etm_dying_cpu);
  690. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
  691. "arm/coresight:online",
  692. etm_online_cpu, NULL);
  693. if (ret < 0)
  694. goto err_arch_supported;
  695. hp_online = ret;
  696. }
  697. cpus_read_unlock();
  698. if (etm_arch_supported(drvdata->arch) == false) {
  699. ret = -EINVAL;
  700. goto err_arch_supported;
  701. }
  702. etm_init_trace_id(drvdata);
  703. etm_set_default(&drvdata->config);
  704. desc.type = CORESIGHT_DEV_TYPE_SOURCE;
  705. desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
  706. desc.ops = &etm_cs_ops;
  707. desc.pdata = pdata;
  708. desc.dev = dev;
  709. desc.groups = coresight_etm_groups;
  710. drvdata->csdev = coresight_register(&desc);
  711. if (IS_ERR(drvdata->csdev)) {
  712. ret = PTR_ERR(drvdata->csdev);
  713. goto err_arch_supported;
  714. }
  715. ret = etm_perf_symlink(drvdata->csdev, true);
  716. if (ret) {
  717. coresight_unregister(drvdata->csdev);
  718. goto err_arch_supported;
  719. }
  720. pm_runtime_put(&adev->dev);
  721. dev_info(dev, "%s initialized\n", (char *)id->data);
  722. if (boot_enable) {
  723. coresight_enable(drvdata->csdev);
  724. drvdata->boot_enable = true;
  725. }
  726. return 0;
  727. err_arch_supported:
  728. if (--etm_count == 0) {
  729. cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
  730. if (hp_online)
  731. cpuhp_remove_state_nocalls(hp_online);
  732. }
  733. return ret;
  734. }
  735. #ifdef CONFIG_PM
  736. static int etm_runtime_suspend(struct device *dev)
  737. {
  738. struct etm_drvdata *drvdata = dev_get_drvdata(dev);
  739. if (drvdata && !IS_ERR(drvdata->atclk))
  740. clk_disable_unprepare(drvdata->atclk);
  741. return 0;
  742. }
  743. static int etm_runtime_resume(struct device *dev)
  744. {
  745. struct etm_drvdata *drvdata = dev_get_drvdata(dev);
  746. if (drvdata && !IS_ERR(drvdata->atclk))
  747. clk_prepare_enable(drvdata->atclk);
  748. return 0;
  749. }
  750. #endif
  751. static const struct dev_pm_ops etm_dev_pm_ops = {
  752. SET_RUNTIME_PM_OPS(etm_runtime_suspend, etm_runtime_resume, NULL)
  753. };
  754. static const struct amba_id etm_ids[] = {
  755. { /* ETM 3.3 */
  756. .id = 0x000bb921,
  757. .mask = 0x000fffff,
  758. .data = "ETM 3.3",
  759. },
  760. { /* ETM 3.5 - Cortex-A5 */
  761. .id = 0x000bb955,
  762. .mask = 0x000fffff,
  763. .data = "ETM 3.5",
  764. },
  765. { /* ETM 3.5 */
  766. .id = 0x000bb956,
  767. .mask = 0x000fffff,
  768. .data = "ETM 3.5",
  769. },
  770. { /* PTM 1.0 */
  771. .id = 0x000bb950,
  772. .mask = 0x000fffff,
  773. .data = "PTM 1.0",
  774. },
  775. { /* PTM 1.1 */
  776. .id = 0x000bb95f,
  777. .mask = 0x000fffff,
  778. .data = "PTM 1.1",
  779. },
  780. { /* PTM 1.1 Qualcomm */
  781. .id = 0x000b006f,
  782. .mask = 0x000fffff,
  783. .data = "PTM 1.1",
  784. },
  785. { 0, 0},
  786. };
  787. static struct amba_driver etm_driver = {
  788. .drv = {
  789. .name = "coresight-etm3x",
  790. .owner = THIS_MODULE,
  791. .pm = &etm_dev_pm_ops,
  792. .suppress_bind_attrs = true,
  793. },
  794. .probe = etm_probe,
  795. .id_table = etm_ids,
  796. };
  797. builtin_amba_driver(etm_driver);