cache.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * arch/xtensa/mm/cache.c
  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. * Copyright (C) 2001-2006 Tensilica Inc.
  9. *
  10. * Chris Zankel <chris@zankel.net>
  11. * Joe Taylor
  12. * Marc Gauthier
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/signal.h>
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/types.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/swap.h>
  25. #include <linux/pagemap.h>
  26. #include <asm/bootparam.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/tlb.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/page.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/pgtable.h>
  33. /*
  34. * Note:
  35. * The kernel provides one architecture bit PG_arch_1 in the page flags that
  36. * can be used for cache coherency.
  37. *
  38. * I$-D$ coherency.
  39. *
  40. * The Xtensa architecture doesn't keep the instruction cache coherent with
  41. * the data cache. We use the architecture bit to indicate if the caches
  42. * are coherent. The kernel clears this bit whenever a page is added to the
  43. * page cache. At that time, the caches might not be in sync. We, therefore,
  44. * define this flag as 'clean' if set.
  45. *
  46. * D-cache aliasing.
  47. *
  48. * With cache aliasing, we have to always flush the cache when pages are
  49. * unmapped (see tlb_start_vma(). So, we use this flag to indicate a dirty
  50. * page.
  51. *
  52. *
  53. *
  54. */
  55. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  56. static inline void kmap_invalidate_coherent(struct page *page,
  57. unsigned long vaddr)
  58. {
  59. if (!DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
  60. unsigned long kvaddr;
  61. if (!PageHighMem(page)) {
  62. kvaddr = (unsigned long)page_to_virt(page);
  63. __invalidate_dcache_page(kvaddr);
  64. } else {
  65. kvaddr = TLBTEMP_BASE_1 +
  66. (page_to_phys(page) & DCACHE_ALIAS_MASK);
  67. __invalidate_dcache_page_alias(kvaddr,
  68. page_to_phys(page));
  69. }
  70. }
  71. }
  72. static inline void *coherent_kvaddr(struct page *page, unsigned long base,
  73. unsigned long vaddr, unsigned long *paddr)
  74. {
  75. if (PageHighMem(page) || !DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
  76. *paddr = page_to_phys(page);
  77. return (void *)(base + (vaddr & DCACHE_ALIAS_MASK));
  78. } else {
  79. *paddr = 0;
  80. return page_to_virt(page);
  81. }
  82. }
  83. void clear_user_highpage(struct page *page, unsigned long vaddr)
  84. {
  85. unsigned long paddr;
  86. void *kvaddr = coherent_kvaddr(page, TLBTEMP_BASE_1, vaddr, &paddr);
  87. preempt_disable();
  88. kmap_invalidate_coherent(page, vaddr);
  89. set_bit(PG_arch_1, &page->flags);
  90. clear_page_alias(kvaddr, paddr);
  91. preempt_enable();
  92. }
  93. EXPORT_SYMBOL(clear_user_highpage);
  94. void copy_user_highpage(struct page *dst, struct page *src,
  95. unsigned long vaddr, struct vm_area_struct *vma)
  96. {
  97. unsigned long dst_paddr, src_paddr;
  98. void *dst_vaddr = coherent_kvaddr(dst, TLBTEMP_BASE_1, vaddr,
  99. &dst_paddr);
  100. void *src_vaddr = coherent_kvaddr(src, TLBTEMP_BASE_2, vaddr,
  101. &src_paddr);
  102. preempt_disable();
  103. kmap_invalidate_coherent(dst, vaddr);
  104. set_bit(PG_arch_1, &dst->flags);
  105. copy_page_alias(dst_vaddr, src_vaddr, dst_paddr, src_paddr);
  106. preempt_enable();
  107. }
  108. EXPORT_SYMBOL(copy_user_highpage);
  109. /*
  110. * Any time the kernel writes to a user page cache page, or it is about to
  111. * read from a page cache page this routine is called.
  112. *
  113. */
  114. void flush_dcache_page(struct page *page)
  115. {
  116. struct address_space *mapping = page_mapping(page);
  117. /*
  118. * If we have a mapping but the page is not mapped to user-space
  119. * yet, we simply mark this page dirty and defer flushing the
  120. * caches until update_mmu().
  121. */
  122. if (mapping && !mapping_mapped(mapping)) {
  123. if (!test_bit(PG_arch_1, &page->flags))
  124. set_bit(PG_arch_1, &page->flags);
  125. return;
  126. } else {
  127. unsigned long phys = page_to_phys(page);
  128. unsigned long temp = page->index << PAGE_SHIFT;
  129. unsigned long alias = !(DCACHE_ALIAS_EQ(temp, phys));
  130. unsigned long virt;
  131. /*
  132. * Flush the page in kernel space and user space.
  133. * Note that we can omit that step if aliasing is not
  134. * an issue, but we do have to synchronize I$ and D$
  135. * if we have a mapping.
  136. */
  137. if (!alias && !mapping)
  138. return;
  139. virt = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  140. __flush_invalidate_dcache_page_alias(virt, phys);
  141. virt = TLBTEMP_BASE_1 + (temp & DCACHE_ALIAS_MASK);
  142. if (alias)
  143. __flush_invalidate_dcache_page_alias(virt, phys);
  144. if (mapping)
  145. __invalidate_icache_page_alias(virt, phys);
  146. }
  147. /* There shouldn't be an entry in the cache for this page anymore. */
  148. }
  149. EXPORT_SYMBOL(flush_dcache_page);
  150. /*
  151. * For now, flush the whole cache. FIXME??
  152. */
  153. void local_flush_cache_range(struct vm_area_struct *vma,
  154. unsigned long start, unsigned long end)
  155. {
  156. __flush_invalidate_dcache_all();
  157. __invalidate_icache_all();
  158. }
  159. EXPORT_SYMBOL(local_flush_cache_range);
  160. /*
  161. * Remove any entry in the cache for this page.
  162. *
  163. * Note that this function is only called for user pages, so use the
  164. * alias versions of the cache flush functions.
  165. */
  166. void local_flush_cache_page(struct vm_area_struct *vma, unsigned long address,
  167. unsigned long pfn)
  168. {
  169. /* Note that we have to use the 'alias' address to avoid multi-hit */
  170. unsigned long phys = page_to_phys(pfn_to_page(pfn));
  171. unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
  172. __flush_invalidate_dcache_page_alias(virt, phys);
  173. __invalidate_icache_page_alias(virt, phys);
  174. }
  175. EXPORT_SYMBOL(local_flush_cache_page);
  176. #endif /* DCACHE_WAY_SIZE > PAGE_SIZE */
  177. void
  178. update_mmu_cache(struct vm_area_struct * vma, unsigned long addr, pte_t *ptep)
  179. {
  180. unsigned long pfn = pte_pfn(*ptep);
  181. struct page *page;
  182. if (!pfn_valid(pfn))
  183. return;
  184. page = pfn_to_page(pfn);
  185. /* Invalidate old entry in TLBs */
  186. flush_tlb_page(vma, addr);
  187. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  188. if (!PageReserved(page) && test_bit(PG_arch_1, &page->flags)) {
  189. unsigned long phys = page_to_phys(page);
  190. unsigned long tmp;
  191. tmp = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  192. __flush_invalidate_dcache_page_alias(tmp, phys);
  193. tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
  194. __flush_invalidate_dcache_page_alias(tmp, phys);
  195. __invalidate_icache_page_alias(tmp, phys);
  196. clear_bit(PG_arch_1, &page->flags);
  197. }
  198. #else
  199. if (!PageReserved(page) && !test_bit(PG_arch_1, &page->flags)
  200. && (vma->vm_flags & VM_EXEC) != 0) {
  201. unsigned long paddr = (unsigned long)kmap_atomic(page);
  202. __flush_dcache_page(paddr);
  203. __invalidate_icache_page(paddr);
  204. set_bit(PG_arch_1, &page->flags);
  205. kunmap_atomic((void *)paddr);
  206. }
  207. #endif
  208. }
  209. /*
  210. * access_process_vm() has called get_user_pages(), which has done a
  211. * flush_dcache_page() on the page.
  212. */
  213. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  214. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  215. unsigned long vaddr, void *dst, const void *src,
  216. unsigned long len)
  217. {
  218. unsigned long phys = page_to_phys(page);
  219. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  220. /* Flush and invalidate user page if aliased. */
  221. if (alias) {
  222. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  223. __flush_invalidate_dcache_page_alias(t, phys);
  224. }
  225. /* Copy data */
  226. memcpy(dst, src, len);
  227. /*
  228. * Flush and invalidate kernel page if aliased and synchronize
  229. * data and instruction caches for executable pages.
  230. */
  231. if (alias) {
  232. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  233. __flush_invalidate_dcache_range((unsigned long) dst, len);
  234. if ((vma->vm_flags & VM_EXEC) != 0)
  235. __invalidate_icache_page_alias(t, phys);
  236. } else if ((vma->vm_flags & VM_EXEC) != 0) {
  237. __flush_dcache_range((unsigned long)dst,len);
  238. __invalidate_icache_range((unsigned long) dst, len);
  239. }
  240. }
  241. extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  242. unsigned long vaddr, void *dst, const void *src,
  243. unsigned long len)
  244. {
  245. unsigned long phys = page_to_phys(page);
  246. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  247. /*
  248. * Flush user page if aliased.
  249. * (Note: a simply flush would be sufficient)
  250. */
  251. if (alias) {
  252. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  253. __flush_invalidate_dcache_page_alias(t, phys);
  254. }
  255. memcpy(dst, src, len);
  256. }
  257. #endif