1 /* 2 * runtime-wrappers.c - Runtime Services function call wrappers 3 * 4 * Implementation summary: 5 * ----------------------- 6 * 1. When user/kernel thread requests to execute efi_runtime_service(), 7 * enqueue work to efi_rts_wq. 8 * 2. Caller thread waits for completion until the work is finished 9 * because it's dependent on the return status and execution of 10 * efi_runtime_service(). 11 * For instance, get_variable() and get_next_variable(). 12 * 13 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org> 14 * 15 * Split off from arch/x86/platform/efi/efi.c 16 * 17 * Copyright (C) 1999 VA Linux Systems 18 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> 19 * Copyright (C) 1999-2002 Hewlett-Packard Co. 20 * Copyright (C) 2005-2008 Intel Co. 21 * Copyright (C) 2013 SuSE Labs 22 * 23 * This file is released under the GPLv2. 24 */ 25 26 #define pr_fmt(fmt) "efi: " fmt 27 28 #include <linux/bug.h> 29 #include <linux/efi.h> 30 #include <linux/irqflags.h> 31 #include <linux/mutex.h> 32 #include <linux/semaphore.h> 33 #include <linux/stringify.h> 34 #include <linux/workqueue.h> 35 #include <linux/completion.h> 36 37 #include <asm/efi.h> 38 39 /* 40 * Wrap around the new efi_call_virt_generic() macros so that the 41 * code doesn't get too cluttered: 42 */ 43 #define efi_call_virt(f, args...) \ 44 efi_call_virt_pointer(efi.systab->runtime, f, args) 45 #define __efi_call_virt(f, args...) \ 46 __efi_call_virt_pointer(efi.systab->runtime, f, args) 47 48 struct efi_runtime_work efi_rts_work; 49 50 /* 51 * efi_queue_work: Queue efi_runtime_service() and wait until it's done 52 * @rts: efi_runtime_service() function identifier 53 * @rts_arg<1-5>: efi_runtime_service() function arguments 54 * 55 * Accesses to efi_runtime_services() are serialized by a binary 56 * semaphore (efi_runtime_lock) and caller waits until the work is 57 * finished, hence _only_ one work is queued at a time and the caller 58 * thread waits for completion. 59 */ 60 #define efi_queue_work(_rts, _arg1, _arg2, _arg3, _arg4, _arg5) \ 61 ({ \ 62 efi_rts_work.status = EFI_ABORTED; \ 63 \ 64 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { \ 65 pr_warn_once("EFI Runtime Services are disabled!\n"); \ 66 goto exit; \ 67 } \ 68 \ 69 init_completion(&efi_rts_work.efi_rts_comp); \ 70 INIT_WORK(&efi_rts_work.work, efi_call_rts); \ 71 efi_rts_work.arg1 = _arg1; \ 72 efi_rts_work.arg2 = _arg2; \ 73 efi_rts_work.arg3 = _arg3; \ 74 efi_rts_work.arg4 = _arg4; \ 75 efi_rts_work.arg5 = _arg5; \ 76 efi_rts_work.efi_rts_id = _rts; \ 77 \ 78 /* \ 79 * queue_work() returns 0 if work was already on queue, \ 80 * _ideally_ this should never happen. \ 81 */ \ 82 if (queue_work(efi_rts_wq, &efi_rts_work.work)) \ 83 wait_for_completion(&efi_rts_work.efi_rts_comp); \ 84 else \ 85 pr_err("Failed to queue work to efi_rts_wq.\n"); \ 86 \ 87 exit: \ 88 efi_rts_work.efi_rts_id = NONE; \ 89 efi_rts_work.status; \ 90 }) 91 92 #ifndef arch_efi_save_flags 93 #define arch_efi_save_flags(state_flags) local_save_flags(state_flags) 94 #define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags) 95 #endif 96 97 unsigned long efi_call_virt_save_flags(void) 98 { 99 unsigned long flags; 100 101 arch_efi_save_flags(flags); 102 return flags; 103 } 104 105 void efi_call_virt_check_flags(unsigned long flags, const char *call) 106 { 107 unsigned long cur_flags, mismatch; 108 109 cur_flags = efi_call_virt_save_flags(); 110 111 mismatch = flags ^ cur_flags; 112 if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK)) 113 return; 114 115 add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE); 116 pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n", 117 flags, cur_flags, call); 118 arch_efi_restore_flags(flags); 119 } 120 121 /* 122 * According to section 7.1 of the UEFI spec, Runtime Services are not fully 123 * reentrant, and there are particular combinations of calls that need to be 124 * serialized. (source: UEFI Specification v2.4A) 125 * 126 * Table 31. Rules for Reentry Into Runtime Services 127 * +------------------------------------+-------------------------------+ 128 * | If previous call is busy in | Forbidden to call | 129 * +------------------------------------+-------------------------------+ 130 * | Any | SetVirtualAddressMap() | 131 * +------------------------------------+-------------------------------+ 132 * | ConvertPointer() | ConvertPointer() | 133 * +------------------------------------+-------------------------------+ 134 * | SetVariable() | ResetSystem() | 135 * | UpdateCapsule() | | 136 * | SetTime() | | 137 * | SetWakeupTime() | | 138 * | GetNextHighMonotonicCount() | | 139 * +------------------------------------+-------------------------------+ 140 * | GetVariable() | GetVariable() | 141 * | GetNextVariableName() | GetNextVariableName() | 142 * | SetVariable() | SetVariable() | 143 * | QueryVariableInfo() | QueryVariableInfo() | 144 * | UpdateCapsule() | UpdateCapsule() | 145 * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() | 146 * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() | 147 * +------------------------------------+-------------------------------+ 148 * | GetTime() | GetTime() | 149 * | SetTime() | SetTime() | 150 * | GetWakeupTime() | GetWakeupTime() | 151 * | SetWakeupTime() | SetWakeupTime() | 152 * +------------------------------------+-------------------------------+ 153 * 154 * Due to the fact that the EFI pstore may write to the variable store in 155 * interrupt context, we need to use a lock for at least the groups that 156 * contain SetVariable() and QueryVariableInfo(). That leaves little else, as 157 * none of the remaining functions are actually ever called at runtime. 158 * So let's just use a single lock to serialize all Runtime Services calls. 159 */ 160 static DEFINE_SEMAPHORE(efi_runtime_lock); 161 162 /* 163 * Calls the appropriate efi_runtime_service() with the appropriate 164 * arguments. 165 * 166 * Semantics followed by efi_call_rts() to understand efi_runtime_work: 167 * 1. If argument was a pointer, recast it from void pointer to original 168 * pointer type. 169 * 2. If argument was a value, recast it from void pointer to original 170 * pointer type and dereference it. 171 */ 172 static void efi_call_rts(struct work_struct *work) 173 { 174 void *arg1, *arg2, *arg3, *arg4, *arg5; 175 efi_status_t status = EFI_NOT_FOUND; 176 177 arg1 = efi_rts_work.arg1; 178 arg2 = efi_rts_work.arg2; 179 arg3 = efi_rts_work.arg3; 180 arg4 = efi_rts_work.arg4; 181 arg5 = efi_rts_work.arg5; 182 183 switch (efi_rts_work.efi_rts_id) { 184 case GET_TIME: 185 status = efi_call_virt(get_time, (efi_time_t *)arg1, 186 (efi_time_cap_t *)arg2); 187 break; 188 case SET_TIME: 189 status = efi_call_virt(set_time, (efi_time_t *)arg1); 190 break; 191 case GET_WAKEUP_TIME: 192 status = efi_call_virt(get_wakeup_time, (efi_bool_t *)arg1, 193 (efi_bool_t *)arg2, (efi_time_t *)arg3); 194 break; 195 case SET_WAKEUP_TIME: 196 status = efi_call_virt(set_wakeup_time, *(efi_bool_t *)arg1, 197 (efi_time_t *)arg2); 198 break; 199 case GET_VARIABLE: 200 status = efi_call_virt(get_variable, (efi_char16_t *)arg1, 201 (efi_guid_t *)arg2, (u32 *)arg3, 202 (unsigned long *)arg4, (void *)arg5); 203 break; 204 case GET_NEXT_VARIABLE: 205 status = efi_call_virt(get_next_variable, (unsigned long *)arg1, 206 (efi_char16_t *)arg2, 207 (efi_guid_t *)arg3); 208 break; 209 case SET_VARIABLE: 210 status = efi_call_virt(set_variable, (efi_char16_t *)arg1, 211 (efi_guid_t *)arg2, *(u32 *)arg3, 212 *(unsigned long *)arg4, (void *)arg5); 213 break; 214 case QUERY_VARIABLE_INFO: 215 status = efi_call_virt(query_variable_info, *(u32 *)arg1, 216 (u64 *)arg2, (u64 *)arg3, (u64 *)arg4); 217 break; 218 case GET_NEXT_HIGH_MONO_COUNT: 219 status = efi_call_virt(get_next_high_mono_count, (u32 *)arg1); 220 break; 221 case UPDATE_CAPSULE: 222 status = efi_call_virt(update_capsule, 223 (efi_capsule_header_t **)arg1, 224 *(unsigned long *)arg2, 225 *(unsigned long *)arg3); 226 break; 227 case QUERY_CAPSULE_CAPS: 228 status = efi_call_virt(query_capsule_caps, 229 (efi_capsule_header_t **)arg1, 230 *(unsigned long *)arg2, (u64 *)arg3, 231 (int *)arg4); 232 break; 233 default: 234 /* 235 * Ideally, we should never reach here because a caller of this 236 * function should have put the right efi_runtime_service() 237 * function identifier into efi_rts_work->efi_rts_id 238 */ 239 pr_err("Requested executing invalid EFI Runtime Service.\n"); 240 } 241 efi_rts_work.status = status; 242 complete(&efi_rts_work.efi_rts_comp); 243 } 244 245 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) 246 { 247 efi_status_t status; 248 249 if (down_interruptible(&efi_runtime_lock)) 250 return EFI_ABORTED; 251 status = efi_queue_work(GET_TIME, tm, tc, NULL, NULL, NULL); 252 up(&efi_runtime_lock); 253 return status; 254 } 255 256 static efi_status_t virt_efi_set_time(efi_time_t *tm) 257 { 258 efi_status_t status; 259 260 if (down_interruptible(&efi_runtime_lock)) 261 return EFI_ABORTED; 262 status = efi_queue_work(SET_TIME, tm, NULL, NULL, NULL, NULL); 263 up(&efi_runtime_lock); 264 return status; 265 } 266 267 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, 268 efi_bool_t *pending, 269 efi_time_t *tm) 270 { 271 efi_status_t status; 272 273 if (down_interruptible(&efi_runtime_lock)) 274 return EFI_ABORTED; 275 status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm, NULL, 276 NULL); 277 up(&efi_runtime_lock); 278 return status; 279 } 280 281 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) 282 { 283 efi_status_t status; 284 285 if (down_interruptible(&efi_runtime_lock)) 286 return EFI_ABORTED; 287 status = efi_queue_work(SET_WAKEUP_TIME, &enabled, tm, NULL, NULL, 288 NULL); 289 up(&efi_runtime_lock); 290 return status; 291 } 292 293 static efi_status_t virt_efi_get_variable(efi_char16_t *name, 294 efi_guid_t *vendor, 295 u32 *attr, 296 unsigned long *data_size, 297 void *data) 298 { 299 efi_status_t status; 300 301 if (down_interruptible(&efi_runtime_lock)) 302 return EFI_ABORTED; 303 status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size, 304 data); 305 up(&efi_runtime_lock); 306 return status; 307 } 308 309 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, 310 efi_char16_t *name, 311 efi_guid_t *vendor) 312 { 313 efi_status_t status; 314 315 if (down_interruptible(&efi_runtime_lock)) 316 return EFI_ABORTED; 317 status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor, 318 NULL, NULL); 319 up(&efi_runtime_lock); 320 return status; 321 } 322 323 static efi_status_t virt_efi_set_variable(efi_char16_t *name, 324 efi_guid_t *vendor, 325 u32 attr, 326 unsigned long data_size, 327 void *data) 328 { 329 efi_status_t status; 330 331 if (down_interruptible(&efi_runtime_lock)) 332 return EFI_ABORTED; 333 status = efi_queue_work(SET_VARIABLE, name, vendor, &attr, &data_size, 334 data); 335 up(&efi_runtime_lock); 336 return status; 337 } 338 339 static efi_status_t 340 virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor, 341 u32 attr, unsigned long data_size, 342 void *data) 343 { 344 efi_status_t status; 345 346 if (down_trylock(&efi_runtime_lock)) 347 return EFI_NOT_READY; 348 349 status = efi_call_virt(set_variable, name, vendor, attr, data_size, 350 data); 351 up(&efi_runtime_lock); 352 return status; 353 } 354 355 356 static efi_status_t virt_efi_query_variable_info(u32 attr, 357 u64 *storage_space, 358 u64 *remaining_space, 359 u64 *max_variable_size) 360 { 361 efi_status_t status; 362 363 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 364 return EFI_UNSUPPORTED; 365 366 if (down_interruptible(&efi_runtime_lock)) 367 return EFI_ABORTED; 368 status = efi_queue_work(QUERY_VARIABLE_INFO, &attr, storage_space, 369 remaining_space, max_variable_size, NULL); 370 up(&efi_runtime_lock); 371 return status; 372 } 373 374 static efi_status_t 375 virt_efi_query_variable_info_nonblocking(u32 attr, 376 u64 *storage_space, 377 u64 *remaining_space, 378 u64 *max_variable_size) 379 { 380 efi_status_t status; 381 382 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 383 return EFI_UNSUPPORTED; 384 385 if (down_trylock(&efi_runtime_lock)) 386 return EFI_NOT_READY; 387 388 status = efi_call_virt(query_variable_info, attr, storage_space, 389 remaining_space, max_variable_size); 390 up(&efi_runtime_lock); 391 return status; 392 } 393 394 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) 395 { 396 efi_status_t status; 397 398 if (down_interruptible(&efi_runtime_lock)) 399 return EFI_ABORTED; 400 status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count, NULL, NULL, 401 NULL, NULL); 402 up(&efi_runtime_lock); 403 return status; 404 } 405 406 static void virt_efi_reset_system(int reset_type, 407 efi_status_t status, 408 unsigned long data_size, 409 efi_char16_t *data) 410 { 411 if (down_interruptible(&efi_runtime_lock)) { 412 pr_warn("failed to invoke the reset_system() runtime service:\n" 413 "could not get exclusive access to the firmware\n"); 414 return; 415 } 416 efi_rts_work.efi_rts_id = RESET_SYSTEM; 417 __efi_call_virt(reset_system, reset_type, status, data_size, data); 418 up(&efi_runtime_lock); 419 } 420 421 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, 422 unsigned long count, 423 unsigned long sg_list) 424 { 425 efi_status_t status; 426 427 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 428 return EFI_UNSUPPORTED; 429 430 if (down_interruptible(&efi_runtime_lock)) 431 return EFI_ABORTED; 432 status = efi_queue_work(UPDATE_CAPSULE, capsules, &count, &sg_list, 433 NULL, NULL); 434 up(&efi_runtime_lock); 435 return status; 436 } 437 438 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, 439 unsigned long count, 440 u64 *max_size, 441 int *reset_type) 442 { 443 efi_status_t status; 444 445 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) 446 return EFI_UNSUPPORTED; 447 448 if (down_interruptible(&efi_runtime_lock)) 449 return EFI_ABORTED; 450 status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, &count, 451 max_size, reset_type, NULL); 452 up(&efi_runtime_lock); 453 return status; 454 } 455 456 void efi_native_runtime_setup(void) 457 { 458 efi.get_time = virt_efi_get_time; 459 efi.set_time = virt_efi_set_time; 460 efi.get_wakeup_time = virt_efi_get_wakeup_time; 461 efi.set_wakeup_time = virt_efi_set_wakeup_time; 462 efi.get_variable = virt_efi_get_variable; 463 efi.get_next_variable = virt_efi_get_next_variable; 464 efi.set_variable = virt_efi_set_variable; 465 efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking; 466 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; 467 efi.reset_system = virt_efi_reset_system; 468 efi.query_variable_info = virt_efi_query_variable_info; 469 efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking; 470 efi.update_capsule = virt_efi_update_capsule; 471 efi.query_capsule_caps = virt_efi_query_capsule_caps; 472 } 473