]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kernel/efi.c
[CPUFREQ] Document units for transition latency
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / efi.c
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>
33 #include <linux/spinlock.h>
34 #include <linux/uaccess.h>
35 #include <linux/time.h>
36 #include <linux/io.h>
37 #include <linux/reboot.h>
38 #include <linux/bcd.h>
39
40 #include <asm/setup.h>
41 #include <asm/efi.h>
42 #include <asm/time.h>
43 #include <asm/cacheflush.h>
44 #include <asm/tlbflush.h>
45 #include <asm/x86_init.h>
46
47 #define EFI_DEBUG 1
48 #define PFX "EFI: "
49
50 int efi_enabled;
51 EXPORT_SYMBOL(efi_enabled);
52
53 struct efi efi;
54 EXPORT_SYMBOL(efi);
55
56 struct efi_memory_map memmap;
57
58 static struct efi efi_phys __initdata;
59 static efi_system_table_t efi_systab __initdata;
60
61 static int __init setup_noefi(char *arg)
62 {
63 efi_enabled = 0;
64 return 0;
65 }
66 early_param("noefi", setup_noefi);
67
68 int add_efi_memmap;
69 EXPORT_SYMBOL(add_efi_memmap);
70
71 static int __init setup_add_efi_memmap(char *arg)
72 {
73 add_efi_memmap = 1;
74 return 0;
75 }
76 early_param("add_efi_memmap", setup_add_efi_memmap);
77
78
79 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
80 {
81 return efi_call_virt2(get_time, tm, tc);
82 }
83
84 static efi_status_t virt_efi_set_time(efi_time_t *tm)
85 {
86 return efi_call_virt1(set_time, tm);
87 }
88
89 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
90 efi_bool_t *pending,
91 efi_time_t *tm)
92 {
93 return efi_call_virt3(get_wakeup_time,
94 enabled, pending, tm);
95 }
96
97 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
98 {
99 return efi_call_virt2(set_wakeup_time,
100 enabled, tm);
101 }
102
103 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
104 efi_guid_t *vendor,
105 u32 *attr,
106 unsigned long *data_size,
107 void *data)
108 {
109 return efi_call_virt5(get_variable,
110 name, vendor, attr,
111 data_size, data);
112 }
113
114 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
115 efi_char16_t *name,
116 efi_guid_t *vendor)
117 {
118 return efi_call_virt3(get_next_variable,
119 name_size, name, vendor);
120 }
121
122 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
123 efi_guid_t *vendor,
124 unsigned long attr,
125 unsigned long data_size,
126 void *data)
127 {
128 return efi_call_virt5(set_variable,
129 name, vendor, attr,
130 data_size, data);
131 }
132
133 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
134 {
135 return efi_call_virt1(get_next_high_mono_count, count);
136 }
137
138 static void virt_efi_reset_system(int reset_type,
139 efi_status_t status,
140 unsigned long data_size,
141 efi_char16_t *data)
142 {
143 efi_call_virt4(reset_system, reset_type, status,
144 data_size, data);
145 }
146
147 static efi_status_t virt_efi_set_virtual_address_map(
148 unsigned long memory_map_size,
149 unsigned long descriptor_size,
150 u32 descriptor_version,
151 efi_memory_desc_t *virtual_map)
152 {
153 return efi_call_virt4(set_virtual_address_map,
154 memory_map_size, descriptor_size,
155 descriptor_version, virtual_map);
156 }
157
158 static efi_status_t __init phys_efi_set_virtual_address_map(
159 unsigned long memory_map_size,
160 unsigned long descriptor_size,
161 u32 descriptor_version,
162 efi_memory_desc_t *virtual_map)
163 {
164 efi_status_t status;
165
166 efi_call_phys_prelog();
167 status = efi_call_phys4(efi_phys.set_virtual_address_map,
168 memory_map_size, descriptor_size,
169 descriptor_version, virtual_map);
170 efi_call_phys_epilog();
171 return status;
172 }
173
174 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
175 efi_time_cap_t *tc)
176 {
177 efi_status_t status;
178
179 efi_call_phys_prelog();
180 status = efi_call_phys2(efi_phys.get_time, tm, tc);
181 efi_call_phys_epilog();
182 return status;
183 }
184
185 int efi_set_rtc_mmss(unsigned long nowtime)
186 {
187 int real_seconds, real_minutes;
188 efi_status_t status;
189 efi_time_t eft;
190 efi_time_cap_t cap;
191
192 status = efi.get_time(&eft, &cap);
193 if (status != EFI_SUCCESS) {
194 printk(KERN_ERR "Oops: efitime: can't read time!\n");
195 return -1;
196 }
197
198 real_seconds = nowtime % 60;
199 real_minutes = nowtime / 60;
200 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
201 real_minutes += 30;
202 real_minutes %= 60;
203 eft.minute = real_minutes;
204 eft.second = real_seconds;
205
206 status = efi.set_time(&eft);
207 if (status != EFI_SUCCESS) {
208 printk(KERN_ERR "Oops: efitime: can't write time!\n");
209 return -1;
210 }
211 return 0;
212 }
213
214 unsigned long efi_get_time(void)
215 {
216 efi_status_t status;
217 efi_time_t eft;
218 efi_time_cap_t cap;
219
220 status = efi.get_time(&eft, &cap);
221 if (status != EFI_SUCCESS)
222 printk(KERN_ERR "Oops: efitime: can't read time!\n");
223
224 return mktime(eft.year, eft.month, eft.day, eft.hour,
225 eft.minute, eft.second);
226 }
227
228 /*
229 * Tell the kernel about the EFI memory map. This might include
230 * more than the max 128 entries that can fit in the e820 legacy
231 * (zeropage) memory map.
232 */
233
234 static void __init do_add_efi_memmap(void)
235 {
236 void *p;
237
238 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
239 efi_memory_desc_t *md = p;
240 unsigned long long start = md->phys_addr;
241 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
242 int e820_type;
243
244 switch (md->type) {
245 case EFI_LOADER_CODE:
246 case EFI_LOADER_DATA:
247 case EFI_BOOT_SERVICES_CODE:
248 case EFI_BOOT_SERVICES_DATA:
249 case EFI_CONVENTIONAL_MEMORY:
250 if (md->attribute & EFI_MEMORY_WB)
251 e820_type = E820_RAM;
252 else
253 e820_type = E820_RESERVED;
254 break;
255 case EFI_ACPI_RECLAIM_MEMORY:
256 e820_type = E820_ACPI;
257 break;
258 case EFI_ACPI_MEMORY_NVS:
259 e820_type = E820_NVS;
260 break;
261 case EFI_UNUSABLE_MEMORY:
262 e820_type = E820_UNUSABLE;
263 break;
264 default:
265 /*
266 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
267 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
268 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
269 */
270 e820_type = E820_RESERVED;
271 break;
272 }
273 e820_add_region(start, size, e820_type);
274 }
275 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
276 }
277
278 void __init efi_reserve_early(void)
279 {
280 unsigned long pmap;
281
282 #ifdef CONFIG_X86_32
283 pmap = boot_params.efi_info.efi_memmap;
284 #else
285 pmap = (boot_params.efi_info.efi_memmap |
286 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
287 #endif
288 memmap.phys_map = (void *)pmap;
289 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
290 boot_params.efi_info.efi_memdesc_size;
291 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
292 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
293 reserve_early(pmap, pmap + memmap.nr_map * memmap.desc_size,
294 "EFI memmap");
295 }
296
297 #if EFI_DEBUG
298 static void __init print_efi_memmap(void)
299 {
300 efi_memory_desc_t *md;
301 void *p;
302 int i;
303
304 for (p = memmap.map, i = 0;
305 p < memmap.map_end;
306 p += memmap.desc_size, i++) {
307 md = p;
308 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
309 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
310 i, md->type, md->attribute, md->phys_addr,
311 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
312 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
313 }
314 }
315 #endif /* EFI_DEBUG */
316
317 void __init efi_init(void)
318 {
319 efi_config_table_t *config_tables;
320 efi_runtime_services_t *runtime;
321 efi_char16_t *c16;
322 char vendor[100] = "unknown";
323 int i = 0;
324 void *tmp;
325
326 #ifdef CONFIG_X86_32
327 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
328 #else
329 efi_phys.systab = (efi_system_table_t *)
330 (boot_params.efi_info.efi_systab |
331 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
332 #endif
333
334 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
335 sizeof(efi_system_table_t));
336 if (efi.systab == NULL)
337 printk(KERN_ERR "Couldn't map the EFI system table!\n");
338 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
339 early_iounmap(efi.systab, sizeof(efi_system_table_t));
340 efi.systab = &efi_systab;
341
342 /*
343 * Verify the EFI Table
344 */
345 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
346 printk(KERN_ERR "EFI system table signature incorrect!\n");
347 if ((efi.systab->hdr.revision >> 16) == 0)
348 printk(KERN_ERR "Warning: EFI system table version "
349 "%d.%02d, expected 1.00 or greater!\n",
350 efi.systab->hdr.revision >> 16,
351 efi.systab->hdr.revision & 0xffff);
352
353 /*
354 * Show what we know for posterity
355 */
356 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
357 if (c16) {
358 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
359 vendor[i] = *c16++;
360 vendor[i] = '\0';
361 } else
362 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
363 early_iounmap(tmp, 2);
364
365 printk(KERN_INFO "EFI v%u.%.02u by %s \n",
366 efi.systab->hdr.revision >> 16,
367 efi.systab->hdr.revision & 0xffff, vendor);
368
369 /*
370 * Let's see what config tables the firmware passed to us.
371 */
372 config_tables = early_ioremap(
373 efi.systab->tables,
374 efi.systab->nr_tables * sizeof(efi_config_table_t));
375 if (config_tables == NULL)
376 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
377
378 printk(KERN_INFO);
379 for (i = 0; i < efi.systab->nr_tables; i++) {
380 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
381 efi.mps = config_tables[i].table;
382 printk(" MPS=0x%lx ", config_tables[i].table);
383 } else if (!efi_guidcmp(config_tables[i].guid,
384 ACPI_20_TABLE_GUID)) {
385 efi.acpi20 = config_tables[i].table;
386 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
387 } else if (!efi_guidcmp(config_tables[i].guid,
388 ACPI_TABLE_GUID)) {
389 efi.acpi = config_tables[i].table;
390 printk(" ACPI=0x%lx ", config_tables[i].table);
391 } else if (!efi_guidcmp(config_tables[i].guid,
392 SMBIOS_TABLE_GUID)) {
393 efi.smbios = config_tables[i].table;
394 printk(" SMBIOS=0x%lx ", config_tables[i].table);
395 #ifdef CONFIG_X86_UV
396 } else if (!efi_guidcmp(config_tables[i].guid,
397 UV_SYSTEM_TABLE_GUID)) {
398 efi.uv_systab = config_tables[i].table;
399 printk(" UVsystab=0x%lx ", config_tables[i].table);
400 #endif
401 } else if (!efi_guidcmp(config_tables[i].guid,
402 HCDP_TABLE_GUID)) {
403 efi.hcdp = config_tables[i].table;
404 printk(" HCDP=0x%lx ", config_tables[i].table);
405 } else if (!efi_guidcmp(config_tables[i].guid,
406 UGA_IO_PROTOCOL_GUID)) {
407 efi.uga = config_tables[i].table;
408 printk(" UGA=0x%lx ", config_tables[i].table);
409 }
410 }
411 printk("\n");
412 early_iounmap(config_tables,
413 efi.systab->nr_tables * sizeof(efi_config_table_t));
414
415 /*
416 * Check out the runtime services table. We need to map
417 * the runtime services table so that we can grab the physical
418 * address of several of the EFI runtime functions, needed to
419 * set the firmware into virtual mode.
420 */
421 runtime = early_ioremap((unsigned long)efi.systab->runtime,
422 sizeof(efi_runtime_services_t));
423 if (runtime != NULL) {
424 /*
425 * We will only need *early* access to the following
426 * two EFI runtime services before set_virtual_address_map
427 * is invoked.
428 */
429 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
430 efi_phys.set_virtual_address_map =
431 (efi_set_virtual_address_map_t *)
432 runtime->set_virtual_address_map;
433 /*
434 * Make efi_get_time can be called before entering
435 * virtual mode.
436 */
437 efi.get_time = phys_efi_get_time;
438 } else
439 printk(KERN_ERR "Could not map the EFI runtime service "
440 "table!\n");
441 early_iounmap(runtime, sizeof(efi_runtime_services_t));
442
443 /* Map the EFI memory map */
444 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
445 memmap.nr_map * memmap.desc_size);
446 if (memmap.map == NULL)
447 printk(KERN_ERR "Could not map the EFI memory map!\n");
448 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
449
450 if (memmap.desc_size != sizeof(efi_memory_desc_t))
451 printk(KERN_WARNING
452 "Kernel-defined memdesc doesn't match the one from EFI!\n");
453
454 if (add_efi_memmap)
455 do_add_efi_memmap();
456
457 #ifdef CONFIG_X86_32
458 x86_platform.get_wallclock = efi_get_time;
459 x86_platform.set_wallclock = efi_set_rtc_mmss;
460 #endif
461
462 /* Setup for EFI runtime service */
463 reboot_type = BOOT_EFI;
464
465 #if EFI_DEBUG
466 print_efi_memmap();
467 #endif
468 }
469
470 static void __init runtime_code_page_mkexec(void)
471 {
472 efi_memory_desc_t *md;
473 void *p;
474 u64 addr, npages;
475
476 /* Make EFI runtime service code area executable */
477 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
478 md = p;
479
480 if (md->type != EFI_RUNTIME_SERVICES_CODE)
481 continue;
482
483 addr = md->virt_addr;
484 npages = md->num_pages;
485 memrange_efi_to_native(&addr, &npages);
486 set_memory_x(addr, npages);
487 }
488 }
489
490 /*
491 * This function will switch the EFI runtime services to virtual mode.
492 * Essentially, look through the EFI memmap and map every region that
493 * has the runtime attribute bit set in its memory descriptor and update
494 * that memory descriptor with the virtual address obtained from ioremap().
495 * This enables the runtime services to be called without having to
496 * thunk back into physical mode for every invocation.
497 */
498 void __init efi_enter_virtual_mode(void)
499 {
500 efi_memory_desc_t *md;
501 efi_status_t status;
502 unsigned long size;
503 u64 end, systab, addr, npages, end_pfn;
504 void *p, *va;
505
506 efi.systab = NULL;
507 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
508 md = p;
509 if (!(md->attribute & EFI_MEMORY_RUNTIME))
510 continue;
511
512 size = md->num_pages << EFI_PAGE_SHIFT;
513 end = md->phys_addr + size;
514
515 end_pfn = PFN_UP(end);
516 if (end_pfn <= max_low_pfn_mapped
517 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
518 && end_pfn <= max_pfn_mapped))
519 va = __va(md->phys_addr);
520 else
521 va = efi_ioremap(md->phys_addr, size, md->type);
522
523 md->virt_addr = (u64) (unsigned long) va;
524
525 if (!va) {
526 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
527 (unsigned long long)md->phys_addr);
528 continue;
529 }
530
531 if (!(md->attribute & EFI_MEMORY_WB)) {
532 addr = md->virt_addr;
533 npages = md->num_pages;
534 memrange_efi_to_native(&addr, &npages);
535 set_memory_uc(addr, npages);
536 }
537
538 systab = (u64) (unsigned long) efi_phys.systab;
539 if (md->phys_addr <= systab && systab < end) {
540 systab += md->virt_addr - md->phys_addr;
541 efi.systab = (efi_system_table_t *) (unsigned long) systab;
542 }
543 }
544
545 BUG_ON(!efi.systab);
546
547 status = phys_efi_set_virtual_address_map(
548 memmap.desc_size * memmap.nr_map,
549 memmap.desc_size,
550 memmap.desc_version,
551 memmap.phys_map);
552
553 if (status != EFI_SUCCESS) {
554 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
555 "(status=%lx)!\n", status);
556 panic("EFI call to SetVirtualAddressMap() failed!");
557 }
558
559 /*
560 * Now that EFI is in virtual mode, update the function
561 * pointers in the runtime service table to the new virtual addresses.
562 *
563 * Call EFI services through wrapper functions.
564 */
565 efi.get_time = virt_efi_get_time;
566 efi.set_time = virt_efi_set_time;
567 efi.get_wakeup_time = virt_efi_get_wakeup_time;
568 efi.set_wakeup_time = virt_efi_set_wakeup_time;
569 efi.get_variable = virt_efi_get_variable;
570 efi.get_next_variable = virt_efi_get_next_variable;
571 efi.set_variable = virt_efi_set_variable;
572 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
573 efi.reset_system = virt_efi_reset_system;
574 efi.set_virtual_address_map = virt_efi_set_virtual_address_map;
575 if (__supported_pte_mask & _PAGE_NX)
576 runtime_code_page_mkexec();
577 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
578 memmap.map = NULL;
579 }
580
581 /*
582 * Convenience functions to obtain memory types and attributes
583 */
584 u32 efi_mem_type(unsigned long phys_addr)
585 {
586 efi_memory_desc_t *md;
587 void *p;
588
589 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
590 md = p;
591 if ((md->phys_addr <= phys_addr) &&
592 (phys_addr < (md->phys_addr +
593 (md->num_pages << EFI_PAGE_SHIFT))))
594 return md->type;
595 }
596 return 0;
597 }
598
599 u64 efi_mem_attributes(unsigned long phys_addr)
600 {
601 efi_memory_desc_t *md;
602 void *p;
603
604 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
605 md = p;
606 if ((md->phys_addr <= phys_addr) &&
607 (phys_addr < (md->phys_addr +
608 (md->num_pages << EFI_PAGE_SHIFT))))
609 return md->attribute;
610 }
611 return 0;
612 }