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