mc_smp.c 24 KB

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