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