cacheflush.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/cachetlb.txt 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. };
  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_map_area cpu_cache.dma_map_area
  131. #define dmac_unmap_area cpu_cache.dma_unmap_area
  132. #define dmac_flush_range cpu_cache.dma_flush_range
  133. #else
  134. extern void __cpuc_flush_icache_all(void);
  135. extern void __cpuc_flush_kern_all(void);
  136. extern void __cpuc_flush_kern_louis(void);
  137. extern void __cpuc_flush_user_all(void);
  138. extern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
  139. extern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
  140. extern int __cpuc_coherent_user_range(unsigned long, unsigned long);
  141. extern void __cpuc_flush_dcache_area(void *, size_t);
  142. /*
  143. * These are private to the dma-mapping API. Do not use directly.
  144. * Their sole purpose is to ensure that data held in the cache
  145. * is visible to DMA, or data written by DMA to system memory is
  146. * visible to the CPU.
  147. */
  148. extern void dmac_map_area(const void *, size_t, int);
  149. extern void dmac_unmap_area(const void *, size_t, int);
  150. extern void dmac_flush_range(const void *, const void *);
  151. #endif
  152. /*
  153. * Copy user data from/to a page which is mapped into a different
  154. * processes address space. Really, we want to allow our "user
  155. * space" model to handle this.
  156. */
  157. extern void copy_to_user_page(struct vm_area_struct *, struct page *,
  158. unsigned long, void *, const void *, unsigned long);
  159. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  160. do { \
  161. memcpy(dst, src, len); \
  162. } while (0)
  163. /*
  164. * Convert calls to our calling convention.
  165. */
  166. /* Invalidate I-cache */
  167. #define __flush_icache_all_generic() \
  168. asm("mcr p15, 0, %0, c7, c5, 0" \
  169. : : "r" (0));
  170. /* Invalidate I-cache inner shareable */
  171. #define __flush_icache_all_v7_smp() \
  172. asm("mcr p15, 0, %0, c7, c1, 0" \
  173. : : "r" (0));
  174. /*
  175. * Optimized __flush_icache_all for the common cases. Note that UP ARMv7
  176. * will fall through to use __flush_icache_all_generic.
  177. */
  178. #if (defined(CONFIG_CPU_V7) && \
  179. (defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K))) || \
  180. defined(CONFIG_SMP_ON_UP)
  181. #define __flush_icache_preferred __cpuc_flush_icache_all
  182. #elif __LINUX_ARM_ARCH__ >= 7 && defined(CONFIG_SMP)
  183. #define __flush_icache_preferred __flush_icache_all_v7_smp
  184. #elif __LINUX_ARM_ARCH__ == 6 && defined(CONFIG_ARM_ERRATA_411920)
  185. #define __flush_icache_preferred __cpuc_flush_icache_all
  186. #else
  187. #define __flush_icache_preferred __flush_icache_all_generic
  188. #endif
  189. static inline void __flush_icache_all(void)
  190. {
  191. __flush_icache_preferred();
  192. }
  193. /*
  194. * Flush caches up to Level of Unification Inner Shareable
  195. */
  196. #define flush_cache_louis() __cpuc_flush_kern_louis()
  197. #define flush_cache_all() __cpuc_flush_kern_all()
  198. static inline void vivt_flush_cache_mm(struct mm_struct *mm)
  199. {
  200. if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  201. __cpuc_flush_user_all();
  202. }
  203. static inline void
  204. vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  205. {
  206. struct mm_struct *mm = vma->vm_mm;
  207. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  208. __cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
  209. vma->vm_flags);
  210. }
  211. static inline void
  212. vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  213. {
  214. struct mm_struct *mm = vma->vm_mm;
  215. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
  216. unsigned long addr = user_addr & PAGE_MASK;
  217. __cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
  218. }
  219. }
  220. #ifndef CONFIG_CPU_CACHE_VIPT
  221. #define flush_cache_mm(mm) \
  222. vivt_flush_cache_mm(mm)
  223. #define flush_cache_range(vma,start,end) \
  224. vivt_flush_cache_range(vma,start,end)
  225. #define flush_cache_page(vma,addr,pfn) \
  226. vivt_flush_cache_page(vma,addr,pfn)
  227. #else
  228. extern void flush_cache_mm(struct mm_struct *mm);
  229. extern void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  230. extern void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn);
  231. #endif
  232. #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
  233. /*
  234. * flush_cache_user_range is used when we want to ensure that the
  235. * Harvard caches are synchronised for the user space address range.
  236. * This is used for the ARM private sys_cacheflush system call.
  237. */
  238. #define flush_cache_user_range(start,end) \
  239. __cpuc_coherent_user_range((start) & PAGE_MASK, PAGE_ALIGN(end))
  240. /*
  241. * Perform necessary cache operations to ensure that data previously
  242. * stored within this range of addresses can be executed by the CPU.
  243. */
  244. #define flush_icache_range(s,e) __cpuc_coherent_kern_range(s,e)
  245. /*
  246. * Perform necessary cache operations to ensure that the TLB will
  247. * see data written in the specified area.
  248. */
  249. #define clean_dcache_area(start,size) cpu_dcache_clean_area(start, size)
  250. /*
  251. * flush_dcache_page is used when the kernel has written to the page
  252. * cache page at virtual address page->virtual.
  253. *
  254. * If this page isn't mapped (ie, page_mapping == NULL), or it might
  255. * have userspace mappings, then we _must_ always clean + invalidate
  256. * the dcache entries associated with the kernel mapping.
  257. *
  258. * Otherwise we can defer the operation, and clean the cache when we are
  259. * about to change to user space. This is the same method as used on SPARC64.
  260. * See update_mmu_cache for the user space part.
  261. */
  262. #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
  263. extern void flush_dcache_page(struct page *);
  264. static inline void flush_kernel_vmap_range(void *addr, int size)
  265. {
  266. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  267. __cpuc_flush_dcache_area(addr, (size_t)size);
  268. }
  269. static inline void invalidate_kernel_vmap_range(void *addr, int size)
  270. {
  271. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  272. __cpuc_flush_dcache_area(addr, (size_t)size);
  273. }
  274. #define ARCH_HAS_FLUSH_ANON_PAGE
  275. static inline void flush_anon_page(struct vm_area_struct *vma,
  276. struct page *page, unsigned long vmaddr)
  277. {
  278. extern void __flush_anon_page(struct vm_area_struct *vma,
  279. struct page *, unsigned long);
  280. if (PageAnon(page))
  281. __flush_anon_page(vma, page, vmaddr);
  282. }
  283. #define ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
  284. static inline void flush_kernel_dcache_page(struct page *page)
  285. {
  286. }
  287. #define flush_dcache_mmap_lock(mapping) \
  288. spin_lock_irq(&(mapping)->tree_lock)
  289. #define flush_dcache_mmap_unlock(mapping) \
  290. spin_unlock_irq(&(mapping)->tree_lock)
  291. #define flush_icache_user_range(vma,page,addr,len) \
  292. flush_dcache_page(page)
  293. /*
  294. * We don't appear to need to do anything here. In fact, if we did, we'd
  295. * duplicate cache flushing elsewhere performed by flush_dcache_page().
  296. */
  297. #define flush_icache_page(vma,page) do { } while (0)
  298. /*
  299. * flush_cache_vmap() is used when creating mappings (eg, via vmap,
  300. * vmalloc, ioremap etc) in kernel space for pages. On non-VIPT
  301. * caches, since the direct-mappings of these pages may contain cached
  302. * data, we need to do a full cache flush to ensure that writebacks
  303. * don't corrupt data placed into these pages via the new mappings.
  304. */
  305. static inline void flush_cache_vmap(unsigned long start, unsigned long end)
  306. {
  307. if (!cache_is_vipt_nonaliasing())
  308. flush_cache_all();
  309. else
  310. /*
  311. * set_pte_at() called from vmap_pte_range() does not
  312. * have a DSB after cleaning the cache line.
  313. */
  314. dsb();
  315. }
  316. static inline void flush_cache_vunmap(unsigned long start, unsigned long end)
  317. {
  318. if (!cache_is_vipt_nonaliasing())
  319. flush_cache_all();
  320. }
  321. /*
  322. * Memory synchronization helpers for mixed cached vs non cached accesses.
  323. *
  324. * Some synchronization algorithms have to set states in memory with the
  325. * cache enabled or disabled depending on the code path. It is crucial
  326. * to always ensure proper cache maintenance to update main memory right
  327. * away in that case.
  328. *
  329. * Any cached write must be followed by a cache clean operation.
  330. * Any cached read must be preceded by a cache invalidate operation.
  331. * Yet, in the read case, a cache flush i.e. atomic clean+invalidate
  332. * operation is needed to avoid discarding possible concurrent writes to the
  333. * accessed memory.
  334. *
  335. * Also, in order to prevent a cached writer from interfering with an
  336. * adjacent non-cached writer, each state variable must be located to
  337. * a separate cache line.
  338. */
  339. /*
  340. * This needs to be >= the max cache writeback size of all
  341. * supported platforms included in the current kernel configuration.
  342. * This is used to align state variables to their own cache lines.
  343. */
  344. #define __CACHE_WRITEBACK_ORDER 6 /* guessed from existing platforms */
  345. #define __CACHE_WRITEBACK_GRANULE (1 << __CACHE_WRITEBACK_ORDER)
  346. /*
  347. * There is no __cpuc_clean_dcache_area but we use it anyway for
  348. * code intent clarity, and alias it to __cpuc_flush_dcache_area.
  349. */
  350. #define __cpuc_clean_dcache_area __cpuc_flush_dcache_area
  351. /*
  352. * Ensure preceding writes to *p by this CPU are visible to
  353. * subsequent reads by other CPUs:
  354. */
  355. static inline void __sync_cache_range_w(volatile void *p, size_t size)
  356. {
  357. char *_p = (char *)p;
  358. __cpuc_clean_dcache_area(_p, size);
  359. outer_clean_range(__pa(_p), __pa(_p + size));
  360. }
  361. /*
  362. * Ensure preceding writes to *p by other CPUs are visible to
  363. * subsequent reads by this CPU. We must be careful not to
  364. * discard data simultaneously written by another CPU, hence the
  365. * usage of flush rather than invalidate operations.
  366. */
  367. static inline void __sync_cache_range_r(volatile void *p, size_t size)
  368. {
  369. char *_p = (char *)p;
  370. #ifdef CONFIG_OUTER_CACHE
  371. if (outer_cache.flush_range) {
  372. /*
  373. * Ensure dirty data migrated from other CPUs into our cache
  374. * are cleaned out safely before the outer cache is cleaned:
  375. */
  376. __cpuc_clean_dcache_area(_p, size);
  377. /* Clean and invalidate stale data for *p from outer ... */
  378. outer_flush_range(__pa(_p), __pa(_p + size));
  379. }
  380. #endif
  381. /* ... and inner cache: */
  382. __cpuc_flush_dcache_area(_p, size);
  383. }
  384. #define sync_cache_w(ptr) __sync_cache_range_w(ptr, sizeof *(ptr))
  385. #define sync_cache_r(ptr) __sync_cache_range_r(ptr, sizeof *(ptr))
  386. #endif