mp_machdep.c revision 222813
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 222813 2011-06-07 08:46:13Z attilio $"); 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/cpuset.h> 36#include <sys/lock.h> 37#include <sys/mutex.h> 38#include <sys/pcpu.h> 39#include <sys/proc.h> 40#include <sys/sched.h> 41#include <sys/smp.h> 42 43#include <vm/vm.h> 44#include <vm/vm_param.h> 45#include <vm/pmap.h> 46#include <vm/vm_map.h> 47#include <vm/vm_extern.h> 48#include <vm/vm_kern.h> 49 50#include <machine/bus.h> 51#include <machine/cpu.h> 52#include <machine/intr_machdep.h> 53#include <machine/pcb.h> 54#include <machine/platform.h> 55#include <machine/md_var.h> 56#include <machine/smp.h> 57 58#include "pic_if.h" 59 60extern struct pcpu __pcpu[MAXCPU]; 61 62volatile static int ap_awake; 63volatile static u_int ap_letgo; 64volatile static u_quad_t ap_timebase; 65static u_int ipi_msg_cnt[32]; 66static struct mtx ap_boot_mtx; 67struct pcb stoppcbs[MAXCPU]; 68 69void 70machdep_ap_bootstrap(void) 71{ 72 /* Set up important bits on the CPU (HID registers, etc.) */ 73 cpudep_ap_setup(); 74 75 /* Set PIR */ 76 PCPU_SET(pir, mfspr(SPR_PIR)); 77 PCPU_SET(awake, 1); 78 __asm __volatile("msync; isync"); 79 80 while (ap_letgo == 0) 81 ; 82 83 /* Initialize DEC and TB, sync with the BSP values */ 84#ifdef __powerpc64__ 85 /* Writing to the time base register is hypervisor-privileged */ 86 if (mfmsr() & PSL_HV) 87 mttb(ap_timebase); 88#else 89 mttb(ap_timebase); 90#endif 91 decr_ap_init(); 92 93 /* Serialize console output and AP count increment */ 94 mtx_lock_spin(&ap_boot_mtx); 95 ap_awake++; 96 printf("SMP: AP CPU #%d launched\n", PCPU_GET(cpuid)); 97 mtx_unlock_spin(&ap_boot_mtx); 98 99 /* Initialize curthread */ 100 PCPU_SET(curthread, PCPU_GET(idlethread)); 101 PCPU_SET(curpcb, curthread->td_pcb); 102 103 /* Start per-CPU event timers. */ 104 cpu_initclocks_ap(); 105 106 /* Announce ourselves awake, and enter the scheduler */ 107 sched_throw(NULL); 108} 109 110void 111cpu_mp_setmaxid(void) 112{ 113 struct cpuref cpuref; 114 int error; 115 116 mp_ncpus = 0; 117 error = platform_smp_first_cpu(&cpuref); 118 while (!error) { 119 mp_ncpus++; 120 error = platform_smp_next_cpu(&cpuref); 121 } 122 /* Sanity. */ 123 if (mp_ncpus == 0) 124 mp_ncpus = 1; 125 126 /* 127 * Set the largest cpuid we're going to use. This is necessary 128 * for VM initialization. 129 */ 130 mp_maxid = min(mp_ncpus, MAXCPU) - 1; 131} 132 133int 134cpu_mp_probe(void) 135{ 136 137 /* 138 * We're not going to enable SMP if there's only 1 processor. 139 */ 140 return (mp_ncpus > 1); 141} 142 143void 144cpu_mp_start(void) 145{ 146 struct cpuref bsp, cpu; 147 struct pcpu *pc; 148 int error; 149 150 error = platform_smp_get_bsp(&bsp); 151 KASSERT(error == 0, ("Don't know BSP")); 152 KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__)); 153 154 error = platform_smp_first_cpu(&cpu); 155 while (!error) { 156 if (cpu.cr_cpuid >= MAXCPU) { 157 printf("SMP: cpu%d: skipped -- ID out of range\n", 158 cpu.cr_cpuid); 159 goto next; 160 } 161 if (CPU_ISSET(cpu.cr_cpuid, &all_cpus)) { 162 printf("SMP: cpu%d: skipped - duplicate ID\n", 163 cpu.cr_cpuid); 164 goto next; 165 } 166 if (cpu.cr_cpuid != bsp.cr_cpuid) { 167 void *dpcpu; 168 169 pc = &__pcpu[cpu.cr_cpuid]; 170 dpcpu = (void *)kmem_alloc(kernel_map, DPCPU_SIZE); 171 pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc)); 172 dpcpu_init(dpcpu, cpu.cr_cpuid); 173 } else { 174 pc = pcpup; 175 pc->pc_cpuid = bsp.cr_cpuid; 176 pc->pc_bsp = 1; 177 } 178 CPU_SETOF(pc->pc_cpuid, &pc->pc_cpumask); 179 pc->pc_hwref = cpu.cr_hwref; 180 CPU_OR(&all_cpus, &pc->pc_cpumask); 181next: 182 error = platform_smp_next_cpu(&cpu); 183 } 184} 185 186void 187cpu_mp_announce(void) 188{ 189 struct pcpu *pc; 190 int i; 191 192 for (i = 0; i <= mp_maxid; i++) { 193 pc = pcpu_find(i); 194 if (pc == NULL) 195 continue; 196 printf("cpu%d: dev=%x", i, (int)pc->pc_hwref); 197 if (pc->pc_bsp) 198 printf(" (BSP)"); 199 printf("\n"); 200 } 201} 202 203static void 204cpu_mp_unleash(void *dummy) 205{ 206 struct pcpu *pc; 207 int cpus, timeout; 208 209 if (mp_ncpus <= 1) 210 return; 211 212 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 213 214 cpus = 0; 215 smp_cpus = 0; 216 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) { 217 cpus++; 218 pc->pc_other_cpus = all_cpus; 219 CPU_NAND(&pc->pc_other_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 CPU_SET(pc->pc_cpuid, &stopped_cpus); 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#ifdef __powerpc64__ 254 /* Writing to the time base register is hypervisor-privileged */ 255 if (mfmsr() & PSL_HV) 256 mttb(ap_timebase); 257#else 258 mttb(ap_timebase); 259#endif 260 261 while (ap_awake < smp_cpus) 262 ; 263 264 if (smp_cpus != cpus || cpus != mp_ncpus) { 265 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n", 266 mp_ncpus, cpus, smp_cpus); 267 } 268 269 /* Let the APs get into the scheduler */ 270 DELAY(10000); 271 272 smp_active = 1; 273 smp_started = 1; 274} 275 276SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL); 277 278int 279powerpc_ipi_handler(void *arg) 280{ 281 cpuset_t self; 282 uint32_t ipimask; 283 int msg; 284 285 CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr()); 286 287 ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask)); 288 if (ipimask == 0) 289 return (FILTER_STRAY); 290 while ((msg = ffs(ipimask) - 1) != -1) { 291 ipimask &= ~(1u << msg); 292 ipi_msg_cnt[msg]++; 293 switch (msg) { 294 case IPI_AST: 295 CTR1(KTR_SMP, "%s: IPI_AST", __func__); 296 break; 297 case IPI_PREEMPT: 298 CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__); 299 sched_preempt(curthread); 300 break; 301 case IPI_RENDEZVOUS: 302 CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__); 303 smp_rendezvous_action(); 304 break; 305 case IPI_STOP: 306 307 /* 308 * IPI_STOP_HARD is mapped to IPI_STOP so it is not 309 * necessary to add such case in the switch. 310 */ 311 CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)", 312 __func__); 313 savectx(&stoppcbs[PCPU_GET(cpuid)]); 314 self = PCPU_GET(cpumask); 315 savectx(PCPU_GET(curpcb)); 316 CPU_OR_ATOMIC(&stopped_cpus, &self); 317 while (!CPU_OVERLAP(&started_cpus, &self)) 318 cpu_spinwait(); 319 CPU_NAND_ATOMIC(&started_cpus, &self); 320 CPU_NAND_ATOMIC(&stopped_cpus, &self); 321 CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__); 322 break; 323 case IPI_HARDCLOCK: 324 CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__); 325 hardclockintr(); 326 break; 327 } 328 } 329 330 return (FILTER_HANDLED); 331} 332 333static void 334ipi_send(struct pcpu *pc, int ipi) 335{ 336 337 CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__, 338 pc, pc->pc_cpuid, ipi); 339 340 atomic_set_32(&pc->pc_ipimask, (1 << ipi)); 341 PIC_IPI(root_pic, pc->pc_cpuid); 342 343 CTR1(KTR_SMP, "%s: sent", __func__); 344} 345 346/* Send an IPI to a set of cpus. */ 347void 348ipi_selected(cpuset_t cpus, int ipi) 349{ 350 struct pcpu *pc; 351 352 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) { 353 if (CPU_OVERLAP(&cpus, &pc->pc_cpumask)) 354 ipi_send(pc, ipi); 355 } 356} 357 358/* Send an IPI to a specific CPU. */ 359void 360ipi_cpu(int cpu, u_int ipi) 361{ 362 363 ipi_send(cpuid_to_pcpu[cpu], ipi); 364} 365 366/* Send an IPI to all CPUs EXCEPT myself. */ 367void 368ipi_all_but_self(int ipi) 369{ 370 struct pcpu *pc; 371 372 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) { 373 if (pc != pcpup) 374 ipi_send(pc, ipi); 375 } 376} 377