xref: /freebsd/stand/libsa/powerpc/syncicache.c (revision dcc20bced51583df83e49af269ff7be5fb48dbf8)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
3ca987d46SWarner Losh  * Copyright (C) 1995-1997, 1999 TooLs GmbH.
4ca987d46SWarner Losh  * All rights reserved.
5ca987d46SWarner Losh  *
6ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
7ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
8ca987d46SWarner Losh  * are met:
9ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
10ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
11ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
12ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
13ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
14ca987d46SWarner Losh  * 3. All advertising materials mentioning features or use of this software
15ca987d46SWarner Losh  *    must display the following acknowledgement:
16ca987d46SWarner Losh  *	This product includes software developed by TooLs GmbH.
17ca987d46SWarner Losh  * 4. The name of TooLs GmbH may not be used to endorse or promote products
18ca987d46SWarner Losh  *    derived from this software without specific prior written permission.
19ca987d46SWarner Losh  *
20ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21ca987d46SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22ca987d46SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23ca987d46SWarner Losh  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24ca987d46SWarner Losh  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25ca987d46SWarner Losh  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26ca987d46SWarner Losh  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27ca987d46SWarner Losh  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28ca987d46SWarner Losh  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29ca987d46SWarner Losh  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ca987d46SWarner Losh  *
31ca987d46SWarner Losh  * $NetBSD: syncicache.c,v 1.2 1999/05/05 12:36:40 tsubai Exp $
32ca987d46SWarner Losh  */
33ca987d46SWarner Losh 
34ca987d46SWarner Losh #include <sys/param.h>
35ca987d46SWarner Losh #if	defined(_KERNEL) || defined(_STANDALONE)
36ca987d46SWarner Losh #include <sys/time.h>
37ca987d46SWarner Losh #include <sys/proc.h>
38ca987d46SWarner Losh #include <vm/vm.h>
39ca987d46SWarner Losh #endif
40ca987d46SWarner Losh #include <sys/sysctl.h>
41ca987d46SWarner Losh 
42ca987d46SWarner Losh #include <machine/cpu.h>
43ca987d46SWarner Losh #include <machine/md_var.h>
44ca987d46SWarner Losh 
45ca987d46SWarner Losh #ifdef _STANDALONE
46ca987d46SWarner Losh int cacheline_size = 32;
47ca987d46SWarner Losh #endif
48ca987d46SWarner Losh 
49ca987d46SWarner Losh #if	!defined(_KERNEL) && !defined(_STANDALONE)
50ca987d46SWarner Losh #include <stdlib.h>
51ca987d46SWarner Losh 
52ca987d46SWarner Losh int cacheline_size = 0;
53ca987d46SWarner Losh 
54ca987d46SWarner Losh static void getcachelinesize(void);
55ca987d46SWarner Losh 
56ca987d46SWarner Losh static void
getcachelinesize(void)57*dcc20bceSWarner Losh getcachelinesize(void)
58ca987d46SWarner Losh {
59ca987d46SWarner Losh 	static int	cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
60ca987d46SWarner Losh 	int		clen;
61ca987d46SWarner Losh 
62ca987d46SWarner Losh 	clen = sizeof(cacheline_size);
63ca987d46SWarner Losh 
64ca987d46SWarner Losh 	if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]),
65ca987d46SWarner Losh 	    &cacheline_size, &clen, NULL, 0) < 0 || !cacheline_size) {
66ca987d46SWarner Losh 		abort();
67ca987d46SWarner Losh 	}
68ca987d46SWarner Losh }
69ca987d46SWarner Losh #endif
70ca987d46SWarner Losh 
71ca987d46SWarner Losh void
__syncicache(void * from,int len)72ca987d46SWarner Losh __syncicache(void *from, int len)
73ca987d46SWarner Losh {
74ca987d46SWarner Losh 	int	l, off;
75ca987d46SWarner Losh 	char	*p;
76ca987d46SWarner Losh 
77ca987d46SWarner Losh #if	!defined(_KERNEL) && !defined(_STANDALONE)
78ca987d46SWarner Losh 	if (!cacheline_size)
79ca987d46SWarner Losh 		getcachelinesize();
80ca987d46SWarner Losh #endif
81ca987d46SWarner Losh 
82ca987d46SWarner Losh 	off = (u_int)from & (cacheline_size - 1);
83ca987d46SWarner Losh 	l = len += off;
84ca987d46SWarner Losh 	p = (char *)from - off;
85ca987d46SWarner Losh 
86ca987d46SWarner Losh 	do {
87ca987d46SWarner Losh 		__asm __volatile ("dcbst 0,%0" :: "r"(p));
88ca987d46SWarner Losh 		p += cacheline_size;
89ca987d46SWarner Losh 	} while ((l -= cacheline_size) > 0);
90ca987d46SWarner Losh 	__asm __volatile ("sync");
91ca987d46SWarner Losh 	p = (char *)from - off;
92ca987d46SWarner Losh 	do {
93ca987d46SWarner Losh 		__asm __volatile ("icbi 0,%0" :: "r"(p));
94ca987d46SWarner Losh 		p += cacheline_size;
95ca987d46SWarner Losh 	} while ((len -= cacheline_size) > 0);
96ca987d46SWarner Losh 	__asm __volatile ("sync; isync");
97ca987d46SWarner Losh }
98ca987d46SWarner Losh 
99