1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/bootmem.h>
26#include <linux/crash_dump.h>
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/slab.h>
31#include <linux/time.h>
32#include <linux/efi.h>
33#include <linux/kexec.h>
34#include <linux/mm.h>
35
36#include <asm/io.h>
37#include <asm/kregs.h>
38#include <asm/meminit.h>
39#include <asm/pgtable.h>
40#include <asm/processor.h>
41#include <asm/mca.h>
42#include <asm/setup.h>
43#include <asm/tlbflush.h>
44
45#define EFI_DEBUG 0
46
47static __initdata unsigned long palo_phys;
48
49static __initdata efi_config_table_type_t arch_tables[] = {
50 {PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID, "PALO", &palo_phys},
51 {NULL_GUID, NULL, 0},
52};
53
54extern efi_status_t efi_call_phys (void *, ...);
55
56static efi_runtime_services_t *runtime;
57static u64 mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
58
59#define efi_call_virt(f, args...) (*(f))(args)
60
61#define STUB_GET_TIME(prefix, adjust_arg) \
62static efi_status_t \
63prefix##_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), \
73 adjust_arg(tm), atc); \
74 ia64_load_scratch_fpregs(fr); \
75 return ret; \
76}
77
78#define STUB_SET_TIME(prefix, adjust_arg) \
79static efi_status_t \
80prefix##_set_time (efi_time_t *tm) \
81{ \
82 struct ia64_fpreg fr[6]; \
83 efi_status_t ret; \
84 \
85 ia64_save_scratch_fpregs(fr); \
86 ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time), \
87 adjust_arg(tm)); \
88 ia64_load_scratch_fpregs(fr); \
89 return ret; \
90}
91
92#define STUB_GET_WAKEUP_TIME(prefix, adjust_arg) \
93static efi_status_t \
94prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending, \
95 efi_time_t *tm) \
96{ \
97 struct ia64_fpreg fr[6]; \
98 efi_status_t ret; \
99 \
100 ia64_save_scratch_fpregs(fr); \
101 ret = efi_call_##prefix( \
102 (efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time), \
103 adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm)); \
104 ia64_load_scratch_fpregs(fr); \
105 return ret; \
106}
107
108#define STUB_SET_WAKEUP_TIME(prefix, adjust_arg) \
109static efi_status_t \
110prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm) \
111{ \
112 struct ia64_fpreg fr[6]; \
113 efi_time_t *atm = NULL; \
114 efi_status_t ret; \
115 \
116 if (tm) \
117 atm = adjust_arg(tm); \
118 ia64_save_scratch_fpregs(fr); \
119 ret = efi_call_##prefix( \
120 (efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time), \
121 enabled, atm); \
122 ia64_load_scratch_fpregs(fr); \
123 return ret; \
124}
125
126#define STUB_GET_VARIABLE(prefix, adjust_arg) \
127static efi_status_t \
128prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr, \
129 unsigned long *data_size, void *data) \
130{ \
131 struct ia64_fpreg fr[6]; \
132 u32 *aattr = NULL; \
133 efi_status_t ret; \
134 \
135 if (attr) \
136 aattr = adjust_arg(attr); \
137 ia64_save_scratch_fpregs(fr); \
138 ret = efi_call_##prefix( \
139 (efi_get_variable_t *) __va(runtime->get_variable), \
140 adjust_arg(name), adjust_arg(vendor), aattr, \
141 adjust_arg(data_size), adjust_arg(data)); \
142 ia64_load_scratch_fpregs(fr); \
143 return ret; \
144}
145
146#define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg) \
147static efi_status_t \
148prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name, \
149 efi_guid_t *vendor) \
150{ \
151 struct ia64_fpreg fr[6]; \
152 efi_status_t ret; \
153 \
154 ia64_save_scratch_fpregs(fr); \
155 ret = efi_call_##prefix( \
156 (efi_get_next_variable_t *) __va(runtime->get_next_variable), \
157 adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor)); \
158 ia64_load_scratch_fpregs(fr); \
159 return ret; \
160}
161
162#define STUB_SET_VARIABLE(prefix, adjust_arg) \
163static efi_status_t \
164prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor, \
165 u32 attr, unsigned long data_size, \
166 void *data) \
167{ \
168 struct ia64_fpreg fr[6]; \
169 efi_status_t ret; \
170 \
171 ia64_save_scratch_fpregs(fr); \
172 ret = efi_call_##prefix( \
173 (efi_set_variable_t *) __va(runtime->set_variable), \
174 adjust_arg(name), adjust_arg(vendor), attr, data_size, \
175 adjust_arg(data)); \
176 ia64_load_scratch_fpregs(fr); \
177 return ret; \
178}
179
180#define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg) \
181static efi_status_t \
182prefix##_get_next_high_mono_count (u32 *count) \
183{ \
184 struct ia64_fpreg fr[6]; \
185 efi_status_t ret; \
186 \
187 ia64_save_scratch_fpregs(fr); \
188 ret = efi_call_##prefix((efi_get_next_high_mono_count_t *) \
189 __va(runtime->get_next_high_mono_count), \
190 adjust_arg(count)); \
191 ia64_load_scratch_fpregs(fr); \
192 return ret; \
193}
194
195#define STUB_RESET_SYSTEM(prefix, adjust_arg) \
196static void \
197prefix##_reset_system (int reset_type, efi_status_t status, \
198 unsigned long data_size, efi_char16_t *data) \
199{ \
200 struct ia64_fpreg fr[6]; \
201 efi_char16_t *adata = NULL; \
202 \
203 if (data) \
204 adata = adjust_arg(data); \
205 \
206 ia64_save_scratch_fpregs(fr); \
207 efi_call_##prefix( \
208 (efi_reset_system_t *) __va(runtime->reset_system), \
209 reset_type, status, data_size, adata); \
210 \
211 ia64_load_scratch_fpregs(fr); \
212}
213
214#define phys_ptr(arg) ((__typeof__(arg)) ia64_tpa(arg))
215
216STUB_GET_TIME(phys, phys_ptr)
217STUB_SET_TIME(phys, phys_ptr)
218STUB_GET_WAKEUP_TIME(phys, phys_ptr)
219STUB_SET_WAKEUP_TIME(phys, phys_ptr)
220STUB_GET_VARIABLE(phys, phys_ptr)
221STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
222STUB_SET_VARIABLE(phys, phys_ptr)
223STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
224STUB_RESET_SYSTEM(phys, phys_ptr)
225
226#define id(arg) arg
227
228STUB_GET_TIME(virt, id)
229STUB_SET_TIME(virt, id)
230STUB_GET_WAKEUP_TIME(virt, id)
231STUB_SET_WAKEUP_TIME(virt, id)
232STUB_GET_VARIABLE(virt, id)
233STUB_GET_NEXT_VARIABLE(virt, id)
234STUB_SET_VARIABLE(virt, id)
235STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
236STUB_RESET_SYSTEM(virt, id)
237
238void
239efi_gettimeofday (struct timespec *ts)
240{
241 efi_time_t tm;
242
243 if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
244 memset(ts, 0, sizeof(*ts));
245 return;
246 }
247
248 ts->tv_sec = mktime(tm.year, tm.month, tm.day,
249 tm.hour, tm.minute, tm.second);
250 ts->tv_nsec = tm.nanosecond;
251}
252
253static int
254is_memory_available (efi_memory_desc_t *md)
255{
256 if (!(md->attribute & EFI_MEMORY_WB))
257 return 0;
258
259 switch (md->type) {
260 case EFI_LOADER_CODE:
261 case EFI_LOADER_DATA:
262 case EFI_BOOT_SERVICES_CODE:
263 case EFI_BOOT_SERVICES_DATA:
264 case EFI_CONVENTIONAL_MEMORY:
265 return 1;
266 }
267 return 0;
268}
269
270typedef struct kern_memdesc {
271 u64 attribute;
272 u64 start;
273 u64 num_pages;
274} kern_memdesc_t;
275
276static kern_memdesc_t *kern_memmap;
277
278#define efi_md_size(md) (md->num_pages << EFI_PAGE_SHIFT)
279
280static inline u64
281kmd_end(kern_memdesc_t *kmd)
282{
283 return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
284}
285
286static inline u64
287efi_md_end(efi_memory_desc_t *md)
288{
289 return (md->phys_addr + efi_md_size(md));
290}
291
292static inline int
293efi_wb(efi_memory_desc_t *md)
294{
295 return (md->attribute & EFI_MEMORY_WB);
296}
297
298static inline int
299efi_uc(efi_memory_desc_t *md)
300{
301 return (md->attribute & EFI_MEMORY_UC);
302}
303
304static void
305walk (efi_freemem_callback_t callback, void *arg, u64 attr)
306{
307 kern_memdesc_t *k;
308 u64 start, end, voff;
309
310 voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
311 for (k = kern_memmap; k->start != ~0UL; k++) {
312 if (k->attribute != attr)
313 continue;
314 start = PAGE_ALIGN(k->start);
315 end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
316 if (start < end)
317 if ((*callback)(start + voff, end + voff, arg) < 0)
318 return;
319 }
320}
321
322
323
324
325
326void
327efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
328{
329 walk(callback, arg, EFI_MEMORY_WB);
330}
331
332
333
334
335
336void
337efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
338{
339 walk(callback, arg, EFI_MEMORY_UC);
340}
341
342
343
344
345
346
347void *
348efi_get_pal_addr (void)
349{
350 void *efi_map_start, *efi_map_end, *p;
351 efi_memory_desc_t *md;
352 u64 efi_desc_size;
353 int pal_code_count = 0;
354 u64 vaddr, mask;
355
356 efi_map_start = __va(ia64_boot_param->efi_memmap);
357 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
358 efi_desc_size = ia64_boot_param->efi_memdesc_size;
359
360 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
361 md = p;
362 if (md->type != EFI_PAL_CODE)
363 continue;
364
365 if (++pal_code_count > 1) {
366 printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
367 "dropped @ %llx\n", md->phys_addr);
368 continue;
369 }
370
371
372
373
374 mask = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
375 vaddr = PAGE_OFFSET + md->phys_addr;
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390 if ((vaddr & mask) == (KERNEL_START & mask)) {
391 printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
392 __func__);
393 continue;
394 }
395
396 if (efi_md_size(md) > IA64_GRANULE_SIZE)
397 panic("Whoa! PAL code size bigger than a granule!");
398
399#if EFI_DEBUG
400 mask = ~((1 << IA64_GRANULE_SHIFT) - 1);
401
402 printk(KERN_INFO "CPU %d: mapping PAL code "
403 "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
404 smp_processor_id(), md->phys_addr,
405 md->phys_addr + efi_md_size(md),
406 vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
407#endif
408 return __va(md->phys_addr);
409 }
410 printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
411 __func__);
412 return NULL;
413}
414
415
416static u8 __init palo_checksum(u8 *buffer, u32 length)
417{
418 u8 sum = 0;
419 u8 *end = buffer + length;
420
421 while (buffer < end)
422 sum = (u8) (sum + *(buffer++));
423
424 return sum;
425}
426
427
428
429
430
431static void __init handle_palo(unsigned long phys_addr)
432{
433 struct palo_table *palo = __va(phys_addr);
434 u8 checksum;
435
436 if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
437 printk(KERN_INFO "PALO signature incorrect.\n");
438 return;
439 }
440
441 checksum = palo_checksum((u8 *)palo, palo->length);
442 if (checksum) {
443 printk(KERN_INFO "PALO checksum incorrect.\n");
444 return;
445 }
446
447 setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
448}
449
450void
451efi_map_pal_code (void)
452{
453 void *pal_vaddr = efi_get_pal_addr ();
454 u64 psr;
455
456 if (!pal_vaddr)
457 return;
458
459
460
461
462 psr = ia64_clear_ic();
463 ia64_itr(0x1, IA64_TR_PALCODE,
464 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
465 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
466 IA64_GRANULE_SHIFT);
467 paravirt_dv_serialize_data();
468 ia64_set_psr(psr);
469}
470
471void __init
472efi_init (void)
473{
474 void *efi_map_start, *efi_map_end;
475 efi_char16_t *c16;
476 u64 efi_desc_size;
477 char *cp, vendor[100] = "unknown";
478 int i;
479
480 set_bit(EFI_BOOT, &efi.flags);
481 set_bit(EFI_64BIT, &efi.flags);
482
483
484
485
486
487 for (cp = boot_command_line; *cp; ) {
488 if (memcmp(cp, "mem=", 4) == 0) {
489 mem_limit = memparse(cp + 4, &cp);
490 } else if (memcmp(cp, "max_addr=", 9) == 0) {
491 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
492 } else if (memcmp(cp, "min_addr=", 9) == 0) {
493 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
494 } else {
495 while (*cp != ' ' && *cp)
496 ++cp;
497 while (*cp == ' ')
498 ++cp;
499 }
500 }
501 if (min_addr != 0UL)
502 printk(KERN_INFO "Ignoring memory below %lluMB\n",
503 min_addr >> 20);
504 if (max_addr != ~0UL)
505 printk(KERN_INFO "Ignoring memory above %lluMB\n",
506 max_addr >> 20);
507
508 efi.systab = __va(ia64_boot_param->efi_systab);
509
510
511
512
513 if (efi.systab == NULL)
514 panic("Whoa! Can't find EFI system table.\n");
515 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
516 panic("Whoa! EFI system table signature incorrect\n");
517 if ((efi.systab->hdr.revision >> 16) == 0)
518 printk(KERN_WARNING "Warning: EFI system table version "
519 "%d.%02d, expected 1.00 or greater\n",
520 efi.systab->hdr.revision >> 16,
521 efi.systab->hdr.revision & 0xffff);
522
523
524 c16 = __va(efi.systab->fw_vendor);
525 if (c16) {
526 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
527 vendor[i] = *c16++;
528 vendor[i] = '\0';
529 }
530
531 printk(KERN_INFO "EFI v%u.%.02u by %s:",
532 efi.systab->hdr.revision >> 16,
533 efi.systab->hdr.revision & 0xffff, vendor);
534
535 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
536
537 palo_phys = EFI_INVALID_TABLE_ADDR;
538
539 if (efi_config_init(arch_tables) != 0)
540 return;
541
542 if (palo_phys != EFI_INVALID_TABLE_ADDR)
543 handle_palo(palo_phys);
544
545 runtime = __va(efi.systab->runtime);
546 efi.get_time = phys_get_time;
547 efi.set_time = phys_set_time;
548 efi.get_wakeup_time = phys_get_wakeup_time;
549 efi.set_wakeup_time = phys_set_wakeup_time;
550 efi.get_variable = phys_get_variable;
551 efi.get_next_variable = phys_get_next_variable;
552 efi.set_variable = phys_set_variable;
553 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
554 efi.reset_system = phys_reset_system;
555
556 efi_map_start = __va(ia64_boot_param->efi_memmap);
557 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
558 efi_desc_size = ia64_boot_param->efi_memdesc_size;
559
560#if EFI_DEBUG
561
562 {
563 efi_memory_desc_t *md;
564 void *p;
565
566 for (i = 0, p = efi_map_start; p < efi_map_end;
567 ++i, p += efi_desc_size)
568 {
569 const char *unit;
570 unsigned long size;
571
572 md = p;
573 size = md->num_pages << EFI_PAGE_SHIFT;
574
575 if ((size >> 40) > 0) {
576 size >>= 40;
577 unit = "TB";
578 } else if ((size >> 30) > 0) {
579 size >>= 30;
580 unit = "GB";
581 } else if ((size >> 20) > 0) {
582 size >>= 20;
583 unit = "MB";
584 } else {
585 size >>= 10;
586 unit = "KB";
587 }
588
589 printk("mem%02d: type=%2u, attr=0x%016lx, "
590 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
591 i, md->type, md->attribute, md->phys_addr,
592 md->phys_addr + efi_md_size(md), size, unit);
593 }
594 }
595#endif
596
597 efi_map_pal_code();
598 efi_enter_virtual_mode();
599}
600
601void
602efi_enter_virtual_mode (void)
603{
604 void *efi_map_start, *efi_map_end, *p;
605 efi_memory_desc_t *md;
606 efi_status_t status;
607 u64 efi_desc_size;
608
609 efi_map_start = __va(ia64_boot_param->efi_memmap);
610 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
611 efi_desc_size = ia64_boot_param->efi_memdesc_size;
612
613 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
614 md = p;
615 if (md->attribute & EFI_MEMORY_RUNTIME) {
616
617
618
619
620 if (md->attribute & EFI_MEMORY_WB) {
621 md->virt_addr = (u64) __va(md->phys_addr);
622 } else if (md->attribute & EFI_MEMORY_UC) {
623 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
624 } else if (md->attribute & EFI_MEMORY_WC) {
625#if 0
626 md->virt_addr = ia64_remap(md->phys_addr,
627 (_PAGE_A |
628 _PAGE_P |
629 _PAGE_D |
630 _PAGE_MA_WC |
631 _PAGE_PL_0 |
632 _PAGE_AR_RW));
633#else
634 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
635 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
636#endif
637 } else if (md->attribute & EFI_MEMORY_WT) {
638#if 0
639 md->virt_addr = ia64_remap(md->phys_addr,
640 (_PAGE_A |
641 _PAGE_P |
642 _PAGE_D |
643 _PAGE_MA_WT |
644 _PAGE_PL_0 |
645 _PAGE_AR_RW));
646#else
647 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
648 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
649#endif
650 }
651 }
652 }
653
654 status = efi_call_phys(__va(runtime->set_virtual_address_map),
655 ia64_boot_param->efi_memmap_size,
656 efi_desc_size,
657 ia64_boot_param->efi_memdesc_version,
658 ia64_boot_param->efi_memmap);
659 if (status != EFI_SUCCESS) {
660 printk(KERN_WARNING "warning: unable to switch EFI into "
661 "virtual mode (status=%lu)\n", status);
662 return;
663 }
664
665 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
666
667
668
669
670
671 efi.get_time = virt_get_time;
672 efi.set_time = virt_set_time;
673 efi.get_wakeup_time = virt_get_wakeup_time;
674 efi.set_wakeup_time = virt_set_wakeup_time;
675 efi.get_variable = virt_get_variable;
676 efi.get_next_variable = virt_get_next_variable;
677 efi.set_variable = virt_set_variable;
678 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
679 efi.reset_system = virt_reset_system;
680}
681
682
683
684
685
686u64
687efi_get_iobase (void)
688{
689 void *efi_map_start, *efi_map_end, *p;
690 efi_memory_desc_t *md;
691 u64 efi_desc_size;
692
693 efi_map_start = __va(ia64_boot_param->efi_memmap);
694 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
695 efi_desc_size = ia64_boot_param->efi_memdesc_size;
696
697 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
698 md = p;
699 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
700 if (md->attribute & EFI_MEMORY_UC)
701 return md->phys_addr;
702 }
703 }
704 return 0;
705}
706
707static struct kern_memdesc *
708kern_memory_descriptor (unsigned long phys_addr)
709{
710 struct kern_memdesc *md;
711
712 for (md = kern_memmap; md->start != ~0UL; md++) {
713 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
714 return md;
715 }
716 return NULL;
717}
718
719static efi_memory_desc_t *
720efi_memory_descriptor (unsigned long phys_addr)
721{
722 void *efi_map_start, *efi_map_end, *p;
723 efi_memory_desc_t *md;
724 u64 efi_desc_size;
725
726 efi_map_start = __va(ia64_boot_param->efi_memmap);
727 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
728 efi_desc_size = ia64_boot_param->efi_memdesc_size;
729
730 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
731 md = p;
732
733 if (phys_addr - md->phys_addr < efi_md_size(md))
734 return md;
735 }
736 return NULL;
737}
738
739static int
740efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
741{
742 void *efi_map_start, *efi_map_end, *p;
743 efi_memory_desc_t *md;
744 u64 efi_desc_size;
745 unsigned long end;
746
747 efi_map_start = __va(ia64_boot_param->efi_memmap);
748 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
749 efi_desc_size = ia64_boot_param->efi_memdesc_size;
750
751 end = phys_addr + size;
752
753 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
754 md = p;
755 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
756 return 1;
757 }
758 return 0;
759}
760
761u32
762efi_mem_type (unsigned long phys_addr)
763{
764 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
765
766 if (md)
767 return md->type;
768 return 0;
769}
770
771u64
772efi_mem_attributes (unsigned long phys_addr)
773{
774 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
775
776 if (md)
777 return md->attribute;
778 return 0;
779}
780EXPORT_SYMBOL(efi_mem_attributes);
781
782u64
783efi_mem_attribute (unsigned long phys_addr, unsigned long size)
784{
785 unsigned long end = phys_addr + size;
786 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
787 u64 attr;
788
789 if (!md)
790 return 0;
791
792
793
794
795
796 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
797 do {
798 unsigned long md_end = efi_md_end(md);
799
800 if (end <= md_end)
801 return attr;
802
803 md = efi_memory_descriptor(md_end);
804 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
805 return 0;
806 } while (md);
807 return 0;
808}
809
810u64
811kern_mem_attribute (unsigned long phys_addr, unsigned long size)
812{
813 unsigned long end = phys_addr + size;
814 struct kern_memdesc *md;
815 u64 attr;
816
817
818
819
820
821 if (!kern_memmap) {
822 attr = efi_mem_attribute(phys_addr, size);
823 if (attr & EFI_MEMORY_WB)
824 return EFI_MEMORY_WB;
825 return 0;
826 }
827
828 md = kern_memory_descriptor(phys_addr);
829 if (!md)
830 return 0;
831
832 attr = md->attribute;
833 do {
834 unsigned long md_end = kmd_end(md);
835
836 if (end <= md_end)
837 return attr;
838
839 md = kern_memory_descriptor(md_end);
840 if (!md || md->attribute != attr)
841 return 0;
842 } while (md);
843 return 0;
844}
845EXPORT_SYMBOL(kern_mem_attribute);
846
847int
848valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size)
849{
850 u64 attr;
851
852
853
854
855
856
857
858 attr = kern_mem_attribute(phys_addr, size);
859 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
860 return 1;
861 return 0;
862}
863
864int
865valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
866{
867 unsigned long phys_addr = pfn << PAGE_SHIFT;
868 u64 attr;
869
870 attr = efi_mem_attribute(phys_addr, size);
871
872
873
874
875
876
877 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
878 return 1;
879
880
881
882
883
884
885
886 if (efi_memmap_intersects(phys_addr, size))
887 return 0;
888
889 return 1;
890}
891
892pgprot_t
893phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
894 pgprot_t vma_prot)
895{
896 unsigned long phys_addr = pfn << PAGE_SHIFT;
897 u64 attr;
898
899
900
901
902
903
904 attr = kern_mem_attribute(phys_addr, size);
905 if (attr & EFI_MEMORY_WB)
906 return pgprot_cacheable(vma_prot);
907 else if (attr & EFI_MEMORY_UC)
908 return pgprot_noncached(vma_prot);
909
910
911
912
913
914 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
915 return pgprot_cacheable(vma_prot);
916
917 return pgprot_noncached(vma_prot);
918}
919
920int __init
921efi_uart_console_only(void)
922{
923 efi_status_t status;
924 char *s, name[] = "ConOut";
925 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
926 efi_char16_t *utf16, name_utf16[32];
927 unsigned char data[1024];
928 unsigned long size = sizeof(data);
929 struct efi_generic_dev_path *hdr, *end_addr;
930 int uart = 0;
931
932
933 utf16 = name_utf16;
934 s = name;
935 while (*s)
936 *utf16++ = *s++ & 0x7f;
937 *utf16 = 0;
938
939 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
940 if (status != EFI_SUCCESS) {
941 printk(KERN_ERR "No EFI %s variable?\n", name);
942 return 0;
943 }
944
945 hdr = (struct efi_generic_dev_path *) data;
946 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
947 while (hdr < end_addr) {
948 if (hdr->type == EFI_DEV_MSG &&
949 hdr->sub_type == EFI_DEV_MSG_UART)
950 uart = 1;
951 else if (hdr->type == EFI_DEV_END_PATH ||
952 hdr->type == EFI_DEV_END_PATH2) {
953 if (!uart)
954 return 0;
955 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
956 return 1;
957 uart = 0;
958 }
959 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
960 }
961 printk(KERN_ERR "Malformed %s value\n", name);
962 return 0;
963}
964
965
966
967
968
969
970struct kern_memdesc *
971find_memmap_space (void)
972{
973 u64 contig_low=0, contig_high=0;
974 u64 as = 0, ae;
975 void *efi_map_start, *efi_map_end, *p, *q;
976 efi_memory_desc_t *md, *pmd = NULL, *check_md;
977 u64 space_needed, efi_desc_size;
978 unsigned long total_mem = 0;
979
980 efi_map_start = __va(ia64_boot_param->efi_memmap);
981 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
982 efi_desc_size = ia64_boot_param->efi_memdesc_size;
983
984
985
986
987
988
989 space_needed = sizeof(kern_memdesc_t) *
990 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
991
992 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
993 md = p;
994 if (!efi_wb(md)) {
995 continue;
996 }
997 if (pmd == NULL || !efi_wb(pmd) ||
998 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;
1002 q += efi_desc_size) {
1003 check_md = q;
1004 if (!efi_wb(check_md))
1005 break;
1006 if (contig_high != check_md->phys_addr)
1007 break;
1008 contig_high = efi_md_end(check_md);
1009 }
1010 contig_high = GRANULEROUNDDOWN(contig_high);
1011 }
1012 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
1013 continue;
1014
1015
1016 as = max(contig_low, md->phys_addr);
1017 ae = min(contig_high, efi_md_end(md));
1018
1019
1020 as = max(as, min_addr);
1021 ae = min(ae, max_addr);
1022 if (ae <= as)
1023 continue;
1024
1025
1026 if (total_mem + (ae - as) > mem_limit)
1027 ae -= total_mem + (ae - as) - mem_limit;
1028
1029 if (ae <= as)
1030 continue;
1031
1032 if (ae - as > space_needed)
1033 break;
1034 }
1035 if (p >= efi_map_end)
1036 panic("Can't allocate space for kernel memory descriptors");
1037
1038 return __va(as);
1039}
1040
1041
1042
1043
1044
1045
1046unsigned long
1047efi_memmap_init(u64 *s, u64 *e)
1048{
1049 struct kern_memdesc *k, *prev = NULL;
1050 u64 contig_low=0, contig_high=0;
1051 u64 as, ae, lim;
1052 void *efi_map_start, *efi_map_end, *p, *q;
1053 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1054 u64 efi_desc_size;
1055 unsigned long total_mem = 0;
1056
1057 k = kern_memmap = find_memmap_space();
1058
1059 efi_map_start = __va(ia64_boot_param->efi_memmap);
1060 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1061 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1062
1063 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1064 md = p;
1065 if (!efi_wb(md)) {
1066 if (efi_uc(md) &&
1067 (md->type == EFI_CONVENTIONAL_MEMORY ||
1068 md->type == EFI_BOOT_SERVICES_DATA)) {
1069 k->attribute = EFI_MEMORY_UC;
1070 k->start = md->phys_addr;
1071 k->num_pages = md->num_pages;
1072 k++;
1073 }
1074 continue;
1075 }
1076 if (pmd == NULL || !efi_wb(pmd) ||
1077 efi_md_end(pmd) != md->phys_addr) {
1078 contig_low = GRANULEROUNDUP(md->phys_addr);
1079 contig_high = efi_md_end(md);
1080 for (q = p + efi_desc_size; q < efi_map_end;
1081 q += efi_desc_size) {
1082 check_md = q;
1083 if (!efi_wb(check_md))
1084 break;
1085 if (contig_high != check_md->phys_addr)
1086 break;
1087 contig_high = efi_md_end(check_md);
1088 }
1089 contig_high = GRANULEROUNDDOWN(contig_high);
1090 }
1091 if (!is_memory_available(md))
1092 continue;
1093
1094
1095
1096
1097
1098 if (md->phys_addr < contig_low) {
1099 lim = min(efi_md_end(md), contig_low);
1100 if (efi_uc(md)) {
1101 if (k > kern_memmap &&
1102 (k-1)->attribute == EFI_MEMORY_UC &&
1103 kmd_end(k-1) == md->phys_addr) {
1104 (k-1)->num_pages +=
1105 (lim - md->phys_addr)
1106 >> EFI_PAGE_SHIFT;
1107 } else {
1108 k->attribute = EFI_MEMORY_UC;
1109 k->start = md->phys_addr;
1110 k->num_pages = (lim - md->phys_addr)
1111 >> EFI_PAGE_SHIFT;
1112 k++;
1113 }
1114 }
1115 as = contig_low;
1116 } else
1117 as = md->phys_addr;
1118
1119 if (efi_md_end(md) > contig_high) {
1120 lim = max(md->phys_addr, contig_high);
1121 if (efi_uc(md)) {
1122 if (lim == md->phys_addr && k > kern_memmap &&
1123 (k-1)->attribute == EFI_MEMORY_UC &&
1124 kmd_end(k-1) == md->phys_addr) {
1125 (k-1)->num_pages += md->num_pages;
1126 } else {
1127 k->attribute = EFI_MEMORY_UC;
1128 k->start = lim;
1129 k->num_pages = (efi_md_end(md) - lim)
1130 >> EFI_PAGE_SHIFT;
1131 k++;
1132 }
1133 }
1134 ae = contig_high;
1135 } else
1136 ae = efi_md_end(md);
1137
1138
1139 as = max(as, min_addr);
1140 ae = min(ae, max_addr);
1141 if (ae <= as)
1142 continue;
1143
1144
1145 if (total_mem + (ae - as) > mem_limit)
1146 ae -= total_mem + (ae - as) - mem_limit;
1147
1148 if (ae <= as)
1149 continue;
1150 if (prev && kmd_end(prev) == md->phys_addr) {
1151 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1152 total_mem += ae - as;
1153 continue;
1154 }
1155 k->attribute = EFI_MEMORY_WB;
1156 k->start = as;
1157 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1158 total_mem += ae - as;
1159 prev = k++;
1160 }
1161 k->start = ~0L;
1162
1163
1164 *s = (u64)kern_memmap;
1165 *e = (u64)++k;
1166
1167 return total_mem;
1168}
1169
1170void
1171efi_initialize_iomem_resources(struct resource *code_resource,
1172 struct resource *data_resource,
1173 struct resource *bss_resource)
1174{
1175 struct resource *res;
1176 void *efi_map_start, *efi_map_end, *p;
1177 efi_memory_desc_t *md;
1178 u64 efi_desc_size;
1179 char *name;
1180 unsigned long flags;
1181
1182 efi_map_start = __va(ia64_boot_param->efi_memmap);
1183 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1184 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1185
1186 res = NULL;
1187
1188 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1189 md = p;
1190
1191 if (md->num_pages == 0)
1192 continue;
1193
1194 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1195 switch (md->type) {
1196
1197 case EFI_MEMORY_MAPPED_IO:
1198 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1199 continue;
1200
1201 case EFI_LOADER_CODE:
1202 case EFI_LOADER_DATA:
1203 case EFI_BOOT_SERVICES_DATA:
1204 case EFI_BOOT_SERVICES_CODE:
1205 case EFI_CONVENTIONAL_MEMORY:
1206 if (md->attribute & EFI_MEMORY_WP) {
1207 name = "System ROM";
1208 flags |= IORESOURCE_READONLY;
1209 } else if (md->attribute == EFI_MEMORY_UC)
1210 name = "Uncached RAM";
1211 else
1212 name = "System RAM";
1213 break;
1214
1215 case EFI_ACPI_MEMORY_NVS:
1216 name = "ACPI Non-volatile Storage";
1217 break;
1218
1219 case EFI_UNUSABLE_MEMORY:
1220 name = "reserved";
1221 flags |= IORESOURCE_DISABLED;
1222 break;
1223
1224 case EFI_RESERVED_TYPE:
1225 case EFI_RUNTIME_SERVICES_CODE:
1226 case EFI_RUNTIME_SERVICES_DATA:
1227 case EFI_ACPI_RECLAIM_MEMORY:
1228 default:
1229 name = "reserved";
1230 break;
1231 }
1232
1233 if ((res = kzalloc(sizeof(struct resource),
1234 GFP_KERNEL)) == NULL) {
1235 printk(KERN_ERR
1236 "failed to allocate resource for iomem\n");
1237 return;
1238 }
1239
1240 res->name = name;
1241 res->start = md->phys_addr;
1242 res->end = md->phys_addr + efi_md_size(md) - 1;
1243 res->flags = flags;
1244
1245 if (insert_resource(&iomem_resource, res) < 0)
1246 kfree(res);
1247 else {
1248
1249
1250
1251
1252
1253 insert_resource(res, code_resource);
1254 insert_resource(res, data_resource);
1255 insert_resource(res, bss_resource);
1256#ifdef CONFIG_KEXEC
1257 insert_resource(res, &efi_memmap_res);
1258 insert_resource(res, &boot_param_res);
1259 if (crashk_res.end > crashk_res.start)
1260 insert_resource(res, &crashk_res);
1261#endif
1262 }
1263 }
1264}
1265
1266#ifdef CONFIG_KEXEC
1267
1268
1269
1270unsigned long __init
1271kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1272{
1273 int i;
1274 u64 start, end;
1275 u64 alignment = 1UL << _PAGE_SIZE_64M;
1276 void *efi_map_start, *efi_map_end, *p;
1277 efi_memory_desc_t *md;
1278 u64 efi_desc_size;
1279
1280 efi_map_start = __va(ia64_boot_param->efi_memmap);
1281 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1282 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1283
1284 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1285 md = p;
1286 if (!efi_wb(md))
1287 continue;
1288 start = ALIGN(md->phys_addr, alignment);
1289 end = efi_md_end(md);
1290 for (i = 0; i < n; i++) {
1291 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1292 if (__pa(r[i].start) > start + size)
1293 return start;
1294 start = ALIGN(__pa(r[i].end), alignment);
1295 if (i < n-1 &&
1296 __pa(r[i+1].start) < start + size)
1297 continue;
1298 else
1299 break;
1300 }
1301 }
1302 if (end > start + size)
1303 return start;
1304 }
1305
1306 printk(KERN_WARNING
1307 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1308 return ~0UL;
1309}
1310#endif
1311
1312#ifdef CONFIG_CRASH_DUMP
1313
1314unsigned long __init
1315vmcore_find_descriptor_size (unsigned long address)
1316{
1317 void *efi_map_start, *efi_map_end, *p;
1318 efi_memory_desc_t *md;
1319 u64 efi_desc_size;
1320 unsigned long ret = 0;
1321
1322 efi_map_start = __va(ia64_boot_param->efi_memmap);
1323 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1324 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1325
1326 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1327 md = p;
1328 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1329 && md->phys_addr == address) {
1330 ret = efi_md_size(md);
1331 break;
1332 }
1333 }
1334
1335 if (ret == 0)
1336 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1337
1338 return ret;
1339}
1340#endif
1341