perf_event.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Performance event support for s390x
  3. *
  4. * Copyright IBM Corp. 2012, 2013
  5. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (version 2 only)
  9. * as published by the Free Software Foundation.
  10. */
  11. #define KMSG_COMPONENT "perf"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/perf_event.h>
  15. #include <linux/kvm_host.h>
  16. #include <linux/percpu.h>
  17. #include <linux/export.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/sysfs.h>
  21. #include <asm/irq.h>
  22. #include <asm/cpu_mf.h>
  23. #include <asm/lowcore.h>
  24. #include <asm/processor.h>
  25. #include <asm/sysinfo.h>
  26. const char *perf_pmu_name(void)
  27. {
  28. if (cpum_cf_avail() || cpum_sf_avail())
  29. return "CPU-Measurement Facilities (CPU-MF)";
  30. return "pmu";
  31. }
  32. EXPORT_SYMBOL(perf_pmu_name);
  33. int perf_num_counters(void)
  34. {
  35. int num = 0;
  36. if (cpum_cf_avail())
  37. num += PERF_CPUM_CF_MAX_CTR;
  38. if (cpum_sf_avail())
  39. num += PERF_CPUM_SF_MAX_CTR;
  40. return num;
  41. }
  42. EXPORT_SYMBOL(perf_num_counters);
  43. static struct kvm_s390_sie_block *sie_block(struct pt_regs *regs)
  44. {
  45. struct stack_frame *stack = (struct stack_frame *) regs->gprs[15];
  46. if (!stack)
  47. return NULL;
  48. return (struct kvm_s390_sie_block *) stack->empty1[0];
  49. }
  50. static bool is_in_guest(struct pt_regs *regs)
  51. {
  52. if (user_mode(regs))
  53. return false;
  54. #if IS_ENABLED(CONFIG_KVM)
  55. return instruction_pointer(regs) == (unsigned long) &sie_exit;
  56. #else
  57. return false;
  58. #endif
  59. }
  60. static unsigned long guest_is_user_mode(struct pt_regs *regs)
  61. {
  62. return sie_block(regs)->gpsw.mask & PSW_MASK_PSTATE;
  63. }
  64. static unsigned long instruction_pointer_guest(struct pt_regs *regs)
  65. {
  66. return sie_block(regs)->gpsw.addr;
  67. }
  68. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  69. {
  70. return is_in_guest(regs) ? instruction_pointer_guest(regs)
  71. : instruction_pointer(regs);
  72. }
  73. static unsigned long perf_misc_guest_flags(struct pt_regs *regs)
  74. {
  75. return guest_is_user_mode(regs) ? PERF_RECORD_MISC_GUEST_USER
  76. : PERF_RECORD_MISC_GUEST_KERNEL;
  77. }
  78. static unsigned long perf_misc_flags_sf(struct pt_regs *regs)
  79. {
  80. struct perf_sf_sde_regs *sde_regs;
  81. unsigned long flags;
  82. sde_regs = (struct perf_sf_sde_regs *) &regs->int_parm_long;
  83. if (sde_regs->in_guest)
  84. flags = user_mode(regs) ? PERF_RECORD_MISC_GUEST_USER
  85. : PERF_RECORD_MISC_GUEST_KERNEL;
  86. else
  87. flags = user_mode(regs) ? PERF_RECORD_MISC_USER
  88. : PERF_RECORD_MISC_KERNEL;
  89. return flags;
  90. }
  91. unsigned long perf_misc_flags(struct pt_regs *regs)
  92. {
  93. /* Check if the cpum_sf PMU has created the pt_regs structure.
  94. * In this case, perf misc flags can be easily extracted. Otherwise,
  95. * do regular checks on the pt_regs content.
  96. */
  97. if (regs->int_code == 0x1407 && regs->int_parm == CPU_MF_INT_SF_PRA)
  98. if (!regs->gprs[15])
  99. return perf_misc_flags_sf(regs);
  100. if (is_in_guest(regs))
  101. return perf_misc_guest_flags(regs);
  102. return user_mode(regs) ? PERF_RECORD_MISC_USER
  103. : PERF_RECORD_MISC_KERNEL;
  104. }
  105. static void print_debug_cf(void)
  106. {
  107. struct cpumf_ctr_info cf_info;
  108. int cpu = smp_processor_id();
  109. memset(&cf_info, 0, sizeof(cf_info));
  110. if (!qctri(&cf_info))
  111. pr_info("CPU[%i] CPUM_CF: ver=%u.%u A=%04x E=%04x C=%04x\n",
  112. cpu, cf_info.cfvn, cf_info.csvn,
  113. cf_info.auth_ctl, cf_info.enable_ctl, cf_info.act_ctl);
  114. }
  115. static void print_debug_sf(void)
  116. {
  117. struct hws_qsi_info_block si;
  118. int cpu = smp_processor_id();
  119. memset(&si, 0, sizeof(si));
  120. if (qsi(&si))
  121. return;
  122. pr_info("CPU[%i] CPUM_SF: basic=%i diag=%i min=%lu max=%lu cpu_speed=%u\n",
  123. cpu, si.as, si.ad, si.min_sampl_rate, si.max_sampl_rate,
  124. si.cpu_speed);
  125. if (si.as)
  126. pr_info("CPU[%i] CPUM_SF: Basic-sampling: a=%i e=%i c=%i"
  127. " bsdes=%i tear=%016lx dear=%016lx\n", cpu,
  128. si.as, si.es, si.cs, si.bsdes, si.tear, si.dear);
  129. if (si.ad)
  130. pr_info("CPU[%i] CPUM_SF: Diagnostic-sampling: a=%i e=%i c=%i"
  131. " dsdes=%i tear=%016lx dear=%016lx\n", cpu,
  132. si.ad, si.ed, si.cd, si.dsdes, si.tear, si.dear);
  133. }
  134. void perf_event_print_debug(void)
  135. {
  136. unsigned long flags;
  137. local_irq_save(flags);
  138. if (cpum_cf_avail())
  139. print_debug_cf();
  140. if (cpum_sf_avail())
  141. print_debug_sf();
  142. local_irq_restore(flags);
  143. }
  144. /* Service level infrastructure */
  145. static void sl_print_counter(struct seq_file *m)
  146. {
  147. struct cpumf_ctr_info ci;
  148. memset(&ci, 0, sizeof(ci));
  149. if (qctri(&ci))
  150. return;
  151. seq_printf(m, "CPU-MF: Counter facility: version=%u.%u "
  152. "authorization=%04x\n", ci.cfvn, ci.csvn, ci.auth_ctl);
  153. }
  154. static void sl_print_sampling(struct seq_file *m)
  155. {
  156. struct hws_qsi_info_block si;
  157. memset(&si, 0, sizeof(si));
  158. if (qsi(&si))
  159. return;
  160. if (!si.as && !si.ad)
  161. return;
  162. seq_printf(m, "CPU-MF: Sampling facility: min_rate=%lu max_rate=%lu"
  163. " cpu_speed=%u\n", si.min_sampl_rate, si.max_sampl_rate,
  164. si.cpu_speed);
  165. if (si.as)
  166. seq_printf(m, "CPU-MF: Sampling facility: mode=basic"
  167. " sample_size=%u\n", si.bsdes);
  168. if (si.ad)
  169. seq_printf(m, "CPU-MF: Sampling facility: mode=diagnostic"
  170. " sample_size=%u\n", si.dsdes);
  171. }
  172. static void service_level_perf_print(struct seq_file *m,
  173. struct service_level *sl)
  174. {
  175. if (cpum_cf_avail())
  176. sl_print_counter(m);
  177. if (cpum_sf_avail())
  178. sl_print_sampling(m);
  179. }
  180. static struct service_level service_level_perf = {
  181. .seq_print = service_level_perf_print,
  182. };
  183. static int __init service_level_perf_register(void)
  184. {
  185. return register_service_level(&service_level_perf);
  186. }
  187. arch_initcall(service_level_perf_register);
  188. static int __perf_callchain_kernel(void *data, unsigned long address, int reliable)
  189. {
  190. struct perf_callchain_entry_ctx *entry = data;
  191. perf_callchain_store(entry, address);
  192. return 0;
  193. }
  194. void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  195. struct pt_regs *regs)
  196. {
  197. if (user_mode(regs))
  198. return;
  199. dump_trace(__perf_callchain_kernel, entry, NULL, regs->gprs[15]);
  200. }
  201. /* Perf definitions for PMU event attributes in sysfs */
  202. ssize_t cpumf_events_sysfs_show(struct device *dev,
  203. struct device_attribute *attr, char *page)
  204. {
  205. struct perf_pmu_events_attr *pmu_attr;
  206. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  207. return sprintf(page, "event=0x%04llx,name=%s\n",
  208. pmu_attr->id, attr->attr.name);
  209. }