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/version.h>
24 #include <linux/vmalloc.h>
26 #include <linux/clockchips.h>
27 #include <linux/hyperv.h>
28 #include <linux/slab.h>
29 #include <linux/cpuhotplug.h>
30 #include <asm/hypervisor.h>
31 #include <asm/mshyperv.h>
34 #if IS_ENABLED(CONFIG_HYPERV)
36 static struct apic orig_apic;
38 static u64 hv_apic_icr_read(void)
42 rdmsrl(HV_X64_MSR_ICR, reg_val);
46 static void hv_apic_icr_write(u32 low, u32 id)
50 reg_val = SET_APIC_DEST_FIELD(id);
51 reg_val = reg_val << 32;
54 wrmsrl(HV_X64_MSR_ICR, reg_val);
57 static u32 hv_apic_read(u32 reg)
63 rdmsr(HV_X64_MSR_EOI, reg_val, hi);
66 rdmsr(HV_X64_MSR_TPR, reg_val, hi);
70 return native_apic_mem_read(reg);
74 static void hv_apic_write(u32 reg, u32 val)
78 wrmsr(HV_X64_MSR_EOI, val, 0);
81 wrmsr(HV_X64_MSR_TPR, val, 0);
84 native_apic_mem_write(reg, val);
88 static void hv_apic_eoi_write(u32 reg, u32 val)
90 wrmsr(HV_X64_MSR_EOI, val, 0);
94 * IPI implementation on Hyper-V.
96 static bool __send_ipi_mask(const struct cpumask *mask, int vector)
99 struct ipi_arg_non_ex **arg;
100 struct ipi_arg_non_ex *ipi_arg;
104 if (cpumask_empty(mask))
107 if (!hv_hypercall_pg)
110 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
113 local_irq_save(flags);
114 arg = (struct ipi_arg_non_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
117 if (unlikely(!ipi_arg))
120 ipi_arg->vector = vector;
121 ipi_arg->reserved = 0;
122 ipi_arg->cpu_mask = 0;
124 for_each_cpu(cur_cpu, mask) {
125 vcpu = hv_cpu_number_to_vp_number(cur_cpu);
127 * This particular version of the IPI hypercall can
128 * only target upto 64 CPUs.
133 __set_bit(vcpu, (unsigned long *)&ipi_arg->cpu_mask);
136 ret = hv_do_hypercall(HVCALL_SEND_IPI, ipi_arg, NULL);
139 local_irq_restore(flags);
140 return ((ret == 0) ? true : false);
143 static bool __send_ipi_one(int cpu, int vector)
145 struct cpumask mask = CPU_MASK_NONE;
147 cpumask_set_cpu(cpu, &mask);
148 return __send_ipi_mask(&mask, vector);
151 static void hv_send_ipi(int cpu, int vector)
153 if (!__send_ipi_one(cpu, vector))
154 orig_apic.send_IPI(cpu, vector);
157 static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
159 if (!__send_ipi_mask(mask, vector))
160 orig_apic.send_IPI_mask(mask, vector);
163 static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
165 unsigned int this_cpu = smp_processor_id();
166 struct cpumask new_mask;
167 const struct cpumask *local_mask;
169 cpumask_copy(&new_mask, mask);
170 cpumask_clear_cpu(this_cpu, &new_mask);
171 local_mask = &new_mask;
172 if (!__send_ipi_mask(local_mask, vector))
173 orig_apic.send_IPI_mask_allbutself(mask, vector);
176 static void hv_send_ipi_allbutself(int vector)
178 hv_send_ipi_mask_allbutself(cpu_online_mask, vector);
181 static void hv_send_ipi_all(int vector)
183 if (!__send_ipi_mask(cpu_online_mask, vector))
184 orig_apic.send_IPI_all(vector);
187 static void hv_send_ipi_self(int vector)
189 if (!__send_ipi_one(smp_processor_id(), vector))
190 orig_apic.send_IPI_self(vector);
193 void __init hv_apic_init(void)
195 if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) {
196 pr_info("Hyper-V: Using IPI hypercalls\n");
198 * Set the IPI entry points.
202 apic->send_IPI = hv_send_ipi;
203 apic->send_IPI_mask = hv_send_ipi_mask;
204 apic->send_IPI_mask_allbutself = hv_send_ipi_mask_allbutself;
205 apic->send_IPI_allbutself = hv_send_ipi_allbutself;
206 apic->send_IPI_all = hv_send_ipi_all;
207 apic->send_IPI_self = hv_send_ipi_self;
210 if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) {
211 pr_info("Hyper-V: Using MSR based APIC access\n");
212 apic_set_eoi_write(hv_apic_eoi_write);
213 apic->read = hv_apic_read;
214 apic->write = hv_apic_write;
215 apic->icr_write = hv_apic_icr_write;
216 apic->icr_read = hv_apic_icr_read;
220 #endif /* CONFIG_HYPERV */
221 #endif /* CONFIG_X86_64 */