Cache
概述
应用子系统内置两个 32 KB cache,支持通过 cache 执行 Flash 内的指令(XIP)。
工作原理
Cache 主要用于把外部存储器(Flash、PSRAM、DMEM 等)映射到系统的内存空间,使 CPU 可以通过 Cache 实现透明访问这类大型存储空间,cache 屏蔽了这些外部存储器访问的复杂性。
Cache 本质上是基于缓存行(cache line)数据结构的一种多对一的映射,其工作原理如下图所示。
Cache 工作原理
在上图中,cache 的存储空间是 32 KB,而 cache 由一系列的 cache line 组成,每个 cache line 是 32 bytes。system memory space(CPU 可见的内存空间)的每个 32 KB 的第一个 32 bytes 共享 cache 的第一个 cache line,由 TAG RAM 标识每个 cache line 的 32 bytes 所属的 32KB 空间。
每个 cache line 可采用 2-way 组相连模式。如下图所示,每个 cache line 有两个 32 bytes 的存储器,当发生缓存缺失(cache miss)时,替换两个 way 中的一个(一般是轮询的方式)从而降低 cache line 冲突概率,提高系统访问效率。
多路 Cache 工作原理
WQ7036 的应用子系统有两个 2-way 32 KB cache,蓝牙子系统有一个 2-way 32 KB cache,DSP 子系统有一个 4-way 64 KB cache。
功能特性
应用子系统的缓存具备如下特性:
支持“指定地址范围”的缓存清除(cache flush)操作。
支持全缓存清理(cache clear)操作。
支持 cache miss 统计。
支持“指定地址段范围”访问监测。
当多个 CPU 需要同时 XIP 时,由 SFC 硬件自动仲裁。
工作模式
Cache 主要有四种工作模式:Clear 模式、Flush 模式、Normal 模式以及 Monitor 模式。
Clear Mode
当设置 Clear mode 时,cache 内部的状态机会把 TAG memory 中的信息全部清空,清理完成后的 cache 是完全空白的。
在 clear 过程中,如果系统下发读/写操作,cache 会等 clear 操作完成后再响应这些操作。
如果 cache 正在进行读/写操作时 clear mode 生效,cache 会等待当前读/写操作完成后再响应 clear 操作。
说明
Clear 操作只清空 TAG memory 的信息。在这个过程中如果有 Dirty 的缓存行,不会对这些缓存行发起 Flush 操作,缓存行 dirty 数据会丢失。
Flash Mode
当设置 Flush mode 的时候,软件会预先设置一段将要 flush 的地址空间(起始地址、结束地址),cache 内部的状态机会扫描 TAG memory,检查待 flush 的地址空间内的 cache line 是否存在于 cache 中。
如果扫描到某个 cache line 需要 flush,则检查其是否为 dirty。
如果 dirty,则把这个 cache line 的数据 flush 到 external memory 中对应的位置,然后把这个 cache line 对应的 TAG 设置为无效。
如果这个 cache line 不是 dirty,则直接设置这个 cache line 对应的 TAG 为无效。
说明
Flush mode 主要是把 cache 中的 dirty 数据 flush 到 external memory 中,解决二者间数据不一致的问题。
Normal Mode
normal mode 为 cache 的基本工作模式,此时,cache 正常响应系统的读写请求。
当 cache 被设置为 Normal mode 时:
如果缓存命中(cache hit),则 cache 直接响应系统的读/写请求。
如果 cache miss,则自动进行 cache line data 的替换操作。
Monitor Mode
Cache monitor mode 主要用于调试,需要软件预先配置好需要监测的地址空间段。
Cache 空间中可能存在多个堆栈。当堆栈数据被损坏的时候,cache 的 monitor 模式可以触发错误中断。
参数 cache_acc_saddr 和 cache_acc_eaddr 定义了监测时允许访问的空间。参数 cache_mon_saddr 和 cache_mon_eaddr 定义了监测时允许访问空间内部被禁止访问的空间。
当 CPU 访问到下图中黄色区域外的空间时,会触发访问错误告警。
API 介绍
This section introduces the CACHE module's enum, structure, functions and how to use this driver.
Functions
-
void wq_cache_clear(WQ_CACHE_ID wq_cache_id)
This function is to clear the corresponding ID cache.
- 参数:
wq_cache_id -- is cache id
-
void wq_cache_flush(WQ_CACHE_ID wq_cache_id, uint32_t addr, uint32_t len)
This function is to flush the corresponding ID cache.
- 参数:
wq_cache_id -- is cache id
addr -- is cache addr
len -- is offset len from cache addr
-
void wq_cache_invalidate(WQ_CACHE_ID wq_cache_id, uint32_t addr, uint32_t len)
This function is to invalidate the cache address data for the corresponding ID.
- 参数:
wq_cache_id -- is cache id
addr -- is cache addr
len -- is offset len from cache addr
-
void wq_cache_init(WQ_CACHE_ID wq_cache_id, WQ_SFC_PORT wq_sfc_port)
This function is to init Cache module.
- 参数:
wq_cache_id -- is cache id
wq_sfc_port -- is sfc port
-
void wq_cache_deinit(WQ_CACHE_ID wq_cache_id)
This function is to deinit Cache module.
- 参数:
wq_cache_id -- is cache id
-
uint32_t wq_cache_get_miss_cnt(WQ_CACHE_ID wq_cache_id)
This function is to get the count of the cache missing.
- 参数:
wq_cache_id -- is cache id.
- 返回:
uint32_t is the count of the cache missing.
-
void wq_cache_miss_clear(WQ_CACHE_ID wq_cache_id)
This function is to clear the cache missing.
- 参数:
wq_cache_id -- is cache id.
-
void wq_cache_miss_enable(WQ_CACHE_ID wq_cache_id, bool en)
This function is to enable/disable the cache missing.
- 参数:
wq_cache_id -- wq_cache_id is cache id.
en -- is the enable or disable signal.
-
void wq_cache_config_as_ram_mode(WQ_CACHE_ID wq_cache_id)
This function is used to configure cache ram to inernal ram.
- 参数:
wq_cache_id -- is cache id
-
bool wq_cache_get_restore_status(void)