You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/03/28 11:30:33 UTC

(nuttx) 01/02: qemu/armv7a: add Symmetric Multi-Processing (SMP) support

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c7770fddfe79e4d61c9f316ab2b9a1aeb37348c0
Author: chao an <an...@lixiang.com>
AuthorDate: Thu Mar 28 14:48:10 2024 +0800

    qemu/armv7a: add Symmetric Multi-Processing (SMP) support
    
    Bringup Co-Processor by PSCI(Power State Coordination Interface)
    
    Signed-off-by: chao an <an...@lixiang.com>
---
 arch/arm/src/qemu/Make.defs                    |   4 +
 arch/arm/src/qemu/qemu_boot.c                  |   9 ++
 arch/arm/src/qemu/{qemu_boot.c => qemu_boot.h} |  80 ++++++------
 arch/arm/src/qemu/qemu_cpuboot.c               | 172 +++++++++++++++++++++++++
 4 files changed, 229 insertions(+), 36 deletions(-)

diff --git a/arch/arm/src/qemu/Make.defs b/arch/arm/src/qemu/Make.defs
index 7ba6e1a0dc..f51924e3b0 100644
--- a/arch/arm/src/qemu/Make.defs
+++ b/arch/arm/src/qemu/Make.defs
@@ -22,3 +22,7 @@ include armv7-a/Make.defs
 
 # qemu-specific C source files
 CHIP_CSRCS  = qemu_boot.c qemu_serial.c qemu_irq.c qemu_timer.c qemu_memorymap.c
+
+ifeq ($(CONFIG_SMP),y)
+  CHIP_CSRCS += qemu_cpuboot.c
+endif
diff --git a/arch/arm/src/qemu/qemu_boot.c b/arch/arm/src/qemu/qemu_boot.c
index 29eafa84e4..b61c243d03 100644
--- a/arch/arm/src/qemu/qemu_boot.c
+++ b/arch/arm/src/qemu/qemu_boot.c
@@ -26,6 +26,7 @@
 
 #include "arm_internal.h"
 
+#include "qemu_boot.h"
 #include "qemu_irq.h"
 #include "qemu_memorymap.h"
 
@@ -72,4 +73,12 @@ void arm_boot(void)
 
   arm_earlyserialinit();
 #endif
+
+  /* Now we can enable all other CPUs.  The enabled CPUs will start execution
+   * at __cpuN_start and, after very low-level CPU initialization has been
+   * performed, will branch to arm_cpu_boot()
+   * (see arch/arm/src/armv7-a/smp.h)
+   */
+
+  qemu_cpu_enable();
 }
diff --git a/arch/arm/src/qemu/qemu_boot.c b/arch/arm/src/qemu/qemu_boot.h
similarity index 58%
copy from arch/arm/src/qemu/qemu_boot.c
copy to arch/arm/src/qemu/qemu_boot.h
index 29eafa84e4..3e3a1dbf1a 100644
--- a/arch/arm/src/qemu/qemu_boot.c
+++ b/arch/arm/src/qemu/qemu_boot.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * arch/arm/src/qemu/qemu_boot.c
+ * arch/arm/src/qemu/qemu_boot.h
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,58 +18,66 @@
  *
  ****************************************************************************/
 
+#ifndef __ARCH_ARM_SRC_QEMU_QEMU_BOOT_H
+#define __ARCH_ARM_SRC_QEMU_QEMU_BOOT_H
+
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <nuttx/compiler.h>
+#include <sys/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <arch/chip/chip.h>
 
-#include "arm_internal.h"
-
-#include "qemu_irq.h"
-#include "qemu_memorymap.h"
-
-#ifdef CONFIG_DEVICE_TREE
-#  include <nuttx/fdt.h>
-#endif
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
 
 /****************************************************************************
- * Public Functions
+ * Public Function Prototypes
  ****************************************************************************/
 
+#ifndef __ASSEMBLY__
+
+#undef EXTERN
+#if defined(__cplusplus)
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
 /****************************************************************************
- * Name: arm_boot
+ * Name: qemu_cpu_enable
  *
  * Description:
- *   Complete boot operations started in arm_head.S
+ *   Called from CPU0 to enable all other CPUs.  The enabled CPUs will start
+ *   execution at __cpuN_start and, after very low-level CPU initialization
+ *   has been performed, will branch to arm_cpu_boot()
+ *   (see arch/arm/src/armv7-a/smp.h)
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
  *
  ****************************************************************************/
 
-void arm_boot(void)
-{
-  /* Perf init */
-
-  up_perf_init(0);
-
-  /* Set the page table for section */
-
-  qemu_setupmappings();
-
-  arm_fpuconfig();
-
-#if defined(CONFIG_ARCH_HAVE_PSCI)
-  arm_psci_init("hvc");
+#ifdef CONFIG_SMP
+void qemu_cpu_enable(void);
+#else
+#  define qemu_cpu_enable()
 #endif
 
-#ifdef CONFIG_DEVICE_TREE
-  fdt_register((const char *)0x40000000);
+#undef EXTERN
+#if defined(__cplusplus)
+}
 #endif
 
-#ifdef USE_EARLYSERIALINIT
-  /* Perform early serial initialization if we are going to use the serial
-   * driver.
-   */
-
-  arm_earlyserialinit();
-#endif
-}
+#endif /* __ASSEMBLY__ */
+#endif /* __ARCH_ARM_SRC_QEMU_QEMU_BOOT_H */
diff --git a/arch/arm/src/qemu/qemu_cpuboot.c b/arch/arm/src/qemu/qemu_cpuboot.c
new file mode 100644
index 0000000000..f619d4670b
--- /dev/null
+++ b/arch/arm/src/qemu/qemu_cpuboot.c
@@ -0,0 +1,172 @@
+/****************************************************************************
+ * arch/arm/src/qemu/qemu_cpuboot.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdint.h>
+#include <assert.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/sched.h>
+#include <arch/irq.h>
+
+#include "arm_internal.h"
+#include "sctlr.h"
+#include "smp.h"
+#include "scu.h"
+#include "gic.h"
+#include "mmu.h"
+#include "barriers.h"
+#include "arm_cpu_psci.h"
+
+#ifdef CONFIG_SMP
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const start_t g_cpu_boot[CONFIG_SMP_NCPUS] =
+{
+  0,
+#if CONFIG_SMP_NCPUS > 1
+  __cpu1_start,
+#endif
+#if CONFIG_SMP_NCPUS > 2
+  __cpu2_start,
+#endif
+#if CONFIG_SMP_NCPUS > 3
+  __cpu3_start
+#endif
+};
+
+/* Symbols defined via the linker script */
+
+extern uint8_t _vector_start[]; /* Beginning of vector block */
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: qemu_cpu_enable
+ *
+ * Description:
+ *   Called from CPU0 to enable all other CPUs.  The enabled CPUs will start
+ *   execution at __cpuN_start and, after very low-level CPU initialization
+ *   has been performed, will branch to arm_cpu_boot()
+ *   (see arch/arm/src/armv7-a/smp.h)
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void qemu_cpu_enable(void)
+{
+  int cpu;
+
+  for (cpu = 1; cpu < CONFIG_SMP_NCPUS; cpu++)
+    {
+      /* Then enable the CPU */
+
+      psci_cpu_on(CORE_TO_MPID(cpu, 0), (uintptr_t)g_cpu_boot[cpu]);
+    }
+}
+
+/****************************************************************************
+ * Name: arm_cpu_boot
+ *
+ * Description:
+ *   Continues the C-level initialization started by the assembly language
+ *   __cpu[n]_start function.  At a minimum, this function needs to
+ *   initialize interrupt handling and, perhaps, wait on WFI for
+ *   arm_cpu_start() to issue an SGI.
+ *
+ *   This function must be provided by the each ARMv7-A MCU and implement
+ *   MCU-specific initialization logic.
+ *
+ * Input Parameters:
+ *   cpu - The CPU index.  This is the same value that would be obtained by
+ *      calling up_cpu_index();
+ *
+ * Returned Value:
+ *   Does not return.
+ *
+ ****************************************************************************/
+
+void arm_cpu_boot(int cpu)
+{
+  /* Enable SMP cache coherency for the CPU */
+
+  arm_enable_smp(cpu);
+
+  /* Initialize the FPU */
+
+  arm_fpuconfig();
+
+  /* Initialize the Generic Interrupt Controller (GIC) for CPUn (n != 0) */
+
+  arm_gic_initialize();
+
+#ifdef CONFIG_ARCH_LOWVECTORS
+  /* If CONFIG_ARCH_LOWVECTORS is defined, then the vectors located at the
+   * beginning of the .text region must appear at address at the address
+   * specified in the VBAR.  There are two ways to accomplish this:
+   *
+   *   1. By explicitly mapping the beginning of .text region with a page
+   *      table entry so that the virtual address zero maps to the beginning
+   *      of the .text region.  VBAR == 0x0000:0000.
+   *
+   *   2. Set the Cortex-A5 VBAR register so that the vector table address
+   *      is moved to a location other than 0x0000:0000.
+   *
+   *  The second method is used by this logic.
+   */
+
+  /* Set the VBAR register to the address of the vector table */
+
+  DEBUGASSERT((((uintptr_t)_vector_start) & ~VBAR_MASK) == 0);
+  cp15_wrvbar((uint32_t)_vector_start);
+#endif /* CONFIG_ARCH_LOWVECTORS */
+
+#ifndef CONFIG_SUPPRESS_INTERRUPTS
+  /* And finally, enable interrupts */
+
+  up_irq_enable();
+#endif
+
+  /* The next thing that we expect to happen is for logic running on CPU0
+   * to call up_cpu_start() which generate an SGI and a context switch to
+   * the configured NuttX IDLE task.
+   */
+
+  for (; ; )
+    {
+      asm("WFI");
+    }
+}
+#endif /* CONFIG_SMP */