]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/i386/kernel/efi.c
[PATCH] x86: fix broken SMP boot sequence
[mirror_ubuntu-jammy-kernel.git] / arch / i386 / kernel / efi.c
1 /*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 1.0
5 *
6 * Copyright (C) 1999 VA Linux Systems
7 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8 * Copyright (C) 1999-2002 Hewlett-Packard Co.
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 * Stephane Eranian <eranian@hpl.hp.com>
11 *
12 * All EFI Runtime Services are not implemented yet as EFI only
13 * supports physical mode addressing on SoftSDV. This is to be fixed
14 * in a future version. --drummond 1999-07-20
15 *
16 * Implemented EFI runtime services and virtual mode calls. --davidm
17 *
18 * Goutham Rao: <goutham.rao@intel.com>
19 * Skip non-WB memory and ignore empty memory ranges.
20 */
21
22 #include <linux/config.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/mm.h>
26 #include <linux/types.h>
27 #include <linux/time.h>
28 #include <linux/spinlock.h>
29 #include <linux/bootmem.h>
30 #include <linux/ioport.h>
31 #include <linux/module.h>
32 #include <linux/efi.h>
33 #include <linux/kexec.h>
34
35 #include <asm/setup.h>
36 #include <asm/io.h>
37 #include <asm/page.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
40 #include <asm/desc.h>
41 #include <asm/tlbflush.h>
42
43 #define EFI_DEBUG 0
44 #define PFX "EFI: "
45
46 extern efi_status_t asmlinkage efi_call_phys(void *, ...);
47
48 struct efi efi;
49 EXPORT_SYMBOL(efi);
50 static struct efi efi_phys;
51 struct efi_memory_map memmap;
52
53 /*
54 * We require an early boot_ioremap mapping mechanism initially
55 */
56 extern void * boot_ioremap(unsigned long, unsigned long);
57
58 /*
59 * To make EFI call EFI runtime service in physical addressing mode we need
60 * prelog/epilog before/after the invocation to disable interrupt, to
61 * claim EFI runtime service handler exclusively and to duplicate a memory in
62 * low memory space say 0 - 3G.
63 */
64
65 static unsigned long efi_rt_eflags;
66 static DEFINE_SPINLOCK(efi_rt_lock);
67 static pgd_t efi_bak_pg_dir_pointer[2];
68
69 static void efi_call_phys_prelog(void)
70 {
71 unsigned long cr4;
72 unsigned long temp;
73
74 spin_lock(&efi_rt_lock);
75 local_irq_save(efi_rt_eflags);
76
77 /*
78 * If I don't have PSE, I should just duplicate two entries in page
79 * directory. If I have PSE, I just need to duplicate one entry in
80 * page directory.
81 */
82 cr4 = read_cr4();
83
84 if (cr4 & X86_CR4_PSE) {
85 efi_bak_pg_dir_pointer[0].pgd =
86 swapper_pg_dir[pgd_index(0)].pgd;
87 swapper_pg_dir[0].pgd =
88 swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
89 } else {
90 efi_bak_pg_dir_pointer[0].pgd =
91 swapper_pg_dir[pgd_index(0)].pgd;
92 efi_bak_pg_dir_pointer[1].pgd =
93 swapper_pg_dir[pgd_index(0x400000)].pgd;
94 swapper_pg_dir[pgd_index(0)].pgd =
95 swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
96 temp = PAGE_OFFSET + 0x400000;
97 swapper_pg_dir[pgd_index(0x400000)].pgd =
98 swapper_pg_dir[pgd_index(temp)].pgd;
99 }
100
101 /*
102 * After the lock is released, the original page table is restored.
103 */
104 local_flush_tlb();
105
106 per_cpu(cpu_gdt_descr, 0).address =
107 __pa(per_cpu(cpu_gdt_descr, 0).address);
108 load_gdt((struct Xgt_desc_struct *)__pa(&per_cpu(cpu_gdt_descr, 0)));
109 }
110
111 static void efi_call_phys_epilog(void)
112 {
113 unsigned long cr4;
114
115 per_cpu(cpu_gdt_descr, 0).address =
116 (unsigned long)__va(per_cpu(cpu_gdt_descr, 0).address);
117 load_gdt((struct Xgt_desc_struct *)__va(&per_cpu(cpu_gdt_descr, 0)));
118
119 cr4 = read_cr4();
120
121 if (cr4 & X86_CR4_PSE) {
122 swapper_pg_dir[pgd_index(0)].pgd =
123 efi_bak_pg_dir_pointer[0].pgd;
124 } else {
125 swapper_pg_dir[pgd_index(0)].pgd =
126 efi_bak_pg_dir_pointer[0].pgd;
127 swapper_pg_dir[pgd_index(0x400000)].pgd =
128 efi_bak_pg_dir_pointer[1].pgd;
129 }
130
131 /*
132 * After the lock is released, the original page table is restored.
133 */
134 local_flush_tlb();
135
136 local_irq_restore(efi_rt_eflags);
137 spin_unlock(&efi_rt_lock);
138 }
139
140 static efi_status_t
141 phys_efi_set_virtual_address_map(unsigned long memory_map_size,
142 unsigned long descriptor_size,
143 u32 descriptor_version,
144 efi_memory_desc_t *virtual_map)
145 {
146 efi_status_t status;
147
148 efi_call_phys_prelog();
149 status = efi_call_phys(efi_phys.set_virtual_address_map,
150 memory_map_size, descriptor_size,
151 descriptor_version, virtual_map);
152 efi_call_phys_epilog();
153 return status;
154 }
155
156 static efi_status_t
157 phys_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
158 {
159 efi_status_t status;
160
161 efi_call_phys_prelog();
162 status = efi_call_phys(efi_phys.get_time, tm, tc);
163 efi_call_phys_epilog();
164 return status;
165 }
166
167 inline int efi_set_rtc_mmss(unsigned long nowtime)
168 {
169 int real_seconds, real_minutes;
170 efi_status_t status;
171 efi_time_t eft;
172 efi_time_cap_t cap;
173
174 spin_lock(&efi_rt_lock);
175 status = efi.get_time(&eft, &cap);
176 spin_unlock(&efi_rt_lock);
177 if (status != EFI_SUCCESS)
178 panic("Ooops, efitime: can't read time!\n");
179 real_seconds = nowtime % 60;
180 real_minutes = nowtime / 60;
181
182 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
183 real_minutes += 30;
184 real_minutes %= 60;
185
186 eft.minute = real_minutes;
187 eft.second = real_seconds;
188
189 if (status != EFI_SUCCESS) {
190 printk("Ooops: efitime: can't read time!\n");
191 return -1;
192 }
193 return 0;
194 }
195 /*
196 * This should only be used during kernel init and before runtime
197 * services have been remapped, therefore, we'll need to call in physical
198 * mode. Note, this call isn't used later, so mark it __init.
199 */
200 inline unsigned long __init efi_get_time(void)
201 {
202 efi_status_t status;
203 efi_time_t eft;
204 efi_time_cap_t cap;
205
206 status = phys_efi_get_time(&eft, &cap);
207 if (status != EFI_SUCCESS)
208 printk("Oops: efitime: can't read time status: 0x%lx\n",status);
209
210 return mktime(eft.year, eft.month, eft.day, eft.hour,
211 eft.minute, eft.second);
212 }
213
214 int is_available_memory(efi_memory_desc_t * md)
215 {
216 if (!(md->attribute & EFI_MEMORY_WB))
217 return 0;
218
219 switch (md->type) {
220 case EFI_LOADER_CODE:
221 case EFI_LOADER_DATA:
222 case EFI_BOOT_SERVICES_CODE:
223 case EFI_BOOT_SERVICES_DATA:
224 case EFI_CONVENTIONAL_MEMORY:
225 return 1;
226 }
227 return 0;
228 }
229
230 /*
231 * We need to map the EFI memory map again after paging_init().
232 */
233 void __init efi_map_memmap(void)
234 {
235 memmap.map = NULL;
236
237 memmap.map = bt_ioremap((unsigned long) memmap.phys_map,
238 (memmap.nr_map * memmap.desc_size));
239 if (memmap.map == NULL)
240 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
241
242 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
243 }
244
245 #if EFI_DEBUG
246 static void __init print_efi_memmap(void)
247 {
248 efi_memory_desc_t *md;
249 void *p;
250 int i;
251
252 for (p = memmap.map, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
253 md = p;
254 printk(KERN_INFO "mem%02u: type=%u, attr=0x%llx, "
255 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
256 i, md->type, md->attribute, md->phys_addr,
257 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
258 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
259 }
260 }
261 #endif /* EFI_DEBUG */
262
263 /*
264 * Walks the EFI memory map and calls CALLBACK once for each EFI
265 * memory descriptor that has memory that is available for kernel use.
266 */
267 void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
268 {
269 int prev_valid = 0;
270 struct range {
271 unsigned long start;
272 unsigned long end;
273 } prev, curr;
274 efi_memory_desc_t *md;
275 unsigned long start, end;
276 void *p;
277
278 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
279 md = p;
280
281 if ((md->num_pages == 0) || (!is_available_memory(md)))
282 continue;
283
284 curr.start = md->phys_addr;
285 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
286
287 if (!prev_valid) {
288 prev = curr;
289 prev_valid = 1;
290 } else {
291 if (curr.start < prev.start)
292 printk(KERN_INFO PFX "Unordered memory map\n");
293 if (prev.end == curr.start)
294 prev.end = curr.end;
295 else {
296 start =
297 (unsigned long) (PAGE_ALIGN(prev.start));
298 end = (unsigned long) (prev.end & PAGE_MASK);
299 if ((end > start)
300 && (*callback) (start, end, arg) < 0)
301 return;
302 prev = curr;
303 }
304 }
305 }
306 if (prev_valid) {
307 start = (unsigned long) PAGE_ALIGN(prev.start);
308 end = (unsigned long) (prev.end & PAGE_MASK);
309 if (end > start)
310 (*callback) (start, end, arg);
311 }
312 }
313
314 void __init efi_init(void)
315 {
316 efi_config_table_t *config_tables;
317 efi_runtime_services_t *runtime;
318 efi_char16_t *c16;
319 char vendor[100] = "unknown";
320 unsigned long num_config_tables;
321 int i = 0;
322
323 memset(&efi, 0, sizeof(efi) );
324 memset(&efi_phys, 0, sizeof(efi_phys));
325
326 efi_phys.systab = EFI_SYSTAB;
327 memmap.phys_map = EFI_MEMMAP;
328 memmap.nr_map = EFI_MEMMAP_SIZE/EFI_MEMDESC_SIZE;
329 memmap.desc_version = EFI_MEMDESC_VERSION;
330 memmap.desc_size = EFI_MEMDESC_SIZE;
331
332 efi.systab = (efi_system_table_t *)
333 boot_ioremap((unsigned long) efi_phys.systab,
334 sizeof(efi_system_table_t));
335 /*
336 * Verify the EFI Table
337 */
338 if (efi.systab == NULL)
339 printk(KERN_ERR PFX "Woah! Couldn't map the EFI system table.\n");
340 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
341 printk(KERN_ERR PFX "Woah! EFI system table signature incorrect\n");
342 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
343 printk(KERN_ERR PFX
344 "Warning: EFI system table major version mismatch: "
345 "got %d.%02d, expected %d.%02d\n",
346 efi.systab->hdr.revision >> 16,
347 efi.systab->hdr.revision & 0xffff,
348 EFI_SYSTEM_TABLE_REVISION >> 16,
349 EFI_SYSTEM_TABLE_REVISION & 0xffff);
350 /*
351 * Grab some details from the system table
352 */
353 num_config_tables = efi.systab->nr_tables;
354 config_tables = (efi_config_table_t *)efi.systab->tables;
355 runtime = efi.systab->runtime;
356
357 /*
358 * Show what we know for posterity
359 */
360 c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
361 if (c16) {
362 for (i = 0; i < sizeof(vendor) && *c16; ++i)
363 vendor[i] = *c16++;
364 vendor[i] = '\0';
365 } else
366 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
367
368 printk(KERN_INFO PFX "EFI v%u.%.02u by %s \n",
369 efi.systab->hdr.revision >> 16,
370 efi.systab->hdr.revision & 0xffff, vendor);
371
372 /*
373 * Let's see what config tables the firmware passed to us.
374 */
375 config_tables = (efi_config_table_t *)
376 boot_ioremap((unsigned long) config_tables,
377 num_config_tables * sizeof(efi_config_table_t));
378
379 if (config_tables == NULL)
380 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
381
382 for (i = 0; i < num_config_tables; i++) {
383 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
384 efi.mps = (void *)config_tables[i].table;
385 printk(KERN_INFO " MPS=0x%lx ", config_tables[i].table);
386 } else
387 if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
388 efi.acpi20 = __va(config_tables[i].table);
389 printk(KERN_INFO " ACPI 2.0=0x%lx ", config_tables[i].table);
390 } else
391 if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
392 efi.acpi = __va(config_tables[i].table);
393 printk(KERN_INFO " ACPI=0x%lx ", config_tables[i].table);
394 } else
395 if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
396 efi.smbios = (void *) config_tables[i].table;
397 printk(KERN_INFO " SMBIOS=0x%lx ", config_tables[i].table);
398 } else
399 if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
400 efi.hcdp = (void *)config_tables[i].table;
401 printk(KERN_INFO " HCDP=0x%lx ", config_tables[i].table);
402 } else
403 if (efi_guidcmp(config_tables[i].guid, UGA_IO_PROTOCOL_GUID) == 0) {
404 efi.uga = (void *)config_tables[i].table;
405 printk(KERN_INFO " UGA=0x%lx ", config_tables[i].table);
406 }
407 }
408 printk("\n");
409
410 /*
411 * Check out the runtime services table. We need to map
412 * the runtime services table so that we can grab the physical
413 * address of several of the EFI runtime functions, needed to
414 * set the firmware into virtual mode.
415 */
416
417 runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
418 runtime,
419 sizeof(efi_runtime_services_t));
420 if (runtime != NULL) {
421 /*
422 * We will only need *early* access to the following
423 * two EFI runtime services before set_virtual_address_map
424 * is invoked.
425 */
426 efi_phys.get_time = (efi_get_time_t *) runtime->get_time;
427 efi_phys.set_virtual_address_map =
428 (efi_set_virtual_address_map_t *)
429 runtime->set_virtual_address_map;
430 } else
431 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
432
433 /* Map the EFI memory map for use until paging_init() */
434 memmap.map = boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
435 if (memmap.map == NULL)
436 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
437
438 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
439
440 #if EFI_DEBUG
441 print_efi_memmap();
442 #endif
443 }
444
445 static inline void __init check_range_for_systab(efi_memory_desc_t *md)
446 {
447 if (((unsigned long)md->phys_addr <= (unsigned long)efi_phys.systab) &&
448 ((unsigned long)efi_phys.systab < md->phys_addr +
449 ((unsigned long)md->num_pages << EFI_PAGE_SHIFT))) {
450 unsigned long addr;
451
452 addr = md->virt_addr - md->phys_addr +
453 (unsigned long)efi_phys.systab;
454 efi.systab = (efi_system_table_t *)addr;
455 }
456 }
457
458 /*
459 * This function will switch the EFI runtime services to virtual mode.
460 * Essentially, look through the EFI memmap and map every region that
461 * has the runtime attribute bit set in its memory descriptor and update
462 * that memory descriptor with the virtual address obtained from ioremap().
463 * This enables the runtime services to be called without having to
464 * thunk back into physical mode for every invocation.
465 */
466
467 void __init efi_enter_virtual_mode(void)
468 {
469 efi_memory_desc_t *md;
470 efi_status_t status;
471 void *p;
472
473 efi.systab = NULL;
474
475 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
476 md = p;
477
478 if (!(md->attribute & EFI_MEMORY_RUNTIME))
479 continue;
480
481 md->virt_addr = (unsigned long)ioremap(md->phys_addr,
482 md->num_pages << EFI_PAGE_SHIFT);
483 if (!(unsigned long)md->virt_addr) {
484 printk(KERN_ERR PFX "ioremap of 0x%lX failed\n",
485 (unsigned long)md->phys_addr);
486 }
487 /* update the virtual address of the EFI system table */
488 check_range_for_systab(md);
489 }
490
491 if (!efi.systab)
492 BUG();
493
494 status = phys_efi_set_virtual_address_map(
495 memmap.desc_size * memmap.nr_map,
496 memmap.desc_size,
497 memmap.desc_version,
498 memmap.phys_map);
499
500 if (status != EFI_SUCCESS) {
501 printk (KERN_ALERT "You are screwed! "
502 "Unable to switch EFI into virtual mode "
503 "(status=%lx)\n", status);
504 panic("EFI call to SetVirtualAddressMap() failed!");
505 }
506
507 /*
508 * Now that EFI is in virtual mode, update the function
509 * pointers in the runtime service table to the new virtual addresses.
510 */
511
512 efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
513 efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
514 efi.get_wakeup_time = (efi_get_wakeup_time_t *)
515 efi.systab->runtime->get_wakeup_time;
516 efi.set_wakeup_time = (efi_set_wakeup_time_t *)
517 efi.systab->runtime->set_wakeup_time;
518 efi.get_variable = (efi_get_variable_t *)
519 efi.systab->runtime->get_variable;
520 efi.get_next_variable = (efi_get_next_variable_t *)
521 efi.systab->runtime->get_next_variable;
522 efi.set_variable = (efi_set_variable_t *)
523 efi.systab->runtime->set_variable;
524 efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
525 efi.systab->runtime->get_next_high_mono_count;
526 efi.reset_system = (efi_reset_system_t *)
527 efi.systab->runtime->reset_system;
528 }
529
530 void __init
531 efi_initialize_iomem_resources(struct resource *code_resource,
532 struct resource *data_resource)
533 {
534 struct resource *res;
535 efi_memory_desc_t *md;
536 void *p;
537
538 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
539 md = p;
540
541 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
542 0x100000000ULL)
543 continue;
544 res = alloc_bootmem_low(sizeof(struct resource));
545 switch (md->type) {
546 case EFI_RESERVED_TYPE:
547 res->name = "Reserved Memory";
548 break;
549 case EFI_LOADER_CODE:
550 res->name = "Loader Code";
551 break;
552 case EFI_LOADER_DATA:
553 res->name = "Loader Data";
554 break;
555 case EFI_BOOT_SERVICES_DATA:
556 res->name = "BootServices Data";
557 break;
558 case EFI_BOOT_SERVICES_CODE:
559 res->name = "BootServices Code";
560 break;
561 case EFI_RUNTIME_SERVICES_CODE:
562 res->name = "Runtime Service Code";
563 break;
564 case EFI_RUNTIME_SERVICES_DATA:
565 res->name = "Runtime Service Data";
566 break;
567 case EFI_CONVENTIONAL_MEMORY:
568 res->name = "Conventional Memory";
569 break;
570 case EFI_UNUSABLE_MEMORY:
571 res->name = "Unusable Memory";
572 break;
573 case EFI_ACPI_RECLAIM_MEMORY:
574 res->name = "ACPI Reclaim";
575 break;
576 case EFI_ACPI_MEMORY_NVS:
577 res->name = "ACPI NVS";
578 break;
579 case EFI_MEMORY_MAPPED_IO:
580 res->name = "Memory Mapped IO";
581 break;
582 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
583 res->name = "Memory Mapped IO Port Space";
584 break;
585 default:
586 res->name = "Reserved";
587 break;
588 }
589 res->start = md->phys_addr;
590 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
591 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
592 if (request_resource(&iomem_resource, res) < 0)
593 printk(KERN_ERR PFX "Failed to allocate res %s : 0x%lx-0x%lx\n",
594 res->name, res->start, res->end);
595 /*
596 * We don't know which region contains kernel data so we try
597 * it repeatedly and let the resource manager test it.
598 */
599 if (md->type == EFI_CONVENTIONAL_MEMORY) {
600 request_resource(res, code_resource);
601 request_resource(res, data_resource);
602 #ifdef CONFIG_KEXEC
603 request_resource(res, &crashk_res);
604 #endif
605 }
606 }
607 }
608
609 /*
610 * Convenience functions to obtain memory types and attributes
611 */
612
613 u32 efi_mem_type(unsigned long phys_addr)
614 {
615 efi_memory_desc_t *md;
616 void *p;
617
618 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
619 md = p;
620 if ((md->phys_addr <= phys_addr) && (phys_addr <
621 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
622 return md->type;
623 }
624 return 0;
625 }
626
627 u64 efi_mem_attributes(unsigned long phys_addr)
628 {
629 efi_memory_desc_t *md;
630 void *p;
631
632 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
633 md = p;
634 if ((md->phys_addr <= phys_addr) && (phys_addr <
635 (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
636 return md->attribute;
637 }
638 return 0;
639 }