vcpu_stall_detector.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // VCPU stall detector.
  4. // Copyright (C) Google, 2022
  5. #include <linux/cpu.h>
  6. #include <linux/init.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/nmi.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/param.h>
  16. #include <linux/percpu.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #define VCPU_STALL_REG_STATUS (0x00)
  20. #define VCPU_STALL_REG_LOAD_CNT (0x04)
  21. #define VCPU_STALL_REG_CURRENT_CNT (0x08)
  22. #define VCPU_STALL_REG_CLOCK_FREQ_HZ (0x0C)
  23. #define VCPU_STALL_REG_LEN (0x10)
  24. #define VCPU_STALL_DEFAULT_CLOCK_HZ (10)
  25. #define VCPU_STALL_MAX_CLOCK_HZ (100)
  26. #define VCPU_STALL_DEFAULT_TIMEOUT_SEC (8)
  27. #define VCPU_STALL_MAX_TIMEOUT_SEC (600)
  28. struct vcpu_stall_detect_config {
  29. u32 clock_freq_hz;
  30. u32 stall_timeout_sec;
  31. void __iomem *membase;
  32. struct platform_device *dev;
  33. enum cpuhp_state hp_online;
  34. };
  35. struct vcpu_stall_priv {
  36. struct hrtimer vcpu_hrtimer;
  37. bool is_initialized;
  38. };
  39. /* The vcpu stall configuration structure which applies to all the CPUs */
  40. static struct vcpu_stall_detect_config vcpu_stall_config;
  41. #define vcpu_stall_reg_write(vcpu, reg, value) \
  42. writel_relaxed((value), \
  43. (void __iomem *)(vcpu_stall_config.membase + \
  44. (vcpu) * VCPU_STALL_REG_LEN + (reg)))
  45. static struct vcpu_stall_priv __percpu *vcpu_stall_detectors;
  46. static enum hrtimer_restart
  47. vcpu_stall_detect_timer_fn(struct hrtimer *hrtimer)
  48. {
  49. u32 ticks, ping_timeout_ms;
  50. /* Reload the stall detector counter register every
  51. * `ping_timeout_ms` to prevent the virtual device
  52. * from decrementing it to 0. The virtual device decrements this
  53. * register at 'clock_freq_hz' frequency.
  54. */
  55. ticks = vcpu_stall_config.clock_freq_hz *
  56. vcpu_stall_config.stall_timeout_sec;
  57. vcpu_stall_reg_write(smp_processor_id(),
  58. VCPU_STALL_REG_LOAD_CNT, ticks);
  59. ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
  60. MSEC_PER_SEC / 2;
  61. hrtimer_forward_now(hrtimer,
  62. ms_to_ktime(ping_timeout_ms));
  63. return HRTIMER_RESTART;
  64. }
  65. static int start_stall_detector_cpu(unsigned int cpu)
  66. {
  67. u32 ticks, ping_timeout_ms;
  68. struct vcpu_stall_priv *vcpu_stall_detector =
  69. this_cpu_ptr(vcpu_stall_detectors);
  70. struct hrtimer *vcpu_hrtimer = &vcpu_stall_detector->vcpu_hrtimer;
  71. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_CLOCK_FREQ_HZ,
  72. vcpu_stall_config.clock_freq_hz);
  73. /* Compute the number of ticks required for the stall detector
  74. * counter register based on the internal clock frequency and the
  75. * timeout value given from the device tree.
  76. */
  77. ticks = vcpu_stall_config.clock_freq_hz *
  78. vcpu_stall_config.stall_timeout_sec;
  79. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_LOAD_CNT, ticks);
  80. /* Enable the internal clock and start the stall detector */
  81. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 1);
  82. /* Pet the stall detector at half of its expiration timeout
  83. * to prevent spurious resets.
  84. */
  85. ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
  86. MSEC_PER_SEC / 2;
  87. hrtimer_init(vcpu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  88. vcpu_hrtimer->function = vcpu_stall_detect_timer_fn;
  89. vcpu_stall_detector->is_initialized = true;
  90. hrtimer_start(vcpu_hrtimer, ms_to_ktime(ping_timeout_ms),
  91. HRTIMER_MODE_REL_PINNED);
  92. return 0;
  93. }
  94. static int stop_stall_detector_cpu(unsigned int cpu)
  95. {
  96. struct vcpu_stall_priv *vcpu_stall_detector =
  97. per_cpu_ptr(vcpu_stall_detectors, cpu);
  98. if (!vcpu_stall_detector->is_initialized)
  99. return 0;
  100. /* Disable the stall detector for the current CPU */
  101. hrtimer_cancel(&vcpu_stall_detector->vcpu_hrtimer);
  102. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 0);
  103. vcpu_stall_detector->is_initialized = false;
  104. return 0;
  105. }
  106. static int vcpu_stall_detect_probe(struct platform_device *pdev)
  107. {
  108. int ret;
  109. struct resource *r;
  110. void __iomem *membase;
  111. u32 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
  112. u32 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
  113. struct device_node *np = pdev->dev.of_node;
  114. vcpu_stall_detectors = devm_alloc_percpu(&pdev->dev,
  115. typeof(struct vcpu_stall_priv));
  116. if (!vcpu_stall_detectors)
  117. return -ENOMEM;
  118. membase = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
  119. if (IS_ERR(membase)) {
  120. dev_err(&pdev->dev, "Failed to get memory resource\n");
  121. return PTR_ERR(membase);
  122. }
  123. if (!of_property_read_u32(np, "clock-frequency", &clock_freq_hz)) {
  124. if (!(clock_freq_hz > 0 &&
  125. clock_freq_hz < VCPU_STALL_MAX_CLOCK_HZ)) {
  126. dev_warn(&pdev->dev, "clk out of range\n");
  127. clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
  128. }
  129. }
  130. if (!of_property_read_u32(np, "timeout-sec", &stall_timeout_sec)) {
  131. if (!(stall_timeout_sec > 0 &&
  132. stall_timeout_sec < VCPU_STALL_MAX_TIMEOUT_SEC)) {
  133. dev_warn(&pdev->dev, "stall timeout out of range\n");
  134. stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
  135. }
  136. }
  137. vcpu_stall_config = (struct vcpu_stall_detect_config) {
  138. .membase = membase,
  139. .clock_freq_hz = clock_freq_hz,
  140. .stall_timeout_sec = stall_timeout_sec
  141. };
  142. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  143. "virt/vcpu_stall_detector:online",
  144. start_stall_detector_cpu,
  145. stop_stall_detector_cpu);
  146. if (ret < 0) {
  147. dev_err(&pdev->dev, "failed to install cpu hotplug");
  148. goto err;
  149. }
  150. vcpu_stall_config.hp_online = ret;
  151. return 0;
  152. err:
  153. return ret;
  154. }
  155. static int vcpu_stall_detect_remove(struct platform_device *pdev)
  156. {
  157. int cpu;
  158. cpuhp_remove_state(vcpu_stall_config.hp_online);
  159. for_each_possible_cpu(cpu)
  160. stop_stall_detector_cpu(cpu);
  161. return 0;
  162. }
  163. static const struct of_device_id vcpu_stall_detect_of_match[] = {
  164. { .compatible = "qemu,vcpu-stall-detector", },
  165. {}
  166. };
  167. MODULE_DEVICE_TABLE(of, vcpu_stall_detect_of_match);
  168. static struct platform_driver vcpu_stall_detect_driver = {
  169. .probe = vcpu_stall_detect_probe,
  170. .remove = vcpu_stall_detect_remove,
  171. .driver = {
  172. .name = KBUILD_MODNAME,
  173. .of_match_table = vcpu_stall_detect_of_match,
  174. },
  175. };
  176. module_platform_driver(vcpu_stall_detect_driver);
  177. MODULE_LICENSE("GPL");
  178. MODULE_AUTHOR("Sebastian Ene <[email protected]>");
  179. MODULE_DESCRIPTION("VCPU stall detector");