platsmp.c 11 KB

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