GNU Linux-libre 4.4.288-gnu1
[releases.git] / arch / xtensa / mm / cache.c
1 /*
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
28 #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
62 #if (DCACHE_WAY_SIZE > PAGE_SIZE)
63 static inline void kmap_invalidate_coherent(struct page *page,
64                                             unsigned long vaddr)
65 {
66         if (!DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
67                 unsigned long kvaddr;
68
69                 if (!PageHighMem(page)) {
70                         kvaddr = (unsigned long)page_to_virt(page);
71
72                         __invalidate_dcache_page(kvaddr);
73                 } else {
74                         kvaddr = TLBTEMP_BASE_1 +
75                                 (page_to_phys(page) & DCACHE_ALIAS_MASK);
76
77                         preempt_disable();
78                         __invalidate_dcache_page_alias(kvaddr,
79                                                        page_to_phys(page));
80                         preempt_enable();
81                 }
82         }
83 }
84
85 static inline void *coherent_kvaddr(struct page *page, unsigned long base,
86                                     unsigned long vaddr, unsigned long *paddr)
87 {
88         if (PageHighMem(page) || !DCACHE_ALIAS_EQ(page_to_phys(page), vaddr)) {
89                 *paddr = page_to_phys(page);
90                 return (void *)(base + (vaddr & DCACHE_ALIAS_MASK));
91         } else {
92                 *paddr = 0;
93                 return page_to_virt(page);
94         }
95 }
96
97 void clear_user_highpage(struct page *page, unsigned long vaddr)
98 {
99         unsigned long paddr;
100         void *kvaddr = coherent_kvaddr(page, TLBTEMP_BASE_1, vaddr, &paddr);
101
102         preempt_disable();
103         kmap_invalidate_coherent(page, vaddr);
104         set_bit(PG_arch_1, &page->flags);
105         clear_page_alias(kvaddr, paddr);
106         preempt_enable();
107 }
108
109 void copy_user_highpage(struct page *dst, struct page *src,
110                         unsigned long vaddr, struct vm_area_struct *vma)
111 {
112         unsigned long dst_paddr, src_paddr;
113         void *dst_vaddr = coherent_kvaddr(dst, TLBTEMP_BASE_1, vaddr,
114                                           &dst_paddr);
115         void *src_vaddr = coherent_kvaddr(src, TLBTEMP_BASE_2, vaddr,
116                                           &src_paddr);
117
118         preempt_disable();
119         kmap_invalidate_coherent(dst, vaddr);
120         set_bit(PG_arch_1, &dst->flags);
121         copy_page_alias(dst_vaddr, src_vaddr, dst_paddr, src_paddr);
122         preempt_enable();
123 }
124
125 #endif /* DCACHE_WAY_SIZE > PAGE_SIZE */
126
127 #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
128
129 /*
130  * Any time the kernel writes to a user page cache page, or it is about to
131  * read from a page cache page this routine is called.
132  *
133  */
134
135 void flush_dcache_page(struct page *page)
136 {
137         struct address_space *mapping = page_mapping(page);
138
139         /*
140          * If we have a mapping but the page is not mapped to user-space
141          * yet, we simply mark this page dirty and defer flushing the 
142          * caches until update_mmu().
143          */
144
145         if (mapping && !mapping_mapped(mapping)) {
146                 if (!test_bit(PG_arch_1, &page->flags))
147                         set_bit(PG_arch_1, &page->flags);
148                 return;
149
150         } else {
151
152                 unsigned long phys = page_to_phys(page);
153                 unsigned long temp = page->index << PAGE_SHIFT;
154                 unsigned long alias = !(DCACHE_ALIAS_EQ(temp, phys));
155                 unsigned long virt;
156
157                 /* 
158                  * Flush the page in kernel space and user space.
159                  * Note that we can omit that step if aliasing is not
160                  * an issue, but we do have to synchronize I$ and D$
161                  * if we have a mapping.
162                  */
163
164                 if (!alias && !mapping)
165                         return;
166
167                 preempt_disable();
168                 virt = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
169                 __flush_invalidate_dcache_page_alias(virt, phys);
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                 preempt_enable();
179         }
180
181         /* There shouldn't be an entry in the cache for this page anymore. */
182 }
183
184
185 /*
186  * For now, flush the whole cache. FIXME??
187  */
188
189 void local_flush_cache_range(struct vm_area_struct *vma,
190                        unsigned long start, unsigned long end)
191 {
192         __flush_invalidate_dcache_all();
193         __invalidate_icache_all();
194 }
195
196 /* 
197  * Remove any entry in the cache for this page. 
198  *
199  * Note that this function is only called for user pages, so use the
200  * alias versions of the cache flush functions.
201  */
202
203 void local_flush_cache_page(struct vm_area_struct *vma, unsigned long address,
204                       unsigned long pfn)
205 {
206         /* Note that we have to use the 'alias' address to avoid multi-hit */
207
208         unsigned long phys = page_to_phys(pfn_to_page(pfn));
209         unsigned long virt = TLBTEMP_BASE_1 + (address & DCACHE_ALIAS_MASK);
210
211         preempt_disable();
212         __flush_invalidate_dcache_page_alias(virt, phys);
213         __invalidate_icache_page_alias(virt, phys);
214         preempt_enable();
215 }
216
217 #endif
218
219 void
220 update_mmu_cache(struct vm_area_struct * vma, unsigned long addr, pte_t *ptep)
221 {
222         unsigned long pfn = pte_pfn(*ptep);
223         struct page *page;
224
225         if (!pfn_valid(pfn))
226                 return;
227
228         page = pfn_to_page(pfn);
229
230         /* Invalidate old entry in TLBs */
231
232         flush_tlb_page(vma, addr);
233
234 #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
235
236         if (!PageReserved(page) && test_bit(PG_arch_1, &page->flags)) {
237                 unsigned long phys = page_to_phys(page);
238                 unsigned long tmp;
239
240                 preempt_disable();
241                 tmp = TLBTEMP_BASE_1 + (phys & DCACHE_ALIAS_MASK);
242                 __flush_invalidate_dcache_page_alias(tmp, phys);
243                 tmp = TLBTEMP_BASE_1 + (addr & DCACHE_ALIAS_MASK);
244                 __flush_invalidate_dcache_page_alias(tmp, phys);
245                 __invalidate_icache_page_alias(tmp, phys);
246                 preempt_enable();
247
248                 clear_bit(PG_arch_1, &page->flags);
249         }
250 #else
251         if (!PageReserved(page) && !test_bit(PG_arch_1, &page->flags)
252             && (vma->vm_flags & VM_EXEC) != 0) {
253                 unsigned long paddr = (unsigned long)kmap_atomic(page);
254                 __flush_dcache_page(paddr);
255                 __invalidate_icache_page(paddr);
256                 set_bit(PG_arch_1, &page->flags);
257                 kunmap_atomic((void *)paddr);
258         }
259 #endif
260 }
261
262 /*
263  * access_process_vm() has called get_user_pages(), which has done a
264  * flush_dcache_page() on the page.
265  */
266
267 #if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
268
269 void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
270                 unsigned long vaddr, void *dst, const void *src,
271                 unsigned long len)
272 {
273         unsigned long phys = page_to_phys(page);
274         unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
275
276         /* Flush and invalidate user page if aliased. */
277
278         if (alias) {
279                 unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
280                 preempt_disable();
281                 __flush_invalidate_dcache_page_alias(t, phys);
282                 preempt_enable();
283         }
284
285         /* Copy data */
286         
287         memcpy(dst, src, len);
288
289         /*
290          * Flush and invalidate kernel page if aliased and synchronize 
291          * data and instruction caches for executable pages. 
292          */
293
294         if (alias) {
295                 unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
296
297                 preempt_disable();
298                 __flush_invalidate_dcache_range((unsigned long) dst, len);
299                 if ((vma->vm_flags & VM_EXEC) != 0)
300                         __invalidate_icache_page_alias(t, phys);
301                 preempt_enable();
302
303         } else if ((vma->vm_flags & VM_EXEC) != 0) {
304                 __flush_dcache_range((unsigned long)dst,len);
305                 __invalidate_icache_range((unsigned long) dst, len);
306         }
307 }
308
309 extern void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
310                 unsigned long vaddr, void *dst, const void *src,
311                 unsigned long len)
312 {
313         unsigned long phys = page_to_phys(page);
314         unsigned long alias = !(DCACHE_ALIAS_EQ(vaddr, phys));
315
316         /*
317          * Flush user page if aliased. 
318          * (Note: a simply flush would be sufficient) 
319          */
320
321         if (alias) {
322                 unsigned long t = TLBTEMP_BASE_1 + (vaddr & DCACHE_ALIAS_MASK);
323                 preempt_disable();
324                 __flush_invalidate_dcache_page_alias(t, phys);
325                 preempt_enable();
326         }
327
328         memcpy(dst, src, len);
329 }
330
331 #endif