Linux Audio

Check our new training course

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