smp.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. info[4] = 0;
  55. __sync_synchronize();
  56. ap_started[cpu] = 0;
  57. lapic_send_init(apic);
  58. delay_ms(10);
  59. lapic_send_sipi(apic, AP_TRAMP_ADDR >> 12);
  60. /* The spec recommends a second SIPI if the first one was missed; some
  61. * firmware/boards drop it while the AP is still finishing INIT. */
  62. t0 = get_ticks_ms();
  63. while ((unsigned int)(get_ticks_ms() - t0) < 10) {
  64. if (ap_started[cpu])
  65. return;
  66. cpu_idle();
  67. }
  68. if (!ap_started[cpu])
  69. lapic_send_sipi(apic, AP_TRAMP_ADDR >> 12);
  70. t0 = get_ticks_ms();
  71. while ((unsigned int)(get_ticks_ms() - t0) < 500) {
  72. if (ap_started[cpu])
  73. return;
  74. cpu_idle();
  75. }
  76. /* It did not check in; don't let it start later with stale info. */
  77. lapic_send_init(apic);
  78. }
  79. void smp_init(void)
  80. {
  81. unsigned int n = acpi_cpu_count();
  82. unsigned int i;
  83. online_aps = 0;
  84. if (n <= 1) {
  85. serial_puts(SERIAL_COM1, "smp: unavailable, 1 cpu\n");
  86. return;
  87. }
  88. if (!acpi_lapic_base())
  89. return;
  90. lapic_set_base(acpi_lapic_base());
  91. if (!lapic_init_bsp()) {
  92. serial_puts(SERIAL_COM1, "smp: BSP LAPIC init failed\n");
  93. return;
  94. }
  95. cpus[0].lapic_id = lapic_id();
  96. if (!copy_trampoline()) {
  97. serial_puts(SERIAL_COM1, "smp: bad AP trampoline, staying UP\n");
  98. return;
  99. }
  100. for (i = 1; i < n; i++) {
  101. unsigned int top = process_init_ap(i);
  102. if (!top) {
  103. serial_puts(SERIAL_COM1, "smp: no stack for AP\n");
  104. break;
  105. }
  106. start_one_ap(i, top);
  107. if (!ap_started[i]) {
  108. serial_puts(SERIAL_COM1, "smp: AP ");
  109. serial_putdec(SERIAL_COM1, i);
  110. serial_puts(SERIAL_COM1, " did not start\n");
  111. break;
  112. }
  113. online_aps++;
  114. cpu_count = online_aps + 1;
  115. /* CR0 as the AP woke up (its CD/NW are cleared by the
  116. * trampoline): 0x60000011 means it would have run uncached. */
  117. serial_puts(SERIAL_COM1, "smp: AP ");
  118. serial_putdec(SERIAL_COM1, i);
  119. serial_puts(SERIAL_COM1, " start cr0 ");
  120. serial_puthex(SERIAL_COM1,
  121. ((volatile unsigned int *)AP_INFO_ADDR)[4]);
  122. serial_puts(SERIAL_COM1, "\n");
  123. }
  124. serial_puts(SERIAL_COM1, "smp: ");
  125. serial_putdec(SERIAL_COM1, cpu_count);
  126. serial_puts(SERIAL_COM1, " cpu online\n");
  127. }
  128. void smp_ap_main(unsigned int cpu)
  129. {
  130. struct process *idle;
  131. if (cpu == 0 || cpu >= MAX_CPUS) {
  132. for (;;)
  133. __asm__ volatile ("cli; hlt");
  134. }
  135. gdt_tss_init(cpu);
  136. idt_load_ap();
  137. /* An AP starts with the firmware's CR4; enable the SSE state before it
  138. * can reach the drawing fast paths (the BSP does this lazily). */
  139. cpu_enable_sse();
  140. cpus[cpu].id = cpu;
  141. cpus[cpu].lapic_id = lapic_id();
  142. idle = cpus[cpu].idle;
  143. if (!idle)
  144. for (;;)
  145. __asm__ volatile ("cli; hlt");
  146. cpus[cpu].current = idle;
  147. idle->state = PROC_RUNNING;
  148. idle->on_cpu = cpu;
  149. idle->last_cpu = cpu;
  150. cpus[cpu].tss->esp0 = idle->kstack_top;
  151. lapic_init_ap();
  152. if (!lapic_timer_start(LAPIC_TIMER_VECTOR))
  153. for (;;)
  154. __asm__ volatile ("cli; hlt");
  155. ap_started[cpu] = 1;
  156. __sync_synchronize();
  157. __asm__ volatile ("sti");
  158. for (;;)
  159. cpu_idle();
  160. }
  161. /* Called from the reschedule IPI stub (and from smp_resched_cpu when the
  162. * target is the current CPU): mark the CPU and shorten its idle timer so the
  163. * next tick schedules promptly even when the PIT/LAPIC period is 10 ms. */
  164. void smp_resched_wake(void)
  165. {
  166. struct cpu *c = this_cpu();
  167. c->need_resched = 1;
  168. if (c->id == 0) {
  169. timer_set_hz(1000);
  170. } else {
  171. c->tick_ms = 1;
  172. lapic_timer_set_ms(1);
  173. }
  174. }
  175. void smp_resched_cpu(unsigned int cpu)
  176. {
  177. if (cpu >= cpu_count || cpu >= MAX_CPUS)
  178. return;
  179. if (cpu == this_cpu()->id) {
  180. smp_resched_wake();
  181. return;
  182. }
  183. if (cpus[cpu].lapic_id == 0xFFFFFFFFu)
  184. return;
  185. lapic_send_ipi(cpus[cpu].lapic_id, LAPIC_RESCHED_VECTOR);
  186. }
  187. void smp_enable_sched(void)
  188. {
  189. unsigned int i;
  190. if (cpu_count <= 1)
  191. return;
  192. smp_sched_enabled = 1;
  193. for (i = 1; i < cpu_count; i++)
  194. smp_resched_cpu(i);
  195. }
  196. void smp_stop_aps(void)
  197. {
  198. unsigned int i, n;
  199. /* The kexec trampoline is about to overwrite the kernel. Stop taking
  200. * interrupts first (also on UP, so the final copy cannot be
  201. * preempted), park every AP, and wait long enough for INIT to take
  202. * effect. An AP may have been inside a scheduler critical section, so
  203. * the lock is reinitialized afterwards; the old run queue is dead
  204. * anyway and the new kernel re-creates everything. */
  205. __asm__ volatile ("cli");
  206. if (cpu_count <= 1)
  207. return;
  208. for (i = 1; i < cpu_count; i++) {
  209. if (cpus[i].lapic_id != 0xFFFFFFFFu)
  210. lapic_send_init(cpus[i].lapic_id);
  211. }
  212. /* A few million PAUSE iterations is well over the 10 ms the INIT
  213. * protocol requires on the Atom/P5 range, and costs nothing on the
  214. * few occasions a kexec runs. */
  215. for (n = 0; n < 4000000u; n++)
  216. __asm__ volatile ("pause");
  217. sched_lock_reset();
  218. }
  219. void smp_dump(void)
  220. {
  221. serial_puts(SERIAL_COM1, "smp: online ");
  222. serial_putdec(SERIAL_COM1, cpu_count);
  223. serial_puts(SERIAL_COM1, "\n");
  224. }