init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * S390 Version
  3. * Copyright IBM Corp. 2002, 2011
  4. * Author(s): Thomas Spatzier (tspat@de.ibm.com)
  5. * Author(s): Mahesh Salgaonkar (mahesh@linux.vnet.ibm.com)
  6. * Author(s): Heinz Graalfs (graalfs@linux.vnet.ibm.com)
  7. * Author(s): Andreas Krebbel (krebbel@linux.vnet.ibm.com)
  8. *
  9. * @remark Copyright 2002-2011 OProfile authors
  10. */
  11. #include <linux/oprofile.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/fs.h>
  15. #include <linux/module.h>
  16. #include <asm/processor.h>
  17. #include "../../../drivers/oprofile/oprof.h"
  18. extern void s390_backtrace(struct pt_regs * const regs, unsigned int depth);
  19. #ifdef CONFIG_64BIT
  20. #include "hwsampler.h"
  21. #include "op_counter.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_enabled;
  31. static int hwsampler_running; /* start_mutex must be held to change */
  32. static int hwsampler_available;
  33. static struct oprofile_operations timer_ops;
  34. struct op_counter_config counter_config;
  35. enum __force_cpu_type {
  36. reserved = 0, /* do not force */
  37. timer,
  38. };
  39. static int force_cpu_type;
  40. static int set_cpu_type(const char *str, struct kernel_param *kp)
  41. {
  42. if (!strcmp(str, "timer")) {
  43. force_cpu_type = timer;
  44. printk(KERN_INFO "oprofile: forcing timer to be returned "
  45. "as cpu type\n");
  46. } else {
  47. force_cpu_type = 0;
  48. }
  49. return 0;
  50. }
  51. module_param_call(cpu_type, set_cpu_type, NULL, NULL, 0);
  52. MODULE_PARM_DESC(cpu_type, "Force legacy basic mode sampling"
  53. "(report cpu_type \"timer\"");
  54. static int oprofile_hwsampler_start(void)
  55. {
  56. int retval;
  57. hwsampler_running = hwsampler_enabled;
  58. if (!hwsampler_running)
  59. return timer_ops.start();
  60. retval = hwsampler_allocate(oprofile_sdbt_blocks, oprofile_sdb_blocks);
  61. if (retval)
  62. return retval;
  63. retval = hwsampler_start_all(oprofile_hw_interval);
  64. if (retval)
  65. hwsampler_deallocate();
  66. return retval;
  67. }
  68. static void oprofile_hwsampler_stop(void)
  69. {
  70. if (!hwsampler_running) {
  71. timer_ops.stop();
  72. return;
  73. }
  74. hwsampler_stop_all();
  75. hwsampler_deallocate();
  76. return;
  77. }
  78. /*
  79. * File ops used for:
  80. * /dev/oprofile/0/enabled
  81. * /dev/oprofile/hwsampling/hwsampler (cpu_type = timer)
  82. */
  83. static ssize_t hwsampler_read(struct file *file, char __user *buf,
  84. size_t count, loff_t *offset)
  85. {
  86. return oprofilefs_ulong_to_user(hwsampler_enabled, buf, count, offset);
  87. }
  88. static ssize_t hwsampler_write(struct file *file, char const __user *buf,
  89. size_t count, loff_t *offset)
  90. {
  91. unsigned long val;
  92. int retval;
  93. if (*offset)
  94. return -EINVAL;
  95. retval = oprofilefs_ulong_from_user(&val, buf, count);
  96. if (retval <= 0)
  97. return retval;
  98. if (val != 0 && val != 1)
  99. return -EINVAL;
  100. if (oprofile_started)
  101. /*
  102. * save to do without locking as we set
  103. * hwsampler_running in start() when start_mutex is
  104. * held
  105. */
  106. return -EBUSY;
  107. hwsampler_enabled = val;
  108. return count;
  109. }
  110. static const struct file_operations hwsampler_fops = {
  111. .read = hwsampler_read,
  112. .write = hwsampler_write,
  113. };
  114. /*
  115. * File ops used for:
  116. * /dev/oprofile/0/count
  117. * /dev/oprofile/hwsampling/hw_interval (cpu_type = timer)
  118. *
  119. * Make sure that the value is within the hardware range.
  120. */
  121. static ssize_t hw_interval_read(struct file *file, char __user *buf,
  122. size_t count, loff_t *offset)
  123. {
  124. return oprofilefs_ulong_to_user(oprofile_hw_interval, buf,
  125. count, offset);
  126. }
  127. static ssize_t hw_interval_write(struct file *file, char const __user *buf,
  128. size_t count, loff_t *offset)
  129. {
  130. unsigned long val;
  131. int retval;
  132. if (*offset)
  133. return -EINVAL;
  134. retval = oprofilefs_ulong_from_user(&val, buf, count);
  135. if (retval <= 0)
  136. return retval;
  137. if (val < oprofile_min_interval)
  138. oprofile_hw_interval = oprofile_min_interval;
  139. else if (val > oprofile_max_interval)
  140. oprofile_hw_interval = oprofile_max_interval;
  141. else
  142. oprofile_hw_interval = val;
  143. return count;
  144. }
  145. static const struct file_operations hw_interval_fops = {
  146. .read = hw_interval_read,
  147. .write = hw_interval_write,
  148. };
  149. /*
  150. * File ops used for:
  151. * /dev/oprofile/0/event
  152. * Only a single event with number 0 is supported with this counter.
  153. *
  154. * /dev/oprofile/0/unit_mask
  155. * This is a dummy file needed by the user space tools.
  156. * No value other than 0 is accepted or returned.
  157. */
  158. static ssize_t hwsampler_zero_read(struct file *file, char __user *buf,
  159. size_t count, loff_t *offset)
  160. {
  161. return oprofilefs_ulong_to_user(0, buf, count, offset);
  162. }
  163. static ssize_t hwsampler_zero_write(struct file *file, char const __user *buf,
  164. size_t count, loff_t *offset)
  165. {
  166. unsigned long val;
  167. int retval;
  168. if (*offset)
  169. return -EINVAL;
  170. retval = oprofilefs_ulong_from_user(&val, buf, count);
  171. if (retval <= 0)
  172. return retval;
  173. if (val != 0)
  174. return -EINVAL;
  175. return count;
  176. }
  177. static const struct file_operations zero_fops = {
  178. .read = hwsampler_zero_read,
  179. .write = hwsampler_zero_write,
  180. };
  181. /* /dev/oprofile/0/kernel file ops. */
  182. static ssize_t hwsampler_kernel_read(struct file *file, char __user *buf,
  183. size_t count, loff_t *offset)
  184. {
  185. return oprofilefs_ulong_to_user(counter_config.kernel,
  186. buf, count, offset);
  187. }
  188. static ssize_t hwsampler_kernel_write(struct file *file, char const __user *buf,
  189. size_t count, loff_t *offset)
  190. {
  191. unsigned long val;
  192. int retval;
  193. if (*offset)
  194. return -EINVAL;
  195. retval = oprofilefs_ulong_from_user(&val, buf, count);
  196. if (retval <= 0)
  197. return retval;
  198. if (val != 0 && val != 1)
  199. return -EINVAL;
  200. counter_config.kernel = val;
  201. return count;
  202. }
  203. static const struct file_operations kernel_fops = {
  204. .read = hwsampler_kernel_read,
  205. .write = hwsampler_kernel_write,
  206. };
  207. /* /dev/oprofile/0/user file ops. */
  208. static ssize_t hwsampler_user_read(struct file *file, char __user *buf,
  209. size_t count, loff_t *offset)
  210. {
  211. return oprofilefs_ulong_to_user(counter_config.user,
  212. buf, count, offset);
  213. }
  214. static ssize_t hwsampler_user_write(struct file *file, char const __user *buf,
  215. size_t count, loff_t *offset)
  216. {
  217. unsigned long val;
  218. int retval;
  219. if (*offset)
  220. return -EINVAL;
  221. retval = oprofilefs_ulong_from_user(&val, buf, count);
  222. if (retval <= 0)
  223. return retval;
  224. if (val != 0 && val != 1)
  225. return -EINVAL;
  226. counter_config.user = val;
  227. return count;
  228. }
  229. static const struct file_operations user_fops = {
  230. .read = hwsampler_user_read,
  231. .write = hwsampler_user_write,
  232. };
  233. /*
  234. * File ops used for: /dev/oprofile/timer/enabled
  235. * The value always has to be the inverted value of hwsampler_enabled. So
  236. * no separate variable is created. That way we do not need locking.
  237. */
  238. static ssize_t timer_enabled_read(struct file *file, char __user *buf,
  239. size_t count, loff_t *offset)
  240. {
  241. return oprofilefs_ulong_to_user(!hwsampler_enabled, buf, count, offset);
  242. }
  243. static ssize_t timer_enabled_write(struct file *file, char const __user *buf,
  244. size_t count, loff_t *offset)
  245. {
  246. unsigned long val;
  247. int retval;
  248. if (*offset)
  249. return -EINVAL;
  250. retval = oprofilefs_ulong_from_user(&val, buf, count);
  251. if (retval <= 0)
  252. return retval;
  253. if (val != 0 && val != 1)
  254. return -EINVAL;
  255. /* Timer cannot be disabled without having hardware sampling. */
  256. if (val == 0 && !hwsampler_available)
  257. return -EINVAL;
  258. if (oprofile_started)
  259. /*
  260. * save to do without locking as we set
  261. * hwsampler_running in start() when start_mutex is
  262. * held
  263. */
  264. return -EBUSY;
  265. hwsampler_enabled = !val;
  266. return count;
  267. }
  268. static const struct file_operations timer_enabled_fops = {
  269. .read = timer_enabled_read,
  270. .write = timer_enabled_write,
  271. };
  272. static int oprofile_create_hwsampling_files(struct super_block *sb,
  273. struct dentry *root)
  274. {
  275. struct dentry *dir;
  276. dir = oprofilefs_mkdir(sb, root, "timer");
  277. if (!dir)
  278. return -EINVAL;
  279. oprofilefs_create_file(sb, dir, "enabled", &timer_enabled_fops);
  280. if (!hwsampler_available)
  281. return 0;
  282. /* reinitialize default values */
  283. hwsampler_enabled = 1;
  284. counter_config.kernel = 1;
  285. counter_config.user = 1;
  286. if (!force_cpu_type) {
  287. /*
  288. * Create the counter file system. A single virtual
  289. * counter is created which can be used to
  290. * enable/disable hardware sampling dynamically from
  291. * user space. The user space will configure a single
  292. * counter with a single event. The value of 'event'
  293. * and 'unit_mask' are not evaluated by the kernel code
  294. * and can only be set to 0.
  295. */
  296. dir = oprofilefs_mkdir(sb, root, "0");
  297. if (!dir)
  298. return -EINVAL;
  299. oprofilefs_create_file(sb, dir, "enabled", &hwsampler_fops);
  300. oprofilefs_create_file(sb, dir, "event", &zero_fops);
  301. oprofilefs_create_file(sb, dir, "count", &hw_interval_fops);
  302. oprofilefs_create_file(sb, dir, "unit_mask", &zero_fops);
  303. oprofilefs_create_file(sb, dir, "kernel", &kernel_fops);
  304. oprofilefs_create_file(sb, dir, "user", &user_fops);
  305. oprofilefs_create_ulong(sb, dir, "hw_sdbt_blocks",
  306. &oprofile_sdbt_blocks);
  307. } else {
  308. /*
  309. * Hardware sampling can be used but the cpu_type is
  310. * forced to timer in order to deal with legacy user
  311. * space tools. The /dev/oprofile/hwsampling fs is
  312. * provided in that case.
  313. */
  314. dir = oprofilefs_mkdir(sb, root, "hwsampling");
  315. if (!dir)
  316. return -EINVAL;
  317. oprofilefs_create_file(sb, dir, "hwsampler",
  318. &hwsampler_fops);
  319. oprofilefs_create_file(sb, dir, "hw_interval",
  320. &hw_interval_fops);
  321. oprofilefs_create_ro_ulong(sb, dir, "hw_min_interval",
  322. &oprofile_min_interval);
  323. oprofilefs_create_ro_ulong(sb, dir, "hw_max_interval",
  324. &oprofile_max_interval);
  325. oprofilefs_create_ulong(sb, dir, "hw_sdbt_blocks",
  326. &oprofile_sdbt_blocks);
  327. }
  328. return 0;
  329. }
  330. static int oprofile_hwsampler_init(struct oprofile_operations *ops)
  331. {
  332. /*
  333. * Initialize the timer mode infrastructure as well in order
  334. * to be able to switch back dynamically. oprofile_timer_init
  335. * is not supposed to fail.
  336. */
  337. if (oprofile_timer_init(ops))
  338. BUG();
  339. memcpy(&timer_ops, ops, sizeof(timer_ops));
  340. ops->create_files = oprofile_create_hwsampling_files;
  341. /*
  342. * If the user space tools do not support newer cpu types,
  343. * the force_cpu_type module parameter
  344. * can be used to always return \"timer\" as cpu type.
  345. */
  346. if (force_cpu_type != timer) {
  347. struct cpuid id;
  348. get_cpu_id (&id);
  349. switch (id.machine) {
  350. case 0x2097: case 0x2098: ops->cpu_type = "s390/z10"; break;
  351. case 0x2817: case 0x2818: ops->cpu_type = "s390/z196"; break;
  352. default: return -ENODEV;
  353. }
  354. }
  355. if (hwsampler_setup())
  356. return -ENODEV;
  357. /*
  358. * Query the range for the sampling interval from the
  359. * hardware.
  360. */
  361. oprofile_min_interval = hwsampler_query_min_interval();
  362. if (oprofile_min_interval == 0)
  363. return -ENODEV;
  364. oprofile_max_interval = hwsampler_query_max_interval();
  365. if (oprofile_max_interval == 0)
  366. return -ENODEV;
  367. /* The initial value should be sane */
  368. if (oprofile_hw_interval < oprofile_min_interval)
  369. oprofile_hw_interval = oprofile_min_interval;
  370. if (oprofile_hw_interval > oprofile_max_interval)
  371. oprofile_hw_interval = oprofile_max_interval;
  372. printk(KERN_INFO "oprofile: System z hardware sampling "
  373. "facility found.\n");
  374. ops->start = oprofile_hwsampler_start;
  375. ops->stop = oprofile_hwsampler_stop;
  376. return 0;
  377. }
  378. static void oprofile_hwsampler_exit(void)
  379. {
  380. hwsampler_shutdown();
  381. }
  382. #endif /* CONFIG_64BIT */
  383. int __init oprofile_arch_init(struct oprofile_operations *ops)
  384. {
  385. ops->backtrace = s390_backtrace;
  386. #ifdef CONFIG_64BIT
  387. /*
  388. * -ENODEV is not reported to the caller. The module itself
  389. * will use the timer mode sampling as fallback and this is
  390. * always available.
  391. */
  392. hwsampler_available = oprofile_hwsampler_init(ops) == 0;
  393. return 0;
  394. #else
  395. return -ENODEV;
  396. #endif
  397. }
  398. void oprofile_arch_exit(void)
  399. {
  400. #ifdef CONFIG_64BIT
  401. oprofile_hwsampler_exit();
  402. #endif
  403. }