hv_apic.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_APIC_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. return reg_val;
  53. case APIC_TASKPRI:
  54. rdmsr(HV_X64_MSR_TPR, reg_val, hi);
  55. return reg_val;
  56. default:
  57. return native_apic_mem_read(reg);
  58. }
  59. }
  60. static void hv_apic_write(u32 reg, u32 val)
  61. {
  62. switch (reg) {
  63. case APIC_EOI:
  64. wrmsr(HV_X64_MSR_EOI, val, 0);
  65. break;
  66. case APIC_TASKPRI:
  67. wrmsr(HV_X64_MSR_TPR, val, 0);
  68. break;
  69. default:
  70. native_apic_mem_write(reg, val);
  71. }
  72. }
  73. static void hv_apic_eoi_write(u32 reg, u32 val)
  74. {
  75. wrmsr(HV_X64_MSR_EOI, val, 0);
  76. }
  77. /*
  78. * IPI implementation on Hyper-V.
  79. */
  80. static bool __send_ipi_mask_ex(const struct cpumask *mask, int vector)
  81. {
  82. struct hv_send_ipi_ex **arg;
  83. struct hv_send_ipi_ex *ipi_arg;
  84. unsigned long flags;
  85. int nr_bank = 0;
  86. int ret = 1;
  87. if (!(ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  88. return false;
  89. local_irq_save(flags);
  90. arg = (struct hv_send_ipi_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
  91. ipi_arg = *arg;
  92. if (unlikely(!ipi_arg))
  93. goto ipi_mask_ex_done;
  94. ipi_arg->vector = vector;
  95. ipi_arg->reserved = 0;
  96. ipi_arg->vp_set.valid_bank_mask = 0;
  97. if (!cpumask_equal(mask, cpu_present_mask)) {
  98. ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
  99. nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask);
  100. }
  101. if (nr_bank < 0)
  102. goto ipi_mask_ex_done;
  103. if (!nr_bank)
  104. ipi_arg->vp_set.format = HV_GENERIC_SET_ALL;
  105. ret = hv_do_rep_hypercall(HVCALL_SEND_IPI_EX, 0, nr_bank,
  106. ipi_arg, NULL);
  107. ipi_mask_ex_done:
  108. local_irq_restore(flags);
  109. return ((ret == 0) ? true : false);
  110. }
  111. static bool __send_ipi_mask(const struct cpumask *mask, int vector)
  112. {
  113. int cur_cpu, vcpu;
  114. struct hv_send_ipi ipi_arg;
  115. int ret = 1;
  116. trace_hyperv_send_ipi_mask(mask, vector);
  117. if (cpumask_empty(mask))
  118. return true;
  119. if (!hv_hypercall_pg)
  120. return false;
  121. if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
  122. return false;
  123. /*
  124. * From the supplied CPU set we need to figure out if we can get away
  125. * with cheaper HVCALL_SEND_IPI hypercall. This is possible when the
  126. * highest VP number in the set is < 64. As VP numbers are usually in
  127. * ascending order and match Linux CPU ids, here is an optimization:
  128. * we check the VP number for the highest bit in the supplied set first
  129. * so we can quickly find out if using HVCALL_SEND_IPI_EX hypercall is
  130. * a must. We will also check all VP numbers when walking the supplied
  131. * CPU set to remain correct in all cases.
  132. */
  133. if (hv_cpu_number_to_vp_number(cpumask_last(mask)) >= 64)
  134. goto do_ex_hypercall;
  135. ipi_arg.vector = vector;
  136. ipi_arg.cpu_mask = 0;
  137. for_each_cpu(cur_cpu, mask) {
  138. vcpu = hv_cpu_number_to_vp_number(cur_cpu);
  139. if (vcpu == VP_INVAL)
  140. return false;
  141. /*
  142. * This particular version of the IPI hypercall can
  143. * only target upto 64 CPUs.
  144. */
  145. if (vcpu >= 64)
  146. goto do_ex_hypercall;
  147. __set_bit(vcpu, (unsigned long *)&ipi_arg.cpu_mask);
  148. }
  149. ret = hv_do_fast_hypercall16(HVCALL_SEND_IPI, ipi_arg.vector,
  150. ipi_arg.cpu_mask);
  151. return ((ret == 0) ? true : false);
  152. do_ex_hypercall:
  153. return __send_ipi_mask_ex(mask, vector);
  154. }
  155. static bool __send_ipi_one(int cpu, int vector)
  156. {
  157. struct cpumask mask = CPU_MASK_NONE;
  158. cpumask_set_cpu(cpu, &mask);
  159. return __send_ipi_mask(&mask, vector);
  160. }
  161. static void hv_send_ipi(int cpu, int vector)
  162. {
  163. if (!__send_ipi_one(cpu, vector))
  164. orig_apic.send_IPI(cpu, vector);
  165. }
  166. static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
  167. {
  168. if (!__send_ipi_mask(mask, vector))
  169. orig_apic.send_IPI_mask(mask, vector);
  170. }
  171. static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
  172. {
  173. unsigned int this_cpu = smp_processor_id();
  174. struct cpumask new_mask;
  175. const struct cpumask *local_mask;
  176. cpumask_copy(&new_mask, mask);
  177. cpumask_clear_cpu(this_cpu, &new_mask);
  178. local_mask = &new_mask;
  179. if (!__send_ipi_mask(local_mask, vector))
  180. orig_apic.send_IPI_mask_allbutself(mask, vector);
  181. }
  182. static void hv_send_ipi_allbutself(int vector)
  183. {
  184. hv_send_ipi_mask_allbutself(cpu_online_mask, vector);
  185. }
  186. static void hv_send_ipi_all(int vector)
  187. {
  188. if (!__send_ipi_mask(cpu_online_mask, vector))
  189. orig_apic.send_IPI_all(vector);
  190. }
  191. static void hv_send_ipi_self(int vector)
  192. {
  193. if (!__send_ipi_one(smp_processor_id(), vector))
  194. orig_apic.send_IPI_self(vector);
  195. }
  196. void __init hv_apic_init(void)
  197. {
  198. if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) {
  199. pr_info("Hyper-V: Using IPI hypercalls\n");
  200. /*
  201. * Set the IPI entry points.
  202. */
  203. orig_apic = *apic;
  204. apic->send_IPI = hv_send_ipi;
  205. apic->send_IPI_mask = hv_send_ipi_mask;
  206. apic->send_IPI_mask_allbutself = hv_send_ipi_mask_allbutself;
  207. apic->send_IPI_allbutself = hv_send_ipi_allbutself;
  208. apic->send_IPI_all = hv_send_ipi_all;
  209. apic->send_IPI_self = hv_send_ipi_self;
  210. }
  211. if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) {
  212. pr_info("Hyper-V: Using MSR based APIC access\n");
  213. apic_set_eoi_write(hv_apic_eoi_write);
  214. apic->read = hv_apic_read;
  215. apic->write = hv_apic_write;
  216. apic->icr_write = hv_apic_icr_write;
  217. apic->icr_read = hv_apic_icr_read;
  218. }
  219. }