mshv_common.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2024, Microsoft Corporation.
  4. *
  5. * This file contains functions that will be called from one or more modules.
  6. * If any of these modules are configured to build, this file is built and just
  7. * statically linked in.
  8. *
  9. * Authors: Microsoft Linux virtualization team
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <asm/mshyperv.h>
  14. #include <linux/resume_user_mode.h>
  15. #include <linux/export.h>
  16. #include <linux/acpi.h>
  17. #include <linux/notifier.h>
  18. #include <linux/reboot.h>
  19. #include "mshv.h"
  20. #define HV_GET_REGISTER_BATCH_SIZE \
  21. (HV_HYP_PAGE_SIZE / sizeof(union hv_register_value))
  22. #define HV_SET_REGISTER_BATCH_SIZE \
  23. ((HV_HYP_PAGE_SIZE - sizeof(struct hv_input_set_vp_registers)) \
  24. / sizeof(struct hv_register_assoc))
  25. int hv_call_get_vp_registers(u32 vp_index, u64 partition_id, u16 count,
  26. union hv_input_vtl input_vtl,
  27. struct hv_register_assoc *registers)
  28. {
  29. struct hv_input_get_vp_registers *input_page;
  30. union hv_register_value *output_page;
  31. u16 completed = 0;
  32. unsigned long remaining = count;
  33. int rep_count, i;
  34. u64 status = HV_STATUS_SUCCESS;
  35. unsigned long flags;
  36. local_irq_save(flags);
  37. input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
  38. output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
  39. input_page->partition_id = partition_id;
  40. input_page->vp_index = vp_index;
  41. input_page->input_vtl.as_uint8 = input_vtl.as_uint8;
  42. input_page->rsvd_z8 = 0;
  43. input_page->rsvd_z16 = 0;
  44. while (remaining) {
  45. rep_count = min(remaining, HV_GET_REGISTER_BATCH_SIZE);
  46. for (i = 0; i < rep_count; ++i)
  47. input_page->names[i] = registers[i].name;
  48. status = hv_do_rep_hypercall(HVCALL_GET_VP_REGISTERS, rep_count,
  49. 0, input_page, output_page);
  50. if (!hv_result_success(status))
  51. break;
  52. completed = hv_repcomp(status);
  53. for (i = 0; i < completed; ++i)
  54. registers[i].value = output_page[i];
  55. registers += completed;
  56. remaining -= completed;
  57. }
  58. local_irq_restore(flags);
  59. return hv_result_to_errno(status);
  60. }
  61. EXPORT_SYMBOL_GPL(hv_call_get_vp_registers);
  62. int hv_call_set_vp_registers(u32 vp_index, u64 partition_id, u16 count,
  63. union hv_input_vtl input_vtl,
  64. struct hv_register_assoc *registers)
  65. {
  66. struct hv_input_set_vp_registers *input_page;
  67. u16 completed = 0;
  68. unsigned long remaining = count;
  69. int rep_count;
  70. u64 status = HV_STATUS_SUCCESS;
  71. unsigned long flags;
  72. local_irq_save(flags);
  73. input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
  74. input_page->partition_id = partition_id;
  75. input_page->vp_index = vp_index;
  76. input_page->input_vtl.as_uint8 = input_vtl.as_uint8;
  77. input_page->rsvd_z8 = 0;
  78. input_page->rsvd_z16 = 0;
  79. while (remaining) {
  80. rep_count = min(remaining, HV_SET_REGISTER_BATCH_SIZE);
  81. memcpy(input_page->elements, registers,
  82. sizeof(struct hv_register_assoc) * rep_count);
  83. status = hv_do_rep_hypercall(HVCALL_SET_VP_REGISTERS, rep_count,
  84. 0, input_page, NULL);
  85. if (!hv_result_success(status))
  86. break;
  87. completed = hv_repcomp(status);
  88. registers += completed;
  89. remaining -= completed;
  90. }
  91. local_irq_restore(flags);
  92. return hv_result_to_errno(status);
  93. }
  94. EXPORT_SYMBOL_GPL(hv_call_set_vp_registers);
  95. int hv_call_get_partition_property(u64 partition_id,
  96. u64 property_code,
  97. u64 *property_value)
  98. {
  99. u64 status;
  100. unsigned long flags;
  101. struct hv_input_get_partition_property *input;
  102. struct hv_output_get_partition_property *output;
  103. local_irq_save(flags);
  104. input = *this_cpu_ptr(hyperv_pcpu_input_arg);
  105. output = *this_cpu_ptr(hyperv_pcpu_output_arg);
  106. memset(input, 0, sizeof(*input));
  107. input->partition_id = partition_id;
  108. input->property_code = property_code;
  109. status = hv_do_hypercall(HVCALL_GET_PARTITION_PROPERTY, input, output);
  110. if (!hv_result_success(status)) {
  111. local_irq_restore(flags);
  112. return hv_result_to_errno(status);
  113. }
  114. *property_value = output->property_value;
  115. local_irq_restore(flags);
  116. return 0;
  117. }
  118. EXPORT_SYMBOL_GPL(hv_call_get_partition_property);
  119. #ifdef CONFIG_X86
  120. /*
  121. * Corresponding sleep states have to be initialized in order for a subsequent
  122. * HVCALL_ENTER_SLEEP_STATE call to succeed. Currently only S5 state as per
  123. * ACPI 6.4 chapter 7.4.2 is relevant, while S1, S2 and S3 can be supported.
  124. *
  125. * In order to pass proper PM values to mshv, ACPI should be initialized and
  126. * should support S5 sleep state when this method is invoked.
  127. */
  128. static int hv_initialize_sleep_states(void)
  129. {
  130. u64 status;
  131. unsigned long flags;
  132. struct hv_input_set_system_property *in;
  133. acpi_status acpi_status;
  134. u8 sleep_type_a, sleep_type_b;
  135. if (!acpi_sleep_state_supported(ACPI_STATE_S5)) {
  136. pr_err("%s: S5 sleep state not supported.\n", __func__);
  137. return -ENODEV;
  138. }
  139. acpi_status = acpi_get_sleep_type_data(ACPI_STATE_S5, &sleep_type_a,
  140. &sleep_type_b);
  141. if (ACPI_FAILURE(acpi_status))
  142. return -ENODEV;
  143. local_irq_save(flags);
  144. in = *this_cpu_ptr(hyperv_pcpu_input_arg);
  145. memset(in, 0, sizeof(*in));
  146. in->property_id = HV_SYSTEM_PROPERTY_SLEEP_STATE;
  147. in->set_sleep_state_info.sleep_state = HV_SLEEP_STATE_S5;
  148. in->set_sleep_state_info.pm1a_slp_typ = sleep_type_a;
  149. in->set_sleep_state_info.pm1b_slp_typ = sleep_type_b;
  150. status = hv_do_hypercall(HVCALL_SET_SYSTEM_PROPERTY, in, NULL);
  151. local_irq_restore(flags);
  152. if (!hv_result_success(status)) {
  153. hv_status_err(status, "\n");
  154. return hv_result_to_errno(status);
  155. }
  156. return 0;
  157. }
  158. /*
  159. * This notifier initializes sleep states in mshv hypervisor which will be
  160. * used during power off.
  161. */
  162. static int hv_reboot_notifier_handler(struct notifier_block *this,
  163. unsigned long code, void *another)
  164. {
  165. int ret = 0;
  166. if (code == SYS_HALT || code == SYS_POWER_OFF)
  167. ret = hv_initialize_sleep_states();
  168. return ret ? NOTIFY_DONE : NOTIFY_OK;
  169. }
  170. static struct notifier_block hv_reboot_notifier = {
  171. .notifier_call = hv_reboot_notifier_handler,
  172. };
  173. void hv_sleep_notifiers_register(void)
  174. {
  175. int ret;
  176. ret = register_reboot_notifier(&hv_reboot_notifier);
  177. if (ret)
  178. pr_err("%s: cannot register reboot notifier %d\n", __func__,
  179. ret);
  180. }
  181. /*
  182. * Power off the machine by entering S5 sleep state via Hyper-V hypercall.
  183. * This call does not return if successful.
  184. */
  185. void hv_machine_power_off(void)
  186. {
  187. unsigned long flags;
  188. struct hv_input_enter_sleep_state *in;
  189. local_irq_save(flags);
  190. in = *this_cpu_ptr(hyperv_pcpu_input_arg);
  191. in->sleep_state = HV_SLEEP_STATE_S5;
  192. (void)hv_do_hypercall(HVCALL_ENTER_SLEEP_STATE, in, NULL);
  193. local_irq_restore(flags);
  194. /* should never reach here */
  195. BUG();
  196. }
  197. #endif