tsb.S 10.0 KB

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