190792Sgshapiro/*-
2173340Sgshapiro * Copyright (c) 1996
390792Sgshapiro *	The President and Fellows of Harvard College. All rights reserved.
490792Sgshapiro * Copyright (c) 1992, 1993
590792Sgshapiro *	The Regents of the University of California.  All rights reserved.
690792Sgshapiro *
790792Sgshapiro * This software was developed by the Computer Systems Engineering group
890792Sgshapiro * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9173340Sgshapiro * contributed to Berkeley.
1090792Sgshapiro *
1190792Sgshapiro * All advertising materials mentioning features or use of this software
1290792Sgshapiro * must display the following acknowledgement:
1390792Sgshapiro *	This product includes software developed by Harvard University.
1490792Sgshapiro *
1590792Sgshapiro * Redistribution and use in source and binary forms, with or without
1690792Sgshapiro * modification, are permitted provided that the following conditions
1790792Sgshapiro * are met:
1894334Sgshapiro *
1994334Sgshapiro * 1. Redistributions of source code must retain the above copyright
20168515Sgshapiro *    notice, this list of conditions and the following disclaimer.
2194334Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
2294334Sgshapiro *    notice, this list of conditions and the following disclaimer in the
2390792Sgshapiro *    documentation and/or other materials provided with the distribution.
2490792Sgshapiro * 3. All advertising materials mentioning features or use of this software
2590792Sgshapiro *    must display the following acknowledgement:
2690792Sgshapiro *	This product includes software developed by Aaron Brown and
2790792Sgshapiro *	Harvard University.
2890792Sgshapiro * 4. Neither the name of the University nor the names of its contributors
2990792Sgshapiro *    may be used to endorse or promote products derived from this software
3090792Sgshapiro *    without specific prior written permission.
3190792Sgshapiro *
3290792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3390792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3490792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35168515Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36168515Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37168515Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38168515Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39168515Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40168515Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41168515Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4290792Sgshapiro * SUCH DAMAGE.
43132943Sgshapiro */
44132943Sgshapiro/*-
45132943Sgshapiro * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
46132943Sgshapiro * Copyright (c) 2008, 2010 Marius Strobl <marius@FreeBSD.org>
47132943Sgshapiro * All rights reserved.
48132943Sgshapiro *
4990792Sgshapiro * Redistribution and use in source and binary forms, with or without
5090792Sgshapiro * modification, are permitted provided that the following conditions
51132943Sgshapiro * are met:
52132943Sgshapiro * 1. Redistributions of source code must retain the above copyright
53132943Sgshapiro *    notice, this list of conditions and the following disclaimer.
54157001Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
5594334Sgshapiro *    notice, this list of conditions and the following disclaimer in the
5690792Sgshapiro *    documentation and/or other materials provided with the distribution.
5790792Sgshapiro *
5890792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59132943Sgshapiro * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60132943Sgshapiro * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6190792Sgshapiro * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
6294334Sgshapiro * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
6390792Sgshapiro * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
6490792Sgshapiro * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
6590792Sgshapiro * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
6690792Sgshapiro * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
6790792Sgshapiro * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6890792Sgshapiro *
6990792Sgshapiro *	from: @(#)cache.c	8.2 (Berkeley) 10/30/93
7090792Sgshapiro *	from: NetBSD: cache.c,v 1.5 2000/12/06 01:47:50 mrg Exp
7190792Sgshapiro */
7290792Sgshapiro
7390792Sgshapiro#include <sys/cdefs.h>
7490792Sgshapiro__FBSDID("$FreeBSD$");
7590792Sgshapiro
7690792Sgshapiro#include <sys/param.h>
7790792Sgshapiro#include <sys/systm.h>
7890792Sgshapiro#include <sys/pcpu.h>
7990792Sgshapiro
8090792Sgshapiro#include <dev/ofw/openfirm.h>
8190792Sgshapiro
8290792Sgshapiro#include <machine/cache.h>
8394334Sgshapiro#include <machine/tlb.h>
8490792Sgshapiro#include <machine/ver.h>
85168515Sgshapiro
8690792Sgshapirocache_enable_t *cache_enable;
8790792Sgshapirocache_flush_t *cache_flush;
8890792Sgshapirodcache_page_inval_t *dcache_page_inval;
8990792Sgshapiroicache_page_inval_t *icache_page_inval;
9090792Sgshapiro
9190792Sgshapirou_int dcache_color_ignore;
9290792Sgshapiro
9390792Sgshapiro#define	OF_GET(h, n, v)	OF_getprop((h), (n), &(v), sizeof(v))
94173340Sgshapiro
95173340Sgshapirostatic u_int cache_new_prop(u_int cpu_impl);
96173340Sgshapiro
97173340Sgshapirostatic u_int
9890792Sgshapirocache_new_prop(u_int cpu_impl)
9990792Sgshapiro{
10090792Sgshapiro
10190792Sgshapiro	switch (cpu_impl) {
10290792Sgshapiro	case CPU_IMPL_ULTRASPARCIV:
10390792Sgshapiro	case CPU_IMPL_ULTRASPARCIVp:
10494334Sgshapiro		return (1);
10590792Sgshapiro	default:
106168515Sgshapiro		return (0);
107168515Sgshapiro	}
108168515Sgshapiro}
109168515Sgshapiro
110168515Sgshapiro/*
11190792Sgshapiro * Fill in the cache parameters using the CPU node.
11290792Sgshapiro */
11394334Sgshapirovoid
11494334Sgshapirocache_init(struct pcpu *pcpu)
115168515Sgshapiro{
116168515Sgshapiro	u_long set;
117168515Sgshapiro	u_int use_new_prop;
11894334Sgshapiro
11994334Sgshapiro	/*
12094334Sgshapiro	 * For CPUs which ignore TD_CV and support hardware unaliasing don't
12190792Sgshapiro	 * bother doing page coloring.  This is equal across all CPUs.
12290792Sgshapiro	 */
12390792Sgshapiro	if (pcpu->pc_cpuid == 0 && pcpu->pc_impl == CPU_IMPL_SPARC64V)
12490792Sgshapiro		dcache_color_ignore = 1;
12590792Sgshapiro
12690792Sgshapiro	use_new_prop = cache_new_prop(pcpu->pc_impl);
127168515Sgshapiro	if (OF_GET(pcpu->pc_node, !use_new_prop ? "icache-size" :
12894334Sgshapiro	    "l1-icache-size", pcpu->pc_cache.ic_size) == -1 ||
12994334Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "icache-line-size" :
13090792Sgshapiro	    "l1-icache-line-size", pcpu->pc_cache.ic_linesize) == -1 ||
13190792Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "icache-associativity" :
13290792Sgshapiro	    "l1-icache-associativity", pcpu->pc_cache.ic_assoc) == -1 ||
13390792Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "dcache-size" :
13494334Sgshapiro	    "l1-dcache-size", pcpu->pc_cache.dc_size) == -1 ||
13594334Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "dcache-line-size" :
13694334Sgshapiro	    "l1-dcache-line-size", pcpu->pc_cache.dc_linesize) == -1 ||
13794334Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "dcache-associativity" :
13894334Sgshapiro	    "l1-dcache-associativity", pcpu->pc_cache.dc_assoc) == -1 ||
13994334Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "ecache-size" :
14090792Sgshapiro	    "l2-cache-size", pcpu->pc_cache.ec_size) == -1 ||
14190792Sgshapiro	    OF_GET(pcpu->pc_node, !use_new_prop ? "ecache-line-size" :
142	    "l2-cache-line-size", pcpu->pc_cache.ec_linesize) == -1 ||
143	    OF_GET(pcpu->pc_node, !use_new_prop ? "ecache-associativity" :
144	    "l2-cache-associativity", pcpu->pc_cache.ec_assoc) == -1)
145		OF_panic("%s: could not retrieve cache parameters", __func__);
146
147	set = pcpu->pc_cache.ic_size / pcpu->pc_cache.ic_assoc;
148	if ((set & ~(1UL << (ffs(set) - 1))) != 0)
149		OF_panic("%s: I$ set size not a power of 2", __func__);
150	if ((pcpu->pc_cache.dc_size &
151	    ~(1UL << (ffs(pcpu->pc_cache.dc_size) - 1))) != 0)
152		OF_panic("%s: D$ size not a power of 2", __func__);
153	/*
154	 * For CPUs which don't support unaliasing in hardware ensure that
155	 * the data cache doesn't have too many virtual colors.
156	 */
157	if (dcache_color_ignore == 0 && ((pcpu->pc_cache.dc_size /
158	    pcpu->pc_cache.dc_assoc) / PAGE_SIZE) != DCACHE_COLORS)
159		OF_panic("%s: too many D$ colors", __func__);
160	set = pcpu->pc_cache.ec_size / pcpu->pc_cache.ec_assoc;
161	if ((set & ~(1UL << (ffs(set) - 1))) != 0)
162		OF_panic("%s: E$ set size not a power of 2", __func__);
163
164	if (pcpu->pc_impl >= CPU_IMPL_ULTRASPARCIII) {
165		cache_enable = cheetah_cache_enable;
166		cache_flush = cheetah_cache_flush;
167		dcache_page_inval = cheetah_dcache_page_inval;
168		icache_page_inval = cheetah_icache_page_inval;
169		tlb_flush_nonlocked = cheetah_tlb_flush_nonlocked;
170		tlb_flush_user = cheetah_tlb_flush_user;
171	} else if (pcpu->pc_impl == CPU_IMPL_SPARC64V) {
172		cache_enable = zeus_cache_enable;
173		cache_flush = zeus_cache_flush;
174		dcache_page_inval = zeus_dcache_page_inval;
175		icache_page_inval = zeus_icache_page_inval;
176		tlb_flush_nonlocked = zeus_tlb_flush_nonlocked;
177		tlb_flush_user = zeus_tlb_flush_user;
178	} else if (pcpu->pc_impl >= CPU_IMPL_ULTRASPARCI &&
179	    pcpu->pc_impl < CPU_IMPL_ULTRASPARCIII) {
180		cache_enable = spitfire_cache_enable;
181		cache_flush = spitfire_cache_flush;
182		dcache_page_inval = spitfire_dcache_page_inval;
183		icache_page_inval = spitfire_icache_page_inval;
184		tlb_flush_nonlocked = spitfire_tlb_flush_nonlocked;
185		tlb_flush_user = spitfire_tlb_flush_user;
186	} else
187		OF_panic("%s: unknown CPU", __func__);
188}
189