tsb.S 12 KB

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