efi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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/memblock.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/time.h>
  39. #include <linux/io.h>
  40. #include <linux/reboot.h>
  41. #include <linux/bcd.h>
  42. #include <asm/setup.h>
  43. #include <asm/efi.h>
  44. #include <asm/time.h>
  45. #include <asm/cacheflush.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/x86_init.h>
  48. #define EFI_DEBUG 1
  49. struct efi __read_mostly efi = {
  50. .mps = EFI_INVALID_TABLE_ADDR,
  51. .acpi = EFI_INVALID_TABLE_ADDR,
  52. .acpi20 = EFI_INVALID_TABLE_ADDR,
  53. .smbios = EFI_INVALID_TABLE_ADDR,
  54. .sal_systab = EFI_INVALID_TABLE_ADDR,
  55. .boot_info = EFI_INVALID_TABLE_ADDR,
  56. .hcdp = EFI_INVALID_TABLE_ADDR,
  57. .uga = EFI_INVALID_TABLE_ADDR,
  58. .uv_systab = EFI_INVALID_TABLE_ADDR,
  59. };
  60. EXPORT_SYMBOL(efi);
  61. struct efi_memory_map memmap;
  62. static struct efi efi_phys __initdata;
  63. static efi_system_table_t efi_systab __initdata;
  64. unsigned long x86_efi_facility;
  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 int __init setup_noefi(char *arg)
  74. {
  75. clear_bit(EFI_BOOT, &x86_efi_facility);
  76. return 0;
  77. }
  78. early_param("noefi", setup_noefi);
  79. int add_efi_memmap;
  80. EXPORT_SYMBOL(add_efi_memmap);
  81. static int __init setup_add_efi_memmap(char *arg)
  82. {
  83. add_efi_memmap = 1;
  84. return 0;
  85. }
  86. early_param("add_efi_memmap", setup_add_efi_memmap);
  87. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  88. {
  89. unsigned long flags;
  90. efi_status_t status;
  91. spin_lock_irqsave(&rtc_lock, flags);
  92. status = efi_call_virt2(get_time, tm, tc);
  93. spin_unlock_irqrestore(&rtc_lock, flags);
  94. return status;
  95. }
  96. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  97. {
  98. unsigned long flags;
  99. efi_status_t status;
  100. spin_lock_irqsave(&rtc_lock, flags);
  101. status = efi_call_virt1(set_time, tm);
  102. spin_unlock_irqrestore(&rtc_lock, flags);
  103. return status;
  104. }
  105. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  106. efi_bool_t *pending,
  107. efi_time_t *tm)
  108. {
  109. unsigned long flags;
  110. efi_status_t status;
  111. spin_lock_irqsave(&rtc_lock, flags);
  112. status = efi_call_virt3(get_wakeup_time,
  113. enabled, pending, tm);
  114. spin_unlock_irqrestore(&rtc_lock, flags);
  115. return status;
  116. }
  117. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  118. {
  119. unsigned long flags;
  120. efi_status_t status;
  121. spin_lock_irqsave(&rtc_lock, flags);
  122. status = efi_call_virt2(set_wakeup_time,
  123. enabled, tm);
  124. spin_unlock_irqrestore(&rtc_lock, flags);
  125. return status;
  126. }
  127. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  128. efi_guid_t *vendor,
  129. u32 *attr,
  130. unsigned long *data_size,
  131. void *data)
  132. {
  133. return efi_call_virt5(get_variable,
  134. name, vendor, attr,
  135. data_size, data);
  136. }
  137. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  138. efi_char16_t *name,
  139. efi_guid_t *vendor)
  140. {
  141. return efi_call_virt3(get_next_variable,
  142. name_size, name, vendor);
  143. }
  144. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  145. efi_guid_t *vendor,
  146. u32 attr,
  147. unsigned long data_size,
  148. void *data)
  149. {
  150. return efi_call_virt5(set_variable,
  151. name, vendor, attr,
  152. data_size, data);
  153. }
  154. static efi_status_t virt_efi_query_variable_info(u32 attr,
  155. u64 *storage_space,
  156. u64 *remaining_space,
  157. u64 *max_variable_size)
  158. {
  159. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  160. return EFI_UNSUPPORTED;
  161. return efi_call_virt4(query_variable_info, attr, storage_space,
  162. remaining_space, max_variable_size);
  163. }
  164. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  165. {
  166. return efi_call_virt1(get_next_high_mono_count, count);
  167. }
  168. static void virt_efi_reset_system(int reset_type,
  169. efi_status_t status,
  170. unsigned long data_size,
  171. efi_char16_t *data)
  172. {
  173. efi_call_virt4(reset_system, reset_type, status,
  174. data_size, data);
  175. }
  176. static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
  177. unsigned long count,
  178. unsigned long sg_list)
  179. {
  180. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  181. return EFI_UNSUPPORTED;
  182. return efi_call_virt3(update_capsule, capsules, count, sg_list);
  183. }
  184. static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
  185. unsigned long count,
  186. u64 *max_size,
  187. int *reset_type)
  188. {
  189. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  190. return EFI_UNSUPPORTED;
  191. return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
  192. reset_type);
  193. }
  194. static efi_status_t __init phys_efi_set_virtual_address_map(
  195. unsigned long memory_map_size,
  196. unsigned long descriptor_size,
  197. u32 descriptor_version,
  198. efi_memory_desc_t *virtual_map)
  199. {
  200. efi_status_t status;
  201. efi_call_phys_prelog();
  202. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  203. memory_map_size, descriptor_size,
  204. descriptor_version, virtual_map);
  205. efi_call_phys_epilog();
  206. return status;
  207. }
  208. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  209. efi_time_cap_t *tc)
  210. {
  211. unsigned long flags;
  212. efi_status_t status;
  213. spin_lock_irqsave(&rtc_lock, flags);
  214. efi_call_phys_prelog();
  215. status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
  216. virt_to_phys(tc));
  217. efi_call_phys_epilog();
  218. spin_unlock_irqrestore(&rtc_lock, flags);
  219. return status;
  220. }
  221. int efi_set_rtc_mmss(unsigned long nowtime)
  222. {
  223. int real_seconds, real_minutes;
  224. efi_status_t status;
  225. efi_time_t eft;
  226. efi_time_cap_t cap;
  227. status = efi.get_time(&eft, &cap);
  228. if (status != EFI_SUCCESS) {
  229. pr_err("Oops: efitime: can't read time!\n");
  230. return -1;
  231. }
  232. real_seconds = nowtime % 60;
  233. real_minutes = nowtime / 60;
  234. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  235. real_minutes += 30;
  236. real_minutes %= 60;
  237. eft.minute = real_minutes;
  238. eft.second = real_seconds;
  239. status = efi.set_time(&eft);
  240. if (status != EFI_SUCCESS) {
  241. pr_err("Oops: efitime: can't write time!\n");
  242. return -1;
  243. }
  244. return 0;
  245. }
  246. unsigned long efi_get_time(void)
  247. {
  248. efi_status_t status;
  249. efi_time_t eft;
  250. efi_time_cap_t cap;
  251. status = efi.get_time(&eft, &cap);
  252. if (status != EFI_SUCCESS)
  253. pr_err("Oops: efitime: can't read time!\n");
  254. return mktime(eft.year, eft.month, eft.day, eft.hour,
  255. eft.minute, eft.second);
  256. }
  257. /*
  258. * Tell the kernel about the EFI memory map. This might include
  259. * more than the max 128 entries that can fit in the e820 legacy
  260. * (zeropage) memory map.
  261. */
  262. static void __init do_add_efi_memmap(void)
  263. {
  264. void *p;
  265. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  266. efi_memory_desc_t *md = p;
  267. unsigned long long start = md->phys_addr;
  268. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  269. int e820_type;
  270. switch (md->type) {
  271. case EFI_LOADER_CODE:
  272. case EFI_LOADER_DATA:
  273. case EFI_BOOT_SERVICES_CODE:
  274. case EFI_BOOT_SERVICES_DATA:
  275. case EFI_CONVENTIONAL_MEMORY:
  276. if (md->attribute & EFI_MEMORY_WB)
  277. e820_type = E820_RAM;
  278. else
  279. e820_type = E820_RESERVED;
  280. break;
  281. case EFI_ACPI_RECLAIM_MEMORY:
  282. e820_type = E820_ACPI;
  283. break;
  284. case EFI_ACPI_MEMORY_NVS:
  285. e820_type = E820_NVS;
  286. break;
  287. case EFI_UNUSABLE_MEMORY:
  288. e820_type = E820_UNUSABLE;
  289. break;
  290. default:
  291. /*
  292. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  293. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  294. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  295. */
  296. e820_type = E820_RESERVED;
  297. break;
  298. }
  299. e820_add_region(start, size, e820_type);
  300. }
  301. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  302. }
  303. int __init efi_memblock_x86_reserve_range(void)
  304. {
  305. struct efi_info *e = &boot_params.efi_info;
  306. unsigned long pmap;
  307. #ifdef CONFIG_X86_32
  308. /* Can't handle data above 4GB at this time */
  309. if (e->efi_memmap_hi) {
  310. pr_err("Memory map is above 4GB, disabling EFI.\n");
  311. return -EINVAL;
  312. }
  313. pmap = e->efi_memmap;
  314. #else
  315. pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
  316. #endif
  317. memmap.phys_map = (void *)pmap;
  318. memmap.nr_map = e->efi_memmap_size /
  319. e->efi_memdesc_size;
  320. memmap.desc_size = e->efi_memdesc_size;
  321. memmap.desc_version = e->efi_memdesc_version;
  322. memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
  323. return 0;
  324. }
  325. #if EFI_DEBUG
  326. static void __init print_efi_memmap(void)
  327. {
  328. efi_memory_desc_t *md;
  329. void *p;
  330. int i;
  331. for (p = memmap.map, i = 0;
  332. p < memmap.map_end;
  333. p += memmap.desc_size, i++) {
  334. md = p;
  335. pr_info("mem%02u: type=%u, attr=0x%llx, "
  336. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  337. i, md->type, md->attribute, md->phys_addr,
  338. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  339. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  340. }
  341. }
  342. #endif /* EFI_DEBUG */
  343. void __init efi_reserve_boot_services(void)
  344. {
  345. void *p;
  346. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  347. efi_memory_desc_t *md = p;
  348. u64 start = md->phys_addr;
  349. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  350. if (md->type != EFI_BOOT_SERVICES_CODE &&
  351. md->type != EFI_BOOT_SERVICES_DATA)
  352. continue;
  353. /* Only reserve where possible:
  354. * - Not within any already allocated areas
  355. * - Not over any memory area (really needed, if above?)
  356. * - Not within any part of the kernel
  357. * - Not the bios reserved area
  358. */
  359. if ((start+size >= virt_to_phys(_text)
  360. && start <= virt_to_phys(_end)) ||
  361. !e820_all_mapped(start, start+size, E820_RAM) ||
  362. memblock_is_region_reserved(start, size)) {
  363. /* Could not reserve, skip it */
  364. md->num_pages = 0;
  365. memblock_dbg("Could not reserve boot range "
  366. "[0x%010llx-0x%010llx]\n",
  367. start, start+size-1);
  368. } else
  369. memblock_reserve(start, size);
  370. }
  371. }
  372. void __init efi_unmap_memmap(void)
  373. {
  374. clear_bit(EFI_MEMMAP, &x86_efi_facility);
  375. if (memmap.map) {
  376. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  377. memmap.map = NULL;
  378. }
  379. }
  380. void __init efi_free_boot_services(void)
  381. {
  382. void *p;
  383. if (!efi_is_native())
  384. return;
  385. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  386. efi_memory_desc_t *md = p;
  387. unsigned long long start = md->phys_addr;
  388. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  389. if (md->type != EFI_BOOT_SERVICES_CODE &&
  390. md->type != EFI_BOOT_SERVICES_DATA)
  391. continue;
  392. /* Could not reserve boot area */
  393. if (!size)
  394. continue;
  395. free_bootmem_late(start, size);
  396. }
  397. efi_unmap_memmap();
  398. }
  399. static int __init efi_systab_init(void *phys)
  400. {
  401. if (efi_enabled(EFI_64BIT)) {
  402. efi_system_table_64_t *systab64;
  403. u64 tmp = 0;
  404. systab64 = early_ioremap((unsigned long)phys,
  405. sizeof(*systab64));
  406. if (systab64 == NULL) {
  407. pr_err("Couldn't map the system table!\n");
  408. return -ENOMEM;
  409. }
  410. efi_systab.hdr = systab64->hdr;
  411. efi_systab.fw_vendor = systab64->fw_vendor;
  412. tmp |= systab64->fw_vendor;
  413. efi_systab.fw_revision = systab64->fw_revision;
  414. efi_systab.con_in_handle = systab64->con_in_handle;
  415. tmp |= systab64->con_in_handle;
  416. efi_systab.con_in = systab64->con_in;
  417. tmp |= systab64->con_in;
  418. efi_systab.con_out_handle = systab64->con_out_handle;
  419. tmp |= systab64->con_out_handle;
  420. efi_systab.con_out = systab64->con_out;
  421. tmp |= systab64->con_out;
  422. efi_systab.stderr_handle = systab64->stderr_handle;
  423. tmp |= systab64->stderr_handle;
  424. efi_systab.stderr = systab64->stderr;
  425. tmp |= systab64->stderr;
  426. efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
  427. tmp |= systab64->runtime;
  428. efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
  429. tmp |= systab64->boottime;
  430. efi_systab.nr_tables = systab64->nr_tables;
  431. efi_systab.tables = systab64->tables;
  432. tmp |= systab64->tables;
  433. early_iounmap(systab64, sizeof(*systab64));
  434. #ifdef CONFIG_X86_32
  435. if (tmp >> 32) {
  436. pr_err("EFI data located above 4GB, disabling EFI.\n");
  437. return -EINVAL;
  438. }
  439. #endif
  440. } else {
  441. efi_system_table_32_t *systab32;
  442. systab32 = early_ioremap((unsigned long)phys,
  443. sizeof(*systab32));
  444. if (systab32 == NULL) {
  445. pr_err("Couldn't map the system table!\n");
  446. return -ENOMEM;
  447. }
  448. efi_systab.hdr = systab32->hdr;
  449. efi_systab.fw_vendor = systab32->fw_vendor;
  450. efi_systab.fw_revision = systab32->fw_revision;
  451. efi_systab.con_in_handle = systab32->con_in_handle;
  452. efi_systab.con_in = systab32->con_in;
  453. efi_systab.con_out_handle = systab32->con_out_handle;
  454. efi_systab.con_out = systab32->con_out;
  455. efi_systab.stderr_handle = systab32->stderr_handle;
  456. efi_systab.stderr = systab32->stderr;
  457. efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
  458. efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
  459. efi_systab.nr_tables = systab32->nr_tables;
  460. efi_systab.tables = systab32->tables;
  461. early_iounmap(systab32, sizeof(*systab32));
  462. }
  463. efi.systab = &efi_systab;
  464. /*
  465. * Verify the EFI Table
  466. */
  467. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
  468. pr_err("System table signature incorrect!\n");
  469. return -EINVAL;
  470. }
  471. if ((efi.systab->hdr.revision >> 16) == 0)
  472. pr_err("Warning: System table version "
  473. "%d.%02d, expected 1.00 or greater!\n",
  474. efi.systab->hdr.revision >> 16,
  475. efi.systab->hdr.revision & 0xffff);
  476. return 0;
  477. }
  478. static int __init efi_config_init(u64 tables, int nr_tables)
  479. {
  480. void *config_tables, *tablep;
  481. int i, sz;
  482. if (efi_enabled(EFI_64BIT))
  483. sz = sizeof(efi_config_table_64_t);
  484. else
  485. sz = sizeof(efi_config_table_32_t);
  486. /*
  487. * Let's see what config tables the firmware passed to us.
  488. */
  489. config_tables = early_ioremap(tables, nr_tables * sz);
  490. if (config_tables == NULL) {
  491. pr_err("Could not map Configuration table!\n");
  492. return -ENOMEM;
  493. }
  494. tablep = config_tables;
  495. pr_info("");
  496. for (i = 0; i < efi.systab->nr_tables; i++) {
  497. efi_guid_t guid;
  498. unsigned long table;
  499. if (efi_enabled(EFI_64BIT)) {
  500. u64 table64;
  501. guid = ((efi_config_table_64_t *)tablep)->guid;
  502. table64 = ((efi_config_table_64_t *)tablep)->table;
  503. table = table64;
  504. #ifdef CONFIG_X86_32
  505. if (table64 >> 32) {
  506. pr_cont("\n");
  507. pr_err("Table located above 4GB, disabling EFI.\n");
  508. early_iounmap(config_tables,
  509. efi.systab->nr_tables * sz);
  510. return -EINVAL;
  511. }
  512. #endif
  513. } else {
  514. guid = ((efi_config_table_32_t *)tablep)->guid;
  515. table = ((efi_config_table_32_t *)tablep)->table;
  516. }
  517. if (!efi_guidcmp(guid, MPS_TABLE_GUID)) {
  518. efi.mps = table;
  519. pr_cont(" MPS=0x%lx ", table);
  520. } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) {
  521. efi.acpi20 = table;
  522. pr_cont(" ACPI 2.0=0x%lx ", table);
  523. } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) {
  524. efi.acpi = table;
  525. pr_cont(" ACPI=0x%lx ", table);
  526. } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) {
  527. efi.smbios = table;
  528. pr_cont(" SMBIOS=0x%lx ", table);
  529. #ifdef CONFIG_X86_UV
  530. } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) {
  531. efi.uv_systab = table;
  532. pr_cont(" UVsystab=0x%lx ", table);
  533. #endif
  534. } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) {
  535. efi.hcdp = table;
  536. pr_cont(" HCDP=0x%lx ", table);
  537. } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) {
  538. efi.uga = table;
  539. pr_cont(" UGA=0x%lx ", table);
  540. }
  541. tablep += sz;
  542. }
  543. pr_cont("\n");
  544. early_iounmap(config_tables, efi.systab->nr_tables * sz);
  545. return 0;
  546. }
  547. static int __init efi_runtime_init(void)
  548. {
  549. efi_runtime_services_t *runtime;
  550. /*
  551. * Check out the runtime services table. We need to map
  552. * the runtime services table so that we can grab the physical
  553. * address of several of the EFI runtime functions, needed to
  554. * set the firmware into virtual mode.
  555. */
  556. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  557. sizeof(efi_runtime_services_t));
  558. if (!runtime) {
  559. pr_err("Could not map the runtime service table!\n");
  560. return -ENOMEM;
  561. }
  562. /*
  563. * We will only need *early* access to the following
  564. * two EFI runtime services before set_virtual_address_map
  565. * is invoked.
  566. */
  567. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  568. efi_phys.set_virtual_address_map =
  569. (efi_set_virtual_address_map_t *)
  570. runtime->set_virtual_address_map;
  571. /*
  572. * Make efi_get_time can be called before entering
  573. * virtual mode.
  574. */
  575. efi.get_time = phys_efi_get_time;
  576. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  577. return 0;
  578. }
  579. static int __init efi_memmap_init(void)
  580. {
  581. /* Map the EFI memory map */
  582. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  583. memmap.nr_map * memmap.desc_size);
  584. if (memmap.map == NULL) {
  585. pr_err("Could not map the memory map!\n");
  586. return -ENOMEM;
  587. }
  588. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  589. if (add_efi_memmap)
  590. do_add_efi_memmap();
  591. return 0;
  592. }
  593. void __init efi_init(void)
  594. {
  595. efi_char16_t *c16;
  596. char vendor[100] = "unknown";
  597. int i = 0;
  598. void *tmp;
  599. #ifdef CONFIG_X86_32
  600. if (boot_params.efi_info.efi_systab_hi ||
  601. boot_params.efi_info.efi_memmap_hi) {
  602. pr_info("Table located above 4GB, disabling EFI.\n");
  603. return;
  604. }
  605. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  606. #else
  607. efi_phys.systab = (efi_system_table_t *)
  608. (boot_params.efi_info.efi_systab |
  609. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  610. #endif
  611. if (efi_systab_init(efi_phys.systab))
  612. return;
  613. set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
  614. /*
  615. * Show what we know for posterity
  616. */
  617. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  618. if (c16) {
  619. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  620. vendor[i] = *c16++;
  621. vendor[i] = '\0';
  622. } else
  623. pr_err("Could not map the firmware vendor!\n");
  624. early_iounmap(tmp, 2);
  625. pr_info("EFI v%u.%.02u by %s\n",
  626. efi.systab->hdr.revision >> 16,
  627. efi.systab->hdr.revision & 0xffff, vendor);
  628. if (efi_config_init(efi.systab->tables, efi.systab->nr_tables))
  629. return;
  630. set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
  631. /*
  632. * Note: We currently don't support runtime services on an EFI
  633. * that doesn't match the kernel 32/64-bit mode.
  634. */
  635. if (!efi_is_native())
  636. pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
  637. else {
  638. if (efi_runtime_init())
  639. return;
  640. set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
  641. }
  642. if (efi_memmap_init())
  643. return;
  644. set_bit(EFI_MEMMAP, &x86_efi_facility);
  645. #ifdef CONFIG_X86_32
  646. if (efi_is_native()) {
  647. x86_platform.get_wallclock = efi_get_time;
  648. x86_platform.set_wallclock = efi_set_rtc_mmss;
  649. }
  650. #endif
  651. #if EFI_DEBUG
  652. print_efi_memmap();
  653. #endif
  654. }
  655. void __init efi_late_init(void)
  656. {
  657. efi_bgrt_init();
  658. }
  659. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  660. {
  661. u64 addr, npages;
  662. addr = md->virt_addr;
  663. npages = md->num_pages;
  664. memrange_efi_to_native(&addr, &npages);
  665. if (executable)
  666. set_memory_x(addr, npages);
  667. else
  668. set_memory_nx(addr, npages);
  669. }
  670. static void __init runtime_code_page_mkexec(void)
  671. {
  672. efi_memory_desc_t *md;
  673. void *p;
  674. /* Make EFI runtime service code area executable */
  675. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  676. md = p;
  677. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  678. continue;
  679. efi_set_executable(md, true);
  680. }
  681. }
  682. /*
  683. * We can't ioremap data in EFI boot services RAM, because we've already mapped
  684. * it as RAM. So, look it up in the existing EFI memory map instead. Only
  685. * callable after efi_enter_virtual_mode and before efi_free_boot_services.
  686. */
  687. void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
  688. {
  689. void *p;
  690. if (WARN_ON(!memmap.map))
  691. return NULL;
  692. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  693. efi_memory_desc_t *md = p;
  694. u64 size = md->num_pages << EFI_PAGE_SHIFT;
  695. u64 end = md->phys_addr + size;
  696. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  697. md->type != EFI_BOOT_SERVICES_CODE &&
  698. md->type != EFI_BOOT_SERVICES_DATA)
  699. continue;
  700. if (!md->virt_addr)
  701. continue;
  702. if (phys_addr >= md->phys_addr && phys_addr < end) {
  703. phys_addr += md->virt_addr - md->phys_addr;
  704. return (__force void __iomem *)(unsigned long)phys_addr;
  705. }
  706. }
  707. return NULL;
  708. }
  709. void efi_memory_uc(u64 addr, unsigned long size)
  710. {
  711. unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
  712. u64 npages;
  713. npages = round_up(size, page_shift) / page_shift;
  714. memrange_efi_to_native(&addr, &npages);
  715. set_memory_uc(addr, npages);
  716. }
  717. /*
  718. * This function will switch the EFI runtime services to virtual mode.
  719. * Essentially, look through the EFI memmap and map every region that
  720. * has the runtime attribute bit set in its memory descriptor and update
  721. * that memory descriptor with the virtual address obtained from ioremap().
  722. * This enables the runtime services to be called without having to
  723. * thunk back into physical mode for every invocation.
  724. */
  725. void __init efi_enter_virtual_mode(void)
  726. {
  727. efi_memory_desc_t *md, *prev_md = NULL;
  728. efi_status_t status;
  729. unsigned long size;
  730. u64 end, systab, end_pfn;
  731. void *p, *va, *new_memmap = NULL;
  732. int count = 0;
  733. efi.systab = NULL;
  734. /*
  735. * We don't do virtual mode, since we don't do runtime services, on
  736. * non-native EFI
  737. */
  738. if (!efi_is_native()) {
  739. efi_unmap_memmap();
  740. return;
  741. }
  742. /* Merge contiguous regions of the same type and attribute */
  743. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  744. u64 prev_size;
  745. md = p;
  746. if (!prev_md) {
  747. prev_md = md;
  748. continue;
  749. }
  750. if (prev_md->type != md->type ||
  751. prev_md->attribute != md->attribute) {
  752. prev_md = md;
  753. continue;
  754. }
  755. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  756. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  757. prev_md->num_pages += md->num_pages;
  758. md->type = EFI_RESERVED_TYPE;
  759. md->attribute = 0;
  760. continue;
  761. }
  762. prev_md = md;
  763. }
  764. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  765. md = p;
  766. if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
  767. md->type != EFI_BOOT_SERVICES_CODE &&
  768. md->type != EFI_BOOT_SERVICES_DATA)
  769. continue;
  770. size = md->num_pages << EFI_PAGE_SHIFT;
  771. end = md->phys_addr + size;
  772. end_pfn = PFN_UP(end);
  773. if (end_pfn <= max_low_pfn_mapped
  774. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  775. && end_pfn <= max_pfn_mapped)) {
  776. va = __va(md->phys_addr);
  777. if (!(md->attribute & EFI_MEMORY_WB))
  778. efi_memory_uc((u64)(unsigned long)va, size);
  779. } else
  780. va = efi_ioremap(md->phys_addr, size,
  781. md->type, md->attribute);
  782. md->virt_addr = (u64) (unsigned long) va;
  783. if (!va) {
  784. pr_err("ioremap of 0x%llX failed!\n",
  785. (unsigned long long)md->phys_addr);
  786. continue;
  787. }
  788. systab = (u64) (unsigned long) efi_phys.systab;
  789. if (md->phys_addr <= systab && systab < end) {
  790. systab += md->virt_addr - md->phys_addr;
  791. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  792. }
  793. new_memmap = krealloc(new_memmap,
  794. (count + 1) * memmap.desc_size,
  795. GFP_KERNEL);
  796. memcpy(new_memmap + (count * memmap.desc_size), md,
  797. memmap.desc_size);
  798. count++;
  799. }
  800. BUG_ON(!efi.systab);
  801. status = phys_efi_set_virtual_address_map(
  802. memmap.desc_size * count,
  803. memmap.desc_size,
  804. memmap.desc_version,
  805. (efi_memory_desc_t *)__pa(new_memmap));
  806. if (status != EFI_SUCCESS) {
  807. pr_alert("Unable to switch EFI into virtual mode "
  808. "(status=%lx)!\n", status);
  809. panic("EFI call to SetVirtualAddressMap() failed!");
  810. }
  811. /*
  812. * Now that EFI is in virtual mode, update the function
  813. * pointers in the runtime service table to the new virtual addresses.
  814. *
  815. * Call EFI services through wrapper functions.
  816. */
  817. efi.runtime_version = efi_systab.hdr.revision;
  818. efi.get_time = virt_efi_get_time;
  819. efi.set_time = virt_efi_set_time;
  820. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  821. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  822. efi.get_variable = virt_efi_get_variable;
  823. efi.get_next_variable = virt_efi_get_next_variable;
  824. efi.set_variable = virt_efi_set_variable;
  825. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  826. efi.reset_system = virt_efi_reset_system;
  827. efi.set_virtual_address_map = NULL;
  828. efi.query_variable_info = virt_efi_query_variable_info;
  829. efi.update_capsule = virt_efi_update_capsule;
  830. efi.query_capsule_caps = virt_efi_query_capsule_caps;
  831. if (__supported_pte_mask & _PAGE_NX)
  832. runtime_code_page_mkexec();
  833. kfree(new_memmap);
  834. }
  835. /*
  836. * Convenience functions to obtain memory types and attributes
  837. */
  838. u32 efi_mem_type(unsigned long phys_addr)
  839. {
  840. efi_memory_desc_t *md;
  841. void *p;
  842. if (!efi_enabled(EFI_MEMMAP))
  843. return 0;
  844. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  845. md = p;
  846. if ((md->phys_addr <= phys_addr) &&
  847. (phys_addr < (md->phys_addr +
  848. (md->num_pages << EFI_PAGE_SHIFT))))
  849. return md->type;
  850. }
  851. return 0;
  852. }
  853. u64 efi_mem_attributes(unsigned long phys_addr)
  854. {
  855. efi_memory_desc_t *md;
  856. void *p;
  857. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  858. md = p;
  859. if ((md->phys_addr <= phys_addr) &&
  860. (phys_addr < (md->phys_addr +
  861. (md->num_pages << EFI_PAGE_SHIFT))))
  862. return md->attribute;
  863. }
  864. return 0;
  865. }