ia64/xen-unstable

view xen/arch/ia64/linux-xen/efi.c @ 17886:cfbc535ebf6f

[IA64] efi: remove old efi code to copmile newer linux acpi code.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Wed Jun 11 16:57:42 2008 +0900 (2008-06-11)
parents 003036d7db0f
children fb5cfb8b122e
line source
1 /*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 0.9 April 30, 1999
5 *
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>
13 *
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
17 *
18 * Implemented EFI runtime services and virtual mode calls. --davidm
19 *
20 * Goutham Rao: <goutham.rao@intel.com>
21 * Skip non-WB memory and ignore empty memory ranges.
22 */
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>
32 #include <asm/io.h>
33 #include <asm/kregs.h>
34 #include <asm/meminit.h>
35 #include <asm/pgtable.h>
36 #include <asm/processor.h>
37 #include <asm/mca.h>
39 #define EFI_DEBUG 0
41 extern efi_status_t efi_call_phys (void *, ...);
42 #ifdef XEN
43 /* this should be defined in linux/kernel.h */
44 extern unsigned long long memparse (char *ptr, char **retptr);
45 /* this should be defined in linux/efi.h */
46 //#define EFI_INVALID_TABLE_ADDR (void *)(~0UL)
47 #endif
49 struct efi efi;
50 EXPORT_SYMBOL(efi);
51 static efi_runtime_services_t *runtime;
52 #if defined(XEN) && !defined(CONFIG_VIRTUAL_FRAME_TABLE)
53 // this is a temporary hack to avoid CONFIG_VIRTUAL_MEM_MAP
54 static unsigned long mem_limit = ~0UL, max_addr = 0x100000000UL, min_addr = 0UL;
55 #else
56 static unsigned long mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
57 #endif
59 #define efi_call_virt(f, args...) (*(f))(args)
61 #define STUB_GET_TIME(prefix, adjust_arg) \
62 static efi_status_t \
63 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
64 { \
65 struct ia64_fpreg fr[6]; \
66 efi_time_cap_t *atc = NULL; \
67 efi_status_t ret; \
68 \
69 if (tc) \
70 atc = adjust_arg(tc); \
71 ia64_save_scratch_fpregs(fr); \
72 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
73 ia64_load_scratch_fpregs(fr); \
74 return ret; \
75 }
77 #define STUB_SET_TIME(prefix, adjust_arg) \
78 static efi_status_t \
79 prefix##_set_time (efi_time_t *tm) \
80 { \
81 struct ia64_fpreg fr[6]; \
82 efi_status_t ret; \
83 \
84 ia64_save_scratch_fpregs(fr); \
85 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
86 ia64_load_scratch_fpregs(fr); \
87 return ret; \
88 }
90 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
91 static efi_status_t \
92 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
93 { \
94 struct ia64_fpreg fr[6]; \
95 efi_status_t ret; \
96 \
97 ia64_save_scratch_fpregs(fr); \
98 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
99 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
100 ia64_load_scratch_fpregs(fr); \
101 return ret; \
102 }
104 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
105 static efi_status_t \
106 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
107 { \
108 struct ia64_fpreg fr[6]; \
109 efi_time_t *atm = NULL; \
110 efi_status_t ret; \
111 \
112 if (tm) \
113 atm = adjust_arg(tm); \
114 ia64_save_scratch_fpregs(fr); \
115 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
116 enabled, atm); \
117 ia64_load_scratch_fpregs(fr); \
118 return ret; \
119 }
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) \
125 { \
126 struct ia64_fpreg fr[6]; \
127 u32 *aattr = NULL; \
128 efi_status_t ret; \
129 \
130 if (attr) \
131 aattr = adjust_arg(attr); \
132 ia64_save_scratch_fpregs(fr); \
133 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
134 adjust_arg(name), adjust_arg(vendor), aattr, \
135 adjust_arg(data_size), adjust_arg(data)); \
136 ia64_load_scratch_fpregs(fr); \
137 return ret; \
138 }
140 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
141 static efi_status_t \
142 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
143 { \
144 struct ia64_fpreg fr[6]; \
145 efi_status_t ret; \
146 \
147 ia64_save_scratch_fpregs(fr); \
148 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
149 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
150 ia64_load_scratch_fpregs(fr); \
151 return ret; \
152 }
154 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
155 static efi_status_t \
156 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
157 unsigned long data_size, void *data) \
158 { \
159 struct ia64_fpreg fr[6]; \
160 efi_status_t ret; \
161 \
162 ia64_save_scratch_fpregs(fr); \
163 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
164 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
165 adjust_arg(data)); \
166 ia64_load_scratch_fpregs(fr); \
167 return ret; \
168 }
170 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
171 static efi_status_t \
172 prefix##_get_next_high_mono_count (u32 *count) \
173 { \
174 struct ia64_fpreg fr[6]; \
175 efi_status_t ret; \
176 \
177 ia64_save_scratch_fpregs(fr); \
178 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
179 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
180 ia64_load_scratch_fpregs(fr); \
181 return ret; \
182 }
184 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
185 static void \
186 prefix##_reset_system (int reset_type, efi_status_t status, \
187 unsigned long data_size, efi_char16_t *data) \
188 { \
189 struct ia64_fpreg fr[6]; \
190 efi_char16_t *adata = NULL; \
191 \
192 if (data) \
193 adata = adjust_arg(data); \
194 \
195 ia64_save_scratch_fpregs(fr); \
196 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
197 reset_type, status, data_size, adata); \
198 /* should not return, but just in case... */ \
199 ia64_load_scratch_fpregs(fr); \
200 }
202 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
204 STUB_GET_TIME(phys, phys_ptr)
205 STUB_SET_TIME(phys, phys_ptr)
206 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
207 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
208 STUB_GET_VARIABLE(phys, phys_ptr)
209 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
210 STUB_SET_VARIABLE(phys, phys_ptr)
211 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
212 STUB_RESET_SYSTEM(phys, phys_ptr)
214 #define id(arg) arg
216 STUB_GET_TIME(virt, id)
217 STUB_SET_TIME(virt, id)
218 STUB_GET_WAKEUP_TIME(virt, id)
219 STUB_SET_WAKEUP_TIME(virt, id)
220 STUB_GET_VARIABLE(virt, id)
221 STUB_GET_NEXT_VARIABLE(virt, id)
222 STUB_SET_VARIABLE(virt, id)
223 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
224 STUB_RESET_SYSTEM(virt, id)
226 #ifndef XEN
227 void
228 efi_gettimeofday (struct timespec *ts)
229 {
230 efi_time_t tm;
232 memset(ts, 0, sizeof(ts));
233 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
234 return;
236 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
237 ts->tv_nsec = tm.nanosecond;
238 }
239 #endif
241 static int
242 is_memory_available (efi_memory_desc_t *md)
243 {
244 if (!(md->attribute & EFI_MEMORY_WB))
245 return 0;
247 switch (md->type) {
248 case EFI_LOADER_CODE:
249 case EFI_LOADER_DATA:
250 case EFI_BOOT_SERVICES_CODE:
251 case EFI_BOOT_SERVICES_DATA:
252 case EFI_CONVENTIONAL_MEMORY:
253 return 1;
254 }
255 return 0;
256 }
258 typedef struct kern_memdesc {
259 u64 attribute;
260 u64 start;
261 u64 num_pages;
262 } kern_memdesc_t;
264 static kern_memdesc_t *kern_memmap;
266 #define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
268 static inline u64
269 kmd_end(kern_memdesc_t *kmd)
270 {
271 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
272 }
274 static inline u64
275 efi_md_end(efi_memory_desc_t *md)
276 {
277 return (md->phys_addr + efi_md_size(md));
278 }
280 static inline int
281 efi_wb(efi_memory_desc_t *md)
282 {
283 return (md->attribute & EFI_MEMORY_WB);
284 }
286 static inline int
287 efi_uc(efi_memory_desc_t *md)
288 {
289 return (md->attribute & EFI_MEMORY_UC);
290 }
292 static void
293 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
294 {
295 kern_memdesc_t *k;
296 u64 start, end, voff;
298 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
299 for (k = kern_memmap; k->start != ~0UL; k++) {
300 if (k->attribute != attr)
301 continue;
302 start = PAGE_ALIGN(k->start);
303 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
304 if (start < end)
305 if ((*callback)(start + voff, end + voff, arg) < 0)
306 return;
307 }
308 }
310 /*
311 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
312 * has memory that is available for OS use.
313 */
314 void
315 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
316 {
317 walk(callback, arg, EFI_MEMORY_WB);
318 }
320 /*
321 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
322 * has memory that is available for uncached allocator.
323 */
324 void
325 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
326 {
327 walk(callback, arg, EFI_MEMORY_UC);
328 }
330 /*
331 * Look for the PAL_CODE region reported by EFI and maps it using an
332 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
333 * Abstraction Layer chapter 11 in ADAG
334 */
336 #ifdef XEN
337 static void *
338 __efi_get_pal_addr (void)
339 #else
340 void *
341 efi_get_pal_addr (void)
342 #endif
343 {
344 void *efi_map_start, *efi_map_end, *p;
345 efi_memory_desc_t *md;
346 u64 efi_desc_size;
347 int pal_code_count = 0;
348 u64 vaddr, mask;
350 efi_map_start = __va(ia64_boot_param->efi_memmap);
351 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
352 efi_desc_size = ia64_boot_param->efi_memdesc_size;
354 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
355 md = p;
356 if (md->type != EFI_PAL_CODE)
357 continue;
359 if (++pal_code_count > 1) {
360 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
361 md->phys_addr);
362 continue;
363 }
364 /*
365 * The only ITLB entry in region 7 that is used is the one installed by
366 * __start(). That entry covers a 64MB range.
367 */
368 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
369 vaddr = PAGE_OFFSET + md->phys_addr;
371 /*
372 * We must check that the PAL mapping won't overlap with the kernel
373 * mapping.
374 *
375 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
376 * 256KB and that only one ITR is needed to map it. This implies that the
377 * PAL code is always aligned on its size, i.e., the closest matching page
378 * size supported by the TLB. Therefore PAL code is guaranteed never to
379 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
380 * now the following test is enough to determine whether or not we need a
381 * dedicated ITR for the PAL code.
382 */
383 if ((vaddr & mask) == (KERNEL_START & mask)) {
384 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
385 __FUNCTION__);
386 continue;
387 }
389 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
390 panic("Woah! PAL code size bigger than a granule!");
392 #if EFI_DEBUG
393 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
395 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
396 smp_processor_id(), md->phys_addr,
397 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
398 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
399 #endif
400 return __va(md->phys_addr);
401 }
402 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
403 __FUNCTION__);
404 return NULL;
405 }
407 #ifdef XEN
408 void *pal_vaddr = 0;
410 void *
411 efi_get_pal_addr(void)
412 {
413 if (!pal_vaddr)
414 pal_vaddr = __efi_get_pal_addr();
415 return pal_vaddr;
416 }
417 #endif
419 void
420 efi_map_pal_code (void)
421 {
422 #ifdef XEN
423 u64 psr;
424 (void)efi_get_pal_addr();
425 #else
426 void *pal_vaddr = efi_get_pal_addr ();
427 u64 psr;
429 if (!pal_vaddr)
430 return;
431 #endif
433 /*
434 * Cannot write to CRx with PSR.ic=1
435 */
436 psr = ia64_clear_ic();
437 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
438 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
439 IA64_GRANULE_SHIFT);
440 ia64_set_psr(psr); /* restore psr */
441 ia64_srlz_i();
442 }
444 void __init
445 efi_init (void)
446 {
447 void *efi_map_start, *efi_map_end;
448 efi_config_table_t *config_tables;
449 efi_char16_t *c16;
450 u64 efi_desc_size;
451 char *cp, vendor[100] = "unknown";
452 int i;
454 /* it's too early to be able to use the standard kernel command line support... */
455 #ifdef XEN
456 extern char saved_command_line[];
457 for (cp = saved_command_line; *cp; ) {
458 #else
459 for (cp = boot_command_line; *cp; ) {
460 #endif
461 if (memcmp(cp, "mem=", 4) == 0) {
462 mem_limit = memparse(cp + 4, &cp);
463 } else if (memcmp(cp, "max_addr=", 9) == 0) {
464 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
465 } else if (memcmp(cp, "min_addr=", 9) == 0) {
466 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
467 } else {
468 while (*cp != ' ' && *cp)
469 ++cp;
470 while (*cp == ' ')
471 ++cp;
472 }
473 }
474 if (min_addr != 0UL)
475 printk(KERN_INFO "Ignoring memory below %luMB\n", min_addr >> 20);
476 if (max_addr != ~0UL)
477 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
479 efi.systab = __va(ia64_boot_param->efi_systab);
481 /*
482 * Verify the EFI Table
483 */
484 if (efi.systab == NULL)
485 panic("Woah! Can't find EFI system table.\n");
486 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
487 panic("Woah! EFI system table signature incorrect\n");
488 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
489 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
490 "got %d.%02d, expected %d.%02d\n",
491 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
492 EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
494 config_tables = __va(efi.systab->tables);
496 /* Show what we know for posterity */
497 c16 = __va(efi.systab->fw_vendor);
498 if (c16) {
499 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
500 vendor[i] = *c16++;
501 vendor[i] = '\0';
502 }
504 printk(KERN_INFO "EFI v%u.%.02u by %s:",
505 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
507 efi.mps = EFI_INVALID_TABLE_ADDR;
508 efi.acpi = EFI_INVALID_TABLE_ADDR;
509 efi.acpi20 = EFI_INVALID_TABLE_ADDR;
510 efi.smbios = EFI_INVALID_TABLE_ADDR;
511 efi.sal_systab = EFI_INVALID_TABLE_ADDR;
512 efi.boot_info = EFI_INVALID_TABLE_ADDR;
513 efi.hcdp = EFI_INVALID_TABLE_ADDR;
514 efi.uga = EFI_INVALID_TABLE_ADDR;
516 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
517 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
518 efi.mps = config_tables[i].table;
519 printk(" MPS=0x%lx", config_tables[i].table);
520 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
521 efi.acpi20 = config_tables[i].table;
522 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
523 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
524 efi.acpi = config_tables[i].table;
525 printk(" ACPI=0x%lx", config_tables[i].table);
526 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
527 efi.smbios = config_tables[i].table;
528 printk(" SMBIOS=0x%lx", config_tables[i].table);
529 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
530 efi.sal_systab = config_tables[i].table;
531 printk(" SALsystab=0x%lx", config_tables[i].table);
532 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
533 efi.hcdp = config_tables[i].table;
534 printk(" HCDP=0x%lx", config_tables[i].table);
535 }
536 }
537 printk("\n");
539 runtime = __va(efi.systab->runtime);
540 efi.get_time = phys_get_time;
541 efi.set_time = phys_set_time;
542 efi.get_wakeup_time = phys_get_wakeup_time;
543 efi.set_wakeup_time = phys_set_wakeup_time;
544 efi.get_variable = phys_get_variable;
545 efi.get_next_variable = phys_get_next_variable;
546 efi.set_variable = phys_set_variable;
547 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
548 efi.reset_system = phys_reset_system;
550 efi_map_start = __va(ia64_boot_param->efi_memmap);
551 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
552 efi_desc_size = ia64_boot_param->efi_memdesc_size;
554 #if EFI_DEBUG
555 /* print EFI memory map: */
556 {
557 efi_memory_desc_t *md;
558 void *p;
560 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
561 md = p;
562 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
563 i, md->type, md->attribute, md->phys_addr,
564 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
565 md->num_pages >> (20 - EFI_PAGE_SHIFT));
566 }
567 }
568 #endif
570 efi_map_pal_code();
571 efi_enter_virtual_mode();
572 }
574 void
575 efi_enter_virtual_mode (void)
576 {
577 void *efi_map_start, *efi_map_end, *p;
578 efi_memory_desc_t *md;
579 efi_status_t status;
580 u64 efi_desc_size;
582 efi_map_start = __va(ia64_boot_param->efi_memmap);
583 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
584 efi_desc_size = ia64_boot_param->efi_memdesc_size;
586 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
587 md = p;
588 if (md->attribute & EFI_MEMORY_RUNTIME) {
589 /*
590 * Some descriptors have multiple bits set, so the order of
591 * the tests is relevant.
592 */
593 if (md->attribute & EFI_MEMORY_WB) {
594 md->virt_addr = (u64) __va(md->phys_addr);
595 } else if (md->attribute & EFI_MEMORY_UC) {
596 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
597 } else if (md->attribute & EFI_MEMORY_WC) {
598 #if 0
599 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
600 | _PAGE_D
601 | _PAGE_MA_WC
602 | _PAGE_PL_0
603 | _PAGE_AR_RW));
604 #else
605 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
606 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
607 #endif
608 } else if (md->attribute & EFI_MEMORY_WT) {
609 #if 0
610 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
611 | _PAGE_D | _PAGE_MA_WT
612 | _PAGE_PL_0
613 | _PAGE_AR_RW));
614 #else
615 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
616 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
617 #endif
618 }
619 }
620 }
622 status = efi_call_phys(__va(runtime->set_virtual_address_map),
623 ia64_boot_param->efi_memmap_size,
624 efi_desc_size, ia64_boot_param->efi_memdesc_version,
625 ia64_boot_param->efi_memmap);
626 if (status != EFI_SUCCESS) {
627 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
628 "(status=%lu)\n", status);
629 return;
630 }
632 /*
633 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
634 */
635 efi.get_time = virt_get_time;
636 efi.set_time = virt_set_time;
637 efi.get_wakeup_time = virt_get_wakeup_time;
638 efi.set_wakeup_time = virt_set_wakeup_time;
639 efi.get_variable = virt_get_variable;
640 efi.get_next_variable = virt_get_next_variable;
641 efi.set_variable = virt_set_variable;
642 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
643 efi.reset_system = virt_reset_system;
644 }
646 /*
647 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
648 * this type, other I/O port ranges should be described via ACPI.
649 */
650 u64
651 efi_get_iobase (void)
652 {
653 void *efi_map_start, *efi_map_end, *p;
654 efi_memory_desc_t *md;
655 u64 efi_desc_size;
657 efi_map_start = __va(ia64_boot_param->efi_memmap);
658 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
659 efi_desc_size = ia64_boot_param->efi_memdesc_size;
661 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
662 md = p;
663 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
664 if (md->attribute & EFI_MEMORY_UC)
665 return md->phys_addr;
666 }
667 }
668 return 0;
669 }
671 static struct kern_memdesc *
672 kern_memory_descriptor (unsigned long phys_addr)
673 {
674 struct kern_memdesc *md;
676 for (md = kern_memmap; md->start != ~0UL; md++) {
677 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
678 return md;
679 }
680 return NULL;
681 }
683 static efi_memory_desc_t *
684 efi_memory_descriptor (unsigned long phys_addr)
685 {
686 void *efi_map_start, *efi_map_end, *p;
687 efi_memory_desc_t *md;
688 u64 efi_desc_size;
690 efi_map_start = __va(ia64_boot_param->efi_memmap);
691 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
692 efi_desc_size = ia64_boot_param->efi_memdesc_size;
694 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
695 md = p;
697 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
698 return md;
699 }
700 return NULL;
701 }
703 u32
704 efi_mem_type (unsigned long phys_addr)
705 {
706 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
708 if (md)
709 return md->type;
710 return 0;
711 }
713 u64
714 efi_mem_attributes (unsigned long phys_addr)
715 {
716 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
718 if (md)
719 return md->attribute;
720 return 0;
721 }
722 EXPORT_SYMBOL(efi_mem_attributes);
724 u64
725 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
726 {
727 unsigned long end = phys_addr + size;
728 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
729 u64 attr;
731 if (!md)
732 return 0;
734 /*
735 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
736 * the kernel that firmware needs this region mapped.
737 */
738 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
739 do {
740 unsigned long md_end = efi_md_end(md);
742 if (end <= md_end)
743 return attr;
745 md = efi_memory_descriptor(md_end);
746 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
747 return 0;
748 } while (md);
749 return 0;
750 }
752 u64
753 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
754 {
755 unsigned long end = phys_addr + size;
756 struct kern_memdesc *md;
757 u64 attr;
759 /*
760 * This is a hack for ioremap calls before we set up kern_memmap.
761 * Maybe we should do efi_memmap_init() earlier instead.
762 */
763 if (!kern_memmap) {
764 attr = efi_mem_attribute(phys_addr, size);
765 if (attr & EFI_MEMORY_WB)
766 return EFI_MEMORY_WB;
767 return 0;
768 }
770 md = kern_memory_descriptor(phys_addr);
771 if (!md)
772 return 0;
774 attr = md->attribute;
775 do {
776 unsigned long md_end = kmd_end(md);
778 if (end <= md_end)
779 return attr;
781 md = kern_memory_descriptor(md_end);
782 if (!md || md->attribute != attr)
783 return 0;
784 } while (md);
785 return 0;
786 }
787 EXPORT_SYMBOL(kern_mem_attribute);
789 #ifndef XEN
790 int
791 valid_phys_addr_range (unsigned long phys_addr, unsigned long size)
792 {
793 u64 attr;
795 /*
796 * /dev/mem reads and writes use copy_to_user(), which implicitly
797 * uses a granule-sized kernel identity mapping. It's really
798 * only safe to do this for regions in kern_memmap. For more
799 * details, see Documentation/ia64/aliasing.txt.
800 */
801 attr = kern_mem_attribute(phys_addr, size);
802 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
803 return 1;
804 return 0;
805 }
807 int
808 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
809 {
810 /*
811 * MMIO regions are often missing from the EFI memory map.
812 * We must allow mmap of them for programs like X, so we
813 * currently can't do any useful validation.
814 */
815 return 1;
816 }
818 pgprot_t
819 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
820 pgprot_t vma_prot)
821 {
822 unsigned long phys_addr = pfn << PAGE_SHIFT;
823 u64 attr;
825 /*
826 * For /dev/mem mmap, we use user mappings, but if the region is
827 * in kern_memmap (and hence may be covered by a kernel mapping),
828 * we must use the same attribute as the kernel mapping.
829 */
830 attr = kern_mem_attribute(phys_addr, size);
831 if (attr & EFI_MEMORY_WB)
832 return pgprot_cacheable(vma_prot);
833 else if (attr & EFI_MEMORY_UC)
834 return pgprot_noncached(vma_prot);
836 /*
837 * Some chipsets don't support UC access to memory. If
838 * WB is supported, we prefer that.
839 */
840 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
841 return pgprot_cacheable(vma_prot);
843 return pgprot_noncached(vma_prot);
844 }
845 #endif
847 int __init
848 efi_uart_console_only(void)
849 {
850 efi_status_t status;
851 char *s, name[] = "ConOut";
852 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
853 efi_char16_t *utf16, name_utf16[32];
854 unsigned char data[1024];
855 unsigned long size = sizeof(data);
856 struct efi_generic_dev_path *hdr, *end_addr;
857 int uart = 0;
859 /* Convert to UTF-16 */
860 utf16 = name_utf16;
861 s = name;
862 while (*s)
863 *utf16++ = *s++ & 0x7f;
864 *utf16 = 0;
866 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
867 if (status != EFI_SUCCESS) {
868 printk(KERN_ERR "No EFI %s variable?\n", name);
869 return 0;
870 }
872 hdr = (struct efi_generic_dev_path *) data;
873 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
874 while (hdr < end_addr) {
875 if (hdr->type == EFI_DEV_MSG &&
876 hdr->sub_type == EFI_DEV_MSG_UART)
877 uart = 1;
878 else if (hdr->type == EFI_DEV_END_PATH ||
879 hdr->type == EFI_DEV_END_PATH2) {
880 if (!uart)
881 return 0;
882 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
883 return 1;
884 uart = 0;
885 }
886 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
887 }
888 printk(KERN_ERR "Malformed %s value\n", name);
889 return 0;
890 }
892 /*
893 * Look for the first granule aligned memory descriptor memory
894 * that is big enough to hold EFI memory map. Make sure this
895 * descriptor is atleast granule sized so it does not get trimmed
896 */
897 struct kern_memdesc *
898 find_memmap_space (void)
899 {
900 u64 contig_low=0, contig_high=0;
901 u64 as = 0, ae;
902 void *efi_map_start, *efi_map_end, *p, *q;
903 efi_memory_desc_t *md, *pmd = NULL, *check_md;
904 u64 space_needed, efi_desc_size;
905 unsigned long total_mem = 0;
907 efi_map_start = __va(ia64_boot_param->efi_memmap);
908 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
909 efi_desc_size = ia64_boot_param->efi_memdesc_size;
911 /*
912 * Worst case: we need 3 kernel descriptors for each efi descriptor
913 * (if every entry has a WB part in the middle, and UC head and tail),
914 * plus one for the end marker.
915 */
916 space_needed = sizeof(kern_memdesc_t) *
917 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
919 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
920 md = p;
921 if (!efi_wb(md)) {
922 continue;
923 }
924 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
925 contig_low = GRANULEROUNDUP(md->phys_addr);
926 contig_high = efi_md_end(md);
927 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
928 check_md = q;
929 if (!efi_wb(check_md))
930 break;
931 if (contig_high != check_md->phys_addr)
932 break;
933 contig_high = efi_md_end(check_md);
934 }
935 contig_high = GRANULEROUNDDOWN(contig_high);
936 }
937 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
938 continue;
940 /* Round ends inward to granule boundaries */
941 as = max(contig_low, md->phys_addr);
942 ae = min(contig_high, efi_md_end(md));
944 /* keep within max_addr= and min_addr= command line arg */
945 as = max(as, min_addr);
946 ae = min(ae, max_addr);
947 if (ae <= as)
948 continue;
950 /* avoid going over mem= command line arg */
951 if (total_mem + (ae - as) > mem_limit)
952 ae -= total_mem + (ae - as) - mem_limit;
954 if (ae <= as)
955 continue;
957 if (ae - as > space_needed)
958 break;
959 }
960 if (p >= efi_map_end)
961 panic("Can't allocate space for kernel memory descriptors");
963 return __va(as);
964 }
966 /*
967 * Walk the EFI memory map and gather all memory available for kernel
968 * to use. We can allocate partial granules only if the unavailable
969 * parts exist, and are WB.
970 */
971 void
972 efi_memmap_init(unsigned long *s, unsigned long *e)
973 {
974 struct kern_memdesc *k, *prev = NULL;
975 u64 contig_low=0, contig_high=0;
976 u64 as, ae, lim;
977 void *efi_map_start, *efi_map_end, *p, *q;
978 efi_memory_desc_t *md, *pmd = NULL, *check_md;
979 u64 efi_desc_size;
980 unsigned long total_mem = 0;
982 k = kern_memmap = find_memmap_space();
984 efi_map_start = __va(ia64_boot_param->efi_memmap);
985 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
986 efi_desc_size = ia64_boot_param->efi_memdesc_size;
988 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
989 md = p;
990 if (!efi_wb(md)) {
991 if (efi_uc(md) && (md->type == EFI_CONVENTIONAL_MEMORY ||
992 md->type == EFI_BOOT_SERVICES_DATA)) {
993 k->attribute = EFI_MEMORY_UC;
994 k->start = md->phys_addr;
995 k->num_pages = md->num_pages;
996 k++;
997 }
998 continue;
999 }
1000 #ifdef XEN
1001 /* this works around a problem in the ski bootloader */
1002 if (running_on_sim && md->type != EFI_CONVENTIONAL_MEMORY)
1003 continue;
1004 #endif
1005 if (pmd == NULL || !efi_wb(pmd) || efi_md_end(pmd) != md->phys_addr) {
1006 contig_low = GRANULEROUNDUP(md->phys_addr);
1007 contig_high = efi_md_end(md);
1008 for (q = p + efi_desc_size; q < efi_map_end; q += efi_desc_size) {
1009 check_md = q;
1010 if (!efi_wb(check_md))
1011 break;
1012 if (contig_high != check_md->phys_addr)
1013 break;
1014 contig_high = efi_md_end(check_md);
1016 contig_high = GRANULEROUNDDOWN(contig_high);
1018 if (!is_memory_available(md))
1019 continue;
1021 #ifdef CONFIG_CRASH_DUMP
1022 /* saved_max_pfn should ignore max_addr= command line arg */
1023 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1024 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1025 #endif
1026 /*
1027 * Round ends inward to granule boundaries
1028 * Give trimmings to uncached allocator
1029 */
1030 if (md->phys_addr < contig_low) {
1031 lim = min(efi_md_end(md), contig_low);
1032 if (efi_uc(md)) {
1033 if (k > kern_memmap && (k-1)->attribute == EFI_MEMORY_UC &&
1034 kmd_end(k-1) == md->phys_addr) {
1035 (k-1)->num_pages += (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1036 } else {
1037 k->attribute = EFI_MEMORY_UC;
1038 k->start = md->phys_addr;
1039 k->num_pages = (lim - md->phys_addr) >> EFI_PAGE_SHIFT;
1040 k++;
1043 as = contig_low;
1044 } else
1045 as = md->phys_addr;
1047 if (efi_md_end(md) > contig_high) {
1048 lim = max(md->phys_addr, contig_high);
1049 if (efi_uc(md)) {
1050 if (lim == md->phys_addr && k > kern_memmap &&
1051 (k-1)->attribute == EFI_MEMORY_UC &&
1052 kmd_end(k-1) == md->phys_addr) {
1053 (k-1)->num_pages += md->num_pages;
1054 } else {
1055 k->attribute = EFI_MEMORY_UC;
1056 k->start = lim;
1057 k->num_pages = (efi_md_end(md) - lim) >> EFI_PAGE_SHIFT;
1058 k++;
1061 ae = contig_high;
1062 } else
1063 ae = efi_md_end(md);
1065 /* keep within max_addr= and min_addr= command line arg */
1066 as = max(as, min_addr);
1067 ae = min(ae, max_addr);
1068 if (ae <= as)
1069 continue;
1071 /* avoid going over mem= command line arg */
1072 if (total_mem + (ae - as) > mem_limit)
1073 ae -= total_mem + (ae - as) - mem_limit;
1075 if (ae <= as)
1076 continue;
1077 if (prev && kmd_end(prev) == md->phys_addr) {
1078 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1079 total_mem += ae - as;
1080 continue;
1082 k->attribute = EFI_MEMORY_WB;
1083 k->start = as;
1084 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1085 total_mem += ae - as;
1086 prev = k++;
1088 k->start = ~0L; /* end-marker */
1090 /* reserve the memory we are using for kern_memmap */
1091 *s = (u64)kern_memmap;
1092 *e = (u64)++k;
1095 #ifndef XEN
1096 void
1097 efi_initialize_iomem_resources(struct resource *code_resource,
1098 struct resource *data_resource)
1100 struct resource *res;
1101 void *efi_map_start, *efi_map_end, *p;
1102 efi_memory_desc_t *md;
1103 u64 efi_desc_size;
1104 char *name;
1105 unsigned long flags;
1107 efi_map_start = __va(ia64_boot_param->efi_memmap);
1108 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1109 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1111 res = NULL;
1113 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1114 md = p;
1116 if (md->num_pages == 0) /* should not happen */
1117 continue;
1119 flags = IORESOURCE_MEM;
1120 switch (md->type) {
1122 case EFI_MEMORY_MAPPED_IO:
1123 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1124 continue;
1126 case EFI_LOADER_CODE:
1127 case EFI_LOADER_DATA:
1128 case EFI_BOOT_SERVICES_DATA:
1129 case EFI_BOOT_SERVICES_CODE:
1130 case EFI_CONVENTIONAL_MEMORY:
1131 if (md->attribute & EFI_MEMORY_WP) {
1132 name = "System ROM";
1133 flags |= IORESOURCE_READONLY;
1134 } else {
1135 name = "System RAM";
1137 break;
1139 case EFI_ACPI_MEMORY_NVS:
1140 name = "ACPI Non-volatile Storage";
1141 flags |= IORESOURCE_BUSY;
1142 break;
1144 case EFI_UNUSABLE_MEMORY:
1145 name = "reserved";
1146 flags |= IORESOURCE_BUSY | IORESOURCE_DISABLED;
1147 break;
1149 case EFI_RESERVED_TYPE:
1150 case EFI_RUNTIME_SERVICES_CODE:
1151 case EFI_RUNTIME_SERVICES_DATA:
1152 case EFI_ACPI_RECLAIM_MEMORY:
1153 default:
1154 name = "reserved";
1155 flags |= IORESOURCE_BUSY;
1156 break;
1159 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
1160 printk(KERN_ERR "failed to alocate resource for iomem\n");
1161 return;
1164 res->name = name;
1165 res->start = md->phys_addr;
1166 res->end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1;
1167 res->flags = flags;
1169 if (insert_resource(&iomem_resource, res) < 0)
1170 kfree(res);
1171 else {
1172 /*
1173 * We don't know which region contains
1174 * kernel data so we try it repeatedly and
1175 * let the resource manager test it.
1176 */
1177 insert_resource(res, code_resource);
1178 insert_resource(res, data_resource);
1179 #ifdef CONFIG_KEXEC
1180 insert_resource(res, &efi_memmap_res);
1181 insert_resource(res, &boot_param_res);
1182 if (crashk_res.end > crashk_res.start)
1183 insert_resource(res, &crashk_res);
1184 #endif
1188 #endif /* XEN */
1190 #if defined(CONFIG_KEXEC) || defined(XEN)
1191 /* find a block of memory aligned to 64M exclude reserved regions
1192 rsvd_regions are sorted
1193 */
1194 unsigned long __init
1195 kdump_find_rsvd_region (unsigned long size,
1196 struct rsvd_region *r, int n)
1198 int i;
1199 u64 start, end;
1200 u64 alignment = 1UL << _PAGE_SIZE_64M;
1201 void *efi_map_start, *efi_map_end, *p;
1202 efi_memory_desc_t *md;
1203 u64 efi_desc_size;
1205 efi_map_start = __va(ia64_boot_param->efi_memmap);
1206 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1207 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1209 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1210 md = p;
1211 if (!efi_wb(md))
1212 continue;
1213 start = ALIGN(md->phys_addr, alignment);
1214 end = efi_md_end(md);
1215 for (i = 0; i < n; i++) {
1216 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1217 if (__pa(r[i].start) > start + size)
1218 return start;
1219 start = ALIGN(__pa(r[i].end), alignment);
1220 if (i < n-1 && __pa(r[i+1].start) < start + size)
1221 continue;
1222 else
1223 break;
1226 if (end > start + size)
1227 return start;
1230 printk(KERN_WARNING "Cannot reserve 0x%lx byte of memory for crashdump\n",
1231 size);
1232 return ~0UL;
1234 #endif
1236 #ifndef XEN
1237 #ifdef CONFIG_PROC_VMCORE
1238 /* locate the size find a the descriptor at a certain address */
1239 unsigned long
1240 vmcore_find_descriptor_size (unsigned long address)
1242 void *efi_map_start, *efi_map_end, *p;
1243 efi_memory_desc_t *md;
1244 u64 efi_desc_size;
1245 unsigned long ret = 0;
1247 efi_map_start = __va(ia64_boot_param->efi_memmap);
1248 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1249 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1251 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1252 md = p;
1253 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1254 && md->phys_addr == address) {
1255 ret = efi_md_size(md);
1256 break;
1260 if (ret == 0)
1261 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1263 return ret;
1265 #endif
1266 #endif /* XEN */