cache.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/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. struct folio *folio = page_folio(page);
  82. unsigned long paddr;
  83. void *kvaddr = coherent_kvaddr(page, TLBTEMP_BASE_1, vaddr, &paddr);
  84. preempt_disable();
  85. kmap_invalidate_coherent(page, vaddr);
  86. set_bit(PG_arch_1, folio_flags(folio, 0));
  87. clear_page_alias(kvaddr, paddr);
  88. preempt_enable();
  89. }
  90. EXPORT_SYMBOL(clear_user_highpage);
  91. void copy_user_highpage(struct page *dst, struct page *src,
  92. unsigned long vaddr, struct vm_area_struct *vma)
  93. {
  94. struct folio *folio = page_folio(dst);
  95. unsigned long dst_paddr, src_paddr;
  96. void *dst_vaddr = coherent_kvaddr(dst, TLBTEMP_BASE_1, vaddr,
  97. &dst_paddr);
  98. void *src_vaddr = coherent_kvaddr(src, TLBTEMP_BASE_2, vaddr,
  99. &src_paddr);
  100. preempt_disable();
  101. kmap_invalidate_coherent(dst, vaddr);
  102. set_bit(PG_arch_1, folio_flags(folio, 0));
  103. copy_page_alias(dst_vaddr, src_vaddr, dst_paddr, src_paddr);
  104. preempt_enable();
  105. }
  106. EXPORT_SYMBOL(copy_user_highpage);
  107. /*
  108. * Any time the kernel writes to a user page cache page, or it is about to
  109. * read from a page cache page this routine is called.
  110. *
  111. */
  112. void flush_dcache_folio(struct folio *folio)
  113. {
  114. struct address_space *mapping = folio_flush_mapping(folio);
  115. /*
  116. * If we have a mapping but the page is not mapped to user-space
  117. * yet, we simply mark this page dirty and defer flushing the
  118. * caches until update_mmu().
  119. */
  120. if (mapping && !mapping_mapped(mapping)) {
  121. if (!test_bit(PG_arch_1, &folio->flags))
  122. set_bit(PG_arch_1, &folio->flags);
  123. return;
  124. } else {
  125. unsigned long phys = folio_pfn(folio) * PAGE_SIZE;
  126. unsigned long temp = folio_pos(folio);
  127. unsigned int i, nr = folio_nr_pages(folio);
  128. unsigned long alias = !(DCACHE_ALIAS_EQ(temp, phys));
  129. unsigned long virt;
  130. /*
  131. * Flush the page in kernel space and user space.
  132. * Note that we can omit that step if aliasing is not
  133. * an issue, but we do have to synchronize I$ and D$
  134. * if we have a mapping.
  135. */
  136. if (!alias && !mapping)
  137. return;
  138. preempt_disable();
  139. for (i = 0; i < nr; i++) {
  140. virt = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  141. __flush_invalidate_dcache_page_alias(virt, phys);
  142. virt = TLBTEMP_BASE_1 + (temp & DCACHE_ALIAS_MASK);
  143. if (alias)
  144. __flush_invalidate_dcache_page_alias(virt, phys);
  145. if (mapping)
  146. __invalidate_icache_page_alias(virt, phys);
  147. phys += PAGE_SIZE;
  148. temp += PAGE_SIZE;
  149. }
  150. preempt_enable();
  151. }
  152. /* There shouldn't be an entry in the cache for this page anymore. */
  153. }
  154. EXPORT_SYMBOL(flush_dcache_folio);
  155. /*
  156. * For now, flush the whole cache. FIXME??
  157. */
  158. void local_flush_cache_range(struct vm_area_struct *vma,
  159. unsigned long start, unsigned long end)
  160. {
  161. __flush_invalidate_dcache_all();
  162. __invalidate_icache_all();
  163. }
  164. EXPORT_SYMBOL(local_flush_cache_range);
  165. /*
  166. * Remove any entry in the cache for this page.
  167. *
  168. * Note that this function is only called for user pages, so use the
  169. * alias versions of the cache flush functions.
  170. */
  171. void local_flush_cache_page(struct vm_area_struct *vma, unsigned long address,
  172. unsigned long pfn)
  173. {
  174. /* Note that we have to use the 'alias' address to avoid multi-hit */
  175. unsigned long phys = page_to_phys(pfn_to_page(pfn));
  176. unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
  177. preempt_disable();
  178. __flush_invalidate_dcache_page_alias(virt, phys);
  179. __invalidate_icache_page_alias(virt, phys);
  180. preempt_enable();
  181. }
  182. EXPORT_SYMBOL(local_flush_cache_page);
  183. #endif /* DCACHE_WAY_SIZE > PAGE_SIZE */
  184. void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
  185. unsigned long addr, pte_t *ptep, unsigned int nr)
  186. {
  187. unsigned long pfn = pte_pfn(*ptep);
  188. struct folio *folio;
  189. unsigned int i;
  190. if (!pfn_valid(pfn))
  191. return;
  192. folio = page_folio(pfn_to_page(pfn));
  193. /* Invalidate old entries in TLBs */
  194. for (i = 0; i < nr; i++)
  195. flush_tlb_page(vma, addr + i * PAGE_SIZE);
  196. nr = folio_nr_pages(folio);
  197. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  198. if (!folio_test_reserved(folio) && test_bit(PG_arch_1, &folio->flags)) {
  199. unsigned long phys = folio_pfn(folio) * PAGE_SIZE;
  200. unsigned long tmp;
  201. preempt_disable();
  202. for (i = 0; i < nr; i++) {
  203. tmp = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
  204. __flush_invalidate_dcache_page_alias(tmp, phys);
  205. tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
  206. __flush_invalidate_dcache_page_alias(tmp, phys);
  207. __invalidate_icache_page_alias(tmp, phys);
  208. phys += PAGE_SIZE;
  209. }
  210. preempt_enable();
  211. clear_bit(PG_arch_1, &folio->flags);
  212. }
  213. #else
  214. if (!folio_test_reserved(folio) && !test_bit(PG_arch_1, &folio->flags)
  215. && (vma->vm_flags & VM_EXEC) != 0) {
  216. for (i = 0; i < nr; i++) {
  217. void *paddr = kmap_local_folio(folio, i * PAGE_SIZE);
  218. __flush_dcache_page((unsigned long)paddr);
  219. __invalidate_icache_page((unsigned long)paddr);
  220. kunmap_local(paddr);
  221. }
  222. set_bit(PG_arch_1, &folio->flags);
  223. }
  224. #endif
  225. }
  226. /*
  227. * access_process_vm() has called get_user_pages(), which has done a
  228. * flush_dcache_page() on the page.
  229. */
  230. #if (DCACHE_WAY_SIZE > PAGE_SIZE)
  231. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  232. unsigned long vaddr, void *dst, const void *src,
  233. unsigned long len)
  234. {
  235. unsigned long phys = page_to_phys(page);
  236. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  237. /* Flush and invalidate user page if aliased. */
  238. if (alias) {
  239. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  240. preempt_disable();
  241. __flush_invalidate_dcache_page_alias(t, phys);
  242. preempt_enable();
  243. }
  244. /* Copy data */
  245. memcpy(dst, src, len);
  246. /*
  247. * Flush and invalidate kernel page if aliased and synchronize
  248. * data and instruction caches for executable pages.
  249. */
  250. if (alias) {
  251. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  252. preempt_disable();
  253. __flush_invalidate_dcache_range((unsigned long) dst, len);
  254. if ((vma->vm_flags & VM_EXEC) != 0)
  255. __invalidate_icache_page_alias(t, phys);
  256. preempt_enable();
  257. } else if ((vma->vm_flags & VM_EXEC) != 0) {
  258. __flush_dcache_range((unsigned long)dst,len);
  259. __invalidate_icache_range((unsigned long) dst, len);
  260. }
  261. }
  262. extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  263. unsigned long vaddr, void *dst, const void *src,
  264. unsigned long len)
  265. {
  266. unsigned long phys = page_to_phys(page);
  267. unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
  268. /*
  269. * Flush user page if aliased.
  270. * (Note: a simply flush would be sufficient)
  271. */
  272. if (alias) {
  273. unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
  274. preempt_disable();
  275. __flush_invalidate_dcache_page_alias(t, phys);
  276. preempt_enable();
  277. }
  278. memcpy(dst, src, len);
  279. }
  280. #endif