#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/ktr.h>
#include <sys/bus.h>
#include <sys/cpuset.h>
#include <sys/domainset.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/pcpu.h>
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <vm/vm.h>
#include <vm/vm_param.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_extern.h>
#include <vm/vm_kern.h>
#include <machine/bus.h>
#include <machine/cpu.h>
#include <machine/intr_machdep.h>
#include <machine/pcb.h>
#include <machine/platform.h>
#include <machine/md_var.h>
#include <machine/setjmp.h>
#include <machine/smp.h>
#include "pic_if.h"
volatile static int ap_awake;
volatile static u_int ap_letgo;
volatile static u_quad_t ap_timebase;
static struct mtx ap_boot_mtx;
void
machdep_ap_bootstrap(void)
{
PCPU_SET(awake, 1);
__asm __volatile("msync; isync");
while (ap_letgo == 0)
nop_prio_vlow();
nop_prio_medium();
platform_smp_timebase_sync(ap_timebase, 1);
platform_smp_ap_init();
decr_ap_init();
mtx_lock_spin(&ap_boot_mtx);
ap_awake++;
if (bootverbose)
printf("SMP: AP CPU #%d launched\n", PCPU_GET(cpuid));
else
printf("%s%d%s", ap_awake == 2 ? "Launching APs: " : "",
PCPU_GET(cpuid), ap_awake == mp_ncpus ? "\n" : " ");
mtx_unlock_spin(&ap_boot_mtx);
while(smp_started == 0)
;
cpu_initclocks_ap();
sched_ap_entry();
}
void
cpu_mp_setmaxid(void)
{
struct cpuref cpuref;
int error;
mp_ncpus = 0;
mp_maxid = 0;
error = platform_smp_first_cpu(&cpuref);
while (!error) {
mp_ncpus++;
mp_maxid = max(cpuref.cr_cpuid, mp_maxid);
error = platform_smp_next_cpu(&cpuref);
}
if (mp_ncpus == 0)
mp_ncpus = 1;
}
int
cpu_mp_probe(void)
{
return (mp_ncpus > 1);
}
void
cpu_mp_start(void)
{
struct cpuref bsp, cpu;
struct pcpu *pc;
int domain, error;
error = platform_smp_get_bsp(&bsp);
KASSERT(error == 0, ("Don't know BSP"));
error = platform_smp_first_cpu(&cpu);
while (!error) {
if (cpu.cr_cpuid >= MAXCPU) {
printf("SMP: cpu%d: skipped -- ID out of range\n",
cpu.cr_cpuid);
goto next;
}
if (CPU_ISSET(cpu.cr_cpuid, &all_cpus)) {
printf("SMP: cpu%d: skipped - duplicate ID\n",
cpu.cr_cpuid);
goto next;
}
if (vm_ndomains > 1)
domain = cpu.cr_domain;
else
domain = 0;
if (cpu.cr_cpuid != bsp.cr_cpuid) {
void *dpcpu;
pc = &__pcpu[cpu.cr_cpuid];
dpcpu = kmem_malloc_domainset(DOMAINSET_PREF(domain),
DPCPU_SIZE, M_WAITOK | M_ZERO);
pcpu_init(pc, cpu.cr_cpuid, sizeof(*pc));
dpcpu_init(dpcpu, cpu.cr_cpuid);
} else {
pc = pcpup;
pc->pc_cpuid = bsp.cr_cpuid;
pc->pc_bsp = 1;
}
pc->pc_domain = domain;
pc->pc_hwref = cpu.cr_hwref;
CPU_SET(pc->pc_cpuid, &cpuset_domain[pc->pc_domain]);
KASSERT(pc->pc_domain < MAXMEMDOM, ("bad domain value %d\n",
pc->pc_domain));
CPU_SET(pc->pc_cpuid, &all_cpus);
next:
error = platform_smp_next_cpu(&cpu);
}
#ifdef SMP
platform_smp_probe_threads();
#endif
}
void
cpu_mp_announce(void)
{
struct pcpu *pc;
int i;
if (!bootverbose)
return;
CPU_FOREACH(i) {
pc = pcpu_find(i);
if (pc == NULL)
continue;
printf("cpu%d: dev=%x domain=%d ", i, (int)pc->pc_hwref, pc->pc_domain);
if (pc->pc_bsp)
printf(" (BSP)");
printf("\n");
}
}
static void
cpu_mp_unleash(void *dummy)
{
struct pcpu *pc;
int cpus, timeout;
int ret;
if (mp_ncpus <= 1)
return;
mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
cpus = 0;
smp_cpus = 0;
#ifdef BOOKE
tlb1_ap_prep();
#endif
STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
cpus++;
if (!pc->pc_bsp) {
if (bootverbose)
printf("Waking up CPU %d (dev=%x)\n",
pc->pc_cpuid, (int)pc->pc_hwref);
pc->pc_flags = PCPU_GET(flags);
ret = platform_smp_start_cpu(pc);
if (ret == 0) {
timeout = 2000;
while (!pc->pc_awake && --timeout > 0)
DELAY(1000);
}
} else {
pc->pc_awake = 1;
}
if (pc->pc_awake) {
if (bootverbose)
printf("Adding CPU %d, hwref=%jx, awake=%x\n",
pc->pc_cpuid, (uintmax_t)pc->pc_hwref,
pc->pc_awake);
smp_cpus++;
} else
CPU_SET(pc->pc_cpuid, &stopped_cpus);
}
ap_awake = 1;
ap_timebase = mftb() + 10;
__asm __volatile("msync; isync");
atomic_store_rel_int(&ap_letgo, 1);
platform_smp_timebase_sync(ap_timebase, 0);
while (ap_awake < smp_cpus)
;
if (smp_cpus != cpus || cpus != mp_ncpus) {
printf("SMP: %d CPUs found; %d CPUs usable; %d CPUs woken\n",
mp_ncpus, cpus, smp_cpus);
}
if (smp_cpus > 1)
atomic_store_rel_int(&smp_started, 1);
DELAY(10000);
}
SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, cpu_mp_unleash, NULL);
int
powerpc_ipi_handler(void *arg)
{
u_int cpuid;
uint32_t ipimask;
CTR2(KTR_SMP, "%s: MSR 0x%08x", __func__, mfmsr());
ipimask = atomic_readandclear_32(&(pcpup->pc_ipimask));
if (ipimask == 0)
return (FILTER_STRAY);
if (ipimask & (1 << IPI_AST)) {
CTR1(KTR_SMP, "%s: IPI_AST", __func__);
}
if (ipimask & (1 << IPI_PREEMPT)) {
CTR1(KTR_SMP, "%s: IPI_PREEMPT", __func__);
sched_preempt(curthread);
}
if (ipimask & (1 << IPI_RENDEZVOUS)) {
CTR1(KTR_SMP, "%s: IPI_RENDEZVOUS", __func__);
smp_rendezvous_action();
}
if (ipimask & (1 << IPI_STOP)) {
CTR1(KTR_SMP, "%s: IPI_STOP or IPI_STOP_HARD (stop)",
__func__);
cpuid = PCPU_GET(cpuid);
savectx(&stoppcbs[cpuid]);
CPU_SET_ATOMIC(cpuid, &stopped_cpus);
while (!CPU_ISSET(cpuid, &started_cpus))
cpu_spinwait();
CPU_CLR_ATOMIC(cpuid, &stopped_cpus);
CPU_CLR_ATOMIC(cpuid, &started_cpus);
CTR1(KTR_SMP, "%s: IPI_STOP (restart)", __func__);
}
if (ipimask & (1 << IPI_HARDCLOCK)) {
CTR1(KTR_SMP, "%s: IPI_HARDCLOCK", __func__);
hardclockintr();
}
return (FILTER_HANDLED);
}
static void
ipi_send(struct pcpu *pc, int ipi)
{
CTR4(KTR_SMP, "%s: pc=%p, targetcpu=%d, IPI=%d", __func__,
pc, pc->pc_cpuid, ipi);
atomic_set_32(&pc->pc_ipimask, (1 << ipi));
powerpc_sync();
PIC_IPI(root_pic, pc->pc_cpuid);
CTR1(KTR_SMP, "%s: sent", __func__);
}
void
ipi_selected(cpuset_t cpus, int ipi)
{
struct pcpu *pc;
STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
if (CPU_ISSET(pc->pc_cpuid, &cpus))
ipi_send(pc, ipi);
}
}
void
ipi_cpu(int cpu, u_int ipi)
{
ipi_send(cpuid_to_pcpu[cpu], ipi);
}
void
ipi_all_but_self(int ipi)
{
struct pcpu *pc;
STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
if (pc != pcpup)
ipi_send(pc, ipi);
}
}