cacheflush.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_user_all()
  48. *
  49. * Clean and invalidate all user space cache entries
  50. * before a change of page tables.
  51. *
  52. * flush_user_range(start, end, flags)
  53. *
  54. * Clean and invalidate a range of cache entries in the
  55. * specified address space before a change of page tables.
  56. * - start - user start address (inclusive, page aligned)
  57. * - end - user end address (exclusive, page aligned)
  58. * - flags - vma->vm_flags field
  59. *
  60. * coherent_kern_range(start, end)
  61. *
  62. * Ensure coherency between the Icache and the Dcache in the
  63. * region described by start, end. If you have non-snooping
  64. * Harvard caches, you need to implement this function.
  65. * - start - virtual start address
  66. * - end - virtual end address
  67. *
  68. * coherent_user_range(start, end)
  69. *
  70. * Ensure coherency between the Icache and the Dcache in the
  71. * region described by start, end. If you have non-snooping
  72. * Harvard caches, you need to implement this function.
  73. * - start - virtual start address
  74. * - end - virtual end address
  75. *
  76. * flush_kern_dcache_area(kaddr, size)
  77. *
  78. * Ensure that the data held in page is written back.
  79. * - kaddr - page address
  80. * - size - region size
  81. *
  82. * DMA Cache Coherency
  83. * ===================
  84. *
  85. * dma_flush_range(start, end)
  86. *
  87. * Clean and invalidate the specified virtual address range.
  88. * - start - virtual start address
  89. * - end - virtual end address
  90. */
  91. struct cpu_cache_fns {
  92. void (*flush_icache_all)(void);
  93. void (*flush_kern_all)(void);
  94. void (*flush_user_all)(void);
  95. void (*flush_user_range)(unsigned long, unsigned long, unsigned int);
  96. void (*coherent_kern_range)(unsigned long, unsigned long);
  97. int (*coherent_user_range)(unsigned long, unsigned long);
  98. void (*flush_kern_dcache_area)(void *, size_t);
  99. void (*dma_map_area)(const void *, size_t, int);
  100. void (*dma_unmap_area)(const void *, size_t, int);
  101. void (*dma_flush_range)(const void *, const void *);
  102. };
  103. /*
  104. * Select the calling method
  105. */
  106. #ifdef MULTI_CACHE
  107. extern struct cpu_cache_fns cpu_cache;
  108. #define __cpuc_flush_icache_all cpu_cache.flush_icache_all
  109. #define __cpuc_flush_kern_all cpu_cache.flush_kern_all
  110. #define __cpuc_flush_user_all cpu_cache.flush_user_all
  111. #define __cpuc_flush_user_range cpu_cache.flush_user_range
  112. #define __cpuc_coherent_kern_range cpu_cache.coherent_kern_range
  113. #define __cpuc_coherent_user_range cpu_cache.coherent_user_range
  114. #define __cpuc_flush_dcache_area cpu_cache.flush_kern_dcache_area
  115. /*
  116. * These are private to the dma-mapping API. Do not use directly.
  117. * Their sole purpose is to ensure that data held in the cache
  118. * is visible to DMA, or data written by DMA to system memory is
  119. * visible to the CPU.
  120. */
  121. #define dmac_map_area cpu_cache.dma_map_area
  122. #define dmac_unmap_area cpu_cache.dma_unmap_area
  123. #define dmac_flush_range cpu_cache.dma_flush_range
  124. #else
  125. extern void __cpuc_flush_icache_all(void);
  126. extern void __cpuc_flush_kern_all(void);
  127. extern void __cpuc_flush_user_all(void);
  128. extern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
  129. extern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
  130. extern int __cpuc_coherent_user_range(unsigned long, unsigned long);
  131. extern void __cpuc_flush_dcache_area(void *, size_t);
  132. /*
  133. * These are private to the dma-mapping API. Do not use directly.
  134. * Their sole purpose is to ensure that data held in the cache
  135. * is visible to DMA, or data written by DMA to system memory is
  136. * visible to the CPU.
  137. */
  138. extern void dmac_map_area(const void *, size_t, int);
  139. extern void dmac_unmap_area(const void *, size_t, int);
  140. extern void dmac_flush_range(const void *, const void *);
  141. #endif
  142. /*
  143. * Copy user data from/to a page which is mapped into a different
  144. * processes address space. Really, we want to allow our "user
  145. * space" model to handle this.
  146. */
  147. extern void copy_to_user_page(struct vm_area_struct *, struct page *,
  148. unsigned long, void *, const void *, unsigned long);
  149. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  150. do { \
  151. memcpy(dst, src, len); \
  152. } while (0)
  153. /*
  154. * Convert calls to our calling convention.
  155. */
  156. /* Invalidate I-cache */
  157. #define __flush_icache_all_generic() \
  158. asm("mcr p15, 0, %0, c7, c5, 0" \
  159. : : "r" (0));
  160. /* Invalidate I-cache inner shareable */
  161. #define __flush_icache_all_v7_smp() \
  162. asm("mcr p15, 0, %0, c7, c1, 0" \
  163. : : "r" (0));
  164. /*
  165. * Optimized __flush_icache_all for the common cases. Note that UP ARMv7
  166. * will fall through to use __flush_icache_all_generic.
  167. */
  168. #if (defined(CONFIG_CPU_V7) && \
  169. (defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K))) || \
  170. defined(CONFIG_SMP_ON_UP)
  171. #define __flush_icache_preferred __cpuc_flush_icache_all
  172. #elif __LINUX_ARM_ARCH__ >= 7 && defined(CONFIG_SMP)
  173. #define __flush_icache_preferred __flush_icache_all_v7_smp
  174. #elif __LINUX_ARM_ARCH__ == 6 && defined(CONFIG_ARM_ERRATA_411920)
  175. #define __flush_icache_preferred __cpuc_flush_icache_all
  176. #else
  177. #define __flush_icache_preferred __flush_icache_all_generic
  178. #endif
  179. static inline void __flush_icache_all(void)
  180. {
  181. __flush_icache_preferred();
  182. }
  183. #define flush_cache_all() __cpuc_flush_kern_all()
  184. static inline void vivt_flush_cache_mm(struct mm_struct *mm)
  185. {
  186. if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  187. __cpuc_flush_user_all();
  188. }
  189. static inline void
  190. vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  191. {
  192. struct mm_struct *mm = vma->vm_mm;
  193. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  194. __cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
  195. vma->vm_flags);
  196. }
  197. static inline void
  198. vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  199. {
  200. struct mm_struct *mm = vma->vm_mm;
  201. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
  202. unsigned long addr = user_addr & PAGE_MASK;
  203. __cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
  204. }
  205. }
  206. #ifndef CONFIG_CPU_CACHE_VIPT
  207. #define flush_cache_mm(mm) \
  208. vivt_flush_cache_mm(mm)
  209. #define flush_cache_range(vma,start,end) \
  210. vivt_flush_cache_range(vma,start,end)
  211. #define flush_cache_page(vma,addr,pfn) \
  212. vivt_flush_cache_page(vma,addr,pfn)
  213. #else
  214. extern void flush_cache_mm(struct mm_struct *mm);
  215. extern void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  216. extern void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn);
  217. #endif
  218. #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
  219. /*
  220. * flush_cache_user_range is used when we want to ensure that the
  221. * Harvard caches are synchronised for the user space address range.
  222. * This is used for the ARM private sys_cacheflush system call.
  223. */
  224. #define flush_cache_user_range(start,end) \
  225. __cpuc_coherent_user_range((start) & PAGE_MASK, PAGE_ALIGN(end))
  226. /*
  227. * Perform necessary cache operations to ensure that data previously
  228. * stored within this range of addresses can be executed by the CPU.
  229. */
  230. #define flush_icache_range(s,e) __cpuc_coherent_kern_range(s,e)
  231. /*
  232. * Perform necessary cache operations to ensure that the TLB will
  233. * see data written in the specified area.
  234. */
  235. #define clean_dcache_area(start,size) cpu_dcache_clean_area(start, size)
  236. /*
  237. * flush_dcache_page is used when the kernel has written to the page
  238. * cache page at virtual address page->virtual.
  239. *
  240. * If this page isn't mapped (ie, page_mapping == NULL), or it might
  241. * have userspace mappings, then we _must_ always clean + invalidate
  242. * the dcache entries associated with the kernel mapping.
  243. *
  244. * Otherwise we can defer the operation, and clean the cache when we are
  245. * about to change to user space. This is the same method as used on SPARC64.
  246. * See update_mmu_cache for the user space part.
  247. */
  248. #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
  249. extern void flush_dcache_page(struct page *);
  250. static inline void flush_kernel_vmap_range(void *addr, int size)
  251. {
  252. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  253. __cpuc_flush_dcache_area(addr, (size_t)size);
  254. }
  255. static inline void invalidate_kernel_vmap_range(void *addr, int size)
  256. {
  257. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  258. __cpuc_flush_dcache_area(addr, (size_t)size);
  259. }
  260. #define ARCH_HAS_FLUSH_ANON_PAGE
  261. static inline void flush_anon_page(struct vm_area_struct *vma,
  262. struct page *page, unsigned long vmaddr)
  263. {
  264. extern void __flush_anon_page(struct vm_area_struct *vma,
  265. struct page *, unsigned long);
  266. if (PageAnon(page))
  267. __flush_anon_page(vma, page, vmaddr);
  268. }
  269. #define ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
  270. static inline void flush_kernel_dcache_page(struct page *page)
  271. {
  272. }
  273. #define flush_dcache_mmap_lock(mapping) \
  274. spin_lock_irq(&(mapping)->tree_lock)
  275. #define flush_dcache_mmap_unlock(mapping) \
  276. spin_unlock_irq(&(mapping)->tree_lock)
  277. #define flush_icache_user_range(vma,page,addr,len) \
  278. flush_dcache_page(page)
  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();
  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. #endif