init.c 4.2 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/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 4096
  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)
  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. oprofile_min_interval = 0;
  116. return -ENODEV;
  117. }
  118. oprofile_max_interval = hwsampler_query_max_interval();
  119. if (oprofile_max_interval < 0) {
  120. oprofile_max_interval = 0;
  121. return -ENODEV;
  122. }
  123. if (oprofile_timer_init(ops))
  124. return -ENODEV;
  125. printk(KERN_INFO "oprofile: using hardware sampling\n");
  126. memcpy(&timer_ops, ops, sizeof(timer_ops));
  127. ops->start = oprofile_hwsampler_start;
  128. ops->stop = oprofile_hwsampler_stop;
  129. ops->create_files = oprofile_create_hwsampling_files;
  130. return 0;
  131. }
  132. static void oprofile_hwsampler_exit(void)
  133. {
  134. oprofile_timer_exit();
  135. hwsampler_shutdown();
  136. }
  137. #endif /* CONFIG_64BIT */
  138. int __init oprofile_arch_init(struct oprofile_operations *ops)
  139. {
  140. ops->backtrace = s390_backtrace;
  141. #ifdef CONFIG_64BIT
  142. return oprofile_hwsampler_init(ops);
  143. #else
  144. return -ENODEV;
  145. #endif
  146. }
  147. void oprofile_arch_exit(void)
  148. {
  149. #ifdef CONFIG_64BIT
  150. oprofile_hwsampler_exit();
  151. #endif
  152. }