cacheflush.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * linux/include/asm-arm/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/sched.h>
  13. #include <linux/mm.h>
  14. #include <asm/glue.h>
  15. #include <asm/shmparam.h>
  16. #define CACHE_COLOUR(vaddr) ((vaddr & (SHMLBA - 1)) >> PAGE_SHIFT)
  17. /*
  18. * Cache Model
  19. * ===========
  20. */
  21. #undef _CACHE
  22. #undef MULTI_CACHE
  23. #if defined(CONFIG_CPU_CACHE_V3)
  24. # ifdef _CACHE
  25. # define MULTI_CACHE 1
  26. # else
  27. # define _CACHE v3
  28. # endif
  29. #endif
  30. #if defined(CONFIG_CPU_CACHE_V4)
  31. # ifdef _CACHE
  32. # define MULTI_CACHE 1
  33. # else
  34. # define _CACHE v4
  35. # endif
  36. #endif
  37. #if defined(CONFIG_CPU_ARM920T) || defined(CONFIG_CPU_ARM922T) || \
  38. defined(CONFIG_CPU_ARM925T) || defined(CONFIG_CPU_ARM1020)
  39. # define MULTI_CACHE 1
  40. #endif
  41. #if defined(CONFIG_CPU_ARM926T)
  42. # ifdef _CACHE
  43. # define MULTI_CACHE 1
  44. # else
  45. # define _CACHE arm926
  46. # endif
  47. #endif
  48. #if defined(CONFIG_CPU_ARM940T)
  49. # ifdef _CACHE
  50. # define MULTI_CACHE 1
  51. # else
  52. # define _CACHE arm940
  53. # endif
  54. #endif
  55. #if defined(CONFIG_CPU_ARM946E)
  56. # ifdef _CACHE
  57. # define MULTI_CACHE 1
  58. # else
  59. # define _CACHE arm946
  60. # endif
  61. #endif
  62. #if defined(CONFIG_CPU_CACHE_V4WB)
  63. # ifdef _CACHE
  64. # define MULTI_CACHE 1
  65. # else
  66. # define _CACHE v4wb
  67. # endif
  68. #endif
  69. #if defined(CONFIG_CPU_XSCALE)
  70. # ifdef _CACHE
  71. # define MULTI_CACHE 1
  72. # else
  73. # define _CACHE xscale
  74. # endif
  75. #endif
  76. #if defined(CONFIG_CPU_XSC3)
  77. # ifdef _CACHE
  78. # define MULTI_CACHE 1
  79. # else
  80. # define _CACHE xsc3
  81. # endif
  82. #endif
  83. #if defined(CONFIG_CPU_FEROCEON)
  84. # ifdef _CACHE
  85. # define MULTI_CACHE 1
  86. # else
  87. # define _CACHE feroceon
  88. # endif
  89. #endif
  90. #if defined(CONFIG_CPU_V6)
  91. //# ifdef _CACHE
  92. # define MULTI_CACHE 1
  93. //# else
  94. //# define _CACHE v6
  95. //# endif
  96. #endif
  97. #if defined(CONFIG_CPU_V7)
  98. //# ifdef _CACHE
  99. # define MULTI_CACHE 1
  100. //# else
  101. //# define _CACHE v7
  102. //# endif
  103. #endif
  104. #if !defined(_CACHE) && !defined(MULTI_CACHE)
  105. #error Unknown cache maintainence model
  106. #endif
  107. /*
  108. * This flag is used to indicate that the page pointed to by a pte
  109. * is dirty and requires cleaning before returning it to the user.
  110. */
  111. #define PG_dcache_dirty PG_arch_1
  112. /*
  113. * MM Cache Management
  114. * ===================
  115. *
  116. * The arch/arm/mm/cache-*.S and arch/arm/mm/proc-*.S files
  117. * implement these methods.
  118. *
  119. * Start addresses are inclusive and end addresses are exclusive;
  120. * start addresses should be rounded down, end addresses up.
  121. *
  122. * See Documentation/cachetlb.txt for more information.
  123. * Please note that the implementation of these, and the required
  124. * effects are cache-type (VIVT/VIPT/PIPT) specific.
  125. *
  126. * flush_cache_kern_all()
  127. *
  128. * Unconditionally clean and invalidate the entire cache.
  129. *
  130. * flush_cache_user_mm(mm)
  131. *
  132. * Clean and invalidate all user space cache entries
  133. * before a change of page tables.
  134. *
  135. * flush_cache_user_range(start, end, flags)
  136. *
  137. * Clean and invalidate a range of cache entries in the
  138. * specified address space before a change of page tables.
  139. * - start - user start address (inclusive, page aligned)
  140. * - end - user end address (exclusive, page aligned)
  141. * - flags - vma->vm_flags field
  142. *
  143. * coherent_kern_range(start, end)
  144. *
  145. * Ensure coherency between the Icache and the Dcache in the
  146. * region described by start, end. If you have non-snooping
  147. * Harvard caches, you need to implement this function.
  148. * - start - virtual start address
  149. * - end - virtual end address
  150. *
  151. * DMA Cache Coherency
  152. * ===================
  153. *
  154. * dma_inv_range(start, end)
  155. *
  156. * Invalidate (discard) the specified virtual address range.
  157. * May not write back any entries. If 'start' or 'end'
  158. * are not cache line aligned, those lines must be written
  159. * back.
  160. * - start - virtual start address
  161. * - end - virtual end address
  162. *
  163. * dma_clean_range(start, end)
  164. *
  165. * Clean (write back) the specified virtual address range.
  166. * - start - virtual start address
  167. * - end - virtual end address
  168. *
  169. * dma_flush_range(start, end)
  170. *
  171. * Clean and invalidate the specified virtual address range.
  172. * - start - virtual start address
  173. * - end - virtual end address
  174. */
  175. struct cpu_cache_fns {
  176. void (*flush_kern_all)(void);
  177. void (*flush_user_all)(void);
  178. void (*flush_user_range)(unsigned long, unsigned long, unsigned int);
  179. void (*coherent_kern_range)(unsigned long, unsigned long);
  180. void (*coherent_user_range)(unsigned long, unsigned long);
  181. void (*flush_kern_dcache_page)(void *);
  182. void (*dma_inv_range)(const void *, const void *);
  183. void (*dma_clean_range)(const void *, const void *);
  184. void (*dma_flush_range)(const void *, const void *);
  185. };
  186. struct outer_cache_fns {
  187. void (*inv_range)(unsigned long, unsigned long);
  188. void (*clean_range)(unsigned long, unsigned long);
  189. void (*flush_range)(unsigned long, unsigned long);
  190. };
  191. /*
  192. * Select the calling method
  193. */
  194. #ifdef MULTI_CACHE
  195. extern struct cpu_cache_fns cpu_cache;
  196. #define __cpuc_flush_kern_all cpu_cache.flush_kern_all
  197. #define __cpuc_flush_user_all cpu_cache.flush_user_all
  198. #define __cpuc_flush_user_range cpu_cache.flush_user_range
  199. #define __cpuc_coherent_kern_range cpu_cache.coherent_kern_range
  200. #define __cpuc_coherent_user_range cpu_cache.coherent_user_range
  201. #define __cpuc_flush_dcache_page cpu_cache.flush_kern_dcache_page
  202. /*
  203. * These are private to the dma-mapping API. Do not use directly.
  204. * Their sole purpose is to ensure that data held in the cache
  205. * is visible to DMA, or data written by DMA to system memory is
  206. * visible to the CPU.
  207. */
  208. #define dmac_inv_range cpu_cache.dma_inv_range
  209. #define dmac_clean_range cpu_cache.dma_clean_range
  210. #define dmac_flush_range cpu_cache.dma_flush_range
  211. #else
  212. #define __cpuc_flush_kern_all __glue(_CACHE,_flush_kern_cache_all)
  213. #define __cpuc_flush_user_all __glue(_CACHE,_flush_user_cache_all)
  214. #define __cpuc_flush_user_range __glue(_CACHE,_flush_user_cache_range)
  215. #define __cpuc_coherent_kern_range __glue(_CACHE,_coherent_kern_range)
  216. #define __cpuc_coherent_user_range __glue(_CACHE,_coherent_user_range)
  217. #define __cpuc_flush_dcache_page __glue(_CACHE,_flush_kern_dcache_page)
  218. extern void __cpuc_flush_kern_all(void);
  219. extern void __cpuc_flush_user_all(void);
  220. extern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
  221. extern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
  222. extern void __cpuc_coherent_user_range(unsigned long, unsigned long);
  223. extern void __cpuc_flush_dcache_page(void *);
  224. /*
  225. * These are private to the dma-mapping API. Do not use directly.
  226. * Their sole purpose is to ensure that data held in the cache
  227. * is visible to DMA, or data written by DMA to system memory is
  228. * visible to the CPU.
  229. */
  230. #define dmac_inv_range __glue(_CACHE,_dma_inv_range)
  231. #define dmac_clean_range __glue(_CACHE,_dma_clean_range)
  232. #define dmac_flush_range __glue(_CACHE,_dma_flush_range)
  233. extern void dmac_inv_range(const void *, const void *);
  234. extern void dmac_clean_range(const void *, const void *);
  235. extern void dmac_flush_range(const void *, const void *);
  236. #endif
  237. #ifdef CONFIG_OUTER_CACHE
  238. extern struct outer_cache_fns outer_cache;
  239. static inline void outer_inv_range(unsigned long start, unsigned long end)
  240. {
  241. if (outer_cache.inv_range)
  242. outer_cache.inv_range(start, end);
  243. }
  244. static inline void outer_clean_range(unsigned long start, unsigned long end)
  245. {
  246. if (outer_cache.clean_range)
  247. outer_cache.clean_range(start, end);
  248. }
  249. static inline void outer_flush_range(unsigned long start, unsigned long end)
  250. {
  251. if (outer_cache.flush_range)
  252. outer_cache.flush_range(start, end);
  253. }
  254. #else
  255. static inline void outer_inv_range(unsigned long start, unsigned long end)
  256. { }
  257. static inline void outer_clean_range(unsigned long start, unsigned long end)
  258. { }
  259. static inline void outer_flush_range(unsigned long start, unsigned long end)
  260. { }
  261. #endif
  262. /*
  263. * flush_cache_vmap() is used when creating mappings (eg, via vmap,
  264. * vmalloc, ioremap etc) in kernel space for pages. Since the
  265. * direct-mappings of these pages may contain cached data, we need
  266. * to do a full cache flush to ensure that writebacks don't corrupt
  267. * data placed into these pages via the new mappings.
  268. */
  269. #define flush_cache_vmap(start, end) flush_cache_all()
  270. #define flush_cache_vunmap(start, end) flush_cache_all()
  271. /*
  272. * Copy user data from/to a page which is mapped into a different
  273. * processes address space. Really, we want to allow our "user
  274. * space" model to handle this.
  275. */
  276. #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
  277. do { \
  278. memcpy(dst, src, len); \
  279. flush_ptrace_access(vma, page, vaddr, dst, len, 1);\
  280. } while (0)
  281. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  282. do { \
  283. memcpy(dst, src, len); \
  284. } while (0)
  285. /*
  286. * Convert calls to our calling convention.
  287. */
  288. #define flush_cache_all() __cpuc_flush_kern_all()
  289. #ifndef CONFIG_CPU_CACHE_VIPT
  290. static inline void flush_cache_mm(struct mm_struct *mm)
  291. {
  292. if (cpu_isset(smp_processor_id(), mm->cpu_vm_mask))
  293. __cpuc_flush_user_all();
  294. }
  295. static inline void
  296. flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  297. {
  298. if (cpu_isset(smp_processor_id(), vma->vm_mm->cpu_vm_mask))
  299. __cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
  300. vma->vm_flags);
  301. }
  302. static inline void
  303. flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  304. {
  305. if (cpu_isset(smp_processor_id(), vma->vm_mm->cpu_vm_mask)) {
  306. unsigned long addr = user_addr & PAGE_MASK;
  307. __cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
  308. }
  309. }
  310. static inline void
  311. flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
  312. unsigned long uaddr, void *kaddr,
  313. unsigned long len, int write)
  314. {
  315. if (cpu_isset(smp_processor_id(), vma->vm_mm->cpu_vm_mask)) {
  316. unsigned long addr = (unsigned long)kaddr;
  317. __cpuc_coherent_kern_range(addr, addr + len);
  318. }
  319. }
  320. #else
  321. extern void flush_cache_mm(struct mm_struct *mm);
  322. extern void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
  323. extern void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn);
  324. extern void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
  325. unsigned long uaddr, void *kaddr,
  326. unsigned long len, int write);
  327. #endif
  328. #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
  329. /*
  330. * flush_cache_user_range is used when we want to ensure that the
  331. * Harvard caches are synchronised for the user space address range.
  332. * This is used for the ARM private sys_cacheflush system call.
  333. */
  334. #define flush_cache_user_range(vma,start,end) \
  335. __cpuc_coherent_user_range((start) & PAGE_MASK, PAGE_ALIGN(end))
  336. /*
  337. * Perform necessary cache operations to ensure that data previously
  338. * stored within this range of addresses can be executed by the CPU.
  339. */
  340. #define flush_icache_range(s,e) __cpuc_coherent_kern_range(s,e)
  341. /*
  342. * Perform necessary cache operations to ensure that the TLB will
  343. * see data written in the specified area.
  344. */
  345. #define clean_dcache_area(start,size) cpu_dcache_clean_area(start, size)
  346. /*
  347. * flush_dcache_page is used when the kernel has written to the page
  348. * cache page at virtual address page->virtual.
  349. *
  350. * If this page isn't mapped (ie, page_mapping == NULL), or it might
  351. * have userspace mappings, then we _must_ always clean + invalidate
  352. * the dcache entries associated with the kernel mapping.
  353. *
  354. * Otherwise we can defer the operation, and clean the cache when we are
  355. * about to change to user space. This is the same method as used on SPARC64.
  356. * See update_mmu_cache for the user space part.
  357. */
  358. extern void flush_dcache_page(struct page *);
  359. extern void __flush_dcache_page(struct address_space *mapping, struct page *page);
  360. #define ARCH_HAS_FLUSH_ANON_PAGE
  361. static inline void flush_anon_page(struct vm_area_struct *vma,
  362. struct page *page, unsigned long vmaddr)
  363. {
  364. extern void __flush_anon_page(struct vm_area_struct *vma,
  365. struct page *, unsigned long);
  366. if (PageAnon(page))
  367. __flush_anon_page(vma, page, vmaddr);
  368. }
  369. #define flush_dcache_mmap_lock(mapping) \
  370. write_lock_irq(&(mapping)->tree_lock)
  371. #define flush_dcache_mmap_unlock(mapping) \
  372. write_unlock_irq(&(mapping)->tree_lock)
  373. #define flush_icache_user_range(vma,page,addr,len) \
  374. flush_dcache_page(page)
  375. /*
  376. * We don't appear to need to do anything here. In fact, if we did, we'd
  377. * duplicate cache flushing elsewhere performed by flush_dcache_page().
  378. */
  379. #define flush_icache_page(vma,page) do { } while (0)
  380. static inline void flush_ioremap_region(unsigned long phys, void __iomem *virt,
  381. unsigned offset, size_t size)
  382. {
  383. const void *start = (void __force *)virt + offset;
  384. dmac_inv_range(start, start + size);
  385. }
  386. #define __cacheid_present(val) (val != read_cpuid(CPUID_ID))
  387. #define __cacheid_type_v7(val) ((val & (7 << 29)) == (4 << 29))
  388. #define __cacheid_vivt_prev7(val) ((val & (15 << 25)) != (14 << 25))
  389. #define __cacheid_vipt_prev7(val) ((val & (15 << 25)) == (14 << 25))
  390. #define __cacheid_vipt_nonaliasing_prev7(val) ((val & (15 << 25 | 1 << 23)) == (14 << 25))
  391. #define __cacheid_vipt_aliasing_prev7(val) ((val & (15 << 25 | 1 << 23)) == (14 << 25 | 1 << 23))
  392. #define __cacheid_vivt(val) (__cacheid_type_v7(val) ? 0 : __cacheid_vivt_prev7(val))
  393. #define __cacheid_vipt(val) (__cacheid_type_v7(val) ? 1 : __cacheid_vipt_prev7(val))
  394. #define __cacheid_vipt_nonaliasing(val) (__cacheid_type_v7(val) ? 1 : __cacheid_vipt_nonaliasing_prev7(val))
  395. #define __cacheid_vipt_aliasing(val) (__cacheid_type_v7(val) ? 0 : __cacheid_vipt_aliasing_prev7(val))
  396. #define __cacheid_vivt_asid_tagged_instr(val) (__cacheid_type_v7(val) ? ((val & (3 << 14)) == (1 << 14)) : 0)
  397. #if defined(CONFIG_CPU_CACHE_VIVT) && !defined(CONFIG_CPU_CACHE_VIPT)
  398. #define cache_is_vivt() 1
  399. #define cache_is_vipt() 0
  400. #define cache_is_vipt_nonaliasing() 0
  401. #define cache_is_vipt_aliasing() 0
  402. #define icache_is_vivt_asid_tagged() 0
  403. #elif defined(CONFIG_CPU_CACHE_VIPT)
  404. #define cache_is_vivt() 0
  405. #define cache_is_vipt() 1
  406. #define cache_is_vipt_nonaliasing() \
  407. ({ \
  408. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  409. __cacheid_vipt_nonaliasing(__val); \
  410. })
  411. #define cache_is_vipt_aliasing() \
  412. ({ \
  413. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  414. __cacheid_vipt_aliasing(__val); \
  415. })
  416. #define icache_is_vivt_asid_tagged() \
  417. ({ \
  418. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  419. __cacheid_vivt_asid_tagged_instr(__val); \
  420. })
  421. #else
  422. #define cache_is_vivt() \
  423. ({ \
  424. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  425. (!__cacheid_present(__val)) || __cacheid_vivt(__val); \
  426. })
  427. #define cache_is_vipt() \
  428. ({ \
  429. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  430. __cacheid_present(__val) && __cacheid_vipt(__val); \
  431. })
  432. #define cache_is_vipt_nonaliasing() \
  433. ({ \
  434. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  435. __cacheid_present(__val) && \
  436. __cacheid_vipt_nonaliasing(__val); \
  437. })
  438. #define cache_is_vipt_aliasing() \
  439. ({ \
  440. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  441. __cacheid_present(__val) && \
  442. __cacheid_vipt_aliasing(__val); \
  443. })
  444. #define icache_is_vivt_asid_tagged() \
  445. ({ \
  446. unsigned int __val = read_cpuid(CPUID_CACHETYPE); \
  447. __cacheid_present(__val) && \
  448. __cacheid_vivt_asid_tagged_instr(__val); \
  449. })
  450. #endif
  451. #endif