e500_tlb.c revision 1.2.4.2 1 1.2.4.2 rmind /* $NetBSD: e500_tlb.c,v 1.2.4.2 2011/03/05 20:51:34 rmind Exp $ */
2 1.2.4.2 rmind /*-
3 1.2.4.2 rmind * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 1.2.4.2 rmind * All rights reserved.
5 1.2.4.2 rmind *
6 1.2.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
7 1.2.4.2 rmind * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 1.2.4.2 rmind * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 1.2.4.2 rmind *
10 1.2.4.2 rmind * This material is based upon work supported by the Defense Advanced Research
11 1.2.4.2 rmind * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 1.2.4.2 rmind * Contract No. N66001-09-C-2073.
13 1.2.4.2 rmind * Approved for Public Release, Distribution Unlimited
14 1.2.4.2 rmind *
15 1.2.4.2 rmind * Redistribution and use in source and binary forms, with or without
16 1.2.4.2 rmind * modification, are permitted provided that the following conditions
17 1.2.4.2 rmind * are met:
18 1.2.4.2 rmind * 1. Redistributions of source code must retain the above copyright
19 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer.
20 1.2.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
21 1.2.4.2 rmind * notice, this list of conditions and the following disclaimer in the
22 1.2.4.2 rmind * documentation and/or other materials provided with the distribution.
23 1.2.4.2 rmind *
24 1.2.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.2.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.2.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.2.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.2.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.2.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.2.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.2.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.2.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.2.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.2.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
35 1.2.4.2 rmind */
36 1.2.4.2 rmind
37 1.2.4.2 rmind #include <sys/cdefs.h>
38 1.2.4.2 rmind
39 1.2.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: e500_tlb.c,v 1.2.4.2 2011/03/05 20:51:34 rmind Exp $");
40 1.2.4.2 rmind
41 1.2.4.2 rmind #include <sys/param.h>
42 1.2.4.2 rmind
43 1.2.4.2 rmind #include <uvm/uvm_extern.h>
44 1.2.4.2 rmind
45 1.2.4.2 rmind #include <powerpc/spr.h>
46 1.2.4.2 rmind #include <powerpc/booke/spr.h>
47 1.2.4.2 rmind #include <powerpc/booke/cpuvar.h>
48 1.2.4.2 rmind #include <powerpc/booke/e500var.h>
49 1.2.4.2 rmind #include <powerpc/booke/pmap.h>
50 1.2.4.2 rmind
51 1.2.4.2 rmind struct e500_tlb {
52 1.2.4.2 rmind vaddr_t tlb_va;
53 1.2.4.2 rmind uint32_t tlb_pte;
54 1.2.4.2 rmind uint32_t tlb_asid;
55 1.2.4.2 rmind vsize_t tlb_size;
56 1.2.4.2 rmind };
57 1.2.4.2 rmind
58 1.2.4.2 rmind struct e500_hwtlb {
59 1.2.4.2 rmind uint32_t hwtlb_mas0;
60 1.2.4.2 rmind uint32_t hwtlb_mas1;
61 1.2.4.2 rmind uint32_t hwtlb_mas2;
62 1.2.4.2 rmind uint32_t hwtlb_mas3;
63 1.2.4.2 rmind };
64 1.2.4.2 rmind
65 1.2.4.2 rmind struct e500_xtlb {
66 1.2.4.2 rmind struct e500_tlb e_tlb;
67 1.2.4.2 rmind struct e500_hwtlb e_hwtlb;
68 1.2.4.2 rmind u_long e_refcnt;
69 1.2.4.2 rmind };
70 1.2.4.2 rmind
71 1.2.4.2 rmind static struct e500_tlb1 {
72 1.2.4.2 rmind uint32_t tlb1_maxsize;
73 1.2.4.2 rmind uint32_t tlb1_minsize;
74 1.2.4.2 rmind u_int tlb1_numentries;
75 1.2.4.2 rmind u_int tlb1_numfree;
76 1.2.4.2 rmind u_int tlb1_freelist[32];
77 1.2.4.2 rmind struct e500_xtlb tlb1_entries[32];
78 1.2.4.2 rmind } e500_tlb1;
79 1.2.4.2 rmind
80 1.2.4.2 rmind static inline register_t mftlb0cfg(void) __pure;
81 1.2.4.2 rmind static inline register_t mftlb1cfg(void) __pure;
82 1.2.4.2 rmind
83 1.2.4.2 rmind static inline register_t
84 1.2.4.2 rmind mftlb0cfg(void)
85 1.2.4.2 rmind {
86 1.2.4.2 rmind register_t tlb0cfg;
87 1.2.4.2 rmind __asm("mfspr %0, %1" : "=r"(tlb0cfg) : "n"(SPR_TLB0CFG));
88 1.2.4.2 rmind return tlb0cfg;
89 1.2.4.2 rmind }
90 1.2.4.2 rmind
91 1.2.4.2 rmind static inline register_t
92 1.2.4.2 rmind mftlb1cfg(void)
93 1.2.4.2 rmind {
94 1.2.4.2 rmind register_t tlb1cfg;
95 1.2.4.2 rmind __asm("mfspr %0, %1" : "=r"(tlb1cfg) : "n"(SPR_TLB1CFG));
96 1.2.4.2 rmind return tlb1cfg;
97 1.2.4.2 rmind }
98 1.2.4.2 rmind
99 1.2.4.2 rmind static struct e500_tlb
100 1.2.4.2 rmind hwtlb_to_tlb(const struct e500_hwtlb hwtlb)
101 1.2.4.2 rmind {
102 1.2.4.2 rmind struct e500_tlb tlb;
103 1.2.4.2 rmind register_t prot_mask;
104 1.2.4.2 rmind u_int prot_shift;
105 1.2.4.2 rmind
106 1.2.4.2 rmind tlb.tlb_va = MAS2_EPN & hwtlb.hwtlb_mas2;
107 1.2.4.2 rmind tlb.tlb_size = 1024 << (2 * MASX_TSIZE_GET(hwtlb.hwtlb_mas1));
108 1.2.4.2 rmind tlb.tlb_asid = MASX_TID_GET(hwtlb.hwtlb_mas1);
109 1.2.4.2 rmind tlb.tlb_pte = (hwtlb.hwtlb_mas2 & MAS2_WIMGE)
110 1.2.4.2 rmind | (hwtlb.hwtlb_mas3 & MAS3_RPN);
111 1.2.4.2 rmind if (hwtlb.hwtlb_mas1 & MAS1_TS) {
112 1.2.4.2 rmind prot_mask = MAS3_UX|MAS3_UW|MAS3_UR;
113 1.2.4.2 rmind prot_shift = PTE_RWX_SHIFT - 1;
114 1.2.4.2 rmind } else {
115 1.2.4.2 rmind prot_mask = MAS3_SX|MAS3_SW|MAS3_SR;
116 1.2.4.2 rmind prot_shift = PTE_RWX_SHIFT;
117 1.2.4.2 rmind }
118 1.2.4.2 rmind tlb.tlb_pte |= (prot_mask & hwtlb.hwtlb_mas3) << prot_shift;
119 1.2.4.2 rmind return tlb;
120 1.2.4.2 rmind }
121 1.2.4.2 rmind
122 1.2.4.2 rmind static inline struct e500_hwtlb
123 1.2.4.2 rmind hwtlb_read(uint32_t mas0, u_int slot)
124 1.2.4.2 rmind {
125 1.2.4.2 rmind struct e500_hwtlb hwtlb;
126 1.2.4.2 rmind register_t tlbcfg;
127 1.2.4.2 rmind
128 1.2.4.2 rmind if (__predict_true(mas0 == MAS0_TLBSEL_TLB0)) {
129 1.2.4.2 rmind tlbcfg = mftlb0cfg();
130 1.2.4.2 rmind } else if (mas0 == MAS0_TLBSEL_TLB1) {
131 1.2.4.2 rmind tlbcfg = mftlb1cfg();
132 1.2.4.2 rmind } else {
133 1.2.4.2 rmind panic("%s:%d: unexpected MAS0 %#" PRIx32,
134 1.2.4.2 rmind __func__, __LINE__, mas0);
135 1.2.4.2 rmind }
136 1.2.4.2 rmind
137 1.2.4.2 rmind /*
138 1.2.4.2 rmind * ESEL is the way we want to look up.
139 1.2.4.2 rmind * If tlbassoc is the same as tlbentries (like in TLB1) then the TLB is
140 1.2.4.2 rmind * fully associative, the entire slot is placed into ESEL. If tlbassoc
141 1.2.4.2 rmind * is less then the number of tlb entries, the slot is split in two
142 1.2.4.2 rmind * fields. Since the TLB is M rows by N ways, the lowers bits are for
143 1.2.4.2 rmind * row (MAS2[EPN]) and the upper for the way (MAS1[ESEL]).
144 1.2.4.2 rmind */
145 1.2.4.2 rmind const u_int tlbassoc = TLBCFG_ASSOC(tlbcfg);
146 1.2.4.2 rmind const u_int tlbentries = TLBCFG_NENTRY(tlbcfg);
147 1.2.4.2 rmind const u_int esel_shift =
148 1.2.4.2 rmind __builtin_clz(tlbassoc) - __builtin_clz(tlbentries);
149 1.2.4.2 rmind
150 1.2.4.2 rmind /*
151 1.2.4.2 rmind * Disable interrupts since we don't want anyone else mucking with
152 1.2.4.2 rmind * the MMU Assist registers
153 1.2.4.2 rmind */
154 1.2.4.2 rmind const register_t msr = wrtee(0);
155 1.2.4.2 rmind const register_t saved_mas0 = mfspr(SPR_MAS0);
156 1.2.4.2 rmind mtspr(SPR_MAS0, mas0 | MAS0_ESEL_MAKE(slot >> esel_shift));
157 1.2.4.2 rmind
158 1.2.4.2 rmind if (__predict_true(tlbassoc > tlbentries))
159 1.2.4.2 rmind mtspr(SPR_MAS2, slot << PAGE_SHIFT);
160 1.2.4.2 rmind
161 1.2.4.2 rmind /*
162 1.2.4.2 rmind * Now select the entry and grab its contents.
163 1.2.4.2 rmind */
164 1.2.4.2 rmind __asm volatile("tlbre");
165 1.2.4.2 rmind
166 1.2.4.2 rmind hwtlb.hwtlb_mas0 = mfspr(SPR_MAS0);
167 1.2.4.2 rmind hwtlb.hwtlb_mas1 = mfspr(SPR_MAS1);
168 1.2.4.2 rmind hwtlb.hwtlb_mas2 = mfspr(SPR_MAS2);
169 1.2.4.2 rmind hwtlb.hwtlb_mas3 = mfspr(SPR_MAS3);
170 1.2.4.2 rmind
171 1.2.4.2 rmind mtspr(SPR_MAS0, saved_mas0);
172 1.2.4.2 rmind wrtee(msr); /* restore interrupts */
173 1.2.4.2 rmind
174 1.2.4.2 rmind return hwtlb;
175 1.2.4.2 rmind }
176 1.2.4.2 rmind
177 1.2.4.2 rmind static inline void
178 1.2.4.2 rmind hwtlb_write(const struct e500_hwtlb hwtlb, bool needs_sync)
179 1.2.4.2 rmind {
180 1.2.4.2 rmind const register_t msr = wrtee(0);
181 1.2.4.2 rmind const uint32_t saved_mas0 = mfspr(SPR_MAS0);
182 1.2.4.2 rmind
183 1.2.4.2 rmind /*
184 1.2.4.2 rmind * Need to always write MAS0 and MAS1
185 1.2.4.2 rmind */
186 1.2.4.2 rmind mtspr(SPR_MAS0, hwtlb.hwtlb_mas0);
187 1.2.4.2 rmind mtspr(SPR_MAS1, hwtlb.hwtlb_mas1);
188 1.2.4.2 rmind
189 1.2.4.2 rmind /*
190 1.2.4.2 rmind * Only write the VPN/WIMGE if this is in TLB0 or if a valid mapping.
191 1.2.4.2 rmind */
192 1.2.4.2 rmind if ((hwtlb.hwtlb_mas0 & MAS0_TLBSEL) == MAS0_TLBSEL_TLB0
193 1.2.4.2 rmind || (hwtlb.hwtlb_mas1 & MAS1_V)) {
194 1.2.4.2 rmind mtspr(SPR_MAS2, hwtlb.hwtlb_mas2);
195 1.2.4.2 rmind }
196 1.2.4.2 rmind /*
197 1.2.4.2 rmind * Only need to write the RPN/prot if we are dealing with a valid
198 1.2.4.2 rmind * mapping.
199 1.2.4.2 rmind */
200 1.2.4.2 rmind if (hwtlb.hwtlb_mas1 & MAS1_V) {
201 1.2.4.2 rmind mtspr(SPR_MAS3, hwtlb.hwtlb_mas3);
202 1.2.4.2 rmind }
203 1.2.4.2 rmind
204 1.2.4.2 rmind #if 0
205 1.2.4.2 rmind printf("%s->[%x,%x,%x,%x]\n",
206 1.2.4.2 rmind __func__,
207 1.2.4.2 rmind hwtlb.hwtlb_mas0, hwtlb.hwtlb_mas1,
208 1.2.4.2 rmind hwtlb.hwtlb_mas2, hwtlb.hwtlb_mas3);
209 1.2.4.2 rmind #endif
210 1.2.4.2 rmind __asm volatile("tlbwe");
211 1.2.4.2 rmind if (needs_sync) {
212 1.2.4.2 rmind __asm volatile("tlbsync");
213 1.2.4.2 rmind __asm volatile("isync");
214 1.2.4.2 rmind }
215 1.2.4.2 rmind
216 1.2.4.2 rmind mtspr(SPR_MAS0, saved_mas0);
217 1.2.4.2 rmind wrtee(msr);
218 1.2.4.2 rmind }
219 1.2.4.2 rmind
220 1.2.4.2 rmind static struct e500_hwtlb
221 1.2.4.2 rmind tlb_to_hwtlb(const struct e500_tlb tlb)
222 1.2.4.2 rmind {
223 1.2.4.2 rmind struct e500_hwtlb hwtlb;
224 1.2.4.2 rmind
225 1.2.4.2 rmind KASSERT(trunc_page(tlb.tlb_va) == tlb.tlb_va);
226 1.2.4.2 rmind KASSERT(tlb.tlb_size != 0);
227 1.2.4.2 rmind KASSERT((tlb.tlb_size & (tlb.tlb_size - 1)) == 0);
228 1.2.4.2 rmind const uint32_t prot_mask = tlb.tlb_pte & PTE_RWX_MASK;
229 1.2.4.2 rmind if (__predict_true(tlb.tlb_size == PAGE_SIZE)) {
230 1.2.4.2 rmind hwtlb.hwtlb_mas0 = 0;
231 1.2.4.2 rmind hwtlb.hwtlb_mas1 = MAS1_V | MASX_TSIZE_MAKE(1);
232 1.2.4.2 rmind /*
233 1.2.4.2 rmind * A non-zero ASID means this is a user page so mark it as
234 1.2.4.2 rmind * being in the user's address space.
235 1.2.4.2 rmind */
236 1.2.4.2 rmind if (tlb.tlb_asid) {
237 1.2.4.2 rmind hwtlb.hwtlb_mas1 |= MAS1_TS
238 1.2.4.2 rmind | MASX_TID_MAKE(tlb.tlb_asid);
239 1.2.4.2 rmind hwtlb.hwtlb_mas3 = (prot_mask >> (PTE_RWX_SHIFT - 1))
240 1.2.4.2 rmind | ((prot_mask & ~PTE_xX) >> PTE_RWX_SHIFT);
241 1.2.4.2 rmind KASSERT(prot_mask & PTE_xR);
242 1.2.4.2 rmind KASSERT(hwtlb.hwtlb_mas3 & MAS3_UR);
243 1.2.4.2 rmind CTASSERT(MAS3_UR == (PTE_xR >> (PTE_RWX_SHIFT - 1)));
244 1.2.4.2 rmind CTASSERT(MAS3_SR == (PTE_xR >> PTE_RWX_SHIFT));
245 1.2.4.2 rmind } else {
246 1.2.4.2 rmind hwtlb.hwtlb_mas3 = prot_mask >> PTE_RWX_SHIFT;
247 1.2.4.2 rmind }
248 1.2.4.2 rmind if (tlb.tlb_pte & PTE_UNMODIFIED)
249 1.2.4.2 rmind hwtlb.hwtlb_mas3 &= ~(MAS3_UW|MAS3_SW);
250 1.2.4.2 rmind if (tlb.tlb_pte & PTE_UNSYNCED)
251 1.2.4.2 rmind hwtlb.hwtlb_mas3 &= ~(MAS3_UX|MAS3_SX);
252 1.2.4.2 rmind } else {
253 1.2.4.2 rmind KASSERT(tlb.tlb_asid == 0);
254 1.2.4.2 rmind KASSERT((tlb.tlb_size & 0xaaaaa7ff) == 0);
255 1.2.4.2 rmind u_int cntlz = __builtin_clz(tlb.tlb_size);
256 1.2.4.2 rmind KASSERT(cntlz & 1);
257 1.2.4.2 rmind KASSERT(cntlz <= 19);
258 1.2.4.2 rmind hwtlb.hwtlb_mas0 = MAS0_TLBSEL_TLB1;
259 1.2.4.2 rmind /*
260 1.2.4.2 rmind * TSIZE is defined (4^TSIZE) Kbytes except a TSIZE of is not
261 1.2.4.2 rmind * allowed. So 1K would be 0x00000400 giving 21 leading zero
262 1.2.4.2 rmind * bits. Subtracting the leading number of zero bits from 21
263 1.2.4.2 rmind * and dividing by 2 gives us the number that the MMU wants.
264 1.2.4.2 rmind */
265 1.2.4.2 rmind hwtlb.hwtlb_mas1 = MASX_TSIZE_MAKE(((31 - 10) - cntlz) / 2)
266 1.2.4.2 rmind | MAS1_IPROT | MAS1_V;
267 1.2.4.2 rmind hwtlb.hwtlb_mas3 = prot_mask >> PTE_RWX_SHIFT;
268 1.2.4.2 rmind }
269 1.2.4.2 rmind /* We are done with MAS1, on to MAS2 ... */
270 1.2.4.2 rmind hwtlb.hwtlb_mas2 = tlb.tlb_va | (tlb.tlb_pte & PTE_WIMGE_MASK);
271 1.2.4.2 rmind hwtlb.hwtlb_mas3 |= tlb.tlb_pte & PTE_RPN_MASK;
272 1.2.4.2 rmind
273 1.2.4.2 rmind return hwtlb;
274 1.2.4.2 rmind }
275 1.2.4.2 rmind
276 1.2.4.2 rmind static int
277 1.2.4.2 rmind e500_alloc_tlb1_entry(void)
278 1.2.4.2 rmind {
279 1.2.4.2 rmind struct e500_tlb1 * const tlb1 = &e500_tlb1;
280 1.2.4.2 rmind
281 1.2.4.2 rmind if (tlb1->tlb1_numfree == 0)
282 1.2.4.2 rmind return -1;
283 1.2.4.2 rmind const u_int slot = tlb1->tlb1_freelist[--tlb1->tlb1_numfree];
284 1.2.4.2 rmind KASSERT((tlb1->tlb1_entries[slot].e_hwtlb.hwtlb_mas1 & MAS1_V) == 0);
285 1.2.4.2 rmind tlb1->tlb1_entries[slot].e_hwtlb.hwtlb_mas0 =
286 1.2.4.2 rmind MAS0_TLBSEL_TLB1 | __SHIFTOUT(slot, MAS0_ESEL);
287 1.2.4.2 rmind return slot;
288 1.2.4.2 rmind }
289 1.2.4.2 rmind
290 1.2.4.2 rmind static void
291 1.2.4.2 rmind e500_free_tlb1_entry(struct e500_xtlb *xtlb, u_int slot, bool needs_sync)
292 1.2.4.2 rmind {
293 1.2.4.2 rmind struct e500_tlb1 * const tlb1 = &e500_tlb1;
294 1.2.4.2 rmind KASSERT(slot < tlb1->tlb1_numentries);
295 1.2.4.2 rmind KASSERT(&tlb1->tlb1_entries[slot] == xtlb);
296 1.2.4.2 rmind
297 1.2.4.2 rmind KASSERT(xtlb->e_hwtlb.hwtlb_mas0 == (MAS0_TLBSEL_TLB1|__SHIFTIN(slot, MAS0_ESEL)));
298 1.2.4.2 rmind xtlb->e_hwtlb.hwtlb_mas1 &= ~(MAS1_V|MAS1_IPROT);
299 1.2.4.2 rmind hwtlb_write(xtlb->e_hwtlb, needs_sync);
300 1.2.4.2 rmind
301 1.2.4.2 rmind const register_t msr = wrtee(0);
302 1.2.4.2 rmind tlb1->tlb1_freelist[tlb1->tlb1_numfree++] = slot;
303 1.2.4.2 rmind wrtee(msr);
304 1.2.4.2 rmind }
305 1.2.4.2 rmind
306 1.2.4.2 rmind static void e500_tlb_set_asid(uint32_t asid)
307 1.2.4.2 rmind {
308 1.2.4.2 rmind mtspr(SPR_PID0, asid);
309 1.2.4.2 rmind }
310 1.2.4.2 rmind
311 1.2.4.2 rmind static void e500_tlb_invalidate_all(void)
312 1.2.4.2 rmind {
313 1.2.4.2 rmind /*
314 1.2.4.2 rmind * This does a flash invalidate of all entries in TLB0.
315 1.2.4.2 rmind * We don't touch TLB1 since we don't expect those to be volatile.
316 1.2.4.2 rmind */
317 1.2.4.2 rmind #if 1
318 1.2.4.2 rmind __asm volatile("tlbivax\t0, %0" :: "b"(4)); /* INV_ALL */
319 1.2.4.2 rmind #else
320 1.2.4.2 rmind mtspr(SPR_MMUCSR0, MMUCSR0_TLB0_FL);
321 1.2.4.2 rmind while (mfspr(SPR_MMUCSR0) != 0)
322 1.2.4.2 rmind ;
323 1.2.4.2 rmind #endif
324 1.2.4.2 rmind }
325 1.2.4.2 rmind
326 1.2.4.2 rmind static void
327 1.2.4.2 rmind e500_tlb_invalidate_globals(void)
328 1.2.4.2 rmind {
329 1.2.4.2 rmind const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
330 1.2.4.2 rmind const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
331 1.2.4.2 rmind const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
332 1.2.4.2 rmind const vaddr_t kstack_lo = (uintptr_t)curlwp->l_addr;
333 1.2.4.2 rmind const vaddr_t kstack_hi = kstack_lo + USPACE - 1;
334 1.2.4.2 rmind const vaddr_t epn_kstack_lo = kstack_lo & (max_epn - 1);
335 1.2.4.2 rmind const vaddr_t epn_kstack_hi = kstack_hi & (max_epn - 1);
336 1.2.4.2 rmind
337 1.2.4.2 rmind const register_t msr = wrtee(0);
338 1.2.4.2 rmind for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
339 1.2.4.2 rmind mtspr(SPR_MAS0, MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0);
340 1.2.4.2 rmind for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
341 1.2.4.2 rmind mtspr(SPR_MAS2, epn);
342 1.2.4.2 rmind __asm volatile("tlbre");
343 1.2.4.2 rmind uint32_t mas1 = mfspr(SPR_MAS1);
344 1.2.4.2 rmind
345 1.2.4.2 rmind /*
346 1.2.4.2 rmind * Make sure this is a valid kernel entry first.
347 1.2.4.2 rmind */
348 1.2.4.2 rmind if ((mas1 & (MAS1_V|MAS1_TID|MAS1_TS)) != MAS1_V)
349 1.2.4.2 rmind continue;
350 1.2.4.2 rmind
351 1.2.4.2 rmind /*
352 1.2.4.2 rmind * We have a valid kernel TLB entry. But if it matches
353 1.2.4.2 rmind * the stack we are currently running on, it would
354 1.2.4.2 rmind * unwise to invalidate it. First see if the epn
355 1.2.4.2 rmind * overlaps the stack. If it does then get the
356 1.2.4.2 rmind * VA and see if it really is part of the stack.
357 1.2.4.2 rmind */
358 1.2.4.2 rmind if (epn_kstack_lo < epn_kstack_hi
359 1.2.4.2 rmind ? (epn_kstack_lo <= epn && epn <= epn_kstack_hi)
360 1.2.4.2 rmind : (epn <= epn_kstack_hi || epn_kstack_lo <= epn)) {
361 1.2.4.2 rmind const uint32_t mas2_epn =
362 1.2.4.2 rmind mfspr(SPR_MAS2) & MAS2_EPN;
363 1.2.4.2 rmind if (kstack_lo <= mas2_epn
364 1.2.4.2 rmind && mas2_epn <= kstack_hi)
365 1.2.4.2 rmind continue;
366 1.2.4.2 rmind }
367 1.2.4.2 rmind mtspr(SPR_MAS1, mas1 ^ MAS1_V);
368 1.2.4.2 rmind __asm volatile("tlbwe");
369 1.2.4.2 rmind }
370 1.2.4.2 rmind }
371 1.2.4.2 rmind __asm volatile("isync");
372 1.2.4.2 rmind wrtee(msr);
373 1.2.4.2 rmind }
374 1.2.4.2 rmind
375 1.2.4.2 rmind static void
376 1.2.4.2 rmind e500_tlb_invalidate_asids(uint32_t asid_lo, uint32_t asid_hi)
377 1.2.4.2 rmind {
378 1.2.4.2 rmind const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
379 1.2.4.2 rmind const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
380 1.2.4.2 rmind const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
381 1.2.4.2 rmind
382 1.2.4.2 rmind asid_lo = __SHIFTIN(asid_lo, MAS1_TID);
383 1.2.4.2 rmind asid_hi = __SHIFTIN(asid_hi, MAS1_TID);
384 1.2.4.2 rmind
385 1.2.4.2 rmind const register_t msr = wrtee(0);
386 1.2.4.2 rmind for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
387 1.2.4.2 rmind mtspr(SPR_MAS0, MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0);
388 1.2.4.2 rmind for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
389 1.2.4.2 rmind mtspr(SPR_MAS2, epn);
390 1.2.4.2 rmind __asm volatile("tlbre");
391 1.2.4.2 rmind const uint32_t mas1 = mfspr(SPR_MAS1);
392 1.2.4.2 rmind /*
393 1.2.4.2 rmind * If this is a valid entry for AS space 1 and
394 1.2.4.2 rmind * its asid matches the constraints of the caller,
395 1.2.4.2 rmind * clear its valid bit.
396 1.2.4.2 rmind */
397 1.2.4.2 rmind if ((mas1 & (MAS1_V|MAS1_TS)) == (MAS1_V|MAS1_TS)
398 1.2.4.2 rmind && asid_lo <= (mas1 & MAS1_TID)
399 1.2.4.2 rmind && (mas1 & MAS1_TID) < asid_hi) {
400 1.2.4.2 rmind mtspr(SPR_MAS1, mas1 ^ MAS1_V);
401 1.2.4.2 rmind #if 0
402 1.2.4.2 rmind printf("%s[%zu,%zu]->[%x]\n",
403 1.2.4.2 rmind __func__, assoc, epn, mas1);
404 1.2.4.2 rmind #endif
405 1.2.4.2 rmind __asm volatile("tlbwe");
406 1.2.4.2 rmind }
407 1.2.4.2 rmind }
408 1.2.4.2 rmind }
409 1.2.4.2 rmind __asm volatile("isync");
410 1.2.4.2 rmind wrtee(msr);
411 1.2.4.2 rmind }
412 1.2.4.2 rmind
413 1.2.4.2 rmind static u_int
414 1.2.4.2 rmind e500_tlb_record_asids(u_long *bitmap, uint32_t start_slot)
415 1.2.4.2 rmind {
416 1.2.4.2 rmind const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
417 1.2.4.2 rmind const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
418 1.2.4.2 rmind const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
419 1.2.4.2 rmind const size_t nbits = 8 * sizeof(bitmap[0]);
420 1.2.4.2 rmind u_int found = 0;
421 1.2.4.2 rmind
422 1.2.4.2 rmind const register_t msr = wrtee(0);
423 1.2.4.2 rmind for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
424 1.2.4.2 rmind mtspr(SPR_MAS0, MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0);
425 1.2.4.2 rmind for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
426 1.2.4.2 rmind mtspr(SPR_MAS2, epn);
427 1.2.4.2 rmind __asm volatile("tlbre");
428 1.2.4.2 rmind const uint32_t mas1 = mfspr(SPR_MAS1);
429 1.2.4.2 rmind /*
430 1.2.4.2 rmind * If this is a valid entry for AS space 1 and
431 1.2.4.2 rmind * its asid matches the constraints of the caller,
432 1.2.4.2 rmind * clear its valid bit.
433 1.2.4.2 rmind */
434 1.2.4.2 rmind if ((mas1 & (MAS1_V|MAS1_TS)) == (MAS1_V|MAS1_TS)) {
435 1.2.4.2 rmind const uint32_t asid = MASX_TID_GET(mas1);
436 1.2.4.2 rmind const u_int i = asid / nbits;
437 1.2.4.2 rmind const u_long mask = 1UL << (asid & (nbits - 1));
438 1.2.4.2 rmind if ((bitmap[i] & mask) == 0) {
439 1.2.4.2 rmind bitmap[i] |= mask;
440 1.2.4.2 rmind found++;
441 1.2.4.2 rmind }
442 1.2.4.2 rmind }
443 1.2.4.2 rmind }
444 1.2.4.2 rmind }
445 1.2.4.2 rmind wrtee(msr);
446 1.2.4.2 rmind
447 1.2.4.2 rmind return found;
448 1.2.4.2 rmind }
449 1.2.4.2 rmind
450 1.2.4.2 rmind static void
451 1.2.4.2 rmind e500_tlb_invalidate_addr(vaddr_t va, uint32_t asid)
452 1.2.4.2 rmind {
453 1.2.4.2 rmind KASSERT((va & PAGE_MASK) == 0);
454 1.2.4.2 rmind /*
455 1.2.4.2 rmind * Bits 60 & 61 have meaning
456 1.2.4.2 rmind */
457 1.2.4.2 rmind __asm volatile("tlbivax\t0, %0" :: "b"(va));
458 1.2.4.2 rmind __asm volatile("tlbsync");
459 1.2.4.2 rmind __asm volatile("tlbsync");
460 1.2.4.2 rmind }
461 1.2.4.2 rmind
462 1.2.4.2 rmind static bool
463 1.2.4.2 rmind e500_tlb_update_addr(vaddr_t va, uint32_t asid, uint32_t pte, bool insert)
464 1.2.4.2 rmind {
465 1.2.4.2 rmind struct e500_hwtlb hwtlb = tlb_to_hwtlb(
466 1.2.4.2 rmind (struct e500_tlb){ .tlb_va = va, .tlb_asid = asid,
467 1.2.4.2 rmind .tlb_size = PAGE_SIZE, .tlb_pte = pte,});
468 1.2.4.2 rmind
469 1.2.4.2 rmind register_t msr = wrtee(0);
470 1.2.4.2 rmind mtspr(SPR_MAS6, asid ? __SHIFTIN(asid, MAS6_SPID0) | MAS6_SAS : 0);
471 1.2.4.2 rmind __asm volatile("tlbsx 0, %0" :: "b"(va));
472 1.2.4.2 rmind register_t mas1 = mfspr(SPR_MAS1);
473 1.2.4.2 rmind if ((mas1 & MAS1_V) == 0) {
474 1.2.4.2 rmind if (!insert) {
475 1.2.4.2 rmind wrtee(msr);
476 1.2.4.2 rmind #if 0
477 1.2.4.2 rmind printf("%s(%#lx,%#x,%#x,%x)<no update>\n",
478 1.2.4.2 rmind __func__, va, asid, pte, insert);
479 1.2.4.2 rmind #endif
480 1.2.4.2 rmind return false;
481 1.2.4.2 rmind }
482 1.2.4.2 rmind mtspr(SPR_MAS1, hwtlb.hwtlb_mas1);
483 1.2.4.2 rmind }
484 1.2.4.2 rmind mtspr(SPR_MAS2, hwtlb.hwtlb_mas2);
485 1.2.4.2 rmind mtspr(SPR_MAS3, hwtlb.hwtlb_mas3);
486 1.2.4.2 rmind __asm volatile("tlbwe");
487 1.2.4.2 rmind if (asid == 0)
488 1.2.4.2 rmind __asm volatile("isync");
489 1.2.4.2 rmind wrtee(msr);
490 1.2.4.2 rmind #if 0
491 1.2.4.2 rmind if (asid)
492 1.2.4.2 rmind printf("%s(%#lx,%#x,%#x,%x)->[%x,%x,%x]\n",
493 1.2.4.2 rmind __func__, va, asid, pte, insert,
494 1.2.4.2 rmind hwtlb.hwtlb_mas1, hwtlb.hwtlb_mas2, hwtlb.hwtlb_mas3);
495 1.2.4.2 rmind #endif
496 1.2.4.2 rmind return (mas1 & MAS1_V) != 0;
497 1.2.4.2 rmind }
498 1.2.4.2 rmind
499 1.2.4.2 rmind static void
500 1.2.4.2 rmind e500_tlb_read_entry(size_t index, struct tlbmask *tlb)
501 1.2.4.2 rmind {
502 1.2.4.2 rmind }
503 1.2.4.2 rmind
504 1.2.4.2 rmind static void
505 1.2.4.2 rmind e500_tlb_dump(void (*pr)(const char *, ...))
506 1.2.4.2 rmind {
507 1.2.4.2 rmind const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
508 1.2.4.2 rmind const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
509 1.2.4.2 rmind const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
510 1.2.4.2 rmind const uint32_t saved_mas0 = mfspr(SPR_MAS0);
511 1.2.4.2 rmind size_t valid = 0;
512 1.2.4.2 rmind
513 1.2.4.2 rmind if (pr == NULL)
514 1.2.4.2 rmind pr = printf;
515 1.2.4.2 rmind
516 1.2.4.2 rmind const register_t msr = wrtee(0);
517 1.2.4.2 rmind for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
518 1.2.4.2 rmind struct e500_hwtlb hwtlb;
519 1.2.4.2 rmind hwtlb.hwtlb_mas0 = MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0;
520 1.2.4.2 rmind mtspr(SPR_MAS0, hwtlb.hwtlb_mas0);
521 1.2.4.2 rmind for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
522 1.2.4.2 rmind mtspr(SPR_MAS2, epn);
523 1.2.4.2 rmind __asm volatile("tlbre");
524 1.2.4.2 rmind hwtlb.hwtlb_mas1 = mfspr(SPR_MAS1);
525 1.2.4.2 rmind /*
526 1.2.4.2 rmind * If this is a valid entry for AS space 1 and
527 1.2.4.2 rmind * its asid matches the constraints of the caller,
528 1.2.4.2 rmind * clear its valid bit.
529 1.2.4.2 rmind */
530 1.2.4.2 rmind if (hwtlb.hwtlb_mas1 & MAS1_V) {
531 1.2.4.2 rmind hwtlb.hwtlb_mas2 = mfspr(SPR_MAS2);
532 1.2.4.2 rmind hwtlb.hwtlb_mas3 = mfspr(SPR_MAS3);
533 1.2.4.2 rmind struct e500_tlb tlb = hwtlb_to_tlb(hwtlb);
534 1.2.4.2 rmind (*pr)("[%zu,%zu]->[%x,%x,%x]",
535 1.2.4.2 rmind assoc, atop(epn),
536 1.2.4.2 rmind hwtlb.hwtlb_mas1,
537 1.2.4.2 rmind hwtlb.hwtlb_mas2,
538 1.2.4.2 rmind hwtlb.hwtlb_mas3);
539 1.2.4.2 rmind (*pr)(": VA=%#lx size=4KB asid=%u pte=%x",
540 1.2.4.2 rmind tlb.tlb_va, tlb.tlb_asid, tlb.tlb_pte);
541 1.2.4.2 rmind (*pr)(" (RPN=%#x,%s%s%s%s%s,%s%s%s%s%s)\n",
542 1.2.4.2 rmind tlb.tlb_pte & PTE_RPN_MASK,
543 1.2.4.2 rmind tlb.tlb_pte & PTE_xR ? "R" : "",
544 1.2.4.2 rmind tlb.tlb_pte & PTE_xW ? "W" : "",
545 1.2.4.2 rmind tlb.tlb_pte & PTE_UNMODIFIED ? "*" : "",
546 1.2.4.2 rmind tlb.tlb_pte & PTE_xX ? "X" : "",
547 1.2.4.2 rmind tlb.tlb_pte & PTE_UNSYNCED ? "*" : "",
548 1.2.4.2 rmind tlb.tlb_pte & PTE_W ? "W" : "",
549 1.2.4.2 rmind tlb.tlb_pte & PTE_I ? "I" : "",
550 1.2.4.2 rmind tlb.tlb_pte & PTE_M ? "M" : "",
551 1.2.4.2 rmind tlb.tlb_pte & PTE_G ? "G" : "",
552 1.2.4.2 rmind tlb.tlb_pte & PTE_E ? "E" : "");
553 1.2.4.2 rmind valid++;
554 1.2.4.2 rmind }
555 1.2.4.2 rmind }
556 1.2.4.2 rmind }
557 1.2.4.2 rmind mtspr(SPR_MAS0, saved_mas0);
558 1.2.4.2 rmind wrtee(msr);
559 1.2.4.2 rmind (*pr)("%s: %zu valid entries\n", __func__, valid);
560 1.2.4.2 rmind }
561 1.2.4.2 rmind
562 1.2.4.2 rmind static void
563 1.2.4.2 rmind e500_tlb_walk(void *ctx, bool (*func)(void *, vaddr_t, uint32_t, uint32_t))
564 1.2.4.2 rmind {
565 1.2.4.2 rmind const size_t tlbassoc = TLBCFG_ASSOC(mftlb0cfg());
566 1.2.4.2 rmind const size_t tlbentries = TLBCFG_NENTRY(mftlb0cfg());
567 1.2.4.2 rmind const size_t max_epn = (tlbentries / tlbassoc) << PAGE_SHIFT;
568 1.2.4.2 rmind const uint32_t saved_mas0 = mfspr(SPR_MAS0);
569 1.2.4.2 rmind
570 1.2.4.2 rmind const register_t msr = wrtee(0);
571 1.2.4.2 rmind for (size_t assoc = 0; assoc < tlbassoc; assoc++) {
572 1.2.4.2 rmind struct e500_hwtlb hwtlb;
573 1.2.4.2 rmind hwtlb.hwtlb_mas0 = MAS0_ESEL_MAKE(assoc) | MAS0_TLBSEL_TLB0;
574 1.2.4.2 rmind mtspr(SPR_MAS0, hwtlb.hwtlb_mas0);
575 1.2.4.2 rmind for (size_t epn = 0; epn < max_epn; epn += PAGE_SIZE) {
576 1.2.4.2 rmind mtspr(SPR_MAS2, epn);
577 1.2.4.2 rmind __asm volatile("tlbre");
578 1.2.4.2 rmind hwtlb.hwtlb_mas1 = mfspr(SPR_MAS1);
579 1.2.4.2 rmind /*
580 1.2.4.2 rmind * If this is a valid entry for AS space 1 and
581 1.2.4.2 rmind * its asid matches the constraints of the caller,
582 1.2.4.2 rmind * clear its valid bit.
583 1.2.4.2 rmind */
584 1.2.4.2 rmind if (hwtlb.hwtlb_mas1 & MAS1_V) {
585 1.2.4.2 rmind hwtlb.hwtlb_mas2 = mfspr(SPR_MAS2);
586 1.2.4.2 rmind hwtlb.hwtlb_mas3 = mfspr(SPR_MAS3);
587 1.2.4.2 rmind struct e500_tlb tlb = hwtlb_to_tlb(hwtlb);
588 1.2.4.2 rmind if (!(*func)(ctx, tlb.tlb_va, tlb.tlb_asid,
589 1.2.4.2 rmind tlb.tlb_pte))
590 1.2.4.2 rmind break;
591 1.2.4.2 rmind }
592 1.2.4.2 rmind }
593 1.2.4.2 rmind }
594 1.2.4.2 rmind mtspr(SPR_MAS0, saved_mas0);
595 1.2.4.2 rmind wrtee(msr);
596 1.2.4.2 rmind }
597 1.2.4.2 rmind
598 1.2.4.2 rmind static struct e500_xtlb *
599 1.2.4.2 rmind e500_tlb_lookup_xtlb(vaddr_t va, u_int *slotp)
600 1.2.4.2 rmind {
601 1.2.4.2 rmind struct e500_tlb1 * const tlb1 = &e500_tlb1;
602 1.2.4.2 rmind struct e500_xtlb *xtlb = tlb1->tlb1_entries;
603 1.2.4.2 rmind
604 1.2.4.2 rmind /*
605 1.2.4.2 rmind * See if we have a TLB entry for the pa.
606 1.2.4.2 rmind */
607 1.2.4.2 rmind for (u_int i = 0; i < tlb1->tlb1_numentries; i++, xtlb++) {
608 1.2.4.2 rmind if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V)
609 1.2.4.2 rmind && xtlb->e_tlb.tlb_va <= va
610 1.2.4.2 rmind && va < xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size) {
611 1.2.4.2 rmind if (slotp != NULL)
612 1.2.4.2 rmind *slotp = i;
613 1.2.4.2 rmind return xtlb;
614 1.2.4.2 rmind }
615 1.2.4.2 rmind }
616 1.2.4.2 rmind
617 1.2.4.2 rmind return NULL;
618 1.2.4.2 rmind }
619 1.2.4.2 rmind
620 1.2.4.2 rmind static struct e500_xtlb *
621 1.2.4.2 rmind e500_tlb_lookup_xtlb2(vaddr_t va, vsize_t len)
622 1.2.4.2 rmind {
623 1.2.4.2 rmind struct e500_tlb1 * const tlb1 = &e500_tlb1;
624 1.2.4.2 rmind struct e500_xtlb *xtlb = tlb1->tlb1_entries;
625 1.2.4.2 rmind
626 1.2.4.2 rmind /*
627 1.2.4.2 rmind * See if we have a TLB entry for the pa.
628 1.2.4.2 rmind */
629 1.2.4.2 rmind for (u_int i = 0; i < tlb1->tlb1_numentries; i++, xtlb++) {
630 1.2.4.2 rmind if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V)
631 1.2.4.2 rmind && xtlb->e_tlb.tlb_va < va + len
632 1.2.4.2 rmind && va < xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size) {
633 1.2.4.2 rmind return xtlb;
634 1.2.4.2 rmind }
635 1.2.4.2 rmind }
636 1.2.4.2 rmind
637 1.2.4.2 rmind return NULL;
638 1.2.4.2 rmind }
639 1.2.4.2 rmind
640 1.2.4.2 rmind static void *
641 1.2.4.2 rmind e500_tlb_mapiodev(paddr_t pa, psize_t len)
642 1.2.4.2 rmind {
643 1.2.4.2 rmind struct e500_xtlb * const xtlb = e500_tlb_lookup_xtlb(pa, NULL);
644 1.2.4.2 rmind
645 1.2.4.2 rmind /*
646 1.2.4.2 rmind * See if we have a TLB entry for the pa. If completely falls within
647 1.2.4.2 rmind * mark the reference and return the pa.
648 1.2.4.2 rmind */
649 1.2.4.2 rmind if (xtlb && pa + len <= xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size) {
650 1.2.4.2 rmind xtlb->e_refcnt++;
651 1.2.4.2 rmind return (void *) pa;
652 1.2.4.2 rmind }
653 1.2.4.2 rmind return NULL;
654 1.2.4.2 rmind }
655 1.2.4.2 rmind
656 1.2.4.2 rmind static void
657 1.2.4.2 rmind e500_tlb_unmapiodev(vaddr_t va, vsize_t len)
658 1.2.4.2 rmind {
659 1.2.4.2 rmind if (va < VM_MIN_KERNEL_ADDRESS || VM_MAX_KERNEL_ADDRESS <= va) {
660 1.2.4.2 rmind struct e500_xtlb * const xtlb = e500_tlb_lookup_xtlb(va, NULL);
661 1.2.4.2 rmind if (xtlb)
662 1.2.4.2 rmind xtlb->e_refcnt--;
663 1.2.4.2 rmind }
664 1.2.4.2 rmind }
665 1.2.4.2 rmind
666 1.2.4.2 rmind static int
667 1.2.4.2 rmind e500_tlb_ioreserve(vaddr_t va, vsize_t len, uint32_t pte)
668 1.2.4.2 rmind {
669 1.2.4.2 rmind struct e500_tlb1 * const tlb1 = &e500_tlb1;
670 1.2.4.2 rmind struct e500_xtlb *xtlb;
671 1.2.4.2 rmind
672 1.2.4.2 rmind KASSERT(len & 0x55555000);
673 1.2.4.2 rmind KASSERT((len & ~0x55555000) == 0);
674 1.2.4.2 rmind KASSERT(len >= PAGE_SIZE);
675 1.2.4.2 rmind KASSERT((len & (len - 1)) == 0);
676 1.2.4.2 rmind KASSERT((va & (len - 1)) == 0);
677 1.2.4.2 rmind KASSERT((pte & (len - 1)) == 0);
678 1.2.4.2 rmind
679 1.2.4.2 rmind if ((xtlb = e500_tlb_lookup_xtlb2(va, len)) != NULL) {
680 1.2.4.2 rmind if (va < xtlb->e_tlb.tlb_va
681 1.2.4.2 rmind || xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size < va + len
682 1.2.4.2 rmind || va - xtlb->e_tlb.tlb_va != pte - xtlb->e_tlb.tlb_pte)
683 1.2.4.2 rmind return EBUSY;
684 1.2.4.2 rmind xtlb->e_refcnt++;
685 1.2.4.2 rmind return 0;
686 1.2.4.2 rmind }
687 1.2.4.2 rmind
688 1.2.4.2 rmind const int slot = e500_alloc_tlb1_entry();
689 1.2.4.2 rmind if (slot < 0)
690 1.2.4.2 rmind return ENOMEM;
691 1.2.4.2 rmind
692 1.2.4.2 rmind xtlb = &tlb1->tlb1_entries[slot];
693 1.2.4.2 rmind xtlb->e_tlb.tlb_va = va;
694 1.2.4.2 rmind xtlb->e_tlb.tlb_size = len;
695 1.2.4.2 rmind xtlb->e_tlb.tlb_pte = pte;
696 1.2.4.2 rmind xtlb->e_tlb.tlb_asid = KERNEL_PID;
697 1.2.4.2 rmind
698 1.2.4.2 rmind xtlb->e_hwtlb = tlb_to_hwtlb(xtlb->e_tlb);
699 1.2.4.2 rmind xtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTOUT(slot, MAS0_ESEL);
700 1.2.4.2 rmind hwtlb_write(xtlb->e_hwtlb, true);
701 1.2.4.2 rmind return 0;
702 1.2.4.2 rmind }
703 1.2.4.2 rmind
704 1.2.4.2 rmind static int
705 1.2.4.2 rmind e500_tlb_iorelease(vaddr_t va)
706 1.2.4.2 rmind {
707 1.2.4.2 rmind u_int slot;
708 1.2.4.2 rmind struct e500_xtlb * const xtlb = e500_tlb_lookup_xtlb(va, &slot);
709 1.2.4.2 rmind
710 1.2.4.2 rmind if (xtlb == NULL)
711 1.2.4.2 rmind return ENOENT;
712 1.2.4.2 rmind
713 1.2.4.2 rmind if (xtlb->e_refcnt)
714 1.2.4.2 rmind return EBUSY;
715 1.2.4.2 rmind
716 1.2.4.2 rmind e500_free_tlb1_entry(xtlb, slot, true);
717 1.2.4.2 rmind
718 1.2.4.2 rmind return 0;
719 1.2.4.2 rmind }
720 1.2.4.2 rmind
721 1.2.4.2 rmind static u_int
722 1.2.4.2 rmind e500_tlbmemmap(paddr_t memstart, psize_t memsize, struct e500_tlb1 *tlb1)
723 1.2.4.2 rmind {
724 1.2.4.2 rmind u_int slotmask = 0;
725 1.2.4.2 rmind u_int slots = 0, nextslot = 0;
726 1.2.4.2 rmind KASSERT(tlb1->tlb1_numfree > 1);
727 1.2.4.2 rmind KASSERT(((memstart + memsize - 1) & -memsize) == memstart);
728 1.2.4.2 rmind for (paddr_t lastaddr = memstart; 0 < memsize; ) {
729 1.2.4.2 rmind u_int cnt = __builtin_clz(memsize);
730 1.2.4.2 rmind psize_t size = min(1UL << (31 - (cnt | 1)), tlb1->tlb1_maxsize);
731 1.2.4.2 rmind slots += memsize / size;
732 1.2.4.2 rmind if (slots > 4)
733 1.2.4.2 rmind panic("%s: %d: can't map memory (%#lx) into TLB1: %s",
734 1.2.4.2 rmind __func__, __LINE__, memsize, "too fragmented");
735 1.2.4.2 rmind if (slots > tlb1->tlb1_numfree - 1)
736 1.2.4.2 rmind panic("%s: %d: can't map memory (%#lx) into TLB1: %s",
737 1.2.4.2 rmind __func__, __LINE__, memsize,
738 1.2.4.2 rmind "insufficent TLB entries");
739 1.2.4.2 rmind for (; nextslot < slots; nextslot++) {
740 1.2.4.2 rmind const u_int freeslot = e500_alloc_tlb1_entry();
741 1.2.4.2 rmind struct e500_xtlb * const xtlb =
742 1.2.4.2 rmind &tlb1->tlb1_entries[freeslot];
743 1.2.4.2 rmind xtlb->e_tlb.tlb_asid = KERNEL_PID;
744 1.2.4.2 rmind xtlb->e_tlb.tlb_size = size;
745 1.2.4.2 rmind xtlb->e_tlb.tlb_va = lastaddr;
746 1.2.4.2 rmind xtlb->e_tlb.tlb_pte = lastaddr
747 1.2.4.2 rmind | PTE_M | PTE_xX | PTE_xW | PTE_xR;
748 1.2.4.2 rmind lastaddr += size;
749 1.2.4.2 rmind memsize -= size;
750 1.2.4.2 rmind slotmask |= 1 << (31 - freeslot); /* clz friendly */
751 1.2.4.2 rmind }
752 1.2.4.2 rmind }
753 1.2.4.2 rmind
754 1.2.4.2 rmind return nextslot;
755 1.2.4.2 rmind }
756 1.2.4.2 rmind static const struct tlb_md_ops e500_tlb_ops = {
757 1.2.4.2 rmind .md_tlb_set_asid = e500_tlb_set_asid,
758 1.2.4.2 rmind .md_tlb_invalidate_all = e500_tlb_invalidate_all,
759 1.2.4.2 rmind .md_tlb_invalidate_globals = e500_tlb_invalidate_globals,
760 1.2.4.2 rmind .md_tlb_invalidate_asids = e500_tlb_invalidate_asids,
761 1.2.4.2 rmind .md_tlb_invalidate_addr = e500_tlb_invalidate_addr,
762 1.2.4.2 rmind .md_tlb_update_addr = e500_tlb_update_addr,
763 1.2.4.2 rmind .md_tlb_record_asids = e500_tlb_record_asids,
764 1.2.4.2 rmind .md_tlb_read_entry = e500_tlb_read_entry,
765 1.2.4.2 rmind .md_tlb_mapiodev = e500_tlb_mapiodev,
766 1.2.4.2 rmind .md_tlb_unmapiodev = e500_tlb_unmapiodev,
767 1.2.4.2 rmind .md_tlb_ioreserve = e500_tlb_ioreserve,
768 1.2.4.2 rmind .md_tlb_iorelease = e500_tlb_iorelease,
769 1.2.4.2 rmind .md_tlb_dump = e500_tlb_dump,
770 1.2.4.2 rmind .md_tlb_walk = e500_tlb_walk,
771 1.2.4.2 rmind };
772 1.2.4.2 rmind
773 1.2.4.2 rmind void
774 1.2.4.2 rmind e500_tlb_init(vaddr_t endkernel, psize_t memsize)
775 1.2.4.2 rmind {
776 1.2.4.2 rmind struct e500_tlb1 * const tlb1 = &e500_tlb1;
777 1.2.4.2 rmind
778 1.2.4.2 rmind #if 0
779 1.2.4.2 rmind register_t mmucfg = mfspr(SPR_MMUCFG);
780 1.2.4.2 rmind register_t mas4 = mfspr(SPR_MAS4);
781 1.2.4.2 rmind #endif
782 1.2.4.2 rmind
783 1.2.4.2 rmind const uint32_t tlb1cfg = mftlb1cfg();
784 1.2.4.2 rmind tlb1->tlb1_numentries = TLBCFG_NENTRY(tlb1cfg);
785 1.2.4.2 rmind KASSERT(tlb1->tlb1_numentries <= __arraycount(tlb1->tlb1_entries));
786 1.2.4.2 rmind /*
787 1.2.4.2 rmind * Limit maxsize to 1G since 4G isn't really useful to us.
788 1.2.4.2 rmind */
789 1.2.4.2 rmind tlb1->tlb1_minsize = 1024 << (2 * TLBCFG_MINSIZE(tlb1cfg));
790 1.2.4.2 rmind tlb1->tlb1_maxsize = 1024 << (2 * min(10, TLBCFG_MAXSIZE(tlb1cfg)));
791 1.2.4.2 rmind
792 1.2.4.2 rmind #ifdef VERBOSE_INITPPC
793 1.2.4.2 rmind printf(" tlb1cfg=%#x numentries=%u minsize=%#xKB maxsize=%#xKB",
794 1.2.4.2 rmind tlb1cfg, tlb1->tlb1_numentries, tlb1->tlb1_minsize >> 10,
795 1.2.4.2 rmind tlb1->tlb1_maxsize >> 10);
796 1.2.4.2 rmind #endif
797 1.2.4.2 rmind
798 1.2.4.2 rmind /*
799 1.2.4.2 rmind * Let's see what's in TLB1 and we need to invalidate any entry that
800 1.2.4.2 rmind * would fit within the kernel's mapped address space.
801 1.2.4.2 rmind */
802 1.2.4.2 rmind psize_t memmapped = 0;
803 1.2.4.2 rmind for (u_int i = 0; i < tlb1->tlb1_numentries; i++) {
804 1.2.4.2 rmind struct e500_xtlb * const xtlb = &tlb1->tlb1_entries[i];
805 1.2.4.2 rmind
806 1.2.4.2 rmind xtlb->e_hwtlb = hwtlb_read(MAS0_TLBSEL_TLB1, i);
807 1.2.4.2 rmind
808 1.2.4.2 rmind if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_V) == 0) {
809 1.2.4.2 rmind tlb1->tlb1_freelist[tlb1->tlb1_numfree++] = i;
810 1.2.4.2 rmind #ifdef VERBOSE_INITPPC
811 1.2.4.2 rmind printf(" TLB1[%u]=<unused>", i);
812 1.2.4.2 rmind #endif
813 1.2.4.2 rmind continue;
814 1.2.4.2 rmind }
815 1.2.4.2 rmind
816 1.2.4.2 rmind xtlb->e_tlb = hwtlb_to_tlb(xtlb->e_hwtlb);
817 1.2.4.2 rmind #ifdef VERBOSE_INITPPC
818 1.2.4.2 rmind printf(" TLB1[%u]=<%#lx,%#lx,%#x,%#x>",
819 1.2.4.2 rmind i, xtlb->e_tlb.tlb_va, xtlb->e_tlb.tlb_size,
820 1.2.4.2 rmind xtlb->e_tlb.tlb_asid, xtlb->e_tlb.tlb_pte);
821 1.2.4.2 rmind #endif
822 1.2.4.2 rmind if ((VM_MIN_KERNEL_ADDRESS <= xtlb->e_tlb.tlb_va
823 1.2.4.2 rmind && xtlb->e_tlb.tlb_va < VM_MAX_KERNEL_ADDRESS)
824 1.2.4.2 rmind || (xtlb->e_tlb.tlb_va < VM_MIN_KERNEL_ADDRESS
825 1.2.4.2 rmind && VM_MIN_KERNEL_ADDRESS <
826 1.2.4.2 rmind xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size)) {
827 1.2.4.2 rmind #ifdef VERBOSE_INITPPC
828 1.2.4.2 rmind printf("free");
829 1.2.4.2 rmind #endif
830 1.2.4.2 rmind e500_free_tlb1_entry(xtlb, i, false);
831 1.2.4.2 rmind #ifdef VERBOSE_INITPPC
832 1.2.4.2 rmind printf("d");
833 1.2.4.2 rmind #endif
834 1.2.4.2 rmind continue;
835 1.2.4.2 rmind }
836 1.2.4.2 rmind if ((xtlb->e_hwtlb.hwtlb_mas1 & MAS1_IPROT) == 0) {
837 1.2.4.2 rmind xtlb->e_hwtlb.hwtlb_mas1 |= MAS1_IPROT;
838 1.2.4.2 rmind hwtlb_write(xtlb->e_hwtlb, false);
839 1.2.4.2 rmind #ifdef VERBOSE_INITPPC
840 1.2.4.2 rmind printf("+iprot");
841 1.2.4.2 rmind #endif
842 1.2.4.2 rmind }
843 1.2.4.2 rmind if (xtlb->e_tlb.tlb_pte & PTE_I)
844 1.2.4.2 rmind continue;
845 1.2.4.2 rmind
846 1.2.4.2 rmind if (xtlb->e_tlb.tlb_va == 0
847 1.2.4.2 rmind || xtlb->e_tlb.tlb_va + xtlb->e_tlb.tlb_size <= memsize) {
848 1.2.4.2 rmind memmapped += xtlb->e_tlb.tlb_size;
849 1.2.4.2 rmind }
850 1.2.4.2 rmind }
851 1.2.4.2 rmind
852 1.2.4.2 rmind cpu_md_ops.md_tlb_ops = &e500_tlb_ops;
853 1.2.4.2 rmind
854 1.2.4.2 rmind if (__predict_false(memmapped < memsize)) {
855 1.2.4.2 rmind /*
856 1.2.4.2 rmind * Let's see how many TLB entries are needed to map memory.
857 1.2.4.2 rmind */
858 1.2.4.2 rmind u_int slotmask = e500_tlbmemmap(0, memsize, tlb1);
859 1.2.4.2 rmind
860 1.2.4.2 rmind /*
861 1.2.4.2 rmind * To map main memory into the TLB, we need to flush any
862 1.2.4.2 rmind * existing entries from the TLB that overlap the virtual
863 1.2.4.2 rmind * address space needed to map physical memory. That may
864 1.2.4.2 rmind * include the entries for the pages currently used by the
865 1.2.4.2 rmind * stack or that we are executing. So to avoid problems, we
866 1.2.4.2 rmind * are going to temporarily map the kernel and stack into AS 1,
867 1.2.4.2 rmind * switch to it, and clear out the TLB entries from AS 0,
868 1.2.4.2 rmind * install the new TLB entries to map memory, and then switch
869 1.2.4.2 rmind * back to AS 0 and free the temp entry used for AS1.
870 1.2.4.2 rmind */
871 1.2.4.2 rmind u_int b = __builtin_clz(endkernel);
872 1.2.4.2 rmind
873 1.2.4.2 rmind /*
874 1.2.4.2 rmind * If the kernel doesn't end on a clean power of 2, we need
875 1.2.4.2 rmind * to round the size up (by decrementing the number of leading
876 1.2.4.2 rmind * zero bits). If the size isn't a power of 4KB, decrement
877 1.2.4.2 rmind * again to make it one.
878 1.2.4.2 rmind */
879 1.2.4.2 rmind if (endkernel & (endkernel - 1))
880 1.2.4.2 rmind b--;
881 1.2.4.2 rmind if ((b & 1) == 0)
882 1.2.4.2 rmind b--;
883 1.2.4.2 rmind
884 1.2.4.2 rmind /*
885 1.2.4.2 rmind * Create a TLB1 mapping for the kernel in AS1.
886 1.2.4.2 rmind */
887 1.2.4.2 rmind const u_int kslot = e500_alloc_tlb1_entry();
888 1.2.4.2 rmind struct e500_xtlb * const kxtlb = &tlb1->tlb1_entries[kslot];
889 1.2.4.2 rmind kxtlb->e_tlb.tlb_va = 0;
890 1.2.4.2 rmind kxtlb->e_tlb.tlb_size = 1UL << (31 - b);
891 1.2.4.2 rmind kxtlb->e_tlb.tlb_pte = PTE_M|PTE_xR|PTE_xW|PTE_xX;
892 1.2.4.2 rmind kxtlb->e_tlb.tlb_asid = KERNEL_PID;
893 1.2.4.2 rmind
894 1.2.4.2 rmind kxtlb->e_hwtlb = tlb_to_hwtlb(kxtlb->e_tlb);
895 1.2.4.2 rmind kxtlb->e_hwtlb.hwtlb_mas0 |= __SHIFTOUT(kslot, MAS0_ESEL);
896 1.2.4.2 rmind kxtlb->e_hwtlb.hwtlb_mas1 |= MAS1_TS;
897 1.2.4.2 rmind hwtlb_write(kxtlb->e_hwtlb, true);
898 1.2.4.2 rmind
899 1.2.4.2 rmind /*
900 1.2.4.2 rmind * Now that we have a TLB mapping in AS1 for the kernel and its
901 1.2.4.2 rmind * stack, we switch to AS1 to cleanup the TLB mappings for TLB0.
902 1.2.4.2 rmind */
903 1.2.4.2 rmind const register_t saved_msr = mfmsr();
904 1.2.4.2 rmind mtmsr(saved_msr | PSL_DS | PSL_IS);
905 1.2.4.2 rmind __asm volatile("isync");
906 1.2.4.2 rmind
907 1.2.4.2 rmind /*
908 1.2.4.2 rmind *** Invalidate all the TLB0 entries.
909 1.2.4.2 rmind */
910 1.2.4.2 rmind e500_tlb_invalidate_all();
911 1.2.4.2 rmind
912 1.2.4.2 rmind /*
913 1.2.4.2 rmind *** Now let's see if we have any entries in TLB1 that would
914 1.2.4.2 rmind *** overlap the ones we are about to install. If so, nuke 'em.
915 1.2.4.2 rmind */
916 1.2.4.2 rmind for (u_int i = 0; i < tlb1->tlb1_numentries; i++) {
917 1.2.4.2 rmind struct e500_xtlb * const xtlb = &tlb1->tlb1_entries[i];
918 1.2.4.2 rmind struct e500_hwtlb * const hwtlb = &xtlb->e_hwtlb;
919 1.2.4.2 rmind if ((hwtlb->hwtlb_mas1 & (MAS1_V|MAS1_TS)) == MAS1_V
920 1.2.4.2 rmind && (hwtlb->hwtlb_mas2 & MAS2_EPN) < memsize) {
921 1.2.4.2 rmind e500_free_tlb1_entry(xtlb, i, false);
922 1.2.4.2 rmind }
923 1.2.4.2 rmind }
924 1.2.4.2 rmind
925 1.2.4.2 rmind /*
926 1.2.4.2 rmind *** Now we can add the TLB entries that will map physical
927 1.2.4.2 rmind *** memory. If bit 0 [MSB] in slotmask is set, then tlb
928 1.2.4.2 rmind *** entry 0 contains a mapping for physical memory...
929 1.2.4.2 rmind */
930 1.2.4.2 rmind struct e500_xtlb *entries = tlb1->tlb1_entries;
931 1.2.4.2 rmind while (slotmask != 0) {
932 1.2.4.2 rmind const u_int slot = __builtin_clz(slotmask);
933 1.2.4.2 rmind hwtlb_write(entries[slot].e_hwtlb, false);
934 1.2.4.2 rmind entries += slot + 1;
935 1.2.4.2 rmind slotmask <<= slot + 1;
936 1.2.4.2 rmind }
937 1.2.4.2 rmind
938 1.2.4.2 rmind /*
939 1.2.4.2 rmind *** Synchronize the TLB and the instruction stream.
940 1.2.4.2 rmind */
941 1.2.4.2 rmind __asm volatile("tlbsync");
942 1.2.4.2 rmind __asm volatile("isync");
943 1.2.4.2 rmind
944 1.2.4.2 rmind /*
945 1.2.4.2 rmind *** Switch back to AS 0.
946 1.2.4.2 rmind */
947 1.2.4.2 rmind mtmsr(saved_msr);
948 1.2.4.2 rmind __asm volatile("isync");
949 1.2.4.2 rmind
950 1.2.4.2 rmind /*
951 1.2.4.2 rmind * Free the temporary TLB1 entry.
952 1.2.4.2 rmind */
953 1.2.4.2 rmind e500_free_tlb1_entry(kxtlb, kslot, true);
954 1.2.4.2 rmind }
955 1.2.4.2 rmind
956 1.2.4.2 rmind /*
957 1.2.4.2 rmind * Finally set the MAS4 defaults.
958 1.2.4.2 rmind */
959 1.2.4.2 rmind mtspr(SPR_MAS4, MAS4_TSIZED_4KB | MAS4_MD);
960 1.2.4.2 rmind
961 1.2.4.2 rmind /*
962 1.2.4.2 rmind * Invalidate all the TLB0 entries.
963 1.2.4.2 rmind */
964 1.2.4.2 rmind e500_tlb_invalidate_all();
965 1.2.4.2 rmind }
966