smp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. paging_map_mmio(acpi_lapic_base(), 0x1000);
  91. /* The 4 MB MMIO page could share a PDE with the framebuffer; restore
  92. * the LFB's WC/PCD choice if so. */
  93. paging_lfb_refresh();
  94. if (!lapic_init_bsp()) {
  95. serial_puts(SERIAL_COM1, "smp: BSP LAPIC init failed\n");
  96. return;
  97. }
  98. cpus[0].lapic_id = lapic_id();
  99. if (!copy_trampoline()) {
  100. serial_puts(SERIAL_COM1, "smp: bad AP trampoline, staying UP\n");
  101. return;
  102. }
  103. for (i = 1; i < n; i++) {
  104. unsigned int top = process_init_ap(i);
  105. if (!top) {
  106. serial_puts(SERIAL_COM1, "smp: no stack for AP\n");
  107. break;
  108. }
  109. start_one_ap(i, top);
  110. if (!ap_started[i]) {
  111. serial_puts(SERIAL_COM1, "smp: AP ");
  112. serial_putdec(SERIAL_COM1, i);
  113. serial_puts(SERIAL_COM1, " did not start\n");
  114. break;
  115. }
  116. online_aps++;
  117. cpu_count = online_aps + 1;
  118. }
  119. serial_puts(SERIAL_COM1, "smp: ");
  120. serial_putdec(SERIAL_COM1, cpu_count);
  121. serial_puts(SERIAL_COM1, " cpu online\n");
  122. }
  123. void smp_ap_main(unsigned int cpu)
  124. {
  125. struct process *idle;
  126. if (cpu == 0 || cpu >= MAX_CPUS) {
  127. for (;;)
  128. __asm__ volatile ("cli; hlt");
  129. }
  130. gdt_tss_init(cpu);
  131. idt_load_ap();
  132. /* An AP starts with the firmware's CR4; enable the SSE state before it
  133. * can reach the drawing fast paths (the BSP does this lazily). */
  134. cpu_enable_sse();
  135. cpus[cpu].id = cpu;
  136. cpus[cpu].lapic_id = lapic_id();
  137. idle = cpus[cpu].idle;
  138. if (!idle)
  139. for (;;)
  140. __asm__ volatile ("cli; hlt");
  141. cpus[cpu].current = idle;
  142. idle->state = PROC_RUNNING;
  143. idle->on_cpu = cpu;
  144. idle->last_cpu = cpu;
  145. cpus[cpu].tss->esp0 = idle->kstack_top;
  146. lapic_init_ap();
  147. if (!lapic_timer_start(LAPIC_TIMER_VECTOR))
  148. for (;;)
  149. __asm__ volatile ("cli; hlt");
  150. ap_started[cpu] = 1;
  151. __sync_synchronize();
  152. __asm__ volatile ("sti");
  153. for (;;)
  154. cpu_idle();
  155. }
  156. /* Called from the reschedule IPI stub (and from smp_resched_cpu when the
  157. * target is the current CPU): mark the CPU and shorten its idle timer so the
  158. * next tick schedules promptly even when the PIT/LAPIC period is 10 ms. */
  159. void smp_resched_wake(void)
  160. {
  161. struct cpu *c = this_cpu();
  162. c->need_resched = 1;
  163. if (c->id == 0) {
  164. timer_set_hz(1000);
  165. } else {
  166. c->tick_ms = 1;
  167. lapic_timer_set_ms(1);
  168. }
  169. }
  170. void smp_resched_cpu(unsigned int cpu)
  171. {
  172. if (cpu >= cpu_count || cpu >= MAX_CPUS)
  173. return;
  174. if (cpu == this_cpu()->id) {
  175. smp_resched_wake();
  176. return;
  177. }
  178. if (cpus[cpu].lapic_id == 0xFFFFFFFFu)
  179. return;
  180. lapic_send_ipi(cpus[cpu].lapic_id, LAPIC_RESCHED_VECTOR);
  181. }
  182. void smp_enable_sched(void)
  183. {
  184. unsigned int i;
  185. if (cpu_count <= 1)
  186. return;
  187. smp_sched_enabled = 1;
  188. for (i = 1; i < cpu_count; i++)
  189. smp_resched_cpu(i);
  190. }
  191. void smp_stop_aps(void)
  192. {
  193. unsigned int i, n;
  194. /* The kexec trampoline is about to overwrite the kernel. Stop taking
  195. * interrupts first (also on UP, so the final copy cannot be
  196. * preempted), park every AP, and wait long enough for INIT to take
  197. * effect. An AP may have been inside a scheduler critical section, so
  198. * the lock is reinitialized afterwards; the old run queue is dead
  199. * anyway and the new kernel re-creates everything. */
  200. __asm__ volatile ("cli");
  201. if (cpu_count <= 1)
  202. return;
  203. for (i = 1; i < cpu_count; i++) {
  204. if (cpus[i].lapic_id != 0xFFFFFFFFu)
  205. lapic_send_init(cpus[i].lapic_id);
  206. }
  207. /* A few million PAUSE iterations is well over the 10 ms the INIT
  208. * protocol requires on the Atom/P5 range, and costs nothing on the
  209. * few occasions a kexec runs. */
  210. for (n = 0; n < 4000000u; n++)
  211. __asm__ volatile ("pause");
  212. sched_lock_reset();
  213. }
  214. void smp_dump(void)
  215. {
  216. serial_puts(SERIAL_COM1, "smp: online ");
  217. serial_putdec(SERIAL_COM1, cpu_count);
  218. serial_puts(SERIAL_COM1, "\n");
  219. }