summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/kvm/lib/x86_64/apic.c
blob: 89153a333e83c2bfbde806d1b15b51c62fc2aa3d (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2021, Google LLC.
 */

#include "apic.h"

void apic_disable(void)
{
	wrmsr(MSR_IA32_APICBASE,
	      rdmsr(MSR_IA32_APICBASE) &
		~(MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD));
}

void xapic_enable(void)
{
	uint64_t val = rdmsr(MSR_IA32_APICBASE);

	/* Per SDM: to enable xAPIC when in x2APIC must first disable APIC */
	if (val & MSR_IA32_APICBASE_EXTD) {
		apic_disable();
		wrmsr(MSR_IA32_APICBASE,
		      rdmsr(MSR_IA32_APICBASE) | MSR_IA32_APICBASE_ENABLE);
	} else if (!(val & MSR_IA32_APICBASE_ENABLE)) {
		wrmsr(MSR_IA32_APICBASE, val | MSR_IA32_APICBASE_ENABLE);
	}

	/*
	 * Per SDM: reset value of spurious interrupt vector register has the
	 * APIC software enabled bit=0. It must be enabled in addition to the
	 * enable bit in the MSR.
	 */
	val = xapic_read_reg(APIC_SPIV) | APIC_SPIV_APIC_ENABLED;
	xapic_write_reg(APIC_SPIV, val);
}

void x2apic_enable(void)
{
	wrmsr(MSR_IA32_APICBASE, rdmsr(MSR_IA32_APICBASE) |
	      MSR_IA32_APICBASE_ENABLE | MSR_IA32_APICBASE_EXTD);
	x2apic_write_reg(APIC_SPIV,
			 x2apic_read_reg(APIC_SPIV) | APIC_SPIV_APIC_ENABLED);
}