tlbflush.h 14 KB

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