2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
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>
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
18 * Implemented EFI runtime services and virtual mode calls. --davidm
20 * Goutham Rao: <goutham.rao@intel.com>
21 * Skip non-WB memory and ignore empty memory ranges.
23 #include <linux/module.h>
24 #include <linux/bootmem.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 #include <linux/kexec.h>
33 #include <asm/kregs.h>
34 #include <asm/meminit.h>
35 #include <asm/pgtable.h>
36 #include <asm/processor.h>
41 extern efi_status_t efi_call_phys (void *, ...);
45 static efi_runtime_services_t *runtime;
46 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
48 #define efi_call_virt(f, args...) (*(f))(args)
50 #define STUB_GET_TIME(prefix, adjust_arg) \
52 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
54 struct ia64_fpreg fr[6]; \
55 efi_time_cap_t *atc = NULL; \
59 atc = adjust_arg(tc); \
60 ia64_save_scratch_fpregs(fr); \
61 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
62 ia64_load_scratch_fpregs(fr); \
66 #define STUB_SET_TIME(prefix, adjust_arg) \
68 prefix##_set_time (efi_time_t *tm) \
70 struct ia64_fpreg fr[6]; \
73 ia64_save_scratch_fpregs(fr); \
74 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
75 ia64_load_scratch_fpregs(fr); \
79 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
81 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
83 struct ia64_fpreg fr[6]; \
86 ia64_save_scratch_fpregs(fr); \
87 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
88 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
89 ia64_load_scratch_fpregs(fr); \
93 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
95 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
97 struct ia64_fpreg fr[6]; \
98 efi_time_t *atm = NULL; \
102 atm = adjust_arg(tm); \
103 ia64_save_scratch_fpregs(fr); \
104 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
106 ia64_load_scratch_fpregs(fr); \
110 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
111 static efi_status_t \
112 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
113 unsigned long *data_size, void *data) \
115 struct ia64_fpreg fr[6]; \
120 aattr = adjust_arg(attr); \
121 ia64_save_scratch_fpregs(fr); \
122 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
123 adjust_arg(name), adjust_arg(vendor), aattr, \
124 adjust_arg(data_size), adjust_arg(data)); \
125 ia64_load_scratch_fpregs(fr); \
129 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
130 static efi_status_t \
131 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
133 struct ia64_fpreg fr[6]; \
136 ia64_save_scratch_fpregs(fr); \
137 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
138 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
139 ia64_load_scratch_fpregs(fr); \
143 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
144 static efi_status_t \
145 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
146 unsigned long data_size, void *data) \
148 struct ia64_fpreg fr[6]; \
151 ia64_save_scratch_fpregs(fr); \
152 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
153 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
155 ia64_load_scratch_fpregs(fr); \
159 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
160 static efi_status_t \
161 prefix##_get_next_high_mono_count (u32 *count) \
163 struct ia64_fpreg fr[6]; \
166 ia64_save_scratch_fpregs(fr); \
167 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
168 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
169 ia64_load_scratch_fpregs(fr); \
173 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
175 prefix##_reset_system (int reset_type, efi_status_t status, \
176 unsigned long data_size, efi_char16_t *data) \
178 struct ia64_fpreg fr[6]; \
179 efi_char16_t *adata = NULL; \
182 adata = adjust_arg(data); \
184 ia64_save_scratch_fpregs(fr); \
185 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
186 reset_type, status, data_size, adata); \
187 /* should not return, but just in case... */ \
188 ia64_load_scratch_fpregs(fr); \
191 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
193 STUB_GET_TIME(phys, phys_ptr)
194 STUB_SET_TIME(phys, phys_ptr)
195 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
196 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
197 STUB_GET_VARIABLE(phys, phys_ptr)
198 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
199 STUB_SET_VARIABLE(phys, phys_ptr)
200 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
201 STUB_RESET_SYSTEM(phys, phys_ptr)
205 STUB_GET_TIME(virt, id)
206 STUB_SET_TIME(virt, id)
207 STUB_GET_WAKEUP_TIME(virt, id)
208 STUB_SET_WAKEUP_TIME(virt, id)
209 STUB_GET_VARIABLE(virt, id)
210 STUB_GET_NEXT_VARIABLE(virt, id)
211 STUB_SET_VARIABLE(virt, id)
212 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
213 STUB_RESET_SYSTEM(virt, id)
216 efi_gettimeofday (struct timespec *ts)
220 memset(ts, 0, sizeof(ts));
221 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
224 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
225 ts->tv_nsec = tm.nanosecond;
229 is_memory_available (efi_memory_desc_t *md)
231 if (!(md->attribute & EFI_MEMORY_WB))
235 case EFI_LOADER_CODE:
236 case EFI_LOADER_DATA:
237 case EFI_BOOT_SERVICES_CODE:
238 case EFI_BOOT_SERVICES_DATA:
239 case EFI_CONVENTIONAL_MEMORY:
245 typedef struct kern_memdesc {
251 static kern_memdesc_t *kern_memmap;
253 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
256 kmd_end(kern_memdesc_t *kmd)
258 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
262 efi_md_end(efi_memory_desc_t *md)
264 return (md->phys_addr + efi_md_size(md));
268 efi_wb(efi_memory_desc_t *md)
270 return (md->attribute & EFI_MEMORY_WB);
274 efi_uc(efi_memory_desc_t *md)
276 return (md->attribute & EFI_MEMORY_UC);
280 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
283 u64 start, end, voff;
285 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
286 for (k = kern_memmap; k->start != ~0UL; k++) {
287 if (k->attribute != attr)
289 start = PAGE_ALIGN(k->start);
290 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
292 if ((*callback)(start + voff, end + voff, arg) < 0)
298 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
299 * has memory that is available for OS use.
302 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
304 walk(callback, arg, EFI_MEMORY_WB);
308 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
309 * has memory that is available for uncached allocator.
312 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
314 walk(callback, arg, EFI_MEMORY_UC);
318 * Look for the PAL_CODE region reported by EFI and maps it using an
319 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
320 * Abstraction Layer chapter 11 in ADAG
324 efi_get_pal_addr (void)
326 void *efi_map_start, *efi_map_end, *p;
327 efi_memory_desc_t *md;
329 int pal_code_count = 0;
332 efi_map_start = __va(ia64_boot_param->efi_memmap);
333 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
334 efi_desc_size = ia64_boot_param->efi_memdesc_size;
336 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
338 if (md->type != EFI_PAL_CODE)
341 if (++pal_code_count > 1) {
342 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
347 * The only ITLB entry in region 7 that is used is the one installed by
348 * __start(). That entry covers a 64MB range.
350 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
351 vaddr = PAGE_OFFSET + md->phys_addr;
354 * We must check that the PAL mapping won't overlap with the kernel
357 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
358 * 256KB and that only one ITR is needed to map it. This implies that the
359 * PAL code is always aligned on its size, i.e., the closest matching page
360 * size supported by the TLB. Therefore PAL code is guaranteed never to
361 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
362 * now the following test is enough to determine whether or not we need a
363 * dedicated ITR for the PAL code.
365 if ((vaddr & mask) == (KERNEL_START & mask)) {
366 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
371 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
372 panic("Woah! PAL code size bigger than a granule!");
375 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
377 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
378 smp_processor_id(), md->phys_addr,
379 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
380 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
382 return __va(md->phys_addr);
384 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
390 efi_map_pal_code (void)
392 void *pal_vaddr = efi_get_pal_addr ();
399 * Cannot write to CRx with PSR.ic=1
401 psr = ia64_clear_ic();
402 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
403 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
405 ia64_set_psr(psr); /* restore psr */
412 void *efi_map_start, *efi_map_end;
413 efi_config_table_t *config_tables;
416 char *cp, vendor[100] = "unknown";
419 /* it's too early to be able to use the standard kernel command line support... */
420 for (cp = boot_command_line; *cp; ) {
421 if (memcmp(cp, "mem=", 4) == 0) {
422 mem_limit = memparse(cp + 4, &cp);
423 } else if (memcmp(cp, "max_addr=", 9) == 0) {
424 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
425 } else if (memcmp(cp, "min_addr=", 9) == 0) {
426 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
428 while (*cp != ' ' && *cp)
435 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
436 if (max_addr != ~0UL)
437 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
439 efi.systab = __va(ia64_boot_param->efi_systab);
442 * Verify the EFI Table
444 if (efi.systab == NULL)
445 panic("Woah! Can't find EFI system table.\n");
446 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
447 panic("Woah! EFI system table signature incorrect\n");
448 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
449 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
450 "got %d.%02d, expected %d.%02d\n",
451 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
452 EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
454 config_tables = __va(efi.systab->tables);
456 /* Show what we know for posterity */
457 c16 = __va(efi.systab->fw_vendor);
459 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
464 printk(KERN_INFO "EFI v%u.%.02u by %s:",
465 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
467 efi.mps = EFI_INVALID_TABLE_ADDR;
468 efi.acpi = EFI_INVALID_TABLE_ADDR;
469 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
470 efi.smbios = EFI_INVALID_TABLE_ADDR;
471 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
472 efi.boot_info = EFI_INVALID_TABLE_ADDR;
473 efi.hcdp = EFI_INVALID_TABLE_ADDR;
474 efi.uga = EFI_INVALID_TABLE_ADDR;
476 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
477 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
478 efi.mps = config_tables[i].table;
479 printk(" MPS=0x%lx", config_tables[i].table);
480 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
481 efi.acpi20 = config_tables[i].table;
482 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
483 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
484 efi.acpi = config_tables[i].table;
485 printk(" ACPI=0x%lx", config_tables[i].table);
486 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
487 efi.smbios = config_tables[i].table;
488 printk(" SMBIOS=0x%lx", config_tables[i].table);
489 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
490 efi.sal_systab = config_tables[i].table;
491 printk(" SALsystab=0x%lx", config_tables[i].table);
492 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
493 efi.hcdp = config_tables[i].table;
494 printk(" HCDP=0x%lx", config_tables[i].table);
499 runtime = __va(efi.systab->runtime);
500 efi.get_time = phys_get_time;
501 efi.set_time = phys_set_time;
502 efi.get_wakeup_time = phys_get_wakeup_time;
503 efi.set_wakeup_time = phys_set_wakeup_time;
504 efi.get_variable = phys_get_variable;
505 efi.get_next_variable = phys_get_next_variable;
506 efi.set_variable = phys_set_variable;
507 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
508 efi.reset_system = phys_reset_system;
510 efi_map_start = __va(ia64_boot_param->efi_memmap);
511 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
512 efi_desc_size = ia64_boot_param->efi_memdesc_size;
515 /* print EFI memory map: */
517 efi_memory_desc_t *md;
520 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
522 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
523 i, md->type, md->attribute, md->phys_addr,
524 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
525 md->num_pages >> (20 - EFI_PAGE_SHIFT));
531 efi_enter_virtual_mode();
535 efi_enter_virtual_mode (void)
537 void *efi_map_start, *efi_map_end, *p;
538 efi_memory_desc_t *md;
542 efi_map_start = __va(ia64_boot_param->efi_memmap);
543 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
544 efi_desc_size = ia64_boot_param->efi_memdesc_size;
546 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
548 if (md->attribute & EFI_MEMORY_RUNTIME) {
550 * Some descriptors have multiple bits set, so the order of
551 * the tests is relevant.
553 if (md->attribute & EFI_MEMORY_WB) {
554 md->virt_addr = (u64) __va(md->phys_addr);
555 } else if (md->attribute & EFI_MEMORY_UC) {
556 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
557 } else if (md->attribute & EFI_MEMORY_WC) {
559 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
565 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
566 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
568 } else if (md->attribute & EFI_MEMORY_WT) {
570 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
571 | _PAGE_D | _PAGE_MA_WT
575 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
576 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
582 status = efi_call_phys(__va(runtime->set_virtual_address_map),
583 ia64_boot_param->efi_memmap_size,
584 efi_desc_size, ia64_boot_param->efi_memdesc_version,
585 ia64_boot_param->efi_memmap);
586 if (status != EFI_SUCCESS) {
587 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
588 "(status=%lu)\n", status);
593 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
595 efi.get_time = virt_get_time;
596 efi.set_time = virt_set_time;
597 efi.get_wakeup_time = virt_get_wakeup_time;
598 efi.set_wakeup_time = virt_set_wakeup_time;
599 efi.get_variable = virt_get_variable;
600 efi.get_next_variable = virt_get_next_variable;
601 efi.set_variable = virt_set_variable;
602 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
603 efi.reset_system = virt_reset_system;
607 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
608 * this type, other I/O port ranges should be described via ACPI.
611 efi_get_iobase (void)
613 void *efi_map_start, *efi_map_end, *p;
614 efi_memory_desc_t *md;
617 efi_map_start = __va(ia64_boot_param->efi_memmap);
618 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
619 efi_desc_size = ia64_boot_param->efi_memdesc_size;
621 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
623 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
624 if (md->attribute & EFI_MEMORY_UC)
625 return md->phys_addr;
631 static struct kern_memdesc *
632 kern_memory_descriptor (unsigned long phys_addr)
634 struct kern_memdesc *md;
636 for (md = kern_memmap; md->start != ~0UL; md++) {
637 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
643 static efi_memory_desc_t *
644 efi_memory_descriptor (unsigned long phys_addr)
646 void *efi_map_start, *efi_map_end, *p;
647 efi_memory_desc_t *md;
650 efi_map_start = __va(ia64_boot_param->efi_memmap);
651 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
652 efi_desc_size = ia64_boot_param->efi_memdesc_size;
654 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
657 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
664 efi_mem_type (unsigned long phys_addr)
666 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
674 efi_mem_attributes (unsigned long phys_addr)
676 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
679 return md->attribute;
682 EXPORT_SYMBOL(efi_mem_attributes);
685 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
687 unsigned long end = phys_addr + size;
688 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
695 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
696 * the kernel that firmware needs this region mapped.
698 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
700 unsigned long md_end = efi_md_end(md);
705 md = efi_memory_descriptor(md_end);
706 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
713 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
715 unsigned long end = phys_addr + size;
716 struct kern_memdesc *md;
720 * This is a hack for ioremap calls before we set up kern_memmap.
721 * Maybe we should do efi_memmap_init() earlier instead.
724 attr = efi_mem_attribute(phys_addr, size);
725 if (attr & EFI_MEMORY_WB)
726 return EFI_MEMORY_WB;
730 md = kern_memory_descriptor(phys_addr);
734 attr = md->attribute;
736 unsigned long md_end = kmd_end(md);
741 md = kern_memory_descriptor(md_end);
742 if (!md || md->attribute != attr)
747 EXPORT_SYMBOL(kern_mem_attribute);
750 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
755 * /dev/mem reads and writes use copy_to_user(), which implicitly
756 * uses a granule-sized kernel identity mapping. It's really
757 * only safe to do this for regions in kern_memmap. For more
758 * details, see Documentation/ia64/aliasing.txt.
760 attr = kern_mem_attribute(phys_addr, size);
761 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
767 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
770 * MMIO regions are often missing from the EFI memory map.
771 * We must allow mmap of them for programs like X, so we
772 * currently can't do any useful validation.
778 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
781 unsigned long phys_addr = pfn << PAGE_SHIFT;
785 * For /dev/mem mmap, we use user mappings, but if the region is
786 * in kern_memmap (and hence may be covered by a kernel mapping),
787 * we must use the same attribute as the kernel mapping.
789 attr = kern_mem_attribute(phys_addr, size);
790 if (attr & EFI_MEMORY_WB)
791 return pgprot_cacheable(vma_prot);
792 else if (attr & EFI_MEMORY_UC)
793 return pgprot_noncached(vma_prot);
796 * Some chipsets don't support UC access to memory. If
797 * WB is supported, we prefer that.
799 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
800 return pgprot_cacheable(vma_prot);
802 return pgprot_noncached(vma_prot);
806 efi_uart_console_only(void)
809 char *s, name[] = "ConOut";
810 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
811 efi_char16_t *utf16, name_utf16[32];
812 unsigned char data[1024];
813 unsigned long size = sizeof(data);
814 struct efi_generic_dev_path *hdr, *end_addr;
817 /* Convert to UTF-16 */
821 *utf16++ = *s++ & 0x7f;
824 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
825 if (status != EFI_SUCCESS) {
826 printk(KERN_ERR "No EFI %s variable?\n", name);
830 hdr = (struct efi_generic_dev_path *) data;
831 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
832 while (hdr < end_addr) {
833 if (hdr->type == EFI_DEV_MSG &&
834 hdr->sub_type == EFI_DEV_MSG_UART)
836 else if (hdr->type == EFI_DEV_END_PATH ||
837 hdr->type == EFI_DEV_END_PATH2) {
840 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
844 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
846 printk(KERN_ERR "Malformed %s value\n", name);
851 * Look for the first granule aligned memory descriptor memory
852 * that is big enough to hold EFI memory map. Make sure this
853 * descriptor is atleast granule sized so it does not get trimmed
855 struct kern_memdesc *
856 find_memmap_space (void)
858 u64 contig_low=0, contig_high=0;
860 void *efi_map_start, *efi_map_end, *p, *q;
861 efi_memory_desc_t *md, *pmd = NULL, *check_md;
862 u64 space_needed, efi_desc_size;
863 unsigned long total_mem = 0;
865 efi_map_start = __va(ia64_boot_param->efi_memmap);
866 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
867 efi_desc_size = ia64_boot_param->efi_memdesc_size;
870 * Worst case: we need 3 kernel descriptors for each efi descriptor
871 * (if every entry has a WB part in the middle, and UC head and tail),
872 * plus one for the end marker.
874 space_needed = sizeof(kern_memdesc_t) *
875 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
877 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
882 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
883 contig_low = GRANULEROUNDUP(md->phys_addr);
884 contig_high = efi_md_end(md);
885 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
887 if (!efi_wb(check_md))
889 if (contig_high != check_md->phys_addr)
891 contig_high = efi_md_end(check_md);
893 contig_high = GRANULEROUNDDOWN(contig_high);
895 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
898 /* Round ends inward to granule boundaries */
899 as = max(contig_low, md->phys_addr);
900 ae = min(contig_high, efi_md_end(md));
902 /* keep within max_addr= and min_addr= command line arg */
903 as = max(as, min_addr);
904 ae = min(ae, max_addr);
908 /* avoid going over mem= command line arg */
909 if (total_mem + (ae - as) > mem_limit)
910 ae -= total_mem + (ae - as) - mem_limit;
915 if (ae - as > space_needed)
918 if (p >= efi_map_end)
919 panic("Can't allocate space for kernel memory descriptors");
925 * Walk the EFI memory map and gather all memory available for kernel
926 * to use. We can allocate partial granules only if the unavailable
927 * parts exist, and are WB.
930 efi_memmap_init(unsigned long *s, unsigned long *e)
932 struct kern_memdesc *k, *prev = NULL;
933 u64 contig_low=0, contig_high=0;
935 void *efi_map_start, *efi_map_end, *p, *q;
936 efi_memory_desc_t *md, *pmd = NULL, *check_md;
938 unsigned long total_mem = 0;
940 k = kern_memmap = find_memmap_space();
942 efi_map_start = __va(ia64_boot_param->efi_memmap);
943 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
944 efi_desc_size = ia64_boot_param->efi_memdesc_size;
946 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
949 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
950 md->type == EFI_BOOT_SERVICES_DATA)) {
951 k->attribute = EFI_MEMORY_UC;
952 k->start = md->phys_addr;
953 k->num_pages = md->num_pages;
958 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
959 contig_low = GRANULEROUNDUP(md->phys_addr);
960 contig_high = efi_md_end(md);
961 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
963 if (!efi_wb(check_md))
965 if (contig_high != check_md->phys_addr)
967 contig_high = efi_md_end(check_md);
969 contig_high = GRANULEROUNDDOWN(contig_high);
971 if (!is_memory_available(md))
975 * Round ends inward to granule boundaries
976 * Give trimmings to uncached allocator
978 if (md->phys_addr < contig_low) {
979 lim = min(efi_md_end(md), contig_low);
981 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
982 kmd_end(k-1) == md->phys_addr) {
983 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
985 k->attribute = EFI_MEMORY_UC;
986 k->start = md->phys_addr;
987 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
995 if (efi_md_end(md) > contig_high) {
996 lim = max(md->phys_addr, contig_high);
998 if (lim == md->phys_addr && k > kern_memmap &&
999 (k-1)->attribute == EFI_MEMORY_UC &&
1000 kmd_end(k-1) == md->phys_addr) {
1001 (k-1)->num_pages += md->num_pages;
1003 k->attribute = EFI_MEMORY_UC;
1005 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1011 ae = efi_md_end(md);
1013 #ifdef CONFIG_CRASH_DUMP
1014 /* saved_max_pfn should ignore max_addr= command line arg */
1015 if (saved_max_pfn < (ae >> PAGE_SHIFT))
1016 saved_max_pfn = (ae >> PAGE_SHIFT);
1018 /* keep within max_addr= and min_addr= command line arg */
1019 as = max(as, min_addr);
1020 ae = min(ae, max_addr);
1024 /* avoid going over mem= command line arg */
1025 if (total_mem + (ae - as) > mem_limit)
1026 ae -= total_mem + (ae - as) - mem_limit;
1030 if (prev && kmd_end(prev) == md->phys_addr) {
1031 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1032 total_mem += ae - as;
1035 k->attribute = EFI_MEMORY_WB;
1037 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1038 total_mem += ae - as;
1041 k->start = ~0L; /* end-marker */
1043 /* reserve the memory we are using for kern_memmap */
1044 *s = (u64)kern_memmap;
1049 efi_initialize_iomem_resources(struct resource *code_resource,
1050 struct resource *data_resource)
1052 struct resource *res;
1053 void *efi_map_start, *efi_map_end, *p;
1054 efi_memory_desc_t *md;
1057 unsigned long flags;
1059 efi_map_start = __va(ia64_boot_param->efi_memmap);
1060 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1061 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1065 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1068 if (md->num_pages == 0) /* should not happen */
1071 flags = IORESOURCE_MEM;
1074 case EFI_MEMORY_MAPPED_IO:
1075 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1078 case EFI_LOADER_CODE:
1079 case EFI_LOADER_DATA:
1080 case EFI_BOOT_SERVICES_DATA:
1081 case EFI_BOOT_SERVICES_CODE:
1082 case EFI_CONVENTIONAL_MEMORY:
1083 if (md->attribute & EFI_MEMORY_WP) {
1084 name = "System ROM";
1085 flags |= IORESOURCE_READONLY;
1087 name = "System RAM";
1091 case EFI_ACPI_MEMORY_NVS:
1092 name = "ACPI Non-volatile Storage";
1093 flags |= IORESOURCE_BUSY;
1096 case EFI_UNUSABLE_MEMORY:
1098 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1101 case EFI_RESERVED_TYPE:
1102 case EFI_RUNTIME_SERVICES_CODE:
1103 case EFI_RUNTIME_SERVICES_DATA:
1104 case EFI_ACPI_RECLAIM_MEMORY:
1107 flags |= IORESOURCE_BUSY;
1111 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1112 printk(KERN_ERR "failed to alocate resource for iomem\n");
1117 res->start = md->phys_addr;
1118 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1121 if (insert_resource(&iomem_resource, res) < 0)
1125 * We don't know which region contains
1126 * kernel data so we try it repeatedly and
1127 * let the resource manager test it.
1129 insert_resource(res, code_resource);
1130 insert_resource(res, data_resource);
1132 insert_resource(res, &efi_memmap_res);
1133 insert_resource(res, &boot_param_res);
1134 if (crashk_res.end > crashk_res.start)
1135 insert_resource(res, &crashk_res);
1142 /* find a block of memory aligned to 64M exclude reserved regions
1143 rsvd_regions are sorted
1146 kdump_find_rsvd_region (unsigned long size,
1147 struct rsvd_region *r, int n)
1151 u64 alignment = 1UL << _PAGE_SIZE_64M;
1152 void *efi_map_start, *efi_map_end, *p;
1153 efi_memory_desc_t *md;
1156 efi_map_start = __va(ia64_boot_param->efi_memmap);
1157 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1158 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1160 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1164 start = ALIGN(md->phys_addr, alignment);
1165 end = efi_md_end(md);
1166 for (i = 0; i < n; i++) {
1167 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1168 if (__pa(r[i].start) > start + size)
1170 start = ALIGN(__pa(r[i].end), alignment);
1171 if (i < n-1 && __pa(r[i+1].start) < start + size)
1177 if (end > start + size)
1181 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",
1187 #ifdef CONFIG_PROC_VMCORE
1188 /* locate the size find a the descriptor at a certain address */
1190 vmcore_find_descriptor_size (unsigned long address)
1192 void *efi_map_start, *efi_map_end, *p;
1193 efi_memory_desc_t *md;
1195 unsigned long ret = 0;
1197 efi_map_start = __va(ia64_boot_param->efi_memmap);
1198 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1199 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1201 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1203 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1204 && md->phys_addr == address) {
1205 ret = efi_md_size(md);
1211 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");