efi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. #include <linux/kernel.h>
  29. #include <linux/init.h>
  30. #include <linux/efi.h>
  31. #include <linux/bootmem.h>
  32. #include <linux/memblock.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/time.h>
  36. #include <linux/io.h>
  37. #include <linux/reboot.h>
  38. #include <linux/bcd.h>
  39. #include <asm/setup.h>
  40. #include <asm/efi.h>
  41. #include <asm/time.h>
  42. #include <asm/cacheflush.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/x86_init.h>
  45. #define EFI_DEBUG 1
  46. #define PFX "EFI: "
  47. int efi_enabled;
  48. EXPORT_SYMBOL(efi_enabled);
  49. struct efi efi;
  50. EXPORT_SYMBOL(efi);
  51. struct efi_memory_map memmap;
  52. static struct efi efi_phys __initdata;
  53. static efi_system_table_t efi_systab __initdata;
  54. static int __init setup_noefi(char *arg)
  55. {
  56. efi_enabled = 0;
  57. return 0;
  58. }
  59. early_param("noefi", setup_noefi);
  60. int add_efi_memmap;
  61. EXPORT_SYMBOL(add_efi_memmap);
  62. static int __init setup_add_efi_memmap(char *arg)
  63. {
  64. add_efi_memmap = 1;
  65. return 0;
  66. }
  67. early_param("add_efi_memmap", setup_add_efi_memmap);
  68. static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  69. {
  70. return efi_call_virt2(get_time, tm, tc);
  71. }
  72. static efi_status_t virt_efi_set_time(efi_time_t *tm)
  73. {
  74. return efi_call_virt1(set_time, tm);
  75. }
  76. static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
  77. efi_bool_t *pending,
  78. efi_time_t *tm)
  79. {
  80. return efi_call_virt3(get_wakeup_time,
  81. enabled, pending, tm);
  82. }
  83. static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  84. {
  85. return efi_call_virt2(set_wakeup_time,
  86. enabled, tm);
  87. }
  88. static efi_status_t virt_efi_get_variable(efi_char16_t *name,
  89. efi_guid_t *vendor,
  90. u32 *attr,
  91. unsigned long *data_size,
  92. void *data)
  93. {
  94. return efi_call_virt5(get_variable,
  95. name, vendor, attr,
  96. data_size, data);
  97. }
  98. static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
  99. efi_char16_t *name,
  100. efi_guid_t *vendor)
  101. {
  102. return efi_call_virt3(get_next_variable,
  103. name_size, name, vendor);
  104. }
  105. static efi_status_t virt_efi_set_variable(efi_char16_t *name,
  106. efi_guid_t *vendor,
  107. unsigned long attr,
  108. unsigned long data_size,
  109. void *data)
  110. {
  111. return efi_call_virt5(set_variable,
  112. name, vendor, attr,
  113. data_size, data);
  114. }
  115. static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
  116. {
  117. return efi_call_virt1(get_next_high_mono_count, count);
  118. }
  119. static void virt_efi_reset_system(int reset_type,
  120. efi_status_t status,
  121. unsigned long data_size,
  122. efi_char16_t *data)
  123. {
  124. efi_call_virt4(reset_system, reset_type, status,
  125. data_size, data);
  126. }
  127. static efi_status_t __init phys_efi_set_virtual_address_map(
  128. unsigned long memory_map_size,
  129. unsigned long descriptor_size,
  130. u32 descriptor_version,
  131. efi_memory_desc_t *virtual_map)
  132. {
  133. efi_status_t status;
  134. efi_call_phys_prelog();
  135. status = efi_call_phys4(efi_phys.set_virtual_address_map,
  136. memory_map_size, descriptor_size,
  137. descriptor_version, virtual_map);
  138. efi_call_phys_epilog();
  139. return status;
  140. }
  141. static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
  142. efi_time_cap_t *tc)
  143. {
  144. efi_status_t status;
  145. efi_call_phys_prelog();
  146. status = efi_call_phys2(efi_phys.get_time, tm, tc);
  147. efi_call_phys_epilog();
  148. return status;
  149. }
  150. int efi_set_rtc_mmss(unsigned long nowtime)
  151. {
  152. int real_seconds, real_minutes;
  153. efi_status_t status;
  154. efi_time_t eft;
  155. efi_time_cap_t cap;
  156. status = efi.get_time(&eft, &cap);
  157. if (status != EFI_SUCCESS) {
  158. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  159. return -1;
  160. }
  161. real_seconds = nowtime % 60;
  162. real_minutes = nowtime / 60;
  163. if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
  164. real_minutes += 30;
  165. real_minutes %= 60;
  166. eft.minute = real_minutes;
  167. eft.second = real_seconds;
  168. status = efi.set_time(&eft);
  169. if (status != EFI_SUCCESS) {
  170. printk(KERN_ERR "Oops: efitime: can't write time!\n");
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. unsigned long efi_get_time(void)
  176. {
  177. efi_status_t status;
  178. efi_time_t eft;
  179. efi_time_cap_t cap;
  180. status = efi.get_time(&eft, &cap);
  181. if (status != EFI_SUCCESS)
  182. printk(KERN_ERR "Oops: efitime: can't read time!\n");
  183. return mktime(eft.year, eft.month, eft.day, eft.hour,
  184. eft.minute, eft.second);
  185. }
  186. /*
  187. * Tell the kernel about the EFI memory map. This might include
  188. * more than the max 128 entries that can fit in the e820 legacy
  189. * (zeropage) memory map.
  190. */
  191. static void __init do_add_efi_memmap(void)
  192. {
  193. void *p;
  194. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  195. efi_memory_desc_t *md = p;
  196. unsigned long long start = md->phys_addr;
  197. unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
  198. int e820_type;
  199. switch (md->type) {
  200. case EFI_LOADER_CODE:
  201. case EFI_LOADER_DATA:
  202. case EFI_BOOT_SERVICES_CODE:
  203. case EFI_BOOT_SERVICES_DATA:
  204. case EFI_CONVENTIONAL_MEMORY:
  205. if (md->attribute & EFI_MEMORY_WB)
  206. e820_type = E820_RAM;
  207. else
  208. e820_type = E820_RESERVED;
  209. break;
  210. case EFI_ACPI_RECLAIM_MEMORY:
  211. e820_type = E820_ACPI;
  212. break;
  213. case EFI_ACPI_MEMORY_NVS:
  214. e820_type = E820_NVS;
  215. break;
  216. case EFI_UNUSABLE_MEMORY:
  217. e820_type = E820_UNUSABLE;
  218. break;
  219. default:
  220. /*
  221. * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
  222. * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
  223. * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
  224. */
  225. e820_type = E820_RESERVED;
  226. break;
  227. }
  228. e820_add_region(start, size, e820_type);
  229. }
  230. sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
  231. }
  232. void __init efi_memblock_x86_reserve_range(void)
  233. {
  234. unsigned long pmap;
  235. #ifdef CONFIG_X86_32
  236. pmap = boot_params.efi_info.efi_memmap;
  237. #else
  238. pmap = (boot_params.efi_info.efi_memmap |
  239. ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
  240. #endif
  241. memmap.phys_map = (void *)pmap;
  242. memmap.nr_map = boot_params.efi_info.efi_memmap_size /
  243. boot_params.efi_info.efi_memdesc_size;
  244. memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
  245. memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
  246. memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
  247. "EFI memmap");
  248. }
  249. #if EFI_DEBUG
  250. static void __init print_efi_memmap(void)
  251. {
  252. efi_memory_desc_t *md;
  253. void *p;
  254. int i;
  255. for (p = memmap.map, i = 0;
  256. p < memmap.map_end;
  257. p += memmap.desc_size, i++) {
  258. md = p;
  259. printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
  260. "range=[0x%016llx-0x%016llx) (%lluMB)\n",
  261. i, md->type, md->attribute, md->phys_addr,
  262. md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
  263. (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
  264. }
  265. }
  266. #endif /* EFI_DEBUG */
  267. void __init efi_init(void)
  268. {
  269. efi_config_table_t *config_tables;
  270. efi_runtime_services_t *runtime;
  271. efi_char16_t *c16;
  272. char vendor[100] = "unknown";
  273. int i = 0;
  274. void *tmp;
  275. #ifdef CONFIG_X86_32
  276. efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
  277. #else
  278. efi_phys.systab = (efi_system_table_t *)
  279. (boot_params.efi_info.efi_systab |
  280. ((__u64)boot_params.efi_info.efi_systab_hi<<32));
  281. #endif
  282. efi.systab = early_ioremap((unsigned long)efi_phys.systab,
  283. sizeof(efi_system_table_t));
  284. if (efi.systab == NULL)
  285. printk(KERN_ERR "Couldn't map the EFI system table!\n");
  286. memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
  287. early_iounmap(efi.systab, sizeof(efi_system_table_t));
  288. efi.systab = &efi_systab;
  289. /*
  290. * Verify the EFI Table
  291. */
  292. if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
  293. printk(KERN_ERR "EFI system table signature incorrect!\n");
  294. if ((efi.systab->hdr.revision >> 16) == 0)
  295. printk(KERN_ERR "Warning: EFI system table version "
  296. "%d.%02d, expected 1.00 or greater!\n",
  297. efi.systab->hdr.revision >> 16,
  298. efi.systab->hdr.revision & 0xffff);
  299. /*
  300. * Show what we know for posterity
  301. */
  302. c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
  303. if (c16) {
  304. for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
  305. vendor[i] = *c16++;
  306. vendor[i] = '\0';
  307. } else
  308. printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
  309. early_iounmap(tmp, 2);
  310. printk(KERN_INFO "EFI v%u.%.02u by %s\n",
  311. efi.systab->hdr.revision >> 16,
  312. efi.systab->hdr.revision & 0xffff, vendor);
  313. /*
  314. * Let's see what config tables the firmware passed to us.
  315. */
  316. config_tables = early_ioremap(
  317. efi.systab->tables,
  318. efi.systab->nr_tables * sizeof(efi_config_table_t));
  319. if (config_tables == NULL)
  320. printk(KERN_ERR "Could not map EFI Configuration Table!\n");
  321. printk(KERN_INFO);
  322. for (i = 0; i < efi.systab->nr_tables; i++) {
  323. if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
  324. efi.mps = config_tables[i].table;
  325. printk(" MPS=0x%lx ", config_tables[i].table);
  326. } else if (!efi_guidcmp(config_tables[i].guid,
  327. ACPI_20_TABLE_GUID)) {
  328. efi.acpi20 = config_tables[i].table;
  329. printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
  330. } else if (!efi_guidcmp(config_tables[i].guid,
  331. ACPI_TABLE_GUID)) {
  332. efi.acpi = config_tables[i].table;
  333. printk(" ACPI=0x%lx ", config_tables[i].table);
  334. } else if (!efi_guidcmp(config_tables[i].guid,
  335. SMBIOS_TABLE_GUID)) {
  336. efi.smbios = config_tables[i].table;
  337. printk(" SMBIOS=0x%lx ", config_tables[i].table);
  338. #ifdef CONFIG_X86_UV
  339. } else if (!efi_guidcmp(config_tables[i].guid,
  340. UV_SYSTEM_TABLE_GUID)) {
  341. efi.uv_systab = config_tables[i].table;
  342. printk(" UVsystab=0x%lx ", config_tables[i].table);
  343. #endif
  344. } else if (!efi_guidcmp(config_tables[i].guid,
  345. HCDP_TABLE_GUID)) {
  346. efi.hcdp = config_tables[i].table;
  347. printk(" HCDP=0x%lx ", config_tables[i].table);
  348. } else if (!efi_guidcmp(config_tables[i].guid,
  349. UGA_IO_PROTOCOL_GUID)) {
  350. efi.uga = config_tables[i].table;
  351. printk(" UGA=0x%lx ", config_tables[i].table);
  352. }
  353. }
  354. printk("\n");
  355. early_iounmap(config_tables,
  356. efi.systab->nr_tables * sizeof(efi_config_table_t));
  357. /*
  358. * Check out the runtime services table. We need to map
  359. * the runtime services table so that we can grab the physical
  360. * address of several of the EFI runtime functions, needed to
  361. * set the firmware into virtual mode.
  362. */
  363. runtime = early_ioremap((unsigned long)efi.systab->runtime,
  364. sizeof(efi_runtime_services_t));
  365. if (runtime != NULL) {
  366. /*
  367. * We will only need *early* access to the following
  368. * two EFI runtime services before set_virtual_address_map
  369. * is invoked.
  370. */
  371. efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
  372. efi_phys.set_virtual_address_map =
  373. (efi_set_virtual_address_map_t *)
  374. runtime->set_virtual_address_map;
  375. /*
  376. * Make efi_get_time can be called before entering
  377. * virtual mode.
  378. */
  379. efi.get_time = phys_efi_get_time;
  380. } else
  381. printk(KERN_ERR "Could not map the EFI runtime service "
  382. "table!\n");
  383. early_iounmap(runtime, sizeof(efi_runtime_services_t));
  384. /* Map the EFI memory map */
  385. memmap.map = early_ioremap((unsigned long)memmap.phys_map,
  386. memmap.nr_map * memmap.desc_size);
  387. if (memmap.map == NULL)
  388. printk(KERN_ERR "Could not map the EFI memory map!\n");
  389. memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
  390. if (memmap.desc_size != sizeof(efi_memory_desc_t))
  391. printk(KERN_WARNING
  392. "Kernel-defined memdesc doesn't match the one from EFI!\n");
  393. if (add_efi_memmap)
  394. do_add_efi_memmap();
  395. #ifdef CONFIG_X86_32
  396. x86_platform.get_wallclock = efi_get_time;
  397. x86_platform.set_wallclock = efi_set_rtc_mmss;
  398. #endif
  399. /* Setup for EFI runtime service */
  400. reboot_type = BOOT_EFI;
  401. #if EFI_DEBUG
  402. print_efi_memmap();
  403. #endif
  404. }
  405. void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
  406. {
  407. u64 addr, npages;
  408. addr = md->virt_addr;
  409. npages = md->num_pages;
  410. memrange_efi_to_native(&addr, &npages);
  411. if (executable)
  412. set_memory_x(addr, npages);
  413. else
  414. set_memory_nx(addr, npages);
  415. }
  416. static void __init runtime_code_page_mkexec(void)
  417. {
  418. efi_memory_desc_t *md;
  419. void *p;
  420. /* Make EFI runtime service code area executable */
  421. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  422. md = p;
  423. if (md->type != EFI_RUNTIME_SERVICES_CODE)
  424. continue;
  425. efi_set_executable(md, true);
  426. }
  427. }
  428. /*
  429. * This function will switch the EFI runtime services to virtual mode.
  430. * Essentially, look through the EFI memmap and map every region that
  431. * has the runtime attribute bit set in its memory descriptor and update
  432. * that memory descriptor with the virtual address obtained from ioremap().
  433. * This enables the runtime services to be called without having to
  434. * thunk back into physical mode for every invocation.
  435. */
  436. void __init efi_enter_virtual_mode(void)
  437. {
  438. efi_memory_desc_t *md, *prev_md = NULL;
  439. efi_status_t status;
  440. unsigned long size;
  441. u64 end, systab, addr, npages, end_pfn;
  442. void *p, *va, *new_memmap = NULL;
  443. int count = 0;
  444. efi.systab = NULL;
  445. /* Merge contiguous regions of the same type and attribute */
  446. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  447. u64 prev_size;
  448. md = p;
  449. if (!prev_md) {
  450. prev_md = md;
  451. continue;
  452. }
  453. if (prev_md->type != md->type ||
  454. prev_md->attribute != md->attribute) {
  455. prev_md = md;
  456. continue;
  457. }
  458. prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
  459. if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
  460. prev_md->num_pages += md->num_pages;
  461. md->type = EFI_RESERVED_TYPE;
  462. md->attribute = 0;
  463. continue;
  464. }
  465. prev_md = md;
  466. }
  467. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  468. md = p;
  469. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  470. continue;
  471. size = md->num_pages << EFI_PAGE_SHIFT;
  472. end = md->phys_addr + size;
  473. end_pfn = PFN_UP(end);
  474. if (end_pfn <= max_low_pfn_mapped
  475. || (end_pfn > (1UL << (32 - PAGE_SHIFT))
  476. && end_pfn <= max_pfn_mapped))
  477. va = __va(md->phys_addr);
  478. else
  479. va = efi_ioremap(md->phys_addr, size, md->type);
  480. md->virt_addr = (u64) (unsigned long) va;
  481. if (!va) {
  482. printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
  483. (unsigned long long)md->phys_addr);
  484. continue;
  485. }
  486. if (!(md->attribute & EFI_MEMORY_WB)) {
  487. addr = md->virt_addr;
  488. npages = md->num_pages;
  489. memrange_efi_to_native(&addr, &npages);
  490. set_memory_uc(addr, npages);
  491. }
  492. systab = (u64) (unsigned long) efi_phys.systab;
  493. if (md->phys_addr <= systab && systab < end) {
  494. systab += md->virt_addr - md->phys_addr;
  495. efi.systab = (efi_system_table_t *) (unsigned long) systab;
  496. }
  497. new_memmap = krealloc(new_memmap,
  498. (count + 1) * memmap.desc_size,
  499. GFP_KERNEL);
  500. memcpy(new_memmap + (count * memmap.desc_size), md,
  501. memmap.desc_size);
  502. count++;
  503. }
  504. BUG_ON(!efi.systab);
  505. status = phys_efi_set_virtual_address_map(
  506. memmap.desc_size * count,
  507. memmap.desc_size,
  508. memmap.desc_version,
  509. (efi_memory_desc_t *)__pa(new_memmap));
  510. if (status != EFI_SUCCESS) {
  511. printk(KERN_ALERT "Unable to switch EFI into virtual mode "
  512. "(status=%lx)!\n", status);
  513. panic("EFI call to SetVirtualAddressMap() failed!");
  514. }
  515. /*
  516. * Now that EFI is in virtual mode, update the function
  517. * pointers in the runtime service table to the new virtual addresses.
  518. *
  519. * Call EFI services through wrapper functions.
  520. */
  521. efi.get_time = virt_efi_get_time;
  522. efi.set_time = virt_efi_set_time;
  523. efi.get_wakeup_time = virt_efi_get_wakeup_time;
  524. efi.set_wakeup_time = virt_efi_set_wakeup_time;
  525. efi.get_variable = virt_efi_get_variable;
  526. efi.get_next_variable = virt_efi_get_next_variable;
  527. efi.set_variable = virt_efi_set_variable;
  528. efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
  529. efi.reset_system = virt_efi_reset_system;
  530. efi.set_virtual_address_map = NULL;
  531. if (__supported_pte_mask & _PAGE_NX)
  532. runtime_code_page_mkexec();
  533. early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
  534. memmap.map = NULL;
  535. kfree(new_memmap);
  536. }
  537. /*
  538. * Convenience functions to obtain memory types and attributes
  539. */
  540. u32 efi_mem_type(unsigned long phys_addr)
  541. {
  542. efi_memory_desc_t *md;
  543. void *p;
  544. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  545. md = p;
  546. if ((md->phys_addr <= phys_addr) &&
  547. (phys_addr < (md->phys_addr +
  548. (md->num_pages << EFI_PAGE_SHIFT))))
  549. return md->type;
  550. }
  551. return 0;
  552. }
  553. u64 efi_mem_attributes(unsigned long phys_addr)
  554. {
  555. efi_memory_desc_t *md;
  556. void *p;
  557. for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
  558. md = p;
  559. if ((md->phys_addr <= phys_addr) &&
  560. (phys_addr < (md->phys_addr +
  561. (md->num_pages << EFI_PAGE_SHIFT))))
  562. return md->attribute;
  563. }
  564. return 0;
  565. }