Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // VCPU stall detector.
0004 //  Copyright (C) Google, 2022
0005 
0006 #include <linux/cpu.h>
0007 #include <linux/init.h>
0008 #include <linux/io.h>
0009 #include <linux/kernel.h>
0010 
0011 #include <linux/device.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/module.h>
0014 #include <linux/nmi.h>
0015 #include <linux/of.h>
0016 #include <linux/of_device.h>
0017 #include <linux/param.h>
0018 #include <linux/percpu.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/slab.h>
0021 
0022 #define VCPU_STALL_REG_STATUS       (0x00)
0023 #define VCPU_STALL_REG_LOAD_CNT     (0x04)
0024 #define VCPU_STALL_REG_CURRENT_CNT  (0x08)
0025 #define VCPU_STALL_REG_CLOCK_FREQ_HZ    (0x0C)
0026 #define VCPU_STALL_REG_LEN      (0x10)
0027 
0028 #define VCPU_STALL_DEFAULT_CLOCK_HZ (10)
0029 #define VCPU_STALL_MAX_CLOCK_HZ     (100)
0030 #define VCPU_STALL_DEFAULT_TIMEOUT_SEC  (8)
0031 #define VCPU_STALL_MAX_TIMEOUT_SEC  (600)
0032 
0033 struct vcpu_stall_detect_config {
0034     u32 clock_freq_hz;
0035     u32 stall_timeout_sec;
0036 
0037     void __iomem *membase;
0038     struct platform_device *dev;
0039     enum cpuhp_state hp_online;
0040 };
0041 
0042 struct vcpu_stall_priv {
0043     struct hrtimer vcpu_hrtimer;
0044     bool is_initialized;
0045 };
0046 
0047 /* The vcpu stall configuration structure which applies to all the CPUs */
0048 static struct vcpu_stall_detect_config vcpu_stall_config;
0049 
0050 #define vcpu_stall_reg_write(vcpu, reg, value)              \
0051     writel_relaxed((value),                     \
0052                (void __iomem *)(vcpu_stall_config.membase + \
0053                (vcpu) * VCPU_STALL_REG_LEN + (reg)))
0054 
0055 
0056 static struct vcpu_stall_priv __percpu *vcpu_stall_detectors;
0057 
0058 static enum hrtimer_restart
0059 vcpu_stall_detect_timer_fn(struct hrtimer *hrtimer)
0060 {
0061     u32 ticks, ping_timeout_ms;
0062 
0063     /* Reload the stall detector counter register every
0064      * `ping_timeout_ms` to prevent the virtual device
0065      * from decrementing it to 0. The virtual device decrements this
0066      * register at 'clock_freq_hz' frequency.
0067      */
0068     ticks = vcpu_stall_config.clock_freq_hz *
0069         vcpu_stall_config.stall_timeout_sec;
0070     vcpu_stall_reg_write(smp_processor_id(),
0071                  VCPU_STALL_REG_LOAD_CNT, ticks);
0072 
0073     ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
0074               MSEC_PER_SEC / 2;
0075     hrtimer_forward_now(hrtimer,
0076                 ms_to_ktime(ping_timeout_ms));
0077 
0078     return HRTIMER_RESTART;
0079 }
0080 
0081 static int start_stall_detector_cpu(unsigned int cpu)
0082 {
0083     u32 ticks, ping_timeout_ms;
0084     struct vcpu_stall_priv *vcpu_stall_detector =
0085         this_cpu_ptr(vcpu_stall_detectors);
0086     struct hrtimer *vcpu_hrtimer = &vcpu_stall_detector->vcpu_hrtimer;
0087 
0088     vcpu_stall_reg_write(cpu, VCPU_STALL_REG_CLOCK_FREQ_HZ,
0089                  vcpu_stall_config.clock_freq_hz);
0090 
0091     /* Compute the number of ticks required for the stall detector
0092      * counter register based on the internal clock frequency and the
0093      * timeout value given from the device tree.
0094      */
0095     ticks = vcpu_stall_config.clock_freq_hz *
0096         vcpu_stall_config.stall_timeout_sec;
0097     vcpu_stall_reg_write(cpu, VCPU_STALL_REG_LOAD_CNT, ticks);
0098 
0099     /* Enable the internal clock and start the stall detector */
0100     vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 1);
0101 
0102     /* Pet the stall detector at half of its expiration timeout
0103      * to prevent spurious resets.
0104      */
0105     ping_timeout_ms = vcpu_stall_config.stall_timeout_sec *
0106               MSEC_PER_SEC / 2;
0107 
0108     hrtimer_init(vcpu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
0109     vcpu_hrtimer->function = vcpu_stall_detect_timer_fn;
0110     vcpu_stall_detector->is_initialized = true;
0111 
0112     hrtimer_start(vcpu_hrtimer, ms_to_ktime(ping_timeout_ms),
0113               HRTIMER_MODE_REL_PINNED);
0114 
0115     return 0;
0116 }
0117 
0118 static int stop_stall_detector_cpu(unsigned int cpu)
0119 {
0120     struct vcpu_stall_priv *vcpu_stall_detector =
0121         per_cpu_ptr(vcpu_stall_detectors, cpu);
0122 
0123     if (!vcpu_stall_detector->is_initialized)
0124         return 0;
0125 
0126     /* Disable the stall detector for the current CPU */
0127     hrtimer_cancel(&vcpu_stall_detector->vcpu_hrtimer);
0128     vcpu_stall_reg_write(cpu, VCPU_STALL_REG_STATUS, 0);
0129     vcpu_stall_detector->is_initialized = false;
0130 
0131     return 0;
0132 }
0133 
0134 static int vcpu_stall_detect_probe(struct platform_device *pdev)
0135 {
0136     int ret;
0137     struct resource *r;
0138     void __iomem *membase;
0139     u32 clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
0140     u32 stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
0141     struct device_node *np = pdev->dev.of_node;
0142 
0143     vcpu_stall_detectors = devm_alloc_percpu(&pdev->dev,
0144                          typeof(struct vcpu_stall_priv));
0145     if (!vcpu_stall_detectors)
0146         return -ENOMEM;
0147 
0148     membase = devm_platform_get_and_ioremap_resource(pdev, 0, &r);
0149     if (IS_ERR(membase)) {
0150         dev_err(&pdev->dev, "Failed to get memory resource\n");
0151         return PTR_ERR(membase);
0152     }
0153 
0154     if (!of_property_read_u32(np, "clock-frequency", &clock_freq_hz)) {
0155         if (!(clock_freq_hz > 0 &&
0156               clock_freq_hz < VCPU_STALL_MAX_CLOCK_HZ)) {
0157             dev_warn(&pdev->dev, "clk out of range\n");
0158             clock_freq_hz = VCPU_STALL_DEFAULT_CLOCK_HZ;
0159         }
0160     }
0161 
0162     if (!of_property_read_u32(np, "timeout-sec", &stall_timeout_sec)) {
0163         if (!(stall_timeout_sec > 0 &&
0164               stall_timeout_sec < VCPU_STALL_MAX_TIMEOUT_SEC)) {
0165             dev_warn(&pdev->dev, "stall timeout out of range\n");
0166             stall_timeout_sec = VCPU_STALL_DEFAULT_TIMEOUT_SEC;
0167         }
0168     }
0169 
0170     vcpu_stall_config = (struct vcpu_stall_detect_config) {
0171         .membase        = membase,
0172         .clock_freq_hz      = clock_freq_hz,
0173         .stall_timeout_sec  = stall_timeout_sec
0174     };
0175 
0176     ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
0177                 "virt/vcpu_stall_detector:online",
0178                 start_stall_detector_cpu,
0179                 stop_stall_detector_cpu);
0180     if (ret < 0) {
0181         dev_err(&pdev->dev, "failed to install cpu hotplug");
0182         goto err;
0183     }
0184 
0185     vcpu_stall_config.hp_online = ret;
0186     return 0;
0187 err:
0188     return ret;
0189 }
0190 
0191 static int vcpu_stall_detect_remove(struct platform_device *pdev)
0192 {
0193     int cpu;
0194 
0195     cpuhp_remove_state(vcpu_stall_config.hp_online);
0196 
0197     for_each_possible_cpu(cpu)
0198         stop_stall_detector_cpu(cpu);
0199 
0200     return 0;
0201 }
0202 
0203 static const struct of_device_id vcpu_stall_detect_of_match[] = {
0204     { .compatible = "qemu,vcpu-stall-detector", },
0205     {}
0206 };
0207 
0208 MODULE_DEVICE_TABLE(of, vcpu_stall_detect_of_match);
0209 
0210 static struct platform_driver vcpu_stall_detect_driver = {
0211     .probe  = vcpu_stall_detect_probe,
0212     .remove = vcpu_stall_detect_remove,
0213     .driver = {
0214         .name           = KBUILD_MODNAME,
0215         .of_match_table = vcpu_stall_detect_of_match,
0216     },
0217 };
0218 
0219 module_platform_driver(vcpu_stall_detect_driver);
0220 
0221 MODULE_LICENSE("GPL");
0222 MODULE_AUTHOR("Sebastian Ene <sebastianene@google.com>");
0223 MODULE_DESCRIPTION("VCPU stall detector");