Zephyr Project API  3.2.0
A Scalable Open Source RTOS
cache.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_CACHE_H_
8#define ZEPHYR_INCLUDE_CACHE_H_
9
10#include <zephyr/kernel.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
27#define K_CACHE_WB BIT(0)
28#define K_CACHE_INVD BIT(1)
29#define K_CACHE_WB_INVD (K_CACHE_WB | K_CACHE_INVD)
30
31#if defined(CONFIG_HAS_EXTERNAL_CACHE)
32
33/* Driver interface mirrored in include/drivers/cache.h */
34
35#if defined(CONFIG_DCACHE)
36
37/* Enable d-cache */
38extern void cache_data_enable(void);
39
40/* Disable d-cache */
41extern void cache_data_disable(void);
42
43/* Write-back / Invalidate / Write-back + Invalidate all d-cache */
44extern int cache_data_all(int op);
45
46/* Write-back / Invalidate / Write-back + Invalidate d-cache lines */
47extern int cache_data_range(void *addr, size_t size, int op);
48
49#endif /* CONFIG_DCACHE */
50
51#if defined(CONFIG_ICACHE)
52
53/* Enable i-cache */
54extern void cache_instr_enable(void);
55
56/* Disable i-cache */
57extern void cache_instr_disable(void);
58
59/* Write-back / Invalidate / Write-back + Invalidate all i-cache */
60extern int cache_instr_all(int op);
61
62/* Write-back / Invalidate / Write-back + Invalidate i-cache lines */
63extern int cache_instr_range(void *addr, size_t size, int op);
64
65#endif /* CONFIG_ICACHE */
66
67#else
68
69/* Hooks into arch code */
70
71#if defined(CONFIG_DCACHE)
72
73#define cache_data_enable arch_dcache_enable
74#define cache_data_disable arch_dcache_disable
75#define cache_data_all(op) arch_dcache_all(op)
76#define cache_data_range(addr, size, op) arch_dcache_range(addr, size, op)
77#define cache_data_line_size_get arch_dcache_line_size_get
78
79#endif /* CONFIG_DCACHE */
80
81#if defined(CONFIG_ICACHE)
82
83#define cache_instr_enable arch_icache_enable
84#define cache_instr_disable arch_icache_disable
85#define cache_instr_all(op) arch_icache_all(op)
86#define cache_instr_range(addr, size, op) arch_icache_range(addr, size, op)
87#define cache_instr_line_size_get arch_icache_line_size_get
88
89#endif /* CONFIG_ICACHE */
90
91#endif /* CONFIG_HAS_EXTERNAL_CACHE */
92
93__syscall int sys_cache_data_all(int op);
94static inline int z_impl_sys_cache_data_all(int op)
95{
96#if defined(CONFIG_CACHE_MANAGEMENT) && defined(CONFIG_DCACHE)
97 return cache_data_all(op);
98#endif
99 ARG_UNUSED(op);
100
101 return -ENOTSUP;
102}
103
104__syscall int sys_cache_data_range(void *addr, size_t size, int op);
105static inline int z_impl_sys_cache_data_range(void *addr, size_t size, int op)
106{
107#if defined(CONFIG_CACHE_MANAGEMENT) && defined(CONFIG_DCACHE)
108 return cache_data_range(addr, size, op);
109#endif
110 ARG_UNUSED(addr);
111 ARG_UNUSED(size);
112 ARG_UNUSED(op);
113
114 return -ENOTSUP;
115}
116
117__syscall int sys_cache_instr_all(int op);
118static inline int z_impl_sys_cache_instr_all(int op)
119{
120#if defined(CONFIG_CACHE_MANAGEMENT) && defined(CONFIG_ICACHE)
121 return cache_instr_all(op);
122#endif
123 ARG_UNUSED(op);
124
125 return -ENOTSUP;
126}
127
128__syscall int sys_cache_instr_range(void *addr, size_t size, int op);
129static inline int z_impl_sys_cache_instr_range(void *addr, size_t size, int op)
130{
131#if defined(CONFIG_CACHE_MANAGEMENT) && defined(CONFIG_ICACHE)
132 return cache_instr_range(addr, size, op);
133#endif
134 ARG_UNUSED(addr);
135 ARG_UNUSED(size);
136 ARG_UNUSED(op);
137
138 return -ENOTSUP;
139}
140
141#ifdef CONFIG_LIBMETAL
142static inline void sys_cache_flush(void *addr, size_t size)
143{
144 sys_cache_data_range(addr, size, K_CACHE_WB);
145}
146#endif
147
148#define CPU DT_PATH(cpus, cpu_0)
149
158static inline size_t sys_cache_data_line_size_get(void)
159{
160#ifdef CONFIG_DCACHE_LINE_SIZE_DETECT
161 return cache_data_line_size_get();
162#elif (CONFIG_DCACHE_LINE_SIZE != 0)
163 return CONFIG_DCACHE_LINE_SIZE;
164#else
165 return DT_PROP_OR(CPU, d_cache_line_size, 0);
166#endif
167}
168
169/*
170 *
171 * @brief Get the i-cache line size.
172 *
173 * The API is provided to get the i-cache line size.
174 *
175 * @return size of the i-cache line or 0 if the i-cache is not enabled.
176 */
177static inline size_t sys_cache_instr_line_size_get(void)
178{
179#ifdef CONFIG_ICACHE_LINE_SIZE_DETECT
180 return cache_instr_line_size_get();
181#elif (CONFIG_ICACHE_LINE_SIZE != 0)
182 return CONFIG_ICACHE_LINE_SIZE;
183#else
184 return DT_PROP_OR(CPU, i_cache_line_size, 0);
185#endif
186}
187
188#include <syscalls/cache.h>
189#ifdef __cplusplus
190}
191#endif
192
193#endif /* ZEPHYR_INCLUDE_CACHE_H_ */
#define K_CACHE_WB
Definition: cache.h:27
int sys_cache_data_all(int op)
int sys_cache_instr_range(void *addr, size_t size, int op)
int sys_cache_instr_all(int op)
static size_t sys_cache_data_line_size_get(void)
Get the d-cache line size.
Definition: cache.h:158
int sys_cache_data_range(void *addr, size_t size, int op)
#define CPU
Definition: cache.h:148
static size_t sys_cache_instr_line_size_get(void)
Definition: cache.h:177
#define DT_PROP_OR(node_id, prop, default_value)
Like DT_PROP(), but with a fallback to default_value.
Definition: devicetree.h:760
#define ENOTSUP
Definition: errno.h:115
Public kernel APIs.