arm32_tlb.c revision 1.6 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.6 skrll __KERNEL_RCSID(1, "$NetBSD: arm32_tlb.c,v 1.6 2014/10/30 10:38:57 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.4 matt armreg_tlbiasidis_write(lo);
97 1.1 matt }
98 1.2 matt arm_isb();
99 1.2 matt if (__predict_false(vivt_icache_p)) {
100 1.2 matt armreg_icialluis_write(0);
101 1.2 matt }
102 1.2 matt } else {
103 1.2 matt armreg_tlbiall_write(0);
104 1.2 matt arm_isb();
105 1.2 matt if (__predict_false(vivt_icache_p)) {
106 1.2 matt armreg_iciallu_write(0);
107 1.2 matt }
108 1.1 matt }
109 1.1 matt arm_isb();
110 1.1 matt }
111 1.1 matt
112 1.1 matt void
113 1.1 matt tlb_invalidate_addr(vaddr_t va, tlb_asid_t asid)
114 1.1 matt {
115 1.1 matt arm_dsb();
116 1.1 matt va = trunc_page(va) | asid;
117 1.1 matt for (vaddr_t eva = va + PAGE_SIZE; va < eva; va += L2_S_SIZE) {
118 1.3 matt #ifdef MULTIPROCESSOR
119 1.3 matt armreg_tlbimvais_write(va);
120 1.3 matt #else
121 1.1 matt armreg_tlbimva_write(va);
122 1.3 matt #endif
123 1.1 matt //armreg_tlbiall_write(asid);
124 1.1 matt }
125 1.1 matt arm_isb();
126 1.1 matt }
127 1.1 matt
128 1.1 matt bool
129 1.1 matt tlb_update_addr(vaddr_t va, tlb_asid_t asid, pt_entry_t pte, bool insert_p)
130 1.1 matt {
131 1.1 matt tlb_invalidate_addr(va, asid);
132 1.1 matt return true;
133 1.1 matt }
134 1.1 matt
135 1.1 matt #if !defined(MULTIPROCESSOR) && defined(CPU_CORTEXA5)
136 1.1 matt static u_int
137 1.1 matt tlb_cortex_a5_record_asids(u_long *mapp)
138 1.1 matt {
139 1.1 matt u_int nasids = 0;
140 1.1 matt for (size_t va_index = 0; va_index < 63; va_index++) {
141 1.1 matt for (size_t way = 0; way < 2; way++) {
142 1.1 matt armreg_tlbdataop_write(
143 1.1 matt __SHIFTIN(way, ARM_TLBDATAOP_WAY)
144 1.1 matt | __SHIFTIN(va_index, ARM_A5_TLBDATAOP_INDEX));
145 1.1 matt arm_isb();
146 1.1 matt const uint64_t d = ((uint64_t) armreg_tlbdata1_read())
147 1.1 matt | armreg_tlbdata0_read();
148 1.1 matt if (!(d & ARM_TLBDATA_VALID)
149 1.1 matt || !(d & ARM_V5_TLBDATA_nG))
150 1.1 matt continue;
151 1.1 matt
152 1.1 matt const tlb_asid_t asid = __SHIFTOUT(d,
153 1.1 matt ARM_V5_TLBDATA_ASID);
154 1.1 matt const u_long mask = 1L << (asid & 31);
155 1.1 matt const size_t idx = asid >> 5;
156 1.1 matt if (mapp[idx] & mask)
157 1.1 matt continue;
158 1.1 matt
159 1.1 matt mapp[idx] |= mask;
160 1.1 matt nasids++;
161 1.1 matt }
162 1.1 matt }
163 1.1 matt return nasids;
164 1.1 matt }
165 1.1 matt #endif
166 1.1 matt
167 1.1 matt #if !defined(MULTIPROCESSOR) && defined(CPU_CORTEXA7)
168 1.1 matt static u_int
169 1.1 matt tlb_cortex_a7_record_asids(u_long *mapp)
170 1.1 matt {
171 1.1 matt u_int nasids = 0;
172 1.1 matt for (size_t va_index = 0; va_index < 128; va_index++) {
173 1.1 matt for (size_t way = 0; way < 2; way++) {
174 1.1 matt armreg_tlbdataop_write(
175 1.1 matt __SHIFTIN(way, ARM_TLBDATAOP_WAY)
176 1.1 matt | __SHIFTIN(va_index, ARM_A7_TLBDATAOP_INDEX));
177 1.1 matt arm_isb();
178 1.1 matt const uint32_t d0 = armreg_tlbdata0_read();
179 1.1 matt const uint32_t d1 = armreg_tlbdata1_read();
180 1.1 matt if (!(d0 & ARM_TLBDATA_VALID)
181 1.1 matt || !(d1 & ARM_A7_TLBDATA1_nG))
182 1.1 matt continue;
183 1.1 matt
184 1.1 matt const uint64_t d01 = ((uint64_t) d1)|d0;
185 1.1 matt const tlb_asid_t asid = __SHIFTOUT(d01,
186 1.1 matt ARM_A7_TLBDATA01_ASID);
187 1.1 matt const u_long mask = 1L << (asid & 31);
188 1.1 matt const size_t idx = asid >> 5;
189 1.1 matt if (mapp[idx] & mask)
190 1.1 matt continue;
191 1.1 matt
192 1.1 matt mapp[idx] |= mask;
193 1.1 matt nasids++;
194 1.1 matt }
195 1.1 matt }
196 1.1 matt return nasids;
197 1.1 matt }
198 1.1 matt #endif
199 1.1 matt
200 1.1 matt u_int
201 1.1 matt tlb_record_asids(u_long *mapp)
202 1.1 matt {
203 1.1 matt #ifndef MULTIPROCESSOR
204 1.1 matt #ifdef CPU_CORTEXA5
205 1.1 matt if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid))
206 1.1 matt return tlb_cortex_a5_record_asids(mapp);
207 1.1 matt #endif
208 1.1 matt #ifdef CPU_CORTEXA7
209 1.1 matt if (CPU_ID_CORTEX_A7_P(curcpu()->ci_arm_cpuid))
210 1.1 matt return tlb_cortex_a7_record_asids(mapp);
211 1.1 matt #endif
212 1.1 matt #endif /* MULTIPROCESSOR */
213 1.1 matt #ifdef DIAGNOSTIC
214 1.1 matt mapp[0] = 0xfffffffe;
215 1.1 matt mapp[1] = 0xffffffff;
216 1.1 matt mapp[2] = 0xffffffff;
217 1.1 matt mapp[3] = 0xffffffff;
218 1.1 matt mapp[4] = 0xffffffff;
219 1.1 matt mapp[5] = 0xffffffff;
220 1.1 matt mapp[6] = 0xffffffff;
221 1.1 matt mapp[7] = 0xffffffff;
222 1.1 matt #endif
223 1.1 matt return 255;
224 1.1 matt }
225 1.1 matt
226 1.1 matt void
227 1.1 matt tlb_walk(void *ctx, bool (*func)(void *, vaddr_t, tlb_asid_t, pt_entry_t))
228 1.1 matt {
229 1.1 matt /* no way to view the TLB */
230 1.1 matt }
231