efi.c 11 KB

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