Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * runtime-wrappers.c - Runtime Services function call wrappers |
| 3 | * |
| 4 | * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org> |
| 5 | * |
| 6 | * Split off from arch/x86/platform/efi/efi.c |
| 7 | * |
| 8 | * Copyright (C) 1999 VA Linux Systems |
| 9 | * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> |
| 10 | * Copyright (C) 1999-2002 Hewlett-Packard Co. |
| 11 | * Copyright (C) 2005-2008 Intel Co. |
| 12 | * Copyright (C) 2013 SuSE Labs |
| 13 | * |
| 14 | * This file is released under the GPLv2. |
| 15 | */ |
| 16 | |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 17 | #include <linux/bug.h> |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 18 | #include <linux/efi.h> |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 19 | #include <linux/mutex.h> |
| 20 | #include <linux/spinlock.h> |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 21 | #include <asm/efi.h> |
| 22 | |
| 23 | /* |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 24 | * According to section 7.1 of the UEFI spec, Runtime Services are not fully |
| 25 | * reentrant, and there are particular combinations of calls that need to be |
| 26 | * serialized. (source: UEFI Specification v2.4A) |
| 27 | * |
| 28 | * Table 31. Rules for Reentry Into Runtime Services |
| 29 | * +------------------------------------+-------------------------------+ |
| 30 | * | If previous call is busy in | Forbidden to call | |
| 31 | * +------------------------------------+-------------------------------+ |
| 32 | * | Any | SetVirtualAddressMap() | |
| 33 | * +------------------------------------+-------------------------------+ |
| 34 | * | ConvertPointer() | ConvertPointer() | |
| 35 | * +------------------------------------+-------------------------------+ |
| 36 | * | SetVariable() | ResetSystem() | |
| 37 | * | UpdateCapsule() | | |
| 38 | * | SetTime() | | |
| 39 | * | SetWakeupTime() | | |
| 40 | * | GetNextHighMonotonicCount() | | |
| 41 | * +------------------------------------+-------------------------------+ |
| 42 | * | GetVariable() | GetVariable() | |
| 43 | * | GetNextVariableName() | GetNextVariableName() | |
| 44 | * | SetVariable() | SetVariable() | |
| 45 | * | QueryVariableInfo() | QueryVariableInfo() | |
| 46 | * | UpdateCapsule() | UpdateCapsule() | |
| 47 | * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() | |
| 48 | * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() | |
| 49 | * +------------------------------------+-------------------------------+ |
| 50 | * | GetTime() | GetTime() | |
| 51 | * | SetTime() | SetTime() | |
| 52 | * | GetWakeupTime() | GetWakeupTime() | |
| 53 | * | SetWakeupTime() | SetWakeupTime() | |
| 54 | * +------------------------------------+-------------------------------+ |
| 55 | * |
| 56 | * Due to the fact that the EFI pstore may write to the variable store in |
| 57 | * interrupt context, we need to use a spinlock for at least the groups that |
| 58 | * contain SetVariable() and QueryVariableInfo(). That leaves little else, as |
| 59 | * none of the remaining functions are actually ever called at runtime. |
| 60 | * So let's just use a single spinlock to serialize all Runtime Services calls. |
| 61 | */ |
| 62 | static DEFINE_SPINLOCK(efi_runtime_lock); |
| 63 | |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 64 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
| 65 | { |
| 66 | unsigned long flags; |
| 67 | efi_status_t status; |
| 68 | |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 69 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 70 | status = efi_call_virt(get_time, tm, tc); |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 71 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 72 | return status; |
| 73 | } |
| 74 | |
| 75 | static efi_status_t virt_efi_set_time(efi_time_t *tm) |
| 76 | { |
| 77 | unsigned long flags; |
| 78 | efi_status_t status; |
| 79 | |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 80 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 81 | status = efi_call_virt(set_time, tm); |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 82 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 83 | return status; |
| 84 | } |
| 85 | |
| 86 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, |
| 87 | efi_bool_t *pending, |
| 88 | efi_time_t *tm) |
| 89 | { |
| 90 | unsigned long flags; |
| 91 | efi_status_t status; |
| 92 | |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 93 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 94 | status = efi_call_virt(get_wakeup_time, enabled, pending, tm); |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 95 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 96 | return status; |
| 97 | } |
| 98 | |
| 99 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) |
| 100 | { |
| 101 | unsigned long flags; |
| 102 | efi_status_t status; |
| 103 | |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 104 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 105 | status = efi_call_virt(set_wakeup_time, enabled, tm); |
Ard Biesheuvel | 1bb6936 | 2016-02-01 22:07:00 +0000 | [diff] [blame^] | 106 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 107 | return status; |
| 108 | } |
| 109 | |
| 110 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, |
| 111 | efi_guid_t *vendor, |
| 112 | u32 *attr, |
| 113 | unsigned long *data_size, |
| 114 | void *data) |
| 115 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 116 | unsigned long flags; |
| 117 | efi_status_t status; |
| 118 | |
| 119 | spin_lock_irqsave(&efi_runtime_lock, flags); |
| 120 | status = efi_call_virt(get_variable, name, vendor, attr, data_size, |
| 121 | data); |
| 122 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 123 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, |
| 127 | efi_char16_t *name, |
| 128 | efi_guid_t *vendor) |
| 129 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 130 | unsigned long flags; |
| 131 | efi_status_t status; |
| 132 | |
| 133 | spin_lock_irqsave(&efi_runtime_lock, flags); |
| 134 | status = efi_call_virt(get_next_variable, name_size, name, vendor); |
| 135 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 136 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, |
| 140 | efi_guid_t *vendor, |
| 141 | u32 attr, |
| 142 | unsigned long data_size, |
| 143 | void *data) |
| 144 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 145 | unsigned long flags; |
| 146 | efi_status_t status; |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 147 | |
Matt Fleming | 60b4dc7 | 2014-09-30 15:03:38 +0100 | [diff] [blame] | 148 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 149 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, |
| 150 | data); |
Matt Fleming | 60b4dc7 | 2014-09-30 15:03:38 +0100 | [diff] [blame] | 151 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 152 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Matt Fleming | 6d80dba | 2014-09-30 21:58:52 +0100 | [diff] [blame] | 155 | static efi_status_t |
| 156 | virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor, |
| 157 | u32 attr, unsigned long data_size, |
| 158 | void *data) |
| 159 | { |
| 160 | unsigned long flags; |
| 161 | efi_status_t status; |
| 162 | |
| 163 | if (!spin_trylock_irqsave(&efi_runtime_lock, flags)) |
| 164 | return EFI_NOT_READY; |
| 165 | |
| 166 | status = efi_call_virt(set_variable, name, vendor, attr, data_size, |
| 167 | data); |
| 168 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 169 | return status; |
| 170 | } |
| 171 | |
| 172 | |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 173 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
| 174 | u64 *storage_space, |
| 175 | u64 *remaining_space, |
| 176 | u64 *max_variable_size) |
| 177 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 178 | unsigned long flags; |
| 179 | efi_status_t status; |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 180 | |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 181 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 182 | return EFI_UNSUPPORTED; |
| 183 | |
Matt Fleming | 60b4dc7 | 2014-09-30 15:03:38 +0100 | [diff] [blame] | 184 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 185 | status = efi_call_virt(query_variable_info, attr, storage_space, |
| 186 | remaining_space, max_variable_size); |
Matt Fleming | 60b4dc7 | 2014-09-30 15:03:38 +0100 | [diff] [blame] | 187 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 188 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Ard Biesheuvel | d3cac1f | 2016-02-01 22:06:57 +0000 | [diff] [blame] | 191 | static efi_status_t |
| 192 | virt_efi_query_variable_info_nonblocking(u32 attr, |
| 193 | u64 *storage_space, |
| 194 | u64 *remaining_space, |
| 195 | u64 *max_variable_size) |
| 196 | { |
| 197 | unsigned long flags; |
| 198 | efi_status_t status; |
| 199 | |
| 200 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 201 | return EFI_UNSUPPORTED; |
| 202 | |
| 203 | if (!spin_trylock_irqsave(&efi_runtime_lock, flags)) |
| 204 | return EFI_NOT_READY; |
| 205 | |
| 206 | status = efi_call_virt(query_variable_info, attr, storage_space, |
| 207 | remaining_space, max_variable_size); |
| 208 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 209 | return status; |
| 210 | } |
| 211 | |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 212 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
| 213 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 214 | unsigned long flags; |
| 215 | efi_status_t status; |
| 216 | |
| 217 | spin_lock_irqsave(&efi_runtime_lock, flags); |
| 218 | status = efi_call_virt(get_next_high_mono_count, count); |
| 219 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 220 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void virt_efi_reset_system(int reset_type, |
| 224 | efi_status_t status, |
| 225 | unsigned long data_size, |
| 226 | efi_char16_t *data) |
| 227 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 228 | unsigned long flags; |
| 229 | |
| 230 | spin_lock_irqsave(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 231 | __efi_call_virt(reset_system, reset_type, status, data_size, data); |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 232 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
| 236 | unsigned long count, |
| 237 | unsigned long sg_list) |
| 238 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 239 | unsigned long flags; |
| 240 | efi_status_t status; |
| 241 | |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 242 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 243 | return EFI_UNSUPPORTED; |
| 244 | |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 245 | spin_lock_irqsave(&efi_runtime_lock, flags); |
| 246 | status = efi_call_virt(update_capsule, capsules, count, sg_list); |
| 247 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 248 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, |
| 252 | unsigned long count, |
| 253 | u64 *max_size, |
| 254 | int *reset_type) |
| 255 | { |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 256 | unsigned long flags; |
| 257 | efi_status_t status; |
| 258 | |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 259 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) |
| 260 | return EFI_UNSUPPORTED; |
| 261 | |
Ard Biesheuvel | 161485e | 2014-08-04 18:16:00 +0200 | [diff] [blame] | 262 | spin_lock_irqsave(&efi_runtime_lock, flags); |
| 263 | status = efi_call_virt(query_capsule_caps, capsules, count, max_size, |
| 264 | reset_type); |
| 265 | spin_unlock_irqrestore(&efi_runtime_lock, flags); |
| 266 | return status; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void efi_native_runtime_setup(void) |
| 270 | { |
| 271 | efi.get_time = virt_efi_get_time; |
| 272 | efi.set_time = virt_efi_set_time; |
| 273 | efi.get_wakeup_time = virt_efi_get_wakeup_time; |
| 274 | efi.set_wakeup_time = virt_efi_set_wakeup_time; |
| 275 | efi.get_variable = virt_efi_get_variable; |
| 276 | efi.get_next_variable = virt_efi_get_next_variable; |
| 277 | efi.set_variable = virt_efi_set_variable; |
Matt Fleming | 6d80dba | 2014-09-30 21:58:52 +0100 | [diff] [blame] | 278 | efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 279 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; |
| 280 | efi.reset_system = virt_efi_reset_system; |
| 281 | efi.query_variable_info = virt_efi_query_variable_info; |
Ard Biesheuvel | d3cac1f | 2016-02-01 22:06:57 +0000 | [diff] [blame] | 282 | efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking; |
Ard Biesheuvel | 022ee6c | 2014-06-26 12:09:05 +0200 | [diff] [blame] | 283 | efi.update_capsule = virt_efi_update_capsule; |
| 284 | efi.query_capsule_caps = virt_efi_query_capsule_caps; |
| 285 | } |