genapic_flat_64.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 <asm/smp.h>
  20. #include <asm/ipi.h>
  21. #include <asm/genapic.h>
  22. #include <mach_apicdef.h>
  23. #ifdef CONFIG_ACPI
  24. #include <acpi/acpi_bus.h>
  25. #endif
  26. static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  27. {
  28. return 1;
  29. }
  30. static const struct cpumask *flat_target_cpus(void)
  31. {
  32. return cpu_online_mask;
  33. }
  34. static void flat_vector_allocation_domain(int cpu, struct cpumask *retmask)
  35. {
  36. /* Careful. Some cpus do not strictly honor the set of cpus
  37. * specified in the interrupt destination when using lowest
  38. * priority interrupt delivery mode.
  39. *
  40. * In particular there was a hyperthreading cpu observed to
  41. * deliver interrupts to the wrong hyperthread when only one
  42. * hyperthread was specified in the interrupt desitination.
  43. */
  44. cpumask_clear(retmask);
  45. cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
  46. }
  47. /*
  48. * Set up the logical destination ID.
  49. *
  50. * Intel recommends to set DFR, LDR and TPR before enabling
  51. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  52. * document number 292116). So here it goes...
  53. */
  54. static void flat_init_apic_ldr(void)
  55. {
  56. unsigned long val;
  57. unsigned long num, id;
  58. num = smp_processor_id();
  59. id = 1UL << num;
  60. apic_write(APIC_DFR, APIC_DFR_FLAT);
  61. val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
  62. val |= SET_APIC_LOGICAL_ID(id);
  63. apic_write(APIC_LDR, val);
  64. }
  65. static inline void _flat_send_IPI_mask(unsigned long mask, int vector)
  66. {
  67. unsigned long flags;
  68. local_irq_save(flags);
  69. __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
  70. local_irq_restore(flags);
  71. }
  72. static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
  73. {
  74. unsigned long mask = cpumask_bits(cpumask)[0];
  75. _flat_send_IPI_mask(mask, vector);
  76. }
  77. static void flat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
  78. int vector)
  79. {
  80. unsigned long mask = cpumask_bits(cpumask)[0];
  81. int cpu = smp_processor_id();
  82. if (cpu < BITS_PER_LONG)
  83. clear_bit(cpu, &mask);
  84. _flat_send_IPI_mask(mask, vector);
  85. }
  86. static void flat_send_IPI_allbutself(int vector)
  87. {
  88. int cpu = smp_processor_id();
  89. #ifdef CONFIG_HOTPLUG_CPU
  90. int hotplug = 1;
  91. #else
  92. int hotplug = 0;
  93. #endif
  94. if (hotplug || vector == NMI_VECTOR) {
  95. if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
  96. unsigned long mask = cpumask_bits(cpu_online_mask)[0];
  97. if (cpu < BITS_PER_LONG)
  98. clear_bit(cpu, &mask);
  99. _flat_send_IPI_mask(mask, vector);
  100. }
  101. } else if (num_online_cpus() > 1) {
  102. __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
  103. }
  104. }
  105. static void flat_send_IPI_all(int vector)
  106. {
  107. if (vector == NMI_VECTOR)
  108. flat_send_IPI_mask(cpu_online_mask, vector);
  109. else
  110. __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
  111. }
  112. static unsigned int get_apic_id(unsigned long x)
  113. {
  114. unsigned int id;
  115. id = (((x)>>24) & 0xFFu);
  116. return id;
  117. }
  118. static unsigned long set_apic_id(unsigned int id)
  119. {
  120. unsigned long x;
  121. x = ((id & 0xFFu)<<24);
  122. return x;
  123. }
  124. static unsigned int read_xapic_id(void)
  125. {
  126. unsigned int id;
  127. id = get_apic_id(apic_read(APIC_ID));
  128. return id;
  129. }
  130. static int flat_apic_id_registered(void)
  131. {
  132. return physid_isset(read_xapic_id(), phys_cpu_present_map);
  133. }
  134. static unsigned int flat_cpu_mask_to_apicid(const struct cpumask *cpumask)
  135. {
  136. return cpumask_bits(cpumask)[0] & APIC_ALL_CPUS;
  137. }
  138. static unsigned int flat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  139. const struct cpumask *andmask)
  140. {
  141. unsigned long mask1 = cpumask_bits(cpumask)[0] & APIC_ALL_CPUS;
  142. unsigned long mask2 = cpumask_bits(andmask)[0] & APIC_ALL_CPUS;
  143. return mask1 & mask2;
  144. }
  145. static unsigned int phys_pkg_id(int index_msb)
  146. {
  147. return hard_smp_processor_id() >> index_msb;
  148. }
  149. struct genapic apic_flat = {
  150. .name = "flat",
  151. .acpi_madt_oem_check = flat_acpi_madt_oem_check,
  152. .int_delivery_mode = dest_LowestPrio,
  153. .int_dest_mode = (APIC_DEST_LOGICAL != 0),
  154. .target_cpus = flat_target_cpus,
  155. .vector_allocation_domain = flat_vector_allocation_domain,
  156. .apic_id_registered = flat_apic_id_registered,
  157. .init_apic_ldr = flat_init_apic_ldr,
  158. .send_IPI_all = flat_send_IPI_all,
  159. .send_IPI_allbutself = flat_send_IPI_allbutself,
  160. .send_IPI_mask = flat_send_IPI_mask,
  161. .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
  162. .send_IPI_self = apic_send_IPI_self,
  163. .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
  164. .cpu_mask_to_apicid_and = flat_cpu_mask_to_apicid_and,
  165. .phys_pkg_id = phys_pkg_id,
  166. .get_apic_id = get_apic_id,
  167. .set_apic_id = set_apic_id,
  168. .apic_id_mask = (0xFFu<<24),
  169. };
  170. /*
  171. * Physflat mode is used when there are more than 8 CPUs on a AMD system.
  172. * We cannot use logical delivery in this case because the mask
  173. * overflows, so use physical mode.
  174. */
  175. static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  176. {
  177. #ifdef CONFIG_ACPI
  178. /*
  179. * Quirk: some x86_64 machines can only use physical APIC mode
  180. * regardless of how many processors are present (x86_64 ES7000
  181. * is an example).
  182. */
  183. if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID &&
  184. (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
  185. printk(KERN_DEBUG "system APIC only can use physical flat");
  186. return 1;
  187. }
  188. #endif
  189. return 0;
  190. }
  191. static const struct cpumask *physflat_target_cpus(void)
  192. {
  193. return cpu_online_mask;
  194. }
  195. static void physflat_vector_allocation_domain(int cpu, struct cpumask *retmask)
  196. {
  197. cpumask_clear(retmask);
  198. cpumask_set_cpu(cpu, retmask);
  199. }
  200. static void physflat_send_IPI_mask(const struct cpumask *cpumask, int vector)
  201. {
  202. send_IPI_mask_sequence(cpumask, vector);
  203. }
  204. static void physflat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
  205. int vector)
  206. {
  207. send_IPI_mask_allbutself(cpumask, vector);
  208. }
  209. static void physflat_send_IPI_allbutself(int vector)
  210. {
  211. send_IPI_mask_allbutself(cpu_online_mask, vector);
  212. }
  213. static void physflat_send_IPI_all(int vector)
  214. {
  215. physflat_send_IPI_mask(cpu_online_mask, vector);
  216. }
  217. static unsigned int physflat_cpu_mask_to_apicid(const struct cpumask *cpumask)
  218. {
  219. int cpu;
  220. /*
  221. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  222. * May as well be the first.
  223. */
  224. cpu = cpumask_first(cpumask);
  225. if ((unsigned)cpu < nr_cpu_ids)
  226. return per_cpu(x86_cpu_to_apicid, cpu);
  227. else
  228. return BAD_APICID;
  229. }
  230. static unsigned int
  231. physflat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
  232. const struct cpumask *andmask)
  233. {
  234. int cpu;
  235. /*
  236. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  237. * May as well be the first.
  238. */
  239. for_each_cpu_and(cpu, cpumask, andmask)
  240. if (cpumask_test_cpu(cpu, cpu_online_mask))
  241. break;
  242. if (cpu < nr_cpu_ids)
  243. return per_cpu(x86_cpu_to_apicid, cpu);
  244. return BAD_APICID;
  245. }
  246. struct genapic apic_physflat = {
  247. .name = "physical flat",
  248. .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
  249. .int_delivery_mode = dest_Fixed,
  250. .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
  251. .target_cpus = physflat_target_cpus,
  252. .vector_allocation_domain = physflat_vector_allocation_domain,
  253. .apic_id_registered = flat_apic_id_registered,
  254. .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
  255. .send_IPI_all = physflat_send_IPI_all,
  256. .send_IPI_allbutself = physflat_send_IPI_allbutself,
  257. .send_IPI_mask = physflat_send_IPI_mask,
  258. .send_IPI_mask_allbutself = physflat_send_IPI_mask_allbutself,
  259. .send_IPI_self = apic_send_IPI_self,
  260. .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
  261. .cpu_mask_to_apicid_and = physflat_cpu_mask_to_apicid_and,
  262. .phys_pkg_id = phys_pkg_id,
  263. .get_apic_id = get_apic_id,
  264. .set_apic_id = set_apic_id,
  265. .apic_id_mask = (0xFFu<<24),
  266. };