runtime-wrappers.c 14 KB

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