amd64 port: mainly on the pmap headers, identify_cpu and initcpu
[dragonfly/port-amd64.git] / sys / platform / pc64 / amd64 / mp.c
blob0ae3b46e48fe3a7fd5e151a58c6c59d59941798d
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/platform/pc64/amd64/mp.c,v 1.2 2007/09/24 03:24:45 yanyh Exp $
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/memrange.h>
41 #include <sys/tls.h>
42 #include <sys/types.h>
44 #include <vm/vm_extern.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_page.h>
49 #include <machine/cpu.h>
50 #include <machine/cpufunc.h>
51 #include <machine/globaldata.h>
52 #include <machine/md_var.h>
53 #include <machine/pmap.h>
54 #include <machine/smp.h>
55 #include <machine/tls.h>
57 #include <unistd.h>
58 #include <pthread.h>
59 #include <signal.h>
60 #include <stdio.h>
62 extern pt_entry_t *KPTphys;
64 volatile u_int stopped_cpus;
65 cpumask_t smp_active_mask = 1; /* which cpus are ready for IPIs etc? */
66 static int boot_address;
67 static cpumask_t smp_startup_mask = 1; /* which cpus have been started */
68 int mp_naps; /* # of Applications processors */
69 static int mp_finish;
71 /* function prototypes XXX these should go elsewhere */
72 void bootstrap_idle(void);
73 void single_cpu_ipi(int, int, int);
74 void selected_cpu_ipi(u_int, int, int);
75 #if 0
76 void ipi_handler(int);
77 #endif
79 pt_entry_t *SMPpt;
81 /* AP uses this during bootstrap. Do not staticize. */
82 char *bootSTK;
83 static int bootAP;
86 /* XXX these need to go into the appropriate header file */
87 static int start_all_aps(u_int);
88 void init_secondary(void);
89 void *start_ap(void *);
92 * Get SMP fully working before we start initializing devices.
94 static
95 void
96 ap_finish(void)
98 int i;
99 cpumask_t ncpus_mask = 0;
101 for (i = 1; i <= ncpus; i++)
102 ncpus_mask |= (1 << i);
104 mp_finish = 1;
105 if (bootverbose)
106 kprintf("Finish MP startup\n");
108 /* build our map of 'other' CPUs */
109 mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
112 * Let the other cpu's finish initializing and build their map
113 * of 'other' CPUs.
115 rel_mplock();
116 while (smp_active_mask != smp_startup_mask) {
117 DELAY(100000);
118 cpu_lfence();
121 while (try_mplock() == 0)
122 DELAY(100000);
123 if (bootverbose)
124 kprintf("Active CPU Mask: %08x\n", smp_active_mask);
127 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
130 void *
131 start_ap(void *arg __unused)
133 init_secondary();
134 setrealcpu();
135 bootstrap_idle();
137 return(NULL); /* NOTREACHED */
140 /* storage for AP thread IDs */
141 pthread_t ap_tids[MAXCPU];
143 void
144 mp_start(void)
146 int shift;
148 ncpus = optcpus;
150 mp_naps = ncpus - 1;
152 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
153 for (shift = 0; (1 << shift) <= ncpus; ++shift)
155 --shift;
156 ncpus2_shift = shift;
157 ncpus2 = 1 << shift;
158 ncpus2_mask = ncpus2 - 1;
160 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
161 if ((1 << shift) < ncpus)
162 ++shift;
163 ncpus_fit = 1 << shift;
164 ncpus_fit_mask = ncpus_fit - 1;
167 * cpu0 initialization
169 mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map,
170 sizeof(lwkt_ipiq) * ncpus);
171 bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
174 * cpu 1-(n-1)
176 start_all_aps(boot_address);
180 void
181 mp_announce(void)
183 int x;
185 kprintf("DragonFly/MP: Multiprocessor\n");
186 kprintf(" cpu0 (BSP)\n");
188 for (x = 1; x <= mp_naps; ++x)
189 kprintf(" cpu%d (AP)\n", x);
192 void
193 forward_fastint_remote(void *arg)
195 panic("XXX forward_fastint_remote()");
198 void
199 cpu_send_ipiq(int dcpu)
201 if ((1 << dcpu) & smp_active_mask)
202 if (pthread_kill(ap_tids[dcpu], SIGUSR1) != 0)
203 panic("pthread_kill failed in cpu_send_ipiq");
204 #if 0
205 panic("XXX cpu_send_ipiq()");
206 #endif
209 void
210 smp_invltlb(void)
212 #ifdef SMP
213 #endif
216 void
217 single_cpu_ipi(int cpu, int vector, int delivery_mode)
219 kprintf("XXX single_cpu_ipi\n");
222 void
223 selected_cpu_ipi(u_int target, int vector, int delivery_mode)
225 crit_enter();
226 while (target) {
227 int n = bsfl(target);
228 target &= ~(1 << n);
229 single_cpu_ipi(n, vector, delivery_mode);
231 crit_exit();
235 stop_cpus(u_int map)
237 map &= smp_active_mask;
239 crit_enter();
240 while (map) {
241 int n = bsfl(map);
242 map &= ~(1 << n);
243 if (pthread_kill(ap_tids[n], SIGSTOP) != 0)
244 panic("stop_cpus: pthread_kill failed");
246 crit_exit();
247 #if 0
248 panic("XXX stop_cpus()");
249 #endif
251 return(1);
255 restart_cpus(u_int map)
257 map &= smp_active_mask;
259 crit_enter();
260 while (map) {
261 int n = bsfl(map);
262 map &= ~(1 << n);
263 if (pthread_kill(ap_tids[n], SIGCONT) != 0)
264 panic("restart_cpus: pthread_kill failed");
266 crit_exit();
267 #if 0
268 panic("XXX restart_cpus()");
269 #endif
271 return(1);
274 void
275 ap_init(void)
278 * Adjust smp_startup_mask to signal the BSP that we have started
279 * up successfully. Note that we do not yet hold the BGL. The BSP
280 * is waiting for our signal.
282 * We can't set our bit in smp_active_mask yet because we are holding
283 * interrupts physically disabled and remote cpus could deadlock
284 * trying to send us an IPI.
286 smp_startup_mask |= 1 << mycpu->gd_cpuid;
287 cpu_mfence();
290 * Interlock for finalization. Wait until mp_finish is non-zero,
291 * then get the MP lock.
293 * Note: We are in a critical section.
295 * Note: We have to synchronize td_mpcount to our desired MP state
296 * before calling cpu_try_mplock().
298 * Note: we are the idle thread, we can only spin.
300 * Note: The load fence is memory volatile and prevents the compiler
301 * from improperly caching mp_finish, and the cpu from improperly
302 * caching it.
305 while (mp_finish == 0) {
306 cpu_lfence();
307 DELAY(500000);
309 ++curthread->td_mpcount;
310 while (cpu_try_mplock() == 0)
311 DELAY(100000);
313 /* BSP may have changed PTD while we're waiting for the lock */
314 cpu_invltlb();
316 /* Build our map of 'other' CPUs. */
317 mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
319 kprintf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
322 /* Set memory range attributes for this CPU to match the BSP */
323 mem_range_AP_init();
325 * Once we go active we must process any IPIQ messages that may
326 * have been queued, because no actual IPI will occur until we
327 * set our bit in the smp_active_mask. If we don't the IPI
328 * message interlock could be left set which would also prevent
329 * further IPIs.
331 * The idle loop doesn't expect the BGL to be held and while
332 * lwkt_switch() normally cleans things up this is a special case
333 * because we returning almost directly into the idle loop.
335 * The idle thread is never placed on the runq, make sure
336 * nothing we've done put it there.
338 KKASSERT(curthread->td_mpcount == 1);
339 smp_active_mask |= 1 << mycpu->gd_cpuid;
341 mdcpu->gd_fpending = 0;
342 mdcpu->gd_ipending = 0;
343 initclocks_pcpu(); /* clock interrupts (via IPIs) */
344 lwkt_process_ipiq();
347 * Releasing the mp lock lets the BSP finish up the SMP init
349 rel_mplock();
350 KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
353 void
354 init_secondary(void)
356 int myid = bootAP;
357 struct mdglobaldata *md;
358 struct privatespace *ps;
360 ps = &CPU_prvspace[myid];
362 KKASSERT(ps->mdglobaldata.mi.gd_prvspace == ps);
365 * Setup the %gs for cpu #n. The mycpu macro works after this
366 * point.
368 tls_set_fs(&CPU_prvspace[myid], sizeof(struct privatespace));
370 md = mdcpu; /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
372 md->gd_common_tss.tss_esp0 = 0; /* not used until after switch */
373 md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
374 md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
377 * Set to a known state:
378 * Set by mpboot.s: CR0_PG, CR0_PE
379 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
383 static int
384 start_all_aps(u_int boot_addr)
386 int x, i;
387 struct mdglobaldata *gd;
388 struct privatespace *ps;
389 vm_page_t m;
390 vm_offset_t va;
391 #if 0
392 struct lwp_params params;
393 #endif
396 * needed for ipis to initial thread
397 * FIXME: rename ap_tids?
399 ap_tids[0] = pthread_self();
401 for (x = 1; x <= mp_naps; x++)
403 /* Allocate space for the CPU's private space. */
404 va = (vm_offset_t)&CPU_prvspace[x];
405 for (i = 0; i < sizeof(struct mdglobaldata); i += PAGE_SIZE) {
406 va =(vm_offset_t)&CPU_prvspace[x].mdglobaldata + i;
407 m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
408 pmap_kenter_quick(va, m->phys_addr);
411 for (i = 0; i < sizeof(CPU_prvspace[x].idlestack); i += PAGE_SIZE) {
412 va =(vm_offset_t)&CPU_prvspace[x].idlestack + i;
413 m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
414 pmap_kenter_quick(va, m->phys_addr);
417 gd = &CPU_prvspace[x].mdglobaldata; /* official location */
418 bzero(gd, sizeof(*gd));
419 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
421 /* prime data page for it to use */
422 mi_gdinit(&gd->mi, x);
423 cpu_gdinit(gd, x);
425 #if 0
426 gd->gd_CMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE1);
427 gd->gd_CMAP2 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE2);
428 gd->gd_CMAP3 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE3);
429 gd->gd_PMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].PPAGE1);
430 gd->gd_CADDR1 = ps->CPAGE1;
431 gd->gd_CADDR2 = ps->CPAGE2;
432 gd->gd_CADDR3 = ps->CPAGE3;
433 gd->gd_PADDR1 = (vpte_t *)ps->PPAGE1;
434 #endif
436 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
437 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
440 * Setup the AP boot stack
442 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
443 bootAP = x;
446 * Setup the AP's lwp, this is the 'cpu'
448 pthread_create(&ap_tids[x], NULL, start_ap, NULL);
450 while((smp_startup_mask & (1 << x)) == 0) {
451 cpu_lfence(); /* XXX spin until the AP has started */
452 DELAY(1000);
456 return(ncpus - 1);