x2apic_cluster.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <asm/smp.h>
  9. #include <asm/apic.h>
  10. #include <asm/ipi.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 int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  14. {
  15. return x2apic_enabled();
  16. }
  17. /*
  18. * need to use more than cpu 0, because we need more vectors when
  19. * MSI-X are used.
  20. */
  21. static const struct cpumask *x2apic_target_cpus(void)
  22. {
  23. return cpu_online_mask;
  24. }
  25. /*
  26. * for now each logical cpu is in its own vector allocation domain.
  27. */
  28. static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask)
  29. {
  30. cpumask_clear(retmask);
  31. cpumask_set_cpu(cpu, retmask);
  32. }
  33. static void
  34. __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest)
  35. {
  36. unsigned long cfg;
  37. cfg = __prepare_ICR(0, vector, dest);
  38. /*
  39. * send the IPI.
  40. */
  41. native_x2apic_icr_write(cfg, apicid);
  42. }
  43. static inline u32 x2apic_cluster(int cpu)
  44. {
  45. return per_cpu(x86_cpu_to_logical_apicid, cpu) >> 16;
  46. }
  47. /*
  48. * for now, we send the IPI's one by one in the cpumask.
  49. * TBD: Based on the cpu mask, we can send the IPI's to the cluster group
  50. * at once. We have 16 cpu's in a cluster. This will minimize IPI register
  51. * writes.
  52. */
  53. static void
  54. __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
  55. {
  56. unsigned long query_cpu;
  57. unsigned long this_cpu;
  58. unsigned long flags;
  59. x2apic_wrmsr_fence();
  60. local_irq_save(flags);
  61. this_cpu = smp_processor_id();
  62. for_each_cpu(query_cpu, mask) {
  63. if (apic_dest == APIC_DEST_ALLBUT && query_cpu == this_cpu)
  64. continue;
  65. __x2apic_send_IPI_dest(
  66. per_cpu(x86_cpu_to_logical_apicid, query_cpu),
  67. vector, apic->dest_logical);
  68. }
  69. local_irq_restore(flags);
  70. }
  71. static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
  72. {
  73. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
  74. }
  75. static void
  76. x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
  77. {
  78. __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
  79. }
  80. static void x2apic_send_IPI_allbutself(int vector)
  81. {
  82. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
  83. }
  84. static void x2apic_send_IPI_all(int vector)
  85. {
  86. __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
  87. }
  88. static int x2apic_apic_id_registered(void)
  89. {
  90. return 1;
  91. }
  92. static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask)
  93. {
  94. /*
  95. * We're using fixed IRQ delivery, can only return one logical APIC ID.
  96. * May as well be the first.
  97. */
  98. int cpu = cpumask_first(cpumask);
  99. if ((unsigned)cpu < nr_cpu_ids)
  100. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  101. else
  102. return BAD_APICID;
  103. }
  104. static unsigned int
  105. x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  106. const struct cpumask *andmask)
  107. {
  108. int cpu;
  109. /*
  110. * We're using fixed IRQ delivery, can only return one logical APIC ID.
  111. * May as well be the first.
  112. */
  113. for_each_cpu_and(cpu, cpumask, andmask) {
  114. if (cpumask_test_cpu(cpu, cpu_online_mask))
  115. break;
  116. }
  117. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  118. }
  119. static unsigned int x2apic_cluster_phys_get_apic_id(unsigned long x)
  120. {
  121. unsigned int id;
  122. id = x;
  123. return id;
  124. }
  125. static unsigned long set_apic_id(unsigned int id)
  126. {
  127. unsigned long x;
  128. x = id;
  129. return x;
  130. }
  131. static int x2apic_cluster_phys_pkg_id(int initial_apicid, int index_msb)
  132. {
  133. return initial_apicid >> index_msb;
  134. }
  135. static void x2apic_send_IPI_self(int vector)
  136. {
  137. apic_write(APIC_SELF_IPI, vector);
  138. }
  139. static void init_x2apic_ldr(void)
  140. {
  141. unsigned int this_cpu = smp_processor_id();
  142. unsigned int cpu;
  143. per_cpu(x86_cpu_to_logical_apicid, this_cpu) = apic_read(APIC_LDR);
  144. __cpu_set(this_cpu, per_cpu(cpus_in_cluster, this_cpu));
  145. for_each_online_cpu(cpu) {
  146. if (x2apic_cluster(this_cpu) != x2apic_cluster(cpu))
  147. continue;
  148. __cpu_set(this_cpu, per_cpu(cpus_in_cluster, cpu));
  149. __cpu_set(cpu, per_cpu(cpus_in_cluster, this_cpu));
  150. }
  151. }
  152. /*
  153. * At CPU state changes, update the x2apic cluster sibling info.
  154. */
  155. static int __cpuinit
  156. update_clusterinfo(struct notifier_block *nfb, unsigned long action, void *hcpu)
  157. {
  158. unsigned int this_cpu = (unsigned long)hcpu;
  159. unsigned int cpu;
  160. int err = 0;
  161. switch (action) {
  162. case CPU_UP_PREPARE:
  163. if (!zalloc_cpumask_var(&per_cpu(cpus_in_cluster, this_cpu),
  164. GFP_KERNEL)) {
  165. err = -ENOMEM;
  166. }
  167. break;
  168. case CPU_UP_CANCELED:
  169. case CPU_UP_CANCELED_FROZEN:
  170. case CPU_DEAD:
  171. for_each_online_cpu(cpu) {
  172. if (x2apic_cluster(this_cpu) != x2apic_cluster(cpu))
  173. continue;
  174. __cpu_clear(this_cpu, per_cpu(cpus_in_cluster, cpu));
  175. __cpu_clear(cpu, per_cpu(cpus_in_cluster, this_cpu));
  176. }
  177. free_cpumask_var(per_cpu(cpus_in_cluster, this_cpu));
  178. break;
  179. }
  180. return notifier_from_errno(err);
  181. }
  182. static struct notifier_block __refdata x2apic_cpu_notifier = {
  183. .notifier_call = update_clusterinfo,
  184. };
  185. static int x2apic_init_cpu_notifier(void)
  186. {
  187. int cpu = smp_processor_id();
  188. zalloc_cpumask_var(&per_cpu(cpus_in_cluster, cpu), GFP_KERNEL);
  189. BUG_ON(!per_cpu(cpus_in_cluster, cpu));
  190. __cpu_set(cpu, per_cpu(cpus_in_cluster, cpu));
  191. register_hotcpu_notifier(&x2apic_cpu_notifier);
  192. return 1;
  193. }
  194. static int x2apic_cluster_probe(void)
  195. {
  196. if (x2apic_mode)
  197. return x2apic_init_cpu_notifier();
  198. else
  199. return 0;
  200. }
  201. struct apic apic_x2apic_cluster = {
  202. .name = "cluster x2apic",
  203. .probe = x2apic_cluster_probe,
  204. .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
  205. .apic_id_registered = x2apic_apic_id_registered,
  206. .irq_delivery_mode = dest_LowestPrio,
  207. .irq_dest_mode = 1, /* logical */
  208. .target_cpus = x2apic_target_cpus,
  209. .disable_esr = 0,
  210. .dest_logical = APIC_DEST_LOGICAL,
  211. .check_apicid_used = NULL,
  212. .check_apicid_present = NULL,
  213. .vector_allocation_domain = x2apic_vector_allocation_domain,
  214. .init_apic_ldr = init_x2apic_ldr,
  215. .ioapic_phys_id_map = NULL,
  216. .setup_apic_routing = NULL,
  217. .multi_timer_check = NULL,
  218. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  219. .apicid_to_cpu_present = NULL,
  220. .setup_portio_remap = NULL,
  221. .check_phys_apicid_present = default_check_phys_apicid_present,
  222. .enable_apic_mode = NULL,
  223. .phys_pkg_id = x2apic_cluster_phys_pkg_id,
  224. .mps_oem_check = NULL,
  225. .get_apic_id = x2apic_cluster_phys_get_apic_id,
  226. .set_apic_id = set_apic_id,
  227. .apic_id_mask = 0xFFFFFFFFu,
  228. .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid,
  229. .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
  230. .send_IPI_mask = x2apic_send_IPI_mask,
  231. .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
  232. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  233. .send_IPI_all = x2apic_send_IPI_all,
  234. .send_IPI_self = x2apic_send_IPI_self,
  235. .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
  236. .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
  237. .wait_for_init_deassert = NULL,
  238. .smp_callin_clear_local_apic = NULL,
  239. .inquire_remote_apic = NULL,
  240. .read = native_apic_msr_read,
  241. .write = native_apic_msr_write,
  242. .icr_read = native_x2apic_icr_read,
  243. .icr_write = native_x2apic_icr_write,
  244. .wait_icr_idle = native_x2apic_wait_icr_idle,
  245. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  246. };