genapic_flat_64.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 flat_cpu_mask_to_apicid_and(const cpumask_t *cpumask,
  137. const cpumask_t *andmask)
  138. {
  139. unsigned long mask1 = cpus_addr(*cpumask)[0] & APIC_ALL_CPUS;
  140. unsigned long mask2 = cpus_addr(*andmask)[0] & APIC_ALL_CPUS;
  141. return (int)(mask1 & mask2);
  142. }
  143. static unsigned int phys_pkg_id(int index_msb)
  144. {
  145. return hard_smp_processor_id() >> index_msb;
  146. }
  147. struct genapic apic_flat = {
  148. .name = "flat",
  149. .acpi_madt_oem_check = flat_acpi_madt_oem_check,
  150. .int_delivery_mode = dest_LowestPrio,
  151. .int_dest_mode = (APIC_DEST_LOGICAL != 0),
  152. .target_cpus = flat_target_cpus,
  153. .vector_allocation_domain = flat_vector_allocation_domain,
  154. .apic_id_registered = flat_apic_id_registered,
  155. .init_apic_ldr = flat_init_apic_ldr,
  156. .send_IPI_all = flat_send_IPI_all,
  157. .send_IPI_allbutself = flat_send_IPI_allbutself,
  158. .send_IPI_mask = flat_send_IPI_mask,
  159. .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
  160. .send_IPI_self = apic_send_IPI_self,
  161. .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
  162. .cpu_mask_to_apicid_and = flat_cpu_mask_to_apicid_and,
  163. .phys_pkg_id = phys_pkg_id,
  164. .get_apic_id = get_apic_id,
  165. .set_apic_id = set_apic_id,
  166. .apic_id_mask = (0xFFu<<24),
  167. };
  168. /*
  169. * Physflat mode is used when there are more than 8 CPUs on a AMD system.
  170. * We cannot use logical delivery in this case because the mask
  171. * overflows, so use physical mode.
  172. */
  173. static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  174. {
  175. #ifdef CONFIG_ACPI
  176. /*
  177. * Quirk: some x86_64 machines can only use physical APIC mode
  178. * regardless of how many processors are present (x86_64 ES7000
  179. * is an example).
  180. */
  181. if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID &&
  182. (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
  183. printk(KERN_DEBUG "system APIC only can use physical flat");
  184. return 1;
  185. }
  186. #endif
  187. return 0;
  188. }
  189. static const cpumask_t *physflat_target_cpus(void)
  190. {
  191. return &cpu_online_map;
  192. }
  193. static void physflat_vector_allocation_domain(int cpu, cpumask_t *retmask)
  194. {
  195. cpus_clear(*retmask);
  196. cpu_set(cpu, *retmask);
  197. }
  198. static void physflat_send_IPI_mask(const cpumask_t *cpumask, int vector)
  199. {
  200. send_IPI_mask_sequence(cpumask, vector);
  201. }
  202. static void physflat_send_IPI_mask_allbutself(const cpumask_t *cpumask,
  203. int vector)
  204. {
  205. send_IPI_mask_allbutself(cpumask, vector);
  206. }
  207. static void physflat_send_IPI_allbutself(int vector)
  208. {
  209. send_IPI_mask_allbutself(&cpu_online_map, vector);
  210. }
  211. static void physflat_send_IPI_all(int vector)
  212. {
  213. physflat_send_IPI_mask(&cpu_online_map, vector);
  214. }
  215. static unsigned int physflat_cpu_mask_to_apicid(const cpumask_t *cpumask)
  216. {
  217. int cpu;
  218. /*
  219. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  220. * May as well be the first.
  221. */
  222. cpu = first_cpu(*cpumask);
  223. if ((unsigned)cpu < nr_cpu_ids)
  224. return per_cpu(x86_cpu_to_apicid, cpu);
  225. else
  226. return BAD_APICID;
  227. }
  228. static unsigned int physflat_cpu_mask_to_apicid_and(const cpumask_t *cpumask,
  229. const cpumask_t *andmask)
  230. {
  231. int cpu;
  232. /*
  233. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  234. * May as well be the first.
  235. */
  236. while ((cpu = next_cpu(-1, *cpumask)) < nr_cpu_ids)
  237. if (cpu_isset(cpu, *andmask))
  238. return per_cpu(x86_cpu_to_apicid, cpu);
  239. return BAD_APICID;
  240. }
  241. struct genapic apic_physflat = {
  242. .name = "physical flat",
  243. .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
  244. .int_delivery_mode = dest_Fixed,
  245. .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
  246. .target_cpus = physflat_target_cpus,
  247. .vector_allocation_domain = physflat_vector_allocation_domain,
  248. .apic_id_registered = flat_apic_id_registered,
  249. .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
  250. .send_IPI_all = physflat_send_IPI_all,
  251. .send_IPI_allbutself = physflat_send_IPI_allbutself,
  252. .send_IPI_mask = physflat_send_IPI_mask,
  253. .send_IPI_mask_allbutself = physflat_send_IPI_mask_allbutself,
  254. .send_IPI_self = apic_send_IPI_self,
  255. .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
  256. .cpu_mask_to_apicid_and = physflat_cpu_mask_to_apicid_and,
  257. .phys_pkg_id = phys_pkg_id,
  258. .get_apic_id = get_apic_id,
  259. .set_apic_id = set_apic_id,
  260. .apic_id_mask = (0xFFu<<24),
  261. };