objtool: Re-arrange validate_functions()
[linux-block.git] / drivers / firmware / efi / arm-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 u64 efi_system_table;
26
27 static int __init is_memory(efi_memory_desc_t *md)
28 {
29         if (md->attribute & (EFI_MEMORY_WB|EFI_MEMORY_WT|EFI_MEMORY_WC))
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  */
39 static phys_addr_t efi_to_phys(unsigned long addr)
40 {
41         efi_memory_desc_t *md;
42
43         for_each_efi_memory_desc(md) {
44                 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
56 static __initdata unsigned long screen_info_table = EFI_INVALID_TABLE_ADDR;
57
58 static __initdata efi_config_table_type_t arch_tables[] = {
59         {LINUX_EFI_ARM_SCREEN_INFO_TABLE_GUID, NULL, &screen_info_table},
60         {NULL_GUID, NULL, NULL}
61 };
62
63 static void __init init_screen_info(void)
64 {
65         struct screen_info *si;
66
67         if (screen_info_table != EFI_INVALID_TABLE_ADDR) {
68                 si = early_memremap_ro(screen_info_table, sizeof(*si));
69                 if (!si) {
70                         pr_err("Could not map screen_info config table\n");
71                         return;
72                 }
73                 screen_info = *si;
74                 early_memunmap(si, sizeof(*si));
75
76                 /* dummycon on ARM needs non-zero values for columns/lines */
77                 screen_info.orig_video_cols = 80;
78                 screen_info.orig_video_lines = 25;
79         }
80
81         if (screen_info.orig_video_isVGA == VIDEO_TYPE_EFI &&
82             memblock_is_map_memory(screen_info.lfb_base))
83                 memblock_mark_nomap(screen_info.lfb_base, screen_info.lfb_size);
84 }
85
86 static int __init uefi_init(void)
87 {
88         efi_char16_t *c16;
89         void *config_tables;
90         size_t table_size;
91         char vendor[100] = "unknown";
92         int i, retval;
93
94         efi.systab = early_memremap_ro(efi_system_table,
95                                        sizeof(efi_system_table_t));
96         if (efi.systab == NULL) {
97                 pr_warn("Unable to map EFI system table.\n");
98                 return -ENOMEM;
99         }
100
101         set_bit(EFI_BOOT, &efi.flags);
102         if (IS_ENABLED(CONFIG_64BIT))
103                 set_bit(EFI_64BIT, &efi.flags);
104
105         /*
106          * Verify the EFI Table
107          */
108         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
109                 pr_err("System table signature incorrect\n");
110                 retval = -EINVAL;
111                 goto out;
112         }
113         if ((efi.systab->hdr.revision >> 16) < 2)
114                 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
115                         efi.systab->hdr.revision >> 16,
116                         efi.systab->hdr.revision & 0xffff);
117
118         efi.runtime_version = efi.systab->hdr.revision;
119
120         /* Show what we know for posterity */
121         c16 = early_memremap_ro(efi_to_phys(efi.systab->fw_vendor),
122                                 sizeof(vendor) * sizeof(efi_char16_t));
123         if (c16) {
124                 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
125                         vendor[i] = c16[i];
126                 vendor[i] = '\0';
127                 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
128         }
129
130         pr_info("EFI v%u.%.02u by %s\n",
131                 efi.systab->hdr.revision >> 16,
132                 efi.systab->hdr.revision & 0xffff, vendor);
133
134         table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
135         config_tables = early_memremap_ro(efi_to_phys(efi.systab->tables),
136                                           table_size);
137         if (config_tables == NULL) {
138                 pr_warn("Unable to map EFI config table array.\n");
139                 retval = -ENOMEM;
140                 goto out;
141         }
142         retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
143                                          sizeof(efi_config_table_t),
144                                          arch_tables);
145
146         if (!retval)
147                 efi.config_table = (unsigned long)efi.systab->tables;
148
149         early_memunmap(config_tables, table_size);
150 out:
151         early_memunmap(efi.systab,  sizeof(efi_system_table_t));
152         return retval;
153 }
154
155 /*
156  * Return true for regions that can be used as System RAM.
157  */
158 static __init int is_usable_memory(efi_memory_desc_t *md)
159 {
160         switch (md->type) {
161         case EFI_LOADER_CODE:
162         case EFI_LOADER_DATA:
163         case EFI_ACPI_RECLAIM_MEMORY:
164         case EFI_BOOT_SERVICES_CODE:
165         case EFI_BOOT_SERVICES_DATA:
166         case EFI_CONVENTIONAL_MEMORY:
167         case EFI_PERSISTENT_MEMORY:
168                 /*
169                  * Special purpose memory is 'soft reserved', which means it
170                  * is set aside initially, but can be hotplugged back in or
171                  * be assigned to the dax driver after boot.
172                  */
173                 if (efi_soft_reserve_enabled() &&
174                     (md->attribute & EFI_MEMORY_SP))
175                         return false;
176
177                 /*
178                  * According to the spec, these regions are no longer reserved
179                  * after calling ExitBootServices(). However, we can only use
180                  * them as System RAM if they can be mapped writeback cacheable.
181                  */
182                 return (md->attribute & EFI_MEMORY_WB);
183         default:
184                 break;
185         }
186         return false;
187 }
188
189 static __init void reserve_regions(void)
190 {
191         efi_memory_desc_t *md;
192         u64 paddr, npages, size;
193
194         if (efi_enabled(EFI_DBG))
195                 pr_info("Processing EFI memory map:\n");
196
197         /*
198          * Discard memblocks discovered so far: if there are any at this
199          * point, they originate from memory nodes in the DT, and UEFI
200          * uses its own memory map instead.
201          */
202         memblock_dump_all();
203         memblock_remove(0, PHYS_ADDR_MAX);
204
205         for_each_efi_memory_desc(md) {
206                 paddr = md->phys_addr;
207                 npages = md->num_pages;
208
209                 if (efi_enabled(EFI_DBG)) {
210                         char buf[64];
211
212                         pr_info("  0x%012llx-0x%012llx %s\n",
213                                 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
214                                 efi_md_typeattr_format(buf, sizeof(buf), md));
215                 }
216
217                 memrange_efi_to_native(&paddr, &npages);
218                 size = npages << PAGE_SHIFT;
219
220                 if (is_memory(md)) {
221                         early_init_dt_add_memory_arch(paddr, size);
222
223                         if (!is_usable_memory(md))
224                                 memblock_mark_nomap(paddr, size);
225
226                         /* keep ACPI reclaim memory intact for kexec etc. */
227                         if (md->type == EFI_ACPI_RECLAIM_MEMORY)
228                                 memblock_reserve(paddr, size);
229                 }
230         }
231 }
232
233 void __init efi_init(void)
234 {
235         struct efi_memory_map_data data;
236         struct efi_fdt_params params;
237
238         /* Grab UEFI information placed in FDT by stub */
239         if (!efi_get_fdt_params(&params))
240                 return;
241
242         efi_system_table = params.system_table;
243
244         data.desc_version = params.desc_ver;
245         data.desc_size = params.desc_size;
246         data.size = params.mmap_size;
247         data.phys_map = params.mmap;
248
249         if (efi_memmap_init_early(&data) < 0) {
250                 /*
251                 * If we are booting via UEFI, the UEFI memory map is the only
252                 * description of memory we have, so there is little point in
253                 * proceeding if we cannot access it.
254                 */
255                 panic("Unable to map EFI memory map.\n");
256         }
257
258         WARN(efi.memmap.desc_version != 1,
259              "Unexpected EFI_MEMORY_DESCRIPTOR version %ld",
260               efi.memmap.desc_version);
261
262         if (uefi_init() < 0) {
263                 efi_memmap_unmap();
264                 return;
265         }
266
267         reserve_regions();
268         efi_esrt_init();
269
270         memblock_reserve(params.mmap & PAGE_MASK,
271                          PAGE_ALIGN(params.mmap_size +
272                                     (params.mmap & ~PAGE_MASK)));
273
274         init_screen_info();
275
276         /* ARM does not permit early mappings to persist across paging_init() */
277         if (IS_ENABLED(CONFIG_ARM))
278                 efi_memmap_unmap();
279 }
280
281 static bool efifb_overlaps_pci_range(const struct of_pci_range *range)
282 {
283         u64 fb_base = screen_info.lfb_base;
284
285         if (screen_info.capabilities & VIDEO_CAPABILITY_64BIT_BASE)
286                 fb_base |= (u64)(unsigned long)screen_info.ext_lfb_base << 32;
287
288         return fb_base >= range->cpu_addr &&
289                fb_base < (range->cpu_addr + range->size);
290 }
291
292 static struct device_node *find_pci_overlap_node(void)
293 {
294         struct device_node *np;
295
296         for_each_node_by_type(np, "pci") {
297                 struct of_pci_range_parser parser;
298                 struct of_pci_range range;
299                 int err;
300
301                 err = of_pci_range_parser_init(&parser, np);
302                 if (err) {
303                         pr_warn("of_pci_range_parser_init() failed: %d\n", err);
304                         continue;
305                 }
306
307                 for_each_of_pci_range(&parser, &range)
308                         if (efifb_overlaps_pci_range(&range))
309                                 return np;
310         }
311         return NULL;
312 }
313
314 /*
315  * If the efifb framebuffer is backed by a PCI graphics controller, we have
316  * to ensure that this relation is expressed using a device link when
317  * running in DT mode, or the probe order may be reversed, resulting in a
318  * resource reservation conflict on the memory window that the efifb
319  * framebuffer steals from the PCIe host bridge.
320  */
321 static int efifb_add_links(const struct fwnode_handle *fwnode,
322                            struct device *dev)
323 {
324         struct device_node *sup_np;
325         struct device *sup_dev;
326
327         sup_np = find_pci_overlap_node();
328
329         /*
330          * If there's no PCI graphics controller backing the efifb, we are
331          * done here.
332          */
333         if (!sup_np)
334                 return 0;
335
336         sup_dev = get_dev_from_fwnode(&sup_np->fwnode);
337         of_node_put(sup_np);
338
339         /*
340          * Return -ENODEV if the PCI graphics controller device hasn't been
341          * registered yet.  This ensures that efifb isn't allowed to probe
342          * and this function is retried again when new devices are
343          * registered.
344          */
345         if (!sup_dev)
346                 return -ENODEV;
347
348         /*
349          * If this fails, retrying this function at a later point won't
350          * change anything. So, don't return an error after this.
351          */
352         if (!device_link_add(dev, sup_dev, 0))
353                 dev_warn(dev, "device_link_add() failed\n");
354
355         put_device(sup_dev);
356
357         return 0;
358 }
359
360 static const struct fwnode_operations efifb_fwnode_ops = {
361         .add_links = efifb_add_links,
362 };
363
364 static struct fwnode_handle efifb_fwnode = {
365         .ops = &efifb_fwnode_ops,
366 };
367
368 static int __init register_gop_device(void)
369 {
370         struct platform_device *pd;
371         int err;
372
373         if (screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
374                 return 0;
375
376         pd = platform_device_alloc("efi-framebuffer", 0);
377         if (!pd)
378                 return -ENOMEM;
379
380         if (IS_ENABLED(CONFIG_PCI))
381                 pd->dev.fwnode = &efifb_fwnode;
382
383         err = platform_device_add_data(pd, &screen_info, sizeof(screen_info));
384         if (err)
385                 return err;
386
387         return platform_device_add(pd);
388 }
389 subsys_initcall(register_gop_device);