x2apic_cluster.c 7.2 KB

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