2 * Extensible Firmware Interface
4 * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
6 * Copyright (C) 1999 VA Linux Systems
7 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
8 * Copyright (C) 1999-2003 Hewlett-Packard Co.
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 * Stephane Eranian <eranian@hpl.hp.com>
11 * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
12 * Bjorn Helgaas <bjorn.helgaas@hp.com>
14 * All EFI Runtime Services are not implemented yet as EFI only
15 * supports physical mode addressing on SoftSDV. This is to be fixed
16 * in a future version. --drummond 1999-07-20
18 * Implemented EFI runtime services and virtual mode calls. --davidm
20 * Goutham Rao: <goutham.rao@intel.com>
21 * Skip non-WB memory and ignore empty memory ranges.
23 #include <linux/module.h>
24 #include <linux/bootmem.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/time.h>
29 #include <linux/efi.h>
30 #include <linux/kexec.h>
33 #include <asm/kregs.h>
34 #include <asm/meminit.h>
35 #include <asm/pgtable.h>
36 #include <asm/processor.h>
41 extern efi_status_t
efi_call_phys (void *, ...);
45 static efi_runtime_services_t
*runtime
;
46 static unsigned long mem_limit
= ~0UL, max_addr
= ~0UL, min_addr
= 0UL;
48 #define efi_call_virt(f, args...) (*(f))(args)
50 #define STUB_GET_TIME(prefix, adjust_arg) \
52 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
54 struct ia64_fpreg fr[6]; \
55 efi_time_cap_t *atc = NULL; \
59 atc = adjust_arg(tc); \
60 ia64_save_scratch_fpregs(fr); \
61 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
62 ia64_load_scratch_fpregs(fr); \
66 #define STUB_SET_TIME(prefix, adjust_arg) \
68 prefix##_set_time (efi_time_t *tm) \
70 struct ia64_fpreg fr[6]; \
73 ia64_save_scratch_fpregs(fr); \
74 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
75 ia64_load_scratch_fpregs(fr); \
79 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
81 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
83 struct ia64_fpreg fr[6]; \
86 ia64_save_scratch_fpregs(fr); \
87 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
88 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
89 ia64_load_scratch_fpregs(fr); \
93 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
95 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
97 struct ia64_fpreg fr[6]; \
98 efi_time_t *atm = NULL; \
102 atm = adjust_arg(tm); \
103 ia64_save_scratch_fpregs(fr); \
104 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
106 ia64_load_scratch_fpregs(fr); \
110 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
111 static efi_status_t \
112 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
113 unsigned long *data_size, void *data) \
115 struct ia64_fpreg fr[6]; \
120 aattr = adjust_arg(attr); \
121 ia64_save_scratch_fpregs(fr); \
122 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
123 adjust_arg(name), adjust_arg(vendor), aattr, \
124 adjust_arg(data_size), adjust_arg(data)); \
125 ia64_load_scratch_fpregs(fr); \
129 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
130 static efi_status_t \
131 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
133 struct ia64_fpreg fr[6]; \
136 ia64_save_scratch_fpregs(fr); \
137 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
138 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
139 ia64_load_scratch_fpregs(fr); \
143 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
144 static efi_status_t \
145 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
146 unsigned long data_size, void *data) \
148 struct ia64_fpreg fr[6]; \
151 ia64_save_scratch_fpregs(fr); \
152 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
153 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
155 ia64_load_scratch_fpregs(fr); \
159 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
160 static efi_status_t \
161 prefix##_get_next_high_mono_count (u32 *count) \
163 struct ia64_fpreg fr[6]; \
166 ia64_save_scratch_fpregs(fr); \
167 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
168 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
169 ia64_load_scratch_fpregs(fr); \
173 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
175 prefix##_reset_system (int reset_type, efi_status_t status, \
176 unsigned long data_size, efi_char16_t *data) \
178 struct ia64_fpreg fr[6]; \
179 efi_char16_t *adata = NULL; \
182 adata = adjust_arg(data); \
184 ia64_save_scratch_fpregs(fr); \
185 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
186 reset_type, status, data_size, adata); \
187 /* should not return, but just in case... */ \
188 ia64_load_scratch_fpregs(fr); \
191 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
193 STUB_GET_TIME(phys
, phys_ptr
)
194 STUB_SET_TIME(phys
, phys_ptr
)
195 STUB_GET_WAKEUP_TIME(phys
, phys_ptr
)
196 STUB_SET_WAKEUP_TIME(phys
, phys_ptr
)
197 STUB_GET_VARIABLE(phys
, phys_ptr
)
198 STUB_GET_NEXT_VARIABLE(phys
, phys_ptr
)
199 STUB_SET_VARIABLE(phys
, phys_ptr
)
200 STUB_GET_NEXT_HIGH_MONO_COUNT(phys
, phys_ptr
)
201 STUB_RESET_SYSTEM(phys
, phys_ptr
)
205 STUB_GET_TIME(virt
, id
)
206 STUB_SET_TIME(virt
, id
)
207 STUB_GET_WAKEUP_TIME(virt
, id
)
208 STUB_SET_WAKEUP_TIME(virt
, id
)
209 STUB_GET_VARIABLE(virt
, id
)
210 STUB_GET_NEXT_VARIABLE(virt
, id
)
211 STUB_SET_VARIABLE(virt
, id
)
212 STUB_GET_NEXT_HIGH_MONO_COUNT(virt
, id
)
213 STUB_RESET_SYSTEM(virt
, id
)
216 efi_gettimeofday (struct timespec
*ts
)
220 memset(ts
, 0, sizeof(ts
));
221 if ((*efi
.get_time
)(&tm
, NULL
) != EFI_SUCCESS
)
224 ts
->tv_sec
= mktime(tm
.year
, tm
.month
, tm
.day
, tm
.hour
, tm
.minute
, tm
.second
);
225 ts
->tv_nsec
= tm
.nanosecond
;
229 is_memory_available (efi_memory_desc_t
*md
)
231 if (!(md
->attribute
& EFI_MEMORY_WB
))
235 case EFI_LOADER_CODE
:
236 case EFI_LOADER_DATA
:
237 case EFI_BOOT_SERVICES_CODE
:
238 case EFI_BOOT_SERVICES_DATA
:
239 case EFI_CONVENTIONAL_MEMORY
:
245 typedef struct kern_memdesc
{
251 static kern_memdesc_t
*kern_memmap
;
253 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
256 kmd_end(kern_memdesc_t
*kmd
)
258 return (kmd
->start
+ (kmd
->num_pages
<< EFI_PAGE_SHIFT
));
262 efi_md_end(efi_memory_desc_t
*md
)
264 return (md
->phys_addr
+ efi_md_size(md
));
268 efi_wb(efi_memory_desc_t
*md
)
270 return (md
->attribute
& EFI_MEMORY_WB
);
274 efi_uc(efi_memory_desc_t
*md
)
276 return (md
->attribute
& EFI_MEMORY_UC
);
280 walk (efi_freemem_callback_t callback
, void *arg
, u64 attr
)
283 u64 start
, end
, voff
;
285 voff
= (attr
== EFI_MEMORY_WB
) ? PAGE_OFFSET
: __IA64_UNCACHED_OFFSET
;
286 for (k
= kern_memmap
; k
->start
!= ~0UL; k
++) {
287 if (k
->attribute
!= attr
)
289 start
= PAGE_ALIGN(k
->start
);
290 end
= (k
->start
+ (k
->num_pages
<< EFI_PAGE_SHIFT
)) & PAGE_MASK
;
292 if ((*callback
)(start
+ voff
, end
+ voff
, arg
) < 0)
298 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
299 * has memory that is available for OS use.
302 efi_memmap_walk (efi_freemem_callback_t callback
, void *arg
)
304 walk(callback
, arg
, EFI_MEMORY_WB
);
308 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
309 * has memory that is available for uncached allocator.
312 efi_memmap_walk_uc (efi_freemem_callback_t callback
, void *arg
)
314 walk(callback
, arg
, EFI_MEMORY_UC
);
318 * Look for the PAL_CODE region reported by EFI and maps it using an
319 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
320 * Abstraction Layer chapter 11 in ADAG
324 efi_get_pal_addr (void)
326 void *efi_map_start
, *efi_map_end
, *p
;
327 efi_memory_desc_t
*md
;
329 int pal_code_count
= 0;
332 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
333 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
334 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
336 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
338 if (md
->type
!= EFI_PAL_CODE
)
341 if (++pal_code_count
> 1) {
342 printk(KERN_ERR
"Too many EFI Pal Code memory ranges, dropped @ %lx\n",
347 * The only ITLB entry in region 7 that is used is the one installed by
348 * __start(). That entry covers a 64MB range.
350 mask
= ~((1 << KERNEL_TR_PAGE_SHIFT
) - 1);
351 vaddr
= PAGE_OFFSET
+ md
->phys_addr
;
354 * We must check that the PAL mapping won't overlap with the kernel
357 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
358 * 256KB and that only one ITR is needed to map it. This implies that the
359 * PAL code is always aligned on its size, i.e., the closest matching page
360 * size supported by the TLB. Therefore PAL code is guaranteed never to
361 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
362 * now the following test is enough to determine whether or not we need a
363 * dedicated ITR for the PAL code.
365 if ((vaddr
& mask
) == (KERNEL_START
& mask
)) {
366 printk(KERN_INFO
"%s: no need to install ITR for PAL code\n",
371 if (md
->num_pages
<< EFI_PAGE_SHIFT
> IA64_GRANULE_SIZE
)
372 panic("Woah! PAL code size bigger than a granule!");
375 mask
= ~((1 << IA64_GRANULE_SHIFT
) - 1);
377 printk(KERN_INFO
"CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
378 smp_processor_id(), md
->phys_addr
,
379 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
380 vaddr
& mask
, (vaddr
& mask
) + IA64_GRANULE_SIZE
);
382 return __va(md
->phys_addr
);
384 printk(KERN_WARNING
"%s: no PAL-code memory-descriptor found\n",
390 efi_map_pal_code (void)
392 void *pal_vaddr
= efi_get_pal_addr ();
399 * Cannot write to CRx with PSR.ic=1
401 psr
= ia64_clear_ic();
402 ia64_itr(0x1, IA64_TR_PALCODE
, GRANULEROUNDDOWN((unsigned long) pal_vaddr
),
403 pte_val(pfn_pte(__pa(pal_vaddr
) >> PAGE_SHIFT
, PAGE_KERNEL
)),
405 ia64_set_psr(psr
); /* restore psr */
412 void *efi_map_start
, *efi_map_end
;
413 efi_config_table_t
*config_tables
;
416 char *cp
, vendor
[100] = "unknown";
419 /* it's too early to be able to use the standard kernel command line support... */
420 for (cp
= boot_command_line
; *cp
; ) {
421 if (memcmp(cp
, "mem=", 4) == 0) {
422 mem_limit
= memparse(cp
+ 4, &cp
);
423 } else if (memcmp(cp
, "max_addr=", 9) == 0) {
424 max_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
425 } else if (memcmp(cp
, "min_addr=", 9) == 0) {
426 min_addr
= GRANULEROUNDDOWN(memparse(cp
+ 9, &cp
));
428 while (*cp
!= ' ' && *cp
)
435 printk(KERN_INFO
"Ignoring memory below %luMB\n", min_addr
>> 20);
436 if (max_addr
!= ~0UL)
437 printk(KERN_INFO
"Ignoring memory above %luMB\n", max_addr
>> 20);
439 efi
.systab
= __va(ia64_boot_param
->efi_systab
);
442 * Verify the EFI Table
444 if (efi
.systab
== NULL
)
445 panic("Woah! Can't find EFI system table.\n");
446 if (efi
.systab
->hdr
.signature
!= EFI_SYSTEM_TABLE_SIGNATURE
)
447 panic("Woah! EFI system table signature incorrect\n");
448 if ((efi
.systab
->hdr
.revision
>> 16) == 0)
449 printk(KERN_WARNING
"Warning: EFI system table version "
450 "%d.%02d, expected 1.00 or greater\n",
451 efi
.systab
->hdr
.revision
>> 16,
452 efi
.systab
->hdr
.revision
& 0xffff);
454 config_tables
= __va(efi
.systab
->tables
);
456 /* Show what we know for posterity */
457 c16
= __va(efi
.systab
->fw_vendor
);
459 for (i
= 0;i
< (int) sizeof(vendor
) - 1 && *c16
; ++i
)
464 printk(KERN_INFO
"EFI v%u.%.02u by %s:",
465 efi
.systab
->hdr
.revision
>> 16, efi
.systab
->hdr
.revision
& 0xffff, vendor
);
467 efi
.mps
= EFI_INVALID_TABLE_ADDR
;
468 efi
.acpi
= EFI_INVALID_TABLE_ADDR
;
469 efi
.acpi20
= EFI_INVALID_TABLE_ADDR
;
470 efi
.smbios
= EFI_INVALID_TABLE_ADDR
;
471 efi
.sal_systab
= EFI_INVALID_TABLE_ADDR
;
472 efi
.boot_info
= EFI_INVALID_TABLE_ADDR
;
473 efi
.hcdp
= EFI_INVALID_TABLE_ADDR
;
474 efi
.uga
= EFI_INVALID_TABLE_ADDR
;
476 for (i
= 0; i
< (int) efi
.systab
->nr_tables
; i
++) {
477 if (efi_guidcmp(config_tables
[i
].guid
, MPS_TABLE_GUID
) == 0) {
478 efi
.mps
= config_tables
[i
].table
;
479 printk(" MPS=0x%lx", config_tables
[i
].table
);
480 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_20_TABLE_GUID
) == 0) {
481 efi
.acpi20
= config_tables
[i
].table
;
482 printk(" ACPI 2.0=0x%lx", config_tables
[i
].table
);
483 } else if (efi_guidcmp(config_tables
[i
].guid
, ACPI_TABLE_GUID
) == 0) {
484 efi
.acpi
= config_tables
[i
].table
;
485 printk(" ACPI=0x%lx", config_tables
[i
].table
);
486 } else if (efi_guidcmp(config_tables
[i
].guid
, SMBIOS_TABLE_GUID
) == 0) {
487 efi
.smbios
= config_tables
[i
].table
;
488 printk(" SMBIOS=0x%lx", config_tables
[i
].table
);
489 } else if (efi_guidcmp(config_tables
[i
].guid
, SAL_SYSTEM_TABLE_GUID
) == 0) {
490 efi
.sal_systab
= config_tables
[i
].table
;
491 printk(" SALsystab=0x%lx", config_tables
[i
].table
);
492 } else if (efi_guidcmp(config_tables
[i
].guid
, HCDP_TABLE_GUID
) == 0) {
493 efi
.hcdp
= config_tables
[i
].table
;
494 printk(" HCDP=0x%lx", config_tables
[i
].table
);
499 runtime
= __va(efi
.systab
->runtime
);
500 efi
.get_time
= phys_get_time
;
501 efi
.set_time
= phys_set_time
;
502 efi
.get_wakeup_time
= phys_get_wakeup_time
;
503 efi
.set_wakeup_time
= phys_set_wakeup_time
;
504 efi
.get_variable
= phys_get_variable
;
505 efi
.get_next_variable
= phys_get_next_variable
;
506 efi
.set_variable
= phys_set_variable
;
507 efi
.get_next_high_mono_count
= phys_get_next_high_mono_count
;
508 efi
.reset_system
= phys_reset_system
;
510 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
511 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
512 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
515 /* print EFI memory map: */
517 efi_memory_desc_t
*md
;
520 for (i
= 0, p
= efi_map_start
; p
< efi_map_end
; ++i
, p
+= efi_desc_size
) {
522 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
523 i
, md
->type
, md
->attribute
, md
->phys_addr
,
524 md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
),
525 md
->num_pages
>> (20 - EFI_PAGE_SHIFT
));
531 efi_enter_virtual_mode();
535 efi_enter_virtual_mode (void)
537 void *efi_map_start
, *efi_map_end
, *p
;
538 efi_memory_desc_t
*md
;
542 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
543 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
544 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
546 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
548 if (md
->attribute
& EFI_MEMORY_RUNTIME
) {
550 * Some descriptors have multiple bits set, so the order of
551 * the tests is relevant.
553 if (md
->attribute
& EFI_MEMORY_WB
) {
554 md
->virt_addr
= (u64
) __va(md
->phys_addr
);
555 } else if (md
->attribute
& EFI_MEMORY_UC
) {
556 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
557 } else if (md
->attribute
& EFI_MEMORY_WC
) {
559 md
->virt_addr
= ia64_remap(md
->phys_addr
, (_PAGE_A
| _PAGE_P
565 printk(KERN_INFO
"EFI_MEMORY_WC mapping\n");
566 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
568 } else if (md
->attribute
& EFI_MEMORY_WT
) {
570 md
->virt_addr
= ia64_remap(md
->phys_addr
, (_PAGE_A
| _PAGE_P
571 | _PAGE_D
| _PAGE_MA_WT
575 printk(KERN_INFO
"EFI_MEMORY_WT mapping\n");
576 md
->virt_addr
= (u64
) ioremap(md
->phys_addr
, 0);
582 status
= efi_call_phys(__va(runtime
->set_virtual_address_map
),
583 ia64_boot_param
->efi_memmap_size
,
584 efi_desc_size
, ia64_boot_param
->efi_memdesc_version
,
585 ia64_boot_param
->efi_memmap
);
586 if (status
!= EFI_SUCCESS
) {
587 printk(KERN_WARNING
"warning: unable to switch EFI into virtual mode "
588 "(status=%lu)\n", status
);
593 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
595 efi
.get_time
= virt_get_time
;
596 efi
.set_time
= virt_set_time
;
597 efi
.get_wakeup_time
= virt_get_wakeup_time
;
598 efi
.set_wakeup_time
= virt_set_wakeup_time
;
599 efi
.get_variable
= virt_get_variable
;
600 efi
.get_next_variable
= virt_get_next_variable
;
601 efi
.set_variable
= virt_set_variable
;
602 efi
.get_next_high_mono_count
= virt_get_next_high_mono_count
;
603 efi
.reset_system
= virt_reset_system
;
607 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
608 * this type, other I/O port ranges should be described via ACPI.
611 efi_get_iobase (void)
613 void *efi_map_start
, *efi_map_end
, *p
;
614 efi_memory_desc_t
*md
;
617 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
618 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
619 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
621 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
623 if (md
->type
== EFI_MEMORY_MAPPED_IO_PORT_SPACE
) {
624 if (md
->attribute
& EFI_MEMORY_UC
)
625 return md
->phys_addr
;
631 static struct kern_memdesc
*
632 kern_memory_descriptor (unsigned long phys_addr
)
634 struct kern_memdesc
*md
;
636 for (md
= kern_memmap
; md
->start
!= ~0UL; md
++) {
637 if (phys_addr
- md
->start
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
643 static efi_memory_desc_t
*
644 efi_memory_descriptor (unsigned long phys_addr
)
646 void *efi_map_start
, *efi_map_end
, *p
;
647 efi_memory_desc_t
*md
;
650 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
651 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
652 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
654 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
657 if (phys_addr
- md
->phys_addr
< (md
->num_pages
<< EFI_PAGE_SHIFT
))
664 efi_memmap_intersects (unsigned long phys_addr
, unsigned long size
)
666 void *efi_map_start
, *efi_map_end
, *p
;
667 efi_memory_desc_t
*md
;
671 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
672 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
673 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
675 end
= phys_addr
+ size
;
677 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
680 if (md
->phys_addr
< end
&& efi_md_end(md
) > phys_addr
)
687 efi_mem_type (unsigned long phys_addr
)
689 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
697 efi_mem_attributes (unsigned long phys_addr
)
699 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
702 return md
->attribute
;
705 EXPORT_SYMBOL(efi_mem_attributes
);
708 efi_mem_attribute (unsigned long phys_addr
, unsigned long size
)
710 unsigned long end
= phys_addr
+ size
;
711 efi_memory_desc_t
*md
= efi_memory_descriptor(phys_addr
);
718 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
719 * the kernel that firmware needs this region mapped.
721 attr
= md
->attribute
& ~EFI_MEMORY_RUNTIME
;
723 unsigned long md_end
= efi_md_end(md
);
728 md
= efi_memory_descriptor(md_end
);
729 if (!md
|| (md
->attribute
& ~EFI_MEMORY_RUNTIME
) != attr
)
736 kern_mem_attribute (unsigned long phys_addr
, unsigned long size
)
738 unsigned long end
= phys_addr
+ size
;
739 struct kern_memdesc
*md
;
743 * This is a hack for ioremap calls before we set up kern_memmap.
744 * Maybe we should do efi_memmap_init() earlier instead.
747 attr
= efi_mem_attribute(phys_addr
, size
);
748 if (attr
& EFI_MEMORY_WB
)
749 return EFI_MEMORY_WB
;
753 md
= kern_memory_descriptor(phys_addr
);
757 attr
= md
->attribute
;
759 unsigned long md_end
= kmd_end(md
);
764 md
= kern_memory_descriptor(md_end
);
765 if (!md
|| md
->attribute
!= attr
)
770 EXPORT_SYMBOL(kern_mem_attribute
);
773 valid_phys_addr_range (unsigned long phys_addr
, unsigned long size
)
778 * /dev/mem reads and writes use copy_to_user(), which implicitly
779 * uses a granule-sized kernel identity mapping. It's really
780 * only safe to do this for regions in kern_memmap. For more
781 * details, see Documentation/ia64/aliasing.txt.
783 attr
= kern_mem_attribute(phys_addr
, size
);
784 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
790 valid_mmap_phys_addr_range (unsigned long pfn
, unsigned long size
)
792 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
795 attr
= efi_mem_attribute(phys_addr
, size
);
798 * /dev/mem mmap uses normal user pages, so we don't need the entire
799 * granule, but the entire region we're mapping must support the same
802 if (attr
& EFI_MEMORY_WB
|| attr
& EFI_MEMORY_UC
)
806 * Intel firmware doesn't tell us about all the MMIO regions, so
807 * in general we have to allow mmap requests. But if EFI *does*
808 * tell us about anything inside this region, we should deny it.
809 * The user can always map a smaller region to avoid the overlap.
811 if (efi_memmap_intersects(phys_addr
, size
))
818 phys_mem_access_prot(struct file
*file
, unsigned long pfn
, unsigned long size
,
821 unsigned long phys_addr
= pfn
<< PAGE_SHIFT
;
825 * For /dev/mem mmap, we use user mappings, but if the region is
826 * in kern_memmap (and hence may be covered by a kernel mapping),
827 * we must use the same attribute as the kernel mapping.
829 attr
= kern_mem_attribute(phys_addr
, size
);
830 if (attr
& EFI_MEMORY_WB
)
831 return pgprot_cacheable(vma_prot
);
832 else if (attr
& EFI_MEMORY_UC
)
833 return pgprot_noncached(vma_prot
);
836 * Some chipsets don't support UC access to memory. If
837 * WB is supported, we prefer that.
839 if (efi_mem_attribute(phys_addr
, size
) & EFI_MEMORY_WB
)
840 return pgprot_cacheable(vma_prot
);
842 return pgprot_noncached(vma_prot
);
846 efi_uart_console_only(void)
849 char *s
, name
[] = "ConOut";
850 efi_guid_t guid
= EFI_GLOBAL_VARIABLE_GUID
;
851 efi_char16_t
*utf16
, name_utf16
[32];
852 unsigned char data
[1024];
853 unsigned long size
= sizeof(data
);
854 struct efi_generic_dev_path
*hdr
, *end_addr
;
857 /* Convert to UTF-16 */
861 *utf16
++ = *s
++ & 0x7f;
864 status
= efi
.get_variable(name_utf16
, &guid
, NULL
, &size
, data
);
865 if (status
!= EFI_SUCCESS
) {
866 printk(KERN_ERR
"No EFI %s variable?\n", name
);
870 hdr
= (struct efi_generic_dev_path
*) data
;
871 end_addr
= (struct efi_generic_dev_path
*) ((u8
*) data
+ size
);
872 while (hdr
< end_addr
) {
873 if (hdr
->type
== EFI_DEV_MSG
&&
874 hdr
->sub_type
== EFI_DEV_MSG_UART
)
876 else if (hdr
->type
== EFI_DEV_END_PATH
||
877 hdr
->type
== EFI_DEV_END_PATH2
) {
880 if (hdr
->sub_type
== EFI_DEV_END_ENTIRE
)
884 hdr
= (struct efi_generic_dev_path
*) ((u8
*) hdr
+ hdr
->length
);
886 printk(KERN_ERR
"Malformed %s value\n", name
);
891 * Look for the first granule aligned memory descriptor memory
892 * that is big enough to hold EFI memory map. Make sure this
893 * descriptor is atleast granule sized so it does not get trimmed
895 struct kern_memdesc
*
896 find_memmap_space (void)
898 u64 contig_low
=0, contig_high
=0;
900 void *efi_map_start
, *efi_map_end
, *p
, *q
;
901 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
902 u64 space_needed
, efi_desc_size
;
903 unsigned long total_mem
= 0;
905 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
906 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
907 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
910 * Worst case: we need 3 kernel descriptors for each efi descriptor
911 * (if every entry has a WB part in the middle, and UC head and tail),
912 * plus one for the end marker.
914 space_needed
= sizeof(kern_memdesc_t
) *
915 (3 * (ia64_boot_param
->efi_memmap_size
/efi_desc_size
) + 1);
917 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
922 if (pmd
== NULL
|| !efi_wb(pmd
) || efi_md_end(pmd
) != md
->phys_addr
) {
923 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
924 contig_high
= efi_md_end(md
);
925 for (q
= p
+ efi_desc_size
; q
< efi_map_end
; q
+= efi_desc_size
) {
927 if (!efi_wb(check_md
))
929 if (contig_high
!= check_md
->phys_addr
)
931 contig_high
= efi_md_end(check_md
);
933 contig_high
= GRANULEROUNDDOWN(contig_high
);
935 if (!is_memory_available(md
) || md
->type
== EFI_LOADER_DATA
)
938 /* Round ends inward to granule boundaries */
939 as
= max(contig_low
, md
->phys_addr
);
940 ae
= min(contig_high
, efi_md_end(md
));
942 /* keep within max_addr= and min_addr= command line arg */
943 as
= max(as
, min_addr
);
944 ae
= min(ae
, max_addr
);
948 /* avoid going over mem= command line arg */
949 if (total_mem
+ (ae
- as
) > mem_limit
)
950 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
955 if (ae
- as
> space_needed
)
958 if (p
>= efi_map_end
)
959 panic("Can't allocate space for kernel memory descriptors");
965 * Walk the EFI memory map and gather all memory available for kernel
966 * to use. We can allocate partial granules only if the unavailable
967 * parts exist, and are WB.
970 efi_memmap_init(unsigned long *s
, unsigned long *e
)
972 struct kern_memdesc
*k
, *prev
= NULL
;
973 u64 contig_low
=0, contig_high
=0;
975 void *efi_map_start
, *efi_map_end
, *p
, *q
;
976 efi_memory_desc_t
*md
, *pmd
= NULL
, *check_md
;
978 unsigned long total_mem
= 0;
980 k
= kern_memmap
= find_memmap_space();
982 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
983 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
984 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
986 for (p
= efi_map_start
; p
< efi_map_end
; pmd
= md
, p
+= efi_desc_size
) {
989 if (efi_uc(md
) && (md
->type
== EFI_CONVENTIONAL_MEMORY
||
990 md
->type
== EFI_BOOT_SERVICES_DATA
)) {
991 k
->attribute
= EFI_MEMORY_UC
;
992 k
->start
= md
->phys_addr
;
993 k
->num_pages
= md
->num_pages
;
998 if (pmd
== NULL
|| !efi_wb(pmd
) || efi_md_end(pmd
) != md
->phys_addr
) {
999 contig_low
= GRANULEROUNDUP(md
->phys_addr
);
1000 contig_high
= efi_md_end(md
);
1001 for (q
= p
+ efi_desc_size
; q
< efi_map_end
; 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
))
1014 #ifdef CONFIG_CRASH_DUMP
1015 /* saved_max_pfn should ignore max_addr= command line arg */
1016 if (saved_max_pfn
< (efi_md_end(md
) >> PAGE_SHIFT
))
1017 saved_max_pfn
= (efi_md_end(md
) >> PAGE_SHIFT
);
1020 * Round ends inward to granule boundaries
1021 * Give trimmings to uncached allocator
1023 if (md
->phys_addr
< contig_low
) {
1024 lim
= min(efi_md_end(md
), contig_low
);
1026 if (k
> kern_memmap
&& (k
-1)->attribute
== EFI_MEMORY_UC
&&
1027 kmd_end(k
-1) == md
->phys_addr
) {
1028 (k
-1)->num_pages
+= (lim
- md
->phys_addr
) >> EFI_PAGE_SHIFT
;
1030 k
->attribute
= EFI_MEMORY_UC
;
1031 k
->start
= md
->phys_addr
;
1032 k
->num_pages
= (lim
- md
->phys_addr
) >> EFI_PAGE_SHIFT
;
1040 if (efi_md_end(md
) > contig_high
) {
1041 lim
= max(md
->phys_addr
, contig_high
);
1043 if (lim
== md
->phys_addr
&& k
> kern_memmap
&&
1044 (k
-1)->attribute
== EFI_MEMORY_UC
&&
1045 kmd_end(k
-1) == md
->phys_addr
) {
1046 (k
-1)->num_pages
+= md
->num_pages
;
1048 k
->attribute
= EFI_MEMORY_UC
;
1050 k
->num_pages
= (efi_md_end(md
) - lim
) >> EFI_PAGE_SHIFT
;
1056 ae
= efi_md_end(md
);
1058 /* keep within max_addr= and min_addr= command line arg */
1059 as
= max(as
, min_addr
);
1060 ae
= min(ae
, max_addr
);
1064 /* avoid going over mem= command line arg */
1065 if (total_mem
+ (ae
- as
) > mem_limit
)
1066 ae
-= total_mem
+ (ae
- as
) - mem_limit
;
1070 if (prev
&& kmd_end(prev
) == md
->phys_addr
) {
1071 prev
->num_pages
+= (ae
- as
) >> EFI_PAGE_SHIFT
;
1072 total_mem
+= ae
- as
;
1075 k
->attribute
= EFI_MEMORY_WB
;
1077 k
->num_pages
= (ae
- as
) >> EFI_PAGE_SHIFT
;
1078 total_mem
+= ae
- as
;
1081 k
->start
= ~0L; /* end-marker */
1083 /* reserve the memory we are using for kern_memmap */
1084 *s
= (u64
)kern_memmap
;
1089 efi_initialize_iomem_resources(struct resource
*code_resource
,
1090 struct resource
*data_resource
)
1092 struct resource
*res
;
1093 void *efi_map_start
, *efi_map_end
, *p
;
1094 efi_memory_desc_t
*md
;
1097 unsigned long flags
;
1099 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1100 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1101 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1105 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1108 if (md
->num_pages
== 0) /* should not happen */
1111 flags
= IORESOURCE_MEM
;
1114 case EFI_MEMORY_MAPPED_IO
:
1115 case EFI_MEMORY_MAPPED_IO_PORT_SPACE
:
1118 case EFI_LOADER_CODE
:
1119 case EFI_LOADER_DATA
:
1120 case EFI_BOOT_SERVICES_DATA
:
1121 case EFI_BOOT_SERVICES_CODE
:
1122 case EFI_CONVENTIONAL_MEMORY
:
1123 if (md
->attribute
& EFI_MEMORY_WP
) {
1124 name
= "System ROM";
1125 flags
|= IORESOURCE_READONLY
;
1127 name
= "System RAM";
1131 case EFI_ACPI_MEMORY_NVS
:
1132 name
= "ACPI Non-volatile Storage";
1133 flags
|= IORESOURCE_BUSY
;
1136 case EFI_UNUSABLE_MEMORY
:
1138 flags
|= IORESOURCE_BUSY
| IORESOURCE_DISABLED
;
1141 case EFI_RESERVED_TYPE
:
1142 case EFI_RUNTIME_SERVICES_CODE
:
1143 case EFI_RUNTIME_SERVICES_DATA
:
1144 case EFI_ACPI_RECLAIM_MEMORY
:
1147 flags
|= IORESOURCE_BUSY
;
1151 if ((res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
)) == NULL
) {
1152 printk(KERN_ERR
"failed to alocate resource for iomem\n");
1157 res
->start
= md
->phys_addr
;
1158 res
->end
= md
->phys_addr
+ (md
->num_pages
<< EFI_PAGE_SHIFT
) - 1;
1161 if (insert_resource(&iomem_resource
, res
) < 0)
1165 * We don't know which region contains
1166 * kernel data so we try it repeatedly and
1167 * let the resource manager test it.
1169 insert_resource(res
, code_resource
);
1170 insert_resource(res
, data_resource
);
1172 insert_resource(res
, &efi_memmap_res
);
1173 insert_resource(res
, &boot_param_res
);
1174 if (crashk_res
.end
> crashk_res
.start
)
1175 insert_resource(res
, &crashk_res
);
1182 /* find a block of memory aligned to 64M exclude reserved regions
1183 rsvd_regions are sorted
1185 unsigned long __init
1186 kdump_find_rsvd_region (unsigned long size
,
1187 struct rsvd_region
*r
, int n
)
1191 u64 alignment
= 1UL << _PAGE_SIZE_64M
;
1192 void *efi_map_start
, *efi_map_end
, *p
;
1193 efi_memory_desc_t
*md
;
1196 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1197 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1198 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1200 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1204 start
= ALIGN(md
->phys_addr
, alignment
);
1205 end
= efi_md_end(md
);
1206 for (i
= 0; i
< n
; i
++) {
1207 if (__pa(r
[i
].start
) >= start
&& __pa(r
[i
].end
) < end
) {
1208 if (__pa(r
[i
].start
) > start
+ size
)
1210 start
= ALIGN(__pa(r
[i
].end
), alignment
);
1211 if (i
< n
-1 && __pa(r
[i
+1].start
) < start
+ size
)
1217 if (end
> start
+ size
)
1221 printk(KERN_WARNING
"Cannot reserve 0x%lx byte of memory for crashdump\n",
1227 #ifdef CONFIG_PROC_VMCORE
1228 /* locate the size find a the descriptor at a certain address */
1230 vmcore_find_descriptor_size (unsigned long address
)
1232 void *efi_map_start
, *efi_map_end
, *p
;
1233 efi_memory_desc_t
*md
;
1235 unsigned long ret
= 0;
1237 efi_map_start
= __va(ia64_boot_param
->efi_memmap
);
1238 efi_map_end
= efi_map_start
+ ia64_boot_param
->efi_memmap_size
;
1239 efi_desc_size
= ia64_boot_param
->efi_memdesc_size
;
1241 for (p
= efi_map_start
; p
< efi_map_end
; p
+= efi_desc_size
) {
1243 if (efi_wb(md
) && md
->type
== EFI_LOADER_DATA
1244 && md
->phys_addr
== address
) {
1245 ret
= efi_md_size(md
);
1251 printk(KERN_WARNING
"Cannot locate EFI vmcore descriptor\n");