coresight-etm3x.c 23 KB

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