kona_smp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2014 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/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/sched.h>
  19. #include <asm/smp.h>
  20. #include <asm/smp_plat.h>
  21. #include <asm/smp_scu.h>
  22. /* Size of mapped Cortex A9 SCU address space */
  23. #define CORTEX_A9_SCU_SIZE 0x58
  24. #define SECONDARY_TIMEOUT_NS NSEC_PER_MSEC /* 1 msec (in nanoseconds) */
  25. #define BOOT_ADDR_CPUID_MASK 0x3
  26. /* Name of device node property defining secondary boot register location */
  27. #define OF_SECONDARY_BOOT "secondary-boot-reg"
  28. /* I/O address of register used to coordinate secondary core startup */
  29. static u32 secondary_boot;
  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 void __init bcm_smp_prepare_cpus(unsigned int max_cpus)
  65. {
  66. static cpumask_t only_cpu_0 = { CPU_BITS_CPU0 };
  67. struct device_node *node;
  68. int ret;
  69. BUG_ON(secondary_boot); /* We're called only once */
  70. /*
  71. * This function is only called via smp_ops->smp_prepare_cpu().
  72. * That only happens if a "/cpus" device tree node exists
  73. * and has an "enable-method" property that selects the SMP
  74. * operations defined herein.
  75. */
  76. node = of_find_node_by_path("/cpus");
  77. BUG_ON(!node);
  78. /*
  79. * Our secondary enable method requires a "secondary-boot-reg"
  80. * property to specify a register address used to request the
  81. * ROM code boot a secondary code. If we have any trouble
  82. * getting this we fall back to uniprocessor mode.
  83. */
  84. if (of_property_read_u32(node, OF_SECONDARY_BOOT, &secondary_boot)) {
  85. pr_err("%s: missing/invalid " OF_SECONDARY_BOOT " property\n",
  86. node->name);
  87. ret = -ENOENT; /* Arrange to disable SMP */
  88. goto out;
  89. }
  90. /*
  91. * Enable the SCU on Cortex A9 based SoCs. If -ENOENT is
  92. * returned, the SoC reported a uniprocessor configuration.
  93. * We bail on any other error.
  94. */
  95. ret = scu_a9_enable();
  96. out:
  97. of_node_put(node);
  98. if (ret) {
  99. /* Update the CPU present map to reflect uniprocessor mode */
  100. BUG_ON(ret != -ENOENT);
  101. pr_warn("disabling SMP\n");
  102. init_cpu_present(&only_cpu_0);
  103. }
  104. }
  105. /*
  106. * The ROM code has the secondary cores looping, waiting for an event.
  107. * When an event occurs each core examines the bottom two bits of the
  108. * secondary boot register. When a core finds those bits contain its
  109. * own core id, it performs initialization, including computing its boot
  110. * address by clearing the boot register value's bottom two bits. The
  111. * core signals that it is beginning its execution by writing its boot
  112. * address back to the secondary boot register, and finally jumps to
  113. * that address.
  114. *
  115. * So to start a core executing we need to:
  116. * - Encode the (hardware) CPU id with the bottom bits of the secondary
  117. * start address.
  118. * - Write that value into the secondary boot register.
  119. * - Generate an event to wake up the secondary CPU(s).
  120. * - Wait for the secondary boot register to be re-written, which
  121. * indicates the secondary core has started.
  122. */
  123. static int bcm_boot_secondary(unsigned int cpu, struct task_struct *idle)
  124. {
  125. void __iomem *boot_reg;
  126. phys_addr_t boot_func;
  127. u64 start_clock;
  128. u32 cpu_id;
  129. u32 boot_val;
  130. bool timeout = false;
  131. cpu_id = cpu_logical_map(cpu);
  132. if (cpu_id & ~BOOT_ADDR_CPUID_MASK) {
  133. pr_err("bad cpu id (%u > %u)\n", cpu_id, BOOT_ADDR_CPUID_MASK);
  134. return -EINVAL;
  135. }
  136. if (!secondary_boot) {
  137. pr_err("required secondary boot register not specified\n");
  138. return -EINVAL;
  139. }
  140. boot_reg = ioremap_nocache((phys_addr_t)secondary_boot, sizeof(u32));
  141. if (!boot_reg) {
  142. pr_err("unable to map boot register for cpu %u\n", cpu_id);
  143. return -ENOSYS;
  144. }
  145. /*
  146. * Secondary cores will start in secondary_startup(),
  147. * defined in "arch/arm/kernel/head.S"
  148. */
  149. boot_func = virt_to_phys(secondary_startup);
  150. BUG_ON(boot_func & BOOT_ADDR_CPUID_MASK);
  151. BUG_ON(boot_func > (phys_addr_t)U32_MAX);
  152. /* The core to start is encoded in the low bits */
  153. boot_val = (u32)boot_func | cpu_id;
  154. writel_relaxed(boot_val, boot_reg);
  155. sev();
  156. /* The low bits will be cleared once the core has started */
  157. start_clock = local_clock();
  158. while (!timeout && readl_relaxed(boot_reg) == boot_val)
  159. timeout = local_clock() - start_clock > SECONDARY_TIMEOUT_NS;
  160. iounmap(boot_reg);
  161. if (!timeout)
  162. return 0;
  163. pr_err("timeout waiting for cpu %u to start\n", cpu_id);
  164. return -ENOSYS;
  165. }
  166. static struct smp_operations bcm_smp_ops __initdata = {
  167. .smp_prepare_cpus = bcm_smp_prepare_cpus,
  168. .smp_boot_secondary = bcm_boot_secondary,
  169. };
  170. CPU_METHOD_OF_DECLARE(bcm_smp_bcm281xx, "brcm,bcm11351-cpu-method",
  171. &bcm_smp_ops);