1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
4 #include <asm/x86_init.h>
6 #include <asm/xen/hypercall.h>
9 #include <xen/interface/physdev.h>
14 static unsigned int xen_io_apic_read(unsigned apic
, unsigned reg
)
16 struct physdev_apic apic_op
;
19 apic_op
.apic_physbase
= mpc_ioapic_addr(apic
);
21 ret
= HYPERVISOR_physdev_op(PHYSDEVOP_apic_read
, &apic_op
);
25 /* fallback to return an emulated IO_APIC values */
34 static unsigned long xen_set_apic_id(unsigned int x
)
40 static unsigned int xen_get_apic_id(unsigned long x
)
42 return ((x
)>>24) & 0xFFu
;
45 static u32
xen_apic_read(u32 reg
)
47 struct xen_platform_op op
= {
48 .cmd
= XENPF_get_cpuinfo
,
49 .interface_version
= XENPF_INTERFACE_VERSION
,
50 .u
.pcpu_info
.xen_cpuid
= 0,
54 /* Shouldn't need this as APIC is turned off for PV, and we only
55 * get called on the bootup processor. But just in case. */
56 if (!xen_initial_domain() || smp_processor_id())
63 return SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
68 ret
= HYPERVISOR_platform_op(&op
);
70 op
.u
.pcpu_info
.apic_id
= BAD_APICID
;
72 return op
.u
.pcpu_info
.apic_id
<< 24;
75 static void xen_apic_write(u32 reg
, u32 val
)
77 if (reg
== APIC_LVTPC
) {
78 (void)pmu_apic_update(reg
);
82 /* Warn to see if there's any stray references */
83 WARN(1,"register: %x, value: %x\n", reg
, val
);
86 static u64
xen_apic_icr_read(void)
91 static void xen_apic_icr_write(u32 low
, u32 id
)
93 /* Warn to see if there's any stray references */
97 static u32
xen_safe_apic_wait_icr_idle(void)
102 static int xen_apic_probe_pv(void)
110 static int xen_madt_oem_check(char *oem_id
, char *oem_table_id
)
112 return xen_pv_domain();
115 static int xen_id_always_valid(int apicid
)
120 static int xen_id_always_registered(void)
125 static int xen_phys_pkg_id(int initial_apic_id
, int index_msb
)
127 return initial_apic_id
>> index_msb
;
131 static int xen_x86_32_early_logical_apicid(int cpu
)
133 /* Match with APIC_LDR read. Otherwise setup_local_APIC complains. */
138 static void xen_noop(void)
142 static void xen_silent_inquire(int apicid
)
146 static int xen_cpu_present_to_apicid(int cpu
)
148 if (cpu_present(cpu
))
149 return cpu_data(cpu
).apicid
;
154 static struct apic xen_pv_apic
= {
156 .probe
= xen_apic_probe_pv
,
157 .acpi_madt_oem_check
= xen_madt_oem_check
,
158 .apic_id_valid
= xen_id_always_valid
,
159 .apic_id_registered
= xen_id_always_registered
,
161 /* .irq_delivery_mode - used in native_compose_msi_msg only */
162 /* .irq_dest_mode - used in native_compose_msi_msg only */
164 .target_cpus
= default_target_cpus
,
166 /* .dest_logical - default_send_IPI_ use it but we use our own. */
167 .check_apicid_used
= default_check_apicid_used
, /* Used on 32-bit */
169 .vector_allocation_domain
= flat_vector_allocation_domain
,
170 .init_apic_ldr
= xen_noop
, /* setup_local_APIC calls it */
172 .ioapic_phys_id_map
= default_ioapic_phys_id_map
, /* Used on 32-bit */
173 .setup_apic_routing
= NULL
,
174 .cpu_present_to_apicid
= xen_cpu_present_to_apicid
,
175 .apicid_to_cpu_present
= physid_set_mask_of_physid
, /* Used on 32-bit */
176 .check_phys_apicid_present
= default_check_phys_apicid_present
, /* smp_sanity_check needs it */
177 .phys_pkg_id
= xen_phys_pkg_id
, /* detect_ht */
179 .get_apic_id
= xen_get_apic_id
,
180 .set_apic_id
= xen_set_apic_id
, /* Can be NULL on 32-bit. */
182 .cpu_mask_to_apicid
= flat_cpu_mask_to_apicid
,
185 .send_IPI_mask
= xen_send_IPI_mask
,
186 .send_IPI_mask_allbutself
= xen_send_IPI_mask_allbutself
,
187 .send_IPI_allbutself
= xen_send_IPI_allbutself
,
188 .send_IPI_all
= xen_send_IPI_all
,
189 .send_IPI_self
= xen_send_IPI_self
,
191 /* .wait_for_init_deassert- used by AP bootup - smp_callin which we don't use */
192 .inquire_remote_apic
= xen_silent_inquire
,
194 .read
= xen_apic_read
,
195 .write
= xen_apic_write
,
196 .eoi_write
= xen_apic_write
,
198 .icr_read
= xen_apic_icr_read
,
199 .icr_write
= xen_apic_icr_write
,
200 .wait_icr_idle
= xen_noop
,
201 .safe_wait_icr_idle
= xen_safe_apic_wait_icr_idle
,
204 /* generic_processor_info and setup_local_APIC. */
205 .x86_32_early_logical_apicid
= xen_x86_32_early_logical_apicid
,
209 static void __init
xen_apic_check(void)
211 if (apic
== &xen_pv_apic
)
214 pr_info("Switched APIC routing from %s to %s.\n", apic
->name
,
218 void __init
xen_init_apic(void)
220 x86_io_apic_ops
.read
= xen_io_apic_read
;
221 /* On PV guests the APIC CPUID bit is disabled so none of the
222 * routines end up executing. */
223 if (!xen_initial_domain())
226 x86_platform
.apic_post_init
= xen_apic_check
;
228 apic_driver(xen_pv_apic
);