platsmp-apmu.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * SMP support for SoCs with APMU
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  5. * Copyright (C) 2013 Magnus Damm
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/cpu_pm.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/of_address.h>
  17. #include <linux/smp.h>
  18. #include <linux/suspend.h>
  19. #include <linux/threads.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/cp15.h>
  22. #include <asm/proc-fns.h>
  23. #include <asm/smp_plat.h>
  24. #include <asm/suspend.h>
  25. #include "common.h"
  26. #include "platsmp-apmu.h"
  27. #include "rcar-gen2.h"
  28. static struct {
  29. void __iomem *iomem;
  30. int bit;
  31. } apmu_cpus[NR_CPUS];
  32. #define WUPCR_OFFS 0x10
  33. #define PSTR_OFFS 0x40
  34. #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
  35. static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
  36. {
  37. /* request power on */
  38. writel_relaxed(BIT(bit), p + WUPCR_OFFS);
  39. /* wait for APMU to finish */
  40. while (readl_relaxed(p + WUPCR_OFFS) != 0)
  41. ;
  42. return 0;
  43. }
  44. static int __maybe_unused apmu_power_off(void __iomem *p, int bit)
  45. {
  46. /* request Core Standby for next WFI */
  47. writel_relaxed(3, p + CPUNCR_OFFS(bit));
  48. return 0;
  49. }
  50. static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
  51. {
  52. int k;
  53. for (k = 0; k < 1000; k++) {
  54. if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3)
  55. return 1;
  56. mdelay(1);
  57. }
  58. return 0;
  59. }
  60. static int __maybe_unused apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
  61. {
  62. void __iomem *p = apmu_cpus[cpu].iomem;
  63. return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
  64. }
  65. #ifdef CONFIG_SMP
  66. static void apmu_init_cpu(struct resource *res, int cpu, int bit)
  67. {
  68. if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
  69. return;
  70. apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
  71. apmu_cpus[cpu].bit = bit;
  72. pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
  73. }
  74. static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit),
  75. struct rcar_apmu_config *apmu_config, int num)
  76. {
  77. int id;
  78. int k;
  79. int bit, index;
  80. bool is_allowed;
  81. for (k = 0; k < num; k++) {
  82. /* only enable the cluster that includes the boot CPU */
  83. is_allowed = false;
  84. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  85. id = apmu_config[k].cpus[bit];
  86. if (id >= 0) {
  87. if (id == cpu_logical_map(0))
  88. is_allowed = true;
  89. }
  90. }
  91. if (!is_allowed)
  92. continue;
  93. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  94. id = apmu_config[k].cpus[bit];
  95. if (id >= 0) {
  96. index = get_logical_index(id);
  97. if (index >= 0)
  98. fn(&apmu_config[k].iomem, index, bit);
  99. }
  100. }
  101. }
  102. }
  103. static const struct of_device_id apmu_ids[] = {
  104. { .compatible = "renesas,apmu" },
  105. { /*sentinel*/ }
  106. };
  107. static void apmu_parse_dt(void (*fn)(struct resource *res, int cpu, int bit))
  108. {
  109. struct device_node *np_apmu, *np_cpu;
  110. struct resource res;
  111. int bit, index;
  112. u32 id;
  113. for_each_matching_node(np_apmu, apmu_ids) {
  114. /* only enable the cluster that includes the boot CPU */
  115. bool is_allowed = false;
  116. for (bit = 0; bit < CONFIG_NR_CPUS; bit++) {
  117. np_cpu = of_parse_phandle(np_apmu, "cpus", bit);
  118. if (np_cpu) {
  119. if (!of_property_read_u32(np_cpu, "reg", &id)) {
  120. if (id == cpu_logical_map(0)) {
  121. is_allowed = true;
  122. of_node_put(np_cpu);
  123. break;
  124. }
  125. }
  126. of_node_put(np_cpu);
  127. }
  128. }
  129. if (!is_allowed)
  130. continue;
  131. for (bit = 0; bit < CONFIG_NR_CPUS; bit++) {
  132. np_cpu = of_parse_phandle(np_apmu, "cpus", bit);
  133. if (np_cpu) {
  134. if (!of_property_read_u32(np_cpu, "reg", &id)) {
  135. index = get_logical_index(id);
  136. if ((index >= 0) &&
  137. !of_address_to_resource(np_apmu,
  138. 0, &res))
  139. fn(&res, index, bit);
  140. }
  141. of_node_put(np_cpu);
  142. }
  143. }
  144. }
  145. }
  146. static void __init shmobile_smp_apmu_setup_boot(void)
  147. {
  148. /* install boot code shared by all CPUs */
  149. shmobile_boot_fn = virt_to_phys(shmobile_smp_boot);
  150. }
  151. void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus,
  152. struct rcar_apmu_config *apmu_config,
  153. int num)
  154. {
  155. shmobile_smp_apmu_setup_boot();
  156. apmu_parse_cfg(apmu_init_cpu, apmu_config, num);
  157. }
  158. int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
  159. {
  160. /* For this particular CPU register boot vector */
  161. shmobile_smp_hook(cpu, virt_to_phys(secondary_startup), 0);
  162. return apmu_wrap(cpu, apmu_power_on);
  163. }
  164. static void __init shmobile_smp_apmu_prepare_cpus_dt(unsigned int max_cpus)
  165. {
  166. shmobile_smp_apmu_setup_boot();
  167. apmu_parse_dt(apmu_init_cpu);
  168. rcar_gen2_pm_init();
  169. }
  170. static int shmobile_smp_apmu_boot_secondary_md21(unsigned int cpu,
  171. struct task_struct *idle)
  172. {
  173. /* Error out when hardware debug mode is enabled */
  174. if (rcar_gen2_read_mode_pins() & BIT(21)) {
  175. pr_warn("Unable to boot CPU%u when MD21 is set\n", cpu);
  176. return -ENOTSUPP;
  177. }
  178. return shmobile_smp_apmu_boot_secondary(cpu, idle);
  179. }
  180. static struct smp_operations apmu_smp_ops __initdata = {
  181. .smp_prepare_cpus = shmobile_smp_apmu_prepare_cpus_dt,
  182. .smp_boot_secondary = shmobile_smp_apmu_boot_secondary_md21,
  183. #ifdef CONFIG_HOTPLUG_CPU
  184. .cpu_can_disable = shmobile_smp_cpu_can_disable,
  185. .cpu_die = shmobile_smp_apmu_cpu_die,
  186. .cpu_kill = shmobile_smp_apmu_cpu_kill,
  187. #endif
  188. };
  189. CPU_METHOD_OF_DECLARE(shmobile_smp_apmu, "renesas,apmu", &apmu_smp_ops);
  190. #endif /* CONFIG_SMP */
  191. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
  192. /* nicked from arch/arm/mach-exynos/hotplug.c */
  193. static inline void cpu_enter_lowpower_a15(void)
  194. {
  195. unsigned int v;
  196. asm volatile(
  197. " mrc p15, 0, %0, c1, c0, 0\n"
  198. " bic %0, %0, %1\n"
  199. " mcr p15, 0, %0, c1, c0, 0\n"
  200. : "=&r" (v)
  201. : "Ir" (CR_C)
  202. : "cc");
  203. flush_cache_louis();
  204. asm volatile(
  205. /*
  206. * Turn off coherency
  207. */
  208. " mrc p15, 0, %0, c1, c0, 1\n"
  209. " bic %0, %0, %1\n"
  210. " mcr p15, 0, %0, c1, c0, 1\n"
  211. : "=&r" (v)
  212. : "Ir" (0x40)
  213. : "cc");
  214. isb();
  215. dsb();
  216. }
  217. static void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
  218. {
  219. /* Select next sleep mode using the APMU */
  220. apmu_wrap(cpu, apmu_power_off);
  221. /* Do ARM specific CPU shutdown */
  222. cpu_enter_lowpower_a15();
  223. }
  224. static inline void cpu_leave_lowpower(void)
  225. {
  226. unsigned int v;
  227. asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
  228. " orr %0, %0, %1\n"
  229. " mcr p15, 0, %0, c1, c0, 0\n"
  230. " mrc p15, 0, %0, c1, c0, 1\n"
  231. " orr %0, %0, %2\n"
  232. " mcr p15, 0, %0, c1, c0, 1\n"
  233. : "=&r" (v)
  234. : "Ir" (CR_C), "Ir" (0x40)
  235. : "cc");
  236. }
  237. #endif
  238. #if defined(CONFIG_HOTPLUG_CPU)
  239. void shmobile_smp_apmu_cpu_die(unsigned int cpu)
  240. {
  241. /* For this particular CPU deregister boot vector */
  242. shmobile_smp_hook(cpu, 0, 0);
  243. /* Shutdown CPU core */
  244. shmobile_smp_apmu_cpu_shutdown(cpu);
  245. /* jump to shared mach-shmobile sleep / reset code */
  246. shmobile_smp_sleep();
  247. }
  248. int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
  249. {
  250. return apmu_wrap(cpu, apmu_power_off_poll);
  251. }
  252. #endif
  253. #if defined(CONFIG_SUSPEND)
  254. static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
  255. {
  256. shmobile_smp_hook(cpu, virt_to_phys(cpu_resume), 0);
  257. shmobile_smp_apmu_cpu_shutdown(cpu);
  258. cpu_do_idle(); /* WFI selects Core Standby */
  259. return 1;
  260. }
  261. static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
  262. {
  263. cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
  264. cpu_leave_lowpower();
  265. return 0;
  266. }
  267. void __init shmobile_smp_apmu_suspend_init(void)
  268. {
  269. shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
  270. }
  271. #endif