Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 2 | /* |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2013 Citrix Systems |
| 5 | * |
| 6 | * Author: Stefano Stabellini <stefano.stabellini@eu.citrix.com> |
| 7 | */ |
| 8 | |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 9 | #define pr_fmt(fmt) "arm-pv: " fmt |
| 10 | |
| 11 | #include <linux/arm-smccc.h> |
| 12 | #include <linux/cpuhotplug.h> |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 13 | #include <linux/export.h> |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 14 | #include <linux/io.h> |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 15 | #include <linux/jump_label.h> |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 16 | #include <linux/printk.h> |
| 17 | #include <linux/psci.h> |
| 18 | #include <linux/reboot.h> |
| 19 | #include <linux/slab.h> |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 20 | #include <linux/types.h> |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 21 | |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 22 | #include <asm/paravirt.h> |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 23 | #include <asm/pvclock-abi.h> |
| 24 | #include <asm/smp_plat.h> |
Stefano Stabellini | dfd57bc | 2015-11-23 10:33:49 +0000 | [diff] [blame] | 25 | |
| 26 | struct static_key paravirt_steal_enabled; |
| 27 | struct static_key paravirt_steal_rq_enabled; |
| 28 | |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 29 | struct paravirt_patch_template pv_ops; |
| 30 | EXPORT_SYMBOL_GPL(pv_ops); |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 31 | |
| 32 | struct pv_time_stolen_time_region { |
| 33 | struct pvclock_vcpu_stolen_time *kaddr; |
| 34 | }; |
| 35 | |
| 36 | static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region); |
| 37 | |
| 38 | static bool steal_acc = true; |
| 39 | static int __init parse_no_stealacc(char *arg) |
| 40 | { |
| 41 | steal_acc = false; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | early_param("no-steal-acc", parse_no_stealacc); |
| 46 | |
| 47 | /* return stolen time in ns by asking the hypervisor */ |
| 48 | static u64 pv_steal_clock(int cpu) |
| 49 | { |
| 50 | struct pv_time_stolen_time_region *reg; |
| 51 | |
| 52 | reg = per_cpu_ptr(&stolen_time_region, cpu); |
Andrew Jones | 75df529 | 2020-09-16 17:45:30 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * paravirt_steal_clock() may be called before the CPU |
| 56 | * online notification callback runs. Until the callback |
| 57 | * has run we just return zero. |
| 58 | */ |
| 59 | if (!reg->kaddr) |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 60 | return 0; |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 61 | |
| 62 | return le64_to_cpu(READ_ONCE(reg->kaddr->stolen_time)); |
| 63 | } |
| 64 | |
Andrew Jones | 75df529 | 2020-09-16 17:45:30 +0200 | [diff] [blame] | 65 | static int stolen_time_cpu_down_prepare(unsigned int cpu) |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 66 | { |
| 67 | struct pv_time_stolen_time_region *reg; |
| 68 | |
| 69 | reg = this_cpu_ptr(&stolen_time_region); |
| 70 | if (!reg->kaddr) |
| 71 | return 0; |
| 72 | |
| 73 | memunmap(reg->kaddr); |
| 74 | memset(reg, 0, sizeof(*reg)); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
Andrew Jones | 75df529 | 2020-09-16 17:45:30 +0200 | [diff] [blame] | 79 | static int stolen_time_cpu_online(unsigned int cpu) |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 80 | { |
| 81 | struct pv_time_stolen_time_region *reg; |
| 82 | struct arm_smccc_res res; |
| 83 | |
| 84 | reg = this_cpu_ptr(&stolen_time_region); |
| 85 | |
| 86 | arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_ST, &res); |
| 87 | |
| 88 | if (res.a0 == SMCCC_RET_NOT_SUPPORTED) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | reg->kaddr = memremap(res.a0, |
| 92 | sizeof(struct pvclock_vcpu_stolen_time), |
| 93 | MEMREMAP_WB); |
| 94 | |
| 95 | if (!reg->kaddr) { |
| 96 | pr_warn("Failed to map stolen time data structure\n"); |
| 97 | return -ENOMEM; |
| 98 | } |
| 99 | |
| 100 | if (le32_to_cpu(reg->kaddr->revision) != 0 || |
| 101 | le32_to_cpu(reg->kaddr->attributes) != 0) { |
| 102 | pr_warn_once("Unexpected revision or attributes in stolen time data\n"); |
| 103 | return -ENXIO; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Andrew Jones | 75df529 | 2020-09-16 17:45:30 +0200 | [diff] [blame] | 109 | static int __init pv_time_init_stolen_time(void) |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 110 | { |
| 111 | int ret; |
| 112 | |
Andrew Jones | 75df529 | 2020-09-16 17:45:30 +0200 | [diff] [blame] | 113 | ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, |
| 114 | "hypervisor/arm/pvtime:online", |
| 115 | stolen_time_cpu_online, |
| 116 | stolen_time_cpu_down_prepare); |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | return 0; |
| 120 | } |
| 121 | |
Andrew Jones | 75df529 | 2020-09-16 17:45:30 +0200 | [diff] [blame] | 122 | static bool __init has_pv_steal_clock(void) |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 123 | { |
| 124 | struct arm_smccc_res res; |
| 125 | |
| 126 | /* To detect the presence of PV time support we require SMCCC 1.1+ */ |
Sudeep Holla | ad5a57d | 2020-05-18 10:12:19 +0100 | [diff] [blame] | 127 | if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE) |
Steven Price | e0685fa | 2019-10-21 16:28:23 +0100 | [diff] [blame] | 128 | return false; |
| 129 | |
| 130 | arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, |
| 131 | ARM_SMCCC_HV_PV_TIME_FEATURES, &res); |
| 132 | |
| 133 | if (res.a0 != SMCCC_RET_SUCCESS) |
| 134 | return false; |
| 135 | |
| 136 | arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_TIME_FEATURES, |
| 137 | ARM_SMCCC_HV_PV_TIME_ST, &res); |
| 138 | |
| 139 | return (res.a0 == SMCCC_RET_SUCCESS); |
| 140 | } |
| 141 | |
| 142 | int __init pv_time_init(void) |
| 143 | { |
| 144 | int ret; |
| 145 | |
| 146 | if (!has_pv_steal_clock()) |
| 147 | return 0; |
| 148 | |
| 149 | ret = pv_time_init_stolen_time(); |
| 150 | if (ret) |
| 151 | return ret; |
| 152 | |
| 153 | pv_ops.time.steal_clock = pv_steal_clock; |
| 154 | |
| 155 | static_key_slow_inc(¶virt_steal_enabled); |
| 156 | if (steal_acc) |
| 157 | static_key_slow_inc(¶virt_steal_rq_enabled); |
| 158 | |
| 159 | pr_info("using stolen time PV\n"); |
| 160 | |
| 161 | return 0; |
| 162 | } |