cacheflush.h 7.0 KB

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