Path: blob/main/sys/arm/annapurna/alpine/alpine_machdep_mp.c
39536 views
/*-1* Copyright (c) 2013 Ruslan Bukin <[email protected]>2* Copyright (c) 2015 Semihalf3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/bus.h>31#include <sys/lock.h>32#include <sys/mutex.h>33#include <sys/smp.h>34#include <sys/cpuset.h>3536#include <vm/vm.h>37#include <vm/pmap.h>3839#include <machine/smp.h>40#include <machine/fdt.h>41#include <machine/intr.h>42#include <machine/cpu.h>43#include <machine/platformvar.h>4445#include <dev/fdt/fdt_common.h>46#include <dev/ofw/openfirm.h>47#include <dev/ofw/ofw_cpu.h>48#include <dev/ofw/ofw_bus_subr.h>4950#include <arm/annapurna/alpine/alpine_mp.h>5152#define AL_CPU_RESUME_WATERMARK_REG 0x0053#define AL_CPU_RESUME_FLAGS_REG 0x0454#define AL_CPU_RESUME_PCPU_RADDR_REG(cpu) (0x08 + 0x04 + 8*(cpu))55#define AL_CPU_RESUME_PCPU_FLAGS(cpu) (0x08 + 8*(cpu))5657/* Per-CPU flags */58#define AL_CPU_RESUME_FLG_PERCPU_DONT_RESUME (1 << 2)5960/* The expected magic number for validating the resume addresses */61#define AL_CPU_RESUME_MAGIC_NUM 0xf0e1d20062#define AL_CPU_RESUME_MAGIC_NUM_MASK 0xffffff006364/* The expected minimal version number for validating the capabilities */65#define AL_CPU_RESUME_MIN_VER 0x000000c366#define AL_CPU_RESUME_MIN_VER_MASK 0x000000ff6768/* Field controlling the boot-up of companion cores */69#define AL_NB_INIT_CONTROL (0x8)70#define AL_NB_CONFIG_STATUS_PWR_CTRL(cpu) (0x2020 + (cpu)*0x100)7172extern bus_addr_t al_devmap_pa;73extern bus_addr_t al_devmap_size;7475extern void mpentry(void);7677static int platform_mp_get_core_cnt(void);78static int alpine_get_cpu_resume_base(u_long *pbase, u_long *psize);79static int alpine_get_nb_base(u_long *pbase, u_long *psize);80static bool alpine_validate_cpu(u_int, phandle_t, u_int, pcell_t *);8182static bool83alpine_validate_cpu(u_int id, phandle_t child, u_int addr_cell, pcell_t *reg)84{85return ofw_bus_node_is_compatible(child, "arm,cortex-a15");86}8788static int89platform_mp_get_core_cnt(void)90{91static int ncores = 0;92int nchilds;93uint32_t reg;9495/* Calculate ncores value only once */96if (ncores)97return (ncores);9899reg = cp15_l2ctlr_get();100ncores = CPUV7_L2CTLR_NPROC(reg);101102nchilds = ofw_cpu_early_foreach(alpine_validate_cpu, false);103104/* Limit CPUs if DTS has configured less than available */105if ((nchilds > 0) && (nchilds < ncores)) {106printf("SMP: limiting number of active CPUs to %d out of %d\n",107nchilds, ncores);108ncores = nchilds;109}110111return (ncores);112}113114void115alpine_mp_setmaxid(platform_t plat)116{117118mp_ncpus = platform_mp_get_core_cnt();119mp_maxid = mp_ncpus - 1;120}121122static int123alpine_get_cpu_resume_base(u_long *pbase, u_long *psize)124{125phandle_t node;126u_long base = 0;127u_long size = 0;128129if (pbase == NULL || psize == NULL)130return (EINVAL);131132if ((node = OF_finddevice("/")) == -1)133return (EFAULT);134135if ((node =136ofw_bus_find_compatible(node, "annapurna-labs,al-cpu-resume")) == 0)137return (EFAULT);138139if (fdt_regsize(node, &base, &size))140return (EFAULT);141142*pbase = base;143*psize = size;144145return (0);146}147148static int149alpine_get_nb_base(u_long *pbase, u_long *psize)150{151phandle_t node;152u_long base = 0;153u_long size = 0;154155if (pbase == NULL || psize == NULL)156return (EINVAL);157158if ((node = OF_finddevice("/")) == -1)159return (EFAULT);160161if ((node =162ofw_bus_find_compatible(node, "annapurna-labs,al-nb-service")) == 0)163return (EFAULT);164165if (fdt_regsize(node, &base, &size))166return (EFAULT);167168*pbase = base;169*psize = size;170171return (0);172}173174void175alpine_mp_start_ap(platform_t plat)176{177uint32_t physaddr;178vm_offset_t vaddr;179uint32_t val;180uint32_t start_mask;181u_long cpu_resume_base;182u_long nb_base;183u_long cpu_resume_size;184u_long nb_size;185bus_addr_t cpu_resume_baddr;186bus_addr_t nb_baddr;187int a;188189if (alpine_get_cpu_resume_base(&cpu_resume_base, &cpu_resume_size))190panic("Couldn't resolve cpu_resume_base address\n");191192if (alpine_get_nb_base(&nb_base, &nb_size))193panic("Couldn't resolve_nb_base address\n");194195/* Proceed with start addresses for additional CPUs */196if (bus_space_map(fdtbus_bs_tag, al_devmap_pa + cpu_resume_base,197cpu_resume_size, 0, &cpu_resume_baddr))198panic("Couldn't map CPU-resume area");199if (bus_space_map(fdtbus_bs_tag, al_devmap_pa + nb_base,200nb_size, 0, &nb_baddr))201panic("Couldn't map NB-service area");202203/* Proceed with start addresses for additional CPUs */204val = bus_space_read_4(fdtbus_bs_tag, cpu_resume_baddr,205AL_CPU_RESUME_WATERMARK_REG);206if (((val & AL_CPU_RESUME_MAGIC_NUM_MASK) != AL_CPU_RESUME_MAGIC_NUM) ||207((val & AL_CPU_RESUME_MIN_VER_MASK) < AL_CPU_RESUME_MIN_VER)) {208panic("CPU-resume device is not compatible");209}210211vaddr = (vm_offset_t)mpentry;212physaddr = pmap_kextract(vaddr);213214for (a = 1; a < platform_mp_get_core_cnt(); a++) {215/* Power up the core */216bus_space_write_4(fdtbus_bs_tag, nb_baddr,217AL_NB_CONFIG_STATUS_PWR_CTRL(a), 0);218mb();219220/* Enable resume */221val = bus_space_read_4(fdtbus_bs_tag, cpu_resume_baddr,222AL_CPU_RESUME_PCPU_FLAGS(a));223val &= ~AL_CPU_RESUME_FLG_PERCPU_DONT_RESUME;224bus_space_write_4(fdtbus_bs_tag, cpu_resume_baddr,225AL_CPU_RESUME_PCPU_FLAGS(a), val);226mb();227228/* Set resume physical address */229bus_space_write_4(fdtbus_bs_tag, cpu_resume_baddr,230AL_CPU_RESUME_PCPU_RADDR_REG(a), physaddr);231mb();232}233234/* Release cores from reset */235if (bus_space_map(fdtbus_bs_tag, al_devmap_pa + nb_base,236nb_size, 0, &nb_baddr))237panic("Couldn't map NB-service area");238239start_mask = (1 << platform_mp_get_core_cnt()) - 1;240241/* Release cores from reset */242val = bus_space_read_4(fdtbus_bs_tag, nb_baddr, AL_NB_INIT_CONTROL);243val |= start_mask;244bus_space_write_4(fdtbus_bs_tag, nb_baddr, AL_NB_INIT_CONTROL, val);245dsb();246247bus_space_unmap(fdtbus_bs_tag, nb_baddr, nb_size);248bus_space_unmap(fdtbus_bs_tag, cpu_resume_baddr, cpu_resume_size);249}250251252