apic_flat_64.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2004 James Cleverdon, IBM.
  3. * Subject to the GNU Public License, v.2
  4. *
  5. * Flat APIC subarch code.
  6. *
  7. * Hacked for x86-64 by James Cleverdon from i386 architecture code by
  8. * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
  9. * James Cleverdon.
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/threads.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/string.h>
  15. #include <linux/kernel.h>
  16. #include <linux/ctype.h>
  17. #include <linux/hardirq.h>
  18. #include <linux/export.h>
  19. #include <asm/smp.h>
  20. #include <asm/apic.h>
  21. #include <asm/ipi.h>
  22. #include <linux/acpi.h>
  23. static struct apic apic_physflat;
  24. static struct apic apic_flat;
  25. struct apic *apic __ro_after_init = &apic_flat;
  26. EXPORT_SYMBOL_GPL(apic);
  27. static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  28. {
  29. return 1;
  30. }
  31. /*
  32. * Set up the logical destination ID.
  33. *
  34. * Intel recommends to set DFR, LDR and TPR before enabling
  35. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  36. * document number 292116). So here it goes...
  37. */
  38. void flat_init_apic_ldr(void)
  39. {
  40. unsigned long val;
  41. unsigned long num, id;
  42. num = smp_processor_id();
  43. id = 1UL << num;
  44. apic_write(APIC_DFR, APIC_DFR_FLAT);
  45. val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
  46. val |= SET_APIC_LOGICAL_ID(id);
  47. apic_write(APIC_LDR, val);
  48. }
  49. static void _flat_send_IPI_mask(unsigned long mask, int vector)
  50. {
  51. unsigned long flags;
  52. local_irq_save(flags);
  53. __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
  54. local_irq_restore(flags);
  55. }
  56. static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
  57. {
  58. unsigned long mask = cpumask_bits(cpumask)[0];
  59. _flat_send_IPI_mask(mask, vector);
  60. }
  61. static void
  62. flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
  63. {
  64. unsigned long mask = cpumask_bits(cpumask)[0];
  65. int cpu = smp_processor_id();
  66. if (cpu < BITS_PER_LONG)
  67. clear_bit(cpu, &mask);
  68. _flat_send_IPI_mask(mask, vector);
  69. }
  70. static void flat_send_IPI_allbutself(int vector)
  71. {
  72. int cpu = smp_processor_id();
  73. #ifdef CONFIG_HOTPLUG_CPU
  74. int hotplug = 1;
  75. #else
  76. int hotplug = 0;
  77. #endif
  78. if (hotplug || vector == NMI_VECTOR) {
  79. if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
  80. unsigned long mask = cpumask_bits(cpu_online_mask)[0];
  81. if (cpu < BITS_PER_LONG)
  82. clear_bit(cpu, &mask);
  83. _flat_send_IPI_mask(mask, vector);
  84. }
  85. } else if (num_online_cpus() > 1) {
  86. __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
  87. vector, apic->dest_logical);
  88. }
  89. }
  90. static void flat_send_IPI_all(int vector)
  91. {
  92. if (vector == NMI_VECTOR) {
  93. flat_send_IPI_mask(cpu_online_mask, vector);
  94. } else {
  95. __default_send_IPI_shortcut(APIC_DEST_ALLINC,
  96. vector, apic->dest_logical);
  97. }
  98. }
  99. static unsigned int flat_get_apic_id(unsigned long x)
  100. {
  101. return (x >> 24) & 0xFF;
  102. }
  103. static unsigned long set_apic_id(unsigned int id)
  104. {
  105. return (id & 0xFF) << 24;
  106. }
  107. static unsigned int read_xapic_id(void)
  108. {
  109. return flat_get_apic_id(apic_read(APIC_ID));
  110. }
  111. static int flat_apic_id_registered(void)
  112. {
  113. return physid_isset(read_xapic_id(), phys_cpu_present_map);
  114. }
  115. static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
  116. {
  117. return initial_apic_id >> index_msb;
  118. }
  119. static int flat_probe(void)
  120. {
  121. return 1;
  122. }
  123. static struct apic apic_flat __ro_after_init = {
  124. .name = "flat",
  125. .probe = flat_probe,
  126. .acpi_madt_oem_check = flat_acpi_madt_oem_check,
  127. .apic_id_valid = default_apic_id_valid,
  128. .apic_id_registered = flat_apic_id_registered,
  129. .irq_delivery_mode = dest_LowestPrio,
  130. .irq_dest_mode = 1, /* logical */
  131. .target_cpus = online_target_cpus,
  132. .disable_esr = 0,
  133. .dest_logical = APIC_DEST_LOGICAL,
  134. .check_apicid_used = NULL,
  135. .vector_allocation_domain = flat_vector_allocation_domain,
  136. .init_apic_ldr = flat_init_apic_ldr,
  137. .ioapic_phys_id_map = NULL,
  138. .setup_apic_routing = NULL,
  139. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  140. .apicid_to_cpu_present = NULL,
  141. .check_phys_apicid_present = default_check_phys_apicid_present,
  142. .phys_pkg_id = flat_phys_pkg_id,
  143. .get_apic_id = flat_get_apic_id,
  144. .set_apic_id = set_apic_id,
  145. .cpu_mask_to_apicid_and = flat_cpu_mask_to_apicid_and,
  146. .send_IPI = default_send_IPI_single,
  147. .send_IPI_mask = flat_send_IPI_mask,
  148. .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
  149. .send_IPI_allbutself = flat_send_IPI_allbutself,
  150. .send_IPI_all = flat_send_IPI_all,
  151. .send_IPI_self = apic_send_IPI_self,
  152. .inquire_remote_apic = default_inquire_remote_apic,
  153. .read = native_apic_mem_read,
  154. .write = native_apic_mem_write,
  155. .eoi_write = native_apic_mem_write,
  156. .icr_read = native_apic_icr_read,
  157. .icr_write = native_apic_icr_write,
  158. .wait_icr_idle = native_apic_wait_icr_idle,
  159. .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
  160. };
  161. /*
  162. * Physflat mode is used when there are more than 8 CPUs on a system.
  163. * We cannot use logical delivery in this case because the mask
  164. * overflows, so use physical mode.
  165. */
  166. static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  167. {
  168. #ifdef CONFIG_ACPI
  169. /*
  170. * Quirk: some x86_64 machines can only use physical APIC mode
  171. * regardless of how many processors are present (x86_64 ES7000
  172. * is an example).
  173. */
  174. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  175. (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
  176. printk(KERN_DEBUG "system APIC only can use physical flat");
  177. return 1;
  178. }
  179. if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
  180. printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
  181. return 1;
  182. }
  183. #endif
  184. return 0;
  185. }
  186. static void physflat_send_IPI_allbutself(int vector)
  187. {
  188. default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
  189. }
  190. static void physflat_send_IPI_all(int vector)
  191. {
  192. default_send_IPI_mask_sequence_phys(cpu_online_mask, vector);
  193. }
  194. static int physflat_probe(void)
  195. {
  196. if (apic == &apic_physflat || num_possible_cpus() > 8)
  197. return 1;
  198. return 0;
  199. }
  200. static struct apic apic_physflat __ro_after_init = {
  201. .name = "physical flat",
  202. .probe = physflat_probe,
  203. .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
  204. .apic_id_valid = default_apic_id_valid,
  205. .apic_id_registered = flat_apic_id_registered,
  206. .irq_delivery_mode = dest_Fixed,
  207. .irq_dest_mode = 0, /* physical */
  208. .target_cpus = online_target_cpus,
  209. .disable_esr = 0,
  210. .dest_logical = 0,
  211. .check_apicid_used = NULL,
  212. .vector_allocation_domain = default_vector_allocation_domain,
  213. /* not needed, but shouldn't hurt: */
  214. .init_apic_ldr = flat_init_apic_ldr,
  215. .ioapic_phys_id_map = NULL,
  216. .setup_apic_routing = NULL,
  217. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  218. .apicid_to_cpu_present = NULL,
  219. .check_phys_apicid_present = default_check_phys_apicid_present,
  220. .phys_pkg_id = flat_phys_pkg_id,
  221. .get_apic_id = flat_get_apic_id,
  222. .set_apic_id = set_apic_id,
  223. .cpu_mask_to_apicid_and = default_cpu_mask_to_apicid_and,
  224. .send_IPI = default_send_IPI_single_phys,
  225. .send_IPI_mask = default_send_IPI_mask_sequence_phys,
  226. .send_IPI_mask_allbutself = default_send_IPI_mask_allbutself_phys,
  227. .send_IPI_allbutself = physflat_send_IPI_allbutself,
  228. .send_IPI_all = physflat_send_IPI_all,
  229. .send_IPI_self = apic_send_IPI_self,
  230. .inquire_remote_apic = default_inquire_remote_apic,
  231. .read = native_apic_mem_read,
  232. .write = native_apic_mem_write,
  233. .eoi_write = native_apic_mem_write,
  234. .icr_read = native_apic_icr_read,
  235. .icr_write = native_apic_icr_write,
  236. .wait_icr_idle = native_apic_wait_icr_idle,
  237. .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
  238. };
  239. /*
  240. * We need to check for physflat first, so this order is important.
  241. */
  242. apic_drivers(apic_physflat, apic_flat);