hv_apic.c 6.2 KB

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