perf_event.c 5.9 KB

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