mmu_context.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * include/asm-xtensa/mmu_context.h
  3. *
  4. * Switch an MMU context.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. */
  12. #ifndef _XTENSA_MMU_CONTEXT_H
  13. #define _XTENSA_MMU_CONTEXT_H
  14. #include <linux/stringify.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/tlbflush.h>
  19. /*
  20. * Linux was ported to Xtensa assuming all auto-refill ways in set 0
  21. * had the same properties (a very likely assumption). Multiple sets
  22. * of auto-refill ways will still work properly, but not as optimally
  23. * as the Xtensa designer may have assumed.
  24. *
  25. * We make this case a hard #error, killing the kernel build, to alert
  26. * the developer to this condition (which is more likely an error).
  27. * You super-duper clever developers can change it to a warning or
  28. * remove it altogether if you think you know what you're doing. :)
  29. */
  30. #if (XCHAL_HAVE_TLBS != 1)
  31. # error "Linux must have an MMU!"
  32. #endif
  33. #if ((XCHAL_ITLB_ARF_WAYS == 0) || (XCHAL_DTLB_ARF_WAYS == 0))
  34. # error "MMU must have auto-refill ways"
  35. #endif
  36. #if ((XCHAL_ITLB_ARF_SETS != 1) || (XCHAL_DTLB_ARF_SETS != 1))
  37. # error Linux may not use all auto-refill ways as efficiently as you think
  38. #endif
  39. #if (XCHAL_MMU_MAX_PTE_PAGE_SIZE != XCHAL_MMU_MIN_PTE_PAGE_SIZE)
  40. # error Only one page size allowed!
  41. #endif
  42. extern unsigned long asid_cache;
  43. extern pgd_t *current_pgd;
  44. /*
  45. * Define the number of entries per auto-refill way in set 0 of both I and D
  46. * TLBs. We deal only with set 0 here (an assumption further explained in
  47. * assertions.h). Also, define the total number of ARF entries in both TLBs.
  48. */
  49. #define ITLB_ENTRIES_PER_ARF_WAY (XCHAL_ITLB_SET(XCHAL_ITLB_ARF_SET0,ENTRIES))
  50. #define DTLB_ENTRIES_PER_ARF_WAY (XCHAL_DTLB_SET(XCHAL_DTLB_ARF_SET0,ENTRIES))
  51. #define ITLB_ENTRIES \
  52. (ITLB_ENTRIES_PER_ARF_WAY * (XCHAL_ITLB_SET(XCHAL_ITLB_ARF_SET0,WAYS)))
  53. #define DTLB_ENTRIES \
  54. (DTLB_ENTRIES_PER_ARF_WAY * (XCHAL_DTLB_SET(XCHAL_DTLB_ARF_SET0,WAYS)))
  55. /*
  56. * SMALLEST_NTLB_ENTRIES is the smaller of ITLB_ENTRIES and DTLB_ENTRIES.
  57. * In practice, they are probably equal. This macro simplifies function
  58. * flush_tlb_range().
  59. */
  60. #if (DTLB_ENTRIES < ITLB_ENTRIES)
  61. # define SMALLEST_NTLB_ENTRIES DTLB_ENTRIES
  62. #else
  63. # define SMALLEST_NTLB_ENTRIES ITLB_ENTRIES
  64. #endif
  65. /*
  66. * asid_cache tracks only the ASID[USER_RING] field of the RASID special
  67. * register, which is the current user-task asid allocation value.
  68. * mm->context has the same meaning. When it comes time to write the
  69. * asid_cache or mm->context values to the RASID special register, we first
  70. * shift the value left by 8, then insert the value.
  71. * ASID[0] always contains the kernel's asid value, and we reserve three
  72. * other asid values that we never assign to user tasks.
  73. */
  74. #define ASID_INC 0x1
  75. #define ASID_MASK ((1 << XCHAL_MMU_ASID_BITS) - 1)
  76. /*
  77. * XCHAL_MMU_ASID_INVALID is a configurable Xtensa processor constant
  78. * indicating invalid address space. XCHAL_MMU_ASID_KERNEL is a configurable
  79. * Xtensa processor constant indicating the kernel address space. They can
  80. * be arbitrary values.
  81. *
  82. * We identify three more unique, reserved ASID values to use in the unused
  83. * ring positions. No other user process will be assigned these reserved
  84. * ASID values.
  85. *
  86. * For example, given that
  87. *
  88. * XCHAL_MMU_ASID_INVALID == 0
  89. * XCHAL_MMU_ASID_KERNEL == 1
  90. *
  91. * the following maze of #if statements would generate
  92. *
  93. * ASID_RESERVED_1 == 2
  94. * ASID_RESERVED_2 == 3
  95. * ASID_RESERVED_3 == 4
  96. * ASID_FIRST_NONRESERVED == 5
  97. */
  98. #if (XCHAL_MMU_ASID_INVALID != XCHAL_MMU_ASID_KERNEL + 1)
  99. # define ASID_RESERVED_1 ((XCHAL_MMU_ASID_KERNEL + 1) & ASID_MASK)
  100. #else
  101. # define ASID_RESERVED_1 ((XCHAL_MMU_ASID_KERNEL + 2) & ASID_MASK)
  102. #endif
  103. #if (XCHAL_MMU_ASID_INVALID != ASID_RESERVED_1 + 1)
  104. # define ASID_RESERVED_2 ((ASID_RESERVED_1 + 1) & ASID_MASK)
  105. #else
  106. # define ASID_RESERVED_2 ((ASID_RESERVED_1 + 2) & ASID_MASK)
  107. #endif
  108. #if (XCHAL_MMU_ASID_INVALID != ASID_RESERVED_2 + 1)
  109. # define ASID_RESERVED_3 ((ASID_RESERVED_2 + 1) & ASID_MASK)
  110. #else
  111. # define ASID_RESERVED_3 ((ASID_RESERVED_2 + 2) & ASID_MASK)
  112. #endif
  113. #if (XCHAL_MMU_ASID_INVALID != ASID_RESERVED_3 + 1)
  114. # define ASID_FIRST_NONRESERVED ((ASID_RESERVED_3 + 1) & ASID_MASK)
  115. #else
  116. # define ASID_FIRST_NONRESERVED ((ASID_RESERVED_3 + 2) & ASID_MASK)
  117. #endif
  118. #define ASID_ALL_RESERVED ( ((ASID_RESERVED_1) << 24) + \
  119. ((ASID_RESERVED_2) << 16) + \
  120. ((ASID_RESERVED_3) << 8) + \
  121. ((XCHAL_MMU_ASID_KERNEL)) )
  122. /*
  123. * NO_CONTEXT is the invalid ASID value that we don't ever assign to
  124. * any user or kernel context. NO_CONTEXT is a better mnemonic than
  125. * XCHAL_MMU_ASID_INVALID, so we use it in code instead.
  126. */
  127. #define NO_CONTEXT XCHAL_MMU_ASID_INVALID
  128. #if (KERNEL_RING != 0)
  129. # error The KERNEL_RING really should be zero.
  130. #endif
  131. #if (USER_RING >= XCHAL_MMU_RINGS)
  132. # error USER_RING cannot be greater than the highest numbered ring.
  133. #endif
  134. #if (USER_RING == KERNEL_RING)
  135. # error The user and kernel rings really should not be equal.
  136. #endif
  137. #if (USER_RING == 1)
  138. #define ASID_INSERT(x) ( ((ASID_RESERVED_1) << 24) + \
  139. ((ASID_RESERVED_2) << 16) + \
  140. (((x) & (ASID_MASK)) << 8) + \
  141. ((XCHAL_MMU_ASID_KERNEL)) )
  142. #elif (USER_RING == 2)
  143. #define ASID_INSERT(x) ( ((ASID_RESERVED_1) << 24) + \
  144. (((x) & (ASID_MASK)) << 16) + \
  145. ((ASID_RESERVED_2) << 8) + \
  146. ((XCHAL_MMU_ASID_KERNEL)) )
  147. #elif (USER_RING == 3)
  148. #define ASID_INSERT(x) ( (((x) & (ASID_MASK)) << 24) + \
  149. ((ASID_RESERVED_1) << 16) + \
  150. ((ASID_RESERVED_2) << 8) + \
  151. ((XCHAL_MMU_ASID_KERNEL)) )
  152. #else
  153. #error Goofy value for USER_RING
  154. #endif /* USER_RING == 1 */
  155. /*
  156. * All unused by hardware upper bits will be considered
  157. * as a software asid extension.
  158. */
  159. #define ASID_VERSION_MASK ((unsigned long)~(ASID_MASK|(ASID_MASK-1)))
  160. #define ASID_FIRST_VERSION \
  161. ((unsigned long)(~ASID_VERSION_MASK) + 1 + ASID_FIRST_NONRESERVED)
  162. static inline void set_rasid_register (unsigned long val)
  163. {
  164. __asm__ __volatile__ (" wsr %0, "__stringify(RASID)"\n\t"
  165. " isync\n" : : "a" (val));
  166. }
  167. static inline unsigned long get_rasid_register (void)
  168. {
  169. unsigned long tmp;
  170. __asm__ __volatile__ (" rsr %0, "__stringify(RASID)"\n\t" : "=a" (tmp));
  171. return tmp;
  172. }
  173. #if ((XCHAL_MMU_ASID_INVALID == 0) && (XCHAL_MMU_ASID_KERNEL == 1))
  174. static inline void
  175. get_new_mmu_context(struct mm_struct *mm, unsigned long asid)
  176. {
  177. extern void flush_tlb_all(void);
  178. if (! ((asid += ASID_INC) & ASID_MASK) ) {
  179. flush_tlb_all(); /* start new asid cycle */
  180. if (!asid) /* fix version if needed */
  181. asid = ASID_FIRST_VERSION - ASID_FIRST_NONRESERVED;
  182. asid += ASID_FIRST_NONRESERVED;
  183. }
  184. mm->context = asid_cache = asid;
  185. }
  186. #else
  187. #warning ASID_{INVALID,KERNEL} values impose non-optimal get_new_mmu_context implementation
  188. /* XCHAL_MMU_ASID_INVALID == 0 and XCHAL_MMU_ASID_KERNEL ==1 are
  189. really the best, but if you insist... */
  190. static inline int validate_asid (unsigned long asid)
  191. {
  192. switch (asid) {
  193. case XCHAL_MMU_ASID_INVALID:
  194. case XCHAL_MMU_ASID_KERNEL:
  195. case ASID_RESERVED_1:
  196. case ASID_RESERVED_2:
  197. case ASID_RESERVED_3:
  198. return 0; /* can't use these values as ASIDs */
  199. }
  200. return 1; /* valid */
  201. }
  202. static inline void
  203. get_new_mmu_context(struct mm_struct *mm, unsigned long asid)
  204. {
  205. extern void flush_tlb_all(void);
  206. while (1) {
  207. asid += ASID_INC;
  208. if ( ! (asid & ASID_MASK) ) {
  209. flush_tlb_all(); /* start new asid cycle */
  210. if (!asid) /* fix version if needed */
  211. asid = ASID_FIRST_VERSION - ASID_FIRST_NONRESERVED;
  212. asid += ASID_FIRST_NONRESERVED;
  213. break; /* no need to validate here */
  214. }
  215. if (validate_asid (asid & ASID_MASK))
  216. break;
  217. }
  218. mm->context = asid_cache = asid;
  219. }
  220. #endif
  221. /*
  222. * Initialize the context related info for a new mm_struct
  223. * instance.
  224. */
  225. static inline int
  226. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  227. {
  228. mm->context = NO_CONTEXT;
  229. return 0;
  230. }
  231. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  232. struct task_struct *tsk)
  233. {
  234. unsigned long asid = asid_cache;
  235. /* Check if our ASID is of an older version and thus invalid */
  236. if ((next->context ^ asid) & ASID_VERSION_MASK)
  237. get_new_mmu_context(next, asid);
  238. set_rasid_register (ASID_INSERT(next->context));
  239. invalidate_page_directory();
  240. }
  241. #define deactivate_mm(tsk, mm) do { } while(0)
  242. /*
  243. * Destroy context related info for an mm_struct that is about
  244. * to be put to rest.
  245. */
  246. static inline void destroy_context(struct mm_struct *mm)
  247. {
  248. /* Nothing to do. */
  249. }
  250. /*
  251. * After we have set current->mm to a new value, this activates
  252. * the context for the new mm so we see the new mappings.
  253. */
  254. static inline void
  255. activate_mm(struct mm_struct *prev, struct mm_struct *next)
  256. {
  257. /* Unconditionally get a new ASID. */
  258. get_new_mmu_context(next, asid_cache);
  259. set_rasid_register (ASID_INSERT(next->context));
  260. invalidate_page_directory();
  261. }
  262. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  263. {
  264. /* Nothing to do. */
  265. }
  266. #endif /* _XTENSA_MMU_CONTEXT_H */