efi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. #ifdef CONFIG_X86_32
  595. if (efi_is_native()) {
  596. x86_platform.get_wallclock = efi_get_time;
  597. x86_platform.set_wallclock = efi_set_rtc_mmss;
  598. }
  599. #endif
  600. #if EFI_DEBUG
  601. print_efi_memmap();
  602. #endif
  603. }
  604. void __init efi_late_init(void)
  605. {
  606. efi_bgrt_init();
  607. }
  608. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  609. {
  610. u64 addr, npages;
  611. addr = md->virt_addr;
  612. npages = md->num_pages;
  613. memrange_efi_to_native(&addr, &npages);
  614. if (executable)
  615. set_memory_x(addr, npages);
  616. else
  617. set_memory_nx(addr, npages);
  618. }
  619. static void __init runtime_code_page_mkexec(void)
  620. {
  621. efi_memory_desc_t *md;
  622. void *p;
  623. /* Make EFI runtime service code area executable */
  624. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  625. md = p;
  626. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  627. continue;
  628. efi_set_executable(md, true);
  629. }
  630. }
  631. void efi_memory_uc(u64 addr, unsigned long size)
  632. {
  633. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  634. u64 npages;
  635. npages = round_up(size, page_shift) / page_shift;
  636. memrange_efi_to_native(&addr, &npages);
  637. set_memory_uc(addr, npages);
  638. }
  639. /*
  640. * This function will switch the EFI runtime services to virtual mode.
  641. * Essentially, look through the EFI memmap and map every region that
  642. * has the runtime attribute bit set in its memory descriptor and update
  643. * that memory descriptor with the virtual address obtained from ioremap().
  644. * This enables the runtime services to be called without having to
  645. * thunk back into physical mode for every invocation.
  646. */
  647. void __init efi_enter_virtual_mode(void)
  648. {
  649. efi_memory_desc_t *md, *prev_md = NULL;
  650. efi_status_t status;
  651. unsigned long size;
  652. u64 end, systab, start_pfn, end_pfn;
  653. void *p, *va, *new_memmap = NULL;
  654. int count = 0;
  655. efi.systab = NULL;
  656. /*
  657. * We don't do virtual mode, since we don't do runtime services, on
  658. * non-native EFI
  659. */
  660. if (!efi_is_native()) {
  661. efi_unmap_memmap();
  662. return;
  663. }
  664. /* Merge contiguous regions of the same type and attribute */
  665. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  666. u64 prev_size;
  667. md = p;
  668. if (!prev_md) {
  669. prev_md = md;
  670. continue;
  671. }
  672. if (prev_md->type != md->type ||
  673. prev_md->attribute != md->attribute) {
  674. prev_md = md;
  675. continue;
  676. }
  677. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  678. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  679. prev_md->num_pages += md->num_pages;
  680. md->type = EFI_RESERVED_TYPE;
  681. md->attribute = 0;
  682. continue;
  683. }
  684. prev_md = md;
  685. }
  686. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  687. md = p;
  688. if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
  689. #ifdef CONFIG_X86_64
  690. if (md->type != EFI_BOOT_SERVICES_CODE &&
  691. md->type != EFI_BOOT_SERVICES_DATA)
  692. #endif
  693. continue;
  694. }
  695. size = md->num_pages << EFI_PAGE_SHIFT;
  696. end = md->phys_addr + size;
  697. start_pfn = PFN_DOWN(md->phys_addr);
  698. end_pfn = PFN_UP(end);
  699. if (pfn_range_is_mapped(start_pfn, end_pfn)) {
  700. va = __va(md->phys_addr);
  701. if (!(md->attribute & EFI_MEMORY_WB))
  702. efi_memory_uc((u64)(unsigned long)va, size);
  703. } else
  704. va = efi_ioremap(md->phys_addr, size,
  705. md->type, md->attribute);
  706. md->virt_addr = (u64) (unsigned long) va;
  707. if (!va) {
  708. pr_err("ioremap of 0x%llX failed!\n",
  709. (unsigned long long)md->phys_addr);
  710. continue;
  711. }
  712. systab = (u64) (unsigned long) efi_phys.systab;
  713. if (md->phys_addr <= systab && systab < end) {
  714. systab += md->virt_addr - md->phys_addr;
  715. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  716. }
  717. new_memmap = krealloc(new_memmap,
  718. (count + 1) * memmap.desc_size,
  719. GFP_KERNEL);
  720. memcpy(new_memmap + (count * memmap.desc_size), md,
  721. memmap.desc_size);
  722. count++;
  723. }
  724. BUG_ON(!efi.systab);
  725. status = phys_efi_set_virtual_address_map(
  726. memmap.desc_size * count,
  727. memmap.desc_size,
  728. memmap.desc_version,
  729. (efi_memory_desc_t *)__pa(new_memmap));
  730. if (status != EFI_SUCCESS) {
  731. pr_alert("Unable to switch EFI into virtual mode "
  732. "(status=%lx)!\n", status);
  733. panic("EFI call to SetVirtualAddressMap() failed!");
  734. }
  735. /*
  736. * Now that EFI is in virtual mode, update the function
  737. * pointers in the runtime service table to the new virtual addresses.
  738. *
  739. * Call EFI services through wrapper functions.
  740. */
  741. efi.runtime_version = efi_systab.hdr.revision;
  742. efi.get_time = virt_efi_get_time;
  743. efi.set_time = virt_efi_set_time;
  744. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  745. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  746. efi.get_variable = virt_efi_get_variable;
  747. efi.get_next_variable = virt_efi_get_next_variable;
  748. efi.set_variable = virt_efi_set_variable;
  749. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  750. efi.reset_system = virt_efi_reset_system;
  751. efi.set_virtual_address_map = NULL;
  752. efi.query_variable_info = virt_efi_query_variable_info;
  753. efi.update_capsule = virt_efi_update_capsule;
  754. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  755. if (__supported_pte_mask & _PAGE_NX)
  756. runtime_code_page_mkexec();
  757. kfree(new_memmap);
  758. /* clean DUMMY object */
  759. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  760. EFI_VARIABLE_NON_VOLATILE |
  761. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  762. EFI_VARIABLE_RUNTIME_ACCESS,
  763. 0, NULL);
  764. }
  765. /*
  766. * Convenience functions to obtain memory types and attributes
  767. */
  768. u32 efi_mem_type(unsigned long phys_addr)
  769. {
  770. efi_memory_desc_t *md;
  771. void *p;
  772. if (!efi_enabled(EFI_MEMMAP))
  773. return 0;
  774. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  775. md = p;
  776. if ((md->phys_addr <= phys_addr) &&
  777. (phys_addr < (md->phys_addr +
  778. (md->num_pages << EFI_PAGE_SHIFT))))
  779. return md->type;
  780. }
  781. return 0;
  782. }
  783. u64 efi_mem_attributes(unsigned long phys_addr)
  784. {
  785. efi_memory_desc_t *md;
  786. void *p;
  787. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  788. md = p;
  789. if ((md->phys_addr <= phys_addr) &&
  790. (phys_addr < (md->phys_addr +
  791. (md->num_pages << EFI_PAGE_SHIFT))))
  792. return md->attribute;
  793. }
  794. return 0;
  795. }
  796. /*
  797. * Some firmware has serious problems when using more than 50% of the EFI
  798. * variable store, i.e. it triggers bugs that can brick machines. Ensure that
  799. * we never use more than this safe limit.
  800. *
  801. * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
  802. * store.
  803. */
  804. efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
  805. {
  806. efi_status_t status;
  807. u64 storage_size, remaining_size, max_size;
  808. if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
  809. return 0;
  810. status = efi.query_variable_info(attributes, &storage_size,
  811. &remaining_size, &max_size);
  812. if (status != EFI_SUCCESS)
  813. return status;
  814. /*
  815. * Some firmware implementations refuse to boot if there's insufficient
  816. * space in the variable store. We account for that by refusing the
  817. * write if permitting it would reduce the available space to under
  818. * 5KB. This figure was provided by Samsung, so should be safe.
  819. */
  820. if ((remaining_size - size < EFI_MIN_RESERVE) &&
  821. !efi_no_storage_paranoia) {
  822. /*
  823. * Triggering garbage collection may require that the firmware
  824. * generate a real EFI_OUT_OF_RESOURCES error. We can force
  825. * that by attempting to use more space than is available.
  826. */
  827. unsigned long dummy_size = remaining_size + 1024;
  828. void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
  829. if (!dummy)
  830. return EFI_OUT_OF_RESOURCES;
  831. status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  832. EFI_VARIABLE_NON_VOLATILE |
  833. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  834. EFI_VARIABLE_RUNTIME_ACCESS,
  835. dummy_size, dummy);
  836. if (status == EFI_SUCCESS) {
  837. /*
  838. * This should have failed, so if it didn't make sure
  839. * that we delete it...
  840. */
  841. efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
  842. EFI_VARIABLE_NON_VOLATILE |
  843. EFI_VARIABLE_BOOTSERVICE_ACCESS |
  844. EFI_VARIABLE_RUNTIME_ACCESS,
  845. 0, dummy);
  846. }
  847. kfree(dummy);
  848. /*
  849. * The runtime code may now have triggered a garbage collection
  850. * run, so check the variable info again
  851. */
  852. status = efi.query_variable_info(attributes, &storage_size,
  853. &remaining_size, &max_size);
  854. if (status != EFI_SUCCESS)
  855. return status;
  856. /*
  857. * There still isn't enough room, so return an error
  858. */
  859. if (remaining_size - size < EFI_MIN_RESERVE)
  860. return EFI_OUT_OF_RESOURCES;
  861. }
  862. return EFI_SUCCESS;
  863. }
  864. EXPORT_SYMBOL_GPL(efi_query_variable_store);