x86, hyperv: Correctly guard the local APIC calibration code
[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
90ab9d55 83#ifdef CONFIG_X86_LOCAL_APIC
9e7827b5
S
84 if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) {
85 /*
86 * Get the APIC frequency.
87 */
88 rdmsrl(HV_X64_MSR_APIC_FREQUENCY, hv_lapic_frequency);
89 hv_lapic_frequency = div_u64(hv_lapic_frequency, HZ);
90 lapic_timer_frequency = hv_lapic_frequency;
91 printk(KERN_INFO "HyperV: LAPIC Timer Frequency: %#x\n",
92 lapic_timer_frequency);
93
94 /*
95 * On Hyper-V, when we are booting off an EFI firmware stack,
96 * we do not have many legacy devices including PIC, PIT etc.
97 */
98 if (efi_enabled(EFI_BOOT)) {
99 printk(KERN_INFO "HyperV: Using null_legacy_pic\n");
100 legacy_pic = &null_legacy_pic;
101 }
102 }
90ab9d55 103#endif
9e7827b5 104
32068f65
OH
105 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
106 clocksource_register_hz(&hyperv_cs, NSEC_PER_SEC/100);
a2a47c6c 107}
e08cae41
PA
108
109const __refconst struct hypervisor_x86 x86_hyper_ms_hyperv = {
110 .name = "Microsoft HyperV",
111 .detect = ms_hyperv_platform,
112 .init_platform = ms_hyperv_init_platform,
113};
96f6e775 114EXPORT_SYMBOL(x86_hyper_ms_hyperv);
bc2b0331
S
115
116#if IS_ENABLED(CONFIG_HYPERV)
117static int vmbus_irq = -1;
118static irq_handler_t vmbus_isr;
119
120void hv_register_vmbus_handler(int irq, irq_handler_t handler)
121{
7eff7ded
S
122 /*
123 * Setup the IDT for hypervisor callback.
124 */
125 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, hyperv_callback_vector);
126
bc2b0331
S
127 vmbus_irq = irq;
128 vmbus_isr = handler;
129}
130
131void hyperv_vector_handler(struct pt_regs *regs)
132{
133 struct pt_regs *old_regs = set_irq_regs(regs);
134 struct irq_desc *desc;
135
136 irq_enter();
137 exit_idle();
138
139 desc = irq_to_desc(vmbus_irq);
140
141 if (desc)
142 generic_handle_irq_desc(vmbus_irq, desc);
143
144 irq_exit();
145 set_irq_regs(old_regs);
146}
147#else
148void hv_register_vmbus_handler(int irq, irq_handler_t handler)
149{
150}
151#endif
152EXPORT_SYMBOL_GPL(hv_register_vmbus_handler);