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