ia64/xen-unstable

view xen/arch/ia64/linux-xen/efi.c @ 9757:bb99a6e5456a

[IA64] introduce efi_memmap_walk_type

introduce efi_memmap_walk_type() for the next dom0 builder patch.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author awilliam@ldap.hp.com
date Tue Apr 25 13:11:15 2006 -0600 (2006-04-25)
parents f6e8c269f6af
children 0a226de3fc37
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 *
12 * All EFI Runtime Services are not implemented yet as EFI only
13 * supports physical mode addressing on SoftSDV. This is to be fixed
14 * in a future version. --drummond 1999-07-20
15 *
16 * Implemented EFI runtime services and virtual mode calls. --davidm
17 *
18 * Goutham Rao: <goutham.rao@intel.com>
19 * Skip non-WB memory and ignore empty memory ranges.
20 */
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/time.h>
28 #include <asm/io.h>
29 #include <asm/kregs.h>
30 #include <asm/meminit.h>
31 #include <asm/pgtable.h>
32 #include <asm/processor.h>
33 #include <asm/mca.h>
35 #include <linux/efi.h>
37 #define EFI_DEBUG 0
39 extern efi_status_t efi_call_phys (void *, ...);
40 extern unsigned long long memparse (char *ptr, char **retptr);
42 struct efi efi;
43 EXPORT_SYMBOL(efi);
44 static efi_runtime_services_t *runtime;
45 #if defined(XEN) && !defined(CONFIG_VIRTUAL_FRAME_TABLE)
46 // this is a temporary hack to avoid CONFIG_VIRTUAL_MEM_MAP
47 static unsigned long mem_limit = ~0UL, max_addr = 0x100000000;
48 #else
49 static unsigned long mem_limit = ~0UL, max_addr = ~0UL;
50 #endif
52 #define efi_call_virt(f, args...) (*(f))(args)
54 #define STUB_GET_TIME(prefix, adjust_arg) \
55 static efi_status_t \
56 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc) \
57 { \
58 struct ia64_fpreg fr[6]; \
59 efi_time_cap_t *atc = NULL; \
60 efi_status_t ret; \
61 \
62 if (tc) \
63 atc = adjust_arg(tc); \
64 ia64_save_scratch_fpregs(fr); \
65 ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time), adjust_arg(tm), atc); \
66 ia64_load_scratch_fpregs(fr); \
67 return ret; \
68 }
70 #define STUB_SET_TIME(prefix, adjust_arg) \
71 static efi_status_t \
72 prefix##_set_time (efi_time_t *tm) \
73 { \
74 struct ia64_fpreg fr[6]; \
75 efi_status_t ret; \
76 \
77 ia64_save_scratch_fpregs(fr); \
78 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), adjust_arg(tm)); \
79 ia64_load_scratch_fpregs(fr); \
80 return ret; \
81 }
83 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
84 static efi_status_t \
85 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, efi_time_t *tm) \
86 { \
87 struct ia64_fpreg fr[6]; \
88 efi_status_t ret; \
89 \
90 ia64_save_scratch_fpregs(fr); \
91 ret = efi_call_##prefix((efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
92 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
93 ia64_load_scratch_fpregs(fr); \
94 return ret; \
95 }
97 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
98 static efi_status_t \
99 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
100 { \
101 struct ia64_fpreg fr[6]; \
102 efi_time_t *atm = NULL; \
103 efi_status_t ret; \
104 \
105 if (tm) \
106 atm = adjust_arg(tm); \
107 ia64_save_scratch_fpregs(fr); \
108 ret = efi_call_##prefix((efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
109 enabled, atm); \
110 ia64_load_scratch_fpregs(fr); \
111 return ret; \
112 }
114 #define STUB_GET_VARIABLE(prefix, adjust_arg) \
115 static efi_status_t \
116 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
117 unsigned long *data_size, void *data) \
118 { \
119 struct ia64_fpreg fr[6]; \
120 u32 *aattr = NULL; \
121 efi_status_t ret; \
122 \
123 if (attr) \
124 aattr = adjust_arg(attr); \
125 ia64_save_scratch_fpregs(fr); \
126 ret = efi_call_##prefix((efi_get_variable_t *) __va(runtime->get_variable), \
127 adjust_arg(name), adjust_arg(vendor), aattr, \
128 adjust_arg(data_size), adjust_arg(data)); \
129 ia64_load_scratch_fpregs(fr); \
130 return ret; \
131 }
133 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
134 static efi_status_t \
135 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, efi_guid_t *vendor) \
136 { \
137 struct ia64_fpreg fr[6]; \
138 efi_status_t ret; \
139 \
140 ia64_save_scratch_fpregs(fr); \
141 ret = efi_call_##prefix((efi_get_next_variable_t *) __va(runtime->get_next_variable), \
142 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
143 ia64_load_scratch_fpregs(fr); \
144 return ret; \
145 }
147 #define STUB_SET_VARIABLE(prefix, adjust_arg) \
148 static efi_status_t \
149 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, unsigned long attr, \
150 unsigned long data_size, void *data) \
151 { \
152 struct ia64_fpreg fr[6]; \
153 efi_status_t ret; \
154 \
155 ia64_save_scratch_fpregs(fr); \
156 ret = efi_call_##prefix((efi_set_variable_t *) __va(runtime->set_variable), \
157 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
158 adjust_arg(data)); \
159 ia64_load_scratch_fpregs(fr); \
160 return ret; \
161 }
163 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
164 static efi_status_t \
165 prefix##_get_next_high_mono_count (u32 *count) \
166 { \
167 struct ia64_fpreg fr[6]; \
168 efi_status_t ret; \
169 \
170 ia64_save_scratch_fpregs(fr); \
171 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
172 __va(runtime->get_next_high_mono_count), adjust_arg(count)); \
173 ia64_load_scratch_fpregs(fr); \
174 return ret; \
175 }
177 #define STUB_RESET_SYSTEM(prefix, adjust_arg) \
178 static void \
179 prefix##_reset_system (int reset_type, efi_status_t status, \
180 unsigned long data_size, efi_char16_t *data) \
181 { \
182 struct ia64_fpreg fr[6]; \
183 efi_char16_t *adata = NULL; \
184 \
185 if (data) \
186 adata = adjust_arg(data); \
187 \
188 ia64_save_scratch_fpregs(fr); \
189 efi_call_##prefix((efi_reset_system_t *) __va(runtime->reset_system), \
190 reset_type, status, data_size, adata); \
191 /* should not return, but just in case... */ \
192 ia64_load_scratch_fpregs(fr); \
193 }
195 #define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
197 STUB_GET_TIME(phys, phys_ptr)
198 STUB_SET_TIME(phys, phys_ptr)
199 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
200 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
201 STUB_GET_VARIABLE(phys, phys_ptr)
202 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
203 STUB_SET_VARIABLE(phys, phys_ptr)
204 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
205 STUB_RESET_SYSTEM(phys, phys_ptr)
207 #define id(arg) arg
209 STUB_GET_TIME(virt, id)
210 STUB_SET_TIME(virt, id)
211 STUB_GET_WAKEUP_TIME(virt, id)
212 STUB_SET_WAKEUP_TIME(virt, id)
213 STUB_GET_VARIABLE(virt, id)
214 STUB_GET_NEXT_VARIABLE(virt, id)
215 STUB_SET_VARIABLE(virt, id)
216 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
217 STUB_RESET_SYSTEM(virt, id)
219 void
220 efi_gettimeofday (struct timespec *ts)
221 {
222 efi_time_t tm;
224 memset(ts, 0, sizeof(ts));
225 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS)
226 return;
228 ts->tv_sec = mktime(tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);
229 ts->tv_nsec = tm.nanosecond;
230 }
232 static int
233 is_available_memory (efi_memory_desc_t *md)
234 {
235 if (!(md->attribute & EFI_MEMORY_WB))
236 return 0;
238 switch (md->type) {
239 case EFI_LOADER_CODE:
240 case EFI_LOADER_DATA:
241 case EFI_BOOT_SERVICES_CODE:
242 case EFI_BOOT_SERVICES_DATA:
243 case EFI_CONVENTIONAL_MEMORY:
244 return 1;
245 }
246 return 0;
247 }
249 /*
250 * Trim descriptor MD so its starts at address START_ADDR. If the descriptor covers
251 * memory that is normally available to the kernel, issue a warning that some memory
252 * is being ignored.
253 */
254 static void
255 trim_bottom (efi_memory_desc_t *md, u64 start_addr)
256 {
257 u64 num_skipped_pages;
259 if (md->phys_addr >= start_addr || !md->num_pages)
260 return;
262 num_skipped_pages = (start_addr - md->phys_addr) >> EFI_PAGE_SHIFT;
263 if (num_skipped_pages > md->num_pages)
264 num_skipped_pages = md->num_pages;
266 if (is_available_memory(md))
267 printk(KERN_NOTICE "efi.%s: ignoring %luKB of memory at 0x%lx due to granule hole "
268 "at 0x%lx\n", __FUNCTION__,
269 (num_skipped_pages << EFI_PAGE_SHIFT) >> 10,
270 md->phys_addr, start_addr - IA64_GRANULE_SIZE);
271 /*
272 * NOTE: Don't set md->phys_addr to START_ADDR because that could cause the memory
273 * descriptor list to become unsorted. In such a case, md->num_pages will be
274 * zero, so the Right Thing will happen.
275 */
276 md->phys_addr += num_skipped_pages << EFI_PAGE_SHIFT;
277 md->num_pages -= num_skipped_pages;
278 }
280 static void
281 trim_top (efi_memory_desc_t *md, u64 end_addr)
282 {
283 u64 num_dropped_pages, md_end_addr;
285 md_end_addr = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
287 if (md_end_addr <= end_addr || !md->num_pages)
288 return;
290 num_dropped_pages = (md_end_addr - end_addr) >> EFI_PAGE_SHIFT;
291 if (num_dropped_pages > md->num_pages)
292 num_dropped_pages = md->num_pages;
294 if (is_available_memory(md))
295 printk(KERN_NOTICE "efi.%s: ignoring %luKB of memory at 0x%lx due to granule hole "
296 "at 0x%lx\n", __FUNCTION__,
297 (num_dropped_pages << EFI_PAGE_SHIFT) >> 10,
298 md->phys_addr, end_addr);
299 md->num_pages -= num_dropped_pages;
300 }
302 /*
303 * Walks the EFI memory map and calls CALLBACK once for each EFI memory descriptor that
304 * has memory that is available for OS use.
305 */
306 void
307 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
308 {
309 int prev_valid = 0;
310 struct range {
311 u64 start;
312 u64 end;
313 } prev, curr;
314 void *efi_map_start, *efi_map_end, *p, *q;
315 efi_memory_desc_t *md, *check_md;
316 u64 efi_desc_size, start, end, granule_addr, last_granule_addr, first_non_wb_addr = 0;
317 unsigned long total_mem = 0;
319 efi_map_start = __va(ia64_boot_param->efi_memmap);
320 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
321 efi_desc_size = ia64_boot_param->efi_memdesc_size;
323 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
324 md = p;
326 /* skip over non-WB memory descriptors; that's all we're interested in... */
327 if (!(md->attribute & EFI_MEMORY_WB))
328 continue;
330 #ifdef XEN
331 // this works around a problem in the ski bootloader
332 {
333 extern long running_on_sim;
334 if (running_on_sim && md->type != EFI_CONVENTIONAL_MEMORY)
335 continue;
336 }
337 #endif
338 /*
339 * granule_addr is the base of md's first granule.
340 * [granule_addr - first_non_wb_addr) is guaranteed to
341 * be contiguous WB memory.
342 */
343 granule_addr = GRANULEROUNDDOWN(md->phys_addr);
344 first_non_wb_addr = max(first_non_wb_addr, granule_addr);
346 if (first_non_wb_addr < md->phys_addr) {
347 trim_bottom(md, granule_addr + IA64_GRANULE_SIZE);
348 granule_addr = GRANULEROUNDDOWN(md->phys_addr);
349 first_non_wb_addr = max(first_non_wb_addr, granule_addr);
350 }
352 for (q = p; q < efi_map_end; q += efi_desc_size) {
353 check_md = q;
355 if ((check_md->attribute & EFI_MEMORY_WB) &&
356 (check_md->phys_addr == first_non_wb_addr))
357 first_non_wb_addr += check_md->num_pages << EFI_PAGE_SHIFT;
358 else
359 break; /* non-WB or hole */
360 }
362 last_granule_addr = GRANULEROUNDDOWN(first_non_wb_addr);
363 if (last_granule_addr < md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT))
364 trim_top(md, last_granule_addr);
366 if (is_available_memory(md)) {
367 if (md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) >= max_addr) {
368 if (md->phys_addr >= max_addr)
369 continue;
370 md->num_pages = (max_addr - md->phys_addr) >> EFI_PAGE_SHIFT;
371 first_non_wb_addr = max_addr;
372 }
374 if (total_mem >= mem_limit)
375 continue;
377 if (total_mem + (md->num_pages << EFI_PAGE_SHIFT) > mem_limit) {
378 unsigned long limit_addr = md->phys_addr;
380 limit_addr += mem_limit - total_mem;
381 limit_addr = GRANULEROUNDDOWN(limit_addr);
383 if (md->phys_addr > limit_addr)
384 continue;
386 md->num_pages = (limit_addr - md->phys_addr) >>
387 EFI_PAGE_SHIFT;
388 first_non_wb_addr = max_addr = md->phys_addr +
389 (md->num_pages << EFI_PAGE_SHIFT);
390 }
391 total_mem += (md->num_pages << EFI_PAGE_SHIFT);
393 if (md->num_pages == 0)
394 continue;
396 curr.start = PAGE_OFFSET + md->phys_addr;
397 curr.end = curr.start + (md->num_pages << EFI_PAGE_SHIFT);
399 if (!prev_valid) {
400 prev = curr;
401 prev_valid = 1;
402 } else {
403 if (curr.start < prev.start)
404 printk(KERN_ERR "Oops: EFI memory table not ordered!\n");
406 if (prev.end == curr.start) {
407 /* merge two consecutive memory ranges */
408 prev.end = curr.end;
409 } else {
410 start = PAGE_ALIGN(prev.start);
411 end = prev.end & PAGE_MASK;
412 if ((end > start) && (*callback)(start, end, arg) < 0)
413 return;
414 prev = curr;
415 }
416 }
417 }
418 }
419 if (prev_valid) {
420 start = PAGE_ALIGN(prev.start);
421 end = prev.end & PAGE_MASK;
422 if (end > start)
423 (*callback)(start, end, arg);
424 }
425 }
427 /*
428 * Walk the EFI memory map to pull out leftover pages in the lower
429 * memory regions which do not end up in the regular memory map and
430 * stick them into the uncached allocator
431 *
432 * The regular walk function is significantly more complex than the
433 * uncached walk which means it really doesn't make sense to try and
434 * marge the two.
435 */
436 void __init
437 efi_memmap_walk_uc (efi_freemem_callback_t callback)
438 {
439 void *efi_map_start, *efi_map_end, *p;
440 efi_memory_desc_t *md;
441 u64 efi_desc_size, start, end;
443 efi_map_start = __va(ia64_boot_param->efi_memmap);
444 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
445 efi_desc_size = ia64_boot_param->efi_memdesc_size;
447 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
448 md = p;
449 if (md->attribute == EFI_MEMORY_UC) {
450 start = PAGE_ALIGN(md->phys_addr);
451 end = PAGE_ALIGN((md->phys_addr+(md->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK);
452 if ((*callback)(start, end, NULL) < 0)
453 return;
454 }
455 }
456 }
458 #ifdef XEN
459 void
460 efi_memmap_walk_type(u32 type, efi_walk_type_callback_t callback, void *arg)
461 {
462 void *efi_map_start, *efi_map_end, *p;
463 efi_memory_desc_t *md;
464 u64 efi_desc_size;
466 efi_map_start = __va(ia64_boot_param->efi_memmap);
467 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
468 efi_desc_size = ia64_boot_param->efi_memdesc_size;
470 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
471 md = p;
472 if (md->type == type) {
473 if ((*callback)(md, arg) < 0)
474 return;
475 }
476 }
477 }
478 #endif
480 /*
481 * Look for the PAL_CODE region reported by EFI and maps it using an
482 * ITR to enable safe PAL calls in virtual mode. See IA-64 Processor
483 * Abstraction Layer chapter 11 in ADAG
484 */
486 void *
487 efi_get_pal_addr (void)
488 {
489 void *efi_map_start, *efi_map_end, *p;
490 efi_memory_desc_t *md;
491 u64 efi_desc_size;
492 int pal_code_count = 0;
493 u64 vaddr, mask;
495 efi_map_start = __va(ia64_boot_param->efi_memmap);
496 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
497 efi_desc_size = ia64_boot_param->efi_memdesc_size;
499 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
500 md = p;
501 if (md->type != EFI_PAL_CODE)
502 continue;
504 if (++pal_code_count > 1) {
505 printk(KERN_ERR "Too many EFI Pal Code memory ranges, dropped @ %lx\n",
506 md->phys_addr);
507 continue;
508 }
509 /*
510 * The only ITLB entry in region 7 that is used is the one installed by
511 * __start(). That entry covers a 64MB range.
512 */
513 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
514 vaddr = PAGE_OFFSET + md->phys_addr;
516 /*
517 * We must check that the PAL mapping won't overlap with the kernel
518 * mapping.
519 *
520 * PAL code is guaranteed to be aligned on a power of 2 between 4k and
521 * 256KB and that only one ITR is needed to map it. This implies that the
522 * PAL code is always aligned on its size, i.e., the closest matching page
523 * size supported by the TLB. Therefore PAL code is guaranteed never to
524 * cross a 64MB unless it is bigger than 64MB (very unlikely!). So for
525 * now the following test is enough to determine whether or not we need a
526 * dedicated ITR for the PAL code.
527 */
528 if ((vaddr & mask) == (KERNEL_START & mask)) {
529 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
530 __FUNCTION__);
531 continue;
532 }
534 if (md->num_pages << EFI_PAGE_SHIFT > IA64_GRANULE_SIZE)
535 panic("Woah! PAL code size bigger than a granule!");
537 #if EFI_DEBUG
538 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
540 printk(KERN_INFO "CPU %d: mapping PAL code [0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
541 smp_processor_id(), md->phys_addr,
542 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
543 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
544 #endif
545 return __va(md->phys_addr);
546 }
547 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found",
548 __FUNCTION__);
549 return NULL;
550 }
553 #ifdef XEN
554 void *pal_vaddr = 0;
555 #endif
557 void
558 efi_map_pal_code (void)
559 {
560 #ifdef XEN
561 u64 psr;
562 if (!pal_vaddr) {
563 pal_vaddr = efi_get_pal_addr ();
564 }
565 #else
566 void *pal_vaddr = efi_get_pal_addr ();
567 u64 psr;
569 if (!pal_vaddr)
570 return;
571 #endif
573 /*
574 * Cannot write to CRx with PSR.ic=1
575 */
576 psr = ia64_clear_ic();
577 ia64_itr(0x1, IA64_TR_PALCODE, GRANULEROUNDDOWN((unsigned long) pal_vaddr),
578 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
579 IA64_GRANULE_SHIFT);
580 ia64_set_psr(psr); /* restore psr */
581 ia64_srlz_i();
582 }
584 void __init
585 efi_init (void)
586 {
587 void *efi_map_start, *efi_map_end;
588 efi_config_table_t *config_tables;
589 efi_char16_t *c16;
590 u64 efi_desc_size;
591 char *cp, *end, vendor[100] = "unknown";
592 extern char saved_command_line[];
593 int i;
595 /* it's too early to be able to use the standard kernel command line support... */
596 for (cp = saved_command_line; *cp; ) {
597 if (memcmp(cp, "mem=", 4) == 0) {
598 cp += 4;
599 mem_limit = memparse(cp, &end);
600 if (end != cp)
601 break;
602 cp = end;
603 } else if (memcmp(cp, "max_addr=", 9) == 0) {
604 cp += 9;
605 max_addr = GRANULEROUNDDOWN(memparse(cp, &end));
606 if (end != cp)
607 break;
608 cp = end;
609 } else {
610 while (*cp != ' ' && *cp)
611 ++cp;
612 while (*cp == ' ')
613 ++cp;
614 }
615 }
616 if (max_addr != ~0UL)
617 printk(KERN_INFO "Ignoring memory above %luMB\n", max_addr >> 20);
619 efi.systab = __va(ia64_boot_param->efi_systab);
621 /*
622 * Verify the EFI Table
623 */
624 if (efi.systab == NULL)
625 panic("Woah! Can't find EFI system table.\n");
626 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
627 panic("Woah! EFI system table signature incorrect\n");
628 if ((efi.systab->hdr.revision ^ EFI_SYSTEM_TABLE_REVISION) >> 16 != 0)
629 printk(KERN_WARNING "Warning: EFI system table major version mismatch: "
630 "got %d.%02d, expected %d.%02d\n",
631 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff,
632 EFI_SYSTEM_TABLE_REVISION >> 16, EFI_SYSTEM_TABLE_REVISION & 0xffff);
634 config_tables = __va(efi.systab->tables);
636 /* Show what we know for posterity */
637 c16 = __va(efi.systab->fw_vendor);
638 if (c16) {
639 for (i = 0;i < (int) sizeof(vendor) && *c16; ++i)
640 vendor[i] = *c16++;
641 vendor[i] = '\0';
642 }
644 printk(KERN_INFO "EFI v%u.%.02u by %s:",
645 efi.systab->hdr.revision >> 16, efi.systab->hdr.revision & 0xffff, vendor);
647 for (i = 0; i < (int) efi.systab->nr_tables; i++) {
648 if (efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID) == 0) {
649 efi.mps = __va(config_tables[i].table);
650 printk(" MPS=0x%lx", config_tables[i].table);
651 } else if (efi_guidcmp(config_tables[i].guid, ACPI_20_TABLE_GUID) == 0) {
652 efi.acpi20 = __va(config_tables[i].table);
653 printk(" ACPI 2.0=0x%lx", config_tables[i].table);
654 } else if (efi_guidcmp(config_tables[i].guid, ACPI_TABLE_GUID) == 0) {
655 efi.acpi = __va(config_tables[i].table);
656 printk(" ACPI=0x%lx", config_tables[i].table);
657 } else if (efi_guidcmp(config_tables[i].guid, SMBIOS_TABLE_GUID) == 0) {
658 efi.smbios = __va(config_tables[i].table);
659 printk(" SMBIOS=0x%lx", config_tables[i].table);
660 } else if (efi_guidcmp(config_tables[i].guid, SAL_SYSTEM_TABLE_GUID) == 0) {
661 efi.sal_systab = __va(config_tables[i].table);
662 printk(" SALsystab=0x%lx", config_tables[i].table);
663 } else if (efi_guidcmp(config_tables[i].guid, HCDP_TABLE_GUID) == 0) {
664 efi.hcdp = __va(config_tables[i].table);
665 printk(" HCDP=0x%lx", config_tables[i].table);
666 }
667 }
668 printk("\n");
670 runtime = __va(efi.systab->runtime);
671 efi.get_time = phys_get_time;
672 efi.set_time = phys_set_time;
673 efi.get_wakeup_time = phys_get_wakeup_time;
674 efi.set_wakeup_time = phys_set_wakeup_time;
675 efi.get_variable = phys_get_variable;
676 efi.get_next_variable = phys_get_next_variable;
677 efi.set_variable = phys_set_variable;
678 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
679 efi.reset_system = phys_reset_system;
681 efi_map_start = __va(ia64_boot_param->efi_memmap);
682 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
683 efi_desc_size = ia64_boot_param->efi_memdesc_size;
685 #if EFI_DEBUG
686 /* print EFI memory map: */
687 {
688 efi_memory_desc_t *md;
689 void *p;
691 for (i = 0, p = efi_map_start; p < efi_map_end; ++i, p += efi_desc_size) {
692 md = p;
693 printk("mem%02u: type=%u, attr=0x%lx, range=[0x%016lx-0x%016lx) (%luMB)\n",
694 i, md->type, md->attribute, md->phys_addr,
695 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
696 md->num_pages >> (20 - EFI_PAGE_SHIFT));
697 }
698 }
699 #endif
701 efi_map_pal_code();
702 efi_enter_virtual_mode();
703 }
705 void
706 efi_enter_virtual_mode (void)
707 {
708 void *efi_map_start, *efi_map_end, *p;
709 efi_memory_desc_t *md;
710 efi_status_t status;
711 u64 efi_desc_size;
713 efi_map_start = __va(ia64_boot_param->efi_memmap);
714 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
715 efi_desc_size = ia64_boot_param->efi_memdesc_size;
717 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
718 md = p;
719 if (md->attribute & EFI_MEMORY_RUNTIME) {
720 /*
721 * Some descriptors have multiple bits set, so the order of
722 * the tests is relevant.
723 */
724 if (md->attribute & EFI_MEMORY_WB) {
725 md->virt_addr = (u64) __va(md->phys_addr);
726 } else if (md->attribute & EFI_MEMORY_UC) {
727 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
728 } else if (md->attribute & EFI_MEMORY_WC) {
729 #if 0
730 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
731 | _PAGE_D
732 | _PAGE_MA_WC
733 | _PAGE_PL_0
734 | _PAGE_AR_RW));
735 #else
736 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
737 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
738 #endif
739 } else if (md->attribute & EFI_MEMORY_WT) {
740 #if 0
741 md->virt_addr = ia64_remap(md->phys_addr, (_PAGE_A | _PAGE_P
742 | _PAGE_D | _PAGE_MA_WT
743 | _PAGE_PL_0
744 | _PAGE_AR_RW));
745 #else
746 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
747 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
748 #endif
749 }
750 }
751 }
753 status = efi_call_phys(__va(runtime->set_virtual_address_map),
754 ia64_boot_param->efi_memmap_size,
755 efi_desc_size, ia64_boot_param->efi_memdesc_version,
756 ia64_boot_param->efi_memmap);
757 if (status != EFI_SUCCESS) {
758 printk(KERN_WARNING "warning: unable to switch EFI into virtual mode "
759 "(status=%lu)\n", status);
760 return;
761 }
763 /*
764 * Now that EFI is in virtual mode, we call the EFI functions more efficiently:
765 */
766 efi.get_time = virt_get_time;
767 efi.set_time = virt_set_time;
768 efi.get_wakeup_time = virt_get_wakeup_time;
769 efi.set_wakeup_time = virt_set_wakeup_time;
770 efi.get_variable = virt_get_variable;
771 efi.get_next_variable = virt_get_next_variable;
772 efi.set_variable = virt_set_variable;
773 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
774 efi.reset_system = virt_reset_system;
775 }
777 /*
778 * Walk the EFI memory map looking for the I/O port range. There can only be one entry of
779 * this type, other I/O port ranges should be described via ACPI.
780 */
781 u64
782 efi_get_iobase (void)
783 {
784 void *efi_map_start, *efi_map_end, *p;
785 efi_memory_desc_t *md;
786 u64 efi_desc_size;
788 efi_map_start = __va(ia64_boot_param->efi_memmap);
789 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
790 efi_desc_size = ia64_boot_param->efi_memdesc_size;
792 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
793 md = p;
794 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
795 if (md->attribute & EFI_MEMORY_UC)
796 return md->phys_addr;
797 }
798 }
799 return 0;
800 }
802 #ifdef XEN
803 // variation of efi_get_iobase which returns entire memory descriptor
804 efi_memory_desc_t *
805 efi_get_io_md (void)
806 {
807 void *efi_map_start, *efi_map_end, *p;
808 efi_memory_desc_t *md;
809 u64 efi_desc_size;
811 efi_map_start = __va(ia64_boot_param->efi_memmap);
812 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
813 efi_desc_size = ia64_boot_param->efi_memdesc_size;
815 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
816 md = p;
817 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
818 if (md->attribute & EFI_MEMORY_UC)
819 return md;
820 }
821 }
822 return 0;
823 }
824 #endif
826 u32
827 efi_mem_type (unsigned long phys_addr)
828 {
829 void *efi_map_start, *efi_map_end, *p;
830 efi_memory_desc_t *md;
831 u64 efi_desc_size;
833 efi_map_start = __va(ia64_boot_param->efi_memmap);
834 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
835 efi_desc_size = ia64_boot_param->efi_memdesc_size;
837 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
838 md = p;
840 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
841 return md->type;
842 }
843 return 0;
844 }
846 u64
847 efi_mem_attributes (unsigned long phys_addr)
848 {
849 void *efi_map_start, *efi_map_end, *p;
850 efi_memory_desc_t *md;
851 u64 efi_desc_size;
853 efi_map_start = __va(ia64_boot_param->efi_memmap);
854 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
855 efi_desc_size = ia64_boot_param->efi_memdesc_size;
857 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
858 md = p;
860 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT))
861 return md->attribute;
862 }
863 return 0;
864 }
865 EXPORT_SYMBOL(efi_mem_attributes);
867 int
868 valid_phys_addr_range (unsigned long phys_addr, unsigned long *size)
869 {
870 void *efi_map_start, *efi_map_end, *p;
871 efi_memory_desc_t *md;
872 u64 efi_desc_size;
874 efi_map_start = __va(ia64_boot_param->efi_memmap);
875 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
876 efi_desc_size = ia64_boot_param->efi_memdesc_size;
878 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
879 md = p;
881 if (phys_addr - md->phys_addr < (md->num_pages << EFI_PAGE_SHIFT)) {
882 if (!(md->attribute & EFI_MEMORY_WB))
883 return 0;
885 if (*size > md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - phys_addr)
886 *size = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - phys_addr;
887 return 1;
888 }
889 }
890 return 0;
891 }
893 int __init
894 efi_uart_console_only(void)
895 {
896 efi_status_t status;
897 char *s, name[] = "ConOut";
898 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
899 efi_char16_t *utf16, name_utf16[32];
900 unsigned char data[1024];
901 unsigned long size = sizeof(data);
902 struct efi_generic_dev_path *hdr, *end_addr;
903 int uart = 0;
905 /* Convert to UTF-16 */
906 utf16 = name_utf16;
907 s = name;
908 while (*s)
909 *utf16++ = *s++ & 0x7f;
910 *utf16 = 0;
912 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
913 if (status != EFI_SUCCESS) {
914 printk(KERN_ERR "No EFI %s variable?\n", name);
915 return 0;
916 }
918 hdr = (struct efi_generic_dev_path *) data;
919 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
920 while (hdr < end_addr) {
921 if (hdr->type == EFI_DEV_MSG &&
922 hdr->sub_type == EFI_DEV_MSG_UART)
923 uart = 1;
924 else if (hdr->type == EFI_DEV_END_PATH ||
925 hdr->type == EFI_DEV_END_PATH2) {
926 if (!uart)
927 return 0;
928 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
929 return 1;
930 uart = 0;
931 }
932 hdr = (struct efi_generic_dev_path *) ((u8 *) hdr + hdr->length);
933 }
934 printk(KERN_ERR "Malformed %s value\n", name);
935 return 0;
936 }