aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware/hvmloader/acpi_madt.c
blob: 5a9c3063ae861c18b1f5066615e3096b5302207e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
/*
 * acpi_madt.c: Update ACPI MADT table for multiple processor guest.
 *
 * Yu Ke, ke.yu@intel.com
 * Copyright (c) 2005, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 */

#include "../acpi/acpi2_0.h"
#include "../acpi/acpi_madt.h"
#include "util.h"
#include <xen/hvm/hvm_info_table.h>

#define NULL ((void*)0)

static struct hvm_info_table *table = NULL;

static int validate_hvm_info(struct hvm_info_table *t)
{
	char signature[] = "HVM INFO";
	uint8_t *ptr = (uint8_t *)t;
	uint8_t sum = 0;
	int i;

	/* strncmp(t->signature, "HVM INFO", 8) */
	for (i = 0; i < 8; i++) {
		if (signature[i] != t->signature[i]) {
			puts("Bad hvm info signature\n");
			return 0;
		}
	}

	for (i = 0; i < t->length; i++)
		sum += ptr[i];

	return (sum == 0);
}

/* xc_vmx_builder wrote hvm info at 0x9F800. Return it. */
struct hvm_info_table *
get_hvm_info_table(void)
{
	struct hvm_info_table *t;

	if (table != NULL)
		return table;

	t = (struct hvm_info_table *)HVM_INFO_PADDR;

	if (!validate_hvm_info(t)) {
		puts("Bad hvm info table\n");
		return NULL;
	}

	table = t;

	return table;
}

int
get_vcpu_nr(void)
{
	struct hvm_info_table *t = get_hvm_info_table();
	return (t ? t->nr_vcpus : 1); /* default 1 vcpu */
}

int
get_acpi_enabled(void)
{
	struct hvm_info_table *t = get_hvm_info_table();
	return (t ? t->acpi_enabled : 0); /* default no acpi */
}


static void *
acpi_madt_get_madt(unsigned char *acpi_start)
{
	ACPI_2_0_RSDP *rsdp=NULL;
	ACPI_2_0_RSDT *rsdt=NULL;
	ACPI_MULTIPLE_APIC_DESCRIPTION_TABLE *madt;

	rsdp = (ACPI_2_0_RSDP *)(acpi_start + sizeof(ACPI_2_0_FACS));
	if (rsdp->Signature != ACPI_2_0_RSDP_SIGNATURE) {
		puts("Bad RSDP signature\n");
		return NULL;
	}

	rsdt= (ACPI_2_0_RSDT *)
		(acpi_start + rsdp->RsdtAddress - ACPI_PHYSICAL_ADDRESS);
	if (rsdt->Header.Signature != ACPI_2_0_RSDT_SIGNATURE) {
		puts("Bad RSDT signature\n");
		return NULL;
	}

	madt = (ACPI_MULTIPLE_APIC_DESCRIPTION_TABLE *)
		( acpi_start+ rsdt->Entry[1] - ACPI_PHYSICAL_ADDRESS);
	if (madt->Header.Header.Signature !=
	    ACPI_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE) {
		puts("Bad MADT signature \n");
		return NULL;
	}

	return madt;
}

static void
set_checksum(void *start, int checksum_offset, int len)
{
	unsigned char sum = 0;
	unsigned char *ptr;

	ptr = start;
	ptr[checksum_offset] = 0;
	while (len--)
		sum += *ptr++;

	ptr = start;
	ptr[checksum_offset] = -sum;
}

static int
acpi_madt_set_local_apics(
	int nr_vcpu,
	ACPI_MULTIPLE_APIC_DESCRIPTION_TABLE *madt)
{
	int i;

	if ((nr_vcpu > MAX_VIRT_CPUS) || (nr_vcpu < 0) || !madt)
		return -1;

	for (i = 0; i < nr_vcpu; i++) {
		madt->LocalApic[i].Type            = ACPI_PROCESSOR_LOCAL_APIC;
		madt->LocalApic[i].Length          = sizeof (ACPI_LOCAL_APIC_STRUCTURE);
		madt->LocalApic[i].AcpiProcessorId = i;
		madt->LocalApic[i].ApicId          = i;
		madt->LocalApic[i].Flags           = 1;
	}

	madt->Header.Header.Length =
		sizeof(ACPI_MULTIPLE_APIC_DESCRIPTION_TABLE) -
		(MAX_VIRT_CPUS - nr_vcpu)* sizeof(ACPI_LOCAL_APIC_STRUCTURE);

	return 0;
}

#define FIELD_OFFSET(TYPE,Field) ((unsigned int)(&(((TYPE *) 0)->Field)))

int acpi_madt_update(unsigned char *acpi_start)
{
	int rc;
	ACPI_MULTIPLE_APIC_DESCRIPTION_TABLE *madt;

	madt = acpi_madt_get_madt(acpi_start);
	if (!madt)
		return -1;

	rc = acpi_madt_set_local_apics(get_vcpu_nr(), madt);
	if (rc != 0)
		return rc;

	set_checksum(
		madt, FIELD_OFFSET(ACPI_TABLE_HEADER, Checksum),
		madt->Header.Header.Length);

	return 0;
}

/*
 * Local variables:
 *  c-file-style: "linux"
 *  indent-tabs-mode: t
 *  c-indent-level: 8
 *  c-basic-offset: 8
 *  tab-width: 8
 * End:
 */