platsmp.c 8.9 KB

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