common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <asm/semaphore.h>
  13. #include <linux/sysdev.h>
  14. #include "op_counter.h"
  15. #include "op_arm_model.h"
  16. static struct op_arm_model_spec *pmu_model;
  17. static int pmu_enabled;
  18. static struct semaphore pmu_sem;
  19. static int pmu_start(void);
  20. static int pmu_setup(void);
  21. static void pmu_stop(void);
  22. static int pmu_create_files(struct super_block *, struct dentry *);
  23. #ifdef CONFIG_PM
  24. static int pmu_suspend(struct sys_device *dev, pm_message_t state)
  25. {
  26. if (pmu_enabled)
  27. pmu_stop();
  28. return 0;
  29. }
  30. static int pmu_resume(struct sys_device *dev)
  31. {
  32. if (pmu_enabled)
  33. pmu_start();
  34. return 0;
  35. }
  36. static struct sysdev_class oprofile_sysclass = {
  37. set_kset_name("oprofile"),
  38. .resume = pmu_resume,
  39. .suspend = pmu_suspend,
  40. };
  41. static struct sys_device device_oprofile = {
  42. .id = 0,
  43. .cls = &oprofile_sysclass,
  44. };
  45. static int __init init_driverfs(void)
  46. {
  47. int ret;
  48. if (!(ret = sysdev_class_register(&oprofile_sysclass)))
  49. ret = sysdev_register(&device_oprofile);
  50. return ret;
  51. }
  52. static void exit_driverfs(void)
  53. {
  54. sysdev_unregister(&device_oprofile);
  55. sysdev_class_unregister(&oprofile_sysclass);
  56. }
  57. #else
  58. #define init_driverfs() do { } while (0)
  59. #define exit_driverfs() do { } while (0)
  60. #endif /* CONFIG_PM */
  61. struct op_counter_config counter_config[OP_MAX_COUNTER];
  62. static int pmu_create_files(struct super_block *sb, struct dentry *root)
  63. {
  64. unsigned int i;
  65. for (i = 0; i < pmu_model->num_counters; i++) {
  66. struct dentry *dir;
  67. char buf[2];
  68. snprintf(buf, sizeof buf, "%d", i);
  69. dir = oprofilefs_mkdir(sb, root, buf);
  70. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  71. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  72. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  73. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  74. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  75. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  76. }
  77. return 0;
  78. }
  79. static int pmu_setup(void)
  80. {
  81. int ret;
  82. spin_lock(&oprofilefs_lock);
  83. ret = pmu_model->setup_ctrs();
  84. spin_unlock(&oprofilefs_lock);
  85. return ret;
  86. }
  87. static int pmu_start(void)
  88. {
  89. int ret = -EBUSY;
  90. down(&pmu_sem);
  91. if (!pmu_enabled) {
  92. ret = pmu_model->start();
  93. pmu_enabled = !ret;
  94. }
  95. up(&pmu_sem);
  96. return ret;
  97. }
  98. static void pmu_stop(void)
  99. {
  100. down(&pmu_sem);
  101. if (pmu_enabled)
  102. pmu_model->stop();
  103. pmu_enabled = 0;
  104. up(&pmu_sem);
  105. }
  106. int __init pmu_init(struct oprofile_operations *ops, struct op_arm_model_spec *spec)
  107. {
  108. init_MUTEX(&pmu_sem);
  109. if (spec->init() < 0)
  110. return -ENODEV;
  111. pmu_model = spec;
  112. init_driverfs();
  113. ops->create_files = pmu_create_files;
  114. ops->setup = pmu_setup;
  115. ops->shutdown = pmu_stop;
  116. ops->start = pmu_start;
  117. ops->stop = pmu_stop;
  118. ops->cpu_type = pmu_model->name;
  119. printk(KERN_INFO "oprofile: using %s PMU\n", spec->name);
  120. return 0;
  121. }
  122. void pmu_exit(void)
  123. {
  124. if (pmu_model) {
  125. exit_driverfs();
  126. pmu_model = NULL;
  127. }
  128. }