1 // SPDX-License-Identifier: GPL-2.0
4 * Hyper-V specific APIC code.
6 * Copyright (C) 2018, Microsoft, Inc.
8 * Author : K. Y. Srinivasan <kys@microsoft.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
17 * NON INFRINGEMENT. See the GNU General Public License for more
22 #include <linux/types.h>
23 #include <linux/vmalloc.h>
25 #include <linux/clockchips.h>
26 #include <linux/hyperv.h>
27 #include <linux/slab.h>
28 #include <linux/cpuhotplug.h>
29 #include <asm/hypervisor.h>
30 #include <asm/mshyperv.h>
33 #include <asm/trace/hyperv.h>
35 static struct apic orig_apic
;
37 static u64
hv_apic_icr_read(void)
41 rdmsrl(HV_X64_MSR_ICR
, reg_val
);
45 static void hv_apic_icr_write(u32 low
, u32 id
)
49 reg_val
= SET_APIC_DEST_FIELD(id
);
50 reg_val
= reg_val
<< 32;
53 wrmsrl(HV_X64_MSR_ICR
, reg_val
);
56 static u32
hv_apic_read(u32 reg
)
62 rdmsr(HV_X64_MSR_EOI
, reg_val
, hi
);
65 rdmsr(HV_X64_MSR_TPR
, reg_val
, hi
);
69 return native_apic_mem_read(reg
);
73 static void hv_apic_write(u32 reg
, u32 val
)
77 wrmsr(HV_X64_MSR_EOI
, val
, 0);
80 wrmsr(HV_X64_MSR_TPR
, val
, 0);
83 native_apic_mem_write(reg
, val
);
87 static void hv_apic_eoi_write(u32 reg
, u32 val
)
89 wrmsr(HV_X64_MSR_EOI
, val
, 0);
93 * IPI implementation on Hyper-V.
95 static bool __send_ipi_mask_ex(const struct cpumask
*mask
, int vector
)
97 struct hv_send_ipi_ex
**arg
;
98 struct hv_send_ipi_ex
*ipi_arg
;
103 if (!(ms_hyperv
.hints
& HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
))
106 local_irq_save(flags
);
107 arg
= (struct hv_send_ipi_ex
**)this_cpu_ptr(hyperv_pcpu_input_arg
);
110 if (unlikely(!ipi_arg
))
111 goto ipi_mask_ex_done
;
113 ipi_arg
->vector
= vector
;
114 ipi_arg
->reserved
= 0;
115 ipi_arg
->vp_set
.valid_bank_mask
= 0;
117 if (!cpumask_equal(mask
, cpu_present_mask
)) {
118 ipi_arg
->vp_set
.format
= HV_GENERIC_SET_SPARSE_4K
;
119 nr_bank
= cpumask_to_vpset(&(ipi_arg
->vp_set
), mask
);
122 goto ipi_mask_ex_done
;
124 ipi_arg
->vp_set
.format
= HV_GENERIC_SET_ALL
;
126 ret
= hv_do_rep_hypercall(HVCALL_SEND_IPI_EX
, 0, nr_bank
,
130 local_irq_restore(flags
);
131 return ((ret
== 0) ? true : false);
134 static bool __send_ipi_mask(const struct cpumask
*mask
, int vector
)
137 struct hv_send_ipi ipi_arg
;
140 trace_hyperv_send_ipi_mask(mask
, vector
);
142 if (cpumask_empty(mask
))
145 if (!hv_hypercall_pg
)
148 if ((vector
< HV_IPI_LOW_VECTOR
) || (vector
> HV_IPI_HIGH_VECTOR
))
152 * From the supplied CPU set we need to figure out if we can get away
153 * with cheaper HVCALL_SEND_IPI hypercall. This is possible when the
154 * highest VP number in the set is < 64. As VP numbers are usually in
155 * ascending order and match Linux CPU ids, here is an optimization:
156 * we check the VP number for the highest bit in the supplied set first
157 * so we can quickly find out if using HVCALL_SEND_IPI_EX hypercall is
158 * a must. We will also check all VP numbers when walking the supplied
159 * CPU set to remain correct in all cases.
161 if (hv_cpu_number_to_vp_number(cpumask_last(mask
)) >= 64)
162 goto do_ex_hypercall
;
164 ipi_arg
.vector
= vector
;
165 ipi_arg
.cpu_mask
= 0;
167 for_each_cpu(cur_cpu
, mask
) {
168 vcpu
= hv_cpu_number_to_vp_number(cur_cpu
);
169 if (vcpu
== VP_INVAL
)
173 * This particular version of the IPI hypercall can
174 * only target upto 64 CPUs.
177 goto do_ex_hypercall
;
179 __set_bit(vcpu
, (unsigned long *)&ipi_arg
.cpu_mask
);
182 ret
= hv_do_fast_hypercall16(HVCALL_SEND_IPI
, ipi_arg
.vector
,
184 return ((ret
== 0) ? true : false);
187 return __send_ipi_mask_ex(mask
, vector
);
190 static bool __send_ipi_one(int cpu
, int vector
)
192 struct cpumask mask
= CPU_MASK_NONE
;
194 cpumask_set_cpu(cpu
, &mask
);
195 return __send_ipi_mask(&mask
, vector
);
198 static void hv_send_ipi(int cpu
, int vector
)
200 if (!__send_ipi_one(cpu
, vector
))
201 orig_apic
.send_IPI(cpu
, vector
);
204 static void hv_send_ipi_mask(const struct cpumask
*mask
, int vector
)
206 if (!__send_ipi_mask(mask
, vector
))
207 orig_apic
.send_IPI_mask(mask
, vector
);
210 static void hv_send_ipi_mask_allbutself(const struct cpumask
*mask
, int vector
)
212 unsigned int this_cpu
= smp_processor_id();
213 struct cpumask new_mask
;
214 const struct cpumask
*local_mask
;
216 cpumask_copy(&new_mask
, mask
);
217 cpumask_clear_cpu(this_cpu
, &new_mask
);
218 local_mask
= &new_mask
;
219 if (!__send_ipi_mask(local_mask
, vector
))
220 orig_apic
.send_IPI_mask_allbutself(mask
, vector
);
223 static void hv_send_ipi_allbutself(int vector
)
225 hv_send_ipi_mask_allbutself(cpu_online_mask
, vector
);
228 static void hv_send_ipi_all(int vector
)
230 if (!__send_ipi_mask(cpu_online_mask
, vector
))
231 orig_apic
.send_IPI_all(vector
);
234 static void hv_send_ipi_self(int vector
)
236 if (!__send_ipi_one(smp_processor_id(), vector
))
237 orig_apic
.send_IPI_self(vector
);
240 void __init
hv_apic_init(void)
242 if (ms_hyperv
.hints
& HV_X64_CLUSTER_IPI_RECOMMENDED
) {
243 pr_info("Hyper-V: Using IPI hypercalls\n");
245 * Set the IPI entry points.
249 apic
->send_IPI
= hv_send_ipi
;
250 apic
->send_IPI_mask
= hv_send_ipi_mask
;
251 apic
->send_IPI_mask_allbutself
= hv_send_ipi_mask_allbutself
;
252 apic
->send_IPI_allbutself
= hv_send_ipi_allbutself
;
253 apic
->send_IPI_all
= hv_send_ipi_all
;
254 apic
->send_IPI_self
= hv_send_ipi_self
;
257 if (ms_hyperv
.hints
& HV_X64_APIC_ACCESS_RECOMMENDED
) {
258 pr_info("Hyper-V: Using MSR based APIC access\n");
259 apic_set_eoi_write(hv_apic_eoi_write
);
260 apic
->read
= hv_apic_read
;
261 apic
->write
= hv_apic_write
;
262 apic
->icr_write
= hv_apic_icr_write
;
263 apic
->icr_read
= hv_apic_icr_read
;