diff options
| author | Michael Kelley <mikelley@microsoft.com> | 2019-07-01 00:25:56 -0400 |
|---|---|---|
| committer | Thomas Gleixner <tglx@linutronix.de> | 2019-07-03 05:00:59 -0400 |
| commit | fd1fea6834d0f9f93062ae6685862908a9baed39 (patch) | |
| tree | 5f361b02b874c0517d363a4538d426a6830d4d40 | |
| parent | 341924049558e5f7c1a148a2c461a417933d35d9 (diff) | |
clocksource/drivers: Make Hyper-V clocksource ISA agnostic
Hyper-V clock/timer code and data structures are currently mixed
in with other code in the ISA independent drivers/hv directory as
well as the ISA dependent Hyper-V code under arch/x86.
Consolidate this code and data structures into a Hyper-V clocksource driver
to better follow the Linux model. In doing so, separate out the ISA
dependent portions so the new clocksource driver works for x86 and for the
in-process Hyper-V on ARM64 code.
To start, move the existing clockevents code to create the new clocksource
driver. Update the VMbus driver to call initialization and cleanup routines
since the Hyper-V synthetic timers are not independently enumerated in
ACPI.
No behavior is changed and no new functionality is added.
Suggested-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Michael Kelley <mikelley@microsoft.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Cc: "bp@alien8.de" <bp@alien8.de>
Cc: "will.deacon@arm.com" <will.deacon@arm.com>
Cc: "catalin.marinas@arm.com" <catalin.marinas@arm.com>
Cc: "mark.rutland@arm.com" <mark.rutland@arm.com>
Cc: "linux-arm-kernel@lists.infradead.org" <linux-arm-kernel@lists.infradead.org>
Cc: "gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>
Cc: "linux-hyperv@vger.kernel.org" <linux-hyperv@vger.kernel.org>
Cc: "olaf@aepfle.de" <olaf@aepfle.de>
Cc: "apw@canonical.com" <apw@canonical.com>
Cc: "jasowang@redhat.com" <jasowang@redhat.com>
Cc: "marcelo.cerri@canonical.com" <marcelo.cerri@canonical.com>
Cc: Sunil Muthuswamy <sunilmut@microsoft.com>
Cc: KY Srinivasan <kys@microsoft.com>
Cc: "sashal@kernel.org" <sashal@kernel.org>
Cc: "vincenzo.frascino@arm.com" <vincenzo.frascino@arm.com>
Cc: "linux-arch@vger.kernel.org" <linux-arch@vger.kernel.org>
Cc: "linux-mips@vger.kernel.org" <linux-mips@vger.kernel.org>
Cc: "linux-kselftest@vger.kernel.org" <linux-kselftest@vger.kernel.org>
Cc: "arnd@arndb.de" <arnd@arndb.de>
Cc: "linux@armlinux.org.uk" <linux@armlinux.org.uk>
Cc: "ralf@linux-mips.org" <ralf@linux-mips.org>
Cc: "paul.burton@mips.com" <paul.burton@mips.com>
Cc: "daniel.lezcano@linaro.org" <daniel.lezcano@linaro.org>
Cc: "salyzyn@android.com" <salyzyn@android.com>
Cc: "pcc@google.com" <pcc@google.com>
Cc: "shuah@kernel.org" <shuah@kernel.org>
Cc: "0x7f454c46@gmail.com" <0x7f454c46@gmail.com>
Cc: "linux@rasmusvillemoes.dk" <linux@rasmusvillemoes.dk>
Cc: "huw@codeweavers.com" <huw@codeweavers.com>
Cc: "sfr@canb.auug.org.au" <sfr@canb.auug.org.au>
Cc: "pbonzini@redhat.com" <pbonzini@redhat.com>
Cc: "rkrcmar@redhat.com" <rkrcmar@redhat.com>
Cc: "kvm@vger.kernel.org" <kvm@vger.kernel.org>
Link: https://lkml.kernel.org/r/1561955054-1838-2-git-send-email-mikelley@microsoft.com
| -rw-r--r-- | MAINTAINERS | 2 | ||||
| -rw-r--r-- | arch/x86/include/asm/hyperv-tlfs.h | 6 | ||||
| -rw-r--r-- | arch/x86/kernel/cpu/mshyperv.c | 4 | ||||
| -rw-r--r-- | drivers/clocksource/Makefile | 1 | ||||
| -rw-r--r-- | drivers/clocksource/hyperv_timer.c | 200 | ||||
| -rw-r--r-- | drivers/hv/Kconfig | 3 | ||||
| -rw-r--r-- | drivers/hv/hv.c | 156 | ||||
| -rw-r--r-- | drivers/hv/hyperv_vmbus.h | 3 | ||||
| -rw-r--r-- | drivers/hv/vmbus_drv.c | 42 | ||||
| -rw-r--r-- | include/clocksource/hyperv_timer.h | 27 |
10 files changed, 268 insertions, 176 deletions
diff --git a/MAINTAINERS b/MAINTAINERS index a2812972b7b0..bfde42a76a95 100644 --- a/MAINTAINERS +++ b/MAINTAINERS | |||
| @@ -7313,6 +7313,7 @@ F: arch/x86/include/asm/trace/hyperv.h | |||
| 7313 | F: arch/x86/include/asm/hyperv-tlfs.h | 7313 | F: arch/x86/include/asm/hyperv-tlfs.h |
| 7314 | F: arch/x86/kernel/cpu/mshyperv.c | 7314 | F: arch/x86/kernel/cpu/mshyperv.c |
| 7315 | F: arch/x86/hyperv | 7315 | F: arch/x86/hyperv |
| 7316 | F: drivers/clocksource/hyperv_timer.c | ||
| 7316 | F: drivers/hid/hid-hyperv.c | 7317 | F: drivers/hid/hid-hyperv.c |
| 7317 | F: drivers/hv/ | 7318 | F: drivers/hv/ |
| 7318 | F: drivers/input/serio/hyperv-keyboard.c | 7319 | F: drivers/input/serio/hyperv-keyboard.c |
| @@ -7323,6 +7324,7 @@ F: drivers/uio/uio_hv_generic.c | |||
| 7323 | F: drivers/video/fbdev/hyperv_fb.c | 7324 | F: drivers/video/fbdev/hyperv_fb.c |
| 7324 | F: drivers/iommu/hyperv_iommu.c | 7325 | F: drivers/iommu/hyperv_iommu.c |
| 7325 | F: net/vmw_vsock/hyperv_transport.c | 7326 | F: net/vmw_vsock/hyperv_transport.c |
| 7327 | F: include/clocksource/hyperv_timer.h | ||
| 7326 | F: include/linux/hyperv.h | 7328 | F: include/linux/hyperv.h |
| 7327 | F: include/uapi/linux/hyperv.h | 7329 | F: include/uapi/linux/hyperv.h |
| 7328 | F: tools/hv/ | 7330 | F: tools/hv/ |
diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h index cdf44aa9a501..af78cd72b8f3 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h | |||
| @@ -401,6 +401,12 @@ enum HV_GENERIC_SET_FORMAT { | |||
| 401 | #define HV_STATUS_INVALID_CONNECTION_ID 18 | 401 | #define HV_STATUS_INVALID_CONNECTION_ID 18 |
| 402 | #define HV_STATUS_INSUFFICIENT_BUFFERS 19 | 402 | #define HV_STATUS_INSUFFICIENT_BUFFERS 19 |
| 403 | 403 | ||
| 404 | /* | ||
| 405 | * The Hyper-V TimeRefCount register and the TSC | ||
| 406 | * page provide a guest VM clock with 100ns tick rate | ||
| 407 | */ | ||
| 408 | #define HV_CLOCK_HZ (NSEC_PER_SEC/100) | ||
| 409 | |||
| 404 | typedef struct _HV_REFERENCE_TSC_PAGE { | 410 | typedef struct _HV_REFERENCE_TSC_PAGE { |
| 405 | __u32 tsc_sequence; | 411 | __u32 tsc_sequence; |
| 406 | __u32 res1; | 412 | __u32 res1; |
diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 7df29f08871b..1e5f7a03ddf5 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c | |||
| @@ -17,6 +17,7 @@ | |||
| 17 | #include <linux/irq.h> | 17 | #include <linux/irq.h> |
| 18 | #include <linux/kexec.h> | 18 | #include <linux/kexec.h> |
| 19 | #include <linux/i8253.h> | 19 | #include <linux/i8253.h> |
| 20 | #include <linux/random.h> | ||
| 20 | #include <asm/processor.h> | 21 | #include <asm/processor.h> |
| 21 | #include <asm/hypervisor.h> | 22 | #include <asm/hypervisor.h> |
| 22 | #include <asm/hyperv-tlfs.h> | 23 | #include <asm/hyperv-tlfs.h> |
| @@ -80,6 +81,7 @@ __visible void __irq_entry hv_stimer0_vector_handler(struct pt_regs *regs) | |||
| 80 | inc_irq_stat(hyperv_stimer0_count); | 81 | inc_irq_stat(hyperv_stimer0_count); |
| 81 | if (hv_stimer0_handler) | 82 | if (hv_stimer0_handler) |
| 82 | hv_stimer0_handler(); | 83 | hv_stimer0_handler(); |
| 84 | add_interrupt_randomness(HYPERV_STIMER0_VECTOR, 0); | ||
| 83 | ack_APIC_irq(); | 85 | ack_APIC_irq(); |
| 84 | 86 | ||
| 85 | exiting_irq(); | 87 | exiting_irq(); |
| @@ -89,7 +91,7 @@ __visible void __irq_entry hv_stimer0_vector_handler(struct pt_regs *regs) | |||
| 89 | int hv_setup_stimer0_irq(int *irq, int *vector, void (*handler)(void)) | 91 | int hv_setup_stimer0_irq(int *irq, int *vector, void (*handler)(void)) |
| 90 | { | 92 | { |
| 91 | *vector = HYPERV_STIMER0_VECTOR; | 93 | *vector = HYPERV_STIMER0_VECTOR; |
| 92 | *irq = 0; /* Unused on x86/x64 */ | 94 | *irq = -1; /* Unused on x86/x64 */ |
| 93 | hv_stimer0_handler = handler; | 95 | hv_stimer0_handler = handler; |
| 94 | return 0; | 96 | return 0; |
| 95 | } | 97 | } |
diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile index 5582252efb31..2e7936e7833f 100644 --- a/drivers/clocksource/Makefile +++ b/drivers/clocksource/Makefile | |||
| @@ -86,3 +86,4 @@ obj-$(CONFIG_ATCPIT100_TIMER) += timer-atcpit100.o | |||
| 86 | obj-$(CONFIG_RISCV_TIMER) += timer-riscv.o | 86 | obj-$(CONFIG_RISCV_TIMER) += timer-riscv.o |
| 87 | obj-$(CONFIG_CSKY_MP_TIMER) += timer-mp-csky.o | 87 | obj-$(CONFIG_CSKY_MP_TIMER) += timer-mp-csky.o |
| 88 | obj-$(CONFIG_GX6605S_TIMER) += timer-gx6605s.o | 88 | obj-$(CONFIG_GX6605S_TIMER) += timer-gx6605s.o |
| 89 | obj-$(CONFIG_HYPERV_TIMER) += hyperv_timer.o | ||
diff --git a/drivers/clocksource/hyperv_timer.c b/drivers/clocksource/hyperv_timer.c new file mode 100644 index 000000000000..68a28af31561 --- /dev/null +++ b/drivers/clocksource/hyperv_timer.c | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0 | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Clocksource driver for the synthetic counter and timers | ||
| 5 | * provided by the Hyper-V hypervisor to guest VMs, as described | ||
| 6 | * in the Hyper-V Top Level Functional Spec (TLFS). This driver | ||
| 7 | * is instruction set architecture independent. | ||
| 8 | * | ||
| 9 | * Copyright (C) 2019, Microsoft, Inc. | ||
| 10 | * | ||
| 11 | * Author: Michael Kelley <mikelley@microsoft.com> | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <linux/percpu.h> | ||
| 15 | #include <linux/cpumask.h> | ||
| 16 | #include <linux/clockchips.h> | ||
| 17 | #include <linux/mm.h> | ||
| 18 | #include <clocksource/hyperv_timer.h> | ||
| 19 | #include <asm/hyperv-tlfs.h> | ||
| 20 | #include <asm/mshyperv.h> | ||
| 21 | |||
| 22 | static struct clock_event_device __percpu *hv_clock_event; | ||
| 23 | |||
| 24 | /* | ||
| 25 | * If false, we're using the old mechanism for stimer0 interrupts | ||
| 26 | * where it sends a VMbus message when it expires. The old | ||
| 27 | * mechanism is used when running on older versions of Hyper-V | ||
| 28 | * that don't support Direct Mode. While Hyper-V provides | ||
| 29 | * four stimer's per CPU, Linux uses only stimer0. | ||
| 30 | */ | ||
| 31 | static bool direct_mode_enabled; | ||
| 32 | |||
| 33 | static int stimer0_irq; | ||
| 34 | static int stimer0_vector; | ||
| 35 | static int stimer0_message_sint; | ||
| 36 | |||
| 37 | /* | ||
| 38 | * ISR for when stimer0 is operating in Direct Mode. Direct Mode | ||
| 39 | * does not use VMbus or any VMbus messages, so process here and not | ||
| 40 | * in the VMbus driver code. | ||
| 41 | */ | ||
| 42 | void hv_stimer0_isr(void) | ||
| 43 | { | ||
| 44 | struct clock_event_device *ce; | ||
| 45 | |||
| 46 | ce = this_cpu_ptr(hv_clock_event); | ||
| 47 | ce->event_handler(ce); | ||
| 48 | } | ||
| 49 | EXPORT_SYMBOL_GPL(hv_stimer0_isr); | ||
| 50 | |||
| 51 | static int hv_ce_set_next_event(unsigned long delta, | ||
| 52 | struct clock_event_device *evt) | ||
| 53 | { | ||
| 54 | u64 current_tick; | ||
| 55 | |||
| 56 | current_tick = hyperv_cs->read(NULL); | ||
| 57 | current_tick += delta; | ||
| 58 | hv_init_timer(0, current_tick); | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | static int hv_ce_shutdown(struct clock_event_device *evt) | ||
| 63 | { | ||
| 64 | hv_init_timer(0, 0); | ||
| 65 | hv_init_timer_config(0, 0); | ||
| 66 | if (direct_mode_enabled) | ||
| 67 | hv_disable_stimer0_percpu_irq(stimer0_irq); | ||
| 68 | |||
| 69 | return 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | static int hv_ce_set_oneshot(struct clock_event_device *evt) | ||
| 73 | { | ||
| 74 | union hv_stimer_config timer_cfg; | ||
| 75 | |||
| 76 | timer_cfg.as_uint64 = 0; | ||
| 77 | timer_cfg.enable = 1; | ||
| 78 | timer_cfg.auto_enable = 1; | ||
| 79 | if (direct_mode_enabled) { | ||
| 80 | /* | ||
| 81 | * When it expires, the timer will directly interrupt | ||
| 82 | * on the specified hardware vector/IRQ. | ||
| 83 | */ | ||
| 84 | timer_cfg.direct_mode = 1; | ||
| 85 | timer_cfg.apic_vector = stimer0_vector; | ||
| 86 | hv_enable_stimer0_percpu_irq(stimer0_irq); | ||
| 87 | } else { | ||
| 88 | /* | ||
| 89 | * When it expires, the timer will generate a VMbus message, | ||
| 90 | * to be handled by the normal VMbus interrupt handler. | ||
| 91 | */ | ||
| 92 | timer_cfg.direct_mode = 0; | ||
| 93 | timer_cfg.sintx = stimer0_message_sint; | ||
| 94 | } | ||
| 95 | hv_init_timer_config(0, timer_cfg.as_uint64); | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | /* | ||
| 100 | * hv_stimer_init - Per-cpu initialization of the clockevent | ||
| 101 | */ | ||
| 102 | void hv_stimer_init(unsigned int cpu) | ||
| 103 | { | ||
| 104 | struct clock_event_device *ce; | ||
| 105 | |||
| 106 | /* | ||
| 107 | * Synthetic timers are always available except on old versions of | ||
| 108 | * Hyper-V on x86. In that case, just return as Linux will use a | ||
| 109 | * clocksource based on emulated PIT or LAPIC timer hardware. | ||
| 110 | */ | ||
| 111 | if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)) | ||
| 112 | return; | ||
| 113 | |||
| 114 | ce = per_cpu_ptr(hv_clock_event, cpu); | ||
| 115 | ce->name = "Hyper-V clockevent"; | ||
| 116 | ce->features = CLOCK_EVT_FEAT_ONESHOT; | ||
| 117 | ce->cpumask = cpumask_of(cpu); | ||
| 118 | ce->rating = 1000; | ||
| 119 | ce->set_state_shutdown = hv_ce_shutdown; | ||
| 120 | ce->set_state_oneshot = hv_ce_set_oneshot; | ||
| 121 | ce->set_next_event = hv_ce_set_next_event; | ||
| 122 | |||
| 123 | clockevents_config_and_register(ce, | ||
| 124 | HV_CLOCK_HZ, | ||
| 125 | HV_MIN_DELTA_TICKS, | ||
| 126 | HV_MAX_MAX_DELTA_TICKS); | ||
| 127 | } | ||
| 128 | EXPORT_SYMBOL_GPL(hv_stimer_init); | ||
| 129 | |||
| 130 | /* | ||
| 131 | * hv_stimer_cleanup - Per-cpu cleanup of the clockevent | ||
| 132 | */ | ||
| 133 | void hv_stimer_cleanup(unsigned int cpu) | ||
| 134 | { | ||
| 135 | struct clock_event_device *ce; | ||
| 136 | |||
| 137 | /* Turn off clockevent device */ | ||
| 138 | if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) { | ||
| 139 | ce = per_cpu_ptr(hv_clock_event, cpu); | ||
| 140 | hv_ce_shutdown(ce); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | EXPORT_SYMBOL_GPL(hv_stimer_cleanup); | ||
| 144 | |||
| 145 | /* hv_stimer_alloc - Global initialization of the clockevent and stimer0 */ | ||
| 146 | int hv_stimer_alloc(int sint) | ||
| 147 | { | ||
| 148 | int ret; | ||
| 149 | |||
| 150 | hv_clock_event = alloc_percpu(struct clock_event_device); | ||
| 151 | if (!hv_clock_event) | ||
| 152 | return -ENOMEM; | ||
| 153 | |||
| 154 | direct_mode_enabled = ms_hyperv.misc_features & | ||
| 155 | HV_STIMER_DIRECT_MODE_AVAILABLE; | ||
| 156 | if (direct_mode_enabled) { | ||
| 157 | ret = hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector, | ||
| 158 | hv_stimer0_isr); | ||
| 159 | if (ret) { | ||
| 160 | free_percpu(hv_clock_event); | ||
| 161 | hv_clock_event = NULL; | ||
| 162 | return ret; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | stimer0_message_sint = sint; | ||
| 167 | return 0; | ||
| 168 | } | ||
| 169 | EXPORT_SYMBOL_GPL(hv_stimer_alloc); | ||
| 170 | |||
| 171 | /* hv_stimer_free - Free global resources allocated by hv_stimer_alloc() */ | ||
| 172 | void hv_stimer_free(void) | ||
| 173 | { | ||
| 174 | if (direct_mode_enabled && (stimer0_irq != 0)) { | ||
| 175 | hv_remove_stimer0_irq(stimer0_irq); | ||
| 176 | stimer0_irq = 0; | ||
| 177 | } | ||
| 178 | free_percpu(hv_clock_event); | ||
| 179 | hv_clock_event = NULL; | ||
| 180 | } | ||
| 181 | EXPORT_SYMBOL_GPL(hv_stimer_free); | ||
| 182 | |||
| 183 | /* | ||
| 184 | * Do a global cleanup of clockevents for the cases of kexec and | ||
| 185 | * vmbus exit | ||
| 186 | */ | ||
| 187 | void hv_stimer_global_cleanup(void) | ||
| 188 | { | ||
| 189 | int cpu; | ||
| 190 | struct clock_event_device *ce; | ||
| 191 | |||
| 192 | if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) { | ||
| 193 | for_each_present_cpu(cpu) { | ||
| 194 | ce = per_cpu_ptr(hv_clock_event, cpu); | ||
| 195 | clockevents_unbind_device(ce, cpu); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | hv_stimer_free(); | ||
| 199 | } | ||
| 200 | EXPORT_SYMBOL_GPL(hv_stimer_global_cleanup); | ||
diff --git a/drivers/hv/Kconfig b/drivers/hv/Kconfig index 1c1a2514d6f3..c423e57ae888 100644 --- a/drivers/hv/Kconfig +++ b/drivers/hv/Kconfig | |||
| @@ -10,6 +10,9 @@ config HYPERV | |||
| 10 | Select this option to run Linux as a Hyper-V client operating | 10 | Select this option to run Linux as a Hyper-V client operating |
| 11 | system. | 11 | system. |
| 12 | 12 | ||
| 13 | config HYPERV_TIMER | ||
| 14 | def_bool HYPERV | ||
| 15 | |||
| 13 | config HYPERV_TSCPAGE | 16 | config HYPERV_TSCPAGE |
| 14 | def_bool HYPERV && X86_64 | 17 | def_bool HYPERV && X86_64 |
| 15 | 18 | ||
diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index a1ea482183e8..6188fb7dda42 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | #include <linux/version.h> | 16 | #include <linux/version.h> |
| 17 | #include <linux/random.h> | 17 | #include <linux/random.h> |
| 18 | #include <linux/clockchips.h> | 18 | #include <linux/clockchips.h> |
| 19 | #include <clocksource/hyperv_timer.h> | ||
| 19 | #include <asm/mshyperv.h> | 20 | #include <asm/mshyperv.h> |
| 20 | #include "hyperv_vmbus.h" | 21 | #include "hyperv_vmbus.h" |
| 21 | 22 | ||
| @@ -23,21 +24,6 @@ | |||
| 23 | struct hv_context hv_context; | 24 | struct hv_context hv_context; |
| 24 | 25 | ||
| 25 | /* | 26 | /* |
| 26 | * If false, we're using the old mechanism for stimer0 interrupts | ||
| 27 | * where it sends a VMbus message when it expires. The old | ||
| 28 | * mechanism is used when running on older versions of Hyper-V | ||
| 29 | * that don't support Direct Mode. While Hyper-V provides | ||
| 30 | * four stimer's per CPU, Linux uses only stimer0. | ||
| 31 | */ | ||
| 32 | static bool direct_mode_enabled; | ||
| 33 | static int stimer0_irq; | ||
| 34 | static int stimer0_vector; | ||
| 35 | |||
| 36 | #define HV_TIMER_FREQUENCY (10 * 1000 * 1000) /* 100ns period */ | ||
| 37 | #define HV_MAX_MAX_DELTA_TICKS 0xffffffff | ||
| 38 | #define HV_MIN_DELTA_TICKS 1 | ||
| 39 | |||
| 40 | /* | ||
| 41 | * hv_init - Main initialization routine. | 27 | * hv_init - Main initialization routine. |
| 42 | * | 28 | * |
| 43 | * This routine must be called before any other routines in here are called | 29 | * This routine must be called before any other routines in here are called |
| @@ -47,9 +33,6 @@ int hv_init(void) | |||
| 47 | hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context); | 33 | hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context); |
| 48 | if (!hv_context.cpu_context) | 34 | if (!hv_context.cpu_context) |
| 49 | return -ENOMEM; | 35 | return -ENOMEM; |
| 50 | |||
| 51 | direct_mode_enabled = ms_hyperv.misc_features & | ||
| 52 | HV_STIMER_DIRECT_MODE_AVAILABLE; | ||
| 53 | return 0; | 36 | return 0; |
| 54 | } | 37 | } |
| 55 | 38 | ||
| @@ -88,89 +71,6 @@ int hv_post_message(union hv_connection_id connection_id, | |||
| 88 | return status & 0xFFFF; | 71 | return status & 0xFFFF; |
| 89 | } | 72 | } |
| 90 | 73 | ||
| 91 | /* | ||
| 92 | * ISR for when stimer0 is operating in Direct Mode. Direct Mode | ||
| 93 | * does not use VMbus or any VMbus messages, so process here and not | ||
| 94 | * in the VMbus driver code. | ||
| 95 | */ | ||
| 96 | |||
| 97 | static void hv_stimer0_isr(void) | ||
| 98 | { | ||
| 99 | struct hv_per_cpu_context *hv_cpu; | ||
| 100 | |||
| 101 | hv_cpu = this_cpu_ptr(hv_context.cpu_context); | ||
| 102 | hv_cpu->clk_evt->event_handler(hv_cpu->clk_evt); | ||
| 103 | add_interrupt_randomness(stimer0_vector, 0); | ||
| 104 | } | ||
| 105 | |||
| 106 | static int hv_ce_set_next_event(unsigned long delta, | ||
| 107 | struct clock_event_device *evt) | ||
| 108 | { | ||
| 109 | u64 current_tick; | ||
| 110 | |||
| 111 | WARN_ON(!clockevent_state_oneshot(evt)); | ||
| 112 | |||
| 113 | current_tick = hyperv_cs->read(NULL); | ||
| 114 | current_tick += delta; | ||
| 115 | hv_init_timer(0, current_tick); | ||
| 116 | return 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | static int hv_ce_shutdown(struct clock_event_device *evt) | ||
| 120 | { | ||
| 121 | hv_init_timer(0, 0); | ||
| 122 | hv_init_timer_config(0, 0); | ||
| 123 | if (direct_mode_enabled) | ||
| 124 | hv_disable_stimer0_percpu_irq(stimer0_irq); | ||
| 125 | |||
| 126 | return 0; | ||
| 127 | } | ||
| 128 | |||
| 129 | static int hv_ce_set_oneshot(struct clock_event_device *evt) | ||
| 130 | { | ||
| 131 | union hv_stimer_config timer_cfg; | ||
| 132 | |||
| 133 | timer_cfg.as_uint64 = 0; | ||
| 134 | timer_cfg.enable = 1; | ||
| 135 | timer_cfg.auto_enable = 1; | ||
| 136 | if (direct_mode_enabled) { | ||
| 137 | /* | ||
| 138 | * When it expires, the timer will directly interrupt | ||
| 139 | * on the specified hardware vector/IRQ. | ||
| 140 | */ | ||
| 141 | timer_cfg.direct_mode = 1; | ||
| 142 | timer_cfg.apic_vector = stimer0_vector; | ||
| 143 | hv_enable_stimer0_percpu_irq(stimer0_irq); | ||
| 144 | } else { | ||
| 145 | /* | ||
| 146 | * When it expires, the timer will generate a VMbus message, | ||
| 147 | * to be handled by the normal VMbus interrupt handler. | ||
| 148 | */ | ||
| 149 | timer_cfg.direct_mode = 0; | ||
| 150 | timer_cfg.sintx = VMBUS_MESSAGE_SINT; | ||
| 151 | } | ||
| 152 | hv_init_timer_config(0, timer_cfg.as_uint64); | ||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | |||
| 156 | static void hv_init_clockevent_device(struct clock_event_device *dev, int cpu) | ||
| 157 | { | ||
| 158 | dev->name = "Hyper-V clockevent"; | ||
| 159 | dev->features = CLOCK_EVT_FEAT_ONESHOT; | ||
| 160 | dev->cpumask = cpumask_of(cpu); | ||
| 161 | dev->rating = 1000; | ||
| 162 | /* | ||
| 163 | * Avoid settint dev->owner = THIS_MODULE deliberately as doing so will | ||
| 164 | * result in clockevents_config_and_register() taking additional | ||
| 165 | * references to the hv_vmbus module making it impossible to unload. | ||
| 166 | */ | ||
| 167 | |||
| 168 | dev->set_state_shutdown = hv_ce_shutdown; | ||
| 169 | dev->set_state_oneshot = hv_ce_set_oneshot; | ||
| 170 | dev->set_next_event = hv_ce_set_next_event; | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | int hv_synic_alloc(void) | 74 | int hv_synic_alloc(void) |
| 175 | { | 75 | { |
| 176 | int cpu; | 76 | int cpu; |
| @@ -199,14 +99,6 @@ int hv_synic_alloc(void) | |||
| 199 | tasklet_init(&hv_cpu->msg_dpc, | 99 | tasklet_init(&hv_cpu->msg_dpc, |
| 200 | vmbus_on_msg_dpc, (unsigned long) hv_cpu); | 100 | vmbus_on_msg_dpc, (unsigned long) hv_cpu); |
| 201 | 101 | ||
| 202 | hv_cpu->clk_evt = kzalloc(sizeof(struct clock_event_device), | ||
| 203 | GFP_KERNEL); | ||
| 204 | if (hv_cpu->clk_evt == NULL) { | ||
| 205 | pr_err("Unable to allocate clock event device\n"); | ||
| 206 | goto err; | ||
| 207 | } | ||
| 208 | hv_init_clockevent_device(hv_cpu->clk_evt, cpu); | ||
| 209 | |||
| 210 | hv_cpu->synic_message_page = | 102 | hv_cpu->synic_message_page = |
| 211 | (void *)get_zeroed_page(GFP_ATOMIC); | 103 | (void *)get_zeroed_page(GFP_ATOMIC); |
| 212 | if (hv_cpu->synic_message_page == NULL) { | 104 | if (hv_cpu->synic_message_page == NULL) { |
| @@ -229,11 +121,6 @@ int hv_synic_alloc(void) | |||
| 229 | INIT_LIST_HEAD(&hv_cpu->chan_list); | 121 | INIT_LIST_HEAD(&hv_cpu->chan_list); |
| 230 | } | 122 | } |
| 231 | 123 | ||
| 232 | if (direct_mode_enabled && | ||
| 233 | hv_setup_stimer0_irq(&stimer0_irq, &stimer0_vector, | ||
| 234 | hv_stimer0_isr)) | ||
| 235 | goto err; | ||
| 236 | |||
| 237 | return 0; | 124 | return 0; |
| 238 | err: | 125 | err: |
| 239 | /* | 126 | /* |
| @@ -252,7 +139,6 @@ void hv_synic_free(void) | |||
| 252 | struct hv_per_cpu_context *hv_cpu | 139 | struct hv_per_cpu_context *hv_cpu |
| 253 | = per_cpu_ptr(hv_context.cpu_context, cpu); | 140 | = per_cpu_ptr(hv_context.cpu_context, cpu); |
| 254 | 141 | ||
| 255 | kfree(hv_cpu->clk_evt); | ||
| 256 | free_page((unsigned long)hv_cpu->synic_event_page); | 142 | free_page((unsigned long)hv_cpu->synic_event_page); |
| 257 | free_page((unsigned long)hv_cpu->synic_message_page); | 143 | free_page((unsigned long)hv_cpu->synic_message_page); |
| 258 | free_page((unsigned long)hv_cpu->post_msg_page); | 144 | free_page((unsigned long)hv_cpu->post_msg_page); |
| @@ -311,36 +197,9 @@ int hv_synic_init(unsigned int cpu) | |||
| 311 | 197 | ||
| 312 | hv_set_synic_state(sctrl.as_uint64); | 198 | hv_set_synic_state(sctrl.as_uint64); |
| 313 | 199 | ||
| 314 | /* | 200 | hv_stimer_init(cpu); |
| 315 | * Register the per-cpu clockevent source. | ||
| 316 | */ | ||
| 317 | if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) | ||
| 318 | clockevents_config_and_register(hv_cpu->clk_evt, | ||
| 319 | HV_TIMER_FREQUENCY, | ||
| 320 | HV_MIN_DELTA_TICKS, | ||
| 321 | HV_MAX_MAX_DELTA_TICKS); | ||
| 322 | return 0; | ||
| 323 | } | ||
| 324 | |||
| 325 | /* | ||
| 326 | * hv_synic_clockevents_cleanup - Cleanup clockevent devices | ||
| 327 | */ | ||
| 328 | void hv_synic_clockevents_cleanup(void) | ||
| 329 | { | ||
| 330 | int cpu; | ||
| 331 | 201 | ||
| 332 | if (!(ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE)) | 202 | return 0; |
| 333 | return; | ||
| 334 | |||
| 335 | if (direct_mode_enabled) | ||
| 336 | hv_remove_stimer0_irq(stimer0_irq); | ||
| 337 | |||
| 338 | for_each_present_cpu(cpu) { | ||
| 339 | struct hv_per_cpu_context *hv_cpu | ||
| 340 | = per_cpu_ptr(hv_context.cpu_context, cpu); | ||
| 341 | |||
| 342 | clockevents_unbind_device(hv_cpu->clk_evt, cpu); | ||
| 343 | } | ||
| 344 | } | 203 | } |
| 345 | 204 | ||
| 346 | /* | 205 | /* |
| @@ -388,14 +247,7 @@ int hv_synic_cleanup(unsigned int cpu) | |||
| 388 | if (channel_found && vmbus_connection.conn_state == CONNECTED) | 247 | if (channel_found && vmbus_connection.conn_state == CONNECTED) |
| 389 | return -EBUSY; | 248 | return -EBUSY; |
| 390 | 249 | ||
| 391 | /* Turn off clockevent device */ | 250 | hv_stimer_cleanup(cpu); |
| 392 | if (ms_hyperv.features & HV_MSR_SYNTIMER_AVAILABLE) { | ||
| 393 | struct hv_per_cpu_context *hv_cpu | ||
| 394 | = this_cpu_ptr(hv_context.cpu_context); | ||
| 395 | |||
| 396 | clockevents_unbind_device(hv_cpu->clk_evt, cpu); | ||
| 397 | hv_ce_shutdown(hv_cpu->clk_evt); | ||
| 398 | } | ||
| 399 | 251 | ||
| 400 | hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); | 252 | hv_get_synint_state(VMBUS_MESSAGE_SINT, shared_sint.as_uint64); |
| 401 | 253 | ||
diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h index b8e1ff05f110..362e70e9d145 100644 --- a/drivers/hv/hyperv_vmbus.h +++ b/drivers/hv/hyperv_vmbus.h | |||
| @@ -138,7 +138,6 @@ struct hv_per_cpu_context { | |||
| 138 | * per-cpu list of the channels based on their CPU affinity. | 138 | * per-cpu list of the channels based on their CPU affinity. |
| 139 | */ | 139 | */ |
| 140 | struct list_head chan_list; | 140 | struct list_head chan_list; |
| 141 | struct clock_event_device *clk_evt; | ||
| 142 | }; | 141 | }; |
| 143 | 142 | ||
| 144 | struct hv_context { | 143 | struct hv_context { |
| @@ -176,8 +175,6 @@ extern int hv_synic_init(unsigned int cpu); | |||
| 176 | 175 | ||
| 177 | extern int hv_synic_cleanup(unsigned int cpu); | 176 | extern int hv_synic_cleanup(unsigned int cpu); |
| 178 | 177 | ||
| 179 | extern void hv_synic_clockevents_cleanup(void); | ||
| 180 | |||
| 181 | /* Interface */ | 178 | /* Interface */ |
| 182 | 179 | ||
| 183 | void hv_ringbuffer_pre_init(struct vmbus_channel *channel); | 180 | void hv_ringbuffer_pre_init(struct vmbus_channel *channel); |
diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c index 92b1874b3eb3..72d5a7cde7ea 100644 --- a/drivers/hv/vmbus_drv.c +++ b/drivers/hv/vmbus_drv.c | |||
| @@ -30,6 +30,7 @@ | |||
| 30 | #include <linux/kdebug.h> | 30 | #include <linux/kdebug.h> |
| 31 | #include <linux/efi.h> | 31 | #include <linux/efi.h> |
| 32 | #include <linux/random.h> | 32 | #include <linux/random.h> |
| 33 | #include <clocksource/hyperv_timer.h> | ||
| 33 | #include "hyperv_vmbus.h" | 34 | #include "hyperv_vmbus.h" |
| 34 | 35 | ||
| 35 | struct vmbus_dynid { | 36 | struct vmbus_dynid { |
| @@ -955,17 +956,6 @@ static void vmbus_onmessage_work(struct work_struct *work) | |||
| 955 | kfree(ctx); | 956 | kfree(ctx); |
| 956 | } | 957 | } |
| 957 | 958 | ||
| 958 | static void hv_process_timer_expiration(struct hv_message *msg, | ||
| 959 | struct hv_per_cpu_context *hv_cpu) | ||
| 960 | { | ||
| 961 | struct clock_event_device *dev = hv_cpu->clk_evt; | ||
| 962 | |||
| 963 | if (dev->event_handler) | ||
| 964 | dev->event_handler(dev); | ||
| 965 | |||
| 966 | vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED); | ||
| 967 | } | ||
| 968 | |||
| 969 | void vmbus_on_msg_dpc(unsigned long data) | 959 | void vmbus_on_msg_dpc(unsigned long data) |
| 970 | { | 960 | { |
| 971 | struct hv_per_cpu_context *hv_cpu = (void *)data; | 961 | struct hv_per_cpu_context *hv_cpu = (void *)data; |
| @@ -1159,9 +1149,10 @@ static void vmbus_isr(void) | |||
| 1159 | 1149 | ||
| 1160 | /* Check if there are actual msgs to be processed */ | 1150 | /* Check if there are actual msgs to be processed */ |
| 1161 | if (msg->header.message_type != HVMSG_NONE) { | 1151 | if (msg->header.message_type != HVMSG_NONE) { |
| 1162 | if (msg->header.message_type == HVMSG_TIMER_EXPIRED) | 1152 | if (msg->header.message_type == HVMSG_TIMER_EXPIRED) { |
| 1163 | hv_process_timer_expiration(msg, hv_cpu); | 1153 | hv_stimer0_isr(); |
| 1164 | else | 1154 | vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED); |
| 1155 | } else | ||
| 1165 | tasklet_schedule(&hv_cpu->msg_dpc); | 1156 | tasklet_schedule(&hv_cpu->msg_dpc); |
| 1166 | } | 1157 | } |
| 1167 | 1158 | ||
| @@ -1263,14 +1254,19 @@ static int vmbus_bus_init(void) | |||
| 1263 | ret = hv_synic_alloc(); | 1254 | ret = hv_synic_alloc(); |
| 1264 | if (ret) | 1255 | if (ret) |
| 1265 | goto err_alloc; | 1256 | goto err_alloc; |
| 1257 | |||
| 1258 | ret = hv_stimer_alloc(VMBUS_MESSAGE_SINT); | ||
| 1259 | if (ret < 0) | ||
| 1260 | goto err_alloc; | ||
| 1261 | |||
| 1266 | /* | 1262 | /* |
| 1267 | * Initialize the per-cpu interrupt state and | 1263 | * Initialize the per-cpu interrupt state and stimer state. |
| 1268 | * connect to the host. | 1264 | * Then connect to the host. |
| 1269 | */ | 1265 | */ |
| 1270 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online", | 1266 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online", |
| 1271 | hv_synic_init, hv_synic_cleanup); | 1267 | hv_synic_init, hv_synic_cleanup); |
| 1272 | if (ret < 0) | 1268 | if (ret < 0) |
| 1273 | goto err_alloc; | 1269 | goto err_cpuhp; |
| 1274 | hyperv_cpuhp_online = ret; | 1270 | hyperv_cpuhp_online = ret; |
| 1275 | 1271 | ||
| 1276 | ret = vmbus_connect(); | 1272 | ret = vmbus_connect(); |
| @@ -1318,6 +1314,8 @@ static int vmbus_bus_init(void) | |||
| 1318 | 1314 | ||
| 1319 | err_connect: | 1315 | err_connect: |
| 1320 | cpuhp_remove_state(hyperv_cpuhp_online); | 1316 | cpuhp_remove_state(hyperv_cpuhp_online); |
| 1317 | err_cpuhp: | ||
| 1318 | hv_stimer_free(); | ||
| 1321 | err_alloc: | 1319 | err_alloc: |
| 1322 | hv_synic_free(); | 1320 | hv_synic_free(); |
| 1323 | hv_remove_vmbus_irq(); | 1321 | hv_remove_vmbus_irq(); |
| @@ -2064,7 +2062,7 @@ static struct acpi_driver vmbus_acpi_driver = { | |||
| 2064 | 2062 | ||
| 2065 | static void hv_kexec_handler(void) | 2063 | static void hv_kexec_handler(void) |
| 2066 | { | 2064 | { |
| 2067 | hv_synic_clockevents_cleanup(); | 2065 | hv_stimer_global_cleanup(); |
| 2068 | vmbus_initiate_unload(false); | 2066 | vmbus_initiate_unload(false); |
| 2069 | vmbus_connection.conn_state = DISCONNECTED; | 2067 | vmbus_connection.conn_state = DISCONNECTED; |
| 2070 | /* Make sure conn_state is set as hv_synic_cleanup checks for it */ | 2068 | /* Make sure conn_state is set as hv_synic_cleanup checks for it */ |
| @@ -2075,6 +2073,8 @@ static void hv_kexec_handler(void) | |||
| 2075 | 2073 | ||
| 2076 | static void hv_crash_handler(struct pt_regs *regs) | 2074 | static void hv_crash_handler(struct pt_regs *regs) |
| 2077 | { | 2075 | { |
| 2076 | int cpu; | ||
| 2077 | |||
| 2078 | vmbus_initiate_unload(true); | 2078 | vmbus_initiate_unload(true); |
| 2079 | /* | 2079 | /* |
| 2080 | * In crash handler we can't schedule synic cleanup for all CPUs, | 2080 | * In crash handler we can't schedule synic cleanup for all CPUs, |
| @@ -2082,7 +2082,9 @@ static void hv_crash_handler(struct pt_regs *regs) | |||
| 2082 | * for kdump. | 2082 | * for kdump. |
| 2083 | */ | 2083 | */ |
| 2084 | vmbus_connection.conn_state = DISCONNECTED; | 2084 | vmbus_connection.conn_state = DISCONNECTED; |
| 2085 | hv_synic_cleanup(smp_processor_id()); | 2085 | cpu = smp_processor_id(); |
| 2086 | hv_stimer_cleanup(cpu); | ||
| 2087 | hv_synic_cleanup(cpu); | ||
| 2086 | hyperv_cleanup(); | 2088 | hyperv_cleanup(); |
| 2087 | }; | 2089 | }; |
| 2088 | 2090 | ||
| @@ -2131,7 +2133,7 @@ static void __exit vmbus_exit(void) | |||
| 2131 | hv_remove_kexec_handler(); | 2133 | hv_remove_kexec_handler(); |
| 2132 | hv_remove_crash_handler(); | 2134 | hv_remove_crash_handler(); |
| 2133 | vmbus_connection.conn_state = DISCONNECTED; | 2135 | vmbus_connection.conn_state = DISCONNECTED; |
| 2134 | hv_synic_clockevents_cleanup(); | 2136 | hv_stimer_global_cleanup(); |
| 2135 | vmbus_disconnect(); | 2137 | vmbus_disconnect(); |
| 2136 | hv_remove_vmbus_irq(); | 2138 | hv_remove_vmbus_irq(); |
| 2137 | for_each_online_cpu(cpu) { | 2139 | for_each_online_cpu(cpu) { |
diff --git a/include/clocksource/hyperv_timer.h b/include/clocksource/hyperv_timer.h new file mode 100644 index 000000000000..0cd73f7bc992 --- /dev/null +++ b/include/clocksource/hyperv_timer.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Definitions for the clocksource provided by the Hyper-V | ||
| 5 | * hypervisor to guest VMs, as described in the Hyper-V Top | ||
| 6 | * Level Functional Spec (TLFS). | ||
| 7 | * | ||
| 8 | * Copyright (C) 2019, Microsoft, Inc. | ||
| 9 | * | ||
| 10 | * Author: Michael Kelley <mikelley@microsoft.com> | ||
| 11 | */ | ||
| 12 | |||
| 13 | #ifndef __CLKSOURCE_HYPERV_TIMER_H | ||
| 14 | #define __CLKSOURCE_HYPERV_TIMER_H | ||
| 15 | |||
| 16 | #define HV_MAX_MAX_DELTA_TICKS 0xffffffff | ||
| 17 | #define HV_MIN_DELTA_TICKS 1 | ||
| 18 | |||
| 19 | /* Routines called by the VMbus driver */ | ||
| 20 | extern int hv_stimer_alloc(int sint); | ||
| 21 | extern void hv_stimer_free(void); | ||
| 22 | extern void hv_stimer_init(unsigned int cpu); | ||
| 23 | extern void hv_stimer_cleanup(unsigned int cpu); | ||
| 24 | extern void hv_stimer_global_cleanup(void); | ||
| 25 | extern void hv_stimer0_isr(void); | ||
| 26 | |||
| 27 | #endif | ||
