cache.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. preempt_disable();
  68. __invalidate_dcache_page_alias(kvaddr,
  69. page_to_phys(page));
  70. preempt_enable();
  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_file(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. preempt_disable();
  142. virt = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  143. __flush_invalidate_dcache_page_alias(virt, phys);
  144. virt = TLBTEMP_BASE_1 + (temp & DCACHE_ALIAS_MASK);
  145. if (alias)
  146. __flush_invalidate_dcache_page_alias(virt, phys);
  147. if (mapping)
  148. __invalidate_icache_page_alias(virt, phys);
  149. preempt_enable();
  150. }
  151. /* There shouldn't be an entry in the cache for this page anymore. */
  152. }
  153. EXPORT_SYMBOL(flush_dcache_page);
  154. /*
  155. * For now, flush the whole cache. FIXME??
  156. */
  157. void local_flush_cache_range(struct vm_area_struct *vma,
  158. unsigned long start, unsigned long end)
  159. {
  160. __flush_invalidate_dcache_all();
  161. __invalidate_icache_all();
  162. }
  163. EXPORT_SYMBOL(local_flush_cache_range);
  164. /*
  165. * Remove any entry in the cache for this page.
  166. *
  167. * Note that this function is only called for user pages, so use the
  168. * alias versions of the cache flush functions.
  169. */
  170. void local_flush_cache_page(struct vm_area_struct *vma, unsigned long address,
  171. unsigned long pfn)
  172. {
  173. /* Note that we have to use the 'alias' address to avoid multi-hit */
  174. unsigned long phys = page_to_phys(pfn_to_page(pfn));
  175. unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
  176. preempt_disable();
  177. __flush_invalidate_dcache_page_alias(virt, phys);
  178. __invalidate_icache_page_alias(virt, phys);
  179. preempt_enable();
  180. }
  181. EXPORT_SYMBOL(local_flush_cache_page);
  182. #endif /* DCACHE_WAY_SIZE > PAGE_SIZE */
  183. void
  184. update_mmu_cache(struct vm_area_struct * vma, unsigned long addr, pte_t *ptep)
  185. {
  186. unsigned long pfn = pte_pfn(*ptep);
  187. struct page *page;
  188. if (!pfn_valid(pfn))
  189. return;
  190. page = pfn_to_page(pfn);
  191. /* Invalidate old entry in TLBs */
  192. flush_tlb_page(vma, addr);
  193. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  194. if (!PageReserved(page) && test_bit(PG_arch_1, &page->flags)) {
  195. unsigned long phys = page_to_phys(page);
  196. unsigned long tmp;
  197. preempt_disable();
  198. tmp = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  199. __flush_invalidate_dcache_page_alias(tmp, phys);
  200. tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
  201. __flush_invalidate_dcache_page_alias(tmp, phys);
  202. __invalidate_icache_page_alias(tmp, phys);
  203. preempt_enable();
  204. clear_bit(PG_arch_1, &page->flags);
  205. }
  206. #else
  207. if (!PageReserved(page) && !test_bit(PG_arch_1, &page->flags)
  208. && (vma->vm_flags & VM_EXEC) != 0) {
  209. unsigned long paddr = (unsigned long)kmap_atomic(page);
  210. __flush_dcache_page(paddr);
  211. __invalidate_icache_page(paddr);
  212. set_bit(PG_arch_1, &page->flags);
  213. kunmap_atomic((void *)paddr);
  214. }
  215. #endif
  216. }
  217. /*
  218. * access_process_vm() has called get_user_pages(), which has done a
  219. * flush_dcache_page() on the page.
  220. */
  221. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  222. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  223. unsigned long vaddr, void *dst, const void *src,
  224. unsigned long len)
  225. {
  226. unsigned long phys = page_to_phys(page);
  227. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  228. /* Flush and invalidate user page if aliased. */
  229. if (alias) {
  230. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  231. preempt_disable();
  232. __flush_invalidate_dcache_page_alias(t, phys);
  233. preempt_enable();
  234. }
  235. /* Copy data */
  236. memcpy(dst, src, len);
  237. /*
  238. * Flush and invalidate kernel page if aliased and synchronize
  239. * data and instruction caches for executable pages.
  240. */
  241. if (alias) {
  242. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  243. preempt_disable();
  244. __flush_invalidate_dcache_range((unsigned long) dst, len);
  245. if ((vma->vm_flags & VM_EXEC) != 0)
  246. __invalidate_icache_page_alias(t, phys);
  247. preempt_enable();
  248. } else if ((vma->vm_flags & VM_EXEC) != 0) {
  249. __flush_dcache_range((unsigned long)dst,len);
  250. __invalidate_icache_range((unsigned long) dst, len);
  251. }
  252. }
  253. extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  254. unsigned long vaddr, void *dst, const void *src,
  255. unsigned long len)
  256. {
  257. unsigned long phys = page_to_phys(page);
  258. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  259. /*
  260. * Flush user page if aliased.
  261. * (Note: a simply flush would be sufficient)
  262. */
  263. if (alias) {
  264. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  265. preempt_disable();
  266. __flush_invalidate_dcache_page_alias(t, phys);
  267. preempt_enable();
  268. }
  269. memcpy(dst, src, len);
  270. }
  271. #endif