GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / firmware / efi / efi-init.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Extensible Firmware Interface
4  *
5  * Based on Extensible Firmware Interface Specification version 2.4
6  *
7  * Copyright (C) 2013 - 2015 Linaro Ltd.
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
25 static 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  * Translate a EFI virtual address into a physical address: this is necessary,
34  * as some data members of the EFI system table are virtually remapped after
35  * SetVirtualAddressMap() has been called.
36  */
37 static 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                         /* no virtual mapping has been installed by the stub */
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
54 static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
55 static __initdata unsigned long cpu_state_table = EFI_INVALID_TABLE_ADDR;
56
57 static 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
63 static 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                 /* 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         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
87 static 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);
126 out:
127         early_memunmap(systab, sizeof(efi_system_table_t));
128         return retval;
129 }
130
131 /*
132  * Return true for regions that can be used as System RAM.
133  */
134 static __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                  * According to the spec, these regions are no longer reserved
146                  * after calling ExitBootServices(). However, we can only use
147                  * them as System RAM if they can be mapped writeback cacheable.
148                  */
149                 return (md->attribute & EFI_MEMORY_WB);
150         default:
151                 break;
152         }
153         return false;
154 }
155
156 static __init void reserve_regions(void)
157 {
158         efi_memory_desc_t *md;
159         u64 paddr, npages, size;
160
161         if (efi_enabled(EFI_DBG))
162                 pr_info("Processing EFI memory map:\n");
163
164         /*
165          * Discard memblocks discovered so far: if there are any at this
166          * point, they originate from memory nodes in the DT, and UEFI
167          * uses its own memory map instead.
168          */
169         memblock_dump_all();
170         memblock_remove(0, PHYS_ADDR_MAX);
171
172         for_each_efi_memory_desc(md) {
173                 paddr = md->phys_addr;
174                 npages = md->num_pages;
175
176                 if (efi_enabled(EFI_DBG)) {
177                         char buf[64];
178
179                         pr_info("  0x%012llx-0x%012llx %s\n",
180                                 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
181                                 efi_md_typeattr_format(buf, sizeof(buf), md));
182                 }
183
184                 memrange_efi_to_native(&paddr, &npages);
185                 size = npages << PAGE_SHIFT;
186
187                 if (is_memory(md)) {
188                         /*
189                          * Special purpose memory is 'soft reserved', which
190                          * means it is set aside initially. Don't add a memblock
191                          * for it now so that it can be hotplugged back in or
192                          * be assigned to the dax driver after boot.
193                          */
194                         if (efi_soft_reserve_enabled() &&
195                             (md->attribute & EFI_MEMORY_SP))
196                                 continue;
197
198                         early_init_dt_add_memory_arch(paddr, size);
199
200                         if (!is_usable_memory(md))
201                                 memblock_mark_nomap(paddr, size);
202
203                         /* keep ACPI reclaim memory intact for kexec etc. */
204                         if (md->type == EFI_ACPI_RECLAIM_MEMORY)
205                                 memblock_reserve(paddr, size);
206                 }
207         }
208 }
209
210 void __init efi_init(void)
211 {
212         struct efi_memory_map_data data;
213         u64 efi_system_table;
214
215         /* Grab UEFI information placed in FDT by stub */
216         efi_system_table = efi_get_fdt_params(&data);
217         if (!efi_system_table)
218                 return;
219
220         if (efi_memmap_init_early(&data) < 0) {
221                 /*
222                 * If we are booting via UEFI, the UEFI memory map is the only
223                 * description of memory we have, so there is little point in
224                 * proceeding if we cannot access it.
225                 */
226                 panic("Unable to map EFI memory map.\n");
227         }
228
229         WARN(efi.memmap.desc_version != 1,
230              "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
231               efi.memmap.desc_version);
232
233         if (uefi_init(efi_system_table) < 0) {
234                 efi_memmap_unmap();
235                 return;
236         }
237
238         reserve_regions();
239         efi_esrt_init();
240         efi_mokvar_table_init();
241
242         memblock_reserve(data.phys_map & PAGE_MASK,
243                          PAGE_ALIGN(data.size + (data.phys_map & ~PAGE_MASK)));
244
245         init_screen_info();
246
247 #ifdef CONFIG_ARM
248         /* ARM does not permit early mappings to persist across paging_init() */
249         efi_memmap_unmap();
250
251         if (cpu_state_table != EFI_INVALID_TABLE_ADDR) {
252                 struct efi_arm_entry_state *state;
253                 bool dump_state = true;
254
255                 state = early_memremap_ro(cpu_state_table,
256                                           sizeof(struct efi_arm_entry_state));
257                 if (state == NULL) {
258                         pr_warn("Unable to map CPU entry state table.\n");
259                         return;
260                 }
261
262                 if ((state->sctlr_before_ebs & 1) == 0)
263                         pr_warn(FW_BUG "EFI stub was entered with MMU and Dcache disabled, please fix your firmware!\n");
264                 else if ((state->sctlr_after_ebs & 1) == 0)
265                         pr_warn(FW_BUG "ExitBootServices() returned with MMU and Dcache disabled, please fix your firmware!\n");
266                 else
267                         dump_state = false;
268
269                 if (dump_state || efi_enabled(EFI_DBG)) {
270                         pr_info("CPSR at EFI stub entry        : 0x%08x\n", state->cpsr_before_ebs);
271                         pr_info("SCTLR at EFI stub entry       : 0x%08x\n", state->sctlr_before_ebs);
272                         pr_info("CPSR after ExitBootServices() : 0x%08x\n", state->cpsr_after_ebs);
273                         pr_info("SCTLR after ExitBootServices(): 0x%08x\n", state->sctlr_after_ebs);
274                 }
275                 early_memunmap(state, sizeof(struct efi_arm_entry_state));
276         }
277 #endif
278 }
279
280 static bool efifb_overlaps_pci_range(const struct of_pci_range *range)
281 {
282         u64 fb_base = screen_info.lfb_base;
283
284         if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
285                 fb_base |= (u64)(unsigned long)screen_info.ext_lfb_base << 32;
286
287         return fb_base >= range->cpu_addr &&
288                fb_base < (range->cpu_addr + range->size);
289 }
290
291 static struct device_node *find_pci_overlap_node(void)
292 {
293         struct device_node *np;
294
295         for_each_node_by_type(np, "pci") {
296                 struct of_pci_range_parser parser;
297                 struct of_pci_range range;
298                 int err;
299
300                 err = of_pci_range_parser_init(&parser, np);
301                 if (err) {
302                         pr_warn("of_pci_range_parser_init() failed: %d\n", err);
303                         continue;
304                 }
305
306                 for_each_of_pci_range(&parser, &range)
307                         if (efifb_overlaps_pci_range(&range))
308                                 return np;
309         }
310         return NULL;
311 }
312
313 /*
314  * If the efifb framebuffer is backed by a PCI graphics controller, we have
315  * to ensure that this relation is expressed using a device link when
316  * running in DT mode, or the probe order may be reversed, resulting in a
317  * resource reservation conflict on the memory window that the efifb
318  * framebuffer steals from the PCIe host bridge.
319  */
320 static int efifb_add_links(const struct fwnode_handle *fwnode,
321                            struct device *dev)
322 {
323         struct device_node *sup_np;
324         struct device *sup_dev;
325
326         sup_np = find_pci_overlap_node();
327
328         /*
329          * If there's no PCI graphics controller backing the efifb, we are
330          * done here.
331          */
332         if (!sup_np)
333                 return 0;
334
335         sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
336         of_node_put(sup_np);
337
338         /*
339          * Return -ENODEV if the PCI graphics controller device hasn't been
340          * registered yet.  This ensures that efifb isn't allowed to probe
341          * and this function is retried again when new devices are
342          * registered.
343          */
344         if (!sup_dev)
345                 return -ENODEV;
346
347         /*
348          * If this fails, retrying this function at a later point won't
349          * change anything. So, don't return an error after this.
350          */
351         if (!device_link_add(dev, sup_dev, fw_devlink_get_flags()))
352                 dev_warn(dev, "device_link_add() failed\n");
353
354         put_device(sup_dev);
355
356         return 0;
357 }
358
359 static const struct fwnode_operations efifb_fwnode_ops = {
360         .add_links = efifb_add_links,
361 };
362
363 static struct fwnode_handle efifb_fwnode = {
364         .ops = &efifb_fwnode_ops,
365 };
366
367 static int __init register_gop_device(void)
368 {
369         struct platform_device *pd;
370         int err;
371
372         if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
373                 return 0;
374
375         pd = platform_device_alloc("efi-framebuffer", 0);
376         if (!pd)
377                 return -ENOMEM;
378
379         if (IS_ENABLED(CONFIG_PCI))
380                 pd->dev.fwnode = &efifb_fwnode;
381
382         err = platform_device_add_data(pd, &screen_info, sizeof(screen_info));
383         if (err)
384                 return err;
385
386         return platform_device_add(pd);
387 }
388 subsys_initcall(register_gop_device);