blob: 6f92904a81e9170e0e49678a7f2ac3150a37c88f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/pgtable.h>
22#include <asm/tlbflush.h>
23
Russell Kingbb30f362008-09-06 20:04:59 +010024static unsigned long shared_pte_mask = L_PTE_MT_BUFFERABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * We take the easy way out of this problem - we make the
28 * PTE uncacheable. However, we leave the write buffer on.
Hugh Dickins69b04752005-10-29 18:16:36 -070029 *
30 * Note that the pte lock held when calling update_mmu_cache must also
31 * guard the pte (somewhere else in the same mm) that we modify here.
32 * Therefore those configurations which might call adjust_pte (those
33 * without CONFIG_CPU_CACHE_VIPT) cannot support split page_table_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35static int adjust_pte(struct vm_area_struct *vma, unsigned long address)
36{
37 pgd_t *pgd;
38 pmd_t *pmd;
39 pte_t *pte, entry;
Russell King53cdb272008-07-27 10:35:54 +010040 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 pgd = pgd_offset(vma->vm_mm, address);
43 if (pgd_none(*pgd))
44 goto no_pgd;
45 if (pgd_bad(*pgd))
46 goto bad_pgd;
47
48 pmd = pmd_offset(pgd, address);
49 if (pmd_none(*pmd))
50 goto no_pmd;
51 if (pmd_bad(*pmd))
52 goto bad_pmd;
53
54 pte = pte_offset_map(pmd, address);
55 entry = *pte;
56
57 /*
Russell King53cdb272008-07-27 10:35:54 +010058 * If this page is present, it's actually being shared.
59 */
60 ret = pte_present(entry);
61
62 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * If this page isn't present, or is already setup to
64 * fault (ie, is old), we can safely ignore any issues.
65 */
Russell Kingbb30f362008-09-06 20:04:59 +010066 if (ret && (pte_val(entry) & L_PTE_MT_MASK) != shared_pte_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 flush_cache_page(vma, address, pte_pfn(entry));
Russell Kingbb30f362008-09-06 20:04:59 +010068 pte_val(entry) &= ~L_PTE_MT_MASK;
69 pte_val(entry) |= shared_pte_mask;
Russell Kingad1ae2f2006-12-13 14:34:43 +000070 set_pte_at(vma->vm_mm, address, pte, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 flush_tlb_page(vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73 pte_unmap(pte);
74 return ret;
75
76bad_pgd:
77 pgd_ERROR(*pgd);
78 pgd_clear(pgd);
79no_pgd:
80 return 0;
81
82bad_pmd:
83 pmd_ERROR(*pmd);
84 pmd_clear(pmd);
85no_pmd:
86 return 0;
87}
88
89static void
Russell King8830f042005-06-20 09:51:03 +010090make_coherent(struct address_space *mapping, struct vm_area_struct *vma, unsigned long addr, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 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
Russell King8830f042005-06-20 09:51:03 +0100124 flush_cache_page(vma, addr, pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
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 *
Hugh Dickins69b04752005-10-29 18:16:36 -0700138 * Note that the pte lock will be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
140void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
141{
142 unsigned long pfn = pte_pfn(pte);
Russell King8830f042005-06-20 09:51:03 +0100143 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct page *page;
145
146 if (!pfn_valid(pfn))
147 return;
Russell King8830f042005-06-20 09:51:03 +0100148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 page = pfn_to_page(pfn);
Russell King8830f042005-06-20 09:51:03 +0100150 mapping = page_mapping(page);
151 if (mapping) {
Catalin Marinas826cbda2008-06-13 10:28:36 +0100152#ifndef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
154
Russell King8830f042005-06-20 09:51:03 +0100155 if (dirty)
156 __flush_dcache_page(mapping, page);
Catalin Marinas826cbda2008-06-13 10:28:36 +0100157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (cache_is_vivt())
Russell King8830f042005-06-20 09:51:03 +0100160 make_coherent(mapping, vma, addr, pfn);
Catalin Marinas826cbda2008-06-13 10:28:36 +0100161 else if (vma->vm_flags & VM_EXEC)
162 __flush_icache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
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 */
172static 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
188void __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|
Russell Kingbb30f362008-09-06 20:04:59 +0100201 L_PTE_MT_BUFFERABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
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);
Russell Kingbb30f362008-09-06 20:04:59 +0100222 shared_pte_mask = L_PTE_MT_UNCACHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else {
224 printk("ok\n");
225 }
226}