cache.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 <[email protected]>
  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/memblock.h>
  24. #include <linux/swap.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/pgtable.h>
  27. #include <asm/bootparam.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/tlb.h>
  30. #include <asm/tlbflush.h>
  31. #include <asm/page.h>
  32. /*
  33. * Note:
  34. * The kernel provides one architecture bit PG_arch_1 in the page flags that
  35. * can be used for cache coherency.
  36. *
  37. * I$-D$ coherency.
  38. *
  39. * The Xtensa architecture doesn't keep the instruction cache coherent with
  40. * the data cache. We use the architecture bit to indicate if the caches
  41. * are coherent. The kernel clears this bit whenever a page is added to the
  42. * page cache. At that time, the caches might not be in sync. We, therefore,
  43. * define this flag as 'clean' if set.
  44. *
  45. * D-cache aliasing.
  46. *
  47. * With cache aliasing, we have to always flush the cache when pages are
  48. * unmapped (see tlb_start_vma(). So, we use this flag to indicate a dirty
  49. * page.
  50. *
  51. *
  52. *
  53. */
  54. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  55. static inline void kmap_invalidate_coherent(struct page *page,
  56. unsigned long vaddr)
  57. {
  58. if (!DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
  59. unsigned long kvaddr;
  60. if (!PageHighMem(page)) {
  61. kvaddr = (unsigned long)page_to_virt(page);
  62. __invalidate_dcache_page(kvaddr);
  63. } else {
  64. kvaddr = TLBTEMP_BASE_1 +
  65. (page_to_phys(page) & DCACHE_ALIAS_MASK);
  66. preempt_disable();
  67. __invalidate_dcache_page_alias(kvaddr,
  68. page_to_phys(page));
  69. preempt_enable();
  70. }
  71. }
  72. }
  73. static inline void *coherent_kvaddr(struct page *page, unsigned long base,
  74. unsigned long vaddr, unsigned long *paddr)
  75. {
  76. *paddr = page_to_phys(page);
  77. return (void *)(base + (vaddr & DCACHE_ALIAS_MASK));
  78. }
  79. void clear_user_highpage(struct page *page, unsigned long vaddr)
  80. {
  81. unsigned long paddr;
  82. void *kvaddr = coherent_kvaddr(page, TLBTEMP_BASE_1, vaddr, &paddr);
  83. preempt_disable();
  84. kmap_invalidate_coherent(page, vaddr);
  85. set_bit(PG_arch_1, &page->flags);
  86. clear_page_alias(kvaddr, paddr);
  87. preempt_enable();
  88. }
  89. EXPORT_SYMBOL(clear_user_highpage);
  90. void copy_user_highpage(struct page *dst, struct page *src,
  91. unsigned long vaddr, struct vm_area_struct *vma)
  92. {
  93. unsigned long dst_paddr, src_paddr;
  94. void *dst_vaddr = coherent_kvaddr(dst, TLBTEMP_BASE_1, vaddr,
  95. &dst_paddr);
  96. void *src_vaddr = coherent_kvaddr(src, TLBTEMP_BASE_2, vaddr,
  97. &src_paddr);
  98. preempt_disable();
  99. kmap_invalidate_coherent(dst, vaddr);
  100. set_bit(PG_arch_1, &dst->flags);
  101. copy_page_alias(dst_vaddr, src_vaddr, dst_paddr, src_paddr);
  102. preempt_enable();
  103. }
  104. EXPORT_SYMBOL(copy_user_highpage);
  105. /*
  106. * Any time the kernel writes to a user page cache page, or it is about to
  107. * read from a page cache page this routine is called.
  108. *
  109. */
  110. void flush_dcache_page(struct page *page)
  111. {
  112. struct address_space *mapping = page_mapping_file(page);
  113. /*
  114. * If we have a mapping but the page is not mapped to user-space
  115. * yet, we simply mark this page dirty and defer flushing the
  116. * caches until update_mmu().
  117. */
  118. if (mapping && !mapping_mapped(mapping)) {
  119. if (!test_bit(PG_arch_1, &page->flags))
  120. set_bit(PG_arch_1, &page->flags);
  121. return;
  122. } else {
  123. unsigned long phys = page_to_phys(page);
  124. unsigned long temp = page->index << PAGE_SHIFT;
  125. unsigned long alias = !(DCACHE_ALIAS_EQ(temp, phys));
  126. unsigned long virt;
  127. /*
  128. * Flush the page in kernel space and user space.
  129. * Note that we can omit that step if aliasing is not
  130. * an issue, but we do have to synchronize I$ and D$
  131. * if we have a mapping.
  132. */
  133. if (!alias && !mapping)
  134. return;
  135. preempt_disable();
  136. virt = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  137. __flush_invalidate_dcache_page_alias(virt, phys);
  138. virt = TLBTEMP_BASE_1 + (temp & DCACHE_ALIAS_MASK);
  139. if (alias)
  140. __flush_invalidate_dcache_page_alias(virt, phys);
  141. if (mapping)
  142. __invalidate_icache_page_alias(virt, phys);
  143. preempt_enable();
  144. }
  145. /* There shouldn't be an entry in the cache for this page anymore. */
  146. }
  147. EXPORT_SYMBOL(flush_dcache_page);
  148. /*
  149. * For now, flush the whole cache. FIXME??
  150. */
  151. void local_flush_cache_range(struct vm_area_struct *vma,
  152. unsigned long start, unsigned long end)
  153. {
  154. __flush_invalidate_dcache_all();
  155. __invalidate_icache_all();
  156. }
  157. EXPORT_SYMBOL(local_flush_cache_range);
  158. /*
  159. * Remove any entry in the cache for this page.
  160. *
  161. * Note that this function is only called for user pages, so use the
  162. * alias versions of the cache flush functions.
  163. */
  164. void local_flush_cache_page(struct vm_area_struct *vma, unsigned long address,
  165. unsigned long pfn)
  166. {
  167. /* Note that we have to use the 'alias' address to avoid multi-hit */
  168. unsigned long phys = page_to_phys(pfn_to_page(pfn));
  169. unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
  170. preempt_disable();
  171. __flush_invalidate_dcache_page_alias(virt, phys);
  172. __invalidate_icache_page_alias(virt, phys);
  173. preempt_enable();
  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. preempt_disable();
  192. tmp = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  193. __flush_invalidate_dcache_page_alias(tmp, phys);
  194. tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
  195. __flush_invalidate_dcache_page_alias(tmp, phys);
  196. __invalidate_icache_page_alias(tmp, phys);
  197. preempt_enable();
  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. preempt_disable();
  226. __flush_invalidate_dcache_page_alias(t, phys);
  227. preempt_enable();
  228. }
  229. /* Copy data */
  230. memcpy(dst, src, len);
  231. /*
  232. * Flush and invalidate kernel page if aliased and synchronize
  233. * data and instruction caches for executable pages.
  234. */
  235. if (alias) {
  236. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  237. preempt_disable();
  238. __flush_invalidate_dcache_range((unsigned long) dst, len);
  239. if ((vma->vm_flags & VM_EXEC) != 0)
  240. __invalidate_icache_page_alias(t, phys);
  241. preempt_enable();
  242. } else if ((vma->vm_flags & VM_EXEC) != 0) {
  243. __flush_dcache_range((unsigned long)dst,len);
  244. __invalidate_icache_range((unsigned long) dst, len);
  245. }
  246. }
  247. extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  248. unsigned long vaddr, void *dst, const void *src,
  249. unsigned long len)
  250. {
  251. unsigned long phys = page_to_phys(page);
  252. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  253. /*
  254. * Flush user page if aliased.
  255. * (Note: a simply flush would be sufficient)
  256. */
  257. if (alias) {
  258. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  259. preempt_disable();
  260. __flush_invalidate_dcache_page_alias(t, phys);
  261. preempt_enable();
  262. }
  263. memcpy(dst, src, len);
  264. }
  265. #endif