runtime-wrappers.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * runtime-wrappers.c - Runtime Services function call wrappers
  3. *
  4. * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
  5. *
  6. * Split off from arch/x86/platform/efi/efi.c
  7. *
  8. * Copyright (C) 1999 VA Linux Systems
  9. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  10. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  11. * Copyright (C) 2005-2008 Intel Co.
  12. * Copyright (C) 2013 SuSE Labs
  13. *
  14. * This file is released under the GPLv2.
  15. */
  16. #define pr_fmt(fmt) "efi: " fmt
  17. #include <linux/bug.h>
  18. #include <linux/efi.h>
  19. #include <linux/irqflags.h>
  20. #include <linux/mutex.h>
  21. #include <linux/semaphore.h>
  22. #include <linux/stringify.h>
  23. #include <asm/efi.h>
  24. /*
  25. * Wrap around the new efi_call_virt_generic() macros so that the
  26. * code doesn't get too cluttered:
  27. */
  28. #define efi_call_virt(f, args...) \
  29. efi_call_virt_pointer(efi.systab->runtime, f, args)
  30. #define __efi_call_virt(f, args...) \
  31. __efi_call_virt_pointer(efi.systab->runtime, f, args)
  32. void efi_call_virt_check_flags(unsigned long flags, const char *call)
  33. {
  34. unsigned long cur_flags, mismatch;
  35. local_save_flags(cur_flags);
  36. mismatch = flags ^ cur_flags;
  37. if (!WARN_ON_ONCE(mismatch & ARCH_EFI_IRQ_FLAGS_MASK))
  38. return;
  39. add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_NOW_UNRELIABLE);
  40. pr_err_ratelimited(FW_BUG "IRQ flags corrupted (0x%08lx=>0x%08lx) by EFI %s\n",
  41. flags, cur_flags, call);
  42. local_irq_restore(flags);
  43. }
  44. /*
  45. * According to section 7.1 of the UEFI spec, Runtime Services are not fully
  46. * reentrant, and there are particular combinations of calls that need to be
  47. * serialized. (source: UEFI Specification v2.4A)
  48. *
  49. * Table 31. Rules for Reentry Into Runtime Services
  50. * +------------------------------------+-------------------------------+
  51. * | If previous call is busy in | Forbidden to call |
  52. * +------------------------------------+-------------------------------+
  53. * | Any | SetVirtualAddressMap() |
  54. * +------------------------------------+-------------------------------+
  55. * | ConvertPointer() | ConvertPointer() |
  56. * +------------------------------------+-------------------------------+
  57. * | SetVariable() | ResetSystem() |
  58. * | UpdateCapsule() | |
  59. * | SetTime() | |
  60. * | SetWakeupTime() | |
  61. * | GetNextHighMonotonicCount() | |
  62. * +------------------------------------+-------------------------------+
  63. * | GetVariable() | GetVariable() |
  64. * | GetNextVariableName() | GetNextVariableName() |
  65. * | SetVariable() | SetVariable() |
  66. * | QueryVariableInfo() | QueryVariableInfo() |
  67. * | UpdateCapsule() | UpdateCapsule() |
  68. * | QueryCapsuleCapabilities() | QueryCapsuleCapabilities() |
  69. * | GetNextHighMonotonicCount() | GetNextHighMonotonicCount() |
  70. * +------------------------------------+-------------------------------+
  71. * | GetTime() | GetTime() |
  72. * | SetTime() | SetTime() |
  73. * | GetWakeupTime() | GetWakeupTime() |
  74. * | SetWakeupTime() | SetWakeupTime() |
  75. * +------------------------------------+-------------------------------+
  76. *
  77. * Due to the fact that the EFI pstore may write to the variable store in
  78. * interrupt context, we need to use a lock for at least the groups that
  79. * contain SetVariable() and QueryVariableInfo(). That leaves little else, as
  80. * none of the remaining functions are actually ever called at runtime.
  81. * So let's just use a single lock to serialize all Runtime Services calls.
  82. */
  83. static DEFINE_SEMAPHORE(efi_runtime_lock);
  84. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  85. {
  86. efi_status_t status;
  87. if (down_interruptible(&efi_runtime_lock))
  88. return EFI_ABORTED;
  89. status = efi_call_virt(get_time, tm, tc);
  90. up(&efi_runtime_lock);
  91. return status;
  92. }
  93. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  94. {
  95. efi_status_t status;
  96. if (down_interruptible(&efi_runtime_lock))
  97. return EFI_ABORTED;
  98. status = efi_call_virt(set_time, tm);
  99. up(&efi_runtime_lock);
  100. return status;
  101. }
  102. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  103. efi_bool_t *pending,
  104. efi_time_t *tm)
  105. {
  106. efi_status_t status;
  107. if (down_interruptible(&efi_runtime_lock))
  108. return EFI_ABORTED;
  109. status = efi_call_virt(get_wakeup_time, enabled, pending, tm);
  110. up(&efi_runtime_lock);
  111. return status;
  112. }
  113. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  114. {
  115. efi_status_t status;
  116. if (down_interruptible(&efi_runtime_lock))
  117. return EFI_ABORTED;
  118. status = efi_call_virt(set_wakeup_time, enabled, tm);
  119. up(&efi_runtime_lock);
  120. return status;
  121. }
  122. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  123. efi_guid_t *vendor,
  124. u32 *attr,
  125. unsigned long *data_size,
  126. void *data)
  127. {
  128. efi_status_t status;
  129. if (down_interruptible(&efi_runtime_lock))
  130. return EFI_ABORTED;
  131. status = efi_call_virt(get_variable, name, vendor, attr, data_size,
  132. data);
  133. up(&efi_runtime_lock);
  134. return status;
  135. }
  136. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  137. efi_char16_t *name,
  138. efi_guid_t *vendor)
  139. {
  140. efi_status_t status;
  141. if (down_interruptible(&efi_runtime_lock))
  142. return EFI_ABORTED;
  143. status = efi_call_virt(get_next_variable, name_size, name, vendor);
  144. up(&efi_runtime_lock);
  145. return status;
  146. }
  147. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  148. efi_guid_t *vendor,
  149. u32 attr,
  150. unsigned long data_size,
  151. void *data)
  152. {
  153. efi_status_t status;
  154. if (down_interruptible(&efi_runtime_lock))
  155. return EFI_ABORTED;
  156. status = efi_call_virt(set_variable, name, vendor, attr, data_size,
  157. data);
  158. up(&efi_runtime_lock);
  159. return status;
  160. }
  161. static efi_status_t
  162. virt_efi_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
  163. u32 attr, unsigned long data_size,
  164. void *data)
  165. {
  166. efi_status_t status;
  167. if (down_trylock(&efi_runtime_lock))
  168. return EFI_NOT_READY;
  169. status = efi_call_virt(set_variable, name, vendor, attr, data_size,
  170. data);
  171. up(&efi_runtime_lock);
  172. return status;
  173. }
  174. static efi_status_t virt_efi_query_variable_info(u32 attr,
  175. u64 *storage_space,
  176. u64 *remaining_space,
  177. u64 *max_variable_size)
  178. {
  179. efi_status_t status;
  180. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  181. return EFI_UNSUPPORTED;
  182. if (down_interruptible(&efi_runtime_lock))
  183. return EFI_ABORTED;
  184. status = efi_call_virt(query_variable_info, attr, storage_space,
  185. remaining_space, max_variable_size);
  186. up(&efi_runtime_lock);
  187. return status;
  188. }
  189. static efi_status_t
  190. virt_efi_query_variable_info_nonblocking(u32 attr,
  191. u64 *storage_space,
  192. u64 *remaining_space,
  193. u64 *max_variable_size)
  194. {
  195. efi_status_t status;
  196. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  197. return EFI_UNSUPPORTED;
  198. if (down_trylock(&efi_runtime_lock))
  199. return EFI_NOT_READY;
  200. status = efi_call_virt(query_variable_info, attr, storage_space,
  201. remaining_space, max_variable_size);
  202. up(&efi_runtime_lock);
  203. return status;
  204. }
  205. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  206. {
  207. efi_status_t status;
  208. if (down_interruptible(&efi_runtime_lock))
  209. return EFI_ABORTED;
  210. status = efi_call_virt(get_next_high_mono_count, count);
  211. up(&efi_runtime_lock);
  212. return status;
  213. }
  214. static void virt_efi_reset_system(int reset_type,
  215. efi_status_t status,
  216. unsigned long data_size,
  217. efi_char16_t *data)
  218. {
  219. if (down_interruptible(&efi_runtime_lock)) {
  220. pr_warn("failed to invoke the reset_system() runtime service:\n"
  221. "could not get exclusive access to the firmware\n");
  222. return;
  223. }
  224. __efi_call_virt(reset_system, reset_type, status, data_size, data);
  225. up(&efi_runtime_lock);
  226. }
  227. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  228. unsigned long count,
  229. unsigned long sg_list)
  230. {
  231. efi_status_t status;
  232. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  233. return EFI_UNSUPPORTED;
  234. if (down_interruptible(&efi_runtime_lock))
  235. return EFI_ABORTED;
  236. status = efi_call_virt(update_capsule, capsules, count, sg_list);
  237. up(&efi_runtime_lock);
  238. return status;
  239. }
  240. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  241. unsigned long count,
  242. u64 *max_size,
  243. int *reset_type)
  244. {
  245. efi_status_t status;
  246. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  247. return EFI_UNSUPPORTED;
  248. if (down_interruptible(&efi_runtime_lock))
  249. return EFI_ABORTED;
  250. status = efi_call_virt(query_capsule_caps, capsules, count, max_size,
  251. reset_type);
  252. up(&efi_runtime_lock);
  253. return status;
  254. }
  255. void efi_native_runtime_setup(void)
  256. {
  257. efi.get_time = virt_efi_get_time;
  258. efi.set_time = virt_efi_set_time;
  259. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  260. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  261. efi.get_variable = virt_efi_get_variable;
  262. efi.get_next_variable = virt_efi_get_next_variable;
  263. efi.set_variable = virt_efi_set_variable;
  264. efi.set_variable_nonblocking = virt_efi_set_variable_nonblocking;
  265. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  266. efi.reset_system = virt_efi_reset_system;
  267. efi.query_variable_info = virt_efi_query_variable_info;
  268. efi.query_variable_info_nonblocking = virt_efi_query_variable_info_nonblocking;
  269. efi.update_capsule = virt_efi_update_capsule;
  270. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  271. }