UPSTREAM: arm64/efi: split off EFI init and runtime code for reuse by 32-bit ARM
[firefly-linux-kernel-4.4.55.git] / drivers / firmware / efi / arm-init.c
1 /*
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
14 #include <linux/efi.h>
15 #include <linux/init.h>
16 #include <linux/memblock.h>
17 #include <linux/mm_types.h>
18 #include <linux/of.h>
19 #include <linux/of_fdt.h>
20
21 #include <asm/efi.h>
22
23 struct efi_memory_map memmap;
24
25 u64 efi_system_table;
26
27 static int __init is_normal_ram(efi_memory_desc_t *md)
28 {
29         if (md->attribute & EFI_MEMORY_WB)
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(&memmap, 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 int __init uefi_init(void)
57 {
58         efi_char16_t *c16;
59         void *config_tables;
60         u64 table_size;
61         char vendor[100] = "unknown";
62         int i, retval;
63
64         efi.systab = early_memremap(efi_system_table,
65                                     sizeof(efi_system_table_t));
66         if (efi.systab == NULL) {
67                 pr_warn("Unable to map EFI system table.\n");
68                 return -ENOMEM;
69         }
70
71         set_bit(EFI_BOOT, &efi.flags);
72         set_bit(EFI_64BIT, &efi.flags);
73
74         /*
75          * Verify the EFI Table
76          */
77         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
78                 pr_err("System table signature incorrect\n");
79                 retval = -EINVAL;
80                 goto out;
81         }
82         if ((efi.systab->hdr.revision >> 16) < 2)
83                 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
84                         efi.systab->hdr.revision >> 16,
85                         efi.systab->hdr.revision & 0xffff);
86
87         /* Show what we know for posterity */
88         c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
89                              sizeof(vendor) * sizeof(efi_char16_t));
90         if (c16) {
91                 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
92                         vendor[i] = c16[i];
93                 vendor[i] = '\0';
94                 early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
95         }
96
97         pr_info("EFI v%u.%.02u by %s\n",
98                 efi.systab->hdr.revision >> 16,
99                 efi.systab->hdr.revision & 0xffff, vendor);
100
101         table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
102         config_tables = early_memremap(efi_to_phys(efi.systab->tables),
103                                        table_size);
104         if (config_tables == NULL) {
105                 pr_warn("Unable to map EFI config table array.\n");
106                 retval = -ENOMEM;
107                 goto out;
108         }
109         retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
110                                          sizeof(efi_config_table_64_t), NULL);
111
112         early_memunmap(config_tables, table_size);
113 out:
114         early_memunmap(efi.systab,  sizeof(efi_system_table_t));
115         return retval;
116 }
117
118 /*
119  * Return true for RAM regions we want to permanently reserve.
120  */
121 static __init int is_reserve_region(efi_memory_desc_t *md)
122 {
123         switch (md->type) {
124         case EFI_LOADER_CODE:
125         case EFI_LOADER_DATA:
126         case EFI_BOOT_SERVICES_CODE:
127         case EFI_BOOT_SERVICES_DATA:
128         case EFI_CONVENTIONAL_MEMORY:
129         case EFI_PERSISTENT_MEMORY:
130                 return 0;
131         default:
132                 break;
133         }
134         return is_normal_ram(md);
135 }
136
137 static __init void reserve_regions(void)
138 {
139         efi_memory_desc_t *md;
140         u64 paddr, npages, size;
141
142         if (efi_enabled(EFI_DBG))
143                 pr_info("Processing EFI memory map:\n");
144
145         for_each_efi_memory_desc(&memmap, md) {
146                 paddr = md->phys_addr;
147                 npages = md->num_pages;
148
149                 if (efi_enabled(EFI_DBG)) {
150                         char buf[64];
151
152                         pr_info("  0x%012llx-0x%012llx %s",
153                                 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
154                                 efi_md_typeattr_format(buf, sizeof(buf), md));
155                 }
156
157                 memrange_efi_to_native(&paddr, &npages);
158                 size = npages << PAGE_SHIFT;
159
160                 if (is_normal_ram(md))
161                         early_init_dt_add_memory_arch(paddr, size);
162
163                 if (is_reserve_region(md)) {
164                         memblock_mark_nomap(paddr, size);
165                         if (efi_enabled(EFI_DBG))
166                                 pr_cont("*");
167                 }
168
169                 if (efi_enabled(EFI_DBG))
170                         pr_cont("\n");
171         }
172
173         set_bit(EFI_MEMMAP, &efi.flags);
174 }
175
176 void __init efi_init(void)
177 {
178         struct efi_fdt_params params;
179
180         /* Grab UEFI information placed in FDT by stub */
181         if (!efi_get_fdt_params(&params))
182                 return;
183
184         efi_system_table = params.system_table;
185
186         memmap.phys_map = params.mmap;
187         memmap.map = early_memremap(params.mmap, params.mmap_size);
188         if (memmap.map == NULL) {
189                 /*
190                 * If we are booting via UEFI, the UEFI memory map is the only
191                 * description of memory we have, so there is little point in
192                 * proceeding if we cannot access it.
193                 */
194                 panic("Unable to map EFI memory map.\n");
195         }
196         memmap.map_end = memmap.map + params.mmap_size;
197         memmap.desc_size = params.desc_size;
198         memmap.desc_version = params.desc_ver;
199
200         if (uefi_init() < 0)
201                 return;
202
203         reserve_regions();
204         early_memunmap(memmap.map, params.mmap_size);
205         memblock_mark_nomap(params.mmap & PAGE_MASK,
206                             PAGE_ALIGN(params.mmap_size +
207                                        (params.mmap & ~PAGE_MASK)));
208 }