blob: aad4bd84b475ec4c762e72f62bbd9fb07f7ac2c4 [file] [log] [blame]
Ky Srinivasana2a47c62010-05-06 12:08:41 -07001/*
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
Greg Kroah-Hartman9fa02312010-05-07 16:55:41 -07009 * the Free Software Foundation; version 2 of the License.
Ky Srinivasana2a47c62010-05-06 12:08:41 -070010 *
11 */
12
13#include <linux/types.h>
K. Y. Srinivasan6f4151c82011-09-07 15:25:10 -070014#include <linux/time.h>
15#include <linux/clocksource.h>
H. Peter Anvin3998d092010-05-09 22:46:54 -070016#include <linux/module.h>
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080017#include <linux/hardirq.h>
K. Y. Srinivasan9e7827b2013-09-30 17:28:52 +020018#include <linux/efi.h>
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080019#include <linux/interrupt.h>
Thomas Gleixner1aec1692014-02-23 21:40:22 +000020#include <linux/irq.h>
Ky Srinivasana2a47c62010-05-06 12:08:41 -070021#include <asm/processor.h>
H. Peter Anvine08cae42010-05-07 16:57:28 -070022#include <asm/hypervisor.h>
Ky Srinivasana2a47c62010-05-06 12:08:41 -070023#include <asm/hyperv.h>
24#include <asm/mshyperv.h>
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080025#include <asm/desc.h>
26#include <asm/idle.h>
27#include <asm/irq_regs.h>
K. Y. Srinivasan9e7827b2013-09-30 17:28:52 +020028#include <asm/i8259.h>
David Rientjesd68ce012013-10-11 16:07:31 -070029#include <asm/apic.h>
Jason Wangca3ba2a2014-02-28 11:30:29 +080030#include <asm/timer.h>
Ky Srinivasana2a47c62010-05-06 12:08:41 -070031
H. Peter Anvine08cae42010-05-07 16:57:28 -070032struct ms_hyperv_info ms_hyperv;
Ky Srinivasan9279aa52010-06-28 08:48:55 -060033EXPORT_SYMBOL_GPL(ms_hyperv);
Ky Srinivasana2a47c62010-05-06 12:08:41 -070034
Thomas Gleixner3c433672014-03-04 23:39:58 +010035#if IS_ENABLED(CONFIG_HYPERV)
Thomas Gleixner76d388c2014-03-05 13:42:14 +010036static void (*vmbus_handler)(void);
Thomas Gleixner1aec1692014-02-23 21:40:22 +000037
38void hyperv_vector_handler(struct pt_regs *regs)
39{
40 struct pt_regs *old_regs = set_irq_regs(regs);
41
Thomas Gleixner6af7faf2015-05-15 15:48:25 +020042 entering_irq();
Thomas Gleixner1aec1692014-02-23 21:40:22 +000043 inc_irq_stat(irq_hv_callback_count);
44 if (vmbus_handler)
45 vmbus_handler();
46
Thomas Gleixner6af7faf2015-05-15 15:48:25 +020047 exiting_irq();
Thomas Gleixner1aec1692014-02-23 21:40:22 +000048 set_irq_regs(old_regs);
49}
50
Thomas Gleixner76d388c2014-03-05 13:42:14 +010051void hv_setup_vmbus_irq(void (*handler)(void))
Thomas Gleixner1aec1692014-02-23 21:40:22 +000052{
53 vmbus_handler = handler;
54 /*
55 * Setup the IDT for hypervisor callback. Prevent reallocation
56 * at module reload.
57 */
58 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
59 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
60 hyperv_callback_vector);
61}
62
Thomas Gleixner76d388c2014-03-05 13:42:14 +010063void hv_remove_vmbus_irq(void)
Thomas Gleixner1aec1692014-02-23 21:40:22 +000064{
65 /* We have no way to deallocate the interrupt gate */
66 vmbus_handler = NULL;
67}
68EXPORT_SYMBOL_GPL(hv_setup_vmbus_irq);
69EXPORT_SYMBOL_GPL(hv_remove_vmbus_irq);
70#endif
71
Jason Wang9df56f12013-07-25 16:54:35 +080072static uint32_t __init ms_hyperv_platform(void)
Ky Srinivasana2a47c62010-05-06 12:08:41 -070073{
H. Peter Anvine08cae42010-05-07 16:57:28 -070074 u32 eax;
75 u32 hyp_signature[3];
Ky Srinivasana2a47c62010-05-06 12:08:41 -070076
H. Peter Anvine08cae42010-05-07 16:57:28 -070077 if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
Jason Wang9df56f12013-07-25 16:54:35 +080078 return 0;
Ky Srinivasana2a47c62010-05-06 12:08:41 -070079
H. Peter Anvine08cae42010-05-07 16:57:28 -070080 cpuid(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS,
81 &eax, &hyp_signature[0], &hyp_signature[1], &hyp_signature[2]);
Ky Srinivasana2a47c62010-05-06 12:08:41 -070082
Jason Wang9df56f12013-07-25 16:54:35 +080083 if (eax >= HYPERV_CPUID_MIN &&
84 eax <= HYPERV_CPUID_MAX &&
85 !memcmp("Microsoft Hv", hyp_signature, 12))
86 return HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
87
88 return 0;
Ky Srinivasana2a47c62010-05-06 12:08:41 -070089}
90
K. Y. Srinivasan6f4151c82011-09-07 15:25:10 -070091static cycle_t read_hv_clock(struct clocksource *arg)
92{
93 cycle_t current_tick;
94 /*
95 * Read the partition counter to get the current tick count. This count
96 * is set to 0 when the partition is created and is incremented in
97 * 100 nanosecond units.
98 */
99 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
100 return current_tick;
101}
102
103static struct clocksource hyperv_cs = {
104 .name = "hyperv_clocksource",
105 .rating = 400, /* use this when running on Hyperv*/
106 .read = read_hv_clock,
107 .mask = CLOCKSOURCE_MASK(64),
K. Y. Srinivasan32c65902015-01-12 16:26:02 -0800108 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
K. Y. Srinivasan6f4151c82011-09-07 15:25:10 -0700109};
110
H. Peter Anvine08cae42010-05-07 16:57:28 -0700111static void __init ms_hyperv_init_platform(void)
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700112{
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700113 /*
H. Peter Anvine08cae42010-05-07 16:57:28 -0700114 * Extract the features and hints
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700115 */
H. Peter Anvine08cae42010-05-07 16:57:28 -0700116 ms_hyperv.features = cpuid_eax(HYPERV_CPUID_FEATURES);
117 ms_hyperv.hints = cpuid_eax(HYPERV_CPUID_ENLIGHTMENT_INFO);
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700118
H. Peter Anvine08cae42010-05-07 16:57:28 -0700119 printk(KERN_INFO "HyperV: features 0x%x, hints 0x%x\n",
120 ms_hyperv.features, ms_hyperv.hints);
K. Y. Srinivasan6f4151c82011-09-07 15:25:10 -0700121
K. Y. Srinivasan90ab9d52013-10-10 15:30:24 -0700122#ifdef CONFIG_X86_LOCAL_APIC
K. Y. Srinivasan9e7827b2013-09-30 17:28:52 +0200123 if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) {
124 /*
125 * Get the APIC frequency.
126 */
H. Peter Anvin4c08edd2013-11-06 10:00:05 -0800127 u64 hv_lapic_frequency;
128
K. Y. Srinivasan9e7827b2013-09-30 17:28:52 +0200129 rdmsrl(HV_X64_MSR_APIC_FREQUENCY, hv_lapic_frequency);
130 hv_lapic_frequency = div_u64(hv_lapic_frequency, HZ);
131 lapic_timer_frequency = hv_lapic_frequency;
132 printk(KERN_INFO "HyperV: LAPIC Timer Frequency: %#x\n",
133 lapic_timer_frequency);
K. Y. Srinivasan9e7827b2013-09-30 17:28:52 +0200134 }
K. Y. Srinivasan90ab9d52013-10-10 15:30:24 -0700135#endif
K. Y. Srinivasan9e7827b2013-09-30 17:28:52 +0200136
Olaf Hering32068f62013-02-03 17:22:37 -0800137 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
138 clocksource_register_hz(&hyperv_cs, NSEC_PER_SEC/100);
Jason Wangca3ba2a2014-02-28 11:30:29 +0800139
140#ifdef CONFIG_X86_IO_APIC
141 no_timer_check = 1;
142#endif
143
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700144}
H. Peter Anvine08cae42010-05-07 16:57:28 -0700145
146const __refconst struct hypervisor_x86 x86_hyper_ms_hyperv = {
147 .name = "Microsoft HyperV",
148 .detect = ms_hyperv_platform,
149 .init_platform = ms_hyperv_init_platform,
150};
H. Peter Anvin96f6e772010-05-09 01:10:34 -0700151EXPORT_SYMBOL(x86_hyper_ms_hyperv);