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 | ||
29 | #include <linux/kernel.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/efi.h> | |
32 | #include <linux/bootmem.h> | |
a9ce6bc1 | 33 | #include <linux/memblock.h> |
5b83683f HY |
34 | #include <linux/spinlock.h> |
35 | #include <linux/uaccess.h> | |
36 | #include <linux/time.h> | |
37 | #include <linux/io.h> | |
38 | #include <linux/reboot.h> | |
39 | #include <linux/bcd.h> | |
40 | ||
41 | #include <asm/setup.h> | |
42 | #include <asm/efi.h> | |
43 | #include <asm/time.h> | |
a2172e25 HY |
44 | #include <asm/cacheflush.h> |
45 | #include <asm/tlbflush.h> | |
7bd867df | 46 | #include <asm/x86_init.h> |
5b83683f HY |
47 | |
48 | #define EFI_DEBUG 1 | |
49 | #define PFX "EFI: " | |
50 | ||
51 | int efi_enabled; | |
52 | EXPORT_SYMBOL(efi_enabled); | |
53 | ||
d80603c9 JB |
54 | struct efi __read_mostly efi = { |
55 | .mps = EFI_INVALID_TABLE_ADDR, | |
56 | .acpi = EFI_INVALID_TABLE_ADDR, | |
57 | .acpi20 = EFI_INVALID_TABLE_ADDR, | |
58 | .smbios = EFI_INVALID_TABLE_ADDR, | |
59 | .sal_systab = EFI_INVALID_TABLE_ADDR, | |
60 | .boot_info = EFI_INVALID_TABLE_ADDR, | |
61 | .hcdp = EFI_INVALID_TABLE_ADDR, | |
62 | .uga = EFI_INVALID_TABLE_ADDR, | |
63 | .uv_systab = EFI_INVALID_TABLE_ADDR, | |
64 | }; | |
5b83683f HY |
65 | EXPORT_SYMBOL(efi); |
66 | ||
67 | struct efi_memory_map memmap; | |
68 | ||
ecaea42e | 69 | static struct efi efi_phys __initdata; |
5b83683f HY |
70 | static efi_system_table_t efi_systab __initdata; |
71 | ||
8b2cb7a8 HY |
72 | static int __init setup_noefi(char *arg) |
73 | { | |
74 | efi_enabled = 0; | |
75 | return 0; | |
76 | } | |
77 | early_param("noefi", setup_noefi); | |
78 | ||
200001eb PJ |
79 | int add_efi_memmap; |
80 | EXPORT_SYMBOL(add_efi_memmap); | |
81 | ||
82 | static int __init setup_add_efi_memmap(char *arg) | |
83 | { | |
84 | add_efi_memmap = 1; | |
85 | return 0; | |
86 | } | |
87 | early_param("add_efi_memmap", setup_add_efi_memmap); | |
88 | ||
89 | ||
5b83683f HY |
90 | static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc) |
91 | { | |
92 | return efi_call_virt2(get_time, tm, tc); | |
93 | } | |
94 | ||
95 | static efi_status_t virt_efi_set_time(efi_time_t *tm) | |
96 | { | |
97 | return efi_call_virt1(set_time, tm); | |
98 | } | |
99 | ||
100 | static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled, | |
101 | efi_bool_t *pending, | |
102 | efi_time_t *tm) | |
103 | { | |
104 | return efi_call_virt3(get_wakeup_time, | |
105 | enabled, pending, tm); | |
106 | } | |
107 | ||
108 | static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) | |
109 | { | |
110 | return efi_call_virt2(set_wakeup_time, | |
111 | enabled, tm); | |
112 | } | |
113 | ||
114 | static efi_status_t virt_efi_get_variable(efi_char16_t *name, | |
115 | efi_guid_t *vendor, | |
116 | u32 *attr, | |
117 | unsigned long *data_size, | |
118 | void *data) | |
119 | { | |
120 | return efi_call_virt5(get_variable, | |
121 | name, vendor, attr, | |
122 | data_size, data); | |
123 | } | |
124 | ||
125 | static efi_status_t virt_efi_get_next_variable(unsigned long *name_size, | |
126 | efi_char16_t *name, | |
127 | efi_guid_t *vendor) | |
128 | { | |
129 | return efi_call_virt3(get_next_variable, | |
130 | name_size, name, vendor); | |
131 | } | |
132 | ||
133 | static efi_status_t virt_efi_set_variable(efi_char16_t *name, | |
134 | efi_guid_t *vendor, | |
f7a2d73f | 135 | u32 attr, |
5b83683f HY |
136 | unsigned long data_size, |
137 | void *data) | |
138 | { | |
139 | return efi_call_virt5(set_variable, | |
140 | name, vendor, attr, | |
141 | data_size, data); | |
142 | } | |
143 | ||
3b370237 MG |
144 | static efi_status_t virt_efi_query_variable_info(u32 attr, |
145 | u64 *storage_space, | |
146 | u64 *remaining_space, | |
147 | u64 *max_variable_size) | |
148 | { | |
149 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
150 | return EFI_UNSUPPORTED; | |
151 | ||
152 | return efi_call_virt4(query_variable_info, attr, storage_space, | |
153 | remaining_space, max_variable_size); | |
154 | } | |
155 | ||
5b83683f HY |
156 | static efi_status_t virt_efi_get_next_high_mono_count(u32 *count) |
157 | { | |
158 | return efi_call_virt1(get_next_high_mono_count, count); | |
159 | } | |
160 | ||
161 | static void virt_efi_reset_system(int reset_type, | |
162 | efi_status_t status, | |
163 | unsigned long data_size, | |
164 | efi_char16_t *data) | |
165 | { | |
166 | efi_call_virt4(reset_system, reset_type, status, | |
167 | data_size, data); | |
168 | } | |
169 | ||
3b370237 MG |
170 | static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules, |
171 | unsigned long count, | |
172 | unsigned long sg_list) | |
173 | { | |
174 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
175 | return EFI_UNSUPPORTED; | |
176 | ||
177 | return efi_call_virt3(update_capsule, capsules, count, sg_list); | |
178 | } | |
179 | ||
180 | static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules, | |
181 | unsigned long count, | |
182 | u64 *max_size, | |
183 | int *reset_type) | |
184 | { | |
185 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
186 | return EFI_UNSUPPORTED; | |
187 | ||
188 | return efi_call_virt4(query_capsule_caps, capsules, count, max_size, | |
189 | reset_type); | |
190 | } | |
191 | ||
5b83683f HY |
192 | static efi_status_t __init phys_efi_set_virtual_address_map( |
193 | unsigned long memory_map_size, | |
194 | unsigned long descriptor_size, | |
195 | u32 descriptor_version, | |
196 | efi_memory_desc_t *virtual_map) | |
197 | { | |
198 | efi_status_t status; | |
199 | ||
200 | efi_call_phys_prelog(); | |
201 | status = efi_call_phys4(efi_phys.set_virtual_address_map, | |
202 | memory_map_size, descriptor_size, | |
203 | descriptor_version, virtual_map); | |
204 | efi_call_phys_epilog(); | |
205 | return status; | |
206 | } | |
207 | ||
208 | static efi_status_t __init phys_efi_get_time(efi_time_t *tm, | |
209 | efi_time_cap_t *tc) | |
210 | { | |
211 | efi_status_t status; | |
212 | ||
213 | efi_call_phys_prelog(); | |
214 | status = efi_call_phys2(efi_phys.get_time, tm, tc); | |
215 | efi_call_phys_epilog(); | |
216 | return status; | |
217 | } | |
218 | ||
219 | int efi_set_rtc_mmss(unsigned long nowtime) | |
220 | { | |
221 | int real_seconds, real_minutes; | |
222 | efi_status_t status; | |
223 | efi_time_t eft; | |
224 | efi_time_cap_t cap; | |
225 | ||
226 | status = efi.get_time(&eft, &cap); | |
227 | if (status != EFI_SUCCESS) { | |
228 | printk(KERN_ERR "Oops: efitime: can't read time!\n"); | |
229 | return -1; | |
230 | } | |
231 | ||
232 | real_seconds = nowtime % 60; | |
233 | real_minutes = nowtime / 60; | |
234 | if (((abs(real_minutes - eft.minute) + 15)/30) & 1) | |
235 | real_minutes += 30; | |
236 | real_minutes %= 60; | |
237 | eft.minute = real_minutes; | |
238 | eft.second = real_seconds; | |
239 | ||
240 | status = efi.set_time(&eft); | |
241 | if (status != EFI_SUCCESS) { | |
242 | printk(KERN_ERR "Oops: efitime: can't write time!\n"); | |
243 | return -1; | |
244 | } | |
245 | return 0; | |
246 | } | |
247 | ||
248 | unsigned long efi_get_time(void) | |
249 | { | |
250 | efi_status_t status; | |
251 | efi_time_t eft; | |
252 | efi_time_cap_t cap; | |
253 | ||
254 | status = efi.get_time(&eft, &cap); | |
255 | if (status != EFI_SUCCESS) | |
256 | printk(KERN_ERR "Oops: efitime: can't read time!\n"); | |
257 | ||
258 | return mktime(eft.year, eft.month, eft.day, eft.hour, | |
259 | eft.minute, eft.second); | |
260 | } | |
261 | ||
69c91893 PJ |
262 | /* |
263 | * Tell the kernel about the EFI memory map. This might include | |
264 | * more than the max 128 entries that can fit in the e820 legacy | |
265 | * (zeropage) memory map. | |
266 | */ | |
267 | ||
200001eb | 268 | static void __init do_add_efi_memmap(void) |
69c91893 PJ |
269 | { |
270 | void *p; | |
271 | ||
272 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
273 | efi_memory_desc_t *md = p; | |
274 | unsigned long long start = md->phys_addr; | |
275 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
276 | int e820_type; | |
277 | ||
e2a71476 CW |
278 | switch (md->type) { |
279 | case EFI_LOADER_CODE: | |
280 | case EFI_LOADER_DATA: | |
281 | case EFI_BOOT_SERVICES_CODE: | |
282 | case EFI_BOOT_SERVICES_DATA: | |
283 | case EFI_CONVENTIONAL_MEMORY: | |
284 | if (md->attribute & EFI_MEMORY_WB) | |
285 | e820_type = E820_RAM; | |
286 | else | |
287 | e820_type = E820_RESERVED; | |
288 | break; | |
289 | case EFI_ACPI_RECLAIM_MEMORY: | |
290 | e820_type = E820_ACPI; | |
291 | break; | |
292 | case EFI_ACPI_MEMORY_NVS: | |
293 | e820_type = E820_NVS; | |
294 | break; | |
295 | case EFI_UNUSABLE_MEMORY: | |
296 | e820_type = E820_UNUSABLE; | |
297 | break; | |
298 | default: | |
299 | /* | |
300 | * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE | |
301 | * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO | |
302 | * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE | |
303 | */ | |
69c91893 | 304 | e820_type = E820_RESERVED; |
e2a71476 CW |
305 | break; |
306 | } | |
d0be6bde | 307 | e820_add_region(start, size, e820_type); |
69c91893 PJ |
308 | } |
309 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); | |
310 | } | |
311 | ||
a9ce6bc1 | 312 | void __init efi_memblock_x86_reserve_range(void) |
ecacf09f HY |
313 | { |
314 | unsigned long pmap; | |
315 | ||
05486fa7 | 316 | #ifdef CONFIG_X86_32 |
ecacf09f | 317 | pmap = boot_params.efi_info.efi_memmap; |
05486fa7 PJ |
318 | #else |
319 | pmap = (boot_params.efi_info.efi_memmap | | |
320 | ((__u64)boot_params.efi_info.efi_memmap_hi<<32)); | |
ecacf09f HY |
321 | #endif |
322 | memmap.phys_map = (void *)pmap; | |
323 | memmap.nr_map = boot_params.efi_info.efi_memmap_size / | |
324 | boot_params.efi_info.efi_memdesc_size; | |
325 | memmap.desc_version = boot_params.efi_info.efi_memdesc_version; | |
326 | memmap.desc_size = boot_params.efi_info.efi_memdesc_size; | |
a9ce6bc1 | 327 | memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size, |
ecacf09f HY |
328 | "EFI memmap"); |
329 | } | |
330 | ||
5b83683f HY |
331 | #if EFI_DEBUG |
332 | static void __init print_efi_memmap(void) | |
333 | { | |
334 | efi_memory_desc_t *md; | |
335 | void *p; | |
336 | int i; | |
337 | ||
338 | for (p = memmap.map, i = 0; | |
339 | p < memmap.map_end; | |
340 | p += memmap.desc_size, i++) { | |
341 | md = p; | |
342 | printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, " | |
343 | "range=[0x%016llx-0x%016llx) (%lluMB)\n", | |
344 | i, md->type, md->attribute, md->phys_addr, | |
345 | md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT), | |
346 | (md->num_pages >> (20 - EFI_PAGE_SHIFT))); | |
347 | } | |
348 | } | |
349 | #endif /* EFI_DEBUG */ | |
350 | ||
916f676f MG |
351 | void __init efi_reserve_boot_services(void) |
352 | { | |
353 | void *p; | |
354 | ||
355 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
356 | efi_memory_desc_t *md = p; | |
357 | unsigned long long start = md->phys_addr; | |
358 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
359 | ||
360 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
361 | md->type != EFI_BOOT_SERVICES_DATA) | |
362 | continue; | |
363 | ||
364 | memblock_x86_reserve_range(start, start + size, "EFI Boot"); | |
365 | } | |
366 | } | |
367 | ||
368 | static void __init efi_free_boot_services(void) | |
369 | { | |
370 | void *p; | |
371 | ||
372 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
373 | efi_memory_desc_t *md = p; | |
374 | unsigned long long start = md->phys_addr; | |
375 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
376 | ||
377 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
378 | md->type != EFI_BOOT_SERVICES_DATA) | |
379 | continue; | |
380 | ||
381 | free_bootmem_late(start, size); | |
382 | } | |
383 | } | |
384 | ||
5b83683f HY |
385 | void __init efi_init(void) |
386 | { | |
387 | efi_config_table_t *config_tables; | |
388 | efi_runtime_services_t *runtime; | |
389 | efi_char16_t *c16; | |
390 | char vendor[100] = "unknown"; | |
391 | int i = 0; | |
392 | void *tmp; | |
393 | ||
05486fa7 | 394 | #ifdef CONFIG_X86_32 |
5b83683f | 395 | efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab; |
05486fa7 PJ |
396 | #else |
397 | efi_phys.systab = (efi_system_table_t *) | |
398 | (boot_params.efi_info.efi_systab | | |
399 | ((__u64)boot_params.efi_info.efi_systab_hi<<32)); | |
5b83683f | 400 | #endif |
5b83683f | 401 | |
beacfaac HY |
402 | efi.systab = early_ioremap((unsigned long)efi_phys.systab, |
403 | sizeof(efi_system_table_t)); | |
5b83683f HY |
404 | if (efi.systab == NULL) |
405 | printk(KERN_ERR "Couldn't map the EFI system table!\n"); | |
406 | memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t)); | |
beacfaac | 407 | early_iounmap(efi.systab, sizeof(efi_system_table_t)); |
5b83683f HY |
408 | efi.systab = &efi_systab; |
409 | ||
410 | /* | |
411 | * Verify the EFI Table | |
412 | */ | |
413 | if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) | |
414 | printk(KERN_ERR "EFI system table signature incorrect!\n"); | |
415 | if ((efi.systab->hdr.revision >> 16) == 0) | |
416 | printk(KERN_ERR "Warning: EFI system table version " | |
417 | "%d.%02d, expected 1.00 or greater!\n", | |
418 | efi.systab->hdr.revision >> 16, | |
419 | efi.systab->hdr.revision & 0xffff); | |
420 | ||
421 | /* | |
422 | * Show what we know for posterity | |
423 | */ | |
beacfaac | 424 | c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2); |
5b83683f | 425 | if (c16) { |
fdb8a427 | 426 | for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i) |
5b83683f HY |
427 | vendor[i] = *c16++; |
428 | vendor[i] = '\0'; | |
429 | } else | |
430 | printk(KERN_ERR PFX "Could not map the firmware vendor!\n"); | |
beacfaac | 431 | early_iounmap(tmp, 2); |
5b83683f | 432 | |
3235dc3f | 433 | printk(KERN_INFO "EFI v%u.%.02u by %s\n", |
5b83683f HY |
434 | efi.systab->hdr.revision >> 16, |
435 | efi.systab->hdr.revision & 0xffff, vendor); | |
436 | ||
437 | /* | |
438 | * Let's see what config tables the firmware passed to us. | |
439 | */ | |
beacfaac | 440 | config_tables = early_ioremap( |
5b83683f HY |
441 | efi.systab->tables, |
442 | efi.systab->nr_tables * sizeof(efi_config_table_t)); | |
443 | if (config_tables == NULL) | |
444 | printk(KERN_ERR "Could not map EFI Configuration Table!\n"); | |
445 | ||
446 | printk(KERN_INFO); | |
447 | for (i = 0; i < efi.systab->nr_tables; i++) { | |
448 | if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) { | |
449 | efi.mps = config_tables[i].table; | |
450 | printk(" MPS=0x%lx ", config_tables[i].table); | |
451 | } else if (!efi_guidcmp(config_tables[i].guid, | |
452 | ACPI_20_TABLE_GUID)) { | |
453 | efi.acpi20 = config_tables[i].table; | |
454 | printk(" ACPI 2.0=0x%lx ", config_tables[i].table); | |
455 | } else if (!efi_guidcmp(config_tables[i].guid, | |
456 | ACPI_TABLE_GUID)) { | |
457 | efi.acpi = config_tables[i].table; | |
458 | printk(" ACPI=0x%lx ", config_tables[i].table); | |
459 | } else if (!efi_guidcmp(config_tables[i].guid, | |
460 | SMBIOS_TABLE_GUID)) { | |
461 | efi.smbios = config_tables[i].table; | |
462 | printk(" SMBIOS=0x%lx ", config_tables[i].table); | |
03b48632 | 463 | #ifdef CONFIG_X86_UV |
a50f70b1 RA |
464 | } else if (!efi_guidcmp(config_tables[i].guid, |
465 | UV_SYSTEM_TABLE_GUID)) { | |
466 | efi.uv_systab = config_tables[i].table; | |
467 | printk(" UVsystab=0x%lx ", config_tables[i].table); | |
03b48632 | 468 | #endif |
5b83683f HY |
469 | } else if (!efi_guidcmp(config_tables[i].guid, |
470 | HCDP_TABLE_GUID)) { | |
471 | efi.hcdp = config_tables[i].table; | |
472 | printk(" HCDP=0x%lx ", config_tables[i].table); | |
473 | } else if (!efi_guidcmp(config_tables[i].guid, | |
474 | UGA_IO_PROTOCOL_GUID)) { | |
475 | efi.uga = config_tables[i].table; | |
476 | printk(" UGA=0x%lx ", config_tables[i].table); | |
477 | } | |
478 | } | |
479 | printk("\n"); | |
beacfaac | 480 | early_iounmap(config_tables, |
5b83683f HY |
481 | efi.systab->nr_tables * sizeof(efi_config_table_t)); |
482 | ||
483 | /* | |
484 | * Check out the runtime services table. We need to map | |
485 | * the runtime services table so that we can grab the physical | |
486 | * address of several of the EFI runtime functions, needed to | |
487 | * set the firmware into virtual mode. | |
488 | */ | |
beacfaac HY |
489 | runtime = early_ioremap((unsigned long)efi.systab->runtime, |
490 | sizeof(efi_runtime_services_t)); | |
5b83683f HY |
491 | if (runtime != NULL) { |
492 | /* | |
493 | * We will only need *early* access to the following | |
494 | * two EFI runtime services before set_virtual_address_map | |
495 | * is invoked. | |
496 | */ | |
497 | efi_phys.get_time = (efi_get_time_t *)runtime->get_time; | |
498 | efi_phys.set_virtual_address_map = | |
499 | (efi_set_virtual_address_map_t *) | |
500 | runtime->set_virtual_address_map; | |
501 | /* | |
502 | * Make efi_get_time can be called before entering | |
503 | * virtual mode. | |
504 | */ | |
505 | efi.get_time = phys_efi_get_time; | |
506 | } else | |
507 | printk(KERN_ERR "Could not map the EFI runtime service " | |
508 | "table!\n"); | |
beacfaac | 509 | early_iounmap(runtime, sizeof(efi_runtime_services_t)); |
5b83683f HY |
510 | |
511 | /* Map the EFI memory map */ | |
beacfaac HY |
512 | memmap.map = early_ioremap((unsigned long)memmap.phys_map, |
513 | memmap.nr_map * memmap.desc_size); | |
5b83683f HY |
514 | if (memmap.map == NULL) |
515 | printk(KERN_ERR "Could not map the EFI memory map!\n"); | |
516 | memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size); | |
175e438f | 517 | |
5b83683f | 518 | if (memmap.desc_size != sizeof(efi_memory_desc_t)) |
175e438f RA |
519 | printk(KERN_WARNING |
520 | "Kernel-defined memdesc doesn't match the one from EFI!\n"); | |
521 | ||
200001eb PJ |
522 | if (add_efi_memmap) |
523 | do_add_efi_memmap(); | |
5b83683f | 524 | |
772be899 | 525 | #ifdef CONFIG_X86_32 |
7bd867df FT |
526 | x86_platform.get_wallclock = efi_get_time; |
527 | x86_platform.set_wallclock = efi_set_rtc_mmss; | |
772be899 | 528 | #endif |
7bd867df | 529 | |
5b83683f HY |
530 | /* Setup for EFI runtime service */ |
531 | reboot_type = BOOT_EFI; | |
532 | ||
5b83683f HY |
533 | #if EFI_DEBUG |
534 | print_efi_memmap(); | |
535 | #endif | |
536 | } | |
537 | ||
9cd2b07c MG |
538 | void __init efi_set_executable(efi_memory_desc_t *md, bool executable) |
539 | { | |
540 | u64 addr, npages; | |
541 | ||
542 | addr = md->virt_addr; | |
543 | npages = md->num_pages; | |
544 | ||
545 | memrange_efi_to_native(&addr, &npages); | |
546 | ||
547 | if (executable) | |
548 | set_memory_x(addr, npages); | |
549 | else | |
550 | set_memory_nx(addr, npages); | |
551 | } | |
552 | ||
a2172e25 HY |
553 | static void __init runtime_code_page_mkexec(void) |
554 | { | |
555 | efi_memory_desc_t *md; | |
a2172e25 HY |
556 | void *p; |
557 | ||
a2172e25 HY |
558 | /* Make EFI runtime service code area executable */ |
559 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
560 | md = p; | |
1c083eb2 HY |
561 | |
562 | if (md->type != EFI_RUNTIME_SERVICES_CODE) | |
563 | continue; | |
564 | ||
9cd2b07c | 565 | efi_set_executable(md, true); |
a2172e25 | 566 | } |
a2172e25 | 567 | } |
a2172e25 | 568 | |
5b83683f HY |
569 | /* |
570 | * This function will switch the EFI runtime services to virtual mode. | |
571 | * Essentially, look through the EFI memmap and map every region that | |
572 | * has the runtime attribute bit set in its memory descriptor and update | |
573 | * that memory descriptor with the virtual address obtained from ioremap(). | |
574 | * This enables the runtime services to be called without having to | |
575 | * thunk back into physical mode for every invocation. | |
576 | */ | |
577 | void __init efi_enter_virtual_mode(void) | |
578 | { | |
202f9d0a | 579 | efi_memory_desc_t *md, *prev_md = NULL; |
5b83683f | 580 | efi_status_t status; |
1c083eb2 | 581 | unsigned long size; |
dd39ecf5 | 582 | u64 end, systab, addr, npages, end_pfn; |
7cb00b72 MG |
583 | void *p, *va, *new_memmap = NULL; |
584 | int count = 0; | |
5b83683f HY |
585 | |
586 | efi.systab = NULL; | |
202f9d0a MG |
587 | |
588 | /* Merge contiguous regions of the same type and attribute */ | |
589 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
590 | u64 prev_size; | |
591 | md = p; | |
592 | ||
593 | if (!prev_md) { | |
594 | prev_md = md; | |
595 | continue; | |
596 | } | |
597 | ||
598 | if (prev_md->type != md->type || | |
599 | prev_md->attribute != md->attribute) { | |
600 | prev_md = md; | |
601 | continue; | |
602 | } | |
603 | ||
604 | prev_size = prev_md->num_pages << EFI_PAGE_SHIFT; | |
605 | ||
606 | if (md->phys_addr == (prev_md->phys_addr + prev_size)) { | |
607 | prev_md->num_pages += md->num_pages; | |
608 | md->type = EFI_RESERVED_TYPE; | |
609 | md->attribute = 0; | |
610 | continue; | |
611 | } | |
612 | prev_md = md; | |
613 | } | |
614 | ||
5b83683f HY |
615 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { |
616 | md = p; | |
916f676f MG |
617 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
618 | md->type != EFI_BOOT_SERVICES_CODE && | |
619 | md->type != EFI_BOOT_SERVICES_DATA) | |
5b83683f | 620 | continue; |
1c083eb2 HY |
621 | |
622 | size = md->num_pages << EFI_PAGE_SHIFT; | |
623 | end = md->phys_addr + size; | |
624 | ||
dd39ecf5 HY |
625 | end_pfn = PFN_UP(end); |
626 | if (end_pfn <= max_low_pfn_mapped | |
627 | || (end_pfn > (1UL << (32 - PAGE_SHIFT)) | |
628 | && end_pfn <= max_pfn_mapped)) | |
1c083eb2 | 629 | va = __va(md->phys_addr); |
5b83683f | 630 | else |
6a7bbd57 | 631 | va = efi_ioremap(md->phys_addr, size, md->type); |
1c083eb2 | 632 | |
1c083eb2 HY |
633 | md->virt_addr = (u64) (unsigned long) va; |
634 | ||
635 | if (!va) { | |
5b83683f HY |
636 | printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n", |
637 | (unsigned long long)md->phys_addr); | |
1c083eb2 HY |
638 | continue; |
639 | } | |
640 | ||
4a3575fd HY |
641 | if (!(md->attribute & EFI_MEMORY_WB)) { |
642 | addr = md->virt_addr; | |
643 | npages = md->num_pages; | |
644 | memrange_efi_to_native(&addr, &npages); | |
645 | set_memory_uc(addr, npages); | |
646 | } | |
e85f2051 | 647 | |
1c083eb2 HY |
648 | systab = (u64) (unsigned long) efi_phys.systab; |
649 | if (md->phys_addr <= systab && systab < end) { | |
650 | systab += md->virt_addr - md->phys_addr; | |
651 | efi.systab = (efi_system_table_t *) (unsigned long) systab; | |
652 | } | |
7cb00b72 MG |
653 | new_memmap = krealloc(new_memmap, |
654 | (count + 1) * memmap.desc_size, | |
655 | GFP_KERNEL); | |
656 | memcpy(new_memmap + (count * memmap.desc_size), md, | |
657 | memmap.desc_size); | |
658 | count++; | |
5b83683f HY |
659 | } |
660 | ||
661 | BUG_ON(!efi.systab); | |
662 | ||
663 | status = phys_efi_set_virtual_address_map( | |
7cb00b72 | 664 | memmap.desc_size * count, |
5b83683f HY |
665 | memmap.desc_size, |
666 | memmap.desc_version, | |
7cb00b72 | 667 | (efi_memory_desc_t *)__pa(new_memmap)); |
5b83683f HY |
668 | |
669 | if (status != EFI_SUCCESS) { | |
670 | printk(KERN_ALERT "Unable to switch EFI into virtual mode " | |
671 | "(status=%lx)!\n", status); | |
672 | panic("EFI call to SetVirtualAddressMap() failed!"); | |
673 | } | |
674 | ||
916f676f MG |
675 | /* |
676 | * Thankfully, it does seem that no runtime services other than | |
677 | * SetVirtualAddressMap() will touch boot services code, so we can | |
678 | * get rid of it all at this point | |
679 | */ | |
680 | efi_free_boot_services(); | |
681 | ||
5b83683f HY |
682 | /* |
683 | * Now that EFI is in virtual mode, update the function | |
684 | * pointers in the runtime service table to the new virtual addresses. | |
685 | * | |
686 | * Call EFI services through wrapper functions. | |
687 | */ | |
688 | efi.get_time = virt_efi_get_time; | |
689 | efi.set_time = virt_efi_set_time; | |
690 | efi.get_wakeup_time = virt_efi_get_wakeup_time; | |
691 | efi.set_wakeup_time = virt_efi_set_wakeup_time; | |
692 | efi.get_variable = virt_efi_get_variable; | |
693 | efi.get_next_variable = virt_efi_get_next_variable; | |
694 | efi.set_variable = virt_efi_set_variable; | |
695 | efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count; | |
696 | efi.reset_system = virt_efi_reset_system; | |
2b5e8ef3 | 697 | efi.set_virtual_address_map = NULL; |
3b370237 MG |
698 | efi.query_variable_info = virt_efi_query_variable_info; |
699 | efi.update_capsule = virt_efi_update_capsule; | |
700 | efi.query_capsule_caps = virt_efi_query_capsule_caps; | |
4de0d4a6 HY |
701 | if (__supported_pte_mask & _PAGE_NX) |
702 | runtime_code_page_mkexec(); | |
a3828064 HY |
703 | early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); |
704 | memmap.map = NULL; | |
7cb00b72 | 705 | kfree(new_memmap); |
5b83683f HY |
706 | } |
707 | ||
708 | /* | |
709 | * Convenience functions to obtain memory types and attributes | |
710 | */ | |
711 | u32 efi_mem_type(unsigned long phys_addr) | |
712 | { | |
713 | efi_memory_desc_t *md; | |
714 | void *p; | |
715 | ||
716 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
717 | md = p; | |
718 | if ((md->phys_addr <= phys_addr) && | |
719 | (phys_addr < (md->phys_addr + | |
720 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
721 | return md->type; | |
722 | } | |
723 | return 0; | |
724 | } | |
725 | ||
726 | u64 efi_mem_attributes(unsigned long phys_addr) | |
727 | { | |
728 | efi_memory_desc_t *md; | |
729 | void *p; | |
730 | ||
731 | for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { | |
732 | md = p; | |
733 | if ((md->phys_addr <= phys_addr) && | |
734 | (phys_addr < (md->phys_addr + | |
735 | (md->num_pages << EFI_PAGE_SHIFT)))) | |
736 | return md->attribute; | |
737 | } | |
738 | return 0; | |
739 | } |