common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @file common.c
  3. *
  4. * @remark Copyright 2004 Oprofile Authors
  5. * @remark Read the file COPYING
  6. *
  7. * @author Zwane Mwaikambo
  8. */
  9. #include <linux/init.h>
  10. #include <linux/oprofile.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/sysdev.h>
  14. #include <linux/mutex.h>
  15. #include "op_counter.h"
  16. #include "op_arm_model.h"
  17. static struct op_arm_model_spec *op_arm_model;
  18. static int op_arm_enabled;
  19. static DEFINE_MUTEX(op_arm_mutex);
  20. struct op_counter_config *counter_config;
  21. static int op_arm_create_files(struct super_block *sb, struct dentry *root)
  22. {
  23. unsigned int i;
  24. for (i = 0; i < op_arm_model->num_counters; i++) {
  25. struct dentry *dir;
  26. char buf[4];
  27. snprintf(buf, sizeof buf, "%d", i);
  28. dir = oprofilefs_mkdir(sb, root, buf);
  29. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  30. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  31. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  32. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  33. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  34. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  35. }
  36. return 0;
  37. }
  38. static int op_arm_setup(void)
  39. {
  40. int ret;
  41. spin_lock(&oprofilefs_lock);
  42. ret = op_arm_model->setup_ctrs();
  43. spin_unlock(&oprofilefs_lock);
  44. return ret;
  45. }
  46. static int op_arm_start(void)
  47. {
  48. int ret = -EBUSY;
  49. mutex_lock(&op_arm_mutex);
  50. if (!op_arm_enabled) {
  51. ret = op_arm_model->start();
  52. op_arm_enabled = !ret;
  53. }
  54. mutex_unlock(&op_arm_mutex);
  55. return ret;
  56. }
  57. static void op_arm_stop(void)
  58. {
  59. mutex_lock(&op_arm_mutex);
  60. if (op_arm_enabled)
  61. op_arm_model->stop();
  62. op_arm_enabled = 0;
  63. mutex_unlock(&op_arm_mutex);
  64. }
  65. #ifdef CONFIG_PM
  66. static int op_arm_suspend(struct sys_device *dev, pm_message_t state)
  67. {
  68. mutex_lock(&op_arm_mutex);
  69. if (op_arm_enabled)
  70. op_arm_model->stop();
  71. mutex_unlock(&op_arm_mutex);
  72. return 0;
  73. }
  74. static int op_arm_resume(struct sys_device *dev)
  75. {
  76. mutex_lock(&op_arm_mutex);
  77. if (op_arm_enabled && op_arm_model->start())
  78. op_arm_enabled = 0;
  79. mutex_unlock(&op_arm_mutex);
  80. return 0;
  81. }
  82. static struct sysdev_class oprofile_sysclass = {
  83. set_kset_name("oprofile"),
  84. .resume = op_arm_resume,
  85. .suspend = op_arm_suspend,
  86. };
  87. static struct sys_device device_oprofile = {
  88. .id = 0,
  89. .cls = &oprofile_sysclass,
  90. };
  91. static int __init init_driverfs(void)
  92. {
  93. int ret;
  94. if (!(ret = sysdev_class_register(&oprofile_sysclass)))
  95. ret = sysdev_register(&device_oprofile);
  96. return ret;
  97. }
  98. static void exit_driverfs(void)
  99. {
  100. sysdev_unregister(&device_oprofile);
  101. sysdev_class_unregister(&oprofile_sysclass);
  102. }
  103. #else
  104. #define init_driverfs() do { } while (0)
  105. #define exit_driverfs() do { } while (0)
  106. #endif /* CONFIG_PM */
  107. int __init oprofile_arch_init(struct oprofile_operations *ops)
  108. {
  109. struct op_arm_model_spec *spec = NULL;
  110. int ret = -ENODEV;
  111. #ifdef CONFIG_CPU_XSCALE
  112. spec = &op_xscale_spec;
  113. #endif
  114. if (spec) {
  115. ret = spec->init();
  116. if (ret < 0)
  117. return ret;
  118. counter_config = kcalloc(spec->num_counters, sizeof(struct op_counter_config),
  119. GFP_KERNEL);
  120. if (!counter_config)
  121. return -ENOMEM;
  122. op_arm_model = spec;
  123. init_driverfs();
  124. ops->create_files = op_arm_create_files;
  125. ops->setup = op_arm_setup;
  126. ops->shutdown = op_arm_stop;
  127. ops->start = op_arm_start;
  128. ops->stop = op_arm_stop;
  129. ops->cpu_type = op_arm_model->name;
  130. ops->backtrace = arm_backtrace;
  131. printk(KERN_INFO "oprofile: using %s\n", spec->name);
  132. }
  133. return ret;
  134. }
  135. void oprofile_arch_exit(void)
  136. {
  137. if (op_arm_model) {
  138. exit_driverfs();
  139. op_arm_model = NULL;
  140. }
  141. kfree(counter_config);
  142. }