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