Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / arch / sh / mm / pg-sh4.c
1 /*
2  * arch/sh/mm/pg-sh4.c
3  *
4  * Copyright (C) 1999, 2000, 2002  Niibe Yutaka
5  * Copyright (C) 2002 - 2005  Paul Mundt
6  *
7  * Released under the terms of the GNU GPL v2.0.
8  */
9 #include <linux/init.h>
10 #include <linux/mman.h>
11 #include <linux/mm.h>
12 #include <linux/threads.h>
13 #include <asm/addrspace.h>
14 #include <asm/page.h>
15 #include <asm/pgtable.h>
16 #include <asm/processor.h>
17 #include <asm/cache.h>
18 #include <asm/io.h>
19 #include <asm/uaccess.h>
20 #include <asm/pgalloc.h>
21 #include <asm/mmu_context.h>
22 #include <asm/cacheflush.h>
23
24 extern struct semaphore p3map_sem[];
25
26 #define CACHE_ALIAS (cpu_data->dcache.alias_mask)
27
28 /*
29  * clear_user_page
30  * @to: P1 address
31  * @address: U0 address to be mapped
32  * @page: page (virt_to_page(to))
33  */
34 void clear_user_page(void *to, unsigned long address, struct page *page)
35 {
36         __set_bit(PG_mapped, &page->flags);
37         if (((address ^ (unsigned long)to) & CACHE_ALIAS) == 0)
38                 clear_page(to);
39         else {
40                 pgprot_t pgprot = __pgprot(_PAGE_PRESENT |
41                                            _PAGE_RW | _PAGE_CACHABLE |
42                                            _PAGE_DIRTY | _PAGE_ACCESSED |
43                                            _PAGE_HW_SHARED | _PAGE_FLAGS_HARD);
44                 unsigned long phys_addr = PHYSADDR(to);
45                 unsigned long p3_addr = P3SEG + (address & CACHE_ALIAS);
46                 pgd_t *pgd = pgd_offset_k(p3_addr);
47                 pud_t *pud = pud_offset(pgd, p3_addr);
48                 pmd_t *pmd = pmd_offset(pud, p3_addr);
49                 pte_t *pte = pte_offset_kernel(pmd, p3_addr);
50                 pte_t entry;
51                 unsigned long flags;
52
53                 entry = pfn_pte(phys_addr >> PAGE_SHIFT, pgprot);
54                 down(&p3map_sem[(address & CACHE_ALIAS)>>12]);
55                 set_pte(pte, entry);
56                 local_irq_save(flags);
57                 __flush_tlb_page(get_asid(), p3_addr);
58                 local_irq_restore(flags);
59                 update_mmu_cache(NULL, p3_addr, entry);
60                 __clear_user_page((void *)p3_addr, to);
61                 pte_clear(&init_mm, p3_addr, pte);
62                 up(&p3map_sem[(address & CACHE_ALIAS)>>12]);
63         }
64 }
65
66 /*
67  * copy_user_page
68  * @to: P1 address
69  * @from: P1 address
70  * @address: U0 address to be mapped
71  * @page: page (virt_to_page(to))
72  */
73 void copy_user_page(void *to, void *from, unsigned long address,
74                     struct page *page)
75 {
76         __set_bit(PG_mapped, &page->flags);
77         if (((address ^ (unsigned long)to) & CACHE_ALIAS) == 0)
78                 copy_page(to, from);
79         else {
80                 pgprot_t pgprot = __pgprot(_PAGE_PRESENT |
81                                            _PAGE_RW | _PAGE_CACHABLE |
82                                            _PAGE_DIRTY | _PAGE_ACCESSED |
83                                            _PAGE_HW_SHARED | _PAGE_FLAGS_HARD);
84                 unsigned long phys_addr = PHYSADDR(to);
85                 unsigned long p3_addr = P3SEG + (address & CACHE_ALIAS);
86                 pgd_t *pgd = pgd_offset_k(p3_addr);
87                 pud_t *pud = pud_offset(pgd, p3_addr);
88                 pmd_t *pmd = pmd_offset(pud, p3_addr);
89                 pte_t *pte = pte_offset_kernel(pmd, p3_addr);
90                 pte_t entry;
91                 unsigned long flags;
92
93                 entry = pfn_pte(phys_addr >> PAGE_SHIFT, pgprot);
94                 down(&p3map_sem[(address & CACHE_ALIAS)>>12]);
95                 set_pte(pte, entry);
96                 local_irq_save(flags);
97                 __flush_tlb_page(get_asid(), p3_addr);
98                 local_irq_restore(flags);
99                 update_mmu_cache(NULL, p3_addr, entry);
100                 __copy_user_page((void *)p3_addr, from, to);
101                 pte_clear(&init_mm, p3_addr, pte);
102                 up(&p3map_sem[(address & CACHE_ALIAS)>>12]);
103         }
104 }
105
106 /*
107  * For SH-4, we have our own implementation for ptep_get_and_clear
108  */
109 inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
110 {
111         pte_t pte = *ptep;
112
113         pte_clear(mm, addr, ptep);
114         if (!pte_not_present(pte)) {
115                 unsigned long pfn = pte_pfn(pte);
116                 if (pfn_valid(pfn)) {
117                         struct page *page = pfn_to_page(pfn);
118                         struct address_space *mapping = page_mapping(page);
119                         if (!mapping || !mapping_writably_mapped(mapping))
120                                 __clear_bit(PG_mapped, &page->flags);
121                 }
122         }
123         return pte;
124 }
125