Routines for controlling/optimizing the operation of the (flash/PSRAM) cache of the ESP32-S3 and -C3.
Warning
The workings of the cache with these operations is basically undocumented, specifically any constraints which may exist in the hardware and cause erroneous operation if violated.
Therefore, be careful when using this as data corruption or other malfunction may occur under certain conditions.
The cache(s) on newer ESP32 variants support some advanced features which currently are not supported directly by the ESP-IDF. Specifically, on these chips cache operations include invalidating, write-back (for PSRAM), "preloading" (prefetch), and "autoloading" of memory ranges. The S3 has separate caches for instructions (ICACHE) and data (DCACHE). While the ICACHE can be controlled in the same way as the DCACHE, this code only works on the DCACHE. (Trying to predict/leverage access patterns on instruction memory can be rather futile as the compiler+linker can put code at un-expected locations, with the execution jumping from one location to the other.)
What's special is that these operations run independent of and concurrently to the CPU, so the CPU does not have to wait for the cache/memory but can continue doing other things. This can enhance performance/throughput.
For performance optimizations, "preloading" and "autoloading" are of special interest:
- Preloading ("manual" prefetching): The application can request the MMU to load a given range of memory into the cache before and while the CPU actually accesses/processes (other) data from that memory range.
- Autoloading (automatic prefetching): The MMU can be set up to automatically load (prefetch) a number of adjacent cache lines when an address from a given memory range is accessed. This can be beneficial when the application (usually) accesses some data from a memory range sequentially, as the next cache line worth of memory is already loaded into the cache while the application is processing data from the previous one, i.e. the MMU is not waiting for an actual cache miss to occur before starting to load the requested data.
Both of these may prove useful specifically with inference of on-chip neural networks/LLMs/... as these tend to do sequential accesses to chunks of model data in flash. (Basically, any sequential access to data in more than one of a number of adjacent cache lines may experience a performance gain from autoload or prefetching.)
Currently, this code only implements control of the autoload feature (basically what Cache_Config_DCache_Autoload() & al. from the S3's ROM do); a fast,
thread-safe helper for "manual" prefetching is not quite ready to be released.
The cache on the S3 and C3 supports up to two independent "sections" of memory to be set up for autoloading, each defined by a starting address and a size. This means that for example one section could be pointed to some constant data in flash and the other one pointed to PSRAM.
See main/main.cpp:
#include "mem_cache_autoload.hpp"
...
// Set up a complete autoload configuration and register our FLASH_DATA in section 0
{
mem::cache::Autoload::Ctrl ctrl {};
ctrl
.setSize(3) // autload 3+1 cache lines
.setOrder(mem::cache::Autoload::Order::ASC) // in ascending order
.setTrigger(mem::cache::Autoload::Trigger::ACCESS) // on any access
.enableSect(0) // enable section 0
.disableSect(1) // section 1 not set up, disable!
.enable(); // enable autoload
mem::cache::AUTOLOAD
.setSection(0,std::span {FLASH_DATA}) // set up section 0 *before* enabling autoload
.setCtrl(ctrl);
}
The example code creates a 128KB array of (const) data in flash and for demonstration calculates a CRC32 of this data at runtime, once with autoload disabled, then with autoload enabled for that data.
On an ESP32-S3 running at 240 MHz with QIO flash @ 80MHz and 32-byte cache lines, the log output looks like this:
I (1266) example: Running with autoload DISABLED.
I (1346) example: Took 1651938 CPU clock cycles.
I (1396) example: Enabling autoload.
I (1396) example: Running with autoload ENABLED.
I (1466) example: Took 1367250 CPU clock cycles.
I (1516) example: Speed gained through autoload: 20.8%