x86, hyperv: Get the local APIC timer frequency from the hypervisor
[linux-2.6-block.git] / arch / x86 / kernel / cpu / mshyperv.c
CommitLineData
a2a47c6c
KS
1/*
2 * HyperV Detection code.
3 *
4 * Copyright (C) 2010, Novell, Inc.
5 * Author : K. Y. Srinivasan <ksrinivasan@novell.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 as published by
9fa02317 9 * the Free Software Foundation; version 2 of the License.
a2a47c6c
KS
10 *
11 */
12
13#include <linux/types.h>
6f4151c8
S
14#include <linux/time.h>
15#include <linux/clocksource.h>
3998d095 16#include <linux/module.h>
bc2b0331 17#include <linux/hardirq.h>
9e7827b5 18#include <linux/efi.h>
bc2b0331 19#include <linux/interrupt.h>
a2a47c6c 20#include <asm/processor.h>
e08cae41 21#include <asm/hypervisor.h>
a2a47c6c
KS
22#include <asm/hyperv.h>
23#include <asm/mshyperv.h>
bc2b0331
S
24#include <asm/desc.h>
25#include <asm/idle.h>
26#include <asm/irq_regs.h>
9e7827b5 27#include <asm/i8259.h>
a2a47c6c 28
e08cae41 29struct ms_hyperv_info ms_hyperv;
9279aa55 30EXPORT_SYMBOL_GPL(ms_hyperv);
a2a47c6c 31
9df56f19 32static uint32_t __init ms_hyperv_platform(void)
a2a47c6c 33{
e08cae41
PA
34 u32 eax;
35 u32 hyp_signature[3];
a2a47c6c 36
e08cae41 37 if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
9df56f19 38 return 0;
a2a47c6c 39
e08cae41
PA
40 cpuid(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS,
41 &eax, &hyp_signature[0], &hyp_signature[1], &hyp_signature[2]);
a2a47c6c 42
9df56f19
JW
43 if (eax >= HYPERV_CPUID_MIN &&
44 eax <= HYPERV_CPUID_MAX &&
45 !memcmp("Microsoft Hv", hyp_signature, 12))
46 return HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
47
48 return 0;
a2a47c6c
KS
49}
50
6f4151c8
S
51static cycle_t read_hv_clock(struct clocksource *arg)
52{
53 cycle_t current_tick;
54 /*
55 * Read the partition counter to get the current tick count. This count
56 * is set to 0 when the partition is created and is incremented in
57 * 100 nanosecond units.
58 */
59 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
60 return current_tick;
61}
62
63static struct clocksource hyperv_cs = {
64 .name = "hyperv_clocksource",
65 .rating = 400, /* use this when running on Hyperv*/
66 .read = read_hv_clock,
67 .mask = CLOCKSOURCE_MASK(64),
68};
69
e08cae41 70static void __init ms_hyperv_init_platform(void)
a2a47c6c 71{
9e7827b5
S
72 u64 hv_lapic_frequency;
73
a2a47c6c 74 /*
e08cae41 75 * Extract the features and hints
a2a47c6c 76 */
e08cae41
PA
77 ms_hyperv.features = cpuid_eax(HYPERV_CPUID_FEATURES);
78 ms_hyperv.hints = cpuid_eax(HYPERV_CPUID_ENLIGHTMENT_INFO);
a2a47c6c 79
e08cae41
PA
80 printk(KERN_INFO "HyperV: features 0x%x, hints 0x%x\n",
81 ms_hyperv.features, ms_hyperv.hints);
6f4151c8 82
9e7827b5
S
83 if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) {
84 /*
85 * Get the APIC frequency.
86 */
87 rdmsrl(HV_X64_MSR_APIC_FREQUENCY, hv_lapic_frequency);
88 hv_lapic_frequency = div_u64(hv_lapic_frequency, HZ);
89 lapic_timer_frequency = hv_lapic_frequency;
90 printk(KERN_INFO "HyperV: LAPIC Timer Frequency: %#x\n",
91 lapic_timer_frequency);
92
93 /*
94 * On Hyper-V, when we are booting off an EFI firmware stack,
95 * we do not have many legacy devices including PIC, PIT etc.
96 */
97 if (efi_enabled(EFI_BOOT)) {
98 printk(KERN_INFO "HyperV: Using null_legacy_pic\n");
99 legacy_pic = &null_legacy_pic;
100 }
101 }
102
32068f65
OH
103 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
104 clocksource_register_hz(&hyperv_cs, NSEC_PER_SEC/100);
a2a47c6c 105}
e08cae41
PA
106
107const __refconst struct hypervisor_x86 x86_hyper_ms_hyperv = {
108 .name = "Microsoft HyperV",
109 .detect = ms_hyperv_platform,
110 .init_platform = ms_hyperv_init_platform,
111};
96f6e775 112EXPORT_SYMBOL(x86_hyper_ms_hyperv);
bc2b0331
S
113
114#if IS_ENABLED(CONFIG_HYPERV)
115static int vmbus_irq = -1;
116static irq_handler_t vmbus_isr;
117
118void hv_register_vmbus_handler(int irq, irq_handler_t handler)
119{
7eff7ded
S
120 /*
121 * Setup the IDT for hypervisor callback.
122 */
123 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, hyperv_callback_vector);
124
bc2b0331
S
125 vmbus_irq = irq;
126 vmbus_isr = handler;
127}
128
129void hyperv_vector_handler(struct pt_regs *regs)
130{
131 struct pt_regs *old_regs = set_irq_regs(regs);
132 struct irq_desc *desc;
133
134 irq_enter();
135 exit_idle();
136
137 desc = irq_to_desc(vmbus_irq);
138
139 if (desc)
140 generic_handle_irq_desc(vmbus_irq, desc);
141
142 irq_exit();
143 set_irq_regs(old_regs);
144}
145#else
146void hv_register_vmbus_handler(int irq, irq_handler_t handler)
147{
148}
149#endif
150EXPORT_SYMBOL_GPL(hv_register_vmbus_handler);