genapic_flat_64.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 cpumask_t flat_target_cpus(void)
  31. {
  32. return cpu_online_map;
  33. }
  34. static cpumask_t flat_vector_allocation_domain(int cpu)
  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_t domain = { { [0] = APIC_ALL_CPUS, } };
  45. return domain;
  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 void flat_send_IPI_mask(cpumask_t cpumask, int vector)
  66. {
  67. unsigned long mask = cpus_addr(cpumask)[0];
  68. unsigned long flags;
  69. local_irq_save(flags);
  70. __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
  71. local_irq_restore(flags);
  72. }
  73. static void flat_send_IPI_allbutself(int vector)
  74. {
  75. #ifdef CONFIG_HOTPLUG_CPU
  76. int hotplug = 1;
  77. #else
  78. int hotplug = 0;
  79. #endif
  80. if (hotplug || vector == NMI_VECTOR) {
  81. cpumask_t allbutme = cpu_online_map;
  82. cpu_clear(smp_processor_id(), allbutme);
  83. if (!cpus_empty(allbutme))
  84. flat_send_IPI_mask(allbutme, vector);
  85. } else if (num_online_cpus() > 1) {
  86. __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
  87. }
  88. }
  89. static void flat_send_IPI_all(int vector)
  90. {
  91. if (vector == NMI_VECTOR)
  92. flat_send_IPI_mask(cpu_online_map, vector);
  93. else
  94. __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
  95. }
  96. static unsigned int get_apic_id(unsigned long x)
  97. {
  98. unsigned int id;
  99. id = (((x)>>24) & 0xFFu);
  100. return id;
  101. }
  102. static unsigned long set_apic_id(unsigned int id)
  103. {
  104. unsigned long x;
  105. x = ((id & 0xFFu)<<24);
  106. return x;
  107. }
  108. static unsigned int read_xapic_id(void)
  109. {
  110. unsigned int id;
  111. id = get_apic_id(apic_read(APIC_ID));
  112. return id;
  113. }
  114. static int flat_apic_id_registered(void)
  115. {
  116. return physid_isset(read_xapic_id(), phys_cpu_present_map);
  117. }
  118. static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask)
  119. {
  120. return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
  121. }
  122. static unsigned int phys_pkg_id(int index_msb)
  123. {
  124. return hard_smp_processor_id() >> index_msb;
  125. }
  126. struct genapic apic_flat = {
  127. .name = "flat",
  128. .acpi_madt_oem_check = flat_acpi_madt_oem_check,
  129. .int_delivery_mode = dest_LowestPrio,
  130. .int_dest_mode = (APIC_DEST_LOGICAL != 0),
  131. .target_cpus = flat_target_cpus,
  132. .vector_allocation_domain = flat_vector_allocation_domain,
  133. .apic_id_registered = flat_apic_id_registered,
  134. .init_apic_ldr = flat_init_apic_ldr,
  135. .send_IPI_all = flat_send_IPI_all,
  136. .send_IPI_allbutself = flat_send_IPI_allbutself,
  137. .send_IPI_mask = flat_send_IPI_mask,
  138. .send_IPI_self = apic_send_IPI_self,
  139. .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
  140. .phys_pkg_id = phys_pkg_id,
  141. .get_apic_id = get_apic_id,
  142. .set_apic_id = set_apic_id,
  143. .apic_id_mask = (0xFFu<<24),
  144. };
  145. /*
  146. * Physflat mode is used when there are more than 8 CPUs on a AMD system.
  147. * We cannot use logical delivery in this case because the mask
  148. * overflows, so use physical mode.
  149. */
  150. static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
  151. {
  152. #ifdef CONFIG_ACPI
  153. /*
  154. * Quirk: some x86_64 machines can only use physical APIC mode
  155. * regardless of how many processors are present (x86_64 ES7000
  156. * is an example).
  157. */
  158. if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID &&
  159. (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
  160. printk(KERN_DEBUG "system APIC only can use physical flat");
  161. return 1;
  162. }
  163. #endif
  164. return 0;
  165. }
  166. static cpumask_t physflat_target_cpus(void)
  167. {
  168. return cpu_online_map;
  169. }
  170. static cpumask_t physflat_vector_allocation_domain(int cpu)
  171. {
  172. return cpumask_of_cpu(cpu);
  173. }
  174. static void physflat_send_IPI_mask(cpumask_t cpumask, int vector)
  175. {
  176. send_IPI_mask_sequence(cpumask, vector);
  177. }
  178. static void physflat_send_IPI_allbutself(int vector)
  179. {
  180. cpumask_t allbutme = cpu_online_map;
  181. cpu_clear(smp_processor_id(), allbutme);
  182. physflat_send_IPI_mask(allbutme, vector);
  183. }
  184. static void physflat_send_IPI_all(int vector)
  185. {
  186. physflat_send_IPI_mask(cpu_online_map, vector);
  187. }
  188. static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask)
  189. {
  190. int cpu;
  191. /*
  192. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  193. * May as well be the first.
  194. */
  195. cpu = first_cpu(cpumask);
  196. if ((unsigned)cpu < nr_cpu_ids)
  197. return per_cpu(x86_cpu_to_apicid, cpu);
  198. else
  199. return BAD_APICID;
  200. }
  201. struct genapic apic_physflat = {
  202. .name = "physical flat",
  203. .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
  204. .int_delivery_mode = dest_Fixed,
  205. .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
  206. .target_cpus = physflat_target_cpus,
  207. .vector_allocation_domain = physflat_vector_allocation_domain,
  208. .apic_id_registered = flat_apic_id_registered,
  209. .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
  210. .send_IPI_all = physflat_send_IPI_all,
  211. .send_IPI_allbutself = physflat_send_IPI_allbutself,
  212. .send_IPI_mask = physflat_send_IPI_mask,
  213. .send_IPI_self = apic_send_IPI_self,
  214. .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
  215. .phys_pkg_id = phys_pkg_id,
  216. .get_apic_id = get_apic_id,
  217. .set_apic_id = set_apic_id,
  218. .apic_id_mask = (0xFFu<<24),
  219. };