1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2006 Mike Kravetz IBM Corporation
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Hypervisor Call Instrumentation
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/percpu.h>
10*4882a593Smuzhiyun #include <linux/debugfs.h>
11*4882a593Smuzhiyun #include <linux/seq_file.h>
12*4882a593Smuzhiyun #include <linux/cpumask.h>
13*4882a593Smuzhiyun #include <asm/hvcall.h>
14*4882a593Smuzhiyun #include <asm/firmware.h>
15*4882a593Smuzhiyun #include <asm/cputable.h>
16*4882a593Smuzhiyun #include <asm/trace.h>
17*4882a593Smuzhiyun #include <asm/machdep.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /* For hcall instrumentation. One structure per-hcall, per-CPU */
20*4882a593Smuzhiyun struct hcall_stats {
21*4882a593Smuzhiyun unsigned long num_calls; /* number of calls (on this CPU) */
22*4882a593Smuzhiyun unsigned long tb_total; /* total wall time (mftb) of calls. */
23*4882a593Smuzhiyun unsigned long purr_total; /* total cpu time (PURR) of calls. */
24*4882a593Smuzhiyun unsigned long tb_start;
25*4882a593Smuzhiyun unsigned long purr_start;
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun #define HCALL_STAT_ARRAY_SIZE ((MAX_HCALL_OPCODE >> 2) + 1)
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun DEFINE_PER_CPU(struct hcall_stats[HCALL_STAT_ARRAY_SIZE], hcall_stats);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * Routines for displaying the statistics in debugfs
33*4882a593Smuzhiyun */
hc_start(struct seq_file * m,loff_t * pos)34*4882a593Smuzhiyun static void *hc_start(struct seq_file *m, loff_t *pos)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun if ((int)*pos < (HCALL_STAT_ARRAY_SIZE-1))
37*4882a593Smuzhiyun return (void *)(unsigned long)(*pos + 1);
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun return NULL;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
hc_next(struct seq_file * m,void * p,loff_t * pos)42*4882a593Smuzhiyun static void *hc_next(struct seq_file *m, void *p, loff_t * pos)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun ++*pos;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun return hc_start(m, pos);
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
hc_stop(struct seq_file * m,void * p)49*4882a593Smuzhiyun static void hc_stop(struct seq_file *m, void *p)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
hc_show(struct seq_file * m,void * p)53*4882a593Smuzhiyun static int hc_show(struct seq_file *m, void *p)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun unsigned long h_num = (unsigned long)p;
56*4882a593Smuzhiyun struct hcall_stats *hs = m->private;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun if (hs[h_num].num_calls) {
59*4882a593Smuzhiyun if (cpu_has_feature(CPU_FTR_PURR))
60*4882a593Smuzhiyun seq_printf(m, "%lu %lu %lu %lu\n", h_num<<2,
61*4882a593Smuzhiyun hs[h_num].num_calls,
62*4882a593Smuzhiyun hs[h_num].tb_total,
63*4882a593Smuzhiyun hs[h_num].purr_total);
64*4882a593Smuzhiyun else
65*4882a593Smuzhiyun seq_printf(m, "%lu %lu %lu\n", h_num<<2,
66*4882a593Smuzhiyun hs[h_num].num_calls,
67*4882a593Smuzhiyun hs[h_num].tb_total);
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun return 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun static const struct seq_operations hcall_inst_sops = {
74*4882a593Smuzhiyun .start = hc_start,
75*4882a593Smuzhiyun .next = hc_next,
76*4882a593Smuzhiyun .stop = hc_stop,
77*4882a593Smuzhiyun .show = hc_show
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun DEFINE_SEQ_ATTRIBUTE(hcall_inst);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun #define HCALL_ROOT_DIR "hcall_inst"
83*4882a593Smuzhiyun #define CPU_NAME_BUF_SIZE 32
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun
probe_hcall_entry(void * ignored,unsigned long opcode,unsigned long * args)86*4882a593Smuzhiyun static void probe_hcall_entry(void *ignored, unsigned long opcode, unsigned long *args)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun struct hcall_stats *h;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (opcode > MAX_HCALL_OPCODE)
91*4882a593Smuzhiyun return;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun h = this_cpu_ptr(&hcall_stats[opcode / 4]);
94*4882a593Smuzhiyun h->tb_start = mftb();
95*4882a593Smuzhiyun h->purr_start = mfspr(SPRN_PURR);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
probe_hcall_exit(void * ignored,unsigned long opcode,long retval,unsigned long * retbuf)98*4882a593Smuzhiyun static void probe_hcall_exit(void *ignored, unsigned long opcode, long retval,
99*4882a593Smuzhiyun unsigned long *retbuf)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun struct hcall_stats *h;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun if (opcode > MAX_HCALL_OPCODE)
104*4882a593Smuzhiyun return;
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun h = this_cpu_ptr(&hcall_stats[opcode / 4]);
107*4882a593Smuzhiyun h->num_calls++;
108*4882a593Smuzhiyun h->tb_total += mftb() - h->tb_start;
109*4882a593Smuzhiyun h->purr_total += mfspr(SPRN_PURR) - h->purr_start;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
hcall_inst_init(void)112*4882a593Smuzhiyun static int __init hcall_inst_init(void)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun struct dentry *hcall_root;
115*4882a593Smuzhiyun char cpu_name_buf[CPU_NAME_BUF_SIZE];
116*4882a593Smuzhiyun int cpu;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun if (!firmware_has_feature(FW_FEATURE_LPAR))
119*4882a593Smuzhiyun return 0;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (register_trace_hcall_entry(probe_hcall_entry, NULL))
122*4882a593Smuzhiyun return -EINVAL;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (register_trace_hcall_exit(probe_hcall_exit, NULL)) {
125*4882a593Smuzhiyun unregister_trace_hcall_entry(probe_hcall_entry, NULL);
126*4882a593Smuzhiyun return -EINVAL;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun hcall_root = debugfs_create_dir(HCALL_ROOT_DIR, NULL);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun for_each_possible_cpu(cpu) {
132*4882a593Smuzhiyun snprintf(cpu_name_buf, CPU_NAME_BUF_SIZE, "cpu%d", cpu);
133*4882a593Smuzhiyun debugfs_create_file(cpu_name_buf, 0444, hcall_root,
134*4882a593Smuzhiyun per_cpu(hcall_stats, cpu),
135*4882a593Smuzhiyun &hcall_inst_fops);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun machine_device_initcall(pseries, hcall_inst_init);
141