Cache
Introduction
The Cache of Soc supports Enable/Disable, Flush and Clean operation, as the following table lists.
Operation |
Description |
I-Cache |
D-Cache |
---|---|---|---|
Enable/Disable |
Enable or Disable Cache function |
√ |
√ |
Flush (Invalidate) |
|
√ |
√ |
Clean |
|
x |
√ |
Cache Boot Status
In the ROM code, the default states of Cache are:
KM4 Cache: enabled by default
KM0 Cache: disabled by default
KM4 Cache: enabled by default
KR4 Cache: disabled by default
KM4 Cache: enabled by default
KR4 Cache: disabled by default
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];
Note
TBD
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];
Note
For KR4: non-cacheable attributes can only be defined through MCCA register, which means a data buffer cannot be defined with non-cacheable attribute.
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];
Note
For KR4: non-cacheable attributes can only be defined through MCCA register, which means a data buffer cannot be defined with non-cacheable attribute.
For DSP: to operate the DSP Cache memories, refer to Xtensa LX7 Microprocessor Data Book and Xtensa System Software Reference Manual for more information.
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];
Note
TBD
Cache Consistency When Using DMA
When DMA is used to migrate data from/to memory buffers, the start address 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 a cache line and the first half is occupied by other programs, invalidating or cleaning the current cache line by those programs will affect the entire cache line, resulting in inconsistent cache and memory data of the current buffer.
Note
The DMA operation address requires exclusive ownership of a full cache line. 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 recommendation: call
DCache_Clean()
DMA Tx configuration
DMA Tx interrupt handling
DMA Rx Flow
CPU allocates Rx buffer
DCache_Clean (if the Rx buffer is in a clean state, this step can be skipped)
Caution
For Cortex-A32, if the Rx buffer is in a dirty state in the cache, executing DCache_Invalidate will perform both a clean and invalidate operation. The clean operation may lead to unexpected write behavior to memory.
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 the CPU’s D-Cache becomes full, which could overwrite the content that DMA has already written.
DMA Rx configuration
DMA Rx interrupt handling
DCache_Invalidate (this step is mandatory)
Caution
For CPUs with automatic data prefetching and monitoring capabilities, such as Cortex-A32/DSP, e.g., Cortex-A32 reads the contents of adjacent addresses of the Rx buffer, Cortex-A32 starts line fills in the background to bring the old values of the Rx buffer back into the cache.
Prevents the CPU from reading old values into the cache during DMA processing.
CPU reads Rx buffer (the value returned by DMA Rx)
Caution
For Cortex-A32/DSP, DCache_Clean/DCache_CleanInvalidate operations write entire cache lines to memory. When two CPUs (with different cache line sizes) communicate using a shared memory region, this shared memory must be aligned with the larger of the two cache line sizes. e.g., if the shared memory is only 32 bytes, CPU0 with a 32-byte cache line will only write 32 bytes each time it cleans, while CPU1 with a 64-byte cache line will write 64 bytes each time it cleans, potentially overwriting other data of CPU0.