]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/cheetah.c
Merge ACPICA 20180105.
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / cheetah.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003 Jake Burkholder.
5  * Copyright (c) 2005 - 2011 Marius Strobl <marius@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/smp.h>
38
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41
42 #include <machine/asi.h>
43 #include <machine/cache.h>
44 #include <machine/cpu.h>
45 #include <machine/cpufunc.h>
46 #include <machine/dcr.h>
47 #include <machine/lsu.h>
48 #include <machine/smp.h>
49 #include <machine/tlb.h>
50 #include <machine/ver.h>
51 #include <machine/vmparam.h>
52
53 #define CHEETAH_ICACHE_TAG_LOWER        0x30
54 #define CHEETAH_T16_ENTRIES             16
55 #define CHEETAH_DT512_ENTRIES           512
56 #define CHEETAH_IT128_ENTRIES           128
57 #define CHEETAH_IT512_ENTRIES           512
58
59 /*
60  * CPU-specific initialization for Sun Cheetah and later CPUs
61  */
62 void
63 cheetah_init(u_int cpu_impl)
64 {
65         u_long val;
66
67         /* Ensure the TSB Extension Registers hold 0 as TSB_Base. */
68
69         stxa(AA_DMMU_TSB_PEXT_REG, ASI_DMMU, 0);
70         stxa(AA_IMMU_TSB_PEXT_REG, ASI_IMMU, 0);
71         membar(Sync);
72
73         stxa(AA_DMMU_TSB_SEXT_REG, ASI_DMMU, 0);
74         /*
75          * NB: the secondary context was removed from the iMMU.
76          */
77         membar(Sync);
78
79         stxa(AA_DMMU_TSB_NEXT_REG, ASI_DMMU, 0);
80         stxa(AA_IMMU_TSB_NEXT_REG, ASI_IMMU, 0);
81         membar(Sync);
82
83         /*
84          * Configure the first large dTLB to hold 4MB pages (e.g. for direct
85          * mappings) for all three contexts and ensure the second one is set
86          * up to hold 8k pages for them.  Note that this is constraint by
87          * US-IV+, whose large dTLBs can only hold entries of certain page
88          * sizes each.
89          * For US-IV+, additionally ensure that the large iTLB is set up to
90          * hold 8k pages for nucleus and primary context (still no secondary
91          * iMMU context.
92          * NB: according to documentation, changing the page size of the same
93          * context requires a context demap before changing the corresponding
94          * page size, but we hardly can flush our locked pages here, so we use
95          * a demap all instead.
96          */
97         stxa(TLB_DEMAP_ALL, ASI_DMMU_DEMAP, 0);
98         membar(Sync);
99         val = (TS_4M << TLB_PCXR_N_PGSZ0_SHIFT) |
100             (TS_8K << TLB_PCXR_N_PGSZ1_SHIFT) |
101             (TS_4M << TLB_PCXR_P_PGSZ0_SHIFT) |
102             (TS_8K << TLB_PCXR_P_PGSZ1_SHIFT);
103         if (cpu_impl == CPU_IMPL_ULTRASPARCIVp)
104                 val |= (TS_8K << TLB_PCXR_N_PGSZ_I_SHIFT) |
105                     (TS_8K << TLB_PCXR_P_PGSZ_I_SHIFT);
106         stxa(AA_DMMU_PCXR, ASI_DMMU, val);
107         val = (TS_4M << TLB_SCXR_S_PGSZ0_SHIFT) |
108             (TS_8K << TLB_SCXR_S_PGSZ1_SHIFT);
109         stxa(AA_DMMU_SCXR, ASI_DMMU, val);
110         flush(KERNBASE);
111
112         /*
113          * Ensure DCR_IFPOE is disabled as long as we haven't implemented
114          * support for it (if ever) as most if not all firmware versions
115          * apparently turn it on.  Not making use of DCR_IFPOE should also
116          * avoid Cheetah erratum #109.
117          */
118         val = rd(asr18) & ~DCR_IFPOE;
119         if (cpu_impl == CPU_IMPL_ULTRASPARCIVp) {
120                 /*
121                  * Ensure the branch prediction mode is set to PC indexing
122                  * in order to work around US-IV+ erratum #2.
123                  */
124                 val = (val & ~DCR_BPM_MASK) | DCR_BPM_PC;
125                 /*
126                  * XXX disable dTLB parity error reporting as otherwise we
127                  * get seemingly false positives when copying in the user
128                  * window by simulating a fill trap on return to usermode in
129                  * case single issue is disabled, which thus appears to be
130                  * a CPU bug.
131                  */
132                 val &= ~DCR_DTPE;
133         }
134         wr(asr18, val, 0);
135 }
136
137 /*
138  * Enable level 1 caches.
139  */
140 void
141 cheetah_cache_enable(u_int cpu_impl)
142 {
143         u_long lsu;
144
145         lsu = ldxa(0, ASI_LSU_CTL_REG);
146         if (cpu_impl == CPU_IMPL_ULTRASPARCIII) {
147                 /* Disable P$ due to US-III erratum #18. */
148                 lsu &= ~LSU_PE;
149         }
150         stxa(0, ASI_LSU_CTL_REG, lsu | LSU_IC | LSU_DC);
151         flush(KERNBASE);
152 }
153
154 /*
155  * Flush all lines from the level 1 caches.
156  */
157 void
158 cheetah_cache_flush(void)
159 {
160         u_long addr, lsu;
161         register_t s;
162
163         s = intr_disable();
164         for (addr = 0; addr < PCPU_GET(cache.dc_size);
165             addr += PCPU_GET(cache.dc_linesize))
166                 /*
167                  * Note that US-IV+ additionally require a membar #Sync before
168                  * a load or store to ASI_DCACHE_TAG.
169                  */
170                 __asm __volatile(
171                     "membar #Sync;"
172                     "stxa %%g0, [%0] %1;"
173                     "membar #Sync"
174                     : : "r" (addr), "n" (ASI_DCACHE_TAG));
175
176         /* The I$ must be disabled when flushing it so ensure it's off. */
177         lsu = ldxa(0, ASI_LSU_CTL_REG);
178         stxa(0, ASI_LSU_CTL_REG, lsu & ~(LSU_IC));
179         flush(KERNBASE);
180         for (addr = CHEETAH_ICACHE_TAG_LOWER;
181             addr < PCPU_GET(cache.ic_size) * 2;
182             addr += PCPU_GET(cache.ic_linesize) * 2)
183                 __asm __volatile(
184                     "stxa %%g0, [%0] %1;"
185                     "membar #Sync"
186                     : : "r" (addr), "n" (ASI_ICACHE_TAG));
187         stxa(0, ASI_LSU_CTL_REG, lsu);
188         flush(KERNBASE);
189         intr_restore(s);
190 }
191
192 /*
193  * Flush a physical page from the data cache.
194  */
195 void
196 cheetah_dcache_page_inval(vm_paddr_t spa)
197 {
198         vm_paddr_t pa;
199         void *cookie;
200
201         KASSERT((spa & PAGE_MASK) == 0,
202             ("%s: pa not page aligned", __func__));
203         cookie = ipi_dcache_page_inval(tl_ipi_cheetah_dcache_page_inval, spa);
204         for (pa = spa; pa < spa + PAGE_SIZE;
205             pa += PCPU_GET(cache.dc_linesize))
206                 stxa_sync(pa, ASI_DCACHE_INVALIDATE, 0);
207         ipi_wait(cookie);
208 }
209
210 /*
211  * Flush a physical page from the intsruction cache.  Instruction cache
212  * consistency is maintained by hardware.
213  */
214 void
215 cheetah_icache_page_inval(vm_paddr_t pa __unused)
216 {
217
218 }
219
220 /*
221  * Flush all non-locked mappings from the TLBs.
222  */
223 void
224 cheetah_tlb_flush_nonlocked(void)
225 {
226
227         stxa(TLB_DEMAP_ALL, ASI_DMMU_DEMAP, 0);
228         stxa(TLB_DEMAP_ALL, ASI_IMMU_DEMAP, 0);
229         flush(KERNBASE);
230 }
231
232 /*
233  * Flush all user mappings from the TLBs.
234  */
235 void
236 cheetah_tlb_flush_user(void)
237 {
238         u_long data, tag;
239         register_t s;
240         u_int i, slot;
241
242         /*
243          * We read ASI_{D,I}TLB_DATA_ACCESS_REG twice back-to-back in order
244          * to work around errata of USIII and beyond.
245          */
246         for (i = 0; i < CHEETAH_T16_ENTRIES; i++) {
247                 slot = TLB_DAR_SLOT(TLB_DAR_T16, i);
248                 s = intr_disable();
249                 (void)ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
250                 data = ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
251                 intr_restore(s);
252                 tag = ldxa(slot, ASI_DTLB_TAG_READ_REG);
253                 if ((data & TD_V) != 0 && (data & TD_L) == 0 &&
254                     TLB_TAR_CTX(tag) != TLB_CTX_KERNEL)
255                         stxa_sync(slot, ASI_DTLB_DATA_ACCESS_REG, 0);
256                 s = intr_disable();
257                 (void)ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
258                 data = ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
259                 intr_restore(s);
260                 tag = ldxa(slot, ASI_ITLB_TAG_READ_REG);
261                 if ((data & TD_V) != 0 && (data & TD_L) == 0 &&
262                     TLB_TAR_CTX(tag) != TLB_CTX_KERNEL)
263                         stxa_sync(slot, ASI_ITLB_DATA_ACCESS_REG, 0);
264         }
265         for (i = 0; i < CHEETAH_DT512_ENTRIES; i++) {
266                 slot = TLB_DAR_SLOT(TLB_DAR_DT512_0, i);
267                 s = intr_disable();
268                 (void)ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
269                 data = ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
270                 intr_restore(s);
271                 tag = ldxa(slot, ASI_DTLB_TAG_READ_REG);
272                 if ((data & TD_V) != 0 && TLB_TAR_CTX(tag) != TLB_CTX_KERNEL)
273                         stxa_sync(slot, ASI_DTLB_DATA_ACCESS_REG, 0);
274                 slot = TLB_DAR_SLOT(TLB_DAR_DT512_1, i);
275                 s = intr_disable();
276                 (void)ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
277                 data = ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
278                 intr_restore(s);
279                 tag = ldxa(slot, ASI_DTLB_TAG_READ_REG);
280                 if ((data & TD_V) != 0 && TLB_TAR_CTX(tag) != TLB_CTX_KERNEL)
281                         stxa_sync(slot, ASI_DTLB_DATA_ACCESS_REG, 0);
282         }
283         if (PCPU_GET(impl) == CPU_IMPL_ULTRASPARCIVp) {
284                 for (i = 0; i < CHEETAH_IT512_ENTRIES; i++) {
285                         slot = TLB_DAR_SLOT(TLB_DAR_IT512, i);
286                         s = intr_disable();
287                         (void)ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
288                         data = ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
289                         intr_restore(s);
290                         tag = ldxa(slot, ASI_ITLB_TAG_READ_REG);
291                         if ((data & TD_V) != 0 &&
292                             TLB_TAR_CTX(tag) != TLB_CTX_KERNEL)
293                                 stxa_sync(slot, ASI_ITLB_DATA_ACCESS_REG, 0);
294                 }
295         } else {
296                 for (i = 0; i < CHEETAH_IT128_ENTRIES; i++) {
297                         slot = TLB_DAR_SLOT(TLB_DAR_IT128, i);
298                         s = intr_disable();
299                         (void)ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
300                         data = ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
301                         tag = ldxa(slot, ASI_ITLB_TAG_READ_REG);
302                         intr_restore(s);
303                         if ((data & TD_V) != 0 &&
304                             TLB_TAR_CTX(tag) != TLB_CTX_KERNEL)
305                                 stxa_sync(slot, ASI_ITLB_DATA_ACCESS_REG, 0);
306                 }
307         }
308 }