How to configure a 256MB DDR mapping from STM32 MPU Distribution Package

Revision as of 11:12, 15 February 2023 by Registered User (→‎Introduction)
Applicable for STM32MP13x lines

1. Purpose of article[edit source]

This article describes how to update the STM32 MPU embedded Software distribution to a specific 256Mo DDR mapping in a STM32MP13x based product.
Note that the 256Mo software configuration can be performed on a STM32MP13 DK board (512Mo of DDR), in order to prototype the new mapping memory, but for this exercise, further software updates are requested , there are highlight in the article with the label (DDR512Mo).

2. Pre-requesites[edit source]

You are already familiar with the Yocto build process and OpenSTLinux distribution.

3. Introduction[edit source]

This article is a guideline for the user to update the STM32 MPU embedded Software in a distribution package of the OpenSTLinux distribution, according to his 256Mo hardware configuration.

The process is based on a OpenSTLinux configured for the STM32MP13 DK board (512Mo of DDR), with the following DDR mapping.

The default mapping defined for The OpenSTLinux on the STM32MP13 DK board for a st-image-weston image is :

0xe0000000  | (End of DDR 512Mo ) 
0xde200000  | (DDR_SEC_BASE/CFG_TZDRAM_START 
            | size DDR_SEC_SIZE/CFG_TZDRAM_SIZE (30Mo) protected by TZC 400, 
            | Begin of OP-TEE mapping, 
            | Begin of DDR encrypted DDRMCE, size 30Mo
            | End of SHMEM)
0xde000000  | (DDR_SHARE_BASE/CFG_SHMEM_START
            | size DDR_SHARE_SIZE/CFG_SHMEM_SIZE (2Mo no secure),
            | Begin of reserved memory optee size 32Mo)
0xdd000000  | (Begin of optee-framebuffer protected access by TZC 400 size 16Mo)
0xdc800000  | (End of Linux CMA reserved)
0xd4800000  | (Begin of Linux CMA reserved size CONFIG_CMA_SIZE_MBYTES 128Mo)
0xd0000000  | (End of DDR cacheable in U-Boot)
0xc0000000  | (STM32MP_DDR_BASE
            | DDR_NS_BASE size 464Mo
            | Begin of DDR cacheable in U-Boot, size CONFIG_DDR_CACHEABLE_SIZE 256Mo)

The exercise described in this article is to target the following 256Mo mapping:

0xd0000000  | (End of DDR 256Mo) 
0xcfc00000  | (DDR_SEC_BASE/CFG_TZDRAM_START 
            | size DDR_SEC_SIZE/CFG_TZDRAM_SIZE (4Mo) protected by TZC 400, 
            | Begin of OP-TEE mapping, 
            | Begin of DDR encrypted DDRMCE, size 4Mo
            | End of SHMEM)
0xcfb00000  | (DDR_SHARE_BASE/CFG_SHMEM_START
            | size DDR_SHARE_SIZE/CFG_SHMEM_SIZE (1Mo no secure),
            | Begin of reserved memory optee size 5Mo)
0xceb00000  | (Begin of optee-framebuffer protected access by TZC 400 size 16Mo)
0xcb000000  | (End of Linux CMA reserved)
0xc8000000  | (End of DDR cacheable in U-Boot)  
0xc3000000  | (Begin of Linux CMA reserved size CONFIG_CMA_SIZE_MBYTES 128Mo)
0xc0000000  | (STM32MP_DDR_BASE
            | DDR_NS_BASE size 235Mo
            | Begin of DDR cacheable in U-Boot, size CONFIG_DDR_CACHEABLE_SIZE 128Mo)

The software modification requested in the STM32MP1 Distribution Package, have been performed with the "devtool" and "bitbake" tools. The following components need to be modified (TF-A, U-boot, OP-TEE, Linux Kernel).

4. DDR configuration[edit source]

The DDR is configured with the STM32CubeMX DDR tool, the tool creates the device tree source include file.

5. TF-A updates[edit source]

To update the TF-A firmware in the STM32MP1 Distribution Package, use the devtool tool: $PC > devtool modify tf-a-stm32mp The updates requested according to new mapping: Modify the DDR_SIZE (512Mo->256Mo) defined in file fdts/stm32mp135f-dk-config.dts

  1. define DDR_SIZE 0x10000000

Modify the DDR_SEC_SIZE (30 Mo -> 4Mo) and DDR_SHARE_SIZE (2Mo->1Mo) defined in file fdts/stm32mp13-fw-config.dtsi

  1. define DDR_SEC_SIZE 0x400000
  2. define DDR_SHARE_SIZE 0x100000

DDR512Mo: Modify the DDR_MEM_SIZE (512Mo -> 256Mo) in the file fdts/stm32mp-ddr3-1x4Gb-1066-binF.dtsi . :

  1. define DDR_MEM_SIZE 0x10000000

5.1. Description of the TF-A updates[edit source]

  1. Firewall configuration (link to the page How to configure TF-A FW CONFIG)

The DDR firewall is configured to restrict access to specific areas.The TZC is configured at boot time to setup DDR accesses by the TF-A, redefined by OP-TEE. (link to the page How to configure TF-A FW CONFIG) Three regions are defined : Start address = 0xC000 0000: 251-Mbyte region where secure accesses are forbidden and to which all non-secure peripherals can access Start address = 0xCFB0 0000: 1-Mbyte (SHM) region where secure accesses are forbidden and non-secure accesses are possible only from an A7 CPU Start address = 0xCFC0 0000: 4-Mbyte (OP-TEE) region where only secure accesses are allowed

OP-TEE is the secure OS executed in DDR protected by TZC, the size allocated to OP-TEE depend on how the OP-TEE is configured and used, it includes the firmware Optee-os size, the stack and heap size, stack and heap are almost dedicated to Trusted Applications.
SHM is the share memory between non secure and secure OS (?? usage buffer for TA access ??;..) 

The configuration of the mem firewall is defined in the file fdts/stm32mp13-fw-config.dtsi DDR_NS_BASE : STM32MP_DDR_BASE : 0xc0000000 (default OpenSTLInux RAM mapping)) DDR_SEC_SIZE : 0x00400000 (4 Mo allocated to OP-TEE). DDR_SEC_BASE : STM32MP_DDR_BASE + DDR_SIZE - DDR_SEC_SIZE : 0xc0000000 + 0x10000000 - 0x00400000 : 0xcfc00000 DDR_SHARE_SIZE : 0x00100000 (1 Mo allocated for Share memory) DDR_SHARE_BASE : DDR_SEC_BASE - DDR_SHARE_SIZE : 0xcfc00000 - 0x00100000 : 0xcfb00000 DDR_NS_SIZE : DDR_SHARE_BASE - DDR_NS_BASE : 0xcfb00000 - 0xc0000000 : 0x0fb00000 : 251 Mo

  1. DDR Encryption area (link to the page How to configure TF-A FW CONFIG)

The memory region encrypted is defined in fdts/stm32mp13-fw-config-mem-encrypt.dtsi. The memory region defined by default in OpenSTLinux configuration is the region allocated to OP-TEE, so defined with parameters DDR_SEC_BASE and DDR_SEC_SIZE.

  1. Firmware configuration file

The FW_CONFIG define all binaries to be loading in internal RAM or DDR, with for each image the load adress, the maximum binary size to be loaded, and the ID of the image . The FW_CONFIG and all binaries are embedded in a TF-A FIP (Firmware Image Package). (link to How to configure TF-A FIP) The TF-A FIP contains all boot binaries and optionally their certificates, installed by the TF-A. (U-Boot, U-Boot dtb, OP-TEE, FW_CONFIG)

The configuration is defined in the file fdts/stm32mp13-fw-config.dtsi The structure of the tos-fw (secure os) depend on the parameter DDR_SEC_BASE an DDR_SEC_SIZE.

6. OP-TEE updates[edit source]

To update the OP-TEE firmware in the STM32MP1 Distribution Package, use the devtool tool : $PC > devtool modify optee-os-stm32mp

The updates requested according to new mapping: Modify the CFG_DRAM_SIZE (512Mo->256Mo), CFG_TZDRAM_SIZE (30Mo->4Mo) and CFG_SHMEM_SIZE (2Mo->1Mo) in the file core/arch/arm/plat-stm32mp1/conf.mk. CFG_DRAM_SIZE  ?= 0x10000000 CFG_TZDRAM_SIZE  ?= 0x00400000 CFG_SHMEM_SIZE  ?= 0x00100000

Modify the RAM memory size (512Mo->256Mo), the location of the secure framebuffer memory (0xdd000000->0xceb00000), in the file core/arch/arm/dts/stm32mp135f-dk.dts

memory@c0000000 { device_type = "memory"; reg = <0xc0000000 0x10000000>; };

reserved-memory { #address-cells = <1>; #size-cells = <1>; ranges;

               optee_framebuffer: optee-framebuffer@ceb00000 {
                       /* Secure framebuffer memory */
                       reg = <0xceb00000 0x1000000>;
                       st,protreg = <TZC_REGION_S_RDWR 0>;
               };
       

6.1. Description of the updates[edit source]

  1. memory secure and share memory

The OP-TEE memory configuration is defined in the file core/arch/arm/plat-stm32mp1/conf.mk CFG_TZDRAM_START ?= ($(CFG_DRAM_BASE) + $(CFG_DRAM_SIZE) - $(CFG_TZDRAM_SIZE)) = 0xc0000000 + 0x10000000 - 0x400000 = 0xcfc00000 CFG_SHMEM_START  ?= ($(CFG_TZDRAM_START) - $(CFG_SHMEM_SIZE)) = 0xcfc00000 - 0x100000 = 0xcfb00000

The memory size CFG_TZDRAM_SIZE and mapping reserved for OP-TEE depend on the task allocated to the secure OS. The memory allocated include the size of the Optee-os firmware, the stack and heap size, almost of stack and heap are reserved to the Trusted applications. see the FAQ in OP-TEE documentation the https://optee.readthedocs.io/en/latest/faq/faq.html#q-what-is-the-size-of-op-tee-itself

  1. OP-TEE frame buffer

The OP-TEE frame buffer is a secure memory region dedicated to the display, used by Trusted UI in OP-TEE, protected by TZC, This region named optee-framebuffer is configured in OP-TEE device tree source core/arch/arm/dts/stm32mp135f-dk.dts.

7. U-boot update[edit source]

To update the OP-TEE firmware in the STM32MP1 Distribution Package, use the devtool tool : $PC > devtool modify u-boot-stm32mp

The updates requested according to new mapping: Modify the RAM memory size (512Mo->256Mo), the location of the secure framebuffer memory (0xdd000000->0xceb00000), and the location (0xde000000->0xcfb00000) and size (30Mo->5Mo) of secure memory allocated to OP-TEE and SHM in the file arch/arm/dts/stm32mp135f-dk.dts

       memory@c0000000 {
              device_type = "memory";
              reg = <0xc0000000 0x10000000>;
       };

reserved-memory { #address-cells = <1>; #size-cells = <1>; ranges;

optee_framebuffer@ceb00000 { reg = <0xceb00000 0x1000000>; no-map; };

optee@cfb00000 { reg = <0xcfb00000 0x00500000>; no-map; };

       };

in the file configs/stm32mp13_defconfig CONFIG_DDR_CACHEABLE_SIZE 0x8000000

DDR512Mo:

**** arch/arm/mach-stm32mp/dram_init.c ****

int dram_init(void) {

/*gd->ram_size = ram.size;*/

gd->ram_size = SZ_256M;

return 0; }


7.1. Description of the updates[edit source]

  1. secure memory reserved

It is the memory reserved for secure OS OP-TEE and share memory. The size is calculated as the way CFG_TZDRAM_SIZE + CFG_SHMEM_SIZE = 5Mo. and the OP-TEE framebuffer dedicated to the display, used by Trusted UI in OP-TEE.

  1. Size of the DDR marked cacheable in pre-reloc stage

CONFIG_DDR_CACHEABLE_SIZE define the size of the DDR marked as-cacheable in U-Boot pre-reloc stage. This option can be useful to avoid speculatif access to secured area of DDR used by TS-A or OP-TEE before U-boot initialization.


8. Kernel Linux updates[edit source]

To update the Kernel Linux in the STM32MP1 Distribution Package, use the devtool tool :

$PC > devtool modify linux-stm32mp

The updates requested according to new mapping: Modify the RAM memory size (512Mo->256Mo), the location of the secure framebuffer memory (0xdd000000->0xceb00000), and the location (0xde000000->0xcfb00000) and size (30Mo->5Mo) of secure memory allocated to OP-TEE and SHM in the file arch/arm/dts/stm32mp135f-dk.dts

      memory@c0000000 {
              device_type = "memory";
              reg = <0xc0000000 0x10000000>;
       };


reserved-memory { #address-cells = <1>; #size-cells = <1>; ranges;

optee_framebuffer@ceb00000 { reg = <0xceb00000 0x1000000>; no-map; };

optee@cfb00000 { reg = <0xcfb00000 0x00500000>; no-map; }; };

8.1. Description of the updates[edit source]

  1. secure memory reserved

see U-boot updates description.

8.2. Linux kernel memory usage[edit source]

The resulting DDR Non Secure memory allocated to the Linux Kernel and userspace according to the mapping chosen for this exercise is: 0xceb00000 - 0xc0000000 = 0xeb00000 (235 Mo).

Purpose of this paragraph is to highlight you about some components size you can modify to share the non secure RAM according to your need, the Linux kernel, the RAM File system, the Contiguous Memory Area (CMA).

We can estimate minimal amount of required RAM at system startup of the kernel image, we talk here about the static RAM used par Kernel image and the RAM file system. The kernel image execute the command : $PC > size vmlinux Example of result with OpenSTLinux default image : 16,5Mo

The initramfs/initrd is the temporary Root File system installed in RAM and used at the kernel init step, before mounting the final root file system. The contain of the RAM fs can be adjust according to the new mapping, you need space for copy the .cpio compressed file and the space for the file system unpacked. The default OpenSTLinux st-image-resize-initrd-openstlinux-weston-stm32mp13-disco.cpi.gz file is about 29.3Mo. The decompressed File system size is 50.4 Mo

contiguous memory area (CMA), The size is defined in the kerenl configuration file CONFIG_CMA_SIZE_MBYTES. CONFIG_CMA_SIZE_MBYTES is set to 128 in the OpenSTlinux distribution package.

9. traces/debug[edit source]