Merge with http://kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / arch / ia64 / kernel / efi.c
1 /*
2  * Extensible Firmware Interface
3  *
4  * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
5  *
6  * Copyright (C) 1999 VA Linux Systems
7  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8  * Copyright (C) 1999-2003 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 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/time.h>
27 #include <linux/efi.h>
28
29 #include <asm/io.h>
30 #include <asm/kregs.h>
31 #include <asm/meminit.h>
32 #include <asm/pgtable.h>
33 #include <asm/processor.h>
34 #include <asm/mca.h>
35
36 #define EFI_DEBUG       0
37
38 extern efi_status_t efi_call_phys (void *, ...);
39
40 struct efi efi;
41 EXPORT_SYMBOL(efi);
42 static efi_runtime_services_t *runtime;
43 static unsigned long mem_limit = ~0UL, max_addr = ~0UL;
44
45 #define efi_call_virt(f, args...)       (*(f))(args)
46
47 #define STUB_GET_TIME(prefix, adjust_arg)                                                         \
48 static efi_status_t                                                                               \
49 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc)                                            \
50 {                                                                                                 \
51         struct ia64_fpreg fr[6];                                                                  \
52         efi_time_cap_t *atc = NULL;                                                               \
53         efi_status_t ret;                                                                         \
54                                                                                                   \
55         if (tc)                                                                                   \
56                 atc = adjust_arg(tc);                                                             \
57         ia64_save_scratch_fpregs(fr);                                                             \
58         ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
59         ia64_load_scratch_fpregs(fr);                                                             \
60         return ret;                                                                               \
61 }
62
63 #define STUB_SET_TIME(prefix, adjust_arg)                                                       \
64 static efi_status_t                                                                             \
65 prefix##_set_time (efi_time_t *tm)                                                              \
66 {                                                                                               \
67         struct ia64_fpreg fr[6];                                                                \
68         efi_status_t ret;                                                                       \
69                                                                                                 \
70         ia64_save_scratch_fpregs(fr);                                                           \
71         ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm));    \
72         ia64_load_scratch_fpregs(fr);                                                           \
73         return ret;                                                                             \
74 }
75
76 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg)                                                \
77 static efi_status_t                                                                             \
78 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm)             \
79 {                                                                                               \
80         struct ia64_fpreg fr[6];                                                                \
81         efi_status_t ret;                                                                       \
82                                                                                                 \
83         ia64_save_scratch_fpregs(fr);                                                           \
84         ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time),       \
85                                 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm));      \
86         ia64_load_scratch_fpregs(fr);                                                           \
87         return ret;                                                                             \
88 }
89
90 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg)                                                \
91 static efi_status_t                                                                             \
92 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm)                                   \
93 {                                                                                               \
94         struct ia64_fpreg fr[6];                                                                \
95         efi_time_t *atm = NULL;                                                                 \
96         efi_status_t ret;                                                                       \
97                                                                                                 \
98         if (tm)                                                                                 \
99                 atm = adjust_arg(tm);                                                           \
100         ia64_save_scratch_fpregs(fr);                                                           \
101         ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time),       \
102                                 enabled, atm);                                                  \
103         ia64_load_scratch_fpregs(fr);                                                           \
104         return ret;                                                                             \
105 }
106
107 #define STUB_GET_VARIABLE(prefix, adjust_arg)                                           \
108 static efi_status_t                                                                     \
109 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr,               \
110                        unsigned long *data_size, void *data)                            \
111 {                                                                                       \
112         struct ia64_fpreg fr[6];                                                        \
113         u32 *aattr = NULL;                                                                      \
114         efi_status_t ret;                                                               \
115                                                                                         \
116         if (attr)                                                                       \
117                 aattr = adjust_arg(attr);                                               \
118         ia64_save_scratch_fpregs(fr);                                                   \
119         ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable),     \
120                                 adjust_arg(name), adjust_arg(vendor), aattr,            \
121                                 adjust_arg(data_size), adjust_arg(data));               \
122         ia64_load_scratch_fpregs(fr);                                                   \
123         return ret;                                                                     \
124 }
125
126 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg)                                              \
127 static efi_status_t                                                                             \
128 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor)   \
129 {                                                                                               \
130         struct ia64_fpreg fr[6];                                                                \
131         efi_status_t ret;                                                                       \
132                                                                                                 \
133         ia64_save_scratch_fpregs(fr);                                                           \
134         ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable),   \
135                                 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor));   \
136         ia64_load_scratch_fpregs(fr);                                                           \
137         return ret;                                                                             \
138 }
139
140 #define STUB_SET_VARIABLE(prefix, adjust_arg)                                           \
141 static efi_status_t                                                                     \
142 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr,      \
143                        unsigned long data_size, void *data)                             \
144 {                                                                                       \
145         struct ia64_fpreg fr[6];                                                        \
146         efi_status_t ret;                                                               \
147                                                                                         \
148         ia64_save_scratch_fpregs(fr);                                                   \
149         ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable),     \
150                                 adjust_arg(name), adjust_arg(vendor), attr, data_size,  \
151                                 adjust_arg(data));                                      \
152         ia64_load_scratch_fpregs(fr);                                                   \
153         return ret;                                                                     \
154 }
155
156 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg)                                       \
157 static efi_status_t                                                                             \
158 prefix##_get_next_high_mono_count (u32 *count)                                                  \
159 {                                                                                               \
160         struct ia64_fpreg fr[6];                                                                \
161         efi_status_t ret;                                                                       \
162                                                                                                 \
163         ia64_save_scratch_fpregs(fr);                                                           \
164         ret = efi_call_##prefix((efi_get_next_high_mono_count_t *)                              \
165                                 __va(runtime->get_next_high_mono_count), adjust_arg(count));    \
166         ia64_load_scratch_fpregs(fr);                                                           \
167         return ret;                                                                             \
168 }
169
170 #define STUB_RESET_SYSTEM(prefix, adjust_arg)                                   \
171 static void                                                                     \
172 prefix##_reset_system (int reset_type, efi_status_t status,                     \
173                        unsigned long data_size, efi_char16_t *data)             \
174 {                                                                               \
175         struct ia64_fpreg fr[6];                                                \
176         efi_char16_t *adata = NULL;                                             \
177                                                                                 \
178         if (data)                                                               \
179                 adata = adjust_arg(data);                                       \
180                                                                                 \
181         ia64_save_scratch_fpregs(fr);                                           \
182         efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system),   \
183                           reset_type, status, data_size, adata);                \
184         /* should not return, but just in case... */                            \
185         ia64_load_scratch_fpregs(fr);                                           \
186 }
187
188 #define phys_ptr(arg)   ((__typeof__(arg)) ia64_tpa(arg))
189
190 STUB_GET_TIME(phys, phys_ptr)
191 STUB_SET_TIME(phys, phys_ptr)
192 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
193 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
194 STUB_GET_VARIABLE(phys, phys_ptr)
195 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
196 STUB_SET_VARIABLE(phys, phys_ptr)
197 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
198 STUB_RESET_SYSTEM(phys, phys_ptr)
199
200 #define id(arg) arg
201
202 STUB_GET_TIME(virt, id)
203 STUB_SET_TIME(virt, id)
204 STUB_GET_WAKEUP_TIME(virt, id)
205 STUB_SET_WAKEUP_TIME(virt, id)
206 STUB_GET_VARIABLE(virt, id)
207 STUB_GET_NEXT_VARIABLE(virt, id)
208 STUB_SET_VARIABLE(virt, id)
209 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
210 STUB_RESET_SYSTEM(virt, id)
211
212 void
213 efi_gettimeofday (struct timespec *ts)
214 {
215         efi_time_t tm;
216
217         memset(ts, 0, sizeof(ts));
218         if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
219                 return;
220
221         ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
222         ts->tv_nsec = tm.nanosecond;
223 }
224
225 static int
226 is_available_memory (efi_memory_desc_t *md)
227 {
228         if (!(md->attribute & EFI_MEMORY_WB))
229                 return 0;
230
231         switch (md->type) {
232               case EFI_LOADER_CODE:
233               case EFI_LOADER_DATA:
234               case EFI_BOOT_SERVICES_CODE:
235               case EFI_BOOT_SERVICES_DATA:
236               case EFI_CONVENTIONAL_MEMORY:
237                 return 1;
238         }
239         return 0;
240 }
241
242 typedef struct kern_memdesc {
243         u64 attribute;
244         u64 start;
245         u64 num_pages;
246 } kern_memdesc_t;
247
248 static kern_memdesc_t *kern_memmap;
249
250 static void
251 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
252 {
253         kern_memdesc_t *k;
254         u64 start, end, voff;
255
256         voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
257         for (k = kern_memmap; k->start != ~0UL; k++) {
258                 if (k->attribute != attr)
259                         continue;
260                 start = PAGE_ALIGN(k->start);
261                 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
262                 if (start < end)
263                         if ((*callback)(start + voff, end + voff, arg) < 0)
264                                 return;
265         }
266 }
267
268 /*
269  * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
270  * has memory that is available for OS use.
271  */
272 void
273 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
274 {
275         walk(callback, arg, EFI_MEMORY_WB);
276 }
277
278 /*
279  * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
280  * has memory that is available for uncached allocator.
281  */
282 void
283 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
284 {
285         walk(callback, arg, EFI_MEMORY_UC);
286 }
287
288 /*
289  * Look for the PAL_CODE region reported by EFI and maps it using an
290  * ITR to enable safe PAL calls in virtual mode.  See IA-64 Processor
291  * Abstraction Layer chapter 11 in ADAG
292  */
293
294 void *
295 efi_get_pal_addr (void)
296 {
297         void *efi_map_start, *efi_map_end, *p;
298         efi_memory_desc_t *md;
299         u64 efi_desc_size;
300         int pal_code_count = 0;
301         u64 vaddr, mask;
302
303         efi_map_start = __va(ia64_boot_param->efi_memmap);
304         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
305         efi_desc_size = ia64_boot_param->efi_memdesc_size;
306
307         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
308                 md = p;
309                 if (md->type != EFI_PAL_CODE)
310                         continue;
311
312                 if (++pal_code_count > 1) {
313                         printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
314                                md->phys_addr);
315                         continue;
316                 }
317                 /*
318                  * The only ITLB entry in region 7 that is used is the one installed by
319                  * __start().  That entry covers a 64MB range.
320                  */
321                 mask  = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
322                 vaddr = PAGE_OFFSET + md->phys_addr;
323
324                 /*
325                  * We must check that the PAL mapping won't overlap with the kernel
326                  * mapping.
327                  *
328                  * PAL code is guaranteed to be aligned on a power of 2 between 4k and
329                  * 256KB and that only one ITR is needed to map it. This implies that the
330                  * PAL code is always aligned on its size, i.e., the closest matching page
331                  * size supported by the TLB. Therefore PAL code is guaranteed never to
332                  * cross a 64MB unless it is bigger than 64MB (very unlikely!).  So for
333                  * now the following test is enough to determine whether or not we need a
334                  * dedicated ITR for the PAL code.
335                  */
336                 if ((vaddr & mask) == (KERNEL_START & mask)) {
337                         printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
338                                __FUNCTION__);
339                         continue;
340                 }
341
342                 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
343                         panic("Woah!  PAL code size bigger than a granule!");
344
345 #if EFI_DEBUG
346                 mask  = ~((1 << IA64_GRANULE_SHIFT) - 1);
347
348                 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
349                         smp_processor_id(), md->phys_addr,
350                         md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
351                         vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
352 #endif
353                 return __va(md->phys_addr);
354         }
355         printk(KERN_WARNING "%s: no PAL-code memory-descriptor found",
356                __FUNCTION__);
357         return NULL;
358 }
359
360 void
361 efi_map_pal_code (void)
362 {
363         void *pal_vaddr = efi_get_pal_addr ();
364         u64 psr;
365
366         if (!pal_vaddr)
367                 return;
368
369         /*
370          * Cannot write to CRx with PSR.ic=1
371          */
372         psr = ia64_clear_ic();
373         ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
374                  pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
375                  IA64_GRANULE_SHIFT);
376         ia64_set_psr(psr);              /* restore psr */
377         ia64_srlz_i();
378 }
379
380 void __init
381 efi_init (void)
382 {
383         void *efi_map_start, *efi_map_end;
384         efi_config_table_t *config_tables;
385         efi_char16_t *c16;
386         u64 efi_desc_size;
387         char *cp, *end, vendor[100] = "unknown";
388         extern char saved_command_line[];
389         int i;
390
391         /* it's too early to be able to use the standard kernel command line support... */
392         for (cp = saved_command_line; *cp; ) {
393                 if (memcmp(cp, "mem=", 4) == 0) {
394                         cp += 4;
395                         mem_limit = memparse(cp, &end);
396                         if (end != cp)
397                                 break;
398                         cp = end;
399                 } else if (memcmp(cp, "max_addr=", 9) == 0) {
400                         cp += 9;
401                         max_addr = GRANULEROUNDDOWN(memparse(cp, &end));
402                         if (end != cp)
403                                 break;
404                         cp = end;
405                 } else {
406                         while (*cp != ' ' && *cp)
407                                 ++cp;
408                         while (*cp == ' ')
409                                 ++cp;
410                 }
411         }
412         if (max_addr != ~0UL)
413                 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
414
415         efi.systab = __va(ia64_boot_param->efi_systab);
416
417         /*
418          * Verify the EFI Table
419          */
420         if (efi.systab == NULL)
421                 panic("Woah! Can't find EFI system table.\n");
422         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
423                 panic("Woah! EFI system table signature incorrect\n");
424         if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
425                 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
426                        "got %d.%02d, expected %d.%02d\n",
427                        efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
428                        EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
429
430         config_tables = __va(efi.systab->tables);
431
432         /* Show what we know for posterity */
433         c16 = __va(efi.systab->fw_vendor);
434         if (c16) {
435                 for (i = 0;i < (int) sizeof(vendor) && *c16; ++i)
436                         vendor[i] = *c16++;
437                 vendor[i] = '\0';
438         }
439
440         printk(KERN_INFO "EFI v%u.%.02u by %s:",
441                efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
442
443         for (i = 0; i < (int) efi.systab->nr_tables; i++) {
444                 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
445                         efi.mps = __va(config_tables[i].table);
446                         printk(" MPS=0x%lx", config_tables[i].table);
447                 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
448                         efi.acpi20 = __va(config_tables[i].table);
449                         printk(" ACPI 2.0=0x%lx", config_tables[i].table);
450                 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
451                         efi.acpi = __va(config_tables[i].table);
452                         printk(" ACPI=0x%lx", config_tables[i].table);
453                 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
454                         efi.smbios = __va(config_tables[i].table);
455                         printk(" SMBIOS=0x%lx", config_tables[i].table);
456                 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
457                         efi.sal_systab = __va(config_tables[i].table);
458                         printk(" SALsystab=0x%lx", config_tables[i].table);
459                 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
460                         efi.hcdp = __va(config_tables[i].table);
461                         printk(" HCDP=0x%lx", config_tables[i].table);
462                 }
463         }
464         printk("\n");
465
466         runtime = __va(efi.systab->runtime);
467         efi.get_time = phys_get_time;
468         efi.set_time = phys_set_time;
469         efi.get_wakeup_time = phys_get_wakeup_time;
470         efi.set_wakeup_time = phys_set_wakeup_time;
471         efi.get_variable = phys_get_variable;
472         efi.get_next_variable = phys_get_next_variable;
473         efi.set_variable = phys_set_variable;
474         efi.get_next_high_mono_count = phys_get_next_high_mono_count;
475         efi.reset_system = phys_reset_system;
476
477         efi_map_start = __va(ia64_boot_param->efi_memmap);
478         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
479         efi_desc_size = ia64_boot_param->efi_memdesc_size;
480
481 #if EFI_DEBUG
482         /* print EFI memory map: */
483         {
484                 efi_memory_desc_t *md;
485                 void *p;
486
487                 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
488                         md = p;
489                         printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
490                                i, md->type, md->attribute, md->phys_addr,
491                                md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
492                                md->num_pages >> (20 - EFI_PAGE_SHIFT));
493                 }
494         }
495 #endif
496
497         efi_map_pal_code();
498         efi_enter_virtual_mode();
499 }
500
501 void
502 efi_enter_virtual_mode (void)
503 {
504         void *efi_map_start, *efi_map_end, *p;
505         efi_memory_desc_t *md;
506         efi_status_t status;
507         u64 efi_desc_size;
508
509         efi_map_start = __va(ia64_boot_param->efi_memmap);
510         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
511         efi_desc_size = ia64_boot_param->efi_memdesc_size;
512
513         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
514                 md = p;
515                 if (md->attribute & EFI_MEMORY_RUNTIME) {
516                         /*
517                          * Some descriptors have multiple bits set, so the order of
518                          * the tests is relevant.
519                          */
520                         if (md->attribute & EFI_MEMORY_WB) {
521                                 md->virt_addr = (u64) __va(md->phys_addr);
522                         } else if (md->attribute & EFI_MEMORY_UC) {
523                                 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
524                         } else if (md->attribute & EFI_MEMORY_WC) {
525 #if 0
526                                 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
527                                                                            | _PAGE_D
528                                                                            | _PAGE_MA_WC
529                                                                            | _PAGE_PL_0
530                                                                            | _PAGE_AR_RW));
531 #else
532                                 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
533                                 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
534 #endif
535                         } else if (md->attribute & EFI_MEMORY_WT) {
536 #if 0
537                                 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
538                                                                            | _PAGE_D | _PAGE_MA_WT
539                                                                            | _PAGE_PL_0
540                                                                            | _PAGE_AR_RW));
541 #else
542                                 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
543                                 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
544 #endif
545                         }
546                 }
547         }
548
549         status = efi_call_phys(__va(runtime->set_virtual_address_map),
550                                ia64_boot_param->efi_memmap_size,
551                                efi_desc_size, ia64_boot_param->efi_memdesc_version,
552                                ia64_boot_param->efi_memmap);
553         if (status != EFI_SUCCESS) {
554                 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
555                        "(status=%lu)\n", status);
556                 return;
557         }
558
559         /*
560          * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
561          */
562         efi.get_time = virt_get_time;
563         efi.set_time = virt_set_time;
564         efi.get_wakeup_time = virt_get_wakeup_time;
565         efi.set_wakeup_time = virt_set_wakeup_time;
566         efi.get_variable = virt_get_variable;
567         efi.get_next_variable = virt_get_next_variable;
568         efi.set_variable = virt_set_variable;
569         efi.get_next_high_mono_count = virt_get_next_high_mono_count;
570         efi.reset_system = virt_reset_system;
571 }
572
573 /*
574  * Walk the EFI memory map looking for the I/O port range.  There can only be one entry of
575  * this type, other I/O port ranges should be described via ACPI.
576  */
577 u64
578 efi_get_iobase (void)
579 {
580         void *efi_map_start, *efi_map_end, *p;
581         efi_memory_desc_t *md;
582         u64 efi_desc_size;
583
584         efi_map_start = __va(ia64_boot_param->efi_memmap);
585         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
586         efi_desc_size = ia64_boot_param->efi_memdesc_size;
587
588         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
589                 md = p;
590                 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
591                         if (md->attribute & EFI_MEMORY_UC)
592                                 return md->phys_addr;
593                 }
594         }
595         return 0;
596 }
597
598 u32
599 efi_mem_type (unsigned long phys_addr)
600 {
601         void *efi_map_start, *efi_map_end, *p;
602         efi_memory_desc_t *md;
603         u64 efi_desc_size;
604
605         efi_map_start = __va(ia64_boot_param->efi_memmap);
606         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
607         efi_desc_size = ia64_boot_param->efi_memdesc_size;
608
609         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
610                 md = p;
611
612                 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
613                          return md->type;
614         }
615         return 0;
616 }
617
618 u64
619 efi_mem_attributes (unsigned long phys_addr)
620 {
621         void *efi_map_start, *efi_map_end, *p;
622         efi_memory_desc_t *md;
623         u64 efi_desc_size;
624
625         efi_map_start = __va(ia64_boot_param->efi_memmap);
626         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
627         efi_desc_size = ia64_boot_param->efi_memdesc_size;
628
629         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
630                 md = p;
631
632                 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
633                         return md->attribute;
634         }
635         return 0;
636 }
637 EXPORT_SYMBOL(efi_mem_attributes);
638
639 int
640 valid_phys_addr_range (unsigned long phys_addr, unsigned long *size)
641 {
642         void *efi_map_start, *efi_map_end, *p;
643         efi_memory_desc_t *md;
644         u64 efi_desc_size;
645
646         efi_map_start = __va(ia64_boot_param->efi_memmap);
647         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
648         efi_desc_size = ia64_boot_param->efi_memdesc_size;
649
650         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
651                 md = p;
652
653                 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT)) {
654                         if (!(md->attribute & EFI_MEMORY_WB))
655                                 return 0;
656
657                         if (*size > md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - phys_addr)
658                                 *size = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - phys_addr;
659                         return 1;
660                 }
661         }
662         return 0;
663 }
664
665 int __init
666 efi_uart_console_only(void)
667 {
668         efi_status_t status;
669         char *s, name[] = "ConOut";
670         efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
671         efi_char16_t *utf16, name_utf16[32];
672         unsigned char data[1024];
673         unsigned long size = sizeof(data);
674         struct efi_generic_dev_path *hdr, *end_addr;
675         int uart = 0;
676
677         /* Convert to UTF-16 */
678         utf16 = name_utf16;
679         s = name;
680         while (*s)
681                 *utf16++ = *s++ & 0x7f;
682         *utf16 = 0;
683
684         status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
685         if (status != EFI_SUCCESS) {
686                 printk(KERN_ERR "No EFI %s variable?\n", name);
687                 return 0;
688         }
689
690         hdr = (struct efi_generic_dev_path *) data;
691         end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
692         while (hdr < end_addr) {
693                 if (hdr->type == EFI_DEV_MSG &&
694                     hdr->sub_type == EFI_DEV_MSG_UART)
695                         uart = 1;
696                 else if (hdr->type == EFI_DEV_END_PATH ||
697                           hdr->type == EFI_DEV_END_PATH2) {
698                         if (!uart)
699                                 return 0;
700                         if (hdr->sub_type == EFI_DEV_END_ENTIRE)
701                                 return 1;
702                         uart = 0;
703                 }
704                 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
705         }
706         printk(KERN_ERR "Malformed %s value\n", name);
707         return 0;
708 }
709
710 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
711
712 static inline u64
713 kmd_end(kern_memdesc_t *kmd)
714 {
715         return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
716 }
717
718 static inline u64
719 efi_md_end(efi_memory_desc_t *md)
720 {
721         return (md->phys_addr + efi_md_size(md));
722 }
723
724 static inline int
725 efi_wb(efi_memory_desc_t *md)
726 {
727         return (md->attribute & EFI_MEMORY_WB);
728 }
729
730 static inline int
731 efi_uc(efi_memory_desc_t *md)
732 {
733         return (md->attribute & EFI_MEMORY_UC);
734 }
735
736 /*
737  * Look for the first granule aligned memory descriptor memory
738  * that is big enough to hold EFI memory map. Make sure this
739  * descriptor is atleast granule sized so it does not get trimmed
740  */
741 struct kern_memdesc *
742 find_memmap_space (void)
743 {
744         u64     contig_low=0, contig_high=0;
745         u64     as = 0, ae;
746         void *efi_map_start, *efi_map_end, *p, *q;
747         efi_memory_desc_t *md, *pmd = NULL, *check_md;
748         u64     space_needed, efi_desc_size;
749         unsigned long total_mem = 0;
750
751         efi_map_start = __va(ia64_boot_param->efi_memmap);
752         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
753         efi_desc_size = ia64_boot_param->efi_memdesc_size;
754
755         /*
756          * Worst case: we need 3 kernel descriptors for each efi descriptor
757          * (if every entry has a WB part in the middle, and UC head and tail),
758          * plus one for the end marker.
759          */
760         space_needed = sizeof(kern_memdesc_t) *
761                 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
762
763         for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
764                 md = p;
765                 if (!efi_wb(md)) {
766                         continue;
767                 }
768                 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
769                         contig_low = GRANULEROUNDUP(md->phys_addr);
770                         contig_high = efi_md_end(md);
771                         for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
772                                 check_md = q;
773                                 if (!efi_wb(check_md))
774                                         break;
775                                 if (contig_high != check_md->phys_addr)
776                                         break;
777                                 contig_high = efi_md_end(check_md);
778                         }
779                         contig_high = GRANULEROUNDDOWN(contig_high);
780                 }
781                 if (!is_available_memory(md) || md->type == EFI_LOADER_DATA)
782                         continue;
783
784                 /* Round ends inward to granule boundaries */
785                 as = max(contig_low, md->phys_addr);
786                 ae = min(contig_high, efi_md_end(md));
787
788                 /* keep within max_addr= command line arg */
789                 ae = min(ae, max_addr);
790                 if (ae <= as)
791                         continue;
792
793                 /* avoid going over mem= command line arg */
794                 if (total_mem + (ae - as) > mem_limit)
795                         ae -= total_mem + (ae - as) - mem_limit;
796
797                 if (ae <= as)
798                         continue;
799
800                 if (ae - as > space_needed)
801                         break;
802         }
803         if (p >= efi_map_end)
804                 panic("Can't allocate space for kernel memory descriptors");
805
806         return __va(as);
807 }
808
809 /*
810  * Walk the EFI memory map and gather all memory available for kernel
811  * to use.  We can allocate partial granules only if the unavailable
812  * parts exist, and are WB.
813  */
814 void
815 efi_memmap_init(unsigned long *s, unsigned long *e)
816 {
817         struct kern_memdesc *k, *prev = 0;
818         u64     contig_low=0, contig_high=0;
819         u64     as, ae, lim;
820         void *efi_map_start, *efi_map_end, *p, *q;
821         efi_memory_desc_t *md, *pmd = NULL, *check_md;
822         u64     efi_desc_size;
823         unsigned long total_mem = 0;
824
825         k = kern_memmap = find_memmap_space();
826
827         efi_map_start = __va(ia64_boot_param->efi_memmap);
828         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
829         efi_desc_size = ia64_boot_param->efi_memdesc_size;
830
831         for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
832                 md = p;
833                 if (!efi_wb(md)) {
834                         if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
835                                            md->type == EFI_BOOT_SERVICES_DATA)) {
836                                 k->attribute = EFI_MEMORY_UC;
837                                 k->start = md->phys_addr;
838                                 k->num_pages = md->num_pages;
839                                 k++;
840                         }
841                         continue;
842                 }
843                 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
844                         contig_low = GRANULEROUNDUP(md->phys_addr);
845                         contig_high = efi_md_end(md);
846                         for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
847                                 check_md = q;
848                                 if (!efi_wb(check_md))
849                                         break;
850                                 if (contig_high != check_md->phys_addr)
851                                         break;
852                                 contig_high = efi_md_end(check_md);
853                         }
854                         contig_high = GRANULEROUNDDOWN(contig_high);
855                 }
856                 if (!is_available_memory(md))
857                         continue;
858
859                 /*
860                  * Round ends inward to granule boundaries
861                  * Give trimmings to uncached allocator
862                  */
863                 if (md->phys_addr < contig_low) {
864                         lim = min(efi_md_end(md), contig_low);
865                         if (efi_uc(md)) {
866                                 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
867                                     kmd_end(k-1) == md->phys_addr) {
868                                         (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
869                                 } else {
870                                         k->attribute = EFI_MEMORY_UC;
871                                         k->start = md->phys_addr;
872                                         k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
873                                         k++;
874                                 }
875                         }
876                         as = contig_low;
877                 } else
878                         as = md->phys_addr;
879
880                 if (efi_md_end(md) > contig_high) {
881                         lim = max(md->phys_addr, contig_high);
882                         if (efi_uc(md)) {
883                                 if (lim == md->phys_addr && k > kern_memmap &&
884                                     (k-1)->attribute == EFI_MEMORY_UC &&
885                                     kmd_end(k-1) == md->phys_addr) {
886                                         (k-1)->num_pages += md->num_pages;
887                                 } else {
888                                         k->attribute = EFI_MEMORY_UC;
889                                         k->start = lim;
890                                         k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
891                                         k++;
892                                 }
893                         }
894                         ae = contig_high;
895                 } else
896                         ae = efi_md_end(md);
897
898                 /* keep within max_addr= command line arg */
899                 ae = min(ae, max_addr);
900                 if (ae <= as)
901                         continue;
902
903                 /* avoid going over mem= command line arg */
904                 if (total_mem + (ae - as) > mem_limit)
905                         ae -= total_mem + (ae - as) - mem_limit;
906
907                 if (ae <= as)
908                         continue;
909                 if (prev && kmd_end(prev) == md->phys_addr) {
910                         prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
911                         total_mem += ae - as;
912                         continue;
913                 }
914                 k->attribute = EFI_MEMORY_WB;
915                 k->start = as;
916                 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
917                 total_mem += ae - as;
918                 prev = k++;
919         }
920         k->start = ~0L; /* end-marker */
921
922         /* reserve the memory we are using for kern_memmap */
923         *s = (u64)kern_memmap;
924         *e = (u64)++k;
925 }
926
927 void
928 efi_initialize_iomem_resources(struct resource *code_resource,
929                                struct resource *data_resource)
930 {
931         struct resource *res;
932         void *efi_map_start, *efi_map_end, *p;
933         efi_memory_desc_t *md;
934         u64 efi_desc_size;
935         char *name;
936         unsigned long flags;
937
938         efi_map_start = __va(ia64_boot_param->efi_memmap);
939         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
940         efi_desc_size = ia64_boot_param->efi_memdesc_size;
941
942         res = NULL;
943
944         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
945                 md = p;
946
947                 if (md->num_pages == 0) /* should not happen */
948                         continue;
949
950                 flags = IORESOURCE_MEM;
951                 switch (md->type) {
952
953                         case EFI_MEMORY_MAPPED_IO:
954                         case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
955                                 continue;
956
957                         case EFI_LOADER_CODE:
958                         case EFI_LOADER_DATA:
959                         case EFI_BOOT_SERVICES_DATA:
960                         case EFI_BOOT_SERVICES_CODE:
961                         case EFI_CONVENTIONAL_MEMORY:
962                                 if (md->attribute & EFI_MEMORY_WP) {
963                                         name = "System ROM";
964                                         flags |= IORESOURCE_READONLY;
965                                 } else {
966                                         name = "System RAM";
967                                 }
968                                 break;
969
970                         case EFI_ACPI_MEMORY_NVS:
971                                 name = "ACPI Non-volatile Storage";
972                                 flags |= IORESOURCE_BUSY;
973                                 break;
974
975                         case EFI_UNUSABLE_MEMORY:
976                                 name = "reserved";
977                                 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
978                                 break;
979
980                         case EFI_RESERVED_TYPE:
981                         case EFI_RUNTIME_SERVICES_CODE:
982                         case EFI_RUNTIME_SERVICES_DATA:
983                         case EFI_ACPI_RECLAIM_MEMORY:
984                         default:
985                                 name = "reserved";
986                                 flags |= IORESOURCE_BUSY;
987                                 break;
988                 }
989
990                 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
991                         printk(KERN_ERR "failed to alocate resource for iomem\n");
992                         return;
993                 }
994
995                 res->name = name;
996                 res->start = md->phys_addr;
997                 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
998                 res->flags = flags;
999
1000                 if (insert_resource(&iomem_resource, res) < 0)
1001                         kfree(res);
1002                 else {
1003                         /*
1004                          * We don't know which region contains
1005                          * kernel data so we try it repeatedly and
1006                          * let the resource manager test it.
1007                          */
1008                         insert_resource(res, code_resource);
1009                         insert_resource(res, data_resource);
1010                 }
1011         }
1012 }