genapic_flat.c 5.1 KB

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