xref: /minix3/lib/libc/arch/powerpc64/gen/syncicache.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: syncicache.c,v 1.2 2012/11/24 07:16:04 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
52fe8fb19SBen Gras  * Copyright (C) 1995-1997, 1999 TooLs GmbH.
62fe8fb19SBen Gras  * All rights reserved.
72fe8fb19SBen Gras  *
82fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
92fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
102fe8fb19SBen Gras  * are met:
112fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
132fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
152fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
162fe8fb19SBen Gras  * 3. All advertising materials mentioning features or use of this software
172fe8fb19SBen Gras  *    must display the following acknowledgement:
182fe8fb19SBen Gras  *	This product includes software developed by TooLs GmbH.
192fe8fb19SBen Gras  * 4. The name of TooLs GmbH may not be used to endorse or promote products
202fe8fb19SBen Gras  *    derived from this software without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
232fe8fb19SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
242fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252fe8fb19SBen Gras  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
262fe8fb19SBen Gras  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
272fe8fb19SBen Gras  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
282fe8fb19SBen Gras  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
292fe8fb19SBen Gras  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
302fe8fb19SBen Gras  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
312fe8fb19SBen Gras  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras #include <sys/param.h>
342fe8fb19SBen Gras #if	defined(_KERNEL) || defined(_STANDALONE)
352fe8fb19SBen Gras #include <sys/time.h>
362fe8fb19SBen Gras #include <sys/proc.h>
372fe8fb19SBen Gras #include <uvm/uvm_extern.h>
382fe8fb19SBen Gras #endif
392fe8fb19SBen Gras #include <sys/sysctl.h>
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #include <machine/cpu.h>
422fe8fb19SBen Gras 
432fe8fb19SBen Gras 
442fe8fb19SBen Gras #if defined(_STANDALONE)
452fe8fb19SBen Gras #ifndef	CACHELINESIZE
462fe8fb19SBen Gras #error "Must know the size of a cache line"
472fe8fb19SBen Gras #endif
482fe8fb19SBen Gras static struct cache_info _cache_info = {
492fe8fb19SBen Gras 	CACHELINESIZE,
502fe8fb19SBen Gras 	CACHELINESIZE,
512fe8fb19SBen Gras 	CACHELINESIZE,
522fe8fb19SBen Gras 	CACHELINESIZE
532fe8fb19SBen Gras };
542fe8fb19SBen Gras #define CACHEINFO	_cache_info
552fe8fb19SBen Gras #elif defined(_KERNEL)
562fe8fb19SBen Gras #define	CACHEINFO	(curcpu()->ci_ci)
572fe8fb19SBen Gras #else
582fe8fb19SBen Gras #include <stdlib.h>
592fe8fb19SBen Gras 
602fe8fb19SBen Gras size_t __getcachelinesize (void);
612fe8fb19SBen Gras 
622fe8fb19SBen Gras static int _cachelinesize = 0;
632fe8fb19SBen Gras 
642fe8fb19SBen Gras static struct cache_info _cache_info;
652fe8fb19SBen Gras #define CACHEINFO	_cache_info
662fe8fb19SBen Gras 
672fe8fb19SBen Gras size_t
__getcachelinesize(void)682fe8fb19SBen Gras __getcachelinesize(void)
692fe8fb19SBen Gras {
702fe8fb19SBen Gras 	static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
712fe8fb19SBen Gras 	static int cacheinfomib[] = { CTL_MACHDEP, CPU_CACHEINFO };
722fe8fb19SBen Gras 	size_t clen = sizeof(_cache_info);
732fe8fb19SBen Gras 
742fe8fb19SBen Gras 	if (_cachelinesize)
752fe8fb19SBen Gras 		return _cachelinesize;
762fe8fb19SBen Gras 
77*84d9c625SLionel Sambuc 	if (sysctl(cacheinfomib, (u_int)__arraycount(cacheinfomib),
782fe8fb19SBen Gras 	    &_cache_info, &clen, NULL, 0) == 0) {
792fe8fb19SBen Gras 		_cachelinesize = _cache_info.dcache_line_size;
802fe8fb19SBen Gras 		return _cachelinesize;
812fe8fb19SBen Gras 	}
822fe8fb19SBen Gras 
832fe8fb19SBen Gras 	/* Try older deprecated sysctl */
842fe8fb19SBen Gras 	clen = sizeof(_cachelinesize);
85*84d9c625SLionel Sambuc 	if (sysctl(cachemib, (u_int)__arraycount(cachemib),
86*84d9c625SLionel Sambuc 	    &_cachelinesize, &clen, NULL, 0) < 0 || !_cachelinesize)
872fe8fb19SBen Gras 		abort();
882fe8fb19SBen Gras 
892fe8fb19SBen Gras 	_cache_info.dcache_size = _cachelinesize;
902fe8fb19SBen Gras 	_cache_info.dcache_line_size = _cachelinesize;
912fe8fb19SBen Gras 	_cache_info.icache_size = _cachelinesize;
922fe8fb19SBen Gras 	_cache_info.icache_line_size = _cachelinesize;
932fe8fb19SBen Gras 
942fe8fb19SBen Gras 	/* If there is no cache, indicate we have issued the sysctl. */
952fe8fb19SBen Gras 	if (!_cachelinesize)
962fe8fb19SBen Gras 		_cachelinesize = 1;
972fe8fb19SBen Gras 
982fe8fb19SBen Gras 	return _cachelinesize;
992fe8fb19SBen Gras }
1002fe8fb19SBen Gras #endif
1012fe8fb19SBen Gras 
1022fe8fb19SBen Gras void
__syncicache(void * from,size_t len)1032fe8fb19SBen Gras __syncicache(void *from, size_t len)
1042fe8fb19SBen Gras {
1052fe8fb19SBen Gras 	size_t l, off;
1062fe8fb19SBen Gras 	size_t linesz;
1072fe8fb19SBen Gras 	char *p;
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras #if	!defined(_KERNEL) && !defined(_STANDALONE)
1102fe8fb19SBen Gras 	if (!_cachelinesize)
1112fe8fb19SBen Gras 		__getcachelinesize();
1122fe8fb19SBen Gras #endif
1132fe8fb19SBen Gras 
1142fe8fb19SBen Gras 	if (CACHEINFO.dcache_size > 0) {
1152fe8fb19SBen Gras 		linesz = CACHEINFO.dcache_line_size;
1162fe8fb19SBen Gras 		off = (uintptr_t)from & (linesz - 1);
1172fe8fb19SBen Gras 		l = (len + off + linesz - 1) & ~(linesz - 1);
1182fe8fb19SBen Gras 		p = (char *)from - off;
1192fe8fb19SBen Gras 		do {
1202fe8fb19SBen Gras 			__asm volatile ("dcbst 0,%0" :: "r"(p));
1212fe8fb19SBen Gras 			p += linesz;
1222fe8fb19SBen Gras 		} while ((l -= linesz) != 0);
1232fe8fb19SBen Gras 	}
1242fe8fb19SBen Gras 	__asm volatile ("sync");
1252fe8fb19SBen Gras 
1262fe8fb19SBen Gras 	if (CACHEINFO.icache_size > 0 ) {
1272fe8fb19SBen Gras 		linesz = CACHEINFO.icache_line_size;
1282fe8fb19SBen Gras 		off = (uintptr_t)from & (linesz - 1);
1292fe8fb19SBen Gras 		l = (len + off + linesz - 1) & ~(linesz - 1);
1302fe8fb19SBen Gras 		p = (char *)from - off;
1312fe8fb19SBen Gras 		do {
1322fe8fb19SBen Gras 			__asm volatile ("icbi 0,%0" :: "r"(p));
1332fe8fb19SBen Gras 			p += linesz;
1342fe8fb19SBen Gras 		} while ((l -= linesz) != 0);
1352fe8fb19SBen Gras 	}
1362fe8fb19SBen Gras 	__asm volatile ("sync; isync");
1372fe8fb19SBen Gras }
138