coresight-etm3x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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_vpid[i] = 0x0;
  204. }
  205. config->ctxid_mask = 0x0;
  206. /* Setting default to 1024 as per TRM recommendation */
  207. config->sync_freq = 0x400;
  208. }
  209. void etm_config_trace_mode(struct etm_config *config)
  210. {
  211. u32 flags, mode;
  212. mode = config->mode;
  213. mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER);
  214. /* excluding kernel AND user space doesn't make sense */
  215. if (mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER))
  216. return;
  217. /* nothing to do if neither flags are set */
  218. if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER))
  219. return;
  220. flags = (1 << 0 | /* instruction execute */
  221. 3 << 3 | /* ARM instruction */
  222. 0 << 5 | /* No data value comparison */
  223. 0 << 7 | /* No exact mach */
  224. 0 << 8); /* Ignore context ID */
  225. /* No need to worry about single address comparators. */
  226. config->enable_ctrl2 = 0x0;
  227. /* Bit 0 is address range comparator 1 */
  228. config->enable_ctrl1 = ETMTECR1_ADDR_COMP_1;
  229. /*
  230. * On ETMv3.5:
  231. * ETMACTRn[13,11] == Non-secure state comparison control
  232. * ETMACTRn[12,10] == Secure state comparison control
  233. *
  234. * b00 == Match in all modes in this state
  235. * b01 == Do not match in any more in this state
  236. * b10 == Match in all modes excepts user mode in this state
  237. * b11 == Match only in user mode in this state
  238. */
  239. /* Tracing in secure mode is not supported at this time */
  240. flags |= (0 << 12 | 1 << 10);
  241. if (mode & ETM_MODE_EXCL_USER) {
  242. /* exclude user, match all modes except user mode */
  243. flags |= (1 << 13 | 0 << 11);
  244. } else {
  245. /* exclude kernel, match only in user mode */
  246. flags |= (1 << 13 | 1 << 11);
  247. }
  248. /*
  249. * The ETMEEVR register is already set to "hard wire A". As such
  250. * all there is to do is setup an address comparator that spans
  251. * the entire address range and configure the state and mode bits.
  252. */
  253. config->addr_val[0] = (u32) 0x0;
  254. config->addr_val[1] = (u32) ~0x0;
  255. config->addr_acctype[0] = flags;
  256. config->addr_acctype[1] = flags;
  257. config->addr_type[0] = ETM_ADDR_TYPE_RANGE;
  258. config->addr_type[1] = ETM_ADDR_TYPE_RANGE;
  259. }
  260. #define ETM3X_SUPPORTED_OPTIONS (ETMCR_CYC_ACC | \
  261. ETMCR_TIMESTAMP_EN | \
  262. ETMCR_RETURN_STACK)
  263. static int etm_parse_event_config(struct etm_drvdata *drvdata,
  264. struct perf_event *event)
  265. {
  266. struct etm_config *config = &drvdata->config;
  267. struct perf_event_attr *attr = &event->attr;
  268. if (!attr)
  269. return -EINVAL;
  270. /* Clear configuration from previous run */
  271. memset(config, 0, sizeof(struct etm_config));
  272. if (attr->exclude_kernel)
  273. config->mode = ETM_MODE_EXCL_KERN;
  274. if (attr->exclude_user)
  275. config->mode = ETM_MODE_EXCL_USER;
  276. /* Always start from the default config */
  277. etm_set_default(config);
  278. /*
  279. * By default the tracers are configured to trace the whole address
  280. * range. Narrow the field only if requested by user space.
  281. */
  282. if (config->mode)
  283. etm_config_trace_mode(config);
  284. /*
  285. * At this time only cycle accurate, return stack and timestamp
  286. * options are available.
  287. */
  288. if (attr->config & ~ETM3X_SUPPORTED_OPTIONS)
  289. return -EINVAL;
  290. config->ctrl = attr->config;
  291. /*
  292. * Possible to have cores with PTM (supports ret stack) and ETM
  293. * (never has ret stack) on the same SoC. So if we have a request
  294. * for return stack that can't be honoured on this core then
  295. * clear the bit - trace will still continue normally
  296. */
  297. if ((config->ctrl & ETMCR_RETURN_STACK) &&
  298. !(drvdata->etmccer & ETMCCER_RETSTACK))
  299. config->ctrl &= ~ETMCR_RETURN_STACK;
  300. return 0;
  301. }
  302. static void etm_enable_hw(void *info)
  303. {
  304. int i;
  305. u32 etmcr;
  306. struct etm_drvdata *drvdata = info;
  307. struct etm_config *config = &drvdata->config;
  308. CS_UNLOCK(drvdata->base);
  309. /* Turn engine on */
  310. etm_clr_pwrdwn(drvdata);
  311. /* Apply power to trace registers */
  312. etm_set_pwrup(drvdata);
  313. /* Make sure all registers are accessible */
  314. etm_os_unlock(drvdata);
  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. CS_LOCK(drvdata->base);
  361. dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
  362. }
  363. static int etm_cpu_id(struct coresight_device *csdev)
  364. {
  365. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  366. return drvdata->cpu;
  367. }
  368. int etm_get_trace_id(struct etm_drvdata *drvdata)
  369. {
  370. unsigned long flags;
  371. int trace_id = -1;
  372. if (!drvdata)
  373. goto out;
  374. if (!local_read(&drvdata->mode))
  375. return drvdata->traceid;
  376. pm_runtime_get_sync(drvdata->dev);
  377. spin_lock_irqsave(&drvdata->spinlock, flags);
  378. CS_UNLOCK(drvdata->base);
  379. trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
  380. CS_LOCK(drvdata->base);
  381. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  382. pm_runtime_put(drvdata->dev);
  383. out:
  384. return trace_id;
  385. }
  386. static int etm_trace_id(struct coresight_device *csdev)
  387. {
  388. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  389. return etm_get_trace_id(drvdata);
  390. }
  391. static int etm_enable_perf(struct coresight_device *csdev,
  392. struct perf_event *event)
  393. {
  394. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  395. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  396. return -EINVAL;
  397. /* Configure the tracer based on the session's specifics */
  398. etm_parse_event_config(drvdata, event);
  399. /* And enable it */
  400. etm_enable_hw(drvdata);
  401. return 0;
  402. }
  403. static int etm_enable_sysfs(struct coresight_device *csdev)
  404. {
  405. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  406. int ret;
  407. spin_lock(&drvdata->spinlock);
  408. /*
  409. * Configure the ETM only if the CPU is online. If it isn't online
  410. * hw configuration will take place on the local CPU during bring up.
  411. */
  412. if (cpu_online(drvdata->cpu)) {
  413. ret = smp_call_function_single(drvdata->cpu,
  414. etm_enable_hw, drvdata, 1);
  415. if (ret)
  416. goto err;
  417. }
  418. drvdata->sticky_enable = true;
  419. spin_unlock(&drvdata->spinlock);
  420. dev_info(drvdata->dev, "ETM tracing enabled\n");
  421. return 0;
  422. err:
  423. spin_unlock(&drvdata->spinlock);
  424. return ret;
  425. }
  426. static int etm_enable(struct coresight_device *csdev,
  427. struct perf_event *event, u32 mode)
  428. {
  429. int ret;
  430. u32 val;
  431. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  432. val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
  433. /* Someone is already using the tracer */
  434. if (val)
  435. return -EBUSY;
  436. switch (mode) {
  437. case CS_MODE_SYSFS:
  438. ret = etm_enable_sysfs(csdev);
  439. break;
  440. case CS_MODE_PERF:
  441. ret = etm_enable_perf(csdev, event);
  442. break;
  443. default:
  444. ret = -EINVAL;
  445. }
  446. /* The tracer didn't start */
  447. if (ret)
  448. local_set(&drvdata->mode, CS_MODE_DISABLED);
  449. return ret;
  450. }
  451. static void etm_disable_hw(void *info)
  452. {
  453. int i;
  454. struct etm_drvdata *drvdata = info;
  455. struct etm_config *config = &drvdata->config;
  456. CS_UNLOCK(drvdata->base);
  457. etm_set_prog(drvdata);
  458. /* Read back sequencer and counters for post trace analysis */
  459. config->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
  460. for (i = 0; i < drvdata->nr_cntr; i++)
  461. config->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
  462. etm_set_pwrdwn(drvdata);
  463. CS_LOCK(drvdata->base);
  464. dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
  465. }
  466. static void etm_disable_perf(struct coresight_device *csdev)
  467. {
  468. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  469. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  470. return;
  471. CS_UNLOCK(drvdata->base);
  472. /* Setting the prog bit disables tracing immediately */
  473. etm_set_prog(drvdata);
  474. /*
  475. * There is no way to know when the tracer will be used again so
  476. * power down the tracer.
  477. */
  478. etm_set_pwrdwn(drvdata);
  479. CS_LOCK(drvdata->base);
  480. }
  481. static void etm_disable_sysfs(struct coresight_device *csdev)
  482. {
  483. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  484. /*
  485. * Taking hotplug lock here protects from clocks getting disabled
  486. * with tracing being left on (crash scenario) if user disable occurs
  487. * after cpu online mask indicates the cpu is offline but before the
  488. * DYING hotplug callback is serviced by the ETM driver.
  489. */
  490. cpus_read_lock();
  491. spin_lock(&drvdata->spinlock);
  492. /*
  493. * Executing etm_disable_hw on the cpu whose ETM is being disabled
  494. * ensures that register writes occur when cpu is powered.
  495. */
  496. smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
  497. spin_unlock(&drvdata->spinlock);
  498. cpus_read_unlock();
  499. dev_info(drvdata->dev, "ETM tracing disabled\n");
  500. }
  501. static void etm_disable(struct coresight_device *csdev,
  502. struct perf_event *event)
  503. {
  504. u32 mode;
  505. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  506. /*
  507. * For as long as the tracer isn't disabled another entity can't
  508. * change its status. As such we can read the status here without
  509. * fearing it will change under us.
  510. */
  511. mode = local_read(&drvdata->mode);
  512. switch (mode) {
  513. case CS_MODE_DISABLED:
  514. break;
  515. case CS_MODE_SYSFS:
  516. etm_disable_sysfs(csdev);
  517. break;
  518. case CS_MODE_PERF:
  519. etm_disable_perf(csdev);
  520. break;
  521. default:
  522. WARN_ON_ONCE(mode);
  523. return;
  524. }
  525. if (mode)
  526. local_set(&drvdata->mode, CS_MODE_DISABLED);
  527. }
  528. static const struct coresight_ops_source etm_source_ops = {
  529. .cpu_id = etm_cpu_id,
  530. .trace_id = etm_trace_id,
  531. .enable = etm_enable,
  532. .disable = etm_disable,
  533. };
  534. static const struct coresight_ops etm_cs_ops = {
  535. .source_ops = &etm_source_ops,
  536. };
  537. static int etm_online_cpu(unsigned int cpu)
  538. {
  539. if (!etmdrvdata[cpu])
  540. return 0;
  541. if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
  542. coresight_enable(etmdrvdata[cpu]->csdev);
  543. return 0;
  544. }
  545. static int etm_starting_cpu(unsigned int cpu)
  546. {
  547. if (!etmdrvdata[cpu])
  548. return 0;
  549. spin_lock(&etmdrvdata[cpu]->spinlock);
  550. if (!etmdrvdata[cpu]->os_unlock) {
  551. etm_os_unlock(etmdrvdata[cpu]);
  552. etmdrvdata[cpu]->os_unlock = true;
  553. }
  554. if (local_read(&etmdrvdata[cpu]->mode))
  555. etm_enable_hw(etmdrvdata[cpu]);
  556. spin_unlock(&etmdrvdata[cpu]->spinlock);
  557. return 0;
  558. }
  559. static int etm_dying_cpu(unsigned int cpu)
  560. {
  561. if (!etmdrvdata[cpu])
  562. return 0;
  563. spin_lock(&etmdrvdata[cpu]->spinlock);
  564. if (local_read(&etmdrvdata[cpu]->mode))
  565. etm_disable_hw(etmdrvdata[cpu]);
  566. spin_unlock(&etmdrvdata[cpu]->spinlock);
  567. return 0;
  568. }
  569. static bool etm_arch_supported(u8 arch)
  570. {
  571. switch (arch) {
  572. case ETM_ARCH_V3_3:
  573. break;
  574. case ETM_ARCH_V3_5:
  575. break;
  576. case PFT_ARCH_V1_0:
  577. break;
  578. case PFT_ARCH_V1_1:
  579. break;
  580. default:
  581. return false;
  582. }
  583. return true;
  584. }
  585. static void etm_init_arch_data(void *info)
  586. {
  587. u32 etmidr;
  588. u32 etmccr;
  589. struct etm_drvdata *drvdata = info;
  590. /* Make sure all registers are accessible */
  591. etm_os_unlock(drvdata);
  592. CS_UNLOCK(drvdata->base);
  593. /* First dummy read */
  594. (void)etm_readl(drvdata, ETMPDSR);
  595. /* Provide power to ETM: ETMPDCR[3] == 1 */
  596. etm_set_pwrup(drvdata);
  597. /*
  598. * Clear power down bit since when this bit is set writes to
  599. * certain registers might be ignored.
  600. */
  601. etm_clr_pwrdwn(drvdata);
  602. /*
  603. * Set prog bit. It will be set from reset but this is included to
  604. * ensure it is set
  605. */
  606. etm_set_prog(drvdata);
  607. /* Find all capabilities */
  608. etmidr = etm_readl(drvdata, ETMIDR);
  609. drvdata->arch = BMVAL(etmidr, 4, 11);
  610. drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
  611. drvdata->etmccer = etm_readl(drvdata, ETMCCER);
  612. etmccr = etm_readl(drvdata, ETMCCR);
  613. drvdata->etmccr = etmccr;
  614. drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
  615. drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
  616. drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
  617. drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
  618. drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
  619. etm_set_pwrdwn(drvdata);
  620. etm_clr_pwrup(drvdata);
  621. CS_LOCK(drvdata->base);
  622. }
  623. static void etm_init_trace_id(struct etm_drvdata *drvdata)
  624. {
  625. drvdata->traceid = coresight_get_trace_id(drvdata->cpu);
  626. }
  627. static int etm_probe(struct amba_device *adev, const struct amba_id *id)
  628. {
  629. int ret;
  630. void __iomem *base;
  631. struct device *dev = &adev->dev;
  632. struct coresight_platform_data *pdata = NULL;
  633. struct etm_drvdata *drvdata;
  634. struct resource *res = &adev->res;
  635. struct coresight_desc desc = { 0 };
  636. struct device_node *np = adev->dev.of_node;
  637. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  638. if (!drvdata)
  639. return -ENOMEM;
  640. if (np) {
  641. pdata = of_get_coresight_platform_data(dev, np);
  642. if (IS_ERR(pdata))
  643. return PTR_ERR(pdata);
  644. adev->dev.platform_data = pdata;
  645. drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
  646. }
  647. drvdata->dev = &adev->dev;
  648. dev_set_drvdata(dev, drvdata);
  649. /* Validity for the resource is already checked by the AMBA core */
  650. base = devm_ioremap_resource(dev, res);
  651. if (IS_ERR(base))
  652. return PTR_ERR(base);
  653. drvdata->base = base;
  654. spin_lock_init(&drvdata->spinlock);
  655. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  656. if (!IS_ERR(drvdata->atclk)) {
  657. ret = clk_prepare_enable(drvdata->atclk);
  658. if (ret)
  659. return ret;
  660. }
  661. drvdata->cpu = pdata ? pdata->cpu : 0;
  662. cpus_read_lock();
  663. etmdrvdata[drvdata->cpu] = drvdata;
  664. if (smp_call_function_single(drvdata->cpu,
  665. etm_init_arch_data, drvdata, 1))
  666. dev_err(dev, "ETM arch init failed\n");
  667. if (!etm_count++) {
  668. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ARM_CORESIGHT_STARTING,
  669. "arm/coresight:starting",
  670. etm_starting_cpu, etm_dying_cpu);
  671. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
  672. "arm/coresight:online",
  673. etm_online_cpu, NULL);
  674. if (ret < 0)
  675. goto err_arch_supported;
  676. hp_online = ret;
  677. }
  678. cpus_read_unlock();
  679. if (etm_arch_supported(drvdata->arch) == false) {
  680. ret = -EINVAL;
  681. goto err_arch_supported;
  682. }
  683. etm_init_trace_id(drvdata);
  684. etm_set_default(&drvdata->config);
  685. desc.type = CORESIGHT_DEV_TYPE_SOURCE;
  686. desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
  687. desc.ops = &etm_cs_ops;
  688. desc.pdata = pdata;
  689. desc.dev = dev;
  690. desc.groups = coresight_etm_groups;
  691. drvdata->csdev = coresight_register(&desc);
  692. if (IS_ERR(drvdata->csdev)) {
  693. ret = PTR_ERR(drvdata->csdev);
  694. goto err_arch_supported;
  695. }
  696. ret = etm_perf_symlink(drvdata->csdev, true);
  697. if (ret) {
  698. coresight_unregister(drvdata->csdev);
  699. goto err_arch_supported;
  700. }
  701. pm_runtime_put(&adev->dev);
  702. dev_info(dev, "%s initialized\n", (char *)id->data);
  703. if (boot_enable) {
  704. coresight_enable(drvdata->csdev);
  705. drvdata->boot_enable = true;
  706. }
  707. return 0;
  708. err_arch_supported:
  709. if (--etm_count == 0) {
  710. cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
  711. if (hp_online)
  712. cpuhp_remove_state_nocalls(hp_online);
  713. }
  714. return ret;
  715. }
  716. #ifdef CONFIG_PM
  717. static int etm_runtime_suspend(struct device *dev)
  718. {
  719. struct etm_drvdata *drvdata = dev_get_drvdata(dev);
  720. if (drvdata && !IS_ERR(drvdata->atclk))
  721. clk_disable_unprepare(drvdata->atclk);
  722. return 0;
  723. }
  724. static int etm_runtime_resume(struct device *dev)
  725. {
  726. struct etm_drvdata *drvdata = dev_get_drvdata(dev);
  727. if (drvdata && !IS_ERR(drvdata->atclk))
  728. clk_prepare_enable(drvdata->atclk);
  729. return 0;
  730. }
  731. #endif
  732. static const struct dev_pm_ops etm_dev_pm_ops = {
  733. SET_RUNTIME_PM_OPS(etm_runtime_suspend, etm_runtime_resume, NULL)
  734. };
  735. static const struct amba_id etm_ids[] = {
  736. { /* ETM 3.3 */
  737. .id = 0x000bb921,
  738. .mask = 0x000fffff,
  739. .data = "ETM 3.3",
  740. },
  741. { /* ETM 3.5 - Cortex-A5 */
  742. .id = 0x000bb955,
  743. .mask = 0x000fffff,
  744. .data = "ETM 3.5",
  745. },
  746. { /* ETM 3.5 */
  747. .id = 0x000bb956,
  748. .mask = 0x000fffff,
  749. .data = "ETM 3.5",
  750. },
  751. { /* PTM 1.0 */
  752. .id = 0x000bb950,
  753. .mask = 0x000fffff,
  754. .data = "PTM 1.0",
  755. },
  756. { /* PTM 1.1 */
  757. .id = 0x000bb95f,
  758. .mask = 0x000fffff,
  759. .data = "PTM 1.1",
  760. },
  761. { /* PTM 1.1 Qualcomm */
  762. .id = 0x000b006f,
  763. .mask = 0x000fffff,
  764. .data = "PTM 1.1",
  765. },
  766. { 0, 0},
  767. };
  768. static struct amba_driver etm_driver = {
  769. .drv = {
  770. .name = "coresight-etm3x",
  771. .owner = THIS_MODULE,
  772. .pm = &etm_dev_pm_ops,
  773. .suppress_bind_attrs = true,
  774. },
  775. .probe = etm_probe,
  776. .id_table = etm_ids,
  777. };
  778. builtin_amba_driver(etm_driver);