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 extern efi_status_t
efi_call_phys (void *, ...);
51 static efi_runtime_services_t
*runtime
;
52 static u64 mem_limit
= ~0UL, max_addr
= ~0UL, min_addr
= 0UL;
54 #define efi_call_virt(f, args...) (*(f))(args)
56 #define STUB_GET_TIME(prefix, adjust_arg) \
58 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
60 struct ia64_fpreg fr[6]; \
61 efi_time_cap_t *atc = NULL; \
65 atc = adjust_arg(tc); \
66 ia64_save_scratch_fpregs(fr); \
67 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), \
68 adjust_arg(tm), atc); \
69 ia64_load_scratch_fpregs(fr); \
73 #define STUB_SET_TIME(prefix, adjust_arg) \
75 prefix##_set_time (efi_time_t *tm) \
77 struct ia64_fpreg fr[6]; \
80 ia64_save_scratch_fpregs(fr); \
81 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
83 ia64_load_scratch_fpregs(fr); \
87 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
89 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
92 struct ia64_fpreg fr[6]; \
95 ia64_save_scratch_fpregs(fr); \
96 ret = efi_call_##prefix( \
97 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
98 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
99 ia64_load_scratch_fpregs(fr); \
103 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
104 static efi_status_t \
105 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
107 struct ia64_fpreg fr[6]; \
108 efi_time_t *atm = NULL; \
112 atm = adjust_arg(tm); \
113 ia64_save_scratch_fpregs(fr); \
114 ret = efi_call_##prefix( \
115 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
117 ia64_load_scratch_fpregs(fr); \
121 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
122 static efi_status_t \
123 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
124 unsigned long *data_size, void *data) \
126 struct ia64_fpreg fr[6]; \
131 aattr = adjust_arg(attr); \
132 ia64_save_scratch_fpregs(fr); \
133 ret = efi_call_##prefix( \
134 (efi_get_variable_t *) __va(runtime->get_variable), \
135 adjust_arg(name), adjust_arg(vendor), aattr, \
136 adjust_arg(data_size), adjust_arg(data)); \
137 ia64_load_scratch_fpregs(fr); \
141 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
142 static efi_status_t \
143 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
144 efi_guid_t *vendor) \
146 struct ia64_fpreg fr[6]; \
149 ia64_save_scratch_fpregs(fr); \
150 ret = efi_call_##prefix( \
151 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
152 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
153 ia64_load_scratch_fpregs(fr); \
157 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
158 static efi_status_t \
159 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
160 u32 attr, unsigned long data_size, \
163 struct ia64_fpreg fr[6]; \
166 ia64_save_scratch_fpregs(fr); \
167 ret = efi_call_##prefix( \
168 (efi_set_variable_t *) __va(runtime->set_variable), \
169 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
171 ia64_load_scratch_fpregs(fr); \
175 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
176 static efi_status_t \
177 prefix##_get_next_high_mono_count (u32 *count) \
179 struct ia64_fpreg fr[6]; \
182 ia64_save_scratch_fpregs(fr); \
183 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
184 __va(runtime->get_next_high_mono_count), \
185 adjust_arg(count)); \
186 ia64_load_scratch_fpregs(fr); \
190 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
192 prefix##_reset_system (int reset_type, efi_status_t status, \
193 unsigned long data_size, efi_char16_t *data) \
195 struct ia64_fpreg fr[6]; \
196 efi_char16_t *adata = NULL; \
199 adata = adjust_arg(data); \
201 ia64_save_scratch_fpregs(fr); \
203 (efi_reset_system_t *) __va(runtime->reset_system), \
204 reset_type, status, data_size, adata); \
205 /* should not return, but just in case... */ \
206 ia64_load_scratch_fpregs(fr); \
209 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
211 STUB_GET_TIME(phys
, phys_ptr
)
212 STUB_SET_TIME(phys
, phys_ptr
)
213 STUB_GET_WAKEUP_TIME(phys
, phys_ptr
)
214 STUB_SET_WAKEUP_TIME(phys
, phys_ptr
)
215 STUB_GET_VARIABLE(phys
, phys_ptr
)
216 STUB_GET_NEXT_VARIABLE(phys
, phys_ptr
)
217 STUB_SET_VARIABLE(phys
, phys_ptr
)
218 STUB_GET_NEXT_HIGH_MONO_COUNT(phys
, phys_ptr
)
219 STUB_RESET_SYSTEM(phys
, phys_ptr
)
223 STUB_GET_TIME(virt
, id
)
224 STUB_SET_TIME(virt
, id
)
225 STUB_GET_WAKEUP_TIME(virt
, id
)
226 STUB_SET_WAKEUP_TIME(virt
, id
)
227 STUB_GET_VARIABLE(virt
, id
)
228 STUB_GET_NEXT_VARIABLE(virt
, id
)
229 STUB_SET_VARIABLE(virt
, id
)
230 STUB_GET_NEXT_HIGH_MONO_COUNT(virt
, id
)
231 STUB_RESET_SYSTEM(virt
, id
)
234 efi_gettimeofday (struct timespec
*ts
)
238 if ((*efi
.get_time
)(&tm
, NULL
) != EFI_SUCCESS
) {
239 memset(ts
, 0, sizeof(*ts
));
243 ts
->tv_sec
= mktime(tm
.year
, tm
.month
, tm
.day
,
244 tm
.hour
, tm
.minute
, tm
.second
);
245 ts
->tv_nsec
= tm
.nanosecond
;
249 is_memory_available (efi_memory_desc_t
*md
)
251 if (!(md
->attribute
& EFI_MEMORY_WB
))
255 case EFI_LOADER_CODE
:
256 case EFI_LOADER_DATA
:
257 case EFI_BOOT_SERVICES_CODE
:
258 case EFI_BOOT_SERVICES_DATA
:
259 case EFI_CONVENTIONAL_MEMORY
:
265 typedef struct kern_memdesc
{
271 static kern_memdesc_t
*kern_memmap
;
273 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
276 kmd_end(kern_memdesc_t
*kmd
)
278 return (kmd
->start
+ (kmd
->num_pages
<< EFI_PAGE_SHIFT
));
282 efi_md_end(efi_memory_desc_t
*md
)
284 return (md
->phys_addr
+ efi_md_size(md
));
288 efi_wb(efi_memory_desc_t
*md
)
290 return (md
->attribute
& EFI_MEMORY_WB
);
294 efi_uc(efi_memory_desc_t
*md
)
296 return (md
->attribute
& EFI_MEMORY_UC
);
300 walk (efi_freemem_callback_t callback
, void *arg
, u64 attr
)
303 u64 start
, end
, voff
;
305 voff
= (attr
== EFI_MEMORY_WB
) ? PAGE_OFFSET
: __IA64_UNCACHED_OFFSET
;
306 for (k
= kern_memmap
; k
->start
!= ~0UL; k
++) {
307 if (k
->attribute
!= attr
)
309 start
= PAGE_ALIGN(k
->start
);
310 end
= (k
->start
+ (k
->num_pages
<< EFI_PAGE_SHIFT
)) & PAGE_MASK
;
312 if ((*callback
)(start
+ voff
, end
+ voff
, arg
) < 0)
318 * Walk the EFI memory map and call CALLBACK once for each EFI memory
319 * descriptor that has memory that is available for OS use.
322 efi_memmap_walk (efi_freemem_callback_t callback
, void *arg
)
324 walk(callback
, arg
, EFI_MEMORY_WB
);
328 * Walk the EFI memory map and call CALLBACK once for each EFI memory
329 * descriptor that has memory that is available for uncached allocator.
332 efi_memmap_walk_uc (efi_freemem_callback_t callback
, void *arg
)
334 walk(callback
, arg
, EFI_MEMORY_UC
);
338 * Look for the PAL_CODE region reported by EFI and map it using an
339 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
340 * Abstraction Layer chapter 11 in ADAG
343 efi_get_pal_addr (void)
345 void *efi_map_start
, *efi_map_end
, *p
;
346 efi_memory_desc_t
*md
;
348 int pal_code_count
= 0;
351 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
352 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
353 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
355 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
357 if (md
->type
!= EFI_PAL_CODE
)
360 if (++pal_code_count
> 1) {
361 printk(KERN_ERR
"Too many EFI Pal Code memory ranges, "
362 "dropped @ %llx\n", md
->phys_addr
);
366 * The only ITLB entry in region 7 that is used is the one
367 * installed by __start(). That entry covers a 64MB range.
369 mask
= ~((1 << KERNEL_TR_PAGE_SHIFT
) - 1);
370 vaddr
= PAGE_OFFSET
+ md
->phys_addr
;
373 * We must check that the PAL mapping won't overlap with the
376 * PAL code is guaranteed to be aligned on a power of 2 between
377 * 4k and 256KB and that only one ITR is needed to map it. This
378 * implies that the PAL code is always aligned on its size,
379 * i.e., the closest matching page size supported by the TLB.
380 * Therefore PAL code is guaranteed never to cross a 64MB unless
381 * it is bigger than 64MB (very unlikely!). So for now the
382 * following test is enough to determine whether or not we need
383 * a dedicated ITR for the PAL code.
385 if ((vaddr
& mask
) == (KERNEL_START
& mask
)) {
386 printk(KERN_INFO
"%s: no need to install ITR for PAL code\n",
391 if (efi_md_size(md
) > IA64_GRANULE_SIZE
)
392 panic("Whoa! PAL code size bigger than a granule!");
395 mask
= ~((1 << IA64_GRANULE_SHIFT
) - 1);
397 printk(KERN_INFO
"CPU %d: mapping PAL code "
398 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
399 smp_processor_id(), md
->phys_addr
,
400 md
->phys_addr
+ efi_md_size(md
),
401 vaddr
& mask
, (vaddr
& mask
) + IA64_GRANULE_SIZE
);
403 return __va(md
->phys_addr
);
405 printk(KERN_WARNING
"%s: no PAL-code memory-descriptor found\n",
411 static u8 __init
palo_checksum(u8
*buffer
, u32 length
)
414 u8
*end
= buffer
+ length
;
417 sum
= (u8
) (sum
+ *(buffer
++));
423 * Parse and handle PALO table which is published at:
424 * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
426 static void __init
handle_palo(unsigned long palo_phys
)
428 struct palo_table
*palo
= __va(palo_phys
);
431 if (strncmp(palo
->signature
, PALO_SIG
, sizeof(PALO_SIG
) - 1)) {
432 printk(KERN_INFO
"PALO signature incorrect.\n");
436 checksum
= palo_checksum((u8
*)palo
, palo
->length
);
438 printk(KERN_INFO
"PALO checksum incorrect.\n");
442 setup_ptcg_sem(palo
->max_tlb_purges
, NPTCG_FROM_PALO
);
446 efi_map_pal_code (void)
448 void *pal_vaddr
= efi_get_pal_addr ();
455 * Cannot write to CRx with PSR.ic=1
457 psr
= ia64_clear_ic();
458 ia64_itr(0x1, IA64_TR_PALCODE
,
459 GRANULEROUNDDOWN((unsigned long) pal_vaddr
),
460 pte_val(pfn_pte(__pa(pal_vaddr
) >> PAGE_SHIFT
, PAGE_KERNEL
)),
462 paravirt_dv_serialize_data();
463 ia64_set_psr(psr
); /* restore psr */
469 void *efi_map_start
, *efi_map_end
;
470 efi_config_table_t
*config_tables
;
473 char *cp
, vendor
[100] = "unknown";
475 unsigned long palo_phys
;
478 * It's too early to be able to use the standard kernel command line
481 for (cp
= boot_command_line
; *cp
; ) {
482 if (memcmp(cp
, "mem=", 4) == 0) {
483 mem_limit
= memparse(cp
+ 4, &cp
);
484 } else if (memcmp(cp
, "max_addr=", 9) == 0) {
485 max_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
486 } else if (memcmp(cp
, "min_addr=", 9) == 0) {
487 min_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
489 while (*cp
!= ' ' && *cp
)
496 printk(KERN_INFO
"Ignoring memory below %lluMB\n",
498 if (max_addr
!= ~0UL)
499 printk(KERN_INFO
"Ignoring memory above %lluMB\n",
502 efi
.systab
= __va(ia64_boot_param
->efi_systab
);
505 * Verify the EFI Table
507 if (efi
.systab
== NULL
)
508 panic("Whoa! Can't find EFI system table.\n");
509 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
510 panic("Whoa! EFI system table signature incorrect\n");
511 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
512 printk(KERN_WARNING
"Warning: EFI system table version "
513 "%d.%02d, expected 1.00 or greater\n",
514 efi
.systab
->hdr
.revision
>> 16,
515 efi
.systab
->hdr
.revision
& 0xffff);
517 config_tables
= __va(efi
.systab
->tables
);
519 /* Show what we know for posterity */
520 c16
= __va(efi
.systab
->fw_vendor
);
522 for (i
= 0;i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
527 printk(KERN_INFO
"EFI v%u.%.02u by %s:",
528 efi
.systab
->hdr
.revision
>> 16,
529 efi
.systab
->hdr
.revision
& 0xffff, vendor
);
531 efi
.mps
= EFI_INVALID_TABLE_ADDR
;
532 efi
.acpi
= EFI_INVALID_TABLE_ADDR
;
533 efi
.acpi20
= EFI_INVALID_TABLE_ADDR
;
534 efi
.smbios
= EFI_INVALID_TABLE_ADDR
;
535 efi
.sal_systab
= EFI_INVALID_TABLE_ADDR
;
536 efi
.boot_info
= EFI_INVALID_TABLE_ADDR
;
537 efi
.hcdp
= EFI_INVALID_TABLE_ADDR
;
538 efi
.uga
= EFI_INVALID_TABLE_ADDR
;
540 palo_phys
= EFI_INVALID_TABLE_ADDR
;
542 for (i
= 0; i
< (int) efi
.systab
->nr_tables
; i
++) {
543 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
544 efi
.mps
= config_tables
[i
].table
;
545 printk(" MPS=0x%lx", config_tables
[i
].table
);
546 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
547 efi
.acpi20
= config_tables
[i
].table
;
548 printk(" ACPI 2.0=0x%lx", config_tables
[i
].table
);
549 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
550 efi
.acpi
= config_tables
[i
].table
;
551 printk(" ACPI=0x%lx", config_tables
[i
].table
);
552 } else if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
553 efi
.smbios
= config_tables
[i
].table
;
554 printk(" SMBIOS=0x%lx", config_tables
[i
].table
);
555 } else if (efi_guidcmp(config_tables
[i
].guid
, SAL_SYSTEM_TABLE_GUID
) == 0) {
556 efi
.sal_systab
= config_tables
[i
].table
;
557 printk(" SALsystab=0x%lx", config_tables
[i
].table
);
558 } else if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
559 efi
.hcdp
= config_tables
[i
].table
;
560 printk(" HCDP=0x%lx", config_tables
[i
].table
);
561 } else if (efi_guidcmp(config_tables
[i
].guid
,
562 PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID
) == 0) {
563 palo_phys
= config_tables
[i
].table
;
564 printk(" PALO=0x%lx", config_tables
[i
].table
);
569 if (palo_phys
!= EFI_INVALID_TABLE_ADDR
)
570 handle_palo(palo_phys
);
572 runtime
= __va(efi
.systab
->runtime
);
573 efi
.get_time
= phys_get_time
;
574 efi
.set_time
= phys_set_time
;
575 efi
.get_wakeup_time
= phys_get_wakeup_time
;
576 efi
.set_wakeup_time
= phys_set_wakeup_time
;
577 efi
.get_variable
= phys_get_variable
;
578 efi
.get_next_variable
= phys_get_next_variable
;
579 efi
.set_variable
= phys_set_variable
;
580 efi
.get_next_high_mono_count
= phys_get_next_high_mono_count
;
581 efi
.reset_system
= phys_reset_system
;
583 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
584 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
585 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
588 /* print EFI memory map: */
590 efi_memory_desc_t
*md
;
593 for (i
= 0, p
= efi_map_start
; p
< efi_map_end
;
594 ++i
, p
+= efi_desc_size
)
600 size
= md
->num_pages
<< EFI_PAGE_SHIFT
;
602 if ((size
>> 40) > 0) {
605 } else if ((size
>> 30) > 0) {
608 } else if ((size
>> 20) > 0) {
616 printk("mem%02d: type=%2u, attr=0x%016lx, "
617 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
618 i
, md
->type
, md
->attribute
, md
->phys_addr
,
619 md
->phys_addr
+ efi_md_size(md
), size
, unit
);
625 efi_enter_virtual_mode();
629 efi_enter_virtual_mode (void)
631 void *efi_map_start
, *efi_map_end
, *p
;
632 efi_memory_desc_t
*md
;
636 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
637 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
638 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
640 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
642 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
644 * Some descriptors have multiple bits set, so the
645 * order of the tests is relevant.
647 if (md
->attribute
& EFI_MEMORY_WB
) {
648 md
->virt_addr
= (u64
) __va(md
->phys_addr
);
649 } else if (md
->attribute
& EFI_MEMORY_UC
) {
650 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
651 } else if (md
->attribute
& EFI_MEMORY_WC
) {
653 md
->virt_addr
= ia64_remap(md
->phys_addr
,
661 printk(KERN_INFO
"EFI_MEMORY_WC mapping\n");
662 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
664 } else if (md
->attribute
& EFI_MEMORY_WT
) {
666 md
->virt_addr
= ia64_remap(md
->phys_addr
,
674 printk(KERN_INFO
"EFI_MEMORY_WT mapping\n");
675 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
681 status
= efi_call_phys(__va(runtime
->set_virtual_address_map
),
682 ia64_boot_param
->efi_memmap_size
,
684 ia64_boot_param
->efi_memdesc_version
,
685 ia64_boot_param
->efi_memmap
);
686 if (status
!= EFI_SUCCESS
) {
687 printk(KERN_WARNING
"warning: unable to switch EFI into "
688 "virtual mode (status=%lu)\n", status
);
693 * Now that EFI is in virtual mode, we call the EFI functions more
696 efi
.get_time
= virt_get_time
;
697 efi
.set_time
= virt_set_time
;
698 efi
.get_wakeup_time
= virt_get_wakeup_time
;
699 efi
.set_wakeup_time
= virt_set_wakeup_time
;
700 efi
.get_variable
= virt_get_variable
;
701 efi
.get_next_variable
= virt_get_next_variable
;
702 efi
.set_variable
= virt_set_variable
;
703 efi
.get_next_high_mono_count
= virt_get_next_high_mono_count
;
704 efi
.reset_system
= virt_reset_system
;
708 * Walk the EFI memory map looking for the I/O port range. There can only be
709 * one entry of this type, other I/O port ranges should be described via ACPI.
712 efi_get_iobase (void)
714 void *efi_map_start
, *efi_map_end
, *p
;
715 efi_memory_desc_t
*md
;
718 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
719 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
720 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
722 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
724 if (md
->type
== EFI_MEMORY_MAPPED_IO_PORT_SPACE
) {
725 if (md
->attribute
& EFI_MEMORY_UC
)
726 return md
->phys_addr
;
732 static struct kern_memdesc
*
733 kern_memory_descriptor (unsigned long phys_addr
)
735 struct kern_memdesc
*md
;
737 for (md
= kern_memmap
; md
->start
!= ~0UL; md
++) {
738 if (phys_addr
- md
->start
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
744 static efi_memory_desc_t
*
745 efi_memory_descriptor (unsigned long phys_addr
)
747 void *efi_map_start
, *efi_map_end
, *p
;
748 efi_memory_desc_t
*md
;
751 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
752 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
753 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
755 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
758 if (phys_addr
- md
->phys_addr
< efi_md_size(md
))
765 efi_memmap_intersects (unsigned long phys_addr
, unsigned long size
)
767 void *efi_map_start
, *efi_map_end
, *p
;
768 efi_memory_desc_t
*md
;
772 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
773 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
774 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
776 end
= phys_addr
+ size
;
778 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
780 if (md
->phys_addr
< end
&& efi_md_end(md
) > phys_addr
)
787 efi_mem_type (unsigned long phys_addr
)
789 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
797 efi_mem_attributes (unsigned long phys_addr
)
799 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
802 return md
->attribute
;
805 EXPORT_SYMBOL(efi_mem_attributes
);
808 efi_mem_attribute (unsigned long phys_addr
, unsigned long size
)
810 unsigned long end
= phys_addr
+ size
;
811 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
818 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
819 * the kernel that firmware needs this region mapped.
821 attr
= md
->attribute
& ~EFI_MEMORY_RUNTIME
;
823 unsigned long md_end
= efi_md_end(md
);
828 md
= efi_memory_descriptor(md_end
);
829 if (!md
|| (md
->attribute
& ~EFI_MEMORY_RUNTIME
) != attr
)
832 return 0; /* never reached */
836 kern_mem_attribute (unsigned long phys_addr
, unsigned long size
)
838 unsigned long end
= phys_addr
+ size
;
839 struct kern_memdesc
*md
;
843 * This is a hack for ioremap calls before we set up kern_memmap.
844 * Maybe we should do efi_memmap_init() earlier instead.
847 attr
= efi_mem_attribute(phys_addr
, size
);
848 if (attr
& EFI_MEMORY_WB
)
849 return EFI_MEMORY_WB
;
853 md
= kern_memory_descriptor(phys_addr
);
857 attr
= md
->attribute
;
859 unsigned long md_end
= kmd_end(md
);
864 md
= kern_memory_descriptor(md_end
);
865 if (!md
|| md
->attribute
!= attr
)
868 return 0; /* never reached */
870 EXPORT_SYMBOL(kern_mem_attribute
);
873 valid_phys_addr_range (phys_addr_t phys_addr
, unsigned long size
)
878 * /dev/mem reads and writes use copy_to_user(), which implicitly
879 * uses a granule-sized kernel identity mapping. It's really
880 * only safe to do this for regions in kern_memmap. For more
881 * details, see Documentation/ia64/aliasing.txt.
883 attr
= kern_mem_attribute(phys_addr
, size
);
884 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
890 valid_mmap_phys_addr_range (unsigned long pfn
, unsigned long size
)
892 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
895 attr
= efi_mem_attribute(phys_addr
, size
);
898 * /dev/mem mmap uses normal user pages, so we don't need the entire
899 * granule, but the entire region we're mapping must support the same
902 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
906 * Intel firmware doesn't tell us about all the MMIO regions, so
907 * in general we have to allow mmap requests. But if EFI *does*
908 * tell us about anything inside this region, we should deny it.
909 * The user can always map a smaller region to avoid the overlap.
911 if (efi_memmap_intersects(phys_addr
, size
))
918 phys_mem_access_prot(struct file
*file
, unsigned long pfn
, unsigned long size
,
921 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
925 * For /dev/mem mmap, we use user mappings, but if the region is
926 * in kern_memmap (and hence may be covered by a kernel mapping),
927 * we must use the same attribute as the kernel mapping.
929 attr
= kern_mem_attribute(phys_addr
, size
);
930 if (attr
& EFI_MEMORY_WB
)
931 return pgprot_cacheable(vma_prot
);
932 else if (attr
& EFI_MEMORY_UC
)
933 return pgprot_noncached(vma_prot
);
936 * Some chipsets don't support UC access to memory. If
937 * WB is supported, we prefer that.
939 if (efi_mem_attribute(phys_addr
, size
) & EFI_MEMORY_WB
)
940 return pgprot_cacheable(vma_prot
);
942 return pgprot_noncached(vma_prot
);
946 efi_uart_console_only(void)
949 char *s
, name
[] = "ConOut";
950 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
951 efi_char16_t
*utf16
, name_utf16
[32];
952 unsigned char data
[1024];
953 unsigned long size
= sizeof(data
);
954 struct efi_generic_dev_path
*hdr
, *end_addr
;
957 /* Convert to UTF-16 */
961 *utf16
++ = *s
++ & 0x7f;
964 status
= efi
.get_variable(name_utf16
, &guid
, NULL
, &size
, data
);
965 if (status
!= EFI_SUCCESS
) {
966 printk(KERN_ERR
"No EFI %s variable?\n", name
);
970 hdr
= (struct efi_generic_dev_path
*) data
;
971 end_addr
= (struct efi_generic_dev_path
*) ((u8
*) data
+ size
);
972 while (hdr
< end_addr
) {
973 if (hdr
->type
== EFI_DEV_MSG
&&
974 hdr
->sub_type
== EFI_DEV_MSG_UART
)
976 else if (hdr
->type
== EFI_DEV_END_PATH
||
977 hdr
->type
== EFI_DEV_END_PATH2
) {
980 if (hdr
->sub_type
== EFI_DEV_END_ENTIRE
)
984 hdr
= (struct efi_generic_dev_path
*)((u8
*) hdr
+ hdr
->length
);
986 printk(KERN_ERR
"Malformed %s value\n", name
);
991 * Look for the first granule aligned memory descriptor memory
992 * that is big enough to hold EFI memory map. Make sure this
993 * descriptor is atleast granule sized so it does not get trimmed
995 struct kern_memdesc
*
996 find_memmap_space (void)
998 u64 contig_low
=0, contig_high
=0;
1000 void *efi_map_start
, *efi_map_end
, *p
, *q
;
1001 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
1002 u64 space_needed
, efi_desc_size
;
1003 unsigned long total_mem
= 0;
1005 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1006 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1007 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1010 * Worst case: we need 3 kernel descriptors for each efi descriptor
1011 * (if every entry has a WB part in the middle, and UC head and tail),
1012 * plus one for the end marker.
1014 space_needed
= sizeof(kern_memdesc_t
) *
1015 (3 * (ia64_boot_param
->efi_memmap_size
/efi_desc_size
) + 1);
1017 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
1022 if (pmd
== NULL
|| !efi_wb(pmd
) ||
1023 efi_md_end(pmd
) != md
->phys_addr
) {
1024 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
1025 contig_high
= efi_md_end(md
);
1026 for (q
= p
+ efi_desc_size
; q
< efi_map_end
;
1027 q
+= efi_desc_size
) {
1029 if (!efi_wb(check_md
))
1031 if (contig_high
!= check_md
->phys_addr
)
1033 contig_high
= efi_md_end(check_md
);
1035 contig_high
= GRANULEROUNDDOWN(contig_high
);
1037 if (!is_memory_available(md
) || md
->type
== EFI_LOADER_DATA
)
1040 /* Round ends inward to granule boundaries */
1041 as
= max(contig_low
, md
->phys_addr
);
1042 ae
= min(contig_high
, efi_md_end(md
));
1044 /* keep within max_addr= and min_addr= command line arg */
1045 as
= max(as
, min_addr
);
1046 ae
= min(ae
, max_addr
);
1050 /* avoid going over mem= command line arg */
1051 if (total_mem
+ (ae
- as
) > mem_limit
)
1052 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1057 if (ae
- as
> space_needed
)
1060 if (p
>= efi_map_end
)
1061 panic("Can't allocate space for kernel memory descriptors");
1067 * Walk the EFI memory map and gather all memory available for kernel
1068 * to use. We can allocate partial granules only if the unavailable
1069 * parts exist, and are WB.
1072 efi_memmap_init(u64
*s
, u64
*e
)
1074 struct kern_memdesc
*k
, *prev
= NULL
;
1075 u64 contig_low
=0, contig_high
=0;
1077 void *efi_map_start
, *efi_map_end
, *p
, *q
;
1078 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
1080 unsigned long total_mem
= 0;
1082 k
= kern_memmap
= find_memmap_space();
1084 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1085 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1086 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1088 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
1092 (md
->type
== EFI_CONVENTIONAL_MEMORY
||
1093 md
->type
== EFI_BOOT_SERVICES_DATA
)) {
1094 k
->attribute
= EFI_MEMORY_UC
;
1095 k
->start
= md
->phys_addr
;
1096 k
->num_pages
= md
->num_pages
;
1101 if (pmd
== NULL
|| !efi_wb(pmd
) ||
1102 efi_md_end(pmd
) != md
->phys_addr
) {
1103 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
1104 contig_high
= efi_md_end(md
);
1105 for (q
= p
+ efi_desc_size
; q
< efi_map_end
;
1106 q
+= efi_desc_size
) {
1108 if (!efi_wb(check_md
))
1110 if (contig_high
!= check_md
->phys_addr
)
1112 contig_high
= efi_md_end(check_md
);
1114 contig_high
= GRANULEROUNDDOWN(contig_high
);
1116 if (!is_memory_available(md
))
1120 * Round ends inward to granule boundaries
1121 * Give trimmings to uncached allocator
1123 if (md
->phys_addr
< contig_low
) {
1124 lim
= min(efi_md_end(md
), contig_low
);
1126 if (k
> kern_memmap
&&
1127 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1128 kmd_end(k
-1) == md
->phys_addr
) {
1130 (lim
- md
->phys_addr
)
1133 k
->attribute
= EFI_MEMORY_UC
;
1134 k
->start
= md
->phys_addr
;
1135 k
->num_pages
= (lim
- md
->phys_addr
)
1144 if (efi_md_end(md
) > contig_high
) {
1145 lim
= max(md
->phys_addr
, contig_high
);
1147 if (lim
== md
->phys_addr
&& k
> kern_memmap
&&
1148 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1149 kmd_end(k
-1) == md
->phys_addr
) {
1150 (k
-1)->num_pages
+= md
->num_pages
;
1152 k
->attribute
= EFI_MEMORY_UC
;
1154 k
->num_pages
= (efi_md_end(md
) - lim
)
1161 ae
= efi_md_end(md
);
1163 /* keep within max_addr= and min_addr= command line arg */
1164 as
= max(as
, min_addr
);
1165 ae
= min(ae
, max_addr
);
1169 /* avoid going over mem= command line arg */
1170 if (total_mem
+ (ae
- as
) > mem_limit
)
1171 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1175 if (prev
&& kmd_end(prev
) == md
->phys_addr
) {
1176 prev
->num_pages
+= (ae
- as
) >> EFI_PAGE_SHIFT
;
1177 total_mem
+= ae
- as
;
1180 k
->attribute
= EFI_MEMORY_WB
;
1182 k
->num_pages
= (ae
- as
) >> EFI_PAGE_SHIFT
;
1183 total_mem
+= ae
- as
;
1186 k
->start
= ~0L; /* end-marker */
1188 /* reserve the memory we are using for kern_memmap */
1189 *s
= (u64
)kern_memmap
;
1196 efi_initialize_iomem_resources(struct resource
*code_resource
,
1197 struct resource
*data_resource
,
1198 struct resource
*bss_resource
)
1200 struct resource
*res
;
1201 void *efi_map_start
, *efi_map_end
, *p
;
1202 efi_memory_desc_t
*md
;
1205 unsigned long flags
;
1207 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1208 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1209 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1213 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1216 if (md
->num_pages
== 0) /* should not happen */
1219 flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
1222 case EFI_MEMORY_MAPPED_IO
:
1223 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1226 case EFI_LOADER_CODE
:
1227 case EFI_LOADER_DATA
:
1228 case EFI_BOOT_SERVICES_DATA
:
1229 case EFI_BOOT_SERVICES_CODE
:
1230 case EFI_CONVENTIONAL_MEMORY
:
1231 if (md
->attribute
& EFI_MEMORY_WP
) {
1232 name
= "System ROM";
1233 flags
|= IORESOURCE_READONLY
;
1234 } else if (md
->attribute
== EFI_MEMORY_UC
)
1235 name
= "Uncached RAM";
1237 name
= "System RAM";
1240 case EFI_ACPI_MEMORY_NVS
:
1241 name
= "ACPI Non-volatile Storage";
1244 case EFI_UNUSABLE_MEMORY
:
1246 flags
|= IORESOURCE_DISABLED
;
1249 case EFI_RESERVED_TYPE
:
1250 case EFI_RUNTIME_SERVICES_CODE
:
1251 case EFI_RUNTIME_SERVICES_DATA
:
1252 case EFI_ACPI_RECLAIM_MEMORY
:
1258 if ((res
= kzalloc(sizeof(struct resource
),
1259 GFP_KERNEL
)) == NULL
) {
1261 "failed to allocate resource for iomem\n");
1266 res
->start
= md
->phys_addr
;
1267 res
->end
= md
->phys_addr
+ efi_md_size(md
) - 1;
1270 if (insert_resource(&iomem_resource
, res
) < 0)
1274 * We don't know which region contains
1275 * kernel data so we try it repeatedly and
1276 * let the resource manager test it.
1278 insert_resource(res
, code_resource
);
1279 insert_resource(res
, data_resource
);
1280 insert_resource(res
, bss_resource
);
1282 insert_resource(res
, &efi_memmap_res
);
1283 insert_resource(res
, &boot_param_res
);
1284 if (crashk_res
.end
> crashk_res
.start
)
1285 insert_resource(res
, &crashk_res
);
1292 /* find a block of memory aligned to 64M exclude reserved regions
1293 rsvd_regions are sorted
1295 unsigned long __init
1296 kdump_find_rsvd_region (unsigned long size
, struct rsvd_region
*r
, int n
)
1300 u64 alignment
= 1UL << _PAGE_SIZE_64M
;
1301 void *efi_map_start
, *efi_map_end
, *p
;
1302 efi_memory_desc_t
*md
;
1305 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1306 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1307 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1309 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1313 start
= ALIGN(md
->phys_addr
, alignment
);
1314 end
= efi_md_end(md
);
1315 for (i
= 0; i
< n
; i
++) {
1316 if (__pa(r
[i
].start
) >= start
&& __pa(r
[i
].end
) < end
) {
1317 if (__pa(r
[i
].start
) > start
+ size
)
1319 start
= ALIGN(__pa(r
[i
].end
), alignment
);
1321 __pa(r
[i
+1].start
) < start
+ size
)
1327 if (end
> start
+ size
)
1332 "Cannot reserve 0x%lx byte of memory for crashdump\n", size
);
1337 #ifdef CONFIG_CRASH_DUMP
1338 /* locate the size find a the descriptor at a certain address */
1339 unsigned long __init
1340 vmcore_find_descriptor_size (unsigned long address
)
1342 void *efi_map_start
, *efi_map_end
, *p
;
1343 efi_memory_desc_t
*md
;
1345 unsigned long ret
= 0;
1347 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1348 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1349 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1351 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1353 if (efi_wb(md
) && md
->type
== EFI_LOADER_DATA
1354 && md
->phys_addr
== address
) {
1355 ret
= efi_md_size(md
);
1361 printk(KERN_WARNING
"Cannot locate EFI vmcore descriptor\n");