cacheflush.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2009, Wind River Systems Inc
  7. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  8. */
  9. #include <linux/export.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/fs.h>
  13. #include <linux/pagemap.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/cpuinfo.h>
  16. static void __flush_dcache(unsigned long start, unsigned long end)
  17. {
  18. unsigned long addr;
  19. start &= ~(cpuinfo.dcache_line_size - 1);
  20. end += (cpuinfo.dcache_line_size - 1);
  21. end &= ~(cpuinfo.dcache_line_size - 1);
  22. if (end > start + cpuinfo.dcache_size)
  23. end = start + cpuinfo.dcache_size;
  24. for (addr = start; addr < end; addr += cpuinfo.dcache_line_size) {
  25. __asm__ __volatile__ (" flushd 0(%0)\n"
  26. : /* Outputs */
  27. : /* Inputs */ "r"(addr)
  28. /* : No clobber */);
  29. }
  30. }
  31. static void __invalidate_dcache(unsigned long start, unsigned long end)
  32. {
  33. unsigned long addr;
  34. start &= ~(cpuinfo.dcache_line_size - 1);
  35. end += (cpuinfo.dcache_line_size - 1);
  36. end &= ~(cpuinfo.dcache_line_size - 1);
  37. for (addr = start; addr < end; addr += cpuinfo.dcache_line_size) {
  38. __asm__ __volatile__ (" initda 0(%0)\n"
  39. : /* Outputs */
  40. : /* Inputs */ "r"(addr)
  41. /* : No clobber */);
  42. }
  43. }
  44. static void __flush_icache(unsigned long start, unsigned long end)
  45. {
  46. unsigned long addr;
  47. start &= ~(cpuinfo.icache_line_size - 1);
  48. end += (cpuinfo.icache_line_size - 1);
  49. end &= ~(cpuinfo.icache_line_size - 1);
  50. if (end > start + cpuinfo.icache_size)
  51. end = start + cpuinfo.icache_size;
  52. for (addr = start; addr < end; addr += cpuinfo.icache_line_size) {
  53. __asm__ __volatile__ (" flushi %0\n"
  54. : /* Outputs */
  55. : /* Inputs */ "r"(addr)
  56. /* : No clobber */);
  57. }
  58. __asm__ __volatile(" flushp\n");
  59. }
  60. static void flush_aliases(struct address_space *mapping, struct folio *folio)
  61. {
  62. struct mm_struct *mm = current->active_mm;
  63. struct vm_area_struct *vma;
  64. unsigned long flags;
  65. pgoff_t pgoff;
  66. unsigned long nr = folio_nr_pages(folio);
  67. pgoff = folio->index;
  68. flush_dcache_mmap_lock_irqsave(mapping, flags);
  69. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff + nr - 1) {
  70. unsigned long start;
  71. if (vma->vm_mm != mm)
  72. continue;
  73. if (!(vma->vm_flags & VM_MAYSHARE))
  74. continue;
  75. start = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  76. flush_cache_range(vma, start, start + nr * PAGE_SIZE);
  77. }
  78. flush_dcache_mmap_unlock_irqrestore(mapping, flags);
  79. }
  80. void flush_cache_all(void)
  81. {
  82. __flush_dcache(0, cpuinfo.dcache_size);
  83. __flush_icache(0, cpuinfo.icache_size);
  84. }
  85. void flush_cache_mm(struct mm_struct *mm)
  86. {
  87. flush_cache_all();
  88. }
  89. void flush_cache_dup_mm(struct mm_struct *mm)
  90. {
  91. flush_cache_all();
  92. }
  93. void flush_icache_range(unsigned long start, unsigned long end)
  94. {
  95. __flush_dcache(start, end);
  96. __flush_icache(start, end);
  97. }
  98. void flush_dcache_range(unsigned long start, unsigned long end)
  99. {
  100. __flush_dcache(start, end);
  101. __flush_icache(start, end);
  102. }
  103. EXPORT_SYMBOL(flush_dcache_range);
  104. void invalidate_dcache_range(unsigned long start, unsigned long end)
  105. {
  106. __invalidate_dcache(start, end);
  107. }
  108. EXPORT_SYMBOL(invalidate_dcache_range);
  109. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  110. unsigned long end)
  111. {
  112. __flush_dcache(start, end);
  113. if (vma == NULL || (vma->vm_flags & VM_EXEC))
  114. __flush_icache(start, end);
  115. }
  116. void flush_icache_pages(struct vm_area_struct *vma, struct page *page,
  117. unsigned int nr)
  118. {
  119. unsigned long start = (unsigned long) page_address(page);
  120. unsigned long end = start + nr * PAGE_SIZE;
  121. __flush_dcache(start, end);
  122. __flush_icache(start, end);
  123. }
  124. void flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,
  125. unsigned long pfn)
  126. {
  127. unsigned long start = vmaddr;
  128. unsigned long end = start + PAGE_SIZE;
  129. __flush_dcache(start, end);
  130. if (vma->vm_flags & VM_EXEC)
  131. __flush_icache(start, end);
  132. }
  133. static void __flush_dcache_folio(struct folio *folio)
  134. {
  135. /*
  136. * Writeback any data associated with the kernel mapping of this
  137. * page. This ensures that data in the physical page is mutually
  138. * coherent with the kernels mapping.
  139. */
  140. unsigned long start = (unsigned long)folio_address(folio);
  141. __flush_dcache(start, start + folio_size(folio));
  142. }
  143. void flush_dcache_folio(struct folio *folio)
  144. {
  145. struct address_space *mapping;
  146. /*
  147. * The zero page is never written to, so never has any dirty
  148. * cache lines, and therefore never needs to be flushed.
  149. */
  150. if (is_zero_pfn(folio_pfn(folio)))
  151. return;
  152. mapping = folio_flush_mapping(folio);
  153. /* Flush this page if there are aliases. */
  154. if (mapping && !mapping_mapped(mapping)) {
  155. clear_bit(PG_dcache_clean, &folio->flags.f);
  156. } else {
  157. __flush_dcache_folio(folio);
  158. if (mapping) {
  159. unsigned long start = (unsigned long)folio_address(folio);
  160. flush_aliases(mapping, folio);
  161. flush_icache_range(start, start + folio_size(folio));
  162. }
  163. set_bit(PG_dcache_clean, &folio->flags.f);
  164. }
  165. }
  166. EXPORT_SYMBOL(flush_dcache_folio);
  167. void flush_dcache_page(struct page *page)
  168. {
  169. flush_dcache_folio(page_folio(page));
  170. }
  171. EXPORT_SYMBOL(flush_dcache_page);
  172. void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
  173. unsigned long address, pte_t *ptep, unsigned int nr)
  174. {
  175. pte_t pte = *ptep;
  176. unsigned long pfn = pte_pfn(pte);
  177. struct folio *folio;
  178. struct address_space *mapping;
  179. reload_tlb_page(vma, address, pte);
  180. if (!pfn_valid(pfn))
  181. return;
  182. /*
  183. * The zero page is never written to, so never has any dirty
  184. * cache lines, and therefore never needs to be flushed.
  185. */
  186. if (is_zero_pfn(pfn))
  187. return;
  188. folio = page_folio(pfn_to_page(pfn));
  189. if (!test_and_set_bit(PG_dcache_clean, &folio->flags.f))
  190. __flush_dcache_folio(folio);
  191. mapping = folio_flush_mapping(folio);
  192. if (mapping) {
  193. flush_aliases(mapping, folio);
  194. if (vma->vm_flags & VM_EXEC)
  195. flush_icache_pages(vma, &folio->page,
  196. folio_nr_pages(folio));
  197. }
  198. }
  199. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  200. struct page *to)
  201. {
  202. __flush_dcache(vaddr, vaddr + PAGE_SIZE);
  203. __flush_icache(vaddr, vaddr + PAGE_SIZE);
  204. copy_page(vto, vfrom);
  205. __flush_dcache((unsigned long)vto, (unsigned long)vto + PAGE_SIZE);
  206. __flush_icache((unsigned long)vto, (unsigned long)vto + PAGE_SIZE);
  207. }
  208. void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
  209. {
  210. __flush_dcache(vaddr, vaddr + PAGE_SIZE);
  211. __flush_icache(vaddr, vaddr + PAGE_SIZE);
  212. clear_page(addr);
  213. __flush_dcache((unsigned long)addr, (unsigned long)addr + PAGE_SIZE);
  214. __flush_icache((unsigned long)addr, (unsigned long)addr + PAGE_SIZE);
  215. }
  216. void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  217. unsigned long user_vaddr,
  218. void *dst, void *src, int len)
  219. {
  220. flush_cache_page(vma, user_vaddr, page_to_pfn(page));
  221. memcpy(dst, src, len);
  222. __flush_dcache((unsigned long)src, (unsigned long)src + len);
  223. if (vma->vm_flags & VM_EXEC)
  224. __flush_icache((unsigned long)src, (unsigned long)src + len);
  225. }
  226. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  227. unsigned long user_vaddr,
  228. void *dst, void *src, int len)
  229. {
  230. flush_cache_page(vma, user_vaddr, page_to_pfn(page));
  231. memcpy(dst, src, len);
  232. __flush_dcache((unsigned long)dst, (unsigned long)dst + len);
  233. if (vma->vm_flags & VM_EXEC)
  234. __flush_icache((unsigned long)dst, (unsigned long)dst + len);
  235. }