/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2014 Juergen Weiss <[email protected]>4* Copyright (c) 2014 Ian Lepore <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/bus.h>31#include <sys/kernel.h>32#include <sys/lock.h>33#include <sys/mutex.h>34#include <sys/smp.h>3536#include <vm/vm.h>37#include <vm/pmap.h>3839#include <machine/cpu.h>40#include <machine/smp.h>41#include <machine/fdt.h>42#include <machine/intr.h>43#include <machine/platform.h>44#include <machine/platformvar.h>4546#include <arm/freescale/imx/imx6_machdep.h>4748#define SCU_PHYSBASE 0x00a0000049#define SCU_SIZE 0x000010005051#define SCU_CONTROL_REG 0x0052#define SCU_CONTROL_ENABLE (1 << 0)53#define SCU_CONFIG_REG 0x0454#define SCU_CONFIG_REG_NCPU_MASK 0x0355#define SCU_CPUPOWER_REG 0x0856#define SCU_INV_TAGS_REG 0x0c57#define SCU_DIAG_CONTROL 0x3058#define SCU_DIAG_DISABLE_MIGBIT (1 << 0)59#define SCU_FILTER_START_REG 0x4060#define SCU_FILTER_END_REG 0x4461#define SCU_SECURE_ACCESS_REG 0x5062#define SCU_NONSECURE_ACCESS_REG 0x546364#define SRC_PHYSBASE 0x020d800065#define SRC_SIZE 0x400066#define SRC_CONTROL_REG 0x0067#define SRC_CONTROL_C1ENA_SHIFT 22 /* Bit for Core 1 enable */68#define SRC_CONTROL_C1RST_SHIFT 14 /* Bit for Core 1 reset */69#define SRC_GPR0_C1FUNC 0x20 /* Register for Core 1 entry func */70#define SRC_GPR1_C1ARG 0x24 /* Register for Core 1 entry arg */7172void73imx6_mp_setmaxid(platform_t plat)74{75bus_space_handle_t scu;76int hwcpu, ncpu;77uint32_t val;7879/* If we've already set the global vars don't bother to do it again. */80if (mp_ncpus != 0)81return;8283if (bus_space_map(fdtbus_bs_tag, SCU_PHYSBASE, SCU_SIZE, 0, &scu) != 0)84panic("Couldn't map the SCU\n");85val = bus_space_read_4(fdtbus_bs_tag, scu, SCU_CONFIG_REG);86hwcpu = (val & SCU_CONFIG_REG_NCPU_MASK) + 1;87bus_space_unmap(fdtbus_bs_tag, scu, SCU_SIZE);8889ncpu = hwcpu;90TUNABLE_INT_FETCH("hw.ncpu", &ncpu);91if (ncpu < 1 || ncpu > hwcpu)92ncpu = hwcpu;9394mp_ncpus = ncpu;95mp_maxid = ncpu - 1;96}9798void99imx6_mp_start_ap(platform_t plat)100{101bus_space_handle_t scu;102bus_space_handle_t src;103104uint32_t val;105int i;106107if (bus_space_map(fdtbus_bs_tag, SCU_PHYSBASE, SCU_SIZE, 0, &scu) != 0)108panic("Couldn't map the SCU\n");109if (bus_space_map(fdtbus_bs_tag, SRC_PHYSBASE, SRC_SIZE, 0, &src) != 0)110panic("Couldn't map the system reset controller (SRC)\n");111112/*113* Invalidate SCU cache tags. The 0x0000ffff constant invalidates all114* ways on all cores 0-3. Per the ARM docs, it's harmless to write to115* the bits for cores that are not present.116*/117bus_space_write_4(fdtbus_bs_tag, scu, SCU_INV_TAGS_REG, 0x0000ffff);118119/*120* Erratum ARM/MP: 764369 (problems with cache maintenance).121* Setting the "disable-migratory bit" in the undocumented SCU122* Diagnostic Control Register helps work around the problem.123*/124val = bus_space_read_4(fdtbus_bs_tag, scu, SCU_DIAG_CONTROL);125bus_space_write_4(fdtbus_bs_tag, scu, SCU_DIAG_CONTROL,126val | SCU_DIAG_DISABLE_MIGBIT);127128/*129* Enable the SCU, then clean the cache on this core. After these two130* operations the cache tag ram in the SCU is coherent with the contents131* of the cache on this core. The other cores aren't running yet so132* their caches can't contain valid data yet, but we've initialized133* their SCU tag ram above, so they will be coherent from startup.134*/135val = bus_space_read_4(fdtbus_bs_tag, scu, SCU_CONTROL_REG);136bus_space_write_4(fdtbus_bs_tag, scu, SCU_CONTROL_REG,137val | SCU_CONTROL_ENABLE);138dcache_wbinv_poc_all();139140/*141* For each AP core, set the entry point address and argument registers,142* and set the core-enable and core-reset bits in the control register.143*/144val = bus_space_read_4(fdtbus_bs_tag, src, SRC_CONTROL_REG);145for (i=1; i < mp_ncpus; i++) {146bus_space_write_4(fdtbus_bs_tag, src, SRC_GPR0_C1FUNC + 8*i,147pmap_kextract((vm_offset_t)mpentry));148bus_space_write_4(fdtbus_bs_tag, src, SRC_GPR1_C1ARG + 8*i, 0);149150val |= ((1 << (SRC_CONTROL_C1ENA_SHIFT - 1 + i )) |151( 1 << (SRC_CONTROL_C1RST_SHIFT - 1 + i)));152}153bus_space_write_4(fdtbus_bs_tag, src, SRC_CONTROL_REG, val);154155dsb();156sev();157158bus_space_unmap(fdtbus_bs_tag, scu, SCU_SIZE);159bus_space_unmap(fdtbus_bs_tag, src, SRC_SIZE);160}161162163