2015-04-11 07:44:37 +08:00
|
|
|
/* cache.c - cache manipulation */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-04-11 07:44:37 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-11 07:44:37 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-11 07:44:37 +08:00
|
|
|
*/
|
|
|
|
/*
|
2015-10-21 00:42:33 +08:00
|
|
|
* DESCRIPTION
|
|
|
|
* This module contains functions for manipulation caches.
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
#include <nanokernel.h>
|
2015-05-29 01:56:47 +08:00
|
|
|
#include <arch/cpu.h>
|
2015-04-11 07:44:37 +08:00
|
|
|
#include <misc/util.h>
|
2015-09-19 04:36:57 +08:00
|
|
|
#include <toolchain.h>
|
|
|
|
#include <cache.h>
|
|
|
|
#include <cache_private.h>
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-09-19 04:36:57 +08:00
|
|
|
#if defined(CONFIG_CACHE_FLUSHING)
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-09-19 04:36:57 +08: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-11 07:44:37 +08:00
|
|
|
#error Cannot use this implementation with a cache line size of 0
|
|
|
|
#endif
|
|
|
|
|
2015-07-02 05:22:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2015-09-19 04:36:57 +08:00
|
|
|
* @brief Flush cache lines to main memory
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
|
|
|
* No alignment is required for either <virt> or <size>, but since
|
2015-09-19 04:18:15 +08:00
|
|
|
* sys_cache_flush() iterates on the cache lines, a cache line alignment for
|
|
|
|
* both is optimal.
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
2015-09-19 04:36:57 +08:00
|
|
|
* The cache line size is specified either via the CONFIG_CACHE_LINE_SIZE
|
|
|
|
* kconfig option or it is detected at runtime.
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
2015-07-02 05:29:04 +08:00
|
|
|
* @return N/A
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-09-19 04:36:57 +08:00
|
|
|
_sys_cache_flush_sig(_cache_flush_clflush)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
int end;
|
|
|
|
|
2015-09-19 04:36:57 +08:00
|
|
|
size = ROUND_UP(size, sys_cache_line_size);
|
2015-04-11 07:44:37 +08:00
|
|
|
end = virt + size;
|
|
|
|
|
2015-09-19 04:36:57 +08:00
|
|
|
for (; virt < end; virt += sys_cache_line_size) {
|
2015-04-11 07:44:37 +08:00
|
|
|
__asm__ volatile("clflush %0;\n\t" : : "m"(virt));
|
|
|
|
}
|
|
|
|
|
|
|
|
__asm__ volatile("mfence;\n\t");
|
|
|
|
}
|
|
|
|
|
2015-09-19 04:36:57 +08: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_CLFLUSH_DETECT)
|
|
|
|
_sys_cache_flush_t *sys_cache_flush;
|
|
|
|
static void init_cache_flush(void)
|
|
|
|
{
|
|
|
|
if (_is_clflush_available()) {
|
|
|
|
sys_cache_flush = _cache_flush_clflush;
|
|
|
|
} else {
|
|
|
|
sys_cache_flush = _cache_flush_wbinvd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define init_cache_flush() do { } while ((0))
|
|
|
|
FUNC_ALIAS(_cache_flush_clflush, sys_cache_flush, void);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CACHE_FLUSHING */
|
|
|
|
|
|
|
|
#if defined(CONFIG_CACHE_LINE_SIZE_DETECT)
|
|
|
|
size_t sys_cache_line_size;
|
|
|
|
static void init_cache_line_size(void)
|
|
|
|
{
|
|
|
|
sys_cache_line_size = _cache_line_size_get();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define init_cache_line_size() do { } while ((0))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int init_cache(struct device *unused)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
|
|
|
init_cache_flush();
|
|
|
|
init_cache_line_size();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_DEVICE_INIT_CONFIG(cache, "", init_cache, NULL);
|
2015-10-27 03:56:02 +08:00
|
|
|
SYS_DEFINE_DEVICE(cache, NULL, PRIMARY, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
|
2015-09-19 04:36:57 +08:00
|
|
|
|
|
|
|
#endif /* CONFIG_CLFLUSH_DETECT || CONFIG_CACHE_LINE_SIZE_DETECT */
|