platsmp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 && (soc_is_exynos5420() || soc_is_exynos5800())) {
  112. /*
  113. * Bypass power down for CPU0 during suspend. Check for
  114. * the SYS_PWR_REG value to decide if we are suspending
  115. * the system.
  116. */
  117. int val = pmu_raw_readl(EXYNOS5_ARM_CORE0_SYS_PWR_REG);
  118. if (!(val & S5P_CORE_LOCAL_PWR_EN))
  119. return;
  120. }
  121. pmu_raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  122. }
  123. /**
  124. * exynos_cpu_power_up : power up the specified cpu
  125. * @cpu : the cpu to power up
  126. *
  127. * Power up the specified cpu
  128. */
  129. void exynos_cpu_power_up(int cpu)
  130. {
  131. pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
  132. EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  133. }
  134. /**
  135. * exynos_cpu_power_state : returns the power state of the cpu
  136. * @cpu : the cpu to retrieve the power state from
  137. *
  138. */
  139. int exynos_cpu_power_state(int cpu)
  140. {
  141. return (pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
  142. S5P_CORE_LOCAL_PWR_EN);
  143. }
  144. /**
  145. * exynos_cluster_power_down : power down the specified cluster
  146. * @cluster : the cluster to power down
  147. */
  148. void exynos_cluster_power_down(int cluster)
  149. {
  150. pmu_raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
  151. }
  152. /**
  153. * exynos_cluster_power_up : power up the specified cluster
  154. * @cluster : the cluster to power up
  155. */
  156. void exynos_cluster_power_up(int cluster)
  157. {
  158. pmu_raw_writel(S5P_CORE_LOCAL_PWR_EN,
  159. EXYNOS_COMMON_CONFIGURATION(cluster));
  160. }
  161. /**
  162. * exynos_cluster_power_state : returns the power state of the cluster
  163. * @cluster : the cluster to retrieve the power state from
  164. *
  165. */
  166. int exynos_cluster_power_state(int cluster)
  167. {
  168. return (pmu_raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
  169. S5P_CORE_LOCAL_PWR_EN);
  170. }
  171. void __iomem *cpu_boot_reg_base(void)
  172. {
  173. if (soc_is_exynos4210() && samsung_rev() == EXYNOS4210_REV_1_1)
  174. return pmu_base_addr + S5P_INFORM5;
  175. return sysram_base_addr;
  176. }
  177. static inline void __iomem *cpu_boot_reg(int cpu)
  178. {
  179. void __iomem *boot_reg;
  180. boot_reg = cpu_boot_reg_base();
  181. if (!boot_reg)
  182. return ERR_PTR(-ENODEV);
  183. if (soc_is_exynos4412())
  184. boot_reg += 4*cpu;
  185. else if (soc_is_exynos5420() || soc_is_exynos5800())
  186. boot_reg += 4;
  187. return boot_reg;
  188. }
  189. /*
  190. * Set wake up by local power mode and execute software reset for given core.
  191. *
  192. * Currently this is needed only when booting secondary CPU on Exynos3250.
  193. */
  194. static void exynos_core_restart(u32 core_id)
  195. {
  196. u32 val;
  197. if (!of_machine_is_compatible("samsung,exynos3250"))
  198. return;
  199. val = pmu_raw_readl(EXYNOS_ARM_CORE_STATUS(core_id));
  200. val |= S5P_CORE_WAKEUP_FROM_LOCAL_CFG;
  201. pmu_raw_writel(val, EXYNOS_ARM_CORE_STATUS(core_id));
  202. pr_info("CPU%u: Software reset\n", core_id);
  203. pmu_raw_writel(EXYNOS_CORE_PO_RESET(core_id), EXYNOS_SWRESET);
  204. }
  205. /*
  206. * Write pen_release in a way that is guaranteed to be visible to all
  207. * observers, irrespective of whether they're taking part in coherency
  208. * or not. This is necessary for the hotplug code to work reliably.
  209. */
  210. static void write_pen_release(int val)
  211. {
  212. pen_release = val;
  213. smp_wmb();
  214. sync_cache_w(&pen_release);
  215. }
  216. static void __iomem *scu_base_addr(void)
  217. {
  218. return (void __iomem *)(S5P_VA_SCU);
  219. }
  220. static DEFINE_SPINLOCK(boot_lock);
  221. static void exynos_secondary_init(unsigned int cpu)
  222. {
  223. /*
  224. * let the primary processor know we're out of the
  225. * pen, then head off into the C entry point
  226. */
  227. write_pen_release(-1);
  228. /*
  229. * Synchronise with the boot thread.
  230. */
  231. spin_lock(&boot_lock);
  232. spin_unlock(&boot_lock);
  233. }
  234. static int exynos_boot_secondary(unsigned int cpu, struct task_struct *idle)
  235. {
  236. unsigned long timeout;
  237. u32 mpidr = cpu_logical_map(cpu);
  238. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  239. int ret = -ENOSYS;
  240. /*
  241. * Set synchronisation state between this boot processor
  242. * and the secondary one
  243. */
  244. spin_lock(&boot_lock);
  245. /*
  246. * The secondary processor is waiting to be released from
  247. * the holding pen - release it, then wait for it to flag
  248. * that it has been released by resetting pen_release.
  249. *
  250. * Note that "pen_release" is the hardware CPU core ID, whereas
  251. * "cpu" is Linux's internal ID.
  252. */
  253. write_pen_release(core_id);
  254. if (!exynos_cpu_power_state(core_id)) {
  255. exynos_cpu_power_up(core_id);
  256. timeout = 10;
  257. /* wait max 10 ms until cpu1 is on */
  258. while (exynos_cpu_power_state(core_id)
  259. != S5P_CORE_LOCAL_PWR_EN) {
  260. if (timeout-- == 0)
  261. break;
  262. mdelay(1);
  263. }
  264. if (timeout == 0) {
  265. printk(KERN_ERR "cpu1 power enable failed");
  266. spin_unlock(&boot_lock);
  267. return -ETIMEDOUT;
  268. }
  269. }
  270. exynos_core_restart(core_id);
  271. /*
  272. * Send the secondary CPU a soft interrupt, thereby causing
  273. * the boot monitor to read the system wide flags register,
  274. * and branch to the address found there.
  275. */
  276. timeout = jiffies + (1 * HZ);
  277. while (time_before(jiffies, timeout)) {
  278. unsigned long boot_addr;
  279. smp_rmb();
  280. boot_addr = virt_to_phys(exynos4_secondary_startup);
  281. /*
  282. * Try to set boot address using firmware first
  283. * and fall back to boot register if it fails.
  284. */
  285. ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
  286. if (ret && ret != -ENOSYS)
  287. goto fail;
  288. if (ret == -ENOSYS) {
  289. void __iomem *boot_reg = cpu_boot_reg(core_id);
  290. if (IS_ERR(boot_reg)) {
  291. ret = PTR_ERR(boot_reg);
  292. goto fail;
  293. }
  294. __raw_writel(boot_addr, boot_reg);
  295. }
  296. call_firmware_op(cpu_boot, core_id);
  297. arch_send_wakeup_ipi_mask(cpumask_of(cpu));
  298. if (pen_release == -1)
  299. break;
  300. udelay(10);
  301. }
  302. /* No harm if this is called during first boot of secondary CPU */
  303. exynos_set_delayed_reset_assertion(core_id, false);
  304. /*
  305. * now the secondary core is starting up let it run its
  306. * calibrations, then wait for it to finish
  307. */
  308. fail:
  309. spin_unlock(&boot_lock);
  310. return pen_release != -1 ? ret : 0;
  311. }
  312. /*
  313. * Initialise the CPU possible map early - this describes the CPUs
  314. * which may be present or become present in the system.
  315. */
  316. static void __init exynos_smp_init_cpus(void)
  317. {
  318. void __iomem *scu_base = scu_base_addr();
  319. unsigned int i, ncores;
  320. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  321. ncores = scu_base ? scu_get_core_count(scu_base) : 1;
  322. else
  323. /*
  324. * CPU Nodes are passed thru DT and set_cpu_possible
  325. * is set by "arm_dt_init_cpu_maps".
  326. */
  327. return;
  328. /* sanity check */
  329. if (ncores > nr_cpu_ids) {
  330. pr_warn("SMP: %u cores greater than maximum (%u), clipping\n",
  331. ncores, nr_cpu_ids);
  332. ncores = nr_cpu_ids;
  333. }
  334. for (i = 0; i < ncores; i++)
  335. set_cpu_possible(i, true);
  336. }
  337. static void __init exynos_smp_prepare_cpus(unsigned int max_cpus)
  338. {
  339. int i;
  340. exynos_sysram_init();
  341. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  342. scu_enable(scu_base_addr());
  343. /*
  344. * Write the address of secondary startup into the
  345. * system-wide flags register. The boot monitor waits
  346. * until it receives a soft interrupt, and then the
  347. * secondary CPU branches to this address.
  348. *
  349. * Try using firmware operation first and fall back to
  350. * boot register if it fails.
  351. */
  352. for (i = 1; i < max_cpus; ++i) {
  353. unsigned long boot_addr;
  354. u32 mpidr;
  355. u32 core_id;
  356. int ret;
  357. mpidr = cpu_logical_map(i);
  358. core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  359. boot_addr = virt_to_phys(exynos4_secondary_startup);
  360. ret = call_firmware_op(set_cpu_boot_addr, core_id, boot_addr);
  361. if (ret && ret != -ENOSYS)
  362. break;
  363. if (ret == -ENOSYS) {
  364. void __iomem *boot_reg = cpu_boot_reg(core_id);
  365. if (IS_ERR(boot_reg))
  366. break;
  367. __raw_writel(boot_addr, boot_reg);
  368. }
  369. }
  370. }
  371. #ifdef CONFIG_HOTPLUG_CPU
  372. /*
  373. * platform-specific code to shutdown a CPU
  374. *
  375. * Called with IRQs disabled
  376. */
  377. static void exynos_cpu_die(unsigned int cpu)
  378. {
  379. int spurious = 0;
  380. u32 mpidr = cpu_logical_map(cpu);
  381. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  382. v7_exit_coherency_flush(louis);
  383. platform_do_lowpower(cpu, &spurious);
  384. /*
  385. * bring this CPU back into the world of cache
  386. * coherency, and then restore interrupts
  387. */
  388. cpu_leave_lowpower(core_id);
  389. if (spurious)
  390. pr_warn("CPU%u: %u spurious wakeup calls\n", cpu, spurious);
  391. }
  392. #endif /* CONFIG_HOTPLUG_CPU */
  393. struct smp_operations exynos_smp_ops __initdata = {
  394. .smp_init_cpus = exynos_smp_init_cpus,
  395. .smp_prepare_cpus = exynos_smp_prepare_cpus,
  396. .smp_secondary_init = exynos_secondary_init,
  397. .smp_boot_secondary = exynos_boot_secondary,
  398. #ifdef CONFIG_HOTPLUG_CPU
  399. .cpu_die = exynos_cpu_die,
  400. #endif
  401. };