Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soc: nxp: imxrt: user defined mpu regions #75604

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

GrixaYrev
Copy link
Contributor

@GrixaYrev GrixaYrev commented Jul 8, 2024

Option NXP_IMX_RT_USER_MPU_REGIONS exclude soc file mpu_regions.c and allow user define custom mpu_regions in custom file. Fixes #70920

For test this PR i use:

  • file prj.conf:
...
CONFIG_FLASH_SIZE=5120 
CONFIG_FLASH_BASE_ADDRESS=0x80100000
CONFIG_SRAM_SIZE=26624
CONFIG_SRAM_BASE_ADDRESS=0x80600000
...
  • file with custom MPU regions:
#include <zephyr/devicetree.h>
#include <zephyr/arch/arm/mpu/arm_mpu.h>

#define REGION_RAM_EXEC_ATTR(size) \
{ \
	(NORMAL_OUTER_INNER_WRITE_BACK_WRITE_READ_ALLOCATE_NON_SHAREABLE | \
	 size | P_RW_U_NA_Msk) \
}

static const struct arm_mpu_region mpu_regions[] = {
	/* Region 1 */
	MPU_REGION_ENTRY("SRAM_0",
			 CONFIG_SRAM_BASE_ADDRESS,
			 REGION_RAM_EXEC_ATTR(REGION_32M)),
};

const struct arm_mpu_config mpu_config = {
	.num_regions = ARRAY_SIZE(mpu_regions),
	.mpu_regions = mpu_regions,
};

Option NXP_IMX_RT_USER_MPU_REGIONS exclude soc file mpu_regions.c
and allow user define custom mpu_regions in custom file.
Fixes zephyrproject-rtos#70920

Signed-off-by: Grixa Yrev <[email protected]>
Copy link
Collaborator

@danieldegrasse danieldegrasse left a comment

Choose a reason for hiding this comment

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

While I understand the goal here, I'm not sure I agree with the approach. What prevents you from defining the MPU regions you need using the zephyr,memory-region compatible and zephyr,memory-attr property?

Is there a specific MPU region the IMX RT MPU regions file defines that you need removed?

@GrixaYrev
Copy link
Contributor Author

@danieldegrasse, main trouble is checking of CONFIG_SRAM_SIZE in file zephyr\include\zephyr\arch\arm\cortex_m\arm_mpu_mem_cfg.h (see issue #70920).
Also i think user should can setup mpu regions flexible, because their number is limited. We can not provide all possible memory configurations, so standard file should configure most common case, but user should can use custom file for special configuration.

@danieldegrasse
Copy link
Collaborator

@danieldegrasse, main trouble is checking of CONFIG_SRAM_SIZE in file zephyr\include\zephyr\arch\arm\cortex_m\arm_mpu_mem_cfg.h (see issue #70920). Also i think user should can setup mpu regions flexible, because their number is limited. We can not provide all possible memory configurations, so standard file should configure most common case, but user should can use custom file for special configuration.

Ok- thank you for clarifying. Looking at that issue, I guess you would like to use an SRAM size that is not a multiple supported by the ARM MPU? Perhaps we could solve this by rounding the MPU region size up to the nearest supported size? Or would this not work for your use case?

@GrixaYrev
Copy link
Contributor Author

@danieldegrasse, rounding could solve main trouble probably, but MPU will not work correctly in this case, because start address should be rounded to region size, if i am not wrong. Now i use no MPU regions (all SDRAM configured as executable, readable and writeable RAM), but i want to use it in future. So i want to find flexible solution for my issue.

@danieldegrasse
Copy link
Collaborator

@danieldegrasse, rounding could solve main trouble probably, but MPU will not work correctly in this case, because start address should be rounded to region size, if i am not wrong. Now i use no MPU regions (all SDRAM configured as executable, readable and writeable RAM), but i want to use it in future. So i want to find flexible solution for my issue.

So I think what I am unclear about is how removing the MPU definition file would solve your issue here. Either you don't use the MPU, or you enable the MPU and now you have some restrictions on the size/alignment of your SRAM region. Maybe it would help if you could provide an outline of a case where replacing the MPU file would allow you to accomplish something you currently cannot?

@GrixaYrev
Copy link
Contributor Author

GrixaYrev commented Sep 4, 2024

@danieldegrasse, I want to set follow memory layout in prj.conf:

CONFIG_FLASH_SIZE=5120 
CONFIG_FLASH_BASE_ADDRESS=0x80100000
CONFIG_SRAM_SIZE=26624
CONFIG_SRAM_BASE_ADDRESS=0x80600000

I have error "Unsupported sram size configuration" when i set CONFIG_SRAM_SIZE more than 16M and less than 32M.

I have this error, even i switch off MPU:

CONFIG_ARM_MPU=n
CONFIG_MPU=n

This error is described in zephyr\include\zephyr\arch\arm\cortex_m\arm_mpu_mem_cfg.h:72.

It is not MPU trouble, it is code trouble,

@danieldegrasse
Copy link
Collaborator

danieldegrasse commented Sep 13, 2024

@danieldegrasse, I want to set follow memory layout in prj.conf:

CONFIG_FLASH_SIZE=5120 
CONFIG_FLASH_BASE_ADDRESS=0x80100000
CONFIG_SRAM_SIZE=26624
CONFIG_SRAM_BASE_ADDRESS=0x80600000

I have error "Unsupported sram size configuration" when i set CONFIG_SRAM_SIZE more than 16M and less than 32M.

I have this error, even i switch off MPU:

CONFIG_ARM_MPU=n
CONFIG_MPU=n

This error is described in zephyr\include\zephyr\arch\arm\cortex_m\arm_mpu_mem_cfg.h:72.

It is not MPU trouble, it is code trouble,

Ok, thank you for this example, the issue is easier to follow now. So it seems like we just need to stop compiling the MPU regions file if CONFIG_ARM_MPU=n. Locally this change works for me, if I compile with -DCONFIG_SRAM_SIZE=26624 -DCONFIG_MPU=n -DCONFIG_ARM_MPU=n

diff --git a/soc/nxp/imxrt/CMakeLists.txt b/soc/nxp/imxrt/CMakeLists.txt
index ecdb894bfe3..09e90e87fa5 100644
--- a/soc/nxp/imxrt/CMakeLists.txt
+++ b/soc/nxp/imxrt/CMakeLists.txt
@@ -16,7 +16,7 @@ if(CONFIG_SOC_SERIES_IMXRT10XX OR CONFIG_SOC_SERIES_IMXRT11XX)
   if(CONFIG_EXTERNAL_MEM_CONFIG_DATA)
     set(boot_hdr_xmcd_data_section ".boot_hdr.xmcd_data")
   endif()
-  zephyr_sources(mpu_regions.c)
+  zephyr_sources_ifdef(CONFIG_ARM_MPU mpu_regions.c)
   zephyr_linker_section_configure(
     SECTION .rom_start
     INPUT ".boot_hdr.conf"

@GrixaYrev
Copy link
Contributor Author

I think i should add this change to commit, but it is restricted solution. I want to use MPU in future, and i will have this error again. So I suggest to join your change and my change.

@danieldegrasse
Copy link
Collaborator

I think i should add this change to commit, but it is restricted solution. I want to use MPU in future, and i will have this error again. So I suggest to join your change and my change.

I'm still unclear about the use case for the MPU on your side. Do you plan to only protect part of the region you use as SRAM? The reason I am hesitant to add a Kconfig to exclude the MPU file is because this MPU file effectively mirrors the one used for most ARM cores, plus an additional region. If you want to use an MPU in Zephyr on an ARMv7 platform, you will need SRAM regions aligned to the appropriate size boundaries. By adding a Kconfig to specifically exclude this for the iMX.RT series, it becomes possible to define MPU regions that don't fit the alignment requirements Zephyr MPU support is designed around on ARMv7. Is there a part of the Zephyr MPU support you are looking to use? If not, it truthfully seems to me like configuring the MPU directly and bypassing Zephyr is more logical.

@GrixaYrev
Copy link
Contributor Author

GrixaYrev commented Sep 19, 2024

I plan to use usermode. But i don't have enough experience in it. May be you are right and it is unavailable with this memory layout.
OK, i change this pr for your solution and will return for my variant when start to use usermode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

custom mpu regions for imxrt
4 participants