init.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * arch/s390/oprofile/init.c
  3. *
  4. * S390 Version
  5. * Copyright (C) 2003 IBM Deutschland Entwicklung GmbH, IBM Corporation
  6. * Author(s): Thomas Spatzier (tspat@de.ibm.com)
  7. * Author(s): Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
  8. * Author(s): Heinz Graalfs (graalfs@linux.vnet.ibm.com)
  9. *
  10. * @remark Copyright 2002-2011 OProfile authors
  11. */
  12. #include <linux/oprofile.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/oprofile.h>
  16. #include <linux/errno.h>
  17. #include <linux/fs.h>
  18. #include "../../../drivers/oprofile/oprof.h"
  19. #include "hwsampler.h"
  20. #define DEFAULT_INTERVAL 4096
  21. #define DEFAULT_SDBT_BLOCKS 1
  22. #define DEFAULT_SDB_BLOCKS 511
  23. static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
  24. static unsigned long oprofile_min_interval;
  25. static unsigned long oprofile_max_interval;
  26. static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
  27. static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
  28. static int hwsampler_file;
  29. static int hwsampler_running; /* start_mutex must be held to change */
  30. static struct oprofile_operations timer_ops;
  31. extern void s390_backtrace(struct pt_regs * const regs, unsigned int depth);
  32. static int oprofile_hwsampler_start(void)
  33. {
  34. int retval;
  35. hwsampler_running = hwsampler_file;
  36. if (!hwsampler_running)
  37. return timer_ops.start();
  38. retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
  39. if (retval)
  40. return retval;
  41. retval = hwsampler_start_all(oprofile_hw_interval);
  42. if (retval)
  43. hwsampler_deallocate();
  44. return retval;
  45. }
  46. static void oprofile_hwsampler_stop(void)
  47. {
  48. if (!hwsampler_running) {
  49. timer_ops.stop();
  50. return;
  51. }
  52. hwsampler_stop_all();
  53. hwsampler_deallocate();
  54. return;
  55. }
  56. static ssize_t hwsampler_read(struct file *file, char __user *buf,
  57. size_t count, loff_t *offset)
  58. {
  59. return oprofilefs_ulong_to_user(hwsampler_file, buf, count, offset);
  60. }
  61. static ssize_t hwsampler_write(struct file *file, char const __user *buf,
  62. size_t count, loff_t *offset)
  63. {
  64. unsigned long val;
  65. int retval;
  66. if (*offset)
  67. return -EINVAL;
  68. retval = oprofilefs_ulong_from_user(&val, buf, count);
  69. if (retval)
  70. return retval;
  71. if (oprofile_started)
  72. /*
  73. * save to do without locking as we set
  74. * hwsampler_running in start() when start_mutex is
  75. * held
  76. */
  77. return -EBUSY;
  78. hwsampler_file = val;
  79. return count;
  80. }
  81. static const struct file_operations hwsampler_fops = {
  82. .read = hwsampler_read,
  83. .write = hwsampler_write,
  84. };
  85. static int oprofile_create_hwsampling_files(struct super_block *sb,
  86. struct dentry *root)
  87. {
  88. struct dentry *hw_dir;
  89. /* reinitialize default values */
  90. hwsampler_file = 1;
  91. hw_dir = oprofilefs_mkdir(sb, root, "hwsampling");
  92. if (!hw_dir)
  93. return -EINVAL;
  94. oprofilefs_create_file(sb, hw_dir, "hwsampler", &hwsampler_fops);
  95. oprofilefs_create_ulong(sb, hw_dir, "hw_interval",
  96. &oprofile_hw_interval);
  97. oprofilefs_create_ro_ulong(sb, hw_dir, "hw_min_interval",
  98. &oprofile_min_interval);
  99. oprofilefs_create_ro_ulong(sb, hw_dir, "hw_max_interval",
  100. &oprofile_max_interval);
  101. oprofilefs_create_ulong(sb, hw_dir, "hw_sdbt_blocks",
  102. &oprofile_sdbt_blocks);
  103. return 0;
  104. }
  105. static int oprofile_hwsampler_init(struct oprofile_operations *ops)
  106. {
  107. if (hwsampler_setup())
  108. return -ENODEV;
  109. /*
  110. * create hwsampler files only if hwsampler_setup() succeeds.
  111. */
  112. oprofile_min_interval = hwsampler_query_min_interval();
  113. if (oprofile_min_interval < 0) {
  114. oprofile_min_interval = 0;
  115. return -ENODEV;
  116. }
  117. oprofile_max_interval = hwsampler_query_max_interval();
  118. if (oprofile_max_interval < 0) {
  119. oprofile_max_interval = 0;
  120. return -ENODEV;
  121. }
  122. if (oprofile_timer_init(ops))
  123. return -ENODEV;
  124. printk(KERN_INFO "oprofile: using hardware sampling\n");
  125. memcpy(&timer_ops, ops, sizeof(timer_ops));
  126. ops->start = oprofile_hwsampler_start;
  127. ops->stop = oprofile_hwsampler_stop;
  128. ops->create_files = oprofile_create_hwsampling_files;
  129. return 0;
  130. }
  131. static void oprofile_hwsampler_exit(void)
  132. {
  133. oprofile_timer_exit();
  134. hwsampler_shutdown();
  135. }
  136. int __init oprofile_arch_init(struct oprofile_operations *ops)
  137. {
  138. ops->backtrace = s390_backtrace;
  139. return oprofile_hwsampler_init(ops);
  140. }
  141. void oprofile_arch_exit(void)
  142. {
  143. oprofile_hwsampler_exit();
  144. }