tlbflush.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * linux/include/asm-arm/tlbflush.h
  3. *
  4. * Copyright (C) 1999-2003 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #ifndef _ASMARM_TLBFLUSH_H
  11. #define _ASMARM_TLBFLUSH_H
  12. #ifndef CONFIG_MMU
  13. #define tlb_flush(tlb) ((void) tlb)
  14. #else /* CONFIG_MMU */
  15. #include <asm/glue.h>
  16. #define TLB_V3_PAGE (1 << 0)
  17. #define TLB_V4_U_PAGE (1 << 1)
  18. #define TLB_V4_D_PAGE (1 << 2)
  19. #define TLB_V4_I_PAGE (1 << 3)
  20. #define TLB_V6_U_PAGE (1 << 4)
  21. #define TLB_V6_D_PAGE (1 << 5)
  22. #define TLB_V6_I_PAGE (1 << 6)
  23. #define TLB_V3_FULL (1 << 8)
  24. #define TLB_V4_U_FULL (1 << 9)
  25. #define TLB_V4_D_FULL (1 << 10)
  26. #define TLB_V4_I_FULL (1 << 11)
  27. #define TLB_V6_U_FULL (1 << 12)
  28. #define TLB_V6_D_FULL (1 << 13)
  29. #define TLB_V6_I_FULL (1 << 14)
  30. #define TLB_V6_U_ASID (1 << 16)
  31. #define TLB_V6_D_ASID (1 << 17)
  32. #define TLB_V6_I_ASID (1 << 18)
  33. #define TLB_L2CLEAN_FR (1 << 29) /* Feroceon */
  34. #define TLB_DCLEAN (1 << 30)
  35. #define TLB_WB (1 << 31)
  36. /*
  37. * MMU TLB Model
  38. * =============
  39. *
  40. * We have the following to choose from:
  41. * v3 - ARMv3
  42. * v4 - ARMv4 without write buffer
  43. * v4wb - ARMv4 with write buffer without I TLB flush entry instruction
  44. * v4wbi - ARMv4 with write buffer with I TLB flush entry instruction
  45. * fr - Feroceon (v4wbi with non-outer-cacheable page table walks)
  46. * v6wbi - ARMv6 with write buffer with I TLB flush entry instruction
  47. */
  48. #undef _TLB
  49. #undef MULTI_TLB
  50. #define v3_tlb_flags (TLB_V3_FULL | TLB_V3_PAGE)
  51. #ifdef CONFIG_CPU_TLB_V3
  52. # define v3_possible_flags v3_tlb_flags
  53. # define v3_always_flags v3_tlb_flags
  54. # ifdef _TLB
  55. # define MULTI_TLB 1
  56. # else
  57. # define _TLB v3
  58. # endif
  59. #else
  60. # define v3_possible_flags 0
  61. # define v3_always_flags (-1UL)
  62. #endif
  63. #define v4_tlb_flags (TLB_V4_U_FULL | TLB_V4_U_PAGE)
  64. #ifdef CONFIG_CPU_TLB_V4WT
  65. # define v4_possible_flags v4_tlb_flags
  66. # define v4_always_flags v4_tlb_flags
  67. # ifdef _TLB
  68. # define MULTI_TLB 1
  69. # else
  70. # define _TLB v4
  71. # endif
  72. #else
  73. # define v4_possible_flags 0
  74. # define v4_always_flags (-1UL)
  75. #endif
  76. #define v4wbi_tlb_flags (TLB_WB | TLB_DCLEAN | \
  77. TLB_V4_I_FULL | TLB_V4_D_FULL | \
  78. TLB_V4_I_PAGE | TLB_V4_D_PAGE)
  79. #ifdef CONFIG_CPU_TLB_V4WBI
  80. # define v4wbi_possible_flags v4wbi_tlb_flags
  81. # define v4wbi_always_flags v4wbi_tlb_flags
  82. # ifdef _TLB
  83. # define MULTI_TLB 1
  84. # else
  85. # define _TLB v4wbi
  86. # endif
  87. #else
  88. # define v4wbi_possible_flags 0
  89. # define v4wbi_always_flags (-1UL)
  90. #endif
  91. #define fr_tlb_flags (TLB_WB | TLB_DCLEAN | TLB_L2CLEAN_FR | \
  92. TLB_V4_I_FULL | TLB_V4_D_FULL | \
  93. TLB_V4_I_PAGE | TLB_V4_D_PAGE)
  94. #ifdef CONFIG_CPU_TLB_FEROCEON
  95. # define fr_possible_flags fr_tlb_flags
  96. # define fr_always_flags fr_tlb_flags
  97. # ifdef _TLB
  98. # define MULTI_TLB 1
  99. # else
  100. # define _TLB v4wbi
  101. # endif
  102. #else
  103. # define fr_possible_flags 0
  104. # define fr_always_flags (-1UL)
  105. #endif
  106. #define v4wb_tlb_flags (TLB_WB | TLB_DCLEAN | \
  107. TLB_V4_I_FULL | TLB_V4_D_FULL | \
  108. TLB_V4_D_PAGE)
  109. #ifdef CONFIG_CPU_TLB_V4WB
  110. # define v4wb_possible_flags v4wb_tlb_flags
  111. # define v4wb_always_flags v4wb_tlb_flags
  112. # ifdef _TLB
  113. # define MULTI_TLB 1
  114. # else
  115. # define _TLB v4wb
  116. # endif
  117. #else
  118. # define v4wb_possible_flags 0
  119. # define v4wb_always_flags (-1UL)
  120. #endif
  121. #define v6wbi_tlb_flags (TLB_WB | TLB_DCLEAN | \
  122. TLB_V6_I_FULL | TLB_V6_D_FULL | \
  123. TLB_V6_I_PAGE | TLB_V6_D_PAGE | \
  124. TLB_V6_I_ASID | TLB_V6_D_ASID)
  125. #ifdef CONFIG_CPU_TLB_V6
  126. # define v6wbi_possible_flags v6wbi_tlb_flags
  127. # define v6wbi_always_flags v6wbi_tlb_flags
  128. # ifdef _TLB
  129. # define MULTI_TLB 1
  130. # else
  131. # define _TLB v6wbi
  132. # endif
  133. #else
  134. # define v6wbi_possible_flags 0
  135. # define v6wbi_always_flags (-1UL)
  136. #endif
  137. #ifdef CONFIG_CPU_TLB_V7
  138. # define v7wbi_possible_flags v6wbi_tlb_flags
  139. # define v7wbi_always_flags v6wbi_tlb_flags
  140. # ifdef _TLB
  141. # define MULTI_TLB 1
  142. # else
  143. # define _TLB v7wbi
  144. # endif
  145. #else
  146. # define v7wbi_possible_flags 0
  147. # define v7wbi_always_flags (-1UL)
  148. #endif
  149. #ifndef _TLB
  150. #error Unknown TLB model
  151. #endif
  152. #ifndef __ASSEMBLY__
  153. #include <linux/sched.h>
  154. struct cpu_tlb_fns {
  155. void (*flush_user_range)(unsigned long, unsigned long, struct vm_area_struct *);
  156. void (*flush_kern_range)(unsigned long, unsigned long);
  157. unsigned long tlb_flags;
  158. };
  159. /*
  160. * Select the calling method
  161. */
  162. #ifdef MULTI_TLB
  163. #define __cpu_flush_user_tlb_range cpu_tlb.flush_user_range
  164. #define __cpu_flush_kern_tlb_range cpu_tlb.flush_kern_range
  165. #else
  166. #define __cpu_flush_user_tlb_range __glue(_TLB,_flush_user_tlb_range)
  167. #define __cpu_flush_kern_tlb_range __glue(_TLB,_flush_kern_tlb_range)
  168. extern void __cpu_flush_user_tlb_range(unsigned long, unsigned long, struct vm_area_struct *);
  169. extern void __cpu_flush_kern_tlb_range(unsigned long, unsigned long);
  170. #endif
  171. extern struct cpu_tlb_fns cpu_tlb;
  172. #define __cpu_tlb_flags cpu_tlb.tlb_flags
  173. /*
  174. * TLB Management
  175. * ==============
  176. *
  177. * The arch/arm/mm/tlb-*.S files implement these methods.
  178. *
  179. * The TLB specific code is expected to perform whatever tests it
  180. * needs to determine if it should invalidate the TLB for each
  181. * call. Start addresses are inclusive and end addresses are
  182. * exclusive; it is safe to round these addresses down.
  183. *
  184. * flush_tlb_all()
  185. *
  186. * Invalidate the entire TLB.
  187. *
  188. * flush_tlb_mm(mm)
  189. *
  190. * Invalidate all TLB entries in a particular address
  191. * space.
  192. * - mm - mm_struct describing address space
  193. *
  194. * flush_tlb_range(mm,start,end)
  195. *
  196. * Invalidate a range of TLB entries in the specified
  197. * address space.
  198. * - mm - mm_struct describing address space
  199. * - start - start address (may not be aligned)
  200. * - end - end address (exclusive, may not be aligned)
  201. *
  202. * flush_tlb_page(vaddr,vma)
  203. *
  204. * Invalidate the specified page in the specified address range.
  205. * - vaddr - virtual address (may not be aligned)
  206. * - vma - vma_struct describing address range
  207. *
  208. * flush_kern_tlb_page(kaddr)
  209. *
  210. * Invalidate the TLB entry for the specified page. The address
  211. * will be in the kernels virtual memory space. Current uses
  212. * only require the D-TLB to be invalidated.
  213. * - kaddr - Kernel virtual memory address
  214. */
  215. /*
  216. * We optimise the code below by:
  217. * - building a set of TLB flags that might be set in __cpu_tlb_flags
  218. * - building a set of TLB flags that will always be set in __cpu_tlb_flags
  219. * - if we're going to need __cpu_tlb_flags, access it once and only once
  220. *
  221. * This allows us to build optimal assembly for the single-CPU type case,
  222. * and as close to optimal given the compiler constrants for multi-CPU
  223. * case. We could do better for the multi-CPU case if the compiler
  224. * implemented the "%?" method, but this has been discontinued due to too
  225. * many people getting it wrong.
  226. */
  227. #define possible_tlb_flags (v3_possible_flags | \
  228. v4_possible_flags | \
  229. v4wbi_possible_flags | \
  230. fr_possible_flags | \
  231. v4wb_possible_flags | \
  232. v6wbi_possible_flags)
  233. #define always_tlb_flags (v3_always_flags & \
  234. v4_always_flags & \
  235. v4wbi_always_flags & \
  236. fr_always_flags & \
  237. v4wb_always_flags & \
  238. v6wbi_always_flags)
  239. #define tlb_flag(f) ((always_tlb_flags & (f)) || (__tlb_flag & possible_tlb_flags & (f)))
  240. static inline void local_flush_tlb_all(void)
  241. {
  242. const int zero = 0;
  243. const unsigned int __tlb_flag = __cpu_tlb_flags;
  244. if (tlb_flag(TLB_WB))
  245. dsb();
  246. if (tlb_flag(TLB_V3_FULL))
  247. asm("mcr p15, 0, %0, c6, c0, 0" : : "r" (zero) : "cc");
  248. if (tlb_flag(TLB_V4_U_FULL | TLB_V6_U_FULL))
  249. asm("mcr p15, 0, %0, c8, c7, 0" : : "r" (zero) : "cc");
  250. if (tlb_flag(TLB_V4_D_FULL | TLB_V6_D_FULL))
  251. asm("mcr p15, 0, %0, c8, c6, 0" : : "r" (zero) : "cc");
  252. if (tlb_flag(TLB_V4_I_FULL | TLB_V6_I_FULL))
  253. asm("mcr p15, 0, %0, c8, c5, 0" : : "r" (zero) : "cc");
  254. if (tlb_flag(TLB_V6_I_FULL | TLB_V6_D_FULL |
  255. TLB_V6_I_PAGE | TLB_V6_D_PAGE |
  256. TLB_V6_I_ASID | TLB_V6_D_ASID)) {
  257. /* flush the branch target cache */
  258. asm("mcr p15, 0, %0, c7, c5, 6" : : "r" (zero) : "cc");
  259. dsb();
  260. isb();
  261. }
  262. }
  263. static inline void local_flush_tlb_mm(struct mm_struct *mm)
  264. {
  265. const int zero = 0;
  266. const int asid = ASID(mm);
  267. const unsigned int __tlb_flag = __cpu_tlb_flags;
  268. if (tlb_flag(TLB_WB))
  269. dsb();
  270. if (cpu_isset(smp_processor_id(), mm->cpu_vm_mask)) {
  271. if (tlb_flag(TLB_V3_FULL))
  272. asm("mcr p15, 0, %0, c6, c0, 0" : : "r" (zero) : "cc");
  273. if (tlb_flag(TLB_V4_U_FULL))
  274. asm("mcr p15, 0, %0, c8, c7, 0" : : "r" (zero) : "cc");
  275. if (tlb_flag(TLB_V4_D_FULL))
  276. asm("mcr p15, 0, %0, c8, c6, 0" : : "r" (zero) : "cc");
  277. if (tlb_flag(TLB_V4_I_FULL))
  278. asm("mcr p15, 0, %0, c8, c5, 0" : : "r" (zero) : "cc");
  279. }
  280. if (tlb_flag(TLB_V6_U_ASID))
  281. asm("mcr p15, 0, %0, c8, c7, 2" : : "r" (asid) : "cc");
  282. if (tlb_flag(TLB_V6_D_ASID))
  283. asm("mcr p15, 0, %0, c8, c6, 2" : : "r" (asid) : "cc");
  284. if (tlb_flag(TLB_V6_I_ASID))
  285. asm("mcr p15, 0, %0, c8, c5, 2" : : "r" (asid) : "cc");
  286. if (tlb_flag(TLB_V6_I_FULL | TLB_V6_D_FULL |
  287. TLB_V6_I_PAGE | TLB_V6_D_PAGE |
  288. TLB_V6_I_ASID | TLB_V6_D_ASID)) {
  289. /* flush the branch target cache */
  290. asm("mcr p15, 0, %0, c7, c5, 6" : : "r" (zero) : "cc");
  291. dsb();
  292. }
  293. }
  294. static inline void
  295. local_flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
  296. {
  297. const int zero = 0;
  298. const unsigned int __tlb_flag = __cpu_tlb_flags;
  299. uaddr = (uaddr & PAGE_MASK) | ASID(vma->vm_mm);
  300. if (tlb_flag(TLB_WB))
  301. dsb();
  302. if (cpu_isset(smp_processor_id(), vma->vm_mm->cpu_vm_mask)) {
  303. if (tlb_flag(TLB_V3_PAGE))
  304. asm("mcr p15, 0, %0, c6, c0, 0" : : "r" (uaddr) : "cc");
  305. if (tlb_flag(TLB_V4_U_PAGE))
  306. asm("mcr p15, 0, %0, c8, c7, 1" : : "r" (uaddr) : "cc");
  307. if (tlb_flag(TLB_V4_D_PAGE))
  308. asm("mcr p15, 0, %0, c8, c6, 1" : : "r" (uaddr) : "cc");
  309. if (tlb_flag(TLB_V4_I_PAGE))
  310. asm("mcr p15, 0, %0, c8, c5, 1" : : "r" (uaddr) : "cc");
  311. if (!tlb_flag(TLB_V4_I_PAGE) && tlb_flag(TLB_V4_I_FULL))
  312. asm("mcr p15, 0, %0, c8, c5, 0" : : "r" (zero) : "cc");
  313. }
  314. if (tlb_flag(TLB_V6_U_PAGE))
  315. asm("mcr p15, 0, %0, c8, c7, 1" : : "r" (uaddr) : "cc");
  316. if (tlb_flag(TLB_V6_D_PAGE))
  317. asm("mcr p15, 0, %0, c8, c6, 1" : : "r" (uaddr) : "cc");
  318. if (tlb_flag(TLB_V6_I_PAGE))
  319. asm("mcr p15, 0, %0, c8, c5, 1" : : "r" (uaddr) : "cc");
  320. if (tlb_flag(TLB_V6_I_FULL | TLB_V6_D_FULL |
  321. TLB_V6_I_PAGE | TLB_V6_D_PAGE |
  322. TLB_V6_I_ASID | TLB_V6_D_ASID)) {
  323. /* flush the branch target cache */
  324. asm("mcr p15, 0, %0, c7, c5, 6" : : "r" (zero) : "cc");
  325. dsb();
  326. }
  327. }
  328. static inline void local_flush_tlb_kernel_page(unsigned long kaddr)
  329. {
  330. const int zero = 0;
  331. const unsigned int __tlb_flag = __cpu_tlb_flags;
  332. kaddr &= PAGE_MASK;
  333. if (tlb_flag(TLB_WB))
  334. dsb();
  335. if (tlb_flag(TLB_V3_PAGE))
  336. asm("mcr p15, 0, %0, c6, c0, 0" : : "r" (kaddr) : "cc");
  337. if (tlb_flag(TLB_V4_U_PAGE))
  338. asm("mcr p15, 0, %0, c8, c7, 1" : : "r" (kaddr) : "cc");
  339. if (tlb_flag(TLB_V4_D_PAGE))
  340. asm("mcr p15, 0, %0, c8, c6, 1" : : "r" (kaddr) : "cc");
  341. if (tlb_flag(TLB_V4_I_PAGE))
  342. asm("mcr p15, 0, %0, c8, c5, 1" : : "r" (kaddr) : "cc");
  343. if (!tlb_flag(TLB_V4_I_PAGE) && tlb_flag(TLB_V4_I_FULL))
  344. asm("mcr p15, 0, %0, c8, c5, 0" : : "r" (zero) : "cc");
  345. if (tlb_flag(TLB_V6_U_PAGE))
  346. asm("mcr p15, 0, %0, c8, c7, 1" : : "r" (kaddr) : "cc");
  347. if (tlb_flag(TLB_V6_D_PAGE))
  348. asm("mcr p15, 0, %0, c8, c6, 1" : : "r" (kaddr) : "cc");
  349. if (tlb_flag(TLB_V6_I_PAGE))
  350. asm("mcr p15, 0, %0, c8, c5, 1" : : "r" (kaddr) : "cc");
  351. if (tlb_flag(TLB_V6_I_FULL | TLB_V6_D_FULL |
  352. TLB_V6_I_PAGE | TLB_V6_D_PAGE |
  353. TLB_V6_I_ASID | TLB_V6_D_ASID)) {
  354. /* flush the branch target cache */
  355. asm("mcr p15, 0, %0, c7, c5, 6" : : "r" (zero) : "cc");
  356. dsb();
  357. isb();
  358. }
  359. }
  360. /*
  361. * flush_pmd_entry
  362. *
  363. * Flush a PMD entry (word aligned, or double-word aligned) to
  364. * RAM if the TLB for the CPU we are running on requires this.
  365. * This is typically used when we are creating PMD entries.
  366. *
  367. * clean_pmd_entry
  368. *
  369. * Clean (but don't drain the write buffer) if the CPU requires
  370. * these operations. This is typically used when we are removing
  371. * PMD entries.
  372. */
  373. static inline void flush_pmd_entry(pmd_t *pmd)
  374. {
  375. const unsigned int __tlb_flag = __cpu_tlb_flags;
  376. if (tlb_flag(TLB_DCLEAN))
  377. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pmd"
  378. : : "r" (pmd) : "cc");
  379. if (tlb_flag(TLB_L2CLEAN_FR))
  380. asm("mcr p15, 1, %0, c15, c9, 1 @ L2 flush_pmd"
  381. : : "r" (pmd) : "cc");
  382. if (tlb_flag(TLB_WB))
  383. dsb();
  384. }
  385. static inline void clean_pmd_entry(pmd_t *pmd)
  386. {
  387. const unsigned int __tlb_flag = __cpu_tlb_flags;
  388. if (tlb_flag(TLB_DCLEAN))
  389. asm("mcr p15, 0, %0, c7, c10, 1 @ flush_pmd"
  390. : : "r" (pmd) : "cc");
  391. if (tlb_flag(TLB_L2CLEAN_FR))
  392. asm("mcr p15, 1, %0, c15, c9, 1 @ L2 flush_pmd"
  393. : : "r" (pmd) : "cc");
  394. }
  395. #undef tlb_flag
  396. #undef always_tlb_flags
  397. #undef possible_tlb_flags
  398. /*
  399. * Convert calls to our calling convention.
  400. */
  401. #define local_flush_tlb_range(vma,start,end) __cpu_flush_user_tlb_range(start,end,vma)
  402. #define local_flush_tlb_kernel_range(s,e) __cpu_flush_kern_tlb_range(s,e)
  403. #ifndef CONFIG_SMP
  404. #define flush_tlb_all local_flush_tlb_all
  405. #define flush_tlb_mm local_flush_tlb_mm
  406. #define flush_tlb_page local_flush_tlb_page
  407. #define flush_tlb_kernel_page local_flush_tlb_kernel_page
  408. #define flush_tlb_range local_flush_tlb_range
  409. #define flush_tlb_kernel_range local_flush_tlb_kernel_range
  410. #else
  411. extern void flush_tlb_all(void);
  412. extern void flush_tlb_mm(struct mm_struct *mm);
  413. extern void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr);
  414. extern void flush_tlb_kernel_page(unsigned long kaddr);
  415. extern void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  416. extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
  417. #endif
  418. /*
  419. * if PG_dcache_dirty is set for the page, we need to ensure that any
  420. * cache entries for the kernels virtual memory range are written
  421. * back to the page.
  422. */
  423. extern void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte);
  424. #endif
  425. #endif /* CONFIG_MMU */
  426. #endif