vcpu_stall_detector.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/param.h>
  15. #include <linux/percpu.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #define VCPU_STALL_REG_STATUS (0x00)
  19. #define VCPU_STALL_REG_LOAD_CNT (0x04)
  20. #define VCPU_STALL_REG_CURRENT_CNT (0x08)
  21. #define VCPU_STALL_REG_CLOCK_FREQ_HZ (0x0C)
  22. #define VCPU_STALL_REG_LEN (0x10)
  23. #define VCPU_STALL_DEFAULT_CLOCK_HZ (10)
  24. #define VCPU_STALL_MAX_CLOCK_HZ (100)
  25. #define VCPU_STALL_DEFAULT_TIMEOUT_SEC (8)
  26. #define VCPU_STALL_MAX_TIMEOUT_SEC (600)
  27. struct vcpu_stall_detect_config {
  28. u32 clock_freq_hz;
  29. u32 stall_timeout_sec;
  30. int ppi_irq;
  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 irqreturn_t vcpu_stall_detector_irq(int irq, void *dev)
  66. {
  67. panic("vCPU stall detector");
  68. return IRQ_HANDLED;
  69. }
  70. static int start_stall_detector_cpu(unsigned int cpu)
  71. {
  72. u32 ticks, ping_timeout_ms;
  73. struct vcpu_stall_priv *vcpu_stall_detector =
  74. this_cpu_ptr(vcpu_stall_detectors);
  75. struct hrtimer *vcpu_hrtimer = &vcpu_stall_detector->vcpu_hrtimer;
  76. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_CLOCK_FREQ_HZ,
  77. vcpu_stall_config.clock_freq_hz);
  78. /* Compute the number of ticks required for the stall detector
  79. * counter register based on the internal clock frequency and the
  80. * timeout value given from the device tree.
  81. */
  82. ticks = vcpu_stall_config.clock_freq_hz *
  83. vcpu_stall_config.stall_timeout_sec;
  84. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_LOAD_CNT, ticks);
  85. /* Enable the internal clock and start the stall detector */
  86. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 1);
  87. /* Pet the stall detector at half of its expiration timeout
  88. * to prevent spurious resets.
  89. */
  90. ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
  91. MSEC_PER_SEC / 2;
  92. hrtimer_setup(vcpu_hrtimer, vcpu_stall_detect_timer_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  93. vcpu_stall_detector->is_initialized = true;
  94. hrtimer_start(vcpu_hrtimer, ms_to_ktime(ping_timeout_ms),
  95. HRTIMER_MODE_REL_PINNED);
  96. return 0;
  97. }
  98. static int stop_stall_detector_cpu(unsigned int cpu)
  99. {
  100. struct vcpu_stall_priv *vcpu_stall_detector =
  101. per_cpu_ptr(vcpu_stall_detectors, cpu);
  102. if (!vcpu_stall_detector->is_initialized)
  103. return 0;
  104. /* Disable the stall detector for the current CPU */
  105. hrtimer_cancel(&vcpu_stall_detector->vcpu_hrtimer);
  106. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 0);
  107. vcpu_stall_detector->is_initialized = false;
  108. return 0;
  109. }
  110. static int vcpu_stall_detect_probe(struct platform_device *pdev)
  111. {
  112. int ret, irq;
  113. struct resource *r;
  114. void __iomem *membase;
  115. u32 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
  116. u32 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
  117. struct device_node *np = pdev->dev.of_node;
  118. vcpu_stall_detectors = devm_alloc_percpu(&pdev->dev,
  119. typeof(struct vcpu_stall_priv));
  120. if (!vcpu_stall_detectors)
  121. return -ENOMEM;
  122. membase = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
  123. if (IS_ERR(membase)) {
  124. dev_err(&pdev->dev, "Failed to get memory resource\n");
  125. return PTR_ERR(membase);
  126. }
  127. if (!of_property_read_u32(np, "clock-frequency", &clock_freq_hz)) {
  128. if (!(clock_freq_hz > 0 &&
  129. clock_freq_hz < VCPU_STALL_MAX_CLOCK_HZ)) {
  130. dev_warn(&pdev->dev, "clk out of range\n");
  131. clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
  132. }
  133. }
  134. if (!of_property_read_u32(np, "timeout-sec", &stall_timeout_sec)) {
  135. if (!(stall_timeout_sec > 0 &&
  136. stall_timeout_sec < VCPU_STALL_MAX_TIMEOUT_SEC)) {
  137. dev_warn(&pdev->dev, "stall timeout out of range\n");
  138. stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
  139. }
  140. }
  141. vcpu_stall_config = (struct vcpu_stall_detect_config) {
  142. .membase = membase,
  143. .clock_freq_hz = clock_freq_hz,
  144. .stall_timeout_sec = stall_timeout_sec,
  145. .ppi_irq = -1,
  146. };
  147. irq = platform_get_irq_optional(pdev, 0);
  148. if (irq > 0) {
  149. ret = request_percpu_irq(irq,
  150. vcpu_stall_detector_irq,
  151. "vcpu_stall_detector",
  152. vcpu_stall_detectors);
  153. if (ret)
  154. goto err;
  155. vcpu_stall_config.ppi_irq = irq;
  156. }
  157. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  158. "virt/vcpu_stall_detector:online",
  159. start_stall_detector_cpu,
  160. stop_stall_detector_cpu);
  161. if (ret < 0) {
  162. dev_err(&pdev->dev, "failed to install cpu hotplug");
  163. goto err;
  164. }
  165. vcpu_stall_config.hp_online = ret;
  166. return 0;
  167. err:
  168. if (vcpu_stall_config.ppi_irq > 0)
  169. free_percpu_irq(vcpu_stall_config.ppi_irq,
  170. vcpu_stall_detectors);
  171. return ret;
  172. }
  173. static void vcpu_stall_detect_remove(struct platform_device *pdev)
  174. {
  175. int cpu;
  176. cpuhp_remove_state(vcpu_stall_config.hp_online);
  177. if (vcpu_stall_config.ppi_irq > 0)
  178. free_percpu_irq(vcpu_stall_config.ppi_irq,
  179. vcpu_stall_detectors);
  180. for_each_possible_cpu(cpu)
  181. stop_stall_detector_cpu(cpu);
  182. }
  183. static const struct of_device_id vcpu_stall_detect_of_match[] = {
  184. { .compatible = "qemu,vcpu-stall-detector", },
  185. {}
  186. };
  187. MODULE_DEVICE_TABLE(of, vcpu_stall_detect_of_match);
  188. static struct platform_driver vcpu_stall_detect_driver = {
  189. .probe = vcpu_stall_detect_probe,
  190. .remove = vcpu_stall_detect_remove,
  191. .driver = {
  192. .name = KBUILD_MODNAME,
  193. .of_match_table = vcpu_stall_detect_of_match,
  194. },
  195. };
  196. module_platform_driver(vcpu_stall_detect_driver);
  197. MODULE_LICENSE("GPL");
  198. MODULE_AUTHOR("Sebastian Ene <sebastianene@google.com>");
  199. MODULE_DESCRIPTION("VCPU stall detector");