1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
4  */
5 
6 #ifndef __XEN_PUBLIC_PMU_H__
7 #define __XEN_PUBLIC_PMU_H__
8 
9 #include "xen.h"
10 #if defined(__i386__) || defined(__x86_64__)
11 #include "arch-x86/pmu.h"
12 #elif defined (__arm__) || defined (__aarch64__)
13 #include "arch-arm.h"
14 #elif defined (__powerpc64__)
15 #include "arch-ppc.h"
16 #elif defined(__riscv)
17 #include "arch-riscv.h"
18 #else
19 #error "Unsupported architecture"
20 #endif
21 
22 #define XENPMU_VER_MAJ    0
23 #define XENPMU_VER_MIN    1
24 
25 /*
26  * ` enum neg_errnoval
27  * ` HYPERVISOR_xenpmu_op(enum xenpmu_op cmd, struct xenpmu_params *args);
28  *
29  * @cmd  == XENPMU_* (PMU operation)
30  * @args == struct xenpmu_params
31  */
32 /* ` enum xenpmu_op { */
33 #define XENPMU_mode_get        0 /* Also used for getting PMU version */
34 #define XENPMU_mode_set        1
35 #define XENPMU_feature_get     2
36 #define XENPMU_feature_set     3
37 #define XENPMU_init            4
38 #define XENPMU_finish          5
39 #define XENPMU_lvtpc_set       6
40 #define XENPMU_flush           7 /* Write cached MSR values to HW     */
41 /* ` } */
42 
43 /* Parameters structure for HYPERVISOR_xenpmu_op call */
44 struct xen_pmu_params {
45     /* IN/OUT parameters */
46     struct {
47         uint32_t maj;
48         uint32_t min;
49     } version;
50     uint64_t val;
51 
52     /* IN parameters */
53     uint32_t vcpu;
54     uint32_t pad;
55 };
56 typedef struct xen_pmu_params xen_pmu_params_t;
57 DEFINE_XEN_GUEST_HANDLE(xen_pmu_params_t);
58 
59 /* PMU modes:
60  * - XENPMU_MODE_OFF:   No PMU virtualization
61  * - XENPMU_MODE_SELF:  Guests can profile themselves
62  * - XENPMU_MODE_HV:    Guests can profile themselves, dom0 profiles
63  *                      itself and Xen
64  * - XENPMU_MODE_ALL:   Only dom0 has access to VPMU and it profiles
65  *                      everyone: itself, the hypervisor and the guests.
66  */
67 #define XENPMU_MODE_OFF           0
68 #define XENPMU_MODE_SELF          (1<<0)
69 #define XENPMU_MODE_HV            (1<<1)
70 #define XENPMU_MODE_ALL           (1<<2)
71 
72 /*
73  * PMU features:
74  * - XENPMU_FEATURE_INTEL_BTS:  Intel BTS support (ignored on AMD)
75  * - XENPMU_FEATURE_IPC_ONLY:   Restrict PMCs to the most minimum set possible.
76  *                              Instructions, cycles, and ref cycles. Can be
77  *                              used to calculate instructions-per-cycle (IPC)
78  *                              (ignored on AMD).
79  * - XENPMU_FEATURE_ARCH_ONLY:  Restrict PMCs to the Intel Pre-Defined
80  *                              Architectural Performance Events exposed by
81  *                              cpuid and listed in the Intel developer's manual
82  *                              (ignored on AMD).
83  */
84 #define XENPMU_FEATURE_INTEL_BTS  (1<<0)
85 #define XENPMU_FEATURE_IPC_ONLY   (1<<1)
86 #define XENPMU_FEATURE_ARCH_ONLY  (1<<2)
87 
88 /*
89  * Shared PMU data between hypervisor and PV(H) domains.
90  *
91  * The hypervisor fills out this structure during PMU interrupt and sends an
92  * interrupt to appropriate VCPU.
93  * Architecture-independent fields of xen_pmu_data are WO for the hypervisor
94  * and RO for the guest but some fields in xen_pmu_arch can be writable
95  * by both the hypervisor and the guest (see arch-$arch/pmu.h).
96  */
97 struct xen_pmu_data {
98     /* Interrupted VCPU */
99     uint32_t vcpu_id;
100 
101     /*
102      * Physical processor on which the interrupt occurred. On non-privileged
103      * guests set to vcpu_id;
104      */
105     uint32_t pcpu_id;
106 
107     /*
108      * Domain that was interrupted. On non-privileged guests set to DOMID_SELF.
109      * On privileged guests can be DOMID_SELF, DOMID_XEN, or, when in
110      * XENPMU_MODE_ALL mode, domain ID of another domain.
111      */
112     domid_t  domain_id;
113 
114     uint8_t pad[6];
115 
116     /* Architecture-specific information */
117     xen_pmu_arch_t pmu;
118 };
119 
120 #endif /* __XEN_PUBLIC_PMU_H__ */
121 
122 /*
123  * Local variables:
124  * mode: C
125  * c-file-style: "BSD"
126  * c-basic-offset: 4
127  * tab-width: 4
128  * indent-tabs-mode: nil
129  * End:
130  */
131