^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/thread_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/x86_init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/apic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/io_apic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <xen/interface/physdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "xen-ops.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "pmu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "smp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static unsigned int xen_io_apic_read(unsigned apic, unsigned reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct physdev_apic apic_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) apic_op.apic_physbase = mpc_ioapic_addr(apic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) apic_op.reg = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return apic_op.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* fallback to return an emulated IO_APIC values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (reg == 0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return 0x00170020;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) else if (reg == 0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return apic << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return 0xfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static u32 xen_set_apic_id(unsigned int x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static unsigned int xen_get_apic_id(unsigned long x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return ((x)>>24) & 0xFFu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static u32 xen_apic_read(u32 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct xen_platform_op op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .cmd = XENPF_get_cpuinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .interface_version = XENPF_INTERFACE_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .u.pcpu_info.xen_cpuid = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* Shouldn't need this as APIC is turned off for PV, and we only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * get called on the bootup processor. But just in case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!xen_initial_domain() || smp_processor_id())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (reg == APIC_LVR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0x14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (reg != APIC_ID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ret = HYPERVISOR_platform_op(&op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) op.u.pcpu_info.apic_id = BAD_APICID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return op.u.pcpu_info.apic_id << 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static void xen_apic_write(u32 reg, u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (reg == APIC_LVTPC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) (void)pmu_apic_update(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* Warn to see if there's any stray references */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) WARN(1,"register: %x, value: %x\n", reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static u64 xen_apic_icr_read(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static void xen_apic_icr_write(u32 low, u32 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* Warn to see if there's any stray references */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static u32 xen_safe_apic_wait_icr_idle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static int xen_apic_probe_pv(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (xen_pv_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int xen_madt_oem_check(char *oem_id, char *oem_table_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return xen_pv_domain();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int xen_id_always_valid(u32 apicid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static int xen_id_always_registered(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static int xen_phys_pkg_id(int initial_apic_id, int index_msb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return initial_apic_id >> index_msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void xen_noop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void xen_silent_inquire(int apicid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int xen_cpu_present_to_apicid(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (cpu_present(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return cpu_data(cpu).apicid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return BAD_APICID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static struct apic xen_pv_apic = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .name = "Xen PV",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .probe = xen_apic_probe_pv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .acpi_madt_oem_check = xen_madt_oem_check,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .apic_id_valid = xen_id_always_valid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .apic_id_registered = xen_id_always_registered,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* .irq_delivery_mode - used in native_compose_msi_msg only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* .irq_dest_mode - used in native_compose_msi_msg only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .disable_esr = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* .dest_logical - default_send_IPI_ use it but we use our own. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .check_apicid_used = default_check_apicid_used, /* Used on 32-bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) .init_apic_ldr = xen_noop, /* setup_local_APIC calls it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .ioapic_phys_id_map = default_ioapic_phys_id_map, /* Used on 32-bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .setup_apic_routing = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .cpu_present_to_apicid = xen_cpu_present_to_apicid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .apicid_to_cpu_present = physid_set_mask_of_physid, /* Used on 32-bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .check_phys_apicid_present = default_check_phys_apicid_present, /* smp_sanity_check needs it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .phys_pkg_id = xen_phys_pkg_id, /* detect_ht */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .get_apic_id = xen_get_apic_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .set_apic_id = xen_set_apic_id, /* Can be NULL on 32-bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .calc_dest_apicid = apic_flat_calc_apicid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .send_IPI_mask = xen_send_IPI_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .send_IPI_mask_allbutself = xen_send_IPI_mask_allbutself,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .send_IPI_allbutself = xen_send_IPI_allbutself,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .send_IPI_all = xen_send_IPI_all,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .send_IPI_self = xen_send_IPI_self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* .wait_for_init_deassert- used by AP bootup - smp_callin which we don't use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .inquire_remote_apic = xen_silent_inquire,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .read = xen_apic_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .write = xen_apic_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .eoi_write = xen_apic_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .icr_read = xen_apic_icr_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .icr_write = xen_apic_icr_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .wait_icr_idle = xen_noop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .safe_wait_icr_idle = xen_safe_apic_wait_icr_idle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void __init xen_apic_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (apic == &xen_pv_apic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) pr_info("Switched APIC routing from %s to %s.\n", apic->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) xen_pv_apic.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) apic = &xen_pv_apic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) void __init xen_init_apic(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) x86_apic_ops.io_apic_read = xen_io_apic_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /* On PV guests the APIC CPUID bit is disabled so none of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * routines end up executing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (!xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) apic = &xen_pv_apic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) x86_platform.apic_post_init = xen_apic_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) apic_driver(xen_pv_apic);