Merge tag 'v3.10.72' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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/of.h>
24 #include <linux/of_fdt.h>
25 #include <linux/io.h>
26
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,
37 };
38 EXPORT_SYMBOL(efi);
39
40 static struct kobject *efi_kobj;
41 static struct kobject *efivars_kobj;
42
43 /*
44  * Let's not leave out systab information that snuck into
45  * the efivars driver
46  */
47 static ssize_t systab_show(struct kobject *kobj,
48                            struct kobj_attribute *attr, char *buf)
49 {
50         char *str = buf;
51
52         if (!kobj || !buf)
53                 return -EINVAL;
54
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);
69
70         return str - buf;
71 }
72
73 static struct kobj_attribute efi_attr_systab =
74                         __ATTR(systab, 0400, systab_show, NULL);
75
76 static struct attribute *efi_subsys_attrs[] = {
77         &efi_attr_systab.attr,
78         NULL,   /* maybe more in the future? */
79 };
80
81 static struct attribute_group efi_subsys_attr_group = {
82         .attrs = efi_subsys_attrs,
83 };
84
85 static struct efivars generic_efivars;
86 static struct efivar_operations generic_ops;
87
88 static int generic_ops_register(void)
89 {
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;
94
95         return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
96 }
97
98 static void generic_ops_unregister(void)
99 {
100         efivars_unregister(&generic_efivars);
101 }
102
103 /*
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
106  * EFI.
107  */
108 static int __init efisubsys_init(void)
109 {
110         int error;
111
112         if (!efi_enabled(EFI_BOOT))
113                 return 0;
114
115         /* We register the efi directory at /sys/firmware/efi */
116         efi_kobj = kobject_create_and_add("efi", firmware_kobj);
117         if (!efi_kobj) {
118                 pr_err("efi: Firmware registration failed.\n");
119                 return -ENOMEM;
120         }
121
122         error = generic_ops_register();
123         if (error)
124                 goto err_put;
125
126         error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
127         if (error) {
128                 pr_err("efi: Sysfs attribute export failed with error %d.\n",
129                        error);
130                 goto err_unregister;
131         }
132
133         /* and the standard mountpoint for efivarfs */
134         efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
135         if (!efivars_kobj) {
136                 pr_err("efivars: Subsystem registration failed.\n");
137                 error = -ENOMEM;
138                 goto err_remove_group;
139         }
140
141         return 0;
142
143 err_remove_group:
144         sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
145 err_unregister:
146         generic_ops_unregister();
147 err_put:
148         kobject_put(efi_kobj);
149         return error;
150 }
151
152 subsys_initcall(efisubsys_init);
153
154
155 /*
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.
159  */
160 void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
161 {
162         struct efi_memory_map *map;
163         void *p;
164         map = efi.memmap;
165         if (!map)
166                 return NULL;
167         if (WARN_ON(!map->map))
168                 return NULL;
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)
176                         continue;
177                 if (!md->virt_addr)
178                         continue;
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;
182                 }
183         }
184         return NULL;
185 }
186
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},
196 };
197
198 static __init int match_config_table(efi_guid_t *guid,
199                                      unsigned long table,
200                                      efi_config_table_type_t *table_types)
201 {
202         u8 str[EFI_VARIABLE_GUID_LEN + 1];
203         int i;
204
205         if (table_types) {
206                 efi_guid_unparse(guid, str);
207
208                 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
209                         efi_guid_unparse(&table_types[i].guid, str);
210
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);
215                                 return 1;
216                         }
217                 }
218         }
219
220         return 0;
221 }
222
223 int __init efi_config_init(efi_config_table_type_t *arch_tables)
224 {
225         void *config_tables, *tablep;
226         int i, sz;
227
228         if (efi_enabled(EFI_64BIT))
229                 sz = sizeof(efi_config_table_64_t);
230         else
231                 sz = sizeof(efi_config_table_32_t);
232
233         /*
234          * Let's see what config tables the firmware passed to us.
235          */
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");
240                 return -ENOMEM;
241         }
242
243         tablep = config_tables;
244         pr_info("");
245         for (i = 0; i < efi.systab->nr_tables; i++) {
246                 efi_guid_t guid;
247                 unsigned long table;
248
249                 if (efi_enabled(EFI_64BIT)) {
250                         u64 table64;
251                         guid = ((efi_config_table_64_t *)tablep)->guid;
252                         table64 = ((efi_config_table_64_t *)tablep)->table;
253                         table = table64;
254 #ifndef CONFIG_64BIT
255                         if (table64 >> 32) {
256                                 pr_cont("\n");
257                                 pr_err("Table located above 4GB, disabling EFI.\n");
258                                 early_iounmap(config_tables,
259                                                efi.systab->nr_tables * sz);
260                                 return -EINVAL;
261                         }
262 #endif
263                 } else {
264                         guid = ((efi_config_table_32_t *)tablep)->guid;
265                         table = ((efi_config_table_32_t *)tablep)->table;
266                 }
267
268                 if (!match_config_table(&guid, table, common_tables))
269                         match_config_table(&guid, table, arch_tables);
270
271                 tablep += sz;
272         }
273         pr_cont("\n");
274         early_iounmap(config_tables, efi.systab->nr_tables * sz);
275         return 0;
276 }
277
278 #ifdef CONFIG_EFI_PARAMS_FROM_FDT
279
280 #define UEFI_PARAM(name, prop, field)                      \
281         {                                                  \
282                 { name },                                  \
283                 { prop },                                  \
284                 offsetof(struct efi_fdt_params, field),    \
285                 FIELD_SIZEOF(struct efi_fdt_params, field) \
286         }
287
288 static __initdata struct {
289         const char name[32];
290         const char propname[32];
291         int offset;
292         int size;
293 } dt_params[] = {
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)
299 };
300
301 struct param_info {
302         int verbose;
303         int found;
304         void *params;
305 };
306
307 static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
308                                        int depth, void *data)
309 {
310         struct param_info *info = data;
311         const void *prop;
312         void *dest;
313         u64 val;
314         int i, len;
315
316         if (depth != 1 ||
317             (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
318                 return 0;
319
320         for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
321                 prop = of_get_flat_dt_prop(node, dt_params[i].propname, &len);
322                 if (!prop)
323                         return 0;
324                 dest = info->params + dt_params[i].offset;
325                 info->found++;
326
327                 val = of_read_number(prop, len / sizeof(u32));
328
329                 if (dt_params[i].size == sizeof(u32))
330                         *(u32 *)dest = val;
331                 else
332                         *(u64 *)dest = val;
333
334                 if (info->verbose)
335                         pr_info("  %s: 0x%0*llx\n", dt_params[i].name,
336                                 dt_params[i].size * 2, val);
337         }
338         return 1;
339 }
340
341 int __init efi_get_fdt_params(struct efi_fdt_params *params, int verbose)
342 {
343         struct param_info info;
344         int ret;
345
346         pr_info("Getting EFI parameters from FDT:\n");
347
348         info.verbose = verbose;
349         info.found = 0;
350         info.params = params;
351
352         ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
353         if (!info.found)
354                 pr_info("UEFI not found.\n");
355         else if (!ret)
356                 pr_err("Can't find '%s' in device tree!\n",
357                        dt_params[info.found].name);
358
359         return ret;
360 }
361 #endif /* CONFIG_EFI_PARAMS_FROM_FDT */