Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / firmware / efi / efi.c
1 /*
2  * efi.c - EFI subsystem
3  *
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>
7  *
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.
12  *
13  * This file is released under the GPLv2.
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
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>
23 #include <linux/io.h>
24
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,
35         .fw_vendor  = EFI_INVALID_TABLE_ADDR,
36         .runtime    = EFI_INVALID_TABLE_ADDR,
37         .config_table  = EFI_INVALID_TABLE_ADDR,
38 };
39 EXPORT_SYMBOL(efi);
40
41 static struct kobject *efi_kobj;
42 static struct kobject *efivars_kobj;
43
44 /*
45  * Let's not leave out systab information that snuck into
46  * the efivars driver
47  */
48 static ssize_t systab_show(struct kobject *kobj,
49                            struct kobj_attribute *attr, char *buf)
50 {
51         char *str = buf;
52
53         if (!kobj || !buf)
54                 return -EINVAL;
55
56         if (efi.mps != EFI_INVALID_TABLE_ADDR)
57                 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
58         if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
59                 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
60         if (efi.acpi != EFI_INVALID_TABLE_ADDR)
61                 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
62         if (efi.smbios != EFI_INVALID_TABLE_ADDR)
63                 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
64         if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
65                 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
66         if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
67                 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
68         if (efi.uga != EFI_INVALID_TABLE_ADDR)
69                 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
70
71         return str - buf;
72 }
73
74 static struct kobj_attribute efi_attr_systab =
75                         __ATTR(systab, 0400, systab_show, NULL);
76
77 #define EFI_FIELD(var) efi.var
78
79 #define EFI_ATTR_SHOW(name) \
80 static ssize_t name##_show(struct kobject *kobj, \
81                                 struct kobj_attribute *attr, char *buf) \
82 { \
83         return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
84 }
85
86 EFI_ATTR_SHOW(fw_vendor);
87 EFI_ATTR_SHOW(runtime);
88 EFI_ATTR_SHOW(config_table);
89
90 static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
91 static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
92 static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
93
94 static struct attribute *efi_subsys_attrs[] = {
95         &efi_attr_systab.attr,
96         &efi_attr_fw_vendor.attr,
97         &efi_attr_runtime.attr,
98         &efi_attr_config_table.attr,
99         NULL,
100 };
101
102 static umode_t efi_attr_is_visible(struct kobject *kobj,
103                                    struct attribute *attr, int n)
104 {
105         umode_t mode = attr->mode;
106
107         if (attr == &efi_attr_fw_vendor.attr)
108                 return (efi.fw_vendor == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
109         else if (attr == &efi_attr_runtime.attr)
110                 return (efi.runtime == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
111         else if (attr == &efi_attr_config_table.attr)
112                 return (efi.config_table == EFI_INVALID_TABLE_ADDR) ? 0 : mode;
113
114         return mode;
115 }
116
117 static struct attribute_group efi_subsys_attr_group = {
118         .attrs = efi_subsys_attrs,
119         .is_visible = efi_attr_is_visible,
120 };
121
122 static struct efivars generic_efivars;
123 static struct efivar_operations generic_ops;
124
125 static int generic_ops_register(void)
126 {
127         generic_ops.get_variable = efi.get_variable;
128         generic_ops.set_variable = efi.set_variable;
129         generic_ops.get_next_variable = efi.get_next_variable;
130         generic_ops.query_variable_store = efi_query_variable_store;
131
132         return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
133 }
134
135 static void generic_ops_unregister(void)
136 {
137         efivars_unregister(&generic_efivars);
138 }
139
140 /*
141  * We register the efi subsystem with the firmware subsystem and the
142  * efivars subsystem with the efi subsystem, if the system was booted with
143  * EFI.
144  */
145 static int __init efisubsys_init(void)
146 {
147         int error;
148
149         if (!efi_enabled(EFI_BOOT))
150                 return 0;
151
152         /* We register the efi directory at /sys/firmware/efi */
153         efi_kobj = kobject_create_and_add("efi", firmware_kobj);
154         if (!efi_kobj) {
155                 pr_err("efi: Firmware registration failed.\n");
156                 return -ENOMEM;
157         }
158
159         error = generic_ops_register();
160         if (error)
161                 goto err_put;
162
163         error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
164         if (error) {
165                 pr_err("efi: Sysfs attribute export failed with error %d.\n",
166                        error);
167                 goto err_unregister;
168         }
169
170         error = efi_runtime_map_init(efi_kobj);
171         if (error)
172                 goto err_remove_group;
173
174         /* and the standard mountpoint for efivarfs */
175         efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
176         if (!efivars_kobj) {
177                 pr_err("efivars: Subsystem registration failed.\n");
178                 error = -ENOMEM;
179                 goto err_remove_group;
180         }
181
182         return 0;
183
184 err_remove_group:
185         sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
186 err_unregister:
187         generic_ops_unregister();
188 err_put:
189         kobject_put(efi_kobj);
190         return error;
191 }
192
193 subsys_initcall(efisubsys_init);
194
195
196 /*
197  * We can't ioremap data in EFI boot services RAM, because we've already mapped
198  * it as RAM.  So, look it up in the existing EFI memory map instead.  Only
199  * callable after efi_enter_virtual_mode and before efi_free_boot_services.
200  */
201 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
202 {
203         struct efi_memory_map *map;
204         void *p;
205         map = efi.memmap;
206         if (!map)
207                 return NULL;
208         if (WARN_ON(!map->map))
209                 return NULL;
210         for (p = map->map; p < map->map_end; p += map->desc_size) {
211                 efi_memory_desc_t *md = p;
212                 u64 size = md->num_pages << EFI_PAGE_SHIFT;
213                 u64 end = md->phys_addr + size;
214                 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
215                     md->type != EFI_BOOT_SERVICES_CODE &&
216                     md->type != EFI_BOOT_SERVICES_DATA)
217                         continue;
218                 if (!md->virt_addr)
219                         continue;
220                 if (phys_addr >= md->phys_addr && phys_addr < end) {
221                         phys_addr += md->virt_addr - md->phys_addr;
222                         return (__force void __iomem *)(unsigned long)phys_addr;
223                 }
224         }
225         return NULL;
226 }
227
228 static __initdata efi_config_table_type_t common_tables[] = {
229         {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
230         {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
231         {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
232         {MPS_TABLE_GUID, "MPS", &efi.mps},
233         {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
234         {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
235         {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
236         {NULL_GUID, NULL, NULL},
237 };
238
239 static __init int match_config_table(efi_guid_t *guid,
240                                      unsigned long table,
241                                      efi_config_table_type_t *table_types)
242 {
243         u8 str[EFI_VARIABLE_GUID_LEN + 1];
244         int i;
245
246         if (table_types) {
247                 efi_guid_unparse(guid, str);
248
249                 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
250                         efi_guid_unparse(&table_types[i].guid, str);
251
252                         if (!efi_guidcmp(*guid, table_types[i].guid)) {
253                                 *(table_types[i].ptr) = table;
254                                 pr_cont(" %s=0x%lx ",
255                                         table_types[i].name, table);
256                                 return 1;
257                         }
258                 }
259         }
260
261         return 0;
262 }
263
264 int __init efi_config_init(efi_config_table_type_t *arch_tables)
265 {
266         void *config_tables, *tablep;
267         int i, sz;
268
269         if (efi_enabled(EFI_64BIT))
270                 sz = sizeof(efi_config_table_64_t);
271         else
272                 sz = sizeof(efi_config_table_32_t);
273
274         /*
275          * Let's see what config tables the firmware passed to us.
276          */
277         config_tables = early_memremap(efi.systab->tables,
278                                        efi.systab->nr_tables * sz);
279         if (config_tables == NULL) {
280                 pr_err("Could not map Configuration table!\n");
281                 return -ENOMEM;
282         }
283
284         tablep = config_tables;
285         pr_info("");
286         for (i = 0; i < efi.systab->nr_tables; i++) {
287                 efi_guid_t guid;
288                 unsigned long table;
289
290                 if (efi_enabled(EFI_64BIT)) {
291                         u64 table64;
292                         guid = ((efi_config_table_64_t *)tablep)->guid;
293                         table64 = ((efi_config_table_64_t *)tablep)->table;
294                         table = table64;
295 #ifndef CONFIG_64BIT
296                         if (table64 >> 32) {
297                                 pr_cont("\n");
298                                 pr_err("Table located above 4GB, disabling EFI.\n");
299                                 early_iounmap(config_tables,
300                                                efi.systab->nr_tables * sz);
301                                 return -EINVAL;
302                         }
303 #endif
304                 } else {
305                         guid = ((efi_config_table_32_t *)tablep)->guid;
306                         table = ((efi_config_table_32_t *)tablep)->table;
307                 }
308
309                 if (!match_config_table(&guid, table, common_tables))
310                         match_config_table(&guid, table, arch_tables);
311
312                 tablep += sz;
313         }
314         pr_cont("\n");
315         early_iounmap(config_tables, efi.systab->nr_tables * sz);
316
317         set_bit(EFI_CONFIG_TABLES, &efi.flags);
318
319         return 0;
320 }