xref: /freebsd-src/lib/libc/powerpc/gen/syncicache.c (revision eba230afba4932f02a1ca44efc797cf7499a5cb0)
14c01c0b9SNathan Whitehorn /*-
2*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*d915a14eSPedro F. Giffuni  *
48238b87bSPeter Grehan  * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
58238b87bSPeter Grehan  * Copyright (C) 1995-1997, 1999 TooLs GmbH.
68238b87bSPeter Grehan  * All rights reserved.
78238b87bSPeter Grehan  *
88238b87bSPeter Grehan  * Redistribution and use in source and binary forms, with or without
98238b87bSPeter Grehan  * modification, are permitted provided that the following conditions
108238b87bSPeter Grehan  * are met:
118238b87bSPeter Grehan  * 1. Redistributions of source code must retain the above copyright
128238b87bSPeter Grehan  *    notice, this list of conditions and the following disclaimer.
138238b87bSPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
148238b87bSPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
158238b87bSPeter Grehan  *    documentation and/or other materials provided with the distribution.
168238b87bSPeter Grehan  * 3. All advertising materials mentioning features or use of this software
178238b87bSPeter Grehan  *    must display the following acknowledgement:
188238b87bSPeter Grehan  *	This product includes software developed by TooLs GmbH.
198238b87bSPeter Grehan  * 4. The name of TooLs GmbH may not be used to endorse or promote products
208238b87bSPeter Grehan  *    derived from this software without specific prior written permission.
218238b87bSPeter Grehan  *
228238b87bSPeter Grehan  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
238238b87bSPeter Grehan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248238b87bSPeter Grehan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
258238b87bSPeter Grehan  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
268238b87bSPeter Grehan  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
278238b87bSPeter Grehan  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
288238b87bSPeter Grehan  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
298238b87bSPeter Grehan  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
308238b87bSPeter Grehan  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
318238b87bSPeter Grehan  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
328238b87bSPeter Grehan  *
338238b87bSPeter Grehan  * $NetBSD: syncicache.c,v 1.2 1999/05/05 12:36:40 tsubai Exp $
348238b87bSPeter Grehan  */
358238b87bSPeter Grehan 
368238b87bSPeter Grehan #include <sys/param.h>
378238b87bSPeter Grehan #if	defined(_KERNEL) || defined(_STANDALONE)
388238b87bSPeter Grehan #include <sys/time.h>
398238b87bSPeter Grehan #include <sys/proc.h>
408238b87bSPeter Grehan #include <vm/vm.h>
418238b87bSPeter Grehan #endif
428238b87bSPeter Grehan #include <sys/sysctl.h>
438238b87bSPeter Grehan 
44a29cc9a3SAndriy Gapon #include <machine/cpu.h>
45d23391e3SMarcel Moolenaar #include <machine/md_var.h>
468238b87bSPeter Grehan 
47a63c2f9dSNathan Whitehorn #ifdef _STANDALONE
484c01c0b9SNathan Whitehorn int cacheline_size = 32;
498238b87bSPeter Grehan #endif
504c01c0b9SNathan Whitehorn 
514c01c0b9SNathan Whitehorn #if	!defined(_KERNEL) && !defined(_STANDALONE)
52864dbc10SPeter Grehan #include <stdlib.h>
53864dbc10SPeter Grehan 
54a63c2f9dSNathan Whitehorn int cacheline_size = 0;
55a63c2f9dSNathan Whitehorn 
568238b87bSPeter Grehan static void getcachelinesize(void);
578238b87bSPeter Grehan 
588238b87bSPeter Grehan static void
getcachelinesize()598238b87bSPeter Grehan getcachelinesize()
608238b87bSPeter Grehan {
618238b87bSPeter Grehan 	static int	cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
628238b87bSPeter Grehan 	int		clen;
638238b87bSPeter Grehan 
644c01c0b9SNathan Whitehorn 	clen = sizeof(cacheline_size);
658238b87bSPeter Grehan 
66bf51882aSPedro F. Giffuni 	if (sysctl(cachemib, nitems(cachemib), &cacheline_size, &clen,
67bf51882aSPedro F. Giffuni 	    NULL, 0) < 0 || !cacheline_size) {
688238b87bSPeter Grehan 		abort();
698238b87bSPeter Grehan 	}
708238b87bSPeter Grehan }
718238b87bSPeter Grehan #endif
728238b87bSPeter Grehan 
738238b87bSPeter Grehan void
__syncicache(void * from,int len)748238b87bSPeter Grehan __syncicache(void *from, int len)
758238b87bSPeter Grehan {
768238b87bSPeter Grehan 	int	l, off;
778238b87bSPeter Grehan 	char	*p;
788238b87bSPeter Grehan 
798238b87bSPeter Grehan #if	!defined(_KERNEL) && !defined(_STANDALONE)
804c01c0b9SNathan Whitehorn 	if (!cacheline_size)
818238b87bSPeter Grehan 		getcachelinesize();
828238b87bSPeter Grehan #endif
834c01c0b9SNathan Whitehorn 
844c01c0b9SNathan Whitehorn 	off = (u_int)from & (cacheline_size - 1);
858238b87bSPeter Grehan 	l = len += off;
868238b87bSPeter Grehan 	p = (char *)from - off;
874c01c0b9SNathan Whitehorn 
888238b87bSPeter Grehan 	do {
898238b87bSPeter Grehan 		__asm __volatile ("dcbst 0,%0" :: "r"(p));
904c01c0b9SNathan Whitehorn 		p += cacheline_size;
914c01c0b9SNathan Whitehorn 	} while ((l -= cacheline_size) > 0);
928238b87bSPeter Grehan 	__asm __volatile ("sync");
938238b87bSPeter Grehan 	p = (char *)from - off;
948238b87bSPeter Grehan 	do {
958238b87bSPeter Grehan 		__asm __volatile ("icbi 0,%0" :: "r"(p));
964c01c0b9SNathan Whitehorn 		p += cacheline_size;
974c01c0b9SNathan Whitehorn 	} while ((len -= cacheline_size) > 0);
988238b87bSPeter Grehan 	__asm __volatile ("sync; isync");
998238b87bSPeter Grehan }
1004c01c0b9SNathan Whitehorn 
101