K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Hyper-V specific APIC code. |
| 5 | * |
| 6 | * Copyright (C) 2018, Microsoft, Inc. |
| 7 | * |
| 8 | * Author : K. Y. Srinivasan <kys@microsoft.com> |
| 9 | * |
| 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. |
| 13 | * |
| 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 |
| 18 | * details. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <linux/types.h> |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/mm.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> |
Thomas Gleixner | 61eeb1f | 2018-05-19 16:38:59 +0200 | [diff] [blame] | 31 | #include <asm/apic.h> |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 32 | |
Vitaly Kuznetsov | 58ec5e9 | 2018-06-22 19:06:25 +0200 | [diff] [blame] | 33 | #include <asm/trace/hyperv.h> |
| 34 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 35 | static struct apic orig_apic; |
| 36 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 37 | static u64 hv_apic_icr_read(void) |
| 38 | { |
| 39 | u64 reg_val; |
| 40 | |
| 41 | rdmsrl(HV_X64_MSR_ICR, reg_val); |
| 42 | return reg_val; |
| 43 | } |
| 44 | |
| 45 | static void hv_apic_icr_write(u32 low, u32 id) |
| 46 | { |
| 47 | u64 reg_val; |
| 48 | |
| 49 | reg_val = SET_APIC_DEST_FIELD(id); |
| 50 | reg_val = reg_val << 32; |
| 51 | reg_val |= low; |
| 52 | |
| 53 | wrmsrl(HV_X64_MSR_ICR, reg_val); |
| 54 | } |
| 55 | |
| 56 | static u32 hv_apic_read(u32 reg) |
| 57 | { |
| 58 | u32 reg_val, hi; |
| 59 | |
| 60 | switch (reg) { |
| 61 | case APIC_EOI: |
| 62 | rdmsr(HV_X64_MSR_EOI, reg_val, hi); |
| 63 | return reg_val; |
| 64 | case APIC_TASKPRI: |
| 65 | rdmsr(HV_X64_MSR_TPR, reg_val, hi); |
| 66 | return reg_val; |
| 67 | |
| 68 | default: |
| 69 | return native_apic_mem_read(reg); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void hv_apic_write(u32 reg, u32 val) |
| 74 | { |
| 75 | switch (reg) { |
| 76 | case APIC_EOI: |
| 77 | wrmsr(HV_X64_MSR_EOI, val, 0); |
| 78 | break; |
| 79 | case APIC_TASKPRI: |
| 80 | wrmsr(HV_X64_MSR_TPR, val, 0); |
| 81 | break; |
| 82 | default: |
| 83 | native_apic_mem_write(reg, val); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void hv_apic_eoi_write(u32 reg, u32 val) |
| 88 | { |
Vitaly Kuznetsov | ba69642 | 2019-04-03 19:03:09 +0200 | [diff] [blame] | 89 | struct hv_vp_assist_page *hvp = hv_vp_assist_page[smp_processor_id()]; |
| 90 | |
| 91 | if (hvp && (xchg(&hvp->apic_assist, 0) & 0x1)) |
| 92 | return; |
| 93 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 94 | wrmsr(HV_X64_MSR_EOI, val, 0); |
| 95 | } |
| 96 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 97 | /* |
| 98 | * IPI implementation on Hyper-V. |
| 99 | */ |
K. Y. Srinivasan | 366f03b | 2018-05-16 14:53:32 -0700 | [diff] [blame] | 100 | static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector) |
| 101 | { |
Vitaly Kuznetsov | a1efa9b | 2018-08-27 18:48:57 +0200 | [diff] [blame] | 102 | struct hv_send_ipi_ex **arg; |
| 103 | struct hv_send_ipi_ex *ipi_arg; |
K. Y. Srinivasan | 366f03b | 2018-05-16 14:53:32 -0700 | [diff] [blame] | 104 | unsigned long flags; |
| 105 | int nr_bank = 0; |
| 106 | int ret = 1; |
| 107 | |
Vitaly Kuznetsov | 4bd0606 | 2018-06-22 19:06:24 +0200 | [diff] [blame] | 108 | if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED)) |
| 109 | return false; |
| 110 | |
K. Y. Srinivasan | 366f03b | 2018-05-16 14:53:32 -0700 | [diff] [blame] | 111 | local_irq_save(flags); |
Vitaly Kuznetsov | a1efa9b | 2018-08-27 18:48:57 +0200 | [diff] [blame] | 112 | arg = (struct hv_send_ipi_ex **)this_cpu_ptr(hyperv_pcpu_input_arg); |
K. Y. Srinivasan | 366f03b | 2018-05-16 14:53:32 -0700 | [diff] [blame] | 113 | |
| 114 | ipi_arg = *arg; |
| 115 | if (unlikely(!ipi_arg)) |
| 116 | goto ipi_mask_ex_done; |
| 117 | |
| 118 | ipi_arg->vector = vector; |
| 119 | ipi_arg->reserved = 0; |
| 120 | ipi_arg->vp_set.valid_bank_mask = 0; |
| 121 | |
| 122 | if (!cpumask_equal(mask, cpu_present_mask)) { |
| 123 | ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K; |
| 124 | nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask); |
| 125 | } |
K. Y. Srinivasan | 1268ed0 | 2018-07-03 16:01:55 -0700 | [diff] [blame] | 126 | if (nr_bank < 0) |
| 127 | goto ipi_mask_ex_done; |
K. Y. Srinivasan | 366f03b | 2018-05-16 14:53:32 -0700 | [diff] [blame] | 128 | if (!nr_bank) |
| 129 | ipi_arg->vp_set.format = HV_GENERIC_SET_ALL; |
| 130 | |
| 131 | ret = hv_do_rep_hypercall(HVCALL_SEND_IPI_EX, 0, nr_bank, |
| 132 | ipi_arg, NULL); |
| 133 | |
| 134 | ipi_mask_ex_done: |
| 135 | local_irq_restore(flags); |
| 136 | return ((ret == 0) ? true : false); |
| 137 | } |
| 138 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 139 | static bool __send_ipi_mask(const struct cpumask *mask, int vector) |
| 140 | { |
| 141 | int cur_cpu, vcpu; |
Vitaly Kuznetsov | a1efa9b | 2018-08-27 18:48:57 +0200 | [diff] [blame] | 142 | struct hv_send_ipi ipi_arg; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 143 | int ret = 1; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 144 | |
Vitaly Kuznetsov | 58ec5e9 | 2018-06-22 19:06:25 +0200 | [diff] [blame] | 145 | trace_hyperv_send_ipi_mask(mask, vector); |
| 146 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 147 | if (cpumask_empty(mask)) |
| 148 | return true; |
| 149 | |
| 150 | if (!hv_hypercall_pg) |
| 151 | return false; |
| 152 | |
| 153 | if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR)) |
| 154 | return false; |
| 155 | |
Vitaly Kuznetsov | 4bd0606 | 2018-06-22 19:06:24 +0200 | [diff] [blame] | 156 | /* |
| 157 | * From the supplied CPU set we need to figure out if we can get away |
| 158 | * with cheaper HVCALL_SEND_IPI hypercall. This is possible when the |
| 159 | * highest VP number in the set is < 64. As VP numbers are usually in |
| 160 | * ascending order and match Linux CPU ids, here is an optimization: |
| 161 | * we check the VP number for the highest bit in the supplied set first |
| 162 | * so we can quickly find out if using HVCALL_SEND_IPI_EX hypercall is |
| 163 | * a must. We will also check all VP numbers when walking the supplied |
| 164 | * CPU set to remain correct in all cases. |
| 165 | */ |
| 166 | if (hv_cpu_number_to_vp_number(cpumask_last(mask)) >= 64) |
| 167 | goto do_ex_hypercall; |
K. Y. Srinivasan | 366f03b | 2018-05-16 14:53:32 -0700 | [diff] [blame] | 168 | |
Vitaly Kuznetsov | d8e6b23 | 2018-06-22 19:06:23 +0200 | [diff] [blame] | 169 | ipi_arg.vector = vector; |
| 170 | ipi_arg.cpu_mask = 0; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 171 | |
| 172 | for_each_cpu(cur_cpu, mask) { |
| 173 | vcpu = hv_cpu_number_to_vp_number(cur_cpu); |
K. Y. Srinivasan | 1268ed0 | 2018-07-03 16:01:55 -0700 | [diff] [blame] | 174 | if (vcpu == VP_INVAL) |
K. Y. Srinivasan | be0e16c | 2018-07-20 03:50:09 +0000 | [diff] [blame] | 175 | return false; |
K. Y. Srinivasan | 1268ed0 | 2018-07-03 16:01:55 -0700 | [diff] [blame] | 176 | |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 177 | /* |
| 178 | * This particular version of the IPI hypercall can |
| 179 | * only target upto 64 CPUs. |
| 180 | */ |
| 181 | if (vcpu >= 64) |
Vitaly Kuznetsov | 4bd0606 | 2018-06-22 19:06:24 +0200 | [diff] [blame] | 182 | goto do_ex_hypercall; |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 183 | |
Vitaly Kuznetsov | d8e6b23 | 2018-06-22 19:06:23 +0200 | [diff] [blame] | 184 | __set_bit(vcpu, (unsigned long *)&ipi_arg.cpu_mask); |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Vitaly Kuznetsov | d8e6b23 | 2018-06-22 19:06:23 +0200 | [diff] [blame] | 187 | ret = hv_do_fast_hypercall16(HVCALL_SEND_IPI, ipi_arg.vector, |
| 188 | ipi_arg.cpu_mask); |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 189 | return ((ret == 0) ? true : false); |
Vitaly Kuznetsov | 4bd0606 | 2018-06-22 19:06:24 +0200 | [diff] [blame] | 190 | |
| 191 | do_ex_hypercall: |
| 192 | return __send_ipi_mask_ex(mask, vector); |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static bool __send_ipi_one(int cpu, int vector) |
| 196 | { |
| 197 | struct cpumask mask = CPU_MASK_NONE; |
| 198 | |
| 199 | cpumask_set_cpu(cpu, &mask); |
| 200 | return __send_ipi_mask(&mask, vector); |
| 201 | } |
| 202 | |
| 203 | static void hv_send_ipi(int cpu, int vector) |
| 204 | { |
| 205 | if (!__send_ipi_one(cpu, vector)) |
| 206 | orig_apic.send_IPI(cpu, vector); |
| 207 | } |
| 208 | |
| 209 | static void hv_send_ipi_mask(const struct cpumask *mask, int vector) |
| 210 | { |
| 211 | if (!__send_ipi_mask(mask, vector)) |
| 212 | orig_apic.send_IPI_mask(mask, vector); |
| 213 | } |
| 214 | |
| 215 | static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector) |
| 216 | { |
| 217 | unsigned int this_cpu = smp_processor_id(); |
| 218 | struct cpumask new_mask; |
| 219 | const struct cpumask *local_mask; |
| 220 | |
| 221 | cpumask_copy(&new_mask, mask); |
| 222 | cpumask_clear_cpu(this_cpu, &new_mask); |
| 223 | local_mask = &new_mask; |
| 224 | if (!__send_ipi_mask(local_mask, vector)) |
| 225 | orig_apic.send_IPI_mask_allbutself(mask, vector); |
| 226 | } |
| 227 | |
| 228 | static void hv_send_ipi_allbutself(int vector) |
| 229 | { |
| 230 | hv_send_ipi_mask_allbutself(cpu_online_mask, vector); |
| 231 | } |
| 232 | |
| 233 | static void hv_send_ipi_all(int vector) |
| 234 | { |
| 235 | if (!__send_ipi_mask(cpu_online_mask, vector)) |
| 236 | orig_apic.send_IPI_all(vector); |
| 237 | } |
| 238 | |
| 239 | static void hv_send_ipi_self(int vector) |
| 240 | { |
| 241 | if (!__send_ipi_one(smp_processor_id(), vector)) |
| 242 | orig_apic.send_IPI_self(vector); |
| 243 | } |
| 244 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 245 | void __init hv_apic_init(void) |
| 246 | { |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 247 | if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) { |
Vitaly Kuznetsov | 4bd0606 | 2018-06-22 19:06:24 +0200 | [diff] [blame] | 248 | pr_info("Hyper-V: Using IPI hypercalls\n"); |
K. Y. Srinivasan | 68bb7bf | 2018-05-16 14:53:31 -0700 | [diff] [blame] | 249 | /* |
| 250 | * Set the IPI entry points. |
| 251 | */ |
| 252 | orig_apic = *apic; |
| 253 | |
| 254 | apic->send_IPI = hv_send_ipi; |
| 255 | apic->send_IPI_mask = hv_send_ipi_mask; |
| 256 | apic->send_IPI_mask_allbutself = hv_send_ipi_mask_allbutself; |
| 257 | apic->send_IPI_allbutself = hv_send_ipi_allbutself; |
| 258 | apic->send_IPI_all = hv_send_ipi_all; |
| 259 | apic->send_IPI_self = hv_send_ipi_self; |
| 260 | } |
| 261 | |
K. Y. Srinivasan | 6b48cb5 | 2018-05-16 14:53:30 -0700 | [diff] [blame] | 262 | if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) { |
| 263 | pr_info("Hyper-V: Using MSR based APIC access\n"); |
| 264 | apic_set_eoi_write(hv_apic_eoi_write); |
| 265 | apic->read = hv_apic_read; |
| 266 | apic->write = hv_apic_write; |
| 267 | apic->icr_write = hv_apic_icr_write; |
| 268 | apic->icr_read = hv_apic_icr_read; |
| 269 | } |
| 270 | } |