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