blob: dff58a3db5d5c9f3e5a6b20337ea826c3759f7aa [file] [log] [blame]
Michael Kelley765e33f2019-05-30 00:14:00 +00001/* SPDX-License-Identifier: GPL-2.0 */
2
3/*
4 * Linux-specific definitions for managing interactions with Microsoft's
5 * Hyper-V hypervisor. The definitions in this file are architecture
6 * independent. See arch/<arch>/include/asm/mshyperv.h for definitions
7 * that are specific to architecture <arch>.
8 *
9 * Definitions that are specified in the Hyper-V Top Level Functional
10 * Spec (TLFS) should not go in this file, but should instead go in
11 * hyperv-tlfs.h.
12 *
13 * Copyright (C) 2019, Microsoft, Inc.
14 *
15 * Author : Michael Kelley <mikelley@microsoft.com>
16 */
17
18#ifndef _ASM_GENERIC_MSHYPERV_H
19#define _ASM_GENERIC_MSHYPERV_H
20
21#include <linux/types.h>
22#include <linux/atomic.h>
23#include <linux/bitops.h>
24#include <linux/cpumask.h>
25#include <asm/ptrace.h>
26#include <asm/hyperv-tlfs.h>
27
28struct ms_hyperv_info {
29 u32 features;
Andrea Parri (Microsoft)a6c76bb02021-02-01 15:48:11 +010030 u32 features_b;
Michael Kelley765e33f2019-05-30 00:14:00 +000031 u32 misc_features;
32 u32 hints;
33 u32 nested_features;
34 u32 max_vp_index;
35 u32 max_lp_index;
Andrea Parri (Microsoft)a6c76bb02021-02-01 15:48:11 +010036 u32 isolation_config_a;
37 u32 isolation_config_b;
Michael Kelley765e33f2019-05-30 00:14:00 +000038};
39extern struct ms_hyperv_info ms_hyperv;
40
41extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
42extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
43
44
45/* Generate the guest OS identifier as described in the Hyper-V TLFS */
46static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
47 __u64 d_info2)
48{
49 __u64 guest_id = 0;
50
51 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
52 guest_id |= (d_info1 << 48);
53 guest_id |= (kernel_version << 16);
54 guest_id |= d_info2;
55
56 return guest_id;
57}
58
59
60/* Free the message slot and signal end-of-message if required */
61static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
62{
63 /*
64 * On crash we're reading some other CPU's message page and we need
65 * to be careful: this other CPU may already had cleared the header
66 * and the host may already had delivered some other message there.
67 * In case we blindly write msg->header.message_type we're going
68 * to lose it. We can still lose a message of the same type but
69 * we count on the fact that there can only be one
70 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
71 * on crash.
72 */
73 if (cmpxchg(&msg->header.message_type, old_msg_type,
74 HVMSG_NONE) != old_msg_type)
75 return;
76
77 /*
78 * The cmxchg() above does an implicit memory barrier to
79 * ensure the write to MessageType (ie set to
80 * HVMSG_NONE) happens before we read the
81 * MessagePending and EOMing. Otherwise, the EOMing
82 * will not deliver any more messages since there is
83 * no empty slot
84 */
85 if (msg->header.message_flags.msg_pending) {
86 /*
87 * This will cause message queue rescan to
88 * possibly deliver another msg from the
89 * hypervisor
90 */
91 hv_signal_eom();
92 }
93}
94
Michael Kelley626b9012020-08-14 12:45:04 -070095int hv_setup_vmbus_irq(int irq, void (*handler)(void));
Michael Kelley765e33f2019-05-30 00:14:00 +000096void hv_remove_vmbus_irq(void);
97void hv_enable_vmbus_irq(void);
98void hv_disable_vmbus_irq(void);
99
100void hv_setup_kexec_handler(void (*handler)(void));
101void hv_remove_kexec_handler(void);
102void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
103void hv_remove_crash_handler(void);
104
Michael Kelley626b9012020-08-14 12:45:04 -0700105extern int vmbus_interrupt;
106
Michael Kelley765e33f2019-05-30 00:14:00 +0000107#if IS_ENABLED(CONFIG_HYPERV)
108/*
109 * Hypervisor's notion of virtual processor ID is different from
110 * Linux' notion of CPU ID. This information can only be retrieved
111 * in the context of the calling CPU. Setup a map for easy access
112 * to this information.
113 */
114extern u32 *hv_vp_index;
115extern u32 hv_max_vp_index;
116
117/* Sentinel value for an uninitialized entry in hv_vp_index array */
118#define VP_INVAL U32_MAX
119
120/**
121 * hv_cpu_number_to_vp_number() - Map CPU to VP.
122 * @cpu_number: CPU number in Linux terms
123 *
124 * This function returns the mapping between the Linux processor
125 * number and the hypervisor's virtual processor number, useful
126 * in making hypercalls and such that talk about specific
127 * processors.
128 *
129 * Return: Virtual processor number in Hyper-V terms
130 */
131static inline int hv_cpu_number_to_vp_number(int cpu_number)
132{
133 return hv_vp_index[cpu_number];
134}
135
136static inline int cpumask_to_vpset(struct hv_vpset *vpset,
137 const struct cpumask *cpus)
138{
139 int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
140
141 /* valid_bank_mask can represent up to 64 banks */
142 if (hv_max_vp_index / 64 >= 64)
143 return 0;
144
145 /*
146 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
147 * structs are not cleared between calls, we risk flushing unneeded
148 * vCPUs otherwise.
149 */
150 for (vcpu_bank = 0; vcpu_bank <= hv_max_vp_index / 64; vcpu_bank++)
151 vpset->bank_contents[vcpu_bank] = 0;
152
153 /*
154 * Some banks may end up being empty but this is acceptable.
155 */
156 for_each_cpu(cpu, cpus) {
157 vcpu = hv_cpu_number_to_vp_number(cpu);
158 if (vcpu == VP_INVAL)
159 return -1;
160 vcpu_bank = vcpu / 64;
161 vcpu_offset = vcpu % 64;
162 __set_bit(vcpu_offset, (unsigned long *)
163 &vpset->bank_contents[vcpu_bank]);
164 if (vcpu_bank >= nr_bank)
165 nr_bank = vcpu_bank + 1;
166 }
167 vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
168 return nr_bank;
169}
170
Tianyu Lanf3a99e72020-04-06 08:53:31 -0700171void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
Michael Kelley765e33f2019-05-30 00:14:00 +0000172void hyperv_report_panic_msg(phys_addr_t pa, size_t size);
173bool hv_is_hyperv_initialized(void);
Dexuan Cuib96f8652019-11-19 23:16:04 -0800174bool hv_is_hibernation_supported(void);
Andrea Parri (Microsoft)a6c76bb02021-02-01 15:48:11 +0100175enum hv_isolation_type hv_get_isolation_type(void);
176bool hv_is_isolation_supported(void);
Michael Kelley765e33f2019-05-30 00:14:00 +0000177void hyperv_cleanup(void);
Michael Kelley765e33f2019-05-30 00:14:00 +0000178#else /* CONFIG_HYPERV */
179static inline bool hv_is_hyperv_initialized(void) { return false; }
Dexuan Cuib96f8652019-11-19 23:16:04 -0800180static inline bool hv_is_hibernation_supported(void) { return false; }
Michael Kelley765e33f2019-05-30 00:14:00 +0000181static inline void hyperv_cleanup(void) {}
182#endif /* CONFIG_HYPERV */
183
184#if IS_ENABLED(CONFIG_HYPERV)
185extern int hv_setup_stimer0_irq(int *irq, int *vector, void (*handler)(void));
186extern void hv_remove_stimer0_irq(int irq);
187#endif
188
189#endif