tsb.S 13 KB

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