cacheflush.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/cacheflush.h
  4. *
  5. * Copyright (C) 1999-2002 Russell King
  6. */
  7. #ifndef _ASMARM_CACHEFLUSH_H
  8. #define _ASMARM_CACHEFLUSH_H
  9. #include <linux/mm.h>
  10. #include <asm/glue-cache.h>
  11. #include <asm/shmparam.h>
  12. #include <asm/cachetype.h>
  13. #include <asm/outercache.h>
  14. #define CACHE_COLOUR(vaddr) ((vaddr & (SHMLBA - 1)) >> PAGE_SHIFT)
  15. /*
  16. * This flag is used to indicate that the page pointed to by a pte is clean
  17. * and does not require cleaning before returning it to the user.
  18. */
  19. #define PG_dcache_clean PG_arch_1
  20. /*
  21. * MM Cache Management
  22. * ===================
  23. *
  24. * The arch/arm/mm/cache-*.S and arch/arm/mm/proc-*.S files
  25. * implement these methods.
  26. *
  27. * Start addresses are inclusive and end addresses are exclusive;
  28. * start addresses should be rounded down, end addresses up.
  29. *
  30. * See Documentation/core-api/cachetlb.rst for more information.
  31. * Please note that the implementation of these, and the required
  32. * effects are cache-type (VIVT/VIPT/PIPT) specific.
  33. *
  34. * flush_icache_all()
  35. *
  36. * Unconditionally clean and invalidate the entire icache.
  37. * Currently only needed for cache-v6.S and cache-v7.S, see
  38. * __flush_icache_all for the generic implementation.
  39. *
  40. * flush_kern_all()
  41. *
  42. * Unconditionally clean and invalidate the entire cache.
  43. *
  44. * flush_kern_louis()
  45. *
  46. * Flush data cache levels up to the level of unification
  47. * inner shareable and invalidate the I-cache.
  48. * Only needed from v7 onwards, falls back to flush_cache_all()
  49. * for all other processor versions.
  50. *
  51. * flush_user_all()
  52. *
  53. * Clean and invalidate all user space cache entries
  54. * before a change of page tables.
  55. *
  56. * flush_user_range(start, end, flags)
  57. *
  58. * Clean and invalidate a range of cache entries in the
  59. * specified address space before a change of page tables.
  60. * - start - user start address (inclusive, page aligned)
  61. * - end - user end address (exclusive, page aligned)
  62. * - flags - vma->vm_flags field
  63. *
  64. * coherent_kern_range(start, end)
  65. *
  66. * Ensure coherency between the Icache and the Dcache in the
  67. * region described by start, end. If you have non-snooping
  68. * Harvard caches, you need to implement this function.
  69. * - start - virtual start address
  70. * - end - virtual end address
  71. *
  72. * coherent_user_range(start, end)
  73. *
  74. * Ensure coherency between the Icache and the Dcache in the
  75. * region described by start, end. If you have non-snooping
  76. * Harvard caches, you need to implement this function.
  77. * - start - virtual start address
  78. * - end - virtual end address
  79. *
  80. * flush_kern_dcache_area(kaddr, size)
  81. *
  82. * Ensure that the data held in page is written back.
  83. * - kaddr - page address
  84. * - size - region size
  85. *
  86. * DMA Cache Coherency
  87. * ===================
  88. *
  89. * dma_flush_range(start, end)
  90. *
  91. * Clean and invalidate the specified virtual address range.
  92. * - start - virtual start address
  93. * - end - virtual end address
  94. */
  95. struct cpu_cache_fns {
  96. void (*flush_icache_all)(void);
  97. void (*flush_kern_all)(void);
  98. void (*flush_kern_louis)(void);
  99. void (*flush_user_all)(void);
  100. void (*flush_user_range)(unsigned long, unsigned long, unsigned int);
  101. void (*coherent_kern_range)(unsigned long, unsigned long);
  102. int (*coherent_user_range)(unsigned long, unsigned long);
  103. void (*flush_kern_dcache_area)(void *, size_t);
  104. void (*dma_map_area)(const void *, size_t, int);
  105. void (*dma_unmap_area)(const void *, size_t, int);
  106. void (*dma_flush_range)(const void *, const void *);
  107. } __no_randomize_layout;
  108. /*
  109. * Select the calling method
  110. */
  111. #ifdef MULTI_CACHE
  112. extern struct cpu_cache_fns cpu_cache;
  113. #define __cpuc_flush_icache_all cpu_cache.flush_icache_all
  114. #define __cpuc_flush_kern_all cpu_cache.flush_kern_all
  115. #define __cpuc_flush_kern_louis cpu_cache.flush_kern_louis
  116. #define __cpuc_flush_user_all cpu_cache.flush_user_all
  117. #define __cpuc_flush_user_range cpu_cache.flush_user_range
  118. #define __cpuc_coherent_kern_range cpu_cache.coherent_kern_range
  119. #define __cpuc_coherent_user_range cpu_cache.coherent_user_range
  120. #define __cpuc_flush_dcache_area cpu_cache.flush_kern_dcache_area
  121. /*
  122. * These are private to the dma-mapping API. Do not use directly.
  123. * Their sole purpose is to ensure that data held in the cache
  124. * is visible to DMA, or data written by DMA to system memory is
  125. * visible to the CPU.
  126. */
  127. #define dmac_flush_range cpu_cache.dma_flush_range
  128. #else
  129. extern void __cpuc_flush_icache_all(void);
  130. extern void __cpuc_flush_kern_all(void);
  131. extern void __cpuc_flush_kern_louis(void);
  132. extern void __cpuc_flush_user_all(void);
  133. extern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
  134. extern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
  135. extern int __cpuc_coherent_user_range(unsigned long, unsigned long);
  136. extern void __cpuc_flush_dcache_area(void *, size_t);
  137. /*
  138. * These are private to the dma-mapping API. Do not use directly.
  139. * Their sole purpose is to ensure that data held in the cache
  140. * is visible to DMA, or data written by DMA to system memory is
  141. * visible to the CPU.
  142. */
  143. extern void dmac_flush_range(const void *, const void *);
  144. #endif
  145. /*
  146. * Copy user data from/to a page which is mapped into a different
  147. * processes address space. Really, we want to allow our "user
  148. * space" model to handle this.
  149. */
  150. extern void copy_to_user_page(struct vm_area_struct *, struct page *,
  151. unsigned long, void *, const void *, unsigned long);
  152. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  153. do { \
  154. memcpy(dst, src, len); \
  155. } while (0)
  156. /*
  157. * Convert calls to our calling convention.
  158. */
  159. /* Invalidate I-cache */
  160. #define __flush_icache_all_generic() \
  161. asm("mcr p15, 0, %0, c7, c5, 0" \
  162. : : "r" (0));
  163. /* Invalidate I-cache inner shareable */
  164. #define __flush_icache_all_v7_smp() \
  165. asm("mcr p15, 0, %0, c7, c1, 0" \
  166. : : "r" (0));
  167. /*
  168. * Optimized __flush_icache_all for the common cases. Note that UP ARMv7
  169. * will fall through to use __flush_icache_all_generic.
  170. */
  171. #if (defined(CONFIG_CPU_V7) && \
  172. (defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K))) || \
  173. defined(CONFIG_SMP_ON_UP)
  174. #define __flush_icache_preferred __cpuc_flush_icache_all
  175. #elif __LINUX_ARM_ARCH__ >= 7 && defined(CONFIG_SMP)
  176. #define __flush_icache_preferred __flush_icache_all_v7_smp
  177. #elif __LINUX_ARM_ARCH__ == 6 && defined(CONFIG_ARM_ERRATA_411920)
  178. #define __flush_icache_preferred __cpuc_flush_icache_all
  179. #else
  180. #define __flush_icache_preferred __flush_icache_all_generic
  181. #endif
  182. static inline void __flush_icache_all(void)
  183. {
  184. __flush_icache_preferred();
  185. dsb(ishst);
  186. }
  187. /*
  188. * Flush caches up to Level of Unification Inner Shareable
  189. */
  190. #define flush_cache_louis() __cpuc_flush_kern_louis()
  191. #define flush_cache_all() __cpuc_flush_kern_all()
  192. static inline void vivt_flush_cache_mm(struct mm_struct *mm)
  193. {
  194. if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  195. __cpuc_flush_user_all();
  196. }
  197. static inline void
  198. vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  199. {
  200. struct mm_struct *mm = vma->vm_mm;
  201. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  202. __cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
  203. vma->vm_flags);
  204. }
  205. static inline void
  206. vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  207. {
  208. struct mm_struct *mm = vma->vm_mm;
  209. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
  210. unsigned long addr = user_addr & PAGE_MASK;
  211. __cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
  212. }
  213. }
  214. #ifndef CONFIG_CPU_CACHE_VIPT
  215. #define flush_cache_mm(mm) \
  216. vivt_flush_cache_mm(mm)
  217. #define flush_cache_range(vma,start,end) \
  218. vivt_flush_cache_range(vma,start,end)
  219. #define flush_cache_page(vma,addr,pfn) \
  220. vivt_flush_cache_page(vma,addr,pfn)
  221. #else
  222. extern void flush_cache_mm(struct mm_struct *mm);
  223. extern void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  224. extern void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn);
  225. #endif
  226. #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
  227. /*
  228. * flush_icache_user_range is used when we want to ensure that the
  229. * Harvard caches are synchronised for the user space address range.
  230. * This is used for the ARM private sys_cacheflush system call.
  231. */
  232. #define flush_icache_user_range(s,e) __cpuc_coherent_user_range(s,e)
  233. /*
  234. * Perform necessary cache operations to ensure that data previously
  235. * stored within this range of addresses can be executed by the CPU.
  236. */
  237. #define flush_icache_range(s,e) __cpuc_coherent_kern_range(s,e)
  238. /*
  239. * Perform necessary cache operations to ensure that the TLB will
  240. * see data written in the specified area.
  241. */
  242. #define clean_dcache_area(start,size) cpu_dcache_clean_area(start, size)
  243. /*
  244. * flush_dcache_page is used when the kernel has written to the page
  245. * cache page at virtual address page->virtual.
  246. *
  247. * If this page isn't mapped (ie, page_mapping == NULL), or it might
  248. * have userspace mappings, then we _must_ always clean + invalidate
  249. * the dcache entries associated with the kernel mapping.
  250. *
  251. * Otherwise we can defer the operation, and clean the cache when we are
  252. * about to change to user space. This is the same method as used on SPARC64.
  253. * See update_mmu_cache for the user space part.
  254. */
  255. #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
  256. extern void flush_dcache_page(struct page *);
  257. #define ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE 1
  258. static inline void flush_kernel_vmap_range(void *addr, int size)
  259. {
  260. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  261. __cpuc_flush_dcache_area(addr, (size_t)size);
  262. }
  263. static inline void invalidate_kernel_vmap_range(void *addr, int size)
  264. {
  265. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  266. __cpuc_flush_dcache_area(addr, (size_t)size);
  267. }
  268. #define ARCH_HAS_FLUSH_ANON_PAGE
  269. static inline void flush_anon_page(struct vm_area_struct *vma,
  270. struct page *page, unsigned long vmaddr)
  271. {
  272. extern void __flush_anon_page(struct vm_area_struct *vma,
  273. struct page *, unsigned long);
  274. if (PageAnon(page))
  275. __flush_anon_page(vma, page, vmaddr);
  276. }
  277. #define flush_dcache_mmap_lock(mapping) xa_lock_irq(&mapping->i_pages)
  278. #define flush_dcache_mmap_unlock(mapping) xa_unlock_irq(&mapping->i_pages)
  279. /*
  280. * We don't appear to need to do anything here. In fact, if we did, we'd
  281. * duplicate cache flushing elsewhere performed by flush_dcache_page().
  282. */
  283. #define flush_icache_page(vma,page) do { } while (0)
  284. /*
  285. * flush_cache_vmap() is used when creating mappings (eg, via vmap,
  286. * vmalloc, ioremap etc) in kernel space for pages. On non-VIPT
  287. * caches, since the direct-mappings of these pages may contain cached
  288. * data, we need to do a full cache flush to ensure that writebacks
  289. * don't corrupt data placed into these pages via the new mappings.
  290. */
  291. static inline void flush_cache_vmap(unsigned long start, unsigned long end)
  292. {
  293. if (!cache_is_vipt_nonaliasing())
  294. flush_cache_all();
  295. else
  296. /*
  297. * set_pte_at() called from vmap_pte_range() does not
  298. * have a DSB after cleaning the cache line.
  299. */
  300. dsb(ishst);
  301. }
  302. static inline void flush_cache_vunmap(unsigned long start, unsigned long end)
  303. {
  304. if (!cache_is_vipt_nonaliasing())
  305. flush_cache_all();
  306. }
  307. /*
  308. * Memory synchronization helpers for mixed cached vs non cached accesses.
  309. *
  310. * Some synchronization algorithms have to set states in memory with the
  311. * cache enabled or disabled depending on the code path. It is crucial
  312. * to always ensure proper cache maintenance to update main memory right
  313. * away in that case.
  314. *
  315. * Any cached write must be followed by a cache clean operation.
  316. * Any cached read must be preceded by a cache invalidate operation.
  317. * Yet, in the read case, a cache flush i.e. atomic clean+invalidate
  318. * operation is needed to avoid discarding possible concurrent writes to the
  319. * accessed memory.
  320. *
  321. * Also, in order to prevent a cached writer from interfering with an
  322. * adjacent non-cached writer, each state variable must be located to
  323. * a separate cache line.
  324. */
  325. /*
  326. * This needs to be >= the max cache writeback size of all
  327. * supported platforms included in the current kernel configuration.
  328. * This is used to align state variables to their own cache lines.
  329. */
  330. #define __CACHE_WRITEBACK_ORDER 6 /* guessed from existing platforms */
  331. #define __CACHE_WRITEBACK_GRANULE (1 << __CACHE_WRITEBACK_ORDER)
  332. /*
  333. * There is no __cpuc_clean_dcache_area but we use it anyway for
  334. * code intent clarity, and alias it to __cpuc_flush_dcache_area.
  335. */
  336. #define __cpuc_clean_dcache_area __cpuc_flush_dcache_area
  337. /*
  338. * Ensure preceding writes to *p by this CPU are visible to
  339. * subsequent reads by other CPUs:
  340. */
  341. static inline void __sync_cache_range_w(volatile void *p, size_t size)
  342. {
  343. char *_p = (char *)p;
  344. __cpuc_clean_dcache_area(_p, size);
  345. outer_clean_range(__pa(_p), __pa(_p + size));
  346. }
  347. /*
  348. * Ensure preceding writes to *p by other CPUs are visible to
  349. * subsequent reads by this CPU. We must be careful not to
  350. * discard data simultaneously written by another CPU, hence the
  351. * usage of flush rather than invalidate operations.
  352. */
  353. static inline void __sync_cache_range_r(volatile void *p, size_t size)
  354. {
  355. char *_p = (char *)p;
  356. #ifdef CONFIG_OUTER_CACHE
  357. if (outer_cache.flush_range) {
  358. /*
  359. * Ensure dirty data migrated from other CPUs into our cache
  360. * are cleaned out safely before the outer cache is cleaned:
  361. */
  362. __cpuc_clean_dcache_area(_p, size);
  363. /* Clean and invalidate stale data for *p from outer ... */
  364. outer_flush_range(__pa(_p), __pa(_p + size));
  365. }
  366. #endif
  367. /* ... and inner cache: */
  368. __cpuc_flush_dcache_area(_p, size);
  369. }
  370. #define sync_cache_w(ptr) __sync_cache_range_w(ptr, sizeof *(ptr))
  371. #define sync_cache_r(ptr) __sync_cache_range_r(ptr, sizeof *(ptr))
  372. /*
  373. * Disabling cache access for one CPU in an ARMv7 SMP system is tricky.
  374. * To do so we must:
  375. *
  376. * - Clear the SCTLR.C bit to prevent further cache allocations
  377. * - Flush the desired level of cache
  378. * - Clear the ACTLR "SMP" bit to disable local coherency
  379. *
  380. * ... and so without any intervening memory access in between those steps,
  381. * not even to the stack.
  382. *
  383. * WARNING -- After this has been called:
  384. *
  385. * - No ldrex/strex (and similar) instructions must be used.
  386. * - The CPU is obviously no longer coherent with the other CPUs.
  387. * - This is unlikely to work as expected if Linux is running non-secure.
  388. *
  389. * Note:
  390. *
  391. * - This is known to apply to several ARMv7 processor implementations,
  392. * however some exceptions may exist. Caveat emptor.
  393. *
  394. * - The clobber list is dictated by the call to v7_flush_dcache_*.
  395. */
  396. #define v7_exit_coherency_flush(level) \
  397. asm volatile( \
  398. ".arch armv7-a \n\t" \
  399. "mrc p15, 0, r0, c1, c0, 0 @ get SCTLR \n\t" \
  400. "bic r0, r0, #"__stringify(CR_C)" \n\t" \
  401. "mcr p15, 0, r0, c1, c0, 0 @ set SCTLR \n\t" \
  402. "isb \n\t" \
  403. "bl v7_flush_dcache_"__stringify(level)" \n\t" \
  404. "mrc p15, 0, r0, c1, c0, 1 @ get ACTLR \n\t" \
  405. "bic r0, r0, #(1 << 6) @ disable local coherency \n\t" \
  406. "mcr p15, 0, r0, c1, c0, 1 @ set ACTLR \n\t" \
  407. "isb \n\t" \
  408. "dsb" \
  409. : : : "r0","r1","r2","r3","r4","r5","r6", \
  410. "r9","r10","ip","lr","memory" )
  411. void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
  412. void *kaddr, unsigned long len);
  413. #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
  414. void check_cpu_icache_size(int cpuid);
  415. #else
  416. static inline void check_cpu_icache_size(int cpuid) { }
  417. #endif
  418. #endif