apic_flat_64.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/init.h>
  18. #include <linux/hardirq.h>
  19. #include <linux/module.h>
  20. #include <asm/smp.h>
  21. #include <asm/apic.h>
  22. #include <asm/ipi.h>
  23. #ifdef CONFIG_ACPI
  24. #include <acpi/acpi_bus.h>
  25. #endif
  26. static struct apic apic_physflat;
  27. static struct apic apic_flat;
  28. struct apic __read_mostly *apic = &apic_flat;
  29. EXPORT_SYMBOL_GPL(apic);
  30. static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  31. {
  32. return 1;
  33. }
  34. static const struct cpumask *flat_target_cpus(void)
  35. {
  36. return cpu_online_mask;
  37. }
  38. static void flat_vector_allocation_domain(int cpu, struct cpumask *retmask)
  39. {
  40. /* Careful. Some cpus do not strictly honor the set of cpus
  41. * specified in the interrupt destination when using lowest
  42. * priority interrupt delivery mode.
  43. *
  44. * In particular there was a hyperthreading cpu observed to
  45. * deliver interrupts to the wrong hyperthread when only one
  46. * hyperthread was specified in the interrupt desitination.
  47. */
  48. cpumask_clear(retmask);
  49. cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
  50. }
  51. /*
  52. * Set up the logical destination ID.
  53. *
  54. * Intel recommends to set DFR, LDR and TPR before enabling
  55. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  56. * document number 292116). So here it goes...
  57. */
  58. static void flat_init_apic_ldr(void)
  59. {
  60. unsigned long val;
  61. unsigned long num, id;
  62. num = smp_processor_id();
  63. id = 1UL << num;
  64. apic_write(APIC_DFR, APIC_DFR_FLAT);
  65. val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
  66. val |= SET_APIC_LOGICAL_ID(id);
  67. apic_write(APIC_LDR, val);
  68. }
  69. static inline void _flat_send_IPI_mask(unsigned long mask, int vector)
  70. {
  71. unsigned long flags;
  72. local_irq_save(flags);
  73. __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
  74. local_irq_restore(flags);
  75. }
  76. static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
  77. {
  78. unsigned long mask = cpumask_bits(cpumask)[0];
  79. _flat_send_IPI_mask(mask, vector);
  80. }
  81. static void
  82. flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
  83. {
  84. unsigned long mask = cpumask_bits(cpumask)[0];
  85. int cpu = smp_processor_id();
  86. if (cpu < BITS_PER_LONG)
  87. clear_bit(cpu, &mask);
  88. _flat_send_IPI_mask(mask, vector);
  89. }
  90. static void flat_send_IPI_allbutself(int vector)
  91. {
  92. int cpu = smp_processor_id();
  93. #ifdef CONFIG_HOTPLUG_CPU
  94. int hotplug = 1;
  95. #else
  96. int hotplug = 0;
  97. #endif
  98. if (hotplug || vector == NMI_VECTOR) {
  99. if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
  100. unsigned long mask = cpumask_bits(cpu_online_mask)[0];
  101. if (cpu < BITS_PER_LONG)
  102. clear_bit(cpu, &mask);
  103. _flat_send_IPI_mask(mask, vector);
  104. }
  105. } else if (num_online_cpus() > 1) {
  106. __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
  107. vector, apic->dest_logical);
  108. }
  109. }
  110. static void flat_send_IPI_all(int vector)
  111. {
  112. if (vector == NMI_VECTOR) {
  113. flat_send_IPI_mask(cpu_online_mask, vector);
  114. } else {
  115. __default_send_IPI_shortcut(APIC_DEST_ALLINC,
  116. vector, apic->dest_logical);
  117. }
  118. }
  119. static unsigned int flat_get_apic_id(unsigned long x)
  120. {
  121. unsigned int id;
  122. id = (((x)>>24) & 0xFFu);
  123. return id;
  124. }
  125. static unsigned long set_apic_id(unsigned int id)
  126. {
  127. unsigned long x;
  128. x = ((id & 0xFFu)<<24);
  129. return x;
  130. }
  131. static unsigned int read_xapic_id(void)
  132. {
  133. unsigned int id;
  134. id = flat_get_apic_id(apic_read(APIC_ID));
  135. return id;
  136. }
  137. static int flat_apic_id_registered(void)
  138. {
  139. return physid_isset(read_xapic_id(), phys_cpu_present_map);
  140. }
  141. static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
  142. {
  143. return initial_apic_id >> index_msb;
  144. }
  145. static struct apic apic_flat = {
  146. .name = "flat",
  147. .probe = NULL,
  148. .acpi_madt_oem_check = flat_acpi_madt_oem_check,
  149. .apic_id_registered = flat_apic_id_registered,
  150. .irq_delivery_mode = dest_LowestPrio,
  151. .irq_dest_mode = 1, /* logical */
  152. .target_cpus = flat_target_cpus,
  153. .disable_esr = 0,
  154. .dest_logical = APIC_DEST_LOGICAL,
  155. .check_apicid_used = NULL,
  156. .check_apicid_present = NULL,
  157. .vector_allocation_domain = flat_vector_allocation_domain,
  158. .init_apic_ldr = flat_init_apic_ldr,
  159. .ioapic_phys_id_map = NULL,
  160. .setup_apic_routing = NULL,
  161. .multi_timer_check = NULL,
  162. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  163. .apicid_to_cpu_present = NULL,
  164. .setup_portio_remap = NULL,
  165. .check_phys_apicid_present = default_check_phys_apicid_present,
  166. .enable_apic_mode = NULL,
  167. .phys_pkg_id = flat_phys_pkg_id,
  168. .mps_oem_check = NULL,
  169. .get_apic_id = flat_get_apic_id,
  170. .set_apic_id = set_apic_id,
  171. .apic_id_mask = 0xFFu << 24,
  172. .cpu_mask_to_apicid = default_cpu_mask_to_apicid,
  173. .cpu_mask_to_apicid_and = default_cpu_mask_to_apicid_and,
  174. .send_IPI_mask = flat_send_IPI_mask,
  175. .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
  176. .send_IPI_allbutself = flat_send_IPI_allbutself,
  177. .send_IPI_all = flat_send_IPI_all,
  178. .send_IPI_self = apic_send_IPI_self,
  179. .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
  180. .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
  181. .wait_for_init_deassert = NULL,
  182. .smp_callin_clear_local_apic = NULL,
  183. .inquire_remote_apic = default_inquire_remote_apic,
  184. .read = native_apic_mem_read,
  185. .write = native_apic_mem_write,
  186. .icr_read = native_apic_icr_read,
  187. .icr_write = native_apic_icr_write,
  188. .wait_icr_idle = native_apic_wait_icr_idle,
  189. .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
  190. };
  191. /*
  192. * Physflat mode is used when there are more than 8 CPUs on a system.
  193. * We cannot use logical delivery in this case because the mask
  194. * overflows, so use physical mode.
  195. */
  196. static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  197. {
  198. #ifdef CONFIG_ACPI
  199. /*
  200. * Quirk: some x86_64 machines can only use physical APIC mode
  201. * regardless of how many processors are present (x86_64 ES7000
  202. * is an example).
  203. */
  204. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  205. (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
  206. printk(KERN_DEBUG "system APIC only can use physical flat");
  207. return 1;
  208. }
  209. if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
  210. printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
  211. return 1;
  212. }
  213. #endif
  214. return 0;
  215. }
  216. static const struct cpumask *physflat_target_cpus(void)
  217. {
  218. return cpu_online_mask;
  219. }
  220. static void physflat_vector_allocation_domain(int cpu, struct cpumask *retmask)
  221. {
  222. cpumask_clear(retmask);
  223. cpumask_set_cpu(cpu, retmask);
  224. }
  225. static void physflat_send_IPI_mask(const struct cpumask *cpumask, int vector)
  226. {
  227. default_send_IPI_mask_sequence_phys(cpumask, vector);
  228. }
  229. static void physflat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
  230. int vector)
  231. {
  232. default_send_IPI_mask_allbutself_phys(cpumask, vector);
  233. }
  234. static void physflat_send_IPI_allbutself(int vector)
  235. {
  236. default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
  237. }
  238. static void physflat_send_IPI_all(int vector)
  239. {
  240. physflat_send_IPI_mask(cpu_online_mask, vector);
  241. }
  242. static unsigned int physflat_cpu_mask_to_apicid(const struct cpumask *cpumask)
  243. {
  244. int cpu;
  245. /*
  246. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  247. * May as well be the first.
  248. */
  249. cpu = cpumask_first(cpumask);
  250. if ((unsigned)cpu < nr_cpu_ids)
  251. return per_cpu(x86_cpu_to_apicid, cpu);
  252. else
  253. return BAD_APICID;
  254. }
  255. static unsigned int
  256. physflat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  257. const struct cpumask *andmask)
  258. {
  259. int cpu;
  260. /*
  261. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  262. * May as well be the first.
  263. */
  264. for_each_cpu_and(cpu, cpumask, andmask) {
  265. if (cpumask_test_cpu(cpu, cpu_online_mask))
  266. break;
  267. }
  268. return per_cpu(x86_cpu_to_apicid, cpu);
  269. }
  270. static int physflat_probe(void)
  271. {
  272. if (apic == &apic_physflat || num_possible_cpus() > 8)
  273. return 1;
  274. return 0;
  275. }
  276. static struct apic apic_physflat = {
  277. .name = "physical flat",
  278. .probe = physflat_probe,
  279. .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
  280. .apic_id_registered = flat_apic_id_registered,
  281. .irq_delivery_mode = dest_Fixed,
  282. .irq_dest_mode = 0, /* physical */
  283. .target_cpus = physflat_target_cpus,
  284. .disable_esr = 0,
  285. .dest_logical = 0,
  286. .check_apicid_used = NULL,
  287. .check_apicid_present = NULL,
  288. .vector_allocation_domain = physflat_vector_allocation_domain,
  289. /* not needed, but shouldn't hurt: */
  290. .init_apic_ldr = flat_init_apic_ldr,
  291. .ioapic_phys_id_map = NULL,
  292. .setup_apic_routing = NULL,
  293. .multi_timer_check = NULL,
  294. .cpu_present_to_apicid = default_cpu_present_to_apicid,
  295. .apicid_to_cpu_present = NULL,
  296. .setup_portio_remap = NULL,
  297. .check_phys_apicid_present = default_check_phys_apicid_present,
  298. .enable_apic_mode = NULL,
  299. .phys_pkg_id = flat_phys_pkg_id,
  300. .mps_oem_check = NULL,
  301. .get_apic_id = flat_get_apic_id,
  302. .set_apic_id = set_apic_id,
  303. .apic_id_mask = 0xFFu << 24,
  304. .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
  305. .cpu_mask_to_apicid_and = physflat_cpu_mask_to_apicid_and,
  306. .send_IPI_mask = physflat_send_IPI_mask,
  307. .send_IPI_mask_allbutself = physflat_send_IPI_mask_allbutself,
  308. .send_IPI_allbutself = physflat_send_IPI_allbutself,
  309. .send_IPI_all = physflat_send_IPI_all,
  310. .send_IPI_self = apic_send_IPI_self,
  311. .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
  312. .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
  313. .wait_for_init_deassert = NULL,
  314. .smp_callin_clear_local_apic = NULL,
  315. .inquire_remote_apic = default_inquire_remote_apic,
  316. .read = native_apic_mem_read,
  317. .write = native_apic_mem_write,
  318. .icr_read = native_apic_icr_read,
  319. .icr_write = native_apic_icr_write,
  320. .wait_icr_idle = native_apic_wait_icr_idle,
  321. .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
  322. };
  323. /*
  324. * We need to check for physflat first, so this order is important.
  325. */
  326. apic_drivers(apic_physflat, apic_flat);