runtime-wrappers.c 8.6 KB

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