efi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /*
  2. * Common EFI (Extensible Firmware Interface) support functions
  3. * Based on Extensible Firmware Interface Specification version 1.0
  4. *
  5. * Copyright (C) 1999 VA Linux Systems
  6. * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
  7. * Copyright (C) 1999-2002 Hewlett-Packard Co.
  8. * David Mosberger-Tang <davidm@hpl.hp.com>
  9. * Stephane Eranian <eranian@hpl.hp.com>
  10. * Copyright (C) 2005-2008 Intel Co.
  11. * Fenghua Yu <fenghua.yu@intel.com>
  12. * Bibo Mao <bibo.mao@intel.com>
  13. * Chandramouli Narayanan <mouli@linux.intel.com>
  14. * Huang Ying <ying.huang@intel.com>
  15. *
  16. * Copied from efi_32.c to eliminate the duplicated code between EFI
  17. * 32/64 support code. --ying 2007-10-26
  18. *
  19. * All EFI Runtime Services are not implemented yet as EFI only
  20. * supports physical mode addressing on SoftSDV. This is to be fixed
  21. * in a future version. --drummond 1999-07-20
  22. *
  23. * Implemented EFI runtime services and virtual mode calls. --davidm
  24. *
  25. * Goutham Rao: <goutham.rao@intel.com>
  26. * Skip non-WB memory and ignore empty memory ranges.
  27. */
  28. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  29. #include <linux/kernel.h>
  30. #include <linux/init.h>
  31. #include <linux/efi.h>
  32. #include <linux/efi-bgrt.h>
  33. #include <linux/export.h>
  34. #include <linux/bootmem.h>
  35. #include <linux/slab.h>
  36. #include <linux/memblock.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/time.h>
  40. #include <linux/io.h>
  41. #include <linux/reboot.h>
  42. #include <linux/bcd.h>
  43. #include <asm/setup.h>
  44. #include <asm/efi.h>
  45. #include <asm/time.h>
  46. #include <asm/cacheflush.h>
  47. #include <asm/tlbflush.h>
  48. #include <asm/x86_init.h>
  49. #include <asm/rtc.h>
  50. #define EFI_DEBUG 1
  51. #define EFI_MIN_RESERVE 5120
  52. #define EFI_DUMMY_GUID \
  53. EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
  54. static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
  55. struct efi_memory_map memmap;
  56. static struct efi efi_phys __initdata;
  57. static efi_system_table_t efi_systab __initdata;
  58. unsigned long x86_efi_facility;
  59. static __initdata efi_config_table_type_t arch_tables[] = {
  60. #ifdef CONFIG_X86_UV
  61. {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
  62. #endif
  63. {NULL_GUID, NULL, NULL},
  64. };
  65. /*
  66. * Returns 1 if 'facility' is enabled, 0 otherwise.
  67. */
  68. int efi_enabled(int facility)
  69. {
  70. return test_bit(facility, &x86_efi_facility) != 0;
  71. }
  72. EXPORT_SYMBOL(efi_enabled);
  73. static bool __initdata disable_runtime = false;
  74. static int __init setup_noefi(char *arg)
  75. {
  76. disable_runtime = true;
  77. return 0;
  78. }
  79. early_param("noefi", setup_noefi);
  80. int add_efi_memmap;
  81. EXPORT_SYMBOL(add_efi_memmap);
  82. static int __init setup_add_efi_memmap(char *arg)
  83. {
  84. add_efi_memmap = 1;
  85. return 0;
  86. }
  87. early_param("add_efi_memmap", setup_add_efi_memmap);
  88. static bool efi_no_storage_paranoia;
  89. static int __init setup_storage_paranoia(char *arg)
  90. {
  91. efi_no_storage_paranoia = true;
  92. return 0;
  93. }
  94. early_param("efi_no_storage_paranoia", setup_storage_paranoia);
  95. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  96. {
  97. unsigned long flags;
  98. efi_status_t status;
  99. spin_lock_irqsave(&rtc_lock, flags);
  100. status = efi_call_virt2(get_time, tm, tc);
  101. spin_unlock_irqrestore(&rtc_lock, flags);
  102. return status;
  103. }
  104. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  105. {
  106. unsigned long flags;
  107. efi_status_t status;
  108. spin_lock_irqsave(&rtc_lock, flags);
  109. status = efi_call_virt1(set_time, tm);
  110. spin_unlock_irqrestore(&rtc_lock, flags);
  111. return status;
  112. }
  113. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  114. efi_bool_t *pending,
  115. efi_time_t *tm)
  116. {
  117. unsigned long flags;
  118. efi_status_t status;
  119. spin_lock_irqsave(&rtc_lock, flags);
  120. status = efi_call_virt3(get_wakeup_time,
  121. enabled, pending, tm);
  122. spin_unlock_irqrestore(&rtc_lock, flags);
  123. return status;
  124. }
  125. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  126. {
  127. unsigned long flags;
  128. efi_status_t status;
  129. spin_lock_irqsave(&rtc_lock, flags);
  130. status = efi_call_virt2(set_wakeup_time,
  131. enabled, tm);
  132. spin_unlock_irqrestore(&rtc_lock, flags);
  133. return status;
  134. }
  135. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  136. efi_guid_t *vendor,
  137. u32 *attr,
  138. unsigned long *data_size,
  139. void *data)
  140. {
  141. return efi_call_virt5(get_variable,
  142. name, vendor, attr,
  143. data_size, data);
  144. }
  145. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  146. efi_char16_t *name,
  147. efi_guid_t *vendor)
  148. {
  149. return efi_call_virt3(get_next_variable,
  150. name_size, name, vendor);
  151. }
  152. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  153. efi_guid_t *vendor,
  154. u32 attr,
  155. unsigned long data_size,
  156. void *data)
  157. {
  158. return efi_call_virt5(set_variable,
  159. name, vendor, attr,
  160. data_size, data);
  161. }
  162. static efi_status_t virt_efi_query_variable_info(u32 attr,
  163. u64 *storage_space,
  164. u64 *remaining_space,
  165. u64 *max_variable_size)
  166. {
  167. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  168. return EFI_UNSUPPORTED;
  169. return efi_call_virt4(query_variable_info, attr, storage_space,
  170. remaining_space, max_variable_size);
  171. }
  172. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  173. {
  174. return efi_call_virt1(get_next_high_mono_count, count);
  175. }
  176. static void virt_efi_reset_system(int reset_type,
  177. efi_status_t status,
  178. unsigned long data_size,
  179. efi_char16_t *data)
  180. {
  181. efi_call_virt4(reset_system, reset_type, status,
  182. data_size, data);
  183. }
  184. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  185. unsigned long count,
  186. unsigned long sg_list)
  187. {
  188. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  189. return EFI_UNSUPPORTED;
  190. return efi_call_virt3(update_capsule, capsules, count, sg_list);
  191. }
  192. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  193. unsigned long count,
  194. u64 *max_size,
  195. int *reset_type)
  196. {
  197. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  198. return EFI_UNSUPPORTED;
  199. return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
  200. reset_type);
  201. }
  202. static efi_status_t __init phys_efi_set_virtual_address_map(
  203. unsigned long memory_map_size,
  204. unsigned long descriptor_size,
  205. u32 descriptor_version,
  206. efi_memory_desc_t *virtual_map)
  207. {
  208. efi_status_t status;
  209. efi_call_phys_prelog();
  210. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  211. memory_map_size, descriptor_size,
  212. descriptor_version, virtual_map);
  213. efi_call_phys_epilog();
  214. return status;
  215. }
  216. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  217. efi_time_cap_t *tc)
  218. {
  219. unsigned long flags;
  220. efi_status_t status;
  221. spin_lock_irqsave(&rtc_lock, flags);
  222. efi_call_phys_prelog();
  223. status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
  224. virt_to_phys(tc));
  225. efi_call_phys_epilog();
  226. spin_unlock_irqrestore(&rtc_lock, flags);
  227. return status;
  228. }
  229. int efi_set_rtc_mmss(const struct timespec *now)
  230. {
  231. unsigned long nowtime = now->tv_sec;
  232. efi_status_t status;
  233. efi_time_t eft;
  234. efi_time_cap_t cap;
  235. struct rtc_time tm;
  236. status = efi.get_time(&eft, &cap);
  237. if (status != EFI_SUCCESS) {
  238. pr_err("Oops: efitime: can't read time!\n");
  239. return -1;
  240. }
  241. rtc_time_to_tm(nowtime, &tm);
  242. if (!rtc_valid_tm(&tm)) {
  243. eft.year = tm.tm_year + 1900;
  244. eft.month = tm.tm_mon + 1;
  245. eft.day = tm.tm_mday;
  246. eft.minute = tm.tm_min;
  247. eft.second = tm.tm_sec;
  248. eft.nanosecond = 0;
  249. } else {
  250. printk(KERN_ERR
  251. "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
  252. __FUNCTION__, nowtime);
  253. return -1;
  254. }
  255. status = efi.set_time(&eft);
  256. if (status != EFI_SUCCESS) {
  257. pr_err("Oops: efitime: can't write time!\n");
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. void efi_get_time(struct timespec *now)
  263. {
  264. efi_status_t status;
  265. efi_time_t eft;
  266. efi_time_cap_t cap;
  267. status = efi.get_time(&eft, &cap);
  268. if (status != EFI_SUCCESS)
  269. pr_err("Oops: efitime: can't read time!\n");
  270. now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
  271. eft.minute, eft.second);
  272. now->tv_nsec = 0;
  273. }
  274. /*
  275. * Tell the kernel about the EFI memory map. This might include
  276. * more than the max 128 entries that can fit in the e820 legacy
  277. * (zeropage) memory map.
  278. */
  279. static void __init do_add_efi_memmap(void)
  280. {
  281. void *p;
  282. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  283. efi_memory_desc_t *md = p;
  284. unsigned long long start = md->phys_addr;
  285. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  286. int e820_type;
  287. switch (md->type) {
  288. case EFI_LOADER_CODE:
  289. case EFI_LOADER_DATA:
  290. case EFI_BOOT_SERVICES_CODE:
  291. case EFI_BOOT_SERVICES_DATA:
  292. case EFI_CONVENTIONAL_MEMORY:
  293. if (md->attribute & EFI_MEMORY_WB)
  294. e820_type = E820_RAM;
  295. else
  296. e820_type = E820_RESERVED;
  297. break;
  298. case EFI_ACPI_RECLAIM_MEMORY:
  299. e820_type = E820_ACPI;
  300. break;
  301. case EFI_ACPI_MEMORY_NVS:
  302. e820_type = E820_NVS;
  303. break;
  304. case EFI_UNUSABLE_MEMORY:
  305. e820_type = E820_UNUSABLE;
  306. break;
  307. default:
  308. /*
  309. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  310. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  311. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  312. */
  313. e820_type = E820_RESERVED;
  314. break;
  315. }
  316. e820_add_region(start, size, e820_type);
  317. }
  318. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  319. }
  320. int __init efi_memblock_x86_reserve_range(void)
  321. {
  322. struct efi_info *e = &boot_params.efi_info;
  323. unsigned long pmap;
  324. #ifdef CONFIG_X86_32
  325. /* Can't handle data above 4GB at this time */
  326. if (e->efi_memmap_hi) {
  327. pr_err("Memory map is above 4GB, disabling EFI.\n");
  328. return -EINVAL;
  329. }
  330. pmap = e->efi_memmap;
  331. #else
  332. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  333. #endif
  334. memmap.phys_map = (void *)pmap;
  335. memmap.nr_map = e->efi_memmap_size /
  336. e->efi_memdesc_size;
  337. memmap.desc_size = e->efi_memdesc_size;
  338. memmap.desc_version = e->efi_memdesc_version;
  339. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  340. efi.memmap = &memmap;
  341. return 0;
  342. }
  343. #if EFI_DEBUG
  344. static void __init print_efi_memmap(void)
  345. {
  346. efi_memory_desc_t *md;
  347. void *p;
  348. int i;
  349. for (p = memmap.map, i = 0;
  350. p < memmap.map_end;
  351. p += memmap.desc_size, i++) {
  352. md = p;
  353. pr_info("mem%02u: type=%u, attr=0x%llx, "
  354. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  355. i, md->type, md->attribute, md->phys_addr,
  356. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  357. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  358. }
  359. }
  360. #endif /* EFI_DEBUG */
  361. void __init efi_reserve_boot_services(void)
  362. {
  363. void *p;
  364. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  365. efi_memory_desc_t *md = p;
  366. u64 start = md->phys_addr;
  367. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  368. if (md->type != EFI_BOOT_SERVICES_CODE &&
  369. md->type != EFI_BOOT_SERVICES_DATA)
  370. continue;
  371. /* Only reserve where possible:
  372. * - Not within any already allocated areas
  373. * - Not over any memory area (really needed, if above?)
  374. * - Not within any part of the kernel
  375. * - Not the bios reserved area
  376. */
  377. if ((start+size >= __pa_symbol(_text)
  378. && start <= __pa_symbol(_end)) ||
  379. !e820_all_mapped(start, start+size, E820_RAM) ||
  380. memblock_is_region_reserved(start, size)) {
  381. /* Could not reserve, skip it */
  382. md->num_pages = 0;
  383. memblock_dbg("Could not reserve boot range "
  384. "[0x%010llx-0x%010llx]\n",
  385. start, start+size-1);
  386. } else
  387. memblock_reserve(start, size);
  388. }
  389. }
  390. void __init efi_unmap_memmap(void)
  391. {
  392. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  393. if (memmap.map) {
  394. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  395. memmap.map = NULL;
  396. }
  397. }
  398. void __init efi_free_boot_services(void)
  399. {
  400. void *p;
  401. if (!efi_is_native())
  402. return;
  403. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  404. efi_memory_desc_t *md = p;
  405. unsigned long long start = md->phys_addr;
  406. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  407. if (md->type != EFI_BOOT_SERVICES_CODE &&
  408. md->type != EFI_BOOT_SERVICES_DATA)
  409. continue;
  410. /* Could not reserve boot area */
  411. if (!size)
  412. continue;
  413. free_bootmem_late(start, size);
  414. }
  415. efi_unmap_memmap();
  416. }
  417. static int __init efi_systab_init(void *phys)
  418. {
  419. if (efi_enabled(EFI_64BIT)) {
  420. efi_system_table_64_t *systab64;
  421. u64 tmp = 0;
  422. systab64 = early_ioremap((unsigned long)phys,
  423. sizeof(*systab64));
  424. if (systab64 == NULL) {
  425. pr_err("Couldn't map the system table!\n");
  426. return -ENOMEM;
  427. }
  428. efi_systab.hdr = systab64->hdr;
  429. efi_systab.fw_vendor = systab64->fw_vendor;
  430. tmp |= systab64->fw_vendor;
  431. efi_systab.fw_revision = systab64->fw_revision;
  432. efi_systab.con_in_handle = systab64->con_in_handle;
  433. tmp |= systab64->con_in_handle;
  434. efi_systab.con_in = systab64->con_in;
  435. tmp |= systab64->con_in;
  436. efi_systab.con_out_handle = systab64->con_out_handle;
  437. tmp |= systab64->con_out_handle;
  438. efi_systab.con_out = systab64->con_out;
  439. tmp |= systab64->con_out;
  440. efi_systab.stderr_handle = systab64->stderr_handle;
  441. tmp |= systab64->stderr_handle;
  442. efi_systab.stderr = systab64->stderr;
  443. tmp |= systab64->stderr;
  444. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  445. tmp |= systab64->runtime;
  446. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  447. tmp |= systab64->boottime;
  448. efi_systab.nr_tables = systab64->nr_tables;
  449. efi_systab.tables = systab64->tables;
  450. tmp |= systab64->tables;
  451. early_iounmap(systab64, sizeof(*systab64));
  452. #ifdef CONFIG_X86_32
  453. if (tmp >> 32) {
  454. pr_err("EFI data located above 4GB, disabling EFI.\n");
  455. return -EINVAL;
  456. }
  457. #endif
  458. } else {
  459. efi_system_table_32_t *systab32;
  460. systab32 = early_ioremap((unsigned long)phys,
  461. sizeof(*systab32));
  462. if (systab32 == NULL) {
  463. pr_err("Couldn't map the system table!\n");
  464. return -ENOMEM;
  465. }
  466. efi_systab.hdr = systab32->hdr;
  467. efi_systab.fw_vendor = systab32->fw_vendor;
  468. efi_systab.fw_revision = systab32->fw_revision;
  469. efi_systab.con_in_handle = systab32->con_in_handle;
  470. efi_systab.con_in = systab32->con_in;
  471. efi_systab.con_out_handle = systab32->con_out_handle;
  472. efi_systab.con_out = systab32->con_out;
  473. efi_systab.stderr_handle = systab32->stderr_handle;
  474. efi_systab.stderr = systab32->stderr;
  475. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  476. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  477. efi_systab.nr_tables = systab32->nr_tables;
  478. efi_systab.tables = systab32->tables;
  479. early_iounmap(systab32, sizeof(*systab32));
  480. }
  481. efi.systab = &efi_systab;
  482. /*
  483. * Verify the EFI Table
  484. */
  485. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  486. pr_err("System table signature incorrect!\n");
  487. return -EINVAL;
  488. }
  489. if ((efi.systab->hdr.revision >> 16) == 0)
  490. pr_err("Warning: System table version "
  491. "%d.%02d, expected 1.00 or greater!\n",
  492. efi.systab->hdr.revision >> 16,
  493. efi.systab->hdr.revision & 0xffff);
  494. return 0;
  495. }
  496. static int __init efi_runtime_init(void)
  497. {
  498. efi_runtime_services_t *runtime;
  499. /*
  500. * Check out the runtime services table. We need to map
  501. * the runtime services table so that we can grab the physical
  502. * address of several of the EFI runtime functions, needed to
  503. * set the firmware into virtual mode.
  504. */
  505. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  506. sizeof(efi_runtime_services_t));
  507. if (!runtime) {
  508. pr_err("Could not map the runtime service table!\n");
  509. return -ENOMEM;
  510. }
  511. /*
  512. * We will only need *early* access to the following
  513. * two EFI runtime services before set_virtual_address_map
  514. * is invoked.
  515. */
  516. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  517. efi_phys.set_virtual_address_map =
  518. (efi_set_virtual_address_map_t *)
  519. runtime->set_virtual_address_map;
  520. /*
  521. * Make efi_get_time can be called before entering
  522. * virtual mode.
  523. */
  524. efi.get_time = phys_efi_get_time;
  525. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  526. return 0;
  527. }
  528. static int __init efi_memmap_init(void)
  529. {
  530. /* Map the EFI memory map */
  531. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  532. memmap.nr_map * memmap.desc_size);
  533. if (memmap.map == NULL) {
  534. pr_err("Could not map the memory map!\n");
  535. return -ENOMEM;
  536. }
  537. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  538. if (add_efi_memmap)
  539. do_add_efi_memmap();
  540. return 0;
  541. }
  542. void __init efi_init(void)
  543. {
  544. efi_char16_t *c16;
  545. char vendor[100] = "unknown";
  546. int i = 0;
  547. void *tmp;
  548. #ifdef CONFIG_X86_32
  549. if (boot_params.efi_info.efi_systab_hi ||
  550. boot_params.efi_info.efi_memmap_hi) {
  551. pr_info("Table located above 4GB, disabling EFI.\n");
  552. return;
  553. }
  554. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  555. #else
  556. efi_phys.systab = (efi_system_table_t *)
  557. (boot_params.efi_info.efi_systab |
  558. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  559. #endif
  560. if (efi_systab_init(efi_phys.systab))
  561. return;
  562. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  563. /*
  564. * Show what we know for posterity
  565. */
  566. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  567. if (c16) {
  568. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  569. vendor[i] = *c16++;
  570. vendor[i] = '\0';
  571. } else
  572. pr_err("Could not map the firmware vendor!\n");
  573. early_iounmap(tmp, 2);
  574. pr_info("EFI v%u.%.02u by %s\n",
  575. efi.systab->hdr.revision >> 16,
  576. efi.systab->hdr.revision & 0xffff, vendor);
  577. if (efi_config_init(arch_tables))
  578. return;
  579. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  580. /*
  581. * Note: We currently don't support runtime services on an EFI
  582. * that doesn't match the kernel 32/64-bit mode.
  583. */
  584. if (!efi_is_native())
  585. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  586. else {
  587. if (disable_runtime || efi_runtime_init())
  588. return;
  589. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  590. }
  591. if (efi_memmap_init())
  592. return;
  593. set_bit(EFI_MEMMAP, &x86_efi_facility);
  594. #if EFI_DEBUG
  595. print_efi_memmap();
  596. #endif
  597. }
  598. void __init efi_late_init(void)
  599. {
  600. efi_bgrt_init();
  601. }
  602. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  603. {
  604. u64 addr, npages;
  605. addr = md->virt_addr;
  606. npages = md->num_pages;
  607. memrange_efi_to_native(&addr, &npages);
  608. if (executable)
  609. set_memory_x(addr, npages);
  610. else
  611. set_memory_nx(addr, npages);
  612. }
  613. static void __init runtime_code_page_mkexec(void)
  614. {
  615. efi_memory_desc_t *md;
  616. void *p;
  617. /* Make EFI runtime service code area executable */
  618. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  619. md = p;
  620. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  621. continue;
  622. efi_set_executable(md, true);
  623. }
  624. }
  625. void efi_memory_uc(u64 addr, unsigned long size)
  626. {
  627. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  628. u64 npages;
  629. npages = round_up(size, page_shift) / page_shift;
  630. memrange_efi_to_native(&addr, &npages);
  631. set_memory_uc(addr, npages);
  632. }
  633. /*
  634. * This function will switch the EFI runtime services to virtual mode.
  635. * Essentially, look through the EFI memmap and map every region that
  636. * has the runtime attribute bit set in its memory descriptor and update
  637. * that memory descriptor with the virtual address obtained from ioremap().
  638. * This enables the runtime services to be called without having to
  639. * thunk back into physical mode for every invocation.
  640. */
  641. void __init efi_enter_virtual_mode(void)
  642. {
  643. efi_memory_desc_t *md, *prev_md = NULL;
  644. efi_status_t status;
  645. unsigned long size;
  646. u64 end, systab, start_pfn, end_pfn;
  647. void *p, *va, *new_memmap = NULL;
  648. int count = 0;
  649. efi.systab = NULL;
  650. /*
  651. * We don't do virtual mode, since we don't do runtime services, on
  652. * non-native EFI
  653. */
  654. if (!efi_is_native()) {
  655. efi_unmap_memmap();
  656. return;
  657. }
  658. /* Merge contiguous regions of the same type and attribute */
  659. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  660. u64 prev_size;
  661. md = p;
  662. if (!prev_md) {
  663. prev_md = md;
  664. continue;
  665. }
  666. if (prev_md->type != md->type ||
  667. prev_md->attribute != md->attribute) {
  668. prev_md = md;
  669. continue;
  670. }
  671. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  672. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  673. prev_md->num_pages += md->num_pages;
  674. md->type = EFI_RESERVED_TYPE;
  675. md->attribute = 0;
  676. continue;
  677. }
  678. prev_md = md;
  679. }
  680. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  681. md = p;
  682. if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
  683. #ifdef CONFIG_X86_64
  684. if (md->type != EFI_BOOT_SERVICES_CODE &&
  685. md->type != EFI_BOOT_SERVICES_DATA)
  686. #endif
  687. continue;
  688. }
  689. size = md->num_pages << EFI_PAGE_SHIFT;
  690. end = md->phys_addr + size;
  691. start_pfn = PFN_DOWN(md->phys_addr);
  692. end_pfn = PFN_UP(end);
  693. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  694. va = __va(md->phys_addr);
  695. if (!(md->attribute & EFI_MEMORY_WB))
  696. efi_memory_uc((u64)(unsigned long)va, size);
  697. } else
  698. va = efi_ioremap(md->phys_addr, size,
  699. md->type, md->attribute);
  700. md->virt_addr = (u64) (unsigned long) va;
  701. if (!va) {
  702. pr_err("ioremap of 0x%llX failed!\n",
  703. (unsigned long long)md->phys_addr);
  704. continue;
  705. }
  706. systab = (u64) (unsigned long) efi_phys.systab;
  707. if (md->phys_addr <= systab && systab < end) {
  708. systab += md->virt_addr - md->phys_addr;
  709. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  710. }
  711. new_memmap = krealloc(new_memmap,
  712. (count + 1) * memmap.desc_size,
  713. GFP_KERNEL);
  714. memcpy(new_memmap + (count * memmap.desc_size), md,
  715. memmap.desc_size);
  716. count++;
  717. }
  718. BUG_ON(!efi.systab);
  719. status = phys_efi_set_virtual_address_map(
  720. memmap.desc_size * count,
  721. memmap.desc_size,
  722. memmap.desc_version,
  723. (efi_memory_desc_t *)__pa(new_memmap));
  724. if (status != EFI_SUCCESS) {
  725. pr_alert("Unable to switch EFI into virtual mode "
  726. "(status=%lx)!\n", status);
  727. panic("EFI call to SetVirtualAddressMap() failed!");
  728. }
  729. /*
  730. * Now that EFI is in virtual mode, update the function
  731. * pointers in the runtime service table to the new virtual addresses.
  732. *
  733. * Call EFI services through wrapper functions.
  734. */
  735. efi.runtime_version = efi_systab.hdr.revision;
  736. efi.get_time = virt_efi_get_time;
  737. efi.set_time = virt_efi_set_time;
  738. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  739. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  740. efi.get_variable = virt_efi_get_variable;
  741. efi.get_next_variable = virt_efi_get_next_variable;
  742. efi.set_variable = virt_efi_set_variable;
  743. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  744. efi.reset_system = virt_efi_reset_system;
  745. efi.set_virtual_address_map = NULL;
  746. efi.query_variable_info = virt_efi_query_variable_info;
  747. efi.update_capsule = virt_efi_update_capsule;
  748. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  749. if (__supported_pte_mask & _PAGE_NX)
  750. runtime_code_page_mkexec();
  751. kfree(new_memmap);
  752. /* clean DUMMY object */
  753. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  754. EFI_VARIABLE_NON_VOLATILE |
  755. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  756. EFI_VARIABLE_RUNTIME_ACCESS,
  757. 0, NULL);
  758. }
  759. /*
  760. * Convenience functions to obtain memory types and attributes
  761. */
  762. u32 efi_mem_type(unsigned long phys_addr)
  763. {
  764. efi_memory_desc_t *md;
  765. void *p;
  766. if (!efi_enabled(EFI_MEMMAP))
  767. return 0;
  768. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  769. md = p;
  770. if ((md->phys_addr <= phys_addr) &&
  771. (phys_addr < (md->phys_addr +
  772. (md->num_pages << EFI_PAGE_SHIFT))))
  773. return md->type;
  774. }
  775. return 0;
  776. }
  777. u64 efi_mem_attributes(unsigned long phys_addr)
  778. {
  779. efi_memory_desc_t *md;
  780. void *p;
  781. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  782. md = p;
  783. if ((md->phys_addr <= phys_addr) &&
  784. (phys_addr < (md->phys_addr +
  785. (md->num_pages << EFI_PAGE_SHIFT))))
  786. return md->attribute;
  787. }
  788. return 0;
  789. }
  790. /*
  791. * Some firmware has serious problems when using more than 50% of the EFI
  792. * variable store, i.e. it triggers bugs that can brick machines. Ensure that
  793. * we never use more than this safe limit.
  794. *
  795. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  796. * store.
  797. */
  798. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
  799. {
  800. efi_status_t status;
  801. u64 storage_size, remaining_size, max_size;
  802. if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
  803. return 0;
  804. status = efi.query_variable_info(attributes, &storage_size,
  805. &remaining_size, &max_size);
  806. if (status != EFI_SUCCESS)
  807. return status;
  808. /*
  809. * Some firmware implementations refuse to boot if there's insufficient
  810. * space in the variable store. We account for that by refusing the
  811. * write if permitting it would reduce the available space to under
  812. * 5KB. This figure was provided by Samsung, so should be safe.
  813. */
  814. if ((remaining_size - size < EFI_MIN_RESERVE) &&
  815. !efi_no_storage_paranoia) {
  816. /*
  817. * Triggering garbage collection may require that the firmware
  818. * generate a real EFI_OUT_OF_RESOURCES error. We can force
  819. * that by attempting to use more space than is available.
  820. */
  821. unsigned long dummy_size = remaining_size + 1024;
  822. void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
  823. if (!dummy)
  824. return EFI_OUT_OF_RESOURCES;
  825. status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  826. EFI_VARIABLE_NON_VOLATILE |
  827. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  828. EFI_VARIABLE_RUNTIME_ACCESS,
  829. dummy_size, dummy);
  830. if (status == EFI_SUCCESS) {
  831. /*
  832. * This should have failed, so if it didn't make sure
  833. * that we delete it...
  834. */
  835. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  836. EFI_VARIABLE_NON_VOLATILE |
  837. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  838. EFI_VARIABLE_RUNTIME_ACCESS,
  839. 0, dummy);
  840. }
  841. kfree(dummy);
  842. /*
  843. * The runtime code may now have triggered a garbage collection
  844. * run, so check the variable info again
  845. */
  846. status = efi.query_variable_info(attributes, &storage_size,
  847. &remaining_size, &max_size);
  848. if (status != EFI_SUCCESS)
  849. return status;
  850. /*
  851. * There still isn't enough room, so return an error
  852. */
  853. if (remaining_size - size < EFI_MIN_RESERVE)
  854. return EFI_OUT_OF_RESOURCES;
  855. }
  856. return EFI_SUCCESS;
  857. }
  858. EXPORT_SYMBOL_GPL(efi_query_variable_store);