2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 2.4
6 * Copyright (C) 2013 - 2015 Linaro Ltd.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #define pr_fmt(fmt) "efi: " fmt
16 #include <linux/efi.h>
17 #include <linux/init.h>
18 #include <linux/memblock.h>
19 #include <linux/mm_types.h>
21 #include <linux/of_fdt.h>
22 #include <linux/platform_device.h>
23 #include <linux/screen_info.h>
29 static int __init
is_normal_ram(efi_memory_desc_t
*md
)
31 if (md
->attribute
& EFI_MEMORY_WB
)
37 * Translate a EFI virtual address into a physical address: this is necessary,
38 * as some data members of the EFI system table are virtually remapped after
39 * SetVirtualAddressMap() has been called.
41 static phys_addr_t
efi_to_phys(unsigned long addr
)
43 efi_memory_desc_t
*md
;
45 for_each_efi_memory_desc(md
) {
46 if (!(md
->attribute
& EFI_MEMORY_RUNTIME
))
48 if (md
->virt_addr
== 0)
49 /* no virtual mapping has been installed by the stub */
51 if (md
->virt_addr
<= addr
&&
52 (addr
- md
->virt_addr
) < (md
->num_pages
<< EFI_PAGE_SHIFT
))
53 return md
->phys_addr
+ addr
- md
->virt_addr
;
58 static __initdata
unsigned long screen_info_table
= EFI_INVALID_TABLE_ADDR
;
60 static __initdata efi_config_table_type_t arch_tables
[] = {
61 {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID
, NULL
, &screen_info_table
},
62 {NULL_GUID
, NULL
, NULL
}
65 static void __init
init_screen_info(void)
67 struct screen_info
*si
;
69 if (screen_info_table
!= EFI_INVALID_TABLE_ADDR
) {
70 si
= early_memremap_ro(screen_info_table
, sizeof(*si
));
72 pr_err("Could not map screen_info config table\n");
76 early_memunmap(si
, sizeof(*si
));
78 /* dummycon on ARM needs non-zero values for columns/lines */
79 screen_info
.orig_video_cols
= 80;
80 screen_info
.orig_video_lines
= 25;
83 if (screen_info
.orig_video_isVGA
== VIDEO_TYPE_EFI
&&
84 memblock_is_map_memory(screen_info
.lfb_base
))
85 memblock_mark_nomap(screen_info
.lfb_base
, screen_info
.lfb_size
);
88 static int __init
uefi_init(void)
93 char vendor
[100] = "unknown";
96 efi
.systab
= early_memremap_ro(efi_system_table
,
97 sizeof(efi_system_table_t
));
98 if (efi
.systab
== NULL
) {
99 pr_warn("Unable to map EFI system table.\n");
103 set_bit(EFI_BOOT
, &efi
.flags
);
104 if (IS_ENABLED(CONFIG_64BIT
))
105 set_bit(EFI_64BIT
, &efi
.flags
);
108 * Verify the EFI Table
110 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
) {
111 pr_err("System table signature incorrect\n");
115 if ((efi
.systab
->hdr
.revision
>> 16) < 2)
116 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
117 efi
.systab
->hdr
.revision
>> 16,
118 efi
.systab
->hdr
.revision
& 0xffff);
120 efi
.runtime_version
= efi
.systab
->hdr
.revision
;
122 /* Show what we know for posterity */
123 c16
= early_memremap_ro(efi_to_phys(efi
.systab
->fw_vendor
),
124 sizeof(vendor
) * sizeof(efi_char16_t
));
126 for (i
= 0; i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
129 early_memunmap(c16
, sizeof(vendor
) * sizeof(efi_char16_t
));
132 pr_info("EFI v%u.%.02u by %s\n",
133 efi
.systab
->hdr
.revision
>> 16,
134 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
136 table_size
= sizeof(efi_config_table_64_t
) * efi
.systab
->nr_tables
;
137 config_tables
= early_memremap_ro(efi_to_phys(efi
.systab
->tables
),
139 if (config_tables
== NULL
) {
140 pr_warn("Unable to map EFI config table array.\n");
144 retval
= efi_config_parse_tables(config_tables
, efi
.systab
->nr_tables
,
145 sizeof(efi_config_table_t
),
148 early_memunmap(config_tables
, table_size
);
150 early_memunmap(efi
.systab
, sizeof(efi_system_table_t
));
155 * Return true for RAM regions we want to permanently reserve.
157 static __init
int is_reserve_region(efi_memory_desc_t
*md
)
160 case EFI_LOADER_CODE
:
161 case EFI_LOADER_DATA
:
162 case EFI_BOOT_SERVICES_CODE
:
163 case EFI_BOOT_SERVICES_DATA
:
164 case EFI_CONVENTIONAL_MEMORY
:
165 case EFI_PERSISTENT_MEMORY
:
170 return is_normal_ram(md
);
173 static __init
void reserve_regions(void)
175 efi_memory_desc_t
*md
;
176 u64 paddr
, npages
, size
;
178 if (efi_enabled(EFI_DBG
))
179 pr_info("Processing EFI memory map:\n");
182 * Discard memblocks discovered so far: if there are any at this
183 * point, they originate from memory nodes in the DT, and UEFI
184 * uses its own memory map instead.
187 memblock_remove(0, (phys_addr_t
)ULLONG_MAX
);
189 for_each_efi_memory_desc(md
) {
190 paddr
= md
->phys_addr
;
191 npages
= md
->num_pages
;
193 if (efi_enabled(EFI_DBG
)) {
196 pr_info(" 0x%012llx-0x%012llx %s",
197 paddr
, paddr
+ (npages
<< EFI_PAGE_SHIFT
) - 1,
198 efi_md_typeattr_format(buf
, sizeof(buf
), md
));
201 memrange_efi_to_native(&paddr
, &npages
);
202 size
= npages
<< PAGE_SHIFT
;
204 if (is_normal_ram(md
))
205 early_init_dt_add_memory_arch(paddr
, size
);
207 if (is_reserve_region(md
)) {
208 memblock_mark_nomap(paddr
, size
);
209 if (efi_enabled(EFI_DBG
))
213 if (efi_enabled(EFI_DBG
))
217 set_bit(EFI_MEMMAP
, &efi
.flags
);
220 void __init
efi_init(void)
222 struct efi_fdt_params params
;
224 /* Grab UEFI information placed in FDT by stub */
225 if (!efi_get_fdt_params(¶ms
))
228 efi_system_table
= params
.system_table
;
230 efi
.memmap
.phys_map
= params
.mmap
;
231 efi
.memmap
.map
= early_memremap_ro(params
.mmap
, params
.mmap_size
);
232 if (efi
.memmap
.map
== NULL
) {
234 * If we are booting via UEFI, the UEFI memory map is the only
235 * description of memory we have, so there is little point in
236 * proceeding if we cannot access it.
238 panic("Unable to map EFI memory map.\n");
240 efi
.memmap
.map_end
= efi
.memmap
.map
+ params
.mmap_size
;
241 efi
.memmap
.desc_size
= params
.desc_size
;
242 efi
.memmap
.desc_version
= params
.desc_ver
;
244 WARN(efi
.memmap
.desc_version
!= 1,
245 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
246 efi
.memmap
.desc_version
);
253 early_memunmap(efi
.memmap
.map
, params
.mmap_size
);
255 memblock_reserve(params
.mmap
& PAGE_MASK
,
256 PAGE_ALIGN(params
.mmap_size
+
257 (params
.mmap
& ~PAGE_MASK
)));
262 static int __init
register_gop_device(void)
266 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
)
269 pd
= platform_device_register_data(NULL
, "efi-framebuffer", 0,
270 &screen_info
, sizeof(screen_info
));
271 return PTR_ERR_OR_ZERO(pd
);
273 subsys_initcall(register_gop_device
);