cacheflush.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 flush_dcache_page(page) do { } while (0)
  101. #define flush_cache_page(vma,addr,pfn) do { } while (0)
  102. #define flush_cache_range(vma,start,end) do { } while (0)
  103. #endif
  104. /* Ensure consistency between data and instruction cache. */
  105. #define flush_icache_range(start,end) \
  106. do { \
  107. __flush_dcache_range(start, (end) - (start)); \
  108. __invalidate_icache_range(start,(end) - (start)); \
  109. } while (0)
  110. /* This is not required, see Documentation/cachetlb.txt */
  111. #define flush_icache_page(vma,page) do { } while (0)
  112. #define flush_dcache_mmap_lock(mapping) do { } while (0)
  113. #define flush_dcache_mmap_unlock(mapping) do { } while (0)
  114. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  115. extern void copy_to_user_page(struct vm_area_struct*, struct page*,
  116. unsigned long, void*, const void*, unsigned long);
  117. extern void copy_from_user_page(struct vm_area_struct*, struct page*,
  118. unsigned long, void*, const void*, unsigned long);
  119. #else
  120. #define copy_to_user_page(vma, page, vaddr, dst, src, len) \
  121. do { \
  122. memcpy(dst, src, len); \
  123. __flush_dcache_range((unsigned long) dst, len); \
  124. __invalidate_icache_range((unsigned long) dst, len); \
  125. } while (0)
  126. #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
  127. memcpy(dst, src, len)
  128. #endif
  129. #define XTENSA_CACHEBLK_LOG2 29
  130. #define XTENSA_CACHEBLK_SIZE (1 << XTENSA_CACHEBLK_LOG2)
  131. #define XTENSA_CACHEBLK_MASK (7 << XTENSA_CACHEBLK_LOG2)
  132. #if XCHAL_HAVE_CACHEATTR
  133. static inline u32 xtensa_get_cacheattr(void)
  134. {
  135. u32 r;
  136. asm volatile(" rsr %0, CACHEATTR" : "=a"(r));
  137. return r;
  138. }
  139. static inline u32 xtensa_get_dtlb1(u32 addr)
  140. {
  141. u32 r = addr & XTENSA_CACHEBLK_MASK;
  142. return r | ((xtensa_get_cacheattr() >> (r >> (XTENSA_CACHEBLK_LOG2-2)))
  143. & 0xF);
  144. }
  145. #else
  146. static inline u32 xtensa_get_dtlb1(u32 addr)
  147. {
  148. u32 r;
  149. asm volatile(" rdtlb1 %0, %1" : "=a"(r) : "a"(addr));
  150. asm volatile(" dsync");
  151. return r;
  152. }
  153. static inline u32 xtensa_get_cacheattr(void)
  154. {
  155. u32 r = 0;
  156. u32 a = 0;
  157. do {
  158. a -= XTENSA_CACHEBLK_SIZE;
  159. r = (r << 4) | (xtensa_get_dtlb1(a) & 0xF);
  160. } while (a);
  161. return r;
  162. }
  163. #endif
  164. static inline int xtensa_need_flush_dma_source(u32 addr)
  165. {
  166. return (xtensa_get_dtlb1(addr) & ((1 << XCHAL_CA_BITS) - 1)) >= 4;
  167. }
  168. static inline int xtensa_need_invalidate_dma_destination(u32 addr)
  169. {
  170. return (xtensa_get_dtlb1(addr) & ((1 << XCHAL_CA_BITS) - 1)) != 2;
  171. }
  172. static inline void flush_dcache_unaligned(u32 addr, u32 size)
  173. {
  174. u32 cnt;
  175. if (size) {
  176. cnt = (size + ((XCHAL_DCACHE_LINESIZE - 1) & addr)
  177. + XCHAL_DCACHE_LINESIZE - 1) / XCHAL_DCACHE_LINESIZE;
  178. while (cnt--) {
  179. asm volatile(" dhwb %0, 0" : : "a"(addr));
  180. addr += XCHAL_DCACHE_LINESIZE;
  181. }
  182. asm volatile(" dsync");
  183. }
  184. }
  185. static inline void invalidate_dcache_unaligned(u32 addr, u32 size)
  186. {
  187. int cnt;
  188. if (size) {
  189. asm volatile(" dhwbi %0, 0 ;" : : "a"(addr));
  190. cnt = (size + ((XCHAL_DCACHE_LINESIZE - 1) & addr)
  191. - XCHAL_DCACHE_LINESIZE - 1) / XCHAL_DCACHE_LINESIZE;
  192. while (cnt-- > 0) {
  193. asm volatile(" dhi %0, %1" : : "a"(addr),
  194. "n"(XCHAL_DCACHE_LINESIZE));
  195. addr += XCHAL_DCACHE_LINESIZE;
  196. }
  197. asm volatile(" dhwbi %0, %1" : : "a"(addr),
  198. "n"(XCHAL_DCACHE_LINESIZE));
  199. asm volatile(" dsync");
  200. }
  201. }
  202. static inline void flush_invalidate_dcache_unaligned(u32 addr, u32 size)
  203. {
  204. u32 cnt;
  205. if (size) {
  206. cnt = (size + ((XCHAL_DCACHE_LINESIZE - 1) & addr)
  207. + XCHAL_DCACHE_LINESIZE - 1) / XCHAL_DCACHE_LINESIZE;
  208. while (cnt--) {
  209. asm volatile(" dhwbi %0, 0" : : "a"(addr));
  210. addr += XCHAL_DCACHE_LINESIZE;
  211. }
  212. asm volatile(" dsync");
  213. }
  214. }
  215. #endif /* __KERNEL__ */
  216. #endif /* _XTENSA_CACHEFLUSH_H */