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
6 * Copyright (C) 2009, Wind River Systems Inc
7 * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
10 #include <linux/export.h>
11 #include <linux/sched.h>
14 #include <linux/pagemap.h>
16 #include <asm/cacheflush.h>
17 #include <asm/cpuinfo.h>
19 static void __flush_dcache(unsigned long start, unsigned long end)
23 start &= ~(cpuinfo.dcache_line_size - 1);
24 end += (cpuinfo.dcache_line_size - 1);
25 end &= ~(cpuinfo.dcache_line_size - 1);
27 if (end > start + cpuinfo.dcache_size)
28 end = start + cpuinfo.dcache_size;
30 for (addr = start; addr < end; addr += cpuinfo.dcache_line_size) {
31 __asm__ __volatile__ (" flushd 0(%0)\n"
33 : /* Inputs */ "r"(addr)
38 static void __invalidate_dcache(unsigned long start, unsigned long end)
42 start &= ~(cpuinfo.dcache_line_size - 1);
43 end += (cpuinfo.dcache_line_size - 1);
44 end &= ~(cpuinfo.dcache_line_size - 1);
46 for (addr = start; addr < end; addr += cpuinfo.dcache_line_size) {
47 __asm__ __volatile__ (" initda 0(%0)\n"
49 : /* Inputs */ "r"(addr)
54 static void __flush_icache(unsigned long start, unsigned long end)
58 start &= ~(cpuinfo.icache_line_size - 1);
59 end += (cpuinfo.icache_line_size - 1);
60 end &= ~(cpuinfo.icache_line_size - 1);
62 if (end > start + cpuinfo.icache_size)
63 end = start + cpuinfo.icache_size;
65 for (addr = start; addr < end; addr += cpuinfo.icache_line_size) {
66 __asm__ __volatile__ (" flushi %0\n"
68 : /* Inputs */ "r"(addr)
71 __asm__ __volatile(" flushp\n");
74 static void flush_aliases(struct address_space *mapping, struct page *page)
76 struct mm_struct *mm = current->active_mm;
77 struct vm_area_struct *mpnt;
82 flush_dcache_mmap_lock(mapping);
83 vma_interval_tree_foreach(mpnt, &mapping->i_mmap, pgoff, pgoff) {
86 if (mpnt->vm_mm != mm)
88 if (!(mpnt->vm_flags & VM_MAYSHARE))
91 offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
92 flush_cache_page(mpnt, mpnt->vm_start + offset,
95 flush_dcache_mmap_unlock(mapping);
98 void flush_cache_all(void)
100 __flush_dcache(0, cpuinfo.dcache_size);
101 __flush_icache(0, cpuinfo.icache_size);
104 void flush_cache_mm(struct mm_struct *mm)
109 void flush_cache_dup_mm(struct mm_struct *mm)
114 void flush_icache_range(unsigned long start, unsigned long end)
116 __flush_dcache(start, end);
117 __flush_icache(start, end);
120 void flush_dcache_range(unsigned long start, unsigned long end)
122 __flush_dcache(start, end);
123 __flush_icache(start, end);
125 EXPORT_SYMBOL(flush_dcache_range);
127 void invalidate_dcache_range(unsigned long start, unsigned long end)
129 __invalidate_dcache(start, end);
131 EXPORT_SYMBOL(invalidate_dcache_range);
133 void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
136 __flush_dcache(start, end);
137 if (vma == NULL || (vma->vm_flags & VM_EXEC))
138 __flush_icache(start, end);
141 void flush_icache_page(struct vm_area_struct *vma, struct page *page)
143 unsigned long start = (unsigned long) page_address(page);
144 unsigned long end = start + PAGE_SIZE;
146 __flush_dcache(start, end);
147 __flush_icache(start, end);
150 void flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,
153 unsigned long start = vmaddr;
154 unsigned long end = start + PAGE_SIZE;
156 __flush_dcache(start, end);
157 if (vma->vm_flags & VM_EXEC)
158 __flush_icache(start, end);
161 void __flush_dcache_page(struct address_space *mapping, struct page *page)
164 * Writeback any data associated with the kernel mapping of this
165 * page. This ensures that data in the physical page is mutually
166 * coherent with the kernels mapping.
168 unsigned long start = (unsigned long)page_address(page);
170 __flush_dcache(start, start + PAGE_SIZE);
173 void flush_dcache_page(struct page *page)
175 struct address_space *mapping;
178 * The zero page is never written to, so never has any dirty
179 * cache lines, and therefore never needs to be flushed.
181 if (page == ZERO_PAGE(0))
184 mapping = page_mapping_file(page);
186 /* Flush this page if there are aliases. */
187 if (mapping && !mapping_mapped(mapping)) {
188 clear_bit(PG_dcache_clean, &page->flags);
190 __flush_dcache_page(mapping, page);
192 unsigned long start = (unsigned long)page_address(page);
193 flush_aliases(mapping, page);
194 flush_icache_range(start, start + PAGE_SIZE);
196 set_bit(PG_dcache_clean, &page->flags);
199 EXPORT_SYMBOL(flush_dcache_page);
201 void update_mmu_cache(struct vm_area_struct *vma,
202 unsigned long address, pte_t *ptep)
205 unsigned long pfn = pte_pfn(pte);
207 struct address_space *mapping;
209 reload_tlb_page(vma, address, pte);
215 * The zero page is never written to, so never has any dirty
216 * cache lines, and therefore never needs to be flushed.
218 page = pfn_to_page(pfn);
219 if (page == ZERO_PAGE(0))
222 mapping = page_mapping_file(page);
223 if (!test_and_set_bit(PG_dcache_clean, &page->flags))
224 __flush_dcache_page(mapping, page);
228 flush_aliases(mapping, page);
229 if (vma->vm_flags & VM_EXEC)
230 flush_icache_page(vma, page);
234 void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
237 __flush_dcache(vaddr, vaddr + PAGE_SIZE);
238 __flush_icache(vaddr, vaddr + PAGE_SIZE);
239 copy_page(vto, vfrom);
240 __flush_dcache((unsigned long)vto, (unsigned long)vto + PAGE_SIZE);
241 __flush_icache((unsigned long)vto, (unsigned long)vto + PAGE_SIZE);
244 void clear_user_page(void *addr, unsigned long vaddr, struct page *page)
246 __flush_dcache(vaddr, vaddr + PAGE_SIZE);
247 __flush_icache(vaddr, vaddr + PAGE_SIZE);
249 __flush_dcache((unsigned long)addr, (unsigned long)addr + PAGE_SIZE);
250 __flush_icache((unsigned long)addr, (unsigned long)addr + PAGE_SIZE);
253 void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
254 unsigned long user_vaddr,
255 void *dst, void *src, int len)
257 flush_cache_page(vma, user_vaddr, page_to_pfn(page));
258 memcpy(dst, src, len);
259 __flush_dcache((unsigned long)src, (unsigned long)src + len);
260 if (vma->vm_flags & VM_EXEC)
261 __flush_icache((unsigned long)src, (unsigned long)src + len);
264 void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
265 unsigned long user_vaddr,
266 void *dst, void *src, int len)
268 flush_cache_page(vma, user_vaddr, page_to_pfn(page));
269 memcpy(dst, src, len);
270 __flush_dcache((unsigned long)dst, (unsigned long)dst + len);
271 if (vma->vm_flags & VM_EXEC)
272 __flush_icache((unsigned long)dst, (unsigned long)dst + len);