platsmp.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * Cloned from linux/arch/arm/mach-vexpress/platsmp.c
  6. *
  7. * Copyright (C) 2002 ARM Ltd.
  8. * All Rights Reserved
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/smp.h>
  20. #include <linux/io.h>
  21. #include <linux/of_address.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/smp_plat.h>
  24. #include <asm/smp_scu.h>
  25. #include <asm/firmware.h>
  26. #include <mach/map.h>
  27. #include "common.h"
  28. #include "regs-pmu.h"
  29. extern void exynos4_secondary_startup(void);
  30. /**
  31. * exynos_core_power_down : power down the specified cpu
  32. * @cpu : the cpu to power down
  33. *
  34. * Power down the specified cpu. The sequence must be finished by a
  35. * call to cpu_do_idle()
  36. *
  37. */
  38. void exynos_cpu_power_down(int cpu)
  39. {
  40. pmu_raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  41. }
  42. /**
  43. * exynos_cpu_power_up : power up the specified cpu
  44. * @cpu : the cpu to power up
  45. *
  46. * Power up the specified cpu
  47. */
  48. void exynos_cpu_power_up(int cpu)
  49. {
  50. pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
  51. EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  52. }
  53. /**
  54. * exynos_cpu_power_state : returns the power state of the cpu
  55. * @cpu : the cpu to retrieve the power state from
  56. *
  57. */
  58. int exynos_cpu_power_state(int cpu)
  59. {
  60. return (pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
  61. S5P_CORE_LOCAL_PWR_EN);
  62. }
  63. /**
  64. * exynos_cluster_power_down : power down the specified cluster
  65. * @cluster : the cluster to power down
  66. */
  67. void exynos_cluster_power_down(int cluster)
  68. {
  69. pmu_raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
  70. }
  71. /**
  72. * exynos_cluster_power_up : power up the specified cluster
  73. * @cluster : the cluster to power up
  74. */
  75. void exynos_cluster_power_up(int cluster)
  76. {
  77. pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
  78. EXYNOS_COMMON_CONFIGURATION(cluster));
  79. }
  80. /**
  81. * exynos_cluster_power_state : returns the power state of the cluster
  82. * @cluster : the cluster to retrieve the power state from
  83. *
  84. */
  85. int exynos_cluster_power_state(int cluster)
  86. {
  87. return (pmu_raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
  88. S5P_CORE_LOCAL_PWR_EN);
  89. }
  90. static inline void __iomem *cpu_boot_reg_base(void)
  91. {
  92. if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1)
  93. return pmu_base_addr + S5P_INFORM5;
  94. return sysram_base_addr;
  95. }
  96. static inline void __iomem *cpu_boot_reg(int cpu)
  97. {
  98. void __iomem *boot_reg;
  99. boot_reg = cpu_boot_reg_base();
  100. if (!boot_reg)
  101. return ERR_PTR(-ENODEV);
  102. if (soc_is_exynos4412())
  103. boot_reg += 4*cpu;
  104. else if (soc_is_exynos5420() || soc_is_exynos5800())
  105. boot_reg += 4;
  106. return boot_reg;
  107. }
  108. /*
  109. * Write pen_release in a way that is guaranteed to be visible to all
  110. * observers, irrespective of whether they're taking part in coherency
  111. * or not. This is necessary for the hotplug code to work reliably.
  112. */
  113. static void write_pen_release(int val)
  114. {
  115. pen_release = val;
  116. smp_wmb();
  117. sync_cache_w(&pen_release);
  118. }
  119. static void __iomem *scu_base_addr(void)
  120. {
  121. return (void __iomem *)(S5P_VA_SCU);
  122. }
  123. static DEFINE_SPINLOCK(boot_lock);
  124. static void exynos_secondary_init(unsigned int cpu)
  125. {
  126. /*
  127. * let the primary processor know we're out of the
  128. * pen, then head off into the C entry point
  129. */
  130. write_pen_release(-1);
  131. /*
  132. * Synchronise with the boot thread.
  133. */
  134. spin_lock(&boot_lock);
  135. spin_unlock(&boot_lock);
  136. }
  137. static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  138. {
  139. unsigned long timeout;
  140. u32 mpidr = cpu_logical_map(cpu);
  141. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  142. int ret = -ENOSYS;
  143. /*
  144. * Set synchronisation state between this boot processor
  145. * and the secondary one
  146. */
  147. spin_lock(&boot_lock);
  148. /*
  149. * The secondary processor is waiting to be released from
  150. * the holding pen - release it, then wait for it to flag
  151. * that it has been released by resetting pen_release.
  152. *
  153. * Note that "pen_release" is the hardware CPU core ID, whereas
  154. * "cpu" is Linux's internal ID.
  155. */
  156. write_pen_release(core_id);
  157. if (!exynos_cpu_power_state(core_id)) {
  158. exynos_cpu_power_up(core_id);
  159. timeout = 10;
  160. /* wait max 10 ms until cpu1 is on */
  161. while (exynos_cpu_power_state(core_id)
  162. != S5P_CORE_LOCAL_PWR_EN) {
  163. if (timeout-- == 0)
  164. break;
  165. mdelay(1);
  166. }
  167. if (timeout == 0) {
  168. printk(KERN_ERR "cpu1 power enable failed");
  169. spin_unlock(&boot_lock);
  170. return -ETIMEDOUT;
  171. }
  172. }
  173. /*
  174. * Send the secondary CPU a soft interrupt, thereby causing
  175. * the boot monitor to read the system wide flags register,
  176. * and branch to the address found there.
  177. */
  178. timeout = jiffies + (1 * HZ);
  179. while (time_before(jiffies, timeout)) {
  180. unsigned long boot_addr;
  181. smp_rmb();
  182. boot_addr = virt_to_phys(exynos4_secondary_startup);
  183. /*
  184. * Try to set boot address using firmware first
  185. * and fall back to boot register if it fails.
  186. */
  187. ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
  188. if (ret && ret != -ENOSYS)
  189. goto fail;
  190. if (ret == -ENOSYS) {
  191. void __iomem *boot_reg = cpu_boot_reg(core_id);
  192. if (IS_ERR(boot_reg)) {
  193. ret = PTR_ERR(boot_reg);
  194. goto fail;
  195. }
  196. __raw_writel(boot_addr, cpu_boot_reg(core_id));
  197. }
  198. call_firmware_op(cpu_boot, core_id);
  199. arch_send_wakeup_ipi_mask(cpumask_of(cpu));
  200. if (pen_release == -1)
  201. break;
  202. udelay(10);
  203. }
  204. /*
  205. * now the secondary core is starting up let it run its
  206. * calibrations, then wait for it to finish
  207. */
  208. fail:
  209. spin_unlock(&boot_lock);
  210. return pen_release != -1 ? ret : 0;
  211. }
  212. /*
  213. * Initialise the CPU possible map early - this describes the CPUs
  214. * which may be present or become present in the system.
  215. */
  216. static void __init exynos_smp_init_cpus(void)
  217. {
  218. void __iomem *scu_base = scu_base_addr();
  219. unsigned int i, ncores;
  220. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  221. ncores = scu_base ? scu_get_core_count(scu_base) : 1;
  222. else
  223. /*
  224. * CPU Nodes are passed thru DT and set_cpu_possible
  225. * is set by "arm_dt_init_cpu_maps".
  226. */
  227. return;
  228. /* sanity check */
  229. if (ncores > nr_cpu_ids) {
  230. pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
  231. ncores, nr_cpu_ids);
  232. ncores = nr_cpu_ids;
  233. }
  234. for (i = 0; i < ncores; i++)
  235. set_cpu_possible(i, true);
  236. }
  237. static void __init exynos_smp_prepare_cpus(unsigned int max_cpus)
  238. {
  239. int i;
  240. exynos_sysram_init();
  241. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  242. scu_enable(scu_base_addr());
  243. /*
  244. * Write the address of secondary startup into the
  245. * system-wide flags register. The boot monitor waits
  246. * until it receives a soft interrupt, and then the
  247. * secondary CPU branches to this address.
  248. *
  249. * Try using firmware operation first and fall back to
  250. * boot register if it fails.
  251. */
  252. for (i = 1; i < max_cpus; ++i) {
  253. unsigned long boot_addr;
  254. u32 mpidr;
  255. u32 core_id;
  256. int ret;
  257. mpidr = cpu_logical_map(i);
  258. core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  259. boot_addr = virt_to_phys(exynos4_secondary_startup);
  260. ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
  261. if (ret && ret != -ENOSYS)
  262. break;
  263. if (ret == -ENOSYS) {
  264. void __iomem *boot_reg = cpu_boot_reg(core_id);
  265. if (IS_ERR(boot_reg))
  266. break;
  267. __raw_writel(boot_addr, cpu_boot_reg(core_id));
  268. }
  269. }
  270. }
  271. struct smp_operations exynos_smp_ops __initdata = {
  272. .smp_init_cpus = exynos_smp_init_cpus,
  273. .smp_prepare_cpus = exynos_smp_prepare_cpus,
  274. .smp_secondary_init = exynos_secondary_init,
  275. .smp_boot_secondary = exynos_boot_secondary,
  276. #ifdef CONFIG_HOTPLUG_CPU
  277. .cpu_die = exynos_cpu_die,
  278. #endif
  279. };