apic.c 4.5 KB

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