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