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>
25 struct efi __read_mostly efi = {
26 .mps = EFI_INVALID_TABLE_ADDR,
27 .acpi = EFI_INVALID_TABLE_ADDR,
28 .acpi20 = EFI_INVALID_TABLE_ADDR,
29 .smbios = EFI_INVALID_TABLE_ADDR,
30 .sal_systab = EFI_INVALID_TABLE_ADDR,
31 .boot_info = EFI_INVALID_TABLE_ADDR,
32 .hcdp = EFI_INVALID_TABLE_ADDR,
33 .uga = EFI_INVALID_TABLE_ADDR,
34 .uv_systab = EFI_INVALID_TABLE_ADDR,
38 static struct kobject *efi_kobj;
39 static struct kobject *efivars_kobj;
42 * Let's not leave out systab information that snuck into
45 static ssize_t systab_show(struct kobject *kobj,
46 struct kobj_attribute *attr, char *buf)
53 if (efi.mps != EFI_INVALID_TABLE_ADDR)
54 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
55 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
56 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
57 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
58 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
59 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
60 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
61 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
62 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
63 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
64 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
65 if (efi.uga != EFI_INVALID_TABLE_ADDR)
66 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
71 static struct kobj_attribute efi_attr_systab =
72 __ATTR(systab, 0400, systab_show, NULL);
74 static struct attribute *efi_subsys_attrs[] = {
75 &efi_attr_systab.attr,
76 NULL, /* maybe more in the future? */
79 static struct attribute_group efi_subsys_attr_group = {
80 .attrs = efi_subsys_attrs,
83 static struct efivars generic_efivars;
84 static struct efivar_operations generic_ops;
86 static int generic_ops_register(void)
88 generic_ops.get_variable = efi.get_variable;
89 generic_ops.set_variable = efi.set_variable;
90 generic_ops.get_next_variable = efi.get_next_variable;
91 generic_ops.query_variable_store = efi_query_variable_store;
93 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
96 static void generic_ops_unregister(void)
98 efivars_unregister(&generic_efivars);
102 * We register the efi subsystem with the firmware subsystem and the
103 * efivars subsystem with the efi subsystem, if the system was booted with
106 static int __init efisubsys_init(void)
110 if (!efi_enabled(EFI_BOOT))
113 /* We register the efi directory at /sys/firmware/efi */
114 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
116 pr_err("efi: Firmware registration failed.\n");
120 error = generic_ops_register();
124 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
126 pr_err("efi: Sysfs attribute export failed with error %d.\n",
131 /* and the standard mountpoint for efivarfs */
132 efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
134 pr_err("efivars: Subsystem registration failed.\n");
136 goto err_remove_group;
142 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
144 generic_ops_unregister();
146 kobject_put(efi_kobj);
150 subsys_initcall(efisubsys_init);
154 * We can't ioremap data in EFI boot services RAM, because we've already mapped
155 * it as RAM. So, look it up in the existing EFI memory map instead. Only
156 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
158 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
160 struct efi_memory_map *map;
165 if (WARN_ON(!map->map))
167 for (p = map->map; p < map->map_end; p += map->desc_size) {
168 efi_memory_desc_t *md = p;
169 u64 size = md->num_pages << EFI_PAGE_SHIFT;
170 u64 end = md->phys_addr + size;
171 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
172 md->type != EFI_BOOT_SERVICES_CODE &&
173 md->type != EFI_BOOT_SERVICES_DATA)
177 if (phys_addr >= md->phys_addr && phys_addr < end) {
178 phys_addr += md->virt_addr - md->phys_addr;
179 return (__force void __iomem *)(unsigned long)phys_addr;
185 static __initdata efi_config_table_type_t common_tables[] = {
186 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
187 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
188 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
189 {MPS_TABLE_GUID, "MPS", &efi.mps},
190 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
191 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
192 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
193 {NULL_GUID, NULL, 0},
196 static __init int match_config_table(efi_guid_t *guid,
198 efi_config_table_type_t *table_types)
200 u8 str[EFI_VARIABLE_GUID_LEN + 1];
204 efi_guid_unparse(guid, str);
206 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
207 efi_guid_unparse(&table_types[i].guid, str);
209 if (!efi_guidcmp(*guid, table_types[i].guid)) {
210 *(table_types[i].ptr) = table;
211 pr_cont(" %s=0x%lx ",
212 table_types[i].name, table);
221 int __init efi_config_init(efi_config_table_type_t *arch_tables)
223 void *config_tables, *tablep;
226 if (efi_enabled(EFI_64BIT))
227 sz = sizeof(efi_config_table_64_t);
229 sz = sizeof(efi_config_table_32_t);
232 * Let's see what config tables the firmware passed to us.
234 config_tables = early_memremap(efi.systab->tables,
235 efi.systab->nr_tables * sz);
236 if (config_tables == NULL) {
237 pr_err("Could not map Configuration table!\n");
241 tablep = config_tables;
243 for (i = 0; i < efi.systab->nr_tables; i++) {
247 if (efi_enabled(EFI_64BIT)) {
249 guid = ((efi_config_table_64_t *)tablep)->guid;
250 table64 = ((efi_config_table_64_t *)tablep)->table;
255 pr_err("Table located above 4GB, disabling EFI.\n");
256 early_iounmap(config_tables,
257 efi.systab->nr_tables * sz);
262 guid = ((efi_config_table_32_t *)tablep)->guid;
263 table = ((efi_config_table_32_t *)tablep)->table;
266 if (!match_config_table(&guid, table, common_tables))
267 match_config_table(&guid, table, arch_tables);
272 early_iounmap(config_tables, efi.systab->nr_tables * sz);