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>
34 #include <asm/kregs.h>
35 #include <asm/meminit.h>
36 #include <asm/pgtable.h>
37 #include <asm/processor.h>
42 extern efi_status_t efi_call_phys (void *, ...);
46 static efi_runtime_services_t *runtime;
47 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
49 #define efi_call_virt(f, args...) (*(f))(args)
51 #define STUB_GET_TIME(prefix, adjust_arg) \
53 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
55 struct ia64_fpreg fr[6]; \
56 efi_time_cap_t *atc = NULL; \
60 atc = adjust_arg(tc); \
61 ia64_save_scratch_fpregs(fr); \
62 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
63 ia64_load_scratch_fpregs(fr); \
67 #define STUB_SET_TIME(prefix, adjust_arg) \
69 prefix##_set_time (efi_time_t *tm) \
71 struct ia64_fpreg fr[6]; \
74 ia64_save_scratch_fpregs(fr); \
75 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
76 ia64_load_scratch_fpregs(fr); \
80 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
82 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
84 struct ia64_fpreg fr[6]; \
87 ia64_save_scratch_fpregs(fr); \
88 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
89 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
90 ia64_load_scratch_fpregs(fr); \
94 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
96 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
98 struct ia64_fpreg fr[6]; \
99 efi_time_t *atm = NULL; \
103 atm = adjust_arg(tm); \
104 ia64_save_scratch_fpregs(fr); \
105 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
107 ia64_load_scratch_fpregs(fr); \
111 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
112 static efi_status_t \
113 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
114 unsigned long *data_size, void *data) \
116 struct ia64_fpreg fr[6]; \
121 aattr = adjust_arg(attr); \
122 ia64_save_scratch_fpregs(fr); \
123 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
124 adjust_arg(name), adjust_arg(vendor), aattr, \
125 adjust_arg(data_size), adjust_arg(data)); \
126 ia64_load_scratch_fpregs(fr); \
130 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
131 static efi_status_t \
132 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
134 struct ia64_fpreg fr[6]; \
137 ia64_save_scratch_fpregs(fr); \
138 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
139 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
140 ia64_load_scratch_fpregs(fr); \
144 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
145 static efi_status_t \
146 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
147 unsigned long data_size, void *data) \
149 struct ia64_fpreg fr[6]; \
152 ia64_save_scratch_fpregs(fr); \
153 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
154 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
156 ia64_load_scratch_fpregs(fr); \
160 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
161 static efi_status_t \
162 prefix##_get_next_high_mono_count (u32 *count) \
164 struct ia64_fpreg fr[6]; \
167 ia64_save_scratch_fpregs(fr); \
168 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
169 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
170 ia64_load_scratch_fpregs(fr); \
174 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
176 prefix##_reset_system (int reset_type, efi_status_t status, \
177 unsigned long data_size, efi_char16_t *data) \
179 struct ia64_fpreg fr[6]; \
180 efi_char16_t *adata = NULL; \
183 adata = adjust_arg(data); \
185 ia64_save_scratch_fpregs(fr); \
186 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
187 reset_type, status, data_size, adata); \
188 /* should not return, but just in case... */ \
189 ia64_load_scratch_fpregs(fr); \
192 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
194 STUB_GET_TIME(phys, phys_ptr)
195 STUB_SET_TIME(phys, phys_ptr)
196 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
197 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
198 STUB_GET_VARIABLE(phys, phys_ptr)
199 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
200 STUB_SET_VARIABLE(phys, phys_ptr)
201 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
202 STUB_RESET_SYSTEM(phys, phys_ptr)
206 STUB_GET_TIME(virt, id)
207 STUB_SET_TIME(virt, id)
208 STUB_GET_WAKEUP_TIME(virt, id)
209 STUB_SET_WAKEUP_TIME(virt, id)
210 STUB_GET_VARIABLE(virt, id)
211 STUB_GET_NEXT_VARIABLE(virt, id)
212 STUB_SET_VARIABLE(virt, id)
213 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
214 STUB_RESET_SYSTEM(virt, id)
217 efi_gettimeofday (struct timespec *ts)
221 memset(ts, 0, sizeof(ts));
222 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
225 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
226 ts->tv_nsec = tm.nanosecond;
230 is_memory_available (efi_memory_desc_t *md)
232 if (!(md->attribute & EFI_MEMORY_WB))
236 case EFI_LOADER_CODE:
237 case EFI_LOADER_DATA:
238 case EFI_BOOT_SERVICES_CODE:
239 case EFI_BOOT_SERVICES_DATA:
240 case EFI_CONVENTIONAL_MEMORY:
246 typedef struct kern_memdesc {
252 static kern_memdesc_t *kern_memmap;
254 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
257 kmd_end(kern_memdesc_t *kmd)
259 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
263 efi_md_end(efi_memory_desc_t *md)
265 return (md->phys_addr + efi_md_size(md));
269 efi_wb(efi_memory_desc_t *md)
271 return (md->attribute & EFI_MEMORY_WB);
275 efi_uc(efi_memory_desc_t *md)
277 return (md->attribute & EFI_MEMORY_UC);
281 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
284 u64 start, end, voff;
286 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
287 for (k = kern_memmap; k->start != ~0UL; k++) {
288 if (k->attribute != attr)
290 start = PAGE_ALIGN(k->start);
291 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
293 if ((*callback)(start + voff, end + voff, arg) < 0)
299 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
300 * has memory that is available for OS use.
303 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
305 walk(callback, arg, EFI_MEMORY_WB);
309 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
310 * has memory that is available for uncached allocator.
313 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
315 walk(callback, arg, EFI_MEMORY_UC);
319 * Look for the PAL_CODE region reported by EFI and maps it using an
320 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
321 * Abstraction Layer chapter 11 in ADAG
325 efi_get_pal_addr (void)
327 void *efi_map_start, *efi_map_end, *p;
328 efi_memory_desc_t *md;
330 int pal_code_count = 0;
333 efi_map_start = __va(ia64_boot_param->efi_memmap);
334 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
335 efi_desc_size = ia64_boot_param->efi_memdesc_size;
337 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
339 if (md->type != EFI_PAL_CODE)
342 if (++pal_code_count > 1) {
343 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
348 * The only ITLB entry in region 7 that is used is the one installed by
349 * __start(). That entry covers a 64MB range.
351 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
352 vaddr = PAGE_OFFSET + md->phys_addr;
355 * We must check that the PAL mapping won't overlap with the kernel
358 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
359 * 256KB and that only one ITR is needed to map it. This implies that the
360 * PAL code is always aligned on its size, i.e., the closest matching page
361 * size supported by the TLB. Therefore PAL code is guaranteed never to
362 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
363 * now the following test is enough to determine whether or not we need a
364 * dedicated ITR for the PAL code.
366 if ((vaddr & mask) == (KERNEL_START & mask)) {
367 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
372 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
373 panic("Woah! PAL code size bigger than a granule!");
376 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
378 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
379 smp_processor_id(), md->phys_addr,
380 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
381 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
383 return __va(md->phys_addr);
385 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
391 efi_map_pal_code (void)
393 void *pal_vaddr = efi_get_pal_addr ();
400 * Cannot write to CRx with PSR.ic=1
402 psr = ia64_clear_ic();
403 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
404 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
406 ia64_set_psr(psr); /* restore psr */
413 void *efi_map_start, *efi_map_end;
414 efi_config_table_t *config_tables;
417 char *cp, vendor[100] = "unknown";
420 /* it's too early to be able to use the standard kernel command line support... */
421 for (cp = boot_command_line; *cp; ) {
422 if (memcmp(cp, "mem=", 4) == 0) {
423 mem_limit = memparse(cp + 4, &cp);
424 } else if (memcmp(cp, "max_addr=", 9) == 0) {
425 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
426 } else if (memcmp(cp, "min_addr=", 9) == 0) {
427 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
429 while (*cp != ' ' && *cp)
436 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
437 if (max_addr != ~0UL)
438 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
440 efi.systab = __va(ia64_boot_param->efi_systab);
443 * Verify the EFI Table
445 if (efi.systab == NULL)
446 panic("Woah! Can't find EFI system table.\n");
447 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
448 panic("Woah! EFI system table signature incorrect\n");
449 if ((efi.systab->hdr.revision >> 16) == 0)
450 printk(KERN_WARNING "Warning: EFI system table version "
451 "%d.%02d, expected 1.00 or greater\n",
452 efi.systab->hdr.revision >> 16,
453 efi.systab->hdr.revision & 0xffff);
455 config_tables = __va(efi.systab->tables);
457 /* Show what we know for posterity */
458 c16 = __va(efi.systab->fw_vendor);
460 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
465 printk(KERN_INFO "EFI v%u.%.02u by %s:",
466 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
468 efi.mps = EFI_INVALID_TABLE_ADDR;
469 efi.acpi = EFI_INVALID_TABLE_ADDR;
470 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
471 efi.smbios = EFI_INVALID_TABLE_ADDR;
472 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
473 efi.boot_info = EFI_INVALID_TABLE_ADDR;
474 efi.hcdp = EFI_INVALID_TABLE_ADDR;
475 efi.uga = EFI_INVALID_TABLE_ADDR;
477 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
478 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
479 efi.mps = config_tables[i].table;
480 printk(" MPS=0x%lx", config_tables[i].table);
481 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
482 efi.acpi20 = config_tables[i].table;
483 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
484 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
485 efi.acpi = config_tables[i].table;
486 printk(" ACPI=0x%lx", config_tables[i].table);
487 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
488 efi.smbios = config_tables[i].table;
489 printk(" SMBIOS=0x%lx", config_tables[i].table);
490 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
491 efi.sal_systab = config_tables[i].table;
492 printk(" SALsystab=0x%lx", config_tables[i].table);
493 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
494 efi.hcdp = config_tables[i].table;
495 printk(" HCDP=0x%lx", config_tables[i].table);
500 runtime = __va(efi.systab->runtime);
501 efi.get_time = phys_get_time;
502 efi.set_time = phys_set_time;
503 efi.get_wakeup_time = phys_get_wakeup_time;
504 efi.set_wakeup_time = phys_set_wakeup_time;
505 efi.get_variable = phys_get_variable;
506 efi.get_next_variable = phys_get_next_variable;
507 efi.set_variable = phys_set_variable;
508 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
509 efi.reset_system = phys_reset_system;
511 efi_map_start = __va(ia64_boot_param->efi_memmap);
512 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
513 efi_desc_size = ia64_boot_param->efi_memdesc_size;
516 /* print EFI memory map: */
518 efi_memory_desc_t *md;
521 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
523 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
524 i, md->type, md->attribute, md->phys_addr,
525 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
526 md->num_pages >> (20 - EFI_PAGE_SHIFT));
532 efi_enter_virtual_mode();
536 efi_enter_virtual_mode (void)
538 void *efi_map_start, *efi_map_end, *p;
539 efi_memory_desc_t *md;
543 efi_map_start = __va(ia64_boot_param->efi_memmap);
544 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
545 efi_desc_size = ia64_boot_param->efi_memdesc_size;
547 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
549 if (md->attribute & EFI_MEMORY_RUNTIME) {
551 * Some descriptors have multiple bits set, so the order of
552 * the tests is relevant.
554 if (md->attribute & EFI_MEMORY_WB) {
555 md->virt_addr = (u64) __va(md->phys_addr);
556 } else if (md->attribute & EFI_MEMORY_UC) {
557 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
558 } else if (md->attribute & EFI_MEMORY_WC) {
560 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
566 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
567 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
569 } else if (md->attribute & EFI_MEMORY_WT) {
571 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
572 | _PAGE_D | _PAGE_MA_WT
576 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
577 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
583 status = efi_call_phys(__va(runtime->set_virtual_address_map),
584 ia64_boot_param->efi_memmap_size,
585 efi_desc_size, ia64_boot_param->efi_memdesc_version,
586 ia64_boot_param->efi_memmap);
587 if (status != EFI_SUCCESS) {
588 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
589 "(status=%lu)\n", status);
594 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
596 efi.get_time = virt_get_time;
597 efi.set_time = virt_set_time;
598 efi.get_wakeup_time = virt_get_wakeup_time;
599 efi.set_wakeup_time = virt_set_wakeup_time;
600 efi.get_variable = virt_get_variable;
601 efi.get_next_variable = virt_get_next_variable;
602 efi.set_variable = virt_set_variable;
603 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
604 efi.reset_system = virt_reset_system;
608 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
609 * this type, other I/O port ranges should be described via ACPI.
612 efi_get_iobase (void)
614 void *efi_map_start, *efi_map_end, *p;
615 efi_memory_desc_t *md;
618 efi_map_start = __va(ia64_boot_param->efi_memmap);
619 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
620 efi_desc_size = ia64_boot_param->efi_memdesc_size;
622 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
624 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
625 if (md->attribute & EFI_MEMORY_UC)
626 return md->phys_addr;
632 static struct kern_memdesc *
633 kern_memory_descriptor (unsigned long phys_addr)
635 struct kern_memdesc *md;
637 for (md = kern_memmap; md->start != ~0UL; md++) {
638 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
644 static efi_memory_desc_t *
645 efi_memory_descriptor (unsigned long phys_addr)
647 void *efi_map_start, *efi_map_end, *p;
648 efi_memory_desc_t *md;
651 efi_map_start = __va(ia64_boot_param->efi_memmap);
652 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
653 efi_desc_size = ia64_boot_param->efi_memdesc_size;
655 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
658 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
665 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
667 void *efi_map_start, *efi_map_end, *p;
668 efi_memory_desc_t *md;
672 efi_map_start = __va(ia64_boot_param->efi_memmap);
673 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
674 efi_desc_size = ia64_boot_param->efi_memdesc_size;
676 end = phys_addr + size;
678 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
681 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
688 efi_mem_type (unsigned long phys_addr)
690 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
698 efi_mem_attributes (unsigned long phys_addr)
700 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
703 return md->attribute;
706 EXPORT_SYMBOL(efi_mem_attributes);
709 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
711 unsigned long end = phys_addr + size;
712 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
719 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
720 * the kernel that firmware needs this region mapped.
722 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
724 unsigned long md_end = efi_md_end(md);
729 md = efi_memory_descriptor(md_end);
730 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
737 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
739 unsigned long end = phys_addr + size;
740 struct kern_memdesc *md;
744 * This is a hack for ioremap calls before we set up kern_memmap.
745 * Maybe we should do efi_memmap_init() earlier instead.
748 attr = efi_mem_attribute(phys_addr, size);
749 if (attr & EFI_MEMORY_WB)
750 return EFI_MEMORY_WB;
754 md = kern_memory_descriptor(phys_addr);
758 attr = md->attribute;
760 unsigned long md_end = kmd_end(md);
765 md = kern_memory_descriptor(md_end);
766 if (!md || md->attribute != attr)
771 EXPORT_SYMBOL(kern_mem_attribute);
774 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
779 * /dev/mem reads and writes use copy_to_user(), which implicitly
780 * uses a granule-sized kernel identity mapping. It's really
781 * only safe to do this for regions in kern_memmap. For more
782 * details, see Documentation/ia64/aliasing.txt.
784 attr = kern_mem_attribute(phys_addr, size);
785 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
791 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
793 unsigned long phys_addr = pfn << PAGE_SHIFT;
796 attr = efi_mem_attribute(phys_addr, size);
799 * /dev/mem mmap uses normal user pages, so we don't need the entire
800 * granule, but the entire region we're mapping must support the same
803 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
807 * Intel firmware doesn't tell us about all the MMIO regions, so
808 * in general we have to allow mmap requests. But if EFI *does*
809 * tell us about anything inside this region, we should deny it.
810 * The user can always map a smaller region to avoid the overlap.
812 if (efi_memmap_intersects(phys_addr, size))
819 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
822 unsigned long phys_addr = pfn << PAGE_SHIFT;
826 * For /dev/mem mmap, we use user mappings, but if the region is
827 * in kern_memmap (and hence may be covered by a kernel mapping),
828 * we must use the same attribute as the kernel mapping.
830 attr = kern_mem_attribute(phys_addr, size);
831 if (attr & EFI_MEMORY_WB)
832 return pgprot_cacheable(vma_prot);
833 else if (attr & EFI_MEMORY_UC)
834 return pgprot_noncached(vma_prot);
837 * Some chipsets don't support UC access to memory. If
838 * WB is supported, we prefer that.
840 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
841 return pgprot_cacheable(vma_prot);
843 return pgprot_noncached(vma_prot);
847 efi_uart_console_only(void)
850 char *s, name[] = "ConOut";
851 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
852 efi_char16_t *utf16, name_utf16[32];
853 unsigned char data[1024];
854 unsigned long size = sizeof(data);
855 struct efi_generic_dev_path *hdr, *end_addr;
858 /* Convert to UTF-16 */
862 *utf16++ = *s++ & 0x7f;
865 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
866 if (status != EFI_SUCCESS) {
867 printk(KERN_ERR "No EFI %s variable?\n", name);
871 hdr = (struct efi_generic_dev_path *) data;
872 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
873 while (hdr < end_addr) {
874 if (hdr->type == EFI_DEV_MSG &&
875 hdr->sub_type == EFI_DEV_MSG_UART)
877 else if (hdr->type == EFI_DEV_END_PATH ||
878 hdr->type == EFI_DEV_END_PATH2) {
881 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
885 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
887 printk(KERN_ERR "Malformed %s value\n", name);
892 * Look for the first granule aligned memory descriptor memory
893 * that is big enough to hold EFI memory map. Make sure this
894 * descriptor is atleast granule sized so it does not get trimmed
896 struct kern_memdesc *
897 find_memmap_space (void)
899 u64 contig_low=0, contig_high=0;
901 void *efi_map_start, *efi_map_end, *p, *q;
902 efi_memory_desc_t *md, *pmd = NULL, *check_md;
903 u64 space_needed, efi_desc_size;
904 unsigned long total_mem = 0;
906 efi_map_start = __va(ia64_boot_param->efi_memmap);
907 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
908 efi_desc_size = ia64_boot_param->efi_memdesc_size;
911 * Worst case: we need 3 kernel descriptors for each efi descriptor
912 * (if every entry has a WB part in the middle, and UC head and tail),
913 * plus one for the end marker.
915 space_needed = sizeof(kern_memdesc_t) *
916 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
918 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
923 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
924 contig_low = GRANULEROUNDUP(md->phys_addr);
925 contig_high = efi_md_end(md);
926 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
928 if (!efi_wb(check_md))
930 if (contig_high != check_md->phys_addr)
932 contig_high = efi_md_end(check_md);
934 contig_high = GRANULEROUNDDOWN(contig_high);
936 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
939 /* Round ends inward to granule boundaries */
940 as = max(contig_low, md->phys_addr);
941 ae = min(contig_high, efi_md_end(md));
943 /* keep within max_addr= and min_addr= command line arg */
944 as = max(as, min_addr);
945 ae = min(ae, max_addr);
949 /* avoid going over mem= command line arg */
950 if (total_mem + (ae - as) > mem_limit)
951 ae -= total_mem + (ae - as) - mem_limit;
956 if (ae - as > space_needed)
959 if (p >= efi_map_end)
960 panic("Can't allocate space for kernel memory descriptors");
966 * Walk the EFI memory map and gather all memory available for kernel
967 * to use. We can allocate partial granules only if the unavailable
968 * parts exist, and are WB.
971 efi_memmap_init(unsigned long *s, unsigned long *e)
973 struct kern_memdesc *k, *prev = NULL;
974 u64 contig_low=0, contig_high=0;
976 void *efi_map_start, *efi_map_end, *p, *q;
977 efi_memory_desc_t *md, *pmd = NULL, *check_md;
979 unsigned long total_mem = 0;
981 k = kern_memmap = find_memmap_space();
983 efi_map_start = __va(ia64_boot_param->efi_memmap);
984 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
985 efi_desc_size = ia64_boot_param->efi_memdesc_size;
987 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
990 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
991 md->type == EFI_BOOT_SERVICES_DATA)) {
992 k->attribute = EFI_MEMORY_UC;
993 k->start = md->phys_addr;
994 k->num_pages = md->num_pages;
999 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
1000 contig_low = GRANULEROUNDUP(md->phys_addr);
1001 contig_high = efi_md_end(md);
1002 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
1004 if (!efi_wb(check_md))
1006 if (contig_high != check_md->phys_addr)
1008 contig_high = efi_md_end(check_md);
1010 contig_high = GRANULEROUNDDOWN(contig_high);
1012 if (!is_memory_available(md))
1015 #ifdef CONFIG_CRASH_DUMP
1016 /* saved_max_pfn should ignore max_addr= command line arg */
1017 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1018 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1021 * Round ends inward to granule boundaries
1022 * Give trimmings to uncached allocator
1024 if (md->phys_addr < contig_low) {
1025 lim = min(efi_md_end(md), contig_low);
1027 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
1028 kmd_end(k-1) == md->phys_addr) {
1029 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1031 k->attribute = EFI_MEMORY_UC;
1032 k->start = md->phys_addr;
1033 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1041 if (efi_md_end(md) > contig_high) {
1042 lim = max(md->phys_addr, contig_high);
1044 if (lim == md->phys_addr && k > kern_memmap &&
1045 (k-1)->attribute == EFI_MEMORY_UC &&
1046 kmd_end(k-1) == md->phys_addr) {
1047 (k-1)->num_pages += md->num_pages;
1049 k->attribute = EFI_MEMORY_UC;
1051 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1057 ae = efi_md_end(md);
1059 /* keep within max_addr= and min_addr= command line arg */
1060 as = max(as, min_addr);
1061 ae = min(ae, max_addr);
1065 /* avoid going over mem= command line arg */
1066 if (total_mem + (ae - as) > mem_limit)
1067 ae -= total_mem + (ae - as) - mem_limit;
1071 if (prev && kmd_end(prev) == md->phys_addr) {
1072 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1073 total_mem += ae - as;
1076 k->attribute = EFI_MEMORY_WB;
1078 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1079 total_mem += ae - as;
1082 k->start = ~0L; /* end-marker */
1084 /* reserve the memory we are using for kern_memmap */
1085 *s = (u64)kern_memmap;
1090 efi_initialize_iomem_resources(struct resource *code_resource,
1091 struct resource *data_resource)
1093 struct resource *res;
1094 void *efi_map_start, *efi_map_end, *p;
1095 efi_memory_desc_t *md;
1098 unsigned long flags;
1100 efi_map_start = __va(ia64_boot_param->efi_memmap);
1101 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1102 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1106 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1109 if (md->num_pages == 0) /* should not happen */
1112 flags = IORESOURCE_MEM;
1115 case EFI_MEMORY_MAPPED_IO:
1116 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1119 case EFI_LOADER_CODE:
1120 case EFI_LOADER_DATA:
1121 case EFI_BOOT_SERVICES_DATA:
1122 case EFI_BOOT_SERVICES_CODE:
1123 case EFI_CONVENTIONAL_MEMORY:
1124 if (md->attribute & EFI_MEMORY_WP) {
1125 name = "System ROM";
1126 flags |= IORESOURCE_READONLY;
1128 name = "System RAM";
1132 case EFI_ACPI_MEMORY_NVS:
1133 name = "ACPI Non-volatile Storage";
1134 flags |= IORESOURCE_BUSY;
1137 case EFI_UNUSABLE_MEMORY:
1139 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1142 case EFI_RESERVED_TYPE:
1143 case EFI_RUNTIME_SERVICES_CODE:
1144 case EFI_RUNTIME_SERVICES_DATA:
1145 case EFI_ACPI_RECLAIM_MEMORY:
1148 flags |= IORESOURCE_BUSY;
1152 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1153 printk(KERN_ERR "failed to alocate resource for iomem\n");
1158 res->start = md->phys_addr;
1159 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1162 if (insert_resource(&iomem_resource, res) < 0)
1166 * We don't know which region contains
1167 * kernel data so we try it repeatedly and
1168 * let the resource manager test it.
1170 insert_resource(res, code_resource);
1171 insert_resource(res, data_resource);
1173 insert_resource(res, &efi_memmap_res);
1174 insert_resource(res, &boot_param_res);
1175 if (crashk_res.end > crashk_res.start)
1176 insert_resource(res, &crashk_res);
1183 /* find a block of memory aligned to 64M exclude reserved regions
1184 rsvd_regions are sorted
1186 unsigned long __init
1187 kdump_find_rsvd_region (unsigned long size,
1188 struct rsvd_region *r, int n)
1192 u64 alignment = 1UL << _PAGE_SIZE_64M;
1193 void *efi_map_start, *efi_map_end, *p;
1194 efi_memory_desc_t *md;
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) {
1205 start = ALIGN(md->phys_addr, alignment);
1206 end = efi_md_end(md);
1207 for (i = 0; i < n; i++) {
1208 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1209 if (__pa(r[i].start) > start + size)
1211 start = ALIGN(__pa(r[i].end), alignment);
1212 if (i < n-1 && __pa(r[i+1].start) < start + size)
1218 if (end > start + size)
1222 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",
1228 #ifdef CONFIG_PROC_VMCORE
1229 /* locate the size find a the descriptor at a certain address */
1231 vmcore_find_descriptor_size (unsigned long address)
1233 void *efi_map_start, *efi_map_end, *p;
1234 efi_memory_desc_t *md;
1236 unsigned long ret = 0;
1238 efi_map_start = __va(ia64_boot_param->efi_memmap);
1239 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1240 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1242 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1244 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1245 && md->phys_addr == address) {
1246 ret = efi_md_size(md);
1252 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");