init.c 12 KB

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