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