platsmp-apmu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * SMP support for SoCs with APMU
  3. *
  4. * Copyright (C) 2013 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/cpu_pm.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/of_address.h>
  16. #include <linux/smp.h>
  17. #include <linux/suspend.h>
  18. #include <linux/threads.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/cp15.h>
  21. #include <asm/proc-fns.h>
  22. #include <asm/smp_plat.h>
  23. #include <asm/suspend.h>
  24. #include "common.h"
  25. static struct {
  26. void __iomem *iomem;
  27. int bit;
  28. } apmu_cpus[NR_CPUS];
  29. #define WUPCR_OFFS 0x10
  30. #define PSTR_OFFS 0x40
  31. #define CPUNCR_OFFS(n) (0x100 + (0x10 * (n)))
  32. static int __maybe_unused apmu_power_on(void __iomem *p, int bit)
  33. {
  34. /* request power on */
  35. writel_relaxed(BIT(bit), p + WUPCR_OFFS);
  36. /* wait for APMU to finish */
  37. while (readl_relaxed(p + WUPCR_OFFS) != 0)
  38. ;
  39. return 0;
  40. }
  41. static int apmu_power_off(void __iomem *p, int bit)
  42. {
  43. /* request Core Standby for next WFI */
  44. writel_relaxed(3, p + CPUNCR_OFFS(bit));
  45. return 0;
  46. }
  47. static int __maybe_unused apmu_power_off_poll(void __iomem *p, int bit)
  48. {
  49. int k;
  50. for (k = 0; k < 1000; k++) {
  51. if (((readl_relaxed(p + PSTR_OFFS) >> (bit * 4)) & 0x03) == 3)
  52. return 1;
  53. mdelay(1);
  54. }
  55. return 0;
  56. }
  57. static int apmu_wrap(int cpu, int (*fn)(void __iomem *p, int cpu))
  58. {
  59. void __iomem *p = apmu_cpus[cpu].iomem;
  60. return p ? fn(p, apmu_cpus[cpu].bit) : -EINVAL;
  61. }
  62. static void apmu_init_cpu(struct resource *res, int cpu, int bit)
  63. {
  64. if ((cpu >= ARRAY_SIZE(apmu_cpus)) || apmu_cpus[cpu].iomem)
  65. return;
  66. apmu_cpus[cpu].iomem = ioremap_nocache(res->start, resource_size(res));
  67. apmu_cpus[cpu].bit = bit;
  68. pr_debug("apmu ioremap %d %d %pr\n", cpu, bit, res);
  69. }
  70. static struct {
  71. struct resource iomem;
  72. int cpus[4];
  73. } apmu_config[] = {
  74. {
  75. .iomem = DEFINE_RES_MEM(0xe6152000, 0x88),
  76. .cpus = { 0, 1, 2, 3 },
  77. },
  78. {
  79. .iomem = DEFINE_RES_MEM(0xe6151000, 0x88),
  80. .cpus = { 0x100, 0x101, 0x102, 0x103 },
  81. }
  82. };
  83. static void apmu_parse_cfg(void (*fn)(struct resource *res, int cpu, int bit))
  84. {
  85. u32 id;
  86. int k;
  87. int bit, index;
  88. bool is_allowed;
  89. for (k = 0; k < ARRAY_SIZE(apmu_config); k++) {
  90. /* only enable the cluster that includes the boot CPU */
  91. is_allowed = false;
  92. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  93. id = apmu_config[k].cpus[bit];
  94. if (id >= 0) {
  95. if (id == cpu_logical_map(0))
  96. is_allowed = true;
  97. }
  98. }
  99. if (!is_allowed)
  100. continue;
  101. for (bit = 0; bit < ARRAY_SIZE(apmu_config[k].cpus); bit++) {
  102. id = apmu_config[k].cpus[bit];
  103. if (id >= 0) {
  104. index = get_logical_index(id);
  105. if (index >= 0)
  106. fn(&apmu_config[k].iomem, index, bit);
  107. }
  108. }
  109. }
  110. }
  111. void __init shmobile_smp_apmu_prepare_cpus(unsigned int max_cpus)
  112. {
  113. /* install boot code shared by all CPUs */
  114. shmobile_boot_fn = virt_to_phys(shmobile_smp_boot);
  115. shmobile_boot_arg = MPIDR_HWID_BITMASK;
  116. /* perform per-cpu setup */
  117. apmu_parse_cfg(apmu_init_cpu);
  118. }
  119. #ifdef CONFIG_SMP
  120. int shmobile_smp_apmu_boot_secondary(unsigned int cpu, struct task_struct *idle)
  121. {
  122. /* For this particular CPU register boot vector */
  123. shmobile_smp_hook(cpu, virt_to_phys(shmobile_invalidate_start), 0);
  124. return apmu_wrap(cpu, apmu_power_on);
  125. }
  126. #endif
  127. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_SUSPEND)
  128. /* nicked from arch/arm/mach-exynos/hotplug.c */
  129. static inline void cpu_enter_lowpower_a15(void)
  130. {
  131. unsigned int v;
  132. asm volatile(
  133. " mrc p15, 0, %0, c1, c0, 0\n"
  134. " bic %0, %0, %1\n"
  135. " mcr p15, 0, %0, c1, c0, 0\n"
  136. : "=&r" (v)
  137. : "Ir" (CR_C)
  138. : "cc");
  139. flush_cache_louis();
  140. asm volatile(
  141. /*
  142. * Turn off coherency
  143. */
  144. " mrc p15, 0, %0, c1, c0, 1\n"
  145. " bic %0, %0, %1\n"
  146. " mcr p15, 0, %0, c1, c0, 1\n"
  147. : "=&r" (v)
  148. : "Ir" (0x40)
  149. : "cc");
  150. isb();
  151. dsb();
  152. }
  153. void shmobile_smp_apmu_cpu_shutdown(unsigned int cpu)
  154. {
  155. /* Select next sleep mode using the APMU */
  156. apmu_wrap(cpu, apmu_power_off);
  157. /* Do ARM specific CPU shutdown */
  158. cpu_enter_lowpower_a15();
  159. }
  160. static inline void cpu_leave_lowpower(void)
  161. {
  162. unsigned int v;
  163. asm volatile("mrc p15, 0, %0, c1, c0, 0\n"
  164. " orr %0, %0, %1\n"
  165. " mcr p15, 0, %0, c1, c0, 0\n"
  166. " mrc p15, 0, %0, c1, c0, 1\n"
  167. " orr %0, %0, %2\n"
  168. " mcr p15, 0, %0, c1, c0, 1\n"
  169. : "=&r" (v)
  170. : "Ir" (CR_C), "Ir" (0x40)
  171. : "cc");
  172. }
  173. #endif
  174. #if defined(CONFIG_HOTPLUG_CPU)
  175. void shmobile_smp_apmu_cpu_die(unsigned int cpu)
  176. {
  177. /* For this particular CPU deregister boot vector */
  178. shmobile_smp_hook(cpu, 0, 0);
  179. /* Shutdown CPU core */
  180. shmobile_smp_apmu_cpu_shutdown(cpu);
  181. /* jump to shared mach-shmobile sleep / reset code */
  182. shmobile_smp_sleep();
  183. }
  184. int shmobile_smp_apmu_cpu_kill(unsigned int cpu)
  185. {
  186. return apmu_wrap(cpu, apmu_power_off_poll);
  187. }
  188. #endif
  189. #if defined(CONFIG_SUSPEND)
  190. static int shmobile_smp_apmu_do_suspend(unsigned long cpu)
  191. {
  192. shmobile_smp_hook(cpu, virt_to_phys(cpu_resume), 0);
  193. shmobile_smp_apmu_cpu_shutdown(cpu);
  194. cpu_do_idle(); /* WFI selects Core Standby */
  195. return 1;
  196. }
  197. static int shmobile_smp_apmu_enter_suspend(suspend_state_t state)
  198. {
  199. cpu_suspend(smp_processor_id(), shmobile_smp_apmu_do_suspend);
  200. cpu_leave_lowpower();
  201. return 0;
  202. }
  203. void __init shmobile_smp_apmu_suspend_init(void)
  204. {
  205. shmobile_suspend_ops.enter = shmobile_smp_apmu_enter_suspend;
  206. }
  207. #endif