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);
66 rdmsr(HV_X64_MSR_TPR, reg_val, hi);
71 return native_apic_mem_read(reg);
75 static void hv_apic_write(u32 reg, u32 val)
79 wrmsr(HV_X64_MSR_EOI, val, 0);
82 wrmsr(HV_X64_MSR_TPR, val, 0);
85 native_apic_mem_write(reg, val);
89 static void hv_apic_eoi_write(u32 reg, u32 val)
91 struct hv_vp_assist_page *hvp = hv_vp_assist_page[smp_processor_id()];
93 if (hvp && (xchg(&hvp->apic_assist, 0) & 0x1))
96 wrmsr(HV_X64_MSR_EOI, val, 0);
100 * IPI implementation on Hyper-V.
102 static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector,
105 struct hv_send_ipi_ex **arg;
106 struct hv_send_ipi_ex *ipi_arg;
109 u64 status = HV_STATUS_INVALID_PARAMETER;
111 if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
114 local_irq_save(flags);
115 arg = (struct hv_send_ipi_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
118 if (unlikely(!ipi_arg))
119 goto ipi_mask_ex_done;
121 ipi_arg->vector = vector;
122 ipi_arg->reserved = 0;
123 ipi_arg->vp_set.valid_bank_mask = 0;
125 if (!cpumask_equal(mask, cpu_present_mask)) {
126 ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
128 nr_bank = cpumask_to_vpset_noself(&(ipi_arg->vp_set), mask);
130 nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask);
133 goto ipi_mask_ex_done;
135 ipi_arg->vp_set.format = HV_GENERIC_SET_ALL;
137 status = hv_do_rep_hypercall(HVCALL_SEND_IPI_EX, 0, nr_bank,
141 local_irq_restore(flags);
142 return hv_result_success(status);
145 static bool __send_ipi_mask(const struct cpumask *mask, int vector,
148 int cur_cpu, vcpu, this_cpu = smp_processor_id();
149 struct hv_send_ipi ipi_arg;
153 trace_hyperv_send_ipi_mask(mask, vector);
155 weight = cpumask_weight(mask);
159 * 1. the mask is empty
160 * 2. the mask only contains self when exclude_self is true
163 (exclude_self && weight == 1 && cpumask_test_cpu(this_cpu, mask)))
166 if (!hv_hypercall_pg)
169 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
173 * From the supplied CPU set we need to figure out if we can get away
174 * with cheaper HVCALL_SEND_IPI hypercall. This is possible when the
175 * highest VP number in the set is < 64. As VP numbers are usually in
176 * ascending order and match Linux CPU ids, here is an optimization:
177 * we check the VP number for the highest bit in the supplied set first
178 * so we can quickly find out if using HVCALL_SEND_IPI_EX hypercall is
179 * a must. We will also check all VP numbers when walking the supplied
180 * CPU set to remain correct in all cases.
182 if (hv_cpu_number_to_vp_number(cpumask_last(mask)) >= 64)
183 goto do_ex_hypercall;
185 ipi_arg.vector = vector;
186 ipi_arg.cpu_mask = 0;
188 for_each_cpu(cur_cpu, mask) {
189 if (exclude_self && cur_cpu == this_cpu)
191 vcpu = hv_cpu_number_to_vp_number(cur_cpu);
192 if (vcpu == VP_INVAL)
196 * This particular version of the IPI hypercall can
197 * only target upto 64 CPUs.
200 goto do_ex_hypercall;
202 __set_bit(vcpu, (unsigned long *)&ipi_arg.cpu_mask);
205 status = hv_do_fast_hypercall16(HVCALL_SEND_IPI, ipi_arg.vector,
207 return hv_result_success(status);
210 return __send_ipi_mask_ex(mask, vector, exclude_self);
213 static bool __send_ipi_one(int cpu, int vector)
215 int vp = hv_cpu_number_to_vp_number(cpu);
218 trace_hyperv_send_ipi_one(cpu, vector);
220 if (!hv_hypercall_pg || (vp == VP_INVAL))
223 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
227 return __send_ipi_mask_ex(cpumask_of(cpu), vector, false);
229 status = hv_do_fast_hypercall16(HVCALL_SEND_IPI, vector, BIT_ULL(vp));
230 return hv_result_success(status);
233 static void hv_send_ipi(int cpu, int vector)
235 if (!__send_ipi_one(cpu, vector))
236 orig_apic.send_IPI(cpu, vector);
239 static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
241 if (!__send_ipi_mask(mask, vector, false))
242 orig_apic.send_IPI_mask(mask, vector);
245 static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
247 if (!__send_ipi_mask(mask, vector, true))
248 orig_apic.send_IPI_mask_allbutself(mask, vector);
251 static void hv_send_ipi_allbutself(int vector)
253 hv_send_ipi_mask_allbutself(cpu_online_mask, vector);
256 static void hv_send_ipi_all(int vector)
258 if (!__send_ipi_mask(cpu_online_mask, vector, false))
259 orig_apic.send_IPI_all(vector);
262 static void hv_send_ipi_self(int vector)
264 if (!__send_ipi_one(smp_processor_id(), vector))
265 orig_apic.send_IPI_self(vector);
268 void __init hv_apic_init(void)
270 if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) {
271 pr_info("Hyper-V: Using IPI hypercalls\n");
273 * Set the IPI entry points.
277 apic->send_IPI = hv_send_ipi;
278 apic->send_IPI_mask = hv_send_ipi_mask;
279 apic->send_IPI_mask_allbutself = hv_send_ipi_mask_allbutself;
280 apic->send_IPI_allbutself = hv_send_ipi_allbutself;
281 apic->send_IPI_all = hv_send_ipi_all;
282 apic->send_IPI_self = hv_send_ipi_self;
285 if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) {
286 pr_info("Hyper-V: Using enlightened APIC (%s mode)",
287 x2apic_enabled() ? "x2apic" : "xapic");
289 * When in x2apic mode, don't use the Hyper-V specific APIC
290 * accessors since the field layout in the ICR register is
291 * different in x2apic mode. Furthermore, the architectural
292 * x2apic MSRs function just as well as the Hyper-V
293 * synthetic APIC MSRs, so there's no benefit in having
294 * separate Hyper-V accessors for x2apic mode. The only
295 * exception is hv_apic_eoi_write, because it benefits from
296 * lazy EOI when available, but the same accessor works for
297 * both xapic and x2apic because the field layout is the same.
299 apic_set_eoi_write(hv_apic_eoi_write);
300 if (!x2apic_enabled()) {
301 apic->read = hv_apic_read;
302 apic->write = hv_apic_write;
303 apic->icr_write = hv_apic_icr_write;
304 apic->icr_read = hv_apic_icr_read;