1*2fe8fb19SBen Gras /* $NetBSD: syncicache.c,v 1.15 2008/03/18 20:11:43 he Exp $ */
2*2fe8fb19SBen Gras
3*2fe8fb19SBen Gras /*
4*2fe8fb19SBen Gras * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
5*2fe8fb19SBen Gras * Copyright (C) 1995-1997, 1999 TooLs GmbH.
6*2fe8fb19SBen Gras * All rights reserved.
7*2fe8fb19SBen Gras *
8*2fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
9*2fe8fb19SBen Gras * modification, are permitted provided that the following conditions
10*2fe8fb19SBen Gras * are met:
11*2fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
12*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
13*2fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
14*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
15*2fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
16*2fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software
17*2fe8fb19SBen Gras * must display the following acknowledgement:
18*2fe8fb19SBen Gras * This product includes software developed by TooLs GmbH.
19*2fe8fb19SBen Gras * 4. The name of TooLs GmbH may not be used to endorse or promote products
20*2fe8fb19SBen Gras * derived from this software without specific prior written permission.
21*2fe8fb19SBen Gras *
22*2fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23*2fe8fb19SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24*2fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25*2fe8fb19SBen Gras * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26*2fe8fb19SBen Gras * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27*2fe8fb19SBen Gras * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28*2fe8fb19SBen Gras * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*2fe8fb19SBen Gras * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30*2fe8fb19SBen Gras * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31*2fe8fb19SBen Gras * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*2fe8fb19SBen Gras */
33*2fe8fb19SBen Gras #include <sys/param.h>
34*2fe8fb19SBen Gras #if defined(_KERNEL)
35*2fe8fb19SBen Gras #include <sys/time.h>
36*2fe8fb19SBen Gras #include <sys/proc.h>
37*2fe8fb19SBen Gras #include <uvm/uvm_extern.h>
38*2fe8fb19SBen Gras #endif
39*2fe8fb19SBen Gras #if !defined(_STANDALONE)
40*2fe8fb19SBen Gras #include <sys/sysctl.h>
41*2fe8fb19SBen Gras #endif
42*2fe8fb19SBen Gras
43*2fe8fb19SBen Gras #include <machine/cpu.h>
44*2fe8fb19SBen Gras
45*2fe8fb19SBen Gras
46*2fe8fb19SBen Gras #if defined(_STANDALONE)
47*2fe8fb19SBen Gras #ifndef CACHELINESIZE
48*2fe8fb19SBen Gras #error "Must know the size of a cache line"
49*2fe8fb19SBen Gras #endif
50*2fe8fb19SBen Gras static struct cache_info _cache_info = {
51*2fe8fb19SBen Gras CACHELINESIZE,
52*2fe8fb19SBen Gras CACHELINESIZE,
53*2fe8fb19SBen Gras CACHELINESIZE,
54*2fe8fb19SBen Gras CACHELINESIZE
55*2fe8fb19SBen Gras };
56*2fe8fb19SBen Gras #define CACHEINFO _cache_info
57*2fe8fb19SBen Gras #elif defined(_KERNEL)
58*2fe8fb19SBen Gras #define CACHEINFO (curcpu()->ci_ci)
59*2fe8fb19SBen Gras #else
60*2fe8fb19SBen Gras #include <stdlib.h>
61*2fe8fb19SBen Gras
62*2fe8fb19SBen Gras size_t __getcachelinesize (void);
63*2fe8fb19SBen Gras
64*2fe8fb19SBen Gras static int _cachelinesize = 0;
65*2fe8fb19SBen Gras
66*2fe8fb19SBen Gras static struct cache_info _cache_info;
67*2fe8fb19SBen Gras #define CACHEINFO _cache_info
68*2fe8fb19SBen Gras
69*2fe8fb19SBen Gras size_t
__getcachelinesize(void)70*2fe8fb19SBen Gras __getcachelinesize(void)
71*2fe8fb19SBen Gras {
72*2fe8fb19SBen Gras static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
73*2fe8fb19SBen Gras static int cacheinfomib[] = { CTL_MACHDEP, CPU_CACHEINFO };
74*2fe8fb19SBen Gras size_t clen = sizeof(_cache_info);
75*2fe8fb19SBen Gras
76*2fe8fb19SBen Gras if (_cachelinesize)
77*2fe8fb19SBen Gras return _cachelinesize;
78*2fe8fb19SBen Gras
79*2fe8fb19SBen Gras if (sysctl(cacheinfomib, sizeof(cacheinfomib) / sizeof(cacheinfomib[0]),
80*2fe8fb19SBen Gras &_cache_info, &clen, NULL, 0) == 0) {
81*2fe8fb19SBen Gras _cachelinesize = _cache_info.dcache_line_size;
82*2fe8fb19SBen Gras return _cachelinesize;
83*2fe8fb19SBen Gras }
84*2fe8fb19SBen Gras
85*2fe8fb19SBen Gras /* Try older deprecated sysctl */
86*2fe8fb19SBen Gras clen = sizeof(_cachelinesize);
87*2fe8fb19SBen Gras if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]),
88*2fe8fb19SBen Gras &_cachelinesize, &clen, NULL, 0) < 0
89*2fe8fb19SBen Gras || !_cachelinesize)
90*2fe8fb19SBen Gras abort();
91*2fe8fb19SBen Gras
92*2fe8fb19SBen Gras _cache_info.dcache_size = _cachelinesize;
93*2fe8fb19SBen Gras _cache_info.dcache_line_size = _cachelinesize;
94*2fe8fb19SBen Gras _cache_info.icache_size = _cachelinesize;
95*2fe8fb19SBen Gras _cache_info.icache_line_size = _cachelinesize;
96*2fe8fb19SBen Gras
97*2fe8fb19SBen Gras /* If there is no cache, indicate we have issued the sysctl. */
98*2fe8fb19SBen Gras if (!_cachelinesize)
99*2fe8fb19SBen Gras _cachelinesize = 1;
100*2fe8fb19SBen Gras
101*2fe8fb19SBen Gras return _cachelinesize;
102*2fe8fb19SBen Gras }
103*2fe8fb19SBen Gras #endif
104*2fe8fb19SBen Gras
105*2fe8fb19SBen Gras void
__syncicache(void * from,size_t len)106*2fe8fb19SBen Gras __syncicache(void *from, size_t len)
107*2fe8fb19SBen Gras {
108*2fe8fb19SBen Gras size_t l, off;
109*2fe8fb19SBen Gras size_t linesz;
110*2fe8fb19SBen Gras char *p;
111*2fe8fb19SBen Gras
112*2fe8fb19SBen Gras #if !defined(_KERNEL) && !defined(_STANDALONE)
113*2fe8fb19SBen Gras if (!_cachelinesize)
114*2fe8fb19SBen Gras __getcachelinesize();
115*2fe8fb19SBen Gras #endif
116*2fe8fb19SBen Gras
117*2fe8fb19SBen Gras if (CACHEINFO.dcache_size > 0) {
118*2fe8fb19SBen Gras linesz = CACHEINFO.dcache_line_size;
119*2fe8fb19SBen Gras off = (uintptr_t)from & (linesz - 1);
120*2fe8fb19SBen Gras l = (len + off + linesz - 1) & ~(linesz - 1);
121*2fe8fb19SBen Gras p = (char *)from - off;
122*2fe8fb19SBen Gras do {
123*2fe8fb19SBen Gras __asm volatile ("dcbst 0,%0" :: "r"(p));
124*2fe8fb19SBen Gras p += linesz;
125*2fe8fb19SBen Gras } while ((l -= linesz) != 0);
126*2fe8fb19SBen Gras }
127*2fe8fb19SBen Gras __asm volatile ("sync");
128*2fe8fb19SBen Gras
129*2fe8fb19SBen Gras if (CACHEINFO.icache_size > 0 ) {
130*2fe8fb19SBen Gras linesz = CACHEINFO.icache_line_size;
131*2fe8fb19SBen Gras off = (uintptr_t)from & (linesz - 1);
132*2fe8fb19SBen Gras l = (len + off + linesz - 1) & ~(linesz - 1);
133*2fe8fb19SBen Gras p = (char *)from - off;
134*2fe8fb19SBen Gras do {
135*2fe8fb19SBen Gras __asm volatile ("icbi 0,%0" :: "r"(p));
136*2fe8fb19SBen Gras p += linesz;
137*2fe8fb19SBen Gras } while ((l -= linesz) != 0);
138*2fe8fb19SBen Gras }
139*2fe8fb19SBen Gras __asm volatile ("sync; isync");
140*2fe8fb19SBen Gras }
141