apic.c 5.0 KB

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