platsmp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014-2015 Broadcom Corporation
  4. * Copyright 2014 Linaro Limited
  5. */
  6. #include <linux/cpumask.h>
  7. #include <linux/delay.h>
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/irqchip/irq-bcm2836.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/clock.h>
  17. #include <linux/smp.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/smp.h>
  20. #include <asm/smp_plat.h>
  21. #include <asm/smp_scu.h>
  22. #include "platsmp.h"
  23. /* Size of mapped Cortex A9 SCU address space */
  24. #define CORTEX_A9_SCU_SIZE 0x58
  25. #define SECONDARY_TIMEOUT_NS NSEC_PER_MSEC /* 1 msec (in nanoseconds) */
  26. #define BOOT_ADDR_CPUID_MASK 0x3
  27. /* Name of device node property defining secondary boot register location */
  28. #define OF_SECONDARY_BOOT "secondary-boot-reg"
  29. #define MPIDR_CPUID_BITMASK 0x3
  30. /*
  31. * Enable the Cortex A9 Snoop Control Unit
  32. *
  33. * By the time this is called we already know there are multiple
  34. * cores present. We assume we're running on a Cortex A9 processor,
  35. * so any trouble getting the base address register or getting the
  36. * SCU base is a problem.
  37. *
  38. * Return 0 if successful or an error code otherwise.
  39. */
  40. static int __init scu_a9_enable(void)
  41. {
  42. unsigned long config_base;
  43. void __iomem *scu_base;
  44. if (!scu_a9_has_base()) {
  45. pr_err("no configuration base address register!\n");
  46. return -ENXIO;
  47. }
  48. /* Config base address register value is zero for uniprocessor */
  49. config_base = scu_a9_get_base();
  50. if (!config_base) {
  51. pr_err("hardware reports only one core\n");
  52. return -ENOENT;
  53. }
  54. scu_base = ioremap((phys_addr_t)config_base, CORTEX_A9_SCU_SIZE);
  55. if (!scu_base) {
  56. pr_err("failed to remap config base (%lu/%u) for SCU\n",
  57. config_base, CORTEX_A9_SCU_SIZE);
  58. return -ENOMEM;
  59. }
  60. scu_enable(scu_base);
  61. iounmap(scu_base); /* That's the last we'll need of this */
  62. return 0;
  63. }
  64. static u32 secondary_boot_addr_for(unsigned int cpu)
  65. {
  66. u32 secondary_boot_addr = 0;
  67. struct device_node *cpu_node = of_get_cpu_node(cpu, NULL);
  68. if (!cpu_node) {
  69. pr_err("Failed to find device tree node for CPU%u\n", cpu);
  70. return 0;
  71. }
  72. if (of_property_read_u32(cpu_node,
  73. OF_SECONDARY_BOOT,
  74. &secondary_boot_addr))
  75. pr_err("required secondary boot register not specified for CPU%u\n",
  76. cpu);
  77. of_node_put(cpu_node);
  78. return secondary_boot_addr;
  79. }
  80. static int nsp_write_lut(unsigned int cpu)
  81. {
  82. void __iomem *sku_rom_lut;
  83. phys_addr_t secondary_startup_phy;
  84. const u32 secondary_boot_addr = secondary_boot_addr_for(cpu);
  85. if (!secondary_boot_addr)
  86. return -EINVAL;
  87. sku_rom_lut = ioremap((phys_addr_t)secondary_boot_addr,
  88. sizeof(phys_addr_t));
  89. if (!sku_rom_lut) {
  90. pr_warn("unable to ioremap SKU-ROM LUT register for cpu %u\n", cpu);
  91. return -ENOMEM;
  92. }
  93. secondary_startup_phy = __pa_symbol(secondary_startup);
  94. BUG_ON(secondary_startup_phy > (phys_addr_t)U32_MAX);
  95. writel_relaxed(secondary_startup_phy, sku_rom_lut);
  96. /* Ensure the write is visible to the secondary core */
  97. smp_wmb();
  98. iounmap(sku_rom_lut);
  99. return 0;
  100. }
  101. static void __init bcm_smp_prepare_cpus(unsigned int max_cpus)
  102. {
  103. const cpumask_t only_cpu_0 = { CPU_BITS_CPU0 };
  104. /* Enable the SCU on Cortex A9 based SoCs */
  105. if (scu_a9_enable()) {
  106. /* Update the CPU present map to reflect uniprocessor mode */
  107. pr_warn("failed to enable A9 SCU - disabling SMP\n");
  108. init_cpu_present(&only_cpu_0);
  109. }
  110. }
  111. /*
  112. * The ROM code has the secondary cores looping, waiting for an event.
  113. * When an event occurs each core examines the bottom two bits of the
  114. * secondary boot register. When a core finds those bits contain its
  115. * own core id, it performs initialization, including computing its boot
  116. * address by clearing the boot register value's bottom two bits. The
  117. * core signals that it is beginning its execution by writing its boot
  118. * address back to the secondary boot register, and finally jumps to
  119. * that address.
  120. *
  121. * So to start a core executing we need to:
  122. * - Encode the (hardware) CPU id with the bottom bits of the secondary
  123. * start address.
  124. * - Write that value into the secondary boot register.
  125. * - Generate an event to wake up the secondary CPU(s).
  126. * - Wait for the secondary boot register to be re-written, which
  127. * indicates the secondary core has started.
  128. */
  129. static int kona_boot_secondary(unsigned int cpu, struct task_struct *idle)
  130. {
  131. void __iomem *boot_reg;
  132. phys_addr_t boot_func;
  133. u64 start_clock;
  134. u32 cpu_id;
  135. u32 boot_val;
  136. bool timeout = false;
  137. const u32 secondary_boot_addr = secondary_boot_addr_for(cpu);
  138. cpu_id = cpu_logical_map(cpu);
  139. if (cpu_id & ~BOOT_ADDR_CPUID_MASK) {
  140. pr_err("bad cpu id (%u > %u)\n", cpu_id, BOOT_ADDR_CPUID_MASK);
  141. return -EINVAL;
  142. }
  143. if (!secondary_boot_addr)
  144. return -EINVAL;
  145. boot_reg = ioremap((phys_addr_t)secondary_boot_addr,
  146. sizeof(phys_addr_t));
  147. if (!boot_reg) {
  148. pr_err("unable to map boot register for cpu %u\n", cpu_id);
  149. return -ENOMEM;
  150. }
  151. /*
  152. * Secondary cores will start in secondary_startup(),
  153. * defined in "arch/arm/kernel/head.S"
  154. */
  155. boot_func = __pa_symbol(secondary_startup);
  156. BUG_ON(boot_func & BOOT_ADDR_CPUID_MASK);
  157. BUG_ON(boot_func > (phys_addr_t)U32_MAX);
  158. /* The core to start is encoded in the low bits */
  159. boot_val = (u32)boot_func | cpu_id;
  160. writel_relaxed(boot_val, boot_reg);
  161. sev();
  162. /* The low bits will be cleared once the core has started */
  163. start_clock = local_clock();
  164. while (!timeout && readl_relaxed(boot_reg) == boot_val)
  165. timeout = local_clock() - start_clock > SECONDARY_TIMEOUT_NS;
  166. iounmap(boot_reg);
  167. if (!timeout)
  168. return 0;
  169. pr_err("timeout waiting for cpu %u to start\n", cpu_id);
  170. return -ENXIO;
  171. }
  172. /* Cluster Dormant Control command to bring CPU into a running state */
  173. #define CDC_CMD 6
  174. #define CDC_CMD_OFFSET 0
  175. #define CDC_CMD_REG(cpu) (CDC_CMD_OFFSET + 4*(cpu))
  176. /*
  177. * BCM23550 has a Cluster Dormant Control block that keeps the core in
  178. * idle state. A command needs to be sent to the block to bring the CPU
  179. * into running state.
  180. */
  181. static int bcm23550_boot_secondary(unsigned int cpu, struct task_struct *idle)
  182. {
  183. void __iomem *cdc_base;
  184. struct device_node *dn;
  185. char *name;
  186. int ret;
  187. /* Make sure a CDC node exists before booting the
  188. * secondary core.
  189. */
  190. name = "brcm,bcm23550-cdc";
  191. dn = of_find_compatible_node(NULL, NULL, name);
  192. if (!dn) {
  193. pr_err("unable to find cdc node\n");
  194. return -ENODEV;
  195. }
  196. cdc_base = of_iomap(dn, 0);
  197. of_node_put(dn);
  198. if (!cdc_base) {
  199. pr_err("unable to remap cdc base register\n");
  200. return -ENOMEM;
  201. }
  202. /* Boot the secondary core */
  203. ret = kona_boot_secondary(cpu, idle);
  204. if (ret)
  205. goto out;
  206. /* Bring this CPU to RUN state so that nIRQ nFIQ
  207. * signals are unblocked.
  208. */
  209. writel_relaxed(CDC_CMD, cdc_base + CDC_CMD_REG(cpu));
  210. out:
  211. iounmap(cdc_base);
  212. return ret;
  213. }
  214. static int nsp_boot_secondary(unsigned int cpu, struct task_struct *idle)
  215. {
  216. int ret;
  217. /*
  218. * After wake up, secondary core branches to the startup
  219. * address programmed at SKU ROM LUT location.
  220. */
  221. ret = nsp_write_lut(cpu);
  222. if (ret) {
  223. pr_err("unable to write startup addr to SKU ROM LUT\n");
  224. goto out;
  225. }
  226. /* Send a CPU wakeup interrupt to the secondary core */
  227. arch_send_wakeup_ipi_mask(cpumask_of(cpu));
  228. out:
  229. return ret;
  230. }
  231. static int bcm2836_boot_secondary(unsigned int cpu, struct task_struct *idle)
  232. {
  233. void __iomem *intc_base;
  234. struct device_node *dn;
  235. char *name;
  236. name = "brcm,bcm2836-l1-intc";
  237. dn = of_find_compatible_node(NULL, NULL, name);
  238. if (!dn) {
  239. pr_err("unable to find intc node\n");
  240. return -ENODEV;
  241. }
  242. intc_base = of_iomap(dn, 0);
  243. of_node_put(dn);
  244. if (!intc_base) {
  245. pr_err("unable to remap intc base register\n");
  246. return -ENOMEM;
  247. }
  248. writel(virt_to_phys(secondary_startup),
  249. intc_base + LOCAL_MAILBOX3_SET0 + 16 * cpu);
  250. dsb(sy);
  251. sev();
  252. iounmap(intc_base);
  253. return 0;
  254. }
  255. static const struct smp_operations kona_smp_ops __initconst = {
  256. .smp_prepare_cpus = bcm_smp_prepare_cpus,
  257. .smp_boot_secondary = kona_boot_secondary,
  258. };
  259. CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx, "brcm,bcm11351-cpu-method",
  260. &kona_smp_ops);
  261. static const struct smp_operations bcm23550_smp_ops __initconst = {
  262. .smp_boot_secondary = bcm23550_boot_secondary,
  263. };
  264. CPU_METHOD_OF_DECLARE(bcm_smp_bcm23550, "brcm,bcm23550",
  265. &bcm23550_smp_ops);
  266. static const struct smp_operations nsp_smp_ops __initconst = {
  267. .smp_prepare_cpus = bcm_smp_prepare_cpus,
  268. .smp_boot_secondary = nsp_boot_secondary,
  269. };
  270. CPU_METHOD_OF_DECLARE(bcm_smp_nsp, "brcm,bcm-nsp-smp", &nsp_smp_ops);
  271. const struct smp_operations bcm2836_smp_ops __initconst = {
  272. .smp_boot_secondary = bcm2836_boot_secondary,
  273. };
  274. CPU_METHOD_OF_DECLARE(bcm_smp_bcm2836, "brcm,bcm2836-smp", &bcm2836_smp_ops);