arch/risc-v: Remove riscv_mhartid

Summary:
This commit removes the riscv_mhartid function and replaces all its
usages with up_cpu_index. The functionality is consolidated into
up_cpu_index which provides a more consistent API for getting the
current CPU/hart ID across different execution modes (machine/supervisor).

Impact:
- Removes riscv_mhartid.S and its references from build systems
- Updates all arch-specific code to use up_cpu_index instead
- Adds more detailed documentation for up_cpu_index behavior
- Changes the implementation of up_cpu_index to use percpu scratch
register in supervisor mode

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi 2025-01-14 19:46:52 +08:00 committed by Alan C. Assis
parent 6e82ca3013
commit b1d97688b7
19 changed files with 45 additions and 114 deletions

View file

@ -695,7 +695,27 @@ irqstate_t up_irq_enable(void);
* Name: up_cpu_index
*
* Description:
* Return the real core number regardless CONFIG_SMP setting
* Return the real core number regardless CONFIG_SMP setting,
* context aware way to query hart id (physical core ID)
*
* The function up_cpu_index is designed to retrieve the hardware thread
* ID (hartid) in different execution modes of RISC-V. Its behavior depends
* on the configuration and execution mode:
*
* - In machine mode, up_cpu_index reads directly from the CSR mhartid.
* - In supervisor mode, the hartid is stored in the percpu structure
* during boot because supervisor mode does not have access to CSR
* `shartid`. The SBI (Supervisor Binary Interface) provides the hartid
* in the a0 register (as per SBI ABI requirements), and it is the
* responsibility of the payload OS to store this value internally.
* We use the percpu scratch register for this purpose, as it is the only
* location that is unique for each CPU and non-volatile.
*
* Note: In flat (machine) mode, you could still read the hartid from CSR
* mhartid even if CONFIG_RISCV_PERCPU_SCRATCH is enabled.
*
* Returned Value:
* Hart id
*
****************************************************************************/

View file

@ -56,7 +56,7 @@
#if defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 15
.macro setintstack tmp0, tmp1
riscv_mhartid \tmp0
up_cpu_index \tmp0
li \tmp1, STACK_ALIGN_DOWN(CONFIG_ARCH_INTERRUPTSTACK)
mul \tmp1, \tmp0, \tmp1
la \tmp0, g_intstacktop

View file

@ -22,7 +22,7 @@
set(SRCS)
list(APPEND SRCS riscv_exception_common.S riscv_mhartid.S riscv_vectors.S)
list(APPEND SRCS riscv_exception_common.S riscv_vectors.S)
list(APPEND SRCS riscv_saveusercontext.S)
list(APPEND SRCS riscv_allocateheap.c riscv_cpuidlestack.c)
list(APPEND SRCS riscv_cpuinfo.c riscv_createstack.c riscv_doirq.c

View file

@ -25,7 +25,7 @@ STARTUP_OBJS = crt0$(OBJEXT)
endif
# Specify our general Assembly files
CMN_ASRCS += riscv_vectors.S riscv_exception_common.S riscv_mhartid.S
CMN_ASRCS += riscv_vectors.S riscv_exception_common.S
CMN_ASRCS += riscv_saveusercontext.S
# Specify C code within the common directory to be included

View file

@ -46,7 +46,7 @@
int up_this_cpu(void)
{
return riscv_hartid_to_cpuid((int)riscv_mhartid());
return riscv_hartid_to_cpuid(up_cpu_index());
}
/****************************************************************************
@ -81,6 +81,6 @@ int weak_function riscv_cpuid_to_hartid(int cpu)
#ifdef CONFIG_SMP
return cpu + CONFIG_ARCH_RV_HARTID_BASE;
#else
return (int)riscv_mhartid();
return up_cpu_index();
#endif
}

View file

@ -28,8 +28,10 @@
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <arch/csr.h>
#include "riscv_internal.h"
#include "riscv_percpu.h"
/****************************************************************************
* Public Functions
@ -45,5 +47,5 @@
int up_cpu_index(void)
{
return (int)riscv_mhartid();
return riscv_percpu_get_hartid();
}

View file

@ -356,35 +356,6 @@ void riscv_cpu_boot(int cpu);
int riscv_smp_call_handler(int irq, void *c, void *arg);
#endif
/****************************************************************************
* Name: riscv_mhartid
*
* Description:
* Context aware way to query hart id (physical core ID)
*
* The function riscv_mhartid is designed to retrieve the hardware thread
* ID (hartid) in different execution modes of RISC-V. Its behavior depends
* on the configuration and execution mode:
*
* - In machine mode, riscv_mhartid reads directly from the CSR mhartid.
* - In supervisor mode, the hartid is stored in the percpu structure
* during boot because supervisor mode does not have access to CSR
* `shartid`. The SBI (Supervisor Binary Interface) provides the hartid
* in the a0 register (as per SBI ABI requirements), and it is the
* responsibility of the payload OS to store this value internally.
* We use the percpu scratch register for this purpose, as it is the only
* location that is unique for each CPU and non-volatile.
*
* Note: In flat (machine) mode, you could still read the hartid from CSR
* mhartid even if CONFIG_RISCV_PERCPU_SCRATCH is enabled.
*
* Returned Value:
* Hart id
*
****************************************************************************/
uintptr_t riscv_mhartid(void);
#ifdef CONFIG_ARCH_RV_CPUID_MAP
/****************************************************************************
* Name: riscv_hartid_to_cpuid / riscv_cpuid_to_hartid

View file

@ -348,17 +348,10 @@
#endif /* CONFIG_ARCH_INTERRUPTSTACK > 15 */
/****************************************************************************
* Name: riscv_mhartid
*
* Description:
* Context aware way to query hart id (physical core ID)
*
* Returned Value:
* Hart id
*
* Name: up_cpu_index
****************************************************************************/
.macro riscv_mhartid out
.macro up_cpu_index out
#ifdef CONFIG_RISCV_PERCPU_SCRATCH
csrr \out, CSR_SCRATCH
REGLOAD \out, RISCV_PERCPU_HARTID(\out)

View file

@ -1,55 +0,0 @@
/****************************************************************************
* arch/risc-v/src/common/riscv_mhartid.S
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*
****************************************************************************/
.file "riscv_mhartid.S"
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include "riscv_macros.S"
/****************************************************************************
* Public Symbols
****************************************************************************/
.globl riscv_mhartid
/****************************************************************************
* Name: riscv_mhartid
*
* Description:
* Context aware way to query hart id (physical core ID)
*
* Returned Value:
* Hart id
*
****************************************************************************/
.type riscv_mhartid, function
riscv_mhartid:
riscv_mhartid a0
ret

View file

@ -56,7 +56,7 @@
#if defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 15
.macro setintstack tmp0, tmp1
riscv_mhartid \tmp0
up_cpu_index \tmp0
li \tmp1, STACK_ALIGN_DOWN(CONFIG_ARCH_INTERRUPTSTACK)
mul \tmp1, \tmp0, \tmp1
la \tmp0, g_intstacktop

View file

@ -58,7 +58,7 @@
#if defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 15
.macro setintstack tmp0, tmp1
riscv_mhartid \tmp0
up_cpu_index \tmp0
li \tmp1, STACK_ALIGN_DOWN(CONFIG_ARCH_INTERRUPTSTACK)
mul \tmp1, \tmp0, \tmp1
la \tmp0, g_intstacktop

View file

@ -376,7 +376,7 @@ static uint32_t mpfs_ihc_context_to_local_hart_id(ihc_channel_t channel)
uint32_t hart = UNDEFINED_HART_ID;
uint32_t hart_idx = 0;
uint32_t harts_in_context = LIBERO_SETTING_CONTEXT_B_HART_EN;
uint64_t mhartid = riscv_mhartid();
uint64_t mhartid = up_cpu_index();
/* If we are sending to a Context, assume we are a Context.
* i.e. HSS bootloader will not send directly to a context.
@ -589,7 +589,7 @@ static void mpfs_ihc_rx_message(ihc_channel_t channel, uint32_t mhartid,
static void mpfs_ihc_message_present_isr(void)
{
uint64_t mhartid = riscv_mhartid();
uint64_t mhartid = up_cpu_index();
bool is_ack = false;
bool is_msg = false;
@ -1308,7 +1308,7 @@ static int mpfs_rptun_thread(int argc, char *argv[])
int mpfs_ihc_init(void)
{
uint32_t mhartid = (uint32_t)riscv_mhartid();
uint32_t mhartid = (uint32_t)up_cpu_index();
#ifdef MPFS_RPTUN_USE_THREAD
char *argv[3];
char arg1[19];

View file

@ -56,7 +56,7 @@ void up_irqinitialize(void)
/* Initialize PLIC for current hart */
mpfs_plic_init_hart(riscv_mhartid());
mpfs_plic_init_hart(up_cpu_index());
/* Colorize the interrupt stack for debug purposes */

View file

@ -213,7 +213,7 @@ void mpfs_plic_init_hart(uintptr_t hartid)
uintptr_t mpfs_plic_get_iebase(void)
{
return get_iebase(riscv_mhartid());
return get_iebase(up_cpu_index());
}
/****************************************************************************
@ -229,7 +229,7 @@ uintptr_t mpfs_plic_get_iebase(void)
uintptr_t mpfs_plic_get_claimbase(void)
{
return get_claimbase(riscv_mhartid());
return get_claimbase(up_cpu_index());
}
/****************************************************************************
@ -245,5 +245,5 @@ uintptr_t mpfs_plic_get_claimbase(void)
uintptr_t mpfs_plic_get_thresholdbase(void)
{
return get_thresholdbase(riscv_mhartid());
return get_thresholdbase(up_cpu_index());
}

View file

@ -66,7 +66,7 @@ void up_timer_initialize(void)
{
/* what is our timecmp address for this hart */
uintptr_t hart_id = riscv_mhartid();
uintptr_t hart_id = up_cpu_index();
struct oneshot_lowerhalf_s *lower = riscv_mtimer_initialize(
MPFS_CLINT_MTIME, MPFS_CLINT_MTIMECMP0 + hart_id * sizeof(uintptr_t),

View file

@ -58,7 +58,7 @@
#if defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 15
.macro setintstack tmp0, tmp1
riscv_mhartid \tmp0
up_cpu_index \tmp0
li \tmp1, STACK_ALIGN_DOWN(CONFIG_ARCH_INTERRUPTSTACK)
mul \tmp1, \tmp0, \tmp1
la \tmp0, g_intstacktop

View file

@ -281,7 +281,7 @@ void up_enable_irq(int irq)
#else
riscv_aplic_configure_irq(QEMU_RV_APLIC_BASE, extirq,
RISCV_APLIC_SOURCECFG_SM_EDGE_RISE,
riscv_mhartid());
up_cpu_index());
#ifdef CONFIG_ARCH_RV_HAVE_IMSIC
riscv_imsic_local_eie_enable(extirq);
#endif

View file

@ -63,7 +63,7 @@ static void *riscv_dispatch_irq_ext(uintreg_t irq, uintreg_t *regs)
static void *riscv_dispatch_irq_ext(uintreg_t irq, uintreg_t *regs)
{
int extirq;
int hartid = riscv_mhartid();
int hartid = up_cpu_index();
uintptr_t aplic_base = RISCV_APLIC_IDC(QEMU_RV_APLIC_BASE, hartid) +
RISCV_APLIC_IDC_CLAIMI;

View file

@ -56,7 +56,7 @@
#if defined(CONFIG_SMP) && CONFIG_ARCH_INTERRUPTSTACK > 15
.macro setintstack tmp0, tmp1
riscv_mhartid \tmp0
up_cpu_index \tmp0
li \tmp1, STACK_ALIGN_DOWN(CONFIG_ARCH_INTERRUPTSTACK)
mul \tmp1, \tmp0, \tmp1
la \tmp0, g_intstacktop