45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens/*-
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Copyright (C) 1995-1997, 1999 TooLs GmbH.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * All rights reserved.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * Redistribution and use in source and binary forms, with or without
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * modification, are permitted provided that the following conditions
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * are met:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * 1. Redistributions of source code must retain the above copyright
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * notice, this list of conditions and the following disclaimer.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * 2. Redistributions in binary form must reproduce the above copyright
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * notice, this list of conditions and the following disclaimer in the
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * documentation and/or other materials provided with the distribution.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * 3. All advertising materials mentioning features or use of this software
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * must display the following acknowledgement:
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * This product includes software developed by TooLs GmbH.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * 4. The name of TooLs GmbH may not be used to endorse or promote products
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * derived from this software without specific prior written permission.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens *
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens * $NetBSD: syncicache.c,v 1.2 1999/05/05 12:36:40 tsubai Exp $
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#ifndef lint
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensstatic const char rcsid[] =
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens "$FreeBSD$";
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif /* not lint */
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/param.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if defined(_KERNEL) || defined(_STANDALONE)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/time.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/proc.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <vm/vm.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <sys/sysctl.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <machine/cpu.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <machine/md_var.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#ifdef _STANDALONE
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint cacheline_size = 32;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if !defined(_KERNEL) && !defined(_STANDALONE)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#include <stdlib.h>
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensint cacheline_size = 0;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensstatic void getcachelinesize(void);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensstatic void
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensgetcachelinesize()
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens int clen;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens clen = sizeof(cacheline_size);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (sysctl(cachemib, sizeof(cachemib) / sizeof(cachemib[0]),
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens &cacheline_size, &clen, NULL, 0) < 0 || !cacheline_size) {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens abort();
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens }
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrensvoid
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens__syncicache(void *from, int len)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens{
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens int l, off;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens char *p;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#if !defined(_KERNEL) && !defined(_STANDALONE)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (!cacheline_size)
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens getcachelinesize();
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens#endif
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens off = (u_int)from & (cacheline_size - 1);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens l = len += off;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens p = (char *)from - off;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens do {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm __volatile ("dcbst 0,%0" :: "r"(p));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens p += cacheline_size;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } while ((l -= cacheline_size) > 0);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm __volatile ("sync");
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens p = (char *)from - off;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens do {
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm __volatile ("icbi 0,%0" :: "r"(p));
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens p += cacheline_size;
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens } while ((len -= cacheline_size) > 0);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens __asm __volatile ("sync; isync");
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens}
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens