coresight-cpu-debug.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017 Linaro Limited. All rights reserved.
  4. *
  5. * Author: Leo Yan <leo.yan@linaro.org>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/amba/bus.h>
  9. #include <linux/coresight.h>
  10. #include <linux/cpu.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/panic_notifier.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_qos.h>
  24. #include <linux/slab.h>
  25. #include <linux/smp.h>
  26. #include <linux/types.h>
  27. #include <linux/uaccess.h>
  28. #include "coresight-priv.h"
  29. #define EDPCSR 0x0A0
  30. #define EDCIDSR 0x0A4
  31. #define EDVIDSR 0x0A8
  32. #define EDPCSR_HI 0x0AC
  33. #define EDOSLAR 0x300
  34. #define EDPRCR 0x310
  35. #define EDPRSR 0x314
  36. #define EDDEVID1 0xFC4
  37. #define EDDEVID 0xFC8
  38. #define EDPCSR_PROHIBITED 0xFFFFFFFF
  39. /* bits definition for EDPCSR */
  40. #define EDPCSR_THUMB BIT(0)
  41. #define EDPCSR_ARM_INST_MASK GENMASK(31, 2)
  42. #define EDPCSR_THUMB_INST_MASK GENMASK(31, 1)
  43. /* bits definition for EDPRCR */
  44. #define EDPRCR_COREPURQ BIT(3)
  45. #define EDPRCR_CORENPDRQ BIT(0)
  46. /* bits definition for EDPRSR */
  47. #define EDPRSR_DLK BIT(6)
  48. #define EDPRSR_PU BIT(0)
  49. /* bits definition for EDVIDSR */
  50. #define EDVIDSR_NS BIT(31)
  51. #define EDVIDSR_E2 BIT(30)
  52. #define EDVIDSR_E3 BIT(29)
  53. #define EDVIDSR_HV BIT(28)
  54. #define EDVIDSR_VMID GENMASK(7, 0)
  55. /*
  56. * bits definition for EDDEVID1:PSCROffset
  57. *
  58. * NOTE: armv8 and armv7 have different definition for the register,
  59. * so consolidate the bits definition as below:
  60. *
  61. * 0b0000 - Sample offset applies based on the instruction state, we
  62. * rely on EDDEVID to check if EDPCSR is implemented or not
  63. * 0b0001 - No offset applies.
  64. * 0b0010 - No offset applies, but do not use in AArch32 mode
  65. *
  66. */
  67. #define EDDEVID1_PCSR_OFFSET_MASK GENMASK(3, 0)
  68. #define EDDEVID1_PCSR_OFFSET_INS_SET (0x0)
  69. #define EDDEVID1_PCSR_NO_OFFSET_DIS_AARCH32 (0x2)
  70. /* bits definition for EDDEVID */
  71. #define EDDEVID_PCSAMPLE_MODE GENMASK(3, 0)
  72. #define EDDEVID_IMPL_EDPCSR (0x1)
  73. #define EDDEVID_IMPL_EDPCSR_EDCIDSR (0x2)
  74. #define EDDEVID_IMPL_FULL (0x3)
  75. #define DEBUG_WAIT_SLEEP 1000
  76. #define DEBUG_WAIT_TIMEOUT 32000
  77. struct debug_drvdata {
  78. struct clk *pclk;
  79. void __iomem *base;
  80. struct device *dev;
  81. int cpu;
  82. bool edpcsr_present;
  83. bool edcidsr_present;
  84. bool edvidsr_present;
  85. bool pc_has_offset;
  86. u32 edpcsr;
  87. u32 edpcsr_hi;
  88. u32 edprsr;
  89. u32 edvidsr;
  90. u32 edcidsr;
  91. };
  92. static DEFINE_MUTEX(debug_lock);
  93. static DEFINE_PER_CPU(struct debug_drvdata *, debug_drvdata);
  94. static int debug_count;
  95. static struct dentry *debug_debugfs_dir;
  96. static bool debug_enable = IS_ENABLED(CONFIG_CORESIGHT_CPU_DEBUG_DEFAULT_ON);
  97. module_param_named(enable, debug_enable, bool, 0600);
  98. MODULE_PARM_DESC(enable, "Control to enable coresight CPU debug functionality");
  99. static void debug_os_unlock(struct debug_drvdata *drvdata)
  100. {
  101. /* Unlocks the debug registers */
  102. writel_relaxed(0x0, drvdata->base + EDOSLAR);
  103. /* Make sure the registers are unlocked before accessing */
  104. wmb();
  105. }
  106. /*
  107. * According to ARM DDI 0487A.k, before access external debug
  108. * registers should firstly check the access permission; if any
  109. * below condition has been met then cannot access debug
  110. * registers to avoid lockup issue:
  111. *
  112. * - CPU power domain is powered off;
  113. * - The OS Double Lock is locked;
  114. *
  115. * By checking EDPRSR can get to know if meet these conditions.
  116. */
  117. static bool debug_access_permitted(struct debug_drvdata *drvdata)
  118. {
  119. /* CPU is powered off */
  120. if (!(drvdata->edprsr & EDPRSR_PU))
  121. return false;
  122. /* The OS Double Lock is locked */
  123. if (drvdata->edprsr & EDPRSR_DLK)
  124. return false;
  125. return true;
  126. }
  127. static void debug_force_cpu_powered_up(struct debug_drvdata *drvdata)
  128. {
  129. u32 edprcr;
  130. try_again:
  131. /*
  132. * Send request to power management controller and assert
  133. * DBGPWRUPREQ signal; if power management controller has
  134. * sane implementation, it should enable CPU power domain
  135. * in case CPU is in low power state.
  136. */
  137. edprcr = readl_relaxed(drvdata->base + EDPRCR);
  138. edprcr |= EDPRCR_COREPURQ;
  139. writel_relaxed(edprcr, drvdata->base + EDPRCR);
  140. /* Wait for CPU to be powered up (timeout~=32ms) */
  141. if (readx_poll_timeout_atomic(readl_relaxed, drvdata->base + EDPRSR,
  142. drvdata->edprsr, (drvdata->edprsr & EDPRSR_PU),
  143. DEBUG_WAIT_SLEEP, DEBUG_WAIT_TIMEOUT)) {
  144. /*
  145. * Unfortunately the CPU cannot be powered up, so return
  146. * back and later has no permission to access other
  147. * registers. For this case, should disable CPU low power
  148. * states to ensure CPU power domain is enabled!
  149. */
  150. dev_err(drvdata->dev, "%s: power up request for CPU%d failed\n",
  151. __func__, drvdata->cpu);
  152. return;
  153. }
  154. /*
  155. * At this point the CPU is powered up, so set the no powerdown
  156. * request bit so we don't lose power and emulate power down.
  157. */
  158. edprcr = readl_relaxed(drvdata->base + EDPRCR);
  159. edprcr |= EDPRCR_COREPURQ | EDPRCR_CORENPDRQ;
  160. writel_relaxed(edprcr, drvdata->base + EDPRCR);
  161. drvdata->edprsr = readl_relaxed(drvdata->base + EDPRSR);
  162. /* The core power domain got switched off on use, try again */
  163. if (unlikely(!(drvdata->edprsr & EDPRSR_PU)))
  164. goto try_again;
  165. }
  166. static void debug_read_regs(struct debug_drvdata *drvdata)
  167. {
  168. u32 save_edprcr;
  169. CS_UNLOCK(drvdata->base);
  170. /* Unlock os lock */
  171. debug_os_unlock(drvdata);
  172. /* Save EDPRCR register */
  173. save_edprcr = readl_relaxed(drvdata->base + EDPRCR);
  174. /*
  175. * Ensure CPU power domain is enabled to let registers
  176. * are accessiable.
  177. */
  178. debug_force_cpu_powered_up(drvdata);
  179. if (!debug_access_permitted(drvdata))
  180. goto out;
  181. drvdata->edpcsr = readl_relaxed(drvdata->base + EDPCSR);
  182. /*
  183. * As described in ARM DDI 0487A.k, if the processing
  184. * element (PE) is in debug state, or sample-based
  185. * profiling is prohibited, EDPCSR reads as 0xFFFFFFFF;
  186. * EDCIDSR, EDVIDSR and EDPCSR_HI registers also become
  187. * UNKNOWN state. So directly bail out for this case.
  188. */
  189. if (drvdata->edpcsr == EDPCSR_PROHIBITED)
  190. goto out;
  191. /*
  192. * A read of the EDPCSR normally has the side-effect of
  193. * indirectly writing to EDCIDSR, EDVIDSR and EDPCSR_HI;
  194. * at this point it's safe to read value from them.
  195. */
  196. if (IS_ENABLED(CONFIG_64BIT))
  197. drvdata->edpcsr_hi = readl_relaxed(drvdata->base + EDPCSR_HI);
  198. if (drvdata->edcidsr_present)
  199. drvdata->edcidsr = readl_relaxed(drvdata->base + EDCIDSR);
  200. if (drvdata->edvidsr_present)
  201. drvdata->edvidsr = readl_relaxed(drvdata->base + EDVIDSR);
  202. out:
  203. /* Restore EDPRCR register */
  204. writel_relaxed(save_edprcr, drvdata->base + EDPRCR);
  205. CS_LOCK(drvdata->base);
  206. }
  207. #ifdef CONFIG_64BIT
  208. static unsigned long debug_adjust_pc(struct debug_drvdata *drvdata)
  209. {
  210. return (unsigned long)drvdata->edpcsr_hi << 32 |
  211. (unsigned long)drvdata->edpcsr;
  212. }
  213. #else
  214. static unsigned long debug_adjust_pc(struct debug_drvdata *drvdata)
  215. {
  216. unsigned long arm_inst_offset = 0, thumb_inst_offset = 0;
  217. unsigned long pc;
  218. pc = (unsigned long)drvdata->edpcsr;
  219. if (drvdata->pc_has_offset) {
  220. arm_inst_offset = 8;
  221. thumb_inst_offset = 4;
  222. }
  223. /* Handle thumb instruction */
  224. if (pc & EDPCSR_THUMB) {
  225. pc = (pc & EDPCSR_THUMB_INST_MASK) - thumb_inst_offset;
  226. return pc;
  227. }
  228. /*
  229. * Handle arm instruction offset, if the arm instruction
  230. * is not 4 byte alignment then it's possible the case
  231. * for implementation defined; keep original value for this
  232. * case and print info for notice.
  233. */
  234. if (pc & BIT(1))
  235. dev_emerg(drvdata->dev,
  236. "Instruction offset is implementation defined\n");
  237. else
  238. pc = (pc & EDPCSR_ARM_INST_MASK) - arm_inst_offset;
  239. return pc;
  240. }
  241. #endif
  242. static void debug_dump_regs(struct debug_drvdata *drvdata)
  243. {
  244. struct device *dev = drvdata->dev;
  245. unsigned long pc;
  246. dev_emerg(dev, " EDPRSR: %08x (Power:%s DLK:%s)\n",
  247. drvdata->edprsr,
  248. drvdata->edprsr & EDPRSR_PU ? "On" : "Off",
  249. drvdata->edprsr & EDPRSR_DLK ? "Lock" : "Unlock");
  250. if (!debug_access_permitted(drvdata)) {
  251. dev_emerg(dev, "No permission to access debug registers!\n");
  252. return;
  253. }
  254. if (drvdata->edpcsr == EDPCSR_PROHIBITED) {
  255. dev_emerg(dev, "CPU is in Debug state or profiling is prohibited!\n");
  256. return;
  257. }
  258. pc = debug_adjust_pc(drvdata);
  259. dev_emerg(dev, " EDPCSR: %pS\n", (void *)pc);
  260. if (drvdata->edcidsr_present)
  261. dev_emerg(dev, " EDCIDSR: %08x\n", drvdata->edcidsr);
  262. if (drvdata->edvidsr_present)
  263. dev_emerg(dev, " EDVIDSR: %08x (State:%s Mode:%s Width:%dbits VMID:%x)\n",
  264. drvdata->edvidsr,
  265. drvdata->edvidsr & EDVIDSR_NS ?
  266. "Non-secure" : "Secure",
  267. drvdata->edvidsr & EDVIDSR_E3 ? "EL3" :
  268. (drvdata->edvidsr & EDVIDSR_E2 ?
  269. "EL2" : "EL1/0"),
  270. drvdata->edvidsr & EDVIDSR_HV ? 64 : 32,
  271. drvdata->edvidsr & (u32)EDVIDSR_VMID);
  272. }
  273. static void debug_init_arch_data(void *info)
  274. {
  275. struct debug_drvdata *drvdata = info;
  276. u32 mode, pcsr_offset;
  277. u32 eddevid, eddevid1;
  278. CS_UNLOCK(drvdata->base);
  279. /* Read device info */
  280. eddevid = readl_relaxed(drvdata->base + EDDEVID);
  281. eddevid1 = readl_relaxed(drvdata->base + EDDEVID1);
  282. CS_LOCK(drvdata->base);
  283. /* Parse implementation feature */
  284. mode = eddevid & EDDEVID_PCSAMPLE_MODE;
  285. pcsr_offset = eddevid1 & EDDEVID1_PCSR_OFFSET_MASK;
  286. drvdata->edpcsr_present = false;
  287. drvdata->edcidsr_present = false;
  288. drvdata->edvidsr_present = false;
  289. drvdata->pc_has_offset = false;
  290. switch (mode) {
  291. case EDDEVID_IMPL_FULL:
  292. drvdata->edvidsr_present = true;
  293. fallthrough;
  294. case EDDEVID_IMPL_EDPCSR_EDCIDSR:
  295. drvdata->edcidsr_present = true;
  296. fallthrough;
  297. case EDDEVID_IMPL_EDPCSR:
  298. /*
  299. * In ARM DDI 0487A.k, the EDDEVID1.PCSROffset is used to
  300. * define if has the offset for PC sampling value; if read
  301. * back EDDEVID1.PCSROffset == 0x2, then this means the debug
  302. * module does not sample the instruction set state when
  303. * armv8 CPU in AArch32 state.
  304. */
  305. drvdata->edpcsr_present =
  306. ((IS_ENABLED(CONFIG_64BIT) && pcsr_offset != 0) ||
  307. (pcsr_offset != EDDEVID1_PCSR_NO_OFFSET_DIS_AARCH32));
  308. drvdata->pc_has_offset =
  309. (pcsr_offset == EDDEVID1_PCSR_OFFSET_INS_SET);
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. /*
  316. * Dump out information on panic.
  317. */
  318. static int debug_notifier_call(struct notifier_block *self,
  319. unsigned long v, void *p)
  320. {
  321. int cpu;
  322. struct debug_drvdata *drvdata;
  323. /* Bail out if we can't acquire the mutex or the functionality is off */
  324. if (!mutex_trylock(&debug_lock))
  325. return NOTIFY_DONE;
  326. if (!debug_enable)
  327. goto skip_dump;
  328. pr_emerg("ARM external debug module:\n");
  329. for_each_possible_cpu(cpu) {
  330. drvdata = per_cpu(debug_drvdata, cpu);
  331. if (!drvdata)
  332. continue;
  333. dev_emerg(drvdata->dev, "CPU[%d]:\n", drvdata->cpu);
  334. debug_read_regs(drvdata);
  335. debug_dump_regs(drvdata);
  336. }
  337. skip_dump:
  338. mutex_unlock(&debug_lock);
  339. return NOTIFY_DONE;
  340. }
  341. static struct notifier_block debug_notifier = {
  342. .notifier_call = debug_notifier_call,
  343. };
  344. static int debug_enable_func(void)
  345. {
  346. struct debug_drvdata *drvdata;
  347. int cpu, ret = 0;
  348. cpumask_t mask;
  349. /*
  350. * Use cpumask to track which debug power domains have
  351. * been powered on and use it to handle failure case.
  352. */
  353. cpumask_clear(&mask);
  354. for_each_possible_cpu(cpu) {
  355. drvdata = per_cpu(debug_drvdata, cpu);
  356. if (!drvdata)
  357. continue;
  358. ret = pm_runtime_get_sync(drvdata->dev);
  359. if (ret < 0)
  360. goto err;
  361. else
  362. cpumask_set_cpu(cpu, &mask);
  363. }
  364. return 0;
  365. err:
  366. /*
  367. * If pm_runtime_get_sync() has failed, need rollback on
  368. * all the other CPUs that have been enabled before that.
  369. */
  370. for_each_cpu(cpu, &mask) {
  371. drvdata = per_cpu(debug_drvdata, cpu);
  372. pm_runtime_put_noidle(drvdata->dev);
  373. }
  374. return ret;
  375. }
  376. static void debug_disable_func(void)
  377. {
  378. struct debug_drvdata *drvdata;
  379. int cpu;
  380. /*
  381. * Disable debug power domains, records the error and keep
  382. * circling through all other CPUs when an error has been
  383. * encountered.
  384. */
  385. for_each_possible_cpu(cpu) {
  386. drvdata = per_cpu(debug_drvdata, cpu);
  387. if (!drvdata)
  388. continue;
  389. pm_runtime_put(drvdata->dev);
  390. }
  391. }
  392. static ssize_t debug_func_knob_write(struct file *f,
  393. const char __user *buf, size_t count, loff_t *ppos)
  394. {
  395. u8 val;
  396. int ret;
  397. ret = kstrtou8_from_user(buf, count, 2, &val);
  398. if (ret)
  399. return ret;
  400. mutex_lock(&debug_lock);
  401. if (val == debug_enable)
  402. goto out;
  403. if (val)
  404. ret = debug_enable_func();
  405. else
  406. debug_disable_func();
  407. if (ret) {
  408. pr_err("%s: unable to %s debug function: %d\n",
  409. __func__, val ? "enable" : "disable", ret);
  410. goto err;
  411. }
  412. debug_enable = val;
  413. out:
  414. ret = count;
  415. err:
  416. mutex_unlock(&debug_lock);
  417. return ret;
  418. }
  419. static ssize_t debug_func_knob_read(struct file *f,
  420. char __user *ubuf, size_t count, loff_t *ppos)
  421. {
  422. ssize_t ret;
  423. char buf[3];
  424. mutex_lock(&debug_lock);
  425. snprintf(buf, sizeof(buf), "%d\n", debug_enable);
  426. mutex_unlock(&debug_lock);
  427. ret = simple_read_from_buffer(ubuf, count, ppos, buf, sizeof(buf));
  428. return ret;
  429. }
  430. static const struct file_operations debug_func_knob_fops = {
  431. .open = simple_open,
  432. .read = debug_func_knob_read,
  433. .write = debug_func_knob_write,
  434. };
  435. static int debug_func_init(void)
  436. {
  437. int ret;
  438. /* Create debugfs node */
  439. debug_debugfs_dir = debugfs_create_dir("coresight_cpu_debug", NULL);
  440. debugfs_create_file("enable", 0644, debug_debugfs_dir, NULL,
  441. &debug_func_knob_fops);
  442. /* Register function to be called for panic */
  443. ret = atomic_notifier_chain_register(&panic_notifier_list,
  444. &debug_notifier);
  445. if (ret) {
  446. pr_err("%s: unable to register notifier: %d\n",
  447. __func__, ret);
  448. goto err;
  449. }
  450. return 0;
  451. err:
  452. debugfs_remove_recursive(debug_debugfs_dir);
  453. return ret;
  454. }
  455. static void debug_func_exit(void)
  456. {
  457. atomic_notifier_chain_unregister(&panic_notifier_list,
  458. &debug_notifier);
  459. debugfs_remove_recursive(debug_debugfs_dir);
  460. }
  461. static int __debug_probe(struct device *dev, struct resource *res)
  462. {
  463. struct debug_drvdata *drvdata;
  464. void __iomem *base;
  465. int ret;
  466. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  467. if (!drvdata)
  468. return -ENOMEM;
  469. dev_set_drvdata(dev, drvdata);
  470. ret = coresight_get_enable_clocks(dev, &drvdata->pclk, NULL);
  471. if (ret)
  472. return ret;
  473. drvdata->cpu = coresight_get_cpu(dev);
  474. if (drvdata->cpu < 0)
  475. return drvdata->cpu;
  476. if (per_cpu(debug_drvdata, drvdata->cpu)) {
  477. dev_err(dev, "CPU%d drvdata has already been initialized\n",
  478. drvdata->cpu);
  479. return -EBUSY;
  480. }
  481. drvdata->dev = dev;
  482. base = devm_ioremap_resource(dev, res);
  483. if (IS_ERR(base))
  484. return PTR_ERR(base);
  485. drvdata->base = base;
  486. cpus_read_lock();
  487. per_cpu(debug_drvdata, drvdata->cpu) = drvdata;
  488. ret = smp_call_function_single(drvdata->cpu, debug_init_arch_data,
  489. drvdata, 1);
  490. cpus_read_unlock();
  491. if (ret) {
  492. dev_err(dev, "CPU%d debug arch init failed\n", drvdata->cpu);
  493. goto err;
  494. }
  495. if (!drvdata->edpcsr_present) {
  496. dev_err(dev, "CPU%d sample-based profiling isn't implemented\n",
  497. drvdata->cpu);
  498. ret = -ENXIO;
  499. goto err;
  500. }
  501. if (!debug_count++) {
  502. ret = debug_func_init();
  503. if (ret)
  504. goto err_func_init;
  505. }
  506. mutex_lock(&debug_lock);
  507. /* Turn off debug power domain if debugging is disabled */
  508. if (!debug_enable)
  509. pm_runtime_put(dev);
  510. mutex_unlock(&debug_lock);
  511. dev_info(dev, "Coresight debug-CPU%d initialized\n", drvdata->cpu);
  512. return 0;
  513. err_func_init:
  514. debug_count--;
  515. err:
  516. per_cpu(debug_drvdata, drvdata->cpu) = NULL;
  517. return ret;
  518. }
  519. static int debug_probe(struct amba_device *adev, const struct amba_id *id)
  520. {
  521. return __debug_probe(&adev->dev, &adev->res);
  522. }
  523. static void __debug_remove(struct device *dev)
  524. {
  525. struct debug_drvdata *drvdata = dev_get_drvdata(dev);
  526. per_cpu(debug_drvdata, drvdata->cpu) = NULL;
  527. mutex_lock(&debug_lock);
  528. /* Turn off debug power domain before rmmod the module */
  529. if (debug_enable)
  530. pm_runtime_put(dev);
  531. mutex_unlock(&debug_lock);
  532. if (!--debug_count)
  533. debug_func_exit();
  534. }
  535. static void debug_remove(struct amba_device *adev)
  536. {
  537. __debug_remove(&adev->dev);
  538. }
  539. static const struct amba_cs_uci_id uci_id_debug[] = {
  540. {
  541. /* CPU Debug UCI data */
  542. .devarch = 0x47706a15,
  543. .devarch_mask = 0xfff0ffff,
  544. .devtype = 0x00000015,
  545. }
  546. };
  547. static const struct amba_id debug_ids[] = {
  548. CS_AMBA_ID(0x000bbd03), /* Cortex-A53 */
  549. CS_AMBA_ID(0x000bbd07), /* Cortex-A57 */
  550. CS_AMBA_ID(0x000bbd08), /* Cortex-A72 */
  551. CS_AMBA_ID(0x000bbd09), /* Cortex-A73 */
  552. CS_AMBA_UCI_ID(0x000f0205, uci_id_debug), /* Qualcomm Kryo */
  553. CS_AMBA_UCI_ID(0x000f0211, uci_id_debug), /* Qualcomm Kryo */
  554. {},
  555. };
  556. MODULE_DEVICE_TABLE(amba, debug_ids);
  557. static struct amba_driver debug_driver = {
  558. .drv = {
  559. .name = "coresight-cpu-debug",
  560. .suppress_bind_attrs = true,
  561. },
  562. .probe = debug_probe,
  563. .remove = debug_remove,
  564. .id_table = debug_ids,
  565. };
  566. static int debug_platform_probe(struct platform_device *pdev)
  567. {
  568. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  569. int ret = 0;
  570. pm_runtime_get_noresume(&pdev->dev);
  571. pm_runtime_set_active(&pdev->dev);
  572. pm_runtime_enable(&pdev->dev);
  573. ret = __debug_probe(&pdev->dev, res);
  574. if (ret) {
  575. pm_runtime_put_noidle(&pdev->dev);
  576. pm_runtime_disable(&pdev->dev);
  577. }
  578. return ret;
  579. }
  580. static void debug_platform_remove(struct platform_device *pdev)
  581. {
  582. struct debug_drvdata *drvdata = dev_get_drvdata(&pdev->dev);
  583. if (WARN_ON(!drvdata))
  584. return;
  585. __debug_remove(&pdev->dev);
  586. pm_runtime_disable(&pdev->dev);
  587. }
  588. #ifdef CONFIG_ACPI
  589. static const struct acpi_device_id debug_platform_ids[] = {
  590. {"ARMHC503", 0, 0, 0}, /* ARM CoreSight Debug */
  591. {},
  592. };
  593. MODULE_DEVICE_TABLE(acpi, debug_platform_ids);
  594. #endif
  595. #ifdef CONFIG_PM
  596. static int debug_runtime_suspend(struct device *dev)
  597. {
  598. struct debug_drvdata *drvdata = dev_get_drvdata(dev);
  599. clk_disable_unprepare(drvdata->pclk);
  600. return 0;
  601. }
  602. static int debug_runtime_resume(struct device *dev)
  603. {
  604. struct debug_drvdata *drvdata = dev_get_drvdata(dev);
  605. return clk_prepare_enable(drvdata->pclk);
  606. }
  607. #endif
  608. static const struct dev_pm_ops debug_dev_pm_ops = {
  609. SET_RUNTIME_PM_OPS(debug_runtime_suspend, debug_runtime_resume, NULL)
  610. };
  611. static struct platform_driver debug_platform_driver = {
  612. .probe = debug_platform_probe,
  613. .remove = debug_platform_remove,
  614. .driver = {
  615. .name = "coresight-debug-platform",
  616. .acpi_match_table = ACPI_PTR(debug_platform_ids),
  617. .suppress_bind_attrs = true,
  618. .pm = &debug_dev_pm_ops,
  619. },
  620. };
  621. static int __init debug_init(void)
  622. {
  623. return coresight_init_driver("debug", &debug_driver, &debug_platform_driver,
  624. THIS_MODULE);
  625. }
  626. static void __exit debug_exit(void)
  627. {
  628. coresight_remove_driver(&debug_driver, &debug_platform_driver);
  629. }
  630. module_init(debug_init);
  631. module_exit(debug_exit);
  632. MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
  633. MODULE_DESCRIPTION("ARM Coresight CPU Debug Driver");
  634. MODULE_LICENSE("GPL");