Merge tag 'vfs-6.7.misc' of gitolite.kernel.org:pub/scm/linux/kernel/git/vfs/vfs
[linux-block.git] / include / asm-generic / mshyperv.h
CommitLineData
765e33f5
MK
1/* 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>
ba3f5839 25#include <linux/nmi.h>
765e33f5
MK
26#include <asm/ptrace.h>
27#include <asm/hyperv-tlfs.h>
28
812b0597
MK
29#define VTPM_BASE_ADDRESS 0xfed40000
30
765e33f5
MK
31struct ms_hyperv_info {
32 u32 features;
6dc2a774 33 u32 priv_high;
765e33f5
MK
34 u32 misc_features;
35 u32 hints;
36 u32 nested_features;
37 u32 max_vp_index;
38 u32 max_lp_index;
cfc7461a 39 u8 vtl;
d6e2d652
TL
40 union {
41 u32 isolation_config_a;
42 struct {
43 u32 paravisor_present : 1;
44 u32 reserved_a1 : 31;
45 };
46 };
af788f35
TL
47 union {
48 u32 isolation_config_b;
49 struct {
50 u32 cvm_type : 4;
d6e2d652 51 u32 reserved_b1 : 1;
af788f35
TL
52 u32 shared_gpa_boundary_active : 1;
53 u32 shared_gpa_boundary_bits : 6;
d6e2d652 54 u32 reserved_b2 : 20;
af788f35
TL
55 };
56 };
57 u64 shared_gpa_boundary;
765e33f5
MK
58};
59extern struct ms_hyperv_info ms_hyperv;
c4bdf94f 60extern bool hv_nested;
765e33f5 61
db3c65bc
MK
62extern void * __percpu *hyperv_pcpu_input_arg;
63extern void * __percpu *hyperv_pcpu_output_arg;
afca4d95 64
765e33f5
MK
65extern u64 hv_do_hypercall(u64 control, void *inputaddr, void *outputaddr);
66extern u64 hv_do_fast_hypercall8(u16 control, u64 input8);
e3131f1c 67bool hv_isolation_type_snp(void);
08e9d120 68bool hv_isolation_type_tdx(void);
765e33f5 69
753ed9c9
JS
70/* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */
71static inline int hv_result(u64 status)
72{
73 return status & HV_HYPERCALL_RESULT_MASK;
74}
75
76static inline bool hv_result_success(u64 status)
77{
78 return hv_result(status) == HV_STATUS_SUCCESS;
79}
80
81static inline unsigned int hv_repcomp(u64 status)
82{
83 /* Bits [43:32] of status have 'Reps completed' data. */
84 return (status & HV_HYPERCALL_REP_COMP_MASK) >>
85 HV_HYPERCALL_REP_COMP_OFFSET;
86}
87
6523592c
JS
88/*
89 * Rep hypercalls. Callers of this functions are supposed to ensure that
90 * rep_count and varhead_size comply with Hyper-V hypercall definition.
91 */
92static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
93 void *input, void *output)
94{
95 u64 control = code;
96 u64 status;
97 u16 rep_comp;
98
99 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
100 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
101
102 do {
103 status = hv_do_hypercall(control, input, output);
753ed9c9 104 if (!hv_result_success(status))
6523592c
JS
105 return status;
106
753ed9c9 107 rep_comp = hv_repcomp(status);
6523592c
JS
108
109 control &= ~HV_HYPERCALL_REP_START_MASK;
110 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
111
112 touch_nmi_watchdog();
113 } while (rep_comp < rep_count);
114
115 return status;
116}
765e33f5
MK
117
118/* Generate the guest OS identifier as described in the Hyper-V TLFS */
d5ebde1e 119static inline u64 hv_generate_guest_id(u64 kernel_version)
765e33f5 120{
d5ebde1e 121 u64 guest_id;
765e33f5 122
d5ebde1e 123 guest_id = (((u64)HV_LINUX_VENDOR_ID) << 48);
765e33f5 124 guest_id |= (kernel_version << 16);
765e33f5
MK
125
126 return guest_id;
127}
128
765e33f5
MK
129/* Free the message slot and signal end-of-message if required */
130static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
131{
132 /*
133 * On crash we're reading some other CPU's message page and we need
134 * to be careful: this other CPU may already had cleared the header
135 * and the host may already had delivered some other message there.
136 * In case we blindly write msg->header.message_type we're going
137 * to lose it. We can still lose a message of the same type but
138 * we count on the fact that there can only be one
139 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
140 * on crash.
141 */
142 if (cmpxchg(&msg->header.message_type, old_msg_type,
143 HVMSG_NONE) != old_msg_type)
144 return;
145
146 /*
147 * The cmxchg() above does an implicit memory barrier to
148 * ensure the write to MessageType (ie set to
149 * HVMSG_NONE) happens before we read the
150 * MessagePending and EOMing. Otherwise, the EOMing
151 * will not deliver any more messages since there is
152 * no empty slot
153 */
154 if (msg->header.message_flags.msg_pending) {
155 /*
156 * This will cause message queue rescan to
157 * possibly deliver another msg from the
158 * hypervisor
159 */
f3c5e63c 160 hv_set_register(HV_REGISTER_EOM, 0);
765e33f5
MK
161 }
162}
163
d608715d
MK
164void hv_setup_vmbus_handler(void (*handler)(void));
165void hv_remove_vmbus_handler(void);
a620bbaa
MK
166void hv_setup_stimer0_handler(void (*handler)(void));
167void hv_remove_stimer0_handler(void);
765e33f5
MK
168
169void hv_setup_kexec_handler(void (*handler)(void));
170void hv_remove_kexec_handler(void);
171void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
172void hv_remove_crash_handler(void);
173
626b901f 174extern int vmbus_interrupt;
d608715d 175extern int vmbus_irq;
626b901f 176
afca4d95
MK
177extern bool hv_root_partition;
178
765e33f5
MK
179#if IS_ENABLED(CONFIG_HYPERV)
180/*
181 * Hypervisor's notion of virtual processor ID is different from
182 * Linux' notion of CPU ID. This information can only be retrieved
183 * in the context of the calling CPU. Setup a map for easy access
184 * to this information.
185 */
186extern u32 *hv_vp_index;
187extern u32 hv_max_vp_index;
188
31e5e646
MK
189extern u64 (*hv_read_reference_counter)(void);
190
765e33f5
MK
191/* Sentinel value for an uninitialized entry in hv_vp_index array */
192#define VP_INVAL U32_MAX
193
afca4d95
MK
194int __init hv_common_init(void);
195void __init hv_common_free(void);
196int hv_common_cpu_init(unsigned int cpu);
197int hv_common_cpu_die(unsigned int cpu);
198
ca48739e
MK
199void *hv_alloc_hyperv_page(void);
200void *hv_alloc_hyperv_zeroed_page(void);
a6fe0438 201void hv_free_hyperv_page(void *addr);
ca48739e 202
765e33f5
MK
203/**
204 * hv_cpu_number_to_vp_number() - Map CPU to VP.
205 * @cpu_number: CPU number in Linux terms
206 *
207 * This function returns the mapping between the Linux processor
208 * number and the hypervisor's virtual processor number, useful
209 * in making hypercalls and such that talk about specific
210 * processors.
211 *
212 * Return: Virtual processor number in Hyper-V terms
213 */
214static inline int hv_cpu_number_to_vp_number(int cpu_number)
215{
216 return hv_vp_index[cpu_number];
217}
218
7ad9bb9d
WL
219static inline int __cpumask_to_vpset(struct hv_vpset *vpset,
220 const struct cpumask *cpus,
d7b6ba96 221 bool (*func)(int cpu))
765e33f5
MK
222{
223 int cpu, vcpu, vcpu_bank, vcpu_offset, nr_bank = 1;
bd19c94a 224 int max_vcpu_bank = hv_max_vp_index / HV_VCPUS_PER_SPARSE_BANK;
765e33f5 225
bd19c94a
VK
226 /* vpset.valid_bank_mask can represent up to HV_MAX_SPARSE_VCPU_BANKS banks */
227 if (max_vcpu_bank >= HV_MAX_SPARSE_VCPU_BANKS)
765e33f5
MK
228 return 0;
229
230 /*
231 * Clear all banks up to the maximum possible bank as hv_tlb_flush_ex
232 * structs are not cleared between calls, we risk flushing unneeded
233 * vCPUs otherwise.
234 */
bd19c94a 235 for (vcpu_bank = 0; vcpu_bank <= max_vcpu_bank; vcpu_bank++)
765e33f5
MK
236 vpset->bank_contents[vcpu_bank] = 0;
237
238 /*
239 * Some banks may end up being empty but this is acceptable.
240 */
241 for_each_cpu(cpu, cpus) {
d7b6ba96 242 if (func && func(cpu))
7ad9bb9d 243 continue;
765e33f5
MK
244 vcpu = hv_cpu_number_to_vp_number(cpu);
245 if (vcpu == VP_INVAL)
246 return -1;
bd19c94a
VK
247 vcpu_bank = vcpu / HV_VCPUS_PER_SPARSE_BANK;
248 vcpu_offset = vcpu % HV_VCPUS_PER_SPARSE_BANK;
765e33f5
MK
249 __set_bit(vcpu_offset, (unsigned long *)
250 &vpset->bank_contents[vcpu_bank]);
251 if (vcpu_bank >= nr_bank)
252 nr_bank = vcpu_bank + 1;
253 }
254 vpset->valid_bank_mask = GENMASK_ULL(nr_bank - 1, 0);
255 return nr_bank;
256}
257
d7b6ba96
MK
258/*
259 * Convert a Linux cpumask into a Hyper-V VPset. In the _skip variant,
260 * 'func' is called for each CPU present in cpumask. If 'func' returns
261 * true, that CPU is skipped -- i.e., that CPU from cpumask is *not*
262 * added to the Hyper-V VPset. If 'func' is NULL, no CPUs are
263 * skipped.
264 */
7ad9bb9d
WL
265static inline int cpumask_to_vpset(struct hv_vpset *vpset,
266 const struct cpumask *cpus)
267{
d7b6ba96 268 return __cpumask_to_vpset(vpset, cpus, NULL);
7ad9bb9d
WL
269}
270
d7b6ba96
MK
271static inline int cpumask_to_vpset_skip(struct hv_vpset *vpset,
272 const struct cpumask *cpus,
273 bool (*func)(int cpu))
7ad9bb9d 274{
d7b6ba96 275 return __cpumask_to_vpset(vpset, cpus, func);
7ad9bb9d
WL
276}
277
f3a99e76 278void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die);
765e33f5 279bool hv_is_hyperv_initialized(void);
b96f8653 280bool hv_is_hibernation_supported(void);
a6c76bb0
APM
281enum hv_isolation_type hv_get_isolation_type(void);
282bool hv_is_isolation_supported(void);
0cc4f6d9 283bool hv_isolation_type_snp(void);
20c89a55 284u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size);
d6e0228d 285u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
765e33f5 286void hyperv_cleanup(void);
6dc2a774 287bool hv_query_ext_cap(u64 cap_query);
37200078 288void hv_setup_dma_ops(struct device *dev, bool coherent);
765e33f5
MK
289#else /* CONFIG_HYPERV */
290static inline bool hv_is_hyperv_initialized(void) { return false; }
b96f8653 291static inline bool hv_is_hibernation_supported(void) { return false; }
765e33f5 292static inline void hyperv_cleanup(void) {}
0cc4f6d9
TL
293static inline bool hv_is_isolation_supported(void) { return false; }
294static inline enum hv_isolation_type hv_get_isolation_type(void)
295{
296 return HV_ISOLATION_TYPE_NONE;
297}
765e33f5
MK
298#endif /* CONFIG_HYPERV */
299
765e33f5 300#endif