1183319Snwhitehorn/*-
2107571Sgrehan * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
3107571Sgrehan * Copyright (C) 1995-1997, 1999 TooLs GmbH.
4107571Sgrehan * All rights reserved.
5107571Sgrehan *
6107571Sgrehan * Redistribution and use in source and binary forms, with or without
7107571Sgrehan * modification, are permitted provided that the following conditions
8107571Sgrehan * are met:
9107571Sgrehan * 1. Redistributions of source code must retain the above copyright
10107571Sgrehan *    notice, this list of conditions and the following disclaimer.
11107571Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
12107571Sgrehan *    notice, this list of conditions and the following disclaimer in the
13107571Sgrehan *    documentation and/or other materials provided with the distribution.
14107571Sgrehan * 3. All advertising materials mentioning features or use of this software
15107571Sgrehan *    must display the following acknowledgement:
16107571Sgrehan *	This product includes software developed by TooLs GmbH.
17107571Sgrehan * 4. The name of TooLs GmbH may not be used to endorse or promote products
18107571Sgrehan *    derived from this software without specific prior written permission.
19107571Sgrehan *
20107571Sgrehan * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21107571Sgrehan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22107571Sgrehan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23107571Sgrehan * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24107571Sgrehan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25107571Sgrehan * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26107571Sgrehan * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27107571Sgrehan * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28107571Sgrehan * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29107571Sgrehan * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30107571Sgrehan *
31107571Sgrehan * $NetBSD: syncicache.c,v 1.2 1999/05/05 12:36:40 tsubai Exp $
32107571Sgrehan */
33107571Sgrehan
34107571Sgrehan#ifndef lint
35107571Sgrehanstatic const char rcsid[] =
36107571Sgrehan  "$FreeBSD: releng/10.3/lib/libc/powerpc/gen/syncicache.c 253750 2013-07-28 18:44:17Z avg $";
37107571Sgrehan#endif /* not lint */
38107571Sgrehan
39107571Sgrehan#include <sys/param.h>
40107571Sgrehan#if	defined(_KERNEL) || defined(_STANDALONE)
41107571Sgrehan#include <sys/time.h>
42107571Sgrehan#include <sys/proc.h>
43107571Sgrehan#include <vm/vm.h>
44107571Sgrehan#endif
45107571Sgrehan#include <sys/sysctl.h>
46107571Sgrehan
47253750Savg#include <machine/cpu.h>
48170484Smarcel#include <machine/md_var.h>
49107571Sgrehan
50183320Snwhitehorn#ifdef _STANDALONE
51183319Snwhitehornint cacheline_size = 32;
52107571Sgrehan#endif
53183319Snwhitehorn
54183319Snwhitehorn#if	!defined(_KERNEL) && !defined(_STANDALONE)
55124769Sgrehan#include <stdlib.h>
56124769Sgrehan
57183320Snwhitehornint cacheline_size = 0;
58183320Snwhitehorn
59107571Sgrehanstatic void getcachelinesize(void);
60107571Sgrehan
61107571Sgrehanstatic void
62107571Sgrehangetcachelinesize()
63107571Sgrehan{
64107571Sgrehan	static int	cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
65107571Sgrehan	int		clen;
66107571Sgrehan
67183319Snwhitehorn	clen = sizeof(cacheline_size);
68107571Sgrehan
69107571Sgrehan	if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]),
70183319Snwhitehorn	    &cacheline_size, &clen, NULL, 0) < 0 || !cacheline_size) {
71107571Sgrehan		abort();
72107571Sgrehan	}
73107571Sgrehan}
74107571Sgrehan#endif
75107571Sgrehan
76107571Sgrehanvoid
77107571Sgrehan__syncicache(void *from, int len)
78107571Sgrehan{
79107571Sgrehan	int	l, off;
80107571Sgrehan	char	*p;
81107571Sgrehan
82107571Sgrehan#if	!defined(_KERNEL) && !defined(_STANDALONE)
83183319Snwhitehorn	if (!cacheline_size)
84107571Sgrehan		getcachelinesize();
85107571Sgrehan#endif
86183319Snwhitehorn
87183319Snwhitehorn	off = (u_int)from & (cacheline_size - 1);
88107571Sgrehan	l = len += off;
89107571Sgrehan	p = (char *)from - off;
90183319Snwhitehorn
91107571Sgrehan	do {
92107571Sgrehan		__asm __volatile ("dcbst 0,%0" :: "r"(p));
93183319Snwhitehorn		p += cacheline_size;
94183319Snwhitehorn	} while ((l -= cacheline_size) > 0);
95107571Sgrehan	__asm __volatile ("sync");
96107571Sgrehan	p = (char *)from - off;
97107571Sgrehan	do {
98107571Sgrehan		__asm __volatile ("icbi 0,%0" :: "r"(p));
99183319Snwhitehorn		p += cacheline_size;
100183319Snwhitehorn	} while ((len -= cacheline_size) > 0);
101107571Sgrehan	__asm __volatile ("sync; isync");
102107571Sgrehan}
103183319Snwhitehorn
104