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/kernel.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/time.h>
28 #include <linux/efi.h>
29 #include <linux/kexec.h>
32 #include <asm/kregs.h>
33 #include <asm/meminit.h>
34 #include <asm/pgtable.h>
35 #include <asm/processor.h>
40 extern efi_status_t efi_call_phys (void *, ...);
44 static efi_runtime_services_t *runtime;
45 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
47 #define efi_call_virt(f, args...) (*(f))(args)
49 #define STUB_GET_TIME(prefix, adjust_arg) \
51 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
53 struct ia64_fpreg fr[6]; \
54 efi_time_cap_t *atc = NULL; \
58 atc = adjust_arg(tc); \
59 ia64_save_scratch_fpregs(fr); \
60 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
61 ia64_load_scratch_fpregs(fr); \
65 #define STUB_SET_TIME(prefix, adjust_arg) \
67 prefix##_set_time (efi_time_t *tm) \
69 struct ia64_fpreg fr[6]; \
72 ia64_save_scratch_fpregs(fr); \
73 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
74 ia64_load_scratch_fpregs(fr); \
78 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
80 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
82 struct ia64_fpreg fr[6]; \
85 ia64_save_scratch_fpregs(fr); \
86 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
87 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
88 ia64_load_scratch_fpregs(fr); \
92 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
94 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
96 struct ia64_fpreg fr[6]; \
97 efi_time_t *atm = NULL; \
101 atm = adjust_arg(tm); \
102 ia64_save_scratch_fpregs(fr); \
103 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
105 ia64_load_scratch_fpregs(fr); \
109 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
110 static efi_status_t \
111 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
112 unsigned long *data_size, void *data) \
114 struct ia64_fpreg fr[6]; \
119 aattr = adjust_arg(attr); \
120 ia64_save_scratch_fpregs(fr); \
121 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
122 adjust_arg(name), adjust_arg(vendor), aattr, \
123 adjust_arg(data_size), adjust_arg(data)); \
124 ia64_load_scratch_fpregs(fr); \
128 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
129 static efi_status_t \
130 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
132 struct ia64_fpreg fr[6]; \
135 ia64_save_scratch_fpregs(fr); \
136 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
137 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
138 ia64_load_scratch_fpregs(fr); \
142 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
143 static efi_status_t \
144 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
145 unsigned long data_size, void *data) \
147 struct ia64_fpreg fr[6]; \
150 ia64_save_scratch_fpregs(fr); \
151 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
152 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
154 ia64_load_scratch_fpregs(fr); \
158 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
159 static efi_status_t \
160 prefix##_get_next_high_mono_count (u32 *count) \
162 struct ia64_fpreg fr[6]; \
165 ia64_save_scratch_fpregs(fr); \
166 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
167 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
168 ia64_load_scratch_fpregs(fr); \
172 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
174 prefix##_reset_system (int reset_type, efi_status_t status, \
175 unsigned long data_size, efi_char16_t *data) \
177 struct ia64_fpreg fr[6]; \
178 efi_char16_t *adata = NULL; \
181 adata = adjust_arg(data); \
183 ia64_save_scratch_fpregs(fr); \
184 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
185 reset_type, status, data_size, adata); \
186 /* should not return, but just in case... */ \
187 ia64_load_scratch_fpregs(fr); \
190 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
192 STUB_GET_TIME(phys, phys_ptr)
193 STUB_SET_TIME(phys, phys_ptr)
194 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
195 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
196 STUB_GET_VARIABLE(phys, phys_ptr)
197 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
198 STUB_SET_VARIABLE(phys, phys_ptr)
199 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
200 STUB_RESET_SYSTEM(phys, phys_ptr)
204 STUB_GET_TIME(virt, id)
205 STUB_SET_TIME(virt, id)
206 STUB_GET_WAKEUP_TIME(virt, id)
207 STUB_SET_WAKEUP_TIME(virt, id)
208 STUB_GET_VARIABLE(virt, id)
209 STUB_GET_NEXT_VARIABLE(virt, id)
210 STUB_SET_VARIABLE(virt, id)
211 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
212 STUB_RESET_SYSTEM(virt, id)
215 efi_gettimeofday (struct timespec *ts)
219 memset(ts, 0, sizeof(ts));
220 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
223 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
224 ts->tv_nsec = tm.nanosecond;
228 is_memory_available (efi_memory_desc_t *md)
230 if (!(md->attribute & EFI_MEMORY_WB))
234 case EFI_LOADER_CODE:
235 case EFI_LOADER_DATA:
236 case EFI_BOOT_SERVICES_CODE:
237 case EFI_BOOT_SERVICES_DATA:
238 case EFI_CONVENTIONAL_MEMORY:
244 typedef struct kern_memdesc {
250 static kern_memdesc_t *kern_memmap;
252 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
255 kmd_end(kern_memdesc_t *kmd)
257 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
261 efi_md_end(efi_memory_desc_t *md)
263 return (md->phys_addr + efi_md_size(md));
267 efi_wb(efi_memory_desc_t *md)
269 return (md->attribute & EFI_MEMORY_WB);
273 efi_uc(efi_memory_desc_t *md)
275 return (md->attribute & EFI_MEMORY_UC);
279 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
282 u64 start, end, voff;
284 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
285 for (k = kern_memmap; k->start != ~0UL; k++) {
286 if (k->attribute != attr)
288 start = PAGE_ALIGN(k->start);
289 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
291 if ((*callback)(start + voff, end + voff, arg) < 0)
297 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
298 * has memory that is available for OS use.
301 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
303 walk(callback, arg, EFI_MEMORY_WB);
307 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
308 * has memory that is available for uncached allocator.
311 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
313 walk(callback, arg, EFI_MEMORY_UC);
317 * Look for the PAL_CODE region reported by EFI and maps it using an
318 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
319 * Abstraction Layer chapter 11 in ADAG
323 efi_get_pal_addr (void)
325 void *efi_map_start, *efi_map_end, *p;
326 efi_memory_desc_t *md;
328 int pal_code_count = 0;
331 efi_map_start = __va(ia64_boot_param->efi_memmap);
332 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
333 efi_desc_size = ia64_boot_param->efi_memdesc_size;
335 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
337 if (md->type != EFI_PAL_CODE)
340 if (++pal_code_count > 1) {
341 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
346 * The only ITLB entry in region 7 that is used is the one installed by
347 * __start(). That entry covers a 64MB range.
349 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
350 vaddr = PAGE_OFFSET + md->phys_addr;
353 * We must check that the PAL mapping won't overlap with the kernel
356 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
357 * 256KB and that only one ITR is needed to map it. This implies that the
358 * PAL code is always aligned on its size, i.e., the closest matching page
359 * size supported by the TLB. Therefore PAL code is guaranteed never to
360 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
361 * now the following test is enough to determine whether or not we need a
362 * dedicated ITR for the PAL code.
364 if ((vaddr & mask) == (KERNEL_START & mask)) {
365 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
370 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
371 panic("Woah! PAL code size bigger than a granule!");
374 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
376 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
377 smp_processor_id(), md->phys_addr,
378 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
379 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
381 return __va(md->phys_addr);
383 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
389 efi_map_pal_code (void)
391 void *pal_vaddr = efi_get_pal_addr ();
398 * Cannot write to CRx with PSR.ic=1
400 psr = ia64_clear_ic();
401 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
402 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
404 ia64_set_psr(psr); /* restore psr */
411 void *efi_map_start, *efi_map_end;
412 efi_config_table_t *config_tables;
415 char *cp, vendor[100] = "unknown";
418 /* it's too early to be able to use the standard kernel command line support... */
419 for (cp = boot_command_line; *cp; ) {
420 if (memcmp(cp, "mem=", 4) == 0) {
421 mem_limit = memparse(cp + 4, &cp);
422 } else if (memcmp(cp, "max_addr=", 9) == 0) {
423 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
424 } else if (memcmp(cp, "min_addr=", 9) == 0) {
425 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
427 while (*cp != ' ' && *cp)
434 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
435 if (max_addr != ~0UL)
436 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
438 efi.systab = __va(ia64_boot_param->efi_systab);
441 * Verify the EFI Table
443 if (efi.systab == NULL)
444 panic("Woah! Can't find EFI system table.\n");
445 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
446 panic("Woah! EFI system table signature incorrect\n");
447 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
448 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
449 "got %d.%02d, expected %d.%02d\n",
450 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
451 EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
453 config_tables = __va(efi.systab->tables);
455 /* Show what we know for posterity */
456 c16 = __va(efi.systab->fw_vendor);
458 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
463 printk(KERN_INFO "EFI v%u.%.02u by %s:",
464 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
466 efi.mps = EFI_INVALID_TABLE_ADDR;
467 efi.acpi = EFI_INVALID_TABLE_ADDR;
468 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
469 efi.smbios = EFI_INVALID_TABLE_ADDR;
470 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
471 efi.boot_info = EFI_INVALID_TABLE_ADDR;
472 efi.hcdp = EFI_INVALID_TABLE_ADDR;
473 efi.uga = EFI_INVALID_TABLE_ADDR;
475 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
476 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
477 efi.mps = config_tables[i].table;
478 printk(" MPS=0x%lx", config_tables[i].table);
479 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
480 efi.acpi20 = config_tables[i].table;
481 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
482 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
483 efi.acpi = config_tables[i].table;
484 printk(" ACPI=0x%lx", config_tables[i].table);
485 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
486 efi.smbios = config_tables[i].table;
487 printk(" SMBIOS=0x%lx", config_tables[i].table);
488 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
489 efi.sal_systab = config_tables[i].table;
490 printk(" SALsystab=0x%lx", config_tables[i].table);
491 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
492 efi.hcdp = config_tables[i].table;
493 printk(" HCDP=0x%lx", config_tables[i].table);
498 runtime = __va(efi.systab->runtime);
499 efi.get_time = phys_get_time;
500 efi.set_time = phys_set_time;
501 efi.get_wakeup_time = phys_get_wakeup_time;
502 efi.set_wakeup_time = phys_set_wakeup_time;
503 efi.get_variable = phys_get_variable;
504 efi.get_next_variable = phys_get_next_variable;
505 efi.set_variable = phys_set_variable;
506 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
507 efi.reset_system = phys_reset_system;
509 efi_map_start = __va(ia64_boot_param->efi_memmap);
510 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
511 efi_desc_size = ia64_boot_param->efi_memdesc_size;
514 /* print EFI memory map: */
516 efi_memory_desc_t *md;
519 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
521 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
522 i, md->type, md->attribute, md->phys_addr,
523 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
524 md->num_pages >> (20 - EFI_PAGE_SHIFT));
530 efi_enter_virtual_mode();
534 efi_enter_virtual_mode (void)
536 void *efi_map_start, *efi_map_end, *p;
537 efi_memory_desc_t *md;
541 efi_map_start = __va(ia64_boot_param->efi_memmap);
542 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
543 efi_desc_size = ia64_boot_param->efi_memdesc_size;
545 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
547 if (md->attribute & EFI_MEMORY_RUNTIME) {
549 * Some descriptors have multiple bits set, so the order of
550 * the tests is relevant.
552 if (md->attribute & EFI_MEMORY_WB) {
553 md->virt_addr = (u64) __va(md->phys_addr);
554 } else if (md->attribute & EFI_MEMORY_UC) {
555 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
556 } else if (md->attribute & EFI_MEMORY_WC) {
558 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
564 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
565 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
567 } else if (md->attribute & EFI_MEMORY_WT) {
569 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
570 | _PAGE_D | _PAGE_MA_WT
574 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
575 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
581 status = efi_call_phys(__va(runtime->set_virtual_address_map),
582 ia64_boot_param->efi_memmap_size,
583 efi_desc_size, ia64_boot_param->efi_memdesc_version,
584 ia64_boot_param->efi_memmap);
585 if (status != EFI_SUCCESS) {
586 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
587 "(status=%lu)\n", status);
592 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
594 efi.get_time = virt_get_time;
595 efi.set_time = virt_set_time;
596 efi.get_wakeup_time = virt_get_wakeup_time;
597 efi.set_wakeup_time = virt_set_wakeup_time;
598 efi.get_variable = virt_get_variable;
599 efi.get_next_variable = virt_get_next_variable;
600 efi.set_variable = virt_set_variable;
601 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
602 efi.reset_system = virt_reset_system;
606 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
607 * this type, other I/O port ranges should be described via ACPI.
610 efi_get_iobase (void)
612 void *efi_map_start, *efi_map_end, *p;
613 efi_memory_desc_t *md;
616 efi_map_start = __va(ia64_boot_param->efi_memmap);
617 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
618 efi_desc_size = ia64_boot_param->efi_memdesc_size;
620 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
622 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
623 if (md->attribute & EFI_MEMORY_UC)
624 return md->phys_addr;
630 static struct kern_memdesc *
631 kern_memory_descriptor (unsigned long phys_addr)
633 struct kern_memdesc *md;
635 for (md = kern_memmap; md->start != ~0UL; md++) {
636 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
642 static efi_memory_desc_t *
643 efi_memory_descriptor (unsigned long phys_addr)
645 void *efi_map_start, *efi_map_end, *p;
646 efi_memory_desc_t *md;
649 efi_map_start = __va(ia64_boot_param->efi_memmap);
650 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
651 efi_desc_size = ia64_boot_param->efi_memdesc_size;
653 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
656 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
663 efi_mem_type (unsigned long phys_addr)
665 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
673 efi_mem_attributes (unsigned long phys_addr)
675 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
678 return md->attribute;
681 EXPORT_SYMBOL(efi_mem_attributes);
684 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
686 unsigned long end = phys_addr + size;
687 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
694 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
695 * the kernel that firmware needs this region mapped.
697 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
699 unsigned long md_end = efi_md_end(md);
704 md = efi_memory_descriptor(md_end);
705 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
712 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
714 unsigned long end = phys_addr + size;
715 struct kern_memdesc *md;
719 * This is a hack for ioremap calls before we set up kern_memmap.
720 * Maybe we should do efi_memmap_init() earlier instead.
723 attr = efi_mem_attribute(phys_addr, size);
724 if (attr & EFI_MEMORY_WB)
725 return EFI_MEMORY_WB;
729 md = kern_memory_descriptor(phys_addr);
733 attr = md->attribute;
735 unsigned long md_end = kmd_end(md);
740 md = kern_memory_descriptor(md_end);
741 if (!md || md->attribute != attr)
746 EXPORT_SYMBOL(kern_mem_attribute);
749 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
754 * /dev/mem reads and writes use copy_to_user(), which implicitly
755 * uses a granule-sized kernel identity mapping. It's really
756 * only safe to do this for regions in kern_memmap. For more
757 * details, see Documentation/ia64/aliasing.txt.
759 attr = kern_mem_attribute(phys_addr, size);
760 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
766 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
769 * MMIO regions are often missing from the EFI memory map.
770 * We must allow mmap of them for programs like X, so we
771 * currently can't do any useful validation.
777 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
780 unsigned long phys_addr = pfn << PAGE_SHIFT;
784 * For /dev/mem mmap, we use user mappings, but if the region is
785 * in kern_memmap (and hence may be covered by a kernel mapping),
786 * we must use the same attribute as the kernel mapping.
788 attr = kern_mem_attribute(phys_addr, size);
789 if (attr & EFI_MEMORY_WB)
790 return pgprot_cacheable(vma_prot);
791 else if (attr & EFI_MEMORY_UC)
792 return pgprot_noncached(vma_prot);
795 * Some chipsets don't support UC access to memory. If
796 * WB is supported, we prefer that.
798 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
799 return pgprot_cacheable(vma_prot);
801 return pgprot_noncached(vma_prot);
805 efi_uart_console_only(void)
808 char *s, name[] = "ConOut";
809 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
810 efi_char16_t *utf16, name_utf16[32];
811 unsigned char data[1024];
812 unsigned long size = sizeof(data);
813 struct efi_generic_dev_path *hdr, *end_addr;
816 /* Convert to UTF-16 */
820 *utf16++ = *s++ & 0x7f;
823 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
824 if (status != EFI_SUCCESS) {
825 printk(KERN_ERR "No EFI %s variable?\n", name);
829 hdr = (struct efi_generic_dev_path *) data;
830 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
831 while (hdr < end_addr) {
832 if (hdr->type == EFI_DEV_MSG &&
833 hdr->sub_type == EFI_DEV_MSG_UART)
835 else if (hdr->type == EFI_DEV_END_PATH ||
836 hdr->type == EFI_DEV_END_PATH2) {
839 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
843 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
845 printk(KERN_ERR "Malformed %s value\n", name);
850 * Look for the first granule aligned memory descriptor memory
851 * that is big enough to hold EFI memory map. Make sure this
852 * descriptor is atleast granule sized so it does not get trimmed
854 struct kern_memdesc *
855 find_memmap_space (void)
857 u64 contig_low=0, contig_high=0;
859 void *efi_map_start, *efi_map_end, *p, *q;
860 efi_memory_desc_t *md, *pmd = NULL, *check_md;
861 u64 space_needed, efi_desc_size;
862 unsigned long total_mem = 0;
864 efi_map_start = __va(ia64_boot_param->efi_memmap);
865 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
866 efi_desc_size = ia64_boot_param->efi_memdesc_size;
869 * Worst case: we need 3 kernel descriptors for each efi descriptor
870 * (if every entry has a WB part in the middle, and UC head and tail),
871 * plus one for the end marker.
873 space_needed = sizeof(kern_memdesc_t) *
874 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
876 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
881 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
882 contig_low = GRANULEROUNDUP(md->phys_addr);
883 contig_high = efi_md_end(md);
884 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
886 if (!efi_wb(check_md))
888 if (contig_high != check_md->phys_addr)
890 contig_high = efi_md_end(check_md);
892 contig_high = GRANULEROUNDDOWN(contig_high);
894 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
897 /* Round ends inward to granule boundaries */
898 as = max(contig_low, md->phys_addr);
899 ae = min(contig_high, efi_md_end(md));
901 /* keep within max_addr= and min_addr= command line arg */
902 as = max(as, min_addr);
903 ae = min(ae, max_addr);
907 /* avoid going over mem= command line arg */
908 if (total_mem + (ae - as) > mem_limit)
909 ae -= total_mem + (ae - as) - mem_limit;
914 if (ae - as > space_needed)
917 if (p >= efi_map_end)
918 panic("Can't allocate space for kernel memory descriptors");
924 * Walk the EFI memory map and gather all memory available for kernel
925 * to use. We can allocate partial granules only if the unavailable
926 * parts exist, and are WB.
929 efi_memmap_init(unsigned long *s, unsigned long *e)
931 struct kern_memdesc *k, *prev = NULL;
932 u64 contig_low=0, contig_high=0;
934 void *efi_map_start, *efi_map_end, *p, *q;
935 efi_memory_desc_t *md, *pmd = NULL, *check_md;
937 unsigned long total_mem = 0;
939 k = kern_memmap = find_memmap_space();
941 efi_map_start = __va(ia64_boot_param->efi_memmap);
942 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
943 efi_desc_size = ia64_boot_param->efi_memdesc_size;
945 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
948 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
949 md->type == EFI_BOOT_SERVICES_DATA)) {
950 k->attribute = EFI_MEMORY_UC;
951 k->start = md->phys_addr;
952 k->num_pages = md->num_pages;
957 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
958 contig_low = GRANULEROUNDUP(md->phys_addr);
959 contig_high = efi_md_end(md);
960 for (q = p + efi_desc_size; q < efi_map_end; 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))
974 * Round ends inward to granule boundaries
975 * Give trimmings to uncached allocator
977 if (md->phys_addr < contig_low) {
978 lim = min(efi_md_end(md), contig_low);
980 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
981 kmd_end(k-1) == md->phys_addr) {
982 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
984 k->attribute = EFI_MEMORY_UC;
985 k->start = md->phys_addr;
986 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
994 if (efi_md_end(md) > contig_high) {
995 lim = max(md->phys_addr, contig_high);
997 if (lim == md->phys_addr && k > kern_memmap &&
998 (k-1)->attribute == EFI_MEMORY_UC &&
999 kmd_end(k-1) == md->phys_addr) {
1000 (k-1)->num_pages += md->num_pages;
1002 k->attribute = EFI_MEMORY_UC;
1004 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1010 ae = efi_md_end(md);
1012 /* keep within max_addr= and min_addr= command line arg */
1013 as = max(as, min_addr);
1014 ae = min(ae, max_addr);
1018 /* avoid going over mem= command line arg */
1019 if (total_mem + (ae - as) > mem_limit)
1020 ae -= total_mem + (ae - as) - mem_limit;
1024 if (prev && kmd_end(prev) == md->phys_addr) {
1025 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1026 total_mem += ae - as;
1029 k->attribute = EFI_MEMORY_WB;
1031 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1032 total_mem += ae - as;
1035 k->start = ~0L; /* end-marker */
1037 /* reserve the memory we are using for kern_memmap */
1038 *s = (u64)kern_memmap;
1043 efi_initialize_iomem_resources(struct resource *code_resource,
1044 struct resource *data_resource)
1046 struct resource *res;
1047 void *efi_map_start, *efi_map_end, *p;
1048 efi_memory_desc_t *md;
1051 unsigned long flags;
1053 efi_map_start = __va(ia64_boot_param->efi_memmap);
1054 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1055 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1059 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1062 if (md->num_pages == 0) /* should not happen */
1065 flags = IORESOURCE_MEM;
1068 case EFI_MEMORY_MAPPED_IO:
1069 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1072 case EFI_LOADER_CODE:
1073 case EFI_LOADER_DATA:
1074 case EFI_BOOT_SERVICES_DATA:
1075 case EFI_BOOT_SERVICES_CODE:
1076 case EFI_CONVENTIONAL_MEMORY:
1077 if (md->attribute & EFI_MEMORY_WP) {
1078 name = "System ROM";
1079 flags |= IORESOURCE_READONLY;
1081 name = "System RAM";
1085 case EFI_ACPI_MEMORY_NVS:
1086 name = "ACPI Non-volatile Storage";
1087 flags |= IORESOURCE_BUSY;
1090 case EFI_UNUSABLE_MEMORY:
1092 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1095 case EFI_RESERVED_TYPE:
1096 case EFI_RUNTIME_SERVICES_CODE:
1097 case EFI_RUNTIME_SERVICES_DATA:
1098 case EFI_ACPI_RECLAIM_MEMORY:
1101 flags |= IORESOURCE_BUSY;
1105 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1106 printk(KERN_ERR "failed to alocate resource for iomem\n");
1111 res->start = md->phys_addr;
1112 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1115 if (insert_resource(&iomem_resource, res) < 0)
1119 * We don't know which region contains
1120 * kernel data so we try it repeatedly and
1121 * let the resource manager test it.
1123 insert_resource(res, code_resource);
1124 insert_resource(res, data_resource);
1126 insert_resource(res, &efi_memmap_res);
1127 insert_resource(res, &boot_param_res);
1128 if (crashk_res.end > crashk_res.start)
1129 insert_resource(res, &crashk_res);
1136 /* find a block of memory aligned to 64M exclude reserved regions
1137 rsvd_regions are sorted
1140 kdump_find_rsvd_region (unsigned long size,
1141 struct rsvd_region *r, int n)
1145 u64 alignment = 1UL << _PAGE_SIZE_64M;
1146 void *efi_map_start, *efi_map_end, *p;
1147 efi_memory_desc_t *md;
1150 efi_map_start = __va(ia64_boot_param->efi_memmap);
1151 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1152 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1154 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1158 start = ALIGN(md->phys_addr, alignment);
1159 end = efi_md_end(md);
1160 for (i = 0; i < n; i++) {
1161 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1162 if (__pa(r[i].start) > start + size)
1164 start = ALIGN(__pa(r[i].end), alignment);
1165 if (i < n-1 && __pa(r[i+1].start) < start + size)
1171 if (end > start + size)
1175 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",