x2apic_phys.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/threads.h>
  3. #include <linux/cpumask.h>
  4. #include <linux/string.h>
  5. #include <linux/kernel.h>
  6. #include <linux/ctype.h>
  7. #include <linux/dmar.h>
  8. #include <asm/smp.h>
  9. #include <asm/ipi.h>
  10. #include "x2apic.h"
  11. int x2apic_phys;
  12. static struct apic apic_x2apic_phys;
  13. static u32 x2apic_max_apicid __ro_after_init;
  14. void __init x2apic_set_max_apicid(u32 apicid)
  15. {
  16. x2apic_max_apicid = apicid;
  17. }
  18. static int __init set_x2apic_phys_mode(char *arg)
  19. {
  20. x2apic_phys = 1;
  21. return 0;
  22. }
  23. early_param("x2apic_phys", set_x2apic_phys_mode);
  24. static bool x2apic_fadt_phys(void)
  25. {
  26. #ifdef CONFIG_ACPI
  27. if ((acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) &&
  28. (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
  29. printk(KERN_DEBUG "System requires x2apic physical mode\n");
  30. return true;
  31. }
  32. #endif
  33. return false;
  34. }
  35. static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  36. {
  37. return x2apic_enabled() && (x2apic_phys || x2apic_fadt_phys());
  38. }
  39. static void x2apic_send_IPI(int cpu, int vector)
  40. {
  41. u32 dest = per_cpu(x86_cpu_to_apicid, cpu);
  42. /* x2apic MSRs are special and need a special fence: */
  43. weak_wrmsr_fence();
  44. __x2apic_send_IPI_dest(dest, vector, APIC_DEST_PHYSICAL);
  45. }
  46. static void
  47. __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
  48. {
  49. unsigned long query_cpu;
  50. unsigned long this_cpu;
  51. unsigned long flags;
  52. /* x2apic MSRs are special and need a special fence: */
  53. weak_wrmsr_fence();
  54. local_irq_save(flags);
  55. this_cpu = smp_processor_id();
  56. for_each_cpu(query_cpu, mask) {
  57. if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu)
  58. continue;
  59. __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
  60. vector, APIC_DEST_PHYSICAL);
  61. }
  62. local_irq_restore(flags);
  63. }
  64. static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
  65. {
  66. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
  67. }
  68. static void
  69. x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
  70. {
  71. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
  72. }
  73. static void x2apic_send_IPI_allbutself(int vector)
  74. {
  75. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
  76. }
  77. static void x2apic_send_IPI_all(int vector)
  78. {
  79. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
  80. }
  81. static void init_x2apic_ldr(void)
  82. {
  83. }
  84. static int x2apic_phys_probe(void)
  85. {
  86. if (x2apic_mode && (x2apic_phys || x2apic_fadt_phys()))
  87. return 1;
  88. return apic == &apic_x2apic_phys;
  89. }
  90. /* Common x2apic functions, also used by x2apic_cluster */
  91. int x2apic_apic_id_valid(u32 apicid)
  92. {
  93. if (x2apic_max_apicid && apicid > x2apic_max_apicid)
  94. return 0;
  95. return 1;
  96. }
  97. int x2apic_apic_id_registered(void)
  98. {
  99. return 1;
  100. }
  101. void __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest)
  102. {
  103. unsigned long cfg = __prepare_ICR(0, vector, dest);
  104. native_x2apic_icr_write(cfg, apicid);
  105. }
  106. unsigned int x2apic_get_apic_id(unsigned long id)
  107. {
  108. return id;
  109. }
  110. u32 x2apic_set_apic_id(unsigned int id)
  111. {
  112. return id;
  113. }
  114. int x2apic_phys_pkg_id(int initial_apicid, int index_msb)
  115. {
  116. return initial_apicid >> index_msb;
  117. }
  118. void x2apic_send_IPI_self(int vector)
  119. {
  120. apic_write(APIC_SELF_IPI, vector);
  121. }
  122. static struct apic apic_x2apic_phys __ro_after_init = {
  123. .name = "physical x2apic",
  124. .probe = x2apic_phys_probe,
  125. .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
  126. .apic_id_valid = x2apic_apic_id_valid,
  127. .apic_id_registered = x2apic_apic_id_registered,
  128. .irq_delivery_mode = dest_Fixed,
  129. .irq_dest_mode = 0, /* physical */
  130. .disable_esr = 0,
  131. .dest_logical = 0,
  132. .check_apicid_used = NULL,
  133. .init_apic_ldr = init_x2apic_ldr,
  134. .ioapic_phys_id_map = NULL,
  135. .setup_apic_routing = NULL,
  136. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  137. .apicid_to_cpu_present = NULL,
  138. .check_phys_apicid_present = default_check_phys_apicid_present,
  139. .phys_pkg_id = x2apic_phys_pkg_id,
  140. .get_apic_id = x2apic_get_apic_id,
  141. .set_apic_id = x2apic_set_apic_id,
  142. .calc_dest_apicid = apic_default_calc_apicid,
  143. .send_IPI = x2apic_send_IPI,
  144. .send_IPI_mask = x2apic_send_IPI_mask,
  145. .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
  146. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  147. .send_IPI_all = x2apic_send_IPI_all,
  148. .send_IPI_self = x2apic_send_IPI_self,
  149. .inquire_remote_apic = NULL,
  150. .read = native_apic_msr_read,
  151. .write = native_apic_msr_write,
  152. .eoi_write = native_apic_msr_eoi_write,
  153. .icr_read = native_x2apic_icr_read,
  154. .icr_write = native_x2apic_icr_write,
  155. .wait_icr_idle = native_x2apic_wait_icr_idle,
  156. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  157. };
  158. apic_driver(apic_x2apic_phys);