mp_machdep.c revision 192067
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 192067 2009-05-14 00:34:26Z nwhitehorn $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/bus.h> 34#include <sys/pcpu.h> 35#include <sys/proc.h> 36#include <sys/sched.h> 37#include <sys/smp.h> 38 39#include <machine/bus.h> 40#include <machine/cpu.h> 41#include <machine/intr_machdep.h> 42#include <machine/platform.h> 43#include <machine/smp.h> 44 45#include "pic_if.h" 46 47extern struct pcpu __pcpu[MAXCPU]; 48 49volatile static int ap_awake; 50volatile static u_int ap_state; 51volatile static uint32_t ap_decr; 52volatile static uint32_t ap_tbl; 53 54void 55machdep_ap_bootstrap(void) 56{ 57 58 pcpup->pc_awake = 1; 59 60 while (ap_state == 0) 61 ; 62 63 mtspr(SPR_TBL, 0); 64 mtspr(SPR_TBU, 0); 65 mtspr(SPR_TBL, ap_tbl); 66 __asm __volatile("mtdec %0" :: "r"(ap_decr)); 67 68 ap_awake++; 69 70 /* Initialize curthread. */ 71 PCPU_SET(curthread, PCPU_GET(idlethread)); 72 PCPU_SET(curpcb, curthread->td_pcb); 73 74 mtmsr(mfmsr() | PSL_EE); 75 sched_throw(NULL); 76} 77 78struct cpu_group * 79cpu_topo(void) 80{ 81 82 return (smp_topo_none()); 83} 84 85void 86cpu_mp_setmaxid(void) 87{ 88 struct cpuref cpuref; 89 int error; 90 91 mp_ncpus = 0; 92 error = platform_smp_first_cpu(&cpuref); 93 while (!error) { 94 mp_ncpus++; 95 error = platform_smp_next_cpu(&cpuref); 96 } 97 /* Sanity. */ 98 if (mp_ncpus == 0) 99 mp_ncpus = 1; 100 101 /* 102 * Set the largest cpuid we're going to use. This is necessary 103 * for VM initialization. 104 */ 105 mp_maxid = min(mp_ncpus, MAXCPU) - 1; 106} 107 108int 109cpu_mp_probe(void) 110{ 111 112 /* 113 * We're not going to enable SMP if there's only 1 processor. 114 */ 115 return (mp_ncpus > 1); 116} 117 118void 119cpu_mp_start(void) 120{ 121 struct cpuref bsp, cpu; 122 struct pcpu *pc; 123 int error; 124 125 error = platform_smp_get_bsp(&bsp); 126 KASSERT(error == 0, ("Don't know BSP")); 127 KASSERT(bsp.cr_cpuid == 0, ("%s: cpuid != 0", __func__)); 128 129 error = platform_smp_first_cpu(&cpu); 130 while (!error) { 131 if (cpu.cr_cpuid >= MAXCPU) { 132 printf("SMP: cpu%d: skipped -- ID out of range\n", 133 cpu.cr_cpuid); 134 goto next; 135 } 136 if (all_cpus & (1 << cpu.cr_cpuid)) { 137 printf("SMP: cpu%d: skipped - duplicate ID\n", 138 cpu.cr_cpuid); 139 goto next; 140 } 141 if (cpu.cr_cpuid != bsp.cr_cpuid) { 142 pc = &__pcpu[cpu.cr_cpuid]; 143 pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc)); 144 } else { 145 pc = pcpup; 146 pc->pc_cpuid = bsp.cr_cpuid; 147 pc->pc_bsp = 1; 148 } 149 pc->pc_cpumask = 1 << pc->pc_cpuid; 150 pc->pc_hwref = cpu.cr_hwref; 151 all_cpus |= pc->pc_cpumask; 152 153 next: 154 error = platform_smp_next_cpu(&cpu); 155 } 156} 157 158void 159cpu_mp_announce(void) 160{ 161 struct pcpu *pc; 162 int i; 163 164 for (i = 0; i <= mp_maxid; i++) { 165 pc = pcpu_find(i); 166 if (pc == NULL) 167 continue; 168 printf("cpu%d: dev=%x", i, pc->pc_hwref); 169 if (pc->pc_bsp) 170 printf(" (BSP)"); 171 printf("\n"); 172 } 173} 174 175static void 176cpu_mp_unleash(void *dummy) 177{ 178 struct pcpu *pc; 179 int cpus; 180 181 if (mp_ncpus <= 1) 182 return; 183 184 cpus = 0; 185 smp_cpus = 0; 186 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 187 cpus++; 188 pc->pc_other_cpus = all_cpus & ~pc->pc_cpumask; 189 if (!pc->pc_bsp) { 190 printf("Waking up CPU %d (dev=%x)\n", pc->pc_cpuid, 191 pc->pc_hwref); 192 platform_smp_start_cpu(pc); 193 } else { 194 __asm __volatile("mfspr %0,1023" : "=r"(pc->pc_pir)); 195 pc->pc_awake = 1; 196 } 197 if (pc->pc_awake) 198 smp_cpus++; 199 } 200 201 ap_awake = 1; 202 203 __asm __volatile("mftb %0" : "=r"(ap_tbl)); 204 ap_tbl += 10; 205 __asm __volatile("mfdec %0" : "=r"(ap_decr)); 206 ap_state++; 207 powerpc_sync(); 208 209 mtspr(SPR_TBL, 0); 210 mtspr(SPR_TBU, 0); 211 mtspr(SPR_TBL, ap_tbl); 212 213 while (ap_awake < smp_cpus) 214 ; 215 216 if (smp_cpus != cpus || cpus != mp_ncpus) { 217 printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n", 218 mp_ncpus, cpus, smp_cpus); 219 } 220 221 smp_active = 1; 222 smp_started = 1; 223} 224 225SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL); 226 227static u_int ipi_msg_cnt[32]; 228 229int 230powerpc_ipi_handler(void *arg) 231{ 232 cpumask_t self; 233 uint32_t ipimask; 234 int msg; 235 236 ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask)); 237 if (ipimask == 0) 238 return (FILTER_STRAY); 239 while ((msg = ffs(ipimask) - 1) != -1) { 240 ipimask &= ~(1u << msg); 241 ipi_msg_cnt[msg]++; 242 switch (msg) { 243 case IPI_AST: 244 break; 245 case IPI_PREEMPT: 246 sched_preempt(curthread); 247 break; 248 case IPI_RENDEZVOUS: 249 smp_rendezvous_action(); 250 break; 251 case IPI_STOP: 252 self = PCPU_GET(cpumask); 253 savectx(PCPU_GET(curpcb)); 254 atomic_set_int(&stopped_cpus, self); 255 while ((started_cpus & self) == 0) 256 cpu_spinwait(); 257 atomic_clear_int(&started_cpus, self); 258 atomic_clear_int(&stopped_cpus, self); 259 break; 260 } 261 } 262 263 return (FILTER_HANDLED); 264} 265 266static void 267ipi_send(struct pcpu *pc, int ipi) 268{ 269 270 atomic_set_32(&pc->pc_ipimask, (1 << ipi)); 271 PIC_IPI(pic, pc->pc_cpuid); 272} 273 274/* Send an IPI to a set of cpus. */ 275void 276ipi_selected(cpumask_t cpus, int ipi) 277{ 278 struct pcpu *pc; 279 280 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 281 if (cpus & pc->pc_cpumask) 282 ipi_send(pc, ipi); 283 } 284} 285 286/* Send an IPI to all CPUs EXCEPT myself. */ 287void 288ipi_all_but_self(int ipi) 289{ 290 struct pcpu *pc; 291 292 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 293 if (pc != pcpup) 294 ipi_send(pc, ipi); 295 } 296} 297