aboutsummaryrefslogtreecommitdiffstats
path: root/techlibs/common
diff options
context:
space:
mode:
Diffstat (limited to 'techlibs/common')
0 files changed, 0 insertions, 0 deletions
3' href='#n23'>23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
/*
 *  Xen domain firmware emulation support
 *  Copyright (C) 2004 Hewlett-Packard Co.
 *       Dan Magenheimer (dan.magenheimer@hp.com)
 *
 * Copyright (c) 2006, 2007
 *                    Isaku Yamahata <yamahata at valinux co jp>
 *                    VA Linux Systems Japan K.K.
 *                    dom0 vp model support
 */

#ifdef __XEN__
#include <asm/system.h>
#include <asm/dom_fw_dom0.h>
#include <asm/dom_fw_utils.h>
#else
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <inttypes.h>

#include <xen/xen.h>
#include <asm/bundle.h>

#include "xg_private.h"
#include "xc_dom.h"
#include "ia64/xc_dom_ia64_util.h"

#define ia64_fc(addr)   asm volatile ("fc %0" :: "r"(addr) : "memory")
#endif /* __XEN__ */

#include <xen/acpi.h>
#include <acpi/actables.h>
#include <asm/dom_fw.h>
#include <asm/dom_fw_domu.h>

void
xen_ia64_efi_make_md(efi_memory_desc_t *md,
		     uint32_t type, uint64_t attr, 
		     uint64_t start, uint64_t end)
{
	md->type = type;
	md->pad = 0;
	md->phys_addr = start;
	md->virt_addr = 0;
	md->num_pages = (end - start) >> EFI_PAGE_SHIFT;
	md->attribute = attr;
}

#define EFI_HYPERCALL_PATCH(tgt, call)					\
	do {								\
		dom_efi_hypercall_patch(brkimm,				\
					FW_HYPERCALL_##call##_PADDR,	\
		                 FW_HYPERCALL_##call, hypercalls_imva);	\
		/* Descriptor address.  */                              \
		tables->efi_runtime.tgt =                               \
		                    FW_FIELD_MPA(func_ptrs) + 8 * pfn;  \
		/* Descriptor.  */                                      \
		tables->func_ptrs[pfn++] = FW_HYPERCALL_##call##_PADDR;	\
		tables->func_ptrs[pfn++] = 0;                     	\
	} while (0)

/**************************************************************************
Hypercall bundle creation
**************************************************************************/

static void
build_hypercall_bundle(uint64_t *imva, uint64_t brkimm, uint64_t hypnum, uint64_t ret)
{
	INST64_A5 slot0;
	INST64_I19 slot1;
	INST64_B4 slot2;
	IA64_BUNDLE bundle;

	// slot1: mov r2 = hypnum (low 20 bits)
	slot0.inst = 0;
	slot0.qp = 0; slot0.r1 = 2; slot0.r3 = 0; slot0.major = 0x9;
	slot0.imm7b = hypnum; slot0.imm9d = hypnum >> 7;
	slot0.imm5c = hypnum >> 16; slot0.s = 0;
	// slot1: break brkimm
	slot1.inst = 0;
	slot1.qp = 0; slot1.x6 = 0; slot1.x3 = 0; slot1.major = 0x0;
	slot1.imm20 = brkimm; slot1.i = brkimm >> 20;
	// if ret slot2:  br.ret.sptk.many rp
	// else   slot2:  br.cond.sptk.many rp
	slot2.inst = 0; slot2.qp = 0; slot2.p = 1; slot2.b2 = 0;
	slot2.wh = 0; slot2.d = 0; slot2.major = 0x0;
	if (ret) {
		slot2.btype = 4; slot2.x6 = 0x21;
	}
	else {
		slot2.btype = 0; slot2.x6 = 0x20;
	}
	
	bundle.i64[0] = 0; bundle.i64[1] = 0;
	bundle.template = 0x11;
	bundle.slot0 = slot0.inst; bundle.slot2 = slot2.inst;
	bundle.slot1a = slot1.inst; bundle.slot1b = slot1.inst >> 18;
	
	imva[0] = bundle.i64[0]; imva[1] = bundle.i64[1];
	ia64_fc(imva);
	ia64_fc(imva + 1);
}

static void
build_pal_hypercall_bundles(uint64_t *imva, uint64_t brkimm, uint64_t hypnum)
{
	extern unsigned long xen_ia64_pal_call_stub[];
	IA64_BUNDLE bundle;
	INST64_A5 slot_a5;
	INST64_M37 slot_m37;

	/*
	 * The source of the hypercall stub is
	 * the xen_ia64_pal_call_stub function defined in dom_fw_asm.S. 
	 */

	/* Copy the first bundle and patch the hypercall number.  */
	bundle.i64[0] = xen_ia64_pal_call_stub[0];
	bundle.i64[1] = xen_ia64_pal_call_stub[1];
	slot_a5.inst = bundle.slot0;
	slot_a5.imm7b = hypnum;
	slot_a5.imm9d = hypnum >> 7;
	slot_a5.imm5c = hypnum >> 16;
	bundle.slot0 = slot_a5.inst;
	imva[0] = bundle.i64[0];
	imva[1] = bundle.i64[1];
	ia64_fc(imva);
	ia64_fc(imva + 1);
	
	/* Copy the second bundle and patch the hypercall vector.  */
	bundle.i64[0] = xen_ia64_pal_call_stub[2];
	bundle.i64[1] = xen_ia64_pal_call_stub[3];
	slot_m37.inst = bundle.slot0;
	slot_m37.imm20a = brkimm;
	slot_m37.i = brkimm >> 20;
	bundle.slot0 = slot_m37.inst;
	imva[2] = bundle.i64[0];
	imva[3] = bundle.i64[1];
	ia64_fc(imva + 2);
	ia64_fc(imva + 3);
}

// builds a hypercall bundle at domain physical address
static void
dom_fpswa_hypercall_patch(uint64_t brkimm, unsigned long imva)
{
	unsigned long *entry_imva, *patch_imva;
	const unsigned long entry_paddr = FW_HYPERCALL_FPSWA_ENTRY_PADDR;
	const unsigned long patch_paddr = FW_HYPERCALL_FPSWA_PATCH_PADDR;

	entry_imva = (unsigned long *)(imva + entry_paddr -
	                               FW_HYPERCALL_BASE_PADDR);
	patch_imva = (unsigned long *)(imva + patch_paddr -
	                               FW_HYPERCALL_BASE_PADDR);

	/* Descriptor.  */
	*entry_imva++ = patch_paddr;
	*entry_imva   = 0;

	build_hypercall_bundle(patch_imva, brkimm, FW_HYPERCALL_FPSWA, 1);
}

// builds a hypercall bundle at domain physical address
static void
dom_efi_hypercall_patch(uint64_t brkimm, unsigned long paddr,
                        unsigned long hypercall, unsigned long imva)
{
	build_hypercall_bundle((uint64_t *)(imva + paddr -
			       FW_HYPERCALL_BASE_PADDR),
			       brkimm, hypercall, 1);
}

// builds a hypercall bundle at domain physical address
static void
dom_fw_hypercall_patch(uint64_t brkimm, unsigned long paddr,
		       unsigned long hypercall,unsigned long ret,
                       unsigned long imva)
{
	build_hypercall_bundle((uint64_t *)(imva + paddr -
			       FW_HYPERCALL_BASE_PADDR),
			       brkimm, hypercall, ret);
}

static void
dom_fw_pal_hypercall_patch(uint64_t brkimm, unsigned long paddr, unsigned long imva)
{
	build_pal_hypercall_bundles((uint64_t*)(imva + paddr -
	                            FW_HYPERCALL_BASE_PADDR),
	                            brkimm, FW_HYPERCALL_PAL_CALL);
}

static inline void
print_md(efi_memory_desc_t *md)
{
	uint64_t size;
	
	printk(XENLOG_INFO "dom mem: type=%2u, attr=0x%016lx, "
	       "range=[0x%016lx-0x%016lx) ",
	       md->type, md->attribute, md->phys_addr,
	       md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT));

	size = md->num_pages << EFI_PAGE_SHIFT;
	if (size > ONE_MB)
		printk("(%luMB)\n", size >> 20);
	else
		printk("(%luKB)\n", size >> 10);
}

struct fake_acpi_tables {
	struct acpi_table_rsdp rsdp;
	struct acpi_table_xsdt xsdt;
	uint64_t madt_ptr;
	struct acpi_table_fadt fadt;
	struct acpi_table_facs facs;
	struct acpi_table_header dsdt;
	uint8_t aml[8 + 11 * MAX_VIRT_CPUS];
	struct acpi_table_madt madt;
	struct acpi_table_lsapic lsapic[MAX_VIRT_CPUS];
	uint8_t pm1a_event_block[4];
	uint8_t pm1a_control_block[1];
	uint8_t pm_timer_block[4];
};
#define ACPI_TABLE_MPA(field)                                       \
    FW_ACPI_BASE_PADDR + offsetof(struct fake_acpi_tables, field);

/* Create enough of an ACPI structure to make the guest OS ACPI happy. */
void
dom_fw_fake_acpi(domain_t *d, struct fake_acpi_tables *tables)
{
	struct acpi_table_rsdp *rsdp = &tables->rsdp;
	struct acpi_table_xsdt *xsdt = &tables->xsdt;
	struct acpi_table_fadt *fadt = &tables->fadt;
	struct acpi_table_facs *facs = &tables->facs;
	struct acpi_table_header *dsdt = &tables->dsdt;
	struct acpi_table_madt *madt = &tables->madt;
	struct acpi_table_lsapic *lsapic = tables->lsapic;
	int i;
	int aml_len;
	int nbr_cpus;

	BUILD_BUG_ON(sizeof(struct fake_acpi_tables) >
	             (FW_ACPI_END_PADDR - FW_ACPI_BASE_PADDR));

	memset(tables, 0, sizeof(struct fake_acpi_tables));

	/* setup XSDT (64bit version of RSDT) */
	memcpy(xsdt->header.signature, ACPI_SIG_XSDT,
	       sizeof(xsdt->header.signature));
	/* XSDT points to both the FADT and the MADT, so add one entry */
	xsdt->header.length = sizeof(struct acpi_table_xsdt) + sizeof(uint64_t);
	xsdt->header.revision = 1;
	memcpy(xsdt->header.oem_id, "XEN", 3);
	memcpy(xsdt->header.oem_table_id, "Xen/ia64", 8);
	memcpy(xsdt->header.asl_compiler_id, "XEN", 3);
	xsdt->header.asl_compiler_revision = xen_ia64_version(d);

	xsdt->table_offset_entry[0] = ACPI_TABLE_MPA(fadt);
	tables->madt_ptr = ACPI_TABLE_MPA(madt);

	xsdt->header.checksum = -acpi_tb_checksum((u8*)xsdt,
						  xsdt->header.length);

	/* setup FADT */
	memcpy(fadt->header.signature, ACPI_SIG_FADT,
	       sizeof(fadt->header.signature));
	fadt->header.length = sizeof(struct acpi_table_fadt);
	fadt->header.revision = FADT2_REVISION_ID;
	memcpy(fadt->header.oem_id, "XEN", 3);
	memcpy(fadt->header.oem_table_id, "Xen/ia64", 8);
	memcpy(fadt->header.asl_compiler_id, "XEN", 3);
	fadt->header.asl_compiler_revision = xen_ia64_version(d);

	memcpy(facs->signature, ACPI_SIG_FACS, sizeof(facs->signature));
	facs->version = 1;
	facs->length = sizeof(struct acpi_table_facs);

	fadt->Xfacs = ACPI_TABLE_MPA(facs);
	fadt->Xdsdt = ACPI_TABLE_MPA(dsdt);

	/*
	 * All of the below FADT entries are filled it to prevent warnings
	 * from sanity checks in the ACPI CA.  Emulate required ACPI hardware
	 * registers in system memory.
	 */
	fadt->pm1_event_length = 4;
	fadt->xpm1a_event_block.space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
	fadt->xpm1a_event_block.bit_width = 8;
	fadt->xpm1a_event_block.address = ACPI_TABLE_MPA(pm1a_event_block);
	fadt->pm1_control_length = 1;
	fadt->xpm1a_control_block.space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
	fadt->xpm1a_control_block.bit_width = 8;
	fadt->xpm1a_control_block.address = ACPI_TABLE_MPA(pm1a_control_block);
	fadt->pm_timer_length = 4;
	fadt->xpm_timer_block.space_id = ACPI_ADR_SPACE_SYSTEM_MEMORY;
	fadt->xpm_timer_block.bit_width = 8;
	fadt->xpm_timer_block.address = ACPI_TABLE_MPA(pm_timer_block);

	fadt->header.checksum = -acpi_tb_checksum((u8*)fadt,
						  fadt->header.length);

	/* setup RSDP */
	memcpy(rsdp->signature, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP));
	memcpy(rsdp->oem_id, "XEN", 3);
	rsdp->revision = 2; /* ACPI 2.0 includes XSDT */
	rsdp->length = sizeof(struct acpi_table_rsdp);
	rsdp->xsdt_physical_address = ACPI_TABLE_MPA(xsdt);

	rsdp->checksum = -acpi_tb_checksum((u8*)rsdp,
					   ACPI_RSDP_CHECKSUM_LENGTH);
	rsdp->extended_checksum = -acpi_tb_checksum((u8*)rsdp, rsdp->length);

	/* setup DSDT with trivial namespace. */ 
	memcpy(dsdt->signature, ACPI_SIG_DSDT, strlen(ACPI_SIG_DSDT));
	dsdt->revision = 1;
	memcpy(dsdt->oem_id, "XEN", 3);
	memcpy(dsdt->oem_table_id, "Xen/ia64", 8);
	memcpy(dsdt->asl_compiler_id, "XEN", 3);
	dsdt->asl_compiler_revision = xen_ia64_version(d);

	/* Trivial namespace, avoids ACPI CA complaints */
	tables->aml[0] = 0x10; /* Scope */
	tables->aml[1] = 0x40; /* length/offset to next object (patched) */
	tables->aml[2] = 0x00;
	memcpy(&tables->aml[3], "_SB_", 4);

	/* The processor object isn't absolutely necessary, revist for SMP */
	aml_len = 7;
	for (i = 0; i < 3; i++) {
		unsigned char *p = tables->aml + aml_len;
		p[0] = 0x5b; /* processor object */
		p[1] = 0x83;
		p[2] = 0x0b; /* next */
		p[3] = 'C';
		p[4] = 'P';
		snprintf ((char *)p + 5, 3, "%02x", i);
		if (i < 16)
			p[5] = 'U';
		p[7] = i;	/* acpi_id */
		p[8] = 0;	/* pblk_addr */