paravirt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2013 Citrix Systems
  5. *
  6. * Author: Stefano Stabellini <[email protected]>
  7. */
  8. #define pr_fmt(fmt) "arm-pv: " fmt
  9. #include <linux/arm-smccc.h>
  10. #include <linux/cpuhotplug.h>
  11. #include <linux/export.h>
  12. #include <linux/io.h>
  13. #include <linux/jump_label.h>
  14. #include <linux/printk.h>
  15. #include <linux/psci.h>
  16. #include <linux/reboot.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/static_call.h>
  20. #include <asm/paravirt.h>
  21. #include <asm/pvclock-abi.h>
  22. #include <asm/smp_plat.h>
  23. struct static_key paravirt_steal_enabled;
  24. struct static_key paravirt_steal_rq_enabled;
  25. static u64 native_steal_clock(int cpu)
  26. {
  27. return 0;
  28. }
  29. DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
  30. struct pv_time_stolen_time_region {
  31. struct pvclock_vcpu_stolen_time __rcu *kaddr;
  32. };
  33. static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
  34. static bool steal_acc = true;
  35. static int __init parse_no_stealacc(char *arg)
  36. {
  37. steal_acc = false;
  38. return 0;
  39. }
  40. early_param("no-steal-acc", parse_no_stealacc);
  41. /* return stolen time in ns by asking the hypervisor */
  42. static u64 para_steal_clock(int cpu)
  43. {
  44. struct pvclock_vcpu_stolen_time *kaddr = NULL;
  45. struct pv_time_stolen_time_region *reg;
  46. u64 ret = 0;
  47. reg = per_cpu_ptr(&stolen_time_region, cpu);
  48. /*
  49. * paravirt_steal_clock() may be called before the CPU
  50. * online notification callback runs. Until the callback
  51. * has run we just return zero.
  52. */
  53. rcu_read_lock();
  54. kaddr = rcu_dereference(reg->kaddr);
  55. if (!kaddr) {
  56. rcu_read_unlock();
  57. return 0;
  58. }
  59. ret = le64_to_cpu(READ_ONCE(kaddr->stolen_time));
  60. rcu_read_unlock();
  61. return ret;
  62. }
  63. static int stolen_time_cpu_down_prepare(unsigned int cpu)
  64. {
  65. struct pvclock_vcpu_stolen_time *kaddr = NULL;
  66. struct pv_time_stolen_time_region *reg;
  67. reg = this_cpu_ptr(&stolen_time_region);
  68. if (!reg->kaddr)
  69. return 0;
  70. kaddr = rcu_replace_pointer(reg->kaddr, NULL, true);
  71. synchronize_rcu();
  72. memunmap(kaddr);
  73. return 0;
  74. }
  75. static int stolen_time_cpu_online(unsigned int cpu)
  76. {
  77. struct pvclock_vcpu_stolen_time *kaddr = NULL;
  78. struct pv_time_stolen_time_region *reg;
  79. struct arm_smccc_res res;
  80. reg = this_cpu_ptr(&stolen_time_region);
  81. arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
  82. if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
  83. return -EINVAL;
  84. kaddr = memremap(res.a0,
  85. sizeof(struct pvclock_vcpu_stolen_time),
  86. MEMREMAP_WB);
  87. rcu_assign_pointer(reg->kaddr, kaddr);
  88. if (!reg->kaddr) {
  89. pr_warn("Failed to map stolen time data structure\n");
  90. return -ENOMEM;
  91. }
  92. if (le32_to_cpu(kaddr->revision) != 0 ||
  93. le32_to_cpu(kaddr->attributes) != 0) {
  94. pr_warn_once("Unexpected revision or attributes in stolen time data\n");
  95. return -ENXIO;
  96. }
  97. return 0;
  98. }
  99. static int __init pv_time_init_stolen_time(void)
  100. {
  101. int ret;
  102. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  103. "hypervisor/arm/pvtime:online",
  104. stolen_time_cpu_online,
  105. stolen_time_cpu_down_prepare);
  106. if (ret < 0)
  107. return ret;
  108. return 0;
  109. }
  110. static bool __init has_pv_steal_clock(void)
  111. {
  112. struct arm_smccc_res res;
  113. /* To detect the presence of PV time support we require SMCCC 1.1+ */
  114. if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE)
  115. return false;
  116. arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
  117. ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
  118. if (res.a0 != SMCCC_RET_SUCCESS)
  119. return false;
  120. arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
  121. ARM_SMCCC_HV_PV_TIME_ST, &res);
  122. return (res.a0 == SMCCC_RET_SUCCESS);
  123. }
  124. int __init pv_time_init(void)
  125. {
  126. int ret;
  127. if (!has_pv_steal_clock())
  128. return 0;
  129. ret = pv_time_init_stolen_time();
  130. if (ret)
  131. return ret;
  132. static_call_update(pv_steal_clock, para_steal_clock);
  133. static_key_slow_inc(&paravirt_steal_enabled);
  134. if (steal_acc)
  135. static_key_slow_inc(&paravirt_steal_rq_enabled);
  136. pr_info("using stolen time PV\n");
  137. return 0;
  138. }