2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 0.9
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>
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
19 * Implemented EFI runtime services and virtual mode calls. --davidm
21 * Goutham Rao: <goutham.rao@intel.com>
22 * Skip non-WB memory and ignore empty memory ranges.
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/time.h>
30 #include <linux/efi.h>
31 #include <linux/kexec.h>
35 #include <asm/kregs.h>
36 #include <asm/meminit.h>
37 #include <asm/pgtable.h>
38 #include <asm/processor.h>
40 #include <asm/tlbflush.h>
44 extern efi_status_t efi_call_phys (void *, ...);
48 static efi_runtime_services_t *runtime;
49 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
51 #define efi_call_virt(f, args...) (*(f))(args)
53 #define STUB_GET_TIME(prefix, adjust_arg) \
55 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
57 struct ia64_fpreg fr[6]; \
58 efi_time_cap_t *atc = NULL; \
62 atc = adjust_arg(tc); \
63 ia64_save_scratch_fpregs(fr); \
64 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
65 adjust_arg(tm), atc); \
66 ia64_load_scratch_fpregs(fr); \
70 #define STUB_SET_TIME(prefix, adjust_arg) \
72 prefix##_set_time (efi_time_t *tm) \
74 struct ia64_fpreg fr[6]; \
77 ia64_save_scratch_fpregs(fr); \
78 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
80 ia64_load_scratch_fpregs(fr); \
84 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
86 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
89 struct ia64_fpreg fr[6]; \
92 ia64_save_scratch_fpregs(fr); \
93 ret = efi_call_##prefix( \
94 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
95 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
96 ia64_load_scratch_fpregs(fr); \
100 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
101 static efi_status_t \
102 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
104 struct ia64_fpreg fr[6]; \
105 efi_time_t *atm = NULL; \
109 atm = adjust_arg(tm); \
110 ia64_save_scratch_fpregs(fr); \
111 ret = efi_call_##prefix( \
112 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
114 ia64_load_scratch_fpregs(fr); \
118 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
119 static efi_status_t \
120 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
121 unsigned long *data_size, void *data) \
123 struct ia64_fpreg fr[6]; \
128 aattr = adjust_arg(attr); \
129 ia64_save_scratch_fpregs(fr); \
130 ret = efi_call_##prefix( \
131 (efi_get_variable_t *) __va(runtime->get_variable), \
132 adjust_arg(name), adjust_arg(vendor), aattr, \
133 adjust_arg(data_size), adjust_arg(data)); \
134 ia64_load_scratch_fpregs(fr); \
138 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
139 static efi_status_t \
140 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
141 efi_guid_t *vendor) \
143 struct ia64_fpreg fr[6]; \
146 ia64_save_scratch_fpregs(fr); \
147 ret = efi_call_##prefix( \
148 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
149 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
150 ia64_load_scratch_fpregs(fr); \
154 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
155 static efi_status_t \
156 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
157 unsigned long attr, unsigned long data_size, \
160 struct ia64_fpreg fr[6]; \
163 ia64_save_scratch_fpregs(fr); \
164 ret = efi_call_##prefix( \
165 (efi_set_variable_t *) __va(runtime->set_variable), \
166 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
168 ia64_load_scratch_fpregs(fr); \
172 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
173 static efi_status_t \
174 prefix##_get_next_high_mono_count (u32 *count) \
176 struct ia64_fpreg fr[6]; \
179 ia64_save_scratch_fpregs(fr); \
180 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
181 __va(runtime->get_next_high_mono_count), \
182 adjust_arg(count)); \
183 ia64_load_scratch_fpregs(fr); \
187 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
189 prefix##_reset_system (int reset_type, efi_status_t status, \
190 unsigned long data_size, efi_char16_t *data) \
192 struct ia64_fpreg fr[6]; \
193 efi_char16_t *adata = NULL; \
196 adata = adjust_arg(data); \
198 ia64_save_scratch_fpregs(fr); \
200 (efi_reset_system_t *) __va(runtime->reset_system), \
201 reset_type, status, data_size, adata); \
202 /* should not return, but just in case... */ \
203 ia64_load_scratch_fpregs(fr); \
206 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
208 STUB_GET_TIME(phys, phys_ptr)
209 STUB_SET_TIME(phys, phys_ptr)
210 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
211 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
212 STUB_GET_VARIABLE(phys, phys_ptr)
213 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
214 STUB_SET_VARIABLE(phys, phys_ptr)
215 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
216 STUB_RESET_SYSTEM(phys, phys_ptr)
220 STUB_GET_TIME(virt, id)
221 STUB_SET_TIME(virt, id)
222 STUB_GET_WAKEUP_TIME(virt, id)
223 STUB_SET_WAKEUP_TIME(virt, id)
224 STUB_GET_VARIABLE(virt, id)
225 STUB_GET_NEXT_VARIABLE(virt, id)
226 STUB_SET_VARIABLE(virt, id)
227 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
228 STUB_RESET_SYSTEM(virt, id)
231 efi_gettimeofday (struct timespec *ts)
235 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
236 memset(ts, 0, sizeof(*ts));
240 ts->tv_sec = mktime(tm.year, tm.month, tm.day,
241 tm.hour, tm.minute, tm.second);
242 ts->tv_nsec = tm.nanosecond;
246 is_memory_available (efi_memory_desc_t *md)
248 if (!(md->attribute & EFI_MEMORY_WB))
252 case EFI_LOADER_CODE:
253 case EFI_LOADER_DATA:
254 case EFI_BOOT_SERVICES_CODE:
255 case EFI_BOOT_SERVICES_DATA:
256 case EFI_CONVENTIONAL_MEMORY:
262 typedef struct kern_memdesc {
268 static kern_memdesc_t *kern_memmap;
270 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
273 kmd_end(kern_memdesc_t *kmd)
275 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
279 efi_md_end(efi_memory_desc_t *md)
281 return (md->phys_addr + efi_md_size(md));
285 efi_wb(efi_memory_desc_t *md)
287 return (md->attribute & EFI_MEMORY_WB);
291 efi_uc(efi_memory_desc_t *md)
293 return (md->attribute & EFI_MEMORY_UC);
297 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
300 u64 start, end, voff;
302 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
303 for (k = kern_memmap; k->start != ~0UL; k++) {
304 if (k->attribute != attr)
306 start = PAGE_ALIGN(k->start);
307 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
309 if ((*callback)(start + voff, end + voff, arg) < 0)
315 * Walk the EFI memory map and call CALLBACK once for each EFI memory
316 * descriptor that has memory that is available for OS use.
319 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
321 walk(callback, arg, EFI_MEMORY_WB);
325 * Walk the EFI memory map and call CALLBACK once for each EFI memory
326 * descriptor that has memory that is available for uncached allocator.
329 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
331 walk(callback, arg, EFI_MEMORY_UC);
335 * Look for the PAL_CODE region reported by EFI and map it using an
336 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
337 * Abstraction Layer chapter 11 in ADAG
340 efi_get_pal_addr (void)
342 void *efi_map_start, *efi_map_end, *p;
343 efi_memory_desc_t *md;
345 int pal_code_count = 0;
348 efi_map_start = __va(ia64_boot_param->efi_memmap);
349 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
350 efi_desc_size = ia64_boot_param->efi_memdesc_size;
352 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
354 if (md->type != EFI_PAL_CODE)
357 if (++pal_code_count > 1) {
358 printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
359 "dropped @ %lx\n", md->phys_addr);
363 * The only ITLB entry in region 7 that is used is the one
364 * installed by __start(). That entry covers a 64MB range.
366 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
367 vaddr = PAGE_OFFSET + md->phys_addr;
370 * We must check that the PAL mapping won't overlap with the
373 * PAL code is guaranteed to be aligned on a power of 2 between
374 * 4k and 256KB and that only one ITR is needed to map it. This
375 * implies that the PAL code is always aligned on its size,
376 * i.e., the closest matching page size supported by the TLB.
377 * Therefore PAL code is guaranteed never to cross a 64MB unless
378 * it is bigger than 64MB (very unlikely!). So for now the
379 * following test is enough to determine whether or not we need
380 * a dedicated ITR for the PAL code.
382 if ((vaddr & mask) == (KERNEL_START & mask)) {
383 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
388 if (efi_md_size(md) > IA64_GRANULE_SIZE)
389 panic("Whoa! PAL code size bigger than a granule!");
392 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
394 printk(KERN_INFO "CPU %d: mapping PAL code "
395 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
396 smp_processor_id(), md->phys_addr,
397 md->phys_addr + efi_md_size(md),
398 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
400 return __va(md->phys_addr);
402 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
408 static u8 __init palo_checksum(u8 *buffer, u32 length)
411 u8 *end = buffer + length;
414 sum = (u8) (sum + *(buffer++));
420 * Parse and handle PALO table which is published at:
421 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
423 static void __init handle_palo(unsigned long palo_phys)
425 struct palo_table *palo = __va(palo_phys);
428 if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
429 printk(KERN_INFO "PALO signature incorrect.\n");
433 checksum = palo_checksum((u8 *)palo, palo->length);
435 printk(KERN_INFO "PALO checksum incorrect.\n");
439 setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
443 efi_map_pal_code (void)
445 void *pal_vaddr = efi_get_pal_addr ();
452 * Cannot write to CRx with PSR.ic=1
454 psr = ia64_clear_ic();
455 ia64_itr(0x1, IA64_TR_PALCODE,
456 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
457 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
459 ia64_set_psr(psr); /* restore psr */
465 void *efi_map_start, *efi_map_end;
466 efi_config_table_t *config_tables;
469 char *cp, vendor[100] = "unknown";
471 unsigned long palo_phys;
474 * It's too early to be able to use the standard kernel command line
477 for (cp = boot_command_line; *cp; ) {
478 if (memcmp(cp, "mem=", 4) == 0) {
479 mem_limit = memparse(cp + 4, &cp);
480 } else if (memcmp(cp, "max_addr=", 9) == 0) {
481 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
482 } else if (memcmp(cp, "min_addr=", 9) == 0) {
483 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
485 while (*cp != ' ' && *cp)
492 printk(KERN_INFO "Ignoring memory below %luMB\n",
494 if (max_addr != ~0UL)
495 printk(KERN_INFO "Ignoring memory above %luMB\n",
498 efi.systab = __va(ia64_boot_param->efi_systab);
501 * Verify the EFI Table
503 if (efi.systab == NULL)
504 panic("Whoa! Can't find EFI system table.\n");
505 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
506 panic("Whoa! EFI system table signature incorrect\n");
507 if ((efi.systab->hdr.revision >> 16) == 0)
508 printk(KERN_WARNING "Warning: EFI system table version "
509 "%d.%02d, expected 1.00 or greater\n",
510 efi.systab->hdr.revision >> 16,
511 efi.systab->hdr.revision & 0xffff);
513 config_tables = __va(efi.systab->tables);
515 /* Show what we know for posterity */
516 c16 = __va(efi.systab->fw_vendor);
518 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
523 printk(KERN_INFO "EFI v%u.%.02u by %s:",
524 efi.systab->hdr.revision >> 16,
525 efi.systab->hdr.revision & 0xffff, vendor);
527 efi.mps = EFI_INVALID_TABLE_ADDR;
528 efi.acpi = EFI_INVALID_TABLE_ADDR;
529 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
530 efi.smbios = EFI_INVALID_TABLE_ADDR;
531 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
532 efi.boot_info = EFI_INVALID_TABLE_ADDR;
533 efi.hcdp = EFI_INVALID_TABLE_ADDR;
534 efi.uga = EFI_INVALID_TABLE_ADDR;
536 palo_phys = EFI_INVALID_TABLE_ADDR;
538 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
539 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
540 efi.mps = config_tables[i].table;
541 printk(" MPS=0x%lx", config_tables[i].table);
542 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
543 efi.acpi20 = config_tables[i].table;
544 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
545 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
546 efi.acpi = config_tables[i].table;
547 printk(" ACPI=0x%lx", config_tables[i].table);
548 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
549 efi.smbios = config_tables[i].table;
550 printk(" SMBIOS=0x%lx", config_tables[i].table);
551 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
552 efi.sal_systab = config_tables[i].table;
553 printk(" SALsystab=0x%lx", config_tables[i].table);
554 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
555 efi.hcdp = config_tables[i].table;
556 printk(" HCDP=0x%lx", config_tables[i].table);
557 } else if (efi_guidcmp(config_tables[i].guid,
558 PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID) == 0) {
559 palo_phys = config_tables[i].table;
560 printk(" PALO=0x%lx", config_tables[i].table);
565 if (palo_phys != EFI_INVALID_TABLE_ADDR)
566 handle_palo(palo_phys);
568 runtime = __va(efi.systab->runtime);
569 efi.get_time = phys_get_time;
570 efi.set_time = phys_set_time;
571 efi.get_wakeup_time = phys_get_wakeup_time;
572 efi.set_wakeup_time = phys_set_wakeup_time;
573 efi.get_variable = phys_get_variable;
574 efi.get_next_variable = phys_get_next_variable;
575 efi.set_variable = phys_set_variable;
576 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
577 efi.reset_system = phys_reset_system;
579 efi_map_start = __va(ia64_boot_param->efi_memmap);
580 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
581 efi_desc_size = ia64_boot_param->efi_memdesc_size;
584 /* print EFI memory map: */
586 efi_memory_desc_t *md;
589 for (i = 0, p = efi_map_start; p < efi_map_end;
590 ++i, p += efi_desc_size)
596 size = md->num_pages << EFI_PAGE_SHIFT;
598 if ((size >> 40) > 0) {
601 } else if ((size >> 30) > 0) {
604 } else if ((size >> 20) > 0) {
612 printk("mem%02d: type=%2u, attr=0x%016lx, "
613 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
614 i, md->type, md->attribute, md->phys_addr,
615 md->phys_addr + efi_md_size(md), size, unit);
621 efi_enter_virtual_mode();
625 efi_enter_virtual_mode (void)
627 void *efi_map_start, *efi_map_end, *p;
628 efi_memory_desc_t *md;
632 efi_map_start = __va(ia64_boot_param->efi_memmap);
633 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
634 efi_desc_size = ia64_boot_param->efi_memdesc_size;
636 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
638 if (md->attribute & EFI_MEMORY_RUNTIME) {
640 * Some descriptors have multiple bits set, so the
641 * order of the tests is relevant.
643 if (md->attribute & EFI_MEMORY_WB) {
644 md->virt_addr = (u64) __va(md->phys_addr);
645 } else if (md->attribute & EFI_MEMORY_UC) {
646 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
647 } else if (md->attribute & EFI_MEMORY_WC) {
649 md->virt_addr = ia64_remap(md->phys_addr,
657 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
658 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
660 } else if (md->attribute & EFI_MEMORY_WT) {
662 md->virt_addr = ia64_remap(md->phys_addr,
670 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
671 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
677 status = efi_call_phys(__va(runtime->set_virtual_address_map),
678 ia64_boot_param->efi_memmap_size,
680 ia64_boot_param->efi_memdesc_version,
681 ia64_boot_param->efi_memmap);
682 if (status != EFI_SUCCESS) {
683 printk(KERN_WARNING "warning: unable to switch EFI into "
684 "virtual mode (status=%lu)\n", status);
689 * Now that EFI is in virtual mode, we call the EFI functions more
692 efi.get_time = virt_get_time;
693 efi.set_time = virt_set_time;
694 efi.get_wakeup_time = virt_get_wakeup_time;
695 efi.set_wakeup_time = virt_set_wakeup_time;
696 efi.get_variable = virt_get_variable;
697 efi.get_next_variable = virt_get_next_variable;
698 efi.set_variable = virt_set_variable;
699 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
700 efi.reset_system = virt_reset_system;
704 * Walk the EFI memory map looking for the I/O port range. There can only be
705 * one entry of this type, other I/O port ranges should be described via ACPI.
708 efi_get_iobase (void)
710 void *efi_map_start, *efi_map_end, *p;
711 efi_memory_desc_t *md;
714 efi_map_start = __va(ia64_boot_param->efi_memmap);
715 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
716 efi_desc_size = ia64_boot_param->efi_memdesc_size;
718 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
720 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
721 if (md->attribute & EFI_MEMORY_UC)
722 return md->phys_addr;
728 static struct kern_memdesc *
729 kern_memory_descriptor (unsigned long phys_addr)
731 struct kern_memdesc *md;
733 for (md = kern_memmap; md->start != ~0UL; md++) {
734 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
740 static efi_memory_desc_t *
741 efi_memory_descriptor (unsigned long phys_addr)
743 void *efi_map_start, *efi_map_end, *p;
744 efi_memory_desc_t *md;
747 efi_map_start = __va(ia64_boot_param->efi_memmap);
748 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
749 efi_desc_size = ia64_boot_param->efi_memdesc_size;
751 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
754 if (phys_addr - md->phys_addr < efi_md_size(md))
761 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
763 void *efi_map_start, *efi_map_end, *p;
764 efi_memory_desc_t *md;
768 efi_map_start = __va(ia64_boot_param->efi_memmap);
769 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
770 efi_desc_size = ia64_boot_param->efi_memdesc_size;
772 end = phys_addr + size;
774 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
776 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
783 efi_mem_type (unsigned long phys_addr)
785 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
793 efi_mem_attributes (unsigned long phys_addr)
795 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
798 return md->attribute;
801 EXPORT_SYMBOL(efi_mem_attributes);
804 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
806 unsigned long end = phys_addr + size;
807 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
814 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
815 * the kernel that firmware needs this region mapped.
817 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
819 unsigned long md_end = efi_md_end(md);
824 md = efi_memory_descriptor(md_end);
825 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
828 return 0; /* never reached */
832 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
834 unsigned long end = phys_addr + size;
835 struct kern_memdesc *md;
839 * This is a hack for ioremap calls before we set up kern_memmap.
840 * Maybe we should do efi_memmap_init() earlier instead.
843 attr = efi_mem_attribute(phys_addr, size);
844 if (attr & EFI_MEMORY_WB)
845 return EFI_MEMORY_WB;
849 md = kern_memory_descriptor(phys_addr);
853 attr = md->attribute;
855 unsigned long md_end = kmd_end(md);
860 md = kern_memory_descriptor(md_end);
861 if (!md || md->attribute != attr)
864 return 0; /* never reached */
866 EXPORT_SYMBOL(kern_mem_attribute);
869 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
874 * /dev/mem reads and writes use copy_to_user(), which implicitly
875 * uses a granule-sized kernel identity mapping. It's really
876 * only safe to do this for regions in kern_memmap. For more
877 * details, see Documentation/ia64/aliasing.txt.
879 attr = kern_mem_attribute(phys_addr, size);
880 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
886 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
888 unsigned long phys_addr = pfn << PAGE_SHIFT;
891 attr = efi_mem_attribute(phys_addr, size);
894 * /dev/mem mmap uses normal user pages, so we don't need the entire
895 * granule, but the entire region we're mapping must support the same
898 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
902 * Intel firmware doesn't tell us about all the MMIO regions, so
903 * in general we have to allow mmap requests. But if EFI *does*
904 * tell us about anything inside this region, we should deny it.
905 * The user can always map a smaller region to avoid the overlap.
907 if (efi_memmap_intersects(phys_addr, size))
914 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
917 unsigned long phys_addr = pfn << PAGE_SHIFT;
921 * For /dev/mem mmap, we use user mappings, but if the region is
922 * in kern_memmap (and hence may be covered by a kernel mapping),
923 * we must use the same attribute as the kernel mapping.
925 attr = kern_mem_attribute(phys_addr, size);
926 if (attr & EFI_MEMORY_WB)
927 return pgprot_cacheable(vma_prot);
928 else if (attr & EFI_MEMORY_UC)
929 return pgprot_noncached(vma_prot);
932 * Some chipsets don't support UC access to memory. If
933 * WB is supported, we prefer that.
935 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
936 return pgprot_cacheable(vma_prot);
938 return pgprot_noncached(vma_prot);
942 efi_uart_console_only(void)
945 char *s, name[] = "ConOut";
946 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
947 efi_char16_t *utf16, name_utf16[32];
948 unsigned char data[1024];
949 unsigned long size = sizeof(data);
950 struct efi_generic_dev_path *hdr, *end_addr;
953 /* Convert to UTF-16 */
957 *utf16++ = *s++ & 0x7f;
960 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
961 if (status != EFI_SUCCESS) {
962 printk(KERN_ERR "No EFI %s variable?\n", name);
966 hdr = (struct efi_generic_dev_path *) data;
967 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
968 while (hdr < end_addr) {
969 if (hdr->type == EFI_DEV_MSG &&
970 hdr->sub_type == EFI_DEV_MSG_UART)
972 else if (hdr->type == EFI_DEV_END_PATH ||
973 hdr->type == EFI_DEV_END_PATH2) {
976 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
980 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
982 printk(KERN_ERR "Malformed %s value\n", name);
987 * Look for the first granule aligned memory descriptor memory
988 * that is big enough to hold EFI memory map. Make sure this
989 * descriptor is atleast granule sized so it does not get trimmed
991 struct kern_memdesc *
992 find_memmap_space (void)
994 u64 contig_low=0, contig_high=0;
996 void *efi_map_start, *efi_map_end, *p, *q;
997 efi_memory_desc_t *md, *pmd = NULL, *check_md;
998 u64 space_needed, efi_desc_size;
999 unsigned long total_mem = 0;
1001 efi_map_start = __va(ia64_boot_param->efi_memmap);
1002 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1003 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1006 * Worst case: we need 3 kernel descriptors for each efi descriptor
1007 * (if every entry has a WB part in the middle, and UC head and tail),
1008 * plus one for the end marker.
1010 space_needed = sizeof(kern_memdesc_t) *
1011 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
1013 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1018 if (pmd == NULL || !efi_wb(pmd) ||
1019 efi_md_end(pmd) != md->phys_addr) {
1020 contig_low = GRANULEROUNDUP(md->phys_addr);
1021 contig_high = efi_md_end(md);
1022 for (q = p + efi_desc_size; q < efi_map_end;
1023 q += efi_desc_size) {
1025 if (!efi_wb(check_md))
1027 if (contig_high != check_md->phys_addr)
1029 contig_high = efi_md_end(check_md);
1031 contig_high = GRANULEROUNDDOWN(contig_high);
1033 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
1036 /* Round ends inward to granule boundaries */
1037 as = max(contig_low, md->phys_addr);
1038 ae = min(contig_high, efi_md_end(md));
1040 /* keep within max_addr= and min_addr= command line arg */
1041 as = max(as, min_addr);
1042 ae = min(ae, max_addr);
1046 /* avoid going over mem= command line arg */
1047 if (total_mem + (ae - as) > mem_limit)
1048 ae -= total_mem + (ae - as) - mem_limit;
1053 if (ae - as > space_needed)
1056 if (p >= efi_map_end)
1057 panic("Can't allocate space for kernel memory descriptors");
1063 * Walk the EFI memory map and gather all memory available for kernel
1064 * to use. We can allocate partial granules only if the unavailable
1065 * parts exist, and are WB.
1068 efi_memmap_init(unsigned long *s, unsigned long *e)
1070 struct kern_memdesc *k, *prev = NULL;
1071 u64 contig_low=0, contig_high=0;
1073 void *efi_map_start, *efi_map_end, *p, *q;
1074 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1076 unsigned long total_mem = 0;
1078 k = kern_memmap = find_memmap_space();
1080 efi_map_start = __va(ia64_boot_param->efi_memmap);
1081 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1082 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1084 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1088 (md->type == EFI_CONVENTIONAL_MEMORY ||
1089 md->type == EFI_BOOT_SERVICES_DATA)) {
1090 k->attribute = EFI_MEMORY_UC;
1091 k->start = md->phys_addr;
1092 k->num_pages = md->num_pages;
1097 if (pmd == NULL || !efi_wb(pmd) ||
1098 efi_md_end(pmd) != md->phys_addr) {
1099 contig_low = GRANULEROUNDUP(md->phys_addr);
1100 contig_high = efi_md_end(md);
1101 for (q = p + efi_desc_size; q < efi_map_end;
1102 q += efi_desc_size) {
1104 if (!efi_wb(check_md))
1106 if (contig_high != check_md->phys_addr)
1108 contig_high = efi_md_end(check_md);
1110 contig_high = GRANULEROUNDDOWN(contig_high);
1112 if (!is_memory_available(md))
1115 #ifdef CONFIG_CRASH_DUMP
1116 /* saved_max_pfn should ignore max_addr= command line arg */
1117 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1118 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1121 * Round ends inward to granule boundaries
1122 * Give trimmings to uncached allocator
1124 if (md->phys_addr < contig_low) {
1125 lim = min(efi_md_end(md), contig_low);
1127 if (k > kern_memmap &&
1128 (k-1)->attribute == EFI_MEMORY_UC &&
1129 kmd_end(k-1) == md->phys_addr) {
1131 (lim - md->phys_addr)
1134 k->attribute = EFI_MEMORY_UC;
1135 k->start = md->phys_addr;
1136 k->num_pages = (lim - md->phys_addr)
1145 if (efi_md_end(md) > contig_high) {
1146 lim = max(md->phys_addr, contig_high);
1148 if (lim == md->phys_addr && k > kern_memmap &&
1149 (k-1)->attribute == EFI_MEMORY_UC &&
1150 kmd_end(k-1) == md->phys_addr) {
1151 (k-1)->num_pages += md->num_pages;
1153 k->attribute = EFI_MEMORY_UC;
1155 k->num_pages = (efi_md_end(md) - lim)
1162 ae = efi_md_end(md);
1164 /* keep within max_addr= and min_addr= command line arg */
1165 as = max(as, min_addr);
1166 ae = min(ae, max_addr);
1170 /* avoid going over mem= command line arg */
1171 if (total_mem + (ae - as) > mem_limit)
1172 ae -= total_mem + (ae - as) - mem_limit;
1176 if (prev && kmd_end(prev) == md->phys_addr) {
1177 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1178 total_mem += ae - as;
1181 k->attribute = EFI_MEMORY_WB;
1183 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1184 total_mem += ae - as;
1187 k->start = ~0L; /* end-marker */
1189 /* reserve the memory we are using for kern_memmap */
1190 *s = (u64)kern_memmap;
1197 efi_initialize_iomem_resources(struct resource *code_resource,
1198 struct resource *data_resource,
1199 struct resource *bss_resource)
1201 struct resource *res;
1202 void *efi_map_start, *efi_map_end, *p;
1203 efi_memory_desc_t *md;
1206 unsigned long flags;
1208 efi_map_start = __va(ia64_boot_param->efi_memmap);
1209 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1210 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1214 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1217 if (md->num_pages == 0) /* should not happen */
1220 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1223 case EFI_MEMORY_MAPPED_IO:
1224 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1227 case EFI_LOADER_CODE:
1228 case EFI_LOADER_DATA:
1229 case EFI_BOOT_SERVICES_DATA:
1230 case EFI_BOOT_SERVICES_CODE:
1231 case EFI_CONVENTIONAL_MEMORY:
1232 if (md->attribute & EFI_MEMORY_WP) {
1233 name = "System ROM";
1234 flags |= IORESOURCE_READONLY;
1235 } else if (md->attribute == EFI_MEMORY_UC)
1236 name = "Uncached RAM";
1238 name = "System RAM";
1241 case EFI_ACPI_MEMORY_NVS:
1242 name = "ACPI Non-volatile Storage";
1245 case EFI_UNUSABLE_MEMORY:
1247 flags |= IORESOURCE_DISABLED;
1250 case EFI_RESERVED_TYPE:
1251 case EFI_RUNTIME_SERVICES_CODE:
1252 case EFI_RUNTIME_SERVICES_DATA:
1253 case EFI_ACPI_RECLAIM_MEMORY:
1259 if ((res = kzalloc(sizeof(struct resource),
1260 GFP_KERNEL)) == NULL) {
1262 "failed to allocate resource for iomem\n");
1267 res->start = md->phys_addr;
1268 res->end = md->phys_addr + efi_md_size(md) - 1;
1271 if (insert_resource(&iomem_resource, res) < 0)
1275 * We don't know which region contains
1276 * kernel data so we try it repeatedly and
1277 * let the resource manager test it.
1279 insert_resource(res, code_resource);
1280 insert_resource(res, data_resource);
1281 insert_resource(res, bss_resource);
1283 insert_resource(res, &efi_memmap_res);
1284 insert_resource(res, &boot_param_res);
1285 if (crashk_res.end > crashk_res.start)
1286 insert_resource(res, &crashk_res);
1293 /* find a block of memory aligned to 64M exclude reserved regions
1294 rsvd_regions are sorted
1296 unsigned long __init
1297 kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1301 u64 alignment = 1UL << _PAGE_SIZE_64M;
1302 void *efi_map_start, *efi_map_end, *p;
1303 efi_memory_desc_t *md;
1306 efi_map_start = __va(ia64_boot_param->efi_memmap);
1307 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1308 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1310 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1314 start = ALIGN(md->phys_addr, alignment);
1315 end = efi_md_end(md);
1316 for (i = 0; i < n; i++) {
1317 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1318 if (__pa(r[i].start) > start + size)
1320 start = ALIGN(__pa(r[i].end), alignment);
1322 __pa(r[i+1].start) < start + size)
1328 if (end > start + size)
1333 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1338 #ifdef CONFIG_PROC_VMCORE
1339 /* locate the size find a the descriptor at a certain address */
1340 unsigned long __init
1341 vmcore_find_descriptor_size (unsigned long address)
1343 void *efi_map_start, *efi_map_end, *p;
1344 efi_memory_desc_t *md;
1346 unsigned long ret = 0;
1348 efi_map_start = __va(ia64_boot_param->efi_memmap);
1349 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1350 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1352 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1354 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1355 && md->phys_addr == address) {
1356 ret = efi_md_size(md);
1362 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");