tsb.S 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* tsb.S: Sparc64 TSB table handling.
  2. *
  3. * Copyright (C) 2006 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <asm/tsb.h>
  6. #include <asm/hypervisor.h>
  7. #include <asm/page.h>
  8. #include <asm/cpudata.h>
  9. #include <asm/mmu.h>
  10. .text
  11. .align 32
  12. /* Invoked from TLB miss handler, we are in the
  13. * MMU global registers and they are setup like
  14. * this:
  15. *
  16. * %g1: TSB entry pointer
  17. * %g2: available temporary
  18. * %g3: FAULT_CODE_{D,I}TLB
  19. * %g4: available temporary
  20. * %g5: available temporary
  21. * %g6: TAG TARGET
  22. * %g7: available temporary, will be loaded by us with
  23. * the physical address base of the linux page
  24. * tables for the current address space
  25. */
  26. tsb_miss_dtlb:
  27. mov TLB_TAG_ACCESS, %g4
  28. ba,pt %xcc, tsb_miss_page_table_walk
  29. ldxa [%g4] ASI_DMMU, %g4
  30. tsb_miss_itlb:
  31. mov TLB_TAG_ACCESS, %g4
  32. ba,pt %xcc, tsb_miss_page_table_walk
  33. ldxa [%g4] ASI_IMMU, %g4
  34. /* At this point we have:
  35. * %g1 -- PAGE_SIZE TSB entry address
  36. * %g3 -- FAULT_CODE_{D,I}TLB
  37. * %g4 -- missing virtual address
  38. * %g6 -- TAG TARGET (vaddr >> 22)
  39. */
  40. tsb_miss_page_table_walk:
  41. TRAP_LOAD_TRAP_BLOCK(%g7, %g5)
  42. /* Before committing to a full page table walk,
  43. * check the huge page TSB.
  44. */
  45. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  46. 661: ldx [%g7 + TRAP_PER_CPU_TSB_HUGE], %g5
  47. nop
  48. .section .sun4v_2insn_patch, "ax"
  49. .word 661b
  50. mov SCRATCHPAD_UTSBREG2, %g5
  51. ldxa [%g5] ASI_SCRATCHPAD, %g5
  52. .previous
  53. cmp %g5, -1
  54. be,pt %xcc, 80f
  55. nop
  56. /* We need an aligned pair of registers containing 2 values
  57. * which can be easily rematerialized. %g6 and %g7 foot the
  58. * bill just nicely. We'll save %g6 away into %g2 for the
  59. * huge page TSB TAG comparison.
  60. *
  61. * Perform a huge page TSB lookup.
  62. */
  63. mov %g6, %g2
  64. and %g5, 0x7, %g6
  65. mov 512, %g7
  66. andn %g5, 0x7, %g5
  67. sllx %g7, %g6, %g7
  68. srlx %g4, HPAGE_SHIFT, %g6
  69. sub %g7, 1, %g7
  70. and %g6, %g7, %g6
  71. sllx %g6, 4, %g6
  72. add %g5, %g6, %g5
  73. TSB_LOAD_QUAD(%g5, %g6)
  74. cmp %g6, %g2
  75. be,a,pt %xcc, tsb_tlb_reload
  76. mov %g7, %g5
  77. /* No match, remember the huge page TSB entry address,
  78. * and restore %g6 and %g7.
  79. */
  80. TRAP_LOAD_TRAP_BLOCK(%g7, %g6)
  81. srlx %g4, 22, %g6
  82. 80: stx %g5, [%g7 + TRAP_PER_CPU_TSB_HUGE_TEMP]
  83. #endif
  84. ldx [%g7 + TRAP_PER_CPU_PGD_PADDR], %g7
  85. /* At this point we have:
  86. * %g1 -- TSB entry address
  87. * %g3 -- FAULT_CODE_{D,I}TLB
  88. * %g4 -- missing virtual address
  89. * %g6 -- TAG TARGET (vaddr >> 22)
  90. * %g7 -- page table physical address
  91. *
  92. * We know that both the base PAGE_SIZE TSB and the HPAGE_SIZE
  93. * TSB both lack a matching entry.
  94. */
  95. tsb_miss_page_table_walk_sun4v_fastpath:
  96. USER_PGTABLE_WALK_TL1(%g4, %g7, %g5, %g2, tsb_do_fault)
  97. /* Valid PTE is now in %g5. */
  98. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  99. 661: sethi %uhi(_PAGE_SZALL_4U), %g7
  100. sllx %g7, 32, %g7
  101. .section .sun4v_2insn_patch, "ax"
  102. .word 661b
  103. mov _PAGE_SZALL_4V, %g7
  104. nop
  105. .previous
  106. and %g5, %g7, %g2
  107. 661: sethi %uhi(_PAGE_SZHUGE_4U), %g7
  108. sllx %g7, 32, %g7
  109. .section .sun4v_2insn_patch, "ax"
  110. .word 661b
  111. mov _PAGE_SZHUGE_4V, %g7
  112. nop
  113. .previous
  114. cmp %g2, %g7
  115. bne,pt %xcc, 60f
  116. nop
  117. /* It is a huge page, use huge page TSB entry address we
  118. * calculated above.
  119. */
  120. TRAP_LOAD_TRAP_BLOCK(%g7, %g2)
  121. ldx [%g7 + TRAP_PER_CPU_TSB_HUGE_TEMP], %g2
  122. cmp %g2, -1
  123. movne %xcc, %g2, %g1
  124. 60:
  125. #endif
  126. /* At this point we have:
  127. * %g1 -- TSB entry address
  128. * %g3 -- FAULT_CODE_{D,I}TLB
  129. * %g5 -- valid PTE
  130. * %g6 -- TAG TARGET (vaddr >> 22)
  131. */
  132. tsb_reload:
  133. TSB_LOCK_TAG(%g1, %g2, %g7)
  134. TSB_WRITE(%g1, %g5, %g6)
  135. /* Finally, load TLB and return from trap. */
  136. tsb_tlb_reload:
  137. cmp %g3, FAULT_CODE_DTLB
  138. bne,pn %xcc, tsb_itlb_load
  139. nop
  140. tsb_dtlb_load:
  141. 661: stxa %g5, [%g0] ASI_DTLB_DATA_IN
  142. retry
  143. .section .sun4v_2insn_patch, "ax"
  144. .word 661b
  145. nop
  146. nop
  147. .previous
  148. /* For sun4v the ASI_DTLB_DATA_IN store and the retry
  149. * instruction get nop'd out and we get here to branch
  150. * to the sun4v tlb load code. The registers are setup
  151. * as follows:
  152. *
  153. * %g4: vaddr
  154. * %g5: PTE
  155. * %g6: TAG
  156. *
  157. * The sun4v TLB load wants the PTE in %g3 so we fix that
  158. * up here.
  159. */
  160. ba,pt %xcc, sun4v_dtlb_load
  161. mov %g5, %g3
  162. tsb_itlb_load:
  163. /* Executable bit must be set. */
  164. 661: sethi %hi(_PAGE_EXEC_4U), %g4
  165. andcc %g5, %g4, %g0
  166. .section .sun4v_2insn_patch, "ax"
  167. .word 661b
  168. andcc %g5, _PAGE_EXEC_4V, %g0
  169. nop
  170. .previous
  171. be,pn %xcc, tsb_do_fault
  172. nop
  173. 661: stxa %g5, [%g0] ASI_ITLB_DATA_IN
  174. retry
  175. .section .sun4v_2insn_patch, "ax"
  176. .word 661b
  177. nop
  178. nop
  179. .previous
  180. /* For sun4v the ASI_ITLB_DATA_IN store and the retry
  181. * instruction get nop'd out and we get here to branch
  182. * to the sun4v tlb load code. The registers are setup
  183. * as follows:
  184. *
  185. * %g4: vaddr
  186. * %g5: PTE
  187. * %g6: TAG
  188. *
  189. * The sun4v TLB load wants the PTE in %g3 so we fix that
  190. * up here.
  191. */
  192. ba,pt %xcc, sun4v_itlb_load
  193. mov %g5, %g3
  194. /* No valid entry in the page tables, do full fault
  195. * processing.
  196. */
  197. .globl tsb_do_fault
  198. tsb_do_fault:
  199. cmp %g3, FAULT_CODE_DTLB
  200. 661: rdpr %pstate, %g5
  201. wrpr %g5, PSTATE_AG | PSTATE_MG, %pstate
  202. .section .sun4v_2insn_patch, "ax"
  203. .word 661b
  204. SET_GL(1)
  205. ldxa [%g0] ASI_SCRATCHPAD, %g4
  206. .previous
  207. bne,pn %xcc, tsb_do_itlb_fault
  208. nop
  209. tsb_do_dtlb_fault:
  210. rdpr %tl, %g3
  211. cmp %g3, 1
  212. 661: mov TLB_TAG_ACCESS, %g4
  213. ldxa [%g4] ASI_DMMU, %g5
  214. .section .sun4v_2insn_patch, "ax"
  215. .word 661b
  216. ldx [%g4 + HV_FAULT_D_ADDR_OFFSET], %g5
  217. nop
  218. .previous
  219. be,pt %xcc, sparc64_realfault_common
  220. mov FAULT_CODE_DTLB, %g4
  221. ba,pt %xcc, winfix_trampoline
  222. nop
  223. tsb_do_itlb_fault:
  224. rdpr %tpc, %g5
  225. ba,pt %xcc, sparc64_realfault_common
  226. mov FAULT_CODE_ITLB, %g4
  227. .globl sparc64_realfault_common
  228. sparc64_realfault_common:
  229. /* fault code in %g4, fault address in %g5, etrap will
  230. * preserve these two values in %l4 and %l5 respectively
  231. */
  232. ba,pt %xcc, etrap ! Save trap state
  233. 1: rd %pc, %g7 ! ...
  234. stb %l4, [%g6 + TI_FAULT_CODE] ! Save fault code
  235. stx %l5, [%g6 + TI_FAULT_ADDR] ! Save fault address
  236. call do_sparc64_fault ! Call fault handler
  237. add %sp, PTREGS_OFF, %o0 ! Compute pt_regs arg
  238. ba,pt %xcc, rtrap ! Restore cpu state
  239. nop ! Delay slot (fill me)
  240. winfix_trampoline:
  241. rdpr %tpc, %g3 ! Prepare winfixup TNPC
  242. or %g3, 0x7c, %g3 ! Compute branch offset
  243. wrpr %g3, %tnpc ! Write it into TNPC
  244. done ! Trap return
  245. /* Insert an entry into the TSB.
  246. *
  247. * %o0: TSB entry pointer (virt or phys address)
  248. * %o1: tag
  249. * %o2: pte
  250. */
  251. .align 32
  252. .globl __tsb_insert
  253. __tsb_insert:
  254. rdpr %pstate, %o5
  255. wrpr %o5, PSTATE_IE, %pstate
  256. TSB_LOCK_TAG(%o0, %g2, %g3)
  257. TSB_WRITE(%o0, %o2, %o1)
  258. wrpr %o5, %pstate
  259. retl
  260. nop
  261. .size __tsb_insert, .-__tsb_insert
  262. /* Flush the given TSB entry if it has the matching
  263. * tag.
  264. *
  265. * %o0: TSB entry pointer (virt or phys address)
  266. * %o1: tag
  267. */
  268. .align 32
  269. .globl tsb_flush
  270. .type tsb_flush,#function
  271. tsb_flush:
  272. sethi %hi(TSB_TAG_LOCK_HIGH), %g2
  273. 1: TSB_LOAD_TAG(%o0, %g1)
  274. srlx %g1, 32, %o3
  275. andcc %o3, %g2, %g0
  276. bne,pn %icc, 1b
  277. nop
  278. cmp %g1, %o1
  279. mov 1, %o3
  280. bne,pt %xcc, 2f
  281. sllx %o3, TSB_TAG_INVALID_BIT, %o3
  282. TSB_CAS_TAG(%o0, %g1, %o3)
  283. cmp %g1, %o3
  284. bne,pn %xcc, 1b
  285. nop
  286. 2: retl
  287. nop
  288. .size tsb_flush, .-tsb_flush
  289. /* Reload MMU related context switch state at
  290. * schedule() time.
  291. *
  292. * %o0: page table physical address
  293. * %o1: TSB base config pointer
  294. * %o2: TSB huge config pointer, or NULL if none
  295. * %o3: Hypervisor TSB descriptor physical address
  296. *
  297. * We have to run this whole thing with interrupts
  298. * disabled so that the current cpu doesn't change
  299. * due to preemption.
  300. */
  301. .align 32
  302. .globl __tsb_context_switch
  303. .type __tsb_context_switch,#function
  304. __tsb_context_switch:
  305. rdpr %pstate, %g1
  306. wrpr %g1, PSTATE_IE, %pstate
  307. TRAP_LOAD_TRAP_BLOCK(%g2, %g3)
  308. stx %o0, [%g2 + TRAP_PER_CPU_PGD_PADDR]
  309. ldx [%o1 + TSB_CONFIG_REG_VAL], %o0
  310. brz,pt %o2, 1f
  311. mov -1, %g3
  312. ldx [%o2 + TSB_CONFIG_REG_VAL], %g3
  313. 1: stx %g3, [%g2 + TRAP_PER_CPU_TSB_HUGE]
  314. sethi %hi(tlb_type), %g2
  315. lduw [%g2 + %lo(tlb_type)], %g2
  316. cmp %g2, 3
  317. bne,pt %icc, 50f
  318. nop
  319. /* Hypervisor TSB switch. */
  320. mov SCRATCHPAD_UTSBREG1, %o5
  321. stxa %o0, [%o5] ASI_SCRATCHPAD
  322. mov SCRATCHPAD_UTSBREG2, %o5
  323. stxa %g3, [%o5] ASI_SCRATCHPAD
  324. mov 2, %o0
  325. cmp %g3, -1
  326. move %xcc, 1, %o0
  327. mov HV_FAST_MMU_TSB_CTXNON0, %o5
  328. mov %o3, %o1
  329. ta HV_FAST_TRAP
  330. /* Finish up. */
  331. ba,pt %xcc, 9f
  332. nop
  333. /* SUN4U TSB switch. */
  334. 50: mov TSB_REG, %o5
  335. stxa %o0, [%o5] ASI_DMMU
  336. membar #Sync
  337. stxa %o0, [%o5] ASI_IMMU
  338. membar #Sync
  339. 2: ldx [%o1 + TSB_CONFIG_MAP_VADDR], %o4
  340. brz %o4, 9f
  341. ldx [%o1 + TSB_CONFIG_MAP_PTE], %o5
  342. sethi %hi(sparc64_highest_unlocked_tlb_ent), %g2
  343. mov TLB_TAG_ACCESS, %g3
  344. lduw [%g2 + %lo(sparc64_highest_unlocked_tlb_ent)], %g2
  345. stxa %o4, [%g3] ASI_DMMU
  346. membar #Sync
  347. sllx %g2, 3, %g2
  348. stxa %o5, [%g2] ASI_DTLB_DATA_ACCESS
  349. membar #Sync
  350. brz,pt %o2, 9f
  351. nop
  352. ldx [%o2 + TSB_CONFIG_MAP_VADDR], %o4
  353. ldx [%o2 + TSB_CONFIG_MAP_PTE], %o5
  354. mov TLB_TAG_ACCESS, %g3
  355. stxa %o4, [%g3] ASI_DMMU
  356. membar #Sync
  357. sub %g2, (1 << 3), %g2
  358. stxa %o5, [%g2] ASI_DTLB_DATA_ACCESS
  359. membar #Sync
  360. 9:
  361. wrpr %g1, %pstate
  362. retl
  363. nop
  364. .size __tsb_context_switch, .-__tsb_context_switch
  365. #define TSB_PASS_BITS ((1 << TSB_TAG_LOCK_BIT) | \
  366. (1 << TSB_TAG_INVALID_BIT))
  367. .align 32
  368. .globl copy_tsb
  369. .type copy_tsb,#function
  370. copy_tsb: /* %o0=old_tsb_base, %o1=old_tsb_size
  371. * %o2=new_tsb_base, %o3=new_tsb_size
  372. */
  373. sethi %uhi(TSB_PASS_BITS), %g7
  374. srlx %o3, 4, %o3
  375. add %o0, %o1, %g1 /* end of old tsb */
  376. sllx %g7, 32, %g7
  377. sub %o3, 1, %o3 /* %o3 == new tsb hash mask */
  378. 661: prefetcha [%o0] ASI_N, #one_read
  379. .section .tsb_phys_patch, "ax"
  380. .word 661b
  381. prefetcha [%o0] ASI_PHYS_USE_EC, #one_read
  382. .previous
  383. 90: andcc %o0, (64 - 1), %g0
  384. bne 1f
  385. add %o0, 64, %o5
  386. 661: prefetcha [%o5] ASI_N, #one_read
  387. .section .tsb_phys_patch, "ax"
  388. .word 661b
  389. prefetcha [%o5] ASI_PHYS_USE_EC, #one_read
  390. .previous
  391. 1: TSB_LOAD_QUAD(%o0, %g2) /* %g2/%g3 == TSB entry */
  392. andcc %g2, %g7, %g0 /* LOCK or INVALID set? */
  393. bne,pn %xcc, 80f /* Skip it */
  394. sllx %g2, 22, %o4 /* TAG --> VADDR */
  395. /* This can definitely be computed faster... */
  396. srlx %o0, 4, %o5 /* Build index */
  397. and %o5, 511, %o5 /* Mask index */
  398. sllx %o5, PAGE_SHIFT, %o5 /* Put into vaddr position */
  399. or %o4, %o5, %o4 /* Full VADDR. */
  400. srlx %o4, PAGE_SHIFT, %o4 /* Shift down to create index */
  401. and %o4, %o3, %o4 /* Mask with new_tsb_nents-1 */
  402. sllx %o4, 4, %o4 /* Shift back up into tsb ent offset */
  403. TSB_STORE(%o2 + %o4, %g2) /* Store TAG */
  404. add %o4, 0x8, %o4 /* Advance to TTE */
  405. TSB_STORE(%o2 + %o4, %g3) /* Store TTE */
  406. 80: add %o0, 16, %o0
  407. cmp %o0, %g1
  408. bne,pt %xcc, 90b
  409. nop
  410. retl
  411. nop
  412. .size copy_tsb, .-copy_tsb
  413. /* Set the invalid bit in all TSB entries. */
  414. .align 32
  415. .globl tsb_init
  416. .type tsb_init,#function
  417. tsb_init: /* %o0 = TSB vaddr, %o1 = size in bytes */
  418. prefetch [%o0 + 0x000], #n_writes
  419. mov 1, %g1
  420. prefetch [%o0 + 0x040], #n_writes
  421. sllx %g1, TSB_TAG_INVALID_BIT, %g1
  422. prefetch [%o0 + 0x080], #n_writes
  423. 1: prefetch [%o0 + 0x0c0], #n_writes
  424. stx %g1, [%o0 + 0x00]
  425. stx %g1, [%o0 + 0x10]
  426. stx %g1, [%o0 + 0x20]
  427. stx %g1, [%o0 + 0x30]
  428. prefetch [%o0 + 0x100], #n_writes
  429. stx %g1, [%o0 + 0x40]
  430. stx %g1, [%o0 + 0x50]
  431. stx %g1, [%o0 + 0x60]
  432. stx %g1, [%o0 + 0x70]
  433. prefetch [%o0 + 0x140], #n_writes
  434. stx %g1, [%o0 + 0x80]
  435. stx %g1, [%o0 + 0x90]
  436. stx %g1, [%o0 + 0xa0]
  437. stx %g1, [%o0 + 0xb0]
  438. prefetch [%o0 + 0x180], #n_writes
  439. stx %g1, [%o0 + 0xc0]
  440. stx %g1, [%o0 + 0xd0]
  441. stx %g1, [%o0 + 0xe0]
  442. stx %g1, [%o0 + 0xf0]
  443. subcc %o1, 0x100, %o1
  444. bne,pt %xcc, 1b
  445. add %o0, 0x100, %o0
  446. retl
  447. nop
  448. nop
  449. nop
  450. .size tsb_init, .-tsb_init
  451. .globl NGtsb_init
  452. .type NGtsb_init,#function
  453. NGtsb_init:
  454. rd %asi, %g2
  455. mov 1, %g1
  456. wr %g0, ASI_BLK_INIT_QUAD_LDD_P, %asi
  457. sllx %g1, TSB_TAG_INVALID_BIT, %g1
  458. 1: stxa %g1, [%o0 + 0x00] %asi
  459. stxa %g1, [%o0 + 0x10] %asi
  460. stxa %g1, [%o0 + 0x20] %asi
  461. stxa %g1, [%o0 + 0x30] %asi
  462. stxa %g1, [%o0 + 0x40] %asi
  463. stxa %g1, [%o0 + 0x50] %asi
  464. stxa %g1, [%o0 + 0x60] %asi
  465. stxa %g1, [%o0 + 0x70] %asi
  466. stxa %g1, [%o0 + 0x80] %asi
  467. stxa %g1, [%o0 + 0x90] %asi
  468. stxa %g1, [%o0 + 0xa0] %asi
  469. stxa %g1, [%o0 + 0xb0] %asi
  470. stxa %g1, [%o0 + 0xc0] %asi
  471. stxa %g1, [%o0 + 0xd0] %asi
  472. stxa %g1, [%o0 + 0xe0] %asi
  473. stxa %g1, [%o0 + 0xf0] %asi
  474. subcc %o1, 0x100, %o1
  475. bne,pt %xcc, 1b
  476. add %o0, 0x100, %o0
  477. membar #Sync
  478. retl
  479. wr %g2, 0x0, %asi
  480. .size NGtsb_init, .-NGtsb_init