Skip to content

Commit

Permalink
arm/cortex-a,r: replace cp15 instruct to macros to align operation
Browse files Browse the repository at this point in the history
This is continue work of apache#13486

Discussion here:
apache#13486 (comment)

1. move cp15.h to arch public
2. replace cp15 instruct to macros to align operation
3. add memory barrier to avoid compiler optimization

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao committed Sep 19, 2024
1 parent f6c1d88 commit 4a87f90
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* arch/arm/src/armv7-a/cp15.h
* arch/arm/include/armv7-a/cp15.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand Down
22 changes: 5 additions & 17 deletions arch/arm/include/armv7-a/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
# include <stdint.h>
#endif

#include "cp15.h"

/****************************************************************************
* Pre-processor Prototypes
****************************************************************************/
Expand Down Expand Up @@ -457,11 +459,7 @@ static inline_function int up_cpu_index(void)

/* Read the Multiprocessor Affinity Register (MPIDR) */

__asm__ __volatile__
(
"mrc " "p15, " "0" ", %0, " "c0" ", " "c0" ", " "5" "\n"
: "=r"(mpidr)
);
mpidr = CP15_GET(MPIDR);

/* And return the CPU ID field */

Expand Down Expand Up @@ -500,23 +498,13 @@ static inline_function uint32_t up_getsp(void)
noinstrument_function
static inline_function uint32_t *up_current_regs(void)
{
uint32_t *regs;
__asm__ __volatile__
(
"mrc " "p15, " "0" ", %0, " "c13" ", " "c0" ", " "4" "\n"
: "=r"(regs)
);
return regs;
return (uint32_t *)CP15_GET(TPIDRPRW);
}

noinstrument_function
static inline_function void up_set_current_regs(uint32_t *regs)
{
__asm__ __volatile__
(
"mcr " "p15, " "0" ", %0, " "c13" ", " "c0" ", " "4" "\n"
:: "r"(regs)
);
CP15_SET(TPIDRPRW, regs);
}

noinstrument_function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* arch/arm/src/armv7-r/cp15.h
* arch/arm/include/armv7-r/cp15.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand Down
39 changes: 20 additions & 19 deletions arch/arm/include/armv7-r/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
# include <stdint.h>
#endif

#include "cp15.h"

/****************************************************************************
* Pre-processor Prototypes
****************************************************************************/
Expand Down Expand Up @@ -357,7 +359,7 @@ static inline irqstate_t irqstate(void)

/* Disable IRQs and return the previous IRQ state */

static inline irqstate_t up_irq_save(void)
noinstrument_function static inline irqstate_t up_irq_save(void)
{
unsigned int cpsr;

Expand Down Expand Up @@ -417,7 +419,7 @@ static inline irqstate_t up_irq_disable(void)

/* Restore saved IRQ & FIQ state */

static inline void up_irq_restore(irqstate_t flags)
noinstrument_function static inline void up_irq_restore(irqstate_t flags)
{
__asm__ __volatile__
(
Expand Down Expand Up @@ -452,11 +454,7 @@ static inline_function int up_cpu_index(void)

/* Read the Multiprocessor Affinity Register (MPIDR) */

__asm__ __volatile__
(
"mrc " "p15, " "0" ", %0, " "c0" ", " "c0" ", " "5" "\n"
: "=r"(mpidr)
);
mpidr = CP15_GET(MPIDR);

/* And return the CPU ID field */

Expand All @@ -479,26 +477,29 @@ static inline_function uint32_t up_getsp(void)
return sp;
}

/****************************************************************************
* Name:
* up_current_regs/up_set_current_regs
*
* Description:
* We use the following code to manipulate the TPIDRPRW register,
* which exists uniquely for each CPU and is primarily designed to store
* current thread information. Currently, we leverage it to store interrupt
* information, with plans to further optimize its use for storing both
* thread and interrupt information in the future.
*
****************************************************************************/

noinstrument_function
static inline_function uint32_t *up_current_regs(void)
{
uint32_t *regs;
__asm__ __volatile__
(
"mrc " "p15, " "0" ", %0, " "c13" ", " "c0" ", " "4" "\n"
: "=r"(regs)
);
return regs;
return (uint32_t *)CP15_GET(TPIDRPRW);
}

noinstrument_function
static inline_function void up_set_current_regs(uint32_t *regs)
{
__asm__ __volatile__
(
"mcr " "p15, " "0" ", %0, " "c13" ", " "c0" ", " "4" "\n"
:: "r"(regs)
);
CP15_SET(TPIDRPRW, regs);
}

noinstrument_function
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* arch/arm/src/armv8-r/cp15.h
* arch/arm/include/armv8-r/cp15.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
Expand Down
39 changes: 20 additions & 19 deletions arch/arm/include/armv8-r/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
# include <stdint.h>
#endif

#include "cp15.h"

/****************************************************************************
* Pre-processor Prototypes
****************************************************************************/
Expand Down Expand Up @@ -357,7 +359,7 @@ static inline irqstate_t irqstate(void)

/* Disable IRQs and return the previous IRQ state */

static inline irqstate_t up_irq_save(void)
noinstrument_function static inline irqstate_t up_irq_save(void)
{
unsigned int cpsr;

Expand Down Expand Up @@ -417,7 +419,7 @@ static inline irqstate_t up_irq_disable(void)

/* Restore saved IRQ & FIQ state */

static inline void up_irq_restore(irqstate_t flags)
noinstrument_function static inline void up_irq_restore(irqstate_t flags)
{
__asm__ __volatile__
(
Expand Down Expand Up @@ -452,11 +454,7 @@ static inline_function int up_cpu_index(void)

/* Read the Multiprocessor Affinity Register (MPIDR) */

__asm__ __volatile__
(
"mrc " "p15, " "0" ", %0, " "c0" ", " "c0" ", " "5" "\n"
: "=r"(mpidr)
);
mpidr = CP15_GET(MPIDR);

/* And return the CPU ID field */

Expand All @@ -479,26 +477,29 @@ static inline_function uint32_t up_getsp(void)
return sp;
}

/****************************************************************************
* Name:
* up_current_regs/up_set_current_regs
*
* Description:
* We use the following code to manipulate the TPIDRPRW register,
* which exists uniquely for each CPU and is primarily designed to store
* current thread information. Currently, we leverage it to store interrupt
* information, with plans to further optimize its use for storing both
* thread and interrupt information in the future.
*
****************************************************************************/

noinstrument_function
static inline_function uint32_t *up_current_regs(void)
{
uint32_t *regs;
__asm__ __volatile__
(
"mrc " "p15, " "0" ", %0, " "c13" ", " "c0" ", " "4" "\n"
: "=r"(regs)
);
return regs;
return (uint32_t *)CP15_GET(TPIDRPRW);
}

noinstrument_function
static inline_function void up_set_current_regs(uint32_t *regs)
{
__asm__ __volatile__
(
"mcr " "p15, " "0" ", %0, " "c13" ", " "c0" ", " "4" "\n"
:: "r"(regs)
);
CP15_SET(TPIDRPRW, regs);
}

noinstrument_function
Expand Down
11 changes: 7 additions & 4 deletions arch/arm/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ add_subdirectory(common)

# Include directories (before system ones) as PUBLIC so that it can be exposed
# to libboard
target_include_directories(arch BEFORE PUBLIC ${NUTTX_CHIP_ABS_DIR} common
${ARCH_SUBDIR})
target_include_directories(
arch BEFORE PUBLIC ${NUTTX_CHIP_ABS_DIR} common ${ARCH_SUBDIR}
${CMAKE_CURRENT_LIST_DIR}/../include/${CONFIG_ARCH_FAMILY})

if(NOT CONFIG_BUILD_FLAT)
target_include_directories(arch_interface BEFORE PUBLIC ${NUTTX_CHIP_ABS_DIR}
common ${ARCH_SUBDIR})
target_include_directories(
arch_interface BEFORE
PUBLIC ${NUTTX_CHIP_ABS_DIR} common ${ARCH_SUBDIR}
${CMAKE_CURRENT_LIST_DIR}/../include/${CONFIG_ARCH_FAMILY})
endif()
9 changes: 5 additions & 4 deletions arch/arm/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ else # ARM9, ARM7TDMI, etc.
ARCH_SUBDIR = arm
endif

ARCH_SRCDIR = $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src
ARCH_SRCDIR = $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)

INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)chip
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)common
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)$(ARCH_SUBDIR)
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)src$(DELIM)chip
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)src$(DELIM)common
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)src$(DELIM)$(ARCH_SUBDIR)
INCLUDES += ${INCDIR_PREFIX}$(ARCH_SRCDIR)$(DELIM)include$(DELIM)$(ARCH_SUBDIR)
INCLUDES += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)sched

CPPFLAGS += $(INCLUDES)
Expand Down

0 comments on commit 4a87f90

Please sign in to comment.