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
481
482
483
484 for (cp = boot_command_line; *cp; ) {
485 if (memcmp(cp, "mem=", 4) == 0) {
486 mem_limit = memparse(cp + 4, &cp);
487 } else if (memcmp(cp, "max_addr=", 9) == 0) {
488 max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
489 } else if (memcmp(cp, "min_addr=", 9) == 0) {
490 min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
491 } else {
492 while (*cp != ' ' && *cp)
493 ++cp;
494 while (*cp == ' ')
495 ++cp;
496 }
497 }
498 if (min_addr != 0UL)
499 printk(KERN_INFO "Ignoring memory below %lluMB\n",
500 min_addr >> 20);
501 if (max_addr != ~0UL)
502 printk(KERN_INFO "Ignoring memory above %lluMB\n",
503 max_addr >> 20);
504
505 efi.systab = __va(ia64_boot_param->efi_systab);
506
507
508
509
510 if (efi.systab == NULL)
511 panic("Whoa! Can't find EFI system table.\n");
512 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
513 panic("Whoa! EFI system table signature incorrect\n");
514 if ((efi.systab->hdr.revision >> 16) == 0)
515 printk(KERN_WARNING "Warning: EFI system table version "
516 "%d.%02d, expected 1.00 or greater\n",
517 efi.systab->hdr.revision >> 16,
518 efi.systab->hdr.revision & 0xffff);
519
520
521 c16 = __va(efi.systab->fw_vendor);
522 if (c16) {
523 for (i = 0;i < (int) sizeof(vendor) - 1 && *c16; ++i)
524 vendor[i] = *c16++;
525 vendor[i] = '\0';
526 }
527
528 printk(KERN_INFO "EFI v%u.%.02u by %s:",
529 efi.systab->hdr.revision >> 16,
530 efi.systab->hdr.revision & 0xffff, vendor);
531
532 palo_phys = EFI_INVALID_TABLE_ADDR;
533
534 if (efi_config_init(arch_tables) != 0)
535 return;
536
537 if (palo_phys != EFI_INVALID_TABLE_ADDR)
538 handle_palo(palo_phys);
539
540 runtime = __va(efi.systab->runtime);
541 efi.get_time = phys_get_time;
542 efi.set_time = phys_set_time;
543 efi.get_wakeup_time = phys_get_wakeup_time;
544 efi.set_wakeup_time = phys_set_wakeup_time;
545 efi.get_variable = phys_get_variable;
546 efi.get_next_variable = phys_get_next_variable;
547 efi.set_variable = phys_set_variable;
548 efi.get_next_high_mono_count = phys_get_next_high_mono_count;
549 efi.reset_system = phys_reset_system;
550
551 efi_map_start = __va(ia64_boot_param->efi_memmap);
552 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
553 efi_desc_size = ia64_boot_param->efi_memdesc_size;
554
555#if EFI_DEBUG
556
557 {
558 efi_memory_desc_t *md;
559 void *p;
560
561 for (i = 0, p = efi_map_start; p < efi_map_end;
562 ++i, p += efi_desc_size)
563 {
564 const char *unit;
565 unsigned long size;
566
567 md = p;
568 size = md->num_pages << EFI_PAGE_SHIFT;
569
570 if ((size >> 40) > 0) {
571 size >>= 40;
572 unit = "TB";
573 } else if ((size >> 30) > 0) {
574 size >>= 30;
575 unit = "GB";
576 } else if ((size >> 20) > 0) {
577 size >>= 20;
578 unit = "MB";
579 } else {
580 size >>= 10;
581 unit = "KB";
582 }
583
584 printk("mem%02d: type=%2u, attr=0x%016lx, "
585 "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
586 i, md->type, md->attribute, md->phys_addr,
587 md->phys_addr + efi_md_size(md), size, unit);
588 }
589 }
590#endif
591
592 efi_map_pal_code();
593 efi_enter_virtual_mode();
594}
595
596void
597efi_enter_virtual_mode (void)
598{
599 void *efi_map_start, *efi_map_end, *p;
600 efi_memory_desc_t *md;
601 efi_status_t status;
602 u64 efi_desc_size;
603
604 efi_map_start = __va(ia64_boot_param->efi_memmap);
605 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
606 efi_desc_size = ia64_boot_param->efi_memdesc_size;
607
608 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
609 md = p;
610 if (md->attribute & EFI_MEMORY_RUNTIME) {
611
612
613
614
615 if (md->attribute & EFI_MEMORY_WB) {
616 md->virt_addr = (u64) __va(md->phys_addr);
617 } else if (md->attribute & EFI_MEMORY_UC) {
618 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
619 } else if (md->attribute & EFI_MEMORY_WC) {
620#if 0
621 md->virt_addr = ia64_remap(md->phys_addr,
622 (_PAGE_A |
623 _PAGE_P |
624 _PAGE_D |
625 _PAGE_MA_WC |
626 _PAGE_PL_0 |
627 _PAGE_AR_RW));
628#else
629 printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
630 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
631#endif
632 } else if (md->attribute & EFI_MEMORY_WT) {
633#if 0
634 md->virt_addr = ia64_remap(md->phys_addr,
635 (_PAGE_A |
636 _PAGE_P |
637 _PAGE_D |
638 _PAGE_MA_WT |
639 _PAGE_PL_0 |
640 _PAGE_AR_RW));
641#else
642 printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
643 md->virt_addr = (u64) ioremap(md->phys_addr, 0);
644#endif
645 }
646 }
647 }
648
649 status = efi_call_phys(__va(runtime->set_virtual_address_map),
650 ia64_boot_param->efi_memmap_size,
651 efi_desc_size,
652 ia64_boot_param->efi_memdesc_version,
653 ia64_boot_param->efi_memmap);
654 if (status != EFI_SUCCESS) {
655 printk(KERN_WARNING "warning: unable to switch EFI into "
656 "virtual mode (status=%lu)\n", status);
657 return;
658 }
659
660
661
662
663
664 efi.get_time = virt_get_time;
665 efi.set_time = virt_set_time;
666 efi.get_wakeup_time = virt_get_wakeup_time;
667 efi.set_wakeup_time = virt_set_wakeup_time;
668 efi.get_variable = virt_get_variable;
669 efi.get_next_variable = virt_get_next_variable;
670 efi.set_variable = virt_set_variable;
671 efi.get_next_high_mono_count = virt_get_next_high_mono_count;
672 efi.reset_system = virt_reset_system;
673}
674
675
676
677
678
679u64
680efi_get_iobase (void)
681{
682 void *efi_map_start, *efi_map_end, *p;
683 efi_memory_desc_t *md;
684 u64 efi_desc_size;
685
686 efi_map_start = __va(ia64_boot_param->efi_memmap);
687 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
688 efi_desc_size = ia64_boot_param->efi_memdesc_size;
689
690 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
691 md = p;
692 if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
693 if (md->attribute & EFI_MEMORY_UC)
694 return md->phys_addr;
695 }
696 }
697 return 0;
698}
699
700static struct kern_memdesc *
701kern_memory_descriptor (unsigned long phys_addr)
702{
703 struct kern_memdesc *md;
704
705 for (md = kern_memmap; md->start != ~0UL; md++) {
706 if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
707 return md;
708 }
709 return NULL;
710}
711
712static efi_memory_desc_t *
713efi_memory_descriptor (unsigned long phys_addr)
714{
715 void *efi_map_start, *efi_map_end, *p;
716 efi_memory_desc_t *md;
717 u64 efi_desc_size;
718
719 efi_map_start = __va(ia64_boot_param->efi_memmap);
720 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
721 efi_desc_size = ia64_boot_param->efi_memdesc_size;
722
723 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
724 md = p;
725
726 if (phys_addr - md->phys_addr < efi_md_size(md))
727 return md;
728 }
729 return NULL;
730}
731
732static int
733efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
734{
735 void *efi_map_start, *efi_map_end, *p;
736 efi_memory_desc_t *md;
737 u64 efi_desc_size;
738 unsigned long end;
739
740 efi_map_start = __va(ia64_boot_param->efi_memmap);
741 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
742 efi_desc_size = ia64_boot_param->efi_memdesc_size;
743
744 end = phys_addr + size;
745
746 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
747 md = p;
748 if (md->phys_addr < end && efi_md_end(md) > phys_addr)
749 return 1;
750 }
751 return 0;
752}
753
754int
755efi_mem_type (unsigned long phys_addr)
756{
757 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
758
759 if (md)
760 return md->type;
761 return -EINVAL;
762}
763
764u64
765efi_mem_attributes (unsigned long phys_addr)
766{
767 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
768
769 if (md)
770 return md->attribute;
771 return 0;
772}
773EXPORT_SYMBOL(efi_mem_attributes);
774
775u64
776efi_mem_attribute (unsigned long phys_addr, unsigned long size)
777{
778 unsigned long end = phys_addr + size;
779 efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
780 u64 attr;
781
782 if (!md)
783 return 0;
784
785
786
787
788
789 attr = md->attribute & ~EFI_MEMORY_RUNTIME;
790 do {
791 unsigned long md_end = efi_md_end(md);
792
793 if (end <= md_end)
794 return attr;
795
796 md = efi_memory_descriptor(md_end);
797 if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
798 return 0;
799 } while (md);
800 return 0;
801}
802
803u64
804kern_mem_attribute (unsigned long phys_addr, unsigned long size)
805{
806 unsigned long end = phys_addr + size;
807 struct kern_memdesc *md;
808 u64 attr;
809
810
811
812
813
814 if (!kern_memmap) {
815 attr = efi_mem_attribute(phys_addr, size);
816 if (attr & EFI_MEMORY_WB)
817 return EFI_MEMORY_WB;
818 return 0;
819 }
820
821 md = kern_memory_descriptor(phys_addr);
822 if (!md)
823 return 0;
824
825 attr = md->attribute;
826 do {
827 unsigned long md_end = kmd_end(md);
828
829 if (end <= md_end)
830 return attr;
831
832 md = kern_memory_descriptor(md_end);
833 if (!md || md->attribute != attr)
834 return 0;
835 } while (md);
836 return 0;
837}
838EXPORT_SYMBOL(kern_mem_attribute);
839
840int
841valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size)
842{
843 u64 attr;
844
845
846
847
848
849
850
851 attr = kern_mem_attribute(phys_addr, size);
852 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
853 return 1;
854 return 0;
855}
856
857int
858valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
859{
860 unsigned long phys_addr = pfn << PAGE_SHIFT;
861 u64 attr;
862
863 attr = efi_mem_attribute(phys_addr, size);
864
865
866
867
868
869
870 if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
871 return 1;
872
873
874
875
876
877
878
879 if (efi_memmap_intersects(phys_addr, size))
880 return 0;
881
882 return 1;
883}
884
885pgprot_t
886phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
887 pgprot_t vma_prot)
888{
889 unsigned long phys_addr = pfn << PAGE_SHIFT;
890 u64 attr;
891
892
893
894
895
896
897 attr = kern_mem_attribute(phys_addr, size);
898 if (attr & EFI_MEMORY_WB)
899 return pgprot_cacheable(vma_prot);
900 else if (attr & EFI_MEMORY_UC)
901 return pgprot_noncached(vma_prot);
902
903
904
905
906
907 if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
908 return pgprot_cacheable(vma_prot);
909
910 return pgprot_noncached(vma_prot);
911}
912
913int __init
914efi_uart_console_only(void)
915{
916 efi_status_t status;
917 char *s, name[] = "ConOut";
918 efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
919 efi_char16_t *utf16, name_utf16[32];
920 unsigned char data[1024];
921 unsigned long size = sizeof(data);
922 struct efi_generic_dev_path *hdr, *end_addr;
923 int uart = 0;
924
925
926 utf16 = name_utf16;
927 s = name;
928 while (*s)
929 *utf16++ = *s++ & 0x7f;
930 *utf16 = 0;
931
932 status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
933 if (status != EFI_SUCCESS) {
934 printk(KERN_ERR "No EFI %s variable?\n", name);
935 return 0;
936 }
937
938 hdr = (struct efi_generic_dev_path *) data;
939 end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
940 while (hdr < end_addr) {
941 if (hdr->type == EFI_DEV_MSG &&
942 hdr->sub_type == EFI_DEV_MSG_UART)
943 uart = 1;
944 else if (hdr->type == EFI_DEV_END_PATH ||
945 hdr->type == EFI_DEV_END_PATH2) {
946 if (!uart)
947 return 0;
948 if (hdr->sub_type == EFI_DEV_END_ENTIRE)
949 return 1;
950 uart = 0;
951 }
952 hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
953 }
954 printk(KERN_ERR "Malformed %s value\n", name);
955 return 0;
956}
957
958
959
960
961
962
963struct kern_memdesc *
964find_memmap_space (void)
965{
966 u64 contig_low=0, contig_high=0;
967 u64 as = 0, ae;
968 void *efi_map_start, *efi_map_end, *p, *q;
969 efi_memory_desc_t *md, *pmd = NULL, *check_md;
970 u64 space_needed, efi_desc_size;
971 unsigned long total_mem = 0;
972
973 efi_map_start = __va(ia64_boot_param->efi_memmap);
974 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
975 efi_desc_size = ia64_boot_param->efi_memdesc_size;
976
977
978
979
980
981
982 space_needed = sizeof(kern_memdesc_t) *
983 (3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
984
985 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
986 md = p;
987 if (!efi_wb(md)) {
988 continue;
989 }
990 if (pmd == NULL || !efi_wb(pmd) ||
991 efi_md_end(pmd) != md->phys_addr) {
992 contig_low = GRANULEROUNDUP(md->phys_addr);
993 contig_high = efi_md_end(md);
994 for (q = p + efi_desc_size; q < efi_map_end;
995 q += efi_desc_size) {
996 check_md = q;
997 if (!efi_wb(check_md))
998 break;
999 if (contig_high != check_md->phys_addr)
1000 break;
1001 contig_high = efi_md_end(check_md);
1002 }
1003 contig_high = GRANULEROUNDDOWN(contig_high);
1004 }
1005 if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
1006 continue;
1007
1008
1009 as = max(contig_low, md->phys_addr);
1010 ae = min(contig_high, efi_md_end(md));
1011
1012
1013 as = max(as, min_addr);
1014 ae = min(ae, max_addr);
1015 if (ae <= as)
1016 continue;
1017
1018
1019 if (total_mem + (ae - as) > mem_limit)
1020 ae -= total_mem + (ae - as) - mem_limit;
1021
1022 if (ae <= as)
1023 continue;
1024
1025 if (ae - as > space_needed)
1026 break;
1027 }
1028 if (p >= efi_map_end)
1029 panic("Can't allocate space for kernel memory descriptors");
1030
1031 return __va(as);
1032}
1033
1034
1035
1036
1037
1038
1039unsigned long
1040efi_memmap_init(u64 *s, u64 *e)
1041{
1042 struct kern_memdesc *k, *prev = NULL;
1043 u64 contig_low=0, contig_high=0;
1044 u64 as, ae, lim;
1045 void *efi_map_start, *efi_map_end, *p, *q;
1046 efi_memory_desc_t *md, *pmd = NULL, *check_md;
1047 u64 efi_desc_size;
1048 unsigned long total_mem = 0;
1049
1050 k = kern_memmap = find_memmap_space();
1051
1052 efi_map_start = __va(ia64_boot_param->efi_memmap);
1053 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1054 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1055
1056 for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1057 md = p;
1058 if (!efi_wb(md)) {
1059 if (efi_uc(md) &&
1060 (md->type == EFI_CONVENTIONAL_MEMORY ||
1061 md->type == EFI_BOOT_SERVICES_DATA)) {
1062 k->attribute = EFI_MEMORY_UC;
1063 k->start = md->phys_addr;
1064 k->num_pages = md->num_pages;
1065 k++;
1066 }
1067 continue;
1068 }
1069 if (pmd == NULL || !efi_wb(pmd) ||
1070 efi_md_end(pmd) != md->phys_addr) {
1071 contig_low = GRANULEROUNDUP(md->phys_addr);
1072 contig_high = efi_md_end(md);
1073 for (q = p + efi_desc_size; q < efi_map_end;
1074 q += efi_desc_size) {
1075 check_md = q;
1076 if (!efi_wb(check_md))
1077 break;
1078 if (contig_high != check_md->phys_addr)
1079 break;
1080 contig_high = efi_md_end(check_md);
1081 }
1082 contig_high = GRANULEROUNDDOWN(contig_high);
1083 }
1084 if (!is_memory_available(md))
1085 continue;
1086
1087#ifdef CONFIG_CRASH_DUMP
1088
1089 if (saved_max_pfn < (efi_md_end(md) >> PAGE_SHIFT))
1090 saved_max_pfn = (efi_md_end(md) >> PAGE_SHIFT);
1091#endif
1092
1093
1094
1095
1096 if (md->phys_addr < contig_low) {
1097 lim = min(efi_md_end(md), contig_low);
1098 if (efi_uc(md)) {
1099 if (k > kern_memmap &&
1100 (k-1)->attribute == EFI_MEMORY_UC &&
1101 kmd_end(k-1) == md->phys_addr) {
1102 (k-1)->num_pages +=
1103 (lim - md->phys_addr)
1104 >> EFI_PAGE_SHIFT;
1105 } else {
1106 k->attribute = EFI_MEMORY_UC;
1107 k->start = md->phys_addr;
1108 k->num_pages = (lim - md->phys_addr)
1109 >> EFI_PAGE_SHIFT;
1110 k++;
1111 }
1112 }
1113 as = contig_low;
1114 } else
1115 as = md->phys_addr;
1116
1117 if (efi_md_end(md) > contig_high) {
1118 lim = max(md->phys_addr, contig_high);
1119 if (efi_uc(md)) {
1120 if (lim == md->phys_addr && k > kern_memmap &&
1121 (k-1)->attribute == EFI_MEMORY_UC &&
1122 kmd_end(k-1) == md->phys_addr) {
1123 (k-1)->num_pages += md->num_pages;
1124 } else {
1125 k->attribute = EFI_MEMORY_UC;
1126 k->start = lim;
1127 k->num_pages = (efi_md_end(md) - lim)
1128 >> EFI_PAGE_SHIFT;
1129 k++;
1130 }
1131 }
1132 ae = contig_high;
1133 } else
1134 ae = efi_md_end(md);
1135
1136
1137 as = max(as, min_addr);
1138 ae = min(ae, max_addr);
1139 if (ae <= as)
1140 continue;
1141
1142
1143 if (total_mem + (ae - as) > mem_limit)
1144 ae -= total_mem + (ae - as) - mem_limit;
1145
1146 if (ae <= as)
1147 continue;
1148 if (prev && kmd_end(prev) == md->phys_addr) {
1149 prev->num_pages += (ae - as) >> EFI_PAGE_SHIFT;
1150 total_mem += ae - as;
1151 continue;
1152 }
1153 k->attribute = EFI_MEMORY_WB;
1154 k->start = as;
1155 k->num_pages = (ae - as) >> EFI_PAGE_SHIFT;
1156 total_mem += ae - as;
1157 prev = k++;
1158 }
1159 k->start = ~0L;
1160
1161
1162 *s = (u64)kern_memmap;
1163 *e = (u64)++k;
1164
1165 return total_mem;
1166}
1167
1168void
1169efi_initialize_iomem_resources(struct resource *code_resource,
1170 struct resource *data_resource,
1171 struct resource *bss_resource)
1172{
1173 struct resource *res;
1174 void *efi_map_start, *efi_map_end, *p;
1175 efi_memory_desc_t *md;
1176 u64 efi_desc_size;
1177 char *name;
1178 unsigned long flags;
1179
1180 efi_map_start = __va(ia64_boot_param->efi_memmap);
1181 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1182 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1183
1184 res = NULL;
1185
1186 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1187 md = p;
1188
1189 if (md->num_pages == 0)
1190 continue;
1191
1192 flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1193 switch (md->type) {
1194
1195 case EFI_MEMORY_MAPPED_IO:
1196 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1197 continue;
1198
1199 case EFI_LOADER_CODE:
1200 case EFI_LOADER_DATA:
1201 case EFI_BOOT_SERVICES_DATA:
1202 case EFI_BOOT_SERVICES_CODE:
1203 case EFI_CONVENTIONAL_MEMORY:
1204 if (md->attribute & EFI_MEMORY_WP) {
1205 name = "System ROM";
1206 flags |= IORESOURCE_READONLY;
1207 } else if (md->attribute == EFI_MEMORY_UC)
1208 name = "Uncached RAM";
1209 else
1210 name = "System RAM";
1211 break;
1212
1213 case EFI_ACPI_MEMORY_NVS:
1214 name = "ACPI Non-volatile Storage";
1215 break;
1216
1217 case EFI_UNUSABLE_MEMORY:
1218 name = "reserved";
1219 flags |= IORESOURCE_DISABLED;
1220 break;
1221
1222 case EFI_PERSISTENT_MEMORY:
1223 name = "Persistent Memory";
1224 break;
1225
1226 case EFI_RESERVED_TYPE:
1227 case EFI_RUNTIME_SERVICES_CODE:
1228 case EFI_RUNTIME_SERVICES_DATA:
1229 case EFI_ACPI_RECLAIM_MEMORY:
1230 default:
1231 name = "reserved";
1232 break;
1233 }
1234
1235 if ((res = kzalloc(sizeof(struct resource),
1236 GFP_KERNEL)) == NULL) {
1237 printk(KERN_ERR
1238 "failed to allocate resource for iomem\n");
1239 return;
1240 }
1241
1242 res->name = name;
1243 res->start = md->phys_addr;
1244 res->end = md->phys_addr + efi_md_size(md) - 1;
1245 res->flags = flags;
1246
1247 if (insert_resource(&iomem_resource, res) < 0)
1248 kfree(res);
1249 else {
1250
1251
1252
1253
1254
1255 insert_resource(res, code_resource);
1256 insert_resource(res, data_resource);
1257 insert_resource(res, bss_resource);
1258#ifdef CONFIG_KEXEC
1259 insert_resource(res, &efi_memmap_res);
1260 insert_resource(res, &boot_param_res);
1261 if (crashk_res.end > crashk_res.start)
1262 insert_resource(res, &crashk_res);
1263#endif
1264 }
1265 }
1266}
1267
1268#ifdef CONFIG_KEXEC
1269
1270
1271
1272unsigned long __init
1273kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1274{
1275 int i;
1276 u64 start, end;
1277 u64 alignment = 1UL << _PAGE_SIZE_64M;
1278 void *efi_map_start, *efi_map_end, *p;
1279 efi_memory_desc_t *md;
1280 u64 efi_desc_size;
1281
1282 efi_map_start = __va(ia64_boot_param->efi_memmap);
1283 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1284 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1285
1286 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1287 md = p;
1288 if (!efi_wb(md))
1289 continue;
1290 start = ALIGN(md->phys_addr, alignment);
1291 end = efi_md_end(md);
1292 for (i = 0; i < n; i++) {
1293 if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1294 if (__pa(r[i].start) > start + size)
1295 return start;
1296 start = ALIGN(__pa(r[i].end), alignment);
1297 if (i < n-1 &&
1298 __pa(r[i+1].start) < start + size)
1299 continue;
1300 else
1301 break;
1302 }
1303 }
1304 if (end > start + size)
1305 return start;
1306 }
1307
1308 printk(KERN_WARNING
1309 "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1310 return ~0UL;
1311}
1312#endif
1313
1314#ifdef CONFIG_CRASH_DUMP
1315
1316unsigned long __init
1317vmcore_find_descriptor_size (unsigned long address)
1318{
1319 void *efi_map_start, *efi_map_end, *p;
1320 efi_memory_desc_t *md;
1321 u64 efi_desc_size;
1322 unsigned long ret = 0;
1323
1324 efi_map_start = __va(ia64_boot_param->efi_memmap);
1325 efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
1326 efi_desc_size = ia64_boot_param->efi_memdesc_size;
1327
1328 for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1329 md = p;
1330 if (efi_wb(md) && md->type == EFI_LOADER_DATA
1331 && md->phys_addr == address) {
1332 ret = efi_md_size(md);
1333 break;
1334 }
1335 }
1336
1337 if (ret == 0)
1338 printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1339
1340 return ret;
1341}
1342#endif
1343