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