tlbflush.h 19 KB

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