tlbflush.h 14 KB

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