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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3214e11d4b6 armv7-r: add mpu_allocregions() and mpu_modify_regions()
3214e11d4b6 is described below

commit 3214e11d4b64ce68b8b49093e56d5daf0e32fa16
Author: anjiahao <[email protected]>
AuthorDate: Thu Nov 26 21:18:07 2026 +0800

    armv7-r: add mpu_allocregions() and mpu_modify_regions()
    
    mpu_configure_region() can only allocate and configure one region at
    a time.  Add an mpu_allocregions() variant to reserve several
    consecutive regions at once, keep mpu_allocregion() as a one-region
    macro, and add mpu_modify_regions() to configure a range that spans
    two adjacent MPU regions.
    
    Signed-off-by: anjiahao <[email protected]>
---
 arch/arm/src/armv7-r/arm_mpu.c | 70 ++++++++++++++++++++++++++++++++++++++----
 arch/arm/src/armv7-r/mpu.h     | 35 ++++++++++++++++++++-
 2 files changed, 98 insertions(+), 7 deletions(-)

diff --git a/arch/arm/src/armv7-r/arm_mpu.c b/arch/arm/src/armv7-r/arm_mpu.c
index 489ababd56b..19972056354 100644
--- a/arch/arm/src/armv7-r/arm_mpu.c
+++ b/arch/arm/src/armv7-r/arm_mpu.c
@@ -213,6 +213,7 @@ static void mpu_reset_internal(void)
 {
   int region;
   int regions;
+
   regions = (getreg32(MPU_TYPE) & MPU_TYPE_DREGION_MASK)
                                   >> MPU_TYPE_DREGION_SHIFT;
 
@@ -232,27 +233,28 @@ static void mpu_reset_internal(void)
  ****************************************************************************/
 
 /****************************************************************************
- * Name: mpu_allocregion
+ * Name: mpu_allocregions
  *
  * Description:
- *   Allocate the next region
+ *   Allocate the regions
  *
  * Input Parameters:
- *   None
+ *   nregions - The number of regions to be allocated.
  *
  * Returned Value:
  *   The index of the allocated region.
  *
  ****************************************************************************/
 
-unsigned int mpu_allocregion(void)
+unsigned int mpu_allocregions(unsigned int nregions)
 {
   unsigned int i = ffs(~g_mpu_region) - 1;
 
   /* There are not enough regions to apply */
 
-  DEBUGASSERT(i < CONFIG_ARM_MPU_NREGIONS);
-  g_mpu_region |= 1 << i;
+  DEBUGASSERT((i + nregions - 1) < CONFIG_ARM_MPU_NREGIONS);
+  DEBUGASSERT(((g_mpu_region >> i) & ((1 << nregions) - 1)) == 0);
+  g_mpu_region |= ((1 << nregions) - 1) << i;
   return i;
 }
 
@@ -475,10 +477,66 @@ unsigned int mpu_configure_region(uintptr_t base, size_t 
size,
                                   uint32_t flags)
 {
   unsigned int region = mpu_allocregion();
+
   mpu_modify_region(region, base, size, flags);
   return region;
 }
 
+/****************************************************************************
+ * Name: mpu_modify_regions
+ *
+ * Description:
+ *   Configure multi regions for privileged, strongly ordered memory
+ *
+ * Input Parameters:
+ *   region  - First region number to modify.
+ *   base    - Base address of the region.
+ *   size    - Size of the region.
+ *   flags   - Flags to configure the region.
+ *
+ ****************************************************************************/
+
+void mpu_modify_regions(unsigned int region, uintptr_t base, size_t size,
+                        uint32_t flags)
+{
+  uintptr_t    alignedbase;
+  uint8_t      l2size;
+  size_t       region1_size;
+  size_t       region2_size;
+
+  alignedbase  = base & MPU_RBAR_ADDR_MASK;
+  l2size       = mpu_log2regionceil(size + base - alignedbase);
+  alignedbase &= ~((1 << l2size) - 1);
+  l2size       = mpu_log2regionceil(size + base - alignedbase);
+  alignedbase &= ~((1 << l2size) - 1);
+
+  if (alignedbase + (1 << l2size) < base + size)
+    {
+      region1_size = alignedbase + (1 << l2size) - base;
+      region2_size = size - region1_size;
+    }
+  else
+    {
+      region1_size = size;
+      region2_size = 0;
+    }
+
+  mpu_modify_region(region, base, region1_size, flags);
+
+  if (region2_size != 0)
+    {
+      mpu_modify_region(region + 1, base + region1_size,
+                        region2_size, flags);
+    }
+  else
+    {
+      mpu_set_rgnr(region + 1);
+      mpu_set_drbar(0);
+      mpu_set_dracr(0);
+      mpu_set_drsr(0);
+    }
+}
+
 /****************************************************************************
  * Name: mpu_initialize
  *
diff --git a/arch/arm/src/armv7-r/mpu.h b/arch/arm/src/armv7-r/mpu.h
index 575bb336122..d6615b47345 100644
--- a/arch/arm/src/armv7-r/mpu.h
+++ b/arch/arm/src/armv7-r/mpu.h
@@ -177,6 +177,22 @@ void mpu_early_reset(void);
 #  define mpu_early_reset() do { } while (0)
 #endif
 
+/****************************************************************************
+ * Name: mpu_allocregions
+ *
+ * Description:
+ *   Allocate the regions
+ *
+ * Input Parameters:
+ *   nregions - The number of regions to be allocated.
+ *
+ * Returned Value:
+ *   The index of the allocated region.
+ *
+ ****************************************************************************/
+
+unsigned int mpu_allocregions(unsigned int nregions);
+
 /****************************************************************************
  * Name: mpu_allocregion
  *
@@ -191,7 +207,7 @@ void mpu_early_reset(void);
  *
  ****************************************************************************/
 
-unsigned int mpu_allocregion(void);
+#define mpu_allocregion() mpu_allocregions(1)
 
 /****************************************************************************
  * Name: mpu_freeregion
@@ -307,6 +323,23 @@ uint32_t mpu_subregion(uintptr_t base, size_t size, 
uint8_t l2size);
 void mpu_modify_region(unsigned int region, uintptr_t base, size_t size,
                        uint32_t flags);
 
+/****************************************************************************
+ * Name: mpu_modify_regions
+ *
+ * Description:
+ *   Configure multi regions for privileged, strongly ordered memory
+ *
+ * Input Parameters:
+ *   region  - First region number to modify.
+ *   base    - Base address of the region.
+ *   size    - Size of the region.
+ *   flags   - Flags to configure the region.
+ *
+ ****************************************************************************/
+
+void mpu_modify_regions(unsigned int region, uintptr_t base, size_t size,
+                        uint32_t flags);
+
 /****************************************************************************
  * Name: mpu_configure_region
  *

Reply via email to