hw_watchpoint_aarch64.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #include <linux/spinlock.h>
  2. #include <linux/errno.h>
  3. #include <linux/module.h>
  4. #include <asm/signal.h>
  5. #include <asm/ptrace.h>
  6. #include "hw_watchpoint_aarch64.h"
  7. #include "mt_dbg_aarch64.h"
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/of_address.h>
  12. #include <linux/cpu.h>
  13. #include <linux/delay.h>
  14. #include <mt-plat/sync_write.h>
  15. #include <asm/system_misc.h>
  16. struct wp_trace_context_t wp_tracer;
  17. #ifdef WATCHPOINT_TEST_SUIT
  18. struct wp_event wp_event;
  19. int err;
  20. volatile int my_watch_data;
  21. int wp_flag;
  22. int my_wp_handler1(phys_addr_t addr)
  23. {
  24. wp_flag++;
  25. pr_debug("[MTK WP] Access my data from an instruction at %p\n", &addr);
  26. return 0;
  27. }
  28. int my_wp_handler2(phys_addr_t addr)
  29. {
  30. pr_debug("[MTK WP] In my_wp_handler2 Access my data from an instruction at %p\n", &addr);
  31. /* trigger exception */
  32. return 0;
  33. }
  34. void wp_test1(void)
  35. {
  36. int i;
  37. init_wp_event(&wp_event, (unsigned long)&my_watch_data, (unsigned long)&my_watch_data,
  38. WP_EVENT_TYPE_ALL, my_wp_handler1);
  39. err = add_hw_watchpoint(&wp_event);
  40. if (err)
  41. /* fail to add watchpoing */
  42. pr_debug("[MTK WP] add hw watch point failed...\n");
  43. else
  44. /* the memory address is under watching */
  45. pr_debug("[MTK WP] add hw watch point success...\n");
  46. for (i = 0; i < num_possible_cpus(); i++) {
  47. #ifdef DBG_REG_DUMP
  48. dump_dbgregs(i);
  49. print_dbgregs(i);
  50. #endif
  51. }
  52. pr_debug("start to access my_watch_data\n");
  53. /* test watchpoint */
  54. my_watch_data = 1;
  55. }
  56. void smp_specific_write(int *p)
  57. {
  58. *p = 1;
  59. pr_debug("[MTK WP] wite data in specific address ok,addr=0x%p\n", p);
  60. }
  61. void wp_test2(void)
  62. {
  63. int i;
  64. int ret = 0;
  65. wp_flag = 0;
  66. pr_debug("[MTK WP] Init wp.. ");
  67. init_wp_event(&wp_event, (unsigned long)&my_watch_data, (unsigned long)&my_watch_data,
  68. WP_EVENT_TYPE_ALL, my_wp_handler1);
  69. err = add_hw_watchpoint(&wp_event);
  70. if (err) {
  71. pr_debug("[MTK WP] add hw watch point failed...\n");
  72. /* fail to add watchpoing */
  73. } else {
  74. /* the memory address is under watching */
  75. pr_debug("[MTK WP] add hw watch point success...\n");
  76. }
  77. pr_debug("[MTK WP] dump standard dbgsys setting\n");
  78. for (i = 1; i < num_possible_cpus(); i++) {
  79. ret = cpu_down(i); /* FIXME Walkround for build error */
  80. if (ret != 0)
  81. pr_debug("[MTK WP] cpu %d power down failed\n", i);
  82. if (!cpu_online(i))
  83. pr_debug("[MTK WP] cpu %d already power down\n", i);
  84. }
  85. for (i = 0; i < num_possible_cpus(); i++) {
  86. ret = cpu_up(i);
  87. if (ret != 0)
  88. pr_debug("[MTK WP] cpu %d power up failed\n", i);
  89. if (cpu_online(i))
  90. pr_debug("[MTK WP] cpu %d already power up\n", i);
  91. pr_debug("[MTK WP] dump dbgsys setting restored\n");
  92. #ifdef DBG_REG_DUMP
  93. dump_dbgregs(i);
  94. print_dbgregs(i);
  95. #endif
  96. ret =
  97. smp_call_function_single(i, (smp_call_func_t) smp_specific_write,
  98. (void *)&my_watch_data, 1);
  99. if (ret == 0)
  100. pr_debug("[MTK WP] cpu %d, wite data in specific address ok\n", i);
  101. else
  102. pr_debug("[MTK WP] cpu %d, wite data in specific address failed\n", i);
  103. }
  104. if (wp_flag == num_possible_cpus())
  105. pr_debug("[MTK WP] Watchpoint item2 verfication pass 0x%x\n", wp_flag);
  106. else
  107. pr_debug("[MTK WP] Watchpoint item2 verfication failed 0x%x\n", wp_flag);
  108. }
  109. void wp_test3(void)
  110. {
  111. int i;
  112. int ret;
  113. wp_flag = 0;
  114. for (i = 0; i < num_possible_cpus(); i++) {
  115. ret = cpu_up(i);
  116. if (ret != 0)
  117. pr_debug("[MTK WP] cpu %d power up failed\n", i);
  118. if (cpu_online(i))
  119. pr_debug("[MTK WP] cpu %d already power up\n", i);
  120. pr_debug("[MTK WP] dump dbgsys setting restored\n");
  121. #ifdef DBG_REG_DUMP
  122. dump_dbgregs(i);
  123. print_dbgregs(i);
  124. #endif
  125. ret =
  126. smp_call_function_single(i, (smp_call_func_t) smp_specific_write,
  127. (void *)&my_watch_data, 1);
  128. if (ret == 0)
  129. pr_debug("[MTK WP] cpu %d, wite data in specific address ok\n", i);
  130. else
  131. pr_debug("[MTK WP] cpu %d, wite data in specific address failed\n", i);
  132. }
  133. if (wp_flag == num_possible_cpus())
  134. pr_debug("[MTK WP] Watchpoint item3 verfication pass 0x%x\n", wp_flag);
  135. else
  136. pr_debug("[MTK WP] Watchpoint item3 verfication failed 0x%x\n", wp_flag);
  137. }
  138. #endif
  139. void smp_read_MDSCR_EL1_callback(void *info)
  140. {
  141. unsigned int tmp;
  142. unsigned int *val = info;
  143. asm volatile ("mrs %0, MDSCR_EL1":"=r" (tmp));
  144. *val = tmp;
  145. }
  146. void smp_write_MDSCR_EL1_callback(void *info)
  147. {
  148. unsigned int *val = info;
  149. unsigned int tmp = *val;
  150. asm volatile ("msr MDSCR_EL1, %0" : : "r" (tmp));
  151. saved_MDSCR_EL1 = tmp;
  152. }
  153. void smp_read_OSLSR_EL1_callback(void *info)
  154. {
  155. unsigned int tmp;
  156. unsigned int *val = info;
  157. asm volatile ("mrs %0, OSLSR_EL1":"=r" (tmp));
  158. *val = tmp;
  159. }
  160. static spinlock_t wp_lock;
  161. int register_wp_context(struct wp_trace_context_t **wp_tracer_context)
  162. {
  163. *wp_tracer_context = &wp_tracer;
  164. return 0;
  165. }
  166. /*
  167. * enable_hw_watchpoint: Enable the H/W watchpoint.
  168. * Return error code.
  169. */
  170. int enable_hw_watchpoint(void)
  171. {
  172. int i;
  173. unsigned int args;
  174. int oslsr_el1;
  175. int edlsr;
  176. pr_debug("[MTK WP] Hotplug disable\n");
  177. cpu_hotplug_disable();
  178. for (i = 0; i < num_possible_cpus(); i++) {
  179. if (cpu_online(i)) {
  180. cs_cpu_write(wp_tracer.debug_regs[i], EDLAR, UNLOCK_KEY);
  181. cs_cpu_write(wp_tracer.debug_regs[i], OSLAR_EL1, ~UNLOCK_KEY);
  182. args = cs_cpu_read(wp_tracer.debug_regs[i], EDSCR);
  183. pr_debug("[MTK WP] cpu %d, EDSCR &0x%lx= 0x%x\n", i,
  184. ((vmalloc_to_pfn((void *)wp_tracer.debug_regs[i]) << 12) + EDSCR),
  185. args);
  186. if (args & HDE) {
  187. pr_debug
  188. ("[MTK WP] halting debug mode enabled. Unable to access hardware resources.\n");
  189. return -EPERM;
  190. }
  191. smp_call_function_single(i, smp_read_MDSCR_EL1_callback, &args, 1);
  192. pr_debug("[MTK WP] cpu %d, MDSCR 0x%x\n", i, args);
  193. if (args & MDBGEN) {
  194. /* already enabled */
  195. pr_debug("[MTK WP] already enabled, MDSCR = 0x%x\n", args);
  196. }
  197. /*
  198. * Since Watchpoint taken from EL1 to EL1, so we have to enable KDE.
  199. * refer ARMv8 architecture spec section -
  200. * D2.4.1 Enabling debug exceptions from the current Exception level
  201. */
  202. args = args | MDBGEN | KDE;
  203. smp_call_function_single(i, smp_write_MDSCR_EL1_callback, &args, 1);
  204. smp_call_function_single(i, smp_read_MDSCR_EL1_callback, &args, 1);
  205. smp_call_function_single(i, smp_read_OSLSR_EL1_callback, &oslsr_el1, 1);
  206. edlsr = cs_cpu_read(wp_tracer.debug_regs[i], EDLSR);
  207. pr_debug("[MTK WP] cpu %d, EDLSR 0x%x, OSLSR_EL1 0x%x\n", i, edlsr,
  208. oslsr_el1);
  209. pr_debug("[MTK WP] cpu %d, MDSCR_EL1 0x%x. (after set MDSCR_EL1)\n", i,
  210. args);
  211. } else {
  212. pr_debug("[MTK WP] cpu %d, power down(%d) so skip enable_hw_watchpoint\n",
  213. i, cpu_online(i));
  214. }
  215. }
  216. pr_debug("[MTK WP] Hotplug enable\n");
  217. cpu_hotplug_enable();
  218. return 0;
  219. }
  220. void reset_watchpoint(void)
  221. {
  222. int j;
  223. int i;
  224. unsigned int args;
  225. for (j = 0; j < num_possible_cpus(); j++) {
  226. if (cpu_online(j)) {
  227. cs_cpu_write(wp_tracer.debug_regs[j], EDLAR, UNLOCK_KEY);
  228. cs_cpu_write(wp_tracer.debug_regs[j], OSLAR_EL1, ~UNLOCK_KEY);
  229. args = cs_cpu_read(wp_tracer.debug_regs[j], EDSCR);
  230. pr_debug("[MTK WP] Reset flow cpu %d, EDSCR 0x%x\n", j, args);
  231. if (args & HDE) {
  232. pr_debug("[MTK WP]Reset flow halting debug mode enabled. Unable to reset hardware resources.\n");
  233. cs_cpu_write(wp_tracer.debug_regs[j], EDLAR, ~UNLOCK_KEY);
  234. cs_cpu_write(wp_tracer.debug_regs[j], OSLAR_EL1, UNLOCK_KEY);
  235. return;
  236. }
  237. for (i = 0; i < wp_tracer.wp_nr; i++) {
  238. cs_cpu_write_64(wp_tracer.debug_regs[j], DBGWVR + (i << 4), 0);
  239. cs_cpu_write(wp_tracer.debug_regs[j], DBGWCR + (i << 4), 0);
  240. pr_debug("[MTK WP] Reset flow cpu %d, DBGWVR%d, &0x%p=0x%lx\n", j,
  241. i, wp_tracer.debug_regs[j] + DBGWVR + (i << 4),
  242. cs_cpu_read_64(wp_tracer.debug_regs[j],
  243. DBGWVR + (i << 4)));
  244. pr_debug("[MTK WP] Reset flow cpu %d, DBGWCR%d, &0x%p=0x%x\n", j,
  245. i, wp_tracer.debug_regs[j] + DBGWCR + (i << 4),
  246. cs_cpu_read(wp_tracer.debug_regs[j], DBGWCR + (i << 4)));
  247. }
  248. for (i = 0; i < wp_tracer.bp_nr; i++) {
  249. cs_cpu_write_64(wp_tracer.debug_regs[j], DBGBVR + (i << 4), 0);
  250. cs_cpu_write(wp_tracer.debug_regs[j], DBGBCR + (i << 4), 0);
  251. pr_debug("[MTK WP] Reset flow cpu %d, DBGBVR%d, &0x%p=0x%lx\n", j,
  252. i, wp_tracer.debug_regs[j] + DBGBVR + (i << 4),
  253. cs_cpu_read_64(wp_tracer.debug_regs[j],
  254. DBGBVR + (i << 4)));
  255. pr_debug("[MTK WP] Reset flow cpu %d, DBGBCR%d, &0x%p=0x%x\n", j,
  256. i, wp_tracer.debug_regs[j] + DBGBCR + (i << 4),
  257. cs_cpu_read(wp_tracer.debug_regs[j], DBGBCR + (i << 4)));
  258. }
  259. cs_cpu_write(wp_tracer.debug_regs[j], EDLAR, ~UNLOCK_KEY);
  260. cs_cpu_write(wp_tracer.debug_regs[j], OSLAR_EL1, UNLOCK_KEY);
  261. } else {
  262. pr_debug("[MTK WP] cpu %d, power down(%d) so skip adding watchpoint\n", j,
  263. cpu_online(j));
  264. }
  265. }
  266. }
  267. /*
  268. * add_hw_watchpoint: add a watch point.
  269. * @wp_event: pointer to the struct wp_event.
  270. * Return error code.
  271. */
  272. int add_hw_watchpoint(struct wp_event *wp_event)
  273. {
  274. int ret, i, j;
  275. unsigned long flags;
  276. unsigned int ctl;
  277. if (!wp_event)
  278. return -EINVAL;
  279. if (!(wp_event->handler))
  280. return -EINVAL;
  281. ret = enable_hw_watchpoint();
  282. if (ret)
  283. return ret;
  284. ctl = DBGWCR_VAL;
  285. if (wp_event->type == WP_EVENT_TYPE_READ)
  286. ctl |= LSC_LDR;
  287. else if (wp_event->type == WP_EVENT_TYPE_WRITE)
  288. ctl |= LSC_STR;
  289. else if (wp_event->type == WP_EVENT_TYPE_ALL)
  290. ctl |= LSC_ALL;
  291. else
  292. return -EINVAL;
  293. spin_lock_irqsave(&wp_lock, flags);
  294. for (i = 0; i < MAX_NR_WATCH_POINT; i++) {
  295. if (!wp_tracer.wp_events[i].in_use) {
  296. wp_tracer.wp_events[i].in_use = 1;
  297. break;
  298. }
  299. if (wp_tracer.wp_events[i].virt == (wp_event->virt & ~3)) {
  300. pr_err("This address have been watched in %d's watchpoint\n", i);
  301. spin_unlock_irqrestore(&wp_lock, flags);
  302. return -EAGAIN;
  303. }
  304. }
  305. spin_unlock_irqrestore(&wp_lock, flags);
  306. if (i == MAX_NR_WATCH_POINT)
  307. return -EAGAIN;
  308. wp_tracer.wp_events[i].virt = wp_event->virt & ~3; /* enforce word-aligned */
  309. wp_tracer.wp_events[i].phys = wp_event->phys; /* no use currently */
  310. wp_tracer.wp_events[i].type = wp_event->type;
  311. wp_tracer.wp_events[i].handler = wp_event->handler;
  312. wp_tracer.wp_events[i].auto_disable = wp_event->auto_disable;
  313. pr_debug("[MTK WP] Hotplug disable\n");
  314. cpu_hotplug_disable();
  315. pr_debug("[MTK WP] Add watchpoint %d at address %p\n", i, &(wp_tracer.wp_events[i].virt));
  316. for (j = 0; j < num_possible_cpus(); j++) {
  317. if (cpu_online(j)) {
  318. cs_cpu_write_64(wp_tracer.debug_regs[j], DBGWVR + (i << 4),
  319. wp_tracer.wp_events[i].virt);
  320. cs_cpu_write(wp_tracer.debug_regs[j], DBGWCR + (i << 4), ctl);
  321. pr_debug("[MTK WP] cpu %d, DBGWVR%d, &0x%p=0x%lx\n", j, i,
  322. wp_tracer.debug_regs[j] + DBGWVR + (i << 4),
  323. cs_cpu_read_64(wp_tracer.debug_regs[j], DBGWVR + (i << 4)));
  324. pr_debug("[MTK WP] cpu %d, DBGWCR%d, &0x%p=0x%x\n", j, i,
  325. wp_tracer.debug_regs[j] + DBGWCR + (i << 4),
  326. cs_cpu_read(wp_tracer.debug_regs[j], DBGWCR + (i << 4)));
  327. } else {
  328. pr_debug("[MTK WP] cpu %d, power down(%d) so skip adding watchpoint\n", j,
  329. cpu_online(j));
  330. }
  331. }
  332. pr_debug("[MTK WP] Hotplug enable\n");
  333. cpu_hotplug_enable();
  334. return 0;
  335. }
  336. /*
  337. * del_hw_watchpoint: delete a watch point.
  338. * @wp_event: pointer to the struct wp_event.
  339. * Return error code.
  340. */
  341. int del_hw_watchpoint(struct wp_event *wp_event)
  342. {
  343. unsigned long flags;
  344. int i, j;
  345. if (!wp_event)
  346. return -EINVAL;
  347. pr_debug("[MTK WP] Hotplug disable\n");
  348. cpu_hotplug_disable();
  349. spin_lock_irqsave(&wp_lock, flags);
  350. for (i = 0; i < MAX_NR_WATCH_POINT; i++) {
  351. if (wp_tracer.wp_events[i].in_use
  352. && (wp_tracer.wp_events[i].virt == wp_event->virt)) {
  353. wp_tracer.wp_events[i].virt = 0;
  354. wp_tracer.wp_events[i].phys = 0;
  355. wp_tracer.wp_events[i].type = 0;
  356. wp_tracer.wp_events[i].handler = NULL;
  357. wp_tracer.wp_events[i].in_use = 0;
  358. for (j = 0; j < num_possible_cpus(); j++) {
  359. if (cpu_online(j)) {
  360. cs_cpu_write(wp_tracer.debug_regs[j], DBGWCR + (i << 4),
  361. cs_cpu_read(wp_tracer.debug_regs[j],
  362. DBGWCR + (i << 4)) & (~WP_EN));
  363. cs_cpu_write(wp_tracer.debug_regs[j], EDLAR, ~UNLOCK_KEY);
  364. cs_cpu_write(wp_tracer.debug_regs[j], OSLAR_EL1,
  365. UNLOCK_KEY);
  366. }
  367. }
  368. break;
  369. }
  370. }
  371. spin_unlock_irqrestore(&wp_lock, flags);
  372. pr_debug("[MTK WP] Hotplug enable\n");
  373. cpu_hotplug_enable();
  374. if (i == MAX_NR_WATCH_POINT)
  375. return -EINVAL;
  376. else
  377. return 0;
  378. }
  379. static int watchpoint_handler(unsigned long addr, unsigned int esr, struct pt_regs *regs)
  380. {
  381. unsigned long wfar, daddr, iaddr;
  382. int i, ret, j;
  383. /* Notes
  384. * wfar is the watched data address which is accessed . and it is from FAR_EL1
  385. */
  386. asm volatile ("mrs %0, FAR_EL1":"=r" (wfar));
  387. daddr = addr & ~3;
  388. iaddr = regs->pc; /* this is the instruction address that access the data that is watching. */
  389. pr_debug("[MTK WP] addr = 0x%lx, DBGWFAR/DFAR = 0x%lx\n", (unsigned long)addr, wfar);
  390. pr_debug("[MTK WP] daddr = 0x%lx, iaddr = 0x%lx\n", daddr, iaddr);
  391. /* update PC to avoid re-execution of the instruction under watching */
  392. regs->pc += compat_thumb_mode(regs) ? 2 : 8;
  393. for (i = 0; i < MAX_NR_WATCH_POINT; i++) {
  394. if (wp_tracer.wp_events[i].in_use && wp_tracer.wp_events[i].virt == (daddr)) {
  395. pr_debug("[MTK WP] Watchpoint %d triggers.\n", i);
  396. if (!(wp_tracer.wp_events[i].handler)) {
  397. pr_debug("[MTK WP] No watchpoint handler. Ignore.\n");
  398. return 0;
  399. }
  400. if (wp_tracer.wp_events[i].auto_disable) {
  401. for (j = 0; j < num_possible_cpus(); j++) {
  402. if (cpu_online(j)) {
  403. cs_cpu_write(wp_tracer.debug_regs[j],
  404. DBGWCR + (i << 4),
  405. cs_cpu_read
  406. (wp_tracer.debug_regs[j],
  407. DBGWCR +
  408. (i << 4)) & (~WP_EN));
  409. } else {
  410. pr_debug("[MTK WP] cpu %d, power down(%d) so skip adding watchpoint auto-disable\n",
  411. j, cpu_online(j));
  412. }
  413. }
  414. }
  415. ret = wp_tracer.wp_events[i].handler(iaddr);
  416. if (wp_tracer.wp_events[i].auto_disable) {
  417. for (j = 0; j < num_possible_cpus(); j++) {
  418. if (cpu_online(j)) {
  419. cs_cpu_write(wp_tracer.debug_regs[j],
  420. DBGWCR + (i << 4),
  421. cs_cpu_read
  422. (wp_tracer.debug_regs[j],
  423. DBGWCR + (i << 4)) | WP_EN);
  424. } else {
  425. pr_debug("[MTK WP] cpu %d, power down(%d) so skip watchpoint auto-disable\n",
  426. j, cpu_online(j));
  427. }
  428. }
  429. }
  430. return ret;
  431. }
  432. }
  433. return 0;
  434. }
  435. int wp_probe(struct platform_device *pdev)
  436. {
  437. int ret = 0;
  438. int i;
  439. pr_debug("[MTK WP] watchpoint_probe\n");
  440. of_property_read_u32(pdev->dev.of_node, "num", &wp_tracer.nr_dbg);
  441. pr_debug("[MTK WP] get %d debug interface\n", wp_tracer.nr_dbg);
  442. wp_tracer.debug_regs =
  443. kmalloc(sizeof(void *) * (unsigned long)wp_tracer.nr_dbg, GFP_KERNEL);
  444. if (!wp_tracer.debug_regs) {
  445. pr_err("[MTK WP] Failed to allocate watchpoint register array\n");
  446. ret = -ENOMEM;
  447. goto out;
  448. }
  449. for (i = 0; i < wp_tracer.nr_dbg; i++) {
  450. wp_tracer.debug_regs[i] = of_iomap(pdev->dev.of_node, i);
  451. if (wp_tracer.debug_regs[i] == NULL)
  452. pr_debug("[MTK WP] debug_interface %d devicetree mapping failed\n", i);
  453. else
  454. pr_debug("[MTK WP] debug_interface %d @ vm:0x%p pm:0x%lx\n", i,
  455. wp_tracer.debug_regs[i],
  456. vmalloc_to_pfn((void *)wp_tracer.debug_regs[i]) << 12);
  457. }
  458. asm volatile ("mrs %0, ID_AA64DFR0_EL1":"=r" (wp_tracer.id_aaa64dfr0_el1));
  459. wp_tracer.wp_nr = ((wp_tracer.id_aaa64dfr0_el1 & (0xf << 20)) >> 20) + 1;
  460. wp_tracer.bp_nr = ((wp_tracer.id_aaa64dfr0_el1 & (0xf << 12)) >> 12) + 1;
  461. pr_debug("[MTK WP] Probe:Reset watchpoints");
  462. reset_watchpoint();
  463. out:
  464. return ret;
  465. }
  466. static const struct of_device_id dbg_of_ids[] = {
  467. {.compatible = "mediatek,mt6735-dbg_debug",},
  468. {}
  469. };
  470. static struct platform_driver wp_driver = {
  471. .probe = wp_probe,
  472. .driver = {
  473. .name = "wp",
  474. .bus = &platform_bus_type,
  475. .owner = THIS_MODULE,
  476. .of_match_table = dbg_of_ids,
  477. },
  478. };
  479. #define WATCHPOINT_TEST_SUIT
  480. #ifdef WATCHPOINT_TEST_SUIT
  481. static ssize_t wp_test_suit_show(struct device_driver *driver, char *buf)
  482. {
  483. return snprintf(buf, PAGE_SIZE, "==Watchpoint test==\n"
  484. "1. test watchpoints in online cpu\n"
  485. "2. verified all cpu's watchpoint after hotplug\n"
  486. "3. verified all cpu's watchpoing after suspend/resume. Not:you have to run test item 1 or 2 to init watchpoint first\n");
  487. }
  488. static ssize_t wp_test_suit_store(struct device_driver *driver, const char *buf, size_t count)
  489. {
  490. char *p = (char *)buf;
  491. unsigned long int num;
  492. ssize_t ret;
  493. ret = kstrtoul(p, 10, &num);
  494. if (ret == 0) {
  495. switch (num) {
  496. /* Test watchpoint Function */
  497. case 1:
  498. wp_test1();
  499. break;
  500. case 2:
  501. wp_test2();
  502. break;
  503. case 3:
  504. wp_test3();
  505. default:
  506. break;
  507. }
  508. }
  509. return count;
  510. }
  511. DRIVER_ATTR(wp_test_suit, 0664, wp_test_suit_show, wp_test_suit_store);
  512. #endif
  513. static int __init hw_watchpoint_init(void)
  514. {
  515. int err;
  516. #ifdef WATCHPOINT_TEST_SUIT
  517. int ret;
  518. #endif
  519. spin_lock_init(&wp_lock);
  520. err = platform_driver_register(&wp_driver);
  521. if (err) {
  522. pr_err("[MTK WP] watchpoint registration failed\n");
  523. return err;
  524. }
  525. #ifdef WATCHPOINT_TEST_SUIT
  526. ret = driver_create_file(&wp_driver.driver, &driver_attr_wp_test_suit);
  527. if (ret)
  528. pr_err("[MTK WP] Fail to create systracker_drv sysfs files");
  529. #endif
  530. hook_debug_fault_code(DBG_ESR_EVT_HWWP, watchpoint_handler, SIGTRAP, TRAP_HWBKPT,
  531. "hw-watchpoint handler");
  532. pr_debug("[MTK WP] watchpoint handler init.\n");
  533. return 0;
  534. }
  535. /*
  536. EXPORT_SYMBOL(add_hw_watchpoint);
  537. */
  538. arch_initcall(hw_watchpoint_init);