Commit | Line | Data |
---|---|---|
5b83683f HY |
1 | /* |
2 | * Common EFI (Extensible Firmware Interface) support functions | |
3 | * Based on Extensible Firmware Interface Specification version 1.0 | |
4 | * | |
5 | * Copyright (C) 1999 VA Linux Systems | |
6 | * Copyright (C) 1999 Walt Drummond <drummond@valinux.com> | |
7 | * Copyright (C) 1999-2002 Hewlett-Packard Co. | |
8 | * David Mosberger-Tang <davidm@hpl.hp.com> | |
9 | * Stephane Eranian <eranian@hpl.hp.com> | |
10 | * Copyright (C) 2005-2008 Intel Co. | |
11 | * Fenghua Yu <fenghua.yu@intel.com> | |
12 | * Bibo Mao <bibo.mao@intel.com> | |
13 | * Chandramouli Narayanan <mouli@linux.intel.com> | |
14 | * Huang Ying <ying.huang@intel.com> | |
d2f7cbe7 BP |
15 | * Copyright (C) 2013 SuSE Labs |
16 | * Borislav Petkov <bp@suse.de> - runtime services VA mapping | |
5b83683f HY |
17 | * |
18 | * Copied from efi_32.c to eliminate the duplicated code between EFI | |
19 | * 32/64 support code. --ying 2007-10-26 | |
20 | * | |
21 | * All EFI Runtime Services are not implemented yet as EFI only | |
22 | * supports physical mode addressing on SoftSDV. This is to be fixed | |
23 | * in a future version. --drummond 1999-07-20 | |
24 | * | |
25 | * Implemented EFI runtime services and virtual mode calls. --davidm | |
26 | * | |
27 | * Goutham Rao: <goutham.rao@intel.com> | |
28 | * Skip non-WB memory and ignore empty memory ranges. | |
29 | */ | |
30 | ||
e3cb3f5a OJ |
31 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
32 | ||
5b83683f HY |
33 | #include <linux/kernel.h> |
34 | #include <linux/init.h> | |
35 | #include <linux/efi.h> | |
2223af38 | 36 | #include <linux/efi-bgrt.h> |
69c60c88 | 37 | #include <linux/export.h> |
5b83683f | 38 | #include <linux/bootmem.h> |
0d01ff25 | 39 | #include <linux/slab.h> |
a9ce6bc1 | 40 | #include <linux/memblock.h> |
5b83683f HY |
41 | #include <linux/spinlock.h> |
42 | #include <linux/uaccess.h> | |
43 | #include <linux/time.h> | |
44 | #include <linux/io.h> | |
45 | #include <linux/reboot.h> | |
46 | #include <linux/bcd.h> | |
47 | ||
48 | #include <asm/setup.h> | |
49 | #include <asm/efi.h> | |
50 | #include <asm/time.h> | |
a2172e25 HY |
51 | #include <asm/cacheflush.h> |
52 | #include <asm/tlbflush.h> | |
7bd867df | 53 | #include <asm/x86_init.h> |
3195ef59 | 54 | #include <asm/rtc.h> |
a5d90c92 | 55 | #include <asm/uv/uv.h> |
5b83683f | 56 | |
f4fccac0 | 57 | #define EFI_DEBUG |
5b83683f | 58 | |
5b83683f HY |
59 | struct efi_memory_map memmap; |
60 | ||
ecaea42e | 61 | static struct efi efi_phys __initdata; |
5b83683f HY |
62 | static efi_system_table_t efi_systab __initdata; |
63 | ||
9b7d2049 | 64 | static efi_config_table_type_t arch_tables[] __initdata = { |
272686bf LL |
65 | #ifdef CONFIG_X86_UV |
66 | {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab}, | |
67 | #endif | |
722da9d2 | 68 | {NULL_GUID, NULL, NULL}, |
272686bf LL |
69 | }; |
70 | ||
1fec0533 | 71 | u64 efi_setup; /* efi setup_data physical address */ |
926172d4 | 72 | |
9b7d2049 | 73 | static bool disable_runtime __initdata = false; |
8b2cb7a8 HY |
74 | static int __init setup_noefi(char *arg) |
75 | { | |
fb834c7a | 76 | disable_runtime = true; |
8b2cb7a8 HY |
77 | return 0; |
78 | } | |
79 | early_param("noefi", setup_noefi); | |
80 | ||
200001eb PJ |
81 | int add_efi_memmap; |
82 | EXPORT_SYMBOL(add_efi_memmap); | |
83 | ||
84 | static int __init setup_add_efi_memmap(char *arg) | |
85 | { | |
86 | add_efi_memmap = 1; | |
87 | return 0; | |
88 | } | |
89 | early_param("add_efi_memmap", setup_add_efi_memmap); | |
90 | ||
5b83683f HY |
91 | static efi_status_t __init phys_efi_set_virtual_address_map( |
92 | unsigned long memory_map_size, | |
93 | unsigned long descriptor_size, | |
94 | u32 descriptor_version, | |
95 | efi_memory_desc_t *virtual_map) | |
96 | { | |
97 | efi_status_t status; | |
98 | ||
99 | efi_call_phys_prelog(); | |
62fa6e69 MF |
100 | status = efi_call_phys(efi_phys.set_virtual_address_map, |
101 | memory_map_size, descriptor_size, | |
102 | descriptor_version, virtual_map); | |
5b83683f HY |
103 | efi_call_phys_epilog(); |
104 | return status; | |
105 | } | |
106 | ||
3565184e | 107 | int efi_set_rtc_mmss(const struct timespec *now) |
5b83683f | 108 | { |
3565184e | 109 | unsigned long nowtime = now->tv_sec; |
9b7d2049 JP |
110 | efi_status_t status; |
111 | efi_time_t eft; | |
112 | efi_time_cap_t cap; | |
3195ef59 | 113 | struct rtc_time tm; |
5b83683f HY |
114 | |
115 | status = efi.get_time(&eft, &cap); | |
116 | if (status != EFI_SUCCESS) { | |
e3cb3f5a | 117 | pr_err("Oops: efitime: can't read time!\n"); |
5b83683f HY |
118 | return -1; |
119 | } | |
120 | ||
3195ef59 PB |
121 | rtc_time_to_tm(nowtime, &tm); |
122 | if (!rtc_valid_tm(&tm)) { | |
123 | eft.year = tm.tm_year + 1900; | |
124 | eft.month = tm.tm_mon + 1; | |
125 | eft.day = tm.tm_mday; | |
126 | eft.minute = tm.tm_min; | |
127 | eft.second = tm.tm_sec; | |
128 | eft.nanosecond = 0; | |
129 | } else { | |
9b7d2049 JP |
130 | pr_err("%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n", |
131 | __func__, nowtime); | |
3195ef59 PB |
132 | return -1; |
133 | } | |
5b83683f HY |
134 | |
135 | status = efi.set_time(&eft); | |
136 | if (status != EFI_SUCCESS) { | |
e3cb3f5a | 137 | pr_err("Oops: efitime: can't write time!\n"); |
5b83683f HY |
138 | return -1; |
139 | } | |
140 | return 0; | |
141 | } | |
142 | ||
3565184e | 143 | void efi_get_time(struct timespec *now) |
5b83683f HY |
144 | { |
145 | efi_status_t status; | |
146 | efi_time_t eft; | |
147 | efi_time_cap_t cap; | |
148 | ||
149 | status = efi.get_time(&eft, &cap); | |
150 | if (status != EFI_SUCCESS) | |
e3cb3f5a | 151 | pr_err("Oops: efitime: can't read time!\n"); |
5b83683f | 152 | |
3565184e DV |
153 | now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour, |
154 | eft.minute, eft.second); | |
155 | now->tv_nsec = 0; | |
5b83683f HY |
156 | } |
157 | ||
69c91893 PJ |
158 | /* |
159 | * Tell the kernel about the EFI memory map. This might include | |
160 | * more than the max 128 entries that can fit in the e820 legacy | |
161 | * (zeropage) memory map. | |
162 | */ | |
163 | ||
200001eb | 164 | static void __init do_add_efi_memmap(void) |
69c91893 PJ |
165 | { |
166 | void *p; | |
167 | ||
168 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
169 | efi_memory_desc_t *md = p; | |
170 | unsigned long long start = md->phys_addr; | |
171 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
172 | int e820_type; | |
173 | ||
e2a71476 CW |
174 | switch (md->type) { |
175 | case EFI_LOADER_CODE: | |
176 | case EFI_LOADER_DATA: | |
177 | case EFI_BOOT_SERVICES_CODE: | |
178 | case EFI_BOOT_SERVICES_DATA: | |
179 | case EFI_CONVENTIONAL_MEMORY: | |
180 | if (md->attribute & EFI_MEMORY_WB) | |
181 | e820_type = E820_RAM; | |
182 | else | |
183 | e820_type = E820_RESERVED; | |
184 | break; | |
185 | case EFI_ACPI_RECLAIM_MEMORY: | |
186 | e820_type = E820_ACPI; | |
187 | break; | |
188 | case EFI_ACPI_MEMORY_NVS: | |
189 | e820_type = E820_NVS; | |
190 | break; | |
191 | case EFI_UNUSABLE_MEMORY: | |
192 | e820_type = E820_UNUSABLE; | |
193 | break; | |
194 | default: | |
195 | /* | |
196 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE | |
197 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO | |
198 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE | |
199 | */ | |
69c91893 | 200 | e820_type = E820_RESERVED; |
e2a71476 CW |
201 | break; |
202 | } | |
d0be6bde | 203 | e820_add_region(start, size, e820_type); |
69c91893 PJ |
204 | } |
205 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); | |
206 | } | |
207 | ||
1adbfa35 | 208 | int __init efi_memblock_x86_reserve_range(void) |
ecacf09f | 209 | { |
15b9c359 | 210 | struct efi_info *e = &boot_params.efi_info; |
ecacf09f HY |
211 | unsigned long pmap; |
212 | ||
05486fa7 | 213 | #ifdef CONFIG_X86_32 |
1adbfa35 | 214 | /* Can't handle data above 4GB at this time */ |
15b9c359 | 215 | if (e->efi_memmap_hi) { |
1adbfa35 OJ |
216 | pr_err("Memory map is above 4GB, disabling EFI.\n"); |
217 | return -EINVAL; | |
218 | } | |
15b9c359 | 219 | pmap = e->efi_memmap; |
05486fa7 | 220 | #else |
15b9c359 | 221 | pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32)); |
ecacf09f | 222 | #endif |
15b9c359 BP |
223 | memmap.phys_map = (void *)pmap; |
224 | memmap.nr_map = e->efi_memmap_size / | |
225 | e->efi_memdesc_size; | |
226 | memmap.desc_size = e->efi_memdesc_size; | |
227 | memmap.desc_version = e->efi_memdesc_version; | |
228 | ||
24aa0788 | 229 | memblock_reserve(pmap, memmap.nr_map * memmap.desc_size); |
1adbfa35 | 230 | |
258f6fd7 LL |
231 | efi.memmap = &memmap; |
232 | ||
1adbfa35 | 233 | return 0; |
ecacf09f HY |
234 | } |
235 | ||
5b83683f HY |
236 | static void __init print_efi_memmap(void) |
237 | { | |
f4fccac0 | 238 | #ifdef EFI_DEBUG |
5b83683f HY |
239 | efi_memory_desc_t *md; |
240 | void *p; | |
241 | int i; | |
242 | ||
243 | for (p = memmap.map, i = 0; | |
244 | p < memmap.map_end; | |
245 | p += memmap.desc_size, i++) { | |
246 | md = p; | |
9b7d2049 | 247 | pr_info("mem%02u: type=%u, attr=0x%llx, range=[0x%016llx-0x%016llx) (%lluMB)\n", |
5b83683f HY |
248 | i, md->type, md->attribute, md->phys_addr, |
249 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), | |
250 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); | |
251 | } | |
5b83683f | 252 | #endif /* EFI_DEBUG */ |
f4fccac0 | 253 | } |
5b83683f | 254 | |
5189c2a7 | 255 | void __init efi_unmap_memmap(void) |
78510792 | 256 | { |
3e909599 | 257 | clear_bit(EFI_MEMMAP, &efi.flags); |
78510792 JT |
258 | if (memmap.map) { |
259 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); | |
260 | memmap.map = NULL; | |
261 | } | |
262 | } | |
263 | ||
140bf275 | 264 | static int __init efi_systab_init(void *phys) |
5b83683f | 265 | { |
83e68189 | 266 | if (efi_enabled(EFI_64BIT)) { |
1adbfa35 | 267 | efi_system_table_64_t *systab64; |
1fec0533 | 268 | struct efi_setup_data *data = NULL; |
1adbfa35 OJ |
269 | u64 tmp = 0; |
270 | ||
1fec0533 DY |
271 | if (efi_setup) { |
272 | data = early_memremap(efi_setup, sizeof(*data)); | |
273 | if (!data) | |
274 | return -ENOMEM; | |
275 | } | |
1adbfa35 OJ |
276 | systab64 = early_ioremap((unsigned long)phys, |
277 | sizeof(*systab64)); | |
278 | if (systab64 == NULL) { | |
279 | pr_err("Couldn't map the system table!\n"); | |
1fec0533 DY |
280 | if (data) |
281 | early_iounmap(data, sizeof(*data)); | |
1adbfa35 OJ |
282 | return -ENOMEM; |
283 | } | |
284 | ||
285 | efi_systab.hdr = systab64->hdr; | |
1fec0533 DY |
286 | efi_systab.fw_vendor = data ? (unsigned long)data->fw_vendor : |
287 | systab64->fw_vendor; | |
288 | tmp |= data ? data->fw_vendor : systab64->fw_vendor; | |
1adbfa35 OJ |
289 | efi_systab.fw_revision = systab64->fw_revision; |
290 | efi_systab.con_in_handle = systab64->con_in_handle; | |
291 | tmp |= systab64->con_in_handle; | |
292 | efi_systab.con_in = systab64->con_in; | |
293 | tmp |= systab64->con_in; | |
294 | efi_systab.con_out_handle = systab64->con_out_handle; | |
295 | tmp |= systab64->con_out_handle; | |
296 | efi_systab.con_out = systab64->con_out; | |
297 | tmp |= systab64->con_out; | |
298 | efi_systab.stderr_handle = systab64->stderr_handle; | |
299 | tmp |= systab64->stderr_handle; | |
300 | efi_systab.stderr = systab64->stderr; | |
301 | tmp |= systab64->stderr; | |
1fec0533 DY |
302 | efi_systab.runtime = data ? |
303 | (void *)(unsigned long)data->runtime : | |
304 | (void *)(unsigned long)systab64->runtime; | |
305 | tmp |= data ? data->runtime : systab64->runtime; | |
1adbfa35 OJ |
306 | efi_systab.boottime = (void *)(unsigned long)systab64->boottime; |
307 | tmp |= systab64->boottime; | |
308 | efi_systab.nr_tables = systab64->nr_tables; | |
1fec0533 DY |
309 | efi_systab.tables = data ? (unsigned long)data->tables : |
310 | systab64->tables; | |
311 | tmp |= data ? data->tables : systab64->tables; | |
1adbfa35 OJ |
312 | |
313 | early_iounmap(systab64, sizeof(*systab64)); | |
1fec0533 DY |
314 | if (data) |
315 | early_iounmap(data, sizeof(*data)); | |
1adbfa35 OJ |
316 | #ifdef CONFIG_X86_32 |
317 | if (tmp >> 32) { | |
318 | pr_err("EFI data located above 4GB, disabling EFI.\n"); | |
319 | return -EINVAL; | |
320 | } | |
321 | #endif | |
322 | } else { | |
323 | efi_system_table_32_t *systab32; | |
324 | ||
325 | systab32 = early_ioremap((unsigned long)phys, | |
326 | sizeof(*systab32)); | |
327 | if (systab32 == NULL) { | |
328 | pr_err("Couldn't map the system table!\n"); | |
329 | return -ENOMEM; | |
330 | } | |
331 | ||
332 | efi_systab.hdr = systab32->hdr; | |
333 | efi_systab.fw_vendor = systab32->fw_vendor; | |
334 | efi_systab.fw_revision = systab32->fw_revision; | |
335 | efi_systab.con_in_handle = systab32->con_in_handle; | |
336 | efi_systab.con_in = systab32->con_in; | |
337 | efi_systab.con_out_handle = systab32->con_out_handle; | |
338 | efi_systab.con_out = systab32->con_out; | |
339 | efi_systab.stderr_handle = systab32->stderr_handle; | |
340 | efi_systab.stderr = systab32->stderr; | |
341 | efi_systab.runtime = (void *)(unsigned long)systab32->runtime; | |
342 | efi_systab.boottime = (void *)(unsigned long)systab32->boottime; | |
343 | efi_systab.nr_tables = systab32->nr_tables; | |
344 | efi_systab.tables = systab32->tables; | |
345 | ||
346 | early_iounmap(systab32, sizeof(*systab32)); | |
140bf275 | 347 | } |
1adbfa35 | 348 | |
5b83683f HY |
349 | efi.systab = &efi_systab; |
350 | ||
351 | /* | |
352 | * Verify the EFI Table | |
353 | */ | |
140bf275 | 354 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
e3cb3f5a | 355 | pr_err("System table signature incorrect!\n"); |
140bf275 OJ |
356 | return -EINVAL; |
357 | } | |
5b83683f | 358 | if ((efi.systab->hdr.revision >> 16) == 0) |
9b7d2049 | 359 | pr_err("Warning: System table version %d.%02d, expected 1.00 or greater!\n", |
5b83683f HY |
360 | efi.systab->hdr.revision >> 16, |
361 | efi.systab->hdr.revision & 0xffff); | |
140bf275 | 362 | |
0f8093a9 MF |
363 | set_bit(EFI_SYSTEM_TABLES, &efi.flags); |
364 | ||
140bf275 | 365 | return 0; |
83e7ee66 | 366 | } |
5b83683f | 367 | |
4f9dbcfc | 368 | static int __init efi_runtime_init32(void) |
83e7ee66 | 369 | { |
4f9dbcfc MF |
370 | efi_runtime_services_32_t *runtime; |
371 | ||
372 | runtime = early_ioremap((unsigned long)efi.systab->runtime, | |
373 | sizeof(efi_runtime_services_32_t)); | |
374 | if (!runtime) { | |
375 | pr_err("Could not map the runtime service table!\n"); | |
376 | return -ENOMEM; | |
377 | } | |
5b83683f HY |
378 | |
379 | /* | |
4f9dbcfc MF |
380 | * We will only need *early* access to the following two |
381 | * EFI runtime services before set_virtual_address_map | |
382 | * is invoked. | |
5b83683f | 383 | */ |
4f9dbcfc MF |
384 | efi_phys.set_virtual_address_map = |
385 | (efi_set_virtual_address_map_t *) | |
386 | (unsigned long)runtime->set_virtual_address_map; | |
4f9dbcfc MF |
387 | early_iounmap(runtime, sizeof(efi_runtime_services_32_t)); |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static int __init efi_runtime_init64(void) | |
393 | { | |
394 | efi_runtime_services_64_t *runtime; | |
395 | ||
beacfaac | 396 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
4f9dbcfc | 397 | sizeof(efi_runtime_services_64_t)); |
140bf275 | 398 | if (!runtime) { |
e3cb3f5a | 399 | pr_err("Could not map the runtime service table!\n"); |
140bf275 OJ |
400 | return -ENOMEM; |
401 | } | |
4f9dbcfc | 402 | |
140bf275 | 403 | /* |
4f9dbcfc MF |
404 | * We will only need *early* access to the following two |
405 | * EFI runtime services before set_virtual_address_map | |
140bf275 OJ |
406 | * is invoked. |
407 | */ | |
140bf275 | 408 | efi_phys.set_virtual_address_map = |
4f9dbcfc MF |
409 | (efi_set_virtual_address_map_t *) |
410 | (unsigned long)runtime->set_virtual_address_map; | |
4f9dbcfc MF |
411 | early_iounmap(runtime, sizeof(efi_runtime_services_64_t)); |
412 | ||
413 | return 0; | |
414 | } | |
415 | ||
416 | static int __init efi_runtime_init(void) | |
417 | { | |
418 | int rv; | |
419 | ||
420 | /* | |
421 | * Check out the runtime services table. We need to map | |
422 | * the runtime services table so that we can grab the physical | |
423 | * address of several of the EFI runtime functions, needed to | |
424 | * set the firmware into virtual mode. | |
425 | */ | |
426 | if (efi_enabled(EFI_64BIT)) | |
427 | rv = efi_runtime_init64(); | |
428 | else | |
429 | rv = efi_runtime_init32(); | |
430 | ||
431 | if (rv) | |
432 | return rv; | |
140bf275 | 433 | |
0f8093a9 MF |
434 | set_bit(EFI_RUNTIME_SERVICES, &efi.flags); |
435 | ||
140bf275 | 436 | return 0; |
83e7ee66 | 437 | } |
5b83683f | 438 | |
140bf275 | 439 | static int __init efi_memmap_init(void) |
83e7ee66 | 440 | { |
5b83683f | 441 | /* Map the EFI memory map */ |
beacfaac HY |
442 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
443 | memmap.nr_map * memmap.desc_size); | |
140bf275 | 444 | if (memmap.map == NULL) { |
e3cb3f5a | 445 | pr_err("Could not map the memory map!\n"); |
140bf275 OJ |
446 | return -ENOMEM; |
447 | } | |
5b83683f | 448 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
175e438f | 449 | |
200001eb PJ |
450 | if (add_efi_memmap) |
451 | do_add_efi_memmap(); | |
140bf275 | 452 | |
0f8093a9 MF |
453 | set_bit(EFI_MEMMAP, &efi.flags); |
454 | ||
140bf275 | 455 | return 0; |
83e7ee66 OJ |
456 | } |
457 | ||
458 | void __init efi_init(void) | |
459 | { | |
460 | efi_char16_t *c16; | |
461 | char vendor[100] = "unknown"; | |
462 | int i = 0; | |
463 | void *tmp; | |
464 | ||
465 | #ifdef CONFIG_X86_32 | |
1adbfa35 OJ |
466 | if (boot_params.efi_info.efi_systab_hi || |
467 | boot_params.efi_info.efi_memmap_hi) { | |
468 | pr_info("Table located above 4GB, disabling EFI.\n"); | |
1adbfa35 OJ |
469 | return; |
470 | } | |
83e7ee66 OJ |
471 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
472 | #else | |
473 | efi_phys.systab = (efi_system_table_t *) | |
1adbfa35 OJ |
474 | (boot_params.efi_info.efi_systab | |
475 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); | |
83e7ee66 OJ |
476 | #endif |
477 | ||
83e68189 | 478 | if (efi_systab_init(efi_phys.systab)) |
140bf275 | 479 | return; |
83e68189 | 480 | |
3e909599 | 481 | set_bit(EFI_SYSTEM_TABLES, &efi.flags); |
83e7ee66 | 482 | |
a0998eb1 DY |
483 | efi.config_table = (unsigned long)efi.systab->tables; |
484 | efi.fw_vendor = (unsigned long)efi.systab->fw_vendor; | |
485 | efi.runtime = (unsigned long)efi.systab->runtime; | |
486 | ||
83e7ee66 OJ |
487 | /* |
488 | * Show what we know for posterity | |
489 | */ | |
490 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); | |
491 | if (c16) { | |
492 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) | |
493 | vendor[i] = *c16++; | |
494 | vendor[i] = '\0'; | |
495 | } else | |
e3cb3f5a | 496 | pr_err("Could not map the firmware vendor!\n"); |
83e7ee66 OJ |
497 | early_iounmap(tmp, 2); |
498 | ||
e3cb3f5a OJ |
499 | pr_info("EFI v%u.%.02u by %s\n", |
500 | efi.systab->hdr.revision >> 16, | |
501 | efi.systab->hdr.revision & 0xffff, vendor); | |
83e7ee66 | 502 | |
1fec0533 DY |
503 | if (efi_reuse_config(efi.systab->tables, efi.systab->nr_tables)) |
504 | return; | |
505 | ||
272686bf | 506 | if (efi_config_init(arch_tables)) |
140bf275 | 507 | return; |
83e68189 | 508 | |
1adbfa35 OJ |
509 | /* |
510 | * Note: We currently don't support runtime services on an EFI | |
511 | * that doesn't match the kernel 32/64-bit mode. | |
512 | */ | |
513 | ||
7d453eee | 514 | if (!efi_runtime_supported()) |
1adbfa35 | 515 | pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n"); |
83e68189 | 516 | else { |
fb834c7a | 517 | if (disable_runtime || efi_runtime_init()) |
83e68189 | 518 | return; |
140bf275 | 519 | } |
83e68189 | 520 | if (efi_memmap_init()) |
140bf275 | 521 | return; |
83e68189 | 522 | |
3e909599 | 523 | set_bit(EFI_MEMMAP, &efi.flags); |
83e68189 | 524 | |
5b83683f | 525 | print_efi_memmap(); |
5b83683f HY |
526 | } |
527 | ||
2223af38 JT |
528 | void __init efi_late_init(void) |
529 | { | |
530 | efi_bgrt_init(); | |
531 | } | |
532 | ||
9cd2b07c MG |
533 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
534 | { | |
535 | u64 addr, npages; | |
536 | ||
537 | addr = md->virt_addr; | |
538 | npages = md->num_pages; | |
539 | ||
540 | memrange_efi_to_native(&addr, &npages); | |
541 | ||
542 | if (executable) | |
543 | set_memory_x(addr, npages); | |
544 | else | |
545 | set_memory_nx(addr, npages); | |
546 | } | |
547 | ||
c55d016f | 548 | void __init runtime_code_page_mkexec(void) |
a2172e25 HY |
549 | { |
550 | efi_memory_desc_t *md; | |
a2172e25 HY |
551 | void *p; |
552 | ||
a2172e25 HY |
553 | /* Make EFI runtime service code area executable */ |
554 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
555 | md = p; | |
1c083eb2 HY |
556 | |
557 | if (md->type != EFI_RUNTIME_SERVICES_CODE) | |
558 | continue; | |
559 | ||
9cd2b07c | 560 | efi_set_executable(md, true); |
a2172e25 | 561 | } |
a2172e25 | 562 | } |
a2172e25 | 563 | |
3e8fa263 MF |
564 | void efi_memory_uc(u64 addr, unsigned long size) |
565 | { | |
566 | unsigned long page_shift = 1UL << EFI_PAGE_SHIFT; | |
567 | u64 npages; | |
568 | ||
569 | npages = round_up(size, page_shift) / page_shift; | |
570 | memrange_efi_to_native(&addr, &npages); | |
571 | set_memory_uc(addr, npages); | |
572 | } | |
573 | ||
d2f7cbe7 BP |
574 | void __init old_map_region(efi_memory_desc_t *md) |
575 | { | |
576 | u64 start_pfn, end_pfn, end; | |
577 | unsigned long size; | |
578 | void *va; | |
579 | ||
580 | start_pfn = PFN_DOWN(md->phys_addr); | |
581 | size = md->num_pages << PAGE_SHIFT; | |
582 | end = md->phys_addr + size; | |
583 | end_pfn = PFN_UP(end); | |
584 | ||
585 | if (pfn_range_is_mapped(start_pfn, end_pfn)) { | |
586 | va = __va(md->phys_addr); | |
587 | ||
588 | if (!(md->attribute & EFI_MEMORY_WB)) | |
589 | efi_memory_uc((u64)(unsigned long)va, size); | |
590 | } else | |
591 | va = efi_ioremap(md->phys_addr, size, | |
592 | md->type, md->attribute); | |
593 | ||
594 | md->virt_addr = (u64) (unsigned long) va; | |
595 | if (!va) | |
596 | pr_err("ioremap of 0x%llX failed!\n", | |
597 | (unsigned long long)md->phys_addr); | |
598 | } | |
599 | ||
481f75c0 DY |
600 | /* Merge contiguous regions of the same type and attribute */ |
601 | static void __init efi_merge_regions(void) | |
5b83683f | 602 | { |
481f75c0 | 603 | void *p; |
202f9d0a | 604 | efi_memory_desc_t *md, *prev_md = NULL; |
202f9d0a | 605 | |
202f9d0a MG |
606 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
607 | u64 prev_size; | |
608 | md = p; | |
609 | ||
610 | if (!prev_md) { | |
611 | prev_md = md; | |
612 | continue; | |
613 | } | |
614 | ||
615 | if (prev_md->type != md->type || | |
616 | prev_md->attribute != md->attribute) { | |
617 | prev_md = md; | |
618 | continue; | |
619 | } | |
620 | ||
621 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; | |
622 | ||
623 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { | |
624 | prev_md->num_pages += md->num_pages; | |
625 | md->type = EFI_RESERVED_TYPE; | |
626 | md->attribute = 0; | |
627 | continue; | |
628 | } | |
629 | prev_md = md; | |
481f75c0 DY |
630 | } |
631 | } | |
632 | ||
633 | static void __init get_systab_virt_addr(efi_memory_desc_t *md) | |
634 | { | |
635 | unsigned long size; | |
636 | u64 end, systab; | |
d2f7cbe7 | 637 | |
481f75c0 DY |
638 | size = md->num_pages << EFI_PAGE_SHIFT; |
639 | end = md->phys_addr + size; | |
640 | systab = (u64)(unsigned long)efi_phys.systab; | |
641 | if (md->phys_addr <= systab && systab < end) { | |
642 | systab += md->virt_addr - md->phys_addr; | |
643 | efi.systab = (efi_system_table_t *)(unsigned long)systab; | |
202f9d0a | 644 | } |
481f75c0 DY |
645 | } |
646 | ||
fabb37c7 | 647 | static void __init save_runtime_map(void) |
926172d4 | 648 | { |
fabb37c7 | 649 | #ifdef CONFIG_KEXEC |
926172d4 DY |
650 | efi_memory_desc_t *md; |
651 | void *tmp, *p, *q = NULL; | |
652 | int count = 0; | |
653 | ||
a3530e8f DY |
654 | if (efi_enabled(EFI_OLD_MEMMAP)) |
655 | return; | |
656 | ||
926172d4 DY |
657 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
658 | md = p; | |
659 | ||
660 | if (!(md->attribute & EFI_MEMORY_RUNTIME) || | |
661 | (md->type == EFI_BOOT_SERVICES_CODE) || | |
662 | (md->type == EFI_BOOT_SERVICES_DATA)) | |
663 | continue; | |
664 | tmp = krealloc(q, (count + 1) * memmap.desc_size, GFP_KERNEL); | |
665 | if (!tmp) | |
666 | goto out; | |
667 | q = tmp; | |
668 | ||
669 | memcpy(q + count * memmap.desc_size, md, memmap.desc_size); | |
670 | count++; | |
671 | } | |
672 | ||
518548ab | 673 | efi_runtime_map_setup(q, count, memmap.desc_size); |
fabb37c7 | 674 | return; |
926172d4 | 675 | |
926172d4 DY |
676 | out: |
677 | kfree(q); | |
fabb37c7 BP |
678 | pr_err("Error saving runtime map, efi runtime on kexec non-functional!!\n"); |
679 | #endif | |
1fec0533 DY |
680 | } |
681 | ||
b7b898ae BP |
682 | static void *realloc_pages(void *old_memmap, int old_shift) |
683 | { | |
684 | void *ret; | |
685 | ||
686 | ret = (void *)__get_free_pages(GFP_KERNEL, old_shift + 1); | |
687 | if (!ret) | |
688 | goto out; | |
689 | ||
690 | /* | |
691 | * A first-time allocation doesn't have anything to copy. | |
692 | */ | |
693 | if (!old_memmap) | |
694 | return ret; | |
695 | ||
696 | memcpy(ret, old_memmap, PAGE_SIZE << old_shift); | |
697 | ||
698 | out: | |
699 | free_pages((unsigned long)old_memmap, old_shift); | |
700 | return ret; | |
701 | } | |
702 | ||
481f75c0 | 703 | /* |
b7b898ae BP |
704 | * Map the efi memory ranges of the runtime services and update new_mmap with |
705 | * virtual addresses. | |
481f75c0 | 706 | */ |
b7b898ae | 707 | static void * __init efi_map_regions(int *count, int *pg_shift) |
481f75c0 | 708 | { |
b7b898ae BP |
709 | void *p, *new_memmap = NULL; |
710 | unsigned long left = 0; | |
481f75c0 | 711 | efi_memory_desc_t *md; |
202f9d0a | 712 | |
5b83683f HY |
713 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
714 | md = p; | |
70087011 JB |
715 | if (!(md->attribute & EFI_MEMORY_RUNTIME)) { |
716 | #ifdef CONFIG_X86_64 | |
717 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
718 | md->type != EFI_BOOT_SERVICES_DATA) | |
719 | #endif | |
720 | continue; | |
721 | } | |
1c083eb2 | 722 | |
d2f7cbe7 | 723 | efi_map_region(md); |
481f75c0 DY |
724 | get_systab_virt_addr(md); |
725 | ||
b7b898ae BP |
726 | if (left < memmap.desc_size) { |
727 | new_memmap = realloc_pages(new_memmap, *pg_shift); | |
728 | if (!new_memmap) | |
729 | return NULL; | |
730 | ||
731 | left += PAGE_SIZE << *pg_shift; | |
732 | (*pg_shift)++; | |
733 | } | |
734 | ||
481f75c0 DY |
735 | memcpy(new_memmap + (*count * memmap.desc_size), md, |
736 | memmap.desc_size); | |
b7b898ae BP |
737 | |
738 | left -= memmap.desc_size; | |
481f75c0 DY |
739 | (*count)++; |
740 | } | |
d2f7cbe7 | 741 | |
481f75c0 | 742 | return new_memmap; |
481f75c0 DY |
743 | } |
744 | ||
fabb37c7 BP |
745 | static void __init kexec_enter_virtual_mode(void) |
746 | { | |
747 | #ifdef CONFIG_KEXEC | |
748 | efi_memory_desc_t *md; | |
749 | void *p; | |
750 | ||
751 | efi.systab = NULL; | |
752 | ||
753 | /* | |
754 | * We don't do virtual mode, since we don't do runtime services, on | |
755 | * non-native EFI | |
756 | */ | |
757 | if (!efi_is_native()) { | |
758 | efi_unmap_memmap(); | |
759 | return; | |
760 | } | |
761 | ||
762 | /* | |
763 | * Map efi regions which were passed via setup_data. The virt_addr is a | |
764 | * fixed addr which was used in first kernel of a kexec boot. | |
765 | */ | |
766 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
767 | md = p; | |
768 | efi_map_region_fixed(md); /* FIXME: add error handling */ | |
769 | get_systab_virt_addr(md); | |
770 | } | |
771 | ||
772 | save_runtime_map(); | |
773 | ||
774 | BUG_ON(!efi.systab); | |
775 | ||
776 | efi_sync_low_kernel_mappings(); | |
777 | ||
778 | /* | |
779 | * Now that EFI is in virtual mode, update the function | |
780 | * pointers in the runtime service table to the new virtual addresses. | |
781 | * | |
782 | * Call EFI services through wrapper functions. | |
783 | */ | |
784 | efi.runtime_version = efi_systab.hdr.revision; | |
994448f1 | 785 | |
022ee6c5 | 786 | efi_native_runtime_setup(); |
994448f1 | 787 | |
fabb37c7 | 788 | efi.set_virtual_address_map = NULL; |
fabb37c7 BP |
789 | |
790 | if (efi_enabled(EFI_OLD_MEMMAP) && (__supported_pte_mask & _PAGE_NX)) | |
791 | runtime_code_page_mkexec(); | |
792 | ||
793 | /* clean DUMMY object */ | |
eeb9db09 | 794 | efi_delete_dummy_variable(); |
fabb37c7 BP |
795 | #endif |
796 | } | |
797 | ||
481f75c0 DY |
798 | /* |
799 | * This function will switch the EFI runtime services to virtual mode. | |
800 | * Essentially, we look through the EFI memmap and map every region that | |
801 | * has the runtime attribute bit set in its memory descriptor into the | |
802 | * ->trampoline_pgd page table using a top-down VA allocation scheme. | |
803 | * | |
804 | * The old method which used to update that memory descriptor with the | |
805 | * virtual address obtained from ioremap() is still supported when the | |
806 | * kernel is booted with efi=old_map on its command line. Same old | |
807 | * method enabled the runtime services to be called without having to | |
808 | * thunk back into physical mode for every invocation. | |
809 | * | |
810 | * The new method does a pagetable switch in a preemption-safe manner | |
811 | * so that we're in a different address space when calling a runtime | |
812 | * function. For function arguments passing we do copy the PGDs of the | |
813 | * kernel page table into ->trampoline_pgd prior to each call. | |
1fec0533 DY |
814 | * |
815 | * Specially for kexec boot, efi runtime maps in previous kernel should | |
816 | * be passed in via setup_data. In that case runtime ranges will be mapped | |
fabb37c7 BP |
817 | * to the same virtual addresses as the first kernel, see |
818 | * kexec_enter_virtual_mode(). | |
481f75c0 | 819 | */ |
fabb37c7 | 820 | static void __init __efi_enter_virtual_mode(void) |
481f75c0 | 821 | { |
fabb37c7 | 822 | int count = 0, pg_shift = 0; |
481f75c0 | 823 | void *new_memmap = NULL; |
b7b898ae | 824 | efi_status_t status; |
1c083eb2 | 825 | |
481f75c0 | 826 | efi.systab = NULL; |
d2f7cbe7 | 827 | |
fabb37c7 BP |
828 | efi_merge_regions(); |
829 | new_memmap = efi_map_regions(&count, &pg_shift); | |
830 | if (!new_memmap) { | |
831 | pr_err("Error reallocating memory, EFI runtime non-functional!\n"); | |
832 | return; | |
b7b898ae | 833 | } |
926172d4 | 834 | |
fabb37c7 BP |
835 | save_runtime_map(); |
836 | ||
5b83683f HY |
837 | BUG_ON(!efi.systab); |
838 | ||
fabb37c7 BP |
839 | if (efi_setup_page_tables(__pa(new_memmap), 1 << pg_shift)) |
840 | return; | |
b7b898ae | 841 | |
d2f7cbe7 | 842 | efi_sync_low_kernel_mappings(); |
11cc8512 | 843 | efi_dump_pagetable(); |
d2f7cbe7 | 844 | |
994448f1 MF |
845 | if (efi_is_native()) { |
846 | status = phys_efi_set_virtual_address_map( | |
847 | memmap.desc_size * count, | |
848 | memmap.desc_size, | |
849 | memmap.desc_version, | |
850 | (efi_memory_desc_t *)__pa(new_memmap)); | |
851 | } else { | |
852 | status = efi_thunk_set_virtual_address_map( | |
853 | efi_phys.set_virtual_address_map, | |
854 | memmap.desc_size * count, | |
855 | memmap.desc_size, | |
856 | memmap.desc_version, | |
857 | (efi_memory_desc_t *)__pa(new_memmap)); | |
858 | } | |
1fec0533 | 859 | |
fabb37c7 BP |
860 | if (status != EFI_SUCCESS) { |
861 | pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n", | |
862 | status); | |
863 | panic("EFI call to SetVirtualAddressMap() failed!"); | |
5b83683f HY |
864 | } |
865 | ||
866 | /* | |
867 | * Now that EFI is in virtual mode, update the function | |
868 | * pointers in the runtime service table to the new virtual addresses. | |
869 | * | |
870 | * Call EFI services through wrapper functions. | |
871 | */ | |
712ba9e9 | 872 | efi.runtime_version = efi_systab.hdr.revision; |
4f9dbcfc MF |
873 | |
874 | if (efi_is_native()) | |
022ee6c5 | 875 | efi_native_runtime_setup(); |
4f9dbcfc MF |
876 | else |
877 | efi_thunk_runtime_setup(); | |
878 | ||
2b5e8ef3 | 879 | efi.set_virtual_address_map = NULL; |
d2f7cbe7 | 880 | |
c55d016f | 881 | efi_runtime_mkexec(); |
1adbfa35 | 882 | |
b7b898ae BP |
883 | /* |
884 | * We mapped the descriptor array into the EFI pagetable above but we're | |
885 | * not unmapping it here. Here's why: | |
886 | * | |
887 | * We're copying select PGDs from the kernel page table to the EFI page | |
888 | * table and when we do so and make changes to those PGDs like unmapping | |
889 | * stuff from them, those changes appear in the kernel page table and we | |
890 | * go boom. | |
891 | * | |
892 | * From setup_real_mode(): | |
893 | * | |
894 | * ... | |
895 | * trampoline_pgd[0] = init_level4_pgt[pgd_index(__PAGE_OFFSET)].pgd; | |
896 | * | |
897 | * In this particular case, our allocation is in PGD 0 of the EFI page | |
898 | * table but we've copied that PGD from PGD[272] of the EFI page table: | |
899 | * | |
900 | * pgd_index(__PAGE_OFFSET = 0xffff880000000000) = 272 | |
901 | * | |
902 | * where the direct memory mapping in kernel space is. | |
903 | * | |
904 | * new_memmap's VA comes from that direct mapping and thus clearing it, | |
905 | * it would get cleared in the kernel page table too. | |
906 | * | |
907 | * efi_cleanup_page_tables(__pa(new_memmap), 1 << pg_shift); | |
908 | */ | |
fabb37c7 | 909 | free_pages((unsigned long)new_memmap, pg_shift); |
f8b84043 MG |
910 | |
911 | /* clean DUMMY object */ | |
eeb9db09 | 912 | efi_delete_dummy_variable(); |
5b83683f HY |
913 | } |
914 | ||
fabb37c7 BP |
915 | void __init efi_enter_virtual_mode(void) |
916 | { | |
917 | if (efi_setup) | |
918 | kexec_enter_virtual_mode(); | |
919 | else | |
920 | __efi_enter_virtual_mode(); | |
921 | } | |
922 | ||
5b83683f HY |
923 | /* |
924 | * Convenience functions to obtain memory types and attributes | |
925 | */ | |
926 | u32 efi_mem_type(unsigned long phys_addr) | |
927 | { | |
928 | efi_memory_desc_t *md; | |
929 | void *p; | |
930 | ||
83e68189 MF |
931 | if (!efi_enabled(EFI_MEMMAP)) |
932 | return 0; | |
933 | ||
5b83683f HY |
934 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
935 | md = p; | |
936 | if ((md->phys_addr <= phys_addr) && | |
937 | (phys_addr < (md->phys_addr + | |
938 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
939 | return md->type; | |
940 | } | |
941 | return 0; | |
942 | } | |
943 | ||
944 | u64 efi_mem_attributes(unsigned long phys_addr) | |
945 | { | |
946 | efi_memory_desc_t *md; | |
947 | void *p; | |
948 | ||
949 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
950 | md = p; | |
951 | if ((md->phys_addr <= phys_addr) && | |
952 | (phys_addr < (md->phys_addr + | |
953 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
954 | return md->attribute; | |
955 | } | |
956 | return 0; | |
957 | } | |
a6e4d5a0 | 958 | |
d2f7cbe7 BP |
959 | static int __init parse_efi_cmdline(char *str) |
960 | { | |
961 | if (*str == '=') | |
962 | str++; | |
963 | ||
964 | if (!strncmp(str, "old_map", 7)) | |
0f8093a9 | 965 | set_bit(EFI_OLD_MEMMAP, &efi.flags); |
d2f7cbe7 BP |
966 | |
967 | return 0; | |
968 | } | |
969 | early_param("efi", parse_efi_cmdline); |