init.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. extern void s390_backtrace(struct pt_regs * const regs, unsigned int depth);
  20. #ifdef CONFIG_64BIT
  21. #include "hwsampler.h"
  22. #define DEFAULT_INTERVAL 4127518
  23. #define DEFAULT_SDBT_BLOCKS 1
  24. #define DEFAULT_SDB_BLOCKS 511
  25. static unsigned long oprofile_hw_interval = DEFAULT_INTERVAL;
  26. static unsigned long oprofile_min_interval;
  27. static unsigned long oprofile_max_interval;
  28. static unsigned long oprofile_sdbt_blocks = DEFAULT_SDBT_BLOCKS;
  29. static unsigned long oprofile_sdb_blocks = DEFAULT_SDB_BLOCKS;
  30. static int hwsampler_file;
  31. static int hwsampler_running; /* start_mutex must be held to change */
  32. static struct oprofile_operations timer_ops;
  33. static int oprofile_hwsampler_start(void)
  34. {
  35. int retval;
  36. hwsampler_running = hwsampler_file;
  37. if (!hwsampler_running)
  38. return timer_ops.start();
  39. retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
  40. if (retval)
  41. return retval;
  42. retval = hwsampler_start_all(oprofile_hw_interval);
  43. if (retval)
  44. hwsampler_deallocate();
  45. return retval;
  46. }
  47. static void oprofile_hwsampler_stop(void)
  48. {
  49. if (!hwsampler_running) {
  50. timer_ops.stop();
  51. return;
  52. }
  53. hwsampler_stop_all();
  54. hwsampler_deallocate();
  55. return;
  56. }
  57. static ssize_t hwsampler_read(struct file *file, char __user *buf,
  58. size_t count, loff_t *offset)
  59. {
  60. return oprofilefs_ulong_to_user(hwsampler_file, buf, count, offset);
  61. }
  62. static ssize_t hwsampler_write(struct file *file, char const __user *buf,
  63. size_t count, loff_t *offset)
  64. {
  65. unsigned long val;
  66. int retval;
  67. if (*offset)
  68. return -EINVAL;
  69. retval = oprofilefs_ulong_from_user(&val, buf, count);
  70. if (retval <= 0)
  71. return retval;
  72. if (oprofile_started)
  73. /*
  74. * save to do without locking as we set
  75. * hwsampler_running in start() when start_mutex is
  76. * held
  77. */
  78. return -EBUSY;
  79. hwsampler_file = val;
  80. return count;
  81. }
  82. static const struct file_operations hwsampler_fops = {
  83. .read = hwsampler_read,
  84. .write = hwsampler_write,
  85. };
  86. static int oprofile_create_hwsampling_files(struct super_block *sb,
  87. struct dentry *root)
  88. {
  89. struct dentry *hw_dir;
  90. /* reinitialize default values */
  91. hwsampler_file = 1;
  92. hw_dir = oprofilefs_mkdir(sb, root, "hwsampling");
  93. if (!hw_dir)
  94. return -EINVAL;
  95. oprofilefs_create_file(sb, hw_dir, "hwsampler", &hwsampler_fops);
  96. oprofilefs_create_ulong(sb, hw_dir, "hw_interval",
  97. &oprofile_hw_interval);
  98. oprofilefs_create_ro_ulong(sb, hw_dir, "hw_min_interval",
  99. &oprofile_min_interval);
  100. oprofilefs_create_ro_ulong(sb, hw_dir, "hw_max_interval",
  101. &oprofile_max_interval);
  102. oprofilefs_create_ulong(sb, hw_dir, "hw_sdbt_blocks",
  103. &oprofile_sdbt_blocks);
  104. return 0;
  105. }
  106. static int oprofile_hwsampler_init(struct oprofile_operations *ops)
  107. {
  108. if (hwsampler_setup())
  109. return -ENODEV;
  110. /*
  111. * create hwsampler files only if hwsampler_setup() succeeds.
  112. */
  113. oprofile_min_interval = hwsampler_query_min_interval();
  114. if (oprofile_min_interval == 0)
  115. return -ENODEV;
  116. oprofile_max_interval = hwsampler_query_max_interval();
  117. if (oprofile_max_interval == 0)
  118. return -ENODEV;
  119. /* The initial value should be sane */
  120. if (oprofile_hw_interval < oprofile_min_interval)
  121. oprofile_hw_interval = oprofile_min_interval;
  122. if (oprofile_hw_interval > oprofile_max_interval)
  123. oprofile_hw_interval = oprofile_max_interval;
  124. if (oprofile_timer_init(ops))
  125. return -ENODEV;
  126. printk(KERN_INFO "oprofile: using hardware sampling\n");
  127. memcpy(&timer_ops, ops, sizeof(timer_ops));
  128. ops->start = oprofile_hwsampler_start;
  129. ops->stop = oprofile_hwsampler_stop;
  130. ops->create_files = oprofile_create_hwsampling_files;
  131. return 0;
  132. }
  133. static void oprofile_hwsampler_exit(void)
  134. {
  135. oprofile_timer_exit();
  136. hwsampler_shutdown();
  137. }
  138. #endif /* CONFIG_64BIT */
  139. int __init oprofile_arch_init(struct oprofile_operations *ops)
  140. {
  141. ops->backtrace = s390_backtrace;
  142. #ifdef CONFIG_64BIT
  143. return oprofile_hwsampler_init(ops);
  144. #else
  145. return -ENODEV;
  146. #endif
  147. }
  148. void oprofile_arch_exit(void)
  149. {
  150. #ifdef CONFIG_64BIT
  151. oprofile_hwsampler_exit();
  152. #endif
  153. }