vkernel{,64} - CPU topology support
[dragonfly.git] / sys / platform / vkernel64 / x86_64 / mp.c
CommitLineData
da673940
JG
1/*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
20 *
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.
da673940
JG
33 */
34
35
36#include <sys/interrupt.h>
37#include <sys/kernel.h>
38#include <sys/memrange.h>
39#include <sys/tls.h>
40#include <sys/types.h>
41
42#include <vm/vm_extern.h>
43#include <vm/vm_kern.h>
44#include <vm/vm_object.h>
45#include <vm/vm_page.h>
46
0e6594a8
SW
47#include <sys/mplock2.h>
48
da673940
JG
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>
56
57#include <unistd.h>
58#include <pthread.h>
59#include <signal.h>
60#include <stdio.h>
61
62extern pt_entry_t *KPTphys;
63
da23a592 64volatile cpumask_t stopped_cpus;
da673940
JG
65cpumask_t smp_active_mask = 1; /* which cpus are ready for IPIs etc? */
66static int boot_address;
67static cpumask_t smp_startup_mask = 1; /* which cpus have been started */
68int mp_naps; /* # of Applications processors */
69static int mp_finish;
70
9bea6114
MC
71/* Local data for detecting CPU TOPOLOGY */
72static int core_bits = 0;
73static int logical_CPU_bits = 0;
74
da673940
JG
75/* function prototypes XXX these should go elsewhere */
76void bootstrap_idle(void);
77void single_cpu_ipi(int, int, int);
da23a592 78void selected_cpu_ipi(cpumask_t, int, int);
da673940
JG
79#if 0
80void ipi_handler(int);
81#endif
82
83pt_entry_t *SMPpt;
84
85/* AP uses this during bootstrap. Do not staticize. */
86char *bootSTK;
87static int bootAP;
88
89
90/* XXX these need to go into the appropriate header file */
91static int start_all_aps(u_int);
92void init_secondary(void);
93void *start_ap(void *);
94
95/*
96 * Get SMP fully working before we start initializing devices.
97 */
98static
99void
100ap_finish(void)
101{
102 int i;
103 cpumask_t ncpus_mask = 0;
104
105 for (i = 1; i <= ncpus; i++)
da23a592 106 ncpus_mask |= CPUMASK(i);
da673940
JG
107
108 mp_finish = 1;
109 if (bootverbose)
110 kprintf("Finish MP startup\n");
111
112 /* build our map of 'other' CPUs */
da23a592 113 mycpu->gd_other_cpus = smp_startup_mask & ~CPUMASK(mycpu->gd_cpuid);
da673940
JG
114
115 /*
116 * Let the other cpu's finish initializing and build their map
117 * of 'other' CPUs.
118 */
119 rel_mplock();
120 while (smp_active_mask != smp_startup_mask) {
121 DELAY(100000);
122 cpu_lfence();
123 }
124
125 while (try_mplock() == 0)
126 DELAY(100000);
127 if (bootverbose)
b12defdc 128 kprintf("Active CPU Mask: %08lx\n", (long)smp_active_mask);
da673940
JG
129}
130
131SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
132
133
134void *
135start_ap(void *arg __unused)
136{
137 init_secondary();
138 setrealcpu();
139 bootstrap_idle();
140
141 return(NULL); /* NOTREACHED */
142}
143
144/* storage for AP thread IDs */
145pthread_t ap_tids[MAXCPU];
146
147void
148mp_start(void)
149{
150 int shift;
151
152 ncpus = optcpus;
153
154 mp_naps = ncpus - 1;
155
156 /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
157 for (shift = 0; (1 << shift) <= ncpus; ++shift)
158 ;
159 --shift;
160 ncpus2_shift = shift;
161 ncpus2 = 1 << shift;
162 ncpus2_mask = ncpus2 - 1;
163
164 /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
165 if ((1 << shift) < ncpus)
166 ++shift;
167 ncpus_fit = 1 << shift;
168 ncpus_fit_mask = ncpus_fit - 1;
169
170 /*
171 * cpu0 initialization
172 */
173 mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map,
174 sizeof(lwkt_ipiq) * ncpus);
175 bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
176
177 /*
178 * cpu 1-(n-1)
179 */
180 start_all_aps(boot_address);
181
182}
183
184void
185mp_announce(void)
186{
187 int x;
188
189 kprintf("DragonFly/MP: Multiprocessor\n");
190 kprintf(" cpu0 (BSP)\n");
191
192 for (x = 1; x <= mp_naps; ++x)
193 kprintf(" cpu%d (AP)\n", x);
194}
195
196void
da673940
JG
197cpu_send_ipiq(int dcpu)
198{
da0b0e8b 199 if (CPUMASK(dcpu) & smp_active_mask) {
da673940
JG
200 if (pthread_kill(ap_tids[dcpu], SIGUSR1) != 0)
201 panic("pthread_kill failed in cpu_send_ipiq");
da0b0e8b 202 }
da673940
JG
203#if 0
204 panic("XXX cpu_send_ipiq()");
205#endif
206}
207
208void
209smp_invltlb(void)
210{
211#ifdef SMP
212#endif
213}
214
215void
216single_cpu_ipi(int cpu, int vector, int delivery_mode)
217{
218 kprintf("XXX single_cpu_ipi\n");
219}
220
221void
da23a592 222selected_cpu_ipi(cpumask_t target, int vector, int delivery_mode)
da673940
JG
223{
224 crit_enter();
225 while (target) {
da23a592
MD
226 int n = BSFCPUMASK(target);
227 target &= ~CPUMASK(n);
da673940
JG
228 single_cpu_ipi(n, vector, delivery_mode);
229 }
230 crit_exit();
231}
232
233int
da23a592 234stop_cpus(cpumask_t map)
da673940
JG
235{
236 map &= smp_active_mask;
237
238 crit_enter();
239 while (map) {
da23a592
MD
240 int n = BSFCPUMASK(map);
241 map &= ~CPUMASK(n);
242 stopped_cpus |= CPUMASK(n);
da673940
JG
243 if (pthread_kill(ap_tids[n], SIGXCPU) != 0)
244 panic("stop_cpus: pthread_kill failed");
245 }
246 crit_exit();
247#if 0
248 panic("XXX stop_cpus()");
249#endif
250
251 return(1);
252}
253
254int
da23a592 255restart_cpus(cpumask_t map)
da673940
JG
256{
257 map &= smp_active_mask;
258
259 crit_enter();
260 while (map) {
da23a592
MD
261 int n = BSFCPUMASK(map);
262 map &= ~CPUMASK(n);
263 stopped_cpus &= ~CPUMASK(n);
da673940
JG
264 if (pthread_kill(ap_tids[n], SIGXCPU) != 0)
265 panic("restart_cpus: pthread_kill failed");
266 }
267 crit_exit();
268#if 0
269 panic("XXX restart_cpus()");
270#endif
271
272 return(1);
273}
274
275void
276ap_init(void)
277{
278 /*
279 * Adjust smp_startup_mask to signal the BSP that we have started
280 * up successfully. Note that we do not yet hold the BGL. The BSP
281 * is waiting for our signal.
282 *
283 * We can't set our bit in smp_active_mask yet because we are holding
284 * interrupts physically disabled and remote cpus could deadlock
285 * trying to send us an IPI.
286 */
da23a592 287 smp_startup_mask |= CPUMASK(mycpu->gd_cpuid);
da673940
JG
288 cpu_mfence();
289
290 /*
291 * Interlock for finalization. Wait until mp_finish is non-zero,
292 * then get the MP lock.
293 *
294 * Note: We are in a critical section.
295 *
da673940
JG
296 * Note: we are the idle thread, we can only spin.
297 *
298 * Note: The load fence is memory volatile and prevents the compiler
299 * from improperly caching mp_finish, and the cpu from improperly
300 * caching it.
301 */
302
303 while (mp_finish == 0) {
304 cpu_lfence();
305 DELAY(500000);
306 }
b5d16701 307 while (try_mplock() == 0)
da673940
JG
308 DELAY(100000);
309
310 /* BSP may have changed PTD while we're waiting for the lock */
311 cpu_invltlb();
312
313 /* Build our map of 'other' CPUs. */
da23a592 314 mycpu->gd_other_cpus = smp_startup_mask & ~CPUMASK(mycpu->gd_cpuid);
da673940
JG
315
316 kprintf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
317
318
319 /* Set memory range attributes for this CPU to match the BSP */
320 mem_range_AP_init();
321 /*
322 * Once we go active we must process any IPIQ messages that may
323 * have been queued, because no actual IPI will occur until we
324 * set our bit in the smp_active_mask. If we don't the IPI
325 * message interlock could be left set which would also prevent
326 * further IPIs.
327 *
328 * The idle loop doesn't expect the BGL to be held and while
329 * lwkt_switch() normally cleans things up this is a special case
330 * because we returning almost directly into the idle loop.
331 *
332 * The idle thread is never placed on the runq, make sure
333 * nothing we've done put it there.
334 */
b5d16701 335 KKASSERT(get_mplock_count(curthread) == 1);
da23a592 336 smp_active_mask |= CPUMASK(mycpu->gd_cpuid);
da673940
JG
337
338 mdcpu->gd_fpending = 0;
339 mdcpu->gd_ipending = 0;
340 initclocks_pcpu(); /* clock interrupts (via IPIs) */
341 lwkt_process_ipiq();
342
343 /*
344 * Releasing the mp lock lets the BSP finish up the SMP init
345 */
346 rel_mplock();
347 KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
348}
349
350void
351init_secondary(void)
352{
353 int myid = bootAP;
354 struct mdglobaldata *md;
355 struct privatespace *ps;
356
357 ps = &CPU_prvspace[myid];
358
359 KKASSERT(ps->mdglobaldata.mi.gd_prvspace == ps);
360
361 /*
362 * Setup the %gs for cpu #n. The mycpu macro works after this
363 * point. Note that %fs is used by pthreads.
364 */
365 tls_set_gs(&CPU_prvspace[myid], sizeof(struct privatespace));
366
367 md = mdcpu; /* loaded through %gs:0 (mdglobaldata.mi.gd_prvspace)*/
368
369 /* JG */
370 md->gd_common_tss.tss_rsp0 = 0; /* not used until after switch */
371 //md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
372 //md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
373
374 /*
375 * Set to a known state:
376 * Set by mpboot.s: CR0_PG, CR0_PE
377 * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
378 */
379}
380
381static int
382start_all_aps(u_int boot_addr)
383{
384 int x, i;
385 struct mdglobaldata *gd;
386 struct privatespace *ps;
387 vm_page_t m;
388 vm_offset_t va;
389#if 0
390 struct lwp_params params;
391#endif
392
393 /*
394 * needed for ipis to initial thread
395 * FIXME: rename ap_tids?
396 */
397 ap_tids[0] = pthread_self();
398
b12defdc 399 vm_object_hold(&kernel_object);
da673940
JG
400 for (x = 1; x <= mp_naps; x++)
401 {
402 /* Allocate space for the CPU's private space. */
da673940
JG
403 for (i = 0; i < sizeof(struct mdglobaldata); i += PAGE_SIZE) {
404 va =(vm_offset_t)&CPU_prvspace[x].mdglobaldata + i;
405 m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
406 pmap_kenter_quick(va, m->phys_addr);
407 }
408
409 for (i = 0; i < sizeof(CPU_prvspace[x].idlestack); i += PAGE_SIZE) {
410 va =(vm_offset_t)&CPU_prvspace[x].idlestack + i;
411 m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
412 pmap_kenter_quick(va, m->phys_addr);
413 }
414
415 gd = &CPU_prvspace[x].mdglobaldata; /* official location */
416 bzero(gd, sizeof(*gd));
417 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
418
419 /* prime data page for it to use */
420 mi_gdinit(&gd->mi, x);
421 cpu_gdinit(gd, x);
422
423#if 0
424 gd->gd_CMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE1);
425 gd->gd_CMAP2 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE2);
426 gd->gd_CMAP3 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE3);
427 gd->gd_PMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].PPAGE1);
428 gd->gd_CADDR1 = ps->CPAGE1;
429 gd->gd_CADDR2 = ps->CPAGE2;
430 gd->gd_CADDR3 = ps->CPAGE3;
431 gd->gd_PADDR1 = (vpte_t *)ps->PPAGE1;
432#endif
433
434 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
435 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
436
437 /*
438 * Setup the AP boot stack
439 */
440 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
441 bootAP = x;
442
443 /*
444 * Setup the AP's lwp, this is the 'cpu'
445 *
446 * We have to make sure our signals are masked or the new LWP
447 * may pick up a signal that it isn't ready for yet. SMP
448 * startup occurs after SI_BOOT2_LEAVE_CRIT so interrupts
449 * have already been enabled.
450 */
451 cpu_disable_intr();
452 pthread_create(&ap_tids[x], NULL, start_ap, NULL);
453 cpu_enable_intr();
454
da23a592 455 while((smp_startup_mask & CPUMASK(x)) == 0) {
da673940
JG
456 cpu_lfence(); /* XXX spin until the AP has started */
457 DELAY(1000);
458 }
459 }
b12defdc 460 vm_object_drop(&kernel_object);
da673940
JG
461
462 return(ncpus - 1);
463}
9bea6114
MC
464
465/*
466 * CPU TOPOLOGY DETECTION FUNCTIONS.
467 */
468
469void
470detect_cpu_topology(void)
471{
472 logical_CPU_bits = vkernel_b_arg;
473 core_bits = vkernel_B_arg;
474}
475
476int
477get_chip_ID(int cpuid)
478{
479 return get_apicid_from_cpuid(cpuid) >>
480 (logical_CPU_bits + core_bits);
481}
482
483int
484get_core_number_within_chip(int cpuid)
485{
486 return (get_apicid_from_cpuid(cpuid) >> logical_CPU_bits) &
487 ( (1 << core_bits) -1);
488}
489
490int
491get_logical_CPU_number_within_core(int cpuid)
492{
493 return get_apicid_from_cpuid(cpuid) &
494 ( (1 << logical_CPU_bits) -1);
495}
496