platsmp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/sched.h>
  22. #include <linux/smp.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/smp.h>
  25. #include <asm/smp_plat.h>
  26. #include <asm/smp_scu.h>
  27. /* Size of mapped Cortex A9 SCU address space */
  28. #define CORTEX_A9_SCU_SIZE 0x58
  29. #define SECONDARY_TIMEOUT_NS NSEC_PER_MSEC /* 1 msec (in nanoseconds) */
  30. #define BOOT_ADDR_CPUID_MASK 0x3
  31. /* Name of device node property defining secondary boot register location */
  32. #define OF_SECONDARY_BOOT "secondary-boot-reg"
  33. #define MPIDR_CPUID_BITMASK 0x3
  34. /* I/O address of register used to coordinate secondary core startup */
  35. static u32 secondary_boot_addr;
  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 int nsp_write_lut(void)
  71. {
  72. void __iomem *sku_rom_lut;
  73. phys_addr_t secondary_startup_phy;
  74. if (!secondary_boot_addr) {
  75. pr_warn("required secondary boot register not specified\n");
  76. return -EINVAL;
  77. }
  78. sku_rom_lut = ioremap_nocache((phys_addr_t)secondary_boot_addr,
  79. sizeof(secondary_boot_addr));
  80. if (!sku_rom_lut) {
  81. pr_warn("unable to ioremap SKU-ROM LUT register\n");
  82. return -ENOMEM;
  83. }
  84. secondary_startup_phy = virt_to_phys(secondary_startup);
  85. BUG_ON(secondary_startup_phy > (phys_addr_t)U32_MAX);
  86. writel_relaxed(secondary_startup_phy, sku_rom_lut);
  87. /* Ensure the write is visible to the secondary core */
  88. smp_wmb();
  89. iounmap(sku_rom_lut);
  90. return 0;
  91. }
  92. static void __init bcm_smp_prepare_cpus(unsigned int max_cpus)
  93. {
  94. static cpumask_t only_cpu_0 = { CPU_BITS_CPU0 };
  95. struct device_node *cpus_node = NULL;
  96. struct device_node *cpu_node = NULL;
  97. int ret;
  98. /*
  99. * This function is only called via smp_ops->smp_prepare_cpu().
  100. * That only happens if a "/cpus" device tree node exists
  101. * and has an "enable-method" property that selects the SMP
  102. * operations defined herein.
  103. */
  104. cpus_node = of_find_node_by_path("/cpus");
  105. if (!cpus_node)
  106. return;
  107. for_each_child_of_node(cpus_node, cpu_node) {
  108. u32 cpuid;
  109. if (of_node_cmp(cpu_node->type, "cpu"))
  110. continue;
  111. if (of_property_read_u32(cpu_node, "reg", &cpuid)) {
  112. pr_debug("%s: missing reg property\n",
  113. cpu_node->full_name);
  114. ret = -ENOENT;
  115. goto out;
  116. }
  117. /*
  118. * "secondary-boot-reg" property should be defined only
  119. * for secondary cpu
  120. */
  121. if ((cpuid & MPIDR_CPUID_BITMASK) == 1) {
  122. /*
  123. * Our secondary enable method requires a
  124. * "secondary-boot-reg" property to specify a register
  125. * address used to request the ROM code boot a secondary
  126. * core. If we have any trouble getting this we fall
  127. * back to uniprocessor mode.
  128. */
  129. if (of_property_read_u32(cpu_node,
  130. OF_SECONDARY_BOOT,
  131. &secondary_boot_addr)) {
  132. pr_warn("%s: no" OF_SECONDARY_BOOT "property\n",
  133. cpu_node->name);
  134. ret = -ENOENT;
  135. goto out;
  136. }
  137. }
  138. }
  139. /*
  140. * Enable the SCU on Cortex A9 based SoCs. If -ENOENT is
  141. * returned, the SoC reported a uniprocessor configuration.
  142. * We bail on any other error.
  143. */
  144. ret = scu_a9_enable();
  145. out:
  146. of_node_put(cpu_node);
  147. of_node_put(cpus_node);
  148. if (ret) {
  149. /* Update the CPU present map to reflect uniprocessor mode */
  150. pr_warn("disabling SMP\n");
  151. init_cpu_present(&only_cpu_0);
  152. }
  153. }
  154. /*
  155. * The ROM code has the secondary cores looping, waiting for an event.
  156. * When an event occurs each core examines the bottom two bits of the
  157. * secondary boot register. When a core finds those bits contain its
  158. * own core id, it performs initialization, including computing its boot
  159. * address by clearing the boot register value's bottom two bits. The
  160. * core signals that it is beginning its execution by writing its boot
  161. * address back to the secondary boot register, and finally jumps to
  162. * that address.
  163. *
  164. * So to start a core executing we need to:
  165. * - Encode the (hardware) CPU id with the bottom bits of the secondary
  166. * start address.
  167. * - Write that value into the secondary boot register.
  168. * - Generate an event to wake up the secondary CPU(s).
  169. * - Wait for the secondary boot register to be re-written, which
  170. * indicates the secondary core has started.
  171. */
  172. static int kona_boot_secondary(unsigned int cpu, struct task_struct *idle)
  173. {
  174. void __iomem *boot_reg;
  175. phys_addr_t boot_func;
  176. u64 start_clock;
  177. u32 cpu_id;
  178. u32 boot_val;
  179. bool timeout = false;
  180. cpu_id = cpu_logical_map(cpu);
  181. if (cpu_id & ~BOOT_ADDR_CPUID_MASK) {
  182. pr_err("bad cpu id (%u > %u)\n", cpu_id, BOOT_ADDR_CPUID_MASK);
  183. return -EINVAL;
  184. }
  185. if (!secondary_boot_addr) {
  186. pr_err("required secondary boot register not specified\n");
  187. return -EINVAL;
  188. }
  189. boot_reg = ioremap_nocache(
  190. (phys_addr_t)secondary_boot_addr, sizeof(u32));
  191. if (!boot_reg) {
  192. pr_err("unable to map boot register for cpu %u\n", cpu_id);
  193. return -ENOMEM;
  194. }
  195. /*
  196. * Secondary cores will start in secondary_startup(),
  197. * defined in "arch/arm/kernel/head.S"
  198. */
  199. boot_func = virt_to_phys(secondary_startup);
  200. BUG_ON(boot_func & BOOT_ADDR_CPUID_MASK);
  201. BUG_ON(boot_func > (phys_addr_t)U32_MAX);
  202. /* The core to start is encoded in the low bits */
  203. boot_val = (u32)boot_func | cpu_id;
  204. writel_relaxed(boot_val, boot_reg);
  205. sev();
  206. /* The low bits will be cleared once the core has started */
  207. start_clock = local_clock();
  208. while (!timeout && readl_relaxed(boot_reg) == boot_val)
  209. timeout = local_clock() - start_clock > SECONDARY_TIMEOUT_NS;
  210. iounmap(boot_reg);
  211. if (!timeout)
  212. return 0;
  213. pr_err("timeout waiting for cpu %u to start\n", cpu_id);
  214. return -ENXIO;
  215. }
  216. static int nsp_boot_secondary(unsigned int cpu, struct task_struct *idle)
  217. {
  218. int ret;
  219. /*
  220. * After wake up, secondary core branches to the startup
  221. * address programmed at SKU ROM LUT location.
  222. */
  223. ret = nsp_write_lut();
  224. if (ret) {
  225. pr_err("unable to write startup addr to SKU ROM LUT\n");
  226. goto out;
  227. }
  228. /* Send a CPU wakeup interrupt to the secondary core */
  229. arch_send_wakeup_ipi_mask(cpumask_of(cpu));
  230. out:
  231. return ret;
  232. }
  233. static const struct smp_operations bcm_smp_ops __initconst = {
  234. .smp_prepare_cpus = bcm_smp_prepare_cpus,
  235. .smp_boot_secondary = kona_boot_secondary,
  236. };
  237. CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx, "brcm,bcm11351-cpu-method",
  238. &bcm_smp_ops);
  239. struct smp_operations nsp_smp_ops __initdata = {
  240. .smp_prepare_cpus = bcm_smp_prepare_cpus,
  241. .smp_boot_secondary = nsp_boot_secondary,
  242. };
  243. CPU_METHOD_OF_DECLARE(bcm_smp_nsp, "brcm,bcm-nsp-smp", &nsp_smp_ops);