runtime-wrappers.c 14 KB

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