1840b91ccSNathan Whitehorn /*-
2*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
3*d915a14eSPedro F. Giffuni *
4840b91ccSNathan Whitehorn * Copyright (C) 1995-1997, 1999 Wolfgang Solfrank.
5840b91ccSNathan Whitehorn * Copyright (C) 1995-1997, 1999 TooLs GmbH.
6840b91ccSNathan Whitehorn * All rights reserved.
7840b91ccSNathan Whitehorn *
8840b91ccSNathan Whitehorn * Redistribution and use in source and binary forms, with or without
9840b91ccSNathan Whitehorn * modification, are permitted provided that the following conditions
10840b91ccSNathan Whitehorn * are met:
11840b91ccSNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
12840b91ccSNathan Whitehorn * notice, this list of conditions and the following disclaimer.
13840b91ccSNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
14840b91ccSNathan Whitehorn * notice, this list of conditions and the following disclaimer in the
15840b91ccSNathan Whitehorn * documentation and/or other materials provided with the distribution.
16840b91ccSNathan Whitehorn * 3. All advertising materials mentioning features or use of this software
17840b91ccSNathan Whitehorn * must display the following acknowledgement:
18840b91ccSNathan Whitehorn * This product includes software developed by TooLs GmbH.
19840b91ccSNathan Whitehorn * 4. The name of TooLs GmbH may not be used to endorse or promote products
20840b91ccSNathan Whitehorn * derived from this software without specific prior written permission.
21840b91ccSNathan Whitehorn *
22840b91ccSNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23840b91ccSNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24840b91ccSNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25840b91ccSNathan Whitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26840b91ccSNathan Whitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27840b91ccSNathan Whitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28840b91ccSNathan Whitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29840b91ccSNathan Whitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30840b91ccSNathan Whitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31840b91ccSNathan Whitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32840b91ccSNathan Whitehorn *
33840b91ccSNathan Whitehorn * $NetBSD: syncicache.c,v 1.2 1999/05/05 12:36:40 tsubai Exp $
34840b91ccSNathan Whitehorn */
35840b91ccSNathan Whitehorn
36840b91ccSNathan Whitehorn #include <sys/param.h>
37840b91ccSNathan Whitehorn #if defined(_KERNEL) || defined(_STANDALONE)
38840b91ccSNathan Whitehorn #include <sys/time.h>
39840b91ccSNathan Whitehorn #include <sys/proc.h>
40840b91ccSNathan Whitehorn #include <vm/vm.h>
41840b91ccSNathan Whitehorn #endif
42840b91ccSNathan Whitehorn #include <sys/sysctl.h>
43840b91ccSNathan Whitehorn
44a29cc9a3SAndriy Gapon #include <machine/cpu.h>
45840b91ccSNathan Whitehorn #include <machine/md_var.h>
46840b91ccSNathan Whitehorn
47840b91ccSNathan Whitehorn #ifdef _STANDALONE
48840b91ccSNathan Whitehorn int cacheline_size = 32;
49840b91ccSNathan Whitehorn #endif
50840b91ccSNathan Whitehorn
51840b91ccSNathan Whitehorn #if !defined(_KERNEL) && !defined(_STANDALONE)
52840b91ccSNathan Whitehorn #include <stdlib.h>
53840b91ccSNathan Whitehorn
54840b91ccSNathan Whitehorn int cacheline_size = 0;
55840b91ccSNathan Whitehorn
56840b91ccSNathan Whitehorn static void getcachelinesize(void);
57840b91ccSNathan Whitehorn
58840b91ccSNathan Whitehorn static void
getcachelinesize()59840b91ccSNathan Whitehorn getcachelinesize()
60840b91ccSNathan Whitehorn {
61840b91ccSNathan Whitehorn static int cachemib[] = { CTL_MACHDEP, CPU_CACHELINE };
62840b91ccSNathan Whitehorn long clen;
63840b91ccSNathan Whitehorn
64840b91ccSNathan Whitehorn clen = sizeof(cacheline_size);
65840b91ccSNathan Whitehorn
66bf51882aSPedro F. Giffuni if (sysctl(cachemib, nitems(cachemib), &cacheline_size, &clen,
67bf51882aSPedro F. Giffuni NULL, 0) < 0 || !cacheline_size) {
68840b91ccSNathan Whitehorn abort();
69840b91ccSNathan Whitehorn }
70840b91ccSNathan Whitehorn }
71840b91ccSNathan Whitehorn #endif
72840b91ccSNathan Whitehorn
73840b91ccSNathan Whitehorn void
__syncicache(void * from,int len)74840b91ccSNathan Whitehorn __syncicache(void *from, int len)
75840b91ccSNathan Whitehorn {
76840b91ccSNathan Whitehorn off_t l, off;
77840b91ccSNathan Whitehorn char *p;
78840b91ccSNathan Whitehorn
79840b91ccSNathan Whitehorn #if !defined(_KERNEL) && !defined(_STANDALONE)
80840b91ccSNathan Whitehorn if (!cacheline_size)
81840b91ccSNathan Whitehorn getcachelinesize();
82840b91ccSNathan Whitehorn #endif
83840b91ccSNathan Whitehorn
84840b91ccSNathan Whitehorn off = (uintptr_t)from & (cacheline_size - 1);
85840b91ccSNathan Whitehorn l = len += off;
86840b91ccSNathan Whitehorn p = (char *)from - off;
87840b91ccSNathan Whitehorn
88840b91ccSNathan Whitehorn do {
89840b91ccSNathan Whitehorn __asm __volatile ("dcbst 0,%0" :: "r"(p));
90840b91ccSNathan Whitehorn p += cacheline_size;
91840b91ccSNathan Whitehorn } while ((l -= cacheline_size) > 0);
92840b91ccSNathan Whitehorn __asm __volatile ("sync");
93840b91ccSNathan Whitehorn p = (char *)from - off;
94840b91ccSNathan Whitehorn do {
95840b91ccSNathan Whitehorn __asm __volatile ("icbi 0,%0" :: "r"(p));
96840b91ccSNathan Whitehorn p += cacheline_size;
97840b91ccSNathan Whitehorn } while ((len -= cacheline_size) > 0);
98840b91ccSNathan Whitehorn __asm __volatile ("sync; isync");
99840b91ccSNathan Whitehorn }
100840b91ccSNathan Whitehorn
101