1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) "efi: " fmt
11
12#include <linux/efi.h>
13#include <linux/fwnode.h>
14#include <linux/init.h>
15#include <linux/memblock.h>
16#include <linux/mm_types.h>
17#include <linux/of.h>
18#include <linux/of_address.h>
19#include <linux/of_fdt.h>
20#include <linux/platform_device.h>
21#include <linux/screen_info.h>
22
23#include <asm/efi.h>
24
25static int __init is_memory(efi_memory_desc_t *md)
26{
27 if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
28 return 1;
29 return 0;
30}
31
32
33
34
35
36
37static phys_addr_t __init efi_to_phys(unsigned long addr)
38{
39 efi_memory_desc_t *md;
40
41 for_each_efi_memory_desc(md) {
42 if (!(md->attribute & EFI_MEMORY_RUNTIME))
43 continue;
44 if (md->virt_addr == 0)
45
46 break;
47 if (md->virt_addr <= addr &&
48 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
49 return md->phys_addr + addr - md->virt_addr;
50 }
51 return addr;
52}
53
54static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
55static __initdata unsigned long cpu_state_table = EFI_INVALID_TABLE_ADDR;
56
57static const efi_config_table_type_t arch_tables[] __initconst = {
58 {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, &screen_info_table},
59 {LINUX_EFI_ARM_CPU_STATE_TABLE_GUID, &cpu_state_table},
60 {}
61};
62
63static void __init init_screen_info(void)
64{
65 struct screen_info *si;
66
67 if (IS_ENABLED(CONFIG_ARM) &&
68 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
78 screen_info.orig_video_cols = 80;
79 screen_info.orig_video_lines = 25;
80 }
81
82 if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
83 memblock_is_map_memory(screen_info.lfb_base))
84 memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
85}
86
87static int __init uefi_init(u64 efi_system_table)
88{
89 efi_config_table_t *config_tables;
90 efi_system_table_t *systab;
91 size_t table_size;
92 int retval;
93
94 systab = early_memremap_ro(efi_system_table, sizeof(efi_system_table_t));
95 if (systab == NULL) {
96 pr_warn("Unable to map EFI system table.\n");
97 return -ENOMEM;
98 }
99
100 set_bit(EFI_BOOT, &efi.flags);
101 if (IS_ENABLED(CONFIG_64BIT))
102 set_bit(EFI_64BIT, &efi.flags);
103
104 retval = efi_systab_check_header(&systab->hdr, 2);
105 if (retval)
106 goto out;
107
108 efi.runtime = systab->runtime;
109 efi.runtime_version = systab->hdr.revision;
110
111 efi_systab_report_header(&systab->hdr, efi_to_phys(systab->fw_vendor));
112
113 table_size = sizeof(efi_config_table_t) * systab->nr_tables;
114 config_tables = early_memremap_ro(efi_to_phys(systab->tables),
115 table_size);
116 if (config_tables == NULL) {
117 pr_warn("Unable to map EFI config table array.\n");
118 retval = -ENOMEM;
119 goto out;
120 }
121 retval = efi_config_parse_tables(config_tables, systab->nr_tables,
122 IS_ENABLED(CONFIG_ARM) ? arch_tables
123 : NULL);
124
125 early_memunmap(config_tables, table_size);
126out:
127 early_memunmap(systab, sizeof(efi_system_table_t));
128 return retval;
129}
130
131
132
133
134static __init int is_usable_memory(efi_memory_desc_t *md)
135{
136 switch (md->type) {
137 case EFI_LOADER_CODE:
138 case EFI_LOADER_DATA:
139 case EFI_ACPI_RECLAIM_MEMORY:
140 case EFI_BOOT_SERVICES_CODE:
141 case EFI_BOOT_SERVICES_DATA:
142 case EFI_CONVENTIONAL_MEMORY:
143 case EFI_PERSISTENT_MEMORY:
144
145
146
147
148
149 if (efi_soft_reserve_enabled() &&
150 (md->attribute & EFI_MEMORY_SP))
151 return false;
152
153
154
155
156
157
158 return (md->attribute & EFI_MEMORY_WB);
159 default:
160 break;
161 }
162 return false;
163}
164
165static __init void reserve_regions(void)
166{
167 efi_memory_desc_t *md;
168 u64 paddr, npages, size;
169
170 if (efi_enabled(EFI_DBG))
171 pr_info("Processing EFI memory map:\n");
172
173
174
175
176
177
178 memblock_dump_all();
179 memblock_remove(0, PHYS_ADDR_MAX);
180
181 for_each_efi_memory_desc(md) {
182 paddr = md->phys_addr;
183 npages = md->num_pages;
184
185 if (efi_enabled(EFI_DBG)) {
186 char buf[64];
187
188 pr_info(" 0x%012llx-0x%012llx %s\n",
189 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
190 efi_md_typeattr_format(buf, sizeof(buf), md));
191 }
192
193 memrange_efi_to_native(&paddr, &npages);
194 size = npages << PAGE_SHIFT;
195
196 if (is_memory(md)) {
197 early_init_dt_add_memory_arch(paddr, size);
198
199 if (!is_usable_memory(md))
200 memblock_mark_nomap(paddr, size);
201
202
203 if (md->type == EFI_ACPI_RECLAIM_MEMORY)
204 memblock_reserve(paddr, size);
205 }
206 }
207}
208
209void __init efi_init(void)
210{
211 struct efi_memory_map_data data;
212 u64 efi_system_table;
213
214
215 efi_system_table = efi_get_fdt_params(&data);
216 if (!efi_system_table)
217 return;
218
219 if (efi_memmap_init_early(&data) < 0) {
220
221
222
223
224
225 panic("Unable to map EFI memory map.\n");
226 }
227
228 WARN(efi.memmap.desc_version != 1,
229 "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
230 efi.memmap.desc_version);
231
232 if (uefi_init(efi_system_table) < 0) {
233 efi_memmap_unmap();
234 return;
235 }
236
237 reserve_regions();
238 efi_esrt_init();
239
240 memblock_reserve(data.phys_map & PAGE_MASK,
241 PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
242
243 init_screen_info();
244
245#ifdef CONFIG_ARM
246
247 efi_memmap_unmap();
248
249 if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
250 struct efi_arm_entry_state *state;
251 bool dump_state = true;
252
253 state = early_memremap_ro(cpu_state_table,
254 sizeof(struct efi_arm_entry_state));
255 if (state == NULL) {
256 pr_warn("Unable to map CPU entry state table.\n");
257 return;
258 }
259
260 if ((state->sctlr_before_ebs & 1) == 0)
261 pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
262 else if ((state->sctlr_after_ebs & 1) == 0)
263 pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
264 else
265 dump_state = false;
266
267 if (dump_state || efi_enabled(EFI_DBG)) {
268 pr_info("CPSR at EFI stub entry : 0x%08x\n", state->cpsr_before_ebs);
269 pr_info("SCTLR at EFI stub entry : 0x%08x\n", state->sctlr_before_ebs);
270 pr_info("CPSR after ExitBootServices() : 0x%08x\n", state->cpsr_after_ebs);
271 pr_info("SCTLR after ExitBootServices(): 0x%08x\n", state->sctlr_after_ebs);
272 }
273 early_memunmap(state, sizeof(struct efi_arm_entry_state));
274 }
275#endif
276}
277
278static bool efifb_overlaps_pci_range(const struct of_pci_range *range)
279{
280 u64 fb_base = screen_info.lfb_base;
281
282 if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
283 fb_base |= (u64)(unsigned long)screen_info.ext_lfb_base << 32;
284
285 return fb_base >= range->cpu_addr &&
286 fb_base < (range->cpu_addr + range->size);
287}
288
289static struct device_node *find_pci_overlap_node(void)
290{
291 struct device_node *np;
292
293 for_each_node_by_type(np, "pci") {
294 struct of_pci_range_parser parser;
295 struct of_pci_range range;
296 int err;
297
298 err = of_pci_range_parser_init(&parser, np);
299 if (err) {
300 pr_warn("of_pci_range_parser_init() failed: %d\n", err);
301 continue;
302 }
303
304 for_each_of_pci_range(&parser, &range)
305 if (efifb_overlaps_pci_range(&range))
306 return np;
307 }
308 return NULL;
309}
310
311
312
313
314
315
316
317
318static int efifb_add_links(const struct fwnode_handle *fwnode,
319 struct device *dev)
320{
321 struct device_node *sup_np;
322 struct device *sup_dev;
323
324 sup_np = find_pci_overlap_node();
325
326
327
328
329
330 if (!sup_np)
331 return 0;
332
333 sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
334 of_node_put(sup_np);
335
336
337
338
339
340
341
342 if (!sup_dev)
343 return -ENODEV;
344
345
346
347
348
349 if (!device_link_add(dev, sup_dev, fw_devlink_get_flags()))
350 dev_warn(dev, "device_link_add() failed\n");
351
352 put_device(sup_dev);
353
354 return 0;
355}
356
357static const struct fwnode_operations efifb_fwnode_ops = {
358 .add_links = efifb_add_links,
359};
360
361static struct fwnode_handle efifb_fwnode = {
362 .ops = &efifb_fwnode_ops,
363};
364
365static int __init register_gop_device(void)
366{
367 struct platform_device *pd;
368 int err;
369
370 if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
371 return 0;
372
373 pd = platform_device_alloc("efi-framebuffer", 0);
374 if (!pd)
375 return -ENOMEM;
376
377 if (IS_ENABLED(CONFIG_PCI))
378 pd->dev.fwnode = &efifb_fwnode;
379
380 err = platform_device_add_data(pd, &screen_info, sizeof(screen_info));
381 if (err)
382 return err;
383
384 return platform_device_add(pd);
385}
386subsys_initcall(register_gop_device);
387