Path: blob/master/drivers/firmware/efi/runtime-wrappers.c
49621 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* runtime-wrappers.c - Runtime Services function call wrappers3*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 finished9* because it's dependent on the return status and execution of10* efi_runtime_service().11* For instance, get_variable() and get_next_variable().12*13* Copyright (C) 2014 Linaro Ltd. <[email protected]>14*15* Split off from arch/x86/platform/efi/efi.c16*17* Copyright (C) 1999 VA Linux Systems18* Copyright (C) 1999 Walt Drummond <[email protected]>19* Copyright (C) 1999-2002 Hewlett-Packard Co.20* Copyright (C) 2005-2008 Intel Co.21* Copyright (C) 2013 SuSE Labs22*/2324#define pr_fmt(fmt) "efi: " fmt2526#include <linux/bug.h>27#include <linux/efi.h>28#include <linux/irqflags.h>29#include <linux/mutex.h>30#include <linux/semaphore.h>31#include <linux/stringify.h>32#include <linux/workqueue.h>33#include <linux/completion.h>3435#include <asm/efi.h>3637/*38* Wrap around the new efi_call_virt_generic() macros so that the39* code doesn't get too cluttered:40*/41#define efi_call_virt(f, args...) \42arch_efi_call_virt(efi.runtime, f, args)4344union efi_rts_args {45struct {46efi_time_t *time;47efi_time_cap_t *capabilities;48} GET_TIME;4950struct {51efi_time_t *time;52} SET_TIME;5354struct {55efi_bool_t *enabled;56efi_bool_t *pending;57efi_time_t *time;58} GET_WAKEUP_TIME;5960struct {61efi_bool_t enable;62efi_time_t *time;63} SET_WAKEUP_TIME;6465struct {66efi_char16_t *name;67efi_guid_t *vendor;68u32 *attr;69unsigned long *data_size;70void *data;71} GET_VARIABLE;7273struct {74unsigned long *name_size;75efi_char16_t *name;76efi_guid_t *vendor;77} GET_NEXT_VARIABLE;7879struct {80efi_char16_t *name;81efi_guid_t *vendor;82u32 attr;83unsigned long data_size;84void *data;85} SET_VARIABLE;8687struct {88u32 attr;89u64 *storage_space;90u64 *remaining_space;91u64 *max_variable_size;92} QUERY_VARIABLE_INFO;9394struct {95u32 *high_count;96} GET_NEXT_HIGH_MONO_COUNT;9798struct {99efi_capsule_header_t **capsules;100unsigned long count;101unsigned long sg_list;102} UPDATE_CAPSULE;103104struct {105efi_capsule_header_t **capsules;106unsigned long count;107u64 *max_size;108int *reset_type;109} QUERY_CAPSULE_CAPS;110111struct {112efi_status_t (__efiapi *acpi_prm_handler)(u64, void *);113u64 param_buffer_addr;114void *context;115} ACPI_PRM_HANDLER;116};117118struct efi_runtime_work efi_rts_work;119120/*121* efi_queue_work: Queue EFI runtime service call and wait for completion122* @_rts: EFI runtime service function identifier123* @_args: Arguments to pass to the EFI runtime service124*125* Accesses to efi_runtime_services() are serialized by a binary126* semaphore (efi_runtime_lock) and caller waits until the work is127* finished, hence _only_ one work is queued at a time and the caller128* thread waits for completion.129*/130#define efi_queue_work(_rts, _args...) \131__efi_queue_work(EFI_ ## _rts, \132&(union efi_rts_args){ ._rts = { _args }})133134#ifndef arch_efi_save_flags135#define arch_efi_save_flags(state_flags) local_save_flags(state_flags)136#define arch_efi_restore_flags(state_flags) local_irq_restore(state_flags)137#endif138139unsigned long efi_call_virt_save_flags(void)140{141unsigned long flags;142143arch_efi_save_flags(flags);144return flags;145}146147void efi_call_virt_check_flags(unsigned long flags, const void *caller)148{149unsigned long cur_flags, mismatch;150151cur_flags = efi_call_virt_save_flags();152153mismatch = flags ^ cur_flags;154if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))155return;156157add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);158pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI call from %pS\n",159flags, cur_flags, caller ?: __builtin_return_address(0));160arch_efi_restore_flags(flags);161}162163/*164* According to section 7.1 of the UEFI spec, Runtime Services are not fully165* reentrant, and there are particular combinations of calls that need to be166* serialized. (source: UEFI Specification v2.4A)167*168* Table 31. Rules for Reentry Into Runtime Services169* +------------------------------------+-------------------------------+170* | If previous call is busy in | Forbidden to call |171* +------------------------------------+-------------------------------+172* | Any | SetVirtualAddressMap() |173* +------------------------------------+-------------------------------+174* | ConvertPointer() | ConvertPointer() |175* +------------------------------------+-------------------------------+176* | SetVariable() | ResetSystem() |177* | UpdateCapsule() | |178* | SetTime() | |179* | SetWakeupTime() | |180* | GetNextHighMonotonicCount() | |181* +------------------------------------+-------------------------------+182* | GetVariable() | GetVariable() |183* | GetNextVariableName() | GetNextVariableName() |184* | SetVariable() | SetVariable() |185* | QueryVariableInfo() | QueryVariableInfo() |186* | UpdateCapsule() | UpdateCapsule() |187* | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |188* | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |189* +------------------------------------+-------------------------------+190* | GetTime() | GetTime() |191* | SetTime() | SetTime() |192* | GetWakeupTime() | GetWakeupTime() |193* | SetWakeupTime() | SetWakeupTime() |194* +------------------------------------+-------------------------------+195*196* Due to the fact that the EFI pstore may write to the variable store in197* interrupt context, we need to use a lock for at least the groups that198* contain SetVariable() and QueryVariableInfo(). That leaves little else, as199* none of the remaining functions are actually ever called at runtime.200* So let's just use a single lock to serialize all Runtime Services calls.201*/202static DEFINE_SEMAPHORE(efi_runtime_lock, 1);203204static struct task_struct *efi_runtime_lock_owner;205206/*207* Expose the EFI runtime lock to the UV platform208*/209#ifdef CONFIG_X86_UV210extern struct semaphore __efi_uv_runtime_lock __alias(efi_runtime_lock);211#endif212213/*214* Calls the appropriate efi_runtime_service() with the appropriate215* arguments.216*/217static void __nocfi efi_call_rts(struct work_struct *work)218{219const union efi_rts_args *args = efi_rts_work.args;220efi_status_t status = EFI_NOT_FOUND;221unsigned long flags;222223efi_runtime_lock_owner = current;224225arch_efi_call_virt_setup();226flags = efi_call_virt_save_flags();227228switch (efi_rts_work.efi_rts_id) {229case EFI_GET_TIME:230status = efi_call_virt(get_time,231args->GET_TIME.time,232args->GET_TIME.capabilities);233break;234case EFI_SET_TIME:235status = efi_call_virt(set_time,236args->SET_TIME.time);237break;238case EFI_GET_WAKEUP_TIME:239status = efi_call_virt(get_wakeup_time,240args->GET_WAKEUP_TIME.enabled,241args->GET_WAKEUP_TIME.pending,242args->GET_WAKEUP_TIME.time);243break;244case EFI_SET_WAKEUP_TIME:245status = efi_call_virt(set_wakeup_time,246args->SET_WAKEUP_TIME.enable,247args->SET_WAKEUP_TIME.time);248break;249case EFI_GET_VARIABLE:250status = efi_call_virt(get_variable,251args->GET_VARIABLE.name,252args->GET_VARIABLE.vendor,253args->GET_VARIABLE.attr,254args->GET_VARIABLE.data_size,255args->GET_VARIABLE.data);256break;257case EFI_GET_NEXT_VARIABLE:258status = efi_call_virt(get_next_variable,259args->GET_NEXT_VARIABLE.name_size,260args->GET_NEXT_VARIABLE.name,261args->GET_NEXT_VARIABLE.vendor);262break;263case EFI_SET_VARIABLE:264status = efi_call_virt(set_variable,265args->SET_VARIABLE.name,266args->SET_VARIABLE.vendor,267args->SET_VARIABLE.attr,268args->SET_VARIABLE.data_size,269args->SET_VARIABLE.data);270break;271case EFI_QUERY_VARIABLE_INFO:272status = efi_call_virt(query_variable_info,273args->QUERY_VARIABLE_INFO.attr,274args->QUERY_VARIABLE_INFO.storage_space,275args->QUERY_VARIABLE_INFO.remaining_space,276args->QUERY_VARIABLE_INFO.max_variable_size);277break;278case EFI_GET_NEXT_HIGH_MONO_COUNT:279status = efi_call_virt(get_next_high_mono_count,280args->GET_NEXT_HIGH_MONO_COUNT.high_count);281break;282case EFI_UPDATE_CAPSULE:283status = efi_call_virt(update_capsule,284args->UPDATE_CAPSULE.capsules,285args->UPDATE_CAPSULE.count,286args->UPDATE_CAPSULE.sg_list);287break;288case EFI_QUERY_CAPSULE_CAPS:289status = efi_call_virt(query_capsule_caps,290args->QUERY_CAPSULE_CAPS.capsules,291args->QUERY_CAPSULE_CAPS.count,292args->QUERY_CAPSULE_CAPS.max_size,293args->QUERY_CAPSULE_CAPS.reset_type);294break;295case EFI_ACPI_PRM_HANDLER:296#ifdef CONFIG_ACPI_PRMT297status = arch_efi_call_virt(args, ACPI_PRM_HANDLER.acpi_prm_handler,298args->ACPI_PRM_HANDLER.param_buffer_addr,299args->ACPI_PRM_HANDLER.context);300break;301#endif302default:303/*304* Ideally, we should never reach here because a caller of this305* function should have put the right efi_runtime_service()306* function identifier into efi_rts_work->efi_rts_id307*/308pr_err("Requested executing invalid EFI Runtime Service.\n");309}310311efi_call_virt_check_flags(flags, efi_rts_work.caller);312arch_efi_call_virt_teardown();313314efi_rts_work.status = status;315complete(&efi_rts_work.efi_rts_comp);316efi_runtime_lock_owner = NULL;317}318319static efi_status_t __efi_queue_work(enum efi_rts_ids id,320union efi_rts_args *args)321{322efi_rts_work.efi_rts_id = id;323efi_rts_work.args = args;324efi_rts_work.caller = __builtin_return_address(0);325efi_rts_work.status = EFI_ABORTED;326327if (!efi_enabled(EFI_RUNTIME_SERVICES)) {328pr_warn_once("EFI Runtime Services are disabled!\n");329efi_rts_work.status = EFI_DEVICE_ERROR;330goto exit;331}332333init_completion(&efi_rts_work.efi_rts_comp);334INIT_WORK(&efi_rts_work.work, efi_call_rts);335336/*337* queue_work() returns 0 if work was already on queue,338* _ideally_ this should never happen.339*/340if (queue_work(efi_rts_wq, &efi_rts_work.work))341wait_for_completion(&efi_rts_work.efi_rts_comp);342else343pr_err("Failed to queue work to efi_rts_wq.\n");344345WARN_ON_ONCE(efi_rts_work.status == EFI_ABORTED);346exit:347efi_rts_work.efi_rts_id = EFI_NONE;348return efi_rts_work.status;349}350351static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)352{353efi_status_t status;354355if (down_interruptible(&efi_runtime_lock))356return EFI_ABORTED;357status = efi_queue_work(GET_TIME, tm, tc);358up(&efi_runtime_lock);359return status;360}361362static efi_status_t virt_efi_set_time(efi_time_t *tm)363{364efi_status_t status;365366if (down_interruptible(&efi_runtime_lock))367return EFI_ABORTED;368status = efi_queue_work(SET_TIME, tm);369up(&efi_runtime_lock);370return status;371}372373static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,374efi_bool_t *pending,375efi_time_t *tm)376{377efi_status_t status;378379if (down_interruptible(&efi_runtime_lock))380return EFI_ABORTED;381status = efi_queue_work(GET_WAKEUP_TIME, enabled, pending, tm);382up(&efi_runtime_lock);383return status;384}385386static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)387{388efi_status_t status;389390if (down_interruptible(&efi_runtime_lock))391return EFI_ABORTED;392status = efi_queue_work(SET_WAKEUP_TIME, enabled, tm);393up(&efi_runtime_lock);394return status;395}396397static efi_status_t virt_efi_get_variable(efi_char16_t *name,398efi_guid_t *vendor,399u32 *attr,400unsigned long *data_size,401void *data)402{403efi_status_t status;404405if (down_interruptible(&efi_runtime_lock))406return EFI_ABORTED;407status = efi_queue_work(GET_VARIABLE, name, vendor, attr, data_size,408data);409up(&efi_runtime_lock);410return status;411}412413static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,414efi_char16_t *name,415efi_guid_t *vendor)416{417efi_status_t status;418419if (down_interruptible(&efi_runtime_lock))420return EFI_ABORTED;421status = efi_queue_work(GET_NEXT_VARIABLE, name_size, name, vendor);422up(&efi_runtime_lock);423return status;424}425426static efi_status_t virt_efi_set_variable(efi_char16_t *name,427efi_guid_t *vendor,428u32 attr,429unsigned long data_size,430void *data)431{432efi_status_t status;433434if (down_interruptible(&efi_runtime_lock))435return EFI_ABORTED;436status = efi_queue_work(SET_VARIABLE, name, vendor, attr, data_size,437data);438up(&efi_runtime_lock);439return status;440}441442static efi_status_t __nocfi443virt_efi_set_variable_nb(efi_char16_t *name, efi_guid_t *vendor, u32 attr,444unsigned long data_size, void *data)445{446efi_status_t status;447448if (down_trylock(&efi_runtime_lock))449return EFI_NOT_READY;450451efi_runtime_lock_owner = current;452status = efi_call_virt_pointer(efi.runtime, set_variable, name, vendor,453attr, data_size, data);454efi_runtime_lock_owner = NULL;455up(&efi_runtime_lock);456return status;457}458459460static efi_status_t virt_efi_query_variable_info(u32 attr,461u64 *storage_space,462u64 *remaining_space,463u64 *max_variable_size)464{465efi_status_t status;466467if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)468return EFI_UNSUPPORTED;469470if (down_interruptible(&efi_runtime_lock))471return EFI_ABORTED;472status = efi_queue_work(QUERY_VARIABLE_INFO, attr, storage_space,473remaining_space, max_variable_size);474up(&efi_runtime_lock);475return status;476}477478static efi_status_t __nocfi479virt_efi_query_variable_info_nb(u32 attr, u64 *storage_space,480u64 *remaining_space, u64 *max_variable_size)481{482efi_status_t status;483484if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)485return EFI_UNSUPPORTED;486487if (down_trylock(&efi_runtime_lock))488return EFI_NOT_READY;489490efi_runtime_lock_owner = current;491status = efi_call_virt_pointer(efi.runtime, query_variable_info, attr,492storage_space, remaining_space,493max_variable_size);494efi_runtime_lock_owner = NULL;495up(&efi_runtime_lock);496return status;497}498499static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)500{501efi_status_t status;502503if (down_interruptible(&efi_runtime_lock))504return EFI_ABORTED;505status = efi_queue_work(GET_NEXT_HIGH_MONO_COUNT, count);506up(&efi_runtime_lock);507return status;508}509510static void __nocfi511virt_efi_reset_system(int reset_type, efi_status_t status,512unsigned long data_size, efi_char16_t *data)513{514if (down_trylock(&efi_runtime_lock)) {515pr_warn("failed to invoke the reset_system() runtime service:\n"516"could not get exclusive access to the firmware\n");517return;518}519520efi_runtime_lock_owner = current;521arch_efi_call_virt_setup();522efi_rts_work.efi_rts_id = EFI_RESET_SYSTEM;523arch_efi_call_virt(efi.runtime, reset_system, reset_type, status,524data_size, data);525arch_efi_call_virt_teardown();526efi_runtime_lock_owner = NULL;527up(&efi_runtime_lock);528}529530static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,531unsigned long count,532unsigned long sg_list)533{534efi_status_t status;535536if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)537return EFI_UNSUPPORTED;538539if (down_interruptible(&efi_runtime_lock))540return EFI_ABORTED;541status = efi_queue_work(UPDATE_CAPSULE, capsules, count, sg_list);542up(&efi_runtime_lock);543return status;544}545546static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,547unsigned long count,548u64 *max_size,549int *reset_type)550{551efi_status_t status;552553if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)554return EFI_UNSUPPORTED;555556if (down_interruptible(&efi_runtime_lock))557return EFI_ABORTED;558status = efi_queue_work(QUERY_CAPSULE_CAPS, capsules, count,559max_size, reset_type);560up(&efi_runtime_lock);561return status;562}563564void __init efi_native_runtime_setup(void)565{566efi.get_time = virt_efi_get_time;567efi.set_time = virt_efi_set_time;568efi.get_wakeup_time = virt_efi_get_wakeup_time;569efi.set_wakeup_time = virt_efi_set_wakeup_time;570efi.get_variable = virt_efi_get_variable;571efi.get_next_variable = virt_efi_get_next_variable;572efi.set_variable = virt_efi_set_variable;573efi.set_variable_nonblocking = virt_efi_set_variable_nb;574efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;575efi.reset_system = virt_efi_reset_system;576efi.query_variable_info = virt_efi_query_variable_info;577efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nb;578efi.update_capsule = virt_efi_update_capsule;579efi.query_capsule_caps = virt_efi_query_capsule_caps;580}581582#ifdef CONFIG_ACPI_PRMT583584efi_status_t585efi_call_acpi_prm_handler(efi_status_t (__efiapi *handler_addr)(u64, void *),586u64 param_buffer_addr, void *context)587{588efi_status_t status;589590if (down_interruptible(&efi_runtime_lock))591return EFI_ABORTED;592status = efi_queue_work(ACPI_PRM_HANDLER, handler_addr,593param_buffer_addr, context);594up(&efi_runtime_lock);595return status;596}597598#endif599600void efi_runtime_assert_lock_held(void)601{602WARN_ON(efi_runtime_lock_owner != current);603}604605606