init.c 4.3 KB

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