14552386aSPankaj Dubey /* 283014579SKukjin Kim * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. 383014579SKukjin Kim * http://www.samsung.com 483014579SKukjin Kim * 583014579SKukjin Kim * Cloned from linux/arch/arm/mach-vexpress/platsmp.c 683014579SKukjin Kim * 783014579SKukjin Kim * Copyright (C) 2002 ARM Ltd. 883014579SKukjin Kim * All Rights Reserved 983014579SKukjin Kim * 1083014579SKukjin Kim * This program is free software; you can redistribute it and/or modify 1183014579SKukjin Kim * it under the terms of the GNU General Public License version 2 as 1283014579SKukjin Kim * published by the Free Software Foundation. 1383014579SKukjin Kim */ 1483014579SKukjin Kim 1583014579SKukjin Kim #include <linux/init.h> 1683014579SKukjin Kim #include <linux/errno.h> 1783014579SKukjin Kim #include <linux/delay.h> 1883014579SKukjin Kim #include <linux/device.h> 1983014579SKukjin Kim #include <linux/jiffies.h> 2083014579SKukjin Kim #include <linux/smp.h> 2183014579SKukjin Kim #include <linux/io.h> 22b3205deaSSachin Kamat #include <linux/of_address.h> 2383014579SKukjin Kim 2483014579SKukjin Kim #include <asm/cacheflush.h> 25eb50439bSWill Deacon #include <asm/smp_plat.h> 2683014579SKukjin Kim #include <asm/smp_scu.h> 27beddf63fSTomasz Figa #include <asm/firmware.h> 2883014579SKukjin Kim 292e94ac42SPankaj Dubey #include <mach/map.h> 302e94ac42SPankaj Dubey 3106853ae4SMarc Zyngier #include "common.h" 3265c9a853SKukjin Kim #include "regs-pmu.h" 3306853ae4SMarc Zyngier 3483014579SKukjin Kim extern void exynos4_secondary_startup(void); 3583014579SKukjin Kim 367310d99fSKrzysztof Kozlowski /** 377310d99fSKrzysztof Kozlowski * exynos_core_power_down : power down the specified cpu 387310d99fSKrzysztof Kozlowski * @cpu : the cpu to power down 397310d99fSKrzysztof Kozlowski * 407310d99fSKrzysztof Kozlowski * Power down the specified cpu. The sequence must be finished by a 417310d99fSKrzysztof Kozlowski * call to cpu_do_idle() 427310d99fSKrzysztof Kozlowski * 437310d99fSKrzysztof Kozlowski */ 447310d99fSKrzysztof Kozlowski void exynos_cpu_power_down(int cpu) 457310d99fSKrzysztof Kozlowski { 46*944483d0SArnd Bergmann pmu_raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu)); 477310d99fSKrzysztof Kozlowski } 487310d99fSKrzysztof Kozlowski 497310d99fSKrzysztof Kozlowski /** 507310d99fSKrzysztof Kozlowski * exynos_cpu_power_up : power up the specified cpu 517310d99fSKrzysztof Kozlowski * @cpu : the cpu to power up 527310d99fSKrzysztof Kozlowski * 537310d99fSKrzysztof Kozlowski * Power up the specified cpu 547310d99fSKrzysztof Kozlowski */ 557310d99fSKrzysztof Kozlowski void exynos_cpu_power_up(int cpu) 567310d99fSKrzysztof Kozlowski { 57*944483d0SArnd Bergmann pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN, 587310d99fSKrzysztof Kozlowski EXYNOS_ARM_CORE_CONFIGURATION(cpu)); 597310d99fSKrzysztof Kozlowski } 607310d99fSKrzysztof Kozlowski 617310d99fSKrzysztof Kozlowski /** 627310d99fSKrzysztof Kozlowski * exynos_cpu_power_state : returns the power state of the cpu 637310d99fSKrzysztof Kozlowski * @cpu : the cpu to retrieve the power state from 647310d99fSKrzysztof Kozlowski * 657310d99fSKrzysztof Kozlowski */ 667310d99fSKrzysztof Kozlowski int exynos_cpu_power_state(int cpu) 677310d99fSKrzysztof Kozlowski { 68*944483d0SArnd Bergmann return (pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) & 697310d99fSKrzysztof Kozlowski S5P_CORE_LOCAL_PWR_EN); 707310d99fSKrzysztof Kozlowski } 717310d99fSKrzysztof Kozlowski 727310d99fSKrzysztof Kozlowski /** 737310d99fSKrzysztof Kozlowski * exynos_cluster_power_down : power down the specified cluster 747310d99fSKrzysztof Kozlowski * @cluster : the cluster to power down 757310d99fSKrzysztof Kozlowski */ 767310d99fSKrzysztof Kozlowski void exynos_cluster_power_down(int cluster) 777310d99fSKrzysztof Kozlowski { 78*944483d0SArnd Bergmann pmu_raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster)); 797310d99fSKrzysztof Kozlowski } 807310d99fSKrzysztof Kozlowski 817310d99fSKrzysztof Kozlowski /** 827310d99fSKrzysztof Kozlowski * exynos_cluster_power_up : power up the specified cluster 837310d99fSKrzysztof Kozlowski * @cluster : the cluster to power up 847310d99fSKrzysztof Kozlowski */ 857310d99fSKrzysztof Kozlowski void exynos_cluster_power_up(int cluster) 867310d99fSKrzysztof Kozlowski { 87*944483d0SArnd Bergmann pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN, 887310d99fSKrzysztof Kozlowski EXYNOS_COMMON_CONFIGURATION(cluster)); 897310d99fSKrzysztof Kozlowski } 907310d99fSKrzysztof Kozlowski 917310d99fSKrzysztof Kozlowski /** 927310d99fSKrzysztof Kozlowski * exynos_cluster_power_state : returns the power state of the cluster 937310d99fSKrzysztof Kozlowski * @cluster : the cluster to retrieve the power state from 947310d99fSKrzysztof Kozlowski * 957310d99fSKrzysztof Kozlowski */ 967310d99fSKrzysztof Kozlowski int exynos_cluster_power_state(int cluster) 977310d99fSKrzysztof Kozlowski { 98*944483d0SArnd Bergmann return (pmu_raw_readl(EXYNOS_COMMON_STATUS(cluster)) & 997310d99fSKrzysztof Kozlowski S5P_CORE_LOCAL_PWR_EN); 1007310d99fSKrzysztof Kozlowski } 1017310d99fSKrzysztof Kozlowski 1021f054f52STomasz Figa static inline void __iomem *cpu_boot_reg_base(void) 1031f054f52STomasz Figa { 1041f054f52STomasz Figa if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1) 1052e94ac42SPankaj Dubey return pmu_base_addr + S5P_INFORM5; 106b3205deaSSachin Kamat return sysram_base_addr; 1071f054f52STomasz Figa } 1081f054f52STomasz Figa 1091f054f52STomasz Figa static inline void __iomem *cpu_boot_reg(int cpu) 1101f054f52STomasz Figa { 1111f054f52STomasz Figa void __iomem *boot_reg; 1121f054f52STomasz Figa 1131f054f52STomasz Figa boot_reg = cpu_boot_reg_base(); 114b3205deaSSachin Kamat if (!boot_reg) 115b3205deaSSachin Kamat return ERR_PTR(-ENODEV); 1161f054f52STomasz Figa if (soc_is_exynos4412()) 1171f054f52STomasz Figa boot_reg += 4*cpu; 11886c6f148SArun Kumar K else if (soc_is_exynos5420() || soc_is_exynos5800()) 1191580be3dSChander Kashyap boot_reg += 4; 1201f054f52STomasz Figa return boot_reg; 1211f054f52STomasz Figa } 12283014579SKukjin Kim 12383014579SKukjin Kim /* 12483014579SKukjin Kim * Write pen_release in a way that is guaranteed to be visible to all 12583014579SKukjin Kim * observers, irrespective of whether they're taking part in coherency 12683014579SKukjin Kim * or not. This is necessary for the hotplug code to work reliably. 12783014579SKukjin Kim */ 12883014579SKukjin Kim static void write_pen_release(int val) 12983014579SKukjin Kim { 13083014579SKukjin Kim pen_release = val; 13183014579SKukjin Kim smp_wmb(); 132f45913fdSNicolas Pitre sync_cache_w(&pen_release); 13383014579SKukjin Kim } 13483014579SKukjin Kim 13583014579SKukjin Kim static void __iomem *scu_base_addr(void) 13683014579SKukjin Kim { 13783014579SKukjin Kim return (void __iomem *)(S5P_VA_SCU); 13883014579SKukjin Kim } 13983014579SKukjin Kim 14083014579SKukjin Kim static DEFINE_SPINLOCK(boot_lock); 14183014579SKukjin Kim 1428bd26e3aSPaul Gortmaker static void exynos_secondary_init(unsigned int cpu) 14383014579SKukjin Kim { 14483014579SKukjin Kim /* 14583014579SKukjin Kim * let the primary processor know we're out of the 14683014579SKukjin Kim * pen, then head off into the C entry point 14783014579SKukjin Kim */ 14883014579SKukjin Kim write_pen_release(-1); 14983014579SKukjin Kim 15083014579SKukjin Kim /* 15183014579SKukjin Kim * Synchronise with the boot thread. 15283014579SKukjin Kim */ 15383014579SKukjin Kim spin_lock(&boot_lock); 15483014579SKukjin Kim spin_unlock(&boot_lock); 15583014579SKukjin Kim } 15683014579SKukjin Kim 1578bd26e3aSPaul Gortmaker static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle) 15883014579SKukjin Kim { 15983014579SKukjin Kim unsigned long timeout; 1609637f30eSTomasz Figa u32 mpidr = cpu_logical_map(cpu); 1619637f30eSTomasz Figa u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); 162b3205deaSSachin Kamat int ret = -ENOSYS; 16383014579SKukjin Kim 16483014579SKukjin Kim /* 16583014579SKukjin Kim * Set synchronisation state between this boot processor 16683014579SKukjin Kim * and the secondary one 16783014579SKukjin Kim */ 16883014579SKukjin Kim spin_lock(&boot_lock); 16983014579SKukjin Kim 17083014579SKukjin Kim /* 17183014579SKukjin Kim * The secondary processor is waiting to be released from 17283014579SKukjin Kim * the holding pen - release it, then wait for it to flag 17383014579SKukjin Kim * that it has been released by resetting pen_release. 17483014579SKukjin Kim * 1759637f30eSTomasz Figa * Note that "pen_release" is the hardware CPU core ID, whereas 17683014579SKukjin Kim * "cpu" is Linux's internal ID. 17783014579SKukjin Kim */ 1789637f30eSTomasz Figa write_pen_release(core_id); 17983014579SKukjin Kim 1809637f30eSTomasz Figa if (!exynos_cpu_power_state(core_id)) { 1819637f30eSTomasz Figa exynos_cpu_power_up(core_id); 18283014579SKukjin Kim timeout = 10; 18383014579SKukjin Kim 18483014579SKukjin Kim /* wait max 10 ms until cpu1 is on */ 1859637f30eSTomasz Figa while (exynos_cpu_power_state(core_id) 1869637f30eSTomasz Figa != S5P_CORE_LOCAL_PWR_EN) { 18783014579SKukjin Kim if (timeout-- == 0) 18883014579SKukjin Kim break; 18983014579SKukjin Kim 19083014579SKukjin Kim mdelay(1); 19183014579SKukjin Kim } 19283014579SKukjin Kim 19383014579SKukjin Kim if (timeout == 0) { 19483014579SKukjin Kim printk(KERN_ERR "cpu1 power enable failed"); 19583014579SKukjin Kim spin_unlock(&boot_lock); 19683014579SKukjin Kim return -ETIMEDOUT; 19783014579SKukjin Kim } 19883014579SKukjin Kim } 19983014579SKukjin Kim /* 20083014579SKukjin Kim * Send the secondary CPU a soft interrupt, thereby causing 20183014579SKukjin Kim * the boot monitor to read the system wide flags register, 20283014579SKukjin Kim * and branch to the address found there. 20383014579SKukjin Kim */ 20483014579SKukjin Kim 20583014579SKukjin Kim timeout = jiffies + (1 * HZ); 20683014579SKukjin Kim while (time_before(jiffies, timeout)) { 207beddf63fSTomasz Figa unsigned long boot_addr; 208beddf63fSTomasz Figa 20983014579SKukjin Kim smp_rmb(); 21083014579SKukjin Kim 211beddf63fSTomasz Figa boot_addr = virt_to_phys(exynos4_secondary_startup); 212beddf63fSTomasz Figa 213beddf63fSTomasz Figa /* 214beddf63fSTomasz Figa * Try to set boot address using firmware first 215beddf63fSTomasz Figa * and fall back to boot register if it fails. 216beddf63fSTomasz Figa */ 2179637f30eSTomasz Figa ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr); 218b3205deaSSachin Kamat if (ret && ret != -ENOSYS) 219b3205deaSSachin Kamat goto fail; 220b3205deaSSachin Kamat if (ret == -ENOSYS) { 2219637f30eSTomasz Figa void __iomem *boot_reg = cpu_boot_reg(core_id); 222b3205deaSSachin Kamat 223b3205deaSSachin Kamat if (IS_ERR(boot_reg)) { 224b3205deaSSachin Kamat ret = PTR_ERR(boot_reg); 225b3205deaSSachin Kamat goto fail; 226b3205deaSSachin Kamat } 2279637f30eSTomasz Figa __raw_writel(boot_addr, cpu_boot_reg(core_id)); 228b3205deaSSachin Kamat } 229beddf63fSTomasz Figa 2309637f30eSTomasz Figa call_firmware_op(cpu_boot, core_id); 231beddf63fSTomasz Figa 232b1cffebfSRob Herring arch_send_wakeup_ipi_mask(cpumask_of(cpu)); 23383014579SKukjin Kim 23483014579SKukjin Kim if (pen_release == -1) 23583014579SKukjin Kim break; 23683014579SKukjin Kim 23783014579SKukjin Kim udelay(10); 23883014579SKukjin Kim } 23983014579SKukjin Kim 24083014579SKukjin Kim /* 24183014579SKukjin Kim * now the secondary core is starting up let it run its 24283014579SKukjin Kim * calibrations, then wait for it to finish 24383014579SKukjin Kim */ 244b3205deaSSachin Kamat fail: 24583014579SKukjin Kim spin_unlock(&boot_lock); 24683014579SKukjin Kim 247b3205deaSSachin Kamat return pen_release != -1 ? ret : 0; 24883014579SKukjin Kim } 24983014579SKukjin Kim 25083014579SKukjin Kim /* 25183014579SKukjin Kim * Initialise the CPU possible map early - this describes the CPUs 25283014579SKukjin Kim * which may be present or become present in the system. 25383014579SKukjin Kim */ 25483014579SKukjin Kim 25506853ae4SMarc Zyngier static void __init exynos_smp_init_cpus(void) 25683014579SKukjin Kim { 25783014579SKukjin Kim void __iomem *scu_base = scu_base_addr(); 25883014579SKukjin Kim unsigned int i, ncores; 25983014579SKukjin Kim 2601897d2f3SChander Kashyap if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9) 26183014579SKukjin Kim ncores = scu_base ? scu_get_core_count(scu_base) : 1; 2621897d2f3SChander Kashyap else 2631897d2f3SChander Kashyap /* 2641897d2f3SChander Kashyap * CPU Nodes are passed thru DT and set_cpu_possible 2651897d2f3SChander Kashyap * is set by "arm_dt_init_cpu_maps". 2661897d2f3SChander Kashyap */ 2671897d2f3SChander Kashyap return; 26883014579SKukjin Kim 26983014579SKukjin Kim /* sanity check */ 27083014579SKukjin Kim if (ncores > nr_cpu_ids) { 27183014579SKukjin Kim pr_warn("SMP: %u cores greater than maximum (%u), clipping\n", 27283014579SKukjin Kim ncores, nr_cpu_ids); 27383014579SKukjin Kim ncores = nr_cpu_ids; 27483014579SKukjin Kim } 27583014579SKukjin Kim 27683014579SKukjin Kim for (i = 0; i < ncores; i++) 27783014579SKukjin Kim set_cpu_possible(i, true); 27883014579SKukjin Kim } 27983014579SKukjin Kim 28006853ae4SMarc Zyngier static void __init exynos_smp_prepare_cpus(unsigned int max_cpus) 28183014579SKukjin Kim { 2821f054f52STomasz Figa int i; 2831f054f52STomasz Figa 2841754c42eSOlof Johansson exynos_sysram_init(); 2851754c42eSOlof Johansson 286b5f3c75aSLeela Krishna Amudala if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9) 28783014579SKukjin Kim scu_enable(scu_base_addr()); 28883014579SKukjin Kim 28983014579SKukjin Kim /* 29083014579SKukjin Kim * Write the address of secondary startup into the 29183014579SKukjin Kim * system-wide flags register. The boot monitor waits 29283014579SKukjin Kim * until it receives a soft interrupt, and then the 29383014579SKukjin Kim * secondary CPU branches to this address. 294beddf63fSTomasz Figa * 295beddf63fSTomasz Figa * Try using firmware operation first and fall back to 296beddf63fSTomasz Figa * boot register if it fails. 29783014579SKukjin Kim */ 298beddf63fSTomasz Figa for (i = 1; i < max_cpus; ++i) { 299beddf63fSTomasz Figa unsigned long boot_addr; 3009637f30eSTomasz Figa u32 mpidr; 3019637f30eSTomasz Figa u32 core_id; 302b3205deaSSachin Kamat int ret; 303beddf63fSTomasz Figa 3049637f30eSTomasz Figa mpidr = cpu_logical_map(i); 3059637f30eSTomasz Figa core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); 306beddf63fSTomasz Figa boot_addr = virt_to_phys(exynos4_secondary_startup); 307beddf63fSTomasz Figa 3089637f30eSTomasz Figa ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr); 309b3205deaSSachin Kamat if (ret && ret != -ENOSYS) 310b3205deaSSachin Kamat break; 311b3205deaSSachin Kamat if (ret == -ENOSYS) { 3129637f30eSTomasz Figa void __iomem *boot_reg = cpu_boot_reg(core_id); 313b3205deaSSachin Kamat 314b3205deaSSachin Kamat if (IS_ERR(boot_reg)) 315b3205deaSSachin Kamat break; 3169637f30eSTomasz Figa __raw_writel(boot_addr, cpu_boot_reg(core_id)); 317beddf63fSTomasz Figa } 31883014579SKukjin Kim } 319b3205deaSSachin Kamat } 32006853ae4SMarc Zyngier 32106853ae4SMarc Zyngier struct smp_operations exynos_smp_ops __initdata = { 32206853ae4SMarc Zyngier .smp_init_cpus = exynos_smp_init_cpus, 32306853ae4SMarc Zyngier .smp_prepare_cpus = exynos_smp_prepare_cpus, 32406853ae4SMarc Zyngier .smp_secondary_init = exynos_secondary_init, 32506853ae4SMarc Zyngier .smp_boot_secondary = exynos_boot_secondary, 32606853ae4SMarc Zyngier #ifdef CONFIG_HOTPLUG_CPU 32706853ae4SMarc Zyngier .cpu_die = exynos_cpu_die, 32806853ae4SMarc Zyngier #endif 32906853ae4SMarc Zyngier }; 330