arm32_tlb.c revision 1.8 1 /*-
2 * Copyright (c) 2013 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "opt_multiprocessor.h"
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(1, "$NetBSD: arm32_tlb.c,v 1.8 2015/02/07 00:08:34 jmcneill Exp $");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37
38 #include <uvm/uvm.h>
39
40 #include <arm/locore.h>
41
42 bool arm_has_tlbiasid_p; // CPU supports TLBIASID system coprocessor op
43
44 tlb_asid_t
45 tlb_get_asid(void)
46 {
47 return armreg_contextidr_read() & 0xff;
48 }
49
50 void
51 tlb_set_asid(tlb_asid_t asid)
52 {
53 arm_dsb();
54 if (asid == KERNEL_PID) {
55 armreg_ttbcr_write(armreg_ttbcr_read() | TTBCR_S_PD0);
56 arm_isb();
57 }
58 armreg_contextidr_write(asid);
59 arm_isb();
60 }
61
62 void
63 tlb_invalidate_all(void)
64 {
65 const bool vivt_icache_p = arm_pcache.icache_type == CACHE_TYPE_VIVT;
66 arm_dsb();
67 #ifdef MULTIPROCESSOR
68 armreg_tlbiallis_write(0);
69 #else
70 armreg_tlbiall_write(0);
71 #endif
72 arm_isb();
73 if (__predict_false(vivt_icache_p)) {
74 if (arm_has_tlbiasid_p) {
75 armreg_icialluis_write(0);
76 } else {
77 armreg_iciallu_write(0);
78 }
79 }
80 arm_isb();
81 }
82
83 void
84 tlb_invalidate_globals(void)
85 {
86 tlb_invalidate_all();
87 }
88
89 void
90 tlb_invalidate_asids(tlb_asid_t lo, tlb_asid_t hi)
91 {
92 const bool vivt_icache_p = arm_pcache.icache_type == CACHE_TYPE_VIVT;
93 arm_dsb();
94 if (arm_has_tlbiasid_p) {
95 for (; lo <= hi; lo++) {
96 armreg_tlbiasidis_write(lo);
97 }
98 arm_isb();
99 if (__predict_false(vivt_icache_p)) {
100 armreg_icialluis_write(0);
101 }
102 } else {
103 armreg_tlbiall_write(0);
104 arm_isb();
105 if (__predict_false(vivt_icache_p)) {
106 armreg_iciallu_write(0);
107 }
108 }
109 arm_isb();
110 }
111
112 void
113 tlb_invalidate_addr(vaddr_t va, tlb_asid_t asid)
114 {
115 arm_dsb();
116 va = trunc_page(va) | asid;
117 for (vaddr_t eva = va + PAGE_SIZE; va < eva; va += L2_S_SIZE) {
118 #ifdef MULTIPROCESSOR
119 armreg_tlbimvais_write(va);
120 #else
121 armreg_tlbimva_write(va);
122 #endif
123 //armreg_tlbiall_write(asid);
124 }
125 arm_dsb();
126 arm_isb();
127 }
128
129 bool
130 tlb_update_addr(vaddr_t va, tlb_asid_t asid, pt_entry_t pte, bool insert_p)
131 {
132 tlb_invalidate_addr(va, asid);
133 return true;
134 }
135
136 #if !defined(MULTIPROCESSOR) && defined(CPU_CORTEXA5)
137 static u_int
138 tlb_cortex_a5_record_asids(u_long *mapp)
139 {
140 u_int nasids = 0;
141 for (size_t va_index = 0; va_index < 63; va_index++) {
142 for (size_t way = 0; way < 2; way++) {
143 armreg_tlbdataop_write(
144 __SHIFTIN(way, ARM_TLBDATAOP_WAY)
145 | __SHIFTIN(va_index, ARM_A5_TLBDATAOP_INDEX));
146 arm_isb();
147 const uint64_t d = ((uint64_t) armreg_tlbdata1_read())
148 | armreg_tlbdata0_read();
149 if (!(d & ARM_TLBDATA_VALID)
150 || !(d & ARM_A5_TLBDATA_nG))
151 continue;
152
153 const tlb_asid_t asid = __SHIFTOUT(d,
154 ARM_A5_TLBDATA_ASID);
155 const u_long mask = 1L << (asid & 31);
156 const size_t idx = asid >> 5;
157 if (mapp[idx] & mask)
158 continue;
159
160 mapp[idx] |= mask;
161 nasids++;
162 }
163 }
164 return nasids;
165 }
166 #endif
167
168 #if !defined(MULTIPROCESSOR) && defined(CPU_CORTEXA7)
169 static u_int
170 tlb_cortex_a7_record_asids(u_long *mapp)
171 {
172 u_int nasids = 0;
173 for (size_t va_index = 0; va_index < 128; va_index++) {
174 for (size_t way = 0; way < 2; way++) {
175 armreg_tlbdataop_write(
176 __SHIFTIN(way, ARM_TLBDATAOP_WAY)
177 | __SHIFTIN(va_index, ARM_A7_TLBDATAOP_INDEX));
178 arm_isb();
179 const uint32_t d0 = armreg_tlbdata0_read();
180 const uint32_t d1 = armreg_tlbdata1_read();
181 if (!(d0 & ARM_TLBDATA_VALID)
182 || !(d1 & ARM_A7_TLBDATA1_nG))
183 continue;
184
185 const uint64_t d01 = ((uint64_t) d1)|d0;
186 const tlb_asid_t asid = __SHIFTOUT(d01,
187 ARM_A7_TLBDATA01_ASID);
188 const u_long mask = 1L << (asid & 31);
189 const size_t idx = asid >> 5;
190 if (mapp[idx] & mask)
191 continue;
192
193 mapp[idx] |= mask;
194 nasids++;
195 }
196 }
197 return nasids;
198 }
199 #endif
200
201 u_int
202 tlb_record_asids(u_long *mapp)
203 {
204 #ifndef MULTIPROCESSOR
205 #ifdef CPU_CORTEXA5
206 if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid))
207 return tlb_cortex_a5_record_asids(mapp);
208 #endif
209 #ifdef CPU_CORTEXA7
210 if (CPU_ID_CORTEX_A7_P(curcpu()->ci_arm_cpuid))
211 return tlb_cortex_a7_record_asids(mapp);
212 #endif
213 #endif /* MULTIPROCESSOR */
214 #ifdef DIAGNOSTIC
215 mapp[0] = 0xfffffffe;
216 mapp[1] = 0xffffffff;
217 mapp[2] = 0xffffffff;
218 mapp[3] = 0xffffffff;
219 mapp[4] = 0xffffffff;
220 mapp[5] = 0xffffffff;
221 mapp[6] = 0xffffffff;
222 mapp[7] = 0xffffffff;
223 #endif
224 return 255;
225 }
226
227 void
228 tlb_walk(void *ctx, bool (*func)(void *, vaddr_t, tlb_asid_t, pt_entry_t))
229 {
230 /* no way to view the TLB */
231 }
232