blob: d5f6b0ca521e7d49b5070f4cd043c3c064d86344 [file] [log] [blame]
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +01001/*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 2.4
5 *
6 * Copyright (C) 2013 - 2015 Linaro Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/efi.h>
15#include <linux/init.h>
16#include <linux/memblock.h>
17#include <linux/mm_types.h>
18#include <linux/of.h>
19#include <linux/of_fdt.h>
20
21#include <asm/efi.h>
22
23struct efi_memory_map memmap;
24
25u64 efi_system_table;
26
27static int __init is_normal_ram(efi_memory_desc_t *md)
28{
29 if (md->attribute & EFI_MEMORY_WB)
30 return 1;
31 return 0;
32}
33
34/*
35 * Translate a EFI virtual address into a physical address: this is necessary,
36 * as some data members of the EFI system table are virtually remapped after
37 * SetVirtualAddressMap() has been called.
38 */
39static phys_addr_t efi_to_phys(unsigned long addr)
40{
41 efi_memory_desc_t *md;
42
Matt Fleming78ce2482016-04-25 21:06:38 +010043 for_each_efi_memory_desc_in_map(&memmap, md) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010044 if (!(md->attribute & EFI_MEMORY_RUNTIME))
45 continue;
46 if (md->virt_addr == 0)
47 /* no virtual mapping has been installed by the stub */
48 break;
49 if (md->virt_addr <= addr &&
50 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
51 return md->phys_addr + addr - md->virt_addr;
52 }
53 return addr;
54}
55
56static int __init uefi_init(void)
57{
58 efi_char16_t *c16;
59 void *config_tables;
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010060 size_t table_size;
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010061 char vendor[100] = "unknown";
62 int i, retval;
63
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +000064 efi.systab = early_memremap_ro(efi_system_table,
65 sizeof(efi_system_table_t));
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010066 if (efi.systab == NULL) {
67 pr_warn("Unable to map EFI system table.\n");
68 return -ENOMEM;
69 }
70
71 set_bit(EFI_BOOT, &efi.flags);
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010072 if (IS_ENABLED(CONFIG_64BIT))
73 set_bit(EFI_64BIT, &efi.flags);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010074
75 /*
76 * Verify the EFI Table
77 */
78 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
79 pr_err("System table signature incorrect\n");
80 retval = -EINVAL;
81 goto out;
82 }
83 if ((efi.systab->hdr.revision >> 16) < 2)
84 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
85 efi.systab->hdr.revision >> 16,
86 efi.systab->hdr.revision & 0xffff);
87
Ard Biesheuvel14c43be2016-04-25 21:06:34 +010088 efi.runtime_version = efi.systab->hdr.revision;
89
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010090 /* Show what we know for posterity */
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +000091 c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
92 sizeof(vendor) * sizeof(efi_char16_t));
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010093 if (c16) {
94 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
95 vendor[i] = c16[i];
96 vendor[i] = '\0';
97 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
98 }
99
100 pr_info("EFI v%u.%.02u by %s\n",
101 efi.systab->hdr.revision >> 16,
102 efi.systab->hdr.revision & 0xffff, vendor);
103
104 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +0000105 config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
106 table_size);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100107 if (config_tables == NULL) {
108 pr_warn("Unable to map EFI config table array.\n");
109 retval = -ENOMEM;
110 goto out;
111 }
112 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
Ard Biesheuvelf7d92482015-11-30 13:28:19 +0100113 sizeof(efi_config_table_t), NULL);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100114
115 early_memunmap(config_tables, table_size);
116out:
117 early_memunmap(efi.systab, sizeof(efi_system_table_t));
118 return retval;
119}
120
121/*
122 * Return true for RAM regions we want to permanently reserve.
123 */
124static __init int is_reserve_region(efi_memory_desc_t *md)
125{
126 switch (md->type) {
127 case EFI_LOADER_CODE:
128 case EFI_LOADER_DATA:
129 case EFI_BOOT_SERVICES_CODE:
130 case EFI_BOOT_SERVICES_DATA:
131 case EFI_CONVENTIONAL_MEMORY:
132 case EFI_PERSISTENT_MEMORY:
133 return 0;
134 default:
135 break;
136 }
137 return is_normal_ram(md);
138}
139
140static __init void reserve_regions(void)
141{
142 efi_memory_desc_t *md;
143 u64 paddr, npages, size;
144
145 if (efi_enabled(EFI_DBG))
146 pr_info("Processing EFI memory map:\n");
147
Matt Fleming78ce2482016-04-25 21:06:38 +0100148 for_each_efi_memory_desc_in_map(&memmap, md) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100149 paddr = md->phys_addr;
150 npages = md->num_pages;
151
152 if (efi_enabled(EFI_DBG)) {
153 char buf[64];
154
155 pr_info(" 0x%012llx-0x%012llx %s",
156 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
157 efi_md_typeattr_format(buf, sizeof(buf), md));
158 }
159
160 memrange_efi_to_native(&paddr, &npages);
161 size = npages << PAGE_SHIFT;
162
163 if (is_normal_ram(md))
164 early_init_dt_add_memory_arch(paddr, size);
165
166 if (is_reserve_region(md)) {
167 memblock_mark_nomap(paddr, size);
168 if (efi_enabled(EFI_DBG))
169 pr_cont("*");
170 }
171
172 if (efi_enabled(EFI_DBG))
173 pr_cont("\n");
174 }
175
176 set_bit(EFI_MEMMAP, &efi.flags);
177}
178
179void __init efi_init(void)
180{
181 struct efi_fdt_params params;
182
183 /* Grab UEFI information placed in FDT by stub */
184 if (!efi_get_fdt_params(&params))
185 return;
186
187 efi_system_table = params.system_table;
188
189 memmap.phys_map = params.mmap;
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +0000190 memmap.map = early_memremap_ro(params.mmap, params.mmap_size);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100191 if (memmap.map == NULL) {
192 /*
193 * If we are booting via UEFI, the UEFI memory map is the only
194 * description of memory we have, so there is little point in
195 * proceeding if we cannot access it.
196 */
197 panic("Unable to map EFI memory map.\n");
198 }
199 memmap.map_end = memmap.map + params.mmap_size;
200 memmap.desc_size = params.desc_size;
201 memmap.desc_version = params.desc_ver;
202
203 if (uefi_init() < 0)
204 return;
205
206 reserve_regions();
207 early_memunmap(memmap.map, params.mmap_size);
Ard Biesheuvel7cc8cbc2016-03-30 09:46:23 +0200208
209 if (IS_ENABLED(CONFIG_ARM)) {
210 /*
211 * ARM currently does not allow ioremap_cache() to be called on
212 * memory regions that are covered by struct page. So remove the
213 * UEFI memory map from the linear mapping.
214 */
215 memblock_mark_nomap(params.mmap & PAGE_MASK,
216 PAGE_ALIGN(params.mmap_size +
217 (params.mmap & ~PAGE_MASK)));
218 } else {
219 memblock_reserve(params.mmap & PAGE_MASK,
220 PAGE_ALIGN(params.mmap_size +
221 (params.mmap & ~PAGE_MASK)));
222 }
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100223}