direct-io.hg

changeset 15297:35530f81e638

[IA64] memmap: copy dom_fw.c to dom_fw_common.c

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Alex Williamson <alex.williamson@hp.com>
date Mon May 21 13:16:01 2007 -0600 (2007-05-21)
parents 8fb8d50272d2
children 8dbc11034f82
files xen/arch/ia64/xen/dom_fw_common.c
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xen/arch/ia64/xen/dom_fw_common.c	Mon May 21 13:16:01 2007 -0600
     1.3 @@ -0,0 +1,1233 @@
     1.4 +/*
     1.5 + *  Xen domain firmware emulation support
     1.6 + *  Copyright (C) 2004 Hewlett-Packard Co.
     1.7 + *       Dan Magenheimer (dan.magenheimer@hp.com)
     1.8 + *
     1.9 + * Copyright (c) 2006, 2007
    1.10 + *                    Isaku Yamahata <yamahata at valinux co jp>
    1.11 + *                    VA Linux Systems Japan K.K.
    1.12 + *                    dom0 vp model support
    1.13 + */
    1.14 +
    1.15 +#include <xen/config.h>
    1.16 +#include <asm/system.h>
    1.17 +#include <asm/pgalloc.h>
    1.18 +
    1.19 +#include <linux/efi.h>
    1.20 +#include <linux/sort.h>
    1.21 +#include <asm/io.h>
    1.22 +#include <asm/pal.h>
    1.23 +#include <asm/sal.h>
    1.24 +#include <asm/meminit.h>
    1.25 +#include <asm/fpswa.h>
    1.26 +#include <xen/version.h>
    1.27 +#include <xen/acpi.h>
    1.28 +#include <xen/errno.h>
    1.29 +
    1.30 +#include <asm/dom_fw.h>
    1.31 +#include <asm/bundle.h>
    1.32 +
    1.33 +#define ONE_MB (1UL << 20)
    1.34 +
    1.35 +extern unsigned long running_on_sim;
    1.36 +
    1.37 +#define FW_VENDOR "X\0e\0n\0/\0i\0a\0\066\0\064\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
    1.38 +
    1.39 +#define MAKE_MD(typ, attr, start, end) 					\
    1.40 +	do {								\
    1.41 +		md = tables->efi_memmap + i++;				\
    1.42 +		md->type = typ;						\
    1.43 +		md->pad = 0;						\
    1.44 +		md->phys_addr = start;					\
    1.45 +		md->virt_addr = 0;					\
    1.46 +		md->num_pages = (end - start) >> EFI_PAGE_SHIFT;	\
    1.47 +		md->attribute = attr;					\
    1.48 +	} while (0)
    1.49 +
    1.50 +#define EFI_HYPERCALL_PATCH(tgt, call)					\
    1.51 +	do {								\
    1.52 +		dom_efi_hypercall_patch(brkimm,				\
    1.53 +					FW_HYPERCALL_##call##_PADDR,	\
    1.54 +		                 FW_HYPERCALL_##call, hypercalls_imva);	\
    1.55 +		/* Descriptor address.  */                              \
    1.56 +		tables->efi_runtime.tgt =                               \
    1.57 +		                    FW_FIELD_MPA(func_ptrs) + 8 * pfn;  \
    1.58 +		/* Descriptor.  */                                      \
    1.59 +		tables->func_ptrs[pfn++] = FW_HYPERCALL_##call##_PADDR;	\
    1.60 +		tables->func_ptrs[pfn++] = 0;                     	\
    1.61 +	} while (0)
    1.62 +
    1.63 +/* allocate a page for fw
    1.64 + * guest_setup() @ libxc/xc_linux_build.c does for domU
    1.65 + */
    1.66 +static inline void
    1.67 +assign_new_domain_page_if_dom0(struct domain *d, unsigned long mpaddr)
    1.68 +{
    1.69 +        if (d == dom0)
    1.70 +            assign_new_domain0_page(d, mpaddr);
    1.71 +}
    1.72 +
    1.73 +/**************************************************************************
    1.74 +Hypercall bundle creation
    1.75 +**************************************************************************/
    1.76 +
    1.77 +static void
    1.78 +build_hypercall_bundle(u64 *imva, u64 brkimm, u64 hypnum, u64 ret)
    1.79 +{
    1.80 +	INST64_A5 slot0;
    1.81 +	INST64_I19 slot1;
    1.82 +	INST64_B4 slot2;
    1.83 +	IA64_BUNDLE bundle;
    1.84 +
    1.85 +	// slot1: mov r2 = hypnum (low 20 bits)
    1.86 +	slot0.inst = 0;
    1.87 +	slot0.qp = 0; slot0.r1 = 2; slot0.r3 = 0; slot0.major = 0x9;
    1.88 +	slot0.imm7b = hypnum; slot0.imm9d = hypnum >> 7;
    1.89 +	slot0.imm5c = hypnum >> 16; slot0.s = 0;
    1.90 +	// slot1: break brkimm
    1.91 +	slot1.inst = 0;
    1.92 +	slot1.qp = 0; slot1.x6 = 0; slot1.x3 = 0; slot1.major = 0x0;
    1.93 +	slot1.imm20 = brkimm; slot1.i = brkimm >> 20;
    1.94 +	// if ret slot2:  br.ret.sptk.many rp
    1.95 +	// else   slot2:  br.cond.sptk.many rp
    1.96 +	slot2.inst = 0; slot2.qp = 0; slot2.p = 1; slot2.b2 = 0;
    1.97 +	slot2.wh = 0; slot2.d = 0; slot2.major = 0x0;
    1.98 +	if (ret) {
    1.99 +		slot2.btype = 4; slot2.x6 = 0x21;
   1.100 +	}
   1.101 +	else {
   1.102 +		slot2.btype = 0; slot2.x6 = 0x20;
   1.103 +	}
   1.104 +	
   1.105 +	bundle.i64[0] = 0; bundle.i64[1] = 0;
   1.106 +	bundle.template = 0x11;
   1.107 +	bundle.slot0 = slot0.inst; bundle.slot2 = slot2.inst;
   1.108 +	bundle.slot1a = slot1.inst; bundle.slot1b = slot1.inst >> 18;
   1.109 +	
   1.110 +	imva[0] = bundle.i64[0]; imva[1] = bundle.i64[1];
   1.111 +	ia64_fc(imva);
   1.112 +	ia64_fc(imva + 1);
   1.113 +}
   1.114 +
   1.115 +static void
   1.116 +build_pal_hypercall_bundles(u64 *imva, u64 brkimm, u64 hypnum)
   1.117 +{
   1.118 +	extern unsigned long pal_call_stub[];
   1.119 +	IA64_BUNDLE bundle;
   1.120 +	INST64_A5 slot_a5;
   1.121 +	INST64_M37 slot_m37;
   1.122 +
   1.123 +	/* The source of the hypercall stub is the pal_call_stub function
   1.124 +	   defined in xenasm.S.  */
   1.125 +
   1.126 +	/* Copy the first bundle and patch the hypercall number.  */
   1.127 +	bundle.i64[0] = pal_call_stub[0];
   1.128 +	bundle.i64[1] = pal_call_stub[1];
   1.129 +	slot_a5.inst = bundle.slot0;
   1.130 +	slot_a5.imm7b = hypnum;
   1.131 +	slot_a5.imm9d = hypnum >> 7;
   1.132 +	slot_a5.imm5c = hypnum >> 16;
   1.133 +	bundle.slot0 = slot_a5.inst;
   1.134 +	imva[0] = bundle.i64[0];
   1.135 +	imva[1] = bundle.i64[1];
   1.136 +	ia64_fc(imva);
   1.137 +	ia64_fc(imva + 1);
   1.138 +	
   1.139 +	/* Copy the second bundle and patch the hypercall vector.  */
   1.140 +	bundle.i64[0] = pal_call_stub[2];
   1.141 +	bundle.i64[1] = pal_call_stub[3];
   1.142 +	slot_m37.inst = bundle.slot0;
   1.143 +	slot_m37.imm20a = brkimm;
   1.144 +	slot_m37.i = brkimm >> 20;
   1.145 +	bundle.slot0 = slot_m37.inst;
   1.146 +	imva[2] = bundle.i64[0];
   1.147 +	imva[3] = bundle.i64[1];
   1.148 +	ia64_fc(imva + 2);
   1.149 +	ia64_fc(imva + 3);
   1.150 +}
   1.151 +
   1.152 +// builds a hypercall bundle at domain physical address
   1.153 +static void
   1.154 +dom_fpswa_hypercall_patch(u64 brkimm, unsigned long imva)
   1.155 +{
   1.156 +	unsigned long *entry_imva, *patch_imva;
   1.157 +	const unsigned long entry_paddr = FW_HYPERCALL_FPSWA_ENTRY_PADDR;
   1.158 +	const unsigned long patch_paddr = FW_HYPERCALL_FPSWA_PATCH_PADDR;
   1.159 +
   1.160 +	entry_imva = (unsigned long *)(imva + entry_paddr -
   1.161 +	                               FW_HYPERCALL_BASE_PADDR);
   1.162 +	patch_imva = (unsigned long *)(imva + patch_paddr -
   1.163 +	                               FW_HYPERCALL_BASE_PADDR);
   1.164 +
   1.165 +	/* Descriptor.  */
   1.166 +	*entry_imva++ = patch_paddr;
   1.167 +	*entry_imva   = 0;
   1.168 +
   1.169 +	build_hypercall_bundle(patch_imva, brkimm, FW_HYPERCALL_FPSWA, 1);
   1.170 +}
   1.171 +
   1.172 +// builds a hypercall bundle at domain physical address
   1.173 +static void
   1.174 +dom_efi_hypercall_patch(u64 brkimm, unsigned long paddr,
   1.175 +                        unsigned long hypercall, unsigned long imva)
   1.176 +{
   1.177 +	build_hypercall_bundle((u64 *)(imva + paddr - FW_HYPERCALL_BASE_PADDR),
   1.178 +	                       brkimm, hypercall, 1);
   1.179 +}
   1.180 +
   1.181 +// builds a hypercall bundle at domain physical address
   1.182 +static void
   1.183 +dom_fw_hypercall_patch(u64 brkimm, unsigned long paddr,
   1.184 +		       unsigned long hypercall,unsigned long ret,
   1.185 +                       unsigned long imva)
   1.186 +{
   1.187 +	build_hypercall_bundle((u64 *)(imva + paddr - FW_HYPERCALL_BASE_PADDR),
   1.188 +	                       brkimm, hypercall, ret);
   1.189 +}
   1.190 +
   1.191 +static void
   1.192 +dom_fw_pal_hypercall_patch(u64 brkimm, unsigned long paddr, unsigned long imva)
   1.193 +{
   1.194 +	build_pal_hypercall_bundles((u64*)(imva + paddr -
   1.195 +	                            FW_HYPERCALL_BASE_PADDR),
   1.196 +	                            brkimm, FW_HYPERCALL_PAL_CALL);
   1.197 +}
   1.198 +
   1.199 +static inline void
   1.200 +print_md(efi_memory_desc_t *md)
   1.201 +{
   1.202 +	u64 size;
   1.203 +	
   1.204 +	printk(XENLOG_INFO "dom mem: type=%2u, attr=0x%016lx, "
   1.205 +	       "range=[0x%016lx-0x%016lx) ",
   1.206 +	       md->type, md->attribute, md->phys_addr,
   1.207 +	       md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT));
   1.208 +
   1.209 +	size = md->num_pages << EFI_PAGE_SHIFT;
   1.210 +	if (size > ONE_MB)
   1.211 +		printk("(%luMB)\n", size >> 20);
   1.212 +	else
   1.213 +		printk("(%luKB)\n", size >> 10);
   1.214 +}
   1.215 +
   1.216 +static u32 lsapic_nbr;
   1.217 +
   1.218 +/* Modify lsapic table.  Provides LPs.  */
   1.219 +static int 
   1.220 +acpi_update_lsapic (acpi_table_entry_header *header, const unsigned long end)
   1.221 +{
   1.222 +	struct acpi_table_lsapic *lsapic;
   1.223 +	int enable;
   1.224 +
   1.225 +	lsapic = (struct acpi_table_lsapic *) header;
   1.226 +	if (!lsapic)
   1.227 +		return -EINVAL;
   1.228 +
   1.229 +	if (lsapic_nbr < MAX_VIRT_CPUS && dom0->vcpu[lsapic_nbr] != NULL)
   1.230 +		enable = 1;
   1.231 +	else
   1.232 +		enable = 0;
   1.233 +	if (lsapic->flags.enabled && enable) {
   1.234 +		printk("enable lsapic entry: 0x%lx\n", (u64)lsapic);
   1.235 +		lsapic->id = lsapic_nbr;
   1.236 +		lsapic->eid = 0;
   1.237 +		lsapic_nbr++;
   1.238 +	} else if (lsapic->flags.enabled) {
   1.239 +		printk("DISABLE lsapic entry: 0x%lx\n", (u64)lsapic);
   1.240 +		lsapic->flags.enabled = 0;
   1.241 +		lsapic->id = 0;
   1.242 +		lsapic->eid = 0;
   1.243 +	}
   1.244 +	return 0;
   1.245 +}
   1.246 +
   1.247 +static u8
   1.248 +generate_acpi_checksum(void *tbl, unsigned long len)
   1.249 +{
   1.250 +	u8 *ptr, sum = 0;
   1.251 +
   1.252 +	for (ptr = tbl; len > 0 ; len--, ptr++)
   1.253 +		sum += *ptr;
   1.254 +
   1.255 +	return 0 - sum;
   1.256 +}
   1.257 +
   1.258 +static int __init
   1.259 +acpi_patch_plat_int_src (
   1.260 +	acpi_table_entry_header *header, const unsigned long end)
   1.261 +{
   1.262 +	struct acpi_table_plat_int_src *plintsrc;
   1.263 +
   1.264 +	plintsrc = (struct acpi_table_plat_int_src *)header;
   1.265 +	if (!plintsrc)
   1.266 +		return -EINVAL;
   1.267 +
   1.268 +	if (plintsrc->type == ACPI_INTERRUPT_CPEI) {
   1.269 +		printk("ACPI_INTERRUPT_CPEI disabled for Domain0\n");
   1.270 +		plintsrc->type = -1;
   1.271 +	}
   1.272 +	return 0;
   1.273 +}
   1.274 +
   1.275 +static int
   1.276 +acpi_update_madt_checksum (unsigned long phys_addr, unsigned long size)
   1.277 +{
   1.278 +	struct acpi_table_madt* acpi_madt;
   1.279 +
   1.280 +	if (!phys_addr || !size)
   1.281 +		return -EINVAL;
   1.282 +
   1.283 +	acpi_madt = (struct acpi_table_madt *) __va(phys_addr);
   1.284 +	acpi_madt->header.checksum = 0;
   1.285 +	acpi_madt->header.checksum = generate_acpi_checksum(acpi_madt, size);
   1.286 +
   1.287 +	return 0;
   1.288 +}
   1.289 +
   1.290 +/* base is physical address of acpi table */
   1.291 +static void touch_acpi_table(void)
   1.292 +{
   1.293 +	lsapic_nbr = 0;
   1.294 +	if (acpi_table_parse_madt(ACPI_MADT_LSAPIC, acpi_update_lsapic, 0) < 0)
   1.295 +		printk("Error parsing MADT - no LAPIC entries\n");
   1.296 +	if (acpi_table_parse_madt(ACPI_MADT_PLAT_INT_SRC,
   1.297 +	                          acpi_patch_plat_int_src, 0) < 0)
   1.298 +		printk("Error parsing MADT - no PLAT_INT_SRC entries\n");
   1.299 +
   1.300 +	acpi_table_parse(ACPI_APIC, acpi_update_madt_checksum);
   1.301 +
   1.302 +	return;
   1.303 +}
   1.304 +
   1.305 +struct fake_acpi_tables {
   1.306 +	struct acpi20_table_rsdp rsdp;
   1.307 +	struct xsdt_descriptor_rev2 xsdt;
   1.308 +	u64 madt_ptr;
   1.309 +	struct fadt_descriptor_rev2 fadt;
   1.310 +	struct facs_descriptor_rev2 facs;
   1.311 +	struct acpi_table_header dsdt;
   1.312 +	u8 aml[8 + 11 * MAX_VIRT_CPUS];
   1.313 +	struct acpi_table_madt madt;
   1.314 +	struct acpi_table_lsapic lsapic[MAX_VIRT_CPUS];
   1.315 +	u8 pm1a_evt_blk[4];
   1.316 +	u8 pm1a_cnt_blk[1];
   1.317 +	u8 pm_tmr_blk[4];
   1.318 +};
   1.319 +#define ACPI_TABLE_MPA(field) \
   1.320 +  FW_ACPI_BASE_PADDR + offsetof(struct fake_acpi_tables, field);
   1.321 +
   1.322 +/* Create enough of an ACPI structure to make the guest OS ACPI happy. */
   1.323 +static void
   1.324 +dom_fw_fake_acpi(struct domain *d, struct fake_acpi_tables *tables)
   1.325 +{
   1.326 +	struct acpi20_table_rsdp *rsdp = &tables->rsdp;
   1.327 +	struct xsdt_descriptor_rev2 *xsdt = &tables->xsdt;
   1.328 +	struct fadt_descriptor_rev2 *fadt = &tables->fadt;
   1.329 +	struct facs_descriptor_rev2 *facs = &tables->facs;
   1.330 +	struct acpi_table_header *dsdt = &tables->dsdt;
   1.331 +	struct acpi_table_madt *madt = &tables->madt;
   1.332 +	struct acpi_table_lsapic *lsapic = tables->lsapic;
   1.333 +	int i;
   1.334 +	int aml_len;
   1.335 +	int nbr_cpus;
   1.336 +
   1.337 +	memset(tables, 0, sizeof(struct fake_acpi_tables));
   1.338 +
   1.339 +	/* setup XSDT (64bit version of RSDT) */
   1.340 +	memcpy(xsdt->signature, XSDT_SIG, sizeof(xsdt->signature));
   1.341 +	/* XSDT points to both the FADT and the MADT, so add one entry */
   1.342 +	xsdt->length = sizeof(struct xsdt_descriptor_rev2) + sizeof(u64);
   1.343 +	xsdt->revision = 1;
   1.344 +	memcpy(xsdt->oem_id, "XEN", 3);
   1.345 +	memcpy(xsdt->oem_table_id, "Xen/ia64", 8);
   1.346 +	memcpy(xsdt->asl_compiler_id, "XEN", 3);
   1.347 +	xsdt->asl_compiler_revision = (xen_major_version() << 16) |
   1.348 +		xen_minor_version();
   1.349 +
   1.350 +	xsdt->table_offset_entry[0] = ACPI_TABLE_MPA(fadt);
   1.351 +	tables->madt_ptr = ACPI_TABLE_MPA(madt);
   1.352 +
   1.353 +	xsdt->checksum = generate_acpi_checksum(xsdt, xsdt->length);
   1.354 +
   1.355 +	/* setup FADT */
   1.356 +	memcpy(fadt->signature, FADT_SIG, sizeof(fadt->signature));
   1.357 +	fadt->length = sizeof(struct fadt_descriptor_rev2);
   1.358 +	fadt->revision = FADT2_REVISION_ID;
   1.359 +	memcpy(fadt->oem_id, "XEN", 3);
   1.360 +	memcpy(fadt->oem_table_id, "Xen/ia64", 8);
   1.361 +	memcpy(fadt->asl_compiler_id, "XEN", 3);
   1.362 +	fadt->asl_compiler_revision = (xen_major_version() << 16) |
   1.363 +		xen_minor_version();
   1.364 +
   1.365 +	memcpy(facs->signature, FACS_SIG, sizeof(facs->signature));
   1.366 +	facs->version = 1;
   1.367 +	facs->length = sizeof(struct facs_descriptor_rev2);
   1.368 +
   1.369 +	fadt->xfirmware_ctrl = ACPI_TABLE_MPA(facs);
   1.370 +	fadt->Xdsdt = ACPI_TABLE_MPA(dsdt);
   1.371 +
   1.372 +	/*
   1.373 +	 * All of the below FADT entries are filled it to prevent warnings
   1.374 +	 * from sanity checks in the ACPI CA.  Emulate required ACPI hardware
   1.375 +	 * registers in system memory.
   1.376 +	 */
   1.377 +	fadt->pm1_evt_len = 4;
   1.378 +	fadt->xpm1a_evt_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
   1.379 +	fadt->xpm1a_evt_blk.register_bit_width = 8;
   1.380 +	fadt->xpm1a_evt_blk.address = ACPI_TABLE_MPA(pm1a_evt_blk);
   1.381 +	fadt->pm1_cnt_len = 1;
   1.382 +	fadt->xpm1a_cnt_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
   1.383 +	fadt->xpm1a_cnt_blk.register_bit_width = 8;
   1.384 +	fadt->xpm1a_cnt_blk.address = ACPI_TABLE_MPA(pm1a_cnt_blk);
   1.385 +	fadt->pm_tm_len = 4;
   1.386 +	fadt->xpm_tmr_blk.address_space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
   1.387 +	fadt->xpm_tmr_blk.register_bit_width = 8;
   1.388 +	fadt->xpm_tmr_blk.address = ACPI_TABLE_MPA(pm_tmr_blk);
   1.389 +
   1.390 +	fadt->checksum = generate_acpi_checksum(fadt, fadt->length);
   1.391 +
   1.392 +	/* setup RSDP */
   1.393 +	memcpy(rsdp->signature, RSDP_SIG, strlen(RSDP_SIG));
   1.394 +	memcpy(rsdp->oem_id, "XEN", 3);
   1.395 +	rsdp->revision = 2; /* ACPI 2.0 includes XSDT */
   1.396 +	rsdp->length = sizeof(struct acpi20_table_rsdp);
   1.397 +	rsdp->xsdt_address = ACPI_TABLE_MPA(xsdt);
   1.398 +
   1.399 +	rsdp->checksum = generate_acpi_checksum(rsdp,
   1.400 +	                                        ACPI_RSDP_CHECKSUM_LENGTH);
   1.401 +	rsdp->ext_checksum = generate_acpi_checksum(rsdp, rsdp->length);
   1.402 +
   1.403 +	/* setup DSDT with trivial namespace. */ 
   1.404 +	memcpy(dsdt->signature, DSDT_SIG, strlen(DSDT_SIG));
   1.405 +	dsdt->revision = 1;
   1.406 +	memcpy(dsdt->oem_id, "XEN", 3);
   1.407 +	memcpy(dsdt->oem_table_id, "Xen/ia64", 8);
   1.408 +	memcpy(dsdt->asl_compiler_id, "XEN", 3);
   1.409 +	dsdt->asl_compiler_revision = (xen_major_version() << 16) |
   1.410 +		xen_minor_version();
   1.411 +
   1.412 +	/* Trivial namespace, avoids ACPI CA complaints */
   1.413 +	tables->aml[0] = 0x10; /* Scope */
   1.414 +	tables->aml[1] = 0x40; /* length/offset to next object (patched) */
   1.415 +	tables->aml[2] = 0x00;
   1.416 +	memcpy(&tables->aml[3], "_SB_", 4);
   1.417 +
   1.418 +	/* The processor object isn't absolutely necessary, revist for SMP */
   1.419 +	aml_len = 7;
   1.420 +	for (i = 0; i < 3; i++) {
   1.421 +		unsigned char *p = tables->aml + aml_len;
   1.422 +		p[0] = 0x5b; /* processor object */
   1.423 +		p[1] = 0x83;
   1.424 +		p[2] = 0x0b; /* next */
   1.425 +		p[3] = 'C';
   1.426 +		p[4] = 'P';
   1.427 +		snprintf ((char *)p + 5, 3, "%02x", i);
   1.428 +		if (i < 16)
   1.429 +			p[5] = 'U';
   1.430 +		p[7] = i;	/* acpi_id */
   1.431 +		p[8] = 0;	/* pblk_addr */
   1.432 +		p[9] = 0;
   1.433 +		p[10] = 0;
   1.434 +		p[11] = 0;
   1.435 +		p[12] = 0;	/* pblk_len */
   1.436 +		aml_len += 13;
   1.437 +	}
   1.438 +	tables->aml[1] = 0x40 + ((aml_len - 1) & 0x0f);
   1.439 +	tables->aml[2] = (aml_len - 1) >> 4;
   1.440 +	dsdt->length = sizeof(struct acpi_table_header) + aml_len;
   1.441 +	dsdt->checksum = generate_acpi_checksum(dsdt, dsdt->length);
   1.442 +
   1.443 +	/* setup MADT */
   1.444 +	memcpy(madt->header.signature, APIC_SIG, sizeof(madt->header.signature));
   1.445 +	madt->header.revision = 2;
   1.446 +	memcpy(madt->header.oem_id, "XEN", 3);
   1.447 +	memcpy(madt->header.oem_table_id, "Xen/ia64", 8);
   1.448 +	memcpy(madt->header.asl_compiler_id, "XEN", 3);
   1.449 +	madt->header.asl_compiler_revision = (xen_major_version() << 16) |
   1.450 +		xen_minor_version();
   1.451 +
   1.452 +	/* An LSAPIC entry describes a CPU.  */
   1.453 +	nbr_cpus = 0;
   1.454 +	for (i = 0; i < MAX_VIRT_CPUS; i++) {
   1.455 +		lsapic[i].header.type = ACPI_MADT_LSAPIC;
   1.456 +		lsapic[i].header.length = sizeof(struct acpi_table_lsapic);
   1.457 +		lsapic[i].acpi_id = i;
   1.458 +		lsapic[i].id = i;
   1.459 +		lsapic[i].eid = 0;
   1.460 +		//XXX replace d->vcpu[i] != NULL with XEN_DOMCTL_getvcpuinfo
   1.461 +		if (d->vcpu[i] != NULL) {
   1.462 +			lsapic[i].flags.enabled = 1;
   1.463 +			nbr_cpus++;
   1.464 +		}
   1.465 +	}
   1.466 +	madt->header.length = sizeof(struct acpi_table_madt) +
   1.467 +	                      nbr_cpus * sizeof(struct acpi_table_lsapic);
   1.468 +	madt->header.checksum = generate_acpi_checksum(madt,
   1.469 +	                                               madt->header.length);
   1.470 +	return;
   1.471 +}
   1.472 +
   1.473 +static int
   1.474 +efi_mdt_cmp(const void *a, const void *b)
   1.475 +{
   1.476 +	const efi_memory_desc_t *x = a, *y = b;
   1.477 +
   1.478 +	if (x->phys_addr > y->phys_addr)
   1.479 +		return 1;
   1.480 +	if (x->phys_addr < y->phys_addr)
   1.481 +		return -1;
   1.482 +
   1.483 +	// num_pages == 0 is allowed.
   1.484 +	if (x->num_pages > y->num_pages)
   1.485 +		return 1;
   1.486 +	if (x->num_pages < y->num_pages)
   1.487 +		return -1;
   1.488 +
   1.489 +	return 0;
   1.490 +}
   1.491 +
   1.492 +#define NFUNCPTRS 16
   1.493 +#define NUM_EFI_SYS_TABLES 6
   1.494 +#define NUM_MEM_DESCS 64 //large enough
   1.495 +
   1.496 +struct fw_tables {
   1.497 +	efi_system_table_t efi_systab;
   1.498 +	efi_runtime_services_t efi_runtime;
   1.499 +	efi_config_table_t efi_tables[NUM_EFI_SYS_TABLES];
   1.500 +
   1.501 +	struct ia64_sal_systab sal_systab;
   1.502 +	struct ia64_sal_desc_entry_point sal_ed;
   1.503 +	struct ia64_sal_desc_ap_wakeup sal_wakeup;
   1.504 +	/* End of SAL descriptors.  Do not forget to update checkum bound.  */
   1.505 +
   1.506 +	fpswa_interface_t fpswa_inf;
   1.507 +	efi_memory_desc_t efi_memmap[NUM_MEM_DESCS];
   1.508 +	unsigned long func_ptrs[2*NFUNCPTRS];
   1.509 + 	struct xen_sal_data sal_data;
   1.510 +	unsigned char fw_vendor[sizeof(FW_VENDOR)];
   1.511 +};
   1.512 +#define FW_FIELD_MPA(field) \
   1.513 +   FW_TABLES_BASE_PADDR + offsetof(struct fw_tables, field)
   1.514 +
   1.515 +static void
   1.516 +efi_systable_init_dom0(struct fw_tables *tables)
   1.517 +{
   1.518 +	int i = 1;
   1.519 +	/* Write messages to the console.  */
   1.520 +	touch_acpi_table();
   1.521 +
   1.522 +	printk("Domain0 EFI passthrough:");
   1.523 +	if (efi.mps) {
   1.524 +		tables->efi_tables[i].guid = MPS_TABLE_GUID;
   1.525 +		tables->efi_tables[i].table = __pa(efi.mps);
   1.526 +		printk(" MPS=0x%lx",tables->efi_tables[i].table);
   1.527 +		i++;
   1.528 +	}
   1.529 +
   1.530 +	if (efi.acpi20) {
   1.531 +		tables->efi_tables[i].guid = ACPI_20_TABLE_GUID;
   1.532 +		tables->efi_tables[i].table = __pa(efi.acpi20);
   1.533 +		printk(" ACPI 2.0=0x%lx",tables->efi_tables[i].table);
   1.534 +		i++;
   1.535 +	}
   1.536 +	if (efi.acpi) {
   1.537 +		tables->efi_tables[i].guid = ACPI_TABLE_GUID;
   1.538 +		tables->efi_tables[i].table = __pa(efi.acpi);
   1.539 +		printk(" ACPI=0x%lx",tables->efi_tables[i].table);
   1.540 +		i++;
   1.541 +	}
   1.542 +	if (efi.smbios) {
   1.543 +		tables->efi_tables[i].guid = SMBIOS_TABLE_GUID;
   1.544 +		tables->efi_tables[i].table = __pa(efi.smbios);
   1.545 +		printk(" SMBIOS=0x%lx",tables->efi_tables[i].table);
   1.546 +		i++;
   1.547 +	}
   1.548 +	if (efi.hcdp) {
   1.549 +		tables->efi_tables[i].guid = HCDP_TABLE_GUID;
   1.550 +		tables->efi_tables[i].table = __pa(efi.hcdp);
   1.551 +		printk(" HCDP=0x%lx",tables->efi_tables[i].table);
   1.552 +		i++;
   1.553 +	}
   1.554 +	printk("\n");
   1.555 +	BUG_ON(i > NUM_EFI_SYS_TABLES);
   1.556 +}
   1.557 +
   1.558 +static void
   1.559 +setup_dom0_memmap_info(struct domain *d, struct fw_tables *tables,
   1.560 +		       int *num_mds)
   1.561 +{
   1.562 +	int i;
   1.563 +	efi_memory_desc_t *md;
   1.564 +	efi_memory_desc_t *last_mem_md = NULL;
   1.565 +	xen_ia64_memmap_info_t* memmap_info;
   1.566 +	unsigned long paddr_start;
   1.567 +	unsigned long paddr_end;
   1.568 +
   1.569 +	for (i = *num_mds - 1; i >= 0; i--) {
   1.570 +		md = &tables->efi_memmap[i];
   1.571 +		if (md->attribute == EFI_MEMORY_WB &&
   1.572 +		    md->type == EFI_CONVENTIONAL_MEMORY &&
   1.573 +		    md->num_pages >
   1.574 +		    2 * (1UL << (PAGE_SHIFT - EFI_PAGE_SHIFT))) {
   1.575 +			last_mem_md = md;
   1.576 +			break;
   1.577 +		}
   1.578 +	}
   1.579 +
   1.580 +	if (last_mem_md == NULL) {
   1.581 +		printk("%s: warning: "
   1.582 +		       "no dom0 contiguous memory to hold memory map\n",
   1.583 +		       __func__);
   1.584 +		return;
   1.585 +	}
   1.586 +	paddr_end = last_mem_md->phys_addr +
   1.587 +		(last_mem_md->num_pages << EFI_PAGE_SHIFT);
   1.588 +	paddr_start = (paddr_end - PAGE_SIZE) & PAGE_MASK;
   1.589 +	last_mem_md->num_pages -=
   1.590 +		(paddr_end - paddr_start) / (1UL << EFI_PAGE_SHIFT);
   1.591 +
   1.592 +	md = &tables->efi_memmap[*num_mds];
   1.593 +	(*num_mds)++;
   1.594 +	md->type = EFI_RUNTIME_SERVICES_DATA;
   1.595 +	md->phys_addr = paddr_start;
   1.596 +	md->virt_addr = 0;
   1.597 +	md->num_pages = 1UL << (PAGE_SHIFT - EFI_PAGE_SHIFT);
   1.598 +	md->attribute = EFI_MEMORY_WB;
   1.599 +
   1.600 +	memmap_info = domain_mpa_to_imva(d, md->phys_addr);
   1.601 +	BUG_ON(*num_mds > NUM_MEM_DESCS);
   1.602 +
   1.603 +	memmap_info->efi_memdesc_size = sizeof(md[0]);
   1.604 +	memmap_info->efi_memdesc_version = EFI_MEMORY_DESCRIPTOR_VERSION;
   1.605 +	memmap_info->efi_memmap_size = *num_mds * sizeof(md[0]);
   1.606 +	memcpy(&memmap_info->memdesc, &tables->efi_memmap[0],
   1.607 +	       memmap_info->efi_memmap_size);
   1.608 +	d->shared_info->arch.memmap_info_num_pages = 1;
   1.609 +	d->shared_info->arch.memmap_info_pfn = md->phys_addr >> PAGE_SHIFT;
   1.610 +
   1.611 +	sort(tables->efi_memmap, *num_mds, sizeof(efi_memory_desc_t),
   1.612 +	     efi_mdt_cmp, NULL);
   1.613 +}
   1.614 +
   1.615 +/* Complete the dom0 memmap.  */
   1.616 +static int
   1.617 +complete_dom0_memmap(struct domain *d,
   1.618 +                     struct fw_tables *tables,
   1.619 +                     unsigned long maxmem,
   1.620 +                     int num_mds)
   1.621 +{
   1.622 +	efi_memory_desc_t *md;
   1.623 +	u64 addr;
   1.624 +	void *efi_map_start, *efi_map_end, *p;
   1.625 +	u64 efi_desc_size;
   1.626 +	int i;
   1.627 +	unsigned long dom_mem = maxmem - (d->tot_pages << PAGE_SHIFT);
   1.628 +
   1.629 +	/* Walk through all MDT entries.
   1.630 +	   Copy all interesting entries.  */
   1.631 +	efi_map_start = __va(ia64_boot_param->efi_memmap);
   1.632 +	efi_map_end = efi_map_start + ia64_boot_param->efi_memmap_size;
   1.633 +	efi_desc_size = ia64_boot_param->efi_memdesc_size;
   1.634 +
   1.635 +	for (p = efi_map_start; p < efi_map_end; p += efi_desc_size) {
   1.636 +		const efi_memory_desc_t *md = p;
   1.637 +		efi_memory_desc_t *dom_md = &tables->efi_memmap[num_mds];
   1.638 +		u64 start = md->phys_addr;
   1.639 +		u64 size = md->num_pages << EFI_PAGE_SHIFT;
   1.640 +		u64 end = start + size;
   1.641 +		u64 mpaddr;
   1.642 +		unsigned long flags;
   1.643 +
   1.644 +		switch (md->type) {
   1.645 +		case EFI_RUNTIME_SERVICES_CODE:
   1.646 +		case EFI_RUNTIME_SERVICES_DATA:
   1.647 +		case EFI_ACPI_RECLAIM_MEMORY:
   1.648 +		case EFI_ACPI_MEMORY_NVS:
   1.649 +		case EFI_RESERVED_TYPE:
   1.650 +			/*
   1.651 +			 * Map into dom0 - We must respect protection
   1.652 +			 * and cache attributes.  Not all of these pages
   1.653 +			 * are writable!!!
   1.654 +			 */
   1.655 +			flags = ASSIGN_writable;	/* dummy - zero */
   1.656 +			if (md->attribute & EFI_MEMORY_WP)
   1.657 +				flags |= ASSIGN_readonly;
   1.658 +			if ((md->attribute & EFI_MEMORY_UC) &&
   1.659 +			    !(md->attribute & EFI_MEMORY_WB))
   1.660 +				flags |= ASSIGN_nocache;
   1.661 +
   1.662 +			assign_domain_mach_page(d, start, size, flags);
   1.663 +
   1.664 +			/* Fall-through.  */
   1.665 +		case EFI_MEMORY_MAPPED_IO:
   1.666 +			/* Will be mapped with ioremap.  */
   1.667 +			/* Copy descriptor.  */
   1.668 +			*dom_md = *md;
   1.669 +			dom_md->virt_addr = 0;
   1.670 +			num_mds++;
   1.671 +			break;
   1.672 +
   1.673 +		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
   1.674 +			flags = ASSIGN_writable;	/* dummy - zero */
   1.675 +			if (md->attribute & EFI_MEMORY_UC)
   1.676 +				flags |= ASSIGN_nocache;
   1.677 +
   1.678 +			if (start > 0x1ffffffff0000000UL) {
   1.679 +				mpaddr = 0x4000000000000UL - size;
   1.680 +				printk(XENLOG_INFO "Remapping IO ports from "
   1.681 +				       "%lx to %lx\n", start, mpaddr);
   1.682 +			} else
   1.683 +				mpaddr = start;
   1.684 +
   1.685 +			/* Map into dom0.  */
   1.686 +			assign_domain_mmio_page(d, mpaddr, start, size, flags);
   1.687 +			/* Copy descriptor.  */
   1.688 +			*dom_md = *md;
   1.689 +			dom_md->phys_addr = mpaddr;
   1.690 +			dom_md->virt_addr = 0;
   1.691 +			num_mds++;
   1.692 +			break;
   1.693 +
   1.694 +		case EFI_CONVENTIONAL_MEMORY:
   1.695 +		case EFI_LOADER_CODE:
   1.696 +		case EFI_LOADER_DATA:
   1.697 +		case EFI_BOOT_SERVICES_CODE:
   1.698 +		case EFI_BOOT_SERVICES_DATA:
   1.699 +			if (!(md->attribute & EFI_MEMORY_WB))
   1.700 +				break;
   1.701 +
   1.702 +			start = max(FW_END_PADDR, start);
   1.703 +			end = min(start + dom_mem, end);
   1.704 +			if (end <= start)
   1.705 +				break;
   1.706 +
   1.707 +			dom_md->type = EFI_CONVENTIONAL_MEMORY;
   1.708 +			dom_md->phys_addr = start;
   1.709 +			dom_md->virt_addr = 0;
   1.710 +			dom_md->num_pages = (end - start) >> EFI_PAGE_SHIFT;
   1.711 +			dom_md->attribute = EFI_MEMORY_WB;
   1.712 +			num_mds++;
   1.713 +
   1.714 +			dom_mem -= dom_md->num_pages << EFI_PAGE_SHIFT;
   1.715 +			break;
   1.716 +
   1.717 +		case EFI_UNUSABLE_MEMORY:
   1.718 +		case EFI_PAL_CODE:
   1.719 +			/*
   1.720 +			 * We don't really need these, but holes in the
   1.721 +			 * memory map may cause Linux to assume there are
   1.722 +			 * uncacheable ranges within a granule.
   1.723 +			 */
   1.724 +			dom_md->type = EFI_UNUSABLE_MEMORY;
   1.725 +			dom_md->phys_addr = start;
   1.726 +			dom_md->virt_addr = 0;
   1.727 +			dom_md->num_pages = (end - start) >> EFI_PAGE_SHIFT;
   1.728 +			dom_md->attribute = EFI_MEMORY_WB;
   1.729 +			num_mds++;
   1.730 +			break;
   1.731 +
   1.732 +		default:
   1.733 +			/* Print a warning but continue.  */
   1.734 +			printk("complete_dom0_memmap: warning: "
   1.735 +			       "unhandled MDT entry type %u\n", md->type);
   1.736 +		}
   1.737 +	}
   1.738 +	BUG_ON(num_mds > NUM_MEM_DESCS);
   1.739 +	
   1.740 +	sort(tables->efi_memmap, num_mds, sizeof(efi_memory_desc_t),
   1.741 +	     efi_mdt_cmp, NULL);
   1.742 +
   1.743 +	/* setup_guest() @ libxc/xc_linux_build() arranges memory for domU.
   1.744 +	 * however no one arranges memory for dom0,
   1.745 +	 * instead we allocate pages manually.
   1.746 +	 */
   1.747 +	for (i = 0; i < num_mds; i++) {
   1.748 +		md = &tables->efi_memmap[i];
   1.749 +
   1.750 +		if (md->type == EFI_LOADER_DATA ||
   1.751 +		    md->type == EFI_PAL_CODE ||
   1.752 +		    md->type == EFI_CONVENTIONAL_MEMORY) {
   1.753 +			unsigned long start = md->phys_addr & PAGE_MASK;
   1.754 +			unsigned long end = md->phys_addr +
   1.755 +				(md->num_pages << EFI_PAGE_SHIFT);
   1.756 +
   1.757 +			if (end == start) {
   1.758 +				/* md->num_pages = 0 is allowed. */
   1.759 +				continue;
   1.760 +			}
   1.761 +			
   1.762 +			for (addr = start; addr < end; addr += PAGE_SIZE)
   1.763 +				assign_new_domain0_page(d, addr);
   1.764 +		}
   1.765 +	}
   1.766 +	// Map low-memory holes & unmapped MMIO for legacy drivers
   1.767 +	for (addr = 0; addr < ONE_MB; addr += PAGE_SIZE) {
   1.768 +		if (domain_page_mapped(d, addr))
   1.769 +			continue;
   1.770 +		
   1.771 +		if (efi_mmio(addr, PAGE_SIZE)) {
   1.772 +			unsigned long flags;
   1.773 +			flags = ASSIGN_writable | ASSIGN_nocache;
   1.774 +			assign_domain_mmio_page(d, addr, addr,
   1.775 +						PAGE_SIZE, flags);
   1.776 +		}
   1.777 +	}
   1.778 +	setup_dom0_memmap_info(d, tables, &num_mds);
   1.779 +	return num_mds;
   1.780 +}
   1.781 +	
   1.782 +static void
   1.783 +efi_systable_init_domu(struct fw_tables *tables)
   1.784 +{
   1.785 +	int i = 1;
   1.786 +	printk(XENLOG_GUEST XENLOG_INFO "DomainU EFI build up:");
   1.787 +
   1.788 +	tables->efi_tables[i].guid = ACPI_20_TABLE_GUID;
   1.789 +	tables->efi_tables[i].table = FW_ACPI_BASE_PADDR;
   1.790 +	printk(" ACPI 2.0=0x%lx",tables->efi_tables[i].table);
   1.791 +	i++;
   1.792 +	printk("\n");
   1.793 +	BUG_ON(i > NUM_EFI_SYS_TABLES);
   1.794 +}
   1.795 +
   1.796 +static int
   1.797 +complete_domu_memmap(struct domain *d,
   1.798 +                     struct fw_tables *tables,
   1.799 +                     unsigned long maxmem,
   1.800 +                     int num_mds,
   1.801 +		     unsigned long memmap_info_pfn,
   1.802 +		     unsigned long reserved_size)
   1.803 +{
   1.804 +	efi_memory_desc_t *md;
   1.805 +	int i = num_mds; /* for MAKE_MD */
   1.806 +	int create_memmap = 0;
   1.807 +	xen_ia64_memmap_info_t* memmap_info;
   1.808 +	unsigned long paddr_start;
   1.809 +	unsigned long paddr_end;
   1.810 +	void *p;
   1.811 +	void *memmap_start;
   1.812 +	void *memmap_end;
   1.813 +
   1.814 +	if (memmap_info_pfn == 0 || reserved_size == 0) {
   1.815 +		/* old domain builder which doesn't setup
   1.816 +		 * memory map. create it for compatibility */
   1.817 +		memmap_info_pfn = (maxmem >> PAGE_SHIFT) - 1;
   1.818 +		/* 4 = memmap info page, start info page, xenstore page and
   1.819 +		   console page */
   1.820 +		reserved_size = 4 << PAGE_SHIFT;
   1.821 +		create_memmap = 1;
   1.822 +	}
   1.823 +	paddr_start = memmap_info_pfn << PAGE_SHIFT;
   1.824 +	paddr_end = paddr_start + reserved_size;
   1.825 +	memmap_info = domain_mpa_to_imva(d, paddr_start);//XXX replace this with xc_map_foreign_map_range()
   1.826 +	if (memmap_info->efi_memmap_size == 0) {
   1.827 +		create_memmap = 1;
   1.828 +	} else if (memmap_info->efi_memdesc_size != sizeof(md[0]) ||
   1.829 +		   memmap_info->efi_memdesc_version !=
   1.830 +		   EFI_MEMORY_DESCRIPTOR_VERSION) {
   1.831 +		printk(XENLOG_WARNING
   1.832 +		       "%s: Warning: unknown memory map "
   1.833 +		       "memmap size %"PRIu64" "
   1.834 +		       "memdesc size %"PRIu64" "
   1.835 +		       "version %"PRIu32"\n",
   1.836 +		       __func__,
   1.837 +		       memmap_info->efi_memmap_size,
   1.838 +		       memmap_info->efi_memdesc_size,
   1.839 +		       memmap_info->efi_memdesc_version);
   1.840 +		create_memmap = 1;
   1.841 +	} else if (reserved_size < memmap_info->efi_memmap_size) {
   1.842 +		printk(XENLOG_WARNING
   1.843 +		       "%s: Warning: too short reserved size %"PRIu64"\n",
   1.844 +		       __func__, reserved_size);
   1.845 +		return -EINVAL;
   1.846 +	} else if (memmap_info->efi_memmap_size >
   1.847 +		   PAGE_SIZE - sizeof(*memmap_info)) {
   1.848 +		/*
   1.849 +		 * curently memmap spanning more than single page isn't
   1.850 +		 * supported.
   1.851 +		 */
   1.852 +		printk(XENLOG_WARNING
   1.853 +		       "%s: Warning: too large reserved_size %"PRIu64"\n",
   1.854 +		       __func__, memmap_info->efi_memmap_size);
   1.855 +		return -ENOSYS;
   1.856 +	}
   1.857 +	
   1.858 +	if (create_memmap) {
   1.859 +		/*
   1.860 +		 * old domain builder which doesn't setup
   1.861 +		 * memory map. create it for compatibility
   1.862 +		 */
   1.863 +		memmap_info->efi_memdesc_size = sizeof(md[0]);
   1.864 +		memmap_info->efi_memdesc_version =
   1.865 +			EFI_MEMORY_DESCRIPTOR_VERSION;
   1.866 +		memmap_info->efi_memmap_size = 1 * sizeof(md[0]);
   1.867 +		md = (efi_memory_desc_t*)&memmap_info->memdesc;
   1.868 +		md[num_mds].type = EFI_CONVENTIONAL_MEMORY;
   1.869 +		md[num_mds].pad = 0;
   1.870 +		md[num_mds].phys_addr = 0;
   1.871 +		md[num_mds].virt_addr = 0;
   1.872 +		md[num_mds].num_pages = maxmem >> EFI_PAGE_SHIFT;
   1.873 +		md[num_mds].attribute = EFI_MEMORY_WB;
   1.874 +	}
   1.875 +
   1.876 +	memmap_start = &memmap_info->memdesc;
   1.877 +	memmap_end = memmap_start + memmap_info->efi_memmap_size;
   1.878 +	/* XXX Currently the table must be in a single page. */
   1.879 +	if ((unsigned long)memmap_end > (unsigned long)memmap_info + PAGE_SIZE)
   1.880 +		return -EINVAL;
   1.881 +
   1.882 +	/* sort it bofore use
   1.883 +	 * XXX: this is created by user space domain builder so that
   1.884 +	 * we should check its integrity */
   1.885 +	sort(&memmap_info->memdesc,
   1.886 +	     memmap_info->efi_memmap_size / memmap_info->efi_memdesc_size,
   1.887 +	     memmap_info->efi_memdesc_size,
   1.888 +	     efi_mdt_cmp, NULL);
   1.889 +
   1.890 +	for (p = memmap_start; p < memmap_end; p += memmap_info->efi_memdesc_size) {
   1.891 +		unsigned long start;
   1.892 +		unsigned long end;
   1.893 +		md = p;
   1.894 +		start = md->phys_addr;
   1.895 +		end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
   1.896 +
   1.897 +		if (start < FW_END_PADDR)
   1.898 +			start = FW_END_PADDR;
   1.899 +		if (end <= start)
   1.900 +			continue;
   1.901 +
   1.902 +		/* exclude [paddr_start, paddr_end) */
   1.903 +		if (paddr_end <= start || end <= paddr_start) {
   1.904 +			MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
   1.905 +				start, end);
   1.906 +		} else if (paddr_start <= start && paddr_end < end) {
   1.907 +			MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
   1.908 +				paddr_end, end);
   1.909 +		} else if (start < paddr_start && end <= paddr_end) {
   1.910 +			MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
   1.911 +				start, paddr_start);
   1.912 +		} else {
   1.913 +			MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
   1.914 +				start, paddr_start);
   1.915 +			MAKE_MD(EFI_CONVENTIONAL_MEMORY, EFI_MEMORY_WB,
   1.916 +				paddr_end, end);
   1.917 +		}
   1.918 +	}
   1.919 +
   1.920 +	/* memmap info page. */
   1.921 +	MAKE_MD(EFI_RUNTIME_SERVICES_DATA, EFI_MEMORY_WB,
   1.922 +		paddr_start, paddr_end);
   1.923 +
   1.924 +	/* Create an entry for IO ports.  */
   1.925 +	MAKE_MD(EFI_MEMORY_MAPPED_IO_PORT_SPACE, EFI_MEMORY_UC,
   1.926 +		IO_PORTS_PADDR, IO_PORTS_PADDR + IO_PORTS_SIZE);
   1.927 +
   1.928 +	num_mds = i;
   1.929 +	sort(tables->efi_memmap, num_mds, sizeof(efi_memory_desc_t),
   1.930 +	     efi_mdt_cmp, NULL);
   1.931 +	return num_mds;
   1.932 +}
   1.933 +
   1.934 +static int
   1.935 +dom_fw_init(struct domain *d,
   1.936 +	    u64 brkimm,
   1.937 +            struct ia64_boot_param *bp,
   1.938 +            struct fw_tables *tables,
   1.939 +            unsigned long hypercalls_imva,
   1.940 +            unsigned long maxmem)
   1.941 +{
   1.942 +	efi_memory_desc_t *md;
   1.943 +	unsigned long pfn;
   1.944 +	unsigned char checksum;
   1.945 +	char *cp;
   1.946 +	int num_mds, i;
   1.947 +
   1.948 +	memset(tables, 0, sizeof(struct fw_tables));
   1.949 +
   1.950 +	/* EFI systab.  */
   1.951 +	tables->efi_systab.hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
   1.952 +	tables->efi_systab.hdr.revision  = EFI_SYSTEM_TABLE_REVISION;
   1.953 +	tables->efi_systab.hdr.headersize = sizeof(tables->efi_systab.hdr);
   1.954 +
   1.955 +	memcpy(tables->fw_vendor,FW_VENDOR,sizeof(FW_VENDOR));
   1.956 +	tables->efi_systab.fw_vendor = FW_FIELD_MPA(fw_vendor);
   1.957 +	tables->efi_systab.fw_revision = 1;
   1.958 +	tables->efi_systab.runtime = (void *)FW_FIELD_MPA(efi_runtime);
   1.959 +	tables->efi_systab.nr_tables = NUM_EFI_SYS_TABLES;
   1.960 +	tables->efi_systab.tables = FW_FIELD_MPA(efi_tables);
   1.961 +
   1.962 +	/* EFI runtime.  */
   1.963 +	tables->efi_runtime.hdr.signature = EFI_RUNTIME_SERVICES_SIGNATURE;
   1.964 +	tables->efi_runtime.hdr.revision = EFI_RUNTIME_SERVICES_REVISION;
   1.965 +	tables->efi_runtime.hdr.headersize = sizeof(tables->efi_runtime.hdr);
   1.966 +
   1.967 +	pfn = 0;
   1.968 +	EFI_HYPERCALL_PATCH(get_time,EFI_GET_TIME);
   1.969 +	EFI_HYPERCALL_PATCH(set_time,EFI_SET_TIME);
   1.970 +	EFI_HYPERCALL_PATCH(get_wakeup_time,EFI_GET_WAKEUP_TIME);
   1.971 +	EFI_HYPERCALL_PATCH(set_wakeup_time,EFI_SET_WAKEUP_TIME);
   1.972 +	EFI_HYPERCALL_PATCH(set_virtual_address_map,
   1.973 +	                    EFI_SET_VIRTUAL_ADDRESS_MAP);
   1.974 +	EFI_HYPERCALL_PATCH(get_variable,EFI_GET_VARIABLE);
   1.975 +	EFI_HYPERCALL_PATCH(get_next_variable,EFI_GET_NEXT_VARIABLE);
   1.976 +	EFI_HYPERCALL_PATCH(set_variable,EFI_SET_VARIABLE);
   1.977 +	EFI_HYPERCALL_PATCH(get_next_high_mono_count,
   1.978 +	                    EFI_GET_NEXT_HIGH_MONO_COUNT);
   1.979 +	EFI_HYPERCALL_PATCH(reset_system,EFI_RESET_SYSTEM);
   1.980 +
   1.981 +	/* System tables.  */
   1.982 +	tables->efi_tables[0].guid = SAL_SYSTEM_TABLE_GUID;
   1.983 +	tables->efi_tables[0].table = FW_FIELD_MPA(sal_systab);
   1.984 +	for (i = 1; i < NUM_EFI_SYS_TABLES; i++) {
   1.985 +		tables->efi_tables[i].guid = NULL_GUID;
   1.986 +		tables->efi_tables[i].table = 0;
   1.987 +	}
   1.988 +	if (d == dom0) {
   1.989 +		efi_systable_init_dom0(tables);
   1.990 +	} else {
   1.991 +		efi_systable_init_domu(tables);
   1.992 +	}
   1.993 +
   1.994 +	/* fill in the SAL system table: */
   1.995 +	memcpy(tables->sal_systab.signature, "SST_", 4);
   1.996 +	tables->sal_systab.size = sizeof(tables->sal_systab);
   1.997 +	tables->sal_systab.sal_rev_minor = 1;
   1.998 +	tables->sal_systab.sal_rev_major = 0;
   1.999 +	tables->sal_systab.entry_count = 2;
  1.1000 +
  1.1001 +	memcpy((char *)tables->sal_systab.oem_id, "Xen/ia64", 8);
  1.1002 +	memcpy((char *)tables->sal_systab.product_id, "Xen/ia64", 8);
  1.1003 +
  1.1004 +	/* PAL entry point: */
  1.1005 +	tables->sal_ed.type = SAL_DESC_ENTRY_POINT;
  1.1006 +	tables->sal_ed.pal_proc = FW_HYPERCALL_PAL_CALL_PADDR;
  1.1007 +	dom_fw_pal_hypercall_patch(brkimm, tables->sal_ed.pal_proc,
  1.1008 +				   hypercalls_imva);
  1.1009 +	/* SAL entry point.  */
  1.1010 +	tables->sal_ed.sal_proc = FW_HYPERCALL_SAL_CALL_PADDR;
  1.1011 +	dom_fw_hypercall_patch(brkimm, tables->sal_ed.sal_proc,
  1.1012 +	                       FW_HYPERCALL_SAL_CALL, 1, hypercalls_imva);
  1.1013 +	tables->sal_ed.gp = 0;  /* will be ignored */
  1.1014 +
  1.1015 +	/* Fill an AP wakeup descriptor.  */
  1.1016 +	tables->sal_wakeup.type = SAL_DESC_AP_WAKEUP;
  1.1017 +	tables->sal_wakeup.mechanism = IA64_SAL_AP_EXTERNAL_INT;
  1.1018 +	tables->sal_wakeup.vector = XEN_SAL_BOOT_RENDEZ_VEC;
  1.1019 +
  1.1020 +	/* Compute checksum.  */
  1.1021 +	checksum = 0;
  1.1022 +	for (cp = (char *)&tables->sal_systab;
  1.1023 +	     cp < (char *)&tables->fpswa_inf;
  1.1024 +	     ++cp)
  1.1025 +		checksum += *cp;
  1.1026 +	tables->sal_systab.checksum = -checksum;
  1.1027 +
  1.1028 +	/* SAL return point.  */
  1.1029 +	dom_fw_hypercall_patch(brkimm, FW_HYPERCALL_SAL_RETURN_PADDR,
  1.1030 +	                       FW_HYPERCALL_SAL_RETURN, 0, hypercalls_imva);
  1.1031 +
  1.1032 +	/* Fill in the FPSWA interface: */
  1.1033 +	if (fpswa_interface) {
  1.1034 +		tables->fpswa_inf.revision = fpswa_interface->revision;
  1.1035 +		dom_fpswa_hypercall_patch(brkimm, hypercalls_imva);
  1.1036 +		tables->fpswa_inf.fpswa = 
  1.1037 +		                       (void *)FW_HYPERCALL_FPSWA_ENTRY_PADDR;
  1.1038 +	}
  1.1039 +
  1.1040 +	i = 0; /* Used by MAKE_MD */
  1.1041 +
  1.1042 +	/* hypercall patches live here, masquerade as reserved PAL memory */
  1.1043 +	MAKE_MD(EFI_PAL_CODE,EFI_MEMORY_WB|EFI_MEMORY_RUNTIME,
  1.1044 +	        FW_HYPERCALL_BASE_PADDR, FW_HYPERCALL_END_PADDR);
  1.1045 +
  1.1046 +	/* Create dom0/domu md entry for fw and cpi tables area.  */
  1.1047 +	MAKE_MD(EFI_ACPI_MEMORY_NVS, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
  1.1048 +	        FW_ACPI_BASE_PADDR, FW_ACPI_END_PADDR);
  1.1049 +	MAKE_MD(EFI_RUNTIME_SERVICES_DATA, EFI_MEMORY_WB | EFI_MEMORY_RUNTIME,
  1.1050 +	        FW_TABLES_BASE_PADDR, FW_TABLES_END_PADDR);
  1.1051 +
  1.1052 +	if (d != dom0 || running_on_sim) {
  1.1053 +		/* DomU (or hp-ski).
  1.1054 +		   Create a continuous memory area.  */
  1.1055 +		/* kludge: bp->efi_memmap is used to pass memmap_info
  1.1056 +		 * page's pfn and number of pages to reserve. 
  1.1057 +		 * Currently the following pages must be reserved.
  1.1058 +		 * memmap info page, start info page, xenstore page
  1.1059 +		 * and console page.
  1.1060 +		 * see ia64_setup_memmap() @ xc_dom_boot.c
  1.1061 +		 */
  1.1062 +		num_mds = complete_domu_memmap(d, tables, maxmem, i,
  1.1063 +					       bp->efi_memmap,
  1.1064 +					       bp->efi_memmap_size);
  1.1065 +	} else {
  1.1066 +		/* Dom0.
  1.1067 +		   We must preserve ACPI data from real machine,
  1.1068 +		   as well as IO areas.  */
  1.1069 +		num_mds = complete_dom0_memmap(d, tables, maxmem, i);
  1.1070 +	}
  1.1071 +	if (num_mds < 0)
  1.1072 +		return num_mds;
  1.1073 +
  1.1074 +	/* Display memmap.  */
  1.1075 +	for (i = 0 ; i < num_mds; i++)
  1.1076 +		print_md(&tables->efi_memmap[i]);
  1.1077 +
  1.1078 +	/* Fill boot_param  */
  1.1079 +	bp->efi_systab = FW_FIELD_MPA(efi_systab);
  1.1080 +	bp->efi_memmap = FW_FIELD_MPA(efi_memmap);
  1.1081 +	bp->efi_memmap_size = num_mds * sizeof(efi_memory_desc_t);
  1.1082 +	bp->efi_memdesc_size = sizeof(efi_memory_desc_t);
  1.1083 +	bp->efi_memdesc_version = EFI_MEMDESC_VERSION;
  1.1084 +	bp->command_line = 0;
  1.1085 +	bp->console_info.num_cols = 80;
  1.1086 +	bp->console_info.num_rows = 25;
  1.1087 +	bp->console_info.orig_x = 0;
  1.1088 +	bp->console_info.orig_y = 24;
  1.1089 +	if (fpswa_interface)
  1.1090 +		bp->fpswa = FW_FIELD_MPA(fpswa_inf);
  1.1091 +	return 0;
  1.1092 +}
  1.1093 +
  1.1094 +static void
  1.1095 +dom_fw_domain_init(struct domain *d, struct fw_tables *tables)
  1.1096 +{
  1.1097 +	/* Initialise for EFI_SET_VIRTUAL_ADDRESS_MAP emulation */
  1.1098 +	d->arch.efi_runtime = &tables->efi_runtime;
  1.1099 +	d->arch.fpswa_inf   = &tables->fpswa_inf;
  1.1100 +	d->arch.sal_data    = &tables->sal_data;
  1.1101 +}
  1.1102 +
  1.1103 +static int
  1.1104 +dom_fw_set_convmem_end(struct domain *d)
  1.1105 +{
  1.1106 +	xen_ia64_memmap_info_t* memmap_info;
  1.1107 +	efi_memory_desc_t *md;
  1.1108 +	void *p;
  1.1109 +	void *memmap_start;
  1.1110 +	void *memmap_end;
  1.1111 +
  1.1112 +	if (d->shared_info->arch.memmap_info_pfn == 0)
  1.1113 +		return -EINVAL;
  1.1114 +
  1.1115 +	memmap_info = domain_mpa_to_imva(d, d->shared_info->arch.memmap_info_pfn << PAGE_SHIFT);
  1.1116 +	if (memmap_info->efi_memmap_size == 0 ||
  1.1117 +	    memmap_info->efi_memdesc_size != sizeof(*md) ||
  1.1118 +	    memmap_info->efi_memdesc_version !=
  1.1119 +	    EFI_MEMORY_DESCRIPTOR_VERSION)
  1.1120 +		return -EINVAL;
  1.1121 +	/* only 1page case is supported */
  1.1122 +	if (d->shared_info->arch.memmap_info_num_pages != 1)
  1.1123 +		return -ENOSYS;
  1.1124 +
  1.1125 +	memmap_start = &memmap_info->memdesc;
  1.1126 +	memmap_end = memmap_start + memmap_info->efi_memmap_size;
  1.1127 +
  1.1128 +	/* XXX Currently the table must be in a single page. */
  1.1129 +	if ((unsigned long)memmap_end > (unsigned long)memmap_info + PAGE_SIZE)
  1.1130 +		return -EINVAL;
  1.1131 +
  1.1132 +	/* sort it bofore use
  1.1133 +	 * XXX: this is created by user space domain builder so that
  1.1134 +	 * we should check its integrity */
  1.1135 +	sort(&memmap_info->memdesc,
  1.1136 +	     memmap_info->efi_memmap_size / memmap_info->efi_memdesc_size,
  1.1137 +	     memmap_info->efi_memdesc_size,
  1.1138 +	     efi_mdt_cmp, NULL);
  1.1139 +
  1.1140 +	if (d->arch.convmem_end == 0)
  1.1141 +		d->arch.convmem_end = d->max_pages << PAGE_SHIFT;
  1.1142 +	for (p = memmap_start; p < memmap_end; p += memmap_info->efi_memdesc_size) {
  1.1143 +		unsigned long end;
  1.1144 +		md = p;
  1.1145 +		end = md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT);
  1.1146 +		if (md->attribute == EFI_MEMORY_WB &&
  1.1147 +		    md->type == EFI_CONVENTIONAL_MEMORY &&
  1.1148 +		    md->num_pages > 0 &&
  1.1149 +		    d->arch.convmem_end < end)
  1.1150 +			d->arch.convmem_end = end;
  1.1151 +	}
  1.1152 +	return 0;
  1.1153 +}
  1.1154 +
  1.1155 +int
  1.1156 +dom_fw_setup(struct domain *d, unsigned long bp_mpa, unsigned long maxmem)
  1.1157 +{
  1.1158 +	int ret = 0;
  1.1159 +	struct ia64_boot_param *bp;
  1.1160 +	unsigned long imva_tables_base;
  1.1161 +	unsigned long imva_hypercall_base;
  1.1162 +
  1.1163 +	BUILD_BUG_ON(sizeof(struct fw_tables) >
  1.1164 +	             (FW_TABLES_END_PADDR - FW_TABLES_BASE_PADDR));
  1.1165 +
  1.1166 +	BUILD_BUG_ON(sizeof(struct fake_acpi_tables) >
  1.1167 +	             (FW_ACPI_END_PADDR - FW_ACPI_BASE_PADDR));
  1.1168 +
  1.1169 +	/* Create page for hypercalls.  */
  1.1170 +	assign_new_domain_page_if_dom0(d, FW_HYPERCALL_BASE_PADDR);
  1.1171 +	imva_hypercall_base = (unsigned long)domain_mpa_to_imva
  1.1172 +	                                     (d, FW_HYPERCALL_BASE_PADDR);
  1.1173 +
  1.1174 +	/* Create page for acpi tables.  */
  1.1175 +	if (d != dom0) {
  1.1176 +		void *imva;
  1.1177 +
  1.1178 +		assign_new_domain_page_if_dom0(d, FW_ACPI_BASE_PADDR);
  1.1179 +		imva = domain_mpa_to_imva (d, FW_ACPI_BASE_PADDR);
  1.1180 +		dom_fw_fake_acpi(d, (struct fake_acpi_tables *)imva);
  1.1181 +	}
  1.1182 +
  1.1183 +	/* Create page for FW tables.  */
  1.1184 +	assign_new_domain_page_if_dom0(d, FW_TABLES_BASE_PADDR);
  1.1185 +	imva_tables_base = (unsigned long)domain_mpa_to_imva
  1.1186 +	                                  (d, FW_TABLES_BASE_PADDR);
  1.1187 +
  1.1188 +	/* Create page for boot_param.  */
  1.1189 +	assign_new_domain_page_if_dom0(d, bp_mpa);
  1.1190 +	bp = domain_mpa_to_imva(d, bp_mpa);
  1.1191 +	if (d != dom0) {
  1.1192 +		/*
  1.1193 +		 * XXX kludge.
  1.1194 +		 * when XEN_DOMCTL_arch_setup is called, shared_info can't
  1.1195 +		 * be accessed by libxc so that memmap_info_pfn isn't
  1.1196 +		 * initialized. But dom_fw_set_convmem_end() requires it, 
  1.1197 +		 * so here we initialize it.
  1.1198 +		 * note:dom_fw_init() overwrites memmap_info_num_pages,
  1.1199 +		 *      memmap_info_pfns.
  1.1200 +		 */
  1.1201 +		if ((bp->efi_memmap_size & ~PAGE_MASK) != 0) {
  1.1202 +			printk("%s:%d size 0x%lx 0x%lx 0x%lx\n",
  1.1203 +			       __func__, __LINE__,
  1.1204 +			       bp->efi_memmap_size,
  1.1205 +			       bp->efi_memmap_size & ~PAGE_SIZE,
  1.1206 +			       ~PAGE_SIZE);
  1.1207 +			return -EINVAL;
  1.1208 +		}
  1.1209 +		if (bp->efi_memmap_size == 0) {
  1.1210 +			/* old domain builder compatibility */
  1.1211 +			d->shared_info->arch.memmap_info_num_pages = 1;
  1.1212 +			d->shared_info->arch.memmap_info_pfn =
  1.1213 +				(maxmem >> PAGE_SHIFT) - 1;
  1.1214 +		} else {
  1.1215 +			/*
  1.1216 +			 * 3: start info page, xenstore page and console page
  1.1217 +			 */
  1.1218 +			if (bp->efi_memmap_size < 4 * PAGE_SIZE)
  1.1219 +				return -EINVAL;
  1.1220 +			d->shared_info->arch.memmap_info_num_pages =
  1.1221 +				(bp->efi_memmap_size >> PAGE_SHIFT) - 3;
  1.1222 +			d->shared_info->arch.memmap_info_pfn = bp->efi_memmap;
  1.1223 +			/* currently multi page memmap isn't supported */
  1.1224 +			if (d->shared_info->arch.memmap_info_num_pages != 1)
  1.1225 +				return -ENOSYS;
  1.1226 +		}
  1.1227 +	}
  1.1228 +	ret = dom_fw_init(d, d->arch.breakimm, bp,
  1.1229 +			    (struct fw_tables *)imva_tables_base,
  1.1230 +			    imva_hypercall_base, maxmem);
  1.1231 +	if (ret < 0)
  1.1232 +		return ret;
  1.1233 +
  1.1234 +	dom_fw_domain_init(d, (struct fw_tables *)imva_tables_base);
  1.1235 +	return dom_fw_set_convmem_end(d);
  1.1236 +}