platsmp.c 10 KB

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