MPU
Functional Description
The Memory Protection Unit (MPU) is a component provided by Arm and is used to provide hardware protection by software definition. The code in SDK provides the mpu_region_config struct to set the region memory attribute of MPU.
The following table shows the member variables of the mpu_region_config
struct.
Member variable |
Type |
Description |
---|---|---|
region_base |
uint32_t |
MPU region base, 32 bytes aligned |
region_size |
uint32_t |
MPU region size, 32 bytes aligned |
xn |
uint8_t |
Execute Never attribute
|
ap |
uint8_t |
Access permissions
|
sh |
uint8_t |
Share ability for Normal memory
|
attr_idx |
uint8_t |
Memory attribute indirect index, can be a value of 0 ~ 7, the detailed attribute is defined in
|
MPU APIs
mpu_init
Items |
Description |
---|---|
Introduction |
Initialize MPU region memory attribute to typical value |
Parameters |
None |
Return |
None |
mpu_set_mem_attr
Items |
Description |
---|---|
Introduction |
Change MPU region memory attribute |
Parameters |
|
Return |
None |
mpu_region_cfg
Items |
Description |
---|---|
Introduction |
Configure MPU region memory attribute. |
Parameters |
|
Return |
None |
mpu_entry_free
Items |
Description |
---|---|
Introduction |
Free MPU entry |
Parameters |
MPU entry index:
|
Return |
None |
mpu_entry_alloc
Items |
Description |
---|---|
Introduction |
Allocate a free MPU entry |
Parameters |
None |
Return |
MPU entry index:
|
Usage
Follow these steps to set a MPU region:
Define a new variable and struct
Variable to store MPU entry index
Struct mpu_region_config to store the region memory attribute
Call
mpu_entry_alloc()
to allocate a free MPU entrySet the struct of region memory attribute
Call
mpu_region_cfg()
to configure MPU region memory attribute
Cache
Functional Description
The Cache of RTL8721Dx supports Enable/Disable, Flush and Clean operation, as following table lists.
Operation |
Description |
I-Cache |
D-Cache |
---|---|---|---|
Enable/Disable |
Enable or Disable Cache function |
√ |
√ |
Flush (Invalidate) |
|
√ |
√ |
Clean |
|
x |
√ |
Note
In the ROM code, the default states of Cache are:
KM4 Cache: enabled by default
KM0 Cache: disabled by default
Cache APIs
ICache_Enable
Items |
Description |
---|---|
Introduction |
Enable I-Cache |
Parameters |
None |
Return |
None |
ICache_Disable
Items |
Description |
---|---|
Introduction |
Disable I-Cache |
Parameters |
None |
Return |
None |
ICache_Invalidate
Items |
Description |
---|---|
Introduction |
Invalidate I-Cache |
Parameters |
None |
Return |
None |
DCache_IsEnabled
Items |
Description |
---|---|
Introduction |
Check D-Cache enabled or not |
Parameters |
None |
Return |
D-Cache enable status:
|
DCache_Enable
Items |
Description |
---|---|
Introduction |
Enable D-Cache |
Parameters |
None |
Return |
None |
DCache_Disable
Items |
Description |
---|---|
Introduction |
Disable D-Cache |
Parameters |
None |
Return |
None |
DCache_Invalidate
Items |
Description |
---|---|
Introduction |
Invalidate D-Cache by address |
Parameters |
|
Return |
None |
DCache_Clean
Items |
Description |
---|---|
Introduction |
Clean D-Cache by address |
Parameters |
Note Address set 0xFFFFFFFF is used to clean all D-Cache. |
Return |
None |
DCache_CleanInvalidate
Items |
Description |
---|---|
Introduction |
Clean and invalidate D-Cache by address |
Parameters |
Note Address set 0xFFFFFFFF is used to clean and flush all D-Cache. |
Return |
None |
How to Define a Non-cacheable Data Buffer
Add SRAM_NOCACHE_DATA_SECTION before the buffer definition to define a data buffer with non-cacheable attribute.
SRAM_NOCACHE_DATA_SECTION u8 noncache_buffer[DATA_BUFFER_SIZE];
Cache Consistency When Using DMA
When DMA is used to migrate data from/to memory buffers, the start and end address of the buffer must be aligned with the cache line to avoid inconsistencies between cache data and memory data. For example, if the start address of a buffer is in the middle of the cache line and the first half is occupied by other programs, when other programs invalidate or clean the current cache line, this operation will affect the entire cache line, resulting in inconsistent cache and memory data of the current buffer.
Caution
The DMA operation address requires exclusive ownership of a complete cache line. You can define the buffer using malloc()
or ALIGNMTO(CACHE_LINE_SIZE) u8 op_buffer[CACHE_LINE_ALIGMENT(op_buffer_size)]()
.
DMA Tx Flow
CPU allocates Tx buffer
CPU writes Tx buffer
Realtek recommends:
DCache_Clean()
DMA Tx Config
DMA Tx Interrupt
DMA Rx Flow
CPU allocates Rx buffer
DCache_Clean (if the Rx buffer is in a clean state, this step can be skipped)
Caution
If the Rx buffer is in a dirty state in the cache, the CPU may write the Rx buffer back to memory from the cache when CPU’s D-Cache becomes full, which could overwrite content that DMA Rx has already written.
DMA Rx Config
DMA Rx interrupt
DCache_Invalidate (this step is mandatory)
Caution
Prevents the CPU from reading old values into the cache during DMA processing.
CPU reads Rx buffer (the value returned by DMA Rx)