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/config.h>
24 #include <linux/module.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>
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;
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_available_memory (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",
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";
416 extern char saved_command_line[];
419 /* it's too early to be able to use the standard kernel command line support... */
420 for (cp = saved_command_line; *cp; ) {
421 if (memcmp(cp, "mem=", 4) == 0) {
422 mem_limit = memparse(cp + 4, &cp);
423 } else if (memcmp(cp, "max_addr=", 9) == 0) {
424 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
426 while (*cp != ' ' && *cp)
432 if (max_addr != ~0UL)
433 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
435 efi.systab = __va(ia64_boot_param->efi_systab);
438 * Verify the EFI Table
440 if (efi.systab == NULL)
441 panic("Woah! Can't find EFI system table.\n");
442 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
443 panic("Woah! EFI system table signature incorrect\n");
444 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
445 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
446 "got %d.%02d, expected %d.%02d\n",
447 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
448 EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
450 config_tables = __va(efi.systab->tables);
452 /* Show what we know for posterity */
453 c16 = __va(efi.systab->fw_vendor);
455 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
460 printk(KERN_INFO "EFI v%u.%.02u by %s:",
461 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
463 efi.mps = EFI_INVALID_TABLE_ADDR;
464 efi.acpi = EFI_INVALID_TABLE_ADDR;
465 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
466 efi.smbios = EFI_INVALID_TABLE_ADDR;
467 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
468 efi.boot_info = EFI_INVALID_TABLE_ADDR;
469 efi.hcdp = EFI_INVALID_TABLE_ADDR;
470 efi.uga = EFI_INVALID_TABLE_ADDR;
472 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
473 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
474 efi.mps = config_tables[i].table;
475 printk(" MPS=0x%lx", config_tables[i].table);
476 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
477 efi.acpi20 = config_tables[i].table;
478 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
479 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
480 efi.acpi = config_tables[i].table;
481 printk(" ACPI=0x%lx", config_tables[i].table);
482 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
483 efi.smbios = config_tables[i].table;
484 printk(" SMBIOS=0x%lx", config_tables[i].table);
485 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
486 efi.sal_systab = config_tables[i].table;
487 printk(" SALsystab=0x%lx", config_tables[i].table);
488 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
489 efi.hcdp = config_tables[i].table;
490 printk(" HCDP=0x%lx", config_tables[i].table);
495 runtime = __va(efi.systab->runtime);
496 efi.get_time = phys_get_time;
497 efi.set_time = phys_set_time;
498 efi.get_wakeup_time = phys_get_wakeup_time;
499 efi.set_wakeup_time = phys_set_wakeup_time;
500 efi.get_variable = phys_get_variable;
501 efi.get_next_variable = phys_get_next_variable;
502 efi.set_variable = phys_set_variable;
503 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
504 efi.reset_system = phys_reset_system;
506 efi_map_start = __va(ia64_boot_param->efi_memmap);
507 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
508 efi_desc_size = ia64_boot_param->efi_memdesc_size;
511 /* print EFI memory map: */
513 efi_memory_desc_t *md;
516 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
518 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
519 i, md->type, md->attribute, md->phys_addr,
520 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
521 md->num_pages >> (20 - EFI_PAGE_SHIFT));
527 efi_enter_virtual_mode();
531 efi_enter_virtual_mode (void)
533 void *efi_map_start, *efi_map_end, *p;
534 efi_memory_desc_t *md;
538 efi_map_start = __va(ia64_boot_param->efi_memmap);
539 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
540 efi_desc_size = ia64_boot_param->efi_memdesc_size;
542 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
544 if (md->attribute & EFI_MEMORY_RUNTIME) {
546 * Some descriptors have multiple bits set, so the order of
547 * the tests is relevant.
549 if (md->attribute & EFI_MEMORY_WB) {
550 md->virt_addr = (u64) __va(md->phys_addr);
551 } else if (md->attribute & EFI_MEMORY_UC) {
552 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
553 } else if (md->attribute & EFI_MEMORY_WC) {
555 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
561 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
562 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
564 } else if (md->attribute & EFI_MEMORY_WT) {
566 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
567 | _PAGE_D | _PAGE_MA_WT
571 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
572 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
578 status = efi_call_phys(__va(runtime->set_virtual_address_map),
579 ia64_boot_param->efi_memmap_size,
580 efi_desc_size, ia64_boot_param->efi_memdesc_version,
581 ia64_boot_param->efi_memmap);
582 if (status != EFI_SUCCESS) {
583 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
584 "(status=%lu)\n", status);
589 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
591 efi.get_time = virt_get_time;
592 efi.set_time = virt_set_time;
593 efi.get_wakeup_time = virt_get_wakeup_time;
594 efi.set_wakeup_time = virt_set_wakeup_time;
595 efi.get_variable = virt_get_variable;
596 efi.get_next_variable = virt_get_next_variable;
597 efi.set_variable = virt_set_variable;
598 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
599 efi.reset_system = virt_reset_system;
603 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
604 * this type, other I/O port ranges should be described via ACPI.
607 efi_get_iobase (void)
609 void *efi_map_start, *efi_map_end, *p;
610 efi_memory_desc_t *md;
613 efi_map_start = __va(ia64_boot_param->efi_memmap);
614 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
615 efi_desc_size = ia64_boot_param->efi_memdesc_size;
617 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
619 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
620 if (md->attribute & EFI_MEMORY_UC)
621 return md->phys_addr;
627 static struct kern_memdesc *
628 kern_memory_descriptor (unsigned long phys_addr)
630 struct kern_memdesc *md;
632 for (md = kern_memmap; md->start != ~0UL; md++) {
633 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
639 static efi_memory_desc_t *
640 efi_memory_descriptor (unsigned long phys_addr)
642 void *efi_map_start, *efi_map_end, *p;
643 efi_memory_desc_t *md;
646 efi_map_start = __va(ia64_boot_param->efi_memmap);
647 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
648 efi_desc_size = ia64_boot_param->efi_memdesc_size;
650 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
653 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
660 efi_mem_type (unsigned long phys_addr)
662 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
670 efi_mem_attributes (unsigned long phys_addr)
672 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
675 return md->attribute;
678 EXPORT_SYMBOL(efi_mem_attributes);
681 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
683 unsigned long end = phys_addr + size;
684 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
691 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
692 * the kernel that firmware needs this region mapped.
694 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
696 unsigned long md_end = efi_md_end(md);
701 md = efi_memory_descriptor(md_end);
702 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
709 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
711 unsigned long end = phys_addr + size;
712 struct kern_memdesc *md;
716 * This is a hack for ioremap calls before we set up kern_memmap.
717 * Maybe we should do efi_memmap_init() earlier instead.
720 attr = efi_mem_attribute(phys_addr, size);
721 if (attr & EFI_MEMORY_WB)
722 return EFI_MEMORY_WB;
726 md = kern_memory_descriptor(phys_addr);
730 attr = md->attribute;
732 unsigned long md_end = kmd_end(md);
737 md = kern_memory_descriptor(md_end);
738 if (!md || md->attribute != attr)
743 EXPORT_SYMBOL(kern_mem_attribute);
746 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
751 * /dev/mem reads and writes use copy_to_user(), which implicitly
752 * uses a granule-sized kernel identity mapping. It's really
753 * only safe to do this for regions in kern_memmap. For more
754 * details, see Documentation/ia64/aliasing.txt.
756 attr = kern_mem_attribute(phys_addr, size);
757 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
763 valid_mmap_phys_addr_range (unsigned long phys_addr, unsigned long size)
766 * MMIO regions are often missing from the EFI memory map.
767 * We must allow mmap of them for programs like X, so we
768 * currently can't do any useful validation.
774 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
777 unsigned long phys_addr = pfn << PAGE_SHIFT;
781 * For /dev/mem mmap, we use user mappings, but if the region is
782 * in kern_memmap (and hence may be covered by a kernel mapping),
783 * we must use the same attribute as the kernel mapping.
785 attr = kern_mem_attribute(phys_addr, size);
786 if (attr & EFI_MEMORY_WB)
787 return pgprot_cacheable(vma_prot);
788 else if (attr & EFI_MEMORY_UC)
789 return pgprot_noncached(vma_prot);
792 * Some chipsets don't support UC access to memory. If
793 * WB is supported, we prefer that.
795 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
796 return pgprot_cacheable(vma_prot);
798 return pgprot_noncached(vma_prot);
802 efi_uart_console_only(void)
805 char *s, name[] = "ConOut";
806 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
807 efi_char16_t *utf16, name_utf16[32];
808 unsigned char data[1024];
809 unsigned long size = sizeof(data);
810 struct efi_generic_dev_path *hdr, *end_addr;
813 /* Convert to UTF-16 */
817 *utf16++ = *s++ & 0x7f;
820 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
821 if (status != EFI_SUCCESS) {
822 printk(KERN_ERR "No EFI %s variable?\n", name);
826 hdr = (struct efi_generic_dev_path *) data;
827 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
828 while (hdr < end_addr) {
829 if (hdr->type == EFI_DEV_MSG &&
830 hdr->sub_type == EFI_DEV_MSG_UART)
832 else if (hdr->type == EFI_DEV_END_PATH ||
833 hdr->type == EFI_DEV_END_PATH2) {
836 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
840 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
842 printk(KERN_ERR "Malformed %s value\n", name);
847 * Look for the first granule aligned memory descriptor memory
848 * that is big enough to hold EFI memory map. Make sure this
849 * descriptor is atleast granule sized so it does not get trimmed
851 struct kern_memdesc *
852 find_memmap_space (void)
854 u64 contig_low=0, contig_high=0;
856 void *efi_map_start, *efi_map_end, *p, *q;
857 efi_memory_desc_t *md, *pmd = NULL, *check_md;
858 u64 space_needed, efi_desc_size;
859 unsigned long total_mem = 0;
861 efi_map_start = __va(ia64_boot_param->efi_memmap);
862 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
863 efi_desc_size = ia64_boot_param->efi_memdesc_size;
866 * Worst case: we need 3 kernel descriptors for each efi descriptor
867 * (if every entry has a WB part in the middle, and UC head and tail),
868 * plus one for the end marker.
870 space_needed = sizeof(kern_memdesc_t) *
871 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
873 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
878 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
879 contig_low = GRANULEROUNDUP(md->phys_addr);
880 contig_high = efi_md_end(md);
881 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
883 if (!efi_wb(check_md))
885 if (contig_high != check_md->phys_addr)
887 contig_high = efi_md_end(check_md);
889 contig_high = GRANULEROUNDDOWN(contig_high);
891 if (!is_available_memory(md) || md->type == EFI_LOADER_DATA)
894 /* Round ends inward to granule boundaries */
895 as = max(contig_low, md->phys_addr);
896 ae = min(contig_high, efi_md_end(md));
898 /* keep within max_addr= command line arg */
899 ae = min(ae, max_addr);
903 /* avoid going over mem= command line arg */
904 if (total_mem + (ae - as) > mem_limit)
905 ae -= total_mem + (ae - as) - mem_limit;
910 if (ae - as > space_needed)
913 if (p >= efi_map_end)
914 panic("Can't allocate space for kernel memory descriptors");
920 * Walk the EFI memory map and gather all memory available for kernel
921 * to use. We can allocate partial granules only if the unavailable
922 * parts exist, and are WB.
925 efi_memmap_init(unsigned long *s, unsigned long *e)
927 struct kern_memdesc *k, *prev = 0;
928 u64 contig_low=0, contig_high=0;
930 void *efi_map_start, *efi_map_end, *p, *q;
931 efi_memory_desc_t *md, *pmd = NULL, *check_md;
933 unsigned long total_mem = 0;
935 k = kern_memmap = find_memmap_space();
937 efi_map_start = __va(ia64_boot_param->efi_memmap);
938 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
939 efi_desc_size = ia64_boot_param->efi_memdesc_size;
941 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
944 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
945 md->type == EFI_BOOT_SERVICES_DATA)) {
946 k->attribute = EFI_MEMORY_UC;
947 k->start = md->phys_addr;
948 k->num_pages = md->num_pages;
953 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
954 contig_low = GRANULEROUNDUP(md->phys_addr);
955 contig_high = efi_md_end(md);
956 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
958 if (!efi_wb(check_md))
960 if (contig_high != check_md->phys_addr)
962 contig_high = efi_md_end(check_md);
964 contig_high = GRANULEROUNDDOWN(contig_high);
966 if (!is_available_memory(md))
970 * Round ends inward to granule boundaries
971 * Give trimmings to uncached allocator
973 if (md->phys_addr < contig_low) {
974 lim = min(efi_md_end(md), contig_low);
976 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
977 kmd_end(k-1) == md->phys_addr) {
978 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
980 k->attribute = EFI_MEMORY_UC;
981 k->start = md->phys_addr;
982 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
990 if (efi_md_end(md) > contig_high) {
991 lim = max(md->phys_addr, contig_high);
993 if (lim == md->phys_addr && k > kern_memmap &&
994 (k-1)->attribute == EFI_MEMORY_UC &&
995 kmd_end(k-1) == md->phys_addr) {
996 (k-1)->num_pages += md->num_pages;
998 k->attribute = EFI_MEMORY_UC;
1000 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1006 ae = efi_md_end(md);
1008 /* keep within max_addr= command line arg */
1009 ae = min(ae, max_addr);
1013 /* avoid going over mem= command line arg */
1014 if (total_mem + (ae - as) > mem_limit)
1015 ae -= total_mem + (ae - as) - mem_limit;
1019 if (prev && kmd_end(prev) == md->phys_addr) {
1020 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1021 total_mem += ae - as;
1024 k->attribute = EFI_MEMORY_WB;
1026 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1027 total_mem += ae - as;
1030 k->start = ~0L; /* end-marker */
1032 /* reserve the memory we are using for kern_memmap */
1033 *s = (u64)kern_memmap;
1038 efi_initialize_iomem_resources(struct resource *code_resource,
1039 struct resource *data_resource)
1041 struct resource *res;
1042 void *efi_map_start, *efi_map_end, *p;
1043 efi_memory_desc_t *md;
1046 unsigned long flags;
1048 efi_map_start = __va(ia64_boot_param->efi_memmap);
1049 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1050 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1054 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1057 if (md->num_pages == 0) /* should not happen */
1060 flags = IORESOURCE_MEM;
1063 case EFI_MEMORY_MAPPED_IO:
1064 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1067 case EFI_LOADER_CODE:
1068 case EFI_LOADER_DATA:
1069 case EFI_BOOT_SERVICES_DATA:
1070 case EFI_BOOT_SERVICES_CODE:
1071 case EFI_CONVENTIONAL_MEMORY:
1072 if (md->attribute & EFI_MEMORY_WP) {
1073 name = "System ROM";
1074 flags |= IORESOURCE_READONLY;
1076 name = "System RAM";
1080 case EFI_ACPI_MEMORY_NVS:
1081 name = "ACPI Non-volatile Storage";
1082 flags |= IORESOURCE_BUSY;
1085 case EFI_UNUSABLE_MEMORY:
1087 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1090 case EFI_RESERVED_TYPE:
1091 case EFI_RUNTIME_SERVICES_CODE:
1092 case EFI_RUNTIME_SERVICES_DATA:
1093 case EFI_ACPI_RECLAIM_MEMORY:
1096 flags |= IORESOURCE_BUSY;
1100 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1101 printk(KERN_ERR "failed to alocate resource for iomem\n");
1106 res->start = md->phys_addr;
1107 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1110 if (insert_resource(&iomem_resource, res) < 0)
1114 * We don't know which region contains
1115 * kernel data so we try it repeatedly and
1116 * let the resource manager test it.
1118 insert_resource(res, code_resource);
1119 insert_resource(res, data_resource);