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>
43 extern efi_status_t efi_call_phys (void *, ...);
47 static efi_runtime_services_t *runtime;
48 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
50 #define efi_call_virt(f, args...) (*(f))(args)
52 #define STUB_GET_TIME(prefix, adjust_arg) \
54 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
56 struct ia64_fpreg fr[6]; \
57 efi_time_cap_t *atc = NULL; \
61 atc = adjust_arg(tc); \
62 ia64_save_scratch_fpregs(fr); \
63 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
64 adjust_arg(tm), atc); \
65 ia64_load_scratch_fpregs(fr); \
69 #define STUB_SET_TIME(prefix, adjust_arg) \
71 prefix##_set_time (efi_time_t *tm) \
73 struct ia64_fpreg fr[6]; \
76 ia64_save_scratch_fpregs(fr); \
77 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
79 ia64_load_scratch_fpregs(fr); \
83 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
85 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
88 struct ia64_fpreg fr[6]; \
91 ia64_save_scratch_fpregs(fr); \
92 ret = efi_call_##prefix( \
93 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
94 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
95 ia64_load_scratch_fpregs(fr); \
99 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
100 static efi_status_t \
101 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
103 struct ia64_fpreg fr[6]; \
104 efi_time_t *atm = NULL; \
108 atm = adjust_arg(tm); \
109 ia64_save_scratch_fpregs(fr); \
110 ret = efi_call_##prefix( \
111 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
113 ia64_load_scratch_fpregs(fr); \
117 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
118 static efi_status_t \
119 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
120 unsigned long *data_size, void *data) \
122 struct ia64_fpreg fr[6]; \
127 aattr = adjust_arg(attr); \
128 ia64_save_scratch_fpregs(fr); \
129 ret = efi_call_##prefix( \
130 (efi_get_variable_t *) __va(runtime->get_variable), \
131 adjust_arg(name), adjust_arg(vendor), aattr, \
132 adjust_arg(data_size), adjust_arg(data)); \
133 ia64_load_scratch_fpregs(fr); \
137 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
138 static efi_status_t \
139 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
140 efi_guid_t *vendor) \
142 struct ia64_fpreg fr[6]; \
145 ia64_save_scratch_fpregs(fr); \
146 ret = efi_call_##prefix( \
147 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
148 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
149 ia64_load_scratch_fpregs(fr); \
153 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
154 static efi_status_t \
155 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
156 unsigned long attr, unsigned long data_size, \
159 struct ia64_fpreg fr[6]; \
162 ia64_save_scratch_fpregs(fr); \
163 ret = efi_call_##prefix( \
164 (efi_set_variable_t *) __va(runtime->set_variable), \
165 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
167 ia64_load_scratch_fpregs(fr); \
171 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
172 static efi_status_t \
173 prefix##_get_next_high_mono_count (u32 *count) \
175 struct ia64_fpreg fr[6]; \
178 ia64_save_scratch_fpregs(fr); \
179 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
180 __va(runtime->get_next_high_mono_count), \
181 adjust_arg(count)); \
182 ia64_load_scratch_fpregs(fr); \
186 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
188 prefix##_reset_system (int reset_type, efi_status_t status, \
189 unsigned long data_size, efi_char16_t *data) \
191 struct ia64_fpreg fr[6]; \
192 efi_char16_t *adata = NULL; \
195 adata = adjust_arg(data); \
197 ia64_save_scratch_fpregs(fr); \
199 (efi_reset_system_t *) __va(runtime->reset_system), \
200 reset_type, status, data_size, adata); \
201 /* should not return, but just in case... */ \
202 ia64_load_scratch_fpregs(fr); \
205 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
207 STUB_GET_TIME(phys, phys_ptr)
208 STUB_SET_TIME(phys, phys_ptr)
209 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
210 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
211 STUB_GET_VARIABLE(phys, phys_ptr)
212 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
213 STUB_SET_VARIABLE(phys, phys_ptr)
214 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
215 STUB_RESET_SYSTEM(phys, phys_ptr)
219 STUB_GET_TIME(virt, id)
220 STUB_SET_TIME(virt, id)
221 STUB_GET_WAKEUP_TIME(virt, id)
222 STUB_SET_WAKEUP_TIME(virt, id)
223 STUB_GET_VARIABLE(virt, id)
224 STUB_GET_NEXT_VARIABLE(virt, id)
225 STUB_SET_VARIABLE(virt, id)
226 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
227 STUB_RESET_SYSTEM(virt, id)
230 efi_gettimeofday (struct timespec *ts)
234 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
235 memset(ts, 0, sizeof(*ts));
239 ts->tv_sec = mktime(tm.year, tm.month, tm.day,
240 tm.hour, tm.minute, tm.second);
241 ts->tv_nsec = tm.nanosecond;
245 is_memory_available (efi_memory_desc_t *md)
247 if (!(md->attribute & EFI_MEMORY_WB))
251 case EFI_LOADER_CODE:
252 case EFI_LOADER_DATA:
253 case EFI_BOOT_SERVICES_CODE:
254 case EFI_BOOT_SERVICES_DATA:
255 case EFI_CONVENTIONAL_MEMORY:
261 typedef struct kern_memdesc {
267 static kern_memdesc_t *kern_memmap;
269 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
272 kmd_end(kern_memdesc_t *kmd)
274 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
278 efi_md_end(efi_memory_desc_t *md)
280 return (md->phys_addr + efi_md_size(md));
284 efi_wb(efi_memory_desc_t *md)
286 return (md->attribute & EFI_MEMORY_WB);
290 efi_uc(efi_memory_desc_t *md)
292 return (md->attribute & EFI_MEMORY_UC);
296 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
299 u64 start, end, voff;
301 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
302 for (k = kern_memmap; k->start != ~0UL; k++) {
303 if (k->attribute != attr)
305 start = PAGE_ALIGN(k->start);
306 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
308 if ((*callback)(start + voff, end + voff, arg) < 0)
314 * Walks the EFI memory map and calls CALLBACK once for each EFI memory
315 * descriptor that has memory that is available for OS use.
318 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
320 walk(callback, arg, EFI_MEMORY_WB);
324 * Walks the EFI memory map and calls CALLBACK once for each EFI memory
325 * descriptor that has memory that is available for uncached allocator.
328 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
330 walk(callback, arg, EFI_MEMORY_UC);
334 * Look for the PAL_CODE region reported by EFI and maps it using an
335 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
336 * Abstraction Layer chapter 11 in ADAG
339 efi_get_pal_addr (void)
341 void *efi_map_start, *efi_map_end, *p;
342 efi_memory_desc_t *md;
344 int pal_code_count = 0;
347 efi_map_start = __va(ia64_boot_param->efi_memmap);
348 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
349 efi_desc_size = ia64_boot_param->efi_memdesc_size;
351 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
353 if (md->type != EFI_PAL_CODE)
356 if (++pal_code_count > 1) {
357 printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
358 "dropped @ %lx\n", md->phys_addr);
362 * The only ITLB entry in region 7 that is used is the one
363 * installed by __start(). That entry covers a 64MB range.
365 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
366 vaddr = PAGE_OFFSET + md->phys_addr;
369 * We must check that the PAL mapping won't overlap with the
372 * PAL code is guaranteed to be aligned on a power of 2 between
373 * 4k and 256KB and that only one ITR is needed to map it. This
374 * implies that the PAL code is always aligned on its size,
375 * i.e., the closest matching page size supported by the TLB.
376 * Therefore PAL code is guaranteed never to cross a 64MB unless
377 * it is bigger than 64MB (very unlikely!). So for now the
378 * following test is enough to determine whether or not we need
379 * a dedicated ITR for the PAL code.
381 if ((vaddr & mask) == (KERNEL_START & mask)) {
382 printk(KERN_INFO "%s: no need to install ITR for "
383 "PAL code\n", __FUNCTION__);
387 if (efi_md_size(md) > IA64_GRANULE_SIZE)
388 panic("Woah! PAL code size bigger than a granule!");
391 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
393 printk(KERN_INFO "CPU %d: mapping PAL code "
394 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
395 smp_processor_id(), md->phys_addr,
396 md->phys_addr + efi_md_size(md),
397 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
399 return __va(md->phys_addr);
401 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
407 efi_map_pal_code (void)
409 void *pal_vaddr = efi_get_pal_addr ();
416 * Cannot write to CRx with PSR.ic=1
418 psr = ia64_clear_ic();
419 ia64_itr(0x1, IA64_TR_PALCODE,
420 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
421 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
423 ia64_set_psr(psr); /* restore psr */
430 void *efi_map_start, *efi_map_end;
431 efi_config_table_t *config_tables;
434 char *cp, vendor[100] = "unknown";
438 * it's too early to be able to use the standard kernel command line
441 for (cp = boot_command_line; *cp; ) {
442 if (memcmp(cp, "mem=", 4) == 0) {
443 mem_limit = memparse(cp + 4, &cp);
444 } else if (memcmp(cp, "max_addr=", 9) == 0) {
445 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
446 } else if (memcmp(cp, "min_addr=", 9) == 0) {
447 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
449 while (*cp != ' ' && *cp)
456 printk(KERN_INFO "Ignoring memory below %luMB\n",
458 if (max_addr != ~0UL)
459 printk(KERN_INFO "Ignoring memory above %luMB\n",
462 efi.systab = __va(ia64_boot_param->efi_systab);
465 * Verify the EFI Table
467 if (efi.systab == NULL)
468 panic("Woah! Can't find EFI system table.\n");
469 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
470 panic("Woah! EFI system table signature incorrect\n");
471 if ((efi.systab->hdr.revision >> 16) == 0)
472 printk(KERN_WARNING "Warning: EFI system table version "
473 "%d.%02d, expected 1.00 or greater\n",
474 efi.systab->hdr.revision >> 16,
475 efi.systab->hdr.revision & 0xffff);
477 config_tables = __va(efi.systab->tables);
479 /* Show what we know for posterity */
480 c16 = __va(efi.systab->fw_vendor);
482 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
487 printk(KERN_INFO "EFI v%u.%.02u by %s:",
488 efi.systab->hdr.revision >> 16,
489 efi.systab->hdr.revision & 0xffff, vendor);
491 efi.mps = EFI_INVALID_TABLE_ADDR;
492 efi.acpi = EFI_INVALID_TABLE_ADDR;
493 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
494 efi.smbios = EFI_INVALID_TABLE_ADDR;
495 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
496 efi.boot_info = EFI_INVALID_TABLE_ADDR;
497 efi.hcdp = EFI_INVALID_TABLE_ADDR;
498 efi.uga = EFI_INVALID_TABLE_ADDR;
500 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
501 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
502 efi.mps = config_tables[i].table;
503 printk(" MPS=0x%lx", config_tables[i].table);
504 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
505 efi.acpi20 = config_tables[i].table;
506 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
507 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
508 efi.acpi = config_tables[i].table;
509 printk(" ACPI=0x%lx", config_tables[i].table);
510 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
511 efi.smbios = config_tables[i].table;
512 printk(" SMBIOS=0x%lx", config_tables[i].table);
513 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
514 efi.sal_systab = config_tables[i].table;
515 printk(" SALsystab=0x%lx", config_tables[i].table);
516 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
517 efi.hcdp = config_tables[i].table;
518 printk(" HCDP=0x%lx", config_tables[i].table);
523 runtime = __va(efi.systab->runtime);
524 efi.get_time = phys_get_time;
525 efi.set_time = phys_set_time;
526 efi.get_wakeup_time = phys_get_wakeup_time;
527 efi.set_wakeup_time = phys_set_wakeup_time;
528 efi.get_variable = phys_get_variable;
529 efi.get_next_variable = phys_get_next_variable;
530 efi.set_variable = phys_set_variable;
531 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
532 efi.reset_system = phys_reset_system;
534 efi_map_start = __va(ia64_boot_param->efi_memmap);
535 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
536 efi_desc_size = ia64_boot_param->efi_memdesc_size;
539 /* print EFI memory map: */
541 efi_memory_desc_t *md;
544 for (i = 0, p = efi_map_start; p < efi_map_end;
545 ++i, p += efi_desc_size)
548 printk("mem%02u: type=%u, attr=0x%lx, "
549 "range=[0x%016lx-0x%016lx) (%luMB)\n",
550 i, md->type, md->attribute, md->phys_addr,
551 md->phys_addr + efi_md_size(md),
552 md->num_pages >> (20 - EFI_PAGE_SHIFT));
558 efi_enter_virtual_mode();
562 efi_enter_virtual_mode (void)
564 void *efi_map_start, *efi_map_end, *p;
565 efi_memory_desc_t *md;
569 efi_map_start = __va(ia64_boot_param->efi_memmap);
570 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
571 efi_desc_size = ia64_boot_param->efi_memdesc_size;
573 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
575 if (md->attribute & EFI_MEMORY_RUNTIME) {
577 * Some descriptors have multiple bits set, so the
578 * order of the tests is relevant.
580 if (md->attribute & EFI_MEMORY_WB) {
581 md->virt_addr = (u64) __va(md->phys_addr);
582 } else if (md->attribute & EFI_MEMORY_UC) {
583 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
584 } else if (md->attribute & EFI_MEMORY_WC) {
586 md->virt_addr = ia64_remap(md->phys_addr,
594 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
595 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
597 } else if (md->attribute & EFI_MEMORY_WT) {
599 md->virt_addr = ia64_remap(md->phys_addr,
607 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
608 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
614 status = efi_call_phys(__va(runtime->set_virtual_address_map),
615 ia64_boot_param->efi_memmap_size,
617 ia64_boot_param->efi_memdesc_version,
618 ia64_boot_param->efi_memmap);
619 if (status != EFI_SUCCESS) {
620 printk(KERN_WARNING "warning: unable to switch EFI into "
621 "virtual mode (status=%lu)\n", status);
626 * Now that EFI is in virtual mode, we call the EFI functions more
629 efi.get_time = virt_get_time;
630 efi.set_time = virt_set_time;
631 efi.get_wakeup_time = virt_get_wakeup_time;
632 efi.set_wakeup_time = virt_set_wakeup_time;
633 efi.get_variable = virt_get_variable;
634 efi.get_next_variable = virt_get_next_variable;
635 efi.set_variable = virt_set_variable;
636 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
637 efi.reset_system = virt_reset_system;
641 * Walk the EFI memory map looking for the I/O port range. There can only be
642 * one entry of this type, other I/O port ranges should be described via ACPI.
645 efi_get_iobase (void)
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) {
657 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
658 if (md->attribute & EFI_MEMORY_UC)
659 return md->phys_addr;
665 static struct kern_memdesc *
666 kern_memory_descriptor (unsigned long phys_addr)
668 struct kern_memdesc *md;
670 for (md = kern_memmap; md->start != ~0UL; md++) {
671 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
677 static efi_memory_desc_t *
678 efi_memory_descriptor (unsigned long phys_addr)
680 void *efi_map_start, *efi_map_end, *p;
681 efi_memory_desc_t *md;
684 efi_map_start = __va(ia64_boot_param->efi_memmap);
685 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
686 efi_desc_size = ia64_boot_param->efi_memdesc_size;
688 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
691 if (phys_addr - md->phys_addr < efi_md_size(md))
698 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
700 void *efi_map_start, *efi_map_end, *p;
701 efi_memory_desc_t *md;
705 efi_map_start = __va(ia64_boot_param->efi_memmap);
706 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
707 efi_desc_size = ia64_boot_param->efi_memdesc_size;
709 end = phys_addr + size;
711 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
713 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
720 efi_mem_type (unsigned long phys_addr)
722 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
730 efi_mem_attributes (unsigned long phys_addr)
732 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
735 return md->attribute;
738 EXPORT_SYMBOL(efi_mem_attributes);
741 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
743 unsigned long end = phys_addr + size;
744 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
751 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
752 * the kernel that firmware needs this region mapped.
754 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
756 unsigned long md_end = efi_md_end(md);
761 md = efi_memory_descriptor(md_end);
762 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
769 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
771 unsigned long end = phys_addr + size;
772 struct kern_memdesc *md;
776 * This is a hack for ioremap calls before we set up kern_memmap.
777 * Maybe we should do efi_memmap_init() earlier instead.
780 attr = efi_mem_attribute(phys_addr, size);
781 if (attr & EFI_MEMORY_WB)
782 return EFI_MEMORY_WB;
786 md = kern_memory_descriptor(phys_addr);
790 attr = md->attribute;
792 unsigned long md_end = kmd_end(md);
797 md = kern_memory_descriptor(md_end);
798 if (!md || md->attribute != attr)
803 EXPORT_SYMBOL(kern_mem_attribute);
806 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
811 * /dev/mem reads and writes use copy_to_user(), which implicitly
812 * uses a granule-sized kernel identity mapping. It's really
813 * only safe to do this for regions in kern_memmap. For more
814 * details, see Documentation/ia64/aliasing.txt.
816 attr = kern_mem_attribute(phys_addr, size);
817 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
823 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
825 unsigned long phys_addr = pfn << PAGE_SHIFT;
828 attr = efi_mem_attribute(phys_addr, size);
831 * /dev/mem mmap uses normal user pages, so we don't need the entire
832 * granule, but the entire region we're mapping must support the same
835 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
839 * Intel firmware doesn't tell us about all the MMIO regions, so
840 * in general we have to allow mmap requests. But if EFI *does*
841 * tell us about anything inside this region, we should deny it.
842 * The user can always map a smaller region to avoid the overlap.
844 if (efi_memmap_intersects(phys_addr, size))
851 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
854 unsigned long phys_addr = pfn << PAGE_SHIFT;
858 * For /dev/mem mmap, we use user mappings, but if the region is
859 * in kern_memmap (and hence may be covered by a kernel mapping),
860 * we must use the same attribute as the kernel mapping.
862 attr = kern_mem_attribute(phys_addr, size);
863 if (attr & EFI_MEMORY_WB)
864 return pgprot_cacheable(vma_prot);
865 else if (attr & EFI_MEMORY_UC)
866 return pgprot_noncached(vma_prot);
869 * Some chipsets don't support UC access to memory. If
870 * WB is supported, we prefer that.
872 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
873 return pgprot_cacheable(vma_prot);
875 return pgprot_noncached(vma_prot);
879 efi_uart_console_only(void)
882 char *s, name[] = "ConOut";
883 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
884 efi_char16_t *utf16, name_utf16[32];
885 unsigned char data[1024];
886 unsigned long size = sizeof(data);
887 struct efi_generic_dev_path *hdr, *end_addr;
890 /* Convert to UTF-16 */
894 *utf16++ = *s++ & 0x7f;
897 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
898 if (status != EFI_SUCCESS) {
899 printk(KERN_ERR "No EFI %s variable?\n", name);
903 hdr = (struct efi_generic_dev_path *) data;
904 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
905 while (hdr < end_addr) {
906 if (hdr->type == EFI_DEV_MSG &&
907 hdr->sub_type == EFI_DEV_MSG_UART)
909 else if (hdr->type == EFI_DEV_END_PATH ||
910 hdr->type == EFI_DEV_END_PATH2) {
913 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
917 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
919 printk(KERN_ERR "Malformed %s value\n", name);
924 * Look for the first granule aligned memory descriptor memory
925 * that is big enough to hold EFI memory map. Make sure this
926 * descriptor is atleast granule sized so it does not get trimmed
928 struct kern_memdesc *
929 find_memmap_space (void)
931 u64 contig_low=0, contig_high=0;
933 void *efi_map_start, *efi_map_end, *p, *q;
934 efi_memory_desc_t *md, *pmd = NULL, *check_md;
935 u64 space_needed, efi_desc_size;
936 unsigned long total_mem = 0;
938 efi_map_start = __va(ia64_boot_param->efi_memmap);
939 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
940 efi_desc_size = ia64_boot_param->efi_memdesc_size;
943 * Worst case: we need 3 kernel descriptors for each efi descriptor
944 * (if every entry has a WB part in the middle, and UC head and tail),
945 * plus one for the end marker.
947 space_needed = sizeof(kern_memdesc_t) *
948 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
950 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
955 if (pmd == NULL || !efi_wb(pmd) ||
956 efi_md_end(pmd) != md->phys_addr) {
957 contig_low = GRANULEROUNDUP(md->phys_addr);
958 contig_high = efi_md_end(md);
959 for (q = p + efi_desc_size; q < efi_map_end;
960 q += efi_desc_size) {
962 if (!efi_wb(check_md))
964 if (contig_high != check_md->phys_addr)
966 contig_high = efi_md_end(check_md);
968 contig_high = GRANULEROUNDDOWN(contig_high);
970 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
973 /* Round ends inward to granule boundaries */
974 as = max(contig_low, md->phys_addr);
975 ae = min(contig_high, efi_md_end(md));
977 /* keep within max_addr= and min_addr= command line arg */
978 as = max(as, min_addr);
979 ae = min(ae, max_addr);
983 /* avoid going over mem= command line arg */
984 if (total_mem + (ae - as) > mem_limit)
985 ae -= total_mem + (ae - as) - mem_limit;
990 if (ae - as > space_needed)
993 if (p >= efi_map_end)
994 panic("Can't allocate space for kernel memory descriptors");
1000 * Walk the EFI memory map and gather all memory available for kernel
1001 * to use. We can allocate partial granules only if the unavailable
1002 * parts exist, and are WB.
1005 efi_memmap_init(unsigned long *s, unsigned long *e)
1007 struct kern_memdesc *k, *prev = NULL;
1008 u64 contig_low=0, contig_high=0;
1010 void *efi_map_start, *efi_map_end, *p, *q;
1011 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1013 unsigned long total_mem = 0;
1015 k = kern_memmap = find_memmap_space();
1017 efi_map_start = __va(ia64_boot_param->efi_memmap);
1018 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1019 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1021 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1025 (md->type == EFI_CONVENTIONAL_MEMORY ||
1026 md->type == EFI_BOOT_SERVICES_DATA)) {
1027 k->attribute = EFI_MEMORY_UC;
1028 k->start = md->phys_addr;
1029 k->num_pages = md->num_pages;
1034 if (pmd == NULL || !efi_wb(pmd) ||
1035 efi_md_end(pmd) != md->phys_addr) {
1036 contig_low = GRANULEROUNDUP(md->phys_addr);
1037 contig_high = efi_md_end(md);
1038 for (q = p + efi_desc_size; q < efi_map_end;
1039 q += efi_desc_size) {
1041 if (!efi_wb(check_md))
1043 if (contig_high != check_md->phys_addr)
1045 contig_high = efi_md_end(check_md);
1047 contig_high = GRANULEROUNDDOWN(contig_high);
1049 if (!is_memory_available(md))
1052 #ifdef CONFIG_CRASH_DUMP
1053 /* saved_max_pfn should ignore max_addr= command line arg */
1054 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1055 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1058 * Round ends inward to granule boundaries
1059 * Give trimmings to uncached allocator
1061 if (md->phys_addr < contig_low) {
1062 lim = min(efi_md_end(md), contig_low);
1064 if (k > kern_memmap &&
1065 (k-1)->attribute == EFI_MEMORY_UC &&
1066 kmd_end(k-1) == md->phys_addr) {
1068 (lim - md->phys_addr)
1071 k->attribute = EFI_MEMORY_UC;
1072 k->start = md->phys_addr;
1073 k->num_pages = (lim - md->phys_addr)
1082 if (efi_md_end(md) > contig_high) {
1083 lim = max(md->phys_addr, contig_high);
1085 if (lim == md->phys_addr && k > kern_memmap &&
1086 (k-1)->attribute == EFI_MEMORY_UC &&
1087 kmd_end(k-1) == md->phys_addr) {
1088 (k-1)->num_pages += md->num_pages;
1090 k->attribute = EFI_MEMORY_UC;
1092 k->num_pages = (efi_md_end(md) - lim)
1099 ae = efi_md_end(md);
1101 /* keep within max_addr= and min_addr= command line arg */
1102 as = max(as, min_addr);
1103 ae = min(ae, max_addr);
1107 /* avoid going over mem= command line arg */
1108 if (total_mem + (ae - as) > mem_limit)
1109 ae -= total_mem + (ae - as) - mem_limit;
1113 if (prev && kmd_end(prev) == md->phys_addr) {
1114 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1115 total_mem += ae - as;
1118 k->attribute = EFI_MEMORY_WB;
1120 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1121 total_mem += ae - as;
1124 k->start = ~0L; /* end-marker */
1126 /* reserve the memory we are using for kern_memmap */
1127 *s = (u64)kern_memmap;
1134 efi_initialize_iomem_resources(struct resource *code_resource,
1135 struct resource *data_resource,
1136 struct resource *bss_resource)
1138 struct resource *res;
1139 void *efi_map_start, *efi_map_end, *p;
1140 efi_memory_desc_t *md;
1143 unsigned long flags;
1145 efi_map_start = __va(ia64_boot_param->efi_memmap);
1146 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1147 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1151 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1154 if (md->num_pages == 0) /* should not happen */
1157 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1160 case EFI_MEMORY_MAPPED_IO:
1161 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1164 case EFI_LOADER_CODE:
1165 case EFI_LOADER_DATA:
1166 case EFI_BOOT_SERVICES_DATA:
1167 case EFI_BOOT_SERVICES_CODE:
1168 case EFI_CONVENTIONAL_MEMORY:
1169 if (md->attribute & EFI_MEMORY_WP) {
1170 name = "System ROM";
1171 flags |= IORESOURCE_READONLY;
1173 name = "System RAM";
1177 case EFI_ACPI_MEMORY_NVS:
1178 name = "ACPI Non-volatile Storage";
1181 case EFI_UNUSABLE_MEMORY:
1183 flags |= IORESOURCE_DISABLED;
1186 case EFI_RESERVED_TYPE:
1187 case EFI_RUNTIME_SERVICES_CODE:
1188 case EFI_RUNTIME_SERVICES_DATA:
1189 case EFI_ACPI_RECLAIM_MEMORY:
1195 if ((res = kzalloc(sizeof(struct resource),
1196 GFP_KERNEL)) == NULL) {
1198 "failed to alocate resource for iomem\n");
1203 res->start = md->phys_addr;
1204 res->end = md->phys_addr + efi_md_size(md) - 1;
1207 if (insert_resource(&iomem_resource, res) < 0)
1211 * We don't know which region contains
1212 * kernel data so we try it repeatedly and
1213 * let the resource manager test it.
1215 insert_resource(res, code_resource);
1216 insert_resource(res, data_resource);
1217 insert_resource(res, bss_resource);
1219 insert_resource(res, &efi_memmap_res);
1220 insert_resource(res, &boot_param_res);
1221 if (crashk_res.end > crashk_res.start)
1222 insert_resource(res, &crashk_res);
1229 /* find a block of memory aligned to 64M exclude reserved regions
1230 rsvd_regions are sorted
1232 unsigned long __init
1233 kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1237 u64 alignment = 1UL << _PAGE_SIZE_64M;
1238 void *efi_map_start, *efi_map_end, *p;
1239 efi_memory_desc_t *md;
1242 efi_map_start = __va(ia64_boot_param->efi_memmap);
1243 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1244 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1246 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1250 start = ALIGN(md->phys_addr, alignment);
1251 end = efi_md_end(md);
1252 for (i = 0; i < n; i++) {
1253 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1254 if (__pa(r[i].start) > start + size)
1256 start = ALIGN(__pa(r[i].end), alignment);
1258 __pa(r[i+1].start) < start + size)
1264 if (end > start + size)
1269 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1274 #ifdef CONFIG_PROC_VMCORE
1275 /* locate the size find a the descriptor at a certain address */
1276 unsigned long __init
1277 vmcore_find_descriptor_size (unsigned long address)
1279 void *efi_map_start, *efi_map_end, *p;
1280 efi_memory_desc_t *md;
1282 unsigned long ret = 0;
1284 efi_map_start = __va(ia64_boot_param->efi_memmap);
1285 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1286 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1288 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1290 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1291 && md->phys_addr == address) {
1292 ret = efi_md_size(md);
1298 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");