genapic_flat_64.c 6.8 KB

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