flush.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * linux/arch/arm/mm/flush.c
  3. *
  4. * Copyright (C) 1995-2002 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/highmem.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/cachetype.h>
  16. #include <asm/highmem.h>
  17. #include <asm/smp_plat.h>
  18. #include <asm/system.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/smp_plat.h>
  21. #include "mm.h"
  22. #ifdef CONFIG_CPU_CACHE_VIPT
  23. #define ALIAS_FLUSH_START 0xffff4000
  24. static void flush_pfn_alias(unsigned long pfn, unsigned long vaddr)
  25. {
  26. unsigned long to = ALIAS_FLUSH_START + (CACHE_COLOUR(vaddr) << PAGE_SHIFT);
  27. const int zero = 0;
  28. set_pte_ext(TOP_PTE(to), pfn_pte(pfn, PAGE_KERNEL), 0);
  29. flush_tlb_kernel_page(to);
  30. asm( "mcrr p15, 0, %1, %0, c14\n"
  31. " mcr p15, 0, %2, c7, c10, 4"
  32. :
  33. : "r" (to), "r" (to + PAGE_SIZE - L1_CACHE_BYTES), "r" (zero)
  34. : "cc");
  35. }
  36. static void flush_icache_alias(unsigned long pfn, unsigned long vaddr, unsigned long len)
  37. {
  38. unsigned long colour = CACHE_COLOUR(vaddr);
  39. unsigned long offset = vaddr & (PAGE_SIZE - 1);
  40. unsigned long to;
  41. set_pte_ext(TOP_PTE(ALIAS_FLUSH_START) + colour, pfn_pte(pfn, PAGE_KERNEL), 0);
  42. to = ALIAS_FLUSH_START + (colour << PAGE_SHIFT) + offset;
  43. flush_tlb_kernel_page(to);
  44. flush_icache_range(to, to + len);
  45. }
  46. void flush_cache_mm(struct mm_struct *mm)
  47. {
  48. if (cache_is_vivt()) {
  49. vivt_flush_cache_mm(mm);
  50. return;
  51. }
  52. if (cache_is_vipt_aliasing()) {
  53. asm( "mcr p15, 0, %0, c7, c14, 0\n"
  54. " mcr p15, 0, %0, c7, c10, 4"
  55. :
  56. : "r" (0)
  57. : "cc");
  58. }
  59. }
  60. void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  61. {
  62. if (cache_is_vivt()) {
  63. vivt_flush_cache_range(vma, start, end);
  64. return;
  65. }
  66. if (cache_is_vipt_aliasing()) {
  67. asm( "mcr p15, 0, %0, c7, c14, 0\n"
  68. " mcr p15, 0, %0, c7, c10, 4"
  69. :
  70. : "r" (0)
  71. : "cc");
  72. }
  73. if (vma->vm_flags & VM_EXEC)
  74. __flush_icache_all();
  75. }
  76. void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
  77. {
  78. if (cache_is_vivt()) {
  79. vivt_flush_cache_page(vma, user_addr, pfn);
  80. return;
  81. }
  82. if (cache_is_vipt_aliasing()) {
  83. flush_pfn_alias(pfn, user_addr);
  84. __flush_icache_all();
  85. }
  86. if (vma->vm_flags & VM_EXEC && icache_is_vivt_asid_tagged())
  87. __flush_icache_all();
  88. }
  89. #else
  90. #define flush_pfn_alias(pfn,vaddr) do { } while (0)
  91. #define flush_icache_alias(pfn,vaddr,len) do { } while (0)
  92. #endif
  93. static void flush_ptrace_access_other(void *args)
  94. {
  95. __flush_icache_all();
  96. }
  97. static
  98. void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
  99. unsigned long uaddr, void *kaddr, unsigned long len)
  100. {
  101. if (cache_is_vivt()) {
  102. if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm))) {
  103. unsigned long addr = (unsigned long)kaddr;
  104. __cpuc_coherent_kern_range(addr, addr + len);
  105. }
  106. return;
  107. }
  108. if (cache_is_vipt_aliasing()) {
  109. flush_pfn_alias(page_to_pfn(page), uaddr);
  110. __flush_icache_all();
  111. return;
  112. }
  113. /* VIPT non-aliasing D-cache */
  114. if (vma->vm_flags & VM_EXEC) {
  115. unsigned long addr = (unsigned long)kaddr;
  116. if (icache_is_vipt_aliasing())
  117. flush_icache_alias(page_to_pfn(page), uaddr, len);
  118. else
  119. __cpuc_coherent_kern_range(addr, addr + len);
  120. if (cache_ops_need_broadcast())
  121. smp_call_function(flush_ptrace_access_other,
  122. NULL, 1);
  123. }
  124. }
  125. /*
  126. * Copy user data from/to a page which is mapped into a different
  127. * processes address space. Really, we want to allow our "user
  128. * space" model to handle this.
  129. *
  130. * Note that this code needs to run on the current CPU.
  131. */
  132. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  133. unsigned long uaddr, void *dst, const void *src,
  134. unsigned long len)
  135. {
  136. #ifdef CONFIG_SMP
  137. preempt_disable();
  138. #endif
  139. memcpy(dst, src, len);
  140. flush_ptrace_access(vma, page, uaddr, dst, len);
  141. #ifdef CONFIG_SMP
  142. preempt_enable();
  143. #endif
  144. }
  145. void __flush_dcache_page(struct address_space *mapping, struct page *page)
  146. {
  147. /*
  148. * Writeback any data associated with the kernel mapping of this
  149. * page. This ensures that data in the physical page is mutually
  150. * coherent with the kernels mapping.
  151. */
  152. if (!PageHighMem(page)) {
  153. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  154. } else {
  155. void *addr = kmap_high_get(page);
  156. if (addr) {
  157. __cpuc_flush_dcache_area(addr, PAGE_SIZE);
  158. kunmap_high(page);
  159. } else if (cache_is_vipt()) {
  160. /* unmapped pages might still be cached */
  161. addr = kmap_atomic(page);
  162. __cpuc_flush_dcache_area(addr, PAGE_SIZE);
  163. kunmap_atomic(addr);
  164. }
  165. }
  166. /*
  167. * If this is a page cache page, and we have an aliasing VIPT cache,
  168. * we only need to do one flush - which would be at the relevant
  169. * userspace colour, which is congruent with page->index.
  170. */
  171. if (mapping && cache_is_vipt_aliasing())
  172. flush_pfn_alias(page_to_pfn(page),
  173. page->index << PAGE_CACHE_SHIFT);
  174. }
  175. static void __flush_dcache_aliases(struct address_space *mapping, struct page *page)
  176. {
  177. struct mm_struct *mm = current->active_mm;
  178. struct vm_area_struct *mpnt;
  179. struct prio_tree_iter iter;
  180. pgoff_t pgoff;
  181. /*
  182. * There are possible user space mappings of this page:
  183. * - VIVT cache: we need to also write back and invalidate all user
  184. * data in the current VM view associated with this page.
  185. * - aliasing VIPT: we only need to find one mapping of this page.
  186. */
  187. pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  188. flush_dcache_mmap_lock(mapping);
  189. vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
  190. unsigned long offset;
  191. /*
  192. * If this VMA is not in our MM, we can ignore it.
  193. */
  194. if (mpnt->vm_mm != mm)
  195. continue;
  196. if (!(mpnt->vm_flags & VM_MAYSHARE))
  197. continue;
  198. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  199. flush_cache_page(mpnt, mpnt->vm_start + offset, page_to_pfn(page));
  200. }
  201. flush_dcache_mmap_unlock(mapping);
  202. }
  203. #if __LINUX_ARM_ARCH__ >= 6
  204. void __sync_icache_dcache(pte_t pteval)
  205. {
  206. unsigned long pfn;
  207. struct page *page;
  208. struct address_space *mapping;
  209. if (!pte_present_user(pteval))
  210. return;
  211. if (cache_is_vipt_nonaliasing() && !pte_exec(pteval))
  212. /* only flush non-aliasing VIPT caches for exec mappings */
  213. return;
  214. pfn = pte_pfn(pteval);
  215. if (!pfn_valid(pfn))
  216. return;
  217. page = pfn_to_page(pfn);
  218. if (cache_is_vipt_aliasing())
  219. mapping = page_mapping(page);
  220. else
  221. mapping = NULL;
  222. if (!test_and_set_bit(PG_dcache_clean, &page->flags))
  223. __flush_dcache_page(mapping, page);
  224. /* pte_exec() already checked above for non-aliasing VIPT cache */
  225. if (cache_is_vipt_nonaliasing() || pte_exec(pteval))
  226. __flush_icache_all();
  227. }
  228. #endif
  229. /*
  230. * Ensure cache coherency between kernel mapping and userspace mapping
  231. * of this page.
  232. *
  233. * We have three cases to consider:
  234. * - VIPT non-aliasing cache: fully coherent so nothing required.
  235. * - VIVT: fully aliasing, so we need to handle every alias in our
  236. * current VM view.
  237. * - VIPT aliasing: need to handle one alias in our current VM view.
  238. *
  239. * If we need to handle aliasing:
  240. * If the page only exists in the page cache and there are no user
  241. * space mappings, we can be lazy and remember that we may have dirty
  242. * kernel cache lines for later. Otherwise, we assume we have
  243. * aliasing mappings.
  244. *
  245. * Note that we disable the lazy flush for SMP.
  246. */
  247. void flush_dcache_page(struct page *page)
  248. {
  249. struct address_space *mapping;
  250. /*
  251. * The zero page is never written to, so never has any dirty
  252. * cache lines, and therefore never needs to be flushed.
  253. */
  254. if (page == ZERO_PAGE(0))
  255. return;
  256. mapping = page_mapping(page);
  257. if (!cache_ops_need_broadcast() &&
  258. mapping && !mapping_mapped(mapping))
  259. clear_bit(PG_dcache_clean, &page->flags);
  260. else {
  261. __flush_dcache_page(mapping, page);
  262. if (mapping && cache_is_vivt())
  263. __flush_dcache_aliases(mapping, page);
  264. else if (mapping)
  265. __flush_icache_all();
  266. set_bit(PG_dcache_clean, &page->flags);
  267. }
  268. }
  269. EXPORT_SYMBOL(flush_dcache_page);
  270. /*
  271. * Flush an anonymous page so that users of get_user_pages()
  272. * can safely access the data. The expected sequence is:
  273. *
  274. * get_user_pages()
  275. * -> flush_anon_page
  276. * memcpy() to/from page
  277. * if written to page, flush_dcache_page()
  278. */
  279. void __flush_anon_page(struct vm_area_struct *vma, struct page *page, unsigned long vmaddr)
  280. {
  281. unsigned long pfn;
  282. /* VIPT non-aliasing caches need do nothing */
  283. if (cache_is_vipt_nonaliasing())
  284. return;
  285. /*
  286. * Write back and invalidate userspace mapping.
  287. */
  288. pfn = page_to_pfn(page);
  289. if (cache_is_vivt()) {
  290. flush_cache_page(vma, vmaddr, pfn);
  291. } else {
  292. /*
  293. * For aliasing VIPT, we can flush an alias of the
  294. * userspace address only.
  295. */
  296. flush_pfn_alias(pfn, vmaddr);
  297. __flush_icache_all();
  298. }
  299. /*
  300. * Invalidate kernel mapping. No data should be contained
  301. * in this mapping of the page. FIXME: this is overkill
  302. * since we actually ask for a write-back and invalidate.
  303. */
  304. __cpuc_flush_dcache_area(page_address(page), PAGE_SIZE);
  305. }