Skip to content

Commit e233326

Browse files
carlocaionecarlescufi
authored andcommitted
cache: Introduce external cache controller system support
The cache API currently shipped in Zephyr is assuming that the cache controller is always on-core thus managed at the arch level. This is not always the case because many SoCs rely on external cache controllers as a peripheral external to the core (for example PL310 cache controller and the L2Cxxx family). In some cases you also want a single driver to control a whole set of cache controllers. Rework the cache code introducing support for external cache controllers. Signed-off-by: Carlo Caione <[email protected]>
1 parent 3b00571 commit e233326

File tree

10 files changed

+243
-115
lines changed

10 files changed

+243
-115
lines changed

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
/drivers/adc/ @anangl
185185
/drivers/adc/adc_stm32.c @cybertale
186186
/drivers/bluetooth/ @joerchan @jhedberg @Vudentz
187+
/drivers/cache/ @carlocaione
187188
/drivers/can/ @alexanderwachter
188189
/drivers/can/*mcp2515* @karstenkoenig
189190
/drivers/clock_control/*nrf* @nordic-krch

arch/Kconfig

+17
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,23 @@ config ICACHE_LINE_SIZE
919919

920920
Detect automatically at runtime by selecting ICACHE_LINE_SIZE_DETECT.
921921

922+
choice
923+
prompt "Cache type"
924+
depends on CACHE_MANAGEMENT
925+
default HAS_ARCH_CACHE
926+
927+
config HAS_ARCH_CACHE
928+
bool "Integrated cache controller"
929+
help
930+
"Integrade on-core cache controller"
931+
932+
config HAS_EXTERNAL_CACHE
933+
bool "External cache controller"
934+
help
935+
"External cache controller or cache management system"
936+
937+
endchoice
938+
922939
endmenu
923940

924941
config ARCH

drivers/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ add_subdirectory_ifdef(CONFIG_SYS_CLOCK_EXISTS timer)
5656
add_subdirectory_ifdef(CONFIG_NEURAL_NET_ACCEL neural_net)
5757
add_subdirectory_ifdef(CONFIG_PTP_CLOCK ptp_clock)
5858
add_subdirectory_ifdef(CONFIG_EDAC edac)
59+
add_subdirectory_ifdef(CONFIG_CACHE_MANAGEMENT cache)

drivers/Kconfig

+2
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@ source "drivers/misc/Kconfig"
113113

114114
source "drivers/disk/Kconfig"
115115

116+
source "drivers/cache/Kconfig"
117+
116118
endmenu

drivers/cache/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# SPDX-License-Identifier: Apache-2.0

drivers/cache/Kconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) 2021 Carlo Caione <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
menuconfig CACHE
5+
bool "External cache controllers drivers"
6+
help
7+
Enable support for external cache controllers drivers
8+
9+
if CACHE
10+
11+
module = CACHE
12+
module-str = cache
13+
source "subsys/logging/Kconfig.template.log_config"
14+
15+
config CACHE_HAS_DRIVER
16+
bool
17+
18+
endif # CACHE

include/cache.h

+53-112
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define ZEPHYR_INCLUDE_CACHE_H_
99

1010
#include <kernel.h>
11+
#include <kernel_structs.h>
1112

1213
#ifdef __cplusplus
1314
extern "C" {
@@ -22,149 +23,89 @@ extern "C" {
2223
* INVD means invalidate and will mark cache lines as not valid. A future
2324
* access to the associated address is guaranteed to generate a memory fetch.
2425
*/
26+
2527
#define K_CACHE_WB BIT(0)
2628
#define K_CACHE_INVD BIT(1)
2729
#define K_CACHE_WB_INVD (K_CACHE_WB | K_CACHE_INVD)
2830

29-
/**
30-
*
31-
* @brief Enable d-cache
32-
*
33-
* Enable the d-cache.
34-
*
35-
* @return N/A
36-
*/
37-
void arch_dcache_enable(void);
31+
#if defined(CONFIG_HAS_EXTERNAL_CACHE)
3832

39-
/**
40-
*
41-
* @brief Disable d-cache
42-
*
43-
* Disable the d-cache.
44-
*
45-
* @return N/A
46-
*/
47-
void arch_dcache_disable(void);
33+
/* Driver interface mirrored in include/drivers/cache.h */
4834

49-
/**
50-
*
51-
* @brief Enable i-cache
52-
*
53-
* Enable the i-cache.
54-
*
55-
* @return N/A
56-
*/
57-
void arch_icache_enable(void);
35+
/* Enable d-cache */
36+
extern void dcache_enable(void);
5837

59-
/**
60-
*
61-
* @brief Disable i-cache
62-
*
63-
* Disable the i-cache.
64-
*
65-
* @return N/A
66-
*/
67-
void arch_icache_disable(void);
38+
/* Disable d-cache */
39+
extern void dcache_disable(void);
6840

69-
/**
70-
*
71-
* @brief Write-back / Invalidate / Write-back + Invalidate all d-cache
72-
*
73-
* Write-back, Invalidate or Write-back + Invalidate the whole d-cache.
74-
*
75-
* @param op Operation to perform (one of the K_CACHE_* operations)
76-
*
77-
* @retval 0 On success
78-
* @retval -ENOTSUP If the operation is not supported
79-
*/
80-
int arch_dcache_all(int op);
41+
/* Enable i-cache */
42+
extern void icache_enable(void);
8143

82-
/**
83-
*
84-
* @brief Write-back / Invalidate / Write-back + Invalidate d-cache lines
85-
*
86-
* No alignment is required for either addr or size, but since
87-
* arch_dcache_range() iterates on the d-cache lines, a d-cache line alignment
88-
* for both is optimal.
89-
*
90-
* The d-cache line size is specified either via the CONFIG_DCACHE_LINE_SIZE
91-
* kconfig option or it is detected at runtime.
92-
*
93-
* @param addr The pointer to start the multi-line operation
94-
* @param size The number of bytes that are to be acted on
95-
* @param op Operation to perform (one of the K_CACHE_* operations)
96-
*
97-
* @retval 0 On success
98-
* @retval -ENOTSUP If the operation is not supported
99-
*/
100-
int arch_dcache_range(void *addr, size_t size, int op);
44+
/* Disable i-cache */
45+
extern void icache_disable(void);
10146

102-
/**
103-
*
104-
* @brief Write-back / Invalidate / Write-back + Invalidate all i-cache
105-
*
106-
* Write-back, Invalidate or Write-back + Invalidate the whole i-cache.
107-
*
108-
* @param op Operation to perform (one of the K_CACHE_* operations)
109-
*
110-
* @retval 0 On success
111-
* @retval -ENOTSUP If the operation is not supported
112-
*/
113-
int arch_icache_all(int op);
47+
/* Write-back / Invalidate / Write-back + Invalidate all d-cache */
48+
extern int dcache_all(int op);
11449

115-
/**
116-
*
117-
* @brief Write-back / Invalidate / Write-back + Invalidate i-cache lines
118-
*
119-
* No alignment is required for either addr or size, but since
120-
* arch_icache_range() iterates on the i-cache lines, an i-cache line alignment
121-
* for both is optimal.
122-
*
123-
* The i-cache line size is specified either via the CONFIG_ICACHE_LINE_SIZE
124-
* kconfig option or it is detected at runtime.
125-
*
126-
* @param addr The pointer to start the multi-line operation
127-
* @param size The number of bytes that are to be acted on
128-
* @param op Operation to perform (one of the K_CACHE_* operations)
129-
*
130-
* @retval 0 On success
131-
* @retval -ENOTSUP If the operation is not supported
132-
*/
133-
int arch_icache_range(void *addr, size_t size, int op);
50+
/* Write-back / Invalidate / Write-back + Invalidate d-cache lines */
51+
extern int dcache_range(void *addr, size_t size, int op);
52+
53+
/* Write-back / Invalidate / Write-back + Invalidate all i-cache */
54+
extern int icache_all(int op);
55+
56+
/* Write-back / Invalidate / Write-back + Invalidate i-cache lines */
57+
extern int icache_range(void *addr, size_t size, int op);
58+
59+
#else
60+
61+
/* Hooks into arch code */
62+
63+
#define dcache_enable arch_dcache_enable
64+
#define dcache_disable arch_dcache_disable
65+
#define icache_enable arch_icache_enable
66+
#define icache_disable arch_icache_disable
67+
#define dcache_all(op) arch_dcache_all(op)
68+
#define dcache_range(addr, size, op) arch_dcache_range(addr, size, op)
69+
#define icache_all(op) arch_icache_all(op)
70+
#define icache_range(addr, size, op) arch_icache_range(addr, size, op)
71+
#define dcache_line_size_get arch_dcache_line_size_get
72+
#define icache_line_size_get arch_icache_line_size_get
73+
74+
#endif
13475

13576
__syscall int sys_dcache_all(int op);
13677
static inline int z_impl_sys_dcache_all(int op)
13778
{
138-
if (IS_ENABLED(CONFIG_CACHE_MANAGEMENT)) {
139-
return arch_dcache_all(op);
140-
}
79+
#if defined(CONFIG_CACHE_MANAGEMENT)
80+
return dcache_all(op);
81+
#endif
14182
return -ENOTSUP;
14283
}
14384

14485
__syscall int sys_dcache_range(void *addr, size_t size, int op);
14586
static inline int z_impl_sys_dcache_range(void *addr, size_t size, int op)
14687
{
147-
if (IS_ENABLED(CONFIG_CACHE_MANAGEMENT)) {
148-
return arch_dcache_range(addr, size, op);
149-
}
88+
#if defined(CONFIG_CACHE_MANAGEMENT)
89+
return dcache_range(addr, size, op);
90+
#endif
15091
return -ENOTSUP;
15192
}
15293

15394
__syscall int sys_icache_all(int op);
15495
static inline int z_impl_sys_icache_all(int op)
15596
{
156-
if (IS_ENABLED(CONFIG_CACHE_MANAGEMENT)) {
157-
return arch_icache_all(op);
158-
}
97+
#if defined(CONFIG_CACHE_MANAGEMENT)
98+
return icache_all(op);
99+
#endif
159100
return -ENOTSUP;
160101
}
161102

162103
__syscall int sys_icache_range(void *addr, size_t size, int op);
163104
static inline int z_impl_sys_icache_range(void *addr, size_t size, int op)
164105
{
165-
if (IS_ENABLED(CONFIG_CACHE_MANAGEMENT)) {
166-
return arch_icache_range(addr, size, op);
167-
}
106+
#if defined(CONFIG_CACHE_MANAGEMENT)
107+
return icache_range(addr, size, op);
108+
#endif
168109
return -ENOTSUP;
169110
}
170111

@@ -188,7 +129,7 @@ static inline void sys_cache_flush(void *addr, size_t size)
188129
static inline size_t sys_dcache_line_size_get(void)
189130
{
190131
#ifdef CONFIG_DCACHE_LINE_SIZE_DETECT
191-
return arch_dcache_line_size_get();
132+
return dcache_line_size_get();
192133
#elif (CONFIG_DCACHE_LINE_SIZE != 0)
193134
return CONFIG_DCACHE_LINE_SIZE;
194135
#else
@@ -207,7 +148,7 @@ static inline size_t sys_dcache_line_size_get(void)
207148
static inline size_t sys_icache_line_size_get(void)
208149
{
209150
#ifdef CONFIG_ICACHE_LINE_SIZE_DETECT
210-
return arch_icache_line_size_get();
151+
return icache_line_size_get();
211152
#elif (CONFIG_ICACHE_LINE_SIZE != 0)
212153
return CONFIG_ICACHE_LINE_SIZE;
213154
#else

0 commit comments

Comments
 (0)