tsb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _SPARC64_TSB_H
  3. #define _SPARC64_TSB_H
  4. /* The sparc64 TSB is similar to the powerpc hashtables. It's a
  5. * power-of-2 sized table of TAG/PTE pairs. The cpu precomputes
  6. * pointers into this table for 8K and 64K page sizes, and also a
  7. * comparison TAG based upon the virtual address and context which
  8. * faults.
  9. *
  10. * TLB miss trap handler software does the actual lookup via something
  11. * of the form:
  12. *
  13. * ldxa [%g0] ASI_{D,I}MMU_TSB_8KB_PTR, %g1
  14. * ldxa [%g0] ASI_{D,I}MMU, %g6
  15. * sllx %g6, 22, %g6
  16. * srlx %g6, 22, %g6
  17. * ldda [%g1] ASI_NUCLEUS_QUAD_LDD, %g4
  18. * cmp %g4, %g6
  19. * bne,pn %xcc, tsb_miss_{d,i}tlb
  20. * mov FAULT_CODE_{D,I}TLB, %g3
  21. * stxa %g5, [%g0] ASI_{D,I}TLB_DATA_IN
  22. * retry
  23. *
  24. *
  25. * Each 16-byte slot of the TSB is the 8-byte tag and then the 8-byte
  26. * PTE. The TAG is of the same layout as the TLB TAG TARGET mmu
  27. * register which is:
  28. *
  29. * -------------------------------------------------
  30. * | - | CONTEXT | - | VADDR bits 63:22 |
  31. * -------------------------------------------------
  32. * 63 61 60 48 47 42 41 0
  33. *
  34. * But actually, since we use per-mm TSB's, we zero out the CONTEXT
  35. * field.
  36. *
  37. * Like the powerpc hashtables we need to use locking in order to
  38. * synchronize while we update the entries. PTE updates need locking
  39. * as well.
  40. *
  41. * We need to carefully choose a lock bits for the TSB entry. We
  42. * choose to use bit 47 in the tag. Also, since we never map anything
  43. * at page zero in context zero, we use zero as an invalid tag entry.
  44. * When the lock bit is set, this forces a tag comparison failure.
  45. */
  46. #define TSB_TAG_LOCK_BIT 47
  47. #define TSB_TAG_LOCK_HIGH (1 << (TSB_TAG_LOCK_BIT - 32))
  48. #define TSB_TAG_INVALID_BIT 46
  49. #define TSB_TAG_INVALID_HIGH (1 << (TSB_TAG_INVALID_BIT - 32))
  50. /* Some cpus support physical address quad loads. We want to use
  51. * those if possible so we don't need to hard-lock the TSB mapping
  52. * into the TLB. We encode some instruction patching in order to
  53. * support this.
  54. *
  55. * The kernel TSB is locked into the TLB by virtue of being in the
  56. * kernel image, so we don't play these games for swapper_tsb access.
  57. */
  58. #ifndef __ASSEMBLY__
  59. struct tsb_ldquad_phys_patch_entry {
  60. unsigned int addr;
  61. unsigned int sun4u_insn;
  62. unsigned int sun4v_insn;
  63. };
  64. extern struct tsb_ldquad_phys_patch_entry __tsb_ldquad_phys_patch,
  65. __tsb_ldquad_phys_patch_end;
  66. struct tsb_phys_patch_entry {
  67. unsigned int addr;
  68. unsigned int insn;
  69. };
  70. extern struct tsb_phys_patch_entry __tsb_phys_patch, __tsb_phys_patch_end;
  71. #endif
  72. #define TSB_LOAD_QUAD(TSB, REG) \
  73. 661: ldda [TSB] ASI_NUCLEUS_QUAD_LDD, REG; \
  74. .section .tsb_ldquad_phys_patch, "ax"; \
  75. .word 661b; \
  76. ldda [TSB] ASI_QUAD_LDD_PHYS, REG; \
  77. ldda [TSB] ASI_QUAD_LDD_PHYS_4V, REG; \
  78. .previous
  79. #define TSB_LOAD_TAG_HIGH(TSB, REG) \
  80. 661: lduwa [TSB] ASI_N, REG; \
  81. .section .tsb_phys_patch, "ax"; \
  82. .word 661b; \
  83. lduwa [TSB] ASI_PHYS_USE_EC, REG; \
  84. .previous
  85. #define TSB_LOAD_TAG(TSB, REG) \
  86. 661: ldxa [TSB] ASI_N, REG; \
  87. .section .tsb_phys_patch, "ax"; \
  88. .word 661b; \
  89. ldxa [TSB] ASI_PHYS_USE_EC, REG; \
  90. .previous
  91. #define TSB_CAS_TAG_HIGH(TSB, REG1, REG2) \
  92. 661: casa [TSB] ASI_N, REG1, REG2; \
  93. .section .tsb_phys_patch, "ax"; \
  94. .word 661b; \
  95. casa [TSB] ASI_PHYS_USE_EC, REG1, REG2; \
  96. .previous
  97. #define TSB_CAS_TAG(TSB, REG1, REG2) \
  98. 661: casxa [TSB] ASI_N, REG1, REG2; \
  99. .section .tsb_phys_patch, "ax"; \
  100. .word 661b; \
  101. casxa [TSB] ASI_PHYS_USE_EC, REG1, REG2; \
  102. .previous
  103. #define TSB_STORE(ADDR, VAL) \
  104. 661: stxa VAL, [ADDR] ASI_N; \
  105. .section .tsb_phys_patch, "ax"; \
  106. .word 661b; \
  107. stxa VAL, [ADDR] ASI_PHYS_USE_EC; \
  108. .previous
  109. #define TSB_LOCK_TAG(TSB, REG1, REG2) \
  110. 99: TSB_LOAD_TAG_HIGH(TSB, REG1); \
  111. sethi %hi(TSB_TAG_LOCK_HIGH), REG2;\
  112. andcc REG1, REG2, %g0; \
  113. bne,pn %icc, 99b; \
  114. nop; \
  115. TSB_CAS_TAG_HIGH(TSB, REG1, REG2); \
  116. cmp REG1, REG2; \
  117. bne,pn %icc, 99b; \
  118. nop; \
  119. #define TSB_WRITE(TSB, TTE, TAG) \
  120. add TSB, 0x8, TSB; \
  121. TSB_STORE(TSB, TTE); \
  122. sub TSB, 0x8, TSB; \
  123. TSB_STORE(TSB, TAG);
  124. /* Do a kernel page table walk. Leaves valid PTE value in
  125. * REG1. Jumps to FAIL_LABEL on early page table walk
  126. * termination. VADDR will not be clobbered, but REG2 will.
  127. *
  128. * There are two masks we must apply to propagate bits from
  129. * the virtual address into the PTE physical address field
  130. * when dealing with huge pages. This is because the page
  131. * table boundaries do not match the huge page size(s) the
  132. * hardware supports.
  133. *
  134. * In these cases we propagate the bits that are below the
  135. * page table level where we saw the huge page mapping, but
  136. * are still within the relevant physical bits for the huge
  137. * page size in question. So for PMD mappings (which fall on
  138. * bit 23, for 8MB per PMD) we must propagate bit 22 for a
  139. * 4MB huge page. For huge PUDs (which fall on bit 33, for
  140. * 8GB per PUD), we have to accommodate 256MB and 2GB huge
  141. * pages. So for those we propagate bits 32 to 28.
  142. */
  143. #define KERN_PGTABLE_WALK(VADDR, REG1, REG2, FAIL_LABEL) \
  144. sethi %hi(swapper_pg_dir), REG1; \
  145. or REG1, %lo(swapper_pg_dir), REG1; \
  146. sllx VADDR, 64 - (PGDIR_SHIFT + PGDIR_BITS), REG2; \
  147. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  148. andn REG2, 0x7, REG2; \
  149. ldx [REG1 + REG2], REG1; \
  150. brz,pn REG1, FAIL_LABEL; \
  151. sllx VADDR, 64 - (PUD_SHIFT + PUD_BITS), REG2; \
  152. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  153. andn REG2, 0x7, REG2; \
  154. ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  155. brz,pn REG1, FAIL_LABEL; \
  156. sethi %uhi(_PAGE_PUD_HUGE), REG2; \
  157. brz,pn REG1, FAIL_LABEL; \
  158. sllx REG2, 32, REG2; \
  159. andcc REG1, REG2, %g0; \
  160. sethi %hi(0xf8000000), REG2; \
  161. bne,pt %xcc, 697f; \
  162. sllx REG2, 1, REG2; \
  163. sllx VADDR, 64 - (PMD_SHIFT + PMD_BITS), REG2; \
  164. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  165. andn REG2, 0x7, REG2; \
  166. ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  167. sethi %uhi(_PAGE_PMD_HUGE), REG2; \
  168. brz,pn REG1, FAIL_LABEL; \
  169. sllx REG2, 32, REG2; \
  170. andcc REG1, REG2, %g0; \
  171. be,pn %xcc, 698f; \
  172. sethi %hi(0x400000), REG2; \
  173. 697: brgez,pn REG1, FAIL_LABEL; \
  174. andn REG1, REG2, REG1; \
  175. and VADDR, REG2, REG2; \
  176. ba,pt %xcc, 699f; \
  177. or REG1, REG2, REG1; \
  178. 698: sllx VADDR, 64 - PMD_SHIFT, REG2; \
  179. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  180. andn REG2, 0x7, REG2; \
  181. ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  182. brgez,pn REG1, FAIL_LABEL; \
  183. nop; \
  184. 699:
  185. /* PUD has been loaded into REG1, interpret the value, seeing
  186. * if it is a HUGE PUD or a normal one. If it is not valid
  187. * then jump to FAIL_LABEL. If it is a HUGE PUD, and it
  188. * translates to a valid PTE, branch to PTE_LABEL.
  189. *
  190. * We have to propagate bits [32:22] from the virtual address
  191. * to resolve at 4M granularity.
  192. */
  193. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  194. #define USER_PGTABLE_CHECK_PUD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, PTE_LABEL) \
  195. 700: ba 700f; \
  196. nop; \
  197. .section .pud_huge_patch, "ax"; \
  198. .word 700b; \
  199. nop; \
  200. .previous; \
  201. brz,pn REG1, FAIL_LABEL; \
  202. sethi %uhi(_PAGE_PUD_HUGE), REG2; \
  203. sllx REG2, 32, REG2; \
  204. andcc REG1, REG2, %g0; \
  205. be,pt %xcc, 700f; \
  206. sethi %hi(0xffe00000), REG2; \
  207. sllx REG2, 1, REG2; \
  208. brgez,pn REG1, FAIL_LABEL; \
  209. andn REG1, REG2, REG1; \
  210. and VADDR, REG2, REG2; \
  211. brlz,pt REG1, PTE_LABEL; \
  212. or REG1, REG2, REG1; \
  213. 700:
  214. #else
  215. #define USER_PGTABLE_CHECK_PUD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, PTE_LABEL) \
  216. brz,pn REG1, FAIL_LABEL; \
  217. nop;
  218. #endif
  219. /* PMD has been loaded into REG1, interpret the value, seeing
  220. * if it is a HUGE PMD or a normal one. If it is not valid
  221. * then jump to FAIL_LABEL. If it is a HUGE PMD, and it
  222. * translates to a valid PTE, branch to PTE_LABEL.
  223. *
  224. * We have to propagate the 4MB bit of the virtual address
  225. * because we are fabricating 8MB pages using 4MB hw pages.
  226. */
  227. #if defined(CONFIG_HUGETLB_PAGE) || defined(CONFIG_TRANSPARENT_HUGEPAGE)
  228. #define USER_PGTABLE_CHECK_PMD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, PTE_LABEL) \
  229. brz,pn REG1, FAIL_LABEL; \
  230. sethi %uhi(_PAGE_PMD_HUGE), REG2; \
  231. sllx REG2, 32, REG2; \
  232. andcc REG1, REG2, %g0; \
  233. be,pt %xcc, 700f; \
  234. sethi %hi(4 * 1024 * 1024), REG2; \
  235. brgez,pn REG1, FAIL_LABEL; \
  236. andn REG1, REG2, REG1; \
  237. and VADDR, REG2, REG2; \
  238. brlz,pt REG1, PTE_LABEL; \
  239. or REG1, REG2, REG1; \
  240. 700:
  241. #else
  242. #define USER_PGTABLE_CHECK_PMD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, PTE_LABEL) \
  243. brz,pn REG1, FAIL_LABEL; \
  244. nop;
  245. #endif
  246. /* Do a user page table walk in MMU globals. Leaves final,
  247. * valid, PTE value in REG1. Jumps to FAIL_LABEL on early
  248. * page table walk termination or if the PTE is not valid.
  249. *
  250. * Physical base of page tables is in PHYS_PGD which will not
  251. * be modified.
  252. *
  253. * VADDR will not be clobbered, but REG1 and REG2 will.
  254. */
  255. #define USER_PGTABLE_WALK_TL1(VADDR, PHYS_PGD, REG1, REG2, FAIL_LABEL) \
  256. sllx VADDR, 64 - (PGDIR_SHIFT + PGDIR_BITS), REG2; \
  257. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  258. andn REG2, 0x7, REG2; \
  259. ldxa [PHYS_PGD + REG2] ASI_PHYS_USE_EC, REG1; \
  260. brz,pn REG1, FAIL_LABEL; \
  261. sllx VADDR, 64 - (PUD_SHIFT + PUD_BITS), REG2; \
  262. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  263. andn REG2, 0x7, REG2; \
  264. ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  265. USER_PGTABLE_CHECK_PUD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, 800f) \
  266. brz,pn REG1, FAIL_LABEL; \
  267. sllx VADDR, 64 - (PMD_SHIFT + PMD_BITS), REG2; \
  268. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  269. andn REG2, 0x7, REG2; \
  270. ldxa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  271. USER_PGTABLE_CHECK_PMD_HUGE(VADDR, REG1, REG2, FAIL_LABEL, 800f) \
  272. sllx VADDR, 64 - PMD_SHIFT, REG2; \
  273. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  274. andn REG2, 0x7, REG2; \
  275. add REG1, REG2, REG1; \
  276. ldxa [REG1] ASI_PHYS_USE_EC, REG1; \
  277. brgez,pn REG1, FAIL_LABEL; \
  278. nop; \
  279. 800:
  280. /* Lookup a OBP mapping on VADDR in the prom_trans[] table at TL>0.
  281. * If no entry is found, FAIL_LABEL will be branched to. On success
  282. * the resulting PTE value will be left in REG1. VADDR is preserved
  283. * by this routine.
  284. */
  285. #define OBP_TRANS_LOOKUP(VADDR, REG1, REG2, REG3, FAIL_LABEL) \
  286. sethi %hi(prom_trans), REG1; \
  287. or REG1, %lo(prom_trans), REG1; \
  288. 97: ldx [REG1 + 0x00], REG2; \
  289. brz,pn REG2, FAIL_LABEL; \
  290. nop; \
  291. ldx [REG1 + 0x08], REG3; \
  292. add REG2, REG3, REG3; \
  293. cmp REG2, VADDR; \
  294. bgu,pt %xcc, 98f; \
  295. cmp VADDR, REG3; \
  296. bgeu,pt %xcc, 98f; \
  297. ldx [REG1 + 0x10], REG3; \
  298. sub VADDR, REG2, REG2; \
  299. ba,pt %xcc, 99f; \
  300. add REG3, REG2, REG1; \
  301. 98: ba,pt %xcc, 97b; \
  302. add REG1, (3 * 8), REG1; \
  303. 99:
  304. /* We use a 32K TSB for the whole kernel, this allows to
  305. * handle about 16MB of modules and vmalloc mappings without
  306. * incurring many hash conflicts.
  307. */
  308. #define KERNEL_TSB_SIZE_BYTES (32 * 1024)
  309. #define KERNEL_TSB_NENTRIES \
  310. (KERNEL_TSB_SIZE_BYTES / 16)
  311. #define KERNEL_TSB4M_NENTRIES 4096
  312. /* Do a kernel TSB lookup at tl>0 on VADDR+TAG, branch to OK_LABEL
  313. * on TSB hit. REG1, REG2, REG3, and REG4 are used as temporaries
  314. * and the found TTE will be left in REG1. REG3 and REG4 must
  315. * be an even/odd pair of registers.
  316. *
  317. * VADDR and TAG will be preserved and not clobbered by this macro.
  318. */
  319. #define KERN_TSB_LOOKUP_TL1(VADDR, TAG, REG1, REG2, REG3, REG4, OK_LABEL) \
  320. 661: sethi %uhi(swapper_tsb), REG1; \
  321. sethi %hi(swapper_tsb), REG2; \
  322. or REG1, %ulo(swapper_tsb), REG1; \
  323. or REG2, %lo(swapper_tsb), REG2; \
  324. .section .swapper_tsb_phys_patch, "ax"; \
  325. .word 661b; \
  326. .previous; \
  327. sllx REG1, 32, REG1; \
  328. or REG1, REG2, REG1; \
  329. srlx VADDR, PAGE_SHIFT, REG2; \
  330. and REG2, (KERNEL_TSB_NENTRIES - 1), REG2; \
  331. sllx REG2, 4, REG2; \
  332. add REG1, REG2, REG2; \
  333. TSB_LOAD_QUAD(REG2, REG3); \
  334. cmp REG3, TAG; \
  335. be,a,pt %xcc, OK_LABEL; \
  336. mov REG4, REG1;
  337. #ifndef CONFIG_DEBUG_PAGEALLOC
  338. /* This version uses a trick, the TAG is already (VADDR >> 22) so
  339. * we can make use of that for the index computation.
  340. */
  341. #define KERN_TSB4M_LOOKUP_TL1(TAG, REG1, REG2, REG3, REG4, OK_LABEL) \
  342. 661: sethi %uhi(swapper_4m_tsb), REG1; \
  343. sethi %hi(swapper_4m_tsb), REG2; \
  344. or REG1, %ulo(swapper_4m_tsb), REG1; \
  345. or REG2, %lo(swapper_4m_tsb), REG2; \
  346. .section .swapper_4m_tsb_phys_patch, "ax"; \
  347. .word 661b; \
  348. .previous; \
  349. sllx REG1, 32, REG1; \
  350. or REG1, REG2, REG1; \
  351. and TAG, (KERNEL_TSB4M_NENTRIES - 1), REG2; \
  352. sllx REG2, 4, REG2; \
  353. add REG1, REG2, REG2; \
  354. TSB_LOAD_QUAD(REG2, REG3); \
  355. cmp REG3, TAG; \
  356. be,a,pt %xcc, OK_LABEL; \
  357. mov REG4, REG1;
  358. #endif
  359. #endif /* !(_SPARC64_TSB_H) */