[ARM] cachetype: move definitions to separate header
[pandora-kernel.git] / arch / arm / mm / fault-armv.c
1 /*
2  *  linux/arch/arm/mm/fault-armv.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Modifications for ARM processor (c) 1995-2002 Russell King
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/bitops.h>
16 #include <linux/vmalloc.h>
17 #include <linux/init.h>
18 #include <linux/pagemap.h>
19
20 #include <asm/cacheflush.h>
21 #include <asm/cachetype.h>
22 #include <asm/pgtable.h>
23 #include <asm/tlbflush.h>
24
25 static unsigned long shared_pte_mask = L_PTE_CACHEABLE;
26
27 /*
28  * We take the easy way out of this problem - we make the
29  * PTE uncacheable.  However, we leave the write buffer on.
30  *
31  * Note that the pte lock held when calling update_mmu_cache must also
32  * guard the pte (somewhere else in the same mm) that we modify here.
33  * Therefore those configurations which might call adjust_pte (those
34  * without CONFIG_CPU_CACHE_VIPT) cannot support split page_table_lock.
35  */
36 static int adjust_pte(struct vm_area_struct *vma, unsigned long address)
37 {
38         pgd_t *pgd;
39         pmd_t *pmd;
40         pte_t *pte, entry;
41         int ret;
42
43         pgd = pgd_offset(vma->vm_mm, address);
44         if (pgd_none(*pgd))
45                 goto no_pgd;
46         if (pgd_bad(*pgd))
47                 goto bad_pgd;
48
49         pmd = pmd_offset(pgd, address);
50         if (pmd_none(*pmd))
51                 goto no_pmd;
52         if (pmd_bad(*pmd))
53                 goto bad_pmd;
54
55         pte = pte_offset_map(pmd, address);
56         entry = *pte;
57
58         /*
59          * If this page is present, it's actually being shared.
60          */
61         ret = pte_present(entry);
62
63         /*
64          * If this page isn't present, or is already setup to
65          * fault (ie, is old), we can safely ignore any issues.
66          */
67         if (ret && pte_val(entry) & shared_pte_mask) {
68                 flush_cache_page(vma, address, pte_pfn(entry));
69                 pte_val(entry) &= ~shared_pte_mask;
70                 set_pte_at(vma->vm_mm, address, pte, entry);
71                 flush_tlb_page(vma, address);
72         }
73         pte_unmap(pte);
74         return ret;
75
76 bad_pgd:
77         pgd_ERROR(*pgd);
78         pgd_clear(pgd);
79 no_pgd:
80         return 0;
81
82 bad_pmd:
83         pmd_ERROR(*pmd);
84         pmd_clear(pmd);
85 no_pmd:
86         return 0;
87 }
88
89 static void
90 make_coherent(struct address_space *mapping, struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
91 {
92         struct mm_struct *mm = vma->vm_mm;
93         struct vm_area_struct *mpnt;
94         struct prio_tree_iter iter;
95         unsigned long offset;
96         pgoff_t pgoff;
97         int aliases = 0;
98
99         pgoff = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT);
100
101         /*
102          * If we have any shared mappings that are in the same mm
103          * space, then we need to handle them specially to maintain
104          * cache coherency.
105          */
106         flush_dcache_mmap_lock(mapping);
107         vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
108                 /*
109                  * If this VMA is not in our MM, we can ignore it.
110                  * Note that we intentionally mask out the VMA
111                  * that we are fixing up.
112                  */
113                 if (mpnt->vm_mm != mm || mpnt == vma)
114                         continue;
115                 if (!(mpnt->vm_flags & VM_MAYSHARE))
116                         continue;
117                 offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
118                 aliases += adjust_pte(mpnt, mpnt->vm_start + offset);
119         }
120         flush_dcache_mmap_unlock(mapping);
121         if (aliases)
122                 adjust_pte(vma, addr);
123         else
124                 flush_cache_page(vma, addr, pfn);
125 }
126
127 /*
128  * Take care of architecture specific things when placing a new PTE into
129  * a page table, or changing an existing PTE.  Basically, there are two
130  * things that we need to take care of:
131  *
132  *  1. If PG_dcache_dirty is set for the page, we need to ensure
133  *     that any cache entries for the kernels virtual memory
134  *     range are written back to the page.
135  *  2. If we have multiple shared mappings of the same space in
136  *     an object, we need to deal with the cache aliasing issues.
137  *
138  * Note that the pte lock will be held.
139  */
140 void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
141 {
142         unsigned long pfn = pte_pfn(pte);
143         struct address_space *mapping;
144         struct page *page;
145
146         if (!pfn_valid(pfn))
147                 return;
148
149         page = pfn_to_page(pfn);
150         mapping = page_mapping(page);
151         if (mapping) {
152 #ifndef CONFIG_SMP
153                 int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
154
155                 if (dirty)
156                         __flush_dcache_page(mapping, page);
157 #endif
158
159                 if (cache_is_vivt())
160                         make_coherent(mapping, vma, addr, pfn);
161                 else if (vma->vm_flags & VM_EXEC)
162                         __flush_icache_all();
163         }
164 }
165
166 /*
167  * Check whether the write buffer has physical address aliasing
168  * issues.  If it has, we need to avoid them for the case where
169  * we have several shared mappings of the same object in user
170  * space.
171  */
172 static int __init check_writebuffer(unsigned long *p1, unsigned long *p2)
173 {
174         register unsigned long zero = 0, one = 1, val;
175
176         local_irq_disable();
177         mb();
178         *p1 = one;
179         mb();
180         *p2 = zero;
181         mb();
182         val = *p1;
183         mb();
184         local_irq_enable();
185         return val != zero;
186 }
187
188 void __init check_writebuffer_bugs(void)
189 {
190         struct page *page;
191         const char *reason;
192         unsigned long v = 1;
193
194         printk(KERN_INFO "CPU: Testing write buffer coherency: ");
195
196         page = alloc_page(GFP_KERNEL);
197         if (page) {
198                 unsigned long *p1, *p2;
199                 pgprot_t prot = __pgprot(L_PTE_PRESENT|L_PTE_YOUNG|
200                                          L_PTE_DIRTY|L_PTE_WRITE|
201                                          L_PTE_BUFFERABLE);
202
203                 p1 = vmap(&page, 1, VM_IOREMAP, prot);
204                 p2 = vmap(&page, 1, VM_IOREMAP, prot);
205
206                 if (p1 && p2) {
207                         v = check_writebuffer(p1, p2);
208                         reason = "enabling work-around";
209                 } else {
210                         reason = "unable to map memory\n";
211                 }
212
213                 vunmap(p1);
214                 vunmap(p2);
215                 put_page(page);
216         } else {
217                 reason = "unable to grab page\n";
218         }
219
220         if (v) {
221                 printk("failed, %s\n", reason);
222                 shared_pte_mask |= L_PTE_BUFFERABLE;
223         } else {
224                 printk("ok\n");
225         }
226 }