perf_event.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 & PSW_ADDR_INSN;
  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. /* See also arch/s390/kernel/traps.c */
  189. static unsigned long __store_trace(struct perf_callchain_entry *entry,
  190. unsigned long sp,
  191. unsigned long low, unsigned long high)
  192. {
  193. struct stack_frame *sf;
  194. struct pt_regs *regs;
  195. while (1) {
  196. sp = sp & PSW_ADDR_INSN;
  197. if (sp < low || sp > high - sizeof(*sf))
  198. return sp;
  199. sf = (struct stack_frame *) sp;
  200. perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
  201. /* Follow the backchain. */
  202. while (1) {
  203. low = sp;
  204. sp = sf->back_chain & PSW_ADDR_INSN;
  205. if (!sp)
  206. break;
  207. if (sp <= low || sp > high - sizeof(*sf))
  208. return sp;
  209. sf = (struct stack_frame *) sp;
  210. perf_callchain_store(entry,
  211. sf->gprs[8] & PSW_ADDR_INSN);
  212. }
  213. /* Zero backchain detected, check for interrupt frame. */
  214. sp = (unsigned long) (sf + 1);
  215. if (sp <= low || sp > high - sizeof(*regs))
  216. return sp;
  217. regs = (struct pt_regs *) sp;
  218. perf_callchain_store(entry, sf->gprs[8] & PSW_ADDR_INSN);
  219. low = sp;
  220. sp = regs->gprs[15];
  221. }
  222. }
  223. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  224. struct pt_regs *regs)
  225. {
  226. unsigned long head;
  227. struct stack_frame *head_sf;
  228. if (user_mode(regs))
  229. return;
  230. head = regs->gprs[15];
  231. head_sf = (struct stack_frame *) head;
  232. if (!head_sf || !head_sf->back_chain)
  233. return;
  234. head = head_sf->back_chain;
  235. head = __store_trace(entry, head, S390_lowcore.async_stack - ASYNC_SIZE,
  236. S390_lowcore.async_stack);
  237. __store_trace(entry, head, S390_lowcore.thread_info,
  238. S390_lowcore.thread_info + THREAD_SIZE);
  239. }
  240. /* Perf defintions for PMU event attributes in sysfs */
  241. ssize_t cpumf_events_sysfs_show(struct device *dev,
  242. struct device_attribute *attr, char *page)
  243. {
  244. struct perf_pmu_events_attr *pmu_attr;
  245. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  246. return sprintf(page, "event=0x%04llx,name=%s\n",
  247. pmu_attr->id, attr->attr.name);
  248. }
  249. /* Reserve/release functions for sharing perf hardware */
  250. static DEFINE_SPINLOCK(perf_hw_owner_lock);
  251. static void *perf_sampling_owner;
  252. int perf_reserve_sampling(void)
  253. {
  254. int err;
  255. err = 0;
  256. spin_lock(&perf_hw_owner_lock);
  257. if (perf_sampling_owner) {
  258. pr_warn("The sampling facility is already reserved by %p\n",
  259. perf_sampling_owner);
  260. err = -EBUSY;
  261. } else
  262. perf_sampling_owner = __builtin_return_address(0);
  263. spin_unlock(&perf_hw_owner_lock);
  264. return err;
  265. }
  266. EXPORT_SYMBOL(perf_reserve_sampling);
  267. void perf_release_sampling(void)
  268. {
  269. spin_lock(&perf_hw_owner_lock);
  270. WARN_ON(!perf_sampling_owner);
  271. perf_sampling_owner = NULL;
  272. spin_unlock(&perf_hw_owner_lock);
  273. }
  274. EXPORT_SYMBOL(perf_release_sampling);