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
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
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
36 #include <sys/interrupt.h>
37 #include <sys/kernel.h>
38 #include <sys/memrange.h>
40 #include <sys/types.h>
43 #include <vm/vm_extern.h>
44 #include <vm/vm_kern.h>
45 #include <vm/vm_object.h>
46 #include <vm/vm_page.h>
48 #include <sys/mplock2.h>
50 #include <machine/cpu.h>
51 #include <machine/cpufunc.h>
52 #include <machine/globaldata.h>
53 #include <machine/md_var.h>
54 #include <machine/pmap.h>
55 #include <machine/smp.h>
56 #include <machine/tls.h>
57 #include <machine/param.h>
64 extern pt_entry_t *KPTphys;
66 extern int vmm_enabled;
68 volatile cpumask_t stopped_cpus;
69 /* which cpus are ready for IPIs etc? */
70 cpumask_t smp_active_mask = CPUMASK_INITIALIZER_ONLYONE;
71 static int boot_address;
72 /* which cpus have been started */
73 static cpumask_t smp_startup_mask = CPUMASK_INITIALIZER_ONLYONE;
74 int mp_naps; /* # of Applications processors */
77 /* Local data for detecting CPU TOPOLOGY */
78 static int core_bits = 0;
79 static int logical_CPU_bits = 0;
81 /* function prototypes XXX these should go elsewhere */
82 void bootstrap_idle(void);
83 void single_cpu_ipi(int, int, int);
84 void selected_cpu_ipi(cpumask_t, int, int);
86 void ipi_handler(int);
91 /* AP uses this during bootstrap. Do not staticize. */
96 /* XXX these need to go into the appropriate header file */
97 static int start_all_aps(u_int);
98 void init_secondary(void);
99 void *start_ap(void *);
102 * Get SMP fully working before we start initializing devices.
110 kprintf("Finish MP startup\n");
112 /* build our map of 'other' CPUs */
113 mycpu->gd_other_cpus = smp_startup_mask;
114 CPUMASK_NANDBIT(mycpu->gd_other_cpus, mycpu->gd_cpuid);
117 * Let the other cpu's finish initializing and build their map
121 while (CPUMASK_CMPMASKNEQ(smp_active_mask,smp_startup_mask)) {
126 while (try_mplock() == 0)
129 kprintf("Active CPU Mask: %08lx\n", (long)CPUMASK_LOWMASK(smp_active_mask));
132 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
135 start_ap(void *arg __unused)
141 return(NULL); /* NOTREACHED */
144 /* storage for AP thread IDs */
145 pthread_t ap_tids[MAXCPU];
157 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
158 for (shift = 0; (1 << shift) <= ncpus; ++shift)
161 ncpus2_shift = shift;
163 ncpus2_mask = ncpus2 - 1;
165 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
166 if ((1 << shift) < ncpus)
168 ncpus_fit = 1 << shift;
169 ncpus_fit_mask = ncpus_fit - 1;
172 * cpu0 initialization
174 ipiq_size = sizeof(struct lwkt_ipiq) * ncpus;
175 mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map, ipiq_size);
176 bzero(mycpu->gd_ipiq, ipiq_size);
181 start_all_aps(boot_address);
190 kprintf("DragonFly/MP: Multiprocessor\n");
191 kprintf(" cpu0 (BSP)\n");
193 for (x = 1; x <= mp_naps; ++x)
194 kprintf(" cpu%d (AP)\n", x);
198 cpu_send_ipiq(int dcpu)
200 if (CPUMASK_TESTBIT(smp_active_mask, dcpu)) {
201 if (pthread_kill(ap_tids[dcpu], SIGUSR1) != 0)
202 panic("pthread_kill failed in cpu_send_ipiq");
205 panic("XXX cpu_send_ipiq()");
210 single_cpu_ipi(int cpu, int vector, int delivery_mode)
212 kprintf("XXX single_cpu_ipi\n");
216 selected_cpu_ipi(cpumask_t target, int vector, int delivery_mode)
219 while (CPUMASK_TESTNZERO(target)) {
220 int n = BSFCPUMASK(target);
221 CPUMASK_NANDBIT(target, n);
222 single_cpu_ipi(n, vector, delivery_mode);
228 stop_cpus(cpumask_t map)
230 CPUMASK_ANDMASK(map, smp_active_mask);
233 while (CPUMASK_TESTNZERO(map)) {
234 int n = BSFCPUMASK(map);
235 CPUMASK_NANDBIT(map, n);
236 ATOMIC_CPUMASK_ORBIT(stopped_cpus, n);
237 if (pthread_kill(ap_tids[n], SIGXCPU) != 0)
238 panic("stop_cpus: pthread_kill failed");
242 panic("XXX stop_cpus()");
249 restart_cpus(cpumask_t map)
251 CPUMASK_ANDMASK(map, smp_active_mask);
254 while (CPUMASK_TESTNZERO(map)) {
255 int n = BSFCPUMASK(map);
256 CPUMASK_NANDBIT(map, n);
257 ATOMIC_CPUMASK_NANDBIT(stopped_cpus, n);
258 if (pthread_kill(ap_tids[n], SIGXCPU) != 0)
259 panic("restart_cpus: pthread_kill failed");
263 panic("XXX restart_cpus()");
272 * Adjust smp_startup_mask to signal the BSP that we have started
273 * up successfully. Note that we do not yet hold the BGL. The BSP
274 * is waiting for our signal.
276 * We can't set our bit in smp_active_mask yet because we are holding
277 * interrupts physically disabled and remote cpus could deadlock
278 * trying to send us an IPI.
280 ATOMIC_CPUMASK_ORBIT(smp_startup_mask, mycpu->gd_cpuid);
284 * Interlock for finalization. Wait until mp_finish is non-zero,
285 * then get the MP lock.
287 * Note: We are in a critical section.
289 * Note: we are the idle thread, we can only spin.
291 * Note: The load fence is memory volatile and prevents the compiler
292 * from improperly caching mp_finish, and the cpu from improperly
296 while (mp_finish == 0) {
300 while (try_mplock() == 0)
303 /* BSP may have changed PTD while we're waiting for the lock */
306 /* Build our map of 'other' CPUs. */
307 mycpu->gd_other_cpus = smp_startup_mask;
308 CPUMASK_NANDBIT(mycpu->gd_other_cpus, mycpu->gd_cpuid);
310 kprintf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
313 /* Set memory range attributes for this CPU to match the BSP */
316 * Once we go active we must process any IPIQ messages that may
317 * have been queued, because no actual IPI will occur until we
318 * set our bit in the smp_active_mask. If we don't the IPI
319 * message interlock could be left set which would also prevent
322 * The idle loop doesn't expect the BGL to be held and while
323 * lwkt_switch() normally cleans things up this is a special case
324 * because we returning almost directly into the idle loop.
326 * The idle thread is never placed on the runq, make sure
327 * nothing we've done put it there.
329 KKASSERT(get_mplock_count(curthread) == 1);
330 ATOMIC_CPUMASK_ORBIT(smp_active_mask, mycpu->gd_cpuid);
332 mdcpu->gd_fpending = 0;
333 mdcpu->gd_ipending = 0;
334 initclocks_pcpu(); /* clock interrupts (via IPIs) */
338 * Releasing the mp lock lets the BSP finish up the SMP init
341 KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
348 struct mdglobaldata *md;
349 struct privatespace *ps;
351 ps = &CPU_prvspace[myid];
353 KKASSERT(ps->mdglobaldata.mi.gd_prvspace == ps);
356 * Setup the %gs for cpu #n. The mycpu macro works after this
357 * point. Note that %fs is used by pthreads.
359 tls_set_gs(&CPU_prvspace[myid], sizeof(struct privatespace));
361 md = mdcpu; /* loaded through %gs:0 (mdglobaldata.mi.gd_prvspace)*/
364 md->gd_common_tss.tss_rsp0 = 0; /* not used until after switch */
365 //md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
366 //md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
369 * Set to a known state:
370 * Set by mpboot.s: CR0_PG, CR0_PE
371 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
376 start_all_aps(u_int boot_addr)
379 struct mdglobaldata *gd;
380 struct privatespace *ps;
387 struct lwp_params params;
391 * needed for ipis to initial thread
392 * FIXME: rename ap_tids?
394 ap_tids[0] = pthread_self();
395 pthread_attr_init(&attr);
397 vm_object_hold(&kernel_object);
398 for (x = 1; x <= mp_naps; x++)
400 /* Allocate space for the CPU's private space. */
401 for (i = 0; i < sizeof(struct mdglobaldata); i += PAGE_SIZE) {
402 va =(vm_offset_t)&CPU_prvspace[x].mdglobaldata + i;
403 m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
404 pmap_kenter_quick(va, m->phys_addr);
407 for (i = 0; i < sizeof(CPU_prvspace[x].idlestack); i += PAGE_SIZE) {
408 va =(vm_offset_t)&CPU_prvspace[x].idlestack + i;
409 m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
410 pmap_kenter_quick(va, m->phys_addr);
413 gd = &CPU_prvspace[x].mdglobaldata; /* official location */
414 bzero(gd, sizeof(*gd));
415 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
417 /* prime data page for it to use */
418 mi_gdinit(&gd->mi, x);
422 gd->gd_CMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE1);
423 gd->gd_CMAP2 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE2);
424 gd->gd_CMAP3 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE3);
425 gd->gd_PMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].PPAGE1);
426 gd->gd_CADDR1 = ps->CPAGE1;
427 gd->gd_CADDR2 = ps->CPAGE2;
428 gd->gd_CADDR3 = ps->CPAGE3;
429 gd->gd_PADDR1 = (vpte_t *)ps->PPAGE1;
432 ipiq_size = sizeof(struct lwkt_ipiq) * (mp_naps + 1);
433 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, ipiq_size);
434 bzero(gd->mi.gd_ipiq, ipiq_size);
437 * Setup the AP boot stack
439 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
443 * Setup the AP's lwp, this is the 'cpu'
445 * We have to make sure our signals are masked or the new LWP
446 * may pick up a signal that it isn't ready for yet. SMP
447 * startup occurs after SI_BOOT2_LEAVE_CRIT so interrupts
448 * have already been enabled.
453 stack = mmap(NULL, KERNEL_STACK_SIZE,
454 PROT_READ|PROT_WRITE|PROT_EXEC,
456 if (stack == MAP_FAILED) {
457 panic("Unable to allocate stack for thread %d\n", x);
459 pthread_attr_setstack(&attr, stack, KERNEL_STACK_SIZE);
462 pthread_create(&ap_tids[x], &attr, start_ap, NULL);
465 while (CPUMASK_TESTBIT(smp_startup_mask, x) == 0) {
466 cpu_lfence(); /* XXX spin until the AP has started */
470 vm_object_drop(&kernel_object);
471 pthread_attr_destroy(&attr);
477 * CPU TOPOLOGY DETECTION FUNCTIONS.
481 detect_cpu_topology(void)
483 logical_CPU_bits = vkernel_b_arg;
484 core_bits = vkernel_B_arg;
488 get_chip_ID(int cpuid)
490 return get_apicid_from_cpuid(cpuid) >>
491 (logical_CPU_bits + core_bits);
495 get_core_number_within_chip(int cpuid)
497 return (get_apicid_from_cpuid(cpuid) >> logical_CPU_bits) &
498 ( (1 << core_bits) -1);
502 get_logical_CPU_number_within_core(int cpuid)
504 return get_apicid_from_cpuid(cpuid) &
505 ( (1 << logical_CPU_bits) -1);