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