xref: /linux/arch/x86/xen/apic.c (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/thread_info.h>
4 
5 #include <asm/x86_init.h>
6 #include <asm/apic.h>
7 #include <asm/io_apic.h>
8 #include <asm/xen/hypercall.h>
9 
10 #include <xen/xen.h>
11 #include <xen/interface/physdev.h>
12 #include "xen-ops.h"
13 
14 static unsigned int xen_io_apic_read(unsigned apic, unsigned reg)
15 {
16 	struct physdev_apic apic_op;
17 	int ret;
18 
19 	apic_op.apic_physbase = mpc_ioapic_addr(apic);
20 	apic_op.reg = reg;
21 	ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
22 	if (!ret)
23 		return apic_op.value;
24 
25 	/* fallback to return an emulated IO_APIC values */
26 	if (reg == 0x1)
27 		return 0x00170020;
28 	else if (reg == 0x0)
29 		return apic << 24;
30 
31 	return 0xfd;
32 }
33 
34 static u32 xen_get_apic_id(u32 x)
35 {
36 	return ((x)>>24) & 0xFFu;
37 }
38 
39 static u32 xen_apic_read(u32 reg)
40 {
41 	struct xen_platform_op op = {
42 		.cmd = XENPF_get_cpuinfo,
43 		.interface_version = XENPF_INTERFACE_VERSION,
44 	};
45 	int ret, cpu;
46 
47 	if (reg == APIC_LVR)
48 		return 0x14;
49 	if (reg != APIC_ID)
50 		return 0;
51 
52 	cpu = smp_processor_id();
53 	if (!xen_initial_domain())
54 		return cpu ? cpuid_to_apicid[cpu] << 24 : 0;
55 
56 	op.u.pcpu_info.xen_cpuid = cpu;
57 
58 	ret = HYPERVISOR_platform_op(&op);
59 	if (ret)
60 		op.u.pcpu_info.apic_id = BAD_APICID;
61 
62 	return op.u.pcpu_info.apic_id << 24;
63 }
64 
65 static void xen_apic_write(u32 reg, u32 val)
66 {
67 	if (reg == APIC_LVTPC) {
68 		(void)pmu_apic_update(reg);
69 		return;
70 	}
71 
72 	/* Warn to see if there's any stray references */
73 	WARN(1,"register: %x, value: %x\n", reg, val);
74 }
75 
76 static void xen_apic_eoi(void)
77 {
78 	WARN_ON_ONCE(1);
79 }
80 
81 static u64 xen_apic_icr_read(void)
82 {
83 	return 0;
84 }
85 
86 static void xen_apic_icr_write(u32 low, u32 id)
87 {
88 	/* Warn to see if there's any stray references */
89 	WARN_ON(1);
90 }
91 
92 static int xen_apic_probe_pv(void)
93 {
94 	if (xen_pv_domain())
95 		return 1;
96 
97 	return 0;
98 }
99 
100 static int xen_madt_oem_check(char *oem_id, char *oem_table_id)
101 {
102 	return xen_pv_domain();
103 }
104 
105 static u32 xen_cpu_present_to_apicid(int cpu)
106 {
107 	if (cpu_present(cpu))
108 		return cpu_data(cpu).topo.apicid;
109 	else
110 		return BAD_APICID;
111 }
112 
113 static struct apic xen_pv_apic __ro_after_init = {
114 	.name				= "Xen PV",
115 	.probe				= xen_apic_probe_pv,
116 	.acpi_madt_oem_check		= xen_madt_oem_check,
117 
118 	/* .delivery_mode and .dest_mode_logical not used by XENPV */
119 
120 	.disable_esr			= 0,
121 
122 	.cpu_present_to_apicid		= xen_cpu_present_to_apicid,
123 
124 	.max_apic_id			= UINT_MAX,
125 	.get_apic_id			= xen_get_apic_id,
126 
127 	.calc_dest_apicid		= apic_flat_calc_apicid,
128 
129 #ifdef CONFIG_SMP
130 	.send_IPI_mask			= xen_send_IPI_mask,
131 	.send_IPI_mask_allbutself	= xen_send_IPI_mask_allbutself,
132 	.send_IPI_allbutself		= xen_send_IPI_allbutself,
133 	.send_IPI_all			= xen_send_IPI_all,
134 	.send_IPI_self			= xen_send_IPI_self,
135 #endif
136 	.read				= xen_apic_read,
137 	.write				= xen_apic_write,
138 	.eoi				= xen_apic_eoi,
139 
140 	.icr_read			= xen_apic_icr_read,
141 	.icr_write			= xen_apic_icr_write,
142 };
143 apic_driver(xen_pv_apic);
144 
145 void __init xen_init_apic(void)
146 {
147 	x86_apic_ops.io_apic_read = xen_io_apic_read;
148 }
149