tsb.h 7.8 KB

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