3.3.2. Cache¶
由于打开 Cache 对程序执行效率和内存访问效率都有很大的提升, SPL 的配置默认打开 I-Cache、D-Cache。
使能 Cache 的流程如下:
SPL 阶段,执行时立刻使能 I-Cache
SPL 退出时不关闭 Cache
因此后续的 OpenSBI、U-Boot 阶段,默认是在 Cache 使能的情况下运行的。
注意
- RISCV CPU 中,Cache 与 MMU 两个功能是分开设置的。此处只描述 Cache 的相关配置,与 MMU 没有关系。 
- Cache 的使能必须在 RISCV M-Mode 下操作,由于 U-Boot 运行在 S-Mode,因此 U-Boot 阶段不能开关 Cache 
3.3.2.1. 相关配置¶
Cache Enable
ArtInChip 平台默认使能 I-Cache 和 D-Cache,如果需要关闭相关功能,可以使用下面的配置:
- CONFIG_SYS_ICACHE_OFF 
- CONFIG_SYS_DCACHE_OFF 
3.3.2.2. 代码流程¶
Cache 相关 API:
| API | SPL 可用 | U-Boot 可用 | 
|---|---|---|
| icache_enable | 是 | 否 | 
| icache_disable | 是 | 否 | 
| icache_status | 是 | 否 | 
| dcache_enable | 是 | 否 | 
| dcache_disable | 是 | 否 | 
| dcache_status | 是 | 否 | 
| flush_dcache_all | 是 | 是 | 
| flush_dcache_range | 是 | 是 | 
| invalidate_dcache_range | 是 | 是 | 
其实现在 arch/riscv/cpu/c906/cache.c 。
SPL 中 Cache 初始化流程如下:
_start // arch/riscv/cpu/start.S
|-> save_boot_params
|-> board_init_f_alloc_reserve
|-> harts_early_init
|-> board_init_f_init_reserve
|-> icache_enable   // arch/riscv/cpu/c906/cache.c
|-> dcache_enable   // arch/riscv/cpu/c906/cache.c
|-> board_init_f