tsb.S 12 KB

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