more header changes for amd64 port; the pc64 building infrastructure
[dragonfly.git] / sys / platform / pc64 / amd64 / 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/pc64/amd64/mp.c,v 1.1 2007/09/23 04:29:31 yanyh Exp $
35  * $DragonFly: src/sys/platform/pc64/amd64/mp.c,v 1.1 2007/09/23 04:29:31 yanyh Exp $
36  */
37
38
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/memrange.h>
42 #include <sys/tls.h>
43 #include <sys/types.h>
44
45 #include <vm/vm_extern.h>
46 #include <vm/vm_kern.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_page.h>
49
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
58 #include <unistd.h>
59 #include <pthread.h>
60 #include <signal.h>
61 #include <stdio.h>
62
63 extern pt_entry_t *KPTphys;
64
65 volatile u_int  stopped_cpus;
66 cpumask_t       smp_active_mask = 1;  /* which cpus are ready for IPIs etc? */
67 static int      boot_address;
68 static cpumask_t smp_startup_mask = 1;  /* which cpus have been started */
69 int             mp_naps;                /* # of Applications processors */
70 static int  mp_finish;
71
72 /* function prototypes XXX these should go elsewhere */
73 void bootstrap_idle(void);
74 void single_cpu_ipi(int, int, int);
75 void selected_cpu_ipi(u_int, int, int);
76 #if 0
77 void ipi_handler(int);
78 #endif
79
80 pt_entry_t *SMPpt;
81
82 /* AP uses this during bootstrap.  Do not staticize.  */
83 char *bootSTK;
84 static int bootAP;
85
86
87 /* XXX these need to go into the appropriate header file */
88 static int start_all_aps(u_int);
89 void init_secondary(void);
90 void *start_ap(void *);
91
92 /*
93  * Get SMP fully working before we start initializing devices.
94  */
95 static
96 void
97 ap_finish(void)
98 {
99         int i;
100         cpumask_t ncpus_mask = 0;
101
102         for (i = 1; i <= ncpus; i++)
103                 ncpus_mask |= (1 << i);
104
105         mp_finish = 1;
106         if (bootverbose)
107                 kprintf("Finish MP startup\n");
108
109         /* build our map of 'other' CPUs */
110         mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
111
112         /*
113          * Let the other cpu's finish initializing and build their map
114          * of 'other' CPUs.
115          */
116         rel_mplock();
117         while (smp_active_mask != smp_startup_mask) {
118                 DELAY(100000);
119                 cpu_lfence();
120         }
121
122         while (try_mplock() == 0)
123                 DELAY(100000);
124         if (bootverbose)
125                 kprintf("Active CPU Mask: %08x\n", smp_active_mask);
126 }
127
128 SYSINIT(finishsmp, SI_BOOT2_FINISH_SMP, SI_ORDER_FIRST, ap_finish, NULL)
129
130
131 void *
132 start_ap(void *arg __unused)
133 {
134         init_secondary();
135         setrealcpu();
136         bootstrap_idle();
137
138         return(NULL); /* NOTREACHED */
139 }
140
141 /* storage for AP thread IDs */
142 pthread_t ap_tids[MAXCPU];
143
144 void
145 mp_start(void)
146 {
147         int shift;
148
149         ncpus = optcpus;
150
151         mp_naps = ncpus - 1;
152
153         /* ncpus2 -- ncpus rounded down to the nearest power of 2 */
154         for (shift = 0; (1 << shift) <= ncpus; ++shift)
155                 ;
156         --shift;
157         ncpus2_shift = shift;
158         ncpus2 = 1 << shift;
159         ncpus2_mask = ncpus2 - 1;
160
161         /* ncpus_fit -- ncpus rounded up to the nearest power of 2 */
162         if ((1 << shift) < ncpus)
163                 ++shift;
164         ncpus_fit = 1 << shift;
165         ncpus_fit_mask = ncpus_fit - 1;
166
167         /*
168          * cpu0 initialization
169          */
170         mycpu->gd_ipiq = (void *)kmem_alloc(&kernel_map,
171                                             sizeof(lwkt_ipiq) * ncpus);
172         bzero(mycpu->gd_ipiq, sizeof(lwkt_ipiq) * ncpus);
173
174         /*
175          * cpu 1-(n-1)
176          */
177         start_all_aps(boot_address);
178
179 }
180
181 void
182 mp_announce(void)
183 {
184         int x;
185
186         kprintf("DragonFly/MP: Multiprocessor\n");
187         kprintf(" cpu0 (BSP)\n");
188
189         for (x = 1; x <= mp_naps; ++x)
190                 kprintf(" cpu%d (AP)\n", x);
191 }
192
193 void
194 forward_fastint_remote(void *arg)
195 {
196         panic("XXX forward_fastint_remote()");
197 }
198
199 void
200 cpu_send_ipiq(int dcpu)
201 {
202         if ((1 << dcpu) & smp_active_mask)
203                 if (pthread_kill(ap_tids[dcpu], SIGUSR1) != 0)
204                         panic("pthread_kill failed in cpu_send_ipiq");
205 #if 0
206         panic("XXX cpu_send_ipiq()");
207 #endif
208 }
209
210 void
211 smp_invltlb(void)
212 {
213 #ifdef SMP
214 #endif
215 }
216
217 void
218 single_cpu_ipi(int cpu, int vector, int delivery_mode)
219 {
220         kprintf("XXX single_cpu_ipi\n");
221 }
222
223 void
224 selected_cpu_ipi(u_int target, int vector, int delivery_mode)
225 {
226         crit_enter();
227         while (target) {
228                 int n = bsfl(target);
229                 target &= ~(1 << n);
230                 single_cpu_ipi(n, vector, delivery_mode);
231         }
232         crit_exit();
233 }
234
235 int
236 stop_cpus(u_int map)
237 {
238         map &= smp_active_mask;
239
240         crit_enter();
241         while (map) {
242                 int n = bsfl(map);
243                 map &= ~(1 << n);
244                 if (pthread_kill(ap_tids[n], SIGSTOP) != 0)
245                         panic("stop_cpus: pthread_kill failed");
246         }
247         crit_exit();
248 #if 0
249         panic("XXX stop_cpus()");
250 #endif
251
252         return(1);
253 }
254
255 int
256 restart_cpus(u_int map)
257 {
258         map &= smp_active_mask;
259
260         crit_enter();
261         while (map) {
262                 int n = bsfl(map);
263                 map &= ~(1 << n);
264                 if (pthread_kill(ap_tids[n], SIGCONT) != 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
275 void
276 ap_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          */
287         smp_startup_mask |= 1 << mycpu->gd_cpuid;
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          *
296          * Note: We have to synchronize td_mpcount to our desired MP state
297          * before calling cpu_try_mplock().
298          *
299          * Note: we are the idle thread, we can only spin.
300          *
301          * Note: The load fence is memory volatile and prevents the compiler
302          * from improperly caching mp_finish, and the cpu from improperly
303          * caching it.
304          */
305
306         while (mp_finish == 0) {
307                 cpu_lfence();
308                 DELAY(500000);
309         }
310         ++curthread->td_mpcount;
311         while (cpu_try_mplock() == 0)
312                 DELAY(100000);
313
314         /* BSP may have changed PTD while we're waiting for the lock */
315         cpu_invltlb();
316
317         /* Build our map of 'other' CPUs. */
318         mycpu->gd_other_cpus = smp_startup_mask & ~(1 << mycpu->gd_cpuid);
319
320         kprintf("SMP: AP CPU #%d Launched!\n", mycpu->gd_cpuid);
321
322
323         /* Set memory range attributes for this CPU to match the BSP */
324         mem_range_AP_init();
325         /*
326          * Once we go active we must process any IPIQ messages that may
327          * have been queued, because no actual IPI will occur until we
328          * set our bit in the smp_active_mask.  If we don't the IPI
329          * message interlock could be left set which would also prevent
330          * further IPIs.
331          *
332          * The idle loop doesn't expect the BGL to be held and while
333          * lwkt_switch() normally cleans things up this is a special case
334          * because we returning almost directly into the idle loop.
335          *
336          * The idle thread is never placed on the runq, make sure
337          * nothing we've done put it there.
338          */
339         KKASSERT(curthread->td_mpcount == 1);
340         smp_active_mask |= 1 << mycpu->gd_cpuid;
341
342         mdcpu->gd_fpending = 0;
343         mdcpu->gd_ipending = 0;
344         initclocks_pcpu();      /* clock interrupts (via IPIs) */
345         lwkt_process_ipiq();
346
347         /*
348          * Releasing the mp lock lets the BSP finish up the SMP init
349          */
350         rel_mplock();
351         KKASSERT((curthread->td_flags & TDF_RUNQ) == 0);
352 }
353
354 void
355 init_secondary(void)
356 {
357         int     myid = bootAP;
358         struct mdglobaldata *md;
359         struct privatespace *ps;
360
361         ps = &CPU_prvspace[myid];
362
363         KKASSERT(ps->mdglobaldata.mi.gd_prvspace == ps);
364
365         /*
366          * Setup the %gs for cpu #n.  The mycpu macro works after this
367          * point.
368          */
369         tls_set_fs(&CPU_prvspace[myid], sizeof(struct privatespace));
370
371         md = mdcpu;     /* loaded through %fs:0 (mdglobaldata.mi.gd_prvspace)*/
372
373         md->gd_common_tss.tss_esp0 = 0; /* not used until after switch */
374         md->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
375         md->gd_common_tss.tss_ioopt = (sizeof md->gd_common_tss) << 16;
376
377         /*
378          * Set to a known state:
379          * Set by mpboot.s: CR0_PG, CR0_PE
380          * Set by cpu_setregs: CR0_NE, CR0_MP, CR0_TS, CR0_WP, CR0_AM
381          */
382 }
383
384 static int
385 start_all_aps(u_int boot_addr)
386 {
387         int x, i;
388         struct mdglobaldata *gd;
389         struct privatespace *ps;
390         vm_page_t m;
391         vm_offset_t va;
392 #if 0
393         struct lwp_params params;
394 #endif
395
396         /*
397          * needed for ipis to initial thread
398          * FIXME: rename ap_tids?
399          */
400         ap_tids[0] = pthread_self();
401
402         for (x = 1; x <= mp_naps; x++)
403         {
404                 /* Allocate space for the CPU's private space. */
405                 va = (vm_offset_t)&CPU_prvspace[x];
406                 for (i = 0; i < sizeof(struct mdglobaldata); i += PAGE_SIZE) {
407                         va =(vm_offset_t)&CPU_prvspace[x].mdglobaldata + i;
408                         m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
409                         pmap_kenter_quick(va, m->phys_addr);
410                 }
411
412                 for (i = 0; i < sizeof(CPU_prvspace[x].idlestack); i += PAGE_SIZE) {
413                         va =(vm_offset_t)&CPU_prvspace[x].idlestack + i;
414                         m = vm_page_alloc(&kernel_object, va, VM_ALLOC_SYSTEM);
415                         pmap_kenter_quick(va, m->phys_addr);
416                 }
417
418                 gd = &CPU_prvspace[x].mdglobaldata;     /* official location */
419                 bzero(gd, sizeof(*gd));
420                 gd->mi.gd_prvspace = ps = &CPU_prvspace[x];
421
422                 /* prime data page for it to use */
423                 mi_gdinit(&gd->mi, x);
424                 cpu_gdinit(gd, x);
425
426 #if 0
427                 gd->gd_CMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE1);
428                 gd->gd_CMAP2 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE2);
429                 gd->gd_CMAP3 = pmap_kpte((vm_offset_t)CPU_prvspace[x].CPAGE3);
430                 gd->gd_PMAP1 = pmap_kpte((vm_offset_t)CPU_prvspace[x].PPAGE1);
431                 gd->gd_CADDR1 = ps->CPAGE1;
432                 gd->gd_CADDR2 = ps->CPAGE2;
433                 gd->gd_CADDR3 = ps->CPAGE3;
434                 gd->gd_PADDR1 = (vpte_t *)ps->PPAGE1;
435 #endif
436
437                 gd->mi.gd_ipiq = (void *)kmem_alloc(&kernel_map, sizeof(lwkt_ipiq) * (mp_naps + 1));
438                 bzero(gd->mi.gd_ipiq, sizeof(lwkt_ipiq) * (mp_naps + 1));
439
440                 /*
441                  * Setup the AP boot stack
442                  */
443                 bootSTK = &ps->idlestack[UPAGES*PAGE_SIZE/2];
444                 bootAP = x;
445
446                 /*
447                  * Setup the AP's lwp, this is the 'cpu'
448                  */
449                 pthread_create(&ap_tids[x], NULL, start_ap, NULL);
450
451                 while((smp_startup_mask & (1 << x)) == 0) {
452                         cpu_lfence(); /* XXX spin until the AP has started */
453                         DELAY(1000);
454                 }
455         }
456
457         return(ncpus - 1);
458 }