tlbflush.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/tlbflush.h
  4. *
  5. * Copyright (C) 1999-2003 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #ifndef __ASM_TLBFLUSH_H
  9. #define __ASM_TLBFLUSH_H
  10. #ifndef __ASSEMBLY__
  11. #include <linux/bitfield.h>
  12. #include <linux/mm_types.h>
  13. #include <linux/sched.h>
  14. #include <linux/mmu_notifier.h>
  15. #include <asm/cputype.h>
  16. #include <asm/mmu.h>
  17. /*
  18. * Raw TLBI operations.
  19. *
  20. * Where necessary, use the __tlbi() macro to avoid asm()
  21. * boilerplate. Drivers and most kernel code should use the TLB
  22. * management routines in preference to the macro below.
  23. *
  24. * The macro can be used as __tlbi(op) or __tlbi(op, arg), depending
  25. * on whether a particular TLBI operation takes an argument or
  26. * not. The macros handles invoking the asm with or without the
  27. * register argument as appropriate.
  28. */
  29. #define __TLBI_0(op, arg) asm (ARM64_ASM_PREAMBLE \
  30. "tlbi " #op "\n" \
  31. ALTERNATIVE("nop\n nop", \
  32. "dsb ish\n tlbi " #op, \
  33. ARM64_WORKAROUND_REPEAT_TLBI, \
  34. CONFIG_ARM64_WORKAROUND_REPEAT_TLBI) \
  35. : : )
  36. #define __TLBI_1(op, arg) asm (ARM64_ASM_PREAMBLE \
  37. "tlbi " #op ", %0\n" \
  38. ALTERNATIVE("nop\n nop", \
  39. "dsb ish\n tlbi " #op ", %0", \
  40. ARM64_WORKAROUND_REPEAT_TLBI, \
  41. CONFIG_ARM64_WORKAROUND_REPEAT_TLBI) \
  42. : : "r" (arg))
  43. #define __TLBI_N(op, arg, n, ...) __TLBI_##n(op, arg)
  44. #define __tlbi(op, ...) __TLBI_N(op, ##__VA_ARGS__, 1, 0)
  45. #define __tlbi_user(op, arg) do { \
  46. if (arm64_kernel_unmapped_at_el0()) \
  47. __tlbi(op, (arg) | USER_ASID_FLAG); \
  48. } while (0)
  49. /* This macro creates a properly formatted VA operand for the TLBI */
  50. #define __TLBI_VADDR(addr, asid) \
  51. ({ \
  52. unsigned long __ta = (addr) >> 12; \
  53. __ta &= GENMASK_ULL(43, 0); \
  54. __ta |= (unsigned long)(asid) << 48; \
  55. __ta; \
  56. })
  57. /*
  58. * Get translation granule of the system, which is decided by
  59. * PAGE_SIZE. Used by TTL.
  60. * - 4KB : 1
  61. * - 16KB : 2
  62. * - 64KB : 3
  63. */
  64. #define TLBI_TTL_TG_4K 1
  65. #define TLBI_TTL_TG_16K 2
  66. #define TLBI_TTL_TG_64K 3
  67. static inline unsigned long get_trans_granule(void)
  68. {
  69. switch (PAGE_SIZE) {
  70. case SZ_4K:
  71. return TLBI_TTL_TG_4K;
  72. case SZ_16K:
  73. return TLBI_TTL_TG_16K;
  74. case SZ_64K:
  75. return TLBI_TTL_TG_64K;
  76. default:
  77. return 0;
  78. }
  79. }
  80. /*
  81. * Level-based TLBI operations.
  82. *
  83. * When ARMv8.4-TTL exists, TLBI operations take an additional hint for
  84. * the level at which the invalidation must take place. If the level is
  85. * wrong, no invalidation may take place. In the case where the level
  86. * cannot be easily determined, the value TLBI_TTL_UNKNOWN will perform
  87. * a non-hinted invalidation. Any provided level outside the hint range
  88. * will also cause fall-back to non-hinted invalidation.
  89. *
  90. * For Stage-2 invalidation, use the level values provided to that effect
  91. * in asm/stage2_pgtable.h.
  92. */
  93. #define TLBI_TTL_MASK GENMASK_ULL(47, 44)
  94. #define TLBI_TTL_UNKNOWN INT_MAX
  95. #define __tlbi_level(op, addr, level) do { \
  96. u64 arg = addr; \
  97. \
  98. if (alternative_has_cap_unlikely(ARM64_HAS_ARMv8_4_TTL) && \
  99. level >= 0 && level <= 3) { \
  100. u64 ttl = level & 3; \
  101. ttl |= get_trans_granule() << 2; \
  102. arg &= ~TLBI_TTL_MASK; \
  103. arg |= FIELD_PREP(TLBI_TTL_MASK, ttl); \
  104. } \
  105. \
  106. __tlbi(op, arg); \
  107. } while(0)
  108. #define __tlbi_user_level(op, arg, level) do { \
  109. if (arm64_kernel_unmapped_at_el0()) \
  110. __tlbi_level(op, (arg | USER_ASID_FLAG), level); \
  111. } while (0)
  112. /*
  113. * This macro creates a properly formatted VA operand for the TLB RANGE. The
  114. * value bit assignments are:
  115. *
  116. * +----------+------+-------+-------+-------+----------------------+
  117. * | ASID | TG | SCALE | NUM | TTL | BADDR |
  118. * +-----------------+-------+-------+-------+----------------------+
  119. * |63 48|47 46|45 44|43 39|38 37|36 0|
  120. *
  121. * The address range is determined by below formula: [BADDR, BADDR + (NUM + 1) *
  122. * 2^(5*SCALE + 1) * PAGESIZE)
  123. *
  124. * Note that the first argument, baddr, is pre-shifted; If LPA2 is in use, BADDR
  125. * holds addr[52:16]. Else BADDR holds page number. See for example ARM DDI
  126. * 0487J.a section C5.5.60 "TLBI VAE1IS, TLBI VAE1ISNXS, TLB Invalidate by VA,
  127. * EL1, Inner Shareable".
  128. *
  129. */
  130. #define TLBIR_ASID_MASK GENMASK_ULL(63, 48)
  131. #define TLBIR_TG_MASK GENMASK_ULL(47, 46)
  132. #define TLBIR_SCALE_MASK GENMASK_ULL(45, 44)
  133. #define TLBIR_NUM_MASK GENMASK_ULL(43, 39)
  134. #define TLBIR_TTL_MASK GENMASK_ULL(38, 37)
  135. #define TLBIR_BADDR_MASK GENMASK_ULL(36, 0)
  136. #define __TLBI_VADDR_RANGE(baddr, asid, scale, num, ttl) \
  137. ({ \
  138. unsigned long __ta = 0; \
  139. unsigned long __ttl = (ttl >= 1 && ttl <= 3) ? ttl : 0; \
  140. __ta |= FIELD_PREP(TLBIR_BADDR_MASK, baddr); \
  141. __ta |= FIELD_PREP(TLBIR_TTL_MASK, __ttl); \
  142. __ta |= FIELD_PREP(TLBIR_NUM_MASK, num); \
  143. __ta |= FIELD_PREP(TLBIR_SCALE_MASK, scale); \
  144. __ta |= FIELD_PREP(TLBIR_TG_MASK, get_trans_granule()); \
  145. __ta |= FIELD_PREP(TLBIR_ASID_MASK, asid); \
  146. __ta; \
  147. })
  148. /* These macros are used by the TLBI RANGE feature. */
  149. #define __TLBI_RANGE_PAGES(num, scale) \
  150. ((unsigned long)((num) + 1) << (5 * (scale) + 1))
  151. #define MAX_TLBI_RANGE_PAGES __TLBI_RANGE_PAGES(31, 3)
  152. /*
  153. * Generate 'num' values from -1 to 31 with -1 rejected by the
  154. * __flush_tlb_range() loop below. Its return value is only
  155. * significant for a maximum of MAX_TLBI_RANGE_PAGES pages. If
  156. * 'pages' is more than that, you must iterate over the overall
  157. * range.
  158. */
  159. #define __TLBI_RANGE_NUM(pages, scale) \
  160. ({ \
  161. int __pages = min((pages), \
  162. __TLBI_RANGE_PAGES(31, (scale))); \
  163. (__pages >> (5 * (scale) + 1)) - 1; \
  164. })
  165. /*
  166. * TLB Invalidation
  167. * ================
  168. *
  169. * This header file implements the low-level TLB invalidation routines
  170. * (sometimes referred to as "flushing" in the kernel) for arm64.
  171. *
  172. * Every invalidation operation uses the following template:
  173. *
  174. * DSB ISHST // Ensure prior page-table updates have completed
  175. * TLBI ... // Invalidate the TLB
  176. * DSB ISH // Ensure the TLB invalidation has completed
  177. * if (invalidated kernel mappings)
  178. * ISB // Discard any instructions fetched from the old mapping
  179. *
  180. *
  181. * The following functions form part of the "core" TLB invalidation API,
  182. * as documented in Documentation/core-api/cachetlb.rst:
  183. *
  184. * flush_tlb_all()
  185. * Invalidate the entire TLB (kernel + user) on all CPUs
  186. *
  187. * flush_tlb_mm(mm)
  188. * Invalidate an entire user address space on all CPUs.
  189. * The 'mm' argument identifies the ASID to invalidate.
  190. *
  191. * flush_tlb_range(vma, start, end)
  192. * Invalidate the virtual-address range '[start, end)' on all
  193. * CPUs for the user address space corresponding to 'vma->mm'.
  194. * Note that this operation also invalidates any walk-cache
  195. * entries associated with translations for the specified address
  196. * range.
  197. *
  198. * flush_tlb_kernel_range(start, end)
  199. * Same as flush_tlb_range(..., start, end), but applies to
  200. * kernel mappings rather than a particular user address space.
  201. * Whilst not explicitly documented, this function is used when
  202. * unmapping pages from vmalloc/io space.
  203. *
  204. * flush_tlb_page(vma, addr)
  205. * Invalidate a single user mapping for address 'addr' in the
  206. * address space corresponding to 'vma->mm'. Note that this
  207. * operation only invalidates a single, last-level page-table
  208. * entry and therefore does not affect any walk-caches.
  209. *
  210. *
  211. * Next, we have some undocumented invalidation routines that you probably
  212. * don't want to call unless you know what you're doing:
  213. *
  214. * local_flush_tlb_all()
  215. * Same as flush_tlb_all(), but only applies to the calling CPU.
  216. *
  217. * __flush_tlb_kernel_pgtable(addr)
  218. * Invalidate a single kernel mapping for address 'addr' on all
  219. * CPUs, ensuring that any walk-cache entries associated with the
  220. * translation are also invalidated.
  221. *
  222. * __flush_tlb_range(vma, start, end, stride, last_level, tlb_level)
  223. * Invalidate the virtual-address range '[start, end)' on all
  224. * CPUs for the user address space corresponding to 'vma->mm'.
  225. * The invalidation operations are issued at a granularity
  226. * determined by 'stride' and only affect any walk-cache entries
  227. * if 'last_level' is equal to false. tlb_level is the level at
  228. * which the invalidation must take place. If the level is wrong,
  229. * no invalidation may take place. In the case where the level
  230. * cannot be easily determined, the value TLBI_TTL_UNKNOWN will
  231. * perform a non-hinted invalidation.
  232. *
  233. *
  234. * Finally, take a look at asm/tlb.h to see how tlb_flush() is implemented
  235. * on top of these routines, since that is our interface to the mmu_gather
  236. * API as used by munmap() and friends.
  237. */
  238. static inline void local_flush_tlb_all(void)
  239. {
  240. dsb(nshst);
  241. __tlbi(vmalle1);
  242. dsb(nsh);
  243. isb();
  244. }
  245. static inline void flush_tlb_all(void)
  246. {
  247. dsb(ishst);
  248. __tlbi(vmalle1is);
  249. dsb(ish);
  250. isb();
  251. }
  252. static inline void flush_tlb_mm(struct mm_struct *mm)
  253. {
  254. unsigned long asid;
  255. dsb(ishst);
  256. asid = __TLBI_VADDR(0, ASID(mm));
  257. __tlbi(aside1is, asid);
  258. __tlbi_user(aside1is, asid);
  259. dsb(ish);
  260. mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
  261. }
  262. static inline void __flush_tlb_page_nosync(struct mm_struct *mm,
  263. unsigned long uaddr)
  264. {
  265. unsigned long addr;
  266. dsb(ishst);
  267. addr = __TLBI_VADDR(uaddr, ASID(mm));
  268. __tlbi(vale1is, addr);
  269. __tlbi_user(vale1is, addr);
  270. mmu_notifier_arch_invalidate_secondary_tlbs(mm, uaddr & PAGE_MASK,
  271. (uaddr & PAGE_MASK) + PAGE_SIZE);
  272. }
  273. static inline void flush_tlb_page_nosync(struct vm_area_struct *vma,
  274. unsigned long uaddr)
  275. {
  276. return __flush_tlb_page_nosync(vma->vm_mm, uaddr);
  277. }
  278. static inline void flush_tlb_page(struct vm_area_struct *vma,
  279. unsigned long uaddr)
  280. {
  281. flush_tlb_page_nosync(vma, uaddr);
  282. dsb(ish);
  283. }
  284. static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm)
  285. {
  286. /*
  287. * TLB flush deferral is not required on systems which are affected by
  288. * ARM64_WORKAROUND_REPEAT_TLBI, as __tlbi()/__tlbi_user() implementation
  289. * will have two consecutive TLBI instructions with a dsb(ish) in between
  290. * defeating the purpose (i.e save overall 'dsb ish' cost).
  291. */
  292. if (alternative_has_cap_unlikely(ARM64_WORKAROUND_REPEAT_TLBI))
  293. return false;
  294. return true;
  295. }
  296. static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch,
  297. struct mm_struct *mm,
  298. unsigned long uaddr)
  299. {
  300. __flush_tlb_page_nosync(mm, uaddr);
  301. }
  302. /*
  303. * If mprotect/munmap/etc occurs during TLB batched flushing, we need to ensure
  304. * all the previously issued TLBIs targeting mm have completed. But since we
  305. * can be executing on a remote CPU, a DSB cannot guarantee this like it can
  306. * for arch_tlbbatch_flush(). Our only option is to flush the entire mm.
  307. */
  308. static inline void arch_flush_tlb_batched_pending(struct mm_struct *mm)
  309. {
  310. flush_tlb_mm(mm);
  311. }
  312. /*
  313. * To support TLB batched flush for multiple pages unmapping, we only send
  314. * the TLBI for each page in arch_tlbbatch_add_pending() and wait for the
  315. * completion at the end in arch_tlbbatch_flush(). Since we've already issued
  316. * TLBI for each page so only a DSB is needed to synchronise its effect on the
  317. * other CPUs.
  318. *
  319. * This will save the time waiting on DSB comparing issuing a TLBI;DSB sequence
  320. * for each page.
  321. */
  322. static inline void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
  323. {
  324. dsb(ish);
  325. }
  326. /*
  327. * This is meant to avoid soft lock-ups on large TLB flushing ranges and not
  328. * necessarily a performance improvement.
  329. */
  330. #define MAX_DVM_OPS PTRS_PER_PTE
  331. /*
  332. * __flush_tlb_range_op - Perform TLBI operation upon a range
  333. *
  334. * @op: TLBI instruction that operates on a range (has 'r' prefix)
  335. * @start: The start address of the range
  336. * @pages: Range as the number of pages from 'start'
  337. * @stride: Flush granularity
  338. * @asid: The ASID of the task (0 for IPA instructions)
  339. * @tlb_level: Translation Table level hint, if known
  340. * @tlbi_user: If 'true', call an additional __tlbi_user()
  341. * (typically for user ASIDs). 'flase' for IPA instructions
  342. * @lpa2: If 'true', the lpa2 scheme is used as set out below
  343. *
  344. * When the CPU does not support TLB range operations, flush the TLB
  345. * entries one by one at the granularity of 'stride'. If the TLB
  346. * range ops are supported, then:
  347. *
  348. * 1. If FEAT_LPA2 is in use, the start address of a range operation must be
  349. * 64KB aligned, so flush pages one by one until the alignment is reached
  350. * using the non-range operations. This step is skipped if LPA2 is not in
  351. * use.
  352. *
  353. * 2. The minimum range granularity is decided by 'scale', so multiple range
  354. * TLBI operations may be required. Start from scale = 3, flush the largest
  355. * possible number of pages ((num+1)*2^(5*scale+1)) that fit into the
  356. * requested range, then decrement scale and continue until one or zero pages
  357. * are left. We must start from highest scale to ensure 64KB start alignment
  358. * is maintained in the LPA2 case.
  359. *
  360. * 3. If there is 1 page remaining, flush it through non-range operations. Range
  361. * operations can only span an even number of pages. We save this for last to
  362. * ensure 64KB start alignment is maintained for the LPA2 case.
  363. */
  364. #define __flush_tlb_range_op(op, start, pages, stride, \
  365. asid, tlb_level, tlbi_user, lpa2) \
  366. do { \
  367. typeof(start) __flush_start = start; \
  368. typeof(pages) __flush_pages = pages; \
  369. int num = 0; \
  370. int scale = 3; \
  371. int shift = lpa2 ? 16 : PAGE_SHIFT; \
  372. unsigned long addr; \
  373. \
  374. while (__flush_pages > 0) { \
  375. if (!system_supports_tlb_range() || \
  376. __flush_pages == 1 || \
  377. (lpa2 && __flush_start != ALIGN(__flush_start, SZ_64K))) { \
  378. addr = __TLBI_VADDR(__flush_start, asid); \
  379. __tlbi_level(op, addr, tlb_level); \
  380. if (tlbi_user) \
  381. __tlbi_user_level(op, addr, tlb_level); \
  382. __flush_start += stride; \
  383. __flush_pages -= stride >> PAGE_SHIFT; \
  384. continue; \
  385. } \
  386. \
  387. num = __TLBI_RANGE_NUM(__flush_pages, scale); \
  388. if (num >= 0) { \
  389. addr = __TLBI_VADDR_RANGE(__flush_start >> shift, asid, \
  390. scale, num, tlb_level); \
  391. __tlbi(r##op, addr); \
  392. if (tlbi_user) \
  393. __tlbi_user(r##op, addr); \
  394. __flush_start += __TLBI_RANGE_PAGES(num, scale) << PAGE_SHIFT; \
  395. __flush_pages -= __TLBI_RANGE_PAGES(num, scale);\
  396. } \
  397. scale--; \
  398. } \
  399. } while (0)
  400. #define __flush_s2_tlb_range_op(op, start, pages, stride, tlb_level) \
  401. __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, false, kvm_lpa2_is_enabled());
  402. static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma,
  403. unsigned long start, unsigned long end,
  404. unsigned long stride, bool last_level,
  405. int tlb_level)
  406. {
  407. unsigned long asid, pages;
  408. start = round_down(start, stride);
  409. end = round_up(end, stride);
  410. pages = (end - start) >> PAGE_SHIFT;
  411. /*
  412. * When not uses TLB range ops, we can handle up to
  413. * (MAX_DVM_OPS - 1) pages;
  414. * When uses TLB range ops, we can handle up to
  415. * MAX_TLBI_RANGE_PAGES pages.
  416. */
  417. if ((!system_supports_tlb_range() &&
  418. (end - start) >= (MAX_DVM_OPS * stride)) ||
  419. pages > MAX_TLBI_RANGE_PAGES) {
  420. flush_tlb_mm(vma->vm_mm);
  421. return;
  422. }
  423. dsb(ishst);
  424. asid = ASID(vma->vm_mm);
  425. if (last_level)
  426. __flush_tlb_range_op(vale1is, start, pages, stride, asid,
  427. tlb_level, true, lpa2_is_enabled());
  428. else
  429. __flush_tlb_range_op(vae1is, start, pages, stride, asid,
  430. tlb_level, true, lpa2_is_enabled());
  431. mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, start, end);
  432. }
  433. static inline void __flush_tlb_range(struct vm_area_struct *vma,
  434. unsigned long start, unsigned long end,
  435. unsigned long stride, bool last_level,
  436. int tlb_level)
  437. {
  438. __flush_tlb_range_nosync(vma, start, end, stride,
  439. last_level, tlb_level);
  440. dsb(ish);
  441. }
  442. static inline void flush_tlb_range(struct vm_area_struct *vma,
  443. unsigned long start, unsigned long end)
  444. {
  445. /*
  446. * We cannot use leaf-only invalidation here, since we may be invalidating
  447. * table entries as part of collapsing hugepages or moving page tables.
  448. * Set the tlb_level to TLBI_TTL_UNKNOWN because we can not get enough
  449. * information here.
  450. */
  451. __flush_tlb_range(vma, start, end, PAGE_SIZE, false, TLBI_TTL_UNKNOWN);
  452. }
  453. static inline void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  454. {
  455. unsigned long addr;
  456. if ((end - start) > (MAX_DVM_OPS * PAGE_SIZE)) {
  457. flush_tlb_all();
  458. return;
  459. }
  460. start = __TLBI_VADDR(start, 0);
  461. end = __TLBI_VADDR(end, 0);
  462. dsb(ishst);
  463. for (addr = start; addr < end; addr += 1 << (PAGE_SHIFT - 12))
  464. __tlbi(vaale1is, addr);
  465. dsb(ish);
  466. isb();
  467. }
  468. /*
  469. * Used to invalidate the TLB (walk caches) corresponding to intermediate page
  470. * table levels (pgd/pud/pmd).
  471. */
  472. static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)
  473. {
  474. unsigned long addr = __TLBI_VADDR(kaddr, 0);
  475. dsb(ishst);
  476. __tlbi(vaae1is, addr);
  477. dsb(ish);
  478. isb();
  479. }
  480. #endif
  481. #endif