paravirt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023 Ventana Micro Systems Inc.
  4. */
  5. #define pr_fmt(fmt) "riscv-pv: " fmt
  6. #include <linux/cpuhotplug.h>
  7. #include <linux/compiler.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/jump_label.h>
  11. #include <linux/kconfig.h>
  12. #include <linux/kernel.h>
  13. #include <linux/percpu-defs.h>
  14. #include <linux/printk.h>
  15. #include <linux/static_call.h>
  16. #include <linux/types.h>
  17. #include <asm/barrier.h>
  18. #include <asm/page.h>
  19. #include <asm/paravirt.h>
  20. #include <asm/sbi.h>
  21. struct static_key paravirt_steal_enabled;
  22. struct static_key paravirt_steal_rq_enabled;
  23. static u64 native_steal_clock(int cpu)
  24. {
  25. return 0;
  26. }
  27. DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
  28. static bool steal_acc = true;
  29. static int __init parse_no_stealacc(char *arg)
  30. {
  31. steal_acc = false;
  32. return 0;
  33. }
  34. early_param("no-steal-acc", parse_no_stealacc);
  35. static DEFINE_PER_CPU(struct sbi_sta_struct, steal_time) __aligned(64);
  36. static bool __init has_pv_steal_clock(void)
  37. {
  38. if (sbi_spec_version >= sbi_mk_version(2, 0) &&
  39. sbi_probe_extension(SBI_EXT_STA) > 0) {
  40. pr_info("SBI STA extension detected\n");
  41. return true;
  42. }
  43. return false;
  44. }
  45. static int sbi_sta_steal_time_set_shmem(unsigned long lo, unsigned long hi,
  46. unsigned long flags)
  47. {
  48. struct sbiret ret;
  49. ret = sbi_ecall(SBI_EXT_STA, SBI_EXT_STA_STEAL_TIME_SET_SHMEM,
  50. lo, hi, flags, 0, 0, 0);
  51. if (ret.error) {
  52. if (lo == SBI_SHMEM_DISABLE && hi == SBI_SHMEM_DISABLE)
  53. pr_warn("Failed to disable steal-time shmem");
  54. else
  55. pr_warn("Failed to set steal-time shmem");
  56. return sbi_err_map_linux_errno(ret.error);
  57. }
  58. return 0;
  59. }
  60. static int pv_time_cpu_online(unsigned int cpu)
  61. {
  62. struct sbi_sta_struct *st = this_cpu_ptr(&steal_time);
  63. phys_addr_t pa = __pa(st);
  64. unsigned long lo = (unsigned long)pa;
  65. unsigned long hi = IS_ENABLED(CONFIG_32BIT) ? upper_32_bits((u64)pa) : 0;
  66. return sbi_sta_steal_time_set_shmem(lo, hi, 0);
  67. }
  68. static int pv_time_cpu_down_prepare(unsigned int cpu)
  69. {
  70. return sbi_sta_steal_time_set_shmem(SBI_SHMEM_DISABLE,
  71. SBI_SHMEM_DISABLE, 0);
  72. }
  73. static u64 pv_time_steal_clock(int cpu)
  74. {
  75. struct sbi_sta_struct *st = per_cpu_ptr(&steal_time, cpu);
  76. __le32 sequence;
  77. __le64 steal;
  78. /*
  79. * Check the sequence field before and after reading the steal
  80. * field. Repeat the read if it is different or odd.
  81. */
  82. do {
  83. sequence = READ_ONCE(st->sequence);
  84. virt_rmb();
  85. steal = READ_ONCE(st->steal);
  86. virt_rmb();
  87. } while ((le32_to_cpu(sequence) & 1) ||
  88. sequence != READ_ONCE(st->sequence));
  89. return le64_to_cpu(steal);
  90. }
  91. int __init pv_time_init(void)
  92. {
  93. int ret;
  94. if (!has_pv_steal_clock())
  95. return 0;
  96. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  97. "riscv/pv_time:online",
  98. pv_time_cpu_online,
  99. pv_time_cpu_down_prepare);
  100. if (ret < 0)
  101. return ret;
  102. static_call_update(pv_steal_clock, pv_time_steal_clock);
  103. static_key_slow_inc(&paravirt_steal_enabled);
  104. if (steal_acc)
  105. static_key_slow_inc(&paravirt_steal_rq_enabled);
  106. pr_info("Computing paravirt steal-time\n");
  107. return 0;
  108. }