2 * efi.c - EFI subsystem
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
6 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
8 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
9 * allowing the efivarfs to be mounted or the efivars module to be loaded.
10 * The existance of /sys/firmware/efi may also be used by userspace to
11 * determine that the system supports EFI.
13 * This file is released under the GPLv2.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kobject.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/efi.h>
24 #include <linux/of_fdt.h>
27 struct efi __read_mostly efi = {
28 .mps = EFI_INVALID_TABLE_ADDR,
29 .acpi = EFI_INVALID_TABLE_ADDR,
30 .acpi20 = EFI_INVALID_TABLE_ADDR,
31 .smbios = EFI_INVALID_TABLE_ADDR,
32 .sal_systab = EFI_INVALID_TABLE_ADDR,
33 .boot_info = EFI_INVALID_TABLE_ADDR,
34 .hcdp = EFI_INVALID_TABLE_ADDR,
35 .uga = EFI_INVALID_TABLE_ADDR,
36 .uv_systab = EFI_INVALID_TABLE_ADDR,
40 static struct kobject *efi_kobj;
41 static struct kobject *efivars_kobj;
44 * Let's not leave out systab information that snuck into
47 static ssize_t systab_show(struct kobject *kobj,
48 struct kobj_attribute *attr, char *buf)
55 if (efi.mps != EFI_INVALID_TABLE_ADDR)
56 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
57 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
58 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
59 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
60 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
61 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
62 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
63 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
64 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
65 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
66 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
67 if (efi.uga != EFI_INVALID_TABLE_ADDR)
68 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
73 static struct kobj_attribute efi_attr_systab =
74 __ATTR(systab, 0400, systab_show, NULL);
76 static struct attribute *efi_subsys_attrs[] = {
77 &efi_attr_systab.attr,
78 NULL, /* maybe more in the future? */
81 static struct attribute_group efi_subsys_attr_group = {
82 .attrs = efi_subsys_attrs,
85 static struct efivars generic_efivars;
86 static struct efivar_operations generic_ops;
88 static int generic_ops_register(void)
90 generic_ops.get_variable = efi.get_variable;
91 generic_ops.set_variable = efi.set_variable;
92 generic_ops.get_next_variable = efi.get_next_variable;
93 generic_ops.query_variable_store = efi_query_variable_store;
95 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
98 static void generic_ops_unregister(void)
100 efivars_unregister(&generic_efivars);
104 * We register the efi subsystem with the firmware subsystem and the
105 * efivars subsystem with the efi subsystem, if the system was booted with
108 static int __init efisubsys_init(void)
112 if (!efi_enabled(EFI_BOOT))
115 /* We register the efi directory at /sys/firmware/efi */
116 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
118 pr_err("efi: Firmware registration failed.\n");
122 error = generic_ops_register();
126 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
128 pr_err("efi: Sysfs attribute export failed with error %d.\n",
133 /* and the standard mountpoint for efivarfs */
134 efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
136 pr_err("efivars: Subsystem registration failed.\n");
138 goto err_remove_group;
144 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
146 generic_ops_unregister();
148 kobject_put(efi_kobj);
152 subsys_initcall(efisubsys_init);
156 * We can't ioremap data in EFI boot services RAM, because we've already mapped
157 * it as RAM. So, look it up in the existing EFI memory map instead. Only
158 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
160 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
162 struct efi_memory_map *map;
167 if (WARN_ON(!map->map))
169 for (p = map->map; p < map->map_end; p += map->desc_size) {
170 efi_memory_desc_t *md = p;
171 u64 size = md->num_pages << EFI_PAGE_SHIFT;
172 u64 end = md->phys_addr + size;
173 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
174 md->type != EFI_BOOT_SERVICES_CODE &&
175 md->type != EFI_BOOT_SERVICES_DATA)
179 if (phys_addr >= md->phys_addr && phys_addr < end) {
180 phys_addr += md->virt_addr - md->phys_addr;
181 return (__force void __iomem *)(unsigned long)phys_addr;
187 static __initdata efi_config_table_type_t common_tables[] = {
188 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
189 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
190 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
191 {MPS_TABLE_GUID, "MPS", &efi.mps},
192 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
193 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
194 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
195 {NULL_GUID, NULL, NULL},
198 static __init int match_config_table(efi_guid_t *guid,
200 efi_config_table_type_t *table_types)
202 u8 str[EFI_VARIABLE_GUID_LEN + 1];
206 efi_guid_unparse(guid, str);
208 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
209 efi_guid_unparse(&table_types[i].guid, str);
211 if (!efi_guidcmp(*guid, table_types[i].guid)) {
212 *(table_types[i].ptr) = table;
213 pr_cont(" %s=0x%lx ",
214 table_types[i].name, table);
223 int __init efi_config_init(efi_config_table_type_t *arch_tables)
225 void *config_tables, *tablep;
228 if (efi_enabled(EFI_64BIT))
229 sz = sizeof(efi_config_table_64_t);
231 sz = sizeof(efi_config_table_32_t);
234 * Let's see what config tables the firmware passed to us.
236 config_tables = early_memremap(efi.systab->tables,
237 efi.systab->nr_tables * sz);
238 if (config_tables == NULL) {
239 pr_err("Could not map Configuration table!\n");
243 tablep = config_tables;
245 for (i = 0; i < efi.systab->nr_tables; i++) {
249 if (efi_enabled(EFI_64BIT)) {
251 guid = ((efi_config_table_64_t *)tablep)->guid;
252 table64 = ((efi_config_table_64_t *)tablep)->table;
257 pr_err("Table located above 4GB, disabling EFI.\n");
258 early_iounmap(config_tables,
259 efi.systab->nr_tables * sz);
264 guid = ((efi_config_table_32_t *)tablep)->guid;
265 table = ((efi_config_table_32_t *)tablep)->table;
268 if (!match_config_table(&guid, table, common_tables))
269 match_config_table(&guid, table, arch_tables);
274 early_iounmap(config_tables, efi.systab->nr_tables * sz);
278 #ifdef CONFIG_EFI_PARAMS_FROM_FDT
280 #define UEFI_PARAM(name, prop, field) \
284 offsetof(struct efi_fdt_params, field), \
285 FIELD_SIZEOF(struct efi_fdt_params, field) \
288 static __initdata struct {
290 const char propname[32];
294 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
295 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
296 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
297 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
298 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
307 static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
308 int depth, void *data)
310 struct param_info *info = data;
317 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
320 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
321 prop = of_get_flat_dt_prop(node, dt_params[i].propname, &len);
324 dest = info->params + dt_params[i].offset;
327 val = of_read_number(prop, len / sizeof(u32));
329 if (dt_params[i].size == sizeof(u32))
335 pr_info(" %s: 0x%0*llx\n", dt_params[i].name,
336 dt_params[i].size * 2, val);
341 int __init efi_get_fdt_params(struct efi_fdt_params *params, int verbose)
343 struct param_info info;
346 pr_info("Getting EFI parameters from FDT:\n");
348 info.verbose = verbose;
350 info.params = params;
352 ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
354 pr_info("UEFI not found.\n");
356 pr_err("Can't find '%s' in device tree!\n",
357 dt_params[info.found].name);
361 #endif /* CONFIG_EFI_PARAMS_FROM_FDT */