11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
257852a85SMike Kravetz /*
357852a85SMike Kravetz * Copyright (C) 2006 Mike Kravetz IBM Corporation
457852a85SMike Kravetz *
557852a85SMike Kravetz * Hypervisor Call Instrumentation
657852a85SMike Kravetz */
757852a85SMike Kravetz
857852a85SMike Kravetz #include <linux/kernel.h>
957852a85SMike Kravetz #include <linux/percpu.h>
1057852a85SMike Kravetz #include <linux/debugfs.h>
1157852a85SMike Kravetz #include <linux/seq_file.h>
1257852a85SMike Kravetz #include <linux/cpumask.h>
1357852a85SMike Kravetz #include <asm/hvcall.h>
1457852a85SMike Kravetz #include <asm/firmware.h>
1557852a85SMike Kravetz #include <asm/cputable.h>
16c8cd093aSAnton Blanchard #include <asm/trace.h>
178e83e905SMichael Ellerman #include <asm/machdep.h>
1857852a85SMike Kravetz
19017614a5STobin C. Harding /* For hcall instrumentation. One structure per-hcall, per-CPU */
20017614a5STobin C. Harding struct hcall_stats {
21017614a5STobin C. Harding unsigned long num_calls; /* number of calls (on this CPU) */
22017614a5STobin C. Harding unsigned long tb_total; /* total wall time (mftb) of calls. */
23017614a5STobin C. Harding unsigned long purr_total; /* total cpu time (PURR) of calls. */
24017614a5STobin C. Harding unsigned long tb_start;
25017614a5STobin C. Harding unsigned long purr_start;
26017614a5STobin C. Harding };
27017614a5STobin C. Harding #define HCALL_STAT_ARRAY_SIZE ((MAX_HCALL_OPCODE >> 2) + 1)
28017614a5STobin C. Harding
29*193e4cd8SBixuan Cui static DEFINE_PER_CPU(struct hcall_stats[HCALL_STAT_ARRAY_SIZE], hcall_stats);
3057852a85SMike Kravetz
3157852a85SMike Kravetz /*
3257852a85SMike Kravetz * Routines for displaying the statistics in debugfs
3357852a85SMike Kravetz */
hc_start(struct seq_file * m,loff_t * pos)3457852a85SMike Kravetz static void *hc_start(struct seq_file *m, loff_t *pos)
3557852a85SMike Kravetz {
36dc40127cSAnton Blanchard if ((int)*pos < (HCALL_STAT_ARRAY_SIZE-1))
3757852a85SMike Kravetz return (void *)(unsigned long)(*pos + 1);
3857852a85SMike Kravetz
3957852a85SMike Kravetz return NULL;
4057852a85SMike Kravetz }
4157852a85SMike Kravetz
hc_next(struct seq_file * m,void * p,loff_t * pos)4257852a85SMike Kravetz static void *hc_next(struct seq_file *m, void *p, loff_t * pos)
4357852a85SMike Kravetz {
4457852a85SMike Kravetz ++*pos;
4557852a85SMike Kravetz
4657852a85SMike Kravetz return hc_start(m, pos);
4757852a85SMike Kravetz }
4857852a85SMike Kravetz
hc_stop(struct seq_file * m,void * p)4957852a85SMike Kravetz static void hc_stop(struct seq_file *m, void *p)
5057852a85SMike Kravetz {
5157852a85SMike Kravetz }
5257852a85SMike Kravetz
hc_show(struct seq_file * m,void * p)5357852a85SMike Kravetz static int hc_show(struct seq_file *m, void *p)
5457852a85SMike Kravetz {
5557852a85SMike Kravetz unsigned long h_num = (unsigned long)p;
566d2ad1e3Smatt mooney struct hcall_stats *hs = m->private;
5757852a85SMike Kravetz
5857852a85SMike Kravetz if (hs[h_num].num_calls) {
59dc40127cSAnton Blanchard if (cpu_has_feature(CPU_FTR_PURR))
6057852a85SMike Kravetz seq_printf(m, "%lu %lu %lu %lu\n", h_num<<2,
6157852a85SMike Kravetz hs[h_num].num_calls,
6257852a85SMike Kravetz hs[h_num].tb_total,
6357852a85SMike Kravetz hs[h_num].purr_total);
6457852a85SMike Kravetz else
6557852a85SMike Kravetz seq_printf(m, "%lu %lu %lu\n", h_num<<2,
6657852a85SMike Kravetz hs[h_num].num_calls,
6757852a85SMike Kravetz hs[h_num].tb_total);
6857852a85SMike Kravetz }
6957852a85SMike Kravetz
7057852a85SMike Kravetz return 0;
7157852a85SMike Kravetz }
7257852a85SMike Kravetz
7396543e73SLiu Shixin static const struct seq_operations hcall_inst_sops = {
7457852a85SMike Kravetz .start = hc_start,
7557852a85SMike Kravetz .next = hc_next,
7657852a85SMike Kravetz .stop = hc_stop,
7757852a85SMike Kravetz .show = hc_show
7857852a85SMike Kravetz };
7957852a85SMike Kravetz
8096543e73SLiu Shixin DEFINE_SEQ_ATTRIBUTE(hcall_inst);
8157852a85SMike Kravetz
8257852a85SMike Kravetz #define HCALL_ROOT_DIR "hcall_inst"
8357852a85SMike Kravetz #define CPU_NAME_BUF_SIZE 32
8457852a85SMike Kravetz
85c8cd093aSAnton Blanchard
probe_hcall_entry(void * ignored,unsigned long opcode,unsigned long * args)86969ea5c5SStephen Rothwell static void probe_hcall_entry(void *ignored, unsigned long opcode, unsigned long *args)
87c8cd093aSAnton Blanchard {
88c8cd093aSAnton Blanchard struct hcall_stats *h;
89c8cd093aSAnton Blanchard
90c8cd093aSAnton Blanchard if (opcode > MAX_HCALL_OPCODE)
91c8cd093aSAnton Blanchard return;
92c8cd093aSAnton Blanchard
9369111bacSChristoph Lameter h = this_cpu_ptr(&hcall_stats[opcode / 4]);
94c8cd093aSAnton Blanchard h->tb_start = mftb();
95c8cd093aSAnton Blanchard h->purr_start = mfspr(SPRN_PURR);
96c8cd093aSAnton Blanchard }
97c8cd093aSAnton Blanchard
probe_hcall_exit(void * ignored,unsigned long opcode,long retval,unsigned long * retbuf)988f2133ccSMichael Ellerman static void probe_hcall_exit(void *ignored, unsigned long opcode, long retval,
996f26353cSAnton Blanchard unsigned long *retbuf)
100c8cd093aSAnton Blanchard {
101c8cd093aSAnton Blanchard struct hcall_stats *h;
102c8cd093aSAnton Blanchard
103c8cd093aSAnton Blanchard if (opcode > MAX_HCALL_OPCODE)
104c8cd093aSAnton Blanchard return;
105c8cd093aSAnton Blanchard
10669111bacSChristoph Lameter h = this_cpu_ptr(&hcall_stats[opcode / 4]);
107c8cd093aSAnton Blanchard h->num_calls++;
10825ef231dSWill Schmidt h->tb_total += mftb() - h->tb_start;
10925ef231dSWill Schmidt h->purr_total += mfspr(SPRN_PURR) - h->purr_start;
110c8cd093aSAnton Blanchard }
111c8cd093aSAnton Blanchard
hcall_inst_init(void)11257852a85SMike Kravetz static int __init hcall_inst_init(void)
11357852a85SMike Kravetz {
11457852a85SMike Kravetz struct dentry *hcall_root;
11557852a85SMike Kravetz char cpu_name_buf[CPU_NAME_BUF_SIZE];
11657852a85SMike Kravetz int cpu;
11757852a85SMike Kravetz
11857852a85SMike Kravetz if (!firmware_has_feature(FW_FEATURE_LPAR))
11957852a85SMike Kravetz return 0;
12057852a85SMike Kravetz
121969ea5c5SStephen Rothwell if (register_trace_hcall_entry(probe_hcall_entry, NULL))
122c8cd093aSAnton Blanchard return -EINVAL;
123c8cd093aSAnton Blanchard
124969ea5c5SStephen Rothwell if (register_trace_hcall_exit(probe_hcall_exit, NULL)) {
125969ea5c5SStephen Rothwell unregister_trace_hcall_entry(probe_hcall_entry, NULL);
126c8cd093aSAnton Blanchard return -EINVAL;
127c8cd093aSAnton Blanchard }
128c8cd093aSAnton Blanchard
12957852a85SMike Kravetz hcall_root = debugfs_create_dir(HCALL_ROOT_DIR, NULL);
13057852a85SMike Kravetz
13157852a85SMike Kravetz for_each_possible_cpu(cpu) {
13257852a85SMike Kravetz snprintf(cpu_name_buf, CPU_NAME_BUF_SIZE, "cpu%d", cpu);
133ff229319SGreg Kroah-Hartman debugfs_create_file(cpu_name_buf, 0444, hcall_root,
13457852a85SMike Kravetz per_cpu(hcall_stats, cpu),
13596543e73SLiu Shixin &hcall_inst_fops);
13657852a85SMike Kravetz }
13757852a85SMike Kravetz
13857852a85SMike Kravetz return 0;
13957852a85SMike Kravetz }
1408e83e905SMichael Ellerman machine_device_initcall(pseries, hcall_inst_init);
141