blob: 96aea6624318d0e805b55afa9c0f6c9f7a54fadc [file] [log] [blame]
Chris Zankel66569202007-08-22 10:14:51 -07001/*
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
16#include <linux/init.h>
17#include <linux/signal.h>
18#include <linux/sched.h>
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/string.h>
22#include <linux/types.h>
23#include <linux/ptrace.h>
24#include <linux/bootmem.h>
25#include <linux/swap.h>
26#include <linux/pagemap.h>
27
Chris Zankel66569202007-08-22 10:14:51 -070028#include <asm/bootparam.h>
29#include <asm/mmu_context.h>
30#include <asm/tlb.h>
31#include <asm/tlbflush.h>
32#include <asm/page.h>
33#include <asm/pgalloc.h>
34#include <asm/pgtable.h>
35
36//#define printd(x...) printk(x)
37#define printd(x...) do { } while(0)
38
39/*
40 * Note:
41 * The kernel provides one architecture bit PG_arch_1 in the page flags that
42 * can be used for cache coherency.
43 *
44 * I$-D$ coherency.
45 *
46 * The Xtensa architecture doesn't keep the instruction cache coherent with
47 * the data cache. We use the architecture bit to indicate if the caches
48 * are coherent. The kernel clears this bit whenever a page is added to the
49 * page cache. At that time, the caches might not be in sync. We, therefore,
50 * define this flag as 'clean' if set.
51 *
52 * D-cache aliasing.
53 *
54 * With cache aliasing, we have to always flush the cache when pages are
55 * unmapped (see tlb_start_vma(). So, we use this flag to indicate a dirty
56 * page.
57 *
58 *
59 *
60 */
61
Max Filippov65559102014-02-04 02:17:09 +040062#if (DCACHE_WAY_SIZE > PAGE_SIZE) && defined(CONFIG_HIGHMEM)
63#error "HIGHMEM is not supported on cores with aliasing cache."
64#endif
65
Max Filippova91902d2014-07-21 18:54:11 +040066#if (DCACHE_WAY_SIZE > PAGE_SIZE)
67static inline void kmap_invalidate_coherent(struct page *page,
68 unsigned long vaddr)
69{
70 if (!DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
71 unsigned long kvaddr;
72
73 if (!PageHighMem(page)) {
74 kvaddr = (unsigned long)page_to_virt(page);
75
76 __invalidate_dcache_page(kvaddr);
77 } else {
78 kvaddr = TLBTEMP_BASE_1 +
79 (page_to_phys(page) & DCACHE_ALIAS_MASK);
80
81 __invalidate_dcache_page_alias(kvaddr,
82 page_to_phys(page));
83 }
84 }
85}
86
87static inline void *coherent_kvaddr(struct page *page, unsigned long base,
88 unsigned long vaddr, unsigned long *paddr)
89{
90 if (PageHighMem(page) || !DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
91 *paddr = page_to_phys(page);
92 return (void *)(base + (vaddr & DCACHE_ALIAS_MASK));
93 } else {
94 *paddr = 0;
95 return page_to_virt(page);
96 }
97}
98
99void clear_user_highpage(struct page *page, unsigned long vaddr)
100{
101 unsigned long paddr;
102 void *kvaddr = coherent_kvaddr(page, TLBTEMP_BASE_1, vaddr, &paddr);
103
104 pagefault_disable();
105 kmap_invalidate_coherent(page, vaddr);
106 set_bit(PG_arch_1, &page->flags);
107 clear_page_alias(kvaddr, paddr);
108 pagefault_enable();
109}
110
111void copy_user_highpage(struct page *dst, struct page *src,
112 unsigned long vaddr, struct vm_area_struct *vma)
113{
114 unsigned long dst_paddr, src_paddr;
115 void *dst_vaddr = coherent_kvaddr(dst, TLBTEMP_BASE_1, vaddr,
116 &dst_paddr);
117 void *src_vaddr = coherent_kvaddr(src, TLBTEMP_BASE_2, vaddr,
118 &src_paddr);
119
120 pagefault_disable();
121 kmap_invalidate_coherent(dst, vaddr);
122 set_bit(PG_arch_1, &dst->flags);
123 copy_page_alias(dst_vaddr, src_vaddr, dst_paddr, src_paddr);
124 pagefault_enable();
125}
126
127#endif /* DCACHE_WAY_SIZE > PAGE_SIZE */
128
Chris Zankel66569202007-08-22 10:14:51 -0700129#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
130
131/*
132 * Any time the kernel writes to a user page cache page, or it is about to
133 * read from a page cache page this routine is called.
134 *
135 */
136
137void flush_dcache_page(struct page *page)
138{
139 struct address_space *mapping = page_mapping(page);
140
141 /*
142 * If we have a mapping but the page is not mapped to user-space
143 * yet, we simply mark this page dirty and defer flushing the
144 * caches until update_mmu().
145 */
146
147 if (mapping && !mapping_mapped(mapping)) {
148 if (!test_bit(PG_arch_1, &page->flags))
149 set_bit(PG_arch_1, &page->flags);
150 return;
151
152 } else {
153
154 unsigned long phys = page_to_phys(page);
155 unsigned long temp = page->index << PAGE_SHIFT;
156 unsigned long alias = !(DCACHE_ALIAS_EQ(temp, phys));
157 unsigned long virt;
158
159 /*
160 * Flush the page in kernel space and user space.
161 * Note that we can omit that step if aliasing is not
162 * an issue, but we do have to synchronize I$ and D$
163 * if we have a mapping.
164 */
165
166 if (!alias && !mapping)
167 return;
168
169 __flush_invalidate_dcache_page((long)page_address(page));
170
171 virt = TLBTEMP_BASE_1 + (temp & DCACHE_ALIAS_MASK);
172
173 if (alias)
174 __flush_invalidate_dcache_page_alias(virt, phys);
175
176 if (mapping)
177 __invalidate_icache_page_alias(virt, phys);
178 }
179
180 /* There shouldn't be an entry in the cache for this page anymore. */
181}
182
183
184/*
185 * For now, flush the whole cache. FIXME??
186 */
187
Max Filippovf6151362013-10-17 02:42:26 +0400188void local_flush_cache_range(struct vm_area_struct *vma,
Chris Zankel66569202007-08-22 10:14:51 -0700189 unsigned long start, unsigned long end)
190{
191 __flush_invalidate_dcache_all();
192 __invalidate_icache_all();
193}
194
195/*
196 * Remove any entry in the cache for this page.
197 *
198 * Note that this function is only called for user pages, so use the
199 * alias versions of the cache flush functions.
200 */
201
Max Filippovf6151362013-10-17 02:42:26 +0400202void local_flush_cache_page(struct vm_area_struct *vma, unsigned long address,
Chris Zankelc4c45942012-11-28 16:53:51 -0800203 unsigned long pfn)
Chris Zankel66569202007-08-22 10:14:51 -0700204{
205 /* Note that we have to use the 'alias' address to avoid multi-hit */
206
207 unsigned long phys = page_to_phys(pfn_to_page(pfn));
208 unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
209
210 __flush_invalidate_dcache_page_alias(virt, phys);
211 __invalidate_icache_page_alias(virt, phys);
212}
213
214#endif
215
216void
Russell King4b3073e2009-12-18 16:40:18 +0000217update_mmu_cache(struct vm_area_struct * vma, unsigned long addr, pte_t *ptep)
Chris Zankel66569202007-08-22 10:14:51 -0700218{
Russell King4b3073e2009-12-18 16:40:18 +0000219 unsigned long pfn = pte_pfn(*ptep);
Chris Zankel66569202007-08-22 10:14:51 -0700220 struct page *page;
221
222 if (!pfn_valid(pfn))
223 return;
224
225 page = pfn_to_page(pfn);
226
227 /* Invalidate old entry in TLBs */
228
Max Filippovf6151362013-10-17 02:42:26 +0400229 flush_tlb_page(vma, addr);
Chris Zankel66569202007-08-22 10:14:51 -0700230
231#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
232
233 if (!PageReserved(page) && test_bit(PG_arch_1, &page->flags)) {
234
Chris Zankel66569202007-08-22 10:14:51 -0700235 unsigned long paddr = (unsigned long) page_address(page);
236 unsigned long phys = page_to_phys(page);
Chris Zankelc4c45942012-11-28 16:53:51 -0800237 unsigned long tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
Chris Zankel66569202007-08-22 10:14:51 -0700238
239 __flush_invalidate_dcache_page(paddr);
240
Chris Zankelc4c45942012-11-28 16:53:51 -0800241 __flush_invalidate_dcache_page_alias(tmp, phys);
242 __invalidate_icache_page_alias(tmp, phys);
Chris Zankel66569202007-08-22 10:14:51 -0700243
244 clear_bit(PG_arch_1, &page->flags);
245 }
246#else
247 if (!PageReserved(page) && !test_bit(PG_arch_1, &page->flags)
248 && (vma->vm_flags & VM_EXEC) != 0) {
Max Filippov65559102014-02-04 02:17:09 +0400249 unsigned long paddr = (unsigned long)kmap_atomic(page);
Chris Zankelb67360d2007-09-06 01:38:18 -0700250 __flush_dcache_page(paddr);
251 __invalidate_icache_page(paddr);
Chris Zankel66569202007-08-22 10:14:51 -0700252 set_bit(PG_arch_1, &page->flags);
Max Filippov65559102014-02-04 02:17:09 +0400253 kunmap_atomic((void *)paddr);
Chris Zankel66569202007-08-22 10:14:51 -0700254 }
255#endif
256}
257
258/*
259 * access_process_vm() has called get_user_pages(), which has done a
260 * flush_dcache_page() on the page.
261 */
262
263#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
264
Chris Zankelc4c45942012-11-28 16:53:51 -0800265void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
Chris Zankel66569202007-08-22 10:14:51 -0700266 unsigned long vaddr, void *dst, const void *src,
267 unsigned long len)
268{
269 unsigned long phys = page_to_phys(page);
270 unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
271
272 /* Flush and invalidate user page if aliased. */
273
274 if (alias) {
Chris Zankelc4c45942012-11-28 16:53:51 -0800275 unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
276 __flush_invalidate_dcache_page_alias(t, phys);
Chris Zankel66569202007-08-22 10:14:51 -0700277 }
278
279 /* Copy data */
280
281 memcpy(dst, src, len);
282
283 /*
284 * Flush and invalidate kernel page if aliased and synchronize
285 * data and instruction caches for executable pages.
286 */
287
288 if (alias) {
Chris Zankelc4c45942012-11-28 16:53:51 -0800289 unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
Chris Zankel66569202007-08-22 10:14:51 -0700290
291 __flush_invalidate_dcache_range((unsigned long) dst, len);
Chris Zankelc4c45942012-11-28 16:53:51 -0800292 if ((vma->vm_flags & VM_EXEC) != 0)
293 __invalidate_icache_page_alias(t, phys);
Chris Zankel66569202007-08-22 10:14:51 -0700294
295 } else if ((vma->vm_flags & VM_EXEC) != 0) {
296 __flush_dcache_range((unsigned long)dst,len);
297 __invalidate_icache_range((unsigned long) dst, len);
298 }
299}
300
301extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
302 unsigned long vaddr, void *dst, const void *src,
303 unsigned long len)
304{
305 unsigned long phys = page_to_phys(page);
306 unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
307
308 /*
309 * Flush user page if aliased.
310 * (Note: a simply flush would be sufficient)
311 */
312
313 if (alias) {
Chris Zankelc4c45942012-11-28 16:53:51 -0800314 unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
315 __flush_invalidate_dcache_page_alias(t, phys);
Chris Zankel66569202007-08-22 10:14:51 -0700316 }
317
318 memcpy(dst, src, len);
319}
320
321#endif