Also credit lots of help from Aggelos Economopoulos <aoiko@cc.ece.ntua.gr>
[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  * $DragonFly: src/sys/platform/vkernel/i386/mp.c,v 1.3 2007/07/01 04:02:33 dillon Exp $
35  */
36
37
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>
43
44 #include <vm/vm_extern.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_page.h>
48
49 #include <machine/cpufunc.h>
50 #include <machine/globaldata.h>
51 #include <machine/md_var.h>
52 #include <machine/pmap.h>
53 #include <machine/smp.h>
54 #include <machine/tls.h>
55
56 #include <unistd.h>
57 #include <pthread.h>
58 #include <signal.h>
59 #include <stdio.h>
60
61 extern pt_entry_t *KPTphys;
62
63 volatile u_int  stopped_cpus;
64 cpumask_t       smp_active_mask = 1;  /* which cpus are ready for IPIs etc? */
65 static int      boot_address;
66 static cpumask_t smp_startup_mask = 1;  /* which cpus have been started */
67 int             mp_naps;                /* # of Applications processors */
68 static int  mp_finish;
69
70 /* function prototypes XXX these should go elsewhere */
71 void bootstrap_idle(void);
72 void single_cpu_ipi(int, int, int);
73 void selected_cpu_ipi(u_int, int, int);
74 #if 0
75 void ipi_handler(int);
76 #endif
77
78 pt_entry_t *SMPpt;
79
80 /* AP uses this during bootstrap.  Do not staticize.  */
81 char *bootSTK;
82 static int bootAP;
83
84
85 /* XXX these need to go into the appropriate header file */
86 static int start_all_aps(u_int);
87 void init_secondary(void);
88 void *start_ap(void *);
89
90 /*
91  * Get SMP fully working before we start initializing devices.
92  */
93 static
94 void
95 ap_finish(void)
96 {
97         int i;
98         cpumask_t ncpus_mask = 0;
99
100         for (i = 1; i <= ncpus; i++)
101                 ncpus_mask |= (1 << i);
102
103         mp_finish = 1;
104         if (bootverbose)
105                 kprintf("Finish MP startup\n");
106
107         /* build our map of 'other' CPUs */
108         mycpu->gd_other_cpus = smp_startup_mask & ~(1 << 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 (smp_active_mask != smp_startup_mask)
116                 cpu_lfence();
117
118         while (try_mplock() == 0)
119                 ;
120         if (bootverbose)
121                 kprintf("Active CPU Mask: %08x\n", smp_active_mask);
122 }
123
124 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
125
126
127 void *
128 start_ap(void *arg __unused)
129 {
130         init_secondary();
131
132         bootstrap_idle();
133
134         return(NULL); /* NOTREACHED */
135 }
136
137 /* storage for AP thread IDs */
138 pthread_t ap_tids[MAXCPU];
139
140 void
141 mp_start(void)
142 {
143         int shift;
144
145         /* XXX testing 2 cpus */
146         ncpus = 2;
147
148         mp_naps = ncpus - 1;
149
150         /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
151         for (shift = 0; (1 << shift) <= ncpus; ++shift)
152                 ;
153         --shift;
154         ncpus2_shift = shift;
155         ncpus2 = 1 << shift;
156         ncpus2_mask = ncpus2 - 1;
157
158         /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
159         if ((1 << shift) < ncpus)
160                 ++shift;
161         ncpus_fit = 1 << shift;
162         ncpus_fit_mask = ncpus_fit - 1;
163
164         /*
165          * cpu0 initialization
166          */
167         mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map,
168                                             sizeof(lwkt_ipiq) * ncpus);
169         bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
170
171         /*
172          * cpu 1-(n-1)
173          */
174         start_all_aps(boot_address);
175
176 }
177
178 void
179 mp_announce(void)
180 {
181         int x;
182
183         kprintf("DragonFly/MP: Multiprocessor\n");
184         kprintf(" cpu0 (BSP)\n");
185
186         for (x = 1; x <= mp_naps; ++x)
187                 kprintf(" cpu%d (AP)\n", x);
188 }
189
190 void
191 forward_fastint_remote(void *arg)
192 {
193         panic("XXX forward_fastint_remote()");
194 }
195
196 void
197 cpu_send_ipiq(int dcpu)
198 {
199         if ((1 << dcpu) & smp_active_mask)
200                 if (pthread_kill(ap_tids[dcpu], SIGUSR1) != 0)
201                         panic("pthread_kill failed in cpu_send_ipiq");
202 #if 0
203         panic("XXX cpu_send_ipiq()");
204 #endif
205 }
206
207 void
208 smp_invltlb(void)
209 {
210 #ifdef SMP
211 #endif
212 }
213
214 void
215 single_cpu_ipi(int cpu, int vector, int delivery_mode)
216 {
217         kprintf("XXX single_cpu_ipi\n");
218 }
219
220 void
221 selected_cpu_ipi(u_int target, int vector, int delivery_mode)
222 {
223         crit_enter();
224         while (target) {
225                 int n = bsfl(target);
226                 target &= ~(1 << n);
227                 single_cpu_ipi(n, vector, delivery_mode);
228         }
229         crit_exit();
230 }
231
232 int
233 stop_cpus(u_int map)
234 {
235         map &= smp_active_mask;
236
237         crit_enter();
238         while (map) {
239                 int n = bsfl(map);
240                 map &= ~(1 << n);
241                 if (pthread_kill(ap_tids[n], SIGSTOP) != 0)
242                         panic("stop_cpus: pthread_kill failed");
243         }
244         crit_exit();
245 #if 0
246         panic("XXX stop_cpus()");
247 #endif
248
249         return(1);
250 }
251
252 int
253 restart_cpus(u_int map)
254 {
255         map &= smp_active_mask;
256
257         crit_enter();
258         while (map) {
259                 int n = bsfl(map);
260                 map &= ~(1 << n);
261                 if (pthread_kill(ap_tids[n], SIGCONT) != 0)
262                         panic("restart_cpus: pthread_kill failed");
263         }
264         crit_exit();
265 #if 0
266         panic("XXX restart_cpus()");
267 #endif
268
269         return(1);
270 }
271
272 void
273 ap_init(void)
274 {
275         /*
276          * Adjust smp_startup_mask to signal the BSP that we have started
277          * up successfully.  Note that we do not yet hold the BGL.  The BSP
278          * is waiting for our signal.
279          *
280          * We can't set our bit in smp_active_mask yet because we are holding
281          * interrupts physically disabled and remote cpus could deadlock
282          * trying to send us an IPI.
283          */
284         smp_startup_mask |= 1 << mycpu->gd_cpuid;
285         cpu_mfence();
286
287         /*
288          * Interlock for finalization.  Wait until mp_finish is non-zero,
289          * then get the MP lock.
290          *
291          * Note: We are in a critical section.
292          *
293          * Note: We have to synchronize td_mpcount to our desired MP state
294          * before calling cpu_try_mplock().
295          *
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         }
306         ++curthread->td_mpcount;
307         while (cpu_try_mplock() == 0)
308             ;
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. */
314         mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
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          */
335         KKASSERT(curthread->td_mpcount == 1);
336         smp_active_mask |= 1 << mycpu->gd_cpuid;
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
350 void
351 init_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.
364          */
365         tls_set_fs(&CPU_prvspace[myid], sizeof(struct privatespace));
366
367         md = mdcpu;     /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
368
369         md->gd_common_tss.tss_esp0 = 0; /* not used until after switch */
370         md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
371         md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
372
373         /*
374          * Set to a known state:
375          * Set by mpboot.s: CR0_PG, CR0_PE
376          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
377          */
378 }
379
380 static int
381 start_all_aps(u_int boot_addr)
382 {
383         int x, i;
384         struct mdglobaldata *gd;
385         struct privatespace *ps;
386         vm_page_t m;
387         vm_offset_t va;
388 #if 0
389         struct lwp_params params;
390 #endif
391
392         /*
393          * needed for ipis to initial thread
394          * FIXME: rename ap_tids?
395          */
396         ap_tids[0] = pthread_self();
397
398         for (x = 1; x <= mp_naps; x++)
399         {
400                 /* Allocate space for the CPU's private space. */
401                 va = (vm_offset_t)&CPU_prvspace[x];
402                 for (i = 0; i < sizeof(struct mdglobaldata); i += PAGE_SIZE) {
403                         va =(vm_offset_t)&CPU_prvspace[x].mdglobaldata + i;
404                         m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
405                         pmap_kenter_quick(va, m->phys_addr);
406                 }
407
408                 for (i = 0; i < sizeof(CPU_prvspace[x].idlestack); i += PAGE_SIZE) {
409                         va =(vm_offset_t)&CPU_prvspace[x].idlestack + i;
410                         m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
411                         pmap_kenter_quick(va, m->phys_addr);
412                 }
413
414                 gd = &CPU_prvspace[x].mdglobaldata;     /* official location */
415                 bzero(gd, sizeof(*gd));
416                 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
417
418                 /* prime data page for it to use */
419                 mi_gdinit(&gd->mi, x);
420                 cpu_gdinit(gd, x);
421
422 #if 0
423                 gd->gd_CMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE1);
424                 gd->gd_CMAP2 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE2);
425                 gd->gd_CMAP3 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE3);
426                 gd->gd_PMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].PPAGE1);
427                 gd->gd_CADDR1 = ps->CPAGE1;
428                 gd->gd_CADDR2 = ps->CPAGE2;
429                 gd->gd_CADDR3 = ps->CPAGE3;
430                 gd->gd_PADDR1 = (vpte_t *)ps->PPAGE1;
431 #endif
432
433                 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
434                 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
435
436                 /*
437                  * Setup the AP boot stack
438                  */
439                 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
440                 bootAP = x;
441
442                 /*
443                  * Setup the AP's lwp, this is the 'cpu'
444                  */
445                 pthread_create(&ap_tids[x], NULL, start_ap, NULL);
446
447                 while((smp_startup_mask & (1 << x)) == 0)
448                         cpu_lfence(); /* XXX spin until the AP has started */
449
450                 /* XXX hack, sleep for a second to let the APs start up */
451                 sleep(1);
452         }
453
454         return(ncpus - 1);
455 }