init.c 12 KB

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