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