mc_smp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 Chen-Yu Tsai
  4. *
  5. * Chen-Yu Tsai <[email protected]>
  6. *
  7. * arch/arm/mach-sunxi/mc_smp.c
  8. *
  9. * Based on Allwinner code, arch/arm/mach-exynos/mcpm-exynos.c, and
  10. * arch/arm/mach-hisi/platmcpm.c
  11. * Cluster cache enable trampoline code adapted from MCPM framework
  12. */
  13. #include <linux/arm-cci.h>
  14. #include <linux/cpu_pm.h>
  15. #include <linux/delay.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/irqchip/arm-gic.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/smp.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/cp15.h>
  25. #include <asm/cputype.h>
  26. #include <asm/idmap.h>
  27. #include <asm/smp_plat.h>
  28. #include <asm/suspend.h>
  29. #define SUNXI_CPUS_PER_CLUSTER 4
  30. #define SUNXI_NR_CLUSTERS 2
  31. #define POLL_USEC 100
  32. #define TIMEOUT_USEC 100000
  33. #define CPUCFG_CX_CTRL_REG0(c) (0x10 * (c))
  34. #define CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE(n) BIT(n)
  35. #define CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE_ALL 0xf
  36. #define CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A7 BIT(4)
  37. #define CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A15 BIT(0)
  38. #define CPUCFG_CX_CTRL_REG1(c) (0x10 * (c) + 0x4)
  39. #define CPUCFG_CX_CTRL_REG1_ACINACTM BIT(0)
  40. #define CPUCFG_CX_STATUS(c) (0x30 + 0x4 * (c))
  41. #define CPUCFG_CX_STATUS_STANDBYWFI(n) BIT(16 + (n))
  42. #define CPUCFG_CX_STATUS_STANDBYWFIL2 BIT(0)
  43. #define CPUCFG_CX_RST_CTRL(c) (0x80 + 0x4 * (c))
  44. #define CPUCFG_CX_RST_CTRL_DBG_SOC_RST BIT(24)
  45. #define CPUCFG_CX_RST_CTRL_ETM_RST(n) BIT(20 + (n))
  46. #define CPUCFG_CX_RST_CTRL_ETM_RST_ALL (0xf << 20)
  47. #define CPUCFG_CX_RST_CTRL_DBG_RST(n) BIT(16 + (n))
  48. #define CPUCFG_CX_RST_CTRL_DBG_RST_ALL (0xf << 16)
  49. #define CPUCFG_CX_RST_CTRL_H_RST BIT(12)
  50. #define CPUCFG_CX_RST_CTRL_L2_RST BIT(8)
  51. #define CPUCFG_CX_RST_CTRL_CX_RST(n) BIT(4 + (n))
  52. #define CPUCFG_CX_RST_CTRL_CORE_RST(n) BIT(n)
  53. #define CPUCFG_CX_RST_CTRL_CORE_RST_ALL (0xf << 0)
  54. #define PRCM_CPU_PO_RST_CTRL(c) (0x4 + 0x4 * (c))
  55. #define PRCM_CPU_PO_RST_CTRL_CORE(n) BIT(n)
  56. #define PRCM_CPU_PO_RST_CTRL_CORE_ALL 0xf
  57. #define PRCM_PWROFF_GATING_REG(c) (0x100 + 0x4 * (c))
  58. /* The power off register for clusters are different from a80 and a83t */
  59. #define PRCM_PWROFF_GATING_REG_CLUSTER_SUN8I BIT(0)
  60. #define PRCM_PWROFF_GATING_REG_CLUSTER_SUN9I BIT(4)
  61. #define PRCM_PWROFF_GATING_REG_CORE(n) BIT(n)
  62. #define PRCM_PWR_SWITCH_REG(c, cpu) (0x140 + 0x10 * (c) + 0x4 * (cpu))
  63. #define PRCM_CPU_SOFT_ENTRY_REG 0x164
  64. /* R_CPUCFG registers, specific to sun8i-a83t */
  65. #define R_CPUCFG_CLUSTER_PO_RST_CTRL(c) (0x30 + (c) * 0x4)
  66. #define R_CPUCFG_CLUSTER_PO_RST_CTRL_CORE(n) BIT(n)
  67. #define R_CPUCFG_CPU_SOFT_ENTRY_REG 0x01a4
  68. #define CPU0_SUPPORT_HOTPLUG_MAGIC0 0xFA50392F
  69. #define CPU0_SUPPORT_HOTPLUG_MAGIC1 0x790DCA3A
  70. static void __iomem *cpucfg_base;
  71. static void __iomem *prcm_base;
  72. static void __iomem *sram_b_smp_base;
  73. static void __iomem *r_cpucfg_base;
  74. extern void sunxi_mc_smp_secondary_startup(void);
  75. extern void sunxi_mc_smp_resume(void);
  76. static bool is_a83t;
  77. static bool sunxi_core_is_cortex_a15(unsigned int core, unsigned int cluster)
  78. {
  79. struct device_node *node;
  80. int cpu = cluster * SUNXI_CPUS_PER_CLUSTER + core;
  81. bool is_compatible;
  82. node = of_cpu_device_node_get(cpu);
  83. /* In case of_cpu_device_node_get fails */
  84. if (!node)
  85. node = of_get_cpu_node(cpu, NULL);
  86. if (!node) {
  87. /*
  88. * There's no point in returning an error, since we
  89. * would be mid way in a core or cluster power sequence.
  90. */
  91. pr_err("%s: Couldn't get CPU cluster %u core %u device node\n",
  92. __func__, cluster, core);
  93. return false;
  94. }
  95. is_compatible = of_device_is_compatible(node, "arm,cortex-a15");
  96. of_node_put(node);
  97. return is_compatible;
  98. }
  99. static int sunxi_cpu_power_switch_set(unsigned int cpu, unsigned int cluster,
  100. bool enable)
  101. {
  102. u32 reg;
  103. /* control sequence from Allwinner A80 user manual v1.2 PRCM section */
  104. reg = readl(prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  105. if (enable) {
  106. if (reg == 0x00) {
  107. pr_debug("power clamp for cluster %u cpu %u already open\n",
  108. cluster, cpu);
  109. return 0;
  110. }
  111. writel(0xff, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  112. udelay(10);
  113. writel(0xfe, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  114. udelay(10);
  115. writel(0xf8, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  116. udelay(10);
  117. writel(0xf0, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  118. udelay(10);
  119. writel(0x00, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  120. udelay(10);
  121. } else {
  122. writel(0xff, prcm_base + PRCM_PWR_SWITCH_REG(cluster, cpu));
  123. udelay(10);
  124. }
  125. return 0;
  126. }
  127. static void sunxi_cpu0_hotplug_support_set(bool enable)
  128. {
  129. if (enable) {
  130. writel(CPU0_SUPPORT_HOTPLUG_MAGIC0, sram_b_smp_base);
  131. writel(CPU0_SUPPORT_HOTPLUG_MAGIC1, sram_b_smp_base + 0x4);
  132. } else {
  133. writel(0x0, sram_b_smp_base);
  134. writel(0x0, sram_b_smp_base + 0x4);
  135. }
  136. }
  137. static int sunxi_cpu_powerup(unsigned int cpu, unsigned int cluster)
  138. {
  139. u32 reg;
  140. pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
  141. if (cpu >= SUNXI_CPUS_PER_CLUSTER || cluster >= SUNXI_NR_CLUSTERS)
  142. return -EINVAL;
  143. /* Set hotplug support magic flags for cpu0 */
  144. if (cluster == 0 && cpu == 0)
  145. sunxi_cpu0_hotplug_support_set(true);
  146. /* assert processor power-on reset */
  147. reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
  148. reg &= ~PRCM_CPU_PO_RST_CTRL_CORE(cpu);
  149. writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
  150. if (is_a83t) {
  151. /* assert cpu power-on reset */
  152. reg = readl(r_cpucfg_base +
  153. R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
  154. reg &= ~(R_CPUCFG_CLUSTER_PO_RST_CTRL_CORE(cpu));
  155. writel(reg, r_cpucfg_base +
  156. R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
  157. udelay(10);
  158. }
  159. /* Cortex-A7: hold L1 reset disable signal low */
  160. if (!sunxi_core_is_cortex_a15(cpu, cluster)) {
  161. reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
  162. reg &= ~CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE(cpu);
  163. writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
  164. }
  165. /* assert processor related resets */
  166. reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  167. reg &= ~CPUCFG_CX_RST_CTRL_DBG_RST(cpu);
  168. /*
  169. * Allwinner code also asserts resets for NEON on A15. According
  170. * to ARM manuals, asserting power-on reset is sufficient.
  171. */
  172. if (!sunxi_core_is_cortex_a15(cpu, cluster))
  173. reg &= ~CPUCFG_CX_RST_CTRL_ETM_RST(cpu);
  174. writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  175. /* open power switch */
  176. sunxi_cpu_power_switch_set(cpu, cluster, true);
  177. /* Handle A83T bit swap */
  178. if (is_a83t) {
  179. if (cpu == 0)
  180. cpu = 4;
  181. }
  182. /* clear processor power gate */
  183. reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  184. reg &= ~PRCM_PWROFF_GATING_REG_CORE(cpu);
  185. writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  186. udelay(20);
  187. /* Handle A83T bit swap */
  188. if (is_a83t) {
  189. if (cpu == 4)
  190. cpu = 0;
  191. }
  192. /* de-assert processor power-on reset */
  193. reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
  194. reg |= PRCM_CPU_PO_RST_CTRL_CORE(cpu);
  195. writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
  196. if (is_a83t) {
  197. reg = readl(r_cpucfg_base +
  198. R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
  199. reg |= R_CPUCFG_CLUSTER_PO_RST_CTRL_CORE(cpu);
  200. writel(reg, r_cpucfg_base +
  201. R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
  202. udelay(10);
  203. }
  204. /* de-assert all processor resets */
  205. reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  206. reg |= CPUCFG_CX_RST_CTRL_DBG_RST(cpu);
  207. reg |= CPUCFG_CX_RST_CTRL_CORE_RST(cpu);
  208. if (!sunxi_core_is_cortex_a15(cpu, cluster))
  209. reg |= CPUCFG_CX_RST_CTRL_ETM_RST(cpu);
  210. else
  211. reg |= CPUCFG_CX_RST_CTRL_CX_RST(cpu); /* NEON */
  212. writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  213. return 0;
  214. }
  215. static int sunxi_cluster_powerup(unsigned int cluster)
  216. {
  217. u32 reg;
  218. pr_debug("%s: cluster %u\n", __func__, cluster);
  219. if (cluster >= SUNXI_NR_CLUSTERS)
  220. return -EINVAL;
  221. /* For A83T, assert cluster cores resets */
  222. if (is_a83t) {
  223. reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  224. reg &= ~CPUCFG_CX_RST_CTRL_CORE_RST_ALL; /* Core Reset */
  225. writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  226. udelay(10);
  227. }
  228. /* assert ACINACTM */
  229. reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
  230. reg |= CPUCFG_CX_CTRL_REG1_ACINACTM;
  231. writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
  232. /* assert cluster processor power-on resets */
  233. reg = readl(prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
  234. reg &= ~PRCM_CPU_PO_RST_CTRL_CORE_ALL;
  235. writel(reg, prcm_base + PRCM_CPU_PO_RST_CTRL(cluster));
  236. /* assert cluster cores resets */
  237. if (is_a83t) {
  238. reg = readl(r_cpucfg_base +
  239. R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
  240. reg &= ~CPUCFG_CX_RST_CTRL_CORE_RST_ALL;
  241. writel(reg, r_cpucfg_base +
  242. R_CPUCFG_CLUSTER_PO_RST_CTRL(cluster));
  243. udelay(10);
  244. }
  245. /* assert cluster resets */
  246. reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  247. reg &= ~CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
  248. reg &= ~CPUCFG_CX_RST_CTRL_DBG_RST_ALL;
  249. reg &= ~CPUCFG_CX_RST_CTRL_H_RST;
  250. reg &= ~CPUCFG_CX_RST_CTRL_L2_RST;
  251. /*
  252. * Allwinner code also asserts resets for NEON on A15. According
  253. * to ARM manuals, asserting power-on reset is sufficient.
  254. */
  255. if (!sunxi_core_is_cortex_a15(0, cluster))
  256. reg &= ~CPUCFG_CX_RST_CTRL_ETM_RST_ALL;
  257. writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  258. /* hold L1/L2 reset disable signals low */
  259. reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
  260. if (sunxi_core_is_cortex_a15(0, cluster)) {
  261. /* Cortex-A15: hold L2RSTDISABLE low */
  262. reg &= ~CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A15;
  263. } else {
  264. /* Cortex-A7: hold L1RSTDISABLE and L2RSTDISABLE low */
  265. reg &= ~CPUCFG_CX_CTRL_REG0_L1_RST_DISABLE_ALL;
  266. reg &= ~CPUCFG_CX_CTRL_REG0_L2_RST_DISABLE_A7;
  267. }
  268. writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG0(cluster));
  269. /* clear cluster power gate */
  270. reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  271. if (is_a83t)
  272. reg &= ~PRCM_PWROFF_GATING_REG_CLUSTER_SUN8I;
  273. else
  274. reg &= ~PRCM_PWROFF_GATING_REG_CLUSTER_SUN9I;
  275. writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  276. udelay(20);
  277. /* de-assert cluster resets */
  278. reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  279. reg |= CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
  280. reg |= CPUCFG_CX_RST_CTRL_H_RST;
  281. reg |= CPUCFG_CX_RST_CTRL_L2_RST;
  282. writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  283. /* de-assert ACINACTM */
  284. reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
  285. reg &= ~CPUCFG_CX_CTRL_REG1_ACINACTM;
  286. writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
  287. return 0;
  288. }
  289. /*
  290. * This bit is shared between the initial nocache_trampoline call to
  291. * enable CCI-400 and proper cluster cache disable before power down.
  292. */
  293. static void sunxi_cluster_cache_disable_without_axi(void)
  294. {
  295. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A15) {
  296. /*
  297. * On the Cortex-A15 we need to disable
  298. * L2 prefetching before flushing the cache.
  299. */
  300. asm volatile(
  301. "mcr p15, 1, %0, c15, c0, 3\n"
  302. "isb\n"
  303. "dsb"
  304. : : "r" (0x400));
  305. }
  306. /* Flush all cache levels for this cluster. */
  307. v7_exit_coherency_flush(all);
  308. /*
  309. * Disable cluster-level coherency by masking
  310. * incoming snoops and DVM messages:
  311. */
  312. cci_disable_port_by_cpu(read_cpuid_mpidr());
  313. }
  314. static int sunxi_mc_smp_cpu_table[SUNXI_NR_CLUSTERS][SUNXI_CPUS_PER_CLUSTER];
  315. int sunxi_mc_smp_first_comer;
  316. static DEFINE_SPINLOCK(boot_lock);
  317. static bool sunxi_mc_smp_cluster_is_down(unsigned int cluster)
  318. {
  319. int i;
  320. for (i = 0; i < SUNXI_CPUS_PER_CLUSTER; i++)
  321. if (sunxi_mc_smp_cpu_table[cluster][i])
  322. return false;
  323. return true;
  324. }
  325. static void sunxi_mc_smp_secondary_init(unsigned int cpu)
  326. {
  327. /* Clear hotplug support magic flags for cpu0 */
  328. if (cpu == 0)
  329. sunxi_cpu0_hotplug_support_set(false);
  330. }
  331. static int sunxi_mc_smp_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  332. {
  333. unsigned int mpidr, cpu, cluster;
  334. mpidr = cpu_logical_map(l_cpu);
  335. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  336. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  337. if (!cpucfg_base)
  338. return -ENODEV;
  339. if (cluster >= SUNXI_NR_CLUSTERS || cpu >= SUNXI_CPUS_PER_CLUSTER)
  340. return -EINVAL;
  341. spin_lock_irq(&boot_lock);
  342. if (sunxi_mc_smp_cpu_table[cluster][cpu])
  343. goto out;
  344. if (sunxi_mc_smp_cluster_is_down(cluster)) {
  345. sunxi_mc_smp_first_comer = true;
  346. sunxi_cluster_powerup(cluster);
  347. } else {
  348. sunxi_mc_smp_first_comer = false;
  349. }
  350. /* This is read by incoming CPUs with their cache and MMU disabled */
  351. sync_cache_w(&sunxi_mc_smp_first_comer);
  352. sunxi_cpu_powerup(cpu, cluster);
  353. out:
  354. sunxi_mc_smp_cpu_table[cluster][cpu]++;
  355. spin_unlock_irq(&boot_lock);
  356. return 0;
  357. }
  358. #ifdef CONFIG_HOTPLUG_CPU
  359. static void sunxi_cluster_cache_disable(void)
  360. {
  361. unsigned int cluster = MPIDR_AFFINITY_LEVEL(read_cpuid_mpidr(), 1);
  362. u32 reg;
  363. pr_debug("%s: cluster %u\n", __func__, cluster);
  364. sunxi_cluster_cache_disable_without_axi();
  365. /* last man standing, assert ACINACTM */
  366. reg = readl(cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
  367. reg |= CPUCFG_CX_CTRL_REG1_ACINACTM;
  368. writel(reg, cpucfg_base + CPUCFG_CX_CTRL_REG1(cluster));
  369. }
  370. static void sunxi_mc_smp_cpu_die(unsigned int l_cpu)
  371. {
  372. unsigned int mpidr, cpu, cluster;
  373. bool last_man;
  374. mpidr = cpu_logical_map(l_cpu);
  375. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  376. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  377. pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
  378. spin_lock(&boot_lock);
  379. sunxi_mc_smp_cpu_table[cluster][cpu]--;
  380. if (sunxi_mc_smp_cpu_table[cluster][cpu] == 1) {
  381. /* A power_up request went ahead of us. */
  382. pr_debug("%s: aborting due to a power up request\n",
  383. __func__);
  384. spin_unlock(&boot_lock);
  385. return;
  386. } else if (sunxi_mc_smp_cpu_table[cluster][cpu] > 1) {
  387. pr_err("Cluster %d CPU%d boots multiple times\n",
  388. cluster, cpu);
  389. BUG();
  390. }
  391. last_man = sunxi_mc_smp_cluster_is_down(cluster);
  392. spin_unlock(&boot_lock);
  393. gic_cpu_if_down(0);
  394. if (last_man)
  395. sunxi_cluster_cache_disable();
  396. else
  397. v7_exit_coherency_flush(louis);
  398. for (;;)
  399. wfi();
  400. }
  401. static int sunxi_cpu_powerdown(unsigned int cpu, unsigned int cluster)
  402. {
  403. u32 reg;
  404. int gating_bit = cpu;
  405. pr_debug("%s: cluster %u cpu %u\n", __func__, cluster, cpu);
  406. if (cpu >= SUNXI_CPUS_PER_CLUSTER || cluster >= SUNXI_NR_CLUSTERS)
  407. return -EINVAL;
  408. if (is_a83t && cpu == 0)
  409. gating_bit = 4;
  410. /* gate processor power */
  411. reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  412. reg |= PRCM_PWROFF_GATING_REG_CORE(gating_bit);
  413. writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  414. udelay(20);
  415. /* close power switch */
  416. sunxi_cpu_power_switch_set(cpu, cluster, false);
  417. return 0;
  418. }
  419. static int sunxi_cluster_powerdown(unsigned int cluster)
  420. {
  421. u32 reg;
  422. pr_debug("%s: cluster %u\n", __func__, cluster);
  423. if (cluster >= SUNXI_NR_CLUSTERS)
  424. return -EINVAL;
  425. /* assert cluster resets or system will hang */
  426. pr_debug("%s: assert cluster reset\n", __func__);
  427. reg = readl(cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  428. reg &= ~CPUCFG_CX_RST_CTRL_DBG_SOC_RST;
  429. reg &= ~CPUCFG_CX_RST_CTRL_H_RST;
  430. reg &= ~CPUCFG_CX_RST_CTRL_L2_RST;
  431. writel(reg, cpucfg_base + CPUCFG_CX_RST_CTRL(cluster));
  432. /* gate cluster power */
  433. pr_debug("%s: gate cluster power\n", __func__);
  434. reg = readl(prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  435. if (is_a83t)
  436. reg |= PRCM_PWROFF_GATING_REG_CLUSTER_SUN8I;
  437. else
  438. reg |= PRCM_PWROFF_GATING_REG_CLUSTER_SUN9I;
  439. writel(reg, prcm_base + PRCM_PWROFF_GATING_REG(cluster));
  440. udelay(20);
  441. return 0;
  442. }
  443. static int sunxi_mc_smp_cpu_kill(unsigned int l_cpu)
  444. {
  445. unsigned int mpidr, cpu, cluster;
  446. unsigned int tries, count;
  447. int ret = 0;
  448. u32 reg;
  449. mpidr = cpu_logical_map(l_cpu);
  450. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  451. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  452. /* This should never happen */
  453. if (WARN_ON(cluster >= SUNXI_NR_CLUSTERS ||
  454. cpu >= SUNXI_CPUS_PER_CLUSTER))
  455. return 0;
  456. /* wait for CPU core to die and enter WFI */
  457. count = TIMEOUT_USEC / POLL_USEC;
  458. spin_lock_irq(&boot_lock);
  459. for (tries = 0; tries < count; tries++) {
  460. spin_unlock_irq(&boot_lock);
  461. usleep_range(POLL_USEC / 2, POLL_USEC);
  462. spin_lock_irq(&boot_lock);
  463. /*
  464. * If the user turns off a bunch of cores at the same
  465. * time, the kernel might call cpu_kill before some of
  466. * them are ready. This is because boot_lock serializes
  467. * both cpu_die and cpu_kill callbacks. Either one could
  468. * run first. We should wait for cpu_die to complete.
  469. */
  470. if (sunxi_mc_smp_cpu_table[cluster][cpu])
  471. continue;
  472. reg = readl(cpucfg_base + CPUCFG_CX_STATUS(cluster));
  473. if (reg & CPUCFG_CX_STATUS_STANDBYWFI(cpu))
  474. break;
  475. }
  476. if (tries >= count) {
  477. ret = ETIMEDOUT;
  478. goto out;
  479. }
  480. /* power down CPU core */
  481. sunxi_cpu_powerdown(cpu, cluster);
  482. if (!sunxi_mc_smp_cluster_is_down(cluster))
  483. goto out;
  484. /* wait for cluster L2 WFI */
  485. ret = readl_poll_timeout(cpucfg_base + CPUCFG_CX_STATUS(cluster), reg,
  486. reg & CPUCFG_CX_STATUS_STANDBYWFIL2,
  487. POLL_USEC, TIMEOUT_USEC);
  488. if (ret) {
  489. /*
  490. * Ignore timeout on the cluster. Leaving the cluster on
  491. * will not affect system execution, just use a bit more
  492. * power. But returning an error here will only confuse
  493. * the user as the CPU has already been shutdown.
  494. */
  495. ret = 0;
  496. goto out;
  497. }
  498. /* Power down cluster */
  499. sunxi_cluster_powerdown(cluster);
  500. out:
  501. spin_unlock_irq(&boot_lock);
  502. pr_debug("%s: cluster %u cpu %u powerdown: %d\n",
  503. __func__, cluster, cpu, ret);
  504. return !ret;
  505. }
  506. static bool sunxi_mc_smp_cpu_can_disable(unsigned int cpu)
  507. {
  508. /* CPU0 hotplug not handled for sun8i-a83t */
  509. if (is_a83t)
  510. if (cpu == 0)
  511. return false;
  512. return true;
  513. }
  514. #endif
  515. static const struct smp_operations sunxi_mc_smp_smp_ops __initconst = {
  516. .smp_secondary_init = sunxi_mc_smp_secondary_init,
  517. .smp_boot_secondary = sunxi_mc_smp_boot_secondary,
  518. #ifdef CONFIG_HOTPLUG_CPU
  519. .cpu_die = sunxi_mc_smp_cpu_die,
  520. .cpu_kill = sunxi_mc_smp_cpu_kill,
  521. .cpu_can_disable = sunxi_mc_smp_cpu_can_disable,
  522. #endif
  523. };
  524. static bool __init sunxi_mc_smp_cpu_table_init(void)
  525. {
  526. unsigned int mpidr, cpu, cluster;
  527. mpidr = read_cpuid_mpidr();
  528. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  529. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  530. if (cluster >= SUNXI_NR_CLUSTERS || cpu >= SUNXI_CPUS_PER_CLUSTER) {
  531. pr_err("%s: boot CPU is out of bounds!\n", __func__);
  532. return false;
  533. }
  534. sunxi_mc_smp_cpu_table[cluster][cpu] = 1;
  535. return true;
  536. }
  537. /*
  538. * Adapted from arch/arm/common/mc_smp_entry.c
  539. *
  540. * We need the trampoline code to enable CCI-400 on the first cluster
  541. */
  542. typedef typeof(cpu_reset) phys_reset_t;
  543. static int __init nocache_trampoline(unsigned long __unused)
  544. {
  545. phys_reset_t phys_reset;
  546. setup_mm_for_reboot();
  547. sunxi_cluster_cache_disable_without_axi();
  548. phys_reset = (phys_reset_t)(unsigned long)__pa_symbol(cpu_reset);
  549. phys_reset(__pa_symbol(sunxi_mc_smp_resume), false);
  550. BUG();
  551. }
  552. static int __init sunxi_mc_smp_loopback(void)
  553. {
  554. int ret;
  555. /*
  556. * We're going to soft-restart the current CPU through the
  557. * low-level MCPM code by leveraging the suspend/resume
  558. * infrastructure. Let's play it safe by using cpu_pm_enter()
  559. * in case the CPU init code path resets the VFP or similar.
  560. */
  561. sunxi_mc_smp_first_comer = true;
  562. local_irq_disable();
  563. local_fiq_disable();
  564. ret = cpu_pm_enter();
  565. if (!ret) {
  566. ret = cpu_suspend(0, nocache_trampoline);
  567. cpu_pm_exit();
  568. }
  569. local_fiq_enable();
  570. local_irq_enable();
  571. sunxi_mc_smp_first_comer = false;
  572. return ret;
  573. }
  574. /*
  575. * This holds any device nodes that we requested resources for,
  576. * so that we may easily release resources in the error path.
  577. */
  578. struct sunxi_mc_smp_nodes {
  579. struct device_node *prcm_node;
  580. struct device_node *cpucfg_node;
  581. struct device_node *sram_node;
  582. struct device_node *r_cpucfg_node;
  583. };
  584. /* This structure holds SoC-specific bits tied to an enable-method string. */
  585. struct sunxi_mc_smp_data {
  586. const char *enable_method;
  587. int (*get_smp_nodes)(struct sunxi_mc_smp_nodes *nodes);
  588. bool is_a83t;
  589. };
  590. static void __init sunxi_mc_smp_put_nodes(struct sunxi_mc_smp_nodes *nodes)
  591. {
  592. of_node_put(nodes->prcm_node);
  593. of_node_put(nodes->cpucfg_node);
  594. of_node_put(nodes->sram_node);
  595. of_node_put(nodes->r_cpucfg_node);
  596. memset(nodes, 0, sizeof(*nodes));
  597. }
  598. static int __init sun9i_a80_get_smp_nodes(struct sunxi_mc_smp_nodes *nodes)
  599. {
  600. nodes->prcm_node = of_find_compatible_node(NULL, NULL,
  601. "allwinner,sun9i-a80-prcm");
  602. if (!nodes->prcm_node) {
  603. pr_err("%s: PRCM not available\n", __func__);
  604. return -ENODEV;
  605. }
  606. nodes->cpucfg_node = of_find_compatible_node(NULL, NULL,
  607. "allwinner,sun9i-a80-cpucfg");
  608. if (!nodes->cpucfg_node) {
  609. pr_err("%s: CPUCFG not available\n", __func__);
  610. return -ENODEV;
  611. }
  612. nodes->sram_node = of_find_compatible_node(NULL, NULL,
  613. "allwinner,sun9i-a80-smp-sram");
  614. if (!nodes->sram_node) {
  615. pr_err("%s: Secure SRAM not available\n", __func__);
  616. return -ENODEV;
  617. }
  618. return 0;
  619. }
  620. static int __init sun8i_a83t_get_smp_nodes(struct sunxi_mc_smp_nodes *nodes)
  621. {
  622. nodes->prcm_node = of_find_compatible_node(NULL, NULL,
  623. "allwinner,sun8i-a83t-r-ccu");
  624. if (!nodes->prcm_node) {
  625. pr_err("%s: PRCM not available\n", __func__);
  626. return -ENODEV;
  627. }
  628. nodes->cpucfg_node = of_find_compatible_node(NULL, NULL,
  629. "allwinner,sun8i-a83t-cpucfg");
  630. if (!nodes->cpucfg_node) {
  631. pr_err("%s: CPUCFG not available\n", __func__);
  632. return -ENODEV;
  633. }
  634. nodes->r_cpucfg_node = of_find_compatible_node(NULL, NULL,
  635. "allwinner,sun8i-a83t-r-cpucfg");
  636. if (!nodes->r_cpucfg_node) {
  637. pr_err("%s: RCPUCFG not available\n", __func__);
  638. return -ENODEV;
  639. }
  640. return 0;
  641. }
  642. static const struct sunxi_mc_smp_data sunxi_mc_smp_data[] __initconst = {
  643. {
  644. .enable_method = "allwinner,sun9i-a80-smp",
  645. .get_smp_nodes = sun9i_a80_get_smp_nodes,
  646. },
  647. {
  648. .enable_method = "allwinner,sun8i-a83t-smp",
  649. .get_smp_nodes = sun8i_a83t_get_smp_nodes,
  650. .is_a83t = true,
  651. },
  652. };
  653. static int __init sunxi_mc_smp_init(void)
  654. {
  655. struct sunxi_mc_smp_nodes nodes = { 0 };
  656. struct device_node *node;
  657. struct resource res;
  658. void __iomem *addr;
  659. int i, ret;
  660. /*
  661. * Don't bother checking the "cpus" node, as an enable-method
  662. * property in that node is undocumented.
  663. */
  664. node = of_cpu_device_node_get(0);
  665. if (!node)
  666. return -ENODEV;
  667. /*
  668. * We can't actually use the enable-method magic in the kernel.
  669. * Our loopback / trampoline code uses the CPU suspend framework,
  670. * which requires the identity mapping be available. It would not
  671. * yet be available if we used the .init_cpus or .prepare_cpus
  672. * callbacks in smp_operations, which we would use if we were to
  673. * use CPU_METHOD_OF_DECLARE
  674. */
  675. for (i = 0; i < ARRAY_SIZE(sunxi_mc_smp_data); i++) {
  676. ret = of_property_match_string(node, "enable-method",
  677. sunxi_mc_smp_data[i].enable_method);
  678. if (!ret)
  679. break;
  680. }
  681. is_a83t = sunxi_mc_smp_data[i].is_a83t;
  682. of_node_put(node);
  683. if (ret)
  684. return -ENODEV;
  685. if (!sunxi_mc_smp_cpu_table_init())
  686. return -EINVAL;
  687. if (!cci_probed()) {
  688. pr_err("%s: CCI-400 not available\n", __func__);
  689. return -ENODEV;
  690. }
  691. /* Get needed device tree nodes */
  692. ret = sunxi_mc_smp_data[i].get_smp_nodes(&nodes);
  693. if (ret)
  694. goto err_put_nodes;
  695. /*
  696. * Unfortunately we can not request the I/O region for the PRCM.
  697. * It is shared with the PRCM clock.
  698. */
  699. prcm_base = of_iomap(nodes.prcm_node, 0);
  700. if (!prcm_base) {
  701. pr_err("%s: failed to map PRCM registers\n", __func__);
  702. ret = -ENOMEM;
  703. goto err_put_nodes;
  704. }
  705. cpucfg_base = of_io_request_and_map(nodes.cpucfg_node, 0,
  706. "sunxi-mc-smp");
  707. if (IS_ERR(cpucfg_base)) {
  708. ret = PTR_ERR(cpucfg_base);
  709. pr_err("%s: failed to map CPUCFG registers: %d\n",
  710. __func__, ret);
  711. goto err_unmap_prcm;
  712. }
  713. if (is_a83t) {
  714. r_cpucfg_base = of_io_request_and_map(nodes.r_cpucfg_node,
  715. 0, "sunxi-mc-smp");
  716. if (IS_ERR(r_cpucfg_base)) {
  717. ret = PTR_ERR(r_cpucfg_base);
  718. pr_err("%s: failed to map R-CPUCFG registers\n",
  719. __func__);
  720. goto err_unmap_release_cpucfg;
  721. }
  722. } else {
  723. sram_b_smp_base = of_io_request_and_map(nodes.sram_node, 0,
  724. "sunxi-mc-smp");
  725. if (IS_ERR(sram_b_smp_base)) {
  726. ret = PTR_ERR(sram_b_smp_base);
  727. pr_err("%s: failed to map secure SRAM\n", __func__);
  728. goto err_unmap_release_cpucfg;
  729. }
  730. }
  731. /* Configure CCI-400 for boot cluster */
  732. ret = sunxi_mc_smp_loopback();
  733. if (ret) {
  734. pr_err("%s: failed to configure boot cluster: %d\n",
  735. __func__, ret);
  736. goto err_unmap_release_sram_rcpucfg;
  737. }
  738. /* We don't need the device nodes anymore */
  739. sunxi_mc_smp_put_nodes(&nodes);
  740. /* Set the hardware entry point address */
  741. if (is_a83t)
  742. addr = r_cpucfg_base + R_CPUCFG_CPU_SOFT_ENTRY_REG;
  743. else
  744. addr = prcm_base + PRCM_CPU_SOFT_ENTRY_REG;
  745. writel(__pa_symbol(sunxi_mc_smp_secondary_startup), addr);
  746. /* Actually enable multi cluster SMP */
  747. smp_set_ops(&sunxi_mc_smp_smp_ops);
  748. pr_info("sunxi multi cluster SMP support installed\n");
  749. return 0;
  750. err_unmap_release_sram_rcpucfg:
  751. if (is_a83t) {
  752. iounmap(r_cpucfg_base);
  753. of_address_to_resource(nodes.r_cpucfg_node, 0, &res);
  754. } else {
  755. iounmap(sram_b_smp_base);
  756. of_address_to_resource(nodes.sram_node, 0, &res);
  757. }
  758. release_mem_region(res.start, resource_size(&res));
  759. err_unmap_release_cpucfg:
  760. iounmap(cpucfg_base);
  761. of_address_to_resource(nodes.cpucfg_node, 0, &res);
  762. release_mem_region(res.start, resource_size(&res));
  763. err_unmap_prcm:
  764. iounmap(prcm_base);
  765. err_put_nodes:
  766. sunxi_mc_smp_put_nodes(&nodes);
  767. return ret;
  768. }
  769. early_initcall(sunxi_mc_smp_init);