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