cacheflush.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_inv_range(start, end)
  93. *
  94. * Invalidate (discard) the specified virtual address range.
  95. * May not write back any entries. If 'start' or 'end'
  96. * are not cache line aligned, those lines must be written
  97. * back.
  98. * - start - virtual start address
  99. * - end - virtual end address
  100. *
  101. * dma_clean_range(start, end)
  102. *
  103. * Clean (write back) the specified virtual address range.
  104. * - start - virtual start address
  105. * - end - virtual end address
  106. *
  107. * dma_flush_range(start, end)
  108. *
  109. * Clean and invalidate the specified virtual address range.
  110. * - start - virtual start address
  111. * - end - virtual end address
  112. */
  113. struct cpu_cache_fns {
  114. void (*flush_icache_all)(void);
  115. void (*flush_kern_all)(void);
  116. void (*flush_kern_louis)(void);
  117. void (*flush_user_all)(void);
  118. void (*flush_user_range)(unsigned long, unsigned long, unsigned int);
  119. void (*coherent_kern_range)(unsigned long, unsigned long);
  120. void (*coherent_user_range)(unsigned long, unsigned long);
  121. void (*flush_kern_dcache_area)(void *, size_t);
  122. void (*dma_map_area)(const void *, size_t, int);
  123. void (*dma_unmap_area)(const void *, size_t, int);
  124. void (*dma_inv_range)(const void *, const void *);
  125. void (*dma_clean_range)(const void *, const void *);
  126. void (*dma_flush_range)(const void *, const void *);
  127. };
  128. /*
  129. * Select the calling method
  130. */
  131. #ifdef MULTI_CACHE
  132. extern struct cpu_cache_fns cpu_cache;
  133. #define __cpuc_flush_icache_all cpu_cache.flush_icache_all
  134. #define __cpuc_flush_kern_all cpu_cache.flush_kern_all
  135. #define __cpuc_flush_kern_louis cpu_cache.flush_kern_louis
  136. #define __cpuc_flush_user_all cpu_cache.flush_user_all
  137. #define __cpuc_flush_user_range cpu_cache.flush_user_range
  138. #define __cpuc_coherent_kern_range cpu_cache.coherent_kern_range
  139. #define __cpuc_coherent_user_range cpu_cache.coherent_user_range
  140. #define __cpuc_flush_dcache_area cpu_cache.flush_kern_dcache_area
  141. /*
  142. * These are private to the dma-mapping API. Do not use directly.
  143. * Their sole purpose is to ensure that data held in the cache
  144. * is visible to DMA, or data written by DMA to system memory is
  145. * visible to the CPU.
  146. */
  147. #define dmac_map_area cpu_cache.dma_map_area
  148. #define dmac_unmap_area cpu_cache.dma_unmap_area
  149. #define dmac_inv_range cpu_cache.dma_inv_range
  150. #define dmac_clean_range cpu_cache.dma_clean_range
  151. #define dmac_flush_range cpu_cache.dma_flush_range
  152. #else
  153. extern void __cpuc_flush_icache_all(void);
  154. extern void __cpuc_flush_kern_all(void);
  155. extern void __cpuc_flush_kern_louis(void);
  156. extern void __cpuc_flush_user_all(void);
  157. extern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
  158. extern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
  159. extern void __cpuc_coherent_user_range(unsigned long, unsigned long);
  160. extern void __cpuc_flush_dcache_area(void *, size_t);
  161. /*
  162. * These are private to the dma-mapping API. Do not use directly.
  163. * Their sole purpose is to ensure that data held in the cache
  164. * is visible to DMA, or data written by DMA to system memory is
  165. * visible to the CPU.
  166. */
  167. extern void dmac_map_area(const void *, size_t, int);
  168. extern void dmac_unmap_area(const void *, size_t, int);
  169. extern void dmac_inv_range(const void *, const void *);
  170. extern void dmac_clean_range(const void *, const void *);
  171. extern void dmac_flush_range(const void *, const void *);
  172. #endif
  173. /*
  174. * Copy user data from/to a page which is mapped into a different
  175. * processes address space. Really, we want to allow our "user
  176. * space" model to handle this.
  177. */
  178. extern void copy_to_user_page(struct vm_area_struct *, struct page *,
  179. unsigned long, void *, const void *, unsigned long);
  180. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  181. do { \
  182. memcpy(dst, src, len); \
  183. } while (0)
  184. /*
  185. * Convert calls to our calling convention.
  186. */
  187. /* Invalidate I-cache */
  188. #define __flush_icache_all_generic() \
  189. asm("mcr p15, 0, %0, c7, c5, 0" \
  190. : : "r" (0));
  191. /* Invalidate I-cache inner shareable */
  192. #define __flush_icache_all_v7_smp() \
  193. asm("mcr p15, 0, %0, c7, c1, 0" \
  194. : : "r" (0));
  195. /*
  196. * Optimized __flush_icache_all for the common cases. Note that UP ARMv7
  197. * will fall through to use __flush_icache_all_generic.
  198. */
  199. #if (defined(CONFIG_CPU_V7) && \
  200. (defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K))) || \
  201. defined(CONFIG_SMP_ON_UP)
  202. #define __flush_icache_preferred __cpuc_flush_icache_all
  203. #elif __LINUX_ARM_ARCH__ >= 7 && defined(CONFIG_SMP)
  204. #define __flush_icache_preferred __flush_icache_all_v7_smp
  205. #elif __LINUX_ARM_ARCH__ == 6 && defined(CONFIG_ARM_ERRATA_411920)
  206. #define __flush_icache_preferred __cpuc_flush_icache_all
  207. #else
  208. #define __flush_icache_preferred __flush_icache_all_generic
  209. #endif
  210. static inline void __flush_icache_all(void)
  211. {
  212. __flush_icache_preferred();
  213. dsb();
  214. }
  215. /*
  216. * Flush caches up to Level of Unification Inner Shareable
  217. */
  218. #define flush_cache_louis() __cpuc_flush_kern_louis()
  219. #define flush_cache_all() __cpuc_flush_kern_all()
  220. static inline void vivt_flush_cache_mm(struct mm_struct *mm)
  221. {
  222. if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  223. __cpuc_flush_user_all();
  224. }
  225. static inline void
  226. vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  227. {
  228. struct mm_struct *mm = vma->vm_mm;
  229. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
  230. __cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
  231. vma->vm_flags);
  232. }
  233. static inline void
  234. vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  235. {
  236. struct mm_struct *mm = vma->vm_mm;
  237. if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
  238. unsigned long addr = user_addr & PAGE_MASK;
  239. __cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
  240. }
  241. }
  242. #ifndef CONFIG_CPU_CACHE_VIPT
  243. #define flush_cache_mm(mm) \
  244. vivt_flush_cache_mm(mm)
  245. #define flush_cache_range(vma,start,end) \
  246. vivt_flush_cache_range(vma,start,end)
  247. #define flush_cache_page(vma,addr,pfn) \
  248. vivt_flush_cache_page(vma,addr,pfn)
  249. #else
  250. extern void flush_cache_mm(struct mm_struct *mm);
  251. extern void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  252. extern void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn);
  253. #endif
  254. #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
  255. /*
  256. * flush_cache_user_range is used when we want to ensure that the
  257. * Harvard caches are synchronised for the user space address range.
  258. * This is used for the ARM private sys_cacheflush system call.
  259. */
  260. #define flush_cache_user_range(start,end) \
  261. __cpuc_coherent_user_range((start) & PAGE_MASK, PAGE_ALIGN(end))
  262. /*
  263. * Perform necessary cache operations to ensure that data previously
  264. * stored within this range of addresses can be executed by the CPU.
  265. */
  266. #define flush_icache_range(s,e) __cpuc_coherent_kern_range(s,e)
  267. /*
  268. * Perform necessary cache operations to ensure that the TLB will
  269. * see data written in the specified area.
  270. */
  271. #define clean_dcache_area(start,size) cpu_dcache_clean_area(start, size)
  272. /*
  273. * flush_dcache_page is used when the kernel has written to the page
  274. * cache page at virtual address page->virtual.
  275. *
  276. * If this page isn't mapped (ie, page_mapping == NULL), or it might
  277. * have userspace mappings, then we _must_ always clean + invalidate
  278. * the dcache entries associated with the kernel mapping.
  279. *
  280. * Otherwise we can defer the operation, and clean the cache when we are
  281. * about to change to user space. This is the same method as used on SPARC64.
  282. * See update_mmu_cache for the user space part.
  283. */
  284. #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
  285. extern void flush_dcache_page(struct page *);
  286. static inline void flush_kernel_vmap_range(void *addr, int size)
  287. {
  288. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  289. __cpuc_flush_dcache_area(addr, (size_t)size);
  290. }
  291. static inline void invalidate_kernel_vmap_range(void *addr, int size)
  292. {
  293. if ((cache_is_vivt() || cache_is_vipt_aliasing()))
  294. __cpuc_flush_dcache_area(addr, (size_t)size);
  295. }
  296. #define ARCH_HAS_FLUSH_ANON_PAGE
  297. static inline void flush_anon_page(struct vm_area_struct *vma,
  298. struct page *page, unsigned long vmaddr)
  299. {
  300. extern void __flush_anon_page(struct vm_area_struct *vma,
  301. struct page *, unsigned long);
  302. if (PageAnon(page))
  303. __flush_anon_page(vma, page, vmaddr);
  304. }
  305. #define ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
  306. extern void flush_kernel_dcache_page(struct page *);
  307. #define flush_dcache_mmap_lock(mapping) \
  308. spin_lock_irq(&(mapping)->tree_lock)
  309. #define flush_dcache_mmap_unlock(mapping) \
  310. spin_unlock_irq(&(mapping)->tree_lock)
  311. #define flush_icache_user_range(vma,page,addr,len) \
  312. flush_dcache_page(page)
  313. /*
  314. * We don't appear to need to do anything here. In fact, if we did, we'd
  315. * duplicate cache flushing elsewhere performed by flush_dcache_page().
  316. */
  317. #define flush_icache_page(vma,page) do { } while (0)
  318. /*
  319. * flush_cache_vmap() is used when creating mappings (eg, via vmap,
  320. * vmalloc, ioremap etc) in kernel space for pages. On non-VIPT
  321. * caches, since the direct-mappings of these pages may contain cached
  322. * data, we need to do a full cache flush to ensure that writebacks
  323. * don't corrupt data placed into these pages via the new mappings.
  324. */
  325. static inline void flush_cache_vmap(unsigned long start, unsigned long end)
  326. {
  327. if (!cache_is_vipt_nonaliasing())
  328. flush_cache_all();
  329. else
  330. /*
  331. * set_pte_at() called from vmap_pte_range() does not
  332. * have a DSB after cleaning the cache line.
  333. */
  334. dsb();
  335. }
  336. static inline void flush_cache_vunmap(unsigned long start, unsigned long end)
  337. {
  338. if (!cache_is_vipt_nonaliasing())
  339. flush_cache_all();
  340. }
  341. int set_memory_ro(unsigned long addr, int numpages);
  342. int set_memory_rw(unsigned long addr, int numpages);
  343. int set_memory_x(unsigned long addr, int numpages);
  344. int set_memory_nx(unsigned long addr, int numpages);
  345. #ifdef CONFIG_FREE_PAGES_RDONLY
  346. #define mark_addr_rdonly(a) set_memory_ro((unsigned long)a, 1);
  347. #define mark_addr_rdwrite(a) set_memory_rw((unsigned long)a, 1);
  348. #else
  349. #define mark_addr_rdonly(a)
  350. #define mark_addr_rdwrite(a)
  351. #endif
  352. #endif