mp_machdep.c revision 212541
1/*- 2 * Copyright (c) 2008 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/powerpc/powerpc/mp_machdep.c 212541 2010-09-13 07:25:35Z mav $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/ktr.h> 34#include <sys/bus.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/pcpu.h> 38#include <sys/proc.h> 39#include <sys/sched.h> 40#include <sys/smp.h> 41 42#include <vm/vm.h> 43#include <vm/vm_param.h> 44#include <vm/pmap.h> 45#include <vm/vm_map.h> 46#include <vm/vm_extern.h> 47#include <vm/vm_kern.h> 48 49#include <machine/bus.h> 50#include <machine/cpu.h> 51#include <machine/intr_machdep.h> 52#include <machine/platform.h> 53#include <machine/md_var.h> 54#include <machine/smp.h> 55 56#include "pic_if.h" 57 58extern struct pcpu __pcpu[MAXCPU]; 59 60volatile static int ap_awake; 61volatile static u_int ap_letgo; 62volatile static u_quad_t ap_timebase; 63static u_int ipi_msg_cnt[32]; 64static struct mtx ap_boot_mtx; 65 66void 67machdep_ap_bootstrap(void) 68{ 69 /* Set up important bits on the CPU (HID registers, etc.) */ 70 cpudep_ap_setup(); 71 72 /* Set PIR */ 73 PCPU_SET(pir, mfspr(SPR_PIR)); 74 PCPU_SET(awake, 1); 75 __asm __volatile("msync; isync"); 76 77 while (ap_letgo == 0) 78 ; 79 80 /* Initialize DEC and TB, sync with the BSP values */ 81 mttb(ap_timebase); 82 decr_ap_init(); 83 84 /* Serialize console output and AP count increment */ 85 mtx_lock_spin(&ap_boot_mtx); 86 ap_awake++; 87 printf("SMP: AP CPU #%d launched\n", PCPU_GET(cpuid)); 88 mtx_unlock_spin(&ap_boot_mtx); 89 90 /* Initialize curthread */ 91 PCPU_SET(curthread, PCPU_GET(idlethread)); 92 PCPU_SET(curpcb, curthread->td_pcb); 93 94 /* Let the DEC and external interrupts go */ 95 mtmsr(mfmsr() | PSL_EE); 96 97 /* Start per-CPU event timers. */ 98 cpu_initclocks_ap(); 99 100 /* Announce ourselves awake, and enter the scheduler */ 101 sched_throw(NULL); 102} 103 104struct cpu_group * 105cpu_topo(void) 106{ 107 108 return (smp_topo_none()); 109} 110 111void 112cpu_mp_setmaxid(void) 113{ 114 struct cpuref cpuref; 115 int error; 116 117 mp_ncpus = 0; 118 error = platform_smp_first_cpu(&cpuref); 119 while (!error) { 120 mp_ncpus++; 121 error = platform_smp_next_cpu(&cpuref); 122 } 123 /* Sanity. */ 124 if (mp_ncpus == 0) 125 mp_ncpus = 1; 126 127 /* 128 * Set the largest cpuid we're going to use. This is necessary 129 * for VM initialization. 130 */ 131 mp_maxid = min(mp_ncpus, MAXCPU) - 1; 132} 133 134int 135cpu_mp_probe(void) 136{ 137 138 /* 139 * We're not going to enable SMP if there's only 1 processor. 140 */ 141 return (mp_ncpus > 1); 142} 143 144void 145cpu_mp_start(void) 146{ 147 struct cpuref bsp, cpu; 148 struct pcpu *pc; 149 int error; 150 151 error = platform_smp_get_bsp(&bsp); 152 KASSERT(error == 0, ("Don't know BSP")); 153 KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__)); 154 155 error = platform_smp_first_cpu(&cpu); 156 while (!error) { 157 if (cpu.cr_cpuid >= MAXCPU) { 158 printf("SMP: cpu%d: skipped -- ID out of range\n", 159 cpu.cr_cpuid); 160 goto next; 161 } 162 if (all_cpus & (1 << cpu.cr_cpuid)) { 163 printf("SMP: cpu%d: skipped - duplicate ID\n", 164 cpu.cr_cpuid); 165 goto next; 166 } 167 if (cpu.cr_cpuid != bsp.cr_cpuid) { 168 void *dpcpu; 169 170 pc = &__pcpu[cpu.cr_cpuid]; 171 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE); 172 pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc)); 173 dpcpu_init(dpcpu, cpu.cr_cpuid); 174 } else { 175 pc = pcpup; 176 pc->pc_cpuid = bsp.cr_cpuid; 177 pc->pc_bsp = 1; 178 } 179 pc->pc_cpumask = 1 << pc->pc_cpuid; 180 pc->pc_hwref = cpu.cr_hwref; 181 all_cpus |= pc->pc_cpumask; 182next: 183 error = platform_smp_next_cpu(&cpu); 184 } 185} 186 187void 188cpu_mp_announce(void) 189{ 190 struct pcpu *pc; 191 int i; 192 193 for (i = 0; i <= mp_maxid; i++) { 194 pc = pcpu_find(i); 195 if (pc == NULL) 196 continue; 197 printf("cpu%d: dev=%x", i, (int)pc->pc_hwref); 198 if (pc->pc_bsp) 199 printf(" (BSP)"); 200 printf("\n"); 201 } 202} 203 204static void 205cpu_mp_unleash(void *dummy) 206{ 207 struct pcpu *pc; 208 int cpus, timeout; 209 210 if (mp_ncpus <= 1) 211 return; 212 213 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 214 215 cpus = 0; 216 smp_cpus = 0; 217 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 218 cpus++; 219 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask; 220 if (!pc->pc_bsp) { 221 if (bootverbose) 222 printf("Waking up CPU %d (dev=%x)\n", 223 pc->pc_cpuid, (int)pc->pc_hwref); 224 225 platform_smp_start_cpu(pc); 226 227 timeout = 2000; /* wait 2sec for the AP */ 228 while (!pc->pc_awake && --timeout > 0) 229 DELAY(1000); 230 231 } else { 232 PCPU_SET(pir, mfspr(SPR_PIR)); 233 pc->pc_awake = 1; 234 } 235 if (pc->pc_awake) { 236 if (bootverbose) 237 printf("Adding CPU %d, pir=%x, awake=%x\n", 238 pc->pc_cpuid, pc->pc_pir, pc->pc_awake); 239 smp_cpus++; 240 } else 241 stopped_cpus |= (1 << pc->pc_cpuid); 242 } 243 244 ap_awake = 1; 245 246 /* Provide our current DEC and TB values for APs */ 247 ap_timebase = mftb() + 10; 248 __asm __volatile("msync; isync"); 249 250 /* Let APs continue */ 251 atomic_store_rel_int(&ap_letgo, 1); 252 253 mttb(ap_timebase); 254 255 while (ap_awake < smp_cpus) 256 ; 257 258 if (smp_cpus != cpus || cpus != mp_ncpus) { 259 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n", 260 mp_ncpus, cpus, smp_cpus); 261 } 262 263 /* Let the APs get into the scheduler */ 264 DELAY(10000); 265 266 smp_active = 1; 267 smp_started = 1; 268} 269 270SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL); 271 272int 273powerpc_ipi_handler(void *arg) 274{ 275 cpumask_t self; 276 uint32_t ipimask; 277 int msg; 278 279 CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr()); 280 281 ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask)); 282 if (ipimask == 0) 283 return (FILTER_STRAY); 284 while ((msg = ffs(ipimask) - 1) != -1) { 285 ipimask &= ~(1u << msg); 286 ipi_msg_cnt[msg]++; 287 switch (msg) { 288 case IPI_AST: 289 CTR1(KTR_SMP, "%s: IPI_AST", __func__); 290 break; 291 case IPI_PREEMPT: 292 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__); 293 sched_preempt(curthread); 294 break; 295 case IPI_RENDEZVOUS: 296 CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__); 297 smp_rendezvous_action(); 298 break; 299 case IPI_STOP: 300 301 /* 302 * IPI_STOP_HARD is mapped to IPI_STOP so it is not 303 * necessary to add such case in the switch. 304 */ 305 CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)", 306 __func__); 307 self = PCPU_GET(cpumask); 308 savectx(PCPU_GET(curpcb)); 309 atomic_set_int(&stopped_cpus, self); 310 while ((started_cpus & self) == 0) 311 cpu_spinwait(); 312 atomic_clear_int(&started_cpus, self); 313 atomic_clear_int(&stopped_cpus, self); 314 CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__); 315 break; 316 case IPI_HARDCLOCK: 317 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__); 318 hardclockintr(); 319 break; 320 } 321 } 322 323 return (FILTER_HANDLED); 324} 325 326static void 327ipi_send(struct pcpu *pc, int ipi) 328{ 329 330 CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__, 331 pc, pc->pc_cpuid, ipi); 332 333 atomic_set_32(&pc->pc_ipimask, (1 << ipi)); 334 PIC_IPI(root_pic, pc->pc_cpuid); 335 336 CTR1(KTR_SMP, "%s: sent", __func__); 337} 338 339/* Send an IPI to a set of cpus. */ 340void 341ipi_selected(cpumask_t cpus, int ipi) 342{ 343 struct pcpu *pc; 344 345 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 346 if (cpus & pc->pc_cpumask) 347 ipi_send(pc, ipi); 348 } 349} 350 351/* Send an IPI to a specific CPU. */ 352void 353ipi_cpu(int cpu, u_int ipi) 354{ 355 356 ipi_send(cpuid_to_pcpu[cpu], ipi); 357} 358 359/* Send an IPI to all CPUs EXCEPT myself. */ 360void 361ipi_all_but_self(int ipi) 362{ 363 struct pcpu *pc; 364 365 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 366 if (pc != pcpup) 367 ipi_send(pc, ipi); 368 } 369} 370