platsmp-apmu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. static struct {
  28. void __iomem *iomem;
  29. int bit;
  30. } apmu_cpus[NR_CPUS];
  31. #define WUPCR_OFFS 0x10
  32. #define PSTR_OFFS 0x40
  33. #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
  34. static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
  35. {
  36. /* request power on */
  37. writel_relaxed(BIT(bit), p + WUPCR_OFFS);
  38. /* wait for APMU to finish */
  39. while (readl_relaxed(p + WUPCR_OFFS) != 0)
  40. ;
  41. return 0;
  42. }
  43. static int __maybe_unused apmu_power_off(void __iomem *p, int bit)
  44. {
  45. /* request Core Standby for next WFI */
  46. writel_relaxed(3, p + CPUNCR_OFFS(bit));
  47. return 0;
  48. }
  49. static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
  50. {
  51. int k;
  52. for (k = 0; k < 1000; k++) {
  53. if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3)
  54. return 1;
  55. mdelay(1);
  56. }
  57. return 0;
  58. }
  59. static int __maybe_unused apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
  60. {
  61. void __iomem *p = apmu_cpus[cpu].iomem;
  62. return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
  63. }
  64. static void apmu_init_cpu(struct resource *res, int cpu, int bit)
  65. {
  66. if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
  67. return;
  68. apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
  69. apmu_cpus[cpu].bit = bit;
  70. pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
  71. }
  72. static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit),
  73. struct rcar_apmu_config *apmu_config, int num)
  74. {
  75. int id;
  76. int k;
  77. int bit, index;
  78. bool is_allowed;
  79. for (k = 0; k < num; k++) {
  80. /* only enable the cluster that includes the boot CPU */
  81. is_allowed = false;
  82. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  83. id = apmu_config[k].cpus[bit];
  84. if (id >= 0) {
  85. if (id == cpu_logical_map(0))
  86. is_allowed = true;
  87. }
  88. }
  89. if (!is_allowed)
  90. continue;
  91. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  92. id = apmu_config[k].cpus[bit];
  93. if (id >= 0) {
  94. index = get_logical_index(id);
  95. if (index >= 0)
  96. fn(&apmu_config[k].iomem, index, bit);
  97. }
  98. }
  99. }
  100. }
  101. void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus,
  102. struct rcar_apmu_config *apmu_config,
  103. int num)
  104. {
  105. /* install boot code shared by all CPUs */
  106. shmobile_boot_fn = virt_to_phys(shmobile_smp_boot);
  107. shmobile_boot_arg = MPIDR_HWID_BITMASK;
  108. /* perform per-cpu setup */
  109. apmu_parse_cfg(apmu_init_cpu, apmu_config, num);
  110. }
  111. #ifdef CONFIG_SMP
  112. int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
  113. {
  114. /* For this particular CPU register boot vector */
  115. shmobile_smp_hook(cpu, virt_to_phys(secondary_startup), 0);
  116. return apmu_wrap(cpu, apmu_power_on);
  117. }
  118. #endif
  119. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
  120. /* nicked from arch/arm/mach-exynos/hotplug.c */
  121. static inline void cpu_enter_lowpower_a15(void)
  122. {
  123. unsigned int v;
  124. asm volatile(
  125. " mrc p15, 0, %0, c1, c0, 0\n"
  126. " bic %0, %0, %1\n"
  127. " mcr p15, 0, %0, c1, c0, 0\n"
  128. : "=&r" (v)
  129. : "Ir" (CR_C)
  130. : "cc");
  131. flush_cache_louis();
  132. asm volatile(
  133. /*
  134. * Turn off coherency
  135. */
  136. " mrc p15, 0, %0, c1, c0, 1\n"
  137. " bic %0, %0, %1\n"
  138. " mcr p15, 0, %0, c1, c0, 1\n"
  139. : "=&r" (v)
  140. : "Ir" (0x40)
  141. : "cc");
  142. isb();
  143. dsb();
  144. }
  145. static void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
  146. {
  147. /* Select next sleep mode using the APMU */
  148. apmu_wrap(cpu, apmu_power_off);
  149. /* Do ARM specific CPU shutdown */
  150. cpu_enter_lowpower_a15();
  151. }
  152. static inline void cpu_leave_lowpower(void)
  153. {
  154. unsigned int v;
  155. asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
  156. " orr %0, %0, %1\n"
  157. " mcr p15, 0, %0, c1, c0, 0\n"
  158. " mrc p15, 0, %0, c1, c0, 1\n"
  159. " orr %0, %0, %2\n"
  160. " mcr p15, 0, %0, c1, c0, 1\n"
  161. : "=&r" (v)
  162. : "Ir" (CR_C), "Ir" (0x40)
  163. : "cc");
  164. }
  165. #endif
  166. #if defined(CONFIG_HOTPLUG_CPU)
  167. void shmobile_smp_apmu_cpu_die(unsigned int cpu)
  168. {
  169. /* For this particular CPU deregister boot vector */
  170. shmobile_smp_hook(cpu, 0, 0);
  171. /* Shutdown CPU core */
  172. shmobile_smp_apmu_cpu_shutdown(cpu);
  173. /* jump to shared mach-shmobile sleep / reset code */
  174. shmobile_smp_sleep();
  175. }
  176. int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
  177. {
  178. return apmu_wrap(cpu, apmu_power_off_poll);
  179. }
  180. #endif
  181. #if defined(CONFIG_SUSPEND)
  182. static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
  183. {
  184. shmobile_smp_hook(cpu, virt_to_phys(cpu_resume), 0);
  185. shmobile_smp_apmu_cpu_shutdown(cpu);
  186. cpu_do_idle(); /* WFI selects Core Standby */
  187. return 1;
  188. }
  189. static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
  190. {
  191. cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
  192. cpu_leave_lowpower();
  193. return 0;
  194. }
  195. void __init shmobile_smp_apmu_suspend_init(void)
  196. {
  197. shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
  198. }
  199. #endif