cacheflush.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 <asm/cacheflush.h>
  14. #include <asm/cpuinfo.h>
  15. static void __flush_dcache(unsigned long start, unsigned long end)
  16. {
  17. unsigned long addr;
  18. start &= ~(cpuinfo.dcache_line_size - 1);
  19. end += (cpuinfo.dcache_line_size - 1);
  20. end &= ~(cpuinfo.dcache_line_size - 1);
  21. if (end > start + cpuinfo.dcache_size)
  22. end = start + cpuinfo.dcache_size;
  23. for (addr = start; addr < end; addr += cpuinfo.dcache_line_size) {
  24. __asm__ __volatile__ (" flushd 0(%0)\n"
  25. : /* Outputs */
  26. : /* Inputs */ "r"(addr)
  27. /* : No clobber */);
  28. }
  29. }
  30. static void __invalidate_dcache(unsigned long start, unsigned long end)
  31. {
  32. unsigned long addr;
  33. start &= ~(cpuinfo.dcache_line_size - 1);
  34. end += (cpuinfo.dcache_line_size - 1);
  35. end &= ~(cpuinfo.dcache_line_size - 1);
  36. for (addr = start; addr < end; addr += cpuinfo.dcache_line_size) {
  37. __asm__ __volatile__ (" initda 0(%0)\n"
  38. : /* Outputs */
  39. : /* Inputs */ "r"(addr)
  40. /* : No clobber */);
  41. }
  42. }
  43. static void __flush_icache(unsigned long start, unsigned long end)
  44. {
  45. unsigned long addr;
  46. start &= ~(cpuinfo.icache_line_size - 1);
  47. end += (cpuinfo.icache_line_size - 1);
  48. end &= ~(cpuinfo.icache_line_size - 1);
  49. if (end > start + cpuinfo.icache_size)
  50. end = start + cpuinfo.icache_size;
  51. for (addr = start; addr < end; addr += cpuinfo.icache_line_size) {
  52. __asm__ __volatile__ (" flushi %0\n"
  53. : /* Outputs */
  54. : /* Inputs */ "r"(addr)
  55. /* : No clobber */);
  56. }
  57. __asm__ __volatile(" flushp\n");
  58. }
  59. static void flush_aliases(struct address_space *mapping, struct page *page)
  60. {
  61. struct mm_struct *mm = current->active_mm;
  62. struct vm_area_struct *mpnt;
  63. pgoff_t pgoff;
  64. pgoff = page->index;
  65. flush_dcache_mmap_lock(mapping);
  66. vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {
  67. unsigned long offset;
  68. if (mpnt->vm_mm != mm)
  69. continue;
  70. if (!(mpnt->vm_flags & VM_MAYSHARE))
  71. continue;
  72. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  73. flush_cache_page(mpnt, mpnt->vm_start + offset,
  74. page_to_pfn(page));
  75. }
  76. flush_dcache_mmap_unlock(mapping);
  77. }
  78. void flush_cache_all(void)
  79. {
  80. __flush_dcache(0, cpuinfo.dcache_size);
  81. __flush_icache(0, cpuinfo.icache_size);
  82. }
  83. void flush_cache_mm(struct mm_struct *mm)
  84. {
  85. flush_cache_all();
  86. }
  87. void flush_cache_dup_mm(struct mm_struct *mm)
  88. {
  89. flush_cache_all();
  90. }
  91. void flush_icache_range(unsigned long start, unsigned long end)
  92. {
  93. __flush_dcache(start, end);
  94. __flush_icache(start, end);
  95. }
  96. void flush_dcache_range(unsigned long start, unsigned long end)
  97. {
  98. __flush_dcache(start, end);
  99. __flush_icache(start, end);
  100. }
  101. EXPORT_SYMBOL(flush_dcache_range);
  102. void invalidate_dcache_range(unsigned long start, unsigned long end)
  103. {
  104. __invalidate_dcache(start, end);
  105. }
  106. EXPORT_SYMBOL(invalidate_dcache_range);
  107. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  108. unsigned long end)
  109. {
  110. __flush_dcache(start, end);
  111. if (vma == NULL || (vma->vm_flags & VM_EXEC))
  112. __flush_icache(start, end);
  113. }
  114. void flush_icache_page(struct vm_area_struct *vma, struct page *page)
  115. {
  116. unsigned long start = (unsigned long) page_address(page);
  117. unsigned long end = start + PAGE_SIZE;
  118. __flush_dcache(start, end);
  119. __flush_icache(start, end);
  120. }
  121. void flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,
  122. unsigned long pfn)
  123. {
  124. unsigned long start = vmaddr;
  125. unsigned long end = start + PAGE_SIZE;
  126. __flush_dcache(start, end);
  127. if (vma->vm_flags & VM_EXEC)
  128. __flush_icache(start, end);
  129. }
  130. void __flush_dcache_page(struct address_space *mapping, struct page *page)
  131. {
  132. /*
  133. * Writeback any data associated with the kernel mapping of this
  134. * page. This ensures that data in the physical page is mutually
  135. * coherent with the kernels mapping.
  136. */
  137. unsigned long start = (unsigned long)page_address(page);
  138. __flush_dcache(start, start + PAGE_SIZE);
  139. }
  140. void flush_dcache_page(struct page *page)
  141. {
  142. struct address_space *mapping;
  143. /*
  144. * The zero page is never written to, so never has any dirty
  145. * cache lines, and therefore never needs to be flushed.
  146. */
  147. if (page == ZERO_PAGE(0))
  148. return;
  149. mapping = page_mapping(page);
  150. /* Flush this page if there are aliases. */
  151. if (mapping && !mapping_mapped(mapping)) {
  152. clear_bit(PG_dcache_clean, &page->flags);
  153. } else {
  154. __flush_dcache_page(mapping, page);
  155. if (mapping) {
  156. unsigned long start = (unsigned long)page_address(page);
  157. flush_aliases(mapping, page);
  158. flush_icache_range(start, start + PAGE_SIZE);
  159. }
  160. set_bit(PG_dcache_clean, &page->flags);
  161. }
  162. }
  163. EXPORT_SYMBOL(flush_dcache_page);
  164. void update_mmu_cache(struct vm_area_struct *vma,
  165. unsigned long address, pte_t *pte)
  166. {
  167. unsigned long pfn = pte_pfn(*pte);
  168. struct page *page;
  169. struct address_space *mapping;
  170. if (!pfn_valid(pfn))
  171. return;
  172. /*
  173. * The zero page is never written to, so never has any dirty
  174. * cache lines, and therefore never needs to be flushed.
  175. */
  176. page = pfn_to_page(pfn);
  177. if (page == ZERO_PAGE(0))
  178. return;
  179. mapping = page_mapping(page);
  180. if (!test_and_set_bit(PG_dcache_clean, &page->flags))
  181. __flush_dcache_page(mapping, page);
  182. if(mapping)
  183. {
  184. flush_aliases(mapping, page);
  185. if (vma->vm_flags & VM_EXEC)
  186. flush_icache_page(vma, page);
  187. }
  188. }
  189. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  190. struct page *to)
  191. {
  192. __flush_dcache(vaddr, vaddr + PAGE_SIZE);
  193. __flush_icache(vaddr, vaddr + PAGE_SIZE);
  194. copy_page(vto, vfrom);
  195. __flush_dcache((unsigned long)vto, (unsigned long)vto + PAGE_SIZE);
  196. __flush_icache((unsigned long)vto, (unsigned long)vto + PAGE_SIZE);
  197. }
  198. void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
  199. {
  200. __flush_dcache(vaddr, vaddr + PAGE_SIZE);
  201. __flush_icache(vaddr, vaddr + PAGE_SIZE);
  202. clear_page(addr);
  203. __flush_dcache((unsigned long)addr, (unsigned long)addr + PAGE_SIZE);
  204. __flush_icache((unsigned long)addr, (unsigned long)addr + PAGE_SIZE);
  205. }
  206. void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  207. unsigned long user_vaddr,
  208. void *dst, void *src, int len)
  209. {
  210. flush_cache_page(vma, user_vaddr, page_to_pfn(page));
  211. memcpy(dst, src, len);
  212. __flush_dcache((unsigned long)src, (unsigned long)src + len);
  213. if (vma->vm_flags & VM_EXEC)
  214. __flush_icache((unsigned long)src, (unsigned long)src + len);
  215. }
  216. void copy_to_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)dst, (unsigned long)dst + len);
  223. if (vma->vm_flags & VM_EXEC)
  224. __flush_icache((unsigned long)dst, (unsigned long)dst + len);
  225. }