x2apic_cluster.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 int
  80. x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  81. const struct cpumask *andmask,
  82. unsigned int *apicid)
  83. {
  84. u32 dest = 0;
  85. u16 cluster;
  86. int i;
  87. for_each_cpu_and(i, cpumask, andmask) {
  88. if (!cpumask_test_cpu(i, cpu_online_mask))
  89. continue;
  90. dest = per_cpu(x86_cpu_to_logical_apicid, i);
  91. cluster = x2apic_cluster(i);
  92. break;
  93. }
  94. if (!dest)
  95. return -EINVAL;
  96. for_each_cpu_and(i, cpumask, andmask) {
  97. if (!cpumask_test_cpu(i, cpu_online_mask))
  98. continue;
  99. if (cluster != x2apic_cluster(i))
  100. continue;
  101. dest |= per_cpu(x86_cpu_to_logical_apicid, i);
  102. }
  103. *apicid = dest;
  104. return 0;
  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 const struct cpumask *x2apic_cluster_target_cpus(void)
  175. {
  176. return cpu_all_mask;
  177. }
  178. /*
  179. * Each x2apic cluster is an allocation domain.
  180. */
  181. static void cluster_vector_allocation_domain(int cpu, struct cpumask *retmask,
  182. const struct cpumask *mask)
  183. {
  184. /*
  185. * To minimize vector pressure, default case of boot, device bringup
  186. * etc will use a single cpu for the interrupt destination.
  187. *
  188. * On explicit migration requests coming from irqbalance etc,
  189. * interrupts will be routed to the x2apic cluster (cluster-id
  190. * derived from the first cpu in the mask) members specified
  191. * in the mask.
  192. */
  193. if (mask == x2apic_cluster_target_cpus())
  194. cpumask_copy(retmask, cpumask_of(cpu));
  195. else
  196. cpumask_and(retmask, mask, per_cpu(cpus_in_cluster, cpu));
  197. }
  198. static struct apic apic_x2apic_cluster = {
  199. .name = "cluster x2apic",
  200. .probe = x2apic_cluster_probe,
  201. .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
  202. .apic_id_valid = x2apic_apic_id_valid,
  203. .apic_id_registered = x2apic_apic_id_registered,
  204. .irq_delivery_mode = dest_LowestPrio,
  205. .irq_dest_mode = 1, /* logical */
  206. .target_cpus = x2apic_cluster_target_cpus,
  207. .disable_esr = 0,
  208. .dest_logical = APIC_DEST_LOGICAL,
  209. .check_apicid_used = NULL,
  210. .check_apicid_present = NULL,
  211. .vector_allocation_domain = cluster_vector_allocation_domain,
  212. .init_apic_ldr = init_x2apic_ldr,
  213. .ioapic_phys_id_map = NULL,
  214. .setup_apic_routing = NULL,
  215. .multi_timer_check = NULL,
  216. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  217. .apicid_to_cpu_present = NULL,
  218. .setup_portio_remap = NULL,
  219. .check_phys_apicid_present = default_check_phys_apicid_present,
  220. .enable_apic_mode = NULL,
  221. .phys_pkg_id = x2apic_phys_pkg_id,
  222. .mps_oem_check = NULL,
  223. .get_apic_id = x2apic_get_apic_id,
  224. .set_apic_id = x2apic_set_apic_id,
  225. .apic_id_mask = 0xFFFFFFFFu,
  226. .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
  227. .send_IPI_mask = x2apic_send_IPI_mask,
  228. .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
  229. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  230. .send_IPI_all = x2apic_send_IPI_all,
  231. .send_IPI_self = x2apic_send_IPI_self,
  232. .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
  233. .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
  234. .wait_for_init_deassert = NULL,
  235. .smp_callin_clear_local_apic = NULL,
  236. .inquire_remote_apic = NULL,
  237. .read = native_apic_msr_read,
  238. .write = native_apic_msr_write,
  239. .eoi_write = native_apic_msr_eoi_write,
  240. .icr_read = native_x2apic_icr_read,
  241. .icr_write = native_x2apic_icr_write,
  242. .wait_icr_idle = native_x2apic_wait_icr_idle,
  243. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  244. };
  245. apic_driver(apic_x2apic_cluster);