genapic_flat.c 4.4 KB

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