Skip to content

Commit 6e82ca3

Browse files
no1wudixiaoxiang781216
authored andcommitted
arch/risc-v: Decouple ARCH_RV_CPUID_MAP and up_cpu_index()
Summary: - Separated CPU index functionality from CPU ID mapping configuration - Moved CPU ID mapping functions to new riscv_cpuidmap.c file - Made up_cpu_index() implementation dependent on ARCH_USE_S_MODE - Updated build system to handle new file organization Impact: - Improves code organization by separating concerns between basic CPU index functionality and advanced CPU ID mapping features - Makes CPU index functionality available independently of CPU ID mapping - Reduces conditional compilation complexity in header files - Better aligns with RISC-V architecture modes (M-mode vs S-mode) Testing: GitHub CI and local testing Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
1 parent 0001008 commit 6e82ca3

File tree

5 files changed

+110
-58
lines changed

5 files changed

+110
-58
lines changed

arch/risc-v/include/irq.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,6 @@ EXTERN volatile bool g_interrupt_context[CONFIG_SMP_NCPUS];
691691

692692
irqstate_t up_irq_enable(void);
693693

694-
#ifdef CONFIG_ARCH_RV_CPUID_MAP
695694
/****************************************************************************
696695
* Name: up_cpu_index
697696
*
@@ -700,7 +699,16 @@ irqstate_t up_irq_enable(void);
700699
*
701700
****************************************************************************/
702701

702+
#ifdef CONFIG_ARCH_HAVE_MULTICPU
703+
#ifdef CONFIG_ARCH_USE_S_MODE
703704
int up_cpu_index(void) noinstrument_function;
705+
#else
706+
noinstrument_function static inline int up_cpu_index(void)
707+
{
708+
return READ_CSR(CSR_MHARTID);
709+
}
710+
#endif /* CONFIG_ARCH_USE_S_MODE */
711+
#endif /* CONFIG_ARCH_HAVE_MULTICPU */
704712

705713
/****************************************************************************
706714
* Name: up_this_cpu
@@ -711,13 +719,9 @@ int up_cpu_index(void) noinstrument_function;
711719
*
712720
****************************************************************************/
713721

722+
#ifdef CONFIG_ARCH_RV_CPUID_MAP
714723
int up_this_cpu(void);
715724
#else
716-
noinstrument_function static inline int up_cpu_index(void)
717-
{
718-
return READ_CSR(CSR_MHARTID);
719-
}
720-
721725
#define up_this_cpu() up_cpu_index()
722726
#endif /* CONFIG_ARCH_RV_CPUID_MAP */
723727

arch/risc-v/src/common/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ if(CONFIG_SMP)
4949
list(APPEND SRCS riscv_smpcall.c riscv_cpustart.c)
5050
endif()
5151

52+
if(CONFIG_ARCH_HAVE_MULTICPU)
53+
if(CONFIG_ARCH_USE_S_MODE)
54+
list(APPEND SRCS riscv_cpuindex.c)
55+
endif()
56+
endif()
57+
5258
if(CONFIG_ARCH_RV_CPUID_MAP)
53-
list(APPEND SRCS riscv_cpuindex.c)
59+
list(APPEND SRCS riscv_cpuidmap.c)
5460
endif()
5561

5662
if(CONFIG_RISCV_MISALIGNED_HANDLER)

arch/risc-v/src/common/Make.defs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ ifeq ($(CONFIG_SMP),y)
5252
CMN_CSRCS += riscv_smpcall.c riscv_cpustart.c
5353
endif
5454

55-
ifeq ($(CONFIG_ARCH_RV_CPUID_MAP),y)
55+
ifeq ($(CONFIG_ARCH_HAVE_MULTICPU),y)
56+
ifeq ($(CONFIG_ARCH_USE_S_MODE),y)
5657
CMN_CSRCS += riscv_cpuindex.c
5758
endif
59+
endif
60+
61+
ifeq ($(CONFIG_ARCH_RV_CPUID_MAP),y)
62+
CMN_CSRCS += riscv_cpuidmap.c
63+
endif
5864

5965
ifeq ($(CONFIG_RISCV_MISALIGNED_HANDLER),y)
6066
CMN_CSRCS += riscv_misaligned.c
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/****************************************************************************
2+
* arch/risc-v/src/common/riscv_cpuidmap.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
29+
#include <nuttx/arch.h>
30+
#include <nuttx/irq.h>
31+
32+
#include "riscv_internal.h"
33+
34+
/****************************************************************************
35+
* Public Functions
36+
****************************************************************************/
37+
38+
/****************************************************************************
39+
* Name: up_this_cpu
40+
*
41+
* Description:
42+
* Return the logical core number. Default implementation is 1:1 mapping,
43+
* i.e. physical=logical.
44+
*
45+
****************************************************************************/
46+
47+
int up_this_cpu(void)
48+
{
49+
return riscv_hartid_to_cpuid((int)riscv_mhartid());
50+
}
51+
52+
/****************************************************************************
53+
* Name: riscv_hartid_to_cpuid
54+
*
55+
* Description:
56+
* Convert physical core number to logical core number. Default
57+
* implementation is 1:1 mapping, i.e. physical=logical.
58+
*
59+
****************************************************************************/
60+
61+
int weak_function riscv_hartid_to_cpuid(int hart)
62+
{
63+
#ifdef CONFIG_SMP
64+
return hart - CONFIG_ARCH_RV_HARTID_BASE;
65+
#else
66+
return 0;
67+
#endif
68+
}
69+
70+
/****************************************************************************
71+
* Name: riscv_cpuid_to_hartid
72+
*
73+
* Description:
74+
* Convert logical core number to physical core number. Default
75+
* implementation is 1:1 mapping, i.e. physical=logical.
76+
*
77+
****************************************************************************/
78+
79+
int weak_function riscv_cpuid_to_hartid(int cpu)
80+
{
81+
#ifdef CONFIG_SMP
82+
return cpu + CONFIG_ARCH_RV_HARTID_BASE;
83+
#else
84+
return (int)riscv_mhartid();
85+
#endif
86+
}

arch/risc-v/src/common/riscv_cpuindex.c

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,3 @@ int up_cpu_index(void)
4747
{
4848
return (int)riscv_mhartid();
4949
}
50-
51-
/****************************************************************************
52-
* Name: up_this_cpu
53-
*
54-
* Description:
55-
* Return the logical core number. Default implementation is 1:1 mapping,
56-
* i.e. physical=logical.
57-
*
58-
****************************************************************************/
59-
60-
int up_this_cpu(void)
61-
{
62-
return riscv_hartid_to_cpuid((int)riscv_mhartid());
63-
}
64-
65-
/****************************************************************************
66-
* Name: riscv_hartid_to_cpuid
67-
*
68-
* Description:
69-
* Convert physical core number to logical core number. Default
70-
* implementation is 1:1 mapping, i.e. physical=logical.
71-
*
72-
****************************************************************************/
73-
74-
int weak_function riscv_hartid_to_cpuid(int hart)
75-
{
76-
#ifdef CONFIG_SMP
77-
return hart - CONFIG_ARCH_RV_HARTID_BASE;
78-
#else
79-
return 0;
80-
#endif
81-
}
82-
83-
/****************************************************************************
84-
* Name: riscv_cpuid_to_hartid
85-
*
86-
* Description:
87-
* Convert logical core number to physical core number. Default
88-
* implementation is 1:1 mapping, i.e. physical=logical.
89-
*
90-
****************************************************************************/
91-
92-
int weak_function riscv_cpuid_to_hartid(int cpu)
93-
{
94-
#ifdef CONFIG_SMP
95-
return cpu + CONFIG_ARCH_RV_HARTID_BASE;
96-
#else
97-
return (int)riscv_mhartid();
98-
#endif
99-
}

0 commit comments

Comments
 (0)