blob: ac95dd8b119fc2d2867ed26d277c06c7dd0071cb [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
Ard Biesheuvel801820b2016-04-25 21:06:53 +010014#define pr_fmt(fmt) "efi: " fmt
15
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010016#include <linux/efi.h>
17#include <linux/init.h>
18#include <linux/memblock.h>
19#include <linux/mm_types.h>
20#include <linux/of.h>
21#include <linux/of_fdt.h>
Ard Biesheuvel801820b2016-04-25 21:06:53 +010022#include <linux/screen_info.h>
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010023
24#include <asm/efi.h>
25
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010026u64 efi_system_table;
27
28static int __init is_normal_ram(efi_memory_desc_t *md)
29{
30 if (md->attribute & EFI_MEMORY_WB)
31 return 1;
32 return 0;
33}
34
35/*
36 * Translate a EFI virtual address into a physical address: this is necessary,
37 * as some data members of the EFI system table are virtually remapped after
38 * SetVirtualAddressMap() has been called.
39 */
40static phys_addr_t efi_to_phys(unsigned long addr)
41{
42 efi_memory_desc_t *md;
43
Matt Fleming884f4f62016-04-25 21:06:39 +010044 for_each_efi_memory_desc(md) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010045 if (!(md->attribute & EFI_MEMORY_RUNTIME))
46 continue;
47 if (md->virt_addr == 0)
48 /* no virtual mapping has been installed by the stub */
49 break;
50 if (md->virt_addr <= addr &&
51 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
52 return md->phys_addr + addr - md->virt_addr;
53 }
54 return addr;
55}
56
Ard Biesheuvel801820b2016-04-25 21:06:53 +010057static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
58
59static __initdata efi_config_table_type_t arch_tables[] = {
60 {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, NULL, &screen_info_table},
61 {NULL_GUID, NULL, NULL}
62};
63
64static void __init init_screen_info(void)
65{
66 struct screen_info *si;
67
68 if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
69 si = early_memremap_ro(screen_info_table, sizeof(*si));
70 if (!si) {
71 pr_err("Could not map screen_info config table\n");
72 return;
73 }
74 screen_info = *si;
75 early_memunmap(si, sizeof(*si));
76
77 /* dummycon on ARM needs non-zero values for columns/lines */
78 screen_info.orig_video_cols = 80;
79 screen_info.orig_video_lines = 25;
80 }
81}
82
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010083static int __init uefi_init(void)
84{
85 efi_char16_t *c16;
86 void *config_tables;
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010087 size_t table_size;
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010088 char vendor[100] = "unknown";
89 int i, retval;
90
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +000091 efi.systab = early_memremap_ro(efi_system_table,
92 sizeof(efi_system_table_t));
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +010093 if (efi.systab == NULL) {
94 pr_warn("Unable to map EFI system table.\n");
95 return -ENOMEM;
96 }
97
98 set_bit(EFI_BOOT, &efi.flags);
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010099 if (IS_ENABLED(CONFIG_64BIT))
100 set_bit(EFI_64BIT, &efi.flags);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100101
102 /*
103 * Verify the EFI Table
104 */
105 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
106 pr_err("System table signature incorrect\n");
107 retval = -EINVAL;
108 goto out;
109 }
110 if ((efi.systab->hdr.revision >> 16) < 2)
111 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
112 efi.systab->hdr.revision >> 16,
113 efi.systab->hdr.revision & 0xffff);
114
Ard Biesheuvel14c43be2016-04-25 21:06:34 +0100115 efi.runtime_version = efi.systab->hdr.revision;
116
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100117 /* Show what we know for posterity */
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +0000118 c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
119 sizeof(vendor) * sizeof(efi_char16_t));
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100120 if (c16) {
121 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
122 vendor[i] = c16[i];
123 vendor[i] = '\0';
124 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
125 }
126
127 pr_info("EFI v%u.%.02u by %s\n",
128 efi.systab->hdr.revision >> 16,
129 efi.systab->hdr.revision & 0xffff, vendor);
130
131 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
Ard Biesheuvel2eec5de2016-02-17 12:36:00 +0000132 config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
133 table_size);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100134 if (config_tables == NULL) {
135 pr_warn("Unable to map EFI config table array.\n");
136 retval = -ENOMEM;
137 goto out;
138 }
139 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
Ard Biesheuvel801820b2016-04-25 21:06:53 +0100140 sizeof(efi_config_table_t),
141 arch_tables);
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100142
143 early_memunmap(config_tables, table_size);
144out:
145 early_memunmap(efi.systab, sizeof(efi_system_table_t));
146 return retval;
147}
148
149/*
150 * Return true for RAM regions we want to permanently reserve.
151 */
152static __init int is_reserve_region(efi_memory_desc_t *md)
153{
154 switch (md->type) {
155 case EFI_LOADER_CODE:
156 case EFI_LOADER_DATA:
157 case EFI_BOOT_SERVICES_CODE:
158 case EFI_BOOT_SERVICES_DATA:
159 case EFI_CONVENTIONAL_MEMORY:
160 case EFI_PERSISTENT_MEMORY:
161 return 0;
162 default:
163 break;
164 }
165 return is_normal_ram(md);
166}
167
168static __init void reserve_regions(void)
169{
170 efi_memory_desc_t *md;
171 u64 paddr, npages, size;
172
173 if (efi_enabled(EFI_DBG))
174 pr_info("Processing EFI memory map:\n");
175
Matt Fleming884f4f62016-04-25 21:06:39 +0100176 for_each_efi_memory_desc(md) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100177 paddr = md->phys_addr;
178 npages = md->num_pages;
179
180 if (efi_enabled(EFI_DBG)) {
181 char buf[64];
182
183 pr_info(" 0x%012llx-0x%012llx %s",
184 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
185 efi_md_typeattr_format(buf, sizeof(buf), md));
186 }
187
188 memrange_efi_to_native(&paddr, &npages);
189 size = npages << PAGE_SHIFT;
190
191 if (is_normal_ram(md))
192 early_init_dt_add_memory_arch(paddr, size);
193
194 if (is_reserve_region(md)) {
195 memblock_mark_nomap(paddr, size);
196 if (efi_enabled(EFI_DBG))
197 pr_cont("*");
198 }
199
200 if (efi_enabled(EFI_DBG))
201 pr_cont("\n");
202 }
203
204 set_bit(EFI_MEMMAP, &efi.flags);
205}
206
207void __init efi_init(void)
208{
209 struct efi_fdt_params params;
210
211 /* Grab UEFI information placed in FDT by stub */
212 if (!efi_get_fdt_params(&params))
213 return;
214
215 efi_system_table = params.system_table;
216
Matt Fleming884f4f62016-04-25 21:06:39 +0100217 efi.memmap.phys_map = params.mmap;
218 efi.memmap.map = early_memremap_ro(params.mmap, params.mmap_size);
219 if (efi.memmap.map == NULL) {
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100220 /*
221 * If we are booting via UEFI, the UEFI memory map is the only
222 * description of memory we have, so there is little point in
223 * proceeding if we cannot access it.
224 */
225 panic("Unable to map EFI memory map.\n");
226 }
Matt Fleming884f4f62016-04-25 21:06:39 +0100227 efi.memmap.map_end = efi.memmap.map + params.mmap_size;
228 efi.memmap.desc_size = params.desc_size;
229 efi.memmap.desc_version = params.desc_ver;
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100230
Ard Biesheuvel0d054ad2016-04-25 21:06:40 +0100231 WARN(efi.memmap.desc_version != 1,
232 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
233 efi.memmap.desc_version);
234
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100235 if (uefi_init() < 0)
236 return;
237
238 reserve_regions();
Ard Biesheuvel789957e2016-04-25 21:06:46 +0100239 efi_memattr_init();
Matt Fleming884f4f62016-04-25 21:06:39 +0100240 early_memunmap(efi.memmap.map, params.mmap_size);
Ard Biesheuvel7cc8cbc2016-03-30 09:46:23 +0200241
242 if (IS_ENABLED(CONFIG_ARM)) {
243 /*
244 * ARM currently does not allow ioremap_cache() to be called on
245 * memory regions that are covered by struct page. So remove the
246 * UEFI memory map from the linear mapping.
247 */
248 memblock_mark_nomap(params.mmap & PAGE_MASK,
249 PAGE_ALIGN(params.mmap_size +
250 (params.mmap & ~PAGE_MASK)));
251 } else {
252 memblock_reserve(params.mmap & PAGE_MASK,
253 PAGE_ALIGN(params.mmap_size +
254 (params.mmap & ~PAGE_MASK)));
255 }
Ard Biesheuvel801820b2016-04-25 21:06:53 +0100256
257 init_screen_info();
Ard Biesheuvele5bc22a2015-11-30 13:28:18 +0100258}