2 * Copyright (c) 2020-2021 Arm Limited. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * CMSIS-Core(M) Level 1 Cache API for Armv7-M and later
23 #ifndef ARM_ARMV7M_CACHEL1_H
24 #define ARM_ARMV7M_CACHEL1_H
26 #if defined ( __ICCARM__ )
27 #pragma system_include /* treat file as system include file for MISRA check */
28 #elif defined (__clang__)
29 #pragma clang system_header /* treat file as system include file */
33 \ingroup CMSIS_Core_FunctionInterface
34 \defgroup CMSIS_Core_CacheFunctions Cache Functions
35 \brief Functions that configure Instruction and Data cache.
39 /* Cache Size ID Register Macros */
40 #define CCSIDR_WAYS(x) (((x) & SCB_CCSIDR_ASSOCIATIVITY_Msk) >> SCB_CCSIDR_ASSOCIATIVITY_Pos)
41 #define CCSIDR_SETS(x) (((x) & SCB_CCSIDR_NUMSETS_Msk ) >> SCB_CCSIDR_NUMSETS_Pos )
43 #ifndef __SCB_DCACHE_LINE_SIZE
44 #define __SCB_DCACHE_LINE_SIZE 32U /*!< Cortex-M7 cache line size is fixed to 32 bytes (8 words). See also register SCB_CCSIDR */
47 #ifndef __SCB_ICACHE_LINE_SIZE
48 #define __SCB_ICACHE_LINE_SIZE 32U /*!< Cortex-M7 cache line size is fixed to 32 bytes (8 words). See also register SCB_CCSIDR */
53 \details Turns on I-Cache
55 __STATIC_FORCEINLINE void SCB_EnableICache (void)
57 #if defined (__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
58 if (SCB->CCR & SCB_CCR_IC_Msk) return; /* return if ICache is already enabled */
62 SCB->ICIALLU = 0UL; /* invalidate I-Cache */
65 SCB->CCR |= (uint32_t)SCB_CCR_IC_Msk; /* enable I-Cache */
73 \brief Disable I-Cache
74 \details Turns off I-Cache
76 __STATIC_FORCEINLINE void SCB_DisableICache (void)
78 #if defined (__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
81 SCB->CCR &= ~(uint32_t)SCB_CCR_IC_Msk; /* disable I-Cache */
82 SCB->ICIALLU = 0UL; /* invalidate I-Cache */
90 \brief Invalidate I-Cache
91 \details Invalidates I-Cache
93 __STATIC_FORCEINLINE void SCB_InvalidateICache (void)
95 #if defined (__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
106 \brief I-Cache Invalidate by address
107 \details Invalidates I-Cache for the given address.
108 I-Cache is invalidated starting from a 32 byte aligned address in 32 byte granularity.
109 I-Cache memory blocks which are part of given address + given size are invalidated.
110 \param[in] addr address
111 \param[in] isize size of memory block (in number of bytes)
113 __STATIC_FORCEINLINE void SCB_InvalidateICache_by_Addr (volatile void *addr, int32_t isize)
115 #if defined (__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
117 int32_t op_size = isize + (((uint32_t)addr) & (__SCB_ICACHE_LINE_SIZE - 1U));
118 uint32_t op_addr = (uint32_t)addr /* & ~(__SCB_ICACHE_LINE_SIZE - 1U) */;
123 SCB->ICIMVAU = op_addr; /* register accepts only 32byte aligned values, only bits 31..5 are valid */
124 op_addr += __SCB_ICACHE_LINE_SIZE;
125 op_size -= __SCB_ICACHE_LINE_SIZE;
126 } while ( op_size > 0 );
136 \brief Enable D-Cache
137 \details Turns on D-Cache
139 __STATIC_FORCEINLINE void SCB_EnableDCache (void)
141 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
146 if (SCB->CCR & SCB_CCR_DC_Msk) return; /* return if DCache is already enabled */
148 SCB->CSSELR = 0U; /* select Level 1 data cache */
151 ccsidr = SCB->CCSIDR;
153 /* invalidate D-Cache */
154 sets = (uint32_t)(CCSIDR_SETS(ccsidr));
156 ways = (uint32_t)(CCSIDR_WAYS(ccsidr));
158 SCB->DCISW = (((sets << SCB_DCISW_SET_Pos) & SCB_DCISW_SET_Msk) |
159 ((ways << SCB_DCISW_WAY_Pos) & SCB_DCISW_WAY_Msk) );
160 #if defined ( __CC_ARM )
161 __schedule_barrier();
163 } while (ways-- != 0U);
164 } while(sets-- != 0U);
167 SCB->CCR |= (uint32_t)SCB_CCR_DC_Msk; /* enable D-Cache */
176 \brief Disable D-Cache
177 \details Turns off D-Cache
179 __STATIC_FORCEINLINE void SCB_DisableDCache (void)
181 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
187 #if ((defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__))
188 __ALIGNED(__SCB_DCACHE_LINE_SIZE)
192 SCB->CSSELR = 0U; /* select Level 1 data cache */
195 SCB->CCR &= ~(uint32_t)SCB_CCR_DC_Msk; /* disable D-Cache */
198 #if !defined(__OPTIMIZE__)
200 * For the endless loop issue with no optimization builds.
201 * More details, see https://github.com/ARM-software/CMSIS_5/issues/620
203 * The issue only happens when local variables are in stack. If
204 * local variables are saved in general purpose register, then the function
207 * When local variables are in stack, after disabling the cache, flush the
208 * local variables cache line for data consistency.
210 /* Clean and invalidate the local variable cache. */
211 #if defined(__ICCARM__)
212 /* As we can't align the stack to the cache line size, invalidate each of the variables */
213 SCB->DCCIMVAC = (uint32_t)&locals.sets;
214 SCB->DCCIMVAC = (uint32_t)&locals.ways;
215 SCB->DCCIMVAC = (uint32_t)&locals.ccsidr;
217 SCB->DCCIMVAC = (uint32_t)&locals;
223 locals.ccsidr = SCB->CCSIDR;
224 /* clean & invalidate D-Cache */
225 locals.sets = (uint32_t)(CCSIDR_SETS(locals.ccsidr));
227 locals.ways = (uint32_t)(CCSIDR_WAYS(locals.ccsidr));
229 SCB->DCCISW = (((locals.sets << SCB_DCCISW_SET_Pos) & SCB_DCCISW_SET_Msk) |
230 ((locals.ways << SCB_DCCISW_WAY_Pos) & SCB_DCCISW_WAY_Msk) );
231 #if defined ( __CC_ARM )
232 __schedule_barrier();
234 } while (locals.ways-- != 0U);
235 } while(locals.sets-- != 0U);
244 \brief Invalidate D-Cache
245 \details Invalidates D-Cache
247 __STATIC_FORCEINLINE void SCB_InvalidateDCache (void)
249 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
254 SCB->CSSELR = 0U; /* select Level 1 data cache */
257 ccsidr = SCB->CCSIDR;
259 /* invalidate D-Cache */
260 sets = (uint32_t)(CCSIDR_SETS(ccsidr));
262 ways = (uint32_t)(CCSIDR_WAYS(ccsidr));
264 SCB->DCISW = (((sets << SCB_DCISW_SET_Pos) & SCB_DCISW_SET_Msk) |
265 ((ways << SCB_DCISW_WAY_Pos) & SCB_DCISW_WAY_Msk) );
266 #if defined ( __CC_ARM )
267 __schedule_barrier();
269 } while (ways-- != 0U);
270 } while(sets-- != 0U);
280 \details Cleans D-Cache
282 __STATIC_FORCEINLINE void SCB_CleanDCache (void)
284 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
289 SCB->CSSELR = 0U; /* select Level 1 data cache */
292 ccsidr = SCB->CCSIDR;
295 sets = (uint32_t)(CCSIDR_SETS(ccsidr));
297 ways = (uint32_t)(CCSIDR_WAYS(ccsidr));
299 SCB->DCCSW = (((sets << SCB_DCCSW_SET_Pos) & SCB_DCCSW_SET_Msk) |
300 ((ways << SCB_DCCSW_WAY_Pos) & SCB_DCCSW_WAY_Msk) );
301 #if defined ( __CC_ARM )
302 __schedule_barrier();
304 } while (ways-- != 0U);
305 } while(sets-- != 0U);
314 \brief Clean & Invalidate D-Cache
315 \details Cleans and Invalidates D-Cache
317 __STATIC_FORCEINLINE void SCB_CleanInvalidateDCache (void)
319 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
324 SCB->CSSELR = 0U; /* select Level 1 data cache */
327 ccsidr = SCB->CCSIDR;
329 /* clean & invalidate D-Cache */
330 sets = (uint32_t)(CCSIDR_SETS(ccsidr));
332 ways = (uint32_t)(CCSIDR_WAYS(ccsidr));
334 SCB->DCCISW = (((sets << SCB_DCCISW_SET_Pos) & SCB_DCCISW_SET_Msk) |
335 ((ways << SCB_DCCISW_WAY_Pos) & SCB_DCCISW_WAY_Msk) );
336 #if defined ( __CC_ARM )
337 __schedule_barrier();
339 } while (ways-- != 0U);
340 } while(sets-- != 0U);
349 \brief D-Cache Invalidate by address
350 \details Invalidates D-Cache for the given address.
351 D-Cache is invalidated starting from a 32 byte aligned address in 32 byte granularity.
352 D-Cache memory blocks which are part of given address + given size are invalidated.
353 \param[in] addr address
354 \param[in] dsize size of memory block (in number of bytes)
356 __STATIC_FORCEINLINE void SCB_InvalidateDCache_by_Addr (volatile void *addr, int32_t dsize)
358 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
360 int32_t op_size = dsize + (((uint32_t)addr) & (__SCB_DCACHE_LINE_SIZE - 1U));
361 uint32_t op_addr = (uint32_t)addr /* & ~(__SCB_DCACHE_LINE_SIZE - 1U) */;
366 SCB->DCIMVAC = op_addr; /* register accepts only 32byte aligned values, only bits 31..5 are valid */
367 op_addr += __SCB_DCACHE_LINE_SIZE;
368 op_size -= __SCB_DCACHE_LINE_SIZE;
369 } while ( op_size > 0 );
379 \brief D-Cache Clean by address
380 \details Cleans D-Cache for the given address
381 D-Cache is cleaned starting from a 32 byte aligned address in 32 byte granularity.
382 D-Cache memory blocks which are part of given address + given size are cleaned.
383 \param[in] addr address
384 \param[in] dsize size of memory block (in number of bytes)
386 __STATIC_FORCEINLINE void SCB_CleanDCache_by_Addr (volatile void *addr, int32_t dsize)
388 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
390 int32_t op_size = dsize + (((uint32_t)addr) & (__SCB_DCACHE_LINE_SIZE - 1U));
391 uint32_t op_addr = (uint32_t)addr /* & ~(__SCB_DCACHE_LINE_SIZE - 1U) */;
396 SCB->DCCMVAC = op_addr; /* register accepts only 32byte aligned values, only bits 31..5 are valid */
397 op_addr += __SCB_DCACHE_LINE_SIZE;
398 op_size -= __SCB_DCACHE_LINE_SIZE;
399 } while ( op_size > 0 );
409 \brief D-Cache Clean and Invalidate by address
410 \details Cleans and invalidates D_Cache for the given address
411 D-Cache is cleaned and invalidated starting from a 32 byte aligned address in 32 byte granularity.
412 D-Cache memory blocks which are part of given address + given size are cleaned and invalidated.
413 \param[in] addr address (aligned to 32-byte boundary)
414 \param[in] dsize size of memory block (in number of bytes)
416 __STATIC_FORCEINLINE void SCB_CleanInvalidateDCache_by_Addr (volatile void *addr, int32_t dsize)
418 #if defined (__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
420 int32_t op_size = dsize + (((uint32_t)addr) & (__SCB_DCACHE_LINE_SIZE - 1U));
421 uint32_t op_addr = (uint32_t)addr /* & ~(__SCB_DCACHE_LINE_SIZE - 1U) */;
426 SCB->DCCIMVAC = op_addr; /* register accepts only 32byte aligned values, only bits 31..5 are valid */
427 op_addr += __SCB_DCACHE_LINE_SIZE;
428 op_size -= __SCB_DCACHE_LINE_SIZE;
429 } while ( op_size > 0 );
437 /*@} end of CMSIS_Core_CacheFunctions */
439 #endif /* ARM_ARMV7M_CACHEL1_H */