x2apic_cluster.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  13. {
  14. return x2apic_enabled();
  15. }
  16. /*
  17. * need to use more than cpu 0, because we need more vectors when
  18. * MSI-X are used.
  19. */
  20. static const struct cpumask *x2apic_target_cpus(void)
  21. {
  22. return cpu_online_mask;
  23. }
  24. /*
  25. * for now each logical cpu is in its own vector allocation domain.
  26. */
  27. static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask)
  28. {
  29. cpumask_clear(retmask);
  30. cpumask_set_cpu(cpu, retmask);
  31. }
  32. static void
  33. __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest)
  34. {
  35. unsigned long cfg;
  36. cfg = __prepare_ICR(0, vector, dest);
  37. /*
  38. * send the IPI.
  39. */
  40. native_x2apic_icr_write(cfg, apicid);
  41. }
  42. /*
  43. * for now, we send the IPI's one by one in the cpumask.
  44. * TBD: Based on the cpu mask, we can send the IPI's to the cluster group
  45. * at once. We have 16 cpu's in a cluster. This will minimize IPI register
  46. * writes.
  47. */
  48. static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
  49. {
  50. unsigned long query_cpu;
  51. unsigned long flags;
  52. x2apic_wrmsr_fence();
  53. local_irq_save(flags);
  54. for_each_cpu(query_cpu, mask) {
  55. __x2apic_send_IPI_dest(
  56. per_cpu(x86_cpu_to_logical_apicid, query_cpu),
  57. vector, apic->dest_logical);
  58. }
  59. local_irq_restore(flags);
  60. }
  61. static void
  62. x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
  63. {
  64. unsigned long this_cpu = smp_processor_id();
  65. unsigned long query_cpu;
  66. unsigned long flags;
  67. x2apic_wrmsr_fence();
  68. local_irq_save(flags);
  69. for_each_cpu(query_cpu, mask) {
  70. if (query_cpu == this_cpu)
  71. continue;
  72. __x2apic_send_IPI_dest(
  73. per_cpu(x86_cpu_to_logical_apicid, query_cpu),
  74. vector, apic->dest_logical);
  75. }
  76. local_irq_restore(flags);
  77. }
  78. static void x2apic_send_IPI_allbutself(int vector)
  79. {
  80. unsigned long this_cpu = smp_processor_id();
  81. unsigned long query_cpu;
  82. unsigned long flags;
  83. x2apic_wrmsr_fence();
  84. local_irq_save(flags);
  85. for_each_online_cpu(query_cpu) {
  86. if (query_cpu == this_cpu)
  87. continue;
  88. __x2apic_send_IPI_dest(
  89. per_cpu(x86_cpu_to_logical_apicid, query_cpu),
  90. vector, apic->dest_logical);
  91. }
  92. local_irq_restore(flags);
  93. }
  94. static void x2apic_send_IPI_all(int vector)
  95. {
  96. x2apic_send_IPI_mask(cpu_online_mask, vector);
  97. }
  98. static int x2apic_apic_id_registered(void)
  99. {
  100. return 1;
  101. }
  102. static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask)
  103. {
  104. /*
  105. * We're using fixed IRQ delivery, can only return one logical APIC ID.
  106. * May as well be the first.
  107. */
  108. int cpu = cpumask_first(cpumask);
  109. if ((unsigned)cpu < nr_cpu_ids)
  110. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  111. else
  112. return BAD_APICID;
  113. }
  114. static unsigned int
  115. x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  116. const struct cpumask *andmask)
  117. {
  118. int cpu;
  119. /*
  120. * We're using fixed IRQ delivery, can only return one logical APIC ID.
  121. * May as well be the first.
  122. */
  123. for_each_cpu_and(cpu, cpumask, andmask) {
  124. if (cpumask_test_cpu(cpu, cpu_online_mask))
  125. break;
  126. }
  127. if (cpu < nr_cpu_ids)
  128. return per_cpu(x86_cpu_to_logical_apicid, cpu);
  129. return BAD_APICID;
  130. }
  131. static unsigned int x2apic_cluster_phys_get_apic_id(unsigned long x)
  132. {
  133. unsigned int id;
  134. id = x;
  135. return id;
  136. }
  137. static unsigned long set_apic_id(unsigned int id)
  138. {
  139. unsigned long x;
  140. x = id;
  141. return x;
  142. }
  143. static int x2apic_cluster_phys_pkg_id(int initial_apicid, int index_msb)
  144. {
  145. return initial_apicid >> index_msb;
  146. }
  147. static void x2apic_send_IPI_self(int vector)
  148. {
  149. apic_write(APIC_SELF_IPI, vector);
  150. }
  151. static void init_x2apic_ldr(void)
  152. {
  153. int cpu = smp_processor_id();
  154. per_cpu(x86_cpu_to_logical_apicid, cpu) = apic_read(APIC_LDR);
  155. }
  156. struct apic apic_x2apic_cluster = {
  157. .name = "cluster x2apic",
  158. .probe = NULL,
  159. .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
  160. .apic_id_registered = x2apic_apic_id_registered,
  161. .irq_delivery_mode = dest_LowestPrio,
  162. .irq_dest_mode = 1, /* logical */
  163. .target_cpus = x2apic_target_cpus,
  164. .disable_esr = 0,
  165. .dest_logical = APIC_DEST_LOGICAL,
  166. .check_apicid_used = NULL,
  167. .check_apicid_present = NULL,
  168. .vector_allocation_domain = x2apic_vector_allocation_domain,
  169. .init_apic_ldr = init_x2apic_ldr,
  170. .ioapic_phys_id_map = NULL,
  171. .setup_apic_routing = NULL,
  172. .multi_timer_check = NULL,
  173. .apicid_to_node = NULL,
  174. .cpu_to_logical_apicid = NULL,
  175. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  176. .apicid_to_cpu_present = NULL,
  177. .setup_portio_remap = NULL,
  178. .check_phys_apicid_present = default_check_phys_apicid_present,
  179. .enable_apic_mode = NULL,
  180. .phys_pkg_id = x2apic_cluster_phys_pkg_id,
  181. .mps_oem_check = NULL,
  182. .get_apic_id = x2apic_cluster_phys_get_apic_id,
  183. .set_apic_id = set_apic_id,
  184. .apic_id_mask = 0xFFFFFFFFu,
  185. .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid,
  186. .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
  187. .send_IPI_mask = x2apic_send_IPI_mask,
  188. .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
  189. .send_IPI_allbutself = x2apic_send_IPI_allbutself,
  190. .send_IPI_all = x2apic_send_IPI_all,
  191. .send_IPI_self = x2apic_send_IPI_self,
  192. .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
  193. .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
  194. .wait_for_init_deassert = NULL,
  195. .smp_callin_clear_local_apic = NULL,
  196. .inquire_remote_apic = NULL,
  197. .read = native_apic_msr_read,
  198. .write = native_apic_msr_write,
  199. .icr_read = native_x2apic_icr_read,
  200. .icr_write = native_x2apic_icr_write,
  201. .wait_icr_idle = native_x2apic_wait_icr_idle,
  202. .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
  203. };