mp_machdep.c revision 215159
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 215159 2010-11-12 04:18:19Z nwhitehorn $"); 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 /* Start per-CPU event timers. */ 95 cpu_initclocks_ap(); 96 97 /* Announce ourselves awake, and enter the scheduler */ 98 sched_throw(NULL); 99} 100 101void 102cpu_mp_setmaxid(void) 103{ 104 struct cpuref cpuref; 105 int error; 106 107 mp_ncpus = 0; 108 error = platform_smp_first_cpu(&cpuref); 109 while (!error) { 110 mp_ncpus++; 111 error = platform_smp_next_cpu(&cpuref); 112 } 113 /* Sanity. */ 114 if (mp_ncpus == 0) 115 mp_ncpus = 1; 116 117 /* 118 * Set the largest cpuid we're going to use. This is necessary 119 * for VM initialization. 120 */ 121 mp_maxid = min(mp_ncpus, MAXCPU) - 1; 122} 123 124int 125cpu_mp_probe(void) 126{ 127 128 /* 129 * We're not going to enable SMP if there's only 1 processor. 130 */ 131 return (mp_ncpus > 1); 132} 133 134void 135cpu_mp_start(void) 136{ 137 struct cpuref bsp, cpu; 138 struct pcpu *pc; 139 int error; 140 141 error = platform_smp_get_bsp(&bsp); 142 KASSERT(error == 0, ("Don't know BSP")); 143 KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__)); 144 145 error = platform_smp_first_cpu(&cpu); 146 while (!error) { 147 if (cpu.cr_cpuid >= MAXCPU) { 148 printf("SMP: cpu%d: skipped -- ID out of range\n", 149 cpu.cr_cpuid); 150 goto next; 151 } 152 if (all_cpus & (1 << cpu.cr_cpuid)) { 153 printf("SMP: cpu%d: skipped - duplicate ID\n", 154 cpu.cr_cpuid); 155 goto next; 156 } 157 if (cpu.cr_cpuid != bsp.cr_cpuid) { 158 void *dpcpu; 159 160 pc = &__pcpu[cpu.cr_cpuid]; 161 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE); 162 pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc)); 163 dpcpu_init(dpcpu, cpu.cr_cpuid); 164 } else { 165 pc = pcpup; 166 pc->pc_cpuid = bsp.cr_cpuid; 167 pc->pc_bsp = 1; 168 } 169 pc->pc_cpumask = 1 << pc->pc_cpuid; 170 pc->pc_hwref = cpu.cr_hwref; 171 all_cpus |= pc->pc_cpumask; 172next: 173 error = platform_smp_next_cpu(&cpu); 174 } 175} 176 177void 178cpu_mp_announce(void) 179{ 180 struct pcpu *pc; 181 int i; 182 183 for (i = 0; i <= mp_maxid; i++) { 184 pc = pcpu_find(i); 185 if (pc == NULL) 186 continue; 187 printf("cpu%d: dev=%x", i, (int)pc->pc_hwref); 188 if (pc->pc_bsp) 189 printf(" (BSP)"); 190 printf("\n"); 191 } 192} 193 194static void 195cpu_mp_unleash(void *dummy) 196{ 197 struct pcpu *pc; 198 int cpus, timeout; 199 200 if (mp_ncpus <= 1) 201 return; 202 203 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 204 205 cpus = 0; 206 smp_cpus = 0; 207 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 208 cpus++; 209 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask; 210 if (!pc->pc_bsp) { 211 if (bootverbose) 212 printf("Waking up CPU %d (dev=%x)\n", 213 pc->pc_cpuid, (int)pc->pc_hwref); 214 215 platform_smp_start_cpu(pc); 216 217 timeout = 2000; /* wait 2sec for the AP */ 218 while (!pc->pc_awake && --timeout > 0) 219 DELAY(1000); 220 221 } else { 222 PCPU_SET(pir, mfspr(SPR_PIR)); 223 pc->pc_awake = 1; 224 } 225 if (pc->pc_awake) { 226 if (bootverbose) 227 printf("Adding CPU %d, pir=%x, awake=%x\n", 228 pc->pc_cpuid, pc->pc_pir, pc->pc_awake); 229 smp_cpus++; 230 } else 231 stopped_cpus |= (1 << pc->pc_cpuid); 232 } 233 234 ap_awake = 1; 235 236 /* Provide our current DEC and TB values for APs */ 237 ap_timebase = mftb() + 10; 238 __asm __volatile("msync; isync"); 239 240 /* Let APs continue */ 241 atomic_store_rel_int(&ap_letgo, 1); 242 243 mttb(ap_timebase); 244 245 while (ap_awake < smp_cpus) 246 ; 247 248 if (smp_cpus != cpus || cpus != mp_ncpus) { 249 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n", 250 mp_ncpus, cpus, smp_cpus); 251 } 252 253 /* Let the APs get into the scheduler */ 254 DELAY(10000); 255 256 smp_active = 1; 257 smp_started = 1; 258} 259 260SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL); 261 262int 263powerpc_ipi_handler(void *arg) 264{ 265 cpumask_t self; 266 uint32_t ipimask; 267 int msg; 268 269 CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr()); 270 271 ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask)); 272 if (ipimask == 0) 273 return (FILTER_STRAY); 274 while ((msg = ffs(ipimask) - 1) != -1) { 275 ipimask &= ~(1u << msg); 276 ipi_msg_cnt[msg]++; 277 switch (msg) { 278 case IPI_AST: 279 CTR1(KTR_SMP, "%s: IPI_AST", __func__); 280 break; 281 case IPI_PREEMPT: 282 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__); 283 sched_preempt(curthread); 284 break; 285 case IPI_RENDEZVOUS: 286 CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__); 287 smp_rendezvous_action(); 288 break; 289 case IPI_STOP: 290 291 /* 292 * IPI_STOP_HARD is mapped to IPI_STOP so it is not 293 * necessary to add such case in the switch. 294 */ 295 CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)", 296 __func__); 297 self = PCPU_GET(cpumask); 298 savectx(PCPU_GET(curpcb)); 299 atomic_set_int(&stopped_cpus, self); 300 while ((started_cpus & self) == 0) 301 cpu_spinwait(); 302 atomic_clear_int(&started_cpus, self); 303 atomic_clear_int(&stopped_cpus, self); 304 CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__); 305 break; 306 case IPI_HARDCLOCK: 307 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__); 308 hardclockintr(); 309 break; 310 } 311 } 312 313 return (FILTER_HANDLED); 314} 315 316static void 317ipi_send(struct pcpu *pc, int ipi) 318{ 319 320 CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__, 321 pc, pc->pc_cpuid, ipi); 322 323 atomic_set_32(&pc->pc_ipimask, (1 << ipi)); 324 PIC_IPI(root_pic, pc->pc_cpuid); 325 326 CTR1(KTR_SMP, "%s: sent", __func__); 327} 328 329/* Send an IPI to a set of cpus. */ 330void 331ipi_selected(cpumask_t cpus, int ipi) 332{ 333 struct pcpu *pc; 334 335 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 336 if (cpus & pc->pc_cpumask) 337 ipi_send(pc, ipi); 338 } 339} 340 341/* Send an IPI to a specific CPU. */ 342void 343ipi_cpu(int cpu, u_int ipi) 344{ 345 346 ipi_send(cpuid_to_pcpu[cpu], ipi); 347} 348 349/* Send an IPI to all CPUs EXCEPT myself. */ 350void 351ipi_all_but_self(int ipi) 352{ 353 struct pcpu *pc; 354 355 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 356 if (pc != pcpup) 357 ipi_send(pc, ipi); 358 } 359} 360