x2apic_cluster.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/init.h>
  7. #include <linux/dmar.h>
  8. #include <linux/cpu.h>
  9. #include <asm/smp.h>
  10. #include <asm/x2apic.h>
  11. static DEFINE_PER_CPU(u32, x86_cpu_to_logical_apicid);
  12. static DEFINE_PER_CPU(cpumask_var_t, cpus_in_cluster);
  13. static DEFINE_PER_CPU(cpumask_var_t, ipi_mask);
  14. static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  15. {
  16. return x2apic_enabled();
  17. }
  18. static inline u32 x2apic_cluster(int cpu)
  19. {
  20. return per_cpu(x86_cpu_to_logical_apicid, cpu) >> 16;
  21. }
  22. static void
  23. __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
  24. {
  25. struct cpumask *cpus_in_cluster_ptr;
  26. struct cpumask *ipi_mask_ptr;
  27. unsigned int cpu, this_cpu;
  28. unsigned long flags;
  29. u32 dest;
  30. x2apic_wrmsr_fence();
  31. local_irq_save(flags);
  32. this_cpu = smp_processor_id();
  33. /*
  34. * We are to modify mask, so we need an own copy
  35. * and be sure it's manipulated with irq off.
  36. */
  37. ipi_mask_ptr = __raw_get_cpu_var(ipi_mask);
  38. cpumask_copy(ipi_mask_ptr, mask);
  39. /*
  40. * The idea is to send one IPI per cluster.
  41. */
  42. for_each_cpu(cpu, ipi_mask_ptr) {
  43. unsigned long i;
  44. cpus_in_cluster_ptr = per_cpu(cpus_in_cluster, cpu);
  45. dest = 0;
  46. /* Collect cpus in cluster. */
  47. for_each_cpu_and(i, ipi_mask_ptr, cpus_in_cluster_ptr) {
  48. if (apic_dest == APIC_DEST_ALLINC || i != this_cpu)
  49. dest |= per_cpu(x86_cpu_to_logical_apicid, i);
  50. }
  51. if (!dest)
  52. continue;
  53. __x2apic_send_IPI_dest(dest, vector, apic->dest_logical);
  54. /*
  55. * Cluster sibling cpus should be discared now so
  56. * we would not send IPI them second time.
  57. */
  58. cpumask_andnot(ipi_mask_ptr, ipi_mask_ptr, cpus_in_cluster_ptr);
  59. }
  60. local_irq_restore(flags);
  61. }
  62. static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
  63. {
  64. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
  65. }
  66. static void
  67. x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
  68. {
  69. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
  70. }
  71. static void x2apic_send_IPI_allbutself(int vector)
  72. {
  73. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
  74. }
  75. static void x2apic_send_IPI_all(int vector)
  76. {
  77. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
  78. }
  79. static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask)
  80. {
  81. /*
  82. * We're using fixed IRQ delivery, can only return one logical APIC ID.
  83. * May as well be the first.
  84. */
  85. int cpu = cpumask_first(cpumask);
  86. if ((unsigned)cpu < nr_cpu_ids)
  87. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  88. else
  89. return BAD_APICID;
  90. }
  91. static unsigned int
  92. x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  93. const struct cpumask *andmask)
  94. {
  95. int cpu;
  96. /*
  97. * We're using fixed IRQ delivery, can only return one logical APIC ID.
  98. * May as well be the first.
  99. */
  100. for_each_cpu_and(cpu, cpumask, andmask) {
  101. if (cpumask_test_cpu(cpu, cpu_online_mask))
  102. break;
  103. }
  104. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  105. }
  106. static void init_x2apic_ldr(void)
  107. {
  108. unsigned int this_cpu = smp_processor_id();
  109. unsigned int cpu;
  110. per_cpu(x86_cpu_to_logical_apicid, this_cpu) = apic_read(APIC_LDR);
  111. __cpu_set(this_cpu, per_cpu(cpus_in_cluster, this_cpu));
  112. for_each_online_cpu(cpu) {
  113. if (x2apic_cluster(this_cpu) != x2apic_cluster(cpu))
  114. continue;
  115. __cpu_set(this_cpu, per_cpu(cpus_in_cluster, cpu));
  116. __cpu_set(cpu, per_cpu(cpus_in_cluster, this_cpu));
  117. }
  118. }
  119. /*
  120. * At CPU state changes, update the x2apic cluster sibling info.
  121. */
  122. static int __cpuinit
  123. update_clusterinfo(struct notifier_block *nfb, unsigned long action, void *hcpu)
  124. {
  125. unsigned int this_cpu = (unsigned long)hcpu;
  126. unsigned int cpu;
  127. int err = 0;
  128. switch (action) {
  129. case CPU_UP_PREPARE:
  130. if (!zalloc_cpumask_var(&per_cpu(cpus_in_cluster, this_cpu),
  131. GFP_KERNEL)) {
  132. err = -ENOMEM;
  133. } else if (!zalloc_cpumask_var(&per_cpu(ipi_mask, this_cpu),
  134. GFP_KERNEL)) {
  135. free_cpumask_var(per_cpu(cpus_in_cluster, this_cpu));
  136. err = -ENOMEM;
  137. }
  138. break;
  139. case CPU_UP_CANCELED:
  140. case CPU_UP_CANCELED_FROZEN:
  141. case CPU_DEAD:
  142. for_each_online_cpu(cpu) {
  143. if (x2apic_cluster(this_cpu) != x2apic_cluster(cpu))
  144. continue;
  145. __cpu_clear(this_cpu, per_cpu(cpus_in_cluster, cpu));
  146. __cpu_clear(cpu, per_cpu(cpus_in_cluster, this_cpu));
  147. }
  148. free_cpumask_var(per_cpu(cpus_in_cluster, this_cpu));
  149. free_cpumask_var(per_cpu(ipi_mask, this_cpu));
  150. break;
  151. }
  152. return notifier_from_errno(err);
  153. }
  154. static struct notifier_block __refdata x2apic_cpu_notifier = {
  155. .notifier_call = update_clusterinfo,
  156. };
  157. static int x2apic_init_cpu_notifier(void)
  158. {
  159. int cpu = smp_processor_id();
  160. zalloc_cpumask_var(&per_cpu(cpus_in_cluster, cpu), GFP_KERNEL);
  161. zalloc_cpumask_var(&per_cpu(ipi_mask, cpu), GFP_KERNEL);
  162. BUG_ON(!per_cpu(cpus_in_cluster, cpu) || !per_cpu(ipi_mask, cpu));
  163. __cpu_set(cpu, per_cpu(cpus_in_cluster, cpu));
  164. register_hotcpu_notifier(&x2apic_cpu_notifier);
  165. return 1;
  166. }
  167. static int x2apic_cluster_probe(void)
  168. {
  169. if (x2apic_mode)
  170. return x2apic_init_cpu_notifier();
  171. else
  172. return 0;
  173. }
  174. static struct apic apic_x2apic_cluster = {
  175. .name = "cluster x2apic",
  176. .probe = x2apic_cluster_probe,
  177. .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
  178. .apic_id_valid = x2apic_apic_id_valid,
  179. .apic_id_registered = x2apic_apic_id_registered,
  180. .irq_delivery_mode = dest_LowestPrio,
  181. .irq_dest_mode = 1, /* logical */
  182. .target_cpus = x2apic_target_cpus,
  183. .disable_esr = 0,
  184. .dest_logical = APIC_DEST_LOGICAL,
  185. .check_apicid_used = NULL,
  186. .check_apicid_present = NULL,
  187. .vector_allocation_domain = x2apic_vector_allocation_domain,
  188. .init_apic_ldr = init_x2apic_ldr,
  189. .ioapic_phys_id_map = NULL,
  190. .setup_apic_routing = NULL,
  191. .multi_timer_check = NULL,
  192. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  193. .apicid_to_cpu_present = NULL,
  194. .setup_portio_remap = NULL,
  195. .check_phys_apicid_present = default_check_phys_apicid_present,
  196. .enable_apic_mode = NULL,
  197. .phys_pkg_id = x2apic_phys_pkg_id,
  198. .mps_oem_check = NULL,
  199. .get_apic_id = x2apic_get_apic_id,
  200. .set_apic_id = x2apic_set_apic_id,
  201. .apic_id_mask = 0xFFFFFFFFu,
  202. .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid,
  203. .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
  204. .send_IPI_mask = x2apic_send_IPI_mask,
  205. .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
  206. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  207. .send_IPI_all = x2apic_send_IPI_all,
  208. .send_IPI_self = x2apic_send_IPI_self,
  209. .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
  210. .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
  211. .wait_for_init_deassert = NULL,
  212. .smp_callin_clear_local_apic = NULL,
  213. .inquire_remote_apic = NULL,
  214. .read = native_apic_msr_read,
  215. .write = native_apic_msr_write,
  216. .icr_read = native_x2apic_icr_read,
  217. .icr_write = native_x2apic_icr_write,
  218. .wait_icr_idle = native_x2apic_wait_icr_idle,
  219. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  220. };
  221. apic_driver(apic_x2apic_cluster);