http://www.hht-eu.com/pls/hht/docs/F3140/bcm963xx_Speedport500V.0.09.04L.300L01.V27_c...
[bcm963xx.git] / kernel / linux / 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
34 #include <asm/setup.h>
35 #include <asm/io.h>
36 #include <asm/page.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
39 #include <asm/desc.h>
40 #include <asm/tlbflush.h>
41
42 #define EFI_DEBUG       0
43 #define PFX             "EFI: "
44
45 extern efi_status_t asmlinkage efi_call_phys(void *, ...);
46
47 struct efi efi;
48 EXPORT_SYMBOL(efi);
49 struct efi efi_phys __initdata;
50 struct efi_memory_map memmap __initdata;
51
52 /*
53  * We require an early boot_ioremap mapping mechanism initially
54  */
55 extern void * boot_ioremap(unsigned long, unsigned long);
56
57 /*
58  * To make EFI call EFI runtime service in physical addressing mode we need
59  * prelog/epilog before/after the invocation to disable interrupt, to
60  * claim EFI runtime service handler exclusively and to duplicate a memory in
61  * low memory space say 0 - 3G.
62  */
63
64 static unsigned long efi_rt_eflags;
65 static spinlock_t efi_rt_lock = SPIN_LOCK_UNLOCKED;
66 static pgd_t efi_bak_pg_dir_pointer[2];
67
68 static void efi_call_phys_prelog(void)
69 {
70         unsigned long cr4;
71         unsigned long temp;
72
73         spin_lock(&efi_rt_lock);
74         local_irq_save(efi_rt_eflags);
75
76         /*
77          * If I don't have PSE, I should just duplicate two entries in page
78          * directory. If I have PSE, I just need to duplicate one entry in
79          * page directory.
80          */
81         __asm__ __volatile__("movl %%cr4, %0":"=r"(cr4));
82
83         if (cr4 & X86_CR4_PSE) {
84                 efi_bak_pg_dir_pointer[0].pgd =
85                     swapper_pg_dir[pgd_index(0)].pgd;
86                 swapper_pg_dir[0].pgd =
87                     swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
88         } else {
89                 efi_bak_pg_dir_pointer[0].pgd =
90                     swapper_pg_dir[pgd_index(0)].pgd;
91                 efi_bak_pg_dir_pointer[1].pgd =
92                     swapper_pg_dir[pgd_index(0x400000)].pgd;
93                 swapper_pg_dir[pgd_index(0)].pgd =
94                     swapper_pg_dir[pgd_index(PAGE_OFFSET)].pgd;
95                 temp = PAGE_OFFSET + 0x400000;
96                 swapper_pg_dir[pgd_index(0x400000)].pgd =
97                     swapper_pg_dir[pgd_index(temp)].pgd;
98         }
99
100         /*
101          * After the lock is released, the original page table is restored.
102          */
103         local_flush_tlb();
104
105         cpu_gdt_descr[0].address = __pa(cpu_gdt_descr[0].address);
106         __asm__ __volatile__("lgdt %0":"=m"
107                             (*(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         __asm__ __volatile__("lgdt %0":"=m"(cpu_gdt_descr));
117         __asm__ __volatile__("movl %%cr4, %0":"=r"(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 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 = (efi_memory_desc_t *)
236                 bt_ioremap((unsigned long) memmap.phys_map,
237                         (memmap.nr_map * sizeof(efi_memory_desc_t)));
238
239         if (memmap.map == NULL)
240                 printk(KERN_ERR PFX "Could not remap the EFI memmap!\n");
241 }
242
243 void __init print_efi_memmap(void)
244 {
245         efi_memory_desc_t *md;
246         int i;
247
248         for (i = 0; i < memmap.nr_map; i++) {
249                 md = &memmap.map[i];
250                 printk(KERN_INFO "mem%02u: type=%u, attr=0x%llx, "
251                         "range=[0x%016llx-0x%016llx) (%lluMB)\n",
252                         i, md->type, md->attribute, md->phys_addr,
253                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
254                         (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
255         }
256 }
257
258 /*
259  * Walks the EFI memory map and calls CALLBACK once for each EFI
260  * memory descriptor that has memory that is available for kernel use.
261  */
262 void efi_memmap_walk(efi_freemem_callback_t callback, void *arg)
263 {
264         int prev_valid = 0;
265         struct range {
266                 unsigned long start;
267                 unsigned long end;
268         } prev, curr;
269         efi_memory_desc_t *md;
270         unsigned long start, end;
271         int i;
272
273         for (i = 0; i < memmap.nr_map; i++) {
274                 md = &memmap.map[i];
275
276                 if ((md->num_pages == 0) || (!is_available_memory(md)))
277                         continue;
278
279                 curr.start = md->phys_addr;
280                 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
281
282                 if (!prev_valid) {
283                         prev = curr;
284                         prev_valid = 1;
285                 } else {
286                         if (curr.start < prev.start)
287                                 printk(KERN_INFO PFX "Unordered memory map\n");
288                         if (prev.end == curr.start)
289                                 prev.end = curr.end;
290                         else {
291                                 start =
292                                     (unsigned long) (PAGE_ALIGN(prev.start));
293                                 end = (unsigned long) (prev.end & PAGE_MASK);
294                                 if ((end > start)
295                                     && (*callback) (start, end, arg) < 0)
296                                         return;
297                                 prev = curr;
298                         }
299                 }
300         }
301         if (prev_valid) {
302                 start = (unsigned long) PAGE_ALIGN(prev.start);
303                 end = (unsigned long) (prev.end & PAGE_MASK);
304                 if (end > start)
305                         (*callback) (start, end, arg);
306         }
307 }
308
309 void __init efi_init(void)
310 {
311         efi_config_table_t *config_tables;
312         efi_runtime_services_t *runtime;
313         efi_char16_t *c16;
314         char vendor[100] = "unknown";
315         unsigned long num_config_tables;
316         int i = 0;
317
318         memset(&efi, 0, sizeof(efi) );
319         memset(&efi_phys, 0, sizeof(efi_phys));
320
321         efi_phys.systab = EFI_SYSTAB;
322         memmap.phys_map = EFI_MEMMAP;
323         memmap.nr_map = EFI_MEMMAP_SIZE/EFI_MEMDESC_SIZE;
324         memmap.desc_version = EFI_MEMDESC_VERSION;
325
326         efi.systab = (efi_system_table_t *)
327                 boot_ioremap((unsigned long) efi_phys.systab,
328                         sizeof(efi_system_table_t));
329         /*
330          * Verify the EFI Table
331          */
332         if (efi.systab == NULL)
333                 printk(KERN_ERR PFX "Woah! Couldn't map the EFI system table.\n");
334         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
335                 printk(KERN_ERR PFX "Woah! EFI system table signature incorrect\n");
336         if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
337                 printk(KERN_ERR PFX
338                        "Warning: EFI system table major version mismatch: "
339                        "got %d.%02d, expected %d.%02d\n",
340                        efi.systab->hdr.revision >> 16,
341                        efi.systab->hdr.revision & 0xffff,
342                        EFI_SYSTEM_TABLE_REVISION >> 16,
343                        EFI_SYSTEM_TABLE_REVISION & 0xffff);
344         /*
345          * Grab some details from the system table
346          */
347         num_config_tables = efi.systab->nr_tables;
348         config_tables = (efi_config_table_t *)efi.systab->tables;
349         runtime = efi.systab->runtime;
350
351         /*
352          * Show what we know for posterity
353          */
354         c16 = (efi_char16_t *) boot_ioremap(efi.systab->fw_vendor, 2);
355         if (c16) {
356                 for (i = 0; i < sizeof(vendor) && *c16; ++i)
357                         vendor[i] = *c16++;
358                 vendor[i] = '\0';
359         } else
360                 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
361
362         printk(KERN_INFO PFX "EFI v%u.%.02u by %s \n",
363                efi.systab->hdr.revision >> 16,
364                efi.systab->hdr.revision & 0xffff, vendor);
365
366         /*
367          * Let's see what config tables the firmware passed to us.
368          */
369         config_tables = (efi_config_table_t *)
370                                 boot_ioremap((unsigned long) config_tables,
371                                 num_config_tables * sizeof(efi_config_table_t));
372
373         if (config_tables == NULL)
374                 printk(KERN_ERR PFX "Could not map EFI Configuration Table!\n");
375
376         for (i = 0; i < num_config_tables; i++) {
377                 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
378                         efi.mps = (void *)config_tables[i].table;
379                         printk(KERN_INFO " MPS=0x%lx ", config_tables[i].table);
380                 } else
381                     if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
382                         efi.acpi20 = __va(config_tables[i].table);
383                         printk(KERN_INFO " ACPI 2.0=0x%lx ", config_tables[i].table);
384                 } else
385                     if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
386                         efi.acpi = __va(config_tables[i].table);
387                         printk(KERN_INFO " ACPI=0x%lx ", config_tables[i].table);
388                 } else
389                     if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
390                         efi.smbios = (void *) config_tables[i].table;
391                         printk(KERN_INFO " SMBIOS=0x%lx ", config_tables[i].table);
392                 } else
393                     if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
394                         efi.hcdp = (void *)config_tables[i].table;
395                         printk(KERN_INFO " HCDP=0x%lx ", config_tables[i].table);
396                 } else
397                     if (efi_guidcmp(config_tables[i].guid, UGA_IO_PROTOCOL_GUID) == 0) {
398                         efi.uga = (void *)config_tables[i].table;
399                         printk(KERN_INFO " UGA=0x%lx ", config_tables[i].table);
400                 }
401         }
402         printk("\n");
403
404         /*
405          * Check out the runtime services table. We need to map
406          * the runtime services table so that we can grab the physical
407          * address of several of the EFI runtime functions, needed to
408          * set the firmware into virtual mode.
409          */
410
411         runtime = (efi_runtime_services_t *) boot_ioremap((unsigned long)
412                                                 runtime,
413                                                 sizeof(efi_runtime_services_t));
414         if (runtime != NULL) {
415                 /*
416                  * We will only need *early* access to the following
417                  * two EFI runtime services before set_virtual_address_map
418                  * is invoked.
419                  */
420                 efi_phys.get_time = (efi_get_time_t *) runtime->get_time;
421                 efi_phys.set_virtual_address_map =
422                         (efi_set_virtual_address_map_t *)
423                                 runtime->set_virtual_address_map;
424         } else
425                 printk(KERN_ERR PFX "Could not map the runtime service table!\n");
426
427         /* Map the EFI memory map for use until paging_init() */
428
429         memmap.map = (efi_memory_desc_t *)
430                 boot_ioremap((unsigned long) EFI_MEMMAP, EFI_MEMMAP_SIZE);
431
432         if (memmap.map == NULL)
433                 printk(KERN_ERR PFX "Could not map the EFI memory map!\n");
434
435         if (EFI_MEMDESC_SIZE != sizeof(efi_memory_desc_t)) {
436                 printk(KERN_WARNING PFX "Warning! Kernel-defined memdesc doesn't "
437                            "match the one from EFI!\n");
438         }
439 #if EFI_DEBUG
440         print_efi_memmap();
441 #endif
442 }
443
444 /*
445  * This function will switch the EFI runtime services to virtual mode.
446  * Essentially, look through the EFI memmap and map every region that
447  * has the runtime attribute bit set in its memory descriptor and update
448  * that memory descriptor with the virtual address obtained from ioremap().
449  * This enables the runtime services to be called without having to
450  * thunk back into physical mode for every invocation.
451  */
452
453 void __init efi_enter_virtual_mode(void)
454 {
455         efi_memory_desc_t *md;
456         efi_status_t status;
457         int i;
458
459         efi.systab = NULL;
460
461         for (i = 0; i < memmap.nr_map; i++) {
462                 md = &memmap.map[i];
463
464                 if (md->attribute & EFI_MEMORY_RUNTIME) {
465                         md->virt_addr =
466                                 (unsigned long)ioremap(md->phys_addr,
467                                         md->num_pages << EFI_PAGE_SHIFT);
468                         if (!(unsigned long)md->virt_addr) {
469                                 printk(KERN_ERR PFX "ioremap of 0x%lX failed\n",
470                                         (unsigned long)md->phys_addr);
471                         }
472
473                         if (((unsigned long)md->phys_addr <=
474                                         (unsigned long)efi_phys.systab) &&
475                                 ((unsigned long)efi_phys.systab <
476                                         md->phys_addr +
477                                         ((unsigned long)md->num_pages <<
478                                                 EFI_PAGE_SHIFT))) {
479                                 unsigned long addr;
480
481                                 addr = md->virt_addr - md->phys_addr +
482                                                 (unsigned long)efi_phys.systab;
483                                 efi.systab = (efi_system_table_t *)addr;
484                         }
485                 }
486         }
487
488         if (!efi.systab)
489                 BUG();
490
491         status = phys_efi_set_virtual_address_map(
492                         sizeof(efi_memory_desc_t) * memmap.nr_map,
493                         sizeof(efi_memory_desc_t),
494                         memmap.desc_version,
495                         memmap.phys_map);
496
497         if (status != EFI_SUCCESS) {
498                 printk (KERN_ALERT "You are screwed! "
499                         "Unable to switch EFI into virtual mode "
500                         "(status=%lx)\n", status);
501                 panic("EFI call to SetVirtualAddressMap() failed!");
502         }
503
504         /*
505          * Now that EFI is in virtual mode, update the function
506          * pointers in the runtime service table to the new virtual addresses.
507          */
508
509         efi.get_time = (efi_get_time_t *) efi.systab->runtime->get_time;
510         efi.set_time = (efi_set_time_t *) efi.systab->runtime->set_time;
511         efi.get_wakeup_time = (efi_get_wakeup_time_t *)
512                                         efi.systab->runtime->get_wakeup_time;
513         efi.set_wakeup_time = (efi_set_wakeup_time_t *)
514                                         efi.systab->runtime->set_wakeup_time;
515         efi.get_variable = (efi_get_variable_t *)
516                                         efi.systab->runtime->get_variable;
517         efi.get_next_variable = (efi_get_next_variable_t *)
518                                         efi.systab->runtime->get_next_variable;
519         efi.set_variable = (efi_set_variable_t *)
520                                         efi.systab->runtime->set_variable;
521         efi.get_next_high_mono_count = (efi_get_next_high_mono_count_t *)
522                                         efi.systab->runtime->get_next_high_mono_count;
523         efi.reset_system = (efi_reset_system_t *)
524                                         efi.systab->runtime->reset_system;
525 }
526
527 void __init
528 efi_initialize_iomem_resources(struct resource *code_resource,
529                                struct resource *data_resource)
530 {
531         struct resource *res;
532         efi_memory_desc_t *md;
533         int i;
534
535         for (i = 0; i < memmap.nr_map; i++) {
536                 md = &memmap.map[i];
537
538                 if ((md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT)) >
539                     0x100000000ULL)
540                         continue;
541                 res = alloc_bootmem_low(sizeof(struct resource));
542                 switch (md->type) {
543                 case EFI_RESERVED_TYPE:
544                         res->name = "Reserved Memory";
545                         break;
546                 case EFI_LOADER_CODE:
547                         res->name = "Loader Code";
548                         break;
549                 case EFI_LOADER_DATA:
550                         res->name = "Loader Data";
551                         break;
552                 case EFI_BOOT_SERVICES_DATA:
553                         res->name = "BootServices Data";
554                         break;
555                 case EFI_BOOT_SERVICES_CODE:
556                         res->name = "BootServices Code";
557                         break;
558                 case EFI_RUNTIME_SERVICES_CODE:
559                         res->name = "Runtime Service Code";
560                         break;
561                 case EFI_RUNTIME_SERVICES_DATA:
562                         res->name = "Runtime Service Data";
563                         break;
564                 case EFI_CONVENTIONAL_MEMORY:
565                         res->name = "Conventional Memory";
566                         break;
567                 case EFI_UNUSABLE_MEMORY:
568                         res->name = "Unusable Memory";
569                         break;
570                 case EFI_ACPI_RECLAIM_MEMORY:
571                         res->name = "ACPI Reclaim";
572                         break;
573                 case EFI_ACPI_MEMORY_NVS:
574                         res->name = "ACPI NVS";
575                         break;
576                 case EFI_MEMORY_MAPPED_IO:
577                         res->name = "Memory Mapped IO";
578                         break;
579                 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
580                         res->name = "Memory Mapped IO Port Space";
581                         break;
582                 default:
583                         res->name = "Reserved";
584                         break;
585                 }
586                 res->start = md->phys_addr;
587                 res->end = res->start + ((md->num_pages << EFI_PAGE_SHIFT) - 1);
588                 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
589                 if (request_resource(&iomem_resource, res) < 0)
590                         printk(KERN_ERR PFX "Failed to allocate res %s : 0x%lx-0x%lx\n",
591                                 res->name, res->start, res->end);
592                 /*
593                  * We don't know which region contains kernel data so we try
594                  * it repeatedly and let the resource manager test it.
595                  */
596                 if (md->type == EFI_CONVENTIONAL_MEMORY) {
597                         request_resource(res, code_resource);
598                         request_resource(res, data_resource);
599                 }
600         }
601 }
602
603 /*
604  * Convenience functions to obtain memory types and attributes
605  */
606
607 u32 efi_mem_type(unsigned long phys_addr)
608 {
609         efi_memory_desc_t *md;
610         int i;
611
612         for (i = 0; i < memmap.nr_map; i++) {
613                 md = &memmap.map[i];
614                 if ((md->phys_addr <= phys_addr) && (phys_addr <
615                         (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
616                         return md->type;
617         }
618         return 0;
619 }
620
621 u64 efi_mem_attributes(unsigned long phys_addr)
622 {
623         efi_memory_desc_t *md;
624         int i;
625
626         for (i = 0; i < memmap.nr_map; i++) {
627                 md = &memmap.map[i];
628                 if ((md->phys_addr <= phys_addr) && (phys_addr <
629                         (md->phys_addr + (md-> num_pages << EFI_PAGE_SHIFT)) ))
630                         return md->attribute;
631         }
632         return 0;
633 }