MIPS: Make set_pte() SMP safe.
[pandora-kernel.git] / arch / mips / include / asm / pgtable.h
1 /*
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
4  * for more details.
5  *
6  * Copyright (C) 2003 Ralf Baechle
7  */
8 #ifndef _ASM_PGTABLE_H
9 #define _ASM_PGTABLE_H
10
11 #ifdef CONFIG_32BIT
12 #include <asm/pgtable-32.h>
13 #endif
14 #ifdef CONFIG_64BIT
15 #include <asm/pgtable-64.h>
16 #endif
17
18 #include <asm/io.h>
19 #include <asm/pgtable-bits.h>
20
21 struct mm_struct;
22 struct vm_area_struct;
23
24 #define PAGE_NONE       __pgprot(_PAGE_PRESENT | _CACHE_CACHABLE_NONCOHERENT)
25 #define PAGE_SHARED     __pgprot(_PAGE_PRESENT | _PAGE_WRITE | (kernel_uses_smartmips_rixi ? 0 : _PAGE_READ) | \
26                                  _page_cachable_default)
27 #define PAGE_COPY       __pgprot(_PAGE_PRESENT | (kernel_uses_smartmips_rixi ? 0 : _PAGE_READ) | \
28                                  (kernel_uses_smartmips_rixi ?  _PAGE_NO_EXEC : 0) | _page_cachable_default)
29 #define PAGE_READONLY   __pgprot(_PAGE_PRESENT | (kernel_uses_smartmips_rixi ? 0 : _PAGE_READ) | \
30                                  _page_cachable_default)
31 #define PAGE_KERNEL     __pgprot(_PAGE_PRESENT | __READABLE | __WRITEABLE | \
32                                  _PAGE_GLOBAL | _page_cachable_default)
33 #define PAGE_USERIO     __pgprot(_PAGE_PRESENT | (kernel_uses_smartmips_rixi ? 0 : _PAGE_READ) | _PAGE_WRITE | \
34                                  _page_cachable_default)
35 #define PAGE_KERNEL_UNCACHED __pgprot(_PAGE_PRESENT | __READABLE | \
36                         __WRITEABLE | _PAGE_GLOBAL | _CACHE_UNCACHED)
37
38 /*
39  * If _PAGE_NO_EXEC is not defined, we can't do page protection for
40  * execute, and consider it to be the same as read. Also, write
41  * permissions imply read permissions. This is the closest we can get
42  * by reasonable means..
43  */
44
45 /*
46  * Dummy values to fill the table in mmap.c
47  * The real values will be generated at runtime
48  */
49 #define __P000 __pgprot(0)
50 #define __P001 __pgprot(0)
51 #define __P010 __pgprot(0)
52 #define __P011 __pgprot(0)
53 #define __P100 __pgprot(0)
54 #define __P101 __pgprot(0)
55 #define __P110 __pgprot(0)
56 #define __P111 __pgprot(0)
57
58 #define __S000 __pgprot(0)
59 #define __S001 __pgprot(0)
60 #define __S010 __pgprot(0)
61 #define __S011 __pgprot(0)
62 #define __S100 __pgprot(0)
63 #define __S101 __pgprot(0)
64 #define __S110 __pgprot(0)
65 #define __S111 __pgprot(0)
66
67 extern unsigned long _page_cachable_default;
68
69 /*
70  * ZERO_PAGE is a global shared page that is always zero; used
71  * for zero-mapped memory areas etc..
72  */
73
74 extern unsigned long empty_zero_page;
75 extern unsigned long zero_page_mask;
76
77 #define ZERO_PAGE(vaddr) \
78         (virt_to_page((void *)(empty_zero_page + (((unsigned long)(vaddr)) & zero_page_mask))))
79
80 #define is_zero_pfn is_zero_pfn
81 static inline int is_zero_pfn(unsigned long pfn)
82 {
83         extern unsigned long zero_pfn;
84         unsigned long offset_from_zero_pfn = pfn - zero_pfn;
85         return offset_from_zero_pfn <= (zero_page_mask >> PAGE_SHIFT);
86 }
87
88 #define my_zero_pfn(addr)       page_to_pfn(ZERO_PAGE(addr))
89
90 extern void paging_init(void);
91
92 /*
93  * Conversion functions: convert a page and protection to a page entry,
94  * and a page entry and page directory to the page they refer to.
95  */
96 #define pmd_phys(pmd)           virt_to_phys((void *)pmd_val(pmd))
97 #define pmd_page(pmd)           (pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
98 #define pmd_page_vaddr(pmd)     pmd_val(pmd)
99
100 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
101
102 #define pte_none(pte)           (!(((pte).pte_low | (pte).pte_high) & ~_PAGE_GLOBAL))
103 #define pte_present(pte)        ((pte).pte_low & _PAGE_PRESENT)
104
105 static inline void set_pte(pte_t *ptep, pte_t pte)
106 {
107         ptep->pte_high = pte.pte_high;
108         smp_wmb();
109         ptep->pte_low = pte.pte_low;
110         //printk("pte_high %x pte_low %x\n", ptep->pte_high, ptep->pte_low);
111
112         if (pte.pte_low & _PAGE_GLOBAL) {
113                 pte_t *buddy = ptep_buddy(ptep);
114                 /*
115                  * Make sure the buddy is global too (if it's !none,
116                  * it better already be global)
117                  */
118                 if (pte_none(*buddy)) {
119                         buddy->pte_low  |= _PAGE_GLOBAL;
120                         buddy->pte_high |= _PAGE_GLOBAL;
121                 }
122         }
123 }
124 #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
125
126 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
127 {
128         pte_t null = __pte(0);
129
130         /* Preserve global status for the pair */
131         if (ptep_buddy(ptep)->pte_low & _PAGE_GLOBAL)
132                 null.pte_low = null.pte_high = _PAGE_GLOBAL;
133
134         set_pte_at(mm, addr, ptep, null);
135 }
136 #else
137
138 #define pte_none(pte)           (!(pte_val(pte) & ~_PAGE_GLOBAL))
139 #define pte_present(pte)        (pte_val(pte) & _PAGE_PRESENT)
140
141 /*
142  * Certain architectures need to do special things when pte's
143  * within a page table are directly modified.  Thus, the following
144  * hook is made available.
145  */
146 static inline void set_pte(pte_t *ptep, pte_t pteval)
147 {
148         *ptep = pteval;
149 #if !defined(CONFIG_CPU_R3000) && !defined(CONFIG_CPU_TX39XX)
150         if (pte_val(pteval) & _PAGE_GLOBAL) {
151                 pte_t *buddy = ptep_buddy(ptep);
152                 /*
153                  * Make sure the buddy is global too (if it's !none,
154                  * it better already be global)
155                  */
156 #ifdef CONFIG_SMP
157                 /*
158                  * For SMP, multiple CPUs can race, so we need to do
159                  * this atomically.
160                  */
161 #ifdef CONFIG_64BIT
162 #define LL_INSN "lld"
163 #define SC_INSN "scd"
164 #else /* CONFIG_32BIT */
165 #define LL_INSN "ll"
166 #define SC_INSN "sc"
167 #endif
168                 unsigned long page_global = _PAGE_GLOBAL;
169                 unsigned long tmp;
170
171                 __asm__ __volatile__ (
172                         "       .set    push\n"
173                         "       .set    noreorder\n"
174                         "1:     " LL_INSN "     %[tmp], %[buddy]\n"
175                         "       bnez    %[tmp], 2f\n"
176                         "        or     %[tmp], %[tmp], %[global]\n"
177                         "       " SC_INSN "     %[tmp], %[buddy]\n"
178                         "       beqz    %[tmp], 1b\n"
179                         "        nop\n"
180                         "2:\n"
181                         "       .set pop"
182                         : [buddy] "+m" (buddy->pte),
183                           [tmp] "=&r" (tmp)
184                         : [global] "r" (page_global));
185 #else /* !CONFIG_SMP */
186                 if (pte_none(*buddy))
187                         pte_val(*buddy) = pte_val(*buddy) | _PAGE_GLOBAL;
188 #endif /* CONFIG_SMP */
189         }
190 #endif
191 }
192 #define set_pte_at(mm, addr, ptep, pteval) set_pte(ptep, pteval)
193
194 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
195 {
196 #if !defined(CONFIG_CPU_R3000) && !defined(CONFIG_CPU_TX39XX)
197         /* Preserve global status for the pair */
198         if (pte_val(*ptep_buddy(ptep)) & _PAGE_GLOBAL)
199                 set_pte_at(mm, addr, ptep, __pte(_PAGE_GLOBAL));
200         else
201 #endif
202                 set_pte_at(mm, addr, ptep, __pte(0));
203 }
204 #endif
205
206 /*
207  * (pmds are folded into puds so this doesn't get actually called,
208  * but the define is needed for a generic inline function.)
209  */
210 #define set_pmd(pmdptr, pmdval) do { *(pmdptr) = (pmdval); } while(0)
211
212 #ifndef __PAGETABLE_PMD_FOLDED
213 /*
214  * (puds are folded into pgds so this doesn't get actually called,
215  * but the define is needed for a generic inline function.)
216  */
217 #define set_pud(pudptr, pudval) do { *(pudptr) = (pudval); } while(0)
218 #endif
219
220 #define PGD_T_LOG2      (__builtin_ffs(sizeof(pgd_t)) - 1)
221 #define PMD_T_LOG2      (__builtin_ffs(sizeof(pmd_t)) - 1)
222 #define PTE_T_LOG2      (__builtin_ffs(sizeof(pte_t)) - 1)
223
224 /*
225  * We used to declare this array with size but gcc 3.3 and older are not able
226  * to find that this expression is a constant, so the size is dropped.
227  */
228 extern pgd_t swapper_pg_dir[];
229
230 /*
231  * The following only work if pte_present() is true.
232  * Undefined behaviour if not..
233  */
234 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
235 static inline int pte_write(pte_t pte)  { return pte.pte_low & _PAGE_WRITE; }
236 static inline int pte_dirty(pte_t pte)  { return pte.pte_low & _PAGE_MODIFIED; }
237 static inline int pte_young(pte_t pte)  { return pte.pte_low & _PAGE_ACCESSED; }
238 static inline int pte_file(pte_t pte)   { return pte.pte_low & _PAGE_FILE; }
239
240 static inline pte_t pte_wrprotect(pte_t pte)
241 {
242         pte.pte_low  &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
243         pte.pte_high &= ~_PAGE_SILENT_WRITE;
244         return pte;
245 }
246
247 static inline pte_t pte_mkclean(pte_t pte)
248 {
249         pte.pte_low  &= ~(_PAGE_MODIFIED | _PAGE_SILENT_WRITE);
250         pte.pte_high &= ~_PAGE_SILENT_WRITE;
251         return pte;
252 }
253
254 static inline pte_t pte_mkold(pte_t pte)
255 {
256         pte.pte_low  &= ~(_PAGE_ACCESSED | _PAGE_SILENT_READ);
257         pte.pte_high &= ~_PAGE_SILENT_READ;
258         return pte;
259 }
260
261 static inline pte_t pte_mkwrite(pte_t pte)
262 {
263         pte.pte_low |= _PAGE_WRITE;
264         if (pte.pte_low & _PAGE_MODIFIED) {
265                 pte.pte_low  |= _PAGE_SILENT_WRITE;
266                 pte.pte_high |= _PAGE_SILENT_WRITE;
267         }
268         return pte;
269 }
270
271 static inline pte_t pte_mkdirty(pte_t pte)
272 {
273         pte.pte_low |= _PAGE_MODIFIED;
274         if (pte.pte_low & _PAGE_WRITE) {
275                 pte.pte_low  |= _PAGE_SILENT_WRITE;
276                 pte.pte_high |= _PAGE_SILENT_WRITE;
277         }
278         return pte;
279 }
280
281 static inline pte_t pte_mkyoung(pte_t pte)
282 {
283         pte.pte_low |= _PAGE_ACCESSED;
284         if (pte.pte_low & _PAGE_READ) {
285                 pte.pte_low  |= _PAGE_SILENT_READ;
286                 pte.pte_high |= _PAGE_SILENT_READ;
287         }
288         return pte;
289 }
290 #else
291 static inline int pte_write(pte_t pte)  { return pte_val(pte) & _PAGE_WRITE; }
292 static inline int pte_dirty(pte_t pte)  { return pte_val(pte) & _PAGE_MODIFIED; }
293 static inline int pte_young(pte_t pte)  { return pte_val(pte) & _PAGE_ACCESSED; }
294 static inline int pte_file(pte_t pte)   { return pte_val(pte) & _PAGE_FILE; }
295
296 static inline pte_t pte_wrprotect(pte_t pte)
297 {
298         pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_SILENT_WRITE);
299         return pte;
300 }
301
302 static inline pte_t pte_mkclean(pte_t pte)
303 {
304         pte_val(pte) &= ~(_PAGE_MODIFIED|_PAGE_SILENT_WRITE);
305         return pte;
306 }
307
308 static inline pte_t pte_mkold(pte_t pte)
309 {
310         pte_val(pte) &= ~(_PAGE_ACCESSED|_PAGE_SILENT_READ);
311         return pte;
312 }
313
314 static inline pte_t pte_mkwrite(pte_t pte)
315 {
316         pte_val(pte) |= _PAGE_WRITE;
317         if (pte_val(pte) & _PAGE_MODIFIED)
318                 pte_val(pte) |= _PAGE_SILENT_WRITE;
319         return pte;
320 }
321
322 static inline pte_t pte_mkdirty(pte_t pte)
323 {
324         pte_val(pte) |= _PAGE_MODIFIED;
325         if (pte_val(pte) & _PAGE_WRITE)
326                 pte_val(pte) |= _PAGE_SILENT_WRITE;
327         return pte;
328 }
329
330 static inline pte_t pte_mkyoung(pte_t pte)
331 {
332         pte_val(pte) |= _PAGE_ACCESSED;
333         if (kernel_uses_smartmips_rixi) {
334                 if (!(pte_val(pte) & _PAGE_NO_READ))
335                         pte_val(pte) |= _PAGE_SILENT_READ;
336         } else {
337                 if (pte_val(pte) & _PAGE_READ)
338                         pte_val(pte) |= _PAGE_SILENT_READ;
339         }
340         return pte;
341 }
342
343 #ifdef _PAGE_HUGE
344 static inline int pte_huge(pte_t pte)   { return pte_val(pte) & _PAGE_HUGE; }
345
346 static inline pte_t pte_mkhuge(pte_t pte)
347 {
348         pte_val(pte) |= _PAGE_HUGE;
349         return pte;
350 }
351 #endif /* _PAGE_HUGE */
352 #endif
353 static inline int pte_special(pte_t pte)        { return 0; }
354 static inline pte_t pte_mkspecial(pte_t pte)    { return pte; }
355
356 /*
357  * Macro to make mark a page protection value as "uncacheable".  Note
358  * that "protection" is really a misnomer here as the protection value
359  * contains the memory attribute bits, dirty bits, and various other
360  * bits as well.
361  */
362 #define pgprot_noncached pgprot_noncached
363
364 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
365 {
366         unsigned long prot = pgprot_val(_prot);
367
368         prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
369
370         return __pgprot(prot);
371 }
372
373 /*
374  * Conversion functions: convert a page and protection to a page entry,
375  * and a page entry and page directory to the page they refer to.
376  */
377 #define mk_pte(page, pgprot)    pfn_pte(page_to_pfn(page), (pgprot))
378
379 #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
380 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
381 {
382         pte.pte_low  &= _PAGE_CHG_MASK;
383         pte.pte_high &= ~0x3f;
384         pte.pte_low  |= pgprot_val(newprot);
385         pte.pte_high |= pgprot_val(newprot) & 0x3f;
386         return pte;
387 }
388 #else
389 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
390 {
391         return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
392 }
393 #endif
394
395
396 extern void __update_tlb(struct vm_area_struct *vma, unsigned long address,
397         pte_t pte);
398 extern void __update_cache(struct vm_area_struct *vma, unsigned long address,
399         pte_t pte);
400
401 static inline void update_mmu_cache(struct vm_area_struct *vma,
402         unsigned long address, pte_t *ptep)
403 {
404         pte_t pte = *ptep;
405         __update_tlb(vma, address, pte);
406         __update_cache(vma, address, pte);
407 }
408
409 #define kern_addr_valid(addr)   (1)
410
411 #ifdef CONFIG_64BIT_PHYS_ADDR
412 extern int remap_pfn_range(struct vm_area_struct *vma, unsigned long from, unsigned long pfn, unsigned long size, pgprot_t prot);
413
414 static inline int io_remap_pfn_range(struct vm_area_struct *vma,
415                 unsigned long vaddr,
416                 unsigned long pfn,
417                 unsigned long size,
418                 pgprot_t prot)
419 {
420         phys_t phys_addr_high = fixup_bigphys_addr(pfn << PAGE_SHIFT, size);
421         return remap_pfn_range(vma, vaddr, phys_addr_high >> PAGE_SHIFT, size, prot);
422 }
423 #else
424 #define io_remap_pfn_range(vma, vaddr, pfn, size, prot)         \
425                 remap_pfn_range(vma, vaddr, pfn, size, prot)
426 #endif
427
428 #include <asm-generic/pgtable.h>
429
430 /*
431  * uncached accelerated TLB map for video memory access
432  */
433 #ifdef CONFIG_CPU_SUPPORTS_UNCACHED_ACCELERATED
434 #define __HAVE_PHYS_MEM_ACCESS_PROT
435
436 struct file;
437 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
438                 unsigned long size, pgprot_t vma_prot);
439 int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
440                 unsigned long size, pgprot_t *vma_prot);
441 #endif
442
443 /*
444  * We provide our own get_unmapped area to cope with the virtual aliasing
445  * constraints placed on us by the cache architecture.
446  */
447 #define HAVE_ARCH_UNMAPPED_AREA
448 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
449
450 /*
451  * No page table caches to initialise
452  */
453 #define pgtable_cache_init()    do { } while (0)
454
455 #endif /* _ASM_PGTABLE_H */