platsmp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/cp15.h>
  24. #include <asm/smp_plat.h>
  25. #include <asm/smp_scu.h>
  26. #include <asm/firmware.h>
  27. #include <mach/map.h>
  28. #include "common.h"
  29. #include "regs-pmu.h"
  30. extern void exynos4_secondary_startup(void);
  31. /*
  32. * Set or clear the USE_DELAYED_RESET_ASSERTION option, set on Exynos4 SoCs
  33. * during hot-(un)plugging CPUx.
  34. *
  35. * The feature can be cleared safely during first boot of secondary CPU.
  36. *
  37. * Exynos4 SoCs require setting USE_DELAYED_RESET_ASSERTION during powering
  38. * down a CPU so the CPU idle clock down feature could properly detect global
  39. * idle state when CPUx is off.
  40. */
  41. static void exynos_set_delayed_reset_assertion(u32 core_id, bool enable)
  42. {
  43. if (soc_is_exynos4()) {
  44. unsigned int tmp;
  45. tmp = pmu_raw_readl(EXYNOS_ARM_CORE_OPTION(core_id));
  46. if (enable)
  47. tmp |= S5P_USE_DELAYED_RESET_ASSERTION;
  48. else
  49. tmp &= ~(S5P_USE_DELAYED_RESET_ASSERTION);
  50. pmu_raw_writel(tmp, EXYNOS_ARM_CORE_OPTION(core_id));
  51. }
  52. }
  53. #ifdef CONFIG_HOTPLUG_CPU
  54. static inline void cpu_leave_lowpower(u32 core_id)
  55. {
  56. unsigned int v;
  57. asm volatile(
  58. "mrc p15, 0, %0, c1, c0, 0\n"
  59. " orr %0, %0, %1\n"
  60. " mcr p15, 0, %0, c1, c0, 0\n"
  61. " mrc p15, 0, %0, c1, c0, 1\n"
  62. " orr %0, %0, %2\n"
  63. " mcr p15, 0, %0, c1, c0, 1\n"
  64. : "=&r" (v)
  65. : "Ir" (CR_C), "Ir" (0x40)
  66. : "cc");
  67. exynos_set_delayed_reset_assertion(core_id, false);
  68. }
  69. static inline void platform_do_lowpower(unsigned int cpu, int *spurious)
  70. {
  71. u32 mpidr = cpu_logical_map(cpu);
  72. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  73. for (;;) {
  74. /* Turn the CPU off on next WFI instruction. */
  75. exynos_cpu_power_down(core_id);
  76. /*
  77. * Exynos4 SoCs require setting
  78. * USE_DELAYED_RESET_ASSERTION so the CPU idle
  79. * clock down feature could properly detect
  80. * global idle state when CPUx is off.
  81. */
  82. exynos_set_delayed_reset_assertion(core_id, true);
  83. wfi();
  84. if (pen_release == core_id) {
  85. /*
  86. * OK, proper wakeup, we're done
  87. */
  88. break;
  89. }
  90. /*
  91. * Getting here, means that we have come out of WFI without
  92. * having been woken up - this shouldn't happen
  93. *
  94. * Just note it happening - when we're woken, we can report
  95. * its occurrence.
  96. */
  97. (*spurious)++;
  98. }
  99. }
  100. #endif /* CONFIG_HOTPLUG_CPU */
  101. /**
  102. * exynos_core_power_down : power down the specified cpu
  103. * @cpu : the cpu to power down
  104. *
  105. * Power down the specified cpu. The sequence must be finished by a
  106. * call to cpu_do_idle()
  107. *
  108. */
  109. void exynos_cpu_power_down(int cpu)
  110. {
  111. if (cpu == 0 && (of_machine_is_compatible("samsung,exynos5420") ||
  112. of_machine_is_compatible("samsung,exynos5800"))) {
  113. /*
  114. * Bypass power down for CPU0 during suspend. Check for
  115. * the SYS_PWR_REG value to decide if we are suspending
  116. * the system.
  117. */
  118. int val = pmu_raw_readl(EXYNOS5_ARM_CORE0_SYS_PWR_REG);
  119. if (!(val & S5P_CORE_LOCAL_PWR_EN))
  120. return;
  121. }
  122. pmu_raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  123. }
  124. /**
  125. * exynos_cpu_power_up : power up the specified cpu
  126. * @cpu : the cpu to power up
  127. *
  128. * Power up the specified cpu
  129. */
  130. void exynos_cpu_power_up(int cpu)
  131. {
  132. pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
  133. EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  134. }
  135. /**
  136. * exynos_cpu_power_state : returns the power state of the cpu
  137. * @cpu : the cpu to retrieve the power state from
  138. *
  139. */
  140. int exynos_cpu_power_state(int cpu)
  141. {
  142. return (pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
  143. S5P_CORE_LOCAL_PWR_EN);
  144. }
  145. /**
  146. * exynos_cluster_power_down : power down the specified cluster
  147. * @cluster : the cluster to power down
  148. */
  149. void exynos_cluster_power_down(int cluster)
  150. {
  151. pmu_raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
  152. }
  153. /**
  154. * exynos_cluster_power_up : power up the specified cluster
  155. * @cluster : the cluster to power up
  156. */
  157. void exynos_cluster_power_up(int cluster)
  158. {
  159. pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
  160. EXYNOS_COMMON_CONFIGURATION(cluster));
  161. }
  162. /**
  163. * exynos_cluster_power_state : returns the power state of the cluster
  164. * @cluster : the cluster to retrieve the power state from
  165. *
  166. */
  167. int exynos_cluster_power_state(int cluster)
  168. {
  169. return (pmu_raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
  170. S5P_CORE_LOCAL_PWR_EN);
  171. }
  172. static inline void __iomem *cpu_boot_reg_base(void)
  173. {
  174. if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1)
  175. return pmu_base_addr + S5P_INFORM5;
  176. return sysram_base_addr;
  177. }
  178. static inline void __iomem *cpu_boot_reg(int cpu)
  179. {
  180. void __iomem *boot_reg;
  181. boot_reg = cpu_boot_reg_base();
  182. if (!boot_reg)
  183. return ERR_PTR(-ENODEV);
  184. if (soc_is_exynos4412())
  185. boot_reg += 4*cpu;
  186. else if (soc_is_exynos5420() || soc_is_exynos5800())
  187. boot_reg += 4;
  188. return boot_reg;
  189. }
  190. /*
  191. * Set wake up by local power mode and execute software reset for given core.
  192. *
  193. * Currently this is needed only when booting secondary CPU on Exynos3250.
  194. */
  195. static void exynos_core_restart(u32 core_id)
  196. {
  197. u32 val;
  198. if (!of_machine_is_compatible("samsung,exynos3250"))
  199. return;
  200. val = pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(core_id));
  201. val |= S5P_CORE_WAKEUP_FROM_LOCAL_CFG;
  202. pmu_raw_writel(val, EXYNOS_ARM_CORE_STATUS(core_id));
  203. pr_info("CPU%u: Software reset\n", core_id);
  204. pmu_raw_writel(EXYNOS_CORE_PO_RESET(core_id), EXYNOS_SWRESET);
  205. }
  206. /*
  207. * Write pen_release in a way that is guaranteed to be visible to all
  208. * observers, irrespective of whether they're taking part in coherency
  209. * or not. This is necessary for the hotplug code to work reliably.
  210. */
  211. static void write_pen_release(int val)
  212. {
  213. pen_release = val;
  214. smp_wmb();
  215. sync_cache_w(&pen_release);
  216. }
  217. static void __iomem *scu_base_addr(void)
  218. {
  219. return (void __iomem *)(S5P_VA_SCU);
  220. }
  221. static DEFINE_SPINLOCK(boot_lock);
  222. static void exynos_secondary_init(unsigned int cpu)
  223. {
  224. /*
  225. * let the primary processor know we're out of the
  226. * pen, then head off into the C entry point
  227. */
  228. write_pen_release(-1);
  229. /*
  230. * Synchronise with the boot thread.
  231. */
  232. spin_lock(&boot_lock);
  233. spin_unlock(&boot_lock);
  234. }
  235. static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  236. {
  237. unsigned long timeout;
  238. u32 mpidr = cpu_logical_map(cpu);
  239. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  240. int ret = -ENOSYS;
  241. /*
  242. * Set synchronisation state between this boot processor
  243. * and the secondary one
  244. */
  245. spin_lock(&boot_lock);
  246. /*
  247. * The secondary processor is waiting to be released from
  248. * the holding pen - release it, then wait for it to flag
  249. * that it has been released by resetting pen_release.
  250. *
  251. * Note that "pen_release" is the hardware CPU core ID, whereas
  252. * "cpu" is Linux's internal ID.
  253. */
  254. write_pen_release(core_id);
  255. if (!exynos_cpu_power_state(core_id)) {
  256. exynos_cpu_power_up(core_id);
  257. timeout = 10;
  258. /* wait max 10 ms until cpu1 is on */
  259. while (exynos_cpu_power_state(core_id)
  260. != S5P_CORE_LOCAL_PWR_EN) {
  261. if (timeout-- == 0)
  262. break;
  263. mdelay(1);
  264. }
  265. if (timeout == 0) {
  266. printk(KERN_ERR "cpu1 power enable failed");
  267. spin_unlock(&boot_lock);
  268. return -ETIMEDOUT;
  269. }
  270. }
  271. exynos_core_restart(core_id);
  272. /*
  273. * Send the secondary CPU a soft interrupt, thereby causing
  274. * the boot monitor to read the system wide flags register,
  275. * and branch to the address found there.
  276. */
  277. timeout = jiffies + (1 * HZ);
  278. while (time_before(jiffies, timeout)) {
  279. unsigned long boot_addr;
  280. smp_rmb();
  281. boot_addr = virt_to_phys(exynos4_secondary_startup);
  282. /*
  283. * Try to set boot address using firmware first
  284. * and fall back to boot register if it fails.
  285. */
  286. ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
  287. if (ret && ret != -ENOSYS)
  288. goto fail;
  289. if (ret == -ENOSYS) {
  290. void __iomem *boot_reg = cpu_boot_reg(core_id);
  291. if (IS_ERR(boot_reg)) {
  292. ret = PTR_ERR(boot_reg);
  293. goto fail;
  294. }
  295. __raw_writel(boot_addr, boot_reg);
  296. }
  297. call_firmware_op(cpu_boot, core_id);
  298. arch_send_wakeup_ipi_mask(cpumask_of(cpu));
  299. if (pen_release == -1)
  300. break;
  301. udelay(10);
  302. }
  303. /* No harm if this is called during first boot of secondary CPU */
  304. exynos_set_delayed_reset_assertion(core_id, false);
  305. /*
  306. * now the secondary core is starting up let it run its
  307. * calibrations, then wait for it to finish
  308. */
  309. fail:
  310. spin_unlock(&boot_lock);
  311. return pen_release != -1 ? ret : 0;
  312. }
  313. /*
  314. * Initialise the CPU possible map early - this describes the CPUs
  315. * which may be present or become present in the system.
  316. */
  317. static void __init exynos_smp_init_cpus(void)
  318. {
  319. void __iomem *scu_base = scu_base_addr();
  320. unsigned int i, ncores;
  321. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  322. ncores = scu_base ? scu_get_core_count(scu_base) : 1;
  323. else
  324. /*
  325. * CPU Nodes are passed thru DT and set_cpu_possible
  326. * is set by "arm_dt_init_cpu_maps".
  327. */
  328. return;
  329. /* sanity check */
  330. if (ncores > nr_cpu_ids) {
  331. pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
  332. ncores, nr_cpu_ids);
  333. ncores = nr_cpu_ids;
  334. }
  335. for (i = 0; i < ncores; i++)
  336. set_cpu_possible(i, true);
  337. }
  338. static void __init exynos_smp_prepare_cpus(unsigned int max_cpus)
  339. {
  340. int i;
  341. exynos_sysram_init();
  342. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  343. scu_enable(scu_base_addr());
  344. /*
  345. * Write the address of secondary startup into the
  346. * system-wide flags register. The boot monitor waits
  347. * until it receives a soft interrupt, and then the
  348. * secondary CPU branches to this address.
  349. *
  350. * Try using firmware operation first and fall back to
  351. * boot register if it fails.
  352. */
  353. for (i = 1; i < max_cpus; ++i) {
  354. unsigned long boot_addr;
  355. u32 mpidr;
  356. u32 core_id;
  357. int ret;
  358. mpidr = cpu_logical_map(i);
  359. core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  360. boot_addr = virt_to_phys(exynos4_secondary_startup);
  361. ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
  362. if (ret && ret != -ENOSYS)
  363. break;
  364. if (ret == -ENOSYS) {
  365. void __iomem *boot_reg = cpu_boot_reg(core_id);
  366. if (IS_ERR(boot_reg))
  367. break;
  368. __raw_writel(boot_addr, boot_reg);
  369. }
  370. }
  371. }
  372. #ifdef CONFIG_HOTPLUG_CPU
  373. /*
  374. * platform-specific code to shutdown a CPU
  375. *
  376. * Called with IRQs disabled
  377. */
  378. static void exynos_cpu_die(unsigned int cpu)
  379. {
  380. int spurious = 0;
  381. u32 mpidr = cpu_logical_map(cpu);
  382. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  383. v7_exit_coherency_flush(louis);
  384. platform_do_lowpower(cpu, &spurious);
  385. /*
  386. * bring this CPU back into the world of cache
  387. * coherency, and then restore interrupts
  388. */
  389. cpu_leave_lowpower(core_id);
  390. if (spurious)
  391. pr_warn("CPU%u: %u spurious wakeup calls\n", cpu, spurious);
  392. }
  393. #endif /* CONFIG_HOTPLUG_CPU */
  394. struct smp_operations exynos_smp_ops __initdata = {
  395. .smp_init_cpus = exynos_smp_init_cpus,
  396. .smp_prepare_cpus = exynos_smp_prepare_cpus,
  397. .smp_secondary_init = exynos_secondary_init,
  398. .smp_boot_secondary = exynos_boot_secondary,
  399. #ifdef CONFIG_HOTPLUG_CPU
  400. .cpu_die = exynos_cpu_die,
  401. #endif
  402. };