1 /* tsb.S: Sparc64 TSB table handling.
3 * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
8 #include <asm/hypervisor.h>
10 #include <asm/cpudata.h>
16 /* Invoked from TLB miss handler, we are in the
17 * MMU global registers and they are setup like
20 * %g1: TSB entry pointer
21 * %g2: available temporary
22 * %g3: FAULT_CODE_{D,I}TLB
23 * %g4: available temporary
24 * %g5: available temporary
26 * %g7: available temporary, will be loaded by us with
27 * the physical address base of the linux page
28 * tables for the current address space
31 mov TLB_TAG_ACCESS, %g4
32 ldxa [%g4] ASI_DMMU, %g4
33 srlx %g4, PAGE_SHIFT, %g4
34 ba,pt %xcc, tsb_miss_page_table_walk
35 sllx %g4, PAGE_SHIFT, %g4
38 mov TLB_TAG_ACCESS, %g4
39 ldxa [%g4] ASI_IMMU, %g4
40 srlx %g4, PAGE_SHIFT, %g4
41 ba,pt %xcc, tsb_miss_page_table_walk
42 sllx %g4, PAGE_SHIFT, %g4
44 /* At this point we have:
45 * %g1 -- PAGE_SIZE TSB entry address
46 * %g3 -- FAULT_CODE_{D,I}TLB
47 * %g4 -- missing virtual address
48 * %g6 -- TAG TARGET (vaddr >> 22)
50 tsb_miss_page_table_walk:
51 TRAP_LOAD_TRAP_BLOCK(%g7, %g5)
53 /* Before committing to a full page table walk,
54 * check the huge page TSB.
56 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
58 661: ldx [%g7 + TRAP_PER_CPU_TSB_HUGE], %g5
60 .section .sun4v_2insn_patch, "ax"
62 mov SCRATCHPAD_UTSBREG2, %g5
63 ldxa [%g5] ASI_SCRATCHPAD, %g5
70 /* We need an aligned pair of registers containing 2 values
71 * which can be easily rematerialized. %g6 and %g7 foot the
72 * bill just nicely. We'll save %g6 away into %g2 for the
73 * huge page TSB TAG comparison.
75 * Perform a huge page TSB lookup.
82 srlx %g4, REAL_HPAGE_SHIFT, %g6
88 TSB_LOAD_QUAD(%g5, %g6)
90 be,a,pt %xcc, tsb_tlb_reload
93 /* No match, remember the huge page TSB entry address,
94 * and restore %g6 and %g7.
96 TRAP_LOAD_TRAP_BLOCK(%g7, %g6)
98 80: stx %g5, [%g7 + TRAP_PER_CPU_TSB_HUGE_TEMP]
102 ldx [%g7 + TRAP_PER_CPU_PGD_PADDR], %g7
104 /* At this point we have:
105 * %g1 -- TSB entry address
106 * %g3 -- FAULT_CODE_{D,I}TLB
107 * %g4 -- missing virtual address
108 * %g6 -- TAG TARGET (vaddr >> 22)
109 * %g7 -- page table physical address
111 * We know that both the base PAGE_SIZE TSB and the HPAGE_SIZE
112 * TSB both lack a matching entry.
114 tsb_miss_page_table_walk_sun4v_fastpath:
115 USER_PGTABLE_WALK_TL1(%g4, %g7, %g5, %g2, tsb_do_fault)
117 /* Valid PTE is now in %g5. */
119 #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
120 661: sethi %uhi(_PAGE_SZALL_4U), %g7
122 .section .sun4v_2insn_patch, "ax"
124 mov _PAGE_SZALL_4V, %g7
130 661: sethi %uhi(_PAGE_SZHUGE_4U), %g7
132 .section .sun4v_2insn_patch, "ax"
134 mov _PAGE_SZHUGE_4V, %g7
142 /* It is a huge page, use huge page TSB entry address we
143 * calculated above. If the huge page TSB has not been
144 * allocated, setup a trap stack and call hugetlb_setup()
145 * to do so, then return from the trap to replay the TLB
148 * This is necessary to handle the case of transparent huge
149 * pages where we don't really have a non-atomic context
150 * in which to allocate the hugepage TSB hash table. When
151 * the 'mm' faults in the hugepage for the first time, we
152 * thus handle it here. This also makes sure that we can
153 * allocate the TSB hash table on the correct NUMA node.
155 TRAP_LOAD_TRAP_BLOCK(%g7, %g2)
156 ldx [%g7 + TRAP_PER_CPU_TSB_HUGE_TEMP], %g1
161 661: rdpr %pstate, %g5
162 wrpr %g5, PSTATE_AG | PSTATE_MG, %pstate
163 .section .sun4v_2insn_patch, "ax"
171 bne,pn %xcc, winfix_trampoline
176 add %sp, PTREGS_OFF, %o0
183 /* At this point we have:
184 * %g1 -- TSB entry address
185 * %g3 -- FAULT_CODE_{D,I}TLB
187 * %g6 -- TAG TARGET (vaddr >> 22)
190 TSB_LOCK_TAG(%g1, %g2, %g7)
191 TSB_WRITE(%g1, %g5, %g6)
193 /* Finally, load TLB and return from trap. */
195 cmp %g3, FAULT_CODE_DTLB
196 bne,pn %xcc, tsb_itlb_load
201 661: stxa %g5, [%g0] ASI_DTLB_DATA_IN
203 .section .sun4v_2insn_patch, "ax"
209 /* For sun4v the ASI_DTLB_DATA_IN store and the retry
210 * instruction get nop'd out and we get here to branch
211 * to the sun4v tlb load code. The registers are setup
218 * The sun4v TLB load wants the PTE in %g3 so we fix that
221 ba,pt %xcc, sun4v_dtlb_load
225 /* Executable bit must be set. */
226 661: sethi %hi(_PAGE_EXEC_4U), %g4
228 .section .sun4v_2insn_patch, "ax"
230 andcc %g5, _PAGE_EXEC_4V, %g0
234 be,pn %xcc, tsb_do_fault
237 661: stxa %g5, [%g0] ASI_ITLB_DATA_IN
239 .section .sun4v_2insn_patch, "ax"
245 /* For sun4v the ASI_ITLB_DATA_IN store and the retry
246 * instruction get nop'd out and we get here to branch
247 * to the sun4v tlb load code. The registers are setup
254 * The sun4v TLB load wants the PTE in %g3 so we fix that
257 ba,pt %xcc, sun4v_itlb_load
260 /* No valid entry in the page tables, do full fault
266 cmp %g3, FAULT_CODE_DTLB
268 661: rdpr %pstate, %g5
269 wrpr %g5, PSTATE_AG | PSTATE_MG, %pstate
270 .section .sun4v_2insn_patch, "ax"
273 ldxa [%g0] ASI_SCRATCHPAD, %g4
276 bne,pn %xcc, tsb_do_itlb_fault
283 661: mov TLB_TAG_ACCESS, %g4
284 ldxa [%g4] ASI_DMMU, %g5
285 .section .sun4v_2insn_patch, "ax"
287 ldx [%g4 + HV_FAULT_D_ADDR_OFFSET], %g5
291 /* Clear context ID bits. */
292 srlx %g5, PAGE_SHIFT, %g5
293 sllx %g5, PAGE_SHIFT, %g5
295 be,pt %xcc, sparc64_realfault_common
296 mov FAULT_CODE_DTLB, %g4
297 ba,pt %xcc, winfix_trampoline
302 ba,pt %xcc, sparc64_realfault_common
303 mov FAULT_CODE_ITLB, %g4
305 .globl sparc64_realfault_common
306 sparc64_realfault_common:
307 /* fault code in %g4, fault address in %g5, etrap will
308 * preserve these two values in %l4 and %l5 respectively
310 ba,pt %xcc, etrap ! Save trap state
312 stb %l4, [%g6 + TI_FAULT_CODE] ! Save fault code
313 stx %l5, [%g6 + TI_FAULT_ADDR] ! Save fault address
314 call do_sparc64_fault ! Call fault handler
315 add %sp, PTREGS_OFF, %o0 ! Compute pt_regs arg
316 ba,pt %xcc, rtrap ! Restore cpu state
317 nop ! Delay slot (fill me)
320 rdpr %tpc, %g3 ! Prepare winfixup TNPC
321 or %g3, 0x7c, %g3 ! Compute branch offset
322 wrpr %g3, %tnpc ! Write it into TNPC
325 /* Insert an entry into the TSB.
327 * %o0: TSB entry pointer (virt or phys address)
335 wrpr %o5, PSTATE_IE, %pstate
336 TSB_LOCK_TAG(%o0, %g2, %g3)
337 TSB_WRITE(%o0, %o2, %o1)
341 .size __tsb_insert, .-__tsb_insert
343 /* Flush the given TSB entry if it has the matching
346 * %o0: TSB entry pointer (virt or phys address)
351 .type tsb_flush,#function
353 sethi %hi(TSB_TAG_LOCK_HIGH), %g2
354 1: TSB_LOAD_TAG(%o0, %g1)
362 sllx %o3, TSB_TAG_INVALID_BIT, %o3
363 TSB_CAS_TAG(%o0, %g1, %o3)
369 .size tsb_flush, .-tsb_flush
371 /* Reload MMU related context switch state at
374 * %o0: page table physical address
375 * %o1: TSB base config pointer
376 * %o2: TSB huge config pointer, or NULL if none
377 * %o3: Hypervisor TSB descriptor physical address
379 * We have to run this whole thing with interrupts
380 * disabled so that the current cpu doesn't change
384 .globl __tsb_context_switch
385 .type __tsb_context_switch,#function
386 __tsb_context_switch:
388 wrpr %g1, PSTATE_IE, %pstate
390 TRAP_LOAD_TRAP_BLOCK(%g2, %g3)
392 stx %o0, [%g2 + TRAP_PER_CPU_PGD_PADDR]
394 ldx [%o1 + TSB_CONFIG_REG_VAL], %o0
398 ldx [%o2 + TSB_CONFIG_REG_VAL], %g3
400 1: stx %g3, [%g2 + TRAP_PER_CPU_TSB_HUGE]
402 sethi %hi(tlb_type), %g2
403 lduw [%g2 + %lo(tlb_type)], %g2
408 /* Hypervisor TSB switch. */
409 mov SCRATCHPAD_UTSBREG1, %o5
410 stxa %o0, [%o5] ASI_SCRATCHPAD
411 mov SCRATCHPAD_UTSBREG2, %o5
412 stxa %g3, [%o5] ASI_SCRATCHPAD
418 mov HV_FAST_MMU_TSB_CTXNON0, %o5
426 /* SUN4U TSB switch. */
428 stxa %o0, [%o5] ASI_DMMU
430 stxa %o0, [%o5] ASI_IMMU
433 2: ldx [%o1 + TSB_CONFIG_MAP_VADDR], %o4
435 ldx [%o1 + TSB_CONFIG_MAP_PTE], %o5
437 sethi %hi(sparc64_highest_unlocked_tlb_ent), %g2
438 mov TLB_TAG_ACCESS, %g3
439 lduw [%g2 + %lo(sparc64_highest_unlocked_tlb_ent)], %g2
440 stxa %o4, [%g3] ASI_DMMU
443 stxa %o5, [%g2] ASI_DTLB_DATA_ACCESS
449 ldx [%o2 + TSB_CONFIG_MAP_VADDR], %o4
450 ldx [%o2 + TSB_CONFIG_MAP_PTE], %o5
451 mov TLB_TAG_ACCESS, %g3
452 stxa %o4, [%g3] ASI_DMMU
454 sub %g2, (1 << 3), %g2
455 stxa %o5, [%g2] ASI_DTLB_DATA_ACCESS
463 .size __tsb_context_switch, .-__tsb_context_switch
465 #define TSB_PASS_BITS ((1 << TSB_TAG_LOCK_BIT) | \
466 (1 << TSB_TAG_INVALID_BIT))
470 .type copy_tsb,#function
471 copy_tsb: /* %o0=old_tsb_base, %o1=old_tsb_size
472 * %o2=new_tsb_base, %o3=new_tsb_size
474 sethi %uhi(TSB_PASS_BITS), %g7
476 add %o0, %o1, %g1 /* end of old tsb */
478 sub %o3, 1, %o3 /* %o3 == new tsb hash mask */
480 661: prefetcha [%o0] ASI_N, #one_read
481 .section .tsb_phys_patch, "ax"
483 prefetcha [%o0] ASI_PHYS_USE_EC, #one_read
486 90: andcc %o0, (64 - 1), %g0
490 661: prefetcha [%o5] ASI_N, #one_read
491 .section .tsb_phys_patch, "ax"
493 prefetcha [%o5] ASI_PHYS_USE_EC, #one_read
496 1: TSB_LOAD_QUAD(%o0, %g2) /* %g2/%g3 == TSB entry */
497 andcc %g2, %g7, %g0 /* LOCK or INVALID set? */
498 bne,pn %xcc, 80f /* Skip it */
499 sllx %g2, 22, %o4 /* TAG --> VADDR */
501 /* This can definitely be computed faster... */
502 srlx %o0, 4, %o5 /* Build index */
503 and %o5, 511, %o5 /* Mask index */
504 sllx %o5, PAGE_SHIFT, %o5 /* Put into vaddr position */
505 or %o4, %o5, %o4 /* Full VADDR. */
506 srlx %o4, PAGE_SHIFT, %o4 /* Shift down to create index */
507 and %o4, %o3, %o4 /* Mask with new_tsb_nents-1 */
508 sllx %o4, 4, %o4 /* Shift back up into tsb ent offset */
509 TSB_STORE(%o2 + %o4, %g2) /* Store TAG */
510 add %o4, 0x8, %o4 /* Advance to TTE */
511 TSB_STORE(%o2 + %o4, %g3) /* Store TTE */
520 .size copy_tsb, .-copy_tsb
522 /* Set the invalid bit in all TSB entries. */
525 .type tsb_init,#function
526 tsb_init: /* %o0 = TSB vaddr, %o1 = size in bytes */
527 prefetch [%o0 + 0x000], #n_writes
529 prefetch [%o0 + 0x040], #n_writes
530 sllx %g1, TSB_TAG_INVALID_BIT, %g1
531 prefetch [%o0 + 0x080], #n_writes
532 1: prefetch [%o0 + 0x0c0], #n_writes
533 stx %g1, [%o0 + 0x00]
534 stx %g1, [%o0 + 0x10]
535 stx %g1, [%o0 + 0x20]
536 stx %g1, [%o0 + 0x30]
537 prefetch [%o0 + 0x100], #n_writes
538 stx %g1, [%o0 + 0x40]
539 stx %g1, [%o0 + 0x50]
540 stx %g1, [%o0 + 0x60]
541 stx %g1, [%o0 + 0x70]
542 prefetch [%o0 + 0x140], #n_writes
543 stx %g1, [%o0 + 0x80]
544 stx %g1, [%o0 + 0x90]
545 stx %g1, [%o0 + 0xa0]
546 stx %g1, [%o0 + 0xb0]
547 prefetch [%o0 + 0x180], #n_writes
548 stx %g1, [%o0 + 0xc0]
549 stx %g1, [%o0 + 0xd0]
550 stx %g1, [%o0 + 0xe0]
551 stx %g1, [%o0 + 0xf0]
552 subcc %o1, 0x100, %o1
559 .size tsb_init, .-tsb_init
562 .type NGtsb_init,#function
566 wr %g0, ASI_BLK_INIT_QUAD_LDD_P, %asi
567 sllx %g1, TSB_TAG_INVALID_BIT, %g1
568 1: stxa %g1, [%o0 + 0x00] %asi
569 stxa %g1, [%o0 + 0x10] %asi
570 stxa %g1, [%o0 + 0x20] %asi
571 stxa %g1, [%o0 + 0x30] %asi
572 stxa %g1, [%o0 + 0x40] %asi
573 stxa %g1, [%o0 + 0x50] %asi
574 stxa %g1, [%o0 + 0x60] %asi
575 stxa %g1, [%o0 + 0x70] %asi
576 stxa %g1, [%o0 + 0x80] %asi
577 stxa %g1, [%o0 + 0x90] %asi
578 stxa %g1, [%o0 + 0xa0] %asi
579 stxa %g1, [%o0 + 0xb0] %asi
580 stxa %g1, [%o0 + 0xc0] %asi
581 stxa %g1, [%o0 + 0xd0] %asi
582 stxa %g1, [%o0 + 0xe0] %asi
583 stxa %g1, [%o0 + 0xf0] %asi
584 subcc %o1, 0x100, %o1
590 .size NGtsb_init, .-NGtsb_init