vcpu_stall_detector.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_init(vcpu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  93. vcpu_hrtimer->function = vcpu_stall_detect_timer_fn;
  94. vcpu_stall_detector->is_initialized = true;
  95. hrtimer_start(vcpu_hrtimer, ms_to_ktime(ping_timeout_ms),
  96. HRTIMER_MODE_REL_PINNED);
  97. return 0;
  98. }
  99. static int stop_stall_detector_cpu(unsigned int cpu)
  100. {
  101. struct vcpu_stall_priv *vcpu_stall_detector =
  102. per_cpu_ptr(vcpu_stall_detectors, cpu);
  103. if (!vcpu_stall_detector->is_initialized)
  104. return 0;
  105. /* Disable the stall detector for the current CPU */
  106. hrtimer_cancel(&vcpu_stall_detector->vcpu_hrtimer);
  107. vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 0);
  108. vcpu_stall_detector->is_initialized = false;
  109. return 0;
  110. }
  111. static int vcpu_stall_detect_probe(struct platform_device *pdev)
  112. {
  113. int ret, irq;
  114. struct resource *r;
  115. void __iomem *membase;
  116. u32 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
  117. u32 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
  118. struct device_node *np = pdev->dev.of_node;
  119. vcpu_stall_detectors = devm_alloc_percpu(&pdev->dev,
  120. typeof(struct vcpu_stall_priv));
  121. if (!vcpu_stall_detectors)
  122. return -ENOMEM;
  123. membase = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
  124. if (IS_ERR(membase)) {
  125. dev_err(&pdev->dev, "Failed to get memory resource\n");
  126. return PTR_ERR(membase);
  127. }
  128. if (!of_property_read_u32(np, "clock-frequency", &clock_freq_hz)) {
  129. if (!(clock_freq_hz > 0 &&
  130. clock_freq_hz < VCPU_STALL_MAX_CLOCK_HZ)) {
  131. dev_warn(&pdev->dev, "clk out of range\n");
  132. clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
  133. }
  134. }
  135. if (!of_property_read_u32(np, "timeout-sec", &stall_timeout_sec)) {
  136. if (!(stall_timeout_sec > 0 &&
  137. stall_timeout_sec < VCPU_STALL_MAX_TIMEOUT_SEC)) {
  138. dev_warn(&pdev->dev, "stall timeout out of range\n");
  139. stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
  140. }
  141. }
  142. vcpu_stall_config = (struct vcpu_stall_detect_config) {
  143. .membase = membase,
  144. .clock_freq_hz = clock_freq_hz,
  145. .stall_timeout_sec = stall_timeout_sec,
  146. .ppi_irq = -1,
  147. };
  148. irq = platform_get_irq_optional(pdev, 0);
  149. if (irq > 0) {
  150. ret = request_percpu_irq(irq,
  151. vcpu_stall_detector_irq,
  152. "vcpu_stall_detector",
  153. vcpu_stall_detectors);
  154. if (ret)
  155. goto err;
  156. vcpu_stall_config.ppi_irq = irq;
  157. }
  158. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  159. "virt/vcpu_stall_detector:online",
  160. start_stall_detector_cpu,
  161. stop_stall_detector_cpu);
  162. if (ret < 0) {
  163. dev_err(&pdev->dev, "failed to install cpu hotplug");
  164. goto err;
  165. }
  166. vcpu_stall_config.hp_online = ret;
  167. return 0;
  168. err:
  169. if (vcpu_stall_config.ppi_irq > 0)
  170. free_percpu_irq(vcpu_stall_config.ppi_irq,
  171. vcpu_stall_detectors);
  172. return ret;
  173. }
  174. static void vcpu_stall_detect_remove(struct platform_device *pdev)
  175. {
  176. int cpu;
  177. cpuhp_remove_state(vcpu_stall_config.hp_online);
  178. if (vcpu_stall_config.ppi_irq > 0)
  179. free_percpu_irq(vcpu_stall_config.ppi_irq,
  180. vcpu_stall_detectors);
  181. for_each_possible_cpu(cpu)
  182. stop_stall_detector_cpu(cpu);
  183. }
  184. static const struct of_device_id vcpu_stall_detect_of_match[] = {
  185. { .compatible = "qemu,vcpu-stall-detector", },
  186. {}
  187. };
  188. MODULE_DEVICE_TABLE(of, vcpu_stall_detect_of_match);
  189. static struct platform_driver vcpu_stall_detect_driver = {
  190. .probe = vcpu_stall_detect_probe,
  191. .remove_new = vcpu_stall_detect_remove,
  192. .driver = {
  193. .name = KBUILD_MODNAME,
  194. .of_match_table = vcpu_stall_detect_of_match,
  195. },
  196. };
  197. module_platform_driver(vcpu_stall_detect_driver);
  198. MODULE_LICENSE("GPL");
  199. MODULE_AUTHOR("Sebastian Ene <sebastianene@google.com>");
  200. MODULE_DESCRIPTION("VCPU stall detector");