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/slab.h>
30 #include <linux/time.h>
31 #include <linux/efi.h>
32 #include <linux/kexec.h>
36 #include <asm/kregs.h>
37 #include <asm/meminit.h>
38 #include <asm/pgtable.h>
39 #include <asm/processor.h>
41 #include <asm/tlbflush.h>
45 extern efi_status_t
efi_call_phys (void *, ...);
49 static efi_runtime_services_t
*runtime
;
50 static u64 mem_limit
= ~0UL, max_addr
= ~0UL, min_addr
= 0UL;
52 #define efi_call_virt(f, args...) (*(f))(args)
54 #define STUB_GET_TIME(prefix, adjust_arg) \
56 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
58 struct ia64_fpreg fr[6]; \
59 efi_time_cap_t *atc = NULL; \
63 atc = adjust_arg(tc); \
64 ia64_save_scratch_fpregs(fr); \
65 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
66 adjust_arg(tm), atc); \
67 ia64_load_scratch_fpregs(fr); \
71 #define STUB_SET_TIME(prefix, adjust_arg) \
73 prefix##_set_time (efi_time_t *tm) \
75 struct ia64_fpreg fr[6]; \
78 ia64_save_scratch_fpregs(fr); \
79 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
81 ia64_load_scratch_fpregs(fr); \
85 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
87 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
90 struct ia64_fpreg fr[6]; \
93 ia64_save_scratch_fpregs(fr); \
94 ret = efi_call_##prefix( \
95 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
96 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
97 ia64_load_scratch_fpregs(fr); \
101 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
102 static efi_status_t \
103 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
105 struct ia64_fpreg fr[6]; \
106 efi_time_t *atm = NULL; \
110 atm = adjust_arg(tm); \
111 ia64_save_scratch_fpregs(fr); \
112 ret = efi_call_##prefix( \
113 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
115 ia64_load_scratch_fpregs(fr); \
119 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
120 static efi_status_t \
121 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
122 unsigned long *data_size, void *data) \
124 struct ia64_fpreg fr[6]; \
129 aattr = adjust_arg(attr); \
130 ia64_save_scratch_fpregs(fr); \
131 ret = efi_call_##prefix( \
132 (efi_get_variable_t *) __va(runtime->get_variable), \
133 adjust_arg(name), adjust_arg(vendor), aattr, \
134 adjust_arg(data_size), adjust_arg(data)); \
135 ia64_load_scratch_fpregs(fr); \
139 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
140 static efi_status_t \
141 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
142 efi_guid_t *vendor) \
144 struct ia64_fpreg fr[6]; \
147 ia64_save_scratch_fpregs(fr); \
148 ret = efi_call_##prefix( \
149 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
150 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
151 ia64_load_scratch_fpregs(fr); \
155 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
156 static efi_status_t \
157 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
158 unsigned long attr, unsigned long data_size, \
161 struct ia64_fpreg fr[6]; \
164 ia64_save_scratch_fpregs(fr); \
165 ret = efi_call_##prefix( \
166 (efi_set_variable_t *) __va(runtime->set_variable), \
167 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
169 ia64_load_scratch_fpregs(fr); \
173 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
174 static efi_status_t \
175 prefix##_get_next_high_mono_count (u32 *count) \
177 struct ia64_fpreg fr[6]; \
180 ia64_save_scratch_fpregs(fr); \
181 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
182 __va(runtime->get_next_high_mono_count), \
183 adjust_arg(count)); \
184 ia64_load_scratch_fpregs(fr); \
188 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
190 prefix##_reset_system (int reset_type, efi_status_t status, \
191 unsigned long data_size, efi_char16_t *data) \
193 struct ia64_fpreg fr[6]; \
194 efi_char16_t *adata = NULL; \
197 adata = adjust_arg(data); \
199 ia64_save_scratch_fpregs(fr); \
201 (efi_reset_system_t *) __va(runtime->reset_system), \
202 reset_type, status, data_size, adata); \
203 /* should not return, but just in case... */ \
204 ia64_load_scratch_fpregs(fr); \
207 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
209 STUB_GET_TIME(phys
, phys_ptr
)
210 STUB_SET_TIME(phys
, phys_ptr
)
211 STUB_GET_WAKEUP_TIME(phys
, phys_ptr
)
212 STUB_SET_WAKEUP_TIME(phys
, phys_ptr
)
213 STUB_GET_VARIABLE(phys
, phys_ptr
)
214 STUB_GET_NEXT_VARIABLE(phys
, phys_ptr
)
215 STUB_SET_VARIABLE(phys
, phys_ptr
)
216 STUB_GET_NEXT_HIGH_MONO_COUNT(phys
, phys_ptr
)
217 STUB_RESET_SYSTEM(phys
, phys_ptr
)
221 STUB_GET_TIME(virt
, id
)
222 STUB_SET_TIME(virt
, id
)
223 STUB_GET_WAKEUP_TIME(virt
, id
)
224 STUB_SET_WAKEUP_TIME(virt
, id
)
225 STUB_GET_VARIABLE(virt
, id
)
226 STUB_GET_NEXT_VARIABLE(virt
, id
)
227 STUB_SET_VARIABLE(virt
, id
)
228 STUB_GET_NEXT_HIGH_MONO_COUNT(virt
, id
)
229 STUB_RESET_SYSTEM(virt
, id
)
232 efi_gettimeofday (struct timespec
*ts
)
236 if ((*efi
.get_time
)(&tm
, NULL
) != EFI_SUCCESS
) {
237 memset(ts
, 0, sizeof(*ts
));
241 ts
->tv_sec
= mktime(tm
.year
, tm
.month
, tm
.day
,
242 tm
.hour
, tm
.minute
, tm
.second
);
243 ts
->tv_nsec
= tm
.nanosecond
;
247 is_memory_available (efi_memory_desc_t
*md
)
249 if (!(md
->attribute
& EFI_MEMORY_WB
))
253 case EFI_LOADER_CODE
:
254 case EFI_LOADER_DATA
:
255 case EFI_BOOT_SERVICES_CODE
:
256 case EFI_BOOT_SERVICES_DATA
:
257 case EFI_CONVENTIONAL_MEMORY
:
263 typedef struct kern_memdesc
{
269 static kern_memdesc_t
*kern_memmap
;
271 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
274 kmd_end(kern_memdesc_t
*kmd
)
276 return (kmd
->start
+ (kmd
->num_pages
<< EFI_PAGE_SHIFT
));
280 efi_md_end(efi_memory_desc_t
*md
)
282 return (md
->phys_addr
+ efi_md_size(md
));
286 efi_wb(efi_memory_desc_t
*md
)
288 return (md
->attribute
& EFI_MEMORY_WB
);
292 efi_uc(efi_memory_desc_t
*md
)
294 return (md
->attribute
& EFI_MEMORY_UC
);
298 walk (efi_freemem_callback_t callback
, void *arg
, u64 attr
)
301 u64 start
, end
, voff
;
303 voff
= (attr
== EFI_MEMORY_WB
) ? PAGE_OFFSET
: __IA64_UNCACHED_OFFSET
;
304 for (k
= kern_memmap
; k
->start
!= ~0UL; k
++) {
305 if (k
->attribute
!= attr
)
307 start
= PAGE_ALIGN(k
->start
);
308 end
= (k
->start
+ (k
->num_pages
<< EFI_PAGE_SHIFT
)) & PAGE_MASK
;
310 if ((*callback
)(start
+ voff
, end
+ voff
, arg
) < 0)
316 * Walk the EFI memory map and call CALLBACK once for each EFI memory
317 * descriptor that has memory that is available for OS use.
320 efi_memmap_walk (efi_freemem_callback_t callback
, void *arg
)
322 walk(callback
, arg
, EFI_MEMORY_WB
);
326 * Walk the EFI memory map and call CALLBACK once for each EFI memory
327 * descriptor that has memory that is available for uncached allocator.
330 efi_memmap_walk_uc (efi_freemem_callback_t callback
, void *arg
)
332 walk(callback
, arg
, EFI_MEMORY_UC
);
336 * Look for the PAL_CODE region reported by EFI and map it using an
337 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
338 * Abstraction Layer chapter 11 in ADAG
341 efi_get_pal_addr (void)
343 void *efi_map_start
, *efi_map_end
, *p
;
344 efi_memory_desc_t
*md
;
346 int pal_code_count
= 0;
349 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
350 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
351 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
353 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
355 if (md
->type
!= EFI_PAL_CODE
)
358 if (++pal_code_count
> 1) {
359 printk(KERN_ERR
"Too many EFI Pal Code memory ranges, "
360 "dropped @ %llx\n", md
->phys_addr
);
364 * The only ITLB entry in region 7 that is used is the one
365 * installed by __start(). That entry covers a 64MB range.
367 mask
= ~((1 << KERNEL_TR_PAGE_SHIFT
) - 1);
368 vaddr
= PAGE_OFFSET
+ md
->phys_addr
;
371 * We must check that the PAL mapping won't overlap with the
374 * PAL code is guaranteed to be aligned on a power of 2 between
375 * 4k and 256KB and that only one ITR is needed to map it. This
376 * implies that the PAL code is always aligned on its size,
377 * i.e., the closest matching page size supported by the TLB.
378 * Therefore PAL code is guaranteed never to cross a 64MB unless
379 * it is bigger than 64MB (very unlikely!). So for now the
380 * following test is enough to determine whether or not we need
381 * a dedicated ITR for the PAL code.
383 if ((vaddr
& mask
) == (KERNEL_START
& mask
)) {
384 printk(KERN_INFO
"%s: no need to install ITR for PAL code\n",
389 if (efi_md_size(md
) > IA64_GRANULE_SIZE
)
390 panic("Whoa! PAL code size bigger than a granule!");
393 mask
= ~((1 << IA64_GRANULE_SHIFT
) - 1);
395 printk(KERN_INFO
"CPU %d: mapping PAL code "
396 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
397 smp_processor_id(), md
->phys_addr
,
398 md
->phys_addr
+ efi_md_size(md
),
399 vaddr
& mask
, (vaddr
& mask
) + IA64_GRANULE_SIZE
);
401 return __va(md
->phys_addr
);
403 printk(KERN_WARNING
"%s: no PAL-code memory-descriptor found\n",
409 static u8 __init
palo_checksum(u8
*buffer
, u32 length
)
412 u8
*end
= buffer
+ length
;
415 sum
= (u8
) (sum
+ *(buffer
++));
421 * Parse and handle PALO table which is published at:
422 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
424 static void __init
handle_palo(unsigned long palo_phys
)
426 struct palo_table
*palo
= __va(palo_phys
);
429 if (strncmp(palo
->signature
, PALO_SIG
, sizeof(PALO_SIG
) - 1)) {
430 printk(KERN_INFO
"PALO signature incorrect.\n");
434 checksum
= palo_checksum((u8
*)palo
, palo
->length
);
436 printk(KERN_INFO
"PALO checksum incorrect.\n");
440 setup_ptcg_sem(palo
->max_tlb_purges
, NPTCG_FROM_PALO
);
444 efi_map_pal_code (void)
446 void *pal_vaddr
= efi_get_pal_addr ();
453 * Cannot write to CRx with PSR.ic=1
455 psr
= ia64_clear_ic();
456 ia64_itr(0x1, IA64_TR_PALCODE
,
457 GRANULEROUNDDOWN((unsigned long) pal_vaddr
),
458 pte_val(pfn_pte(__pa(pal_vaddr
) >> PAGE_SHIFT
, PAGE_KERNEL
)),
460 paravirt_dv_serialize_data();
461 ia64_set_psr(psr
); /* restore psr */
467 void *efi_map_start
, *efi_map_end
;
468 efi_config_table_t
*config_tables
;
471 char *cp
, vendor
[100] = "unknown";
473 unsigned long palo_phys
;
476 * It's too early to be able to use the standard kernel command line
479 for (cp
= boot_command_line
; *cp
; ) {
480 if (memcmp(cp
, "mem=", 4) == 0) {
481 mem_limit
= memparse(cp
+ 4, &cp
);
482 } else if (memcmp(cp
, "max_addr=", 9) == 0) {
483 max_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
484 } else if (memcmp(cp
, "min_addr=", 9) == 0) {
485 min_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
487 while (*cp
!= ' ' && *cp
)
494 printk(KERN_INFO
"Ignoring memory below %lluMB\n",
496 if (max_addr
!= ~0UL)
497 printk(KERN_INFO
"Ignoring memory above %lluMB\n",
500 efi
.systab
= __va(ia64_boot_param
->efi_systab
);
503 * Verify the EFI Table
505 if (efi
.systab
== NULL
)
506 panic("Whoa! Can't find EFI system table.\n");
507 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
508 panic("Whoa! EFI system table signature incorrect\n");
509 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
510 printk(KERN_WARNING
"Warning: EFI system table version "
511 "%d.%02d, expected 1.00 or greater\n",
512 efi
.systab
->hdr
.revision
>> 16,
513 efi
.systab
->hdr
.revision
& 0xffff);
515 config_tables
= __va(efi
.systab
->tables
);
517 /* Show what we know for posterity */
518 c16
= __va(efi
.systab
->fw_vendor
);
520 for (i
= 0;i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
525 printk(KERN_INFO
"EFI v%u.%.02u by %s:",
526 efi
.systab
->hdr
.revision
>> 16,
527 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
529 efi
.mps
= EFI_INVALID_TABLE_ADDR
;
530 efi
.acpi
= EFI_INVALID_TABLE_ADDR
;
531 efi
.acpi20
= EFI_INVALID_TABLE_ADDR
;
532 efi
.smbios
= EFI_INVALID_TABLE_ADDR
;
533 efi
.sal_systab
= EFI_INVALID_TABLE_ADDR
;
534 efi
.boot_info
= EFI_INVALID_TABLE_ADDR
;
535 efi
.hcdp
= EFI_INVALID_TABLE_ADDR
;
536 efi
.uga
= EFI_INVALID_TABLE_ADDR
;
538 palo_phys
= EFI_INVALID_TABLE_ADDR
;
540 for (i
= 0; i
< (int) efi
.systab
->nr_tables
; i
++) {
541 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
542 efi
.mps
= config_tables
[i
].table
;
543 printk(" MPS=0x%lx", config_tables
[i
].table
);
544 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
545 efi
.acpi20
= config_tables
[i
].table
;
546 printk(" ACPI 2.0=0x%lx", config_tables
[i
].table
);
547 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
548 efi
.acpi
= config_tables
[i
].table
;
549 printk(" ACPI=0x%lx", config_tables
[i
].table
);
550 } else if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
551 efi
.smbios
= config_tables
[i
].table
;
552 printk(" SMBIOS=0x%lx", config_tables
[i
].table
);
553 } else if (efi_guidcmp(config_tables
[i
].guid
, SAL_SYSTEM_TABLE_GUID
) == 0) {
554 efi
.sal_systab
= config_tables
[i
].table
;
555 printk(" SALsystab=0x%lx", config_tables
[i
].table
);
556 } else if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
557 efi
.hcdp
= config_tables
[i
].table
;
558 printk(" HCDP=0x%lx", config_tables
[i
].table
);
559 } else if (efi_guidcmp(config_tables
[i
].guid
,
560 PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID
) == 0) {
561 palo_phys
= config_tables
[i
].table
;
562 printk(" PALO=0x%lx", config_tables
[i
].table
);
567 if (palo_phys
!= EFI_INVALID_TABLE_ADDR
)
568 handle_palo(palo_phys
);
570 runtime
= __va(efi
.systab
->runtime
);
571 efi
.get_time
= phys_get_time
;
572 efi
.set_time
= phys_set_time
;
573 efi
.get_wakeup_time
= phys_get_wakeup_time
;
574 efi
.set_wakeup_time
= phys_set_wakeup_time
;
575 efi
.get_variable
= phys_get_variable
;
576 efi
.get_next_variable
= phys_get_next_variable
;
577 efi
.set_variable
= phys_set_variable
;
578 efi
.get_next_high_mono_count
= phys_get_next_high_mono_count
;
579 efi
.reset_system
= phys_reset_system
;
581 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
582 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
583 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
586 /* print EFI memory map: */
588 efi_memory_desc_t
*md
;
591 for (i
= 0, p
= efi_map_start
; p
< efi_map_end
;
592 ++i
, p
+= efi_desc_size
)
598 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
600 if ((size
>> 40) > 0) {
603 } else if ((size
>> 30) > 0) {
606 } else if ((size
>> 20) > 0) {
614 printk("mem%02d: type=%2u, attr=0x%016lx, "
615 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
616 i
, md
->type
, md
->attribute
, md
->phys_addr
,
617 md
->phys_addr
+ efi_md_size(md
), size
, unit
);
623 efi_enter_virtual_mode();
627 efi_enter_virtual_mode (void)
629 void *efi_map_start
, *efi_map_end
, *p
;
630 efi_memory_desc_t
*md
;
634 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
635 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
636 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
638 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
640 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
642 * Some descriptors have multiple bits set, so the
643 * order of the tests is relevant.
645 if (md
->attribute
& EFI_MEMORY_WB
) {
646 md
->virt_addr
= (u64
) __va(md
->phys_addr
);
647 } else if (md
->attribute
& EFI_MEMORY_UC
) {
648 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
649 } else if (md
->attribute
& EFI_MEMORY_WC
) {
650 printk(KERN_INFO
"EFI_MEMORY_WC mapping\n");
651 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
652 } else if (md
->attribute
& EFI_MEMORY_WT
) {
653 printk(KERN_INFO
"EFI_MEMORY_WT mapping\n");
654 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
659 status
= efi_call_phys(__va(runtime
->set_virtual_address_map
),
660 ia64_boot_param
->efi_memmap_size
,
662 ia64_boot_param
->efi_memdesc_version
,
663 ia64_boot_param
->efi_memmap
);
664 if (status
!= EFI_SUCCESS
) {
665 printk(KERN_WARNING
"warning: unable to switch EFI into "
666 "virtual mode (status=%lu)\n", status
);
671 * Now that EFI is in virtual mode, we call the EFI functions more
674 efi
.get_time
= virt_get_time
;
675 efi
.set_time
= virt_set_time
;
676 efi
.get_wakeup_time
= virt_get_wakeup_time
;
677 efi
.set_wakeup_time
= virt_set_wakeup_time
;
678 efi
.get_variable
= virt_get_variable
;
679 efi
.get_next_variable
= virt_get_next_variable
;
680 efi
.set_variable
= virt_set_variable
;
681 efi
.get_next_high_mono_count
= virt_get_next_high_mono_count
;
682 efi
.reset_system
= virt_reset_system
;
686 * Walk the EFI memory map looking for the I/O port range. There can only be
687 * one entry of this type, other I/O port ranges should be described via ACPI.
690 efi_get_iobase (void)
692 void *efi_map_start
, *efi_map_end
, *p
;
693 efi_memory_desc_t
*md
;
696 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
697 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
698 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
700 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
702 if (md
->type
== EFI_MEMORY_MAPPED_IO_PORT_SPACE
) {
703 if (md
->attribute
& EFI_MEMORY_UC
)
704 return md
->phys_addr
;
710 static struct kern_memdesc
*
711 kern_memory_descriptor (unsigned long phys_addr
)
713 struct kern_memdesc
*md
;
715 for (md
= kern_memmap
; md
->start
!= ~0UL; md
++) {
716 if (phys_addr
- md
->start
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
722 static efi_memory_desc_t
*
723 efi_memory_descriptor (unsigned long phys_addr
)
725 void *efi_map_start
, *efi_map_end
, *p
;
726 efi_memory_desc_t
*md
;
729 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
730 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
731 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
733 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
736 if (phys_addr
- md
->phys_addr
< efi_md_size(md
))
743 efi_memmap_intersects (unsigned long phys_addr
, unsigned long size
)
745 void *efi_map_start
, *efi_map_end
, *p
;
746 efi_memory_desc_t
*md
;
750 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
751 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
752 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
754 end
= phys_addr
+ size
;
756 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
758 if (md
->phys_addr
< end
&& efi_md_end(md
) > phys_addr
)
765 efi_mem_type (unsigned long phys_addr
)
767 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
775 efi_mem_attributes (unsigned long phys_addr
)
777 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
780 return md
->attribute
;
783 EXPORT_SYMBOL(efi_mem_attributes
);
786 efi_mem_attribute (unsigned long phys_addr
, unsigned long size
)
788 unsigned long end
= phys_addr
+ size
;
789 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
796 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
797 * the kernel that firmware needs this region mapped.
799 attr
= md
->attribute
& ~EFI_MEMORY_RUNTIME
;
801 unsigned long md_end
= efi_md_end(md
);
806 md
= efi_memory_descriptor(md_end
);
807 if (!md
|| (md
->attribute
& ~EFI_MEMORY_RUNTIME
) != attr
)
810 return 0; /* never reached */
814 kern_mem_attribute (unsigned long phys_addr
, unsigned long size
)
816 unsigned long end
= phys_addr
+ size
;
817 struct kern_memdesc
*md
;
821 * This is a hack for ioremap calls before we set up kern_memmap.
822 * Maybe we should do efi_memmap_init() earlier instead.
825 attr
= efi_mem_attribute(phys_addr
, size
);
826 if (attr
& EFI_MEMORY_WB
)
827 return EFI_MEMORY_WB
;
831 md
= kern_memory_descriptor(phys_addr
);
835 attr
= md
->attribute
;
837 unsigned long md_end
= kmd_end(md
);
842 md
= kern_memory_descriptor(md_end
);
843 if (!md
|| md
->attribute
!= attr
)
846 return 0; /* never reached */
848 EXPORT_SYMBOL(kern_mem_attribute
);
851 valid_phys_addr_range (unsigned long phys_addr
, unsigned long size
)
856 * /dev/mem reads and writes use copy_to_user(), which implicitly
857 * uses a granule-sized kernel identity mapping. It's really
858 * only safe to do this for regions in kern_memmap. For more
859 * details, see Documentation/ia64/aliasing.txt.
861 attr
= kern_mem_attribute(phys_addr
, size
);
862 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
868 valid_mmap_phys_addr_range (unsigned long pfn
, unsigned long size
)
870 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
873 attr
= efi_mem_attribute(phys_addr
, size
);
876 * /dev/mem mmap uses normal user pages, so we don't need the entire
877 * granule, but the entire region we're mapping must support the same
880 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
884 * Intel firmware doesn't tell us about all the MMIO regions, so
885 * in general we have to allow mmap requests. But if EFI *does*
886 * tell us about anything inside this region, we should deny it.
887 * The user can always map a smaller region to avoid the overlap.
889 if (efi_memmap_intersects(phys_addr
, size
))
896 phys_mem_access_prot(struct file
*file
, unsigned long pfn
, unsigned long size
,
899 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
903 * For /dev/mem mmap, we use user mappings, but if the region is
904 * in kern_memmap (and hence may be covered by a kernel mapping),
905 * we must use the same attribute as the kernel mapping.
907 attr
= kern_mem_attribute(phys_addr
, size
);
908 if (attr
& EFI_MEMORY_WB
)
909 return pgprot_cacheable(vma_prot
);
910 else if (attr
& EFI_MEMORY_UC
)
911 return pgprot_noncached(vma_prot
);
914 * Some chipsets don't support UC access to memory. If
915 * WB is supported, we prefer that.
917 if (efi_mem_attribute(phys_addr
, size
) & EFI_MEMORY_WB
)
918 return pgprot_cacheable(vma_prot
);
920 return pgprot_noncached(vma_prot
);
924 efi_uart_console_only(void)
927 char *s
, name
[] = "ConOut";
928 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
929 efi_char16_t
*utf16
, name_utf16
[32];
930 unsigned char data
[1024];
931 unsigned long size
= sizeof(data
);
932 struct efi_generic_dev_path
*hdr
, *end_addr
;
935 /* Convert to UTF-16 */
939 *utf16
++ = *s
++ & 0x7f;
942 status
= efi
.get_variable(name_utf16
, &guid
, NULL
, &size
, data
);
943 if (status
!= EFI_SUCCESS
) {
944 printk(KERN_ERR
"No EFI %s variable?\n", name
);
948 hdr
= (struct efi_generic_dev_path
*) data
;
949 end_addr
= (struct efi_generic_dev_path
*) ((u8
*) data
+ size
);
950 while (hdr
< end_addr
) {
951 if (hdr
->type
== EFI_DEV_MSG
&&
952 hdr
->sub_type
== EFI_DEV_MSG_UART
)
954 else if (hdr
->type
== EFI_DEV_END_PATH
||
955 hdr
->type
== EFI_DEV_END_PATH2
) {
958 if (hdr
->sub_type
== EFI_DEV_END_ENTIRE
)
962 hdr
= (struct efi_generic_dev_path
*)((u8
*) hdr
+ hdr
->length
);
964 printk(KERN_ERR
"Malformed %s value\n", name
);
969 * Look for the first granule aligned memory descriptor memory
970 * that is big enough to hold EFI memory map. Make sure this
971 * descriptor is atleast granule sized so it does not get trimmed
973 struct kern_memdesc
*
974 find_memmap_space (void)
976 u64 contig_low
=0, contig_high
=0;
978 void *efi_map_start
, *efi_map_end
, *p
, *q
;
979 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
980 u64 space_needed
, efi_desc_size
;
981 unsigned long total_mem
= 0;
983 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
984 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
985 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
988 * Worst case: we need 3 kernel descriptors for each efi descriptor
989 * (if every entry has a WB part in the middle, and UC head and tail),
990 * plus one for the end marker.
992 space_needed
= sizeof(kern_memdesc_t
) *
993 (3 * (ia64_boot_param
->efi_memmap_size
/efi_desc_size
) + 1);
995 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
1000 if (pmd
== NULL
|| !efi_wb(pmd
) ||
1001 efi_md_end(pmd
) != md
->phys_addr
) {
1002 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
1003 contig_high
= efi_md_end(md
);
1004 for (q
= p
+ efi_desc_size
; q
< efi_map_end
;
1005 q
+= efi_desc_size
) {
1007 if (!efi_wb(check_md
))
1009 if (contig_high
!= check_md
->phys_addr
)
1011 contig_high
= efi_md_end(check_md
);
1013 contig_high
= GRANULEROUNDDOWN(contig_high
);
1015 if (!is_memory_available(md
) || md
->type
== EFI_LOADER_DATA
)
1018 /* Round ends inward to granule boundaries */
1019 as
= max(contig_low
, md
->phys_addr
);
1020 ae
= min(contig_high
, efi_md_end(md
));
1022 /* keep within max_addr= and min_addr= command line arg */
1023 as
= max(as
, min_addr
);
1024 ae
= min(ae
, max_addr
);
1028 /* avoid going over mem= command line arg */
1029 if (total_mem
+ (ae
- as
) > mem_limit
)
1030 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1035 if (ae
- as
> space_needed
)
1038 if (p
>= efi_map_end
)
1039 panic("Can't allocate space for kernel memory descriptors");
1045 * Walk the EFI memory map and gather all memory available for kernel
1046 * to use. We can allocate partial granules only if the unavailable
1047 * parts exist, and are WB.
1050 efi_memmap_init(u64
*s
, u64
*e
)
1052 struct kern_memdesc
*k
, *prev
= NULL
;
1053 u64 contig_low
=0, contig_high
=0;
1055 void *efi_map_start
, *efi_map_end
, *p
, *q
;
1056 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
1058 unsigned long total_mem
= 0;
1060 k
= kern_memmap
= find_memmap_space();
1062 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1063 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1064 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1066 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
1070 (md
->type
== EFI_CONVENTIONAL_MEMORY
||
1071 md
->type
== EFI_BOOT_SERVICES_DATA
)) {
1072 k
->attribute
= EFI_MEMORY_UC
;
1073 k
->start
= md
->phys_addr
;
1074 k
->num_pages
= md
->num_pages
;
1079 if (pmd
== NULL
|| !efi_wb(pmd
) ||
1080 efi_md_end(pmd
) != md
->phys_addr
) {
1081 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
1082 contig_high
= efi_md_end(md
);
1083 for (q
= p
+ efi_desc_size
; q
< efi_map_end
;
1084 q
+= efi_desc_size
) {
1086 if (!efi_wb(check_md
))
1088 if (contig_high
!= check_md
->phys_addr
)
1090 contig_high
= efi_md_end(check_md
);
1092 contig_high
= GRANULEROUNDDOWN(contig_high
);
1094 if (!is_memory_available(md
))
1097 #ifdef CONFIG_CRASH_DUMP
1098 /* saved_max_pfn should ignore max_addr= command line arg */
1099 if (saved_max_pfn
< (efi_md_end(md
) >> PAGE_SHIFT
))
1100 saved_max_pfn
= (efi_md_end(md
) >> PAGE_SHIFT
);
1103 * Round ends inward to granule boundaries
1104 * Give trimmings to uncached allocator
1106 if (md
->phys_addr
< contig_low
) {
1107 lim
= min(efi_md_end(md
), contig_low
);
1109 if (k
> kern_memmap
&&
1110 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1111 kmd_end(k
-1) == md
->phys_addr
) {
1113 (lim
- md
->phys_addr
)
1116 k
->attribute
= EFI_MEMORY_UC
;
1117 k
->start
= md
->phys_addr
;
1118 k
->num_pages
= (lim
- md
->phys_addr
)
1127 if (efi_md_end(md
) > contig_high
) {
1128 lim
= max(md
->phys_addr
, contig_high
);
1130 if (lim
== md
->phys_addr
&& k
> kern_memmap
&&
1131 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1132 kmd_end(k
-1) == md
->phys_addr
) {
1133 (k
-1)->num_pages
+= md
->num_pages
;
1135 k
->attribute
= EFI_MEMORY_UC
;
1137 k
->num_pages
= (efi_md_end(md
) - lim
)
1144 ae
= efi_md_end(md
);
1146 /* keep within max_addr= and min_addr= command line arg */
1147 as
= max(as
, min_addr
);
1148 ae
= min(ae
, max_addr
);
1152 /* avoid going over mem= command line arg */
1153 if (total_mem
+ (ae
- as
) > mem_limit
)
1154 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1158 if (prev
&& kmd_end(prev
) == md
->phys_addr
) {
1159 prev
->num_pages
+= (ae
- as
) >> EFI_PAGE_SHIFT
;
1160 total_mem
+= ae
- as
;
1163 k
->attribute
= EFI_MEMORY_WB
;
1165 k
->num_pages
= (ae
- as
) >> EFI_PAGE_SHIFT
;
1166 total_mem
+= ae
- as
;
1169 k
->start
= ~0L; /* end-marker */
1171 /* reserve the memory we are using for kern_memmap */
1172 *s
= (u64
)kern_memmap
;
1179 efi_initialize_iomem_resources(struct resource
*code_resource
,
1180 struct resource
*data_resource
,
1181 struct resource
*bss_resource
)
1183 struct resource
*res
;
1184 void *efi_map_start
, *efi_map_end
, *p
;
1185 efi_memory_desc_t
*md
;
1188 unsigned long flags
;
1190 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1191 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1192 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1196 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1199 if (md
->num_pages
== 0) /* should not happen */
1202 flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
1205 case EFI_MEMORY_MAPPED_IO
:
1206 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1209 case EFI_LOADER_CODE
:
1210 case EFI_LOADER_DATA
:
1211 case EFI_BOOT_SERVICES_DATA
:
1212 case EFI_BOOT_SERVICES_CODE
:
1213 case EFI_CONVENTIONAL_MEMORY
:
1214 if (md
->attribute
& EFI_MEMORY_WP
) {
1215 name
= "System ROM";
1216 flags
|= IORESOURCE_READONLY
;
1217 } else if (md
->attribute
== EFI_MEMORY_UC
)
1218 name
= "Uncached RAM";
1220 name
= "System RAM";
1223 case EFI_ACPI_MEMORY_NVS
:
1224 name
= "ACPI Non-volatile Storage";
1227 case EFI_UNUSABLE_MEMORY
:
1229 flags
|= IORESOURCE_DISABLED
;
1232 case EFI_RESERVED_TYPE
:
1233 case EFI_RUNTIME_SERVICES_CODE
:
1234 case EFI_RUNTIME_SERVICES_DATA
:
1235 case EFI_ACPI_RECLAIM_MEMORY
:
1241 if ((res
= kzalloc(sizeof(struct resource
),
1242 GFP_KERNEL
)) == NULL
) {
1244 "failed to allocate resource for iomem\n");
1249 res
->start
= md
->phys_addr
;
1250 res
->end
= md
->phys_addr
+ efi_md_size(md
) - 1;
1253 if (insert_resource(&iomem_resource
, res
) < 0)
1257 * We don't know which region contains
1258 * kernel data so we try it repeatedly and
1259 * let the resource manager test it.
1261 insert_resource(res
, code_resource
);
1262 insert_resource(res
, data_resource
);
1263 insert_resource(res
, bss_resource
);
1265 insert_resource(res
, &efi_memmap_res
);
1266 insert_resource(res
, &boot_param_res
);
1267 if (crashk_res
.end
> crashk_res
.start
)
1268 insert_resource(res
, &crashk_res
);
1275 /* find a block of memory aligned to 64M exclude reserved regions
1276 rsvd_regions are sorted
1278 unsigned long __init
1279 kdump_find_rsvd_region (unsigned long size
, struct rsvd_region
*r
, int n
)
1283 u64 alignment
= 1UL << _PAGE_SIZE_64M
;
1284 void *efi_map_start
, *efi_map_end
, *p
;
1285 efi_memory_desc_t
*md
;
1288 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1289 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1290 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1292 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1296 start
= ALIGN(md
->phys_addr
, alignment
);
1297 end
= efi_md_end(md
);
1298 for (i
= 0; i
< n
; i
++) {
1299 if (__pa(r
[i
].start
) >= start
&& __pa(r
[i
].end
) < end
) {
1300 if (__pa(r
[i
].start
) > start
+ size
)
1302 start
= ALIGN(__pa(r
[i
].end
), alignment
);
1304 __pa(r
[i
+1].start
) < start
+ size
)
1310 if (end
> start
+ size
)
1315 "Cannot reserve 0x%lx byte of memory for crashdump\n", size
);
1320 #ifdef CONFIG_CRASH_DUMP
1321 /* locate the size find a the descriptor at a certain address */
1322 unsigned long __init
1323 vmcore_find_descriptor_size (unsigned long address
)
1325 void *efi_map_start
, *efi_map_end
, *p
;
1326 efi_memory_desc_t
*md
;
1328 unsigned long ret
= 0;
1330 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1331 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1332 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1334 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1336 if (efi_wb(md
) && md
->type
== EFI_LOADER_DATA
1337 && md
->phys_addr
== address
) {
1338 ret
= efi_md_size(md
);
1344 printk(KERN_WARNING
"Cannot locate EFI vmcore descriptor\n");