tlbflush.h 14 KB

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