tsb.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef _SPARC64_TSB_H
  2. #define _SPARC64_TSB_H
  3. /* The sparc64 TSB is similar to the powerpc hashtables. It's a
  4. * power-of-2 sized table of TAG/PTE pairs. The cpu precomputes
  5. * pointers into this table for 8K and 64K page sizes, and also a
  6. * comparison TAG based upon the virtual address and context which
  7. * faults.
  8. *
  9. * TLB miss trap handler software does the actual lookup via something
  10. * of the form:
  11. *
  12. * ldxa [%g0] ASI_{D,I}MMU_TSB_8KB_PTR, %g1
  13. * ldxa [%g0] ASI_{D,I}MMU, %g6
  14. * sllx %g6, 22, %g6
  15. * srlx %g6, 22, %g6
  16. * ldda [%g1] ASI_NUCLEUS_QUAD_LDD, %g4
  17. * cmp %g4, %g6
  18. * bne,pn %xcc, tsb_miss_{d,i}tlb
  19. * mov FAULT_CODE_{D,I}TLB, %g3
  20. * stxa %g5, [%g0] ASI_{D,I}TLB_DATA_IN
  21. * retry
  22. *
  23. *
  24. * Each 16-byte slot of the TSB is the 8-byte tag and then the 8-byte
  25. * PTE. The TAG is of the same layout as the TLB TAG TARGET mmu
  26. * register which is:
  27. *
  28. * -------------------------------------------------
  29. * | - | CONTEXT | - | VADDR bits 63:22 |
  30. * -------------------------------------------------
  31. * 63 61 60 48 47 42 41 0
  32. *
  33. * But actually, since we use per-mm TSB's, we zero out the CONTEXT
  34. * field.
  35. *
  36. * Like the powerpc hashtables we need to use locking in order to
  37. * synchronize while we update the entries. PTE updates need locking
  38. * as well.
  39. *
  40. * We need to carefully choose a lock bits for the TSB entry. We
  41. * choose to use bit 47 in the tag. Also, since we never map anything
  42. * at page zero in context zero, we use zero as an invalid tag entry.
  43. * When the lock bit is set, this forces a tag comparison failure.
  44. */
  45. #define TSB_TAG_LOCK_BIT 47
  46. #define TSB_TAG_LOCK_HIGH (1 << (TSB_TAG_LOCK_BIT - 32))
  47. #define TSB_TAG_INVALID_BIT 46
  48. #define TSB_TAG_INVALID_HIGH (1 << (TSB_TAG_INVALID_BIT - 32))
  49. /* Some cpus support physical address quad loads. We want to use
  50. * those if possible so we don't need to hard-lock the TSB mapping
  51. * into the TLB. We encode some instruction patching in order to
  52. * support this.
  53. *
  54. * The kernel TSB is locked into the TLB by virtue of being in the
  55. * kernel image, so we don't play these games for swapper_tsb access.
  56. */
  57. #ifndef __ASSEMBLY__
  58. struct tsb_ldquad_phys_patch_entry {
  59. unsigned int addr;
  60. unsigned int sun4u_insn;
  61. unsigned int sun4v_insn;
  62. };
  63. extern struct tsb_ldquad_phys_patch_entry __tsb_ldquad_phys_patch,
  64. __tsb_ldquad_phys_patch_end;
  65. struct tsb_phys_patch_entry {
  66. unsigned int addr;
  67. unsigned int insn;
  68. };
  69. extern struct tsb_phys_patch_entry __tsb_phys_patch, __tsb_phys_patch_end;
  70. #endif
  71. #define TSB_LOAD_QUAD(TSB, REG) \
  72. 661: ldda [TSB] ASI_NUCLEUS_QUAD_LDD, REG; \
  73. .section .tsb_ldquad_phys_patch, "ax"; \
  74. .word 661b; \
  75. ldda [TSB] ASI_QUAD_LDD_PHYS, REG; \
  76. ldda [TSB] ASI_QUAD_LDD_PHYS_4V, REG; \
  77. .previous
  78. #define TSB_LOAD_TAG_HIGH(TSB, REG) \
  79. 661: lduwa [TSB] ASI_N, REG; \
  80. .section .tsb_phys_patch, "ax"; \
  81. .word 661b; \
  82. lduwa [TSB] ASI_PHYS_USE_EC, REG; \
  83. .previous
  84. #define TSB_LOAD_TAG(TSB, REG) \
  85. 661: ldxa [TSB] ASI_N, REG; \
  86. .section .tsb_phys_patch, "ax"; \
  87. .word 661b; \
  88. ldxa [TSB] ASI_PHYS_USE_EC, REG; \
  89. .previous
  90. #define TSB_CAS_TAG_HIGH(TSB, REG1, REG2) \
  91. 661: casa [TSB] ASI_N, REG1, REG2; \
  92. .section .tsb_phys_patch, "ax"; \
  93. .word 661b; \
  94. casa [TSB] ASI_PHYS_USE_EC, REG1, REG2; \
  95. .previous
  96. #define TSB_CAS_TAG(TSB, REG1, REG2) \
  97. 661: casxa [TSB] ASI_N, REG1, REG2; \
  98. .section .tsb_phys_patch, "ax"; \
  99. .word 661b; \
  100. casxa [TSB] ASI_PHYS_USE_EC, REG1, REG2; \
  101. .previous
  102. #define TSB_STORE(ADDR, VAL) \
  103. 661: stxa VAL, [ADDR] ASI_N; \
  104. .section .tsb_phys_patch, "ax"; \
  105. .word 661b; \
  106. stxa VAL, [ADDR] ASI_PHYS_USE_EC; \
  107. .previous
  108. #define TSB_LOCK_TAG(TSB, REG1, REG2) \
  109. 99: TSB_LOAD_TAG_HIGH(TSB, REG1); \
  110. sethi %hi(TSB_TAG_LOCK_HIGH), REG2;\
  111. andcc REG1, REG2, %g0; \
  112. bne,pn %icc, 99b; \
  113. nop; \
  114. TSB_CAS_TAG_HIGH(TSB, REG1, REG2); \
  115. cmp REG1, REG2; \
  116. bne,pn %icc, 99b; \
  117. nop; \
  118. #define TSB_WRITE(TSB, TTE, TAG) \
  119. add TSB, 0x8, TSB; \
  120. TSB_STORE(TSB, TTE); \
  121. sub TSB, 0x8, TSB; \
  122. TSB_STORE(TSB, TAG);
  123. #define KTSB_LOAD_QUAD(TSB, REG) \
  124. ldda [TSB] ASI_NUCLEUS_QUAD_LDD, REG;
  125. #define KTSB_STORE(ADDR, VAL) \
  126. stxa VAL, [ADDR] ASI_N;
  127. #define KTSB_LOCK_TAG(TSB, REG1, REG2) \
  128. 99: lduwa [TSB] ASI_N, REG1; \
  129. sethi %hi(TSB_TAG_LOCK_HIGH), REG2;\
  130. andcc REG1, REG2, %g0; \
  131. bne,pn %icc, 99b; \
  132. nop; \
  133. casa [TSB] ASI_N, REG1, REG2;\
  134. cmp REG1, REG2; \
  135. bne,pn %icc, 99b; \
  136. nop; \
  137. #define KTSB_WRITE(TSB, TTE, TAG) \
  138. add TSB, 0x8, TSB; \
  139. stxa TTE, [TSB] ASI_N; \
  140. sub TSB, 0x8, TSB; \
  141. stxa TAG, [TSB] ASI_N;
  142. /* Do a kernel page table walk. Leaves physical PTE pointer in
  143. * REG1. Jumps to FAIL_LABEL on early page table walk termination.
  144. * VADDR will not be clobbered, but REG2 will.
  145. */
  146. #define KERN_PGTABLE_WALK(VADDR, REG1, REG2, FAIL_LABEL) \
  147. sethi %hi(swapper_pg_dir), REG1; \
  148. or REG1, %lo(swapper_pg_dir), REG1; \
  149. sllx VADDR, 64 - (PGDIR_SHIFT + PGDIR_BITS), REG2; \
  150. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  151. andn REG2, 0x3, REG2; \
  152. lduw [REG1 + REG2], REG1; \
  153. brz,pn REG1, FAIL_LABEL; \
  154. sllx VADDR, 64 - (PMD_SHIFT + PMD_BITS), REG2; \
  155. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  156. sllx REG1, 11, REG1; \
  157. andn REG2, 0x3, REG2; \
  158. lduwa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  159. brz,pn REG1, FAIL_LABEL; \
  160. sllx VADDR, 64 - PMD_SHIFT, REG2; \
  161. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  162. sllx REG1, 11, REG1; \
  163. andn REG2, 0x7, REG2; \
  164. add REG1, REG2, REG1;
  165. /* Do a user page table walk in MMU globals. Leaves physical PTE
  166. * pointer in REG1. Jumps to FAIL_LABEL on early page table walk
  167. * termination. Physical base of page tables is in PHYS_PGD which
  168. * will not be modified.
  169. *
  170. * VADDR will not be clobbered, but REG1 and REG2 will.
  171. */
  172. #define USER_PGTABLE_WALK_TL1(VADDR, PHYS_PGD, REG1, REG2, FAIL_LABEL) \
  173. sllx VADDR, 64 - (PGDIR_SHIFT + PGDIR_BITS), REG2; \
  174. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  175. andn REG2, 0x3, REG2; \
  176. lduwa [PHYS_PGD + REG2] ASI_PHYS_USE_EC, REG1; \
  177. brz,pn REG1, FAIL_LABEL; \
  178. sllx VADDR, 64 - (PMD_SHIFT + PMD_BITS), REG2; \
  179. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  180. sllx REG1, 11, REG1; \
  181. andn REG2, 0x3, REG2; \
  182. lduwa [REG1 + REG2] ASI_PHYS_USE_EC, REG1; \
  183. brz,pn REG1, FAIL_LABEL; \
  184. sllx VADDR, 64 - PMD_SHIFT, REG2; \
  185. srlx REG2, 64 - PAGE_SHIFT, REG2; \
  186. sllx REG1, 11, REG1; \
  187. andn REG2, 0x7, REG2; \
  188. add REG1, REG2, REG1;
  189. /* Lookup a OBP mapping on VADDR in the prom_trans[] table at TL>0.
  190. * If no entry is found, FAIL_LABEL will be branched to. On success
  191. * the resulting PTE value will be left in REG1. VADDR is preserved
  192. * by this routine.
  193. */
  194. #define OBP_TRANS_LOOKUP(VADDR, REG1, REG2, REG3, FAIL_LABEL) \
  195. sethi %hi(prom_trans), REG1; \
  196. or REG1, %lo(prom_trans), REG1; \
  197. 97: ldx [REG1 + 0x00], REG2; \
  198. brz,pn REG2, FAIL_LABEL; \
  199. nop; \
  200. ldx [REG1 + 0x08], REG3; \
  201. add REG2, REG3, REG3; \
  202. cmp REG2, VADDR; \
  203. bgu,pt %xcc, 98f; \
  204. cmp VADDR, REG3; \
  205. bgeu,pt %xcc, 98f; \
  206. ldx [REG1 + 0x10], REG3; \
  207. sub VADDR, REG2, REG2; \
  208. ba,pt %xcc, 99f; \
  209. add REG3, REG2, REG1; \
  210. 98: ba,pt %xcc, 97b; \
  211. add REG1, (3 * 8), REG1; \
  212. 99:
  213. /* We use a 32K TSB for the whole kernel, this allows to
  214. * handle about 16MB of modules and vmalloc mappings without
  215. * incurring many hash conflicts.
  216. */
  217. #define KERNEL_TSB_SIZE_BYTES (32 * 1024)
  218. #define KERNEL_TSB_NENTRIES \
  219. (KERNEL_TSB_SIZE_BYTES / 16)
  220. #define KERNEL_TSB4M_NENTRIES 4096
  221. /* Do a kernel TSB lookup at tl>0 on VADDR+TAG, branch to OK_LABEL
  222. * on TSB hit. REG1, REG2, REG3, and REG4 are used as temporaries
  223. * and the found TTE will be left in REG1. REG3 and REG4 must
  224. * be an even/odd pair of registers.
  225. *
  226. * VADDR and TAG will be preserved and not clobbered by this macro.
  227. */
  228. #define KERN_TSB_LOOKUP_TL1(VADDR, TAG, REG1, REG2, REG3, REG4, OK_LABEL) \
  229. sethi %hi(swapper_tsb), REG1; \
  230. or REG1, %lo(swapper_tsb), REG1; \
  231. srlx VADDR, PAGE_SHIFT, REG2; \
  232. and REG2, (KERNEL_TSB_NENTRIES - 1), REG2; \
  233. sllx REG2, 4, REG2; \
  234. add REG1, REG2, REG2; \
  235. KTSB_LOAD_QUAD(REG2, REG3); \
  236. cmp REG3, TAG; \
  237. be,a,pt %xcc, OK_LABEL; \
  238. mov REG4, REG1;
  239. #ifndef CONFIG_DEBUG_PAGEALLOC
  240. /* This version uses a trick, the TAG is already (VADDR >> 22) so
  241. * we can make use of that for the index computation.
  242. */
  243. #define KERN_TSB4M_LOOKUP_TL1(TAG, REG1, REG2, REG3, REG4, OK_LABEL) \
  244. sethi %hi(swapper_4m_tsb), REG1; \
  245. or REG1, %lo(swapper_4m_tsb), REG1; \
  246. and TAG, (KERNEL_TSB4M_NENTRIES - 1), REG2; \
  247. sllx REG2, 4, REG2; \
  248. add REG1, REG2, REG2; \
  249. KTSB_LOAD_QUAD(REG2, REG3); \
  250. cmp REG3, TAG; \
  251. be,a,pt %xcc, OK_LABEL; \
  252. mov REG4, REG1;
  253. #endif
  254. #endif /* !(_SPARC64_TSB_H) */