efi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * EFI support for Xen.
  3. *
  4. * Copyright (C) 1999 VA Linux Systems
  5. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  6. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. * Stephane Eranian <eranian@hpl.hp.com>
  9. * Copyright (C) 2005-2008 Intel Co.
  10. * Fenghua Yu <fenghua.yu@intel.com>
  11. * Bibo Mao <bibo.mao@intel.com>
  12. * Chandramouli Narayanan <mouli@linux.intel.com>
  13. * Huang Ying <ying.huang@intel.com>
  14. * Copyright (C) 2011 Novell Co.
  15. * Jan Beulich <JBeulich@suse.com>
  16. * Copyright (C) 2011-2012 Oracle Co.
  17. * Liang Tang <liang.tang@oracle.com>
  18. * Copyright (c) 2014 Oracle Co., Daniel Kiper
  19. */
  20. #include <linux/bug.h>
  21. #include <linux/efi.h>
  22. #include <linux/init.h>
  23. #include <linux/string.h>
  24. #include <xen/interface/xen.h>
  25. #include <xen/interface/platform.h>
  26. #include <xen/xen.h>
  27. #include <asm/xen/hypercall.h>
  28. #define INIT_EFI_OP(name) \
  29. {.cmd = XENPF_efi_runtime_call, \
  30. .u.efi_runtime_call.function = XEN_EFI_##name, \
  31. .u.efi_runtime_call.misc = 0}
  32. #define efi_data(op) (op.u.efi_runtime_call)
  33. static efi_status_t xen_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  34. {
  35. struct xen_platform_op op = INIT_EFI_OP(get_time);
  36. if (HYPERVISOR_dom0_op(&op) < 0)
  37. return EFI_UNSUPPORTED;
  38. if (tm) {
  39. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_time.time));
  40. memcpy(tm, &efi_data(op).u.get_time.time, sizeof(*tm));
  41. }
  42. if (tc) {
  43. tc->resolution = efi_data(op).u.get_time.resolution;
  44. tc->accuracy = efi_data(op).u.get_time.accuracy;
  45. tc->sets_to_zero = !!(efi_data(op).misc &
  46. XEN_EFI_GET_TIME_SET_CLEARS_NS);
  47. }
  48. return efi_data(op).status;
  49. }
  50. static efi_status_t xen_efi_set_time(efi_time_t *tm)
  51. {
  52. struct xen_platform_op op = INIT_EFI_OP(set_time);
  53. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_time));
  54. memcpy(&efi_data(op).u.set_time, tm, sizeof(*tm));
  55. if (HYPERVISOR_dom0_op(&op) < 0)
  56. return EFI_UNSUPPORTED;
  57. return efi_data(op).status;
  58. }
  59. static efi_status_t xen_efi_get_wakeup_time(efi_bool_t *enabled,
  60. efi_bool_t *pending,
  61. efi_time_t *tm)
  62. {
  63. struct xen_platform_op op = INIT_EFI_OP(get_wakeup_time);
  64. if (HYPERVISOR_dom0_op(&op) < 0)
  65. return EFI_UNSUPPORTED;
  66. if (tm) {
  67. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.get_wakeup_time));
  68. memcpy(tm, &efi_data(op).u.get_wakeup_time, sizeof(*tm));
  69. }
  70. if (enabled)
  71. *enabled = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_ENABLED);
  72. if (pending)
  73. *pending = !!(efi_data(op).misc & XEN_EFI_GET_WAKEUP_TIME_PENDING);
  74. return efi_data(op).status;
  75. }
  76. static efi_status_t xen_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  77. {
  78. struct xen_platform_op op = INIT_EFI_OP(set_wakeup_time);
  79. BUILD_BUG_ON(sizeof(*tm) != sizeof(efi_data(op).u.set_wakeup_time));
  80. if (enabled)
  81. efi_data(op).misc = XEN_EFI_SET_WAKEUP_TIME_ENABLE;
  82. if (tm)
  83. memcpy(&efi_data(op).u.set_wakeup_time, tm, sizeof(*tm));
  84. else
  85. efi_data(op).misc |= XEN_EFI_SET_WAKEUP_TIME_ENABLE_ONLY;
  86. if (HYPERVISOR_dom0_op(&op) < 0)
  87. return EFI_UNSUPPORTED;
  88. return efi_data(op).status;
  89. }
  90. static efi_status_t xen_efi_get_variable(efi_char16_t *name,
  91. efi_guid_t *vendor,
  92. u32 *attr,
  93. unsigned long *data_size,
  94. void *data)
  95. {
  96. struct xen_platform_op op = INIT_EFI_OP(get_variable);
  97. set_xen_guest_handle(efi_data(op).u.get_variable.name, name);
  98. BUILD_BUG_ON(sizeof(*vendor) !=
  99. sizeof(efi_data(op).u.get_variable.vendor_guid));
  100. memcpy(&efi_data(op).u.get_variable.vendor_guid, vendor, sizeof(*vendor));
  101. efi_data(op).u.get_variable.size = *data_size;
  102. set_xen_guest_handle(efi_data(op).u.get_variable.data, data);
  103. if (HYPERVISOR_dom0_op(&op) < 0)
  104. return EFI_UNSUPPORTED;
  105. *data_size = efi_data(op).u.get_variable.size;
  106. if (attr)
  107. *attr = efi_data(op).misc;
  108. return efi_data(op).status;
  109. }
  110. static efi_status_t xen_efi_get_next_variable(unsigned long *name_size,
  111. efi_char16_t *name,
  112. efi_guid_t *vendor)
  113. {
  114. struct xen_platform_op op = INIT_EFI_OP(get_next_variable_name);
  115. efi_data(op).u.get_next_variable_name.size = *name_size;
  116. set_xen_guest_handle(efi_data(op).u.get_next_variable_name.name, name);
  117. BUILD_BUG_ON(sizeof(*vendor) !=
  118. sizeof(efi_data(op).u.get_next_variable_name.vendor_guid));
  119. memcpy(&efi_data(op).u.get_next_variable_name.vendor_guid, vendor,
  120. sizeof(*vendor));
  121. if (HYPERVISOR_dom0_op(&op) < 0)
  122. return EFI_UNSUPPORTED;
  123. *name_size = efi_data(op).u.get_next_variable_name.size;
  124. memcpy(vendor, &efi_data(op).u.get_next_variable_name.vendor_guid,
  125. sizeof(*vendor));
  126. return efi_data(op).status;
  127. }
  128. static efi_status_t xen_efi_set_variable(efi_char16_t *name,
  129. efi_guid_t *vendor,
  130. u32 attr,
  131. unsigned long data_size,
  132. void *data)
  133. {
  134. struct xen_platform_op op = INIT_EFI_OP(set_variable);
  135. set_xen_guest_handle(efi_data(op).u.set_variable.name, name);
  136. efi_data(op).misc = attr;
  137. BUILD_BUG_ON(sizeof(*vendor) !=
  138. sizeof(efi_data(op).u.set_variable.vendor_guid));
  139. memcpy(&efi_data(op).u.set_variable.vendor_guid, vendor, sizeof(*vendor));
  140. efi_data(op).u.set_variable.size = data_size;
  141. set_xen_guest_handle(efi_data(op).u.set_variable.data, data);
  142. if (HYPERVISOR_dom0_op(&op) < 0)
  143. return EFI_UNSUPPORTED;
  144. return efi_data(op).status;
  145. }
  146. static efi_status_t xen_efi_query_variable_info(u32 attr,
  147. u64 *storage_space,
  148. u64 *remaining_space,
  149. u64 *max_variable_size)
  150. {
  151. struct xen_platform_op op = INIT_EFI_OP(query_variable_info);
  152. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  153. return EFI_UNSUPPORTED;
  154. efi_data(op).u.query_variable_info.attr = attr;
  155. if (HYPERVISOR_dom0_op(&op) < 0)
  156. return EFI_UNSUPPORTED;
  157. *storage_space = efi_data(op).u.query_variable_info.max_store_size;
  158. *remaining_space = efi_data(op).u.query_variable_info.remain_store_size;
  159. *max_variable_size = efi_data(op).u.query_variable_info.max_size;
  160. return efi_data(op).status;
  161. }
  162. static efi_status_t xen_efi_get_next_high_mono_count(u32 *count)
  163. {
  164. struct xen_platform_op op = INIT_EFI_OP(get_next_high_monotonic_count);
  165. if (HYPERVISOR_dom0_op(&op) < 0)
  166. return EFI_UNSUPPORTED;
  167. *count = efi_data(op).misc;
  168. return efi_data(op).status;
  169. }
  170. static efi_status_t xen_efi_update_capsule(efi_capsule_header_t **capsules,
  171. unsigned long count,
  172. unsigned long sg_list)
  173. {
  174. struct xen_platform_op op = INIT_EFI_OP(update_capsule);
  175. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  176. return EFI_UNSUPPORTED;
  177. set_xen_guest_handle(efi_data(op).u.update_capsule.capsule_header_array,
  178. capsules);
  179. efi_data(op).u.update_capsule.capsule_count = count;
  180. efi_data(op).u.update_capsule.sg_list = sg_list;
  181. if (HYPERVISOR_dom0_op(&op) < 0)
  182. return EFI_UNSUPPORTED;
  183. return efi_data(op).status;
  184. }
  185. static efi_status_t xen_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  186. unsigned long count,
  187. u64 *max_size,
  188. int *reset_type)
  189. {
  190. struct xen_platform_op op = INIT_EFI_OP(query_capsule_capabilities);
  191. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  192. return EFI_UNSUPPORTED;
  193. set_xen_guest_handle(efi_data(op).u.query_capsule_capabilities.capsule_header_array,
  194. capsules);
  195. efi_data(op).u.query_capsule_capabilities.capsule_count = count;
  196. if (HYPERVISOR_dom0_op(&op) < 0)
  197. return EFI_UNSUPPORTED;
  198. *max_size = efi_data(op).u.query_capsule_capabilities.max_capsule_size;
  199. *reset_type = efi_data(op).u.query_capsule_capabilities.reset_type;
  200. return efi_data(op).status;
  201. }
  202. static efi_char16_t vendor[100] __initdata;
  203. static efi_system_table_t efi_systab_xen __initdata = {
  204. .hdr = {
  205. .signature = EFI_SYSTEM_TABLE_SIGNATURE,
  206. .revision = 0, /* Initialized later. */
  207. .headersize = 0, /* Ignored by Linux Kernel. */
  208. .crc32 = 0, /* Ignored by Linux Kernel. */
  209. .reserved = 0
  210. },
  211. .fw_vendor = EFI_INVALID_TABLE_ADDR, /* Initialized later. */
  212. .fw_revision = 0, /* Initialized later. */
  213. .con_in_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  214. .con_in = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  215. .con_out_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  216. .con_out = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  217. .stderr_handle = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  218. .stderr = EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
  219. .runtime = (efi_runtime_services_t *)EFI_INVALID_TABLE_ADDR,
  220. /* Not used under Xen. */
  221. .boottime = (efi_boot_services_t *)EFI_INVALID_TABLE_ADDR,
  222. /* Not used under Xen. */
  223. .nr_tables = 0, /* Initialized later. */
  224. .tables = EFI_INVALID_TABLE_ADDR /* Initialized later. */
  225. };
  226. static const struct efi efi_xen __initconst = {
  227. .systab = NULL, /* Initialized later. */
  228. .runtime_version = 0, /* Initialized later. */
  229. .mps = EFI_INVALID_TABLE_ADDR,
  230. .acpi = EFI_INVALID_TABLE_ADDR,
  231. .acpi20 = EFI_INVALID_TABLE_ADDR,
  232. .smbios = EFI_INVALID_TABLE_ADDR,
  233. .sal_systab = EFI_INVALID_TABLE_ADDR,
  234. .boot_info = EFI_INVALID_TABLE_ADDR,
  235. .hcdp = EFI_INVALID_TABLE_ADDR,
  236. .uga = EFI_INVALID_TABLE_ADDR,
  237. .uv_systab = EFI_INVALID_TABLE_ADDR,
  238. .fw_vendor = EFI_INVALID_TABLE_ADDR,
  239. .runtime = EFI_INVALID_TABLE_ADDR,
  240. .config_table = EFI_INVALID_TABLE_ADDR,
  241. .get_time = xen_efi_get_time,
  242. .set_time = xen_efi_set_time,
  243. .get_wakeup_time = xen_efi_get_wakeup_time,
  244. .set_wakeup_time = xen_efi_set_wakeup_time,
  245. .get_variable = xen_efi_get_variable,
  246. .get_next_variable = xen_efi_get_next_variable,
  247. .set_variable = xen_efi_set_variable,
  248. .query_variable_info = xen_efi_query_variable_info,
  249. .update_capsule = xen_efi_update_capsule,
  250. .query_capsule_caps = xen_efi_query_capsule_caps,
  251. .get_next_high_mono_count = xen_efi_get_next_high_mono_count,
  252. .reset_system = NULL, /* Functionality provided by Xen. */
  253. .set_virtual_address_map = NULL, /* Not used under Xen. */
  254. .memmap = NULL, /* Not used under Xen. */
  255. .flags = 0 /* Initialized later. */
  256. };
  257. efi_system_table_t __init *xen_efi_probe(void)
  258. {
  259. struct xen_platform_op op = {
  260. .cmd = XENPF_firmware_info,
  261. .u.firmware_info = {
  262. .type = XEN_FW_EFI_INFO,
  263. .index = XEN_FW_EFI_CONFIG_TABLE
  264. }
  265. };
  266. union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
  267. if (!xen_initial_domain() || HYPERVISOR_dom0_op(&op) < 0)
  268. return NULL;
  269. /* Here we know that Xen runs on EFI platform. */
  270. efi = efi_xen;
  271. efi_systab_xen.tables = info->cfg.addr;
  272. efi_systab_xen.nr_tables = info->cfg.nent;
  273. op.cmd = XENPF_firmware_info;
  274. op.u.firmware_info.type = XEN_FW_EFI_INFO;
  275. op.u.firmware_info.index = XEN_FW_EFI_VENDOR;
  276. info->vendor.bufsz = sizeof(vendor);
  277. set_xen_guest_handle(info->vendor.name, vendor);
  278. if (HYPERVISOR_dom0_op(&op) == 0) {
  279. efi_systab_xen.fw_vendor = __pa_symbol(vendor);
  280. efi_systab_xen.fw_revision = info->vendor.revision;
  281. } else
  282. efi_systab_xen.fw_vendor = __pa_symbol(L"UNKNOWN");
  283. op.cmd = XENPF_firmware_info;
  284. op.u.firmware_info.type = XEN_FW_EFI_INFO;
  285. op.u.firmware_info.index = XEN_FW_EFI_VERSION;
  286. if (HYPERVISOR_dom0_op(&op) == 0)
  287. efi_systab_xen.hdr.revision = info->version;
  288. op.cmd = XENPF_firmware_info;
  289. op.u.firmware_info.type = XEN_FW_EFI_INFO;
  290. op.u.firmware_info.index = XEN_FW_EFI_RT_VERSION;
  291. if (HYPERVISOR_dom0_op(&op) == 0)
  292. efi.runtime_version = info->version;
  293. return &efi_systab_xen;
  294. }