coresight-etm3x.c 24 KB

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