xref: /openbmc/linux/arch/ia64/kernel/efi.c (revision bd4af432)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Extensible Firmware Interface
4  *
5  * Based on Extensible Firmware Interface Specification version 0.9
6  * April 30, 1999
7  *
8  * Copyright (C) 1999 VA Linux Systems
9  * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
10  * Copyright (C) 1999-2003 Hewlett-Packard Co.
11  *	David Mosberger-Tang <davidm@hpl.hp.com>
12  *	Stephane Eranian <eranian@hpl.hp.com>
13  * (c) Copyright 2006 Hewlett-Packard Development Company, L.P.
14  *	Bjorn Helgaas <bjorn.helgaas@hp.com>
15  *
16  * All EFI Runtime Services are not implemented yet as EFI only
17  * supports physical mode addressing on SoftSDV. This is to be fixed
18  * in a future version.  --drummond 1999-07-20
19  *
20  * Implemented EFI runtime services and virtual mode calls.  --davidm
21  *
22  * Goutham Rao: <goutham.rao@intel.com>
23  *	Skip non-WB memory and ignore empty memory ranges.
24  */
25 #include <linux/module.h>
26 #include <linux/memblock.h>
27 #include <linux/crash_dump.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/time.h>
33 #include <linux/efi.h>
34 #include <linux/kexec.h>
35 #include <linux/mm.h>
36 
37 #include <asm/io.h>
38 #include <asm/kregs.h>
39 #include <asm/meminit.h>
40 #include <asm/pgtable.h>
41 #include <asm/processor.h>
42 #include <asm/mca.h>
43 #include <asm/setup.h>
44 #include <asm/tlbflush.h>
45 
46 #define EFI_DEBUG	0
47 
48 #define ESI_TABLE_GUID					\
49     EFI_GUID(0x43EA58DC, 0xCF28, 0x4b06, 0xB3,		\
50 	     0x91, 0xB7, 0x50, 0x59, 0x34, 0x2B, 0xD4)
51 
52 static unsigned long mps_phys = EFI_INVALID_TABLE_ADDR;
53 static __initdata unsigned long palo_phys;
54 
55 unsigned long __initdata esi_phys = EFI_INVALID_TABLE_ADDR;
56 unsigned long hcdp_phys = EFI_INVALID_TABLE_ADDR;
57 unsigned long sal_systab_phys = EFI_INVALID_TABLE_ADDR;
58 
59 static const efi_config_table_type_t arch_tables[] __initconst = {
60 	{ESI_TABLE_GUID, "ESI", &esi_phys},
61 	{HCDP_TABLE_GUID, "HCDP", &hcdp_phys},
62 	{MPS_TABLE_GUID, "MPS", &mps_phys},
63 	{PROCESSOR_ABSTRACTION_LAYER_OVERWRITE_GUID, "PALO", &palo_phys},
64 	{SAL_SYSTEM_TABLE_GUID, "SALsystab", &sal_systab_phys},
65 	{NULL_GUID, NULL, 0},
66 };
67 
68 extern efi_status_t efi_call_phys (void *, ...);
69 
70 static efi_runtime_services_t *runtime;
71 static u64 mem_limit = ~0UL, max_addr = ~0UL, min_addr = 0UL;
72 
73 #define efi_call_virt(f, args...)	(*(f))(args)
74 
75 #define STUB_GET_TIME(prefix, adjust_arg)				       \
76 static efi_status_t							       \
77 prefix##_get_time (efi_time_t *tm, efi_time_cap_t *tc)			       \
78 {									       \
79 	struct ia64_fpreg fr[6];					       \
80 	efi_time_cap_t *atc = NULL;					       \
81 	efi_status_t ret;						       \
82 									       \
83 	if (tc)								       \
84 		atc = adjust_arg(tc);					       \
85 	ia64_save_scratch_fpregs(fr);					       \
86 	ret = efi_call_##prefix((efi_get_time_t *) __va(runtime->get_time),    \
87 				adjust_arg(tm), atc);			       \
88 	ia64_load_scratch_fpregs(fr);					       \
89 	return ret;							       \
90 }
91 
92 #define STUB_SET_TIME(prefix, adjust_arg)				       \
93 static efi_status_t							       \
94 prefix##_set_time (efi_time_t *tm)					       \
95 {									       \
96 	struct ia64_fpreg fr[6];					       \
97 	efi_status_t ret;						       \
98 									       \
99 	ia64_save_scratch_fpregs(fr);					       \
100 	ret = efi_call_##prefix((efi_set_time_t *) __va(runtime->set_time),    \
101 				adjust_arg(tm));			       \
102 	ia64_load_scratch_fpregs(fr);					       \
103 	return ret;							       \
104 }
105 
106 #define STUB_GET_WAKEUP_TIME(prefix, adjust_arg)			       \
107 static efi_status_t							       \
108 prefix##_get_wakeup_time (efi_bool_t *enabled, efi_bool_t *pending,	       \
109 			  efi_time_t *tm)				       \
110 {									       \
111 	struct ia64_fpreg fr[6];					       \
112 	efi_status_t ret;						       \
113 									       \
114 	ia64_save_scratch_fpregs(fr);					       \
115 	ret = efi_call_##prefix(					       \
116 		(efi_get_wakeup_time_t *) __va(runtime->get_wakeup_time),      \
117 		adjust_arg(enabled), adjust_arg(pending), adjust_arg(tm));     \
118 	ia64_load_scratch_fpregs(fr);					       \
119 	return ret;							       \
120 }
121 
122 #define STUB_SET_WAKEUP_TIME(prefix, adjust_arg)			       \
123 static efi_status_t							       \
124 prefix##_set_wakeup_time (efi_bool_t enabled, efi_time_t *tm)		       \
125 {									       \
126 	struct ia64_fpreg fr[6];					       \
127 	efi_time_t *atm = NULL;						       \
128 	efi_status_t ret;						       \
129 									       \
130 	if (tm)								       \
131 		atm = adjust_arg(tm);					       \
132 	ia64_save_scratch_fpregs(fr);					       \
133 	ret = efi_call_##prefix(					       \
134 		(efi_set_wakeup_time_t *) __va(runtime->set_wakeup_time),      \
135 		enabled, atm);						       \
136 	ia64_load_scratch_fpregs(fr);					       \
137 	return ret;							       \
138 }
139 
140 #define STUB_GET_VARIABLE(prefix, adjust_arg)				       \
141 static efi_status_t							       \
142 prefix##_get_variable (efi_char16_t *name, efi_guid_t *vendor, u32 *attr,      \
143 		       unsigned long *data_size, void *data)		       \
144 {									       \
145 	struct ia64_fpreg fr[6];					       \
146 	u32 *aattr = NULL;						       \
147 	efi_status_t ret;						       \
148 									       \
149 	if (attr)							       \
150 		aattr = adjust_arg(attr);				       \
151 	ia64_save_scratch_fpregs(fr);					       \
152 	ret = efi_call_##prefix(					       \
153 		(efi_get_variable_t *) __va(runtime->get_variable),	       \
154 		adjust_arg(name), adjust_arg(vendor), aattr,		       \
155 		adjust_arg(data_size), adjust_arg(data));		       \
156 	ia64_load_scratch_fpregs(fr);					       \
157 	return ret;							       \
158 }
159 
160 #define STUB_GET_NEXT_VARIABLE(prefix, adjust_arg)			       \
161 static efi_status_t							       \
162 prefix##_get_next_variable (unsigned long *name_size, efi_char16_t *name,      \
163 			    efi_guid_t *vendor)				       \
164 {									       \
165 	struct ia64_fpreg fr[6];					       \
166 	efi_status_t ret;						       \
167 									       \
168 	ia64_save_scratch_fpregs(fr);					       \
169 	ret = efi_call_##prefix(					       \
170 		(efi_get_next_variable_t *) __va(runtime->get_next_variable),  \
171 		adjust_arg(name_size), adjust_arg(name), adjust_arg(vendor));  \
172 	ia64_load_scratch_fpregs(fr);					       \
173 	return ret;							       \
174 }
175 
176 #define STUB_SET_VARIABLE(prefix, adjust_arg)				       \
177 static efi_status_t							       \
178 prefix##_set_variable (efi_char16_t *name, efi_guid_t *vendor,		       \
179 		       u32 attr, unsigned long data_size,		       \
180 		       void *data)					       \
181 {									       \
182 	struct ia64_fpreg fr[6];					       \
183 	efi_status_t ret;						       \
184 									       \
185 	ia64_save_scratch_fpregs(fr);					       \
186 	ret = efi_call_##prefix(					       \
187 		(efi_set_variable_t *) __va(runtime->set_variable),	       \
188 		adjust_arg(name), adjust_arg(vendor), attr, data_size,	       \
189 		adjust_arg(data));					       \
190 	ia64_load_scratch_fpregs(fr);					       \
191 	return ret;							       \
192 }
193 
194 #define STUB_GET_NEXT_HIGH_MONO_COUNT(prefix, adjust_arg)		       \
195 static efi_status_t							       \
196 prefix##_get_next_high_mono_count (u32 *count)				       \
197 {									       \
198 	struct ia64_fpreg fr[6];					       \
199 	efi_status_t ret;						       \
200 									       \
201 	ia64_save_scratch_fpregs(fr);					       \
202 	ret = efi_call_##prefix((efi_get_next_high_mono_count_t *)	       \
203 				__va(runtime->get_next_high_mono_count),       \
204 				adjust_arg(count));			       \
205 	ia64_load_scratch_fpregs(fr);					       \
206 	return ret;							       \
207 }
208 
209 #define STUB_RESET_SYSTEM(prefix, adjust_arg)				       \
210 static void								       \
211 prefix##_reset_system (int reset_type, efi_status_t status,		       \
212 		       unsigned long data_size, efi_char16_t *data)	       \
213 {									       \
214 	struct ia64_fpreg fr[6];					       \
215 	efi_char16_t *adata = NULL;					       \
216 									       \
217 	if (data)							       \
218 		adata = adjust_arg(data);				       \
219 									       \
220 	ia64_save_scratch_fpregs(fr);					       \
221 	efi_call_##prefix(						       \
222 		(efi_reset_system_t *) __va(runtime->reset_system),	       \
223 		reset_type, status, data_size, adata);			       \
224 	/* should not return, but just in case... */			       \
225 	ia64_load_scratch_fpregs(fr);					       \
226 }
227 
228 #define phys_ptr(arg)	((__typeof__(arg)) ia64_tpa(arg))
229 
230 STUB_GET_TIME(phys, phys_ptr)
231 STUB_SET_TIME(phys, phys_ptr)
232 STUB_GET_WAKEUP_TIME(phys, phys_ptr)
233 STUB_SET_WAKEUP_TIME(phys, phys_ptr)
234 STUB_GET_VARIABLE(phys, phys_ptr)
235 STUB_GET_NEXT_VARIABLE(phys, phys_ptr)
236 STUB_SET_VARIABLE(phys, phys_ptr)
237 STUB_GET_NEXT_HIGH_MONO_COUNT(phys, phys_ptr)
238 STUB_RESET_SYSTEM(phys, phys_ptr)
239 
240 #define id(arg)	arg
241 
242 STUB_GET_TIME(virt, id)
243 STUB_SET_TIME(virt, id)
244 STUB_GET_WAKEUP_TIME(virt, id)
245 STUB_SET_WAKEUP_TIME(virt, id)
246 STUB_GET_VARIABLE(virt, id)
247 STUB_GET_NEXT_VARIABLE(virt, id)
248 STUB_SET_VARIABLE(virt, id)
249 STUB_GET_NEXT_HIGH_MONO_COUNT(virt, id)
250 STUB_RESET_SYSTEM(virt, id)
251 
252 void
253 efi_gettimeofday (struct timespec64 *ts)
254 {
255 	efi_time_t tm;
256 
257 	if ((*efi.get_time)(&tm, NULL) != EFI_SUCCESS) {
258 		memset(ts, 0, sizeof(*ts));
259 		return;
260 	}
261 
262 	ts->tv_sec = mktime64(tm.year, tm.month, tm.day,
263 			    tm.hour, tm.minute, tm.second);
264 	ts->tv_nsec = tm.nanosecond;
265 }
266 
267 static int
268 is_memory_available (efi_memory_desc_t *md)
269 {
270 	if (!(md->attribute & EFI_MEMORY_WB))
271 		return 0;
272 
273 	switch (md->type) {
274 	      case EFI_LOADER_CODE:
275 	      case EFI_LOADER_DATA:
276 	      case EFI_BOOT_SERVICES_CODE:
277 	      case EFI_BOOT_SERVICES_DATA:
278 	      case EFI_CONVENTIONAL_MEMORY:
279 		return 1;
280 	}
281 	return 0;
282 }
283 
284 typedef struct kern_memdesc {
285 	u64 attribute;
286 	u64 start;
287 	u64 num_pages;
288 } kern_memdesc_t;
289 
290 static kern_memdesc_t *kern_memmap;
291 
292 #define efi_md_size(md)	(md->num_pages << EFI_PAGE_SHIFT)
293 
294 static inline u64
295 kmd_end(kern_memdesc_t *kmd)
296 {
297 	return (kmd->start + (kmd->num_pages << EFI_PAGE_SHIFT));
298 }
299 
300 static inline u64
301 efi_md_end(efi_memory_desc_t *md)
302 {
303 	return (md->phys_addr + efi_md_size(md));
304 }
305 
306 static inline int
307 efi_wb(efi_memory_desc_t *md)
308 {
309 	return (md->attribute & EFI_MEMORY_WB);
310 }
311 
312 static inline int
313 efi_uc(efi_memory_desc_t *md)
314 {
315 	return (md->attribute & EFI_MEMORY_UC);
316 }
317 
318 static void
319 walk (efi_freemem_callback_t callback, void *arg, u64 attr)
320 {
321 	kern_memdesc_t *k;
322 	u64 start, end, voff;
323 
324 	voff = (attr == EFI_MEMORY_WB) ? PAGE_OFFSET : __IA64_UNCACHED_OFFSET;
325 	for (k = kern_memmap; k->start != ~0UL; k++) {
326 		if (k->attribute != attr)
327 			continue;
328 		start = PAGE_ALIGN(k->start);
329 		end = (k->start + (k->num_pages << EFI_PAGE_SHIFT)) & PAGE_MASK;
330 		if (start < end)
331 			if ((*callback)(start + voff, end + voff, arg) < 0)
332 				return;
333 	}
334 }
335 
336 /*
337  * Walk the EFI memory map and call CALLBACK once for each EFI memory
338  * descriptor that has memory that is available for OS use.
339  */
340 void
341 efi_memmap_walk (efi_freemem_callback_t callback, void *arg)
342 {
343 	walk(callback, arg, EFI_MEMORY_WB);
344 }
345 
346 /*
347  * Walk the EFI memory map and call CALLBACK once for each EFI memory
348  * descriptor that has memory that is available for uncached allocator.
349  */
350 void
351 efi_memmap_walk_uc (efi_freemem_callback_t callback, void *arg)
352 {
353 	walk(callback, arg, EFI_MEMORY_UC);
354 }
355 
356 /*
357  * Look for the PAL_CODE region reported by EFI and map it using an
358  * ITR to enable safe PAL calls in virtual mode.  See IA-64 Processor
359  * Abstraction Layer chapter 11 in ADAG
360  */
361 void *
362 efi_get_pal_addr (void)
363 {
364 	void *efi_map_start, *efi_map_end, *p;
365 	efi_memory_desc_t *md;
366 	u64 efi_desc_size;
367 	int pal_code_count = 0;
368 	u64 vaddr, mask;
369 
370 	efi_map_start = __va(ia64_boot_param->efi_memmap);
371 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
372 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
373 
374 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
375 		md = p;
376 		if (md->type != EFI_PAL_CODE)
377 			continue;
378 
379 		if (++pal_code_count > 1) {
380 			printk(KERN_ERR "Too many EFI Pal Code memory ranges, "
381 			       "dropped @ %llx\n", md->phys_addr);
382 			continue;
383 		}
384 		/*
385 		 * The only ITLB entry in region 7 that is used is the one
386 		 * installed by __start().  That entry covers a 64MB range.
387 		 */
388 		mask  = ~((1 << KERNEL_TR_PAGE_SHIFT) - 1);
389 		vaddr = PAGE_OFFSET + md->phys_addr;
390 
391 		/*
392 		 * We must check that the PAL mapping won't overlap with the
393 		 * kernel mapping.
394 		 *
395 		 * PAL code is guaranteed to be aligned on a power of 2 between
396 		 * 4k and 256KB and that only one ITR is needed to map it. This
397 		 * implies that the PAL code is always aligned on its size,
398 		 * i.e., the closest matching page size supported by the TLB.
399 		 * Therefore PAL code is guaranteed never to cross a 64MB unless
400 		 * it is bigger than 64MB (very unlikely!).  So for now the
401 		 * following test is enough to determine whether or not we need
402 		 * a dedicated ITR for the PAL code.
403 		 */
404 		if ((vaddr & mask) == (KERNEL_START & mask)) {
405 			printk(KERN_INFO "%s: no need to install ITR for PAL code\n",
406 			       __func__);
407 			continue;
408 		}
409 
410 		if (efi_md_size(md) > IA64_GRANULE_SIZE)
411 			panic("Whoa!  PAL code size bigger than a granule!");
412 
413 #if EFI_DEBUG
414 		mask  = ~((1 << IA64_GRANULE_SHIFT) - 1);
415 
416 		printk(KERN_INFO "CPU %d: mapping PAL code "
417                        "[0x%lx-0x%lx) into [0x%lx-0x%lx)\n",
418                        smp_processor_id(), md->phys_addr,
419                        md->phys_addr + efi_md_size(md),
420                        vaddr & mask, (vaddr & mask) + IA64_GRANULE_SIZE);
421 #endif
422 		return __va(md->phys_addr);
423 	}
424 	printk(KERN_WARNING "%s: no PAL-code memory-descriptor found\n",
425 	       __func__);
426 	return NULL;
427 }
428 
429 
430 static u8 __init palo_checksum(u8 *buffer, u32 length)
431 {
432 	u8 sum = 0;
433 	u8 *end = buffer + length;
434 
435 	while (buffer < end)
436 		sum = (u8) (sum + *(buffer++));
437 
438 	return sum;
439 }
440 
441 /*
442  * Parse and handle PALO table which is published at:
443  * http://www.dig64.org/home/DIG64_PALO_R1_0.pdf
444  */
445 static void __init handle_palo(unsigned long phys_addr)
446 {
447 	struct palo_table *palo = __va(phys_addr);
448 	u8  checksum;
449 
450 	if (strncmp(palo->signature, PALO_SIG, sizeof(PALO_SIG) - 1)) {
451 		printk(KERN_INFO "PALO signature incorrect.\n");
452 		return;
453 	}
454 
455 	checksum = palo_checksum((u8 *)palo, palo->length);
456 	if (checksum) {
457 		printk(KERN_INFO "PALO checksum incorrect.\n");
458 		return;
459 	}
460 
461 	setup_ptcg_sem(palo->max_tlb_purges, NPTCG_FROM_PALO);
462 }
463 
464 void
465 efi_map_pal_code (void)
466 {
467 	void *pal_vaddr = efi_get_pal_addr ();
468 	u64 psr;
469 
470 	if (!pal_vaddr)
471 		return;
472 
473 	/*
474 	 * Cannot write to CRx with PSR.ic=1
475 	 */
476 	psr = ia64_clear_ic();
477 	ia64_itr(0x1, IA64_TR_PALCODE,
478 		 GRANULEROUNDDOWN((unsigned long) pal_vaddr),
479 		 pte_val(pfn_pte(__pa(pal_vaddr) >> PAGE_SHIFT, PAGE_KERNEL)),
480 		 IA64_GRANULE_SHIFT);
481 	ia64_set_psr(psr);		/* restore psr */
482 }
483 
484 void __init
485 efi_init (void)
486 {
487 	const efi_system_table_t *efi_systab;
488 	void *efi_map_start, *efi_map_end;
489 	u64 efi_desc_size;
490 	char *cp;
491 
492 	set_bit(EFI_BOOT, &efi.flags);
493 	set_bit(EFI_64BIT, &efi.flags);
494 
495 	/*
496 	 * It's too early to be able to use the standard kernel command line
497 	 * support...
498 	 */
499 	for (cp = boot_command_line; *cp; ) {
500 		if (memcmp(cp, "mem=", 4) == 0) {
501 			mem_limit = memparse(cp + 4, &cp);
502 		} else if (memcmp(cp, "max_addr=", 9) == 0) {
503 			max_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
504 		} else if (memcmp(cp, "min_addr=", 9) == 0) {
505 			min_addr = GRANULEROUNDDOWN(memparse(cp + 9, &cp));
506 		} else {
507 			while (*cp != ' ' && *cp)
508 				++cp;
509 			while (*cp == ' ')
510 				++cp;
511 		}
512 	}
513 	if (min_addr != 0UL)
514 		printk(KERN_INFO "Ignoring memory below %lluMB\n",
515 		       min_addr >> 20);
516 	if (max_addr != ~0UL)
517 		printk(KERN_INFO "Ignoring memory above %lluMB\n",
518 		       max_addr >> 20);
519 
520 	efi_systab = __va(ia64_boot_param->efi_systab);
521 
522 	/*
523 	 * Verify the EFI Table
524 	 */
525 	if (efi_systab == NULL)
526 		panic("Whoa! Can't find EFI system table.\n");
527 	if (efi_systab_check_header(&efi_systab->hdr, 1))
528 		panic("Whoa! EFI system table signature incorrect\n");
529 
530 	efi_systab_report_header(&efi_systab->hdr, efi_systab->fw_vendor);
531 
532 	palo_phys      = EFI_INVALID_TABLE_ADDR;
533 
534 	if (efi_config_parse_tables(__va(efi_systab->tables),
535 				    efi_systab->nr_tables,
536 				    arch_tables) != 0)
537 		return;
538 
539 	if (palo_phys != EFI_INVALID_TABLE_ADDR)
540 		handle_palo(palo_phys);
541 
542 	runtime = __va(efi_systab->runtime);
543 	efi.get_time = phys_get_time;
544 	efi.set_time = phys_set_time;
545 	efi.get_wakeup_time = phys_get_wakeup_time;
546 	efi.set_wakeup_time = phys_set_wakeup_time;
547 	efi.get_variable = phys_get_variable;
548 	efi.get_next_variable = phys_get_next_variable;
549 	efi.set_variable = phys_set_variable;
550 	efi.get_next_high_mono_count = phys_get_next_high_mono_count;
551 	efi.reset_system = phys_reset_system;
552 
553 	efi_map_start = __va(ia64_boot_param->efi_memmap);
554 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
555 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
556 
557 #if EFI_DEBUG
558 	/* print EFI memory map: */
559 	{
560 		efi_memory_desc_t *md;
561 		void *p;
562 
563 		for (i = 0, p = efi_map_start; p < efi_map_end;
564 		     ++i, p += efi_desc_size)
565 		{
566 			const char *unit;
567 			unsigned long size;
568 			char buf[64];
569 
570 			md = p;
571 			size = md->num_pages << EFI_PAGE_SHIFT;
572 
573 			if ((size >> 40) > 0) {
574 				size >>= 40;
575 				unit = "TB";
576 			} else if ((size >> 30) > 0) {
577 				size >>= 30;
578 				unit = "GB";
579 			} else if ((size >> 20) > 0) {
580 				size >>= 20;
581 				unit = "MB";
582 			} else {
583 				size >>= 10;
584 				unit = "KB";
585 			}
586 
587 			printk("mem%02d: %s "
588 			       "range=[0x%016lx-0x%016lx) (%4lu%s)\n",
589 			       i, efi_md_typeattr_format(buf, sizeof(buf), md),
590 			       md->phys_addr,
591 			       md->phys_addr + efi_md_size(md), size, unit);
592 		}
593 	}
594 #endif
595 
596 	efi_map_pal_code();
597 	efi_enter_virtual_mode();
598 }
599 
600 void
601 efi_enter_virtual_mode (void)
602 {
603 	void *efi_map_start, *efi_map_end, *p;
604 	efi_memory_desc_t *md;
605 	efi_status_t status;
606 	u64 efi_desc_size;
607 
608 	efi_map_start = __va(ia64_boot_param->efi_memmap);
609 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
610 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
611 
612 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
613 		md = p;
614 		if (md->attribute & EFI_MEMORY_RUNTIME) {
615 			/*
616 			 * Some descriptors have multiple bits set, so the
617 			 * order of the tests is relevant.
618 			 */
619 			if (md->attribute & EFI_MEMORY_WB) {
620 				md->virt_addr = (u64) __va(md->phys_addr);
621 			} else if (md->attribute & EFI_MEMORY_UC) {
622 				md->virt_addr = (u64) ioremap(md->phys_addr, 0);
623 			} else if (md->attribute & EFI_MEMORY_WC) {
624 #if 0
625 				md->virt_addr = ia64_remap(md->phys_addr,
626 							   (_PAGE_A |
627 							    _PAGE_P |
628 							    _PAGE_D |
629 							    _PAGE_MA_WC |
630 							    _PAGE_PL_0 |
631 							    _PAGE_AR_RW));
632 #else
633 				printk(KERN_INFO "EFI_MEMORY_WC mapping\n");
634 				md->virt_addr = (u64) ioremap(md->phys_addr, 0);
635 #endif
636 			} else if (md->attribute & EFI_MEMORY_WT) {
637 #if 0
638 				md->virt_addr = ia64_remap(md->phys_addr,
639 							   (_PAGE_A |
640 							    _PAGE_P |
641 							    _PAGE_D |
642 							    _PAGE_MA_WT |
643 							    _PAGE_PL_0 |
644 							    _PAGE_AR_RW));
645 #else
646 				printk(KERN_INFO "EFI_MEMORY_WT mapping\n");
647 				md->virt_addr = (u64) ioremap(md->phys_addr, 0);
648 #endif
649 			}
650 		}
651 	}
652 
653 	status = efi_call_phys(__va(runtime->set_virtual_address_map),
654 			       ia64_boot_param->efi_memmap_size,
655 			       efi_desc_size,
656 			       ia64_boot_param->efi_memdesc_version,
657 			       ia64_boot_param->efi_memmap);
658 	if (status != EFI_SUCCESS) {
659 		printk(KERN_WARNING "warning: unable to switch EFI into "
660 		       "virtual mode (status=%lu)\n", status);
661 		return;
662 	}
663 
664 	set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
665 
666 	/*
667 	 * Now that EFI is in virtual mode, we call the EFI functions more
668 	 * efficiently:
669 	 */
670 	efi.get_time = virt_get_time;
671 	efi.set_time = virt_set_time;
672 	efi.get_wakeup_time = virt_get_wakeup_time;
673 	efi.set_wakeup_time = virt_set_wakeup_time;
674 	efi.get_variable = virt_get_variable;
675 	efi.get_next_variable = virt_get_next_variable;
676 	efi.set_variable = virt_set_variable;
677 	efi.get_next_high_mono_count = virt_get_next_high_mono_count;
678 	efi.reset_system = virt_reset_system;
679 }
680 
681 /*
682  * Walk the EFI memory map looking for the I/O port range.  There can only be
683  * one entry of this type, other I/O port ranges should be described via ACPI.
684  */
685 u64
686 efi_get_iobase (void)
687 {
688 	void *efi_map_start, *efi_map_end, *p;
689 	efi_memory_desc_t *md;
690 	u64 efi_desc_size;
691 
692 	efi_map_start = __va(ia64_boot_param->efi_memmap);
693 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
694 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
695 
696 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
697 		md = p;
698 		if (md->type == EFI_MEMORY_MAPPED_IO_PORT_SPACE) {
699 			if (md->attribute & EFI_MEMORY_UC)
700 				return md->phys_addr;
701 		}
702 	}
703 	return 0;
704 }
705 
706 static struct kern_memdesc *
707 kern_memory_descriptor (unsigned long phys_addr)
708 {
709 	struct kern_memdesc *md;
710 
711 	for (md = kern_memmap; md->start != ~0UL; md++) {
712 		if (phys_addr - md->start < (md->num_pages << EFI_PAGE_SHIFT))
713 			 return md;
714 	}
715 	return NULL;
716 }
717 
718 static efi_memory_desc_t *
719 efi_memory_descriptor (unsigned long phys_addr)
720 {
721 	void *efi_map_start, *efi_map_end, *p;
722 	efi_memory_desc_t *md;
723 	u64 efi_desc_size;
724 
725 	efi_map_start = __va(ia64_boot_param->efi_memmap);
726 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
727 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
728 
729 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
730 		md = p;
731 
732 		if (phys_addr - md->phys_addr < efi_md_size(md))
733 			 return md;
734 	}
735 	return NULL;
736 }
737 
738 static int
739 efi_memmap_intersects (unsigned long phys_addr, unsigned long size)
740 {
741 	void *efi_map_start, *efi_map_end, *p;
742 	efi_memory_desc_t *md;
743 	u64 efi_desc_size;
744 	unsigned long end;
745 
746 	efi_map_start = __va(ia64_boot_param->efi_memmap);
747 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
748 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
749 
750 	end = phys_addr + size;
751 
752 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
753 		md = p;
754 		if (md->phys_addr < end && efi_md_end(md) > phys_addr)
755 			return 1;
756 	}
757 	return 0;
758 }
759 
760 int
761 efi_mem_type (unsigned long phys_addr)
762 {
763 	efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
764 
765 	if (md)
766 		return md->type;
767 	return -EINVAL;
768 }
769 
770 u64
771 efi_mem_attributes (unsigned long phys_addr)
772 {
773 	efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
774 
775 	if (md)
776 		return md->attribute;
777 	return 0;
778 }
779 EXPORT_SYMBOL(efi_mem_attributes);
780 
781 u64
782 efi_mem_attribute (unsigned long phys_addr, unsigned long size)
783 {
784 	unsigned long end = phys_addr + size;
785 	efi_memory_desc_t *md = efi_memory_descriptor(phys_addr);
786 	u64 attr;
787 
788 	if (!md)
789 		return 0;
790 
791 	/*
792 	 * EFI_MEMORY_RUNTIME is not a memory attribute; it just tells
793 	 * the kernel that firmware needs this region mapped.
794 	 */
795 	attr = md->attribute & ~EFI_MEMORY_RUNTIME;
796 	do {
797 		unsigned long md_end = efi_md_end(md);
798 
799 		if (end <= md_end)
800 			return attr;
801 
802 		md = efi_memory_descriptor(md_end);
803 		if (!md || (md->attribute & ~EFI_MEMORY_RUNTIME) != attr)
804 			return 0;
805 	} while (md);
806 	return 0;	/* never reached */
807 }
808 
809 u64
810 kern_mem_attribute (unsigned long phys_addr, unsigned long size)
811 {
812 	unsigned long end = phys_addr + size;
813 	struct kern_memdesc *md;
814 	u64 attr;
815 
816 	/*
817 	 * This is a hack for ioremap calls before we set up kern_memmap.
818 	 * Maybe we should do efi_memmap_init() earlier instead.
819 	 */
820 	if (!kern_memmap) {
821 		attr = efi_mem_attribute(phys_addr, size);
822 		if (attr & EFI_MEMORY_WB)
823 			return EFI_MEMORY_WB;
824 		return 0;
825 	}
826 
827 	md = kern_memory_descriptor(phys_addr);
828 	if (!md)
829 		return 0;
830 
831 	attr = md->attribute;
832 	do {
833 		unsigned long md_end = kmd_end(md);
834 
835 		if (end <= md_end)
836 			return attr;
837 
838 		md = kern_memory_descriptor(md_end);
839 		if (!md || md->attribute != attr)
840 			return 0;
841 	} while (md);
842 	return 0;	/* never reached */
843 }
844 
845 int
846 valid_phys_addr_range (phys_addr_t phys_addr, unsigned long size)
847 {
848 	u64 attr;
849 
850 	/*
851 	 * /dev/mem reads and writes use copy_to_user(), which implicitly
852 	 * uses a granule-sized kernel identity mapping.  It's really
853 	 * only safe to do this for regions in kern_memmap.  For more
854 	 * details, see Documentation/ia64/aliasing.rst.
855 	 */
856 	attr = kern_mem_attribute(phys_addr, size);
857 	if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
858 		return 1;
859 	return 0;
860 }
861 
862 int
863 valid_mmap_phys_addr_range (unsigned long pfn, unsigned long size)
864 {
865 	unsigned long phys_addr = pfn << PAGE_SHIFT;
866 	u64 attr;
867 
868 	attr = efi_mem_attribute(phys_addr, size);
869 
870 	/*
871 	 * /dev/mem mmap uses normal user pages, so we don't need the entire
872 	 * granule, but the entire region we're mapping must support the same
873 	 * attribute.
874 	 */
875 	if (attr & EFI_MEMORY_WB || attr & EFI_MEMORY_UC)
876 		return 1;
877 
878 	/*
879 	 * Intel firmware doesn't tell us about all the MMIO regions, so
880 	 * in general we have to allow mmap requests.  But if EFI *does*
881 	 * tell us about anything inside this region, we should deny it.
882 	 * The user can always map a smaller region to avoid the overlap.
883 	 */
884 	if (efi_memmap_intersects(phys_addr, size))
885 		return 0;
886 
887 	return 1;
888 }
889 
890 pgprot_t
891 phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size,
892 		     pgprot_t vma_prot)
893 {
894 	unsigned long phys_addr = pfn << PAGE_SHIFT;
895 	u64 attr;
896 
897 	/*
898 	 * For /dev/mem mmap, we use user mappings, but if the region is
899 	 * in kern_memmap (and hence may be covered by a kernel mapping),
900 	 * we must use the same attribute as the kernel mapping.
901 	 */
902 	attr = kern_mem_attribute(phys_addr, size);
903 	if (attr & EFI_MEMORY_WB)
904 		return pgprot_cacheable(vma_prot);
905 	else if (attr & EFI_MEMORY_UC)
906 		return pgprot_noncached(vma_prot);
907 
908 	/*
909 	 * Some chipsets don't support UC access to memory.  If
910 	 * WB is supported, we prefer that.
911 	 */
912 	if (efi_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
913 		return pgprot_cacheable(vma_prot);
914 
915 	return pgprot_noncached(vma_prot);
916 }
917 
918 int __init
919 efi_uart_console_only(void)
920 {
921 	efi_status_t status;
922 	char *s, name[] = "ConOut";
923 	efi_guid_t guid = EFI_GLOBAL_VARIABLE_GUID;
924 	efi_char16_t *utf16, name_utf16[32];
925 	unsigned char data[1024];
926 	unsigned long size = sizeof(data);
927 	struct efi_generic_dev_path *hdr, *end_addr;
928 	int uart = 0;
929 
930 	/* Convert to UTF-16 */
931 	utf16 = name_utf16;
932 	s = name;
933 	while (*s)
934 		*utf16++ = *s++ & 0x7f;
935 	*utf16 = 0;
936 
937 	status = efi.get_variable(name_utf16, &guid, NULL, &size, data);
938 	if (status != EFI_SUCCESS) {
939 		printk(KERN_ERR "No EFI %s variable?\n", name);
940 		return 0;
941 	}
942 
943 	hdr = (struct efi_generic_dev_path *) data;
944 	end_addr = (struct efi_generic_dev_path *) ((u8 *) data + size);
945 	while (hdr < end_addr) {
946 		if (hdr->type == EFI_DEV_MSG &&
947 		    hdr->sub_type == EFI_DEV_MSG_UART)
948 			uart = 1;
949 		else if (hdr->type == EFI_DEV_END_PATH ||
950 			  hdr->type == EFI_DEV_END_PATH2) {
951 			if (!uart)
952 				return 0;
953 			if (hdr->sub_type == EFI_DEV_END_ENTIRE)
954 				return 1;
955 			uart = 0;
956 		}
957 		hdr = (struct efi_generic_dev_path *)((u8 *) hdr + hdr->length);
958 	}
959 	printk(KERN_ERR "Malformed %s value\n", name);
960 	return 0;
961 }
962 
963 /*
964  * Look for the first granule aligned memory descriptor memory
965  * that is big enough to hold EFI memory map. Make sure this
966  * descriptor is at least granule sized so it does not get trimmed
967  */
968 struct kern_memdesc *
969 find_memmap_space (void)
970 {
971 	u64	contig_low=0, contig_high=0;
972 	u64	as = 0, ae;
973 	void *efi_map_start, *efi_map_end, *p, *q;
974 	efi_memory_desc_t *md, *pmd = NULL, *check_md;
975 	u64	space_needed, efi_desc_size;
976 	unsigned long total_mem = 0;
977 
978 	efi_map_start = __va(ia64_boot_param->efi_memmap);
979 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
980 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
981 
982 	/*
983 	 * Worst case: we need 3 kernel descriptors for each efi descriptor
984 	 * (if every entry has a WB part in the middle, and UC head and tail),
985 	 * plus one for the end marker.
986 	 */
987 	space_needed = sizeof(kern_memdesc_t) *
988 		(3 * (ia64_boot_param->efi_memmap_size/efi_desc_size) + 1);
989 
990 	for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
991 		md = p;
992 		if (!efi_wb(md)) {
993 			continue;
994 		}
995 		if (pmd == NULL || !efi_wb(pmd) ||
996 		    efi_md_end(pmd) != md->phys_addr) {
997 			contig_low = GRANULEROUNDUP(md->phys_addr);
998 			contig_high = efi_md_end(md);
999 			for (q = p + efi_desc_size; q < efi_map_end;
1000 			     q += efi_desc_size) {
1001 				check_md = q;
1002 				if (!efi_wb(check_md))
1003 					break;
1004 				if (contig_high != check_md->phys_addr)
1005 					break;
1006 				contig_high = efi_md_end(check_md);
1007 			}
1008 			contig_high = GRANULEROUNDDOWN(contig_high);
1009 		}
1010 		if (!is_memory_available(md) || md->type == EFI_LOADER_DATA)
1011 			continue;
1012 
1013 		/* Round ends inward to granule boundaries */
1014 		as = max(contig_low, md->phys_addr);
1015 		ae = min(contig_high, efi_md_end(md));
1016 
1017 		/* keep within max_addr= and min_addr= command line arg */
1018 		as = max(as, min_addr);
1019 		ae = min(ae, max_addr);
1020 		if (ae <= as)
1021 			continue;
1022 
1023 		/* avoid going over mem= command line arg */
1024 		if (total_mem + (ae - as) > mem_limit)
1025 			ae -= total_mem + (ae - as) - mem_limit;
1026 
1027 		if (ae <= as)
1028 			continue;
1029 
1030 		if (ae - as > space_needed)
1031 			break;
1032 	}
1033 	if (p >= efi_map_end)
1034 		panic("Can't allocate space for kernel memory descriptors");
1035 
1036 	return __va(as);
1037 }
1038 
1039 /*
1040  * Walk the EFI memory map and gather all memory available for kernel
1041  * to use.  We can allocate partial granules only if the unavailable
1042  * parts exist, and are WB.
1043  */
1044 unsigned long
1045 efi_memmap_init(u64 *s, u64 *e)
1046 {
1047 	struct kern_memdesc *k, *prev = NULL;
1048 	u64	contig_low=0, contig_high=0;
1049 	u64	as, ae, lim;
1050 	void *efi_map_start, *efi_map_end, *p, *q;
1051 	efi_memory_desc_t *md, *pmd = NULL, *check_md;
1052 	u64	efi_desc_size;
1053 	unsigned long total_mem = 0;
1054 
1055 	k = kern_memmap = find_memmap_space();
1056 
1057 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1058 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1059 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1060 
1061 	for (p = efi_map_start; p < efi_map_end; pmd = md, p += efi_desc_size) {
1062 		md = p;
1063 		if (!efi_wb(md)) {
1064 			if (efi_uc(md) &&
1065 			    (md->type == EFI_CONVENTIONAL_MEMORY ||
1066 			     md->type == EFI_BOOT_SERVICES_DATA)) {
1067 				k->attribute = EFI_MEMORY_UC;
1068 				k->start = md->phys_addr;
1069 				k->num_pages = md->num_pages;
1070 				k++;
1071 			}
1072 			continue;
1073 		}
1074 		if (pmd == NULL || !efi_wb(pmd) ||
1075 		    efi_md_end(pmd) != md->phys_addr) {
1076 			contig_low = GRANULEROUNDUP(md->phys_addr);
1077 			contig_high = efi_md_end(md);
1078 			for (q = p + efi_desc_size; q < efi_map_end;
1079 			     q += efi_desc_size) {
1080 				check_md = q;
1081 				if (!efi_wb(check_md))
1082 					break;
1083 				if (contig_high != check_md->phys_addr)
1084 					break;
1085 				contig_high = efi_md_end(check_md);
1086 			}
1087 			contig_high = GRANULEROUNDDOWN(contig_high);
1088 		}
1089 		if (!is_memory_available(md))
1090 			continue;
1091 
1092 		/*
1093 		 * Round ends inward to granule boundaries
1094 		 * Give trimmings to uncached allocator
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 		/* keep within max_addr= and min_addr= command line arg */
1137 		as = max(as, min_addr);
1138 		ae = min(ae, max_addr);
1139 		if (ae <= as)
1140 			continue;
1141 
1142 		/* avoid going over mem= command line arg */
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; /* end-marker */
1160 
1161 	/* reserve the memory we are using for kern_memmap */
1162 	*s = (u64)kern_memmap;
1163 	*e = (u64)++k;
1164 
1165 	return total_mem;
1166 }
1167 
1168 void
1169 efi_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, desc;
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) /* should not happen */
1190 			continue;
1191 
1192 		flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1193 		desc = IORES_DESC_NONE;
1194 
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 					flags |= IORESOURCE_SYSRAM;
1214 				}
1215 				break;
1216 
1217 			case EFI_ACPI_MEMORY_NVS:
1218 				name = "ACPI Non-volatile Storage";
1219 				desc = IORES_DESC_ACPI_NV_STORAGE;
1220 				break;
1221 
1222 			case EFI_UNUSABLE_MEMORY:
1223 				name = "reserved";
1224 				flags |= IORESOURCE_DISABLED;
1225 				break;
1226 
1227 			case EFI_PERSISTENT_MEMORY:
1228 				name = "Persistent Memory";
1229 				desc = IORES_DESC_PERSISTENT_MEMORY;
1230 				break;
1231 
1232 			case EFI_RESERVED_TYPE:
1233 			case EFI_RUNTIME_SERVICES_CODE:
1234 			case EFI_RUNTIME_SERVICES_DATA:
1235 			case EFI_ACPI_RECLAIM_MEMORY:
1236 			default:
1237 				name = "reserved";
1238 				break;
1239 		}
1240 
1241 		if ((res = kzalloc(sizeof(struct resource),
1242 				   GFP_KERNEL)) == NULL) {
1243 			printk(KERN_ERR
1244 			       "failed to allocate resource for iomem\n");
1245 			return;
1246 		}
1247 
1248 		res->name = name;
1249 		res->start = md->phys_addr;
1250 		res->end = md->phys_addr + efi_md_size(md) - 1;
1251 		res->flags = flags;
1252 		res->desc = desc;
1253 
1254 		if (insert_resource(&iomem_resource, res) < 0)
1255 			kfree(res);
1256 		else {
1257 			/*
1258 			 * We don't know which region contains
1259 			 * kernel data so we try it repeatedly and
1260 			 * let the resource manager test it.
1261 			 */
1262 			insert_resource(res, code_resource);
1263 			insert_resource(res, data_resource);
1264 			insert_resource(res, bss_resource);
1265 #ifdef CONFIG_KEXEC
1266                         insert_resource(res, &efi_memmap_res);
1267                         insert_resource(res, &boot_param_res);
1268 			if (crashk_res.end > crashk_res.start)
1269 				insert_resource(res, &crashk_res);
1270 #endif
1271 		}
1272 	}
1273 }
1274 
1275 #ifdef CONFIG_KEXEC
1276 /* find a block of memory aligned to 64M exclude reserved regions
1277    rsvd_regions are sorted
1278  */
1279 unsigned long __init
1280 kdump_find_rsvd_region (unsigned long size, struct rsvd_region *r, int n)
1281 {
1282 	int i;
1283 	u64 start, end;
1284 	u64 alignment = 1UL << _PAGE_SIZE_64M;
1285 	void *efi_map_start, *efi_map_end, *p;
1286 	efi_memory_desc_t *md;
1287 	u64 efi_desc_size;
1288 
1289 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1290 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1291 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1292 
1293 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1294 		md = p;
1295 		if (!efi_wb(md))
1296 			continue;
1297 		start = ALIGN(md->phys_addr, alignment);
1298 		end = efi_md_end(md);
1299 		for (i = 0; i < n; i++) {
1300 			if (__pa(r[i].start) >= start && __pa(r[i].end) < end) {
1301 				if (__pa(r[i].start) > start + size)
1302 					return start;
1303 				start = ALIGN(__pa(r[i].end), alignment);
1304 				if (i < n-1 &&
1305 				    __pa(r[i+1].start) < start + size)
1306 					continue;
1307 				else
1308 					break;
1309 			}
1310 		}
1311 		if (end > start + size)
1312 			return start;
1313 	}
1314 
1315 	printk(KERN_WARNING
1316 	       "Cannot reserve 0x%lx byte of memory for crashdump\n", size);
1317 	return ~0UL;
1318 }
1319 #endif
1320 
1321 #ifdef CONFIG_CRASH_DUMP
1322 /* locate the size find a the descriptor at a certain address */
1323 unsigned long __init
1324 vmcore_find_descriptor_size (unsigned long address)
1325 {
1326 	void *efi_map_start, *efi_map_end, *p;
1327 	efi_memory_desc_t *md;
1328 	u64 efi_desc_size;
1329 	unsigned long ret = 0;
1330 
1331 	efi_map_start = __va(ia64_boot_param->efi_memmap);
1332 	efi_map_end   = efi_map_start + ia64_boot_param->efi_memmap_size;
1333 	efi_desc_size = ia64_boot_param->efi_memdesc_size;
1334 
1335 	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
1336 		md = p;
1337 		if (efi_wb(md) && md->type == EFI_LOADER_DATA
1338 		    && md->phys_addr == address) {
1339 			ret = efi_md_size(md);
1340 			break;
1341 		}
1342 	}
1343 
1344 	if (ret == 0)
1345 		printk(KERN_WARNING "Cannot locate EFI vmcore descriptor\n");
1346 
1347 	return ret;
1348 }
1349 #endif
1350 
1351 char *efi_systab_show_arch(char *str)
1352 {
1353 	if (mps_phys != EFI_INVALID_TABLE_ADDR)
1354 		str += sprintf(str, "MPS=0x%lx\n", mps_phys);
1355 	if (hcdp_phys != EFI_INVALID_TABLE_ADDR)
1356 		str += sprintf(str, "HCDP=0x%lx\n", hcdp_phys);
1357 	return str;
1358 }
1359