Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 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 |
Greg Kroah-Hartman | 9fa0231 | 2010-05-07 16:55:41 -0700 | [diff] [blame] | 9 | * the Free Software Foundation; version 2 of the License. |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <linux/types.h> |
K. Y. Srinivasan | 6f4151c8 | 2011-09-07 15:25:10 -0700 | [diff] [blame] | 14 | #include <linux/time.h> |
| 15 | #include <linux/clocksource.h> |
H. Peter Anvin | 3998d09 | 2010-05-09 22:46:54 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 17 | #include <linux/hardirq.h> |
K. Y. Srinivasan | 9e7827b | 2013-09-30 17:28:52 +0200 | [diff] [blame] | 18 | #include <linux/efi.h> |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Thomas Gleixner | 1aec169 | 2014-02-23 21:40:22 +0000 | [diff] [blame] | 20 | #include <linux/irq.h> |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 21 | #include <asm/processor.h> |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 22 | #include <asm/hypervisor.h> |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 23 | #include <asm/hyperv.h> |
| 24 | #include <asm/mshyperv.h> |
K. Y. Srinivasan | bc2b033 | 2013-02-03 17:22:39 -0800 | [diff] [blame] | 25 | #include <asm/desc.h> |
| 26 | #include <asm/idle.h> |
| 27 | #include <asm/irq_regs.h> |
K. Y. Srinivasan | 9e7827b | 2013-09-30 17:28:52 +0200 | [diff] [blame] | 28 | #include <asm/i8259.h> |
David Rientjes | d68ce01 | 2013-10-11 16:07:31 -0700 | [diff] [blame] | 29 | #include <asm/apic.h> |
Jason Wang | ca3ba2a | 2014-02-28 11:30:29 +0800 | [diff] [blame] | 30 | #include <asm/timer.h> |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 31 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 32 | struct ms_hyperv_info ms_hyperv; |
Ky Srinivasan | 9279aa5 | 2010-06-28 08:48:55 -0600 | [diff] [blame] | 33 | EXPORT_SYMBOL_GPL(ms_hyperv); |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 34 | |
Thomas Gleixner | 3c43367 | 2014-03-04 23:39:58 +0100 | [diff] [blame] | 35 | #if IS_ENABLED(CONFIG_HYPERV) |
Thomas Gleixner | 76d388c | 2014-03-05 13:42:14 +0100 | [diff] [blame] | 36 | static void (*vmbus_handler)(void); |
Thomas Gleixner | 1aec169 | 2014-02-23 21:40:22 +0000 | [diff] [blame] | 37 | |
| 38 | void hyperv_vector_handler(struct pt_regs *regs) |
| 39 | { |
| 40 | struct pt_regs *old_regs = set_irq_regs(regs); |
| 41 | |
Thomas Gleixner | 6af7faf | 2015-05-15 15:48:25 +0200 | [diff] [blame^] | 42 | entering_irq(); |
Thomas Gleixner | 1aec169 | 2014-02-23 21:40:22 +0000 | [diff] [blame] | 43 | inc_irq_stat(irq_hv_callback_count); |
| 44 | if (vmbus_handler) |
| 45 | vmbus_handler(); |
| 46 | |
Thomas Gleixner | 6af7faf | 2015-05-15 15:48:25 +0200 | [diff] [blame^] | 47 | exiting_irq(); |
Thomas Gleixner | 1aec169 | 2014-02-23 21:40:22 +0000 | [diff] [blame] | 48 | set_irq_regs(old_regs); |
| 49 | } |
| 50 | |
Thomas Gleixner | 76d388c | 2014-03-05 13:42:14 +0100 | [diff] [blame] | 51 | void hv_setup_vmbus_irq(void (*handler)(void)) |
Thomas Gleixner | 1aec169 | 2014-02-23 21:40:22 +0000 | [diff] [blame] | 52 | { |
| 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 Gleixner | 76d388c | 2014-03-05 13:42:14 +0100 | [diff] [blame] | 63 | void hv_remove_vmbus_irq(void) |
Thomas Gleixner | 1aec169 | 2014-02-23 21:40:22 +0000 | [diff] [blame] | 64 | { |
| 65 | /* We have no way to deallocate the interrupt gate */ |
| 66 | vmbus_handler = NULL; |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(hv_setup_vmbus_irq); |
| 69 | EXPORT_SYMBOL_GPL(hv_remove_vmbus_irq); |
| 70 | #endif |
| 71 | |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 72 | static uint32_t __init ms_hyperv_platform(void) |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 73 | { |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 74 | u32 eax; |
| 75 | u32 hyp_signature[3]; |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 76 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 77 | if (!boot_cpu_has(X86_FEATURE_HYPERVISOR)) |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 78 | return 0; |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 79 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 80 | cpuid(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS, |
| 81 | &eax, &hyp_signature[0], &hyp_signature[1], &hyp_signature[2]); |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 82 | |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 83 | 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 Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 89 | } |
| 90 | |
K. Y. Srinivasan | 6f4151c8 | 2011-09-07 15:25:10 -0700 | [diff] [blame] | 91 | static 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 | |
| 103 | static 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. Srinivasan | 32c6590 | 2015-01-12 16:26:02 -0800 | [diff] [blame] | 108 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
K. Y. Srinivasan | 6f4151c8 | 2011-09-07 15:25:10 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 111 | static void __init ms_hyperv_init_platform(void) |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 112 | { |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 113 | /* |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 114 | * Extract the features and hints |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 115 | */ |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 116 | ms_hyperv.features = cpuid_eax(HYPERV_CPUID_FEATURES); |
| 117 | ms_hyperv.hints = cpuid_eax(HYPERV_CPUID_ENLIGHTMENT_INFO); |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 118 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 119 | printk(KERN_INFO "HyperV: features 0x%x, hints 0x%x\n", |
| 120 | ms_hyperv.features, ms_hyperv.hints); |
K. Y. Srinivasan | 6f4151c8 | 2011-09-07 15:25:10 -0700 | [diff] [blame] | 121 | |
K. Y. Srinivasan | 90ab9d5 | 2013-10-10 15:30:24 -0700 | [diff] [blame] | 122 | #ifdef CONFIG_X86_LOCAL_APIC |
K. Y. Srinivasan | 9e7827b | 2013-09-30 17:28:52 +0200 | [diff] [blame] | 123 | if (ms_hyperv.features & HV_X64_MSR_APIC_FREQUENCY_AVAILABLE) { |
| 124 | /* |
| 125 | * Get the APIC frequency. |
| 126 | */ |
H. Peter Anvin | 4c08edd | 2013-11-06 10:00:05 -0800 | [diff] [blame] | 127 | u64 hv_lapic_frequency; |
| 128 | |
K. Y. Srinivasan | 9e7827b | 2013-09-30 17:28:52 +0200 | [diff] [blame] | 129 | 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. Srinivasan | 9e7827b | 2013-09-30 17:28:52 +0200 | [diff] [blame] | 134 | } |
K. Y. Srinivasan | 90ab9d5 | 2013-10-10 15:30:24 -0700 | [diff] [blame] | 135 | #endif |
K. Y. Srinivasan | 9e7827b | 2013-09-30 17:28:52 +0200 | [diff] [blame] | 136 | |
Olaf Hering | 32068f6 | 2013-02-03 17:22:37 -0800 | [diff] [blame] | 137 | if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE) |
| 138 | clocksource_register_hz(&hyperv_cs, NSEC_PER_SEC/100); |
Jason Wang | ca3ba2a | 2014-02-28 11:30:29 +0800 | [diff] [blame] | 139 | |
| 140 | #ifdef CONFIG_X86_IO_APIC |
| 141 | no_timer_check = 1; |
| 142 | #endif |
| 143 | |
Ky Srinivasan | a2a47c6 | 2010-05-06 12:08:41 -0700 | [diff] [blame] | 144 | } |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 145 | |
| 146 | const __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 Anvin | 96f6e77 | 2010-05-09 01:10:34 -0700 | [diff] [blame] | 151 | EXPORT_SYMBOL(x86_hyper_ms_hyperv); |