Skip to content

CMSIS: share L1 cache maintenance support across A and R profiles - #319

Open
hamtinepeng wants to merge 4 commits into
ARM-software:mainfrom
hamtinepeng:l1-cache
Open

CMSIS: share L1 cache maintenance support across A and R profiles#319
hamtinepeng wants to merge 4 commits into
ARM-software:mainfrom
hamtinepeng:l1-cache

Conversation

@hamtinepeng

Copy link
Copy Markdown
Contributor

Extract the common L1 cache maintenance implementation into l1_cache.h and reuse it for Armv7-A, Armv7-R and Armv8-R.

@JonatanAntoni
JonatanAntoni requested a lite review from Copilot September 3, 2026 13:50
@JonatanAntoni

Copy link
Copy Markdown
Member

@Masmiseim36

@JonatanAntoni

JonatanAntoni commented Sep 3, 2026

Copy link
Copy Markdown
Member

Shall we add some checks to Core tests for the new/changed features?
In fact, L1 cache functions for A-class are tested in CoreValidation https://github.com/ARM-software/CMSIS_6/blob/main/CMSIS/CoreValidation/Source/CV_CAL1Cache.c.
CoreValidation require a fast model for execution which we don't have available for R-class currently. We could invest in running tests using qemu instead of fast models to cover R-class (R5, R52) as well. But we may be able to check for correct compiler output in the static Core tests.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new shared cache helper header contains a functional bug in __log2_up(), and the R-profile CP15 headers include an AArch64-only header that is likely to break AArch32 builds.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR refactors CMSIS Core cache-maintenance support by extracting the common L1 cache maintenance helpers into a shared header and reusing it across Armv7-A, Armv7-R, and Armv8-R profile headers.

Changes:

  • Added CMSIS/Core/Include/a-profile/l1_cache.h to host shared L1 cache enable/disable and cache maintenance helpers.
  • Updated a-profile/armv7a.h to include the shared l1_cache.h instead of defining the helpers inline.
  • Updated R-profile headers (armv7r.h, armv8r.h) and CP15 glue headers to pull in the shared cache-maintenance implementation and required CP15 interfaces.
File summaries
File Description
CMSIS/Core/Include/r-profile/armv8r.h Includes shared L1 cache maintenance header for Armv8-R profile.
CMSIS/Core/Include/r-profile/armv8r_cp15.h Adjusts CP15 include wiring for Armv8-R (but currently pulls in an AArch64-only header).
CMSIS/Core/Include/r-profile/armv7r.h Includes shared L1 cache maintenance header for Armv7-R profile.
CMSIS/Core/Include/r-profile/armv7r_cp15.h Adjusts CP15 include wiring for Armv7-R (but currently pulls in an AArch64-only header).
CMSIS/Core/Include/a-profile/l1_cache.h New shared header containing L1 cache maintenance helpers reused across profiles.
CMSIS/Core/Include/a-profile/armv7a.h Switches to including the new shared L1 cache maintenance header.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +95 to +109
__STATIC_FORCEINLINE uint8_t __log2_up(uint32_t n)
{
if (n < 2U) {
return 0U;
}
uint8_t log = 0U;
uint32_t t = n;
while(t > 1U)
{
log++;
t >>= 1U;
}
if (n & 1U) { log++; }
return log;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving code did not introduce this issue. I already have un upcoming patch to move variable declarations before statements, this issue could be addressed later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am logging this issue for later analysis and resolution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets fix this issue in a separate PR.

Comment thread CMSIS/Core/Include/r-profile/armv7r_cp15.h Outdated
Comment on lines +20 to +22
// The gic-interface is located in the profile folder for cortex-a devices
#include "../a-profile/armv8a_cp15.h" No newline at end of file
#include "../a-profile/armv7a_cp15.h"
#include "../a-profile/armv8a_cp15.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only include ../a-profile/armv8a_cp15.h

Comment thread CMSIS/Core/Include/a-profile/l1_cache.h Outdated
Comment on lines +40 to +42
/** \brief Clean instruction cache line by address.
* \param [in] va Pointer to instructions to clear the cache for.
*/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 6b19157

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Test Results

   256 files   -    388     256 suites   - 388   0s ⏱️ - 16m 1s
    56 tests +     7      54 ✅ +   10      2 💤  -     3  0 ❌ ±0 
13 860 runs   - 17 696  11 476 ✅  - 8 680  2 384 💤  - 9 016  0 ❌ ±0 

Results for commit 93636ac. ± Comparison against base commit 1c1840a.

This pull request removes 49 and adds 56 tests. Note that renamed tests count towards both.
CMSIS-Core.src ‑ apsr.c
CMSIS-Core.src ‑ basepri.c
CMSIS-Core.src ‑ bkpt.c
CMSIS-Core.src ‑ clrex.c
CMSIS-Core.src ‑ clz.c
CMSIS-Core.src ‑ control.c
CMSIS-Core.src ‑ cp15.c
CMSIS-Core.src ‑ cpsr.c
CMSIS-Core.src ‑ dmb.c
CMSIS-Core.src ‑ dsb.c
…
TC_CML1Cache_CleanDCacheByAddrWhileDisabled
TC_CML1Cache_EnDisableDCache
TC_CML1Cache_EnDisableICache
TC_CoreFunc_APSR
TC_CoreFunc_BASEPRI
TC_CoreFunc_Control
TC_CoreFunc_EnDisIRQ
TC_CoreFunc_EncDecIRQPrio
TC_CoreFunc_FAULTMASK
TC_CoreFunc_FPSCR
…
This pull request removes 5 skipped tests and adds 2 skipped tests. Note that renamed tests count towards both.
CMSIS-Core.src ‑ lda.c
CMSIS-Core.src ‑ ldaex.c
CMSIS-Core.src ‑ stl.c
CMSIS-Core.src ‑ stlex.c
CMSIS-Core.src ‑ systick.c
TC_CoreInstr_WFE
TC_CoreInstr_WFI

♻️ This comment has been updated with latest results.

Extract the common L1 cache maintenance implementation into l1_cache.h
and reuse it for Armv7-A, Armv7-R and Armv8-R.
Comment thread CMSIS/Core/Include/r-profile/armv7r.h Outdated
#include "gicv2.h"
#endif /* (__GIC_PRESENT == 1U) || defined(DOXYGEN) */

#include "../a-profile/l1_cache.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For gicv2 we placed this include into a separate file, because including A-profile code directly from the R-profile header might be confusing. This has no functional impact.

@hamtinepeng hamtinepeng Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed that's clearer d12fd55

JonatanAntoni
JonatanAntoni previously approved these changes Sep 3, 2026
Comment thread CMSIS/Core/Include/r-profile/armv8r.h Outdated
#define SCTLR_M_Pos 0U /*!< \brief SCTLR: M Position */
#define SCTLR_M_Msk (1UL << SCTLR_M_Pos) /*!< \brief SCTLR: M Mask */

#include "../a-profile/l1_cache.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not all members of the ARMv8R family require this driver, we should be able to disable the include using a switch. For example:

#if (defined(__L1C_PRESENT) && (__L1C_PRESENT == 1U)) || defined(DOXYGEN)
#include "../a-profile/l1_cache.h"
#endif

@hamtinepeng hamtinepeng Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Cortex‑R82 support is added to CMSIS in the future, __L1C_PRESENT will likely be enabled. Therefore, this condition alone is not sufficient to determine whether the CP15-based cache maintenance implementation can be used. Cortex‑R82 relies on the AArch64 system register interface rather than CP15 accesses.
This option with the aarch64 flag could solve this issue.

Comment on lines +20 to +22
// The gic-interface is located in the profile folder for cortex-a devices
#include "../a-profile/armv8a_cp15.h" No newline at end of file
#include "../a-profile/armv7a_cp15.h"
#include "../a-profile/armv8a_cp15.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only include ../a-profile/armv8a_cp15.h

#include "../a-profile/armv8a_cp15.h" No newline at end of file
// The cp15-interface is located in the profile folder for cortex-a devices
#include "../a-profile/armv7a_cp15.h"
#include "../a-profile/armv8a_cp15.h"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Masmiseim36 If I understand correctly, the SCTLR accessor for instance should use the following instruction for Cortex-R4/R5/R7/R8/R52/R52+/R82 when running in aarch32 state:
__asm volatile ("mrc p15, 0, %0, c1, c0, 0" : "=r" (sctlr)); as implemented in armv7a_cp15.h.
For Cortex-R82 running in aarch64 state, the equivalent would be:
__asm volatile ("mrs %0, SCTLR_EL1" : "=r" (sctlr)); as implemented in armv8a_cp15.h.

If that's correct, it seems that the current implementation may be a bit misleading, as the existing accessors do not appear to provide the appropriate SCTLR access mechanism for the aarch32 state.

What approach would you recommend? I can think of a few options, although there may well be better alternatives:

  • Make the header selection dependent on the execution state (AArch32 vs AArch64).
  • Duplicate armv7a_cp15.h for now, since Cortex-R82 support is not yet available.
  • Expose all implementations and select the appropriate accessor based on the execution state or target architecture.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants