cache.c 8.1 KB

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