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> | |
15 | * | |
16 | * Copied from efi_32.c to eliminate the duplicated code between EFI | |
17 | * 32/64 support code. --ying 2007-10-26 | |
18 | * | |
19 | * All EFI Runtime Services are not implemented yet as EFI only | |
20 | * supports physical mode addressing on SoftSDV. This is to be fixed | |
21 | * in a future version. --drummond 1999-07-20 | |
22 | * | |
23 | * Implemented EFI runtime services and virtual mode calls. --davidm | |
24 | * | |
25 | * Goutham Rao: <goutham.rao@intel.com> | |
26 | * Skip non-WB memory and ignore empty memory ranges. | |
27 | */ | |
28 | ||
e3cb3f5a OJ |
29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
30 | ||
5b83683f HY |
31 | #include <linux/kernel.h> |
32 | #include <linux/init.h> | |
33 | #include <linux/efi.h> | |
2223af38 | 34 | #include <linux/efi-bgrt.h> |
69c60c88 | 35 | #include <linux/export.h> |
5b83683f | 36 | #include <linux/bootmem.h> |
a9ce6bc1 | 37 | #include <linux/memblock.h> |
5b83683f HY |
38 | #include <linux/spinlock.h> |
39 | #include <linux/uaccess.h> | |
40 | #include <linux/time.h> | |
41 | #include <linux/io.h> | |
42 | #include <linux/reboot.h> | |
43 | #include <linux/bcd.h> | |
44 | ||
45 | #include <asm/setup.h> | |
46 | #include <asm/efi.h> | |
47 | #include <asm/time.h> | |
a2172e25 HY |
48 | #include <asm/cacheflush.h> |
49 | #include <asm/tlbflush.h> | |
7bd867df | 50 | #include <asm/x86_init.h> |
3195ef59 | 51 | #include <asm/rtc.h> |
5b83683f HY |
52 | |
53 | #define EFI_DEBUG 1 | |
5b83683f | 54 | |
d80603c9 JB |
55 | struct efi __read_mostly efi = { |
56 | .mps = EFI_INVALID_TABLE_ADDR, | |
57 | .acpi = EFI_INVALID_TABLE_ADDR, | |
58 | .acpi20 = EFI_INVALID_TABLE_ADDR, | |
59 | .smbios = EFI_INVALID_TABLE_ADDR, | |
60 | .sal_systab = EFI_INVALID_TABLE_ADDR, | |
61 | .boot_info = EFI_INVALID_TABLE_ADDR, | |
62 | .hcdp = EFI_INVALID_TABLE_ADDR, | |
63 | .uga = EFI_INVALID_TABLE_ADDR, | |
64 | .uv_systab = EFI_INVALID_TABLE_ADDR, | |
65 | }; | |
5b83683f HY |
66 | EXPORT_SYMBOL(efi); |
67 | ||
68 | struct efi_memory_map memmap; | |
69 | ||
ecaea42e | 70 | static struct efi efi_phys __initdata; |
5b83683f HY |
71 | static efi_system_table_t efi_systab __initdata; |
72 | ||
83e68189 MF |
73 | unsigned long x86_efi_facility; |
74 | ||
75 | /* | |
76 | * Returns 1 if 'facility' is enabled, 0 otherwise. | |
77 | */ | |
78 | int efi_enabled(int facility) | |
79 | { | |
80 | return test_bit(facility, &x86_efi_facility) != 0; | |
5189c2a7 | 81 | } |
83e68189 | 82 | EXPORT_SYMBOL(efi_enabled); |
5189c2a7 | 83 | |
058e7b58 | 84 | static bool __initdata disable_runtime = false; |
8b2cb7a8 HY |
85 | static int __init setup_noefi(char *arg) |
86 | { | |
fb834c7a | 87 | disable_runtime = true; |
8b2cb7a8 HY |
88 | return 0; |
89 | } | |
90 | early_param("noefi", setup_noefi); | |
91 | ||
200001eb PJ |
92 | int add_efi_memmap; |
93 | EXPORT_SYMBOL(add_efi_memmap); | |
94 | ||
95 | static int __init setup_add_efi_memmap(char *arg) | |
96 | { | |
97 | add_efi_memmap = 1; | |
98 | return 0; | |
99 | } | |
100 | early_param("add_efi_memmap", setup_add_efi_memmap); | |
101 | ||
102 | ||
5b83683f HY |
103 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
104 | { | |
ef68c8f8 JB |
105 | unsigned long flags; |
106 | efi_status_t status; | |
107 | ||
108 | spin_lock_irqsave(&rtc_lock, flags); | |
109 | status = efi_call_virt2(get_time, tm, tc); | |
110 | spin_unlock_irqrestore(&rtc_lock, flags); | |
111 | return status; | |
5b83683f HY |
112 | } |
113 | ||
114 | static efi_status_t virt_efi_set_time(efi_time_t *tm) | |
115 | { | |
ef68c8f8 JB |
116 | unsigned long flags; |
117 | efi_status_t status; | |
118 | ||
119 | spin_lock_irqsave(&rtc_lock, flags); | |
120 | status = efi_call_virt1(set_time, tm); | |
121 | spin_unlock_irqrestore(&rtc_lock, flags); | |
122 | return status; | |
5b83683f HY |
123 | } |
124 | ||
125 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, | |
126 | efi_bool_t *pending, | |
127 | efi_time_t *tm) | |
128 | { | |
ef68c8f8 JB |
129 | unsigned long flags; |
130 | efi_status_t status; | |
131 | ||
132 | spin_lock_irqsave(&rtc_lock, flags); | |
133 | status = efi_call_virt3(get_wakeup_time, | |
134 | enabled, pending, tm); | |
135 | spin_unlock_irqrestore(&rtc_lock, flags); | |
136 | return status; | |
5b83683f HY |
137 | } |
138 | ||
139 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) | |
140 | { | |
ef68c8f8 JB |
141 | unsigned long flags; |
142 | efi_status_t status; | |
143 | ||
144 | spin_lock_irqsave(&rtc_lock, flags); | |
145 | status = efi_call_virt2(set_wakeup_time, | |
146 | enabled, tm); | |
147 | spin_unlock_irqrestore(&rtc_lock, flags); | |
148 | return status; | |
5b83683f HY |
149 | } |
150 | ||
151 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, | |
152 | efi_guid_t *vendor, | |
153 | u32 *attr, | |
154 | unsigned long *data_size, | |
155 | void *data) | |
156 | { | |
157 | return efi_call_virt5(get_variable, | |
158 | name, vendor, attr, | |
159 | data_size, data); | |
160 | } | |
161 | ||
162 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, | |
163 | efi_char16_t *name, | |
164 | efi_guid_t *vendor) | |
165 | { | |
166 | return efi_call_virt3(get_next_variable, | |
167 | name_size, name, vendor); | |
168 | } | |
169 | ||
170 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, | |
171 | efi_guid_t *vendor, | |
f7a2d73f | 172 | u32 attr, |
5b83683f HY |
173 | unsigned long data_size, |
174 | void *data) | |
175 | { | |
176 | return efi_call_virt5(set_variable, | |
177 | name, vendor, attr, | |
178 | data_size, data); | |
179 | } | |
180 | ||
3b370237 MG |
181 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
182 | u64 *storage_space, | |
183 | u64 *remaining_space, | |
184 | u64 *max_variable_size) | |
185 | { | |
186 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
187 | return EFI_UNSUPPORTED; | |
188 | ||
189 | return efi_call_virt4(query_variable_info, attr, storage_space, | |
190 | remaining_space, max_variable_size); | |
191 | } | |
192 | ||
5b83683f HY |
193 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
194 | { | |
195 | return efi_call_virt1(get_next_high_mono_count, count); | |
196 | } | |
197 | ||
198 | static void virt_efi_reset_system(int reset_type, | |
199 | efi_status_t status, | |
200 | unsigned long data_size, | |
201 | efi_char16_t *data) | |
202 | { | |
203 | efi_call_virt4(reset_system, reset_type, status, | |
204 | data_size, data); | |
205 | } | |
206 | ||
3b370237 MG |
207 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
208 | unsigned long count, | |
209 | unsigned long sg_list) | |
210 | { | |
211 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
212 | return EFI_UNSUPPORTED; | |
213 | ||
214 | return efi_call_virt3(update_capsule, capsules, count, sg_list); | |
215 | } | |
216 | ||
217 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, | |
218 | unsigned long count, | |
219 | u64 *max_size, | |
220 | int *reset_type) | |
221 | { | |
222 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
223 | return EFI_UNSUPPORTED; | |
224 | ||
225 | return efi_call_virt4(query_capsule_caps, capsules, count, max_size, | |
226 | reset_type); | |
227 | } | |
228 | ||
5b83683f HY |
229 | static efi_status_t __init phys_efi_set_virtual_address_map( |
230 | unsigned long memory_map_size, | |
231 | unsigned long descriptor_size, | |
232 | u32 descriptor_version, | |
233 | efi_memory_desc_t *virtual_map) | |
234 | { | |
235 | efi_status_t status; | |
236 | ||
237 | efi_call_phys_prelog(); | |
238 | status = efi_call_phys4(efi_phys.set_virtual_address_map, | |
239 | memory_map_size, descriptor_size, | |
240 | descriptor_version, virtual_map); | |
241 | efi_call_phys_epilog(); | |
242 | return status; | |
243 | } | |
244 | ||
11520e5e LT |
245 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, |
246 | efi_time_cap_t *tc) | |
247 | { | |
248 | unsigned long flags; | |
249 | efi_status_t status; | |
250 | ||
251 | spin_lock_irqsave(&rtc_lock, flags); | |
252 | efi_call_phys_prelog(); | |
253 | status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm), | |
254 | virt_to_phys(tc)); | |
255 | efi_call_phys_epilog(); | |
256 | spin_unlock_irqrestore(&rtc_lock, flags); | |
257 | return status; | |
258 | } | |
259 | ||
260 | int efi_set_rtc_mmss(unsigned long nowtime) | |
5b83683f | 261 | { |
5b83683f HY |
262 | efi_status_t status; |
263 | efi_time_t eft; | |
264 | efi_time_cap_t cap; | |
3195ef59 | 265 | struct rtc_time tm; |
5b83683f HY |
266 | |
267 | status = efi.get_time(&eft, &cap); | |
268 | if (status != EFI_SUCCESS) { | |
e3cb3f5a | 269 | pr_err("Oops: efitime: can't read time!\n"); |
5b83683f HY |
270 | return -1; |
271 | } | |
272 | ||
3195ef59 PB |
273 | rtc_time_to_tm(nowtime, &tm); |
274 | if (!rtc_valid_tm(&tm)) { | |
275 | eft.year = tm.tm_year + 1900; | |
276 | eft.month = tm.tm_mon + 1; | |
277 | eft.day = tm.tm_mday; | |
278 | eft.minute = tm.tm_min; | |
279 | eft.second = tm.tm_sec; | |
280 | eft.nanosecond = 0; | |
281 | } else { | |
282 | printk(KERN_ERR | |
283 | "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n", | |
284 | __FUNCTION__, nowtime); | |
285 | return -1; | |
286 | } | |
5b83683f HY |
287 | |
288 | status = efi.set_time(&eft); | |
289 | if (status != EFI_SUCCESS) { | |
e3cb3f5a | 290 | pr_err("Oops: efitime: can't write time!\n"); |
5b83683f HY |
291 | return -1; |
292 | } | |
293 | return 0; | |
294 | } | |
295 | ||
11520e5e | 296 | unsigned long efi_get_time(void) |
5b83683f HY |
297 | { |
298 | efi_status_t status; | |
299 | efi_time_t eft; | |
300 | efi_time_cap_t cap; | |
301 | ||
302 | status = efi.get_time(&eft, &cap); | |
303 | if (status != EFI_SUCCESS) | |
e3cb3f5a | 304 | pr_err("Oops: efitime: can't read time!\n"); |
5b83683f HY |
305 | |
306 | return mktime(eft.year, eft.month, eft.day, eft.hour, | |
307 | eft.minute, eft.second); | |
308 | } | |
309 | ||
69c91893 PJ |
310 | /* |
311 | * Tell the kernel about the EFI memory map. This might include | |
312 | * more than the max 128 entries that can fit in the e820 legacy | |
313 | * (zeropage) memory map. | |
314 | */ | |
315 | ||
200001eb | 316 | static void __init do_add_efi_memmap(void) |
69c91893 PJ |
317 | { |
318 | void *p; | |
319 | ||
320 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
321 | efi_memory_desc_t *md = p; | |
322 | unsigned long long start = md->phys_addr; | |
323 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
324 | int e820_type; | |
325 | ||
e2a71476 CW |
326 | switch (md->type) { |
327 | case EFI_LOADER_CODE: | |
328 | case EFI_LOADER_DATA: | |
329 | case EFI_BOOT_SERVICES_CODE: | |
330 | case EFI_BOOT_SERVICES_DATA: | |
331 | case EFI_CONVENTIONAL_MEMORY: | |
332 | if (md->attribute & EFI_MEMORY_WB) | |
333 | e820_type = E820_RAM; | |
334 | else | |
335 | e820_type = E820_RESERVED; | |
336 | break; | |
337 | case EFI_ACPI_RECLAIM_MEMORY: | |
338 | e820_type = E820_ACPI; | |
339 | break; | |
340 | case EFI_ACPI_MEMORY_NVS: | |
341 | e820_type = E820_NVS; | |
342 | break; | |
343 | case EFI_UNUSABLE_MEMORY: | |
344 | e820_type = E820_UNUSABLE; | |
345 | break; | |
346 | default: | |
347 | /* | |
348 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE | |
349 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO | |
350 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE | |
351 | */ | |
69c91893 | 352 | e820_type = E820_RESERVED; |
e2a71476 CW |
353 | break; |
354 | } | |
d0be6bde | 355 | e820_add_region(start, size, e820_type); |
69c91893 PJ |
356 | } |
357 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); | |
358 | } | |
359 | ||
1adbfa35 | 360 | int __init efi_memblock_x86_reserve_range(void) |
ecacf09f HY |
361 | { |
362 | unsigned long pmap; | |
363 | ||
05486fa7 | 364 | #ifdef CONFIG_X86_32 |
1adbfa35 OJ |
365 | /* Can't handle data above 4GB at this time */ |
366 | if (boot_params.efi_info.efi_memmap_hi) { | |
367 | pr_err("Memory map is above 4GB, disabling EFI.\n"); | |
368 | return -EINVAL; | |
369 | } | |
ecacf09f | 370 | pmap = boot_params.efi_info.efi_memmap; |
05486fa7 PJ |
371 | #else |
372 | pmap = (boot_params.efi_info.efi_memmap | | |
373 | ((__u64)boot_params.efi_info.efi_memmap_hi<<32)); | |
ecacf09f HY |
374 | #endif |
375 | memmap.phys_map = (void *)pmap; | |
376 | memmap.nr_map = boot_params.efi_info.efi_memmap_size / | |
377 | boot_params.efi_info.efi_memdesc_size; | |
378 | memmap.desc_version = boot_params.efi_info.efi_memdesc_version; | |
379 | memmap.desc_size = boot_params.efi_info.efi_memdesc_size; | |
24aa0788 | 380 | memblock_reserve(pmap, memmap.nr_map * memmap.desc_size); |
1adbfa35 OJ |
381 | |
382 | return 0; | |
ecacf09f HY |
383 | } |
384 | ||
5b83683f HY |
385 | #if EFI_DEBUG |
386 | static void __init print_efi_memmap(void) | |
387 | { | |
388 | efi_memory_desc_t *md; | |
389 | void *p; | |
390 | int i; | |
391 | ||
392 | for (p = memmap.map, i = 0; | |
393 | p < memmap.map_end; | |
394 | p += memmap.desc_size, i++) { | |
395 | md = p; | |
e3cb3f5a | 396 | pr_info("mem%02u: type=%u, attr=0x%llx, " |
5b83683f HY |
397 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", |
398 | i, md->type, md->attribute, md->phys_addr, | |
399 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), | |
400 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); | |
401 | } | |
402 | } | |
403 | #endif /* EFI_DEBUG */ | |
404 | ||
916f676f MG |
405 | void __init efi_reserve_boot_services(void) |
406 | { | |
407 | void *p; | |
408 | ||
409 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
410 | efi_memory_desc_t *md = p; | |
7d68dc3f ML |
411 | u64 start = md->phys_addr; |
412 | u64 size = md->num_pages << EFI_PAGE_SHIFT; | |
916f676f MG |
413 | |
414 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
415 | md->type != EFI_BOOT_SERVICES_DATA) | |
416 | continue; | |
7d68dc3f ML |
417 | /* Only reserve where possible: |
418 | * - Not within any already allocated areas | |
419 | * - Not over any memory area (really needed, if above?) | |
420 | * - Not within any part of the kernel | |
421 | * - Not the bios reserved area | |
422 | */ | |
fc8d7826 AD |
423 | if ((start+size >= __pa_symbol(_text) |
424 | && start <= __pa_symbol(_end)) || | |
7d68dc3f | 425 | !e820_all_mapped(start, start+size, E820_RAM) || |
bf61549a | 426 | memblock_is_region_reserved(start, size)) { |
7d68dc3f ML |
427 | /* Could not reserve, skip it */ |
428 | md->num_pages = 0; | |
e3cb3f5a | 429 | memblock_dbg("Could not reserve boot range " |
7d68dc3f ML |
430 | "[0x%010llx-0x%010llx]\n", |
431 | start, start+size-1); | |
432 | } else | |
24aa0788 | 433 | memblock_reserve(start, size); |
916f676f MG |
434 | } |
435 | } | |
436 | ||
5189c2a7 | 437 | void __init efi_unmap_memmap(void) |
78510792 | 438 | { |
83e68189 | 439 | clear_bit(EFI_MEMMAP, &x86_efi_facility); |
78510792 JT |
440 | if (memmap.map) { |
441 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); | |
442 | memmap.map = NULL; | |
443 | } | |
444 | } | |
445 | ||
446 | void __init efi_free_boot_services(void) | |
916f676f MG |
447 | { |
448 | void *p; | |
449 | ||
5189c2a7 | 450 | if (!efi_is_native()) |
78510792 JT |
451 | return; |
452 | ||
916f676f MG |
453 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
454 | efi_memory_desc_t *md = p; | |
455 | unsigned long long start = md->phys_addr; | |
456 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
457 | ||
458 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
459 | md->type != EFI_BOOT_SERVICES_DATA) | |
460 | continue; | |
461 | ||
7d68dc3f ML |
462 | /* Could not reserve boot area */ |
463 | if (!size) | |
464 | continue; | |
465 | ||
916f676f MG |
466 | free_bootmem_late(start, size); |
467 | } | |
78510792 JT |
468 | |
469 | efi_unmap_memmap(); | |
916f676f MG |
470 | } |
471 | ||
140bf275 | 472 | static int __init efi_systab_init(void *phys) |
5b83683f | 473 | { |
83e68189 | 474 | if (efi_enabled(EFI_64BIT)) { |
1adbfa35 OJ |
475 | efi_system_table_64_t *systab64; |
476 | u64 tmp = 0; | |
477 | ||
478 | systab64 = early_ioremap((unsigned long)phys, | |
479 | sizeof(*systab64)); | |
480 | if (systab64 == NULL) { | |
481 | pr_err("Couldn't map the system table!\n"); | |
482 | return -ENOMEM; | |
483 | } | |
484 | ||
485 | efi_systab.hdr = systab64->hdr; | |
486 | efi_systab.fw_vendor = systab64->fw_vendor; | |
487 | tmp |= systab64->fw_vendor; | |
488 | efi_systab.fw_revision = systab64->fw_revision; | |
489 | efi_systab.con_in_handle = systab64->con_in_handle; | |
490 | tmp |= systab64->con_in_handle; | |
491 | efi_systab.con_in = systab64->con_in; | |
492 | tmp |= systab64->con_in; | |
493 | efi_systab.con_out_handle = systab64->con_out_handle; | |
494 | tmp |= systab64->con_out_handle; | |
495 | efi_systab.con_out = systab64->con_out; | |
496 | tmp |= systab64->con_out; | |
497 | efi_systab.stderr_handle = systab64->stderr_handle; | |
498 | tmp |= systab64->stderr_handle; | |
499 | efi_systab.stderr = systab64->stderr; | |
500 | tmp |= systab64->stderr; | |
501 | efi_systab.runtime = (void *)(unsigned long)systab64->runtime; | |
502 | tmp |= systab64->runtime; | |
503 | efi_systab.boottime = (void *)(unsigned long)systab64->boottime; | |
504 | tmp |= systab64->boottime; | |
505 | efi_systab.nr_tables = systab64->nr_tables; | |
506 | efi_systab.tables = systab64->tables; | |
507 | tmp |= systab64->tables; | |
508 | ||
509 | early_iounmap(systab64, sizeof(*systab64)); | |
510 | #ifdef CONFIG_X86_32 | |
511 | if (tmp >> 32) { | |
512 | pr_err("EFI data located above 4GB, disabling EFI.\n"); | |
513 | return -EINVAL; | |
514 | } | |
515 | #endif | |
516 | } else { | |
517 | efi_system_table_32_t *systab32; | |
518 | ||
519 | systab32 = early_ioremap((unsigned long)phys, | |
520 | sizeof(*systab32)); | |
521 | if (systab32 == NULL) { | |
522 | pr_err("Couldn't map the system table!\n"); | |
523 | return -ENOMEM; | |
524 | } | |
525 | ||
526 | efi_systab.hdr = systab32->hdr; | |
527 | efi_systab.fw_vendor = systab32->fw_vendor; | |
528 | efi_systab.fw_revision = systab32->fw_revision; | |
529 | efi_systab.con_in_handle = systab32->con_in_handle; | |
530 | efi_systab.con_in = systab32->con_in; | |
531 | efi_systab.con_out_handle = systab32->con_out_handle; | |
532 | efi_systab.con_out = systab32->con_out; | |
533 | efi_systab.stderr_handle = systab32->stderr_handle; | |
534 | efi_systab.stderr = systab32->stderr; | |
535 | efi_systab.runtime = (void *)(unsigned long)systab32->runtime; | |
536 | efi_systab.boottime = (void *)(unsigned long)systab32->boottime; | |
537 | efi_systab.nr_tables = systab32->nr_tables; | |
538 | efi_systab.tables = systab32->tables; | |
539 | ||
540 | early_iounmap(systab32, sizeof(*systab32)); | |
140bf275 | 541 | } |
1adbfa35 | 542 | |
5b83683f HY |
543 | efi.systab = &efi_systab; |
544 | ||
545 | /* | |
546 | * Verify the EFI Table | |
547 | */ | |
140bf275 | 548 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
e3cb3f5a | 549 | pr_err("System table signature incorrect!\n"); |
140bf275 OJ |
550 | return -EINVAL; |
551 | } | |
5b83683f | 552 | if ((efi.systab->hdr.revision >> 16) == 0) |
e3cb3f5a | 553 | pr_err("Warning: System table version " |
5b83683f HY |
554 | "%d.%02d, expected 1.00 or greater!\n", |
555 | efi.systab->hdr.revision >> 16, | |
556 | efi.systab->hdr.revision & 0xffff); | |
140bf275 OJ |
557 | |
558 | return 0; | |
83e7ee66 | 559 | } |
5b83683f | 560 | |
140bf275 | 561 | static int __init efi_config_init(u64 tables, int nr_tables) |
83e7ee66 | 562 | { |
1adbfa35 OJ |
563 | void *config_tables, *tablep; |
564 | int i, sz; | |
565 | ||
83e68189 | 566 | if (efi_enabled(EFI_64BIT)) |
1adbfa35 OJ |
567 | sz = sizeof(efi_config_table_64_t); |
568 | else | |
569 | sz = sizeof(efi_config_table_32_t); | |
5b83683f HY |
570 | |
571 | /* | |
572 | * Let's see what config tables the firmware passed to us. | |
573 | */ | |
1adbfa35 | 574 | config_tables = early_ioremap(tables, nr_tables * sz); |
140bf275 | 575 | if (config_tables == NULL) { |
e3cb3f5a | 576 | pr_err("Could not map Configuration table!\n"); |
140bf275 OJ |
577 | return -ENOMEM; |
578 | } | |
5b83683f | 579 | |
1adbfa35 | 580 | tablep = config_tables; |
e3cb3f5a | 581 | pr_info(""); |
5b83683f | 582 | for (i = 0; i < efi.systab->nr_tables; i++) { |
1adbfa35 OJ |
583 | efi_guid_t guid; |
584 | unsigned long table; | |
585 | ||
83e68189 | 586 | if (efi_enabled(EFI_64BIT)) { |
1adbfa35 OJ |
587 | u64 table64; |
588 | guid = ((efi_config_table_64_t *)tablep)->guid; | |
589 | table64 = ((efi_config_table_64_t *)tablep)->table; | |
590 | table = table64; | |
591 | #ifdef CONFIG_X86_32 | |
592 | if (table64 >> 32) { | |
593 | pr_cont("\n"); | |
594 | pr_err("Table located above 4GB, disabling EFI.\n"); | |
595 | early_iounmap(config_tables, | |
596 | efi.systab->nr_tables * sz); | |
597 | return -EINVAL; | |
598 | } | |
599 | #endif | |
600 | } else { | |
601 | guid = ((efi_config_table_32_t *)tablep)->guid; | |
602 | table = ((efi_config_table_32_t *)tablep)->table; | |
603 | } | |
a6a46f41 OJ |
604 | if (!efi_guidcmp(guid, MPS_TABLE_GUID)) { |
605 | efi.mps = table; | |
606 | pr_cont(" MPS=0x%lx ", table); | |
607 | } else if (!efi_guidcmp(guid, ACPI_20_TABLE_GUID)) { | |
608 | efi.acpi20 = table; | |
609 | pr_cont(" ACPI 2.0=0x%lx ", table); | |
610 | } else if (!efi_guidcmp(guid, ACPI_TABLE_GUID)) { | |
611 | efi.acpi = table; | |
612 | pr_cont(" ACPI=0x%lx ", table); | |
613 | } else if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) { | |
614 | efi.smbios = table; | |
615 | pr_cont(" SMBIOS=0x%lx ", table); | |
03b48632 | 616 | #ifdef CONFIG_X86_UV |
a6a46f41 OJ |
617 | } else if (!efi_guidcmp(guid, UV_SYSTEM_TABLE_GUID)) { |
618 | efi.uv_systab = table; | |
619 | pr_cont(" UVsystab=0x%lx ", table); | |
03b48632 | 620 | #endif |
a6a46f41 OJ |
621 | } else if (!efi_guidcmp(guid, HCDP_TABLE_GUID)) { |
622 | efi.hcdp = table; | |
623 | pr_cont(" HCDP=0x%lx ", table); | |
624 | } else if (!efi_guidcmp(guid, UGA_IO_PROTOCOL_GUID)) { | |
625 | efi.uga = table; | |
626 | pr_cont(" UGA=0x%lx ", table); | |
5b83683f | 627 | } |
1adbfa35 | 628 | tablep += sz; |
5b83683f | 629 | } |
e3cb3f5a | 630 | pr_cont("\n"); |
a6a46f41 | 631 | early_iounmap(config_tables, efi.systab->nr_tables * sz); |
140bf275 | 632 | return 0; |
83e7ee66 OJ |
633 | } |
634 | ||
140bf275 | 635 | static int __init efi_runtime_init(void) |
83e7ee66 OJ |
636 | { |
637 | efi_runtime_services_t *runtime; | |
5b83683f HY |
638 | |
639 | /* | |
640 | * Check out the runtime services table. We need to map | |
641 | * the runtime services table so that we can grab the physical | |
642 | * address of several of the EFI runtime functions, needed to | |
643 | * set the firmware into virtual mode. | |
644 | */ | |
beacfaac HY |
645 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
646 | sizeof(efi_runtime_services_t)); | |
140bf275 | 647 | if (!runtime) { |
e3cb3f5a | 648 | pr_err("Could not map the runtime service table!\n"); |
140bf275 OJ |
649 | return -ENOMEM; |
650 | } | |
651 | /* | |
652 | * We will only need *early* access to the following | |
11520e5e | 653 | * two EFI runtime services before set_virtual_address_map |
140bf275 OJ |
654 | * is invoked. |
655 | */ | |
11520e5e | 656 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; |
140bf275 OJ |
657 | efi_phys.set_virtual_address_map = |
658 | (efi_set_virtual_address_map_t *) | |
659 | runtime->set_virtual_address_map; | |
11520e5e LT |
660 | /* |
661 | * Make efi_get_time can be called before entering | |
662 | * virtual mode. | |
663 | */ | |
664 | efi.get_time = phys_efi_get_time; | |
beacfaac | 665 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
140bf275 OJ |
666 | |
667 | return 0; | |
83e7ee66 | 668 | } |
5b83683f | 669 | |
140bf275 | 670 | static int __init efi_memmap_init(void) |
83e7ee66 | 671 | { |
5b83683f | 672 | /* Map the EFI memory map */ |
beacfaac HY |
673 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
674 | memmap.nr_map * memmap.desc_size); | |
140bf275 | 675 | if (memmap.map == NULL) { |
e3cb3f5a | 676 | pr_err("Could not map the memory map!\n"); |
140bf275 OJ |
677 | return -ENOMEM; |
678 | } | |
5b83683f | 679 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); |
175e438f | 680 | |
200001eb PJ |
681 | if (add_efi_memmap) |
682 | do_add_efi_memmap(); | |
140bf275 OJ |
683 | |
684 | return 0; | |
83e7ee66 OJ |
685 | } |
686 | ||
687 | void __init efi_init(void) | |
688 | { | |
689 | efi_char16_t *c16; | |
690 | char vendor[100] = "unknown"; | |
691 | int i = 0; | |
692 | void *tmp; | |
693 | ||
694 | #ifdef CONFIG_X86_32 | |
1adbfa35 OJ |
695 | if (boot_params.efi_info.efi_systab_hi || |
696 | boot_params.efi_info.efi_memmap_hi) { | |
697 | pr_info("Table located above 4GB, disabling EFI.\n"); | |
1adbfa35 OJ |
698 | return; |
699 | } | |
83e7ee66 OJ |
700 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
701 | #else | |
702 | efi_phys.systab = (efi_system_table_t *) | |
1adbfa35 OJ |
703 | (boot_params.efi_info.efi_systab | |
704 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); | |
83e7ee66 OJ |
705 | #endif |
706 | ||
83e68189 | 707 | if (efi_systab_init(efi_phys.systab)) |
140bf275 | 708 | return; |
83e68189 MF |
709 | |
710 | set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility); | |
83e7ee66 OJ |
711 | |
712 | /* | |
713 | * Show what we know for posterity | |
714 | */ | |
715 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); | |
716 | if (c16) { | |
717 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) | |
718 | vendor[i] = *c16++; | |
719 | vendor[i] = '\0'; | |
720 | } else | |
e3cb3f5a | 721 | pr_err("Could not map the firmware vendor!\n"); |
83e7ee66 OJ |
722 | early_iounmap(tmp, 2); |
723 | ||
e3cb3f5a OJ |
724 | pr_info("EFI v%u.%.02u by %s\n", |
725 | efi.systab->hdr.revision >> 16, | |
726 | efi.systab->hdr.revision & 0xffff, vendor); | |
83e7ee66 | 727 | |
83e68189 | 728 | if (efi_config_init(efi.systab->tables, efi.systab->nr_tables)) |
140bf275 | 729 | return; |
83e68189 MF |
730 | |
731 | set_bit(EFI_CONFIG_TABLES, &x86_efi_facility); | |
83e7ee66 | 732 | |
1adbfa35 OJ |
733 | /* |
734 | * Note: We currently don't support runtime services on an EFI | |
735 | * that doesn't match the kernel 32/64-bit mode. | |
736 | */ | |
737 | ||
5189c2a7 | 738 | if (!efi_is_native()) |
1adbfa35 | 739 | pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n"); |
83e68189 | 740 | else { |
fb834c7a | 741 | if (disable_runtime || efi_runtime_init()) |
83e68189 MF |
742 | return; |
743 | set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility); | |
140bf275 | 744 | } |
83e7ee66 | 745 | |
83e68189 | 746 | if (efi_memmap_init()) |
140bf275 | 747 | return; |
83e68189 MF |
748 | |
749 | set_bit(EFI_MEMMAP, &x86_efi_facility); | |
750 | ||
11520e5e | 751 | #ifdef CONFIG_X86_32 |
5189c2a7 | 752 | if (efi_is_native()) { |
1adbfa35 OJ |
753 | x86_platform.get_wallclock = efi_get_time; |
754 | x86_platform.set_wallclock = efi_set_rtc_mmss; | |
755 | } | |
11520e5e | 756 | #endif |
7bd867df | 757 | |
5b83683f HY |
758 | #if EFI_DEBUG |
759 | print_efi_memmap(); | |
760 | #endif | |
761 | } | |
762 | ||
2223af38 JT |
763 | void __init efi_late_init(void) |
764 | { | |
765 | efi_bgrt_init(); | |
766 | } | |
767 | ||
9cd2b07c MG |
768 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
769 | { | |
770 | u64 addr, npages; | |
771 | ||
772 | addr = md->virt_addr; | |
773 | npages = md->num_pages; | |
774 | ||
775 | memrange_efi_to_native(&addr, &npages); | |
776 | ||
777 | if (executable) | |
778 | set_memory_x(addr, npages); | |
779 | else | |
780 | set_memory_nx(addr, npages); | |
781 | } | |
782 | ||
a2172e25 HY |
783 | static void __init runtime_code_page_mkexec(void) |
784 | { | |
785 | efi_memory_desc_t *md; | |
a2172e25 HY |
786 | void *p; |
787 | ||
a2172e25 HY |
788 | /* Make EFI runtime service code area executable */ |
789 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
790 | md = p; | |
1c083eb2 HY |
791 | |
792 | if (md->type != EFI_RUNTIME_SERVICES_CODE) | |
793 | continue; | |
794 | ||
9cd2b07c | 795 | efi_set_executable(md, true); |
a2172e25 | 796 | } |
a2172e25 | 797 | } |
a2172e25 | 798 | |
7bc90e01 JT |
799 | /* |
800 | * We can't ioremap data in EFI boot services RAM, because we've already mapped | |
801 | * it as RAM. So, look it up in the existing EFI memory map instead. Only | |
802 | * callable after efi_enter_virtual_mode and before efi_free_boot_services. | |
803 | */ | |
804 | void __iomem *efi_lookup_mapped_addr(u64 phys_addr) | |
805 | { | |
806 | void *p; | |
807 | if (WARN_ON(!memmap.map)) | |
808 | return NULL; | |
809 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
810 | efi_memory_desc_t *md = p; | |
811 | u64 size = md->num_pages << EFI_PAGE_SHIFT; | |
812 | u64 end = md->phys_addr + size; | |
813 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && | |
814 | md->type != EFI_BOOT_SERVICES_CODE && | |
815 | md->type != EFI_BOOT_SERVICES_DATA) | |
816 | continue; | |
817 | if (!md->virt_addr) | |
818 | continue; | |
819 | if (phys_addr >= md->phys_addr && phys_addr < end) { | |
820 | phys_addr += md->virt_addr - md->phys_addr; | |
821 | return (__force void __iomem *)(unsigned long)phys_addr; | |
822 | } | |
823 | } | |
824 | return NULL; | |
825 | } | |
826 | ||
3e8fa263 MF |
827 | void efi_memory_uc(u64 addr, unsigned long size) |
828 | { | |
829 | unsigned long page_shift = 1UL << EFI_PAGE_SHIFT; | |
830 | u64 npages; | |
831 | ||
832 | npages = round_up(size, page_shift) / page_shift; | |
833 | memrange_efi_to_native(&addr, &npages); | |
834 | set_memory_uc(addr, npages); | |
835 | } | |
836 | ||
5b83683f HY |
837 | /* |
838 | * This function will switch the EFI runtime services to virtual mode. | |
839 | * Essentially, look through the EFI memmap and map every region that | |
840 | * has the runtime attribute bit set in its memory descriptor and update | |
841 | * that memory descriptor with the virtual address obtained from ioremap(). | |
842 | * This enables the runtime services to be called without having to | |
843 | * thunk back into physical mode for every invocation. | |
844 | */ | |
845 | void __init efi_enter_virtual_mode(void) | |
846 | { | |
202f9d0a | 847 | efi_memory_desc_t *md, *prev_md = NULL; |
5b83683f | 848 | efi_status_t status; |
1c083eb2 | 849 | unsigned long size; |
dda56e13 | 850 | u64 end, systab, start_pfn, end_pfn; |
7cb00b72 MG |
851 | void *p, *va, *new_memmap = NULL; |
852 | int count = 0; | |
5b83683f HY |
853 | |
854 | efi.systab = NULL; | |
202f9d0a | 855 | |
1adbfa35 OJ |
856 | /* |
857 | * We don't do virtual mode, since we don't do runtime services, on | |
858 | * non-native EFI | |
859 | */ | |
860 | ||
5189c2a7 | 861 | if (!efi_is_native()) { |
78510792 JT |
862 | efi_unmap_memmap(); |
863 | return; | |
864 | } | |
1adbfa35 | 865 | |
202f9d0a MG |
866 | /* Merge contiguous regions of the same type and attribute */ |
867 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
868 | u64 prev_size; | |
869 | md = p; | |
870 | ||
871 | if (!prev_md) { | |
872 | prev_md = md; | |
873 | continue; | |
874 | } | |
875 | ||
876 | if (prev_md->type != md->type || | |
877 | prev_md->attribute != md->attribute) { | |
878 | prev_md = md; | |
879 | continue; | |
880 | } | |
881 | ||
882 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; | |
883 | ||
884 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { | |
885 | prev_md->num_pages += md->num_pages; | |
886 | md->type = EFI_RESERVED_TYPE; | |
887 | md->attribute = 0; | |
888 | continue; | |
889 | } | |
890 | prev_md = md; | |
891 | } | |
892 | ||
5b83683f HY |
893 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
894 | md = p; | |
916f676f MG |
895 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
896 | md->type != EFI_BOOT_SERVICES_CODE && | |
897 | md->type != EFI_BOOT_SERVICES_DATA) | |
5b83683f | 898 | continue; |
1c083eb2 HY |
899 | |
900 | size = md->num_pages << EFI_PAGE_SHIFT; | |
901 | end = md->phys_addr + size; | |
902 | ||
dda56e13 | 903 | start_pfn = PFN_DOWN(md->phys_addr); |
dd39ecf5 | 904 | end_pfn = PFN_UP(end); |
dda56e13 | 905 | if (pfn_range_is_mapped(start_pfn, end_pfn)) { |
1c083eb2 | 906 | va = __va(md->phys_addr); |
3e8fa263 MF |
907 | |
908 | if (!(md->attribute & EFI_MEMORY_WB)) | |
909 | efi_memory_uc((u64)(unsigned long)va, size); | |
910 | } else | |
911 | va = efi_ioremap(md->phys_addr, size, | |
912 | md->type, md->attribute); | |
1c083eb2 | 913 | |
1c083eb2 HY |
914 | md->virt_addr = (u64) (unsigned long) va; |
915 | ||
916 | if (!va) { | |
e3cb3f5a | 917 | pr_err("ioremap of 0x%llX failed!\n", |
5b83683f | 918 | (unsigned long long)md->phys_addr); |
1c083eb2 HY |
919 | continue; |
920 | } | |
921 | ||
922 | systab = (u64) (unsigned long) efi_phys.systab; | |
923 | if (md->phys_addr <= systab && systab < end) { | |
924 | systab += md->virt_addr - md->phys_addr; | |
925 | efi.systab = (efi_system_table_t *) (unsigned long) systab; | |
926 | } | |
7cb00b72 MG |
927 | new_memmap = krealloc(new_memmap, |
928 | (count + 1) * memmap.desc_size, | |
929 | GFP_KERNEL); | |
930 | memcpy(new_memmap + (count * memmap.desc_size), md, | |
931 | memmap.desc_size); | |
932 | count++; | |
5b83683f HY |
933 | } |
934 | ||
935 | BUG_ON(!efi.systab); | |
936 | ||
937 | status = phys_efi_set_virtual_address_map( | |
7cb00b72 | 938 | memmap.desc_size * count, |
5b83683f HY |
939 | memmap.desc_size, |
940 | memmap.desc_version, | |
7cb00b72 | 941 | (efi_memory_desc_t *)__pa(new_memmap)); |
5b83683f HY |
942 | |
943 | if (status != EFI_SUCCESS) { | |
e3cb3f5a OJ |
944 | pr_alert("Unable to switch EFI into virtual mode " |
945 | "(status=%lx)!\n", status); | |
5b83683f HY |
946 | panic("EFI call to SetVirtualAddressMap() failed!"); |
947 | } | |
948 | ||
949 | /* | |
950 | * Now that EFI is in virtual mode, update the function | |
951 | * pointers in the runtime service table to the new virtual addresses. | |
952 | * | |
953 | * Call EFI services through wrapper functions. | |
954 | */ | |
712ba9e9 | 955 | efi.runtime_version = efi_systab.hdr.revision; |
5b83683f HY |
956 | efi.get_time = virt_efi_get_time; |
957 | efi.set_time = virt_efi_set_time; | |
958 | efi.get_wakeup_time = virt_efi_get_wakeup_time; | |
959 | efi.set_wakeup_time = virt_efi_set_wakeup_time; | |
960 | efi.get_variable = virt_efi_get_variable; | |
961 | efi.get_next_variable = virt_efi_get_next_variable; | |
962 | efi.set_variable = virt_efi_set_variable; | |
963 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; | |
964 | efi.reset_system = virt_efi_reset_system; | |
2b5e8ef3 | 965 | efi.set_virtual_address_map = NULL; |
3b370237 MG |
966 | efi.query_variable_info = virt_efi_query_variable_info; |
967 | efi.update_capsule = virt_efi_update_capsule; | |
968 | efi.query_capsule_caps = virt_efi_query_capsule_caps; | |
4de0d4a6 HY |
969 | if (__supported_pte_mask & _PAGE_NX) |
970 | runtime_code_page_mkexec(); | |
1adbfa35 | 971 | |
7cb00b72 | 972 | kfree(new_memmap); |
5b83683f HY |
973 | } |
974 | ||
975 | /* | |
976 | * Convenience functions to obtain memory types and attributes | |
977 | */ | |
978 | u32 efi_mem_type(unsigned long phys_addr) | |
979 | { | |
980 | efi_memory_desc_t *md; | |
981 | void *p; | |
982 | ||
83e68189 MF |
983 | if (!efi_enabled(EFI_MEMMAP)) |
984 | return 0; | |
985 | ||
5b83683f HY |
986 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
987 | md = p; | |
988 | if ((md->phys_addr <= phys_addr) && | |
989 | (phys_addr < (md->phys_addr + | |
990 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
991 | return md->type; | |
992 | } | |
993 | return 0; | |
994 | } | |
995 | ||
996 | u64 efi_mem_attributes(unsigned long phys_addr) | |
997 | { | |
998 | efi_memory_desc_t *md; | |
999 | void *p; | |
1000 | ||
1001 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
1002 | md = p; | |
1003 | if ((md->phys_addr <= phys_addr) && | |
1004 | (phys_addr < (md->phys_addr + | |
1005 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
1006 | return md->attribute; | |
1007 | } | |
1008 | return 0; | |
1009 | } |