183014579SKukjin Kim /* linux/arch/arm/mach-exynos4/platsmp.c 283014579SKukjin Kim * 383014579SKukjin Kim * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. 483014579SKukjin Kim * http://www.samsung.com 583014579SKukjin Kim * 683014579SKukjin Kim * Cloned from linux/arch/arm/mach-vexpress/platsmp.c 783014579SKukjin Kim * 883014579SKukjin Kim * Copyright (C) 2002 ARM Ltd. 983014579SKukjin Kim * All Rights Reserved 1083014579SKukjin Kim * 1183014579SKukjin Kim * This program is free software; you can redistribute it and/or modify 1283014579SKukjin Kim * it under the terms of the GNU General Public License version 2 as 1383014579SKukjin Kim * published by the Free Software Foundation. 1483014579SKukjin Kim */ 1583014579SKukjin Kim 1683014579SKukjin Kim #include <linux/init.h> 1783014579SKukjin Kim #include <linux/errno.h> 1883014579SKukjin Kim #include <linux/delay.h> 1983014579SKukjin Kim #include <linux/device.h> 2083014579SKukjin Kim #include <linux/jiffies.h> 2183014579SKukjin Kim #include <linux/smp.h> 2283014579SKukjin Kim #include <linux/io.h> 23b3205deaSSachin Kamat #include <linux/of_address.h> 2483014579SKukjin Kim 2583014579SKukjin Kim #include <asm/cacheflush.h> 26eb50439bSWill Deacon #include <asm/smp_plat.h> 2783014579SKukjin Kim #include <asm/smp_scu.h> 28beddf63fSTomasz Figa #include <asm/firmware.h> 2983014579SKukjin Kim 3006853ae4SMarc Zyngier #include "common.h" 3165c9a853SKukjin Kim #include "regs-pmu.h" 3206853ae4SMarc Zyngier 3383014579SKukjin Kim extern void exynos4_secondary_startup(void); 3483014579SKukjin Kim 351f054f52STomasz Figa static inline void __iomem *cpu_boot_reg_base(void) 361f054f52STomasz Figa { 371f054f52STomasz Figa if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1) 381f054f52STomasz Figa return S5P_INFORM5; 39b3205deaSSachin Kamat return sysram_base_addr; 401f054f52STomasz Figa } 411f054f52STomasz Figa 421f054f52STomasz Figa static inline void __iomem *cpu_boot_reg(int cpu) 431f054f52STomasz Figa { 441f054f52STomasz Figa void __iomem *boot_reg; 451f054f52STomasz Figa 461f054f52STomasz Figa boot_reg = cpu_boot_reg_base(); 47b3205deaSSachin Kamat if (!boot_reg) 48b3205deaSSachin Kamat return ERR_PTR(-ENODEV); 491f054f52STomasz Figa if (soc_is_exynos4412()) 501f054f52STomasz Figa boot_reg += 4*cpu; 5186c6f148SArun Kumar K else if (soc_is_exynos5420() || soc_is_exynos5800()) 521580be3dSChander Kashyap boot_reg += 4; 531f054f52STomasz Figa return boot_reg; 541f054f52STomasz Figa } 5583014579SKukjin Kim 5683014579SKukjin Kim /* 5783014579SKukjin Kim * Write pen_release in a way that is guaranteed to be visible to all 5883014579SKukjin Kim * observers, irrespective of whether they're taking part in coherency 5983014579SKukjin Kim * or not. This is necessary for the hotplug code to work reliably. 6083014579SKukjin Kim */ 6183014579SKukjin Kim static void write_pen_release(int val) 6283014579SKukjin Kim { 6383014579SKukjin Kim pen_release = val; 6483014579SKukjin Kim smp_wmb(); 65f45913fdSNicolas Pitre sync_cache_w(&pen_release); 6683014579SKukjin Kim } 6783014579SKukjin Kim 6883014579SKukjin Kim static void __iomem *scu_base_addr(void) 6983014579SKukjin Kim { 7083014579SKukjin Kim return (void __iomem *)(S5P_VA_SCU); 7183014579SKukjin Kim } 7283014579SKukjin Kim 7383014579SKukjin Kim static DEFINE_SPINLOCK(boot_lock); 7483014579SKukjin Kim 758bd26e3aSPaul Gortmaker static void exynos_secondary_init(unsigned int cpu) 7683014579SKukjin Kim { 7783014579SKukjin Kim /* 7883014579SKukjin Kim * let the primary processor know we're out of the 7983014579SKukjin Kim * pen, then head off into the C entry point 8083014579SKukjin Kim */ 8183014579SKukjin Kim write_pen_release(-1); 8283014579SKukjin Kim 8383014579SKukjin Kim /* 8483014579SKukjin Kim * Synchronise with the boot thread. 8583014579SKukjin Kim */ 8683014579SKukjin Kim spin_lock(&boot_lock); 8783014579SKukjin Kim spin_unlock(&boot_lock); 8883014579SKukjin Kim } 8983014579SKukjin Kim 908bd26e3aSPaul Gortmaker static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle) 9183014579SKukjin Kim { 9283014579SKukjin Kim unsigned long timeout; 93*9637f30eSTomasz Figa u32 mpidr = cpu_logical_map(cpu); 94*9637f30eSTomasz Figa u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); 95b3205deaSSachin Kamat int ret = -ENOSYS; 9683014579SKukjin Kim 9783014579SKukjin Kim /* 9883014579SKukjin Kim * Set synchronisation state between this boot processor 9983014579SKukjin Kim * and the secondary one 10083014579SKukjin Kim */ 10183014579SKukjin Kim spin_lock(&boot_lock); 10283014579SKukjin Kim 10383014579SKukjin Kim /* 10483014579SKukjin Kim * The secondary processor is waiting to be released from 10583014579SKukjin Kim * the holding pen - release it, then wait for it to flag 10683014579SKukjin Kim * that it has been released by resetting pen_release. 10783014579SKukjin Kim * 108*9637f30eSTomasz Figa * Note that "pen_release" is the hardware CPU core ID, whereas 10983014579SKukjin Kim * "cpu" is Linux's internal ID. 11083014579SKukjin Kim */ 111*9637f30eSTomasz Figa write_pen_release(core_id); 11283014579SKukjin Kim 113*9637f30eSTomasz Figa if (!exynos_cpu_power_state(core_id)) { 114*9637f30eSTomasz Figa exynos_cpu_power_up(core_id); 11583014579SKukjin Kim timeout = 10; 11683014579SKukjin Kim 11783014579SKukjin Kim /* wait max 10 ms until cpu1 is on */ 118*9637f30eSTomasz Figa while (exynos_cpu_power_state(core_id) 119*9637f30eSTomasz Figa != S5P_CORE_LOCAL_PWR_EN) { 12083014579SKukjin Kim if (timeout-- == 0) 12183014579SKukjin Kim break; 12283014579SKukjin Kim 12383014579SKukjin Kim mdelay(1); 12483014579SKukjin Kim } 12583014579SKukjin Kim 12683014579SKukjin Kim if (timeout == 0) { 12783014579SKukjin Kim printk(KERN_ERR "cpu1 power enable failed"); 12883014579SKukjin Kim spin_unlock(&boot_lock); 12983014579SKukjin Kim return -ETIMEDOUT; 13083014579SKukjin Kim } 13183014579SKukjin Kim } 13283014579SKukjin Kim /* 13383014579SKukjin Kim * Send the secondary CPU a soft interrupt, thereby causing 13483014579SKukjin Kim * the boot monitor to read the system wide flags register, 13583014579SKukjin Kim * and branch to the address found there. 13683014579SKukjin Kim */ 13783014579SKukjin Kim 13883014579SKukjin Kim timeout = jiffies + (1 * HZ); 13983014579SKukjin Kim while (time_before(jiffies, timeout)) { 140beddf63fSTomasz Figa unsigned long boot_addr; 141beddf63fSTomasz Figa 14283014579SKukjin Kim smp_rmb(); 14383014579SKukjin Kim 144beddf63fSTomasz Figa boot_addr = virt_to_phys(exynos4_secondary_startup); 145beddf63fSTomasz Figa 146beddf63fSTomasz Figa /* 147beddf63fSTomasz Figa * Try to set boot address using firmware first 148beddf63fSTomasz Figa * and fall back to boot register if it fails. 149beddf63fSTomasz Figa */ 150*9637f30eSTomasz Figa ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr); 151b3205deaSSachin Kamat if (ret && ret != -ENOSYS) 152b3205deaSSachin Kamat goto fail; 153b3205deaSSachin Kamat if (ret == -ENOSYS) { 154*9637f30eSTomasz Figa void __iomem *boot_reg = cpu_boot_reg(core_id); 155b3205deaSSachin Kamat 156b3205deaSSachin Kamat if (IS_ERR(boot_reg)) { 157b3205deaSSachin Kamat ret = PTR_ERR(boot_reg); 158b3205deaSSachin Kamat goto fail; 159b3205deaSSachin Kamat } 160*9637f30eSTomasz Figa __raw_writel(boot_addr, cpu_boot_reg(core_id)); 161b3205deaSSachin Kamat } 162beddf63fSTomasz Figa 163*9637f30eSTomasz Figa call_firmware_op(cpu_boot, core_id); 164beddf63fSTomasz Figa 165b1cffebfSRob Herring arch_send_wakeup_ipi_mask(cpumask_of(cpu)); 16683014579SKukjin Kim 16783014579SKukjin Kim if (pen_release == -1) 16883014579SKukjin Kim break; 16983014579SKukjin Kim 17083014579SKukjin Kim udelay(10); 17183014579SKukjin Kim } 17283014579SKukjin Kim 17383014579SKukjin Kim /* 17483014579SKukjin Kim * now the secondary core is starting up let it run its 17583014579SKukjin Kim * calibrations, then wait for it to finish 17683014579SKukjin Kim */ 177b3205deaSSachin Kamat fail: 17883014579SKukjin Kim spin_unlock(&boot_lock); 17983014579SKukjin Kim 180b3205deaSSachin Kamat return pen_release != -1 ? ret : 0; 18183014579SKukjin Kim } 18283014579SKukjin Kim 18383014579SKukjin Kim /* 18483014579SKukjin Kim * Initialise the CPU possible map early - this describes the CPUs 18583014579SKukjin Kim * which may be present or become present in the system. 18683014579SKukjin Kim */ 18783014579SKukjin Kim 18806853ae4SMarc Zyngier static void __init exynos_smp_init_cpus(void) 18983014579SKukjin Kim { 19083014579SKukjin Kim void __iomem *scu_base = scu_base_addr(); 19183014579SKukjin Kim unsigned int i, ncores; 19283014579SKukjin Kim 1931897d2f3SChander Kashyap if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9) 19483014579SKukjin Kim ncores = scu_base ? scu_get_core_count(scu_base) : 1; 1951897d2f3SChander Kashyap else 1961897d2f3SChander Kashyap /* 1971897d2f3SChander Kashyap * CPU Nodes are passed thru DT and set_cpu_possible 1981897d2f3SChander Kashyap * is set by "arm_dt_init_cpu_maps". 1991897d2f3SChander Kashyap */ 2001897d2f3SChander Kashyap return; 20183014579SKukjin Kim 20283014579SKukjin Kim /* sanity check */ 20383014579SKukjin Kim if (ncores > nr_cpu_ids) { 20483014579SKukjin Kim pr_warn("SMP: %u cores greater than maximum (%u), clipping\n", 20583014579SKukjin Kim ncores, nr_cpu_ids); 20683014579SKukjin Kim ncores = nr_cpu_ids; 20783014579SKukjin Kim } 20883014579SKukjin Kim 20983014579SKukjin Kim for (i = 0; i < ncores; i++) 21083014579SKukjin Kim set_cpu_possible(i, true); 21183014579SKukjin Kim } 21283014579SKukjin Kim 21306853ae4SMarc Zyngier static void __init exynos_smp_prepare_cpus(unsigned int max_cpus) 21483014579SKukjin Kim { 2151f054f52STomasz Figa int i; 2161f054f52STomasz Figa 2171754c42eSOlof Johansson exynos_sysram_init(); 2181754c42eSOlof Johansson 219b5f3c75aSLeela Krishna Amudala if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9) 22083014579SKukjin Kim scu_enable(scu_base_addr()); 22183014579SKukjin Kim 22283014579SKukjin Kim /* 22383014579SKukjin Kim * Write the address of secondary startup into the 22483014579SKukjin Kim * system-wide flags register. The boot monitor waits 22583014579SKukjin Kim * until it receives a soft interrupt, and then the 22683014579SKukjin Kim * secondary CPU branches to this address. 227beddf63fSTomasz Figa * 228beddf63fSTomasz Figa * Try using firmware operation first and fall back to 229beddf63fSTomasz Figa * boot register if it fails. 23083014579SKukjin Kim */ 231beddf63fSTomasz Figa for (i = 1; i < max_cpus; ++i) { 232beddf63fSTomasz Figa unsigned long boot_addr; 233*9637f30eSTomasz Figa u32 mpidr; 234*9637f30eSTomasz Figa u32 core_id; 235b3205deaSSachin Kamat int ret; 236beddf63fSTomasz Figa 237*9637f30eSTomasz Figa mpidr = cpu_logical_map(i); 238*9637f30eSTomasz Figa core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); 239beddf63fSTomasz Figa boot_addr = virt_to_phys(exynos4_secondary_startup); 240beddf63fSTomasz Figa 241*9637f30eSTomasz Figa ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr); 242b3205deaSSachin Kamat if (ret && ret != -ENOSYS) 243b3205deaSSachin Kamat break; 244b3205deaSSachin Kamat if (ret == -ENOSYS) { 245*9637f30eSTomasz Figa void __iomem *boot_reg = cpu_boot_reg(core_id); 246b3205deaSSachin Kamat 247b3205deaSSachin Kamat if (IS_ERR(boot_reg)) 248b3205deaSSachin Kamat break; 249*9637f30eSTomasz Figa __raw_writel(boot_addr, cpu_boot_reg(core_id)); 250beddf63fSTomasz Figa } 25183014579SKukjin Kim } 252b3205deaSSachin Kamat } 25306853ae4SMarc Zyngier 25406853ae4SMarc Zyngier struct smp_operations exynos_smp_ops __initdata = { 25506853ae4SMarc Zyngier .smp_init_cpus = exynos_smp_init_cpus, 25606853ae4SMarc Zyngier .smp_prepare_cpus = exynos_smp_prepare_cpus, 25706853ae4SMarc Zyngier .smp_secondary_init = exynos_secondary_init, 25806853ae4SMarc Zyngier .smp_boot_secondary = exynos_boot_secondary, 25906853ae4SMarc Zyngier #ifdef CONFIG_HOTPLUG_CPU 26006853ae4SMarc Zyngier .cpu_die = exynos_cpu_die, 26106853ae4SMarc Zyngier #endif 26206853ae4SMarc Zyngier }; 263