platsmp.c 9.1 KB

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