genapic_flat_64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. static cpumask_t flat_target_cpus(void)
  24. {
  25. return cpu_online_map;
  26. }
  27. static cpumask_t flat_vector_allocation_domain(int cpu)
  28. {
  29. /* Careful. Some cpus do not strictly honor the set of cpus
  30. * specified in the interrupt destination when using lowest
  31. * priority interrupt delivery mode.
  32. *
  33. * In particular there was a hyperthreading cpu observed to
  34. * deliver interrupts to the wrong hyperthread when only one
  35. * hyperthread was specified in the interrupt desitination.
  36. */
  37. cpumask_t domain = { { [0] = APIC_ALL_CPUS, } };
  38. return domain;
  39. }
  40. /*
  41. * Set up the logical destination ID.
  42. *
  43. * Intel recommends to set DFR, LDR and TPR before enabling
  44. * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
  45. * document number 292116). So here it goes...
  46. */
  47. static void flat_init_apic_ldr(void)
  48. {
  49. unsigned long val;
  50. unsigned long num, id;
  51. num = smp_processor_id();
  52. id = 1UL << num;
  53. apic_write(APIC_DFR, APIC_DFR_FLAT);
  54. val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
  55. val |= SET_APIC_LOGICAL_ID(id);
  56. apic_write(APIC_LDR, val);
  57. }
  58. static void flat_send_IPI_mask(cpumask_t cpumask, int vector)
  59. {
  60. unsigned long mask = cpus_addr(cpumask)[0];
  61. unsigned long flags;
  62. local_irq_save(flags);
  63. __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
  64. local_irq_restore(flags);
  65. }
  66. static void flat_send_IPI_allbutself(int vector)
  67. {
  68. #ifdef CONFIG_HOTPLUG_CPU
  69. int hotplug = 1;
  70. #else
  71. int hotplug = 0;
  72. #endif
  73. if (hotplug || vector == NMI_VECTOR) {
  74. cpumask_t allbutme = cpu_online_map;
  75. cpu_clear(smp_processor_id(), allbutme);
  76. if (!cpus_empty(allbutme))
  77. flat_send_IPI_mask(allbutme, vector);
  78. } else if (num_online_cpus() > 1) {
  79. __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
  80. }
  81. }
  82. static void flat_send_IPI_all(int vector)
  83. {
  84. if (vector == NMI_VECTOR)
  85. flat_send_IPI_mask(cpu_online_map, vector);
  86. else
  87. __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
  88. }
  89. static unsigned int read_xapic_id(void)
  90. {
  91. unsigned int id;
  92. id = GET_XAPIC_ID(apic_read(APIC_ID));
  93. return id;
  94. }
  95. static int flat_apic_id_registered(void)
  96. {
  97. return physid_isset(read_xapic_id(), phys_cpu_present_map);
  98. }
  99. static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask)
  100. {
  101. return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
  102. }
  103. static unsigned int phys_pkg_id(int index_msb)
  104. {
  105. return hard_smp_processor_id() >> index_msb;
  106. }
  107. struct genapic apic_flat = {
  108. .name = "flat",
  109. .int_delivery_mode = dest_LowestPrio,
  110. .int_dest_mode = (APIC_DEST_LOGICAL != 0),
  111. .target_cpus = flat_target_cpus,
  112. .vector_allocation_domain = flat_vector_allocation_domain,
  113. .apic_id_registered = flat_apic_id_registered,
  114. .init_apic_ldr = flat_init_apic_ldr,
  115. .send_IPI_all = flat_send_IPI_all,
  116. .send_IPI_allbutself = flat_send_IPI_allbutself,
  117. .send_IPI_mask = flat_send_IPI_mask,
  118. .send_IPI_self = apic_send_IPI_self,
  119. .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
  120. .phys_pkg_id = phys_pkg_id,
  121. .read_apic_id = read_xapic_id,
  122. };
  123. /*
  124. * Physflat mode is used when there are more than 8 CPUs on a AMD system.
  125. * We cannot use logical delivery in this case because the mask
  126. * overflows, so use physical mode.
  127. */
  128. static cpumask_t physflat_target_cpus(void)
  129. {
  130. return cpu_online_map;
  131. }
  132. static cpumask_t physflat_vector_allocation_domain(int cpu)
  133. {
  134. return cpumask_of_cpu(cpu);
  135. }
  136. static void physflat_send_IPI_mask(cpumask_t cpumask, int vector)
  137. {
  138. send_IPI_mask_sequence(cpumask, vector);
  139. }
  140. static void physflat_send_IPI_allbutself(int vector)
  141. {
  142. cpumask_t allbutme = cpu_online_map;
  143. cpu_clear(smp_processor_id(), allbutme);
  144. physflat_send_IPI_mask(allbutme, vector);
  145. }
  146. static void physflat_send_IPI_all(int vector)
  147. {
  148. physflat_send_IPI_mask(cpu_online_map, vector);
  149. }
  150. static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask)
  151. {
  152. int cpu;
  153. /*
  154. * We're using fixed IRQ delivery, can only return one phys APIC ID.
  155. * May as well be the first.
  156. */
  157. cpu = first_cpu(cpumask);
  158. if ((unsigned)cpu < NR_CPUS)
  159. return per_cpu(x86_cpu_to_apicid, cpu);
  160. else
  161. return BAD_APICID;
  162. }
  163. struct genapic apic_physflat = {
  164. .name = "physical flat",
  165. .int_delivery_mode = dest_Fixed,
  166. .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
  167. .target_cpus = physflat_target_cpus,
  168. .vector_allocation_domain = physflat_vector_allocation_domain,
  169. .apic_id_registered = flat_apic_id_registered,
  170. .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
  171. .send_IPI_all = physflat_send_IPI_all,
  172. .send_IPI_allbutself = physflat_send_IPI_allbutself,
  173. .send_IPI_mask = physflat_send_IPI_mask,
  174. .send_IPI_self = apic_send_IPI_self,
  175. .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
  176. .phys_pkg_id = phys_pkg_id,
  177. .read_apic_id = read_xapic_id,
  178. };