ocd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2007 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/sched.h>
  10. #include <linux/spinlock.h>
  11. #include <asm/ocd.h>
  12. static long ocd_count;
  13. static spinlock_t ocd_lock;
  14. /**
  15. * ocd_enable - enable on-chip debugging
  16. * @child: task to be debugged
  17. *
  18. * If @child is non-NULL, ocd_enable() first checks if debugging has
  19. * already been enabled for @child, and if it has, does nothing.
  20. *
  21. * If @child is NULL (e.g. when debugging the kernel), or debugging
  22. * has not already been enabled for it, ocd_enable() increments the
  23. * reference count and enables the debugging hardware.
  24. */
  25. void ocd_enable(struct task_struct *child)
  26. {
  27. u32 dc;
  28. if (child)
  29. pr_debug("ocd_enable: child=%s [%u]\n",
  30. child->comm, child->pid);
  31. else
  32. pr_debug("ocd_enable (no child)\n");
  33. if (!child || !test_and_set_tsk_thread_flag(child, TIF_DEBUG)) {
  34. spin_lock(&ocd_lock);
  35. ocd_count++;
  36. dc = ocd_read(DC);
  37. dc |= (1 << OCD_DC_MM_BIT) | (1 << OCD_DC_DBE_BIT);
  38. ocd_write(DC, dc);
  39. spin_unlock(&ocd_lock);
  40. }
  41. }
  42. /**
  43. * ocd_disable - disable on-chip debugging
  44. * @child: task that was being debugged, but isn't anymore
  45. *
  46. * If @child is non-NULL, ocd_disable() checks if debugging is enabled
  47. * for @child, and if it isn't, does nothing.
  48. *
  49. * If @child is NULL (e.g. when debugging the kernel), or debugging is
  50. * enabled, ocd_disable() decrements the reference count, and if it
  51. * reaches zero, disables the debugging hardware.
  52. */
  53. void ocd_disable(struct task_struct *child)
  54. {
  55. u32 dc;
  56. if (!child)
  57. pr_debug("ocd_disable (no child)\n");
  58. else if (test_tsk_thread_flag(child, TIF_DEBUG))
  59. pr_debug("ocd_disable: child=%s [%u]\n",
  60. child->comm, child->pid);
  61. if (!child || test_and_clear_tsk_thread_flag(child, TIF_DEBUG)) {
  62. spin_lock(&ocd_lock);
  63. ocd_count--;
  64. WARN_ON(ocd_count < 0);
  65. if (ocd_count <= 0) {
  66. dc = ocd_read(DC);
  67. dc &= ~((1 << OCD_DC_MM_BIT) | (1 << OCD_DC_DBE_BIT));
  68. ocd_write(DC, dc);
  69. }
  70. spin_unlock(&ocd_lock);
  71. }
  72. }
  73. #ifdef CONFIG_DEBUG_FS
  74. #include <linux/debugfs.h>
  75. #include <linux/module.h>
  76. static struct dentry *ocd_debugfs_root;
  77. static struct dentry *ocd_debugfs_DC;
  78. static struct dentry *ocd_debugfs_DS;
  79. static struct dentry *ocd_debugfs_count;
  80. static int ocd_DC_get(void *data, u64 *val)
  81. {
  82. *val = ocd_read(DC);
  83. return 0;
  84. }
  85. static int ocd_DC_set(void *data, u64 val)
  86. {
  87. ocd_write(DC, val);
  88. return 0;
  89. }
  90. DEFINE_SIMPLE_ATTRIBUTE(fops_DC, ocd_DC_get, ocd_DC_set, "0x%08llx\n");
  91. static int ocd_DS_get(void *data, u64 *val)
  92. {
  93. *val = ocd_read(DS);
  94. return 0;
  95. }
  96. DEFINE_SIMPLE_ATTRIBUTE(fops_DS, ocd_DS_get, NULL, "0x%08llx\n");
  97. static int ocd_count_get(void *data, u64 *val)
  98. {
  99. *val = ocd_count;
  100. return 0;
  101. }
  102. DEFINE_SIMPLE_ATTRIBUTE(fops_count, ocd_count_get, NULL, "%lld\n");
  103. static void ocd_debugfs_init(void)
  104. {
  105. struct dentry *root;
  106. root = debugfs_create_dir("ocd", NULL);
  107. if (IS_ERR(root) || !root)
  108. goto err_root;
  109. ocd_debugfs_root = root;
  110. ocd_debugfs_DC = debugfs_create_file("DC", S_IRUSR | S_IWUSR,
  111. root, NULL, &fops_DC);
  112. if (!ocd_debugfs_DC)
  113. goto err_DC;
  114. ocd_debugfs_DS = debugfs_create_file("DS", S_IRUSR, root,
  115. NULL, &fops_DS);
  116. if (!ocd_debugfs_DS)
  117. goto err_DS;
  118. ocd_debugfs_count = debugfs_create_file("count", S_IRUSR, root,
  119. NULL, &fops_count);
  120. if (!ocd_debugfs_count)
  121. goto err_count;
  122. return;
  123. err_count:
  124. debugfs_remove(ocd_debugfs_DS);
  125. err_DS:
  126. debugfs_remove(ocd_debugfs_DC);
  127. err_DC:
  128. debugfs_remove(ocd_debugfs_root);
  129. err_root:
  130. printk(KERN_WARNING "OCD: Failed to create debugfs entries\n");
  131. }
  132. #else
  133. static inline void ocd_debugfs_init(void)
  134. {
  135. }
  136. #endif
  137. static int __init ocd_init(void)
  138. {
  139. spin_lock_init(&ocd_lock);
  140. ocd_debugfs_init();
  141. return 0;
  142. }
  143. arch_initcall(ocd_init);