2015-04-10 16:44:37 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-04-10 16:44:37 -07:00
|
|
|
*/
|
2015-12-04 10:09:39 -05:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Cache manipulation
|
|
|
|
*
|
2015-10-20 09:42:33 -07:00
|
|
|
* This module contains functions for manipulation caches.
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2016-12-23 08:35:34 -05:00
|
|
|
#include <kernel.h>
|
2015-05-28 10:56:47 -07:00
|
|
|
#include <arch/cpu.h>
|
2019-06-26 10:33:55 -04:00
|
|
|
#include <sys/util.h>
|
2015-09-18 16:36:57 -04:00
|
|
|
#include <toolchain.h>
|
|
|
|
#include <cache.h>
|
2018-11-21 13:17:12 -08:00
|
|
|
#include <stdbool.h>
|
2019-06-29 12:43:18 -07:00
|
|
|
#include <cache.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* these functions are defined in cache_s.S
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern int z_is_clflush_available(void);
|
|
|
|
extern void z_cache_flush_wbinvd(vaddr_t addr, size_t len);
|
|
|
|
extern size_t z_cache_line_size_get(void);
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-09-18 16:36:57 -04:00
|
|
|
#if defined(CONFIG_CLFLUSH_INSTRUCTION_SUPPORTED) || \
|
|
|
|
defined(CONFIG_CLFLUSH_DETECT)
|
|
|
|
|
|
|
|
#if (CONFIG_CACHE_LINE_SIZE == 0) && !defined(CONFIG_CACHE_LINE_SIZE_DETECT)
|
2015-04-10 16:44:37 -07:00
|
|
|
#error Cannot use this implementation with a cache line size of 0
|
|
|
|
#endif
|
|
|
|
|
2015-07-01 17:22:39 -04:00
|
|
|
/**
|
|
|
|
*
|
2015-09-18 16:36:57 -04:00
|
|
|
* @brief Flush cache lines to main memory
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
|
|
|
* No alignment is required for either <virt> or <size>, but since
|
2015-09-18 16:18:15 -04:00
|
|
|
* sys_cache_flush() iterates on the cache lines, a cache line alignment for
|
|
|
|
* both is optimal.
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-09-18 16:36:57 -04:00
|
|
|
* The cache line size is specified either via the CONFIG_CACHE_LINE_SIZE
|
|
|
|
* kconfig option or it is detected at runtime.
|
2015-07-01 17:22:39 -04:00
|
|
|
*
|
2015-07-01 17:29:04 -04:00
|
|
|
* @return N/A
|
2015-07-01 17:22:39 -04:00
|
|
|
*/
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2020-06-17 15:50:47 -07:00
|
|
|
void arch_dcache_flush(void *start_addr, size_t size)
|
2015-04-10 16:44:37 -07:00
|
|
|
{
|
2020-06-17 15:50:47 -07:00
|
|
|
uintptr_t end;
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2015-09-18 16:36:57 -04:00
|
|
|
size = ROUND_UP(size, sys_cache_line_size);
|
2020-06-17 15:50:47 -07:00
|
|
|
end = (uintptr_t)start_addr + size;
|
2015-04-10 16:44:37 -07:00
|
|
|
|
2020-06-17 15:50:47 -07:00
|
|
|
for (; (uintptr_t)start_addr < end; (uintptr_t)start_addr += sys_cache_line_size) {
|
|
|
|
__asm__ volatile("clflush %0;\n\t" : : "m"((uintptr_t)start_addr));
|
2015-04-10 16:44:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
__asm__ volatile("mfence;\n\t");
|
|
|
|
}
|
|
|
|
|
2015-09-18 16:36:57 -04:00
|
|
|
#endif /* CONFIG_CLFLUSH_INSTRUCTION_SUPPORTED || CLFLUSH_DETECT */
|
|
|
|
|
|
|
|
#if defined(CONFIG_CLFLUSH_DETECT) || defined(CONFIG_CACHE_LINE_SIZE_DETECT)
|
|
|
|
|
|
|
|
#include <init.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_CACHE_LINE_SIZE_DETECT)
|
|
|
|
size_t sys_cache_line_size;
|
|
|
|
static void init_cache_line_size(void)
|
|
|
|
{
|
2019-03-14 09:20:46 -06:00
|
|
|
sys_cache_line_size = z_cache_line_size_get();
|
2015-09-18 16:36:57 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define init_cache_line_size() do { } while ((0))
|
|
|
|
#endif
|
|
|
|
|
2020-04-28 13:14:54 -07:00
|
|
|
size_t arch_cache_line_size_get(void)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_CACHE_LINE_SIZE_DETECT)
|
|
|
|
return sys_cache_line_size;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int init_cache(const struct device *unused)
|
2015-09-18 16:36:57 -04:00
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
|
|
|
init_cache_line_size();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-08 11:06:55 -08:00
|
|
|
SYS_INIT(init_cache, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|
2015-09-18 16:36:57 -04:00
|
|
|
|
|
|
|
#endif /* CONFIG_CLFLUSH_DETECT || CONFIG_CACHE_LINE_SIZE_DETECT */
|