Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 7 | #ifndef ZEPHYR_INCLUDE_CACHE_H_ |
| 8 | #define ZEPHYR_INCLUDE_CACHE_H_ |
Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 9 | |
Anas Nashif | ea8c6aad | 2016-12-23 07:32:56 -0500 | [diff] [blame] | 10 | #include <kernel.h> |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 11 | #include <kernel_structs.h> |
Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 12 | |
Peter Mitsis | a0e4568 | 2016-01-22 12:38:49 -0500 | [diff] [blame] | 13 | #ifdef __cplusplus |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 17 | /** |
| 18 | * Common operations for the caches |
| 19 | * |
| 20 | * WB means write-back and intends to transfer dirty cache lines to memory in a |
| 21 | * copy-back cache policy. May be a no-op in write-back cache policy. |
| 22 | * |
| 23 | * INVD means invalidate and will mark cache lines as not valid. A future |
| 24 | * access to the associated address is guaranteed to generate a memory fetch. |
| 25 | */ |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 26 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 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) |
Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 30 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 31 | #if defined(CONFIG_HAS_EXTERNAL_CACHE) |
Benjamin Walsh | 7b526e2 | 2015-10-30 17:04:00 -0400 | [diff] [blame] | 32 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 33 | /* Driver interface mirrored in include/drivers/cache.h */ |
Aastha Grover | 2d9b459 | 2020-07-16 12:32:24 -0700 | [diff] [blame] | 34 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 35 | /* Enable d-cache */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 36 | extern void cache_data_enable(void); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 37 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 38 | /* Disable d-cache */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 39 | extern void cache_data_disable(void); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 40 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 41 | /* Enable i-cache */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 42 | extern void cache_instr_enable(void); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 43 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 44 | /* Disable i-cache */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 45 | extern void cache_instr_disable(void); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 46 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 47 | /* Write-back / Invalidate / Write-back + Invalidate all d-cache */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 48 | extern int cache_data_all(int op); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 49 | |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 50 | /* Write-back / Invalidate / Write-back + Invalidate d-cache lines */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 51 | extern int cache_data_range(void *addr, size_t size, int op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 52 | |
| 53 | /* Write-back / Invalidate / Write-back + Invalidate all i-cache */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 54 | extern int cache_instr_all(int op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 55 | |
| 56 | /* Write-back / Invalidate / Write-back + Invalidate i-cache lines */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 57 | extern int cache_instr_range(void *addr, size_t size, int op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 58 | |
| 59 | #else |
| 60 | |
| 61 | /* Hooks into arch code */ |
| 62 | |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 63 | #define cache_data_enable arch_dcache_enable |
| 64 | #define cache_data_disable arch_dcache_disable |
| 65 | #define cache_instr_enable arch_icache_enable |
| 66 | #define cache_instr_disable arch_icache_disable |
| 67 | #define cache_data_all(op) arch_dcache_all(op) |
| 68 | #define cache_data_range(addr, size, op) arch_dcache_range(addr, size, op) |
| 69 | #define cache_instr_all(op) arch_icache_all(op) |
| 70 | #define cache_instr_range(addr, size, op) arch_icache_range(addr, size, op) |
| 71 | #define cache_data_line_size_get arch_dcache_line_size_get |
| 72 | #define cache_instr_line_size_get arch_icache_line_size_get |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 73 | |
| 74 | #endif |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 75 | |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 76 | __syscall int sys_cache_data_all(int op); |
| 77 | static inline int z_impl_sys_cache_data_all(int op) |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 78 | { |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 79 | #if defined(CONFIG_CACHE_MANAGEMENT) |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 80 | return cache_data_all(op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 81 | #endif |
Carlo Caione | 6fed3fd | 2021-05-24 11:51:00 +0200 | [diff] [blame] | 82 | ARG_UNUSED(op); |
| 83 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 84 | return -ENOTSUP; |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 85 | } |
Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 86 | |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 87 | __syscall int sys_cache_data_range(void *addr, size_t size, int op); |
| 88 | static inline int z_impl_sys_cache_data_range(void *addr, size_t size, int op) |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 89 | { |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 90 | #if defined(CONFIG_CACHE_MANAGEMENT) |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 91 | return cache_data_range(addr, size, op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 92 | #endif |
Carlo Caione | 6fed3fd | 2021-05-24 11:51:00 +0200 | [diff] [blame] | 93 | ARG_UNUSED(addr); |
| 94 | ARG_UNUSED(size); |
| 95 | ARG_UNUSED(op); |
| 96 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 97 | return -ENOTSUP; |
| 98 | } |
| 99 | |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 100 | __syscall int sys_cache_instr_all(int op); |
| 101 | static inline int z_impl_sys_cache_instr_all(int op) |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 102 | { |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 103 | #if defined(CONFIG_CACHE_MANAGEMENT) |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 104 | return cache_instr_all(op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 105 | #endif |
Carlo Caione | 6fed3fd | 2021-05-24 11:51:00 +0200 | [diff] [blame] | 106 | ARG_UNUSED(op); |
| 107 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 108 | return -ENOTSUP; |
| 109 | } |
| 110 | |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 111 | __syscall int sys_cache_instr_range(void *addr, size_t size, int op); |
| 112 | static inline int z_impl_sys_cache_instr_range(void *addr, size_t size, int op) |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 113 | { |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 114 | #if defined(CONFIG_CACHE_MANAGEMENT) |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 115 | return cache_instr_range(addr, size, op); |
Carlo Caione | e233326 | 2021-04-28 10:38:27 +0200 | [diff] [blame] | 116 | #endif |
Carlo Caione | 6fed3fd | 2021-05-24 11:51:00 +0200 | [diff] [blame] | 117 | ARG_UNUSED(addr); |
| 118 | ARG_UNUSED(size); |
| 119 | ARG_UNUSED(op); |
| 120 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 121 | return -ENOTSUP; |
| 122 | } |
| 123 | |
Carlo Caione | 9d563e8 | 2020-12-02 21:42:42 +0100 | [diff] [blame] | 124 | #ifdef CONFIG_LIBMETAL |
| 125 | static inline void sys_cache_flush(void *addr, size_t size) |
| 126 | { |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 127 | sys_cache_data_range(addr, size, K_CACHE_WB); |
Carlo Caione | 9d563e8 | 2020-12-02 21:42:42 +0100 | [diff] [blame] | 128 | } |
| 129 | #endif |
| 130 | |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 131 | #define CPU DT_PATH(cpus, cpu_0) |
| 132 | |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 133 | /** |
| 134 | * |
| 135 | * @brief Get the d-cache line size. |
| 136 | * |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 137 | * The API is provided to get the d-cache line size. |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 138 | * |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 139 | * @return size of the d-cache line or 0 if the d-cache is not enabled. |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 140 | */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 141 | static inline size_t sys_cache_data_line_size_get(void) |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 142 | { |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 143 | #ifdef CONFIG_DCACHE_LINE_SIZE_DETECT |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 144 | return cache_data_line_size_get(); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 145 | #elif (CONFIG_DCACHE_LINE_SIZE != 0) |
| 146 | return CONFIG_DCACHE_LINE_SIZE; |
Benjamin Walsh | 7b526e2 | 2015-10-30 17:04:00 -0400 | [diff] [blame] | 147 | #else |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 148 | return DT_PROP_OR(CPU, d_cache_line_size, 0); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * |
| 154 | * @brief Get the i-cache line size. |
| 155 | * |
| 156 | * The API is provided to get the i-cache line size. |
| 157 | * |
| 158 | * @return size of the i-cache line or 0 if the i-cache is not enabled. |
| 159 | */ |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 160 | static inline size_t sys_cache_instr_line_size_get(void) |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 161 | { |
| 162 | #ifdef CONFIG_ICACHE_LINE_SIZE_DETECT |
Carlo Caione | f000695 | 2021-05-04 16:26:10 +0200 | [diff] [blame] | 163 | return cache_instr_line_size_get(); |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 164 | #elif (CONFIG_ICACHE_LINE_SIZE != 0) |
| 165 | return CONFIG_ICACHE_LINE_SIZE; |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 166 | #else |
Carlo Caione | e77c841 | 2020-12-02 12:38:58 +0100 | [diff] [blame] | 167 | return DT_PROP_OR(CPU, i_cache_line_size, 0); |
| 168 | #endif |
Aastha Grover | 97ecad6 | 2020-04-28 13:14:54 -0700 | [diff] [blame] | 169 | } |
Benjamin Walsh | 334c14e | 2015-09-18 16:36:57 -0400 | [diff] [blame] | 170 | |
Aastha Grover | 2d9b459 | 2020-07-16 12:32:24 -0700 | [diff] [blame] | 171 | #include <syscalls/cache.h> |
Peter Mitsis | a0e4568 | 2016-01-22 12:38:49 -0500 | [diff] [blame] | 172 | #ifdef __cplusplus |
| 173 | } |
| 174 | #endif |
| 175 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 176 | #endif /* ZEPHYR_INCLUDE_CACHE_H_ */ |