Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright (C) 2013 Citrix Systems
0005  *
0006  * Author: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
0007  */
0008 
0009 #define pr_fmt(fmt) "arm-pv: " fmt
0010 
0011 #include <linux/arm-smccc.h>
0012 #include <linux/cpuhotplug.h>
0013 #include <linux/export.h>
0014 #include <linux/io.h>
0015 #include <linux/jump_label.h>
0016 #include <linux/printk.h>
0017 #include <linux/psci.h>
0018 #include <linux/reboot.h>
0019 #include <linux/slab.h>
0020 #include <linux/types.h>
0021 #include <linux/static_call.h>
0022 
0023 #include <asm/paravirt.h>
0024 #include <asm/pvclock-abi.h>
0025 #include <asm/smp_plat.h>
0026 
0027 struct static_key paravirt_steal_enabled;
0028 struct static_key paravirt_steal_rq_enabled;
0029 
0030 static u64 native_steal_clock(int cpu)
0031 {
0032     return 0;
0033 }
0034 
0035 DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
0036 
0037 struct pv_time_stolen_time_region {
0038     struct pvclock_vcpu_stolen_time __rcu *kaddr;
0039 };
0040 
0041 static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
0042 
0043 static bool steal_acc = true;
0044 static int __init parse_no_stealacc(char *arg)
0045 {
0046     steal_acc = false;
0047     return 0;
0048 }
0049 
0050 early_param("no-steal-acc", parse_no_stealacc);
0051 
0052 /* return stolen time in ns by asking the hypervisor */
0053 static u64 para_steal_clock(int cpu)
0054 {
0055     struct pvclock_vcpu_stolen_time *kaddr = NULL;
0056     struct pv_time_stolen_time_region *reg;
0057     u64 ret = 0;
0058 
0059     reg = per_cpu_ptr(&stolen_time_region, cpu);
0060 
0061     /*
0062      * paravirt_steal_clock() may be called before the CPU
0063      * online notification callback runs. Until the callback
0064      * has run we just return zero.
0065      */
0066     rcu_read_lock();
0067     kaddr = rcu_dereference(reg->kaddr);
0068     if (!kaddr) {
0069         rcu_read_unlock();
0070         return 0;
0071     }
0072 
0073     ret = le64_to_cpu(READ_ONCE(kaddr->stolen_time));
0074     rcu_read_unlock();
0075     return ret;
0076 }
0077 
0078 static int stolen_time_cpu_down_prepare(unsigned int cpu)
0079 {
0080     struct pvclock_vcpu_stolen_time *kaddr = NULL;
0081     struct pv_time_stolen_time_region *reg;
0082 
0083     reg = this_cpu_ptr(&stolen_time_region);
0084     if (!reg->kaddr)
0085         return 0;
0086 
0087     kaddr = rcu_replace_pointer(reg->kaddr, NULL, true);
0088     synchronize_rcu();
0089     memunmap(kaddr);
0090 
0091     return 0;
0092 }
0093 
0094 static int stolen_time_cpu_online(unsigned int cpu)
0095 {
0096     struct pvclock_vcpu_stolen_time *kaddr = NULL;
0097     struct pv_time_stolen_time_region *reg;
0098     struct arm_smccc_res res;
0099 
0100     reg = this_cpu_ptr(&stolen_time_region);
0101 
0102     arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res);
0103 
0104     if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
0105         return -EINVAL;
0106 
0107     kaddr = memremap(res.a0,
0108                   sizeof(struct pvclock_vcpu_stolen_time),
0109                   MEMREMAP_WB);
0110 
0111     rcu_assign_pointer(reg->kaddr, kaddr);
0112 
0113     if (!reg->kaddr) {
0114         pr_warn("Failed to map stolen time data structure\n");
0115         return -ENOMEM;
0116     }
0117 
0118     if (le32_to_cpu(kaddr->revision) != 0 ||
0119         le32_to_cpu(kaddr->attributes) != 0) {
0120         pr_warn_once("Unexpected revision or attributes in stolen time data\n");
0121         return -ENXIO;
0122     }
0123 
0124     return 0;
0125 }
0126 
0127 static int __init pv_time_init_stolen_time(void)
0128 {
0129     int ret;
0130 
0131     ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
0132                 "hypervisor/arm/pvtime:online",
0133                 stolen_time_cpu_online,
0134                 stolen_time_cpu_down_prepare);
0135     if (ret < 0)
0136         return ret;
0137     return 0;
0138 }
0139 
0140 static bool __init has_pv_steal_clock(void)
0141 {
0142     struct arm_smccc_res res;
0143 
0144     /* To detect the presence of PV time support we require SMCCC 1.1+ */
0145     if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE)
0146         return false;
0147 
0148     arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID,
0149                  ARM_SMCCC_HV_PV_TIME_FEATURES, &res);
0150 
0151     if (res.a0 != SMCCC_RET_SUCCESS)
0152         return false;
0153 
0154     arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES,
0155                  ARM_SMCCC_HV_PV_TIME_ST, &res);
0156 
0157     return (res.a0 == SMCCC_RET_SUCCESS);
0158 }
0159 
0160 int __init pv_time_init(void)
0161 {
0162     int ret;
0163 
0164     if (!has_pv_steal_clock())
0165         return 0;
0166 
0167     ret = pv_time_init_stolen_time();
0168     if (ret)
0169         return ret;
0170 
0171     static_call_update(pv_steal_clock, para_steal_clock);
0172 
0173     static_key_slow_inc(&paravirt_steal_enabled);
0174     if (steal_acc)
0175         static_key_slow_inc(&paravirt_steal_rq_enabled);
0176 
0177     pr_info("using stolen time PV\n");
0178 
0179     return 0;
0180 }