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

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

commit 047eca30ffa9b6715877a5de9ea130ada0477223
Author: ligd <[email protected]>
AuthorDate: Tue May 27 11:47:50 2025 +0800

    cache: add l2cache support for arm-M
    
    add l2cache support for arm-M
    
    Signed-off-by: ligd <[email protected]>
---
 arch/arm/src/armv7-m/arm_cache.c |  29 ++++-
 arch/arm/src/armv7-m/l2cc.h      | 257 +++++++++++++++++++++++++++++++++++++++
 arch/arm/src/armv8-m/arm_cache.c |  29 ++++-
 arch/arm/src/armv8-m/l2cc.h      | 257 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 564 insertions(+), 8 deletions(-)

diff --git a/arch/arm/src/armv7-m/arm_cache.c b/arch/arm/src/armv7-m/arm_cache.c
index c9894f96543..a37096d1f95 100644
--- a/arch/arm/src/armv7-m/arm_cache.c
+++ b/arch/arm/src/armv7-m/arm_cache.c
@@ -43,9 +43,11 @@
 #include <nuttx/config.h>
 #include <nuttx/cache.h>
 #include <arch/barriers.h>
+#include <sys/param.h>
 
 #include "arm_internal.h"
 #include "nvic.h"
+#include "l2cc.h"
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -216,7 +218,7 @@ size_t up_get_icache_linesize(void)
 
   if (clsize == 0)
     {
-      clsize = up_get_cache_linesize(true);
+      clsize = MAX(up_get_cache_linesize(true), l2cc_linesize());
     }
 
   return clsize;
@@ -242,7 +244,7 @@ size_t up_get_icache_size(void)
 
   if (csize == 0)
     {
-      csize = up_get_cache_size(true);
+      csize = MAX(up_get_cache_size(true), l2cc_size());
     }
 
   return csize;
@@ -428,7 +430,7 @@ size_t up_get_dcache_linesize(void)
 
   if (clsize == 0)
     {
-      clsize = up_get_cache_linesize(false);
+      clsize = MAX(up_get_cache_linesize(false), l2cc_linesize());
     }
 
   return clsize;
@@ -454,7 +456,7 @@ size_t up_get_dcache_size(void)
 
   if (csize == 0)
     {
-      csize = up_get_cache_size(false);
+      csize = MAX(up_get_cache_size(false), l2cc_size());
     }
 
   return csize;
@@ -545,6 +547,8 @@ void up_enable_dcache(void)
   putreg32(ccr, NVIC_CFGCON);
 
   UP_MB();
+
+  l2cc_enable();
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -617,6 +621,8 @@ void up_disable_dcache(void)
   while (sets--);
 
   UP_MB();
+
+  l2cc_disable();
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -647,6 +653,7 @@ void up_disable_dcache(void)
 #ifdef CONFIG_ARMV7M_DCACHE
 void up_invalidate_dcache(uintptr_t start, uintptr_t end)
 {
+  uintptr_t start_ = start;
   uint32_t ssize = up_get_dcache_linesize();
 
   /* Invalidate the D-Cache containing this range of addresses
@@ -687,6 +694,8 @@ void up_invalidate_dcache(uintptr_t start, uintptr_t end)
     }
 
   UP_MB();
+
+  l2cc_invalidate(start_, end);
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -752,6 +761,8 @@ void up_invalidate_dcache_all(void)
   while (sets--);
 
   UP_MB();
+
+  l2cc_invalidate_all();
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -782,6 +793,7 @@ void up_invalidate_dcache_all(void)
 #ifdef CONFIG_ARMV7M_DCACHE
 void up_clean_dcache(uintptr_t start, uintptr_t end)
 {
+  uintptr_t start_ = start;
 #ifndef CONFIG_ARMV7M_DCACHE_WRITETHROUGH
   uint32_t ccsidr;
   uint32_t sshift;
@@ -829,6 +841,8 @@ void up_clean_dcache(uintptr_t start, uintptr_t end)
 #endif /* !CONFIG_ARMV7M_DCACHE_WRITETHROUGH */
 
   UP_MB();
+
+  l2cc_clean(start_, end);
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -905,6 +919,8 @@ void up_clean_dcache_all(void)
 #endif /* !CONFIG_ARMV7M_DCACHE_WRITETHROUGH */
 
   UP_MB();
+
+  l2cc_clean_all();
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -935,6 +951,7 @@ void up_clean_dcache_all(void)
 #ifdef CONFIG_ARMV7M_DCACHE
 void up_flush_dcache(uintptr_t start, uintptr_t end)
 {
+  uintptr_t start_ = start;
 #ifndef CONFIG_ARMV7M_DCACHE_WRITETHROUGH
   uint32_t ccsidr;
   uint32_t sshift;
@@ -984,6 +1001,8 @@ void up_flush_dcache(uintptr_t start, uintptr_t end)
 #else
   up_invalidate_dcache(start, end);
 #endif /* !CONFIG_ARMV7M_DCACHE_WRITETHROUGH */
+
+  l2cc_flush(start_, end);
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
@@ -1061,6 +1080,8 @@ void up_flush_dcache_all(void)
 #else
   up_invalidate_dcache_all();
 #endif /* !CONFIG_ARMV7M_DCACHE_WRITETHROUGH */
+
+  l2cc_flush_all();
 }
 #endif /* CONFIG_ARMV7M_DCACHE */
 
diff --git a/arch/arm/src/armv7-m/l2cc.h b/arch/arm/src/armv7-m/l2cc.h
new file mode 100644
index 00000000000..f9c0e9b4867
--- /dev/null
+++ b/arch/arm/src/armv7-m/l2cc.h
@@ -0,0 +1,257 @@
+/****************************************************************************
+ * arch/arm/src/armv7-m/l2cc.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_ARM_SRC_ARMV7_M_L2CC_H
+#define __ARCH_ARM_SRC_ARMV7_M_L2CC_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_ARCH_L2CACHE
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifndef __ASSEMBLY__
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: l2cc_linesize
+ *
+ * Description:
+ *    Get L2 cache linesize
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    L2 cache linesize
+ *
+ ****************************************************************************/
+
+uint32_t l2cc_linesize(void);
+
+/****************************************************************************
+ * Name: l2cc_size
+ *
+ * Description:
+ *    Get L2CC-P310 L2 cache size
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    L2 cache size
+ *
+ ****************************************************************************/
+
+uint32_t l2cc_size(void);
+
+/****************************************************************************
+ * Name: l2cc_enable
+ *
+ * Description:
+ *    Re-enable the L2CC-P310 L2 cache by setting the enable bit in the
+ *    Control Register (CR)
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    None
+ *
+ ****************************************************************************/
+
+void l2cc_enable(void);
+
+/****************************************************************************
+ * Name: l2cc_disable
+ *
+ * Description:
+ *    Disable the L2 cache
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    None
+ *
+ ****************************************************************************/
+
+void l2cc_disable(void);
+
+/****************************************************************************
+ * Name: l2cc_sync
+ *
+ * Description:
+ *   Drain the L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_sync(void);
+
+/****************************************************************************
+ * Name: l2cc_invalidate_all
+ *
+ * Description:
+ *   Invalidate the entire L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_invalidate_all(void);
+
+/****************************************************************************
+ * Name: l2cc_invalidate
+ *
+ * Description:
+ *   Invalidate a range of addresses in the L2 cache
+ *
+ * Input Parameters:
+ *   startaddr - The first address to be invalidated
+ *   endaddr   - The last address to be invalidated
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_invalidate(uintptr_t startaddr, uintptr_t endaddr);
+
+/****************************************************************************
+ * Name: l2cc_clean_all
+ *
+ * Description:
+ *   Clean the entire L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_clean_all(void);
+
+/****************************************************************************
+ * Name: l2cc_clean
+ *
+ * Description:
+ *   Clean a range of address within the L2 cache.
+ *
+ * Input Parameters:
+ *   startaddr - The first address to be cleaned
+ *   endaddr   - The last address to be cleaned
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_clean(uintptr_t startaddr, uintptr_t endaddr);
+
+/****************************************************************************
+ * Name: l2cc_flush_all
+ *
+ * Description:
+ *   Flush the entire L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_flush_all(void);
+
+/****************************************************************************
+ * Name: l2cc_flush
+ *
+ * Description:
+ *   Flush a range of address within the L2 cache.
+ *
+ * Input Parameters:
+ *   startaddr - The first address to be flushed
+ *   endaddr   - The last address to be flushed
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_flush(uint32_t startaddr, uint32_t endaddr);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+#endif /* __ASSEMBLY__ */
+
+#else /* CONFIG_ARCH_L2CACHE */
+  /* Provide simple definitions to concentrate the inline conditional
+   * compilation in one place.
+   */
+
+#  define l2cc_size() 0
+#  define l2cc_linesize() 0
+#  define l2cc_enable()
+#  define l2cc_disable()
+#  define l2cc_sync()
+#  define l2cc_invalidate_all()
+#  define l2cc_invalidate(s,e)
+#  define l2cc_clean_all()
+#  define l2cc_clean(s,e)
+#  define l2cc_flush_all()
+#  define l2cc_flush(s,e)
+
+#endif /* CONFIG_ARCH_L2CACHE */
+#endif /* __ARCH_ARM_SRC_ARMV7_M_L2CC_H */
diff --git a/arch/arm/src/armv8-m/arm_cache.c b/arch/arm/src/armv8-m/arm_cache.c
index 4b723cccc22..1e7da3b161e 100644
--- a/arch/arm/src/armv8-m/arm_cache.c
+++ b/arch/arm/src/armv8-m/arm_cache.c
@@ -43,9 +43,11 @@
 #include <nuttx/config.h>
 #include <nuttx/cache.h>
 #include <arch/barriers.h>
+#include <sys/param.h>
 
 #include "arm_internal.h"
 #include "nvic.h"
+#include "l2cc.h"
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -216,7 +218,7 @@ size_t up_get_icache_linesize(void)
 
   if (clsize == 0)
     {
-      clsize = up_get_cache_linesize(true);
+      clsize = MAX(up_get_cache_linesize(true), l2cc_linesize());
     }
 
   return clsize;
@@ -242,7 +244,7 @@ size_t up_get_icache_size(void)
 
   if (csize == 0)
     {
-      csize = up_get_cache_size(true);
+      csize = MAX(up_get_cache_size(true), l2cc_size());
     }
 
   return csize;
@@ -428,7 +430,7 @@ size_t up_get_dcache_linesize(void)
 
   if (clsize == 0)
     {
-      clsize = up_get_cache_linesize(false);
+      clsize = MAX(up_get_cache_linesize(false), l2cc_linesize());
     }
 
   return clsize;
@@ -454,7 +456,7 @@ size_t up_get_dcache_size(void)
 
   if (csize == 0)
     {
-      csize = up_get_cache_size(false);
+      csize = MAX(up_get_cache_size(false), l2cc_size());
     }
 
   return csize;
@@ -545,6 +547,8 @@ void up_enable_dcache(void)
   putreg32(ccr, NVIC_CFGCON);
 
   UP_MB();
+
+  l2cc_enable();
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -617,6 +621,8 @@ void up_disable_dcache(void)
   while (sets--);
 
   UP_MB();
+
+  l2cc_disable();
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -647,6 +653,7 @@ void up_disable_dcache(void)
 #ifdef CONFIG_ARMV8M_DCACHE
 void up_invalidate_dcache(uintptr_t start, uintptr_t end)
 {
+  uintptr_t start_ = start;
   uint32_t ssize = up_get_dcache_linesize();
 
   /* Invalidate the D-Cache containing this range of addresses
@@ -687,6 +694,8 @@ void up_invalidate_dcache(uintptr_t start, uintptr_t end)
     }
 
   UP_MB();
+
+  l2cc_invalidate(start_, end);
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -752,6 +761,8 @@ void up_invalidate_dcache_all(void)
   while (sets--);
 
   UP_MB();
+
+  l2cc_invalidate_all();
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -782,6 +793,7 @@ void up_invalidate_dcache_all(void)
 #ifdef CONFIG_ARMV8M_DCACHE
 void up_clean_dcache(uintptr_t start, uintptr_t end)
 {
+  uintptr_t start_ = start;
 #ifndef CONFIG_ARMV8M_DCACHE_WRITETHROUGH
   uint32_t ccsidr;
   uint32_t sshift;
@@ -829,6 +841,8 @@ void up_clean_dcache(uintptr_t start, uintptr_t end)
 #endif /* !CONFIG_ARMV8M_DCACHE_WRITETHROUGH */
 
   UP_MB();
+
+  l2cc_clean(start_, end);
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -905,6 +919,8 @@ void up_clean_dcache_all(void)
 #endif /* !CONFIG_ARMV8M_DCACHE_WRITETHROUGH */
 
   UP_MB();
+
+  l2cc_clean_all();
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -935,6 +951,7 @@ void up_clean_dcache_all(void)
 #ifdef CONFIG_ARMV8M_DCACHE
 void up_flush_dcache(uintptr_t start, uintptr_t end)
 {
+  uintptr_t start_ = start;
 #ifndef CONFIG_ARMV8M_DCACHE_WRITETHROUGH
   uint32_t ccsidr;
   uint32_t sshift;
@@ -984,6 +1001,8 @@ void up_flush_dcache(uintptr_t start, uintptr_t end)
 #else
   up_invalidate_dcache(start, end);
 #endif /* !CONFIG_ARMV8M_DCACHE_WRITETHROUGH */
+
+  l2cc_flush(start_, end);
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
@@ -1061,6 +1080,8 @@ void up_flush_dcache_all(void)
 #else
   up_invalidate_dcache_all();
 #endif /* !CONFIG_ARMV8M_DCACHE_WRITETHROUGH */
+
+  l2cc_flush_all();
 }
 #endif /* CONFIG_ARMV8M_DCACHE */
 
diff --git a/arch/arm/src/armv8-m/l2cc.h b/arch/arm/src/armv8-m/l2cc.h
new file mode 100644
index 00000000000..cd56a43b271
--- /dev/null
+++ b/arch/arm/src/armv8-m/l2cc.h
@@ -0,0 +1,257 @@
+/****************************************************************************
+ * arch/arm/src/armv8-m/l2cc.h
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_ARM_SRC_ARMV8_M_L2CC_H
+#define __ARCH_ARM_SRC_ARMV8_M_L2CC_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#ifdef CONFIG_ARCH_L2CACHE
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifndef __ASSEMBLY__
+#ifdef __cplusplus
+#define EXTERN extern "C"
+extern "C"
+{
+#else
+#define EXTERN extern
+#endif
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: l2cc_linesize
+ *
+ * Description:
+ *    Get L2 cache linesize
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    L2 cache linesize
+ *
+ ****************************************************************************/
+
+uint32_t l2cc_linesize(void);
+
+/****************************************************************************
+ * Name: l2cc_size
+ *
+ * Description:
+ *    Get L2CC-P310 L2 cache size
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    L2 cache size
+ *
+ ****************************************************************************/
+
+uint32_t l2cc_size(void);
+
+/****************************************************************************
+ * Name: l2cc_enable
+ *
+ * Description:
+ *    Re-enable the L2CC-P310 L2 cache by setting the enable bit in the
+ *    Control Register (CR)
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    None
+ *
+ ****************************************************************************/
+
+void l2cc_enable(void);
+
+/****************************************************************************
+ * Name: l2cc_disable
+ *
+ * Description:
+ *    Disable the L2 cache
+ *
+ * Input Parameters:
+ *    None
+ *
+ * Returned Value:
+ *    None
+ *
+ ****************************************************************************/
+
+void l2cc_disable(void);
+
+/****************************************************************************
+ * Name: l2cc_sync
+ *
+ * Description:
+ *   Drain the L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_sync(void);
+
+/****************************************************************************
+ * Name: l2cc_invalidate_all
+ *
+ * Description:
+ *   Invalidate the entire L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_invalidate_all(void);
+
+/****************************************************************************
+ * Name: l2cc_invalidate
+ *
+ * Description:
+ *   Invalidate a range of addresses in the L2 cache
+ *
+ * Input Parameters:
+ *   startaddr - The first address to be invalidated
+ *   endaddr   - The last address to be invalidated
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_invalidate(uintptr_t startaddr, uintptr_t endaddr);
+
+/****************************************************************************
+ * Name: l2cc_clean_all
+ *
+ * Description:
+ *   Clean the entire L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_clean_all(void);
+
+/****************************************************************************
+ * Name: l2cc_clean
+ *
+ * Description:
+ *   Clean a range of address within the L2 cache.
+ *
+ * Input Parameters:
+ *   startaddr - The first address to be cleaned
+ *   endaddr   - The last address to be cleaned
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_clean(uintptr_t startaddr, uintptr_t endaddr);
+
+/****************************************************************************
+ * Name: l2cc_flush_all
+ *
+ * Description:
+ *   Flush the entire L2 cache.
+ *
+ * Input Parameters:
+ *   None
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_flush_all(void);
+
+/****************************************************************************
+ * Name: l2cc_flush
+ *
+ * Description:
+ *   Flush a range of address within the L2 cache.
+ *
+ * Input Parameters:
+ *   startaddr - The first address to be flushed
+ *   endaddr   - The last address to be flushed
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void l2cc_flush(uint32_t startaddr, uint32_t endaddr);
+
+#undef EXTERN
+#ifdef __cplusplus
+}
+#endif
+#endif /* __ASSEMBLY__ */
+
+#else /* CONFIG_ARCH_L2CACHE */
+  /* Provide simple definitions to concentrate the inline conditional
+   * compilation in one place.
+   */
+
+#  define l2cc_size() 0
+#  define l2cc_linesize() 0
+#  define l2cc_enable()
+#  define l2cc_disable()
+#  define l2cc_sync()
+#  define l2cc_invalidate_all()
+#  define l2cc_invalidate(s,e)
+#  define l2cc_clean_all()
+#  define l2cc_clean(s,e)
+#  define l2cc_flush_all()
+#  define l2cc_flush(s,e)
+
+#endif /* CONFIG_ARCH_L2CACHE */
+#endif /* __ARCH_ARM_SRC_ARMV8_M_L2CC_H */

Reply via email to