platsmp.c 9.9 KB

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