genapic_flat_64.c 4.9 KB

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