coresight-etm3x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 void etm_enable_hw(void *info)
  301. {
  302. int i;
  303. u32 etmcr;
  304. struct etm_drvdata *drvdata = info;
  305. struct etm_config *config = &drvdata->config;
  306. CS_UNLOCK(drvdata->base);
  307. /* Turn engine on */
  308. etm_clr_pwrdwn(drvdata);
  309. /* Apply power to trace registers */
  310. etm_set_pwrup(drvdata);
  311. /* Make sure all registers are accessible */
  312. etm_os_unlock(drvdata);
  313. etm_set_prog(drvdata);
  314. etmcr = etm_readl(drvdata, ETMCR);
  315. /* Clear setting from a previous run if need be */
  316. etmcr &= ~ETM3X_SUPPORTED_OPTIONS;
  317. etmcr |= drvdata->port_size;
  318. etmcr |= ETMCR_ETM_EN;
  319. etm_writel(drvdata, config->ctrl | etmcr, ETMCR);
  320. etm_writel(drvdata, config->trigger_event, ETMTRIGGER);
  321. etm_writel(drvdata, config->startstop_ctrl, ETMTSSCR);
  322. etm_writel(drvdata, config->enable_event, ETMTEEVR);
  323. etm_writel(drvdata, config->enable_ctrl1, ETMTECR1);
  324. etm_writel(drvdata, config->fifofull_level, ETMFFLR);
  325. for (i = 0; i < drvdata->nr_addr_cmp; i++) {
  326. etm_writel(drvdata, config->addr_val[i], ETMACVRn(i));
  327. etm_writel(drvdata, config->addr_acctype[i], ETMACTRn(i));
  328. }
  329. for (i = 0; i < drvdata->nr_cntr; i++) {
  330. etm_writel(drvdata, config->cntr_rld_val[i], ETMCNTRLDVRn(i));
  331. etm_writel(drvdata, config->cntr_event[i], ETMCNTENRn(i));
  332. etm_writel(drvdata, config->cntr_rld_event[i],
  333. ETMCNTRLDEVRn(i));
  334. etm_writel(drvdata, config->cntr_val[i], ETMCNTVRn(i));
  335. }
  336. etm_writel(drvdata, config->seq_12_event, ETMSQ12EVR);
  337. etm_writel(drvdata, config->seq_21_event, ETMSQ21EVR);
  338. etm_writel(drvdata, config->seq_23_event, ETMSQ23EVR);
  339. etm_writel(drvdata, config->seq_31_event, ETMSQ31EVR);
  340. etm_writel(drvdata, config->seq_32_event, ETMSQ32EVR);
  341. etm_writel(drvdata, config->seq_13_event, ETMSQ13EVR);
  342. etm_writel(drvdata, config->seq_curr_state, ETMSQR);
  343. for (i = 0; i < drvdata->nr_ext_out; i++)
  344. etm_writel(drvdata, ETM_DEFAULT_EVENT_VAL, ETMEXTOUTEVRn(i));
  345. for (i = 0; i < drvdata->nr_ctxid_cmp; i++)
  346. etm_writel(drvdata, config->ctxid_pid[i], ETMCIDCVRn(i));
  347. etm_writel(drvdata, config->ctxid_mask, ETMCIDCMR);
  348. etm_writel(drvdata, config->sync_freq, ETMSYNCFR);
  349. /* No external input selected */
  350. etm_writel(drvdata, 0x0, ETMEXTINSELR);
  351. etm_writel(drvdata, config->timestamp_event, ETMTSEVR);
  352. /* No auxiliary control selected */
  353. etm_writel(drvdata, 0x0, ETMAUXCR);
  354. etm_writel(drvdata, drvdata->traceid, ETMTRACEIDR);
  355. /* No VMID comparator value selected */
  356. etm_writel(drvdata, 0x0, ETMVMIDCVR);
  357. etm_clr_prog(drvdata);
  358. CS_LOCK(drvdata->base);
  359. dev_dbg(drvdata->dev, "cpu: %d enable smp call done\n", drvdata->cpu);
  360. }
  361. static int etm_cpu_id(struct coresight_device *csdev)
  362. {
  363. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  364. return drvdata->cpu;
  365. }
  366. int etm_get_trace_id(struct etm_drvdata *drvdata)
  367. {
  368. unsigned long flags;
  369. int trace_id = -1;
  370. if (!drvdata)
  371. goto out;
  372. if (!local_read(&drvdata->mode))
  373. return drvdata->traceid;
  374. pm_runtime_get_sync(drvdata->dev);
  375. spin_lock_irqsave(&drvdata->spinlock, flags);
  376. CS_UNLOCK(drvdata->base);
  377. trace_id = (etm_readl(drvdata, ETMTRACEIDR) & ETM_TRACEID_MASK);
  378. CS_LOCK(drvdata->base);
  379. spin_unlock_irqrestore(&drvdata->spinlock, flags);
  380. pm_runtime_put(drvdata->dev);
  381. out:
  382. return trace_id;
  383. }
  384. static int etm_trace_id(struct coresight_device *csdev)
  385. {
  386. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  387. return etm_get_trace_id(drvdata);
  388. }
  389. static int etm_enable_perf(struct coresight_device *csdev,
  390. struct perf_event *event)
  391. {
  392. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  393. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  394. return -EINVAL;
  395. /* Configure the tracer based on the session's specifics */
  396. etm_parse_event_config(drvdata, event);
  397. /* And enable it */
  398. etm_enable_hw(drvdata);
  399. return 0;
  400. }
  401. static int etm_enable_sysfs(struct coresight_device *csdev)
  402. {
  403. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  404. int ret;
  405. spin_lock(&drvdata->spinlock);
  406. /*
  407. * Configure the ETM only if the CPU is online. If it isn't online
  408. * hw configuration will take place on the local CPU during bring up.
  409. */
  410. if (cpu_online(drvdata->cpu)) {
  411. ret = smp_call_function_single(drvdata->cpu,
  412. etm_enable_hw, drvdata, 1);
  413. if (ret)
  414. goto err;
  415. }
  416. drvdata->sticky_enable = true;
  417. spin_unlock(&drvdata->spinlock);
  418. dev_info(drvdata->dev, "ETM tracing enabled\n");
  419. return 0;
  420. err:
  421. spin_unlock(&drvdata->spinlock);
  422. return ret;
  423. }
  424. static int etm_enable(struct coresight_device *csdev,
  425. struct perf_event *event, u32 mode)
  426. {
  427. int ret;
  428. u32 val;
  429. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  430. val = local_cmpxchg(&drvdata->mode, CS_MODE_DISABLED, mode);
  431. /* Someone is already using the tracer */
  432. if (val)
  433. return -EBUSY;
  434. switch (mode) {
  435. case CS_MODE_SYSFS:
  436. ret = etm_enable_sysfs(csdev);
  437. break;
  438. case CS_MODE_PERF:
  439. ret = etm_enable_perf(csdev, event);
  440. break;
  441. default:
  442. ret = -EINVAL;
  443. }
  444. /* The tracer didn't start */
  445. if (ret)
  446. local_set(&drvdata->mode, CS_MODE_DISABLED);
  447. return ret;
  448. }
  449. static void etm_disable_hw(void *info)
  450. {
  451. int i;
  452. struct etm_drvdata *drvdata = info;
  453. struct etm_config *config = &drvdata->config;
  454. CS_UNLOCK(drvdata->base);
  455. etm_set_prog(drvdata);
  456. /* Read back sequencer and counters for post trace analysis */
  457. config->seq_curr_state = (etm_readl(drvdata, ETMSQR) & ETM_SQR_MASK);
  458. for (i = 0; i < drvdata->nr_cntr; i++)
  459. config->cntr_val[i] = etm_readl(drvdata, ETMCNTVRn(i));
  460. etm_set_pwrdwn(drvdata);
  461. CS_LOCK(drvdata->base);
  462. dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
  463. }
  464. static void etm_disable_perf(struct coresight_device *csdev)
  465. {
  466. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  467. if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
  468. return;
  469. CS_UNLOCK(drvdata->base);
  470. /* Setting the prog bit disables tracing immediately */
  471. etm_set_prog(drvdata);
  472. /*
  473. * There is no way to know when the tracer will be used again so
  474. * power down the tracer.
  475. */
  476. etm_set_pwrdwn(drvdata);
  477. CS_LOCK(drvdata->base);
  478. }
  479. static void etm_disable_sysfs(struct coresight_device *csdev)
  480. {
  481. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  482. /*
  483. * Taking hotplug lock here protects from clocks getting disabled
  484. * with tracing being left on (crash scenario) if user disable occurs
  485. * after cpu online mask indicates the cpu is offline but before the
  486. * DYING hotplug callback is serviced by the ETM driver.
  487. */
  488. cpus_read_lock();
  489. spin_lock(&drvdata->spinlock);
  490. /*
  491. * Executing etm_disable_hw on the cpu whose ETM is being disabled
  492. * ensures that register writes occur when cpu is powered.
  493. */
  494. smp_call_function_single(drvdata->cpu, etm_disable_hw, drvdata, 1);
  495. spin_unlock(&drvdata->spinlock);
  496. cpus_read_unlock();
  497. dev_info(drvdata->dev, "ETM tracing disabled\n");
  498. }
  499. static void etm_disable(struct coresight_device *csdev,
  500. struct perf_event *event)
  501. {
  502. u32 mode;
  503. struct etm_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
  504. /*
  505. * For as long as the tracer isn't disabled another entity can't
  506. * change its status. As such we can read the status here without
  507. * fearing it will change under us.
  508. */
  509. mode = local_read(&drvdata->mode);
  510. switch (mode) {
  511. case CS_MODE_DISABLED:
  512. break;
  513. case CS_MODE_SYSFS:
  514. etm_disable_sysfs(csdev);
  515. break;
  516. case CS_MODE_PERF:
  517. etm_disable_perf(csdev);
  518. break;
  519. default:
  520. WARN_ON_ONCE(mode);
  521. return;
  522. }
  523. if (mode)
  524. local_set(&drvdata->mode, CS_MODE_DISABLED);
  525. }
  526. static const struct coresight_ops_source etm_source_ops = {
  527. .cpu_id = etm_cpu_id,
  528. .trace_id = etm_trace_id,
  529. .enable = etm_enable,
  530. .disable = etm_disable,
  531. };
  532. static const struct coresight_ops etm_cs_ops = {
  533. .source_ops = &etm_source_ops,
  534. };
  535. static int etm_online_cpu(unsigned int cpu)
  536. {
  537. if (!etmdrvdata[cpu])
  538. return 0;
  539. if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable)
  540. coresight_enable(etmdrvdata[cpu]->csdev);
  541. return 0;
  542. }
  543. static int etm_starting_cpu(unsigned int cpu)
  544. {
  545. if (!etmdrvdata[cpu])
  546. return 0;
  547. spin_lock(&etmdrvdata[cpu]->spinlock);
  548. if (!etmdrvdata[cpu]->os_unlock) {
  549. etm_os_unlock(etmdrvdata[cpu]);
  550. etmdrvdata[cpu]->os_unlock = true;
  551. }
  552. if (local_read(&etmdrvdata[cpu]->mode))
  553. etm_enable_hw(etmdrvdata[cpu]);
  554. spin_unlock(&etmdrvdata[cpu]->spinlock);
  555. return 0;
  556. }
  557. static int etm_dying_cpu(unsigned int cpu)
  558. {
  559. if (!etmdrvdata[cpu])
  560. return 0;
  561. spin_lock(&etmdrvdata[cpu]->spinlock);
  562. if (local_read(&etmdrvdata[cpu]->mode))
  563. etm_disable_hw(etmdrvdata[cpu]);
  564. spin_unlock(&etmdrvdata[cpu]->spinlock);
  565. return 0;
  566. }
  567. static bool etm_arch_supported(u8 arch)
  568. {
  569. switch (arch) {
  570. case ETM_ARCH_V3_3:
  571. break;
  572. case ETM_ARCH_V3_5:
  573. break;
  574. case PFT_ARCH_V1_0:
  575. break;
  576. case PFT_ARCH_V1_1:
  577. break;
  578. default:
  579. return false;
  580. }
  581. return true;
  582. }
  583. static void etm_init_arch_data(void *info)
  584. {
  585. u32 etmidr;
  586. u32 etmccr;
  587. struct etm_drvdata *drvdata = info;
  588. /* Make sure all registers are accessible */
  589. etm_os_unlock(drvdata);
  590. CS_UNLOCK(drvdata->base);
  591. /* First dummy read */
  592. (void)etm_readl(drvdata, ETMPDSR);
  593. /* Provide power to ETM: ETMPDCR[3] == 1 */
  594. etm_set_pwrup(drvdata);
  595. /*
  596. * Clear power down bit since when this bit is set writes to
  597. * certain registers might be ignored.
  598. */
  599. etm_clr_pwrdwn(drvdata);
  600. /*
  601. * Set prog bit. It will be set from reset but this is included to
  602. * ensure it is set
  603. */
  604. etm_set_prog(drvdata);
  605. /* Find all capabilities */
  606. etmidr = etm_readl(drvdata, ETMIDR);
  607. drvdata->arch = BMVAL(etmidr, 4, 11);
  608. drvdata->port_size = etm_readl(drvdata, ETMCR) & PORT_SIZE_MASK;
  609. drvdata->etmccer = etm_readl(drvdata, ETMCCER);
  610. etmccr = etm_readl(drvdata, ETMCCR);
  611. drvdata->etmccr = etmccr;
  612. drvdata->nr_addr_cmp = BMVAL(etmccr, 0, 3) * 2;
  613. drvdata->nr_cntr = BMVAL(etmccr, 13, 15);
  614. drvdata->nr_ext_inp = BMVAL(etmccr, 17, 19);
  615. drvdata->nr_ext_out = BMVAL(etmccr, 20, 22);
  616. drvdata->nr_ctxid_cmp = BMVAL(etmccr, 24, 25);
  617. etm_set_pwrdwn(drvdata);
  618. etm_clr_pwrup(drvdata);
  619. CS_LOCK(drvdata->base);
  620. }
  621. static void etm_init_trace_id(struct etm_drvdata *drvdata)
  622. {
  623. drvdata->traceid = coresight_get_trace_id(drvdata->cpu);
  624. }
  625. static int etm_probe(struct amba_device *adev, const struct amba_id *id)
  626. {
  627. int ret;
  628. void __iomem *base;
  629. struct device *dev = &adev->dev;
  630. struct coresight_platform_data *pdata = NULL;
  631. struct etm_drvdata *drvdata;
  632. struct resource *res = &adev->res;
  633. struct coresight_desc desc = { 0 };
  634. struct device_node *np = adev->dev.of_node;
  635. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  636. if (!drvdata)
  637. return -ENOMEM;
  638. if (np) {
  639. pdata = of_get_coresight_platform_data(dev, np);
  640. if (IS_ERR(pdata))
  641. return PTR_ERR(pdata);
  642. adev->dev.platform_data = pdata;
  643. drvdata->use_cp14 = of_property_read_bool(np, "arm,cp14");
  644. }
  645. drvdata->dev = &adev->dev;
  646. dev_set_drvdata(dev, drvdata);
  647. /* Validity for the resource is already checked by the AMBA core */
  648. base = devm_ioremap_resource(dev, res);
  649. if (IS_ERR(base))
  650. return PTR_ERR(base);
  651. drvdata->base = base;
  652. spin_lock_init(&drvdata->spinlock);
  653. drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
  654. if (!IS_ERR(drvdata->atclk)) {
  655. ret = clk_prepare_enable(drvdata->atclk);
  656. if (ret)
  657. return ret;
  658. }
  659. drvdata->cpu = pdata ? pdata->cpu : 0;
  660. cpus_read_lock();
  661. etmdrvdata[drvdata->cpu] = drvdata;
  662. if (smp_call_function_single(drvdata->cpu,
  663. etm_init_arch_data, drvdata, 1))
  664. dev_err(dev, "ETM arch init failed\n");
  665. if (!etm_count++) {
  666. cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ARM_CORESIGHT_STARTING,
  667. "arm/coresight:starting",
  668. etm_starting_cpu, etm_dying_cpu);
  669. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
  670. "arm/coresight:online",
  671. etm_online_cpu, NULL);
  672. if (ret < 0)
  673. goto err_arch_supported;
  674. hp_online = ret;
  675. }
  676. cpus_read_unlock();
  677. if (etm_arch_supported(drvdata->arch) == false) {
  678. ret = -EINVAL;
  679. goto err_arch_supported;
  680. }
  681. etm_init_trace_id(drvdata);
  682. etm_set_default(&drvdata->config);
  683. desc.type = CORESIGHT_DEV_TYPE_SOURCE;
  684. desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
  685. desc.ops = &etm_cs_ops;
  686. desc.pdata = pdata;
  687. desc.dev = dev;
  688. desc.groups = coresight_etm_groups;
  689. drvdata->csdev = coresight_register(&desc);
  690. if (IS_ERR(drvdata->csdev)) {
  691. ret = PTR_ERR(drvdata->csdev);
  692. goto err_arch_supported;
  693. }
  694. ret = etm_perf_symlink(drvdata->csdev, true);
  695. if (ret) {
  696. coresight_unregister(drvdata->csdev);
  697. goto err_arch_supported;
  698. }
  699. pm_runtime_put(&adev->dev);
  700. dev_info(dev, "%s initialized\n", (char *)id->data);
  701. if (boot_enable) {
  702. coresight_enable(drvdata->csdev);
  703. drvdata->boot_enable = true;
  704. }
  705. return 0;
  706. err_arch_supported:
  707. if (--etm_count == 0) {
  708. cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING);
  709. if (hp_online)
  710. cpuhp_remove_state_nocalls(hp_online);
  711. }
  712. return ret;
  713. }
  714. #ifdef CONFIG_PM
  715. static int etm_runtime_suspend(struct device *dev)
  716. {
  717. struct etm_drvdata *drvdata = dev_get_drvdata(dev);
  718. if (drvdata && !IS_ERR(drvdata->atclk))
  719. clk_disable_unprepare(drvdata->atclk);
  720. return 0;
  721. }
  722. static int etm_runtime_resume(struct device *dev)
  723. {
  724. struct etm_drvdata *drvdata = dev_get_drvdata(dev);
  725. if (drvdata && !IS_ERR(drvdata->atclk))
  726. clk_prepare_enable(drvdata->atclk);
  727. return 0;
  728. }
  729. #endif
  730. static const struct dev_pm_ops etm_dev_pm_ops = {
  731. SET_RUNTIME_PM_OPS(etm_runtime_suspend, etm_runtime_resume, NULL)
  732. };
  733. static const struct amba_id etm_ids[] = {
  734. { /* ETM 3.3 */
  735. .id = 0x000bb921,
  736. .mask = 0x000fffff,
  737. .data = "ETM 3.3",
  738. },
  739. { /* ETM 3.5 - Cortex-A5 */
  740. .id = 0x000bb955,
  741. .mask = 0x000fffff,
  742. .data = "ETM 3.5",
  743. },
  744. { /* ETM 3.5 */
  745. .id = 0x000bb956,
  746. .mask = 0x000fffff,
  747. .data = "ETM 3.5",
  748. },
  749. { /* PTM 1.0 */
  750. .id = 0x000bb950,
  751. .mask = 0x000fffff,
  752. .data = "PTM 1.0",
  753. },
  754. { /* PTM 1.1 */
  755. .id = 0x000bb95f,
  756. .mask = 0x000fffff,
  757. .data = "PTM 1.1",
  758. },
  759. { /* PTM 1.1 Qualcomm */
  760. .id = 0x000b006f,
  761. .mask = 0x000fffff,
  762. .data = "PTM 1.1",
  763. },
  764. { 0, 0},
  765. };
  766. static struct amba_driver etm_driver = {
  767. .drv = {
  768. .name = "coresight-etm3x",
  769. .owner = THIS_MODULE,
  770. .pm = &etm_dev_pm_ops,
  771. .suppress_bind_attrs = true,
  772. },
  773. .probe = etm_probe,
  774. .id_table = etm_ids,
  775. };
  776. builtin_amba_driver(etm_driver);