x2apic_cluster.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <linux/threads.h>
  2. #include <linux/cpumask.h>
  3. #include <linux/string.h>
  4. #include <linux/kernel.h>
  5. #include <linux/ctype.h>
  6. #include <linux/dmar.h>
  7. #include <linux/cpu.h>
  8. #include <asm/smp.h>
  9. #include <asm/x2apic.h>
  10. static DEFINE_PER_CPU(u32, x86_cpu_to_logical_apicid);
  11. static DEFINE_PER_CPU(cpumask_var_t, cpus_in_cluster);
  12. static DEFINE_PER_CPU(cpumask_var_t, ipi_mask);
  13. static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  14. {
  15. return x2apic_enabled();
  16. }
  17. static inline u32 x2apic_cluster(int cpu)
  18. {
  19. return per_cpu(x86_cpu_to_logical_apicid, cpu) >> 16;
  20. }
  21. static void x2apic_send_IPI(int cpu, int vector)
  22. {
  23. u32 dest = per_cpu(x86_cpu_to_logical_apicid, cpu);
  24. x2apic_wrmsr_fence();
  25. __x2apic_send_IPI_dest(dest, vector, APIC_DEST_LOGICAL);
  26. }
  27. static void
  28. __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
  29. {
  30. struct cpumask *cpus_in_cluster_ptr;
  31. struct cpumask *ipi_mask_ptr;
  32. unsigned int cpu, this_cpu;
  33. unsigned long flags;
  34. u32 dest;
  35. x2apic_wrmsr_fence();
  36. local_irq_save(flags);
  37. this_cpu = smp_processor_id();
  38. /*
  39. * We are to modify mask, so we need an own copy
  40. * and be sure it's manipulated with irq off.
  41. */
  42. ipi_mask_ptr = this_cpu_cpumask_var_ptr(ipi_mask);
  43. cpumask_copy(ipi_mask_ptr, mask);
  44. /*
  45. * The idea is to send one IPI per cluster.
  46. */
  47. for_each_cpu(cpu, ipi_mask_ptr) {
  48. unsigned long i;
  49. cpus_in_cluster_ptr = per_cpu(cpus_in_cluster, cpu);
  50. dest = 0;
  51. /* Collect cpus in cluster. */
  52. for_each_cpu_and(i, ipi_mask_ptr, cpus_in_cluster_ptr) {
  53. if (apic_dest == APIC_DEST_ALLINC || i != this_cpu)
  54. dest |= per_cpu(x86_cpu_to_logical_apicid, i);
  55. }
  56. if (!dest)
  57. continue;
  58. __x2apic_send_IPI_dest(dest, vector, apic->dest_logical);
  59. /*
  60. * Cluster sibling cpus should be discared now so
  61. * we would not send IPI them second time.
  62. */
  63. cpumask_andnot(ipi_mask_ptr, ipi_mask_ptr, cpus_in_cluster_ptr);
  64. }
  65. local_irq_restore(flags);
  66. }
  67. static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
  68. {
  69. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
  70. }
  71. static void
  72. x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
  73. {
  74. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
  75. }
  76. static void x2apic_send_IPI_allbutself(int vector)
  77. {
  78. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
  79. }
  80. static void x2apic_send_IPI_all(int vector)
  81. {
  82. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
  83. }
  84. static int
  85. x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  86. const struct cpumask *andmask,
  87. unsigned int *apicid)
  88. {
  89. u32 dest = 0;
  90. u16 cluster;
  91. int i;
  92. for_each_cpu_and(i, cpumask, andmask) {
  93. if (!cpumask_test_cpu(i, cpu_online_mask))
  94. continue;
  95. dest = per_cpu(x86_cpu_to_logical_apicid, i);
  96. cluster = x2apic_cluster(i);
  97. break;
  98. }
  99. if (!dest)
  100. return -EINVAL;
  101. for_each_cpu_and(i, cpumask, andmask) {
  102. if (!cpumask_test_cpu(i, cpu_online_mask))
  103. continue;
  104. if (cluster != x2apic_cluster(i))
  105. continue;
  106. dest |= per_cpu(x86_cpu_to_logical_apicid, i);
  107. }
  108. *apicid = dest;
  109. return 0;
  110. }
  111. static void init_x2apic_ldr(void)
  112. {
  113. unsigned int this_cpu = smp_processor_id();
  114. unsigned int cpu;
  115. per_cpu(x86_cpu_to_logical_apicid, this_cpu) = apic_read(APIC_LDR);
  116. cpumask_set_cpu(this_cpu, per_cpu(cpus_in_cluster, this_cpu));
  117. for_each_online_cpu(cpu) {
  118. if (x2apic_cluster(this_cpu) != x2apic_cluster(cpu))
  119. continue;
  120. cpumask_set_cpu(this_cpu, per_cpu(cpus_in_cluster, cpu));
  121. cpumask_set_cpu(cpu, per_cpu(cpus_in_cluster, this_cpu));
  122. }
  123. }
  124. /*
  125. * At CPU state changes, update the x2apic cluster sibling info.
  126. */
  127. static int x2apic_prepare_cpu(unsigned int cpu)
  128. {
  129. if (!zalloc_cpumask_var(&per_cpu(cpus_in_cluster, cpu), GFP_KERNEL))
  130. return -ENOMEM;
  131. if (!zalloc_cpumask_var(&per_cpu(ipi_mask, cpu), GFP_KERNEL)) {
  132. free_cpumask_var(per_cpu(cpus_in_cluster, cpu));
  133. return -ENOMEM;
  134. }
  135. return 0;
  136. }
  137. static int x2apic_dead_cpu(unsigned int this_cpu)
  138. {
  139. int cpu;
  140. for_each_online_cpu(cpu) {
  141. if (x2apic_cluster(this_cpu) != x2apic_cluster(cpu))
  142. continue;
  143. cpumask_clear_cpu(this_cpu, per_cpu(cpus_in_cluster, cpu));
  144. cpumask_clear_cpu(cpu, per_cpu(cpus_in_cluster, this_cpu));
  145. }
  146. free_cpumask_var(per_cpu(cpus_in_cluster, this_cpu));
  147. free_cpumask_var(per_cpu(ipi_mask, this_cpu));
  148. return 0;
  149. }
  150. static int x2apic_cluster_probe(void)
  151. {
  152. int cpu = smp_processor_id();
  153. int ret;
  154. if (!x2apic_mode)
  155. return 0;
  156. ret = cpuhp_setup_state(CPUHP_X2APIC_PREPARE, "X2APIC_PREPARE",
  157. x2apic_prepare_cpu, x2apic_dead_cpu);
  158. if (ret < 0) {
  159. pr_err("Failed to register X2APIC_PREPARE\n");
  160. return 0;
  161. }
  162. cpumask_set_cpu(cpu, per_cpu(cpus_in_cluster, cpu));
  163. return 1;
  164. }
  165. static const struct cpumask *x2apic_cluster_target_cpus(void)
  166. {
  167. return cpu_all_mask;
  168. }
  169. /*
  170. * Each x2apic cluster is an allocation domain.
  171. */
  172. static void cluster_vector_allocation_domain(int cpu, struct cpumask *retmask,
  173. const struct cpumask *mask)
  174. {
  175. /*
  176. * To minimize vector pressure, default case of boot, device bringup
  177. * etc will use a single cpu for the interrupt destination.
  178. *
  179. * On explicit migration requests coming from irqbalance etc,
  180. * interrupts will be routed to the x2apic cluster (cluster-id
  181. * derived from the first cpu in the mask) members specified
  182. * in the mask.
  183. */
  184. if (mask == x2apic_cluster_target_cpus())
  185. cpumask_copy(retmask, cpumask_of(cpu));
  186. else
  187. cpumask_and(retmask, mask, per_cpu(cpus_in_cluster, cpu));
  188. }
  189. static struct apic apic_x2apic_cluster __ro_after_init = {
  190. .name = "cluster x2apic",
  191. .probe = x2apic_cluster_probe,
  192. .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
  193. .apic_id_valid = x2apic_apic_id_valid,
  194. .apic_id_registered = x2apic_apic_id_registered,
  195. .irq_delivery_mode = dest_LowestPrio,
  196. .irq_dest_mode = 1, /* logical */
  197. .target_cpus = x2apic_cluster_target_cpus,
  198. .disable_esr = 0,
  199. .dest_logical = APIC_DEST_LOGICAL,
  200. .check_apicid_used = NULL,
  201. .vector_allocation_domain = cluster_vector_allocation_domain,
  202. .init_apic_ldr = init_x2apic_ldr,
  203. .ioapic_phys_id_map = NULL,
  204. .setup_apic_routing = NULL,
  205. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  206. .apicid_to_cpu_present = NULL,
  207. .check_phys_apicid_present = default_check_phys_apicid_present,
  208. .phys_pkg_id = x2apic_phys_pkg_id,
  209. .get_apic_id = x2apic_get_apic_id,
  210. .set_apic_id = x2apic_set_apic_id,
  211. .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
  212. .send_IPI = x2apic_send_IPI,
  213. .send_IPI_mask = x2apic_send_IPI_mask,
  214. .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
  215. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  216. .send_IPI_all = x2apic_send_IPI_all,
  217. .send_IPI_self = x2apic_send_IPI_self,
  218. .inquire_remote_apic = NULL,
  219. .read = native_apic_msr_read,
  220. .write = native_apic_msr_write,
  221. .eoi_write = native_apic_msr_eoi_write,
  222. .icr_read = native_x2apic_icr_read,
  223. .icr_write = native_x2apic_icr_write,
  224. .wait_icr_idle = native_x2apic_wait_icr_idle,
  225. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  226. };
  227. apic_driver(apic_x2apic_cluster);