efi.c 28 KB

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