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