cacheflush.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * include/asm-xtensa/cacheflush.h
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * (C) 2001 - 2007 Tensilica Inc.
  9. */
  10. #ifndef _XTENSA_CACHEFLUSH_H
  11. #define _XTENSA_CACHEFLUSH_H
  12. #ifdef __KERNEL__
  13. #include <linux/mm.h>
  14. #include <asm/processor.h>
  15. #include <asm/page.h>
  16. /*
  17. * Lo-level routines for cache flushing.
  18. *
  19. * invalidate data or instruction cache:
  20. *
  21. * __invalidate_icache_all()
  22. * __invalidate_icache_page(adr)
  23. * __invalidate_dcache_page(adr)
  24. * __invalidate_icache_range(from,size)
  25. * __invalidate_dcache_range(from,size)
  26. *
  27. * flush data cache:
  28. *
  29. * __flush_dcache_page(adr)
  30. *
  31. * flush and invalidate data cache:
  32. *
  33. * __flush_invalidate_dcache_all()
  34. * __flush_invalidate_dcache_page(adr)
  35. * __flush_invalidate_dcache_range(from,size)
  36. *
  37. * specials for cache aliasing:
  38. *
  39. * __flush_invalidate_dcache_page_alias(vaddr,paddr)
  40. * __invalidate_icache_page_alias(vaddr,paddr)
  41. */
  42. extern void __invalidate_dcache_all(void);
  43. extern void __invalidate_icache_all(void);
  44. extern void __invalidate_dcache_page(unsigned long);
  45. extern void __invalidate_icache_page(unsigned long);
  46. extern void __invalidate_icache_range(unsigned long, unsigned long);
  47. extern void __invalidate_dcache_range(unsigned long, unsigned long);
  48. #if XCHAL_DCACHE_IS_WRITEBACK
  49. extern void __flush_invalidate_dcache_all(void);
  50. extern void __flush_dcache_page(unsigned long);
  51. extern void __flush_dcache_range(unsigned long, unsigned long);
  52. extern void __flush_invalidate_dcache_page(unsigned long);
  53. extern void __flush_invalidate_dcache_range(unsigned long, unsigned long);
  54. #else
  55. # define __flush_dcache_range(p,s) do { } while(0)
  56. # define __flush_dcache_page(p) do { } while(0)
  57. # define __flush_invalidate_dcache_page(p) __invalidate_dcache_page(p)
  58. # define __flush_invalidate_dcache_range(p,s) __invalidate_dcache_range(p,s)
  59. #endif
  60. #if defined(CONFIG_MMU) && (DCACHE_WAY_SIZE > PAGE_SIZE)
  61. extern void __flush_invalidate_dcache_page_alias(unsigned long, unsigned long);
  62. #else
  63. static inline void __flush_invalidate_dcache_page_alias(unsigned long virt,
  64. unsigned long phys) { }
  65. #endif
  66. #if defined(CONFIG_MMU) && (ICACHE_WAY_SIZE > PAGE_SIZE)
  67. extern void __invalidate_icache_page_alias(unsigned long, unsigned long);
  68. #else
  69. static inline void __invalidate_icache_page_alias(unsigned long virt,
  70. unsigned long phys) { }
  71. #endif
  72. /*
  73. * We have physically tagged caches - nothing to do here -
  74. * unless we have cache aliasing.
  75. *
  76. * Pages can get remapped. Because this might change the 'color' of that page,
  77. * we have to flush the cache before the PTE is changed.
  78. * (see also Documentation/cachetlb.txt)
  79. */
  80. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  81. #define flush_cache_all() \
  82. do { \
  83. __flush_invalidate_dcache_all(); \
  84. __invalidate_icache_all(); \
  85. } while (0)
  86. #define flush_cache_mm(mm) flush_cache_all()
  87. #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
  88. #define flush_cache_vmap(start,end) flush_cache_all()
  89. #define flush_cache_vunmap(start,end) flush_cache_all()
  90. #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
  91. extern void flush_dcache_page(struct page*);
  92. extern void flush_cache_range(struct vm_area_struct*, ulong, ulong);
  93. extern void flush_cache_page(struct vm_area_struct*, unsigned long, unsigned long);
  94. #else
  95. #define flush_cache_all() do { } while (0)
  96. #define flush_cache_mm(mm) do { } while (0)
  97. #define flush_cache_dup_mm(mm) do { } while (0)
  98. #define flush_cache_vmap(start,end) do { } while (0)
  99. #define flush_cache_vunmap(start,end) do { } while (0)
  100. #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 0
  101. #define flush_dcache_page(page) do { } while (0)
  102. #define flush_cache_page(vma,addr,pfn) do { } while (0)
  103. #define flush_cache_range(vma,start,end) do { } while (0)
  104. #endif
  105. /* Ensure consistency between data and instruction cache. */
  106. #define flush_icache_range(start,end) \
  107. do { \
  108. __flush_dcache_range(start, (end) - (start)); \
  109. __invalidate_icache_range(start,(end) - (start)); \
  110. } while (0)
  111. /* This is not required, see Documentation/cachetlb.txt */
  112. #define flush_icache_page(vma,page) do { } while (0)
  113. #define flush_dcache_mmap_lock(mapping) do { } while (0)
  114. #define flush_dcache_mmap_unlock(mapping) do { } while (0)
  115. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  116. extern void copy_to_user_page(struct vm_area_struct*, struct page*,
  117. unsigned long, void*, const void*, unsigned long);
  118. extern void copy_from_user_page(struct vm_area_struct*, struct page*,
  119. unsigned long, void*, const void*, unsigned long);
  120. #else
  121. #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
  122. do { \
  123. memcpy(dst, src, len); \
  124. __flush_dcache_range((unsigned long) dst, len); \
  125. __invalidate_icache_range((unsigned long) dst, len); \
  126. } while (0)
  127. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  128. memcpy(dst, src, len)
  129. #endif
  130. #define XTENSA_CACHEBLK_LOG2 29
  131. #define XTENSA_CACHEBLK_SIZE (1 << XTENSA_CACHEBLK_LOG2)
  132. #define XTENSA_CACHEBLK_MASK (7 << XTENSA_CACHEBLK_LOG2)
  133. #if XCHAL_HAVE_CACHEATTR
  134. static inline u32 xtensa_get_cacheattr(void)
  135. {
  136. u32 r;
  137. asm volatile(" rsr %0, CACHEATTR" : "=a"(r));
  138. return r;
  139. }
  140. static inline u32 xtensa_get_dtlb1(u32 addr)
  141. {
  142. u32 r = addr & XTENSA_CACHEBLK_MASK;
  143. return r | ((xtensa_get_cacheattr() >> (r >> (XTENSA_CACHEBLK_LOG2-2)))
  144. & 0xF);
  145. }
  146. #else
  147. static inline u32 xtensa_get_dtlb1(u32 addr)
  148. {
  149. u32 r;
  150. asm volatile(" rdtlb1 %0, %1" : "=a"(r) : "a"(addr));
  151. asm volatile(" dsync");
  152. return r;
  153. }
  154. static inline u32 xtensa_get_cacheattr(void)
  155. {
  156. u32 r = 0;
  157. u32 a = 0;
  158. do {
  159. a -= XTENSA_CACHEBLK_SIZE;
  160. r = (r << 4) | (xtensa_get_dtlb1(a) & 0xF);
  161. } while (a);
  162. return r;
  163. }
  164. #endif
  165. static inline int xtensa_need_flush_dma_source(u32 addr)
  166. {
  167. return (xtensa_get_dtlb1(addr) & ((1 << XCHAL_CA_BITS) - 1)) >= 4;
  168. }
  169. static inline int xtensa_need_invalidate_dma_destination(u32 addr)
  170. {
  171. return (xtensa_get_dtlb1(addr) & ((1 << XCHAL_CA_BITS) - 1)) != 2;
  172. }
  173. static inline void flush_dcache_unaligned(u32 addr, u32 size)
  174. {
  175. u32 cnt;
  176. if (size) {
  177. cnt = (size + ((XCHAL_DCACHE_LINESIZE - 1) & addr)
  178. + XCHAL_DCACHE_LINESIZE - 1) / XCHAL_DCACHE_LINESIZE;
  179. while (cnt--) {
  180. asm volatile(" dhwb %0, 0" : : "a"(addr));
  181. addr += XCHAL_DCACHE_LINESIZE;
  182. }
  183. asm volatile(" dsync");
  184. }
  185. }
  186. static inline void invalidate_dcache_unaligned(u32 addr, u32 size)
  187. {
  188. int cnt;
  189. if (size) {
  190. asm volatile(" dhwbi %0, 0 ;" : : "a"(addr));
  191. cnt = (size + ((XCHAL_DCACHE_LINESIZE - 1) & addr)
  192. - XCHAL_DCACHE_LINESIZE - 1) / XCHAL_DCACHE_LINESIZE;
  193. while (cnt-- > 0) {
  194. asm volatile(" dhi %0, %1" : : "a"(addr),
  195. "n"(XCHAL_DCACHE_LINESIZE));
  196. addr += XCHAL_DCACHE_LINESIZE;
  197. }
  198. asm volatile(" dhwbi %0, %1" : : "a"(addr),
  199. "n"(XCHAL_DCACHE_LINESIZE));
  200. asm volatile(" dsync");
  201. }
  202. }
  203. static inline void flush_invalidate_dcache_unaligned(u32 addr, u32 size)
  204. {
  205. u32 cnt;
  206. if (size) {
  207. cnt = (size + ((XCHAL_DCACHE_LINESIZE - 1) & addr)
  208. + XCHAL_DCACHE_LINESIZE - 1) / XCHAL_DCACHE_LINESIZE;
  209. while (cnt--) {
  210. asm volatile(" dhwbi %0, 0" : : "a"(addr));
  211. addr += XCHAL_DCACHE_LINESIZE;
  212. }
  213. asm volatile(" dsync");
  214. }
  215. }
  216. #endif /* __KERNEL__ */
  217. #endif /* _XTENSA_CACHEFLUSH_H */