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