tsb.S 13 KB

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