From 2b452f96f51a56980ac2bbbc7c51a3a9d0c767e4 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Mon, 29 Jun 2026 14:17:33 +0200 Subject: [PATCH] hw/mpu: Add link table configuration of MPU This adds way to configure MPU (v7) using link tables. User code will just have to declare MPU etrny in any place to add region to MPU. Signed-off-by: Jerzy Kasenberg --- hw/cmsis-core/include/arm_mpu.h | 45 ++++++++++++++++++++++++++++ hw/cmsis-core/pkg.yml | 3 ++ hw/cmsis-core/src/arm_mpu.c | 53 +++++++++++++++++++++++++++++++++ hw/cmsis-core/syscfg.yml | 23 ++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 hw/cmsis-core/include/arm_mpu.h create mode 100644 hw/cmsis-core/src/arm_mpu.c create mode 100644 hw/cmsis-core/syscfg.yml diff --git a/hw/cmsis-core/include/arm_mpu.h b/hw/cmsis-core/include/arm_mpu.h new file mode 100644 index 0000000000..e8d5061989 --- /dev/null +++ b/hw/cmsis-core/include/arm_mpu.h @@ -0,0 +1,45 @@ +/* + * 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 ARM_MPU_H +#define ARM_MPU_H + +#include +#include + +#if __MPU_PRESENT +#if (__CORTEX_M == 0) || (__CORTEX_M == 3) || (__CORTEX_M == 4) || (__CORTEX_M == 7) + +LINK_TABLE(ARM_MPU_Region_t, mpu_regions); + +#define MPU_ARM_V7_REGION(name, address, rasr) \ + LINK_TABLE_ELEMENT(mpu_regions, name) = { \ + .RBAR = address | MPU_RBAR_VALID_Msk, \ + .RASR = rasr, \ + }; + +#elif (__CORTEX_M == 23) || (__CORTEX_M == 33) || (__CORTEX_M == 34) || \ + (__CORTEX_M == 35) +/* TODO: Add support for MPU ARMV8 */ +#endif + +void arm_mpu_init(void); + +#endif +#endif diff --git a/hw/cmsis-core/pkg.yml b/hw/cmsis-core/pkg.yml index c5e004bb54..0fcf4dbf41 100644 --- a/hw/cmsis-core/pkg.yml +++ b/hw/cmsis-core/pkg.yml @@ -31,6 +31,9 @@ pkg.include_dirs: - "@arm-CMSIS_5/CMSIS/Core/Include" - "@arm-CMSIS_5/CMSIS/DSP/Include" +pkg.link_tables: + - mpu_regions + repository.arm-CMSIS_5: type: github vers: 5.9.0-commit diff --git a/hw/cmsis-core/src/arm_mpu.c b/hw/cmsis-core/src/arm_mpu.c new file mode 100644 index 0000000000..8a064f22ca --- /dev/null +++ b/hw/cmsis-core/src/arm_mpu.c @@ -0,0 +1,53 @@ +/* + * 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. + */ + +#include +#include +#include + +#ifndef __CORTEX_M +#error "Macro __CORTEX_M not defined; must define cortex-m type!" +#endif + +#if __MPU_PRESENT +#if (__CORTEX_M == 0) || (__CORTEX_M == 3) || (__CORTEX_M == 4) || (__CORTEX_M == 7) + +void +arm_mpu_init(void) +{ + ARM_MPU_Region_t regions[8] = { 0 }; + size_t size = min(ARRAY_SIZE(regions), mpu_regions_size()); + + ARM_MPU_Disable(); + + for (size_t i = 0; i < size; ++i) { + regions[i].RBAR = (mpu_regions[i].RBAR & ~MPU_RBAR_REGION_Msk) | i; + regions[i].RASR = mpu_regions[i].RASR; + } + + ARM_MPU_Load(regions, size); + + ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk); +} +#elif (__CORTEX_M == 23) || (__CORTEX_M == 33) || (__CORTEX_M == 34) || \ + (__CORTEX_M == 35) +/* TODO: Add support for MPU ARMV8 */ +#endif + +#endif diff --git a/hw/cmsis-core/syscfg.yml b/hw/cmsis-core/syscfg.yml new file mode 100644 index 0000000000..57fde89876 --- /dev/null +++ b/hw/cmsis-core/syscfg.yml @@ -0,0 +1,23 @@ +# 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. +# + +syscfg.defs: + ARM_MPU: + description: ARM MPU is present + value: +