blob: cc0432f158171f541602bbb5333a7e349ddef2c7 [file] [log] [blame]
Sam Ravnborga00736e2008-06-19 20:26:19 +02001#ifndef _SPARC64_TSB_H
2#define _SPARC64_TSB_H
3
4/* The sparc64 TSB is similar to the powerpc hashtables. It's a
5 * power-of-2 sized table of TAG/PTE pairs. The cpu precomputes
6 * pointers into this table for 8K and 64K page sizes, and also a
7 * comparison TAG based upon the virtual address and context which
8 * faults.
9 *
10 * TLB miss trap handler software does the actual lookup via something
11 * of the form:
12 *
13 * ldxa [%g0] ASI_{D,I}MMU_TSB_8KB_PTR, %g1
14 * ldxa [%g0] ASI_{D,I}MMU, %g6
15 * sllx %g6, 22, %g6
16 * srlx %g6, 22, %g6
17 * ldda [%g1] ASI_NUCLEUS_QUAD_LDD, %g4
18 * cmp %g4, %g6
19 * bne,pn %xcc, tsb_miss_{d,i}tlb
20 * mov FAULT_CODE_{D,I}TLB, %g3
21 * stxa %g5, [%g0] ASI_{D,I}TLB_DATA_IN
22 * retry
23 *
24 *
25 * Each 16-byte slot of the TSB is the 8-byte tag and then the 8-byte
26 * PTE. The TAG is of the same layout as the TLB TAG TARGET mmu
27 * register which is:
28 *
29 * -------------------------------------------------
30 * | - | CONTEXT | - | VADDR bits 63:22 |
31 * -------------------------------------------------
32 * 63 61 60 48 47 42 41 0
33 *
34 * But actually, since we use per-mm TSB's, we zero out the CONTEXT
35 * field.
36 *
37 * Like the powerpc hashtables we need to use locking in order to
38 * synchronize while we update the entries. PTE updates need locking
39 * as well.
40 *
41 * We need to carefully choose a lock bits for the TSB entry. We
42 * choose to use bit 47 in the tag. Also, since we never map anything
43 * at page zero in context zero, we use zero as an invalid tag entry.
44 * When the lock bit is set, this forces a tag comparison failure.
45 */
46
47#define TSB_TAG_LOCK_BIT 47
48#define TSB_TAG_LOCK_HIGH (1 << (TSB_TAG_LOCK_BIT - 32))
49
50#define TSB_TAG_INVALID_BIT 46
51#define TSB_TAG_INVALID_HIGH (1 << (TSB_TAG_INVALID_BIT - 32))
52
Sam Ravnborga00736e2008-06-19 20:26:19 +020053/* Some cpus support physical address quad loads. We want to use
54 * those if possible so we don't need to hard-lock the TSB mapping
55 * into the TLB. We encode some instruction patching in order to
56 * support this.
57 *
58 * The kernel TSB is locked into the TLB by virtue of being in the
59 * kernel image, so we don't play these games for swapper_tsb access.
60 */
61#ifndef __ASSEMBLY__
62struct tsb_ldquad_phys_patch_entry {
63 unsigned int addr;
64 unsigned int sun4u_insn;
65 unsigned int sun4v_insn;
66};
67extern struct tsb_ldquad_phys_patch_entry __tsb_ldquad_phys_patch,
68 __tsb_ldquad_phys_patch_end;
69
70struct tsb_phys_patch_entry {
71 unsigned int addr;
72 unsigned int insn;
73};
74extern struct tsb_phys_patch_entry __tsb_phys_patch, __tsb_phys_patch_end;
75#endif
76#define TSB_LOAD_QUAD(TSB, REG) \
77661: ldda [TSB] ASI_NUCLEUS_QUAD_LDD, REG; \
78 .section .tsb_ldquad_phys_patch, "ax"; \
79 .word 661b; \
80 ldda [TSB] ASI_QUAD_LDD_PHYS, REG; \
81 ldda [TSB] ASI_QUAD_LDD_PHYS_4V, REG; \
82 .previous
83
84#define TSB_LOAD_TAG_HIGH(TSB, REG) \
85661: lduwa [TSB] ASI_N, REG; \
86 .section .tsb_phys_patch, "ax"; \
87 .word 661b; \
88 lduwa [TSB] ASI_PHYS_USE_EC, REG; \
89 .previous
90
91#define TSB_LOAD_TAG(TSB, REG) \
92661: ldxa [TSB] ASI_N, REG; \
93 .section .tsb_phys_patch, "ax"; \
94 .word 661b; \
95 ldxa [TSB] ASI_PHYS_USE_EC, REG; \
96 .previous
97
98#define TSB_CAS_TAG_HIGH(TSB, REG1, REG2) \
99661: casa [TSB] ASI_N, REG1, REG2; \
100 .section .tsb_phys_patch, "ax"; \
101 .word 661b; \
102 casa [TSB] ASI_PHYS_USE_EC, REG1, REG2; \
103 .previous
104
105#define TSB_CAS_TAG(TSB, REG1, REG2) \
106661: casxa [TSB] ASI_N, REG1, REG2; \
107 .section .tsb_phys_patch, "ax"; \
108 .word 661b; \
109 casxa [TSB] ASI_PHYS_USE_EC, REG1, REG2; \
110 .previous
111
112#define TSB_STORE(ADDR, VAL) \
113661: stxa VAL, [ADDR] ASI_N; \
114 .section .tsb_phys_patch, "ax"; \
115 .word 661b; \
116 stxa VAL, [ADDR] ASI_PHYS_USE_EC; \
117 .previous
118
119#define TSB_LOCK_TAG(TSB, REG1, REG2) \
12099: TSB_LOAD_TAG_HIGH(TSB, REG1); \
121 sethi %hi(TSB_TAG_LOCK_HIGH), REG2;\
122 andcc REG1, REG2, %g0; \
123 bne,pn %icc, 99b; \
124 nop; \
125 TSB_CAS_TAG_HIGH(TSB, REG1, REG2); \
126 cmp REG1, REG2; \
127 bne,pn %icc, 99b; \
128 nop; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200129
130#define TSB_WRITE(TSB, TTE, TAG) \
131 add TSB, 0x8, TSB; \
132 TSB_STORE(TSB, TTE); \
133 sub TSB, 0x8, TSB; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200134 TSB_STORE(TSB, TAG);
135
Sam Ravnborga00736e2008-06-19 20:26:19 +0200136 /* Do a kernel page table walk. Leaves physical PTE pointer in
137 * REG1. Jumps to FAIL_LABEL on early page table walk termination.
138 * VADDR will not be clobbered, but REG2 will.
139 */
140#define KERN_PGTABLE_WALK(VADDR, REG1, REG2, FAIL_LABEL) \
141 sethi %hi(swapper_pg_dir), REG1; \
142 or REG1, %lo(swapper_pg_dir), REG1; \
143 sllx VADDR, 64 - (PGDIR_SHIFT + PGDIR_BITS), REG2; \
144 srlx REG2, 64 - PAGE_SHIFT, REG2; \
David S. Miller2b779332013-09-25 14:33:16 -0700145 andn REG2, 0x7, REG2; \
146 ldx [REG1 + REG2], REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200147 brz,pn REG1, FAIL_LABEL; \
148 sllx VADDR, 64 - (PMD_SHIFT + PMD_BITS), REG2; \
149 srlx REG2, 64 - PAGE_SHIFT, REG2; \
David Millerdbc9fdf02012-10-08 16:34:23 -0700150 sllx REG1, PGD_PADDR_SHIFT, REG1; \
David S. Miller2b779332013-09-25 14:33:16 -0700151 andn REG2, 0x7, REG2; \
152 ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200153 brz,pn REG1, FAIL_LABEL; \
154 sllx VADDR, 64 - PMD_SHIFT, REG2; \
David S. Miller37b3a8f2013-09-25 13:48:49 -0700155 srlx REG2, 64 - PAGE_SHIFT, REG2; \
David Millerdbc9fdf02012-10-08 16:34:23 -0700156 sllx REG1, PMD_PADDR_SHIFT, REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200157 andn REG2, 0x7, REG2; \
158 add REG1, REG2, REG1;
159
David S. Miller76968ad2013-02-20 12:38:40 -0800160 /* These macros exists only to make the PMD translator below
161 * easier to read. It hides the ELF section switch for the
162 * sun4v code patching.
David Miller9e695d22012-10-08 16:34:29 -0700163 */
David S. Miller76968ad2013-02-20 12:38:40 -0800164#define OR_PTE_BIT_1INSN(REG, NAME) \
David Miller9e695d22012-10-08 16:34:29 -0700165661: or REG, _PAGE_##NAME##_4U, REG; \
166 .section .sun4v_1insn_patch, "ax"; \
167 .word 661b; \
168 or REG, _PAGE_##NAME##_4V, REG; \
169 .previous;
170
David S. Miller76968ad2013-02-20 12:38:40 -0800171#define OR_PTE_BIT_2INSN(REG, TMP, NAME) \
172661: sethi %hi(_PAGE_##NAME##_4U), TMP; \
173 or REG, TMP, REG; \
174 .section .sun4v_2insn_patch, "ax"; \
175 .word 661b; \
176 mov -1, TMP; \
177 or REG, _PAGE_##NAME##_4V, REG; \
178 .previous;
179
David S. Miller37b3a8f2013-09-25 13:48:49 -0700180 /* Load into REG the PTE value for VALID, CACHE, and SZHUGE.
181 *
182 * We are fabricating an 8MB page using 2 4MB HW pages here.
183 */
184#define BUILD_PTE_VALID_SZHUGE_CACHE(VADDR, PADDR_BITS, REG) \
185 sethi %hi(4 * 1024 * 1024), REG; \
186 andn PADDR_BITS, REG, PADDR_BITS; \
187 and VADDR, REG, REG; \
188 or PADDR_BITS, REG, PADDR_BITS; \
David Miller9e695d22012-10-08 16:34:29 -0700189661: sethi %uhi(_PAGE_VALID|_PAGE_SZHUGE_4U), REG; \
190 .section .sun4v_1insn_patch, "ax"; \
191 .word 661b; \
192 sethi %uhi(_PAGE_VALID), REG; \
193 .previous; \
194 sllx REG, 32, REG; \
195661: or REG, _PAGE_CP_4U|_PAGE_CV_4U, REG; \
196 .section .sun4v_1insn_patch, "ax"; \
197 .word 661b; \
198 or REG, _PAGE_CP_4V|_PAGE_CV_4V|_PAGE_SZHUGE_4V, REG; \
199 .previous;
200
201 /* PMD has been loaded into REG1, interpret the value, seeing
202 * if it is a HUGE PMD or a normal one. If it is not valid
203 * then jump to FAIL_LABEL. If it is a HUGE PMD, and it
204 * translates to a valid PTE, branch to PTE_LABEL.
205 *
206 * We translate the PMD by hand, one bit at a time,
207 * constructing the huge PTE.
208 *
209 * So we construct the PTE in REG2 as follows:
210 *
211 * 1) Extract the PMD PFN from REG1 and place it into REG2.
212 *
213 * 2) Translate PMD protection bits in REG1 into REG2, one bit
214 * at a time using andcc tests on REG1 and OR's into REG2.
215 *
216 * Only two bits to be concerned with here, EXEC and WRITE.
217 * Now REG1 is freed up and we can use it as a temporary.
218 *
219 * 3) Construct the VALID, CACHE, and page size PTE bits in
220 * REG1, OR with REG2 to form final PTE.
221 */
222#ifdef CONFIG_TRANSPARENT_HUGEPAGE
223#define USER_PGTABLE_CHECK_PMD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, PTE_LABEL) \
224 brz,pn REG1, FAIL_LABEL; \
225 andcc REG1, PMD_ISHUGE, %g0; \
226 be,pt %xcc, 700f; \
227 and REG1, PMD_HUGE_PRESENT|PMD_HUGE_ACCESSED, REG2; \
228 cmp REG2, PMD_HUGE_PRESENT|PMD_HUGE_ACCESSED; \
229 bne,pn %xcc, FAIL_LABEL; \
230 andn REG1, PMD_HUGE_PROTBITS, REG2; \
231 sllx REG2, PMD_PADDR_SHIFT, REG2; \
232 /* REG2 now holds PFN << PAGE_SHIFT */ \
David S. Miller76968ad2013-02-20 12:38:40 -0800233 andcc REG1, PMD_HUGE_WRITE, %g0; \
David Miller9e695d22012-10-08 16:34:29 -0700234 bne,a,pt %xcc, 1f; \
David S. Miller76968ad2013-02-20 12:38:40 -0800235 OR_PTE_BIT_1INSN(REG2, W); \
2361: andcc REG1, PMD_HUGE_EXEC, %g0; \
237 be,pt %xcc, 1f; \
238 nop; \
239 OR_PTE_BIT_2INSN(REG2, REG1, EXEC); \
David Miller9e695d22012-10-08 16:34:29 -0700240 /* REG1 can now be clobbered, build final PTE */ \
David S. Miller37b3a8f2013-09-25 13:48:49 -07002411: BUILD_PTE_VALID_SZHUGE_CACHE(VADDR, REG2, REG1); \
David Miller9e695d22012-10-08 16:34:29 -0700242 ba,pt %xcc, PTE_LABEL; \
243 or REG1, REG2, REG1; \
244700:
245#else
246#define USER_PGTABLE_CHECK_PMD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, PTE_LABEL) \
247 brz,pn REG1, FAIL_LABEL; \
248 nop;
249#endif
250
251 /* Do a user page table walk in MMU globals. Leaves final,
252 * valid, PTE value in REG1. Jumps to FAIL_LABEL on early
253 * page table walk termination or if the PTE is not valid.
254 *
255 * Physical base of page tables is in PHYS_PGD which will not
256 * be modified.
Sam Ravnborga00736e2008-06-19 20:26:19 +0200257 *
258 * VADDR will not be clobbered, but REG1 and REG2 will.
259 */
260#define USER_PGTABLE_WALK_TL1(VADDR, PHYS_PGD, REG1, REG2, FAIL_LABEL) \
261 sllx VADDR, 64 - (PGDIR_SHIFT + PGDIR_BITS), REG2; \
262 srlx REG2, 64 - PAGE_SHIFT, REG2; \
David S. Miller2b779332013-09-25 14:33:16 -0700263 andn REG2, 0x7, REG2; \
264 ldxa [PHYS_PGD + REG2] ASI_PHYS_USE_EC, REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200265 brz,pn REG1, FAIL_LABEL; \
266 sllx VADDR, 64 - (PMD_SHIFT + PMD_BITS), REG2; \
267 srlx REG2, 64 - PAGE_SHIFT, REG2; \
David Millerdbc9fdf02012-10-08 16:34:23 -0700268 sllx REG1, PGD_PADDR_SHIFT, REG1; \
David S. Miller2b779332013-09-25 14:33:16 -0700269 andn REG2, 0x7, REG2; \
270 ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
David Miller9e695d22012-10-08 16:34:29 -0700271 USER_PGTABLE_CHECK_PMD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, 800f) \
272 sllx VADDR, 64 - PMD_SHIFT, REG2; \
David S. Miller37b3a8f2013-09-25 13:48:49 -0700273 srlx REG2, 64 - PAGE_SHIFT, REG2; \
David Millerdbc9fdf02012-10-08 16:34:23 -0700274 sllx REG1, PMD_PADDR_SHIFT, REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200275 andn REG2, 0x7, REG2; \
David Miller9e695d22012-10-08 16:34:29 -0700276 add REG1, REG2, REG1; \
277 ldxa [REG1] ASI_PHYS_USE_EC, REG1; \
278 brgez,pn REG1, FAIL_LABEL; \
279 nop; \
280800:
Sam Ravnborga00736e2008-06-19 20:26:19 +0200281
282/* Lookup a OBP mapping on VADDR in the prom_trans[] table at TL>0.
283 * If no entry is found, FAIL_LABEL will be branched to. On success
284 * the resulting PTE value will be left in REG1. VADDR is preserved
285 * by this routine.
286 */
287#define OBP_TRANS_LOOKUP(VADDR, REG1, REG2, REG3, FAIL_LABEL) \
288 sethi %hi(prom_trans), REG1; \
289 or REG1, %lo(prom_trans), REG1; \
29097: ldx [REG1 + 0x00], REG2; \
291 brz,pn REG2, FAIL_LABEL; \
292 nop; \
293 ldx [REG1 + 0x08], REG3; \
294 add REG2, REG3, REG3; \
295 cmp REG2, VADDR; \
296 bgu,pt %xcc, 98f; \
297 cmp VADDR, REG3; \
298 bgeu,pt %xcc, 98f; \
299 ldx [REG1 + 0x10], REG3; \
300 sub VADDR, REG2, REG2; \
301 ba,pt %xcc, 99f; \
302 add REG3, REG2, REG1; \
30398: ba,pt %xcc, 97b; \
304 add REG1, (3 * 8), REG1; \
30599:
306
307 /* We use a 32K TSB for the whole kernel, this allows to
308 * handle about 16MB of modules and vmalloc mappings without
309 * incurring many hash conflicts.
310 */
311#define KERNEL_TSB_SIZE_BYTES (32 * 1024)
312#define KERNEL_TSB_NENTRIES \
313 (KERNEL_TSB_SIZE_BYTES / 16)
314#define KERNEL_TSB4M_NENTRIES 4096
315
David S. Miller9076d0e2011-08-05 00:53:57 -0700316#define KTSB_PHYS_SHIFT 15
317
Sam Ravnborga00736e2008-06-19 20:26:19 +0200318 /* Do a kernel TSB lookup at tl>0 on VADDR+TAG, branch to OK_LABEL
319 * on TSB hit. REG1, REG2, REG3, and REG4 are used as temporaries
320 * and the found TTE will be left in REG1. REG3 and REG4 must
321 * be an even/odd pair of registers.
322 *
323 * VADDR and TAG will be preserved and not clobbered by this macro.
324 */
325#define KERN_TSB_LOOKUP_TL1(VADDR, TAG, REG1, REG2, REG3, REG4, OK_LABEL) \
David S. Miller9076d0e2011-08-05 00:53:57 -0700326661: sethi %hi(swapper_tsb), REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200327 or REG1, %lo(swapper_tsb), REG1; \
David S. Miller9076d0e2011-08-05 00:53:57 -0700328 .section .swapper_tsb_phys_patch, "ax"; \
329 .word 661b; \
330 .previous; \
331661: nop; \
332 .section .tsb_ldquad_phys_patch, "ax"; \
333 .word 661b; \
334 sllx REG1, KTSB_PHYS_SHIFT, REG1; \
335 sllx REG1, KTSB_PHYS_SHIFT, REG1; \
336 .previous; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200337 srlx VADDR, PAGE_SHIFT, REG2; \
338 and REG2, (KERNEL_TSB_NENTRIES - 1), REG2; \
339 sllx REG2, 4, REG2; \
340 add REG1, REG2, REG2; \
David S. Miller9076d0e2011-08-05 00:53:57 -0700341 TSB_LOAD_QUAD(REG2, REG3); \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200342 cmp REG3, TAG; \
343 be,a,pt %xcc, OK_LABEL; \
344 mov REG4, REG1;
345
346#ifndef CONFIG_DEBUG_PAGEALLOC
347 /* This version uses a trick, the TAG is already (VADDR >> 22) so
348 * we can make use of that for the index computation.
349 */
350#define KERN_TSB4M_LOOKUP_TL1(TAG, REG1, REG2, REG3, REG4, OK_LABEL) \
David S. Miller9076d0e2011-08-05 00:53:57 -0700351661: sethi %hi(swapper_4m_tsb), REG1; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200352 or REG1, %lo(swapper_4m_tsb), REG1; \
David S. Miller9076d0e2011-08-05 00:53:57 -0700353 .section .swapper_4m_tsb_phys_patch, "ax"; \
354 .word 661b; \
355 .previous; \
356661: nop; \
357 .section .tsb_ldquad_phys_patch, "ax"; \
358 .word 661b; \
359 sllx REG1, KTSB_PHYS_SHIFT, REG1; \
360 sllx REG1, KTSB_PHYS_SHIFT, REG1; \
361 .previous; \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200362 and TAG, (KERNEL_TSB4M_NENTRIES - 1), REG2; \
363 sllx REG2, 4, REG2; \
364 add REG1, REG2, REG2; \
David S. Miller9076d0e2011-08-05 00:53:57 -0700365 TSB_LOAD_QUAD(REG2, REG3); \
Sam Ravnborga00736e2008-06-19 20:26:19 +0200366 cmp REG3, TAG; \
367 be,a,pt %xcc, OK_LABEL; \
368 mov REG4, REG1;
369#endif
370
371#endif /* !(_SPARC64_TSB_H) */