]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/sparc64/sparc64/cache.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / sparc64 / sparc64 / cache.c
1 /*-
2  * Copyright (c) 1996
3  *      The President and Fellows of Harvard College. All rights reserved.
4  * Copyright (c) 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *      This product includes software developed by Harvard University.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  *
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *      This product includes software developed by Aaron Brown and
27  *      Harvard University.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  */
44 /*-
45  * Copyright (c) 2001 by Thomas Moestl <tmm@FreeBSD.org>.
46  * Copyright (c) 2008, 2010 Marius Strobl <marius@FreeBSD.org>
47  * All rights reserved.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  *    notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  *    notice, this list of conditions and the following disclaimer in the
56  *    documentation and/or other materials provided with the distribution.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
62  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
63  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
64  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
65  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
66  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
67  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  *      from: @(#)cache.c       8.2 (Berkeley) 10/30/93
70  *      from: NetBSD: cache.c,v 1.5 2000/12/06 01:47:50 mrg Exp
71  */
72
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD$");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/pcpu.h>
79
80 #include <dev/ofw/openfirm.h>
81
82 #include <machine/cache.h>
83 #include <machine/tlb.h>
84 #include <machine/ver.h>
85
86 cache_enable_t *cache_enable;
87 cache_flush_t *cache_flush;
88 dcache_page_inval_t *dcache_page_inval;
89 icache_page_inval_t *icache_page_inval;
90
91 #define OF_GET(h, n, v) OF_getprop((h), (n), &(v), sizeof(v))
92
93 static u_int cache_new_prop(u_int cpu_impl);
94
95 static u_int
96 cache_new_prop(u_int cpu_impl)
97 {
98
99         switch (cpu_impl) {
100         case CPU_IMPL_ULTRASPARCIV:
101         case CPU_IMPL_ULTRASPARCIVp:
102                 return (1);
103         default:
104                 return (0);
105         }
106 }
107
108 /*
109  * Fill in the cache parameters using the CPU node.
110  */
111 void
112 cache_init(struct pcpu *pcpu)
113 {
114         u_long set;
115         u_int use_new_prop;
116
117         use_new_prop = cache_new_prop(pcpu->pc_impl);
118         if (OF_GET(pcpu->pc_node, !use_new_prop ? "icache-size" :
119             "l1-icache-size", pcpu->pc_cache.ic_size) == -1 ||
120             OF_GET(pcpu->pc_node, !use_new_prop ? "icache-line-size" :
121             "l1-icache-line-size", pcpu->pc_cache.ic_linesize) == -1 ||
122             OF_GET(pcpu->pc_node, !use_new_prop ? "icache-associativity" :
123             "l1-icache-associativity", pcpu->pc_cache.ic_assoc) == -1 ||
124             OF_GET(pcpu->pc_node, !use_new_prop ? "dcache-size" :
125             "l1-dcache-size", pcpu->pc_cache.dc_size) == -1 ||
126             OF_GET(pcpu->pc_node, !use_new_prop ? "dcache-line-size" :
127             "l1-dcache-line-size", pcpu->pc_cache.dc_linesize) == -1 ||
128             OF_GET(pcpu->pc_node, !use_new_prop ? "dcache-associativity" :
129             "l1-dcache-associativity", pcpu->pc_cache.dc_assoc) == -1 ||
130             OF_GET(pcpu->pc_node, !use_new_prop ? "ecache-size" :
131             "l2-cache-size", pcpu->pc_cache.ec_size) == -1 ||
132             OF_GET(pcpu->pc_node, !use_new_prop ? "ecache-line-size" :
133             "l2-cache-line-size", pcpu->pc_cache.ec_linesize) == -1 ||
134             OF_GET(pcpu->pc_node, !use_new_prop ? "ecache-associativity" :
135             "l2-cache-associativity", pcpu->pc_cache.ec_assoc) == -1)
136                 panic("cache_init: could not retrieve cache parameters");
137
138         set = pcpu->pc_cache.ic_size / pcpu->pc_cache.ic_assoc;
139         if ((set & ~(1UL << (ffs(set) - 1))) != 0)
140                 panic("cache_init: I$ set size not a power of 2");
141         if ((pcpu->pc_cache.dc_size &
142             ~(1UL << (ffs(pcpu->pc_cache.dc_size) - 1))) != 0)
143                 panic("cache_init: D$ size not a power of 2");
144         /*
145          * For CPUs which don't support unaliasing in hardware ensure that
146          * the data cache doesn't have too many virtual colors.
147          */
148         if (pcpu->pc_impl != CPU_IMPL_SPARC64V &&
149             ((pcpu->pc_cache.dc_size / pcpu->pc_cache.dc_assoc) /
150             PAGE_SIZE) != DCACHE_COLORS)
151                 panic("cache_init: too many D$ colors");
152         set = pcpu->pc_cache.ec_size / pcpu->pc_cache.ec_assoc;
153         if ((set & ~(1UL << (ffs(set) - 1))) != 0)
154                 panic("cache_init: E$ set size not a power of 2");
155
156         if (pcpu->pc_impl >= CPU_IMPL_ULTRASPARCIII) {
157                 cache_enable = cheetah_cache_enable;
158                 cache_flush = cheetah_cache_flush;
159                 dcache_page_inval = cheetah_dcache_page_inval;
160                 icache_page_inval = cheetah_icache_page_inval;
161                 tlb_flush_nonlocked = cheetah_tlb_flush_nonlocked;
162                 tlb_flush_user = cheetah_tlb_flush_user;
163         } else if (pcpu->pc_impl == CPU_IMPL_SPARC64V) {
164                 cache_enable = cheetah_cache_enable;
165                 cache_flush = zeus_cache_flush;
166                 dcache_page_inval = zeus_dcache_page_inval;
167                 icache_page_inval = zeus_icache_page_inval;
168                 tlb_flush_nonlocked = cheetah_tlb_flush_nonlocked;
169                 tlb_flush_user = cheetah_tlb_flush_user;
170         } else if (pcpu->pc_impl >= CPU_IMPL_ULTRASPARCI &&
171             pcpu->pc_impl < CPU_IMPL_ULTRASPARCIII) {
172                 cache_enable = spitfire_cache_enable;
173                 cache_flush = spitfire_cache_flush;
174                 dcache_page_inval = spitfire_dcache_page_inval;
175                 icache_page_inval = spitfire_icache_page_inval;
176                 tlb_flush_nonlocked = spitfire_tlb_flush_nonlocked;
177                 tlb_flush_user = spitfire_tlb_flush_user;
178         } else
179                 panic("cache_init: unknown CPU");
180 }