coresight-etm3x.c 23 KB

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