x2apic_cluster.c 5.7 KB

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