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