cacheflush.h 16 KB

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