#include <sys/param.h>
#if defined(_KERNEL)
#include <sys/time.h>
#include <sys/proc.h>
#include <uvm/uvm_extern.h>
#endif
#if !defined(_STANDALONE)
#include <sys/sysctl.h>
#endif
#include <machine/cpu.h>
#if defined(_STANDALONE)
#ifndef CACHELINESIZE
#error "Must know the size of a cache line"
#endif
static struct cache_info _cache_info = {
CACHELINESIZE,
CACHELINESIZE,
CACHELINESIZE,
CACHELINESIZE
};
#define CACHEINFO _cache_info
#elif defined(_KERNEL)
#define CACHEINFO (curcpu()->ci_ci)
#else
#include <stdlib.h>
size_t __getcachelinesize (void);
static int _cachelinesize = 0;
static struct cache_info _cache_info;
#define CACHEINFO _cache_info
size_t
__getcachelinesize(void)
{
static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
static int cacheinfomib[] = { CTL_MACHDEP, CPU_CACHEINFO };
size_t clen = sizeof(_cache_info);
if (_cachelinesize)
return _cachelinesize;
if (sysctl(cacheinfomib, sizeof(cacheinfomib) / sizeof(cacheinfomib[0]),
&_cache_info, &clen, NULL, 0) == 0) {
_cachelinesize = _cache_info.dcache_line_size;
return _cachelinesize;
}
clen = sizeof(_cachelinesize);
if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]),
&_cachelinesize, &clen, NULL, 0) < 0
|| !_cachelinesize)
abort();
_cache_info.dcache_size = _cachelinesize;
_cache_info.dcache_line_size = _cachelinesize;
_cache_info.icache_size = _cachelinesize;
_cache_info.icache_line_size = _cachelinesize;
if (!_cachelinesize)
_cachelinesize = 1;
return _cachelinesize;
}
#endif
void
__syncicache(void *from, size_t len)
{
size_t l, off;
size_t linesz;
char *p;
#if !defined(_KERNEL) && !defined(_STANDALONE)
if (!_cachelinesize)
__getcachelinesize();
#endif
if (CACHEINFO.dcache_size > 0) {
linesz = CACHEINFO.dcache_line_size;
off = (uintptr_t)from & (linesz - 1);
l = (len + off + linesz - 1) & ~(linesz - 1);
p = (char *)from - off;
do {
__asm volatile ("dcbst 0,%0" :: "r"(p));
p += linesz;
} while ((l -= linesz) != 0);
}
__asm volatile ("sync");
if (CACHEINFO.icache_size > 0 ) {
linesz = CACHEINFO.icache_line_size;
off = (uintptr_t)from & (linesz - 1);
l = (len + off + linesz - 1) & ~(linesz - 1);
p = (char *)from - off;
do {
__asm volatile ("icbi 0,%0" :: "r"(p));
p += linesz;
} while ((l -= linesz) != 0);
}
__asm volatile ("sync; isync");
}