platsmp.c 8.1 KB

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