/arch/x86/hyperv/hv_apic.c

https://bitbucket.org/updatelee/v4l-updatelee · C · 256 lines · 183 code · 44 blank · 29 comment · 22 complexity · ec255c0f997ab206f486053d684dd281 MD5 · raw file

  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. 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 ipi_arg_ex **arg;
  83. struct ipi_arg_ex *ipi_arg;
  84. unsigned long flags;
  85. int nr_bank = 0;
  86. int ret = 1;
  87. local_irq_save(flags);
  88. arg = (struct ipi_arg_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
  89. ipi_arg = *arg;
  90. if (unlikely(!ipi_arg))
  91. goto ipi_mask_ex_done;
  92. ipi_arg->vector = vector;
  93. ipi_arg->reserved = 0;
  94. ipi_arg->vp_set.valid_bank_mask = 0;
  95. if (!cpumask_equal(mask, cpu_present_mask)) {
  96. ipi_arg->vp_set.format = HV_GENERIC_SET_SPARSE_4K;
  97. nr_bank = cpumask_to_vpset(&(ipi_arg->vp_set), mask);
  98. }
  99. if (!nr_bank)
  100. ipi_arg->vp_set.format = HV_GENERIC_SET_ALL;
  101. ret = hv_do_rep_hypercall(HVCALL_SEND_IPI_EX, 0, nr_bank,
  102. ipi_arg, NULL);
  103. ipi_mask_ex_done:
  104. local_irq_restore(flags);
  105. return ((ret == 0) ? true : false);
  106. }
  107. static bool __send_ipi_mask(const struct cpumask *mask, int vector)
  108. {
  109. int cur_cpu, vcpu;
  110. struct ipi_arg_non_ex **arg;
  111. struct ipi_arg_non_ex *ipi_arg;
  112. int ret = 1;
  113. unsigned long flags;
  114. if (cpumask_empty(mask))
  115. return true;
  116. if (!hv_hypercall_pg)
  117. return false;
  118. if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
  119. return false;
  120. if ((ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  121. return __send_ipi_mask_ex(mask, vector);
  122. local_irq_save(flags);
  123. arg = (struct ipi_arg_non_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
  124. ipi_arg = *arg;
  125. if (unlikely(!ipi_arg))
  126. goto ipi_mask_done;
  127. ipi_arg->vector = vector;
  128. ipi_arg->reserved = 0;
  129. ipi_arg->cpu_mask = 0;
  130. for_each_cpu(cur_cpu, mask) {
  131. vcpu = hv_cpu_number_to_vp_number(cur_cpu);
  132. /*
  133. * This particular version of the IPI hypercall can
  134. * only target upto 64 CPUs.
  135. */
  136. if (vcpu >= 64)
  137. goto ipi_mask_done;
  138. __set_bit(vcpu, (unsigned long *)&ipi_arg->cpu_mask);
  139. }
  140. ret = hv_do_hypercall(HVCALL_SEND_IPI, ipi_arg, NULL);
  141. ipi_mask_done:
  142. local_irq_restore(flags);
  143. return ((ret == 0) ? true : false);
  144. }
  145. static bool __send_ipi_one(int cpu, int vector)
  146. {
  147. struct cpumask mask = CPU_MASK_NONE;
  148. cpumask_set_cpu(cpu, &mask);
  149. return __send_ipi_mask(&mask, vector);
  150. }
  151. static void hv_send_ipi(int cpu, int vector)
  152. {
  153. if (!__send_ipi_one(cpu, vector))
  154. orig_apic.send_IPI(cpu, vector);
  155. }
  156. static void hv_send_ipi_mask(const struct cpumask *mask, int vector)
  157. {
  158. if (!__send_ipi_mask(mask, vector))
  159. orig_apic.send_IPI_mask(mask, vector);
  160. }
  161. static void hv_send_ipi_mask_allbutself(const struct cpumask *mask, int vector)
  162. {
  163. unsigned int this_cpu = smp_processor_id();
  164. struct cpumask new_mask;
  165. const struct cpumask *local_mask;
  166. cpumask_copy(&new_mask, mask);
  167. cpumask_clear_cpu(this_cpu, &new_mask);
  168. local_mask = &new_mask;
  169. if (!__send_ipi_mask(local_mask, vector))
  170. orig_apic.send_IPI_mask_allbutself(mask, vector);
  171. }
  172. static void hv_send_ipi_allbutself(int vector)
  173. {
  174. hv_send_ipi_mask_allbutself(cpu_online_mask, vector);
  175. }
  176. static void hv_send_ipi_all(int vector)
  177. {
  178. if (!__send_ipi_mask(cpu_online_mask, vector))
  179. orig_apic.send_IPI_all(vector);
  180. }
  181. static void hv_send_ipi_self(int vector)
  182. {
  183. if (!__send_ipi_one(smp_processor_id(), vector))
  184. orig_apic.send_IPI_self(vector);
  185. }
  186. void __init hv_apic_init(void)
  187. {
  188. if (ms_hyperv.hints & HV_X64_CLUSTER_IPI_RECOMMENDED) {
  189. if ((ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
  190. pr_info("Hyper-V: Using ext hypercalls for IPI\n");
  191. else
  192. pr_info("Hyper-V: Using IPI hypercalls\n");
  193. /*
  194. * Set the IPI entry points.
  195. */
  196. orig_apic = *apic;
  197. apic->send_IPI = hv_send_ipi;
  198. apic->send_IPI_mask = hv_send_ipi_mask;
  199. apic->send_IPI_mask_allbutself = hv_send_ipi_mask_allbutself;
  200. apic->send_IPI_allbutself = hv_send_ipi_allbutself;
  201. apic->send_IPI_all = hv_send_ipi_all;
  202. apic->send_IPI_self = hv_send_ipi_self;
  203. }
  204. if (ms_hyperv.hints & HV_X64_APIC_ACCESS_RECOMMENDED) {
  205. pr_info("Hyper-V: Using MSR based APIC access\n");
  206. apic_set_eoi_write(hv_apic_eoi_write);
  207. apic->read = hv_apic_read;
  208. apic->write = hv_apic_write;
  209. apic->icr_write = hv_apic_icr_write;
  210. apic->icr_read = hv_apic_icr_read;
  211. }
  212. }