hv_apic.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hyper-V specific APIC code.
  4. *
  5. * Copyright (C) 2018, Microsoft, Inc.
  6. *
  7. * Author : K. Y. Srinivasan <kys@microsoft.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. */
  20. #include <linux/types.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/mm.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/hyperv.h>
  25. #include <linux/slab.h>
  26. #include <linux/cpuhotplug.h>
  27. #include <asm/hypervisor.h>
  28. #include <asm/mshyperv.h>
  29. #include <asm/apic.h>
  30. #include <asm/trace/hyperv.h>
  31. static struct apic orig_apic;
  32. static u64 hv_apic_icr_read(void)
  33. {
  34. u64 reg_val;
  35. rdmsrl(HV_X64_MSR_ICR, reg_val);
  36. return reg_val;
  37. }
  38. static void hv_apic_icr_write(u32 low, u32 id)
  39. {
  40. u64 reg_val;
  41. reg_val = SET_XAPIC_DEST_FIELD(id);
  42. reg_val = reg_val << 32;
  43. reg_val |= low;
  44. wrmsrl(HV_X64_MSR_ICR, reg_val);
  45. }
  46. static u32 hv_apic_read(u32 reg)
  47. {
  48. u32 reg_val, hi;
  49. switch (reg) {
  50. case APIC_EOI:
  51. rdmsr(HV_X64_MSR_EOI, reg_val, hi);
  52. (void)hi;
  53. return reg_val;
  54. case APIC_TASKPRI:
  55. rdmsr(HV_X64_MSR_TPR, reg_val, hi);
  56. (void)hi;
  57. return reg_val;
  58. default:
  59. return native_apic_mem_read(reg);
  60. }
  61. }
  62. static void hv_apic_write(u32 reg, u32 val)
  63. {
  64. switch (reg) {
  65. case APIC_EOI:
  66. wrmsr(HV_X64_MSR_EOI, val, 0);
  67. break;
  68. case APIC_TASKPRI:
  69. wrmsr(HV_X64_MSR_TPR, val, 0);
  70. break;
  71. default:
  72. native_apic_mem_write(reg, val);
  73. }
  74. }
  75. static void hv_apic_eoi_write(void)
  76. {
  77. struct hv_vp_assist_page *hvp = hv_vp_assist_page[smp_processor_id()];
  78. if (hvp && (xchg(&hvp->apic_assist, 0) & 0x1))
  79. return;
  80. wrmsr(HV_X64_MSR_EOI, APIC_EOI_ACK, 0);
  81. }
  82. static bool cpu_is_self(int cpu)
  83. {
  84. return cpu == smp_processor_id();
  85. }
  86. /*
  87. * IPI implementation on Hyper-V.
  88. */
  89. static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector,
  90. bool exclude_self)
  91. {
  92. struct hv_send_ipi_ex *ipi_arg;
  93. unsigned long flags;
  94. int nr_bank = 0;
  95. u64 status = HV_STATUS_INVALID_PARAMETER;
  96. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  97. return false;
  98. local_irq_save(flags);
  99. ipi_arg = *this_cpu_ptr(hyperv_pcpu_input_arg);
  100. if (unlikely(!ipi_arg))
  101. goto ipi_mask_ex_done;
  102. ipi_arg->vector = vector;
  103. ipi_arg->reserved = 0;
  104. ipi_arg->vp_set.valid_bank_mask = 0;
  105. /*
  106. * Use HV_GENERIC_SET_ALL and avoid converting cpumask to VP_SET
  107. * when the IPI is sent to all currently present CPUs.
  108. */
  109. if (!cpumask_equal(mask, cpu_present_mask) || exclude_self) {
  110. ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
  111. nr_bank = cpumask_to_vpset_skip(&ipi_arg->vp_set, mask,
  112. exclude_self ? cpu_is_self : NULL);
  113. /*
  114. * 'nr_bank <= 0' means some CPUs in cpumask can't be
  115. * represented in VP_SET. Return an error and fall back to
  116. * native (architectural) method of sending IPIs.
  117. */
  118. if (nr_bank <= 0)
  119. goto ipi_mask_ex_done;
  120. } else {
  121. ipi_arg->vp_set.format = HV_GENERIC_SET_ALL;
  122. }
  123. status = hv_do_rep_hypercall(HVCALL_SEND_IPI_EX, 0, nr_bank,
  124. ipi_arg, NULL);
  125. ipi_mask_ex_done:
  126. local_irq_restore(flags);
  127. return hv_result_success(status);
  128. }
  129. static bool __send_ipi_mask(const struct cpumask *mask, int vector,
  130. bool exclude_self)
  131. {
  132. int cur_cpu, vcpu, this_cpu = smp_processor_id();
  133. struct hv_send_ipi ipi_arg;
  134. u64 status;
  135. unsigned int weight;
  136. trace_hyperv_send_ipi_mask(mask, vector);
  137. weight = cpumask_weight(mask);
  138. /*
  139. * Do nothing if
  140. * 1. the mask is empty
  141. * 2. the mask only contains self when exclude_self is true
  142. */
  143. if (weight == 0 ||
  144. (exclude_self && weight == 1 && cpumask_test_cpu(this_cpu, mask)))
  145. return true;
  146. /* A fully enlightened TDX VM uses GHCI rather than hv_hypercall_pg. */
  147. if (!hv_hypercall_pg) {
  148. if (ms_hyperv.paravisor_present || !hv_isolation_type_tdx())
  149. return false;
  150. }
  151. if (vector < HV_IPI_LOW_VECTOR || vector > HV_IPI_HIGH_VECTOR)
  152. return false;
  153. /*
  154. * From the supplied CPU set we need to figure out if we can get away
  155. * with cheaper HVCALL_SEND_IPI hypercall. This is possible when the
  156. * highest VP number in the set is < 64. As VP numbers are usually in
  157. * ascending order and match Linux CPU ids, here is an optimization:
  158. * we check the VP number for the highest bit in the supplied set first
  159. * so we can quickly find out if using HVCALL_SEND_IPI_EX hypercall is
  160. * a must. We will also check all VP numbers when walking the supplied
  161. * CPU set to remain correct in all cases.
  162. */
  163. if (hv_cpu_number_to_vp_number(cpumask_last(mask)) >= 64)
  164. goto do_ex_hypercall;
  165. ipi_arg.vector = vector;
  166. ipi_arg.cpu_mask = 0;
  167. for_each_cpu(cur_cpu, mask) {
  168. if (exclude_self && cur_cpu == this_cpu)
  169. continue;
  170. vcpu = hv_cpu_number_to_vp_number(cur_cpu);
  171. if (vcpu == VP_INVAL)
  172. return false;
  173. /*
  174. * This particular version of the IPI hypercall can
  175. * only target up to 64 CPUs.
  176. */
  177. if (vcpu >= 64)
  178. goto do_ex_hypercall;
  179. __set_bit(vcpu, (unsigned long *)&ipi_arg.cpu_mask);
  180. }
  181. status = hv_do_fast_hypercall16(HVCALL_SEND_IPI, ipi_arg.vector,
  182. ipi_arg.cpu_mask);
  183. return hv_result_success(status);
  184. do_ex_hypercall:
  185. return __send_ipi_mask_ex(mask, vector, exclude_self);
  186. }
  187. static bool __send_ipi_one(int cpu, int vector)
  188. {
  189. int vp = hv_cpu_number_to_vp_number(cpu);
  190. u64 status;
  191. trace_hyperv_send_ipi_one(cpu, vector);
  192. if (vp == VP_INVAL)
  193. return false;
  194. /* A fully enlightened TDX VM uses GHCI rather than hv_hypercall_pg. */
  195. if (!hv_hypercall_pg) {
  196. if (ms_hyperv.paravisor_present || !hv_isolation_type_tdx())
  197. return false;
  198. }
  199. if (vector < HV_IPI_LOW_VECTOR || vector > HV_IPI_HIGH_VECTOR)
  200. return false;
  201. if (vp >= 64)
  202. return __send_ipi_mask_ex(cpumask_of(cpu), vector, false);
  203. status = hv_do_fast_hypercall16(HVCALL_SEND_IPI, vector, BIT_ULL(vp));
  204. return hv_result_success(status);
  205. }
  206. static void hv_send_ipi(int cpu, int vector)
  207. {
  208. if (!__send_ipi_one(cpu, vector))
  209. orig_apic.send_IPI(cpu, vector);
  210. }
  211. static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
  212. {
  213. if (!__send_ipi_mask(mask, vector, false))
  214. orig_apic.send_IPI_mask(mask, vector);
  215. }
  216. static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
  217. {
  218. if (!__send_ipi_mask(mask, vector, true))
  219. orig_apic.send_IPI_mask_allbutself(mask, vector);
  220. }
  221. static void hv_send_ipi_allbutself(int vector)
  222. {
  223. hv_send_ipi_mask_allbutself(cpu_online_mask, vector);
  224. }
  225. static void hv_send_ipi_all(int vector)
  226. {
  227. if (!__send_ipi_mask(cpu_online_mask, vector, false))
  228. orig_apic.send_IPI_all(vector);
  229. }
  230. static void hv_send_ipi_self(int vector)
  231. {
  232. if (!__send_ipi_one(smp_processor_id(), vector))
  233. orig_apic.send_IPI_self(vector);
  234. }
  235. void __init hv_apic_init(void)
  236. {
  237. if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) {
  238. pr_info("Hyper-V: Using IPI hypercalls\n");
  239. /*
  240. * Set the IPI entry points.
  241. */
  242. orig_apic = *apic;
  243. apic_update_callback(send_IPI, hv_send_ipi);
  244. apic_update_callback(send_IPI_mask, hv_send_ipi_mask);
  245. apic_update_callback(send_IPI_mask_allbutself, hv_send_ipi_mask_allbutself);
  246. apic_update_callback(send_IPI_allbutself, hv_send_ipi_allbutself);
  247. apic_update_callback(send_IPI_all, hv_send_ipi_all);
  248. apic_update_callback(send_IPI_self, hv_send_ipi_self);
  249. }
  250. if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) {
  251. pr_info("Hyper-V: Using enlightened APIC (%s mode)",
  252. x2apic_enabled() ? "x2apic" : "xapic");
  253. /*
  254. * When in x2apic mode, don't use the Hyper-V specific APIC
  255. * accessors since the field layout in the ICR register is
  256. * different in x2apic mode. Furthermore, the architectural
  257. * x2apic MSRs function just as well as the Hyper-V
  258. * synthetic APIC MSRs, so there's no benefit in having
  259. * separate Hyper-V accessors for x2apic mode. The only
  260. * exception is hv_apic_eoi_write, because it benefits from
  261. * lazy EOI when available, but the same accessor works for
  262. * both xapic and x2apic because the field layout is the same.
  263. */
  264. apic_update_callback(eoi, hv_apic_eoi_write);
  265. if (!x2apic_enabled()) {
  266. apic_update_callback(read, hv_apic_read);
  267. apic_update_callback(write, hv_apic_write);
  268. apic_update_callback(icr_write, hv_apic_icr_write);
  269. apic_update_callback(icr_read, hv_apic_icr_read);
  270. }
  271. }
  272. }