runtime-wrappers.c 16 KB

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