Merge ../linus
[pandora-kernel.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/config.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/time.h>
29 #include <linux/efi.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;
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 {
426                         while (*cp != ' ' && *cp)
427                                 ++cp;
428                         while (*cp == ' ')
429                                 ++cp;
430                 }
431         }
432         if (max_addr != ~0UL)
433                 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
434
435         efi.systab = __va(ia64_boot_param->efi_systab);
436
437         /*
438          * Verify the EFI Table
439          */
440         if (efi.systab == NULL)
441                 panic("Woah! Can't find EFI system table.\n");
442         if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
443                 panic("Woah! EFI system table signature incorrect\n");
444         if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
445                 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
446                        "got %d.%02d, expected %d.%02d\n",
447                        efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
448                        EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
449
450         config_tables = __va(efi.systab->tables);
451
452         /* Show what we know for posterity */
453         c16 = __va(efi.systab->fw_vendor);
454         if (c16) {
455                 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
456                         vendor[i] = *c16++;
457                 vendor[i] = '\0';
458         }
459
460         printk(KERN_INFO "EFI v%u.%.02u by %s:",
461                efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
462
463         efi.mps        = EFI_INVALID_TABLE_ADDR;
464         efi.acpi       = EFI_INVALID_TABLE_ADDR;
465         efi.acpi20     = EFI_INVALID_TABLE_ADDR;
466         efi.smbios     = EFI_INVALID_TABLE_ADDR;
467         efi.sal_systab = EFI_INVALID_TABLE_ADDR;
468         efi.boot_info  = EFI_INVALID_TABLE_ADDR;
469         efi.hcdp       = EFI_INVALID_TABLE_ADDR;
470         efi.uga        = EFI_INVALID_TABLE_ADDR;
471
472         for (i = 0; i < (int) efi.systab->nr_tables; i++) {
473                 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
474                         efi.mps = config_tables[i].table;
475                         printk(" MPS=0x%lx", config_tables[i].table);
476                 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
477                         efi.acpi20 = config_tables[i].table;
478                         printk(" ACPI 2.0=0x%lx", config_tables[i].table);
479                 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
480                         efi.acpi = config_tables[i].table;
481                         printk(" ACPI=0x%lx", config_tables[i].table);
482                 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
483                         efi.smbios = config_tables[i].table;
484                         printk(" SMBIOS=0x%lx", config_tables[i].table);
485                 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
486                         efi.sal_systab = config_tables[i].table;
487                         printk(" SALsystab=0x%lx", config_tables[i].table);
488                 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
489                         efi.hcdp = config_tables[i].table;
490                         printk(" HCDP=0x%lx", config_tables[i].table);
491                 }
492         }
493         printk("\n");
494
495         runtime = __va(efi.systab->runtime);
496         efi.get_time = phys_get_time;
497         efi.set_time = phys_set_time;
498         efi.get_wakeup_time = phys_get_wakeup_time;
499         efi.set_wakeup_time = phys_set_wakeup_time;
500         efi.get_variable = phys_get_variable;
501         efi.get_next_variable = phys_get_next_variable;
502         efi.set_variable = phys_set_variable;
503         efi.get_next_high_mono_count = phys_get_next_high_mono_count;
504         efi.reset_system = phys_reset_system;
505
506         efi_map_start = __va(ia64_boot_param->efi_memmap);
507         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
508         efi_desc_size = ia64_boot_param->efi_memdesc_size;
509
510 #if EFI_DEBUG
511         /* print EFI memory map: */
512         {
513                 efi_memory_desc_t *md;
514                 void *p;
515
516                 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
517                         md = p;
518                         printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
519                                i, md->type, md->attribute, md->phys_addr,
520                                md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
521                                md->num_pages >> (20 - EFI_PAGE_SHIFT));
522                 }
523         }
524 #endif
525
526         efi_map_pal_code();
527         efi_enter_virtual_mode();
528 }
529
530 void
531 efi_enter_virtual_mode (void)
532 {
533         void *efi_map_start, *efi_map_end, *p;
534         efi_memory_desc_t *md;
535         efi_status_t status;
536         u64 efi_desc_size;
537
538         efi_map_start = __va(ia64_boot_param->efi_memmap);
539         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
540         efi_desc_size = ia64_boot_param->efi_memdesc_size;
541
542         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
543                 md = p;
544                 if (md->attribute & EFI_MEMORY_RUNTIME) {
545                         /*
546                          * Some descriptors have multiple bits set, so the order of
547                          * the tests is relevant.
548                          */
549                         if (md->attribute & EFI_MEMORY_WB) {
550                                 md->virt_addr = (u64) __va(md->phys_addr);
551                         } else if (md->attribute & EFI_MEMORY_UC) {
552                                 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
553                         } else if (md->attribute & EFI_MEMORY_WC) {
554 #if 0
555                                 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
556                                                                            | _PAGE_D
557                                                                            | _PAGE_MA_WC
558                                                                            | _PAGE_PL_0
559                                                                            | _PAGE_AR_RW));
560 #else
561                                 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
562                                 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
563 #endif
564                         } else if (md->attribute & EFI_MEMORY_WT) {
565 #if 0
566                                 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
567                                                                            | _PAGE_D | _PAGE_MA_WT
568                                                                            | _PAGE_PL_0
569                                                                            | _PAGE_AR_RW));
570 #else
571                                 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
572                                 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
573 #endif
574                         }
575                 }
576         }
577
578         status = efi_call_phys(__va(runtime->set_virtual_address_map),
579                                ia64_boot_param->efi_memmap_size,
580                                efi_desc_size, ia64_boot_param->efi_memdesc_version,
581                                ia64_boot_param->efi_memmap);
582         if (status != EFI_SUCCESS) {
583                 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
584                        "(status=%lu)\n", status);
585                 return;
586         }
587
588         /*
589          * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
590          */
591         efi.get_time = virt_get_time;
592         efi.set_time = virt_set_time;
593         efi.get_wakeup_time = virt_get_wakeup_time;
594         efi.set_wakeup_time = virt_set_wakeup_time;
595         efi.get_variable = virt_get_variable;
596         efi.get_next_variable = virt_get_next_variable;
597         efi.set_variable = virt_set_variable;
598         efi.get_next_high_mono_count = virt_get_next_high_mono_count;
599         efi.reset_system = virt_reset_system;
600 }
601
602 /*
603  * Walk the EFI memory map looking for the I/O port range.  There can only be one entry of
604  * this type, other I/O port ranges should be described via ACPI.
605  */
606 u64
607 efi_get_iobase (void)
608 {
609         void *efi_map_start, *efi_map_end, *p;
610         efi_memory_desc_t *md;
611         u64 efi_desc_size;
612
613         efi_map_start = __va(ia64_boot_param->efi_memmap);
614         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
615         efi_desc_size = ia64_boot_param->efi_memdesc_size;
616
617         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
618                 md = p;
619                 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
620                         if (md->attribute & EFI_MEMORY_UC)
621                                 return md->phys_addr;
622                 }
623         }
624         return 0;
625 }
626
627 static struct kern_memdesc *
628 kern_memory_descriptor (unsigned long phys_addr)
629 {
630         struct kern_memdesc *md;
631
632         for (md = kern_memmap; md->start != ~0UL; md++) {
633                 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
634                          return md;
635         }
636         return 0;
637 }
638
639 static efi_memory_desc_t *
640 efi_memory_descriptor (unsigned long phys_addr)
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                          return md;
655         }
656         return 0;
657 }
658
659 u32
660 efi_mem_type (unsigned long phys_addr)
661 {
662         efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
663
664         if (md)
665                 return md->type;
666         return 0;
667 }
668
669 u64
670 efi_mem_attributes (unsigned long phys_addr)
671 {
672         efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
673
674         if (md)
675                 return md->attribute;
676         return 0;
677 }
678 EXPORT_SYMBOL(efi_mem_attributes);
679
680 u64
681 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
682 {
683         unsigned long end = phys_addr + size;
684         efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
685         u64 attr;
686
687         if (!md)
688                 return 0;
689
690         /*
691          * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
692          * the kernel that firmware needs this region mapped.
693          */
694         attr = md->attribute & ~EFI_MEMORY_RUNTIME;
695         do {
696                 unsigned long md_end = efi_md_end(md);
697
698                 if (end <= md_end)
699                         return attr;
700
701                 md = efi_memory_descriptor(md_end);
702                 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
703                         return 0;
704         } while (md);
705         return 0;
706 }
707
708 u64
709 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
710 {
711         unsigned long end = phys_addr + size;
712         struct kern_memdesc *md;
713         u64 attr;
714
715         /*
716          * This is a hack for ioremap calls before we set up kern_memmap.
717          * Maybe we should do efi_memmap_init() earlier instead.
718          */
719         if (!kern_memmap) {
720                 attr = efi_mem_attribute(phys_addr, size);
721                 if (attr & EFI_MEMORY_WB)
722                         return EFI_MEMORY_WB;
723                 return 0;
724         }
725
726         md = kern_memory_descriptor(phys_addr);
727         if (!md)
728                 return 0;
729
730         attr = md->attribute;
731         do {
732                 unsigned long md_end = kmd_end(md);
733
734                 if (end <= md_end)
735                         return attr;
736
737                 md = kern_memory_descriptor(md_end);
738                 if (!md || md->attribute != attr)
739                         return 0;
740         } while (md);
741         return 0;
742 }
743 EXPORT_SYMBOL(kern_mem_attribute);
744
745 int
746 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
747 {
748         u64 attr;
749
750         /*
751          * /dev/mem reads and writes use copy_to_user(), which implicitly
752          * uses a granule-sized kernel identity mapping.  It's really
753          * only safe to do this for regions in kern_memmap.  For more
754          * details, see Documentation/ia64/aliasing.txt.
755          */
756         attr = kern_mem_attribute(phys_addr, size);
757         if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
758                 return 1;
759         return 0;
760 }
761
762 int
763 valid_mmap_phys_addr_range (unsigned long phys_addr, unsigned long size)
764 {
765         /*
766          * MMIO regions are often missing from the EFI memory map.
767          * We must allow mmap of them for programs like X, so we
768          * currently can't do any useful validation.
769          */
770         return 1;
771 }
772
773 pgprot_t
774 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
775                      pgprot_t vma_prot)
776 {
777         unsigned long phys_addr = pfn << PAGE_SHIFT;
778         u64 attr;
779
780         /*
781          * For /dev/mem mmap, we use user mappings, but if the region is
782          * in kern_memmap (and hence may be covered by a kernel mapping),
783          * we must use the same attribute as the kernel mapping.
784          */
785         attr = kern_mem_attribute(phys_addr, size);
786         if (attr & EFI_MEMORY_WB)
787                 return pgprot_cacheable(vma_prot);
788         else if (attr & EFI_MEMORY_UC)
789                 return pgprot_noncached(vma_prot);
790
791         /*
792          * Some chipsets don't support UC access to memory.  If
793          * WB is supported, we prefer that.
794          */
795         if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
796                 return pgprot_cacheable(vma_prot);
797
798         return pgprot_noncached(vma_prot);
799 }
800
801 int __init
802 efi_uart_console_only(void)
803 {
804         efi_status_t status;
805         char *s, name[] = "ConOut";
806         efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
807         efi_char16_t *utf16, name_utf16[32];
808         unsigned char data[1024];
809         unsigned long size = sizeof(data);
810         struct efi_generic_dev_path *hdr, *end_addr;
811         int uart = 0;
812
813         /* Convert to UTF-16 */
814         utf16 = name_utf16;
815         s = name;
816         while (*s)
817                 *utf16++ = *s++ & 0x7f;
818         *utf16 = 0;
819
820         status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
821         if (status != EFI_SUCCESS) {
822                 printk(KERN_ERR "No EFI %s variable?\n", name);
823                 return 0;
824         }
825
826         hdr = (struct efi_generic_dev_path *) data;
827         end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
828         while (hdr < end_addr) {
829                 if (hdr->type == EFI_DEV_MSG &&
830                     hdr->sub_type == EFI_DEV_MSG_UART)
831                         uart = 1;
832                 else if (hdr->type == EFI_DEV_END_PATH ||
833                           hdr->type == EFI_DEV_END_PATH2) {
834                         if (!uart)
835                                 return 0;
836                         if (hdr->sub_type == EFI_DEV_END_ENTIRE)
837                                 return 1;
838                         uart = 0;
839                 }
840                 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
841         }
842         printk(KERN_ERR "Malformed %s value\n", name);
843         return 0;
844 }
845
846 /*
847  * Look for the first granule aligned memory descriptor memory
848  * that is big enough to hold EFI memory map. Make sure this
849  * descriptor is atleast granule sized so it does not get trimmed
850  */
851 struct kern_memdesc *
852 find_memmap_space (void)
853 {
854         u64     contig_low=0, contig_high=0;
855         u64     as = 0, ae;
856         void *efi_map_start, *efi_map_end, *p, *q;
857         efi_memory_desc_t *md, *pmd = NULL, *check_md;
858         u64     space_needed, efi_desc_size;
859         unsigned long total_mem = 0;
860
861         efi_map_start = __va(ia64_boot_param->efi_memmap);
862         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
863         efi_desc_size = ia64_boot_param->efi_memdesc_size;
864
865         /*
866          * Worst case: we need 3 kernel descriptors for each efi descriptor
867          * (if every entry has a WB part in the middle, and UC head and tail),
868          * plus one for the end marker.
869          */
870         space_needed = sizeof(kern_memdesc_t) *
871                 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
872
873         for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
874                 md = p;
875                 if (!efi_wb(md)) {
876                         continue;
877                 }
878                 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
879                         contig_low = GRANULEROUNDUP(md->phys_addr);
880                         contig_high = efi_md_end(md);
881                         for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
882                                 check_md = q;
883                                 if (!efi_wb(check_md))
884                                         break;
885                                 if (contig_high != check_md->phys_addr)
886                                         break;
887                                 contig_high = efi_md_end(check_md);
888                         }
889                         contig_high = GRANULEROUNDDOWN(contig_high);
890                 }
891                 if (!is_available_memory(md) || md->type == EFI_LOADER_DATA)
892                         continue;
893
894                 /* Round ends inward to granule boundaries */
895                 as = max(contig_low, md->phys_addr);
896                 ae = min(contig_high, 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
910                 if (ae - as > space_needed)
911                         break;
912         }
913         if (p >= efi_map_end)
914                 panic("Can't allocate space for kernel memory descriptors");
915
916         return __va(as);
917 }
918
919 /*
920  * Walk the EFI memory map and gather all memory available for kernel
921  * to use.  We can allocate partial granules only if the unavailable
922  * parts exist, and are WB.
923  */
924 void
925 efi_memmap_init(unsigned long *s, unsigned long *e)
926 {
927         struct kern_memdesc *k, *prev = 0;
928         u64     contig_low=0, contig_high=0;
929         u64     as, ae, lim;
930         void *efi_map_start, *efi_map_end, *p, *q;
931         efi_memory_desc_t *md, *pmd = NULL, *check_md;
932         u64     efi_desc_size;
933         unsigned long total_mem = 0;
934
935         k = kern_memmap = find_memmap_space();
936
937         efi_map_start = __va(ia64_boot_param->efi_memmap);
938         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
939         efi_desc_size = ia64_boot_param->efi_memdesc_size;
940
941         for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
942                 md = p;
943                 if (!efi_wb(md)) {
944                         if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
945                                            md->type == EFI_BOOT_SERVICES_DATA)) {
946                                 k->attribute = EFI_MEMORY_UC;
947                                 k->start = md->phys_addr;
948                                 k->num_pages = md->num_pages;
949                                 k++;
950                         }
951                         continue;
952                 }
953                 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
954                         contig_low = GRANULEROUNDUP(md->phys_addr);
955                         contig_high = efi_md_end(md);
956                         for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
957                                 check_md = q;
958                                 if (!efi_wb(check_md))
959                                         break;
960                                 if (contig_high != check_md->phys_addr)
961                                         break;
962                                 contig_high = efi_md_end(check_md);
963                         }
964                         contig_high = GRANULEROUNDDOWN(contig_high);
965                 }
966                 if (!is_available_memory(md))
967                         continue;
968
969                 /*
970                  * Round ends inward to granule boundaries
971                  * Give trimmings to uncached allocator
972                  */
973                 if (md->phys_addr < contig_low) {
974                         lim = min(efi_md_end(md), contig_low);
975                         if (efi_uc(md)) {
976                                 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
977                                     kmd_end(k-1) == md->phys_addr) {
978                                         (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
979                                 } else {
980                                         k->attribute = EFI_MEMORY_UC;
981                                         k->start = md->phys_addr;
982                                         k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
983                                         k++;
984                                 }
985                         }
986                         as = contig_low;
987                 } else
988                         as = md->phys_addr;
989
990                 if (efi_md_end(md) > contig_high) {
991                         lim = max(md->phys_addr, contig_high);
992                         if (efi_uc(md)) {
993                                 if (lim == md->phys_addr && k > kern_memmap &&
994                                     (k-1)->attribute == EFI_MEMORY_UC &&
995                                     kmd_end(k-1) == md->phys_addr) {
996                                         (k-1)->num_pages += md->num_pages;
997                                 } else {
998                                         k->attribute = EFI_MEMORY_UC;
999                                         k->start = lim;
1000                                         k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1001                                         k++;
1002                                 }
1003                         }
1004                         ae = contig_high;
1005                 } else
1006                         ae = efi_md_end(md);
1007
1008                 /* keep within max_addr= command line arg */
1009                 ae = min(ae, max_addr);
1010                 if (ae <= as)
1011                         continue;
1012
1013                 /* avoid going over mem= command line arg */
1014                 if (total_mem + (ae - as) > mem_limit)
1015                         ae -= total_mem + (ae - as) - mem_limit;
1016
1017                 if (ae <= as)
1018                         continue;
1019                 if (prev && kmd_end(prev) == md->phys_addr) {
1020                         prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1021                         total_mem += ae - as;
1022                         continue;
1023                 }
1024                 k->attribute = EFI_MEMORY_WB;
1025                 k->start = as;
1026                 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1027                 total_mem += ae - as;
1028                 prev = k++;
1029         }
1030         k->start = ~0L; /* end-marker */
1031
1032         /* reserve the memory we are using for kern_memmap */
1033         *s = (u64)kern_memmap;
1034         *e = (u64)++k;
1035 }
1036
1037 void
1038 efi_initialize_iomem_resources(struct resource *code_resource,
1039                                struct resource *data_resource)
1040 {
1041         struct resource *res;
1042         void *efi_map_start, *efi_map_end, *p;
1043         efi_memory_desc_t *md;
1044         u64 efi_desc_size;
1045         char *name;
1046         unsigned long flags;
1047
1048         efi_map_start = __va(ia64_boot_param->efi_memmap);
1049         efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1050         efi_desc_size = ia64_boot_param->efi_memdesc_size;
1051
1052         res = NULL;
1053
1054         for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1055                 md = p;
1056
1057                 if (md->num_pages == 0) /* should not happen */
1058                         continue;
1059
1060                 flags = IORESOURCE_MEM;
1061                 switch (md->type) {
1062
1063                         case EFI_MEMORY_MAPPED_IO:
1064                         case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1065                                 continue;
1066
1067                         case EFI_LOADER_CODE:
1068                         case EFI_LOADER_DATA:
1069                         case EFI_BOOT_SERVICES_DATA:
1070                         case EFI_BOOT_SERVICES_CODE:
1071                         case EFI_CONVENTIONAL_MEMORY:
1072                                 if (md->attribute & EFI_MEMORY_WP) {
1073                                         name = "System ROM";
1074                                         flags |= IORESOURCE_READONLY;
1075                                 } else {
1076                                         name = "System RAM";
1077                                 }
1078                                 break;
1079
1080                         case EFI_ACPI_MEMORY_NVS:
1081                                 name = "ACPI Non-volatile Storage";
1082                                 flags |= IORESOURCE_BUSY;
1083                                 break;
1084
1085                         case EFI_UNUSABLE_MEMORY:
1086                                 name = "reserved";
1087                                 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1088                                 break;
1089
1090                         case EFI_RESERVED_TYPE:
1091                         case EFI_RUNTIME_SERVICES_CODE:
1092                         case EFI_RUNTIME_SERVICES_DATA:
1093                         case EFI_ACPI_RECLAIM_MEMORY:
1094                         default:
1095                                 name = "reserved";
1096                                 flags |= IORESOURCE_BUSY;
1097                                 break;
1098                 }
1099
1100                 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1101                         printk(KERN_ERR "failed to alocate resource for iomem\n");
1102                         return;
1103                 }
1104
1105                 res->name = name;
1106                 res->start = md->phys_addr;
1107                 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1108                 res->flags = flags;
1109
1110                 if (insert_resource(&iomem_resource, res) < 0)
1111                         kfree(res);
1112                 else {
1113                         /*
1114                          * We don't know which region contains
1115                          * kernel data so we try it repeatedly and
1116                          * let the resource manager test it.
1117                          */
1118                         insert_resource(res, code_resource);
1119                         insert_resource(res, data_resource);
1120                 }
1121         }
1122 }