blob: ac88ec05eb7027a0ff893231045566c16461297e [file] [log] [blame]
Tom Gundersena9499fa2013-02-08 15:37:06 +00001/*
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
Leif Lindholm272686b2013-09-05 11:34:54 +010016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Tom Gundersena9499fa2013-02-08 15:37:06 +000018#include <linux/kobject.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/device.h>
22#include <linux/efi.h>
Mark Salter0302f712013-12-30 12:12:12 -050023#include <linux/of.h>
24#include <linux/of_fdt.h>
Leif Lindholm272686b2013-09-05 11:34:54 +010025#include <linux/io.h>
26
27struct 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,
Dave Younga0998eb2013-12-20 18:02:17 +080037 .fw_vendor = EFI_INVALID_TABLE_ADDR,
38 .runtime = EFI_INVALID_TABLE_ADDR,
39 .config_table = EFI_INVALID_TABLE_ADDR,
Leif Lindholm272686b2013-09-05 11:34:54 +010040};
41EXPORT_SYMBOL(efi);
Tom Gundersena9499fa2013-02-08 15:37:06 +000042
43static struct kobject *efi_kobj;
44static struct kobject *efivars_kobj;
45
46/*
47 * Let's not leave out systab information that snuck into
48 * the efivars driver
49 */
50static ssize_t systab_show(struct kobject *kobj,
51 struct kobj_attribute *attr, char *buf)
52{
53 char *str = buf;
54
55 if (!kobj || !buf)
56 return -EINVAL;
57
58 if (efi.mps != EFI_INVALID_TABLE_ADDR)
59 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
60 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
61 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
62 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
63 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
64 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
65 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
66 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
67 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
68 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
69 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
70 if (efi.uga != EFI_INVALID_TABLE_ADDR)
71 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
72
73 return str - buf;
74}
75
76static struct kobj_attribute efi_attr_systab =
77 __ATTR(systab, 0400, systab_show, NULL);
78
Dave Younga0998eb2013-12-20 18:02:17 +080079#define EFI_FIELD(var) efi.var
80
81#define EFI_ATTR_SHOW(name) \
82static ssize_t name##_show(struct kobject *kobj, \
83 struct kobj_attribute *attr, char *buf) \
84{ \
85 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
86}
87
88EFI_ATTR_SHOW(fw_vendor);
89EFI_ATTR_SHOW(runtime);
90EFI_ATTR_SHOW(config_table);
91
92static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
93static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
94static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
95
Tom Gundersena9499fa2013-02-08 15:37:06 +000096static struct attribute *efi_subsys_attrs[] = {
97 &efi_attr_systab.attr,
Dave Younga0998eb2013-12-20 18:02:17 +080098 &efi_attr_fw_vendor.attr,
99 &efi_attr_runtime.attr,
100 &efi_attr_config_table.attr,
101 NULL,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000102};
103
Dave Younga0998eb2013-12-20 18:02:17 +0800104static umode_t efi_attr_is_visible(struct kobject *kobj,
105 struct attribute *attr, int n)
106{
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200107 if (attr == &efi_attr_fw_vendor.attr) {
108 if (efi_enabled(EFI_PARAVIRT) ||
109 efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
110 return 0;
111 } else if (attr == &efi_attr_runtime.attr) {
112 if (efi.runtime == EFI_INVALID_TABLE_ADDR)
113 return 0;
114 } else if (attr == &efi_attr_config_table.attr) {
115 if (efi.config_table == EFI_INVALID_TABLE_ADDR)
116 return 0;
117 }
Dave Younga0998eb2013-12-20 18:02:17 +0800118
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200119 return attr->mode;
Dave Younga0998eb2013-12-20 18:02:17 +0800120}
121
Tom Gundersena9499fa2013-02-08 15:37:06 +0000122static struct attribute_group efi_subsys_attr_group = {
123 .attrs = efi_subsys_attrs,
Dave Younga0998eb2013-12-20 18:02:17 +0800124 .is_visible = efi_attr_is_visible,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000125};
126
127static struct efivars generic_efivars;
128static struct efivar_operations generic_ops;
129
130static int generic_ops_register(void)
131{
132 generic_ops.get_variable = efi.get_variable;
133 generic_ops.set_variable = efi.set_variable;
134 generic_ops.get_next_variable = efi.get_next_variable;
Matt Fleminga614e192013-04-30 11:30:24 +0100135 generic_ops.query_variable_store = efi_query_variable_store;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000136
137 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
138}
139
140static void generic_ops_unregister(void)
141{
142 efivars_unregister(&generic_efivars);
143}
144
145/*
146 * We register the efi subsystem with the firmware subsystem and the
147 * efivars subsystem with the efi subsystem, if the system was booted with
148 * EFI.
149 */
150static int __init efisubsys_init(void)
151{
152 int error;
153
154 if (!efi_enabled(EFI_BOOT))
155 return 0;
156
157 /* We register the efi directory at /sys/firmware/efi */
158 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
159 if (!efi_kobj) {
160 pr_err("efi: Firmware registration failed.\n");
161 return -ENOMEM;
162 }
163
164 error = generic_ops_register();
165 if (error)
166 goto err_put;
167
168 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
169 if (error) {
170 pr_err("efi: Sysfs attribute export failed with error %d.\n",
171 error);
172 goto err_unregister;
173 }
174
Dave Young926172d2013-12-20 18:02:18 +0800175 error = efi_runtime_map_init(efi_kobj);
176 if (error)
177 goto err_remove_group;
178
Tom Gundersena9499fa2013-02-08 15:37:06 +0000179 /* and the standard mountpoint for efivarfs */
180 efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
181 if (!efivars_kobj) {
182 pr_err("efivars: Subsystem registration failed.\n");
183 error = -ENOMEM;
184 goto err_remove_group;
185 }
186
187 return 0;
188
189err_remove_group:
190 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
191err_unregister:
192 generic_ops_unregister();
193err_put:
194 kobject_put(efi_kobj);
195 return error;
196}
197
198subsys_initcall(efisubsys_init);
Leif Lindholm272686b2013-09-05 11:34:54 +0100199
200
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100201/*
202 * We can't ioremap data in EFI boot services RAM, because we've already mapped
203 * it as RAM. So, look it up in the existing EFI memory map instead. Only
204 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
205 */
206void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
207{
208 struct efi_memory_map *map;
209 void *p;
210 map = efi.memmap;
211 if (!map)
212 return NULL;
213 if (WARN_ON(!map->map))
214 return NULL;
215 for (p = map->map; p < map->map_end; p += map->desc_size) {
216 efi_memory_desc_t *md = p;
217 u64 size = md->num_pages << EFI_PAGE_SHIFT;
218 u64 end = md->phys_addr + size;
219 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
220 md->type != EFI_BOOT_SERVICES_CODE &&
221 md->type != EFI_BOOT_SERVICES_DATA)
222 continue;
223 if (!md->virt_addr)
224 continue;
225 if (phys_addr >= md->phys_addr && phys_addr < end) {
226 phys_addr += md->virt_addr - md->phys_addr;
227 return (__force void __iomem *)(unsigned long)phys_addr;
228 }
229 }
230 return NULL;
231}
232
Leif Lindholm272686b2013-09-05 11:34:54 +0100233static __initdata efi_config_table_type_t common_tables[] = {
234 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
235 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
236 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
237 {MPS_TABLE_GUID, "MPS", &efi.mps},
238 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
239 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
240 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
Daeseok Youn69e60842014-02-13 17:16:36 +0900241 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +0100242};
243
244static __init int match_config_table(efi_guid_t *guid,
245 unsigned long table,
246 efi_config_table_type_t *table_types)
247{
248 u8 str[EFI_VARIABLE_GUID_LEN + 1];
249 int i;
250
251 if (table_types) {
252 efi_guid_unparse(guid, str);
253
254 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
255 efi_guid_unparse(&table_types[i].guid, str);
256
257 if (!efi_guidcmp(*guid, table_types[i].guid)) {
258 *(table_types[i].ptr) = table;
259 pr_cont(" %s=0x%lx ",
260 table_types[i].name, table);
261 return 1;
262 }
263 }
264 }
265
266 return 0;
267}
268
269int __init efi_config_init(efi_config_table_type_t *arch_tables)
270{
271 void *config_tables, *tablep;
272 int i, sz;
273
274 if (efi_enabled(EFI_64BIT))
275 sz = sizeof(efi_config_table_64_t);
276 else
277 sz = sizeof(efi_config_table_32_t);
278
279 /*
280 * Let's see what config tables the firmware passed to us.
281 */
282 config_tables = early_memremap(efi.systab->tables,
283 efi.systab->nr_tables * sz);
284 if (config_tables == NULL) {
285 pr_err("Could not map Configuration table!\n");
286 return -ENOMEM;
287 }
288
289 tablep = config_tables;
290 pr_info("");
291 for (i = 0; i < efi.systab->nr_tables; i++) {
292 efi_guid_t guid;
293 unsigned long table;
294
295 if (efi_enabled(EFI_64BIT)) {
296 u64 table64;
297 guid = ((efi_config_table_64_t *)tablep)->guid;
298 table64 = ((efi_config_table_64_t *)tablep)->table;
299 table = table64;
300#ifndef CONFIG_64BIT
301 if (table64 >> 32) {
302 pr_cont("\n");
303 pr_err("Table located above 4GB, disabling EFI.\n");
Daniel Kiperabc93f82014-06-30 19:52:56 +0200304 early_memunmap(config_tables,
Leif Lindholm272686b2013-09-05 11:34:54 +0100305 efi.systab->nr_tables * sz);
306 return -EINVAL;
307 }
308#endif
309 } else {
310 guid = ((efi_config_table_32_t *)tablep)->guid;
311 table = ((efi_config_table_32_t *)tablep)->table;
312 }
313
314 if (!match_config_table(&guid, table, common_tables))
315 match_config_table(&guid, table, arch_tables);
316
317 tablep += sz;
318 }
319 pr_cont("\n");
Daniel Kiperabc93f82014-06-30 19:52:56 +0200320 early_memunmap(config_tables, efi.systab->nr_tables * sz);
Matt Fleming0f8093a2014-01-15 13:36:33 +0000321
322 set_bit(EFI_CONFIG_TABLES, &efi.flags);
323
Leif Lindholm272686b2013-09-05 11:34:54 +0100324 return 0;
325}
Mark Salter0302f712013-12-30 12:12:12 -0500326
327#ifdef CONFIG_EFI_PARAMS_FROM_FDT
328
329#define UEFI_PARAM(name, prop, field) \
330 { \
331 { name }, \
332 { prop }, \
333 offsetof(struct efi_fdt_params, field), \
334 FIELD_SIZEOF(struct efi_fdt_params, field) \
335 }
336
337static __initdata struct {
338 const char name[32];
339 const char propname[32];
340 int offset;
341 int size;
342} dt_params[] = {
343 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
344 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
345 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
346 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
347 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
348};
349
350struct param_info {
351 int verbose;
352 void *params;
353};
354
355static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
356 int depth, void *data)
357{
358 struct param_info *info = data;
359 void *prop, *dest;
360 unsigned long len;
361 u64 val;
362 int i;
363
364 if (depth != 1 ||
365 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
366 return 0;
367
368 pr_info("Getting parameters from FDT:\n");
369
370 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
371 prop = of_get_flat_dt_prop(node, dt_params[i].propname, &len);
372 if (!prop) {
373 pr_err("Can't find %s in device tree!\n",
374 dt_params[i].name);
375 return 0;
376 }
377 dest = info->params + dt_params[i].offset;
378
379 val = of_read_number(prop, len / sizeof(u32));
380
381 if (dt_params[i].size == sizeof(u32))
382 *(u32 *)dest = val;
383 else
384 *(u64 *)dest = val;
385
386 if (info->verbose)
387 pr_info(" %s: 0x%0*llx\n", dt_params[i].name,
388 dt_params[i].size * 2, val);
389 }
390 return 1;
391}
392
393int __init efi_get_fdt_params(struct efi_fdt_params *params, int verbose)
394{
395 struct param_info info;
396
397 info.verbose = verbose;
398 info.params = params;
399
400 return of_scan_flat_dt(fdt_find_uefi_params, &info);
401}
402#endif /* CONFIG_EFI_PARAMS_FROM_FDT */