cache.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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) && XCHAL_DCACHE_IS_WRITEBACK
  58. /*
  59. * Any time the kernel writes to a user page cache page, or it is about to
  60. * read from a page cache page this routine is called.
  61. *
  62. */
  63. void flush_dcache_page(struct page *page)
  64. {
  65. struct address_space *mapping = page_mapping(page);
  66. /*
  67. * If we have a mapping but the page is not mapped to user-space
  68. * yet, we simply mark this page dirty and defer flushing the
  69. * caches until update_mmu().
  70. */
  71. if (mapping && !mapping_mapped(mapping)) {
  72. if (!test_bit(PG_arch_1, &page->flags))
  73. set_bit(PG_arch_1, &page->flags);
  74. return;
  75. } else {
  76. unsigned long phys = page_to_phys(page);
  77. unsigned long temp = page->index << PAGE_SHIFT;
  78. unsigned long alias = !(DCACHE_ALIAS_EQ(temp, phys));
  79. unsigned long virt;
  80. /*
  81. * Flush the page in kernel space and user space.
  82. * Note that we can omit that step if aliasing is not
  83. * an issue, but we do have to synchronize I$ and D$
  84. * if we have a mapping.
  85. */
  86. if (!alias && !mapping)
  87. return;
  88. __flush_invalidate_dcache_page((long)page_address(page));
  89. virt = TLBTEMP_BASE_1 + (temp & DCACHE_ALIAS_MASK);
  90. if (alias)
  91. __flush_invalidate_dcache_page_alias(virt, phys);
  92. if (mapping)
  93. __invalidate_icache_page_alias(virt, phys);
  94. }
  95. /* There shouldn't be an entry in the cache for this page anymore. */
  96. }
  97. /*
  98. * For now, flush the whole cache. FIXME??
  99. */
  100. void flush_cache_range(struct vm_area_struct* vma,
  101. unsigned long start, unsigned long end)
  102. {
  103. __flush_invalidate_dcache_all();
  104. __invalidate_icache_all();
  105. }
  106. /*
  107. * Remove any entry in the cache for this page.
  108. *
  109. * Note that this function is only called for user pages, so use the
  110. * alias versions of the cache flush functions.
  111. */
  112. void flush_cache_page(struct vm_area_struct* vma, unsigned long address,
  113. unsigned long pfn)
  114. {
  115. /* Note that we have to use the 'alias' address to avoid multi-hit */
  116. unsigned long phys = page_to_phys(pfn_to_page(pfn));
  117. unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
  118. __flush_invalidate_dcache_page_alias(virt, phys);
  119. __invalidate_icache_page_alias(virt, phys);
  120. }
  121. #endif
  122. void
  123. update_mmu_cache(struct vm_area_struct * vma, unsigned long addr, pte_t *ptep)
  124. {
  125. unsigned long pfn = pte_pfn(*ptep);
  126. struct page *page;
  127. if (!pfn_valid(pfn))
  128. return;
  129. page = pfn_to_page(pfn);
  130. /* Invalidate old entry in TLBs */
  131. invalidate_itlb_mapping(addr);
  132. invalidate_dtlb_mapping(addr);
  133. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  134. if (!PageReserved(page) && test_bit(PG_arch_1, &page->flags)) {
  135. unsigned long vaddr = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
  136. unsigned long paddr = (unsigned long) page_address(page);
  137. unsigned long phys = page_to_phys(page);
  138. __flush_invalidate_dcache_page(paddr);
  139. __flush_invalidate_dcache_page_alias(vaddr, phys);
  140. __invalidate_icache_page_alias(vaddr, phys);
  141. clear_bit(PG_arch_1, &page->flags);
  142. }
  143. #else
  144. if (!PageReserved(page) && !test_bit(PG_arch_1, &page->flags)
  145. && (vma->vm_flags & VM_EXEC) != 0) {
  146. unsigned long paddr = (unsigned long) page_address(page);
  147. __flush_dcache_page(paddr);
  148. __invalidate_icache_page(paddr);
  149. set_bit(PG_arch_1, &page->flags);
  150. }
  151. #endif
  152. }
  153. /*
  154. * access_process_vm() has called get_user_pages(), which has done a
  155. * flush_dcache_page() on the page.
  156. */
  157. #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
  158. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  159. unsigned long vaddr, void *dst, const void *src,
  160. unsigned long len)
  161. {
  162. unsigned long phys = page_to_phys(page);
  163. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  164. /* Flush and invalidate user page if aliased. */
  165. if (alias) {
  166. unsigned long temp = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  167. __flush_invalidate_dcache_page_alias(temp, phys);
  168. }
  169. /* Copy data */
  170. memcpy(dst, src, len);
  171. /*
  172. * Flush and invalidate kernel page if aliased and synchronize
  173. * data and instruction caches for executable pages.
  174. */
  175. if (alias) {
  176. unsigned long temp = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  177. __flush_invalidate_dcache_range((unsigned long) dst, len);
  178. if ((vma->vm_flags & VM_EXEC) != 0) {
  179. __invalidate_icache_page_alias(temp, phys);
  180. }
  181. } else if ((vma->vm_flags & VM_EXEC) != 0) {
  182. __flush_dcache_range((unsigned long)dst,len);
  183. __invalidate_icache_range((unsigned long) dst, len);
  184. }
  185. }
  186. extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  187. unsigned long vaddr, void *dst, const void *src,
  188. unsigned long len)
  189. {
  190. unsigned long phys = page_to_phys(page);
  191. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  192. /*
  193. * Flush user page if aliased.
  194. * (Note: a simply flush would be sufficient)
  195. */
  196. if (alias) {
  197. unsigned long temp = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  198. __flush_invalidate_dcache_page_alias(temp, phys);
  199. }
  200. memcpy(dst, src, len);
  201. }
  202. #endif