smp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* SMP bring-up: ACPI CPU list, AP trampoline, INIT-SIPI and per-CPU idle.
  2. *
  3. * No MP-table fallback and no IOAPIC: this targets ACPI/MADT machines (QEMU
  4. * and the Atom board). If ACPI is absent or has one CPU, smp_init() leaves
  5. * the system exactly as it was.
  6. */
  7. #include "smp.h"
  8. #include "acpi.h"
  9. #include "cpu.h"
  10. #include "idle.h"
  11. #include "lapic.h"
  12. #include "paging.h"
  13. #include "process.h"
  14. #include "sched.h"
  15. #include "serial.h"
  16. #include "timer.h"
  17. #include "tss.h"
  18. #define AP_TRAMP_ADDR 0x6000u
  19. #define AP_INFO_ADDR 0x6F00u
  20. extern unsigned char _binary_ap_tramp_bin_start[];
  21. extern unsigned char _binary_ap_tramp_bin_end[];
  22. extern void idt_load_ap(void);
  23. static volatile unsigned int ap_started[MAX_CPUS];
  24. static unsigned int online_aps;
  25. unsigned int smp_sched_enabled;
  26. static void delay_ms(unsigned int ms)
  27. {
  28. unsigned int t0 = get_ticks_ms();
  29. while ((unsigned int)(get_ticks_ms() - t0) < ms)
  30. cpu_idle();
  31. }
  32. static int copy_trampoline(void)
  33. {
  34. unsigned int n = (unsigned int)(_binary_ap_tramp_bin_end -
  35. _binary_ap_tramp_bin_start);
  36. unsigned char *dst = (unsigned char *)AP_TRAMP_ADDR;
  37. unsigned int i;
  38. if (n == 0 || n > 0xC00)
  39. return 0;
  40. for (i = 0; i < n; i++)
  41. dst[i] = _binary_ap_tramp_bin_start[i];
  42. __asm__ volatile ("wbinvd" ::: "memory");
  43. return 1;
  44. }
  45. static void start_one_ap(unsigned int cpu, unsigned int top)
  46. {
  47. volatile unsigned int *info = (volatile unsigned int *)AP_INFO_ADDR;
  48. unsigned int apic = acpi_cpu_apic_id(cpu);
  49. unsigned int t0;
  50. info[0] = paging_kernel_cr3();
  51. info[1] = top;
  52. info[2] = (unsigned int)smp_ap_main;
  53. info[3] = cpu;
  54. __sync_synchronize();
  55. ap_started[cpu] = 0;
  56. lapic_send_init(apic);
  57. delay_ms(10);
  58. lapic_send_sipi(apic, AP_TRAMP_ADDR >> 12);
  59. /* The spec recommends a second SIPI if the first one was missed; some
  60. * firmware/boards drop it while the AP is still finishing INIT. */
  61. t0 = get_ticks_ms();
  62. while ((unsigned int)(get_ticks_ms() - t0) < 10) {
  63. if (ap_started[cpu])
  64. return;
  65. cpu_idle();
  66. }
  67. if (!ap_started[cpu])
  68. lapic_send_sipi(apic, AP_TRAMP_ADDR >> 12);
  69. t0 = get_ticks_ms();
  70. while ((unsigned int)(get_ticks_ms() - t0) < 500) {
  71. if (ap_started[cpu])
  72. return;
  73. cpu_idle();
  74. }
  75. /* It did not check in; don't let it start later with stale info. */
  76. lapic_send_init(apic);
  77. }
  78. void smp_init(void)
  79. {
  80. unsigned int n = acpi_cpu_count();
  81. unsigned int i;
  82. online_aps = 0;
  83. if (n <= 1) {
  84. serial_puts(SERIAL_COM1, "smp: unavailable, 1 cpu\n");
  85. return;
  86. }
  87. if (!acpi_lapic_base())
  88. return;
  89. lapic_set_base(acpi_lapic_base());
  90. if (!lapic_init_bsp()) {
  91. serial_puts(SERIAL_COM1, "smp: BSP LAPIC init failed\n");
  92. return;
  93. }
  94. cpus[0].lapic_id = lapic_id();
  95. if (!copy_trampoline()) {
  96. serial_puts(SERIAL_COM1, "smp: bad AP trampoline, staying UP\n");
  97. return;
  98. }
  99. for (i = 1; i < n; i++) {
  100. unsigned int top = process_init_ap(i);
  101. if (!top) {
  102. serial_puts(SERIAL_COM1, "smp: no stack for AP\n");
  103. break;
  104. }
  105. start_one_ap(i, top);
  106. if (!ap_started[i]) {
  107. serial_puts(SERIAL_COM1, "smp: AP ");
  108. serial_putdec(SERIAL_COM1, i);
  109. serial_puts(SERIAL_COM1, " did not start\n");
  110. break;
  111. }
  112. online_aps++;
  113. cpu_count = online_aps + 1;
  114. }
  115. serial_puts(SERIAL_COM1, "smp: ");
  116. serial_putdec(SERIAL_COM1, cpu_count);
  117. serial_puts(SERIAL_COM1, " cpu online\n");
  118. }
  119. void smp_ap_main(unsigned int cpu)
  120. {
  121. struct process *idle;
  122. if (cpu == 0 || cpu >= MAX_CPUS) {
  123. for (;;)
  124. __asm__ volatile ("cli; hlt");
  125. }
  126. gdt_tss_init(cpu);
  127. idt_load_ap();
  128. /* An AP starts with the firmware's CR4; enable the SSE state before it
  129. * can reach the drawing fast paths (the BSP does this lazily). */
  130. cpu_enable_sse();
  131. cpus[cpu].id = cpu;
  132. cpus[cpu].lapic_id = lapic_id();
  133. idle = cpus[cpu].idle;
  134. if (!idle)
  135. for (;;)
  136. __asm__ volatile ("cli; hlt");
  137. cpus[cpu].current = idle;
  138. idle->state = PROC_RUNNING;
  139. idle->on_cpu = cpu;
  140. idle->last_cpu = cpu;
  141. cpus[cpu].tss->esp0 = idle->kstack_top;
  142. lapic_init_ap();
  143. if (!lapic_timer_start(LAPIC_TIMER_VECTOR))
  144. for (;;)
  145. __asm__ volatile ("cli; hlt");
  146. ap_started[cpu] = 1;
  147. __sync_synchronize();
  148. __asm__ volatile ("sti");
  149. for (;;)
  150. cpu_idle();
  151. }
  152. /* Called from the reschedule IPI stub (and from smp_resched_cpu when the
  153. * target is the current CPU): mark the CPU and shorten its idle timer so the
  154. * next tick schedules promptly even when the PIT/LAPIC period is 10 ms. */
  155. void smp_resched_wake(void)
  156. {
  157. struct cpu *c = this_cpu();
  158. c->need_resched = 1;
  159. if (c->id == 0) {
  160. timer_set_hz(1000);
  161. } else {
  162. c->tick_ms = 1;
  163. lapic_timer_set_ms(1);
  164. }
  165. }
  166. void smp_resched_cpu(unsigned int cpu)
  167. {
  168. if (cpu >= cpu_count || cpu >= MAX_CPUS)
  169. return;
  170. if (cpu == this_cpu()->id) {
  171. smp_resched_wake();
  172. return;
  173. }
  174. if (cpus[cpu].lapic_id == 0xFFFFFFFFu)
  175. return;
  176. lapic_send_ipi(cpus[cpu].lapic_id, LAPIC_RESCHED_VECTOR);
  177. }
  178. void smp_enable_sched(void)
  179. {
  180. unsigned int i;
  181. if (cpu_count <= 1)
  182. return;
  183. smp_sched_enabled = 1;
  184. for (i = 1; i < cpu_count; i++)
  185. smp_resched_cpu(i);
  186. }
  187. void smp_stop_aps(void)
  188. {
  189. unsigned int i, n;
  190. /* The kexec trampoline is about to overwrite the kernel. Stop taking
  191. * interrupts first (also on UP, so the final copy cannot be
  192. * preempted), park every AP, and wait long enough for INIT to take
  193. * effect. An AP may have been inside a scheduler critical section, so
  194. * the lock is reinitialized afterwards; the old run queue is dead
  195. * anyway and the new kernel re-creates everything. */
  196. __asm__ volatile ("cli");
  197. if (cpu_count <= 1)
  198. return;
  199. for (i = 1; i < cpu_count; i++) {
  200. if (cpus[i].lapic_id != 0xFFFFFFFFu)
  201. lapic_send_init(cpus[i].lapic_id);
  202. }
  203. /* A few million PAUSE iterations is well over the 10 ms the INIT
  204. * protocol requires on the Atom/P5 range, and costs nothing on the
  205. * few occasions a kexec runs. */
  206. for (n = 0; n < 4000000u; n++)
  207. __asm__ volatile ("pause");
  208. sched_lock_reset();
  209. }
  210. void smp_dump(void)
  211. {
  212. serial_puts(SERIAL_COM1, "smp: online ");
  213. serial_putdec(SERIAL_COM1, cpu_count);
  214. serial_puts(SERIAL_COM1, "\n");
  215. }