1 #include <linux/types.h>
2 #include <linux/interrupt.h>
4 #include <asm/xen/hypercall.h>
6 #include <xen/interface/xen.h>
7 #include <xen/interface/vcpu.h>
8 #include <xen/interface/xenpmu.h>
13 /* x86_pmu.handle_irq definition */
14 #include "../events/perf_event.h"
16 #define XENPMU_IRQ_PROCESSING 1
18 /* Shared page between hypervisor and domain */
19 struct xen_pmu_data
*xenpmu_data
;
23 static DEFINE_PER_CPU(struct xenpmu
, xenpmu_shared
);
24 #define get_xenpmu_data() (this_cpu_ptr(&xenpmu_shared)->xenpmu_data)
25 #define get_xenpmu_flags() (this_cpu_ptr(&xenpmu_shared)->flags)
27 /* Macro for computing address of a PMU MSR bank */
28 #define field_offset(ctxt, field) ((void *)((uintptr_t)ctxt + \
29 (uintptr_t)ctxt->field))
32 #define F15H_NUM_COUNTERS 6
33 #define F10H_NUM_COUNTERS 4
35 static __read_mostly
uint32_t amd_counters_base
;
36 static __read_mostly
uint32_t amd_ctrls_base
;
37 static __read_mostly
int amd_msr_step
;
38 static __read_mostly
int k7_counters_mirrored
;
39 static __read_mostly
int amd_num_counters
;
42 #define MSR_TYPE_COUNTER 0
43 #define MSR_TYPE_CTRL 1
44 #define MSR_TYPE_GLOBAL 2
45 #define MSR_TYPE_ARCH_COUNTER 3
46 #define MSR_TYPE_ARCH_CTRL 4
48 /* Number of general pmu registers (CPUID.EAX[0xa].EAX[8..15]) */
49 #define PMU_GENERAL_NR_SHIFT 8
50 #define PMU_GENERAL_NR_BITS 8
51 #define PMU_GENERAL_NR_MASK (((1 << PMU_GENERAL_NR_BITS) - 1) \
52 << PMU_GENERAL_NR_SHIFT)
54 /* Number of fixed pmu registers (CPUID.EDX[0xa].EDX[0..4]) */
55 #define PMU_FIXED_NR_SHIFT 0
56 #define PMU_FIXED_NR_BITS 5
57 #define PMU_FIXED_NR_MASK (((1 << PMU_FIXED_NR_BITS) - 1) \
58 << PMU_FIXED_NR_SHIFT)
60 /* Alias registers (0x4c1) for full-width writes to PMCs */
61 #define MSR_PMC_ALIAS_MASK (~(MSR_IA32_PERFCTR0 ^ MSR_IA32_PMC0))
63 #define INTEL_PMC_TYPE_SHIFT 30
65 static __read_mostly
int intel_num_arch_counters
, intel_num_fixed_counters
;
68 static void xen_pmu_arch_init(void)
70 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
72 switch (boot_cpu_data
.x86
) {
74 amd_num_counters
= F15H_NUM_COUNTERS
;
75 amd_counters_base
= MSR_F15H_PERF_CTR
;
76 amd_ctrls_base
= MSR_F15H_PERF_CTL
;
78 k7_counters_mirrored
= 1;
85 amd_num_counters
= F10H_NUM_COUNTERS
;
86 amd_counters_base
= MSR_K7_PERFCTR0
;
87 amd_ctrls_base
= MSR_K7_EVNTSEL0
;
89 k7_counters_mirrored
= 0;
93 uint32_t eax
, ebx
, ecx
, edx
;
95 cpuid(0xa, &eax
, &ebx
, &ecx
, &edx
);
97 intel_num_arch_counters
= (eax
& PMU_GENERAL_NR_MASK
) >>
99 intel_num_fixed_counters
= (edx
& PMU_FIXED_NR_MASK
) >>
104 static inline uint32_t get_fam15h_addr(u32 addr
)
107 case MSR_K7_PERFCTR0
:
108 case MSR_K7_PERFCTR1
:
109 case MSR_K7_PERFCTR2
:
110 case MSR_K7_PERFCTR3
:
111 return MSR_F15H_PERF_CTR
+ (addr
- MSR_K7_PERFCTR0
);
112 case MSR_K7_EVNTSEL0
:
113 case MSR_K7_EVNTSEL1
:
114 case MSR_K7_EVNTSEL2
:
115 case MSR_K7_EVNTSEL3
:
116 return MSR_F15H_PERF_CTL
+ (addr
- MSR_K7_EVNTSEL0
);
124 static inline bool is_amd_pmu_msr(unsigned int msr
)
126 if ((msr
>= MSR_F15H_PERF_CTL
&&
127 msr
< MSR_F15H_PERF_CTR
+ (amd_num_counters
* 2)) ||
128 (msr
>= MSR_K7_EVNTSEL0
&&
129 msr
< MSR_K7_PERFCTR0
+ amd_num_counters
))
135 static int is_intel_pmu_msr(u32 msr_index
, int *type
, int *index
)
140 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
141 case MSR_IA32_DS_AREA
:
142 case MSR_IA32_PEBS_ENABLE
:
143 *type
= MSR_TYPE_CTRL
;
146 case MSR_CORE_PERF_GLOBAL_CTRL
:
147 case MSR_CORE_PERF_GLOBAL_STATUS
:
148 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
149 *type
= MSR_TYPE_GLOBAL
;
154 if ((msr_index
>= MSR_CORE_PERF_FIXED_CTR0
) &&
155 (msr_index
< MSR_CORE_PERF_FIXED_CTR0
+
156 intel_num_fixed_counters
)) {
157 *index
= msr_index
- MSR_CORE_PERF_FIXED_CTR0
;
158 *type
= MSR_TYPE_COUNTER
;
162 if ((msr_index
>= MSR_P6_EVNTSEL0
) &&
163 (msr_index
< MSR_P6_EVNTSEL0
+ intel_num_arch_counters
)) {
164 *index
= msr_index
- MSR_P6_EVNTSEL0
;
165 *type
= MSR_TYPE_ARCH_CTRL
;
169 msr_index_pmc
= msr_index
& MSR_PMC_ALIAS_MASK
;
170 if ((msr_index_pmc
>= MSR_IA32_PERFCTR0
) &&
171 (msr_index_pmc
< MSR_IA32_PERFCTR0
+
172 intel_num_arch_counters
)) {
173 *type
= MSR_TYPE_ARCH_COUNTER
;
174 *index
= msr_index_pmc
- MSR_IA32_PERFCTR0
;
181 static bool xen_intel_pmu_emulate(unsigned int msr
, u64
*val
, int type
,
182 int index
, bool is_read
)
184 uint64_t *reg
= NULL
;
185 struct xen_pmu_intel_ctxt
*ctxt
;
186 uint64_t *fix_counters
;
187 struct xen_pmu_cntr_pair
*arch_cntr_pair
;
188 struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
189 uint8_t xenpmu_flags
= get_xenpmu_flags();
192 if (!xenpmu_data
|| !(xenpmu_flags
& XENPMU_IRQ_PROCESSING
))
195 ctxt
= &xenpmu_data
->pmu
.c
.intel
;
198 case MSR_CORE_PERF_GLOBAL_OVF_CTRL
:
199 reg
= &ctxt
->global_ovf_ctrl
;
201 case MSR_CORE_PERF_GLOBAL_STATUS
:
202 reg
= &ctxt
->global_status
;
204 case MSR_CORE_PERF_GLOBAL_CTRL
:
205 reg
= &ctxt
->global_ctrl
;
207 case MSR_CORE_PERF_FIXED_CTR_CTRL
:
208 reg
= &ctxt
->fixed_ctrl
;
212 case MSR_TYPE_COUNTER
:
213 fix_counters
= field_offset(ctxt
, fixed_counters
);
214 reg
= &fix_counters
[index
];
216 case MSR_TYPE_ARCH_COUNTER
:
217 arch_cntr_pair
= field_offset(ctxt
, arch_counters
);
218 reg
= &arch_cntr_pair
[index
].counter
;
220 case MSR_TYPE_ARCH_CTRL
:
221 arch_cntr_pair
= field_offset(ctxt
, arch_counters
);
222 reg
= &arch_cntr_pair
[index
].control
;
235 if (msr
== MSR_CORE_PERF_GLOBAL_OVF_CTRL
)
236 ctxt
->global_status
&= (~(*val
));
244 static bool xen_amd_pmu_emulate(unsigned int msr
, u64
*val
, bool is_read
)
246 uint64_t *reg
= NULL
;
248 struct xen_pmu_amd_ctxt
*ctxt
;
249 uint64_t *counter_regs
, *ctrl_regs
;
250 struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
251 uint8_t xenpmu_flags
= get_xenpmu_flags();
253 if (!xenpmu_data
|| !(xenpmu_flags
& XENPMU_IRQ_PROCESSING
))
256 if (k7_counters_mirrored
&&
257 ((msr
>= MSR_K7_EVNTSEL0
) && (msr
<= MSR_K7_PERFCTR3
)))
258 msr
= get_fam15h_addr(msr
);
260 ctxt
= &xenpmu_data
->pmu
.c
.amd
;
261 for (i
= 0; i
< amd_num_counters
; i
++) {
262 if (msr
== amd_ctrls_base
+ off
) {
263 ctrl_regs
= field_offset(ctxt
, ctrls
);
266 } else if (msr
== amd_counters_base
+ off
) {
267 counter_regs
= field_offset(ctxt
, counters
);
268 reg
= &counter_regs
[i
];
285 bool pmu_msr_read(unsigned int msr
, uint64_t *val
, int *err
)
287 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
288 if (is_amd_pmu_msr(msr
)) {
289 if (!xen_amd_pmu_emulate(msr
, val
, 1))
290 *val
= native_read_msr_safe(msr
, err
);
296 if (is_intel_pmu_msr(msr
, &type
, &index
)) {
297 if (!xen_intel_pmu_emulate(msr
, val
, type
, index
, 1))
298 *val
= native_read_msr_safe(msr
, err
);
306 bool pmu_msr_write(unsigned int msr
, uint32_t low
, uint32_t high
, int *err
)
308 uint64_t val
= ((uint64_t)high
<< 32) | low
;
310 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) {
311 if (is_amd_pmu_msr(msr
)) {
312 if (!xen_amd_pmu_emulate(msr
, &val
, 0))
313 *err
= native_write_msr_safe(msr
, low
, high
);
319 if (is_intel_pmu_msr(msr
, &type
, &index
)) {
320 if (!xen_intel_pmu_emulate(msr
, &val
, type
, index
, 0))
321 *err
= native_write_msr_safe(msr
, low
, high
);
329 static unsigned long long xen_amd_read_pmc(int counter
)
331 struct xen_pmu_amd_ctxt
*ctxt
;
332 uint64_t *counter_regs
;
333 struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
334 uint8_t xenpmu_flags
= get_xenpmu_flags();
336 if (!xenpmu_data
|| !(xenpmu_flags
& XENPMU_IRQ_PROCESSING
)) {
340 msr
= amd_counters_base
+ (counter
* amd_msr_step
);
341 return native_read_msr_safe(msr
, &err
);
344 ctxt
= &xenpmu_data
->pmu
.c
.amd
;
345 counter_regs
= field_offset(ctxt
, counters
);
346 return counter_regs
[counter
];
349 static unsigned long long xen_intel_read_pmc(int counter
)
351 struct xen_pmu_intel_ctxt
*ctxt
;
352 uint64_t *fixed_counters
;
353 struct xen_pmu_cntr_pair
*arch_cntr_pair
;
354 struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
355 uint8_t xenpmu_flags
= get_xenpmu_flags();
357 if (!xenpmu_data
|| !(xenpmu_flags
& XENPMU_IRQ_PROCESSING
)) {
361 if (counter
& (1 << INTEL_PMC_TYPE_SHIFT
))
362 msr
= MSR_CORE_PERF_FIXED_CTR0
+ (counter
& 0xffff);
364 msr
= MSR_IA32_PERFCTR0
+ counter
;
366 return native_read_msr_safe(msr
, &err
);
369 ctxt
= &xenpmu_data
->pmu
.c
.intel
;
370 if (counter
& (1 << INTEL_PMC_TYPE_SHIFT
)) {
371 fixed_counters
= field_offset(ctxt
, fixed_counters
);
372 return fixed_counters
[counter
& 0xffff];
375 arch_cntr_pair
= field_offset(ctxt
, arch_counters
);
376 return arch_cntr_pair
[counter
].counter
;
379 unsigned long long xen_read_pmc(int counter
)
381 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
)
382 return xen_amd_read_pmc(counter
);
384 return xen_intel_read_pmc(counter
);
387 int pmu_apic_update(uint32_t val
)
390 struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
393 pr_warn_once("%s: pmudata not initialized\n", __func__
);
397 xenpmu_data
->pmu
.l
.lapic_lvtpc
= val
;
399 if (get_xenpmu_flags() & XENPMU_IRQ_PROCESSING
)
402 ret
= HYPERVISOR_xenpmu_op(XENPMU_lvtpc_set
, NULL
);
408 static int xen_is_in_guest(void)
410 const struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
413 pr_warn_once("%s: pmudata not initialized\n", __func__
);
417 if (!xen_initial_domain() || (xenpmu_data
->domain_id
>= DOMID_SELF
))
423 static int xen_is_user_mode(void)
425 const struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
428 pr_warn_once("%s: pmudata not initialized\n", __func__
);
432 if (xenpmu_data
->pmu
.pmu_flags
& PMU_SAMPLE_PV
)
433 return (xenpmu_data
->pmu
.pmu_flags
& PMU_SAMPLE_USER
);
435 return !!(xenpmu_data
->pmu
.r
.regs
.cpl
& 3);
438 static unsigned long xen_get_guest_ip(void)
440 const struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
443 pr_warn_once("%s: pmudata not initialized\n", __func__
);
447 return xenpmu_data
->pmu
.r
.regs
.ip
;
450 static struct perf_guest_info_callbacks xen_guest_cbs
= {
451 .is_in_guest
= xen_is_in_guest
,
452 .is_user_mode
= xen_is_user_mode
,
453 .get_guest_ip
= xen_get_guest_ip
,
456 /* Convert registers from Xen's format to Linux' */
457 static void xen_convert_regs(const struct xen_pmu_regs
*xen_regs
,
458 struct pt_regs
*regs
, uint64_t pmu_flags
)
460 regs
->ip
= xen_regs
->ip
;
461 regs
->cs
= xen_regs
->cs
;
462 regs
->sp
= xen_regs
->sp
;
464 if (pmu_flags
& PMU_SAMPLE_PV
) {
465 if (pmu_flags
& PMU_SAMPLE_USER
)
477 irqreturn_t
xen_pmu_irq_handler(int irq
, void *dev_id
)
479 int err
, ret
= IRQ_NONE
;
481 const struct xen_pmu_data
*xenpmu_data
= get_xenpmu_data();
482 uint8_t xenpmu_flags
= get_xenpmu_flags();
485 pr_warn_once("%s: pmudata not initialized\n", __func__
);
489 this_cpu_ptr(&xenpmu_shared
)->flags
=
490 xenpmu_flags
| XENPMU_IRQ_PROCESSING
;
491 xen_convert_regs(&xenpmu_data
->pmu
.r
.regs
, ®s
,
492 xenpmu_data
->pmu
.pmu_flags
);
493 if (x86_pmu
.handle_irq(®s
))
496 /* Write out cached context to HW */
497 err
= HYPERVISOR_xenpmu_op(XENPMU_flush
, NULL
);
498 this_cpu_ptr(&xenpmu_shared
)->flags
= xenpmu_flags
;
500 pr_warn_once("%s: failed hypercall, err: %d\n", __func__
, err
);
507 bool is_xen_pmu(int cpu
)
509 return (get_xenpmu_data() != NULL
);
512 void xen_pmu_init(int cpu
)
515 struct xen_pmu_params xp
;
517 struct xen_pmu_data
*xenpmu_data
;
519 BUILD_BUG_ON(sizeof(struct xen_pmu_data
) > PAGE_SIZE
);
521 if (xen_hvm_domain())
524 xenpmu_data
= (struct xen_pmu_data
*)get_zeroed_page(GFP_KERNEL
);
526 pr_err("VPMU init: No memory\n");
529 pfn
= virt_to_pfn(xenpmu_data
);
531 xp
.val
= pfn_to_mfn(pfn
);
533 xp
.version
.maj
= XENPMU_VER_MAJ
;
534 xp
.version
.min
= XENPMU_VER_MIN
;
535 err
= HYPERVISOR_xenpmu_op(XENPMU_init
, &xp
);
539 per_cpu(xenpmu_shared
, cpu
).xenpmu_data
= xenpmu_data
;
540 per_cpu(xenpmu_shared
, cpu
).flags
= 0;
543 perf_register_guest_info_callbacks(&xen_guest_cbs
);
550 if (err
== -EOPNOTSUPP
|| err
== -ENOSYS
)
551 pr_info_once("VPMU disabled by hypervisor.\n");
553 pr_info_once("Could not initialize VPMU for cpu %d, error %d\n",
555 free_pages((unsigned long)xenpmu_data
, 0);
558 void xen_pmu_finish(int cpu
)
560 struct xen_pmu_params xp
;
562 if (xen_hvm_domain())
566 xp
.version
.maj
= XENPMU_VER_MAJ
;
567 xp
.version
.min
= XENPMU_VER_MIN
;
569 (void)HYPERVISOR_xenpmu_op(XENPMU_finish
, &xp
);
571 free_pages((unsigned long)per_cpu(xenpmu_shared
, cpu
).xenpmu_data
, 0);
572 per_cpu(xenpmu_shared
, cpu
).xenpmu_data
= NULL
;