platsmp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2013 MundoReader S.L.
  4. * Author: Heiko Stuebner <[email protected]>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/init.h>
  8. #include <linux/smp.h>
  9. #include <linux/io.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/regmap.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/reset.h>
  15. #include <linux/cpu.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/cp15.h>
  18. #include <asm/smp_scu.h>
  19. #include <asm/smp_plat.h>
  20. #include <asm/mach/map.h>
  21. #include "core.h"
  22. static void __iomem *scu_base_addr;
  23. static void __iomem *sram_base_addr;
  24. static int ncores;
  25. #define PMU_PWRDN_CON 0x08
  26. #define PMU_PWRDN_ST 0x0c
  27. #define PMU_PWRDN_SCU 4
  28. static struct regmap *pmu;
  29. static int has_pmu = true;
  30. static int pmu_power_domain_is_on(int pd)
  31. {
  32. u32 val;
  33. int ret;
  34. ret = regmap_read(pmu, PMU_PWRDN_ST, &val);
  35. if (ret < 0)
  36. return ret;
  37. return !(val & BIT(pd));
  38. }
  39. static struct reset_control *rockchip_get_core_reset(int cpu)
  40. {
  41. struct device *dev = get_cpu_device(cpu);
  42. struct device_node *np;
  43. /* The cpu device is only available after the initial core bringup */
  44. if (dev)
  45. np = dev->of_node;
  46. else
  47. np = of_get_cpu_node(cpu, NULL);
  48. return of_reset_control_get_exclusive(np, NULL);
  49. }
  50. static int pmu_set_power_domain(int pd, bool on)
  51. {
  52. u32 val = (on) ? 0 : BIT(pd);
  53. struct reset_control *rstc = rockchip_get_core_reset(pd);
  54. int ret;
  55. if (IS_ERR(rstc) && read_cpuid_part() != ARM_CPU_PART_CORTEX_A9) {
  56. pr_err("%s: could not get reset control for core %d\n",
  57. __func__, pd);
  58. return PTR_ERR(rstc);
  59. }
  60. /*
  61. * We need to soft reset the cpu when we turn off the cpu power domain,
  62. * or else the active processors might be stalled when the individual
  63. * processor is powered down.
  64. */
  65. if (!IS_ERR(rstc) && !on)
  66. reset_control_assert(rstc);
  67. if (has_pmu) {
  68. ret = regmap_update_bits(pmu, PMU_PWRDN_CON, BIT(pd), val);
  69. if (ret < 0) {
  70. pr_err("%s: could not update power domain\n",
  71. __func__);
  72. return ret;
  73. }
  74. ret = -1;
  75. while (ret != on) {
  76. ret = pmu_power_domain_is_on(pd);
  77. if (ret < 0) {
  78. pr_err("%s: could not read power domain state\n",
  79. __func__);
  80. return ret;
  81. }
  82. }
  83. }
  84. if (!IS_ERR(rstc)) {
  85. if (on)
  86. reset_control_deassert(rstc);
  87. reset_control_put(rstc);
  88. }
  89. return 0;
  90. }
  91. /*
  92. * Handling of CPU cores
  93. */
  94. static int rockchip_boot_secondary(unsigned int cpu, struct task_struct *idle)
  95. {
  96. int ret;
  97. if (!sram_base_addr || (has_pmu && !pmu)) {
  98. pr_err("%s: sram or pmu missing for cpu boot\n", __func__);
  99. return -ENXIO;
  100. }
  101. if (cpu >= ncores) {
  102. pr_err("%s: cpu %d outside maximum number of cpus %d\n",
  103. __func__, cpu, ncores);
  104. return -ENXIO;
  105. }
  106. /* start the core */
  107. ret = pmu_set_power_domain(0 + cpu, true);
  108. if (ret < 0)
  109. return ret;
  110. if (read_cpuid_part() != ARM_CPU_PART_CORTEX_A9) {
  111. /*
  112. * We communicate with the bootrom to active the cpus other
  113. * than cpu0, after a blob of initialize code, they will
  114. * stay at wfe state, once they are activated, they will check
  115. * the mailbox:
  116. * sram_base_addr + 4: 0xdeadbeaf
  117. * sram_base_addr + 8: start address for pc
  118. * The cpu0 need to wait the other cpus other than cpu0 entering
  119. * the wfe state.The wait time is affected by many aspects.
  120. * (e.g: cpu frequency, bootrom frequency, sram frequency, ...)
  121. */
  122. mdelay(1); /* ensure the cpus other than cpu0 to startup */
  123. writel(__pa_symbol(secondary_startup), sram_base_addr + 8);
  124. writel(0xDEADBEAF, sram_base_addr + 4);
  125. dsb_sev();
  126. }
  127. return 0;
  128. }
  129. /**
  130. * rockchip_smp_prepare_sram - populate necessary sram block
  131. * Starting cores execute the code residing at the start of the on-chip sram
  132. * after power-on. Therefore make sure, this sram region is reserved and
  133. * big enough. After this check, copy the trampoline code that directs the
  134. * core to the real startup code in ram into the sram-region.
  135. * @node: mmio-sram device node
  136. */
  137. static int __init rockchip_smp_prepare_sram(struct device_node *node)
  138. {
  139. unsigned int trampoline_sz = &rockchip_secondary_trampoline_end -
  140. &rockchip_secondary_trampoline;
  141. struct resource res;
  142. unsigned int rsize;
  143. int ret;
  144. ret = of_address_to_resource(node, 0, &res);
  145. if (ret < 0) {
  146. pr_err("%s: could not get address for node %pOF\n",
  147. __func__, node);
  148. return ret;
  149. }
  150. rsize = resource_size(&res);
  151. if (rsize < trampoline_sz) {
  152. pr_err("%s: reserved block with size 0x%x is too small for trampoline size 0x%x\n",
  153. __func__, rsize, trampoline_sz);
  154. return -EINVAL;
  155. }
  156. /* set the boot function for the sram code */
  157. rockchip_boot_fn = __pa_symbol(secondary_startup);
  158. /* copy the trampoline to sram, that runs during startup of the core */
  159. memcpy_toio(sram_base_addr, &rockchip_secondary_trampoline, trampoline_sz);
  160. flush_cache_all();
  161. outer_clean_range(0, trampoline_sz);
  162. dsb_sev();
  163. return 0;
  164. }
  165. static const struct regmap_config rockchip_pmu_regmap_config = {
  166. .name = "rockchip-pmu",
  167. .reg_bits = 32,
  168. .val_bits = 32,
  169. .reg_stride = 4,
  170. };
  171. static int __init rockchip_smp_prepare_pmu(void)
  172. {
  173. struct device_node *node;
  174. void __iomem *pmu_base;
  175. /*
  176. * This function is only called via smp_ops->smp_prepare_cpu().
  177. * That only happens if a "/cpus" device tree node exists
  178. * and has an "enable-method" property that selects the SMP
  179. * operations defined herein.
  180. */
  181. node = of_find_node_by_path("/cpus");
  182. pmu = syscon_regmap_lookup_by_phandle(node, "rockchip,pmu");
  183. of_node_put(node);
  184. if (!IS_ERR(pmu))
  185. return 0;
  186. pmu = syscon_regmap_lookup_by_compatible("rockchip,rk3066-pmu");
  187. if (!IS_ERR(pmu))
  188. return 0;
  189. /* fallback, create our own regmap for the pmu area */
  190. pmu = NULL;
  191. node = of_find_compatible_node(NULL, NULL, "rockchip,rk3066-pmu");
  192. if (!node) {
  193. pr_err("%s: could not find pmu dt node\n", __func__);
  194. return -ENODEV;
  195. }
  196. pmu_base = of_iomap(node, 0);
  197. of_node_put(node);
  198. if (!pmu_base) {
  199. pr_err("%s: could not map pmu registers\n", __func__);
  200. return -ENOMEM;
  201. }
  202. pmu = regmap_init_mmio(NULL, pmu_base, &rockchip_pmu_regmap_config);
  203. if (IS_ERR(pmu)) {
  204. int ret = PTR_ERR(pmu);
  205. iounmap(pmu_base);
  206. pmu = NULL;
  207. pr_err("%s: regmap init failed\n", __func__);
  208. return ret;
  209. }
  210. return 0;
  211. }
  212. static void __init rockchip_smp_prepare_cpus(unsigned int max_cpus)
  213. {
  214. struct device_node *node;
  215. unsigned int i;
  216. node = of_find_compatible_node(NULL, NULL, "rockchip,rk3066-smp-sram");
  217. if (!node) {
  218. pr_err("%s: could not find sram dt node\n", __func__);
  219. return;
  220. }
  221. sram_base_addr = of_iomap(node, 0);
  222. if (!sram_base_addr) {
  223. pr_err("%s: could not map sram registers\n", __func__);
  224. of_node_put(node);
  225. return;
  226. }
  227. if (has_pmu && rockchip_smp_prepare_pmu()) {
  228. of_node_put(node);
  229. return;
  230. }
  231. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
  232. if (rockchip_smp_prepare_sram(node)) {
  233. of_node_put(node);
  234. return;
  235. }
  236. /* enable the SCU power domain */
  237. pmu_set_power_domain(PMU_PWRDN_SCU, true);
  238. of_node_put(node);
  239. node = of_find_compatible_node(NULL, NULL, "arm,cortex-a9-scu");
  240. if (!node) {
  241. pr_err("%s: missing scu\n", __func__);
  242. return;
  243. }
  244. scu_base_addr = of_iomap(node, 0);
  245. if (!scu_base_addr) {
  246. pr_err("%s: could not map scu registers\n", __func__);
  247. of_node_put(node);
  248. return;
  249. }
  250. /*
  251. * While the number of cpus is gathered from dt, also get the
  252. * number of cores from the scu to verify this value when
  253. * booting the cores.
  254. */
  255. ncores = scu_get_core_count(scu_base_addr);
  256. pr_err("%s: ncores %d\n", __func__, ncores);
  257. scu_enable(scu_base_addr);
  258. } else {
  259. unsigned int l2ctlr;
  260. asm ("mrc p15, 1, %0, c9, c0, 2\n" : "=r" (l2ctlr));
  261. ncores = ((l2ctlr >> 24) & 0x3) + 1;
  262. }
  263. of_node_put(node);
  264. /* Make sure that all cores except the first are really off */
  265. for (i = 1; i < ncores; i++)
  266. pmu_set_power_domain(0 + i, false);
  267. }
  268. static void __init rk3036_smp_prepare_cpus(unsigned int max_cpus)
  269. {
  270. has_pmu = false;
  271. rockchip_smp_prepare_cpus(max_cpus);
  272. }
  273. #ifdef CONFIG_HOTPLUG_CPU
  274. static int rockchip_cpu_kill(unsigned int cpu)
  275. {
  276. /*
  277. * We need a delay here to ensure that the dying CPU can finish
  278. * executing v7_coherency_exit() and reach the WFI/WFE state
  279. * prior to having the power domain disabled.
  280. */
  281. mdelay(1);
  282. pmu_set_power_domain(0 + cpu, false);
  283. return 1;
  284. }
  285. static void rockchip_cpu_die(unsigned int cpu)
  286. {
  287. v7_exit_coherency_flush(louis);
  288. while (1)
  289. cpu_do_idle();
  290. }
  291. #endif
  292. static const struct smp_operations rk3036_smp_ops __initconst = {
  293. .smp_prepare_cpus = rk3036_smp_prepare_cpus,
  294. .smp_boot_secondary = rockchip_boot_secondary,
  295. #ifdef CONFIG_HOTPLUG_CPU
  296. .cpu_kill = rockchip_cpu_kill,
  297. .cpu_die = rockchip_cpu_die,
  298. #endif
  299. };
  300. static const struct smp_operations rockchip_smp_ops __initconst = {
  301. .smp_prepare_cpus = rockchip_smp_prepare_cpus,
  302. .smp_boot_secondary = rockchip_boot_secondary,
  303. #ifdef CONFIG_HOTPLUG_CPU
  304. .cpu_kill = rockchip_cpu_kill,
  305. .cpu_die = rockchip_cpu_die,
  306. #endif
  307. };
  308. CPU_METHOD_OF_DECLARE(rk3036_smp, "rockchip,rk3036-smp", &rk3036_smp_ops);
  309. CPU_METHOD_OF_DECLARE(rk3066_smp, "rockchip,rk3066-smp", &rockchip_smp_ops);