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/crash_dump.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/time.h>
32 #include <linux/efi.h>
33 #include <linux/kexec.h>
37 #include <asm/kregs.h>
38 #include <asm/meminit.h>
39 #include <asm/pgtable.h>
40 #include <asm/processor.h>
42 #include <asm/setup.h>
43 #include <asm/tlbflush.h>
47 static __initdata
unsigned long palo_phys
;
49 static __initdata efi_config_table_type_t arch_tables
[] = {
50 {PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID
, "PALO", &palo_phys
},
54 extern efi_status_t
efi_call_phys (void *, ...);
56 static efi_runtime_services_t
*runtime
;
57 static u64 mem_limit
= ~0UL, max_addr
= ~0UL, min_addr
= 0UL;
59 #define efi_call_virt(f, args...) (*(f))(args)
61 #define STUB_GET_TIME(prefix, adjust_arg) \
63 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
65 struct ia64_fpreg fr[6]; \
66 efi_time_cap_t *atc = NULL; \
70 atc = adjust_arg(tc); \
71 ia64_save_scratch_fpregs(fr); \
72 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
73 adjust_arg(tm), atc); \
74 ia64_load_scratch_fpregs(fr); \
78 #define STUB_SET_TIME(prefix, adjust_arg) \
80 prefix##_set_time (efi_time_t *tm) \
82 struct ia64_fpreg fr[6]; \
85 ia64_save_scratch_fpregs(fr); \
86 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
88 ia64_load_scratch_fpregs(fr); \
92 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
94 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
97 struct ia64_fpreg fr[6]; \
100 ia64_save_scratch_fpregs(fr); \
101 ret = efi_call_##prefix( \
102 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
103 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
104 ia64_load_scratch_fpregs(fr); \
108 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
109 static efi_status_t \
110 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
112 struct ia64_fpreg fr[6]; \
113 efi_time_t *atm = NULL; \
117 atm = adjust_arg(tm); \
118 ia64_save_scratch_fpregs(fr); \
119 ret = efi_call_##prefix( \
120 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
122 ia64_load_scratch_fpregs(fr); \
126 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
127 static efi_status_t \
128 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
129 unsigned long *data_size, void *data) \
131 struct ia64_fpreg fr[6]; \
136 aattr = adjust_arg(attr); \
137 ia64_save_scratch_fpregs(fr); \
138 ret = efi_call_##prefix( \
139 (efi_get_variable_t *) __va(runtime->get_variable), \
140 adjust_arg(name), adjust_arg(vendor), aattr, \
141 adjust_arg(data_size), adjust_arg(data)); \
142 ia64_load_scratch_fpregs(fr); \
146 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
147 static efi_status_t \
148 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
149 efi_guid_t *vendor) \
151 struct ia64_fpreg fr[6]; \
154 ia64_save_scratch_fpregs(fr); \
155 ret = efi_call_##prefix( \
156 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
157 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
158 ia64_load_scratch_fpregs(fr); \
162 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
163 static efi_status_t \
164 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
165 u32 attr, unsigned long data_size, \
168 struct ia64_fpreg fr[6]; \
171 ia64_save_scratch_fpregs(fr); \
172 ret = efi_call_##prefix( \
173 (efi_set_variable_t *) __va(runtime->set_variable), \
174 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
176 ia64_load_scratch_fpregs(fr); \
180 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
181 static efi_status_t \
182 prefix##_get_next_high_mono_count (u32 *count) \
184 struct ia64_fpreg fr[6]; \
187 ia64_save_scratch_fpregs(fr); \
188 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
189 __va(runtime->get_next_high_mono_count), \
190 adjust_arg(count)); \
191 ia64_load_scratch_fpregs(fr); \
195 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
197 prefix##_reset_system (int reset_type, efi_status_t status, \
198 unsigned long data_size, efi_char16_t *data) \
200 struct ia64_fpreg fr[6]; \
201 efi_char16_t *adata = NULL; \
204 adata = adjust_arg(data); \
206 ia64_save_scratch_fpregs(fr); \
208 (efi_reset_system_t *) __va(runtime->reset_system), \
209 reset_type, status, data_size, adata); \
210 /* should not return, but just in case... */ \
211 ia64_load_scratch_fpregs(fr); \
214 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
216 STUB_GET_TIME(phys
, phys_ptr
)
217 STUB_SET_TIME(phys
, phys_ptr
)
218 STUB_GET_WAKEUP_TIME(phys
, phys_ptr
)
219 STUB_SET_WAKEUP_TIME(phys
, phys_ptr
)
220 STUB_GET_VARIABLE(phys
, phys_ptr
)
221 STUB_GET_NEXT_VARIABLE(phys
, phys_ptr
)
222 STUB_SET_VARIABLE(phys
, phys_ptr
)
223 STUB_GET_NEXT_HIGH_MONO_COUNT(phys
, phys_ptr
)
224 STUB_RESET_SYSTEM(phys
, phys_ptr
)
228 STUB_GET_TIME(virt
, id
)
229 STUB_SET_TIME(virt
, id
)
230 STUB_GET_WAKEUP_TIME(virt
, id
)
231 STUB_SET_WAKEUP_TIME(virt
, id
)
232 STUB_GET_VARIABLE(virt
, id
)
233 STUB_GET_NEXT_VARIABLE(virt
, id
)
234 STUB_SET_VARIABLE(virt
, id
)
235 STUB_GET_NEXT_HIGH_MONO_COUNT(virt
, id
)
236 STUB_RESET_SYSTEM(virt
, id
)
239 efi_gettimeofday (struct timespec64
*ts
)
243 if ((*efi
.get_time
)(&tm
, NULL
) != EFI_SUCCESS
) {
244 memset(ts
, 0, sizeof(*ts
));
248 ts
->tv_sec
= mktime64(tm
.year
, tm
.month
, tm
.day
,
249 tm
.hour
, tm
.minute
, tm
.second
);
250 ts
->tv_nsec
= tm
.nanosecond
;
254 is_memory_available (efi_memory_desc_t
*md
)
256 if (!(md
->attribute
& EFI_MEMORY_WB
))
260 case EFI_LOADER_CODE
:
261 case EFI_LOADER_DATA
:
262 case EFI_BOOT_SERVICES_CODE
:
263 case EFI_BOOT_SERVICES_DATA
:
264 case EFI_CONVENTIONAL_MEMORY
:
270 typedef struct kern_memdesc
{
276 static kern_memdesc_t
*kern_memmap
;
278 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
281 kmd_end(kern_memdesc_t
*kmd
)
283 return (kmd
->start
+ (kmd
->num_pages
<< EFI_PAGE_SHIFT
));
287 efi_md_end(efi_memory_desc_t
*md
)
289 return (md
->phys_addr
+ efi_md_size(md
));
293 efi_wb(efi_memory_desc_t
*md
)
295 return (md
->attribute
& EFI_MEMORY_WB
);
299 efi_uc(efi_memory_desc_t
*md
)
301 return (md
->attribute
& EFI_MEMORY_UC
);
305 walk (efi_freemem_callback_t callback
, void *arg
, u64 attr
)
308 u64 start
, end
, voff
;
310 voff
= (attr
== EFI_MEMORY_WB
) ? PAGE_OFFSET
: __IA64_UNCACHED_OFFSET
;
311 for (k
= kern_memmap
; k
->start
!= ~0UL; k
++) {
312 if (k
->attribute
!= attr
)
314 start
= PAGE_ALIGN(k
->start
);
315 end
= (k
->start
+ (k
->num_pages
<< EFI_PAGE_SHIFT
)) & PAGE_MASK
;
317 if ((*callback
)(start
+ voff
, end
+ voff
, arg
) < 0)
323 * Walk the EFI memory map and call CALLBACK once for each EFI memory
324 * descriptor that has memory that is available for OS use.
327 efi_memmap_walk (efi_freemem_callback_t callback
, void *arg
)
329 walk(callback
, arg
, EFI_MEMORY_WB
);
333 * Walk the EFI memory map and call CALLBACK once for each EFI memory
334 * descriptor that has memory that is available for uncached allocator.
337 efi_memmap_walk_uc (efi_freemem_callback_t callback
, void *arg
)
339 walk(callback
, arg
, EFI_MEMORY_UC
);
343 * Look for the PAL_CODE region reported by EFI and map it using an
344 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
345 * Abstraction Layer chapter 11 in ADAG
348 efi_get_pal_addr (void)
350 void *efi_map_start
, *efi_map_end
, *p
;
351 efi_memory_desc_t
*md
;
353 int pal_code_count
= 0;
356 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
357 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
358 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
360 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
362 if (md
->type
!= EFI_PAL_CODE
)
365 if (++pal_code_count
> 1) {
366 printk(KERN_ERR
"Too many EFI Pal Code memory ranges, "
367 "dropped @ %llx\n", md
->phys_addr
);
371 * The only ITLB entry in region 7 that is used is the one
372 * installed by __start(). That entry covers a 64MB range.
374 mask
= ~((1 << KERNEL_TR_PAGE_SHIFT
) - 1);
375 vaddr
= PAGE_OFFSET
+ md
->phys_addr
;
378 * We must check that the PAL mapping won't overlap with the
381 * PAL code is guaranteed to be aligned on a power of 2 between
382 * 4k and 256KB and that only one ITR is needed to map it. This
383 * implies that the PAL code is always aligned on its size,
384 * i.e., the closest matching page size supported by the TLB.
385 * Therefore PAL code is guaranteed never to cross a 64MB unless
386 * it is bigger than 64MB (very unlikely!). So for now the
387 * following test is enough to determine whether or not we need
388 * a dedicated ITR for the PAL code.
390 if ((vaddr
& mask
) == (KERNEL_START
& mask
)) {
391 printk(KERN_INFO
"%s: no need to install ITR for PAL code\n",
396 if (efi_md_size(md
) > IA64_GRANULE_SIZE
)
397 panic("Whoa! PAL code size bigger than a granule!");
400 mask
= ~((1 << IA64_GRANULE_SHIFT
) - 1);
402 printk(KERN_INFO
"CPU %d: mapping PAL code "
403 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
404 smp_processor_id(), md
->phys_addr
,
405 md
->phys_addr
+ efi_md_size(md
),
406 vaddr
& mask
, (vaddr
& mask
) + IA64_GRANULE_SIZE
);
408 return __va(md
->phys_addr
);
410 printk(KERN_WARNING
"%s: no PAL-code memory-descriptor found\n",
416 static u8 __init
palo_checksum(u8
*buffer
, u32 length
)
419 u8
*end
= buffer
+ length
;
422 sum
= (u8
) (sum
+ *(buffer
++));
428 * Parse and handle PALO table which is published at:
429 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
431 static void __init
handle_palo(unsigned long phys_addr
)
433 struct palo_table
*palo
= __va(phys_addr
);
436 if (strncmp(palo
->signature
, PALO_SIG
, sizeof(PALO_SIG
) - 1)) {
437 printk(KERN_INFO
"PALO signature incorrect.\n");
441 checksum
= palo_checksum((u8
*)palo
, palo
->length
);
443 printk(KERN_INFO
"PALO checksum incorrect.\n");
447 setup_ptcg_sem(palo
->max_tlb_purges
, NPTCG_FROM_PALO
);
451 efi_map_pal_code (void)
453 void *pal_vaddr
= efi_get_pal_addr ();
460 * Cannot write to CRx with PSR.ic=1
462 psr
= ia64_clear_ic();
463 ia64_itr(0x1, IA64_TR_PALCODE
,
464 GRANULEROUNDDOWN((unsigned long) pal_vaddr
),
465 pte_val(pfn_pte(__pa(pal_vaddr
) >> PAGE_SHIFT
, PAGE_KERNEL
)),
467 ia64_set_psr(psr
); /* restore psr */
473 void *efi_map_start
, *efi_map_end
;
476 char *cp
, vendor
[100] = "unknown";
479 set_bit(EFI_BOOT
, &efi
.flags
);
480 set_bit(EFI_64BIT
, &efi
.flags
);
483 * It's too early to be able to use the standard kernel command line
486 for (cp
= boot_command_line
; *cp
; ) {
487 if (memcmp(cp
, "mem=", 4) == 0) {
488 mem_limit
= memparse(cp
+ 4, &cp
);
489 } else if (memcmp(cp
, "max_addr=", 9) == 0) {
490 max_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
491 } else if (memcmp(cp
, "min_addr=", 9) == 0) {
492 min_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
494 while (*cp
!= ' ' && *cp
)
501 printk(KERN_INFO
"Ignoring memory below %lluMB\n",
503 if (max_addr
!= ~0UL)
504 printk(KERN_INFO
"Ignoring memory above %lluMB\n",
507 efi
.systab
= __va(ia64_boot_param
->efi_systab
);
510 * Verify the EFI Table
512 if (efi
.systab
== NULL
)
513 panic("Whoa! Can't find EFI system table.\n");
514 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
515 panic("Whoa! EFI system table signature incorrect\n");
516 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
517 printk(KERN_WARNING
"Warning: EFI system table version "
518 "%d.%02d, expected 1.00 or greater\n",
519 efi
.systab
->hdr
.revision
>> 16,
520 efi
.systab
->hdr
.revision
& 0xffff);
522 /* Show what we know for posterity */
523 c16
= __va(efi
.systab
->fw_vendor
);
525 for (i
= 0;i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
530 printk(KERN_INFO
"EFI v%u.%.02u by %s:",
531 efi
.systab
->hdr
.revision
>> 16,
532 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
534 palo_phys
= EFI_INVALID_TABLE_ADDR
;
536 if (efi_config_init(arch_tables
) != 0)
539 if (palo_phys
!= EFI_INVALID_TABLE_ADDR
)
540 handle_palo(palo_phys
);
542 runtime
= __va(efi
.systab
->runtime
);
543 efi
.get_time
= phys_get_time
;
544 efi
.set_time
= phys_set_time
;
545 efi
.get_wakeup_time
= phys_get_wakeup_time
;
546 efi
.set_wakeup_time
= phys_set_wakeup_time
;
547 efi
.get_variable
= phys_get_variable
;
548 efi
.get_next_variable
= phys_get_next_variable
;
549 efi
.set_variable
= phys_set_variable
;
550 efi
.get_next_high_mono_count
= phys_get_next_high_mono_count
;
551 efi
.reset_system
= phys_reset_system
;
553 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
554 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
555 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
558 /* print EFI memory map: */
560 efi_memory_desc_t
*md
;
563 for (i
= 0, p
= efi_map_start
; p
< efi_map_end
;
564 ++i
, p
+= efi_desc_size
)
571 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
573 if ((size
>> 40) > 0) {
576 } else if ((size
>> 30) > 0) {
579 } else if ((size
>> 20) > 0) {
587 printk("mem%02d: %s "
588 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
589 i
, efi_md_typeattr_format(buf
, sizeof(buf
), md
),
591 md
->phys_addr
+ efi_md_size(md
), size
, unit
);
597 efi_enter_virtual_mode();
601 efi_enter_virtual_mode (void)
603 void *efi_map_start
, *efi_map_end
, *p
;
604 efi_memory_desc_t
*md
;
608 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
609 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
610 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
612 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
614 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
616 * Some descriptors have multiple bits set, so the
617 * order of the tests is relevant.
619 if (md
->attribute
& EFI_MEMORY_WB
) {
620 md
->virt_addr
= (u64
) __va(md
->phys_addr
);
621 } else if (md
->attribute
& EFI_MEMORY_UC
) {
622 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
623 } else if (md
->attribute
& EFI_MEMORY_WC
) {
625 md
->virt_addr
= ia64_remap(md
->phys_addr
,
633 printk(KERN_INFO
"EFI_MEMORY_WC mapping\n");
634 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
636 } else if (md
->attribute
& EFI_MEMORY_WT
) {
638 md
->virt_addr
= ia64_remap(md
->phys_addr
,
646 printk(KERN_INFO
"EFI_MEMORY_WT mapping\n");
647 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
653 status
= efi_call_phys(__va(runtime
->set_virtual_address_map
),
654 ia64_boot_param
->efi_memmap_size
,
656 ia64_boot_param
->efi_memdesc_version
,
657 ia64_boot_param
->efi_memmap
);
658 if (status
!= EFI_SUCCESS
) {
659 printk(KERN_WARNING
"warning: unable to switch EFI into "
660 "virtual mode (status=%lu)\n", status
);
664 set_bit(EFI_RUNTIME_SERVICES
, &efi
.flags
);
667 * Now that EFI is in virtual mode, we call the EFI functions more
670 efi
.get_time
= virt_get_time
;
671 efi
.set_time
= virt_set_time
;
672 efi
.get_wakeup_time
= virt_get_wakeup_time
;
673 efi
.set_wakeup_time
= virt_set_wakeup_time
;
674 efi
.get_variable
= virt_get_variable
;
675 efi
.get_next_variable
= virt_get_next_variable
;
676 efi
.set_variable
= virt_set_variable
;
677 efi
.get_next_high_mono_count
= virt_get_next_high_mono_count
;
678 efi
.reset_system
= virt_reset_system
;
682 * Walk the EFI memory map looking for the I/O port range. There can only be
683 * one entry of this type, other I/O port ranges should be described via ACPI.
686 efi_get_iobase (void)
688 void *efi_map_start
, *efi_map_end
, *p
;
689 efi_memory_desc_t
*md
;
692 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
693 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
694 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
696 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
698 if (md
->type
== EFI_MEMORY_MAPPED_IO_PORT_SPACE
) {
699 if (md
->attribute
& EFI_MEMORY_UC
)
700 return md
->phys_addr
;
706 static struct kern_memdesc
*
707 kern_memory_descriptor (unsigned long phys_addr
)
709 struct kern_memdesc
*md
;
711 for (md
= kern_memmap
; md
->start
!= ~0UL; md
++) {
712 if (phys_addr
- md
->start
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
718 static efi_memory_desc_t
*
719 efi_memory_descriptor (unsigned long phys_addr
)
721 void *efi_map_start
, *efi_map_end
, *p
;
722 efi_memory_desc_t
*md
;
725 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
726 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
727 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
729 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
732 if (phys_addr
- md
->phys_addr
< efi_md_size(md
))
739 efi_memmap_intersects (unsigned long phys_addr
, unsigned long size
)
741 void *efi_map_start
, *efi_map_end
, *p
;
742 efi_memory_desc_t
*md
;
746 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
747 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
748 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
750 end
= phys_addr
+ size
;
752 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
754 if (md
->phys_addr
< end
&& efi_md_end(md
) > phys_addr
)
761 efi_mem_type (unsigned long phys_addr
)
763 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
771 efi_mem_attributes (unsigned long phys_addr
)
773 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
776 return md
->attribute
;
779 EXPORT_SYMBOL(efi_mem_attributes
);
782 efi_mem_attribute (unsigned long phys_addr
, unsigned long size
)
784 unsigned long end
= phys_addr
+ size
;
785 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
792 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
793 * the kernel that firmware needs this region mapped.
795 attr
= md
->attribute
& ~EFI_MEMORY_RUNTIME
;
797 unsigned long md_end
= efi_md_end(md
);
802 md
= efi_memory_descriptor(md_end
);
803 if (!md
|| (md
->attribute
& ~EFI_MEMORY_RUNTIME
) != attr
)
806 return 0; /* never reached */
810 kern_mem_attribute (unsigned long phys_addr
, unsigned long size
)
812 unsigned long end
= phys_addr
+ size
;
813 struct kern_memdesc
*md
;
817 * This is a hack for ioremap calls before we set up kern_memmap.
818 * Maybe we should do efi_memmap_init() earlier instead.
821 attr
= efi_mem_attribute(phys_addr
, size
);
822 if (attr
& EFI_MEMORY_WB
)
823 return EFI_MEMORY_WB
;
827 md
= kern_memory_descriptor(phys_addr
);
831 attr
= md
->attribute
;
833 unsigned long md_end
= kmd_end(md
);
838 md
= kern_memory_descriptor(md_end
);
839 if (!md
|| md
->attribute
!= attr
)
842 return 0; /* never reached */
844 EXPORT_SYMBOL(kern_mem_attribute
);
847 valid_phys_addr_range (phys_addr_t phys_addr
, unsigned long size
)
852 * /dev/mem reads and writes use copy_to_user(), which implicitly
853 * uses a granule-sized kernel identity mapping. It's really
854 * only safe to do this for regions in kern_memmap. For more
855 * details, see Documentation/ia64/aliasing.txt.
857 attr
= kern_mem_attribute(phys_addr
, size
);
858 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
864 valid_mmap_phys_addr_range (unsigned long pfn
, unsigned long size
)
866 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
869 attr
= efi_mem_attribute(phys_addr
, size
);
872 * /dev/mem mmap uses normal user pages, so we don't need the entire
873 * granule, but the entire region we're mapping must support the same
876 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
880 * Intel firmware doesn't tell us about all the MMIO regions, so
881 * in general we have to allow mmap requests. But if EFI *does*
882 * tell us about anything inside this region, we should deny it.
883 * The user can always map a smaller region to avoid the overlap.
885 if (efi_memmap_intersects(phys_addr
, size
))
892 phys_mem_access_prot(struct file
*file
, unsigned long pfn
, unsigned long size
,
895 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
899 * For /dev/mem mmap, we use user mappings, but if the region is
900 * in kern_memmap (and hence may be covered by a kernel mapping),
901 * we must use the same attribute as the kernel mapping.
903 attr
= kern_mem_attribute(phys_addr
, size
);
904 if (attr
& EFI_MEMORY_WB
)
905 return pgprot_cacheable(vma_prot
);
906 else if (attr
& EFI_MEMORY_UC
)
907 return pgprot_noncached(vma_prot
);
910 * Some chipsets don't support UC access to memory. If
911 * WB is supported, we prefer that.
913 if (efi_mem_attribute(phys_addr
, size
) & EFI_MEMORY_WB
)
914 return pgprot_cacheable(vma_prot
);
916 return pgprot_noncached(vma_prot
);
920 efi_uart_console_only(void)
923 char *s
, name
[] = "ConOut";
924 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
925 efi_char16_t
*utf16
, name_utf16
[32];
926 unsigned char data
[1024];
927 unsigned long size
= sizeof(data
);
928 struct efi_generic_dev_path
*hdr
, *end_addr
;
931 /* Convert to UTF-16 */
935 *utf16
++ = *s
++ & 0x7f;
938 status
= efi
.get_variable(name_utf16
, &guid
, NULL
, &size
, data
);
939 if (status
!= EFI_SUCCESS
) {
940 printk(KERN_ERR
"No EFI %s variable?\n", name
);
944 hdr
= (struct efi_generic_dev_path
*) data
;
945 end_addr
= (struct efi_generic_dev_path
*) ((u8
*) data
+ size
);
946 while (hdr
< end_addr
) {
947 if (hdr
->type
== EFI_DEV_MSG
&&
948 hdr
->sub_type
== EFI_DEV_MSG_UART
)
950 else if (hdr
->type
== EFI_DEV_END_PATH
||
951 hdr
->type
== EFI_DEV_END_PATH2
) {
954 if (hdr
->sub_type
== EFI_DEV_END_ENTIRE
)
958 hdr
= (struct efi_generic_dev_path
*)((u8
*) hdr
+ hdr
->length
);
960 printk(KERN_ERR
"Malformed %s value\n", name
);
965 * Look for the first granule aligned memory descriptor memory
966 * that is big enough to hold EFI memory map. Make sure this
967 * descriptor is at least granule sized so it does not get trimmed
969 struct kern_memdesc
*
970 find_memmap_space (void)
972 u64 contig_low
=0, contig_high
=0;
974 void *efi_map_start
, *efi_map_end
, *p
, *q
;
975 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
976 u64 space_needed
, efi_desc_size
;
977 unsigned long total_mem
= 0;
979 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
980 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
981 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
984 * Worst case: we need 3 kernel descriptors for each efi descriptor
985 * (if every entry has a WB part in the middle, and UC head and tail),
986 * plus one for the end marker.
988 space_needed
= sizeof(kern_memdesc_t
) *
989 (3 * (ia64_boot_param
->efi_memmap_size
/efi_desc_size
) + 1);
991 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
996 if (pmd
== NULL
|| !efi_wb(pmd
) ||
997 efi_md_end(pmd
) != md
->phys_addr
) {
998 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
999 contig_high
= efi_md_end(md
);
1000 for (q
= p
+ efi_desc_size
; q
< efi_map_end
;
1001 q
+= efi_desc_size
) {
1003 if (!efi_wb(check_md
))
1005 if (contig_high
!= check_md
->phys_addr
)
1007 contig_high
= efi_md_end(check_md
);
1009 contig_high
= GRANULEROUNDDOWN(contig_high
);
1011 if (!is_memory_available(md
) || md
->type
== EFI_LOADER_DATA
)
1014 /* Round ends inward to granule boundaries */
1015 as
= max(contig_low
, md
->phys_addr
);
1016 ae
= min(contig_high
, efi_md_end(md
));
1018 /* keep within max_addr= and min_addr= command line arg */
1019 as
= max(as
, min_addr
);
1020 ae
= min(ae
, max_addr
);
1024 /* avoid going over mem= command line arg */
1025 if (total_mem
+ (ae
- as
) > mem_limit
)
1026 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1031 if (ae
- as
> space_needed
)
1034 if (p
>= efi_map_end
)
1035 panic("Can't allocate space for kernel memory descriptors");
1041 * Walk the EFI memory map and gather all memory available for kernel
1042 * to use. We can allocate partial granules only if the unavailable
1043 * parts exist, and are WB.
1046 efi_memmap_init(u64
*s
, u64
*e
)
1048 struct kern_memdesc
*k
, *prev
= NULL
;
1049 u64 contig_low
=0, contig_high
=0;
1051 void *efi_map_start
, *efi_map_end
, *p
, *q
;
1052 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
1054 unsigned long total_mem
= 0;
1056 k
= kern_memmap
= find_memmap_space();
1058 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1059 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1060 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1062 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
1066 (md
->type
== EFI_CONVENTIONAL_MEMORY
||
1067 md
->type
== EFI_BOOT_SERVICES_DATA
)) {
1068 k
->attribute
= EFI_MEMORY_UC
;
1069 k
->start
= md
->phys_addr
;
1070 k
->num_pages
= md
->num_pages
;
1075 if (pmd
== NULL
|| !efi_wb(pmd
) ||
1076 efi_md_end(pmd
) != md
->phys_addr
) {
1077 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
1078 contig_high
= efi_md_end(md
);
1079 for (q
= p
+ efi_desc_size
; q
< efi_map_end
;
1080 q
+= efi_desc_size
) {
1082 if (!efi_wb(check_md
))
1084 if (contig_high
!= check_md
->phys_addr
)
1086 contig_high
= efi_md_end(check_md
);
1088 contig_high
= GRANULEROUNDDOWN(contig_high
);
1090 if (!is_memory_available(md
))
1094 * Round ends inward to granule boundaries
1095 * Give trimmings to uncached allocator
1097 if (md
->phys_addr
< contig_low
) {
1098 lim
= min(efi_md_end(md
), contig_low
);
1100 if (k
> kern_memmap
&&
1101 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1102 kmd_end(k
-1) == md
->phys_addr
) {
1104 (lim
- md
->phys_addr
)
1107 k
->attribute
= EFI_MEMORY_UC
;
1108 k
->start
= md
->phys_addr
;
1109 k
->num_pages
= (lim
- md
->phys_addr
)
1118 if (efi_md_end(md
) > contig_high
) {
1119 lim
= max(md
->phys_addr
, contig_high
);
1121 if (lim
== md
->phys_addr
&& k
> kern_memmap
&&
1122 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1123 kmd_end(k
-1) == md
->phys_addr
) {
1124 (k
-1)->num_pages
+= md
->num_pages
;
1126 k
->attribute
= EFI_MEMORY_UC
;
1128 k
->num_pages
= (efi_md_end(md
) - lim
)
1135 ae
= efi_md_end(md
);
1137 /* keep within max_addr= and min_addr= command line arg */
1138 as
= max(as
, min_addr
);
1139 ae
= min(ae
, max_addr
);
1143 /* avoid going over mem= command line arg */
1144 if (total_mem
+ (ae
- as
) > mem_limit
)
1145 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1149 if (prev
&& kmd_end(prev
) == md
->phys_addr
) {
1150 prev
->num_pages
+= (ae
- as
) >> EFI_PAGE_SHIFT
;
1151 total_mem
+= ae
- as
;
1154 k
->attribute
= EFI_MEMORY_WB
;
1156 k
->num_pages
= (ae
- as
) >> EFI_PAGE_SHIFT
;
1157 total_mem
+= ae
- as
;
1160 k
->start
= ~0L; /* end-marker */
1162 /* reserve the memory we are using for kern_memmap */
1163 *s
= (u64
)kern_memmap
;
1170 efi_initialize_iomem_resources(struct resource
*code_resource
,
1171 struct resource
*data_resource
,
1172 struct resource
*bss_resource
)
1174 struct resource
*res
;
1175 void *efi_map_start
, *efi_map_end
, *p
;
1176 efi_memory_desc_t
*md
;
1179 unsigned long flags
, desc
;
1181 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1182 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1183 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1187 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1190 if (md
->num_pages
== 0) /* should not happen */
1193 flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
1194 desc
= IORES_DESC_NONE
;
1198 case EFI_MEMORY_MAPPED_IO
:
1199 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1202 case EFI_LOADER_CODE
:
1203 case EFI_LOADER_DATA
:
1204 case EFI_BOOT_SERVICES_DATA
:
1205 case EFI_BOOT_SERVICES_CODE
:
1206 case EFI_CONVENTIONAL_MEMORY
:
1207 if (md
->attribute
& EFI_MEMORY_WP
) {
1208 name
= "System ROM";
1209 flags
|= IORESOURCE_READONLY
;
1210 } else if (md
->attribute
== EFI_MEMORY_UC
) {
1211 name
= "Uncached RAM";
1213 name
= "System RAM";
1214 flags
|= IORESOURCE_SYSRAM
;
1218 case EFI_ACPI_MEMORY_NVS
:
1219 name
= "ACPI Non-volatile Storage";
1220 desc
= IORES_DESC_ACPI_NV_STORAGE
;
1223 case EFI_UNUSABLE_MEMORY
:
1225 flags
|= IORESOURCE_DISABLED
;
1228 case EFI_PERSISTENT_MEMORY
:
1229 name
= "Persistent Memory";
1230 desc
= IORES_DESC_PERSISTENT_MEMORY
;
1233 case EFI_RESERVED_TYPE
:
1234 case EFI_RUNTIME_SERVICES_CODE
:
1235 case EFI_RUNTIME_SERVICES_DATA
:
1236 case EFI_ACPI_RECLAIM_MEMORY
:
1242 if ((res
= kzalloc(sizeof(struct resource
),
1243 GFP_KERNEL
)) == NULL
) {
1245 "failed to allocate resource for iomem\n");
1250 res
->start
= md
->phys_addr
;
1251 res
->end
= md
->phys_addr
+ efi_md_size(md
) - 1;
1255 if (insert_resource(&iomem_resource
, res
) < 0)
1259 * We don't know which region contains
1260 * kernel data so we try it repeatedly and
1261 * let the resource manager test it.
1263 insert_resource(res
, code_resource
);
1264 insert_resource(res
, data_resource
);
1265 insert_resource(res
, bss_resource
);
1267 insert_resource(res
, &efi_memmap_res
);
1268 insert_resource(res
, &boot_param_res
);
1269 if (crashk_res
.end
> crashk_res
.start
)
1270 insert_resource(res
, &crashk_res
);
1277 /* find a block of memory aligned to 64M exclude reserved regions
1278 rsvd_regions are sorted
1280 unsigned long __init
1281 kdump_find_rsvd_region (unsigned long size
, struct rsvd_region
*r
, int n
)
1285 u64 alignment
= 1UL << _PAGE_SIZE_64M
;
1286 void *efi_map_start
, *efi_map_end
, *p
;
1287 efi_memory_desc_t
*md
;
1290 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1291 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1292 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1294 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1298 start
= ALIGN(md
->phys_addr
, alignment
);
1299 end
= efi_md_end(md
);
1300 for (i
= 0; i
< n
; i
++) {
1301 if (__pa(r
[i
].start
) >= start
&& __pa(r
[i
].end
) < end
) {
1302 if (__pa(r
[i
].start
) > start
+ size
)
1304 start
= ALIGN(__pa(r
[i
].end
), alignment
);
1306 __pa(r
[i
+1].start
) < start
+ size
)
1312 if (end
> start
+ size
)
1317 "Cannot reserve 0x%lx byte of memory for crashdump\n", size
);
1322 #ifdef CONFIG_CRASH_DUMP
1323 /* locate the size find a the descriptor at a certain address */
1324 unsigned long __init
1325 vmcore_find_descriptor_size (unsigned long address
)
1327 void *efi_map_start
, *efi_map_end
, *p
;
1328 efi_memory_desc_t
*md
;
1330 unsigned long ret
= 0;
1332 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1333 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1334 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1336 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1338 if (efi_wb(md
) && md
->type
== EFI_LOADER_DATA
1339 && md
->phys_addr
== address
) {
1340 ret
= efi_md_size(md
);
1346 printk(KERN_WARNING
"Cannot locate EFI vmcore descriptor\n");